| 1 | /*
|
|---|
| 2 | * Copyright (c) 2008 Jiri Svoboda
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup trace
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <stdio.h>
|
|---|
| 36 | #include <stdlib.h>
|
|---|
| 37 | #include <unistd.h>
|
|---|
| 38 | #include <fibril.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <udebug.h>
|
|---|
| 41 | #include <async.h>
|
|---|
| 42 | #include <task.h>
|
|---|
| 43 | #include <mem.h>
|
|---|
| 44 | #include <str.h>
|
|---|
| 45 | #include <bool.h>
|
|---|
| 46 | #include <loader/loader.h>
|
|---|
| 47 | #include <io/console.h>
|
|---|
| 48 | #include <io/keycode.h>
|
|---|
| 49 | #include <fibril_synch.h>
|
|---|
| 50 | #include <sys/types.h>
|
|---|
| 51 | #include <sys/typefmt.h>
|
|---|
| 52 |
|
|---|
| 53 | #include <libc.h>
|
|---|
| 54 |
|
|---|
| 55 | /* Temporary: service and method names */
|
|---|
| 56 | #include "proto.h"
|
|---|
| 57 | #include <ipc/services.h>
|
|---|
| 58 | #include "../../srv/vfs/vfs.h"
|
|---|
| 59 | #include <ipc/console.h>
|
|---|
| 60 |
|
|---|
| 61 | #include "syscalls.h"
|
|---|
| 62 | #include "ipcp.h"
|
|---|
| 63 | #include "errors.h"
|
|---|
| 64 | #include "trace.h"
|
|---|
| 65 |
|
|---|
| 66 | #define THBUF_SIZE 64
|
|---|
| 67 | uintptr_t thread_hash_buf[THBUF_SIZE];
|
|---|
| 68 | int n_threads;
|
|---|
| 69 |
|
|---|
| 70 | int next_thread_id;
|
|---|
| 71 |
|
|---|
| 72 | ipc_call_t thread_ipc_req[THBUF_SIZE];
|
|---|
| 73 |
|
|---|
| 74 | int phoneid;
|
|---|
| 75 | bool abort_trace;
|
|---|
| 76 |
|
|---|
| 77 | uintptr_t thash;
|
|---|
| 78 | static bool paused;
|
|---|
| 79 | static fibril_condvar_t state_cv;
|
|---|
| 80 | static fibril_mutex_t state_lock;
|
|---|
| 81 |
|
|---|
| 82 | static bool cev_valid;
|
|---|
| 83 | static console_event_t cev;
|
|---|
| 84 |
|
|---|
| 85 | void thread_trace_start(uintptr_t thread_hash);
|
|---|
| 86 |
|
|---|
| 87 | static proto_t *proto_console;
|
|---|
| 88 | static task_id_t task_id;
|
|---|
| 89 | static loader_t *task_ldr;
|
|---|
| 90 | static bool task_wait_for;
|
|---|
| 91 |
|
|---|
| 92 | /** Combination of events/data to print. */
|
|---|
| 93 | display_mask_t display_mask;
|
|---|
| 94 |
|
|---|
| 95 | static int program_run_fibril(void *arg);
|
|---|
| 96 | static int cev_fibril(void *arg);
|
|---|
| 97 |
|
|---|
| 98 | static void program_run(void)
|
|---|
| 99 | {
|
|---|
| 100 | fid_t fid;
|
|---|
| 101 |
|
|---|
| 102 | fid = fibril_create(program_run_fibril, NULL);
|
|---|
| 103 | if (fid == 0) {
|
|---|
| 104 | printf("Error creating fibril\n");
|
|---|
| 105 | exit(1);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | fibril_add_ready(fid);
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | static void cev_fibril_start(void)
|
|---|
| 112 | {
|
|---|
| 113 | fid_t fid;
|
|---|
| 114 |
|
|---|
| 115 | fid = fibril_create(cev_fibril, NULL);
|
|---|
| 116 | if (fid == 0) {
|
|---|
| 117 | printf("Error creating fibril\n");
|
|---|
| 118 | exit(1);
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | fibril_add_ready(fid);
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | static int program_run_fibril(void *arg)
|
|---|
| 125 | {
|
|---|
| 126 | int rc;
|
|---|
| 127 |
|
|---|
| 128 | /*
|
|---|
| 129 | * This must be done in background as it will block until
|
|---|
| 130 | * we let the task reply to this call.
|
|---|
| 131 | */
|
|---|
| 132 | rc = loader_run(task_ldr);
|
|---|
| 133 | if (rc != 0) {
|
|---|
| 134 | printf("Error running program\n");
|
|---|
| 135 | exit(1);
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | free(task_ldr);
|
|---|
| 139 | task_ldr = NULL;
|
|---|
| 140 |
|
|---|
| 141 | printf("program_run_fibril exiting\n");
|
|---|
| 142 | return 0;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 | static int connect_task(task_id_t task_id)
|
|---|
| 147 | {
|
|---|
| 148 | int rc;
|
|---|
| 149 |
|
|---|
| 150 | rc = async_connect_kbox(task_id);
|
|---|
| 151 |
|
|---|
| 152 | if (rc == ENOTSUP) {
|
|---|
| 153 | printf("You do not have userspace debugging support "
|
|---|
| 154 | "compiled in the kernel.\n");
|
|---|
| 155 | printf("Compile kernel with 'Support for userspace debuggers' "
|
|---|
| 156 | "(CONFIG_UDEBUG) enabled.\n");
|
|---|
| 157 | return rc;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | if (rc < 0) {
|
|---|
| 161 | printf("Error connecting\n");
|
|---|
| 162 | printf("ipc_connect_task(%" PRIu64 ") -> %d ", task_id, rc);
|
|---|
| 163 | return rc;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | phoneid = rc;
|
|---|
| 167 |
|
|---|
| 168 | rc = udebug_begin(phoneid);
|
|---|
| 169 | if (rc < 0) {
|
|---|
| 170 | printf("udebug_begin() -> %d\n", rc);
|
|---|
| 171 | return rc;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | rc = udebug_set_evmask(phoneid, UDEBUG_EM_ALL);
|
|---|
| 175 | if (rc < 0) {
|
|---|
| 176 | printf("udebug_set_evmask(0x%x) -> %d\n ", UDEBUG_EM_ALL, rc);
|
|---|
| 177 | return rc;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | return 0;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | static int get_thread_list(void)
|
|---|
| 184 | {
|
|---|
| 185 | int rc;
|
|---|
| 186 | size_t tb_copied;
|
|---|
| 187 | size_t tb_needed;
|
|---|
| 188 | int i;
|
|---|
| 189 |
|
|---|
| 190 | rc = udebug_thread_read(phoneid, thread_hash_buf,
|
|---|
| 191 | THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed);
|
|---|
| 192 | if (rc < 0) {
|
|---|
| 193 | printf("udebug_thread_read() -> %d\n", rc);
|
|---|
| 194 | return rc;
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | n_threads = tb_copied / sizeof(uintptr_t);
|
|---|
| 198 |
|
|---|
| 199 | printf("Threads:");
|
|---|
| 200 | for (i = 0; i < n_threads; i++) {
|
|---|
| 201 | printf(" [%d] (hash %p)", 1 + i, (void *) thread_hash_buf[i]);
|
|---|
| 202 | }
|
|---|
| 203 | printf("\ntotal of %zu threads\n", tb_needed / sizeof(uintptr_t));
|
|---|
| 204 |
|
|---|
| 205 | return 0;
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | void val_print(sysarg_t val, val_type_t v_type)
|
|---|
| 209 | {
|
|---|
| 210 | long sval;
|
|---|
| 211 |
|
|---|
| 212 | sval = (long) val;
|
|---|
| 213 |
|
|---|
| 214 | switch (v_type) {
|
|---|
| 215 | case V_VOID:
|
|---|
| 216 | printf("<void>");
|
|---|
| 217 | break;
|
|---|
| 218 |
|
|---|
| 219 | case V_INTEGER:
|
|---|
| 220 | printf("%ld", sval);
|
|---|
| 221 | break;
|
|---|
| 222 |
|
|---|
| 223 | case V_HASH:
|
|---|
| 224 | case V_PTR:
|
|---|
| 225 | printf("%p", (void *) val);
|
|---|
| 226 | break;
|
|---|
| 227 |
|
|---|
| 228 | case V_ERRNO:
|
|---|
| 229 | if (sval >= -15 && sval <= 0) {
|
|---|
| 230 | printf("%ld %s (%s)", sval,
|
|---|
| 231 | err_desc[-sval].name,
|
|---|
| 232 | err_desc[-sval].desc);
|
|---|
| 233 | } else {
|
|---|
| 234 | printf("%ld", sval);
|
|---|
| 235 | }
|
|---|
| 236 | break;
|
|---|
| 237 | case V_INT_ERRNO:
|
|---|
| 238 | if (sval >= -15 && sval < 0) {
|
|---|
| 239 | printf("%ld %s (%s)", sval,
|
|---|
| 240 | err_desc[-sval].name,
|
|---|
| 241 | err_desc[-sval].desc);
|
|---|
| 242 | } else {
|
|---|
| 243 | printf("%ld", sval);
|
|---|
| 244 | }
|
|---|
| 245 | break;
|
|---|
| 246 |
|
|---|
| 247 | case V_CHAR:
|
|---|
| 248 | if (sval >= 0x20 && sval < 0x7f) {
|
|---|
| 249 | printf("'%c'", (char) sval);
|
|---|
| 250 | } else {
|
|---|
| 251 | switch (sval) {
|
|---|
| 252 | case '\a': printf("'\\a'"); break;
|
|---|
| 253 | case '\b': printf("'\\b'"); break;
|
|---|
| 254 | case '\n': printf("'\\n'"); break;
|
|---|
| 255 | case '\r': printf("'\\r'"); break;
|
|---|
| 256 | case '\t': printf("'\\t'"); break;
|
|---|
| 257 | case '\\': printf("'\\\\'"); break;
|
|---|
| 258 | default: printf("'\\x%02" PRIxn "'", val); break;
|
|---|
| 259 | }
|
|---|
| 260 | }
|
|---|
| 261 | break;
|
|---|
| 262 | }
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 | static void print_sc_retval(sysarg_t retval, val_type_t val_type)
|
|---|
| 267 | {
|
|---|
| 268 | printf(" -> ");
|
|---|
| 269 | val_print(retval, val_type);
|
|---|
| 270 | putchar('\n');
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | static void print_sc_args(sysarg_t *sc_args, int n)
|
|---|
| 274 | {
|
|---|
| 275 | int i;
|
|---|
| 276 |
|
|---|
| 277 | putchar('(');
|
|---|
| 278 | if (n > 0) printf("%" PRIun, sc_args[0]);
|
|---|
| 279 | for (i = 1; i < n; i++) {
|
|---|
| 280 | printf(", %" PRIun, sc_args[i]);
|
|---|
| 281 | }
|
|---|
| 282 | putchar(')');
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | static void sc_ipc_call_async_fast(sysarg_t *sc_args, sysarg_t sc_rc)
|
|---|
| 286 | {
|
|---|
| 287 | ipc_call_t call;
|
|---|
| 288 | sysarg_t phoneid;
|
|---|
| 289 |
|
|---|
| 290 | if (sc_rc == (sysarg_t) IPC_CALLRET_FATAL ||
|
|---|
| 291 | sc_rc == (sysarg_t) IPC_CALLRET_TEMPORARY)
|
|---|
| 292 | return;
|
|---|
| 293 |
|
|---|
| 294 | phoneid = sc_args[0];
|
|---|
| 295 |
|
|---|
| 296 | IPC_SET_IMETHOD(call, sc_args[1]);
|
|---|
| 297 | IPC_SET_ARG1(call, sc_args[2]);
|
|---|
| 298 | IPC_SET_ARG2(call, sc_args[3]);
|
|---|
| 299 | IPC_SET_ARG3(call, sc_args[4]);
|
|---|
| 300 | IPC_SET_ARG4(call, sc_args[5]);
|
|---|
| 301 | IPC_SET_ARG5(call, 0);
|
|---|
| 302 |
|
|---|
| 303 | ipcp_call_out(phoneid, &call, sc_rc);
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | static void sc_ipc_call_async_slow(sysarg_t *sc_args, sysarg_t sc_rc)
|
|---|
| 307 | {
|
|---|
| 308 | ipc_call_t call;
|
|---|
| 309 | int rc;
|
|---|
| 310 |
|
|---|
| 311 | if (sc_rc == (sysarg_t) IPC_CALLRET_FATAL ||
|
|---|
| 312 | sc_rc == (sysarg_t) IPC_CALLRET_TEMPORARY)
|
|---|
| 313 | return;
|
|---|
| 314 |
|
|---|
| 315 | memset(&call, 0, sizeof(call));
|
|---|
| 316 | rc = udebug_mem_read(phoneid, &call.args, sc_args[1], sizeof(call.args));
|
|---|
| 317 |
|
|---|
| 318 | if (rc >= 0) {
|
|---|
| 319 | ipcp_call_out(sc_args[0], &call, sc_rc);
|
|---|
| 320 | }
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | static void sc_ipc_call_sync_fast(sysarg_t *sc_args)
|
|---|
| 324 | {
|
|---|
| 325 | ipc_call_t question, reply;
|
|---|
| 326 | int rc;
|
|---|
| 327 | int phoneidx;
|
|---|
| 328 |
|
|---|
| 329 | // printf("sc_ipc_call_sync_fast()\n");
|
|---|
| 330 | phoneidx = sc_args[0];
|
|---|
| 331 |
|
|---|
| 332 | IPC_SET_IMETHOD(question, sc_args[1]);
|
|---|
| 333 | IPC_SET_ARG1(question, sc_args[2]);
|
|---|
| 334 | IPC_SET_ARG2(question, sc_args[3]);
|
|---|
| 335 | IPC_SET_ARG3(question, sc_args[4]);
|
|---|
| 336 | IPC_SET_ARG4(question, 0);
|
|---|
| 337 | IPC_SET_ARG5(question, 0);
|
|---|
| 338 |
|
|---|
| 339 | // printf("memset\n");
|
|---|
| 340 | memset(&reply, 0, sizeof(reply));
|
|---|
| 341 | // printf("udebug_mem_read(phone=%d, buffer_ptr=%u, src_addr=%d, n=%d\n",
|
|---|
| 342 | // phoneid, &reply.args, sc_args[5], sizeof(reply.args));
|
|---|
| 343 | rc = udebug_mem_read(phoneid, &reply.args, sc_args[5], sizeof(reply.args));
|
|---|
| 344 | // printf("dmr->%d\n", rc);
|
|---|
| 345 | if (rc < 0) return;
|
|---|
| 346 |
|
|---|
| 347 | // printf("call ipc_call_sync\n");
|
|---|
| 348 | ipcp_call_sync(phoneidx, &question, &reply);
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | static void sc_ipc_call_sync_slow_b(unsigned thread_id, sysarg_t *sc_args)
|
|---|
| 352 | {
|
|---|
| 353 | ipc_call_t question;
|
|---|
| 354 | int rc;
|
|---|
| 355 |
|
|---|
| 356 | memset(&question, 0, sizeof(question));
|
|---|
| 357 | rc = udebug_mem_read(phoneid, &question.args, sc_args[1],
|
|---|
| 358 | sizeof(question.args));
|
|---|
| 359 |
|
|---|
| 360 | if (rc < 0) {
|
|---|
| 361 | printf("Error: mem_read->%d\n", rc);
|
|---|
| 362 | return;
|
|---|
| 363 | }
|
|---|
| 364 |
|
|---|
| 365 | thread_ipc_req[thread_id] = question;
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| 368 | static void sc_ipc_call_sync_slow_e(unsigned thread_id, sysarg_t *sc_args)
|
|---|
| 369 | {
|
|---|
| 370 | ipc_call_t reply;
|
|---|
| 371 | int rc;
|
|---|
| 372 |
|
|---|
| 373 | memset(&reply, 0, sizeof(reply));
|
|---|
| 374 | rc = udebug_mem_read(phoneid, &reply.args, sc_args[2],
|
|---|
| 375 | sizeof(reply.args));
|
|---|
| 376 |
|
|---|
| 377 | if (rc < 0) {
|
|---|
| 378 | printf("Error: mem_read->%d\n", rc);
|
|---|
| 379 | return;
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| 382 | ipcp_call_sync(sc_args[0], &thread_ipc_req[thread_id], &reply);
|
|---|
| 383 | }
|
|---|
| 384 |
|
|---|
| 385 | static void sc_ipc_wait(sysarg_t *sc_args, int sc_rc)
|
|---|
| 386 | {
|
|---|
| 387 | ipc_call_t call;
|
|---|
| 388 | int rc;
|
|---|
| 389 |
|
|---|
| 390 | if (sc_rc == 0) return;
|
|---|
| 391 |
|
|---|
| 392 | memset(&call, 0, sizeof(call));
|
|---|
| 393 | rc = udebug_mem_read(phoneid, &call, sc_args[0], sizeof(call));
|
|---|
| 394 | // printf("udebug_mem_read(phone %d, dest %d, app-mem src %d, size %d -> %d\n",
|
|---|
| 395 | // phoneid, (int)&call, sc_args[0], sizeof(call), rc);
|
|---|
| 396 |
|
|---|
| 397 | if (rc >= 0) {
|
|---|
| 398 | ipcp_call_in(&call, sc_rc);
|
|---|
| 399 | }
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | static void event_syscall_b(unsigned thread_id, uintptr_t thread_hash,
|
|---|
| 403 | unsigned sc_id, sysarg_t sc_rc)
|
|---|
| 404 | {
|
|---|
| 405 | sysarg_t sc_args[6];
|
|---|
| 406 | int rc;
|
|---|
| 407 |
|
|---|
| 408 | /* Read syscall arguments */
|
|---|
| 409 | rc = udebug_args_read(phoneid, thread_hash, sc_args);
|
|---|
| 410 |
|
|---|
| 411 | async_serialize_start();
|
|---|
| 412 |
|
|---|
| 413 | // printf("[%d] ", thread_id);
|
|---|
| 414 |
|
|---|
| 415 | if (rc < 0) {
|
|---|
| 416 | printf("error\n");
|
|---|
| 417 | async_serialize_end();
|
|---|
| 418 | return;
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | if ((display_mask & DM_SYSCALL) != 0) {
|
|---|
| 422 | /* Print syscall name and arguments */
|
|---|
| 423 | printf("%s", syscall_desc[sc_id].name);
|
|---|
| 424 | print_sc_args(sc_args, syscall_desc[sc_id].n_args);
|
|---|
| 425 | }
|
|---|
| 426 |
|
|---|
| 427 | switch (sc_id) {
|
|---|
| 428 | case SYS_IPC_CALL_SYNC_SLOW:
|
|---|
| 429 | sc_ipc_call_sync_slow_b(thread_id, sc_args);
|
|---|
| 430 | break;
|
|---|
| 431 | default:
|
|---|
| 432 | break;
|
|---|
| 433 | }
|
|---|
| 434 |
|
|---|
| 435 | async_serialize_end();
|
|---|
| 436 | }
|
|---|
| 437 |
|
|---|
| 438 | static void event_syscall_e(unsigned thread_id, uintptr_t thread_hash,
|
|---|
| 439 | unsigned sc_id, sysarg_t sc_rc)
|
|---|
| 440 | {
|
|---|
| 441 | sysarg_t sc_args[6];
|
|---|
| 442 | int rv_type;
|
|---|
| 443 | int rc;
|
|---|
| 444 |
|
|---|
| 445 | /* Read syscall arguments */
|
|---|
| 446 | rc = udebug_args_read(phoneid, thread_hash, sc_args);
|
|---|
| 447 |
|
|---|
| 448 | async_serialize_start();
|
|---|
| 449 |
|
|---|
| 450 | // printf("[%d] ", thread_id);
|
|---|
| 451 |
|
|---|
| 452 | if (rc < 0) {
|
|---|
| 453 | printf("error\n");
|
|---|
| 454 | async_serialize_end();
|
|---|
| 455 | return;
|
|---|
| 456 | }
|
|---|
| 457 |
|
|---|
| 458 | if ((display_mask & DM_SYSCALL) != 0) {
|
|---|
| 459 | /* Print syscall return value */
|
|---|
| 460 | rv_type = syscall_desc[sc_id].rv_type;
|
|---|
| 461 | print_sc_retval(sc_rc, rv_type);
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | switch (sc_id) {
|
|---|
| 465 | case SYS_IPC_CALL_ASYNC_FAST:
|
|---|
| 466 | sc_ipc_call_async_fast(sc_args, sc_rc);
|
|---|
| 467 | break;
|
|---|
| 468 | case SYS_IPC_CALL_ASYNC_SLOW:
|
|---|
| 469 | sc_ipc_call_async_slow(sc_args, sc_rc);
|
|---|
| 470 | break;
|
|---|
| 471 | case SYS_IPC_CALL_SYNC_FAST:
|
|---|
| 472 | sc_ipc_call_sync_fast(sc_args);
|
|---|
| 473 | break;
|
|---|
| 474 | case SYS_IPC_CALL_SYNC_SLOW:
|
|---|
| 475 | sc_ipc_call_sync_slow_e(thread_id, sc_args);
|
|---|
| 476 | break;
|
|---|
| 477 | case SYS_IPC_WAIT:
|
|---|
| 478 | sc_ipc_wait(sc_args, sc_rc);
|
|---|
| 479 | break;
|
|---|
| 480 | default:
|
|---|
| 481 | break;
|
|---|
| 482 | }
|
|---|
| 483 |
|
|---|
| 484 | async_serialize_end();
|
|---|
| 485 | }
|
|---|
| 486 |
|
|---|
| 487 | static void event_thread_b(uintptr_t hash)
|
|---|
| 488 | {
|
|---|
| 489 | async_serialize_start();
|
|---|
| 490 | printf("New thread, hash %p\n", (void *) hash);
|
|---|
| 491 | async_serialize_end();
|
|---|
| 492 |
|
|---|
| 493 | thread_trace_start(hash);
|
|---|
| 494 | }
|
|---|
| 495 |
|
|---|
| 496 | static int trace_loop(void *thread_hash_arg)
|
|---|
| 497 | {
|
|---|
| 498 | int rc;
|
|---|
| 499 | unsigned ev_type;
|
|---|
| 500 | uintptr_t thread_hash;
|
|---|
| 501 | unsigned thread_id;
|
|---|
| 502 | sysarg_t val0, val1;
|
|---|
| 503 |
|
|---|
| 504 | thread_hash = (uintptr_t)thread_hash_arg;
|
|---|
| 505 | thread_id = next_thread_id++;
|
|---|
| 506 | if (thread_id >= THBUF_SIZE) {
|
|---|
| 507 | printf("Too many threads.\n");
|
|---|
| 508 | return ELIMIT;
|
|---|
| 509 | }
|
|---|
| 510 |
|
|---|
| 511 | printf("Start tracing thread [%u] (hash %p).\n",
|
|---|
| 512 | thread_id, (void *) thread_hash);
|
|---|
| 513 |
|
|---|
| 514 | while (!abort_trace) {
|
|---|
| 515 |
|
|---|
| 516 | fibril_mutex_lock(&state_lock);
|
|---|
| 517 | if (paused) {
|
|---|
| 518 | printf("Thread [%u] paused. Press R to resume.\n",
|
|---|
| 519 | thread_id);
|
|---|
| 520 |
|
|---|
| 521 | while (paused)
|
|---|
| 522 | fibril_condvar_wait(&state_cv, &state_lock);
|
|---|
| 523 |
|
|---|
| 524 | printf("Thread [%u] resumed.\n", thread_id);
|
|---|
| 525 | }
|
|---|
| 526 | fibril_mutex_unlock(&state_lock);
|
|---|
| 527 |
|
|---|
| 528 | /* Run thread until an event occurs */
|
|---|
| 529 | rc = udebug_go(phoneid, thread_hash,
|
|---|
| 530 | &ev_type, &val0, &val1);
|
|---|
| 531 |
|
|---|
| 532 | // printf("rc = %d, ev_type=%d\n", rc, ev_type);
|
|---|
| 533 | if (ev_type == UDEBUG_EVENT_FINISHED) {
|
|---|
| 534 | /* Done tracing this thread */
|
|---|
| 535 | break;
|
|---|
| 536 | }
|
|---|
| 537 |
|
|---|
| 538 | if (rc >= 0) {
|
|---|
| 539 | switch (ev_type) {
|
|---|
| 540 | case UDEBUG_EVENT_SYSCALL_B:
|
|---|
| 541 | event_syscall_b(thread_id, thread_hash, val0, (int)val1);
|
|---|
| 542 | break;
|
|---|
| 543 | case UDEBUG_EVENT_SYSCALL_E:
|
|---|
| 544 | event_syscall_e(thread_id, thread_hash, val0, (int)val1);
|
|---|
| 545 | break;
|
|---|
| 546 | case UDEBUG_EVENT_STOP:
|
|---|
| 547 | printf("Stop event\n");
|
|---|
| 548 | fibril_mutex_lock(&state_lock);
|
|---|
| 549 | paused = true;
|
|---|
| 550 | fibril_mutex_unlock(&state_lock);
|
|---|
| 551 | break;
|
|---|
| 552 | case UDEBUG_EVENT_THREAD_B:
|
|---|
| 553 | event_thread_b(val0);
|
|---|
| 554 | break;
|
|---|
| 555 | case UDEBUG_EVENT_THREAD_E:
|
|---|
| 556 | printf("Thread %" PRIun " exited.\n", val0);
|
|---|
| 557 | fibril_mutex_lock(&state_lock);
|
|---|
| 558 | abort_trace = true;
|
|---|
| 559 | fibril_condvar_broadcast(&state_cv);
|
|---|
| 560 | fibril_mutex_unlock(&state_lock);
|
|---|
| 561 | break;
|
|---|
| 562 | default:
|
|---|
| 563 | printf("Unknown event type %d.\n", ev_type);
|
|---|
| 564 | break;
|
|---|
| 565 | }
|
|---|
| 566 | }
|
|---|
| 567 |
|
|---|
| 568 | }
|
|---|
| 569 |
|
|---|
| 570 | printf("Finished tracing thread [%d].\n", thread_id);
|
|---|
| 571 | return 0;
|
|---|
| 572 | }
|
|---|
| 573 |
|
|---|
| 574 | void thread_trace_start(uintptr_t thread_hash)
|
|---|
| 575 | {
|
|---|
| 576 | fid_t fid;
|
|---|
| 577 |
|
|---|
| 578 | thash = thread_hash;
|
|---|
| 579 |
|
|---|
| 580 | fid = fibril_create(trace_loop, (void *)thread_hash);
|
|---|
| 581 | if (fid == 0) {
|
|---|
| 582 | printf("Warning: Failed creating fibril\n");
|
|---|
| 583 | }
|
|---|
| 584 | fibril_add_ready(fid);
|
|---|
| 585 | }
|
|---|
| 586 |
|
|---|
| 587 | static loader_t *preload_task(const char *path, char **argv,
|
|---|
| 588 | task_id_t *task_id)
|
|---|
| 589 | {
|
|---|
| 590 | loader_t *ldr;
|
|---|
| 591 | int rc;
|
|---|
| 592 |
|
|---|
| 593 | /* Spawn a program loader */
|
|---|
| 594 | ldr = loader_connect();
|
|---|
| 595 | if (ldr == NULL)
|
|---|
| 596 | return 0;
|
|---|
| 597 |
|
|---|
| 598 | /* Get task ID. */
|
|---|
| 599 | rc = loader_get_task_id(ldr, task_id);
|
|---|
| 600 | if (rc != EOK)
|
|---|
| 601 | goto error;
|
|---|
| 602 |
|
|---|
| 603 | /* Send program pathname */
|
|---|
| 604 | rc = loader_set_pathname(ldr, path);
|
|---|
| 605 | if (rc != EOK)
|
|---|
| 606 | goto error;
|
|---|
| 607 |
|
|---|
| 608 | /* Send arguments */
|
|---|
| 609 | rc = loader_set_args(ldr, (const char **) argv);
|
|---|
| 610 | if (rc != EOK)
|
|---|
| 611 | goto error;
|
|---|
| 612 |
|
|---|
| 613 | /* Send default files */
|
|---|
| 614 | fdi_node_t *files[4];
|
|---|
| 615 | fdi_node_t stdin_node;
|
|---|
| 616 | fdi_node_t stdout_node;
|
|---|
| 617 | fdi_node_t stderr_node;
|
|---|
| 618 |
|
|---|
| 619 | if ((stdin != NULL) && (fnode(stdin, &stdin_node) == EOK))
|
|---|
| 620 | files[0] = &stdin_node;
|
|---|
| 621 | else
|
|---|
| 622 | files[0] = NULL;
|
|---|
| 623 |
|
|---|
| 624 | if ((stdout != NULL) && (fnode(stdout, &stdout_node) == EOK))
|
|---|
| 625 | files[1] = &stdout_node;
|
|---|
| 626 | else
|
|---|
| 627 | files[1] = NULL;
|
|---|
| 628 |
|
|---|
| 629 | if ((stderr != NULL) && (fnode(stderr, &stderr_node) == EOK))
|
|---|
| 630 | files[2] = &stderr_node;
|
|---|
| 631 | else
|
|---|
| 632 | files[2] = NULL;
|
|---|
| 633 |
|
|---|
| 634 | files[3] = NULL;
|
|---|
| 635 |
|
|---|
| 636 | rc = loader_set_files(ldr, files);
|
|---|
| 637 | if (rc != EOK)
|
|---|
| 638 | goto error;
|
|---|
| 639 |
|
|---|
| 640 | /* Load the program. */
|
|---|
| 641 | rc = loader_load_program(ldr);
|
|---|
| 642 | if (rc != EOK)
|
|---|
| 643 | goto error;
|
|---|
| 644 |
|
|---|
| 645 | /* Success */
|
|---|
| 646 | return ldr;
|
|---|
| 647 |
|
|---|
| 648 | /* Error exit */
|
|---|
| 649 | error:
|
|---|
| 650 | loader_abort(ldr);
|
|---|
| 651 | free(ldr);
|
|---|
| 652 | return NULL;
|
|---|
| 653 | }
|
|---|
| 654 |
|
|---|
| 655 | static int cev_fibril(void *arg)
|
|---|
| 656 | {
|
|---|
| 657 | (void) arg;
|
|---|
| 658 |
|
|---|
| 659 | while (true) {
|
|---|
| 660 | fibril_mutex_lock(&state_lock);
|
|---|
| 661 | while (cev_valid)
|
|---|
| 662 | fibril_condvar_wait(&state_cv, &state_lock);
|
|---|
| 663 | fibril_mutex_unlock(&state_lock);
|
|---|
| 664 |
|
|---|
| 665 | if (!console_get_event(fphone(stdin), &cev))
|
|---|
| 666 | return -1;
|
|---|
| 667 |
|
|---|
| 668 | fibril_mutex_lock(&state_lock);
|
|---|
| 669 | cev_valid = true;
|
|---|
| 670 | fibril_condvar_broadcast(&state_cv);
|
|---|
| 671 | fibril_mutex_unlock(&state_lock);
|
|---|
| 672 | }
|
|---|
| 673 | }
|
|---|
| 674 |
|
|---|
| 675 | static void trace_task(task_id_t task_id)
|
|---|
| 676 | {
|
|---|
| 677 | console_event_t ev;
|
|---|
| 678 | bool done;
|
|---|
| 679 | int i;
|
|---|
| 680 | int rc;
|
|---|
| 681 |
|
|---|
| 682 | ipcp_init();
|
|---|
| 683 |
|
|---|
| 684 | /*
|
|---|
| 685 | * User apps now typically have console on phone 3.
|
|---|
| 686 | * (Phones 1 and 2 are used by the loader).
|
|---|
| 687 | */
|
|---|
| 688 | ipcp_connection_set(3, 0, proto_console);
|
|---|
| 689 |
|
|---|
| 690 | rc = get_thread_list();
|
|---|
| 691 | if (rc < 0) {
|
|---|
| 692 | printf("Failed to get thread list (error %d)\n", rc);
|
|---|
| 693 | return;
|
|---|
| 694 | }
|
|---|
| 695 |
|
|---|
| 696 | abort_trace = false;
|
|---|
| 697 |
|
|---|
| 698 | for (i = 0; i < n_threads; i++) {
|
|---|
| 699 | thread_trace_start(thread_hash_buf[i]);
|
|---|
| 700 | }
|
|---|
| 701 |
|
|---|
| 702 | done = false;
|
|---|
| 703 |
|
|---|
| 704 | while (!done) {
|
|---|
| 705 | fibril_mutex_lock(&state_lock);
|
|---|
| 706 | while (!cev_valid && !abort_trace)
|
|---|
| 707 | fibril_condvar_wait(&state_cv, &state_lock);
|
|---|
| 708 | fibril_mutex_unlock(&state_lock);
|
|---|
| 709 |
|
|---|
| 710 | ev = cev;
|
|---|
| 711 |
|
|---|
| 712 | fibril_mutex_lock(&state_lock);
|
|---|
| 713 | cev_valid = false;
|
|---|
| 714 | fibril_condvar_broadcast(&state_cv);
|
|---|
| 715 | fibril_mutex_unlock(&state_lock);
|
|---|
| 716 |
|
|---|
| 717 | if (abort_trace)
|
|---|
| 718 | break;
|
|---|
| 719 |
|
|---|
| 720 | if (ev.type != KEY_PRESS)
|
|---|
| 721 | continue;
|
|---|
| 722 |
|
|---|
| 723 | switch (ev.key) {
|
|---|
| 724 | case KC_Q:
|
|---|
| 725 | done = true;
|
|---|
| 726 | break;
|
|---|
| 727 | case KC_P:
|
|---|
| 728 | printf("Pause...\n");
|
|---|
| 729 | rc = udebug_stop(phoneid, thash);
|
|---|
| 730 | if (rc != EOK)
|
|---|
| 731 | printf("Error: stop -> %d\n", rc);
|
|---|
| 732 | break;
|
|---|
| 733 | case KC_R:
|
|---|
| 734 | fibril_mutex_lock(&state_lock);
|
|---|
| 735 | paused = false;
|
|---|
| 736 | fibril_condvar_broadcast(&state_cv);
|
|---|
| 737 | fibril_mutex_unlock(&state_lock);
|
|---|
| 738 | printf("Resume...\n");
|
|---|
| 739 | break;
|
|---|
| 740 | }
|
|---|
| 741 | }
|
|---|
| 742 |
|
|---|
| 743 | printf("\nTerminate debugging session...\n");
|
|---|
| 744 | abort_trace = true;
|
|---|
| 745 | udebug_end(phoneid);
|
|---|
| 746 | async_hangup(phoneid);
|
|---|
| 747 |
|
|---|
| 748 | ipcp_cleanup();
|
|---|
| 749 |
|
|---|
| 750 | printf("Done\n");
|
|---|
| 751 | return;
|
|---|
| 752 | }
|
|---|
| 753 |
|
|---|
| 754 | static void main_init(void)
|
|---|
| 755 | {
|
|---|
| 756 | proto_t *p;
|
|---|
| 757 | oper_t *o;
|
|---|
| 758 |
|
|---|
| 759 | val_type_t arg_def[OPER_MAX_ARGS] = {
|
|---|
| 760 | V_INTEGER,
|
|---|
| 761 | V_INTEGER,
|
|---|
| 762 | V_INTEGER,
|
|---|
| 763 | V_INTEGER,
|
|---|
| 764 | V_INTEGER
|
|---|
| 765 | };
|
|---|
| 766 |
|
|---|
| 767 | val_type_t resp_def[OPER_MAX_ARGS] = {
|
|---|
| 768 | V_INTEGER,
|
|---|
| 769 | V_INTEGER,
|
|---|
| 770 | V_INTEGER,
|
|---|
| 771 | V_INTEGER,
|
|---|
| 772 | V_INTEGER
|
|---|
| 773 | };
|
|---|
| 774 |
|
|---|
| 775 | next_thread_id = 1;
|
|---|
| 776 | paused = false;
|
|---|
| 777 | cev_valid = false;
|
|---|
| 778 |
|
|---|
| 779 | fibril_mutex_initialize(&state_lock);
|
|---|
| 780 | fibril_condvar_initialize(&state_cv);
|
|---|
| 781 |
|
|---|
| 782 | proto_init();
|
|---|
| 783 |
|
|---|
| 784 | p = proto_new("vfs");
|
|---|
| 785 | o = oper_new("open", 2, arg_def, V_INT_ERRNO, 0, resp_def);
|
|---|
| 786 | proto_add_oper(p, VFS_IN_OPEN, o);
|
|---|
| 787 | o = oper_new("open_node", 4, arg_def, V_INT_ERRNO, 0, resp_def);
|
|---|
| 788 | proto_add_oper(p, VFS_IN_OPEN_NODE, o);
|
|---|
| 789 | o = oper_new("read", 1, arg_def, V_ERRNO, 1, resp_def);
|
|---|
| 790 | proto_add_oper(p, VFS_IN_READ, o);
|
|---|
| 791 | o = oper_new("write", 1, arg_def, V_ERRNO, 1, resp_def);
|
|---|
| 792 | proto_add_oper(p, VFS_IN_WRITE, o);
|
|---|
| 793 | o = oper_new("seek", 3, arg_def, V_ERRNO, 0, resp_def);
|
|---|
| 794 | proto_add_oper(p, VFS_IN_SEEK, o);
|
|---|
| 795 | o = oper_new("truncate", 5, arg_def, V_ERRNO, 0, resp_def);
|
|---|
| 796 | proto_add_oper(p, VFS_IN_TRUNCATE, o);
|
|---|
| 797 | o = oper_new("fstat", 1, arg_def, V_ERRNO, 0, resp_def);
|
|---|
| 798 | proto_add_oper(p, VFS_IN_FSTAT, o);
|
|---|
| 799 | o = oper_new("close", 1, arg_def, V_ERRNO, 0, resp_def);
|
|---|
| 800 | proto_add_oper(p, VFS_IN_CLOSE, o);
|
|---|
| 801 | o = oper_new("mount", 2, arg_def, V_ERRNO, 0, resp_def);
|
|---|
| 802 | proto_add_oper(p, VFS_IN_MOUNT, o);
|
|---|
| 803 | /* o = oper_new("unmount", 0, arg_def);
|
|---|
| 804 | proto_add_oper(p, VFS_IN_UNMOUNT, o);*/
|
|---|
| 805 | o = oper_new("sync", 1, arg_def, V_ERRNO, 0, resp_def);
|
|---|
| 806 | proto_add_oper(p, VFS_IN_SYNC, o);
|
|---|
| 807 | o = oper_new("mkdir", 1, arg_def, V_ERRNO, 0, resp_def);
|
|---|
| 808 | proto_add_oper(p, VFS_IN_MKDIR, o);
|
|---|
| 809 | o = oper_new("unlink", 0, arg_def, V_ERRNO, 0, resp_def);
|
|---|
| 810 | proto_add_oper(p, VFS_IN_UNLINK, o);
|
|---|
| 811 | o = oper_new("rename", 0, arg_def, V_ERRNO, 0, resp_def);
|
|---|
| 812 | proto_add_oper(p, VFS_IN_RENAME, o);
|
|---|
| 813 | o = oper_new("stat", 0, arg_def, V_ERRNO, 0, resp_def);
|
|---|
| 814 | proto_add_oper(p, VFS_IN_STAT, o);
|
|---|
| 815 |
|
|---|
| 816 | proto_register(SERVICE_VFS, p);
|
|---|
| 817 |
|
|---|
| 818 | p = proto_new("console");
|
|---|
| 819 |
|
|---|
| 820 | o = oper_new("write", 1, arg_def, V_ERRNO, 1, resp_def);
|
|---|
| 821 | proto_add_oper(p, VFS_IN_WRITE, o);
|
|---|
| 822 |
|
|---|
| 823 | resp_def[0] = V_INTEGER; resp_def[1] = V_INTEGER;
|
|---|
| 824 | resp_def[2] = V_INTEGER; resp_def[3] = V_CHAR;
|
|---|
| 825 | o = oper_new("getkey", 0, arg_def, V_ERRNO, 4, resp_def);
|
|---|
| 826 |
|
|---|
| 827 | arg_def[0] = V_CHAR;
|
|---|
| 828 | o = oper_new("clear", 0, arg_def, V_VOID, 0, resp_def);
|
|---|
| 829 | proto_add_oper(p, CONSOLE_CLEAR, o);
|
|---|
| 830 |
|
|---|
| 831 | arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER;
|
|---|
| 832 | o = oper_new("goto", 2, arg_def, V_VOID, 0, resp_def);
|
|---|
| 833 | proto_add_oper(p, CONSOLE_GOTO, o);
|
|---|
| 834 |
|
|---|
| 835 | resp_def[0] = V_INTEGER; resp_def[1] = V_INTEGER;
|
|---|
| 836 | o = oper_new("getsize", 0, arg_def, V_INTEGER, 2, resp_def);
|
|---|
| 837 | proto_add_oper(p, CONSOLE_GET_SIZE, o);
|
|---|
| 838 |
|
|---|
| 839 | arg_def[0] = V_INTEGER;
|
|---|
| 840 | o = oper_new("set_style", 1, arg_def, V_VOID, 0, resp_def);
|
|---|
| 841 | proto_add_oper(p, CONSOLE_SET_STYLE, o);
|
|---|
| 842 | arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER; arg_def[2] = V_INTEGER;
|
|---|
| 843 | o = oper_new("set_color", 3, arg_def, V_VOID, 0, resp_def);
|
|---|
| 844 | proto_add_oper(p, CONSOLE_SET_COLOR, o);
|
|---|
| 845 | arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER;
|
|---|
| 846 | o = oper_new("set_rgb_color", 2, arg_def, V_VOID, 0, resp_def);
|
|---|
| 847 | proto_add_oper(p, CONSOLE_SET_RGB_COLOR, o);
|
|---|
| 848 | o = oper_new("cursor_visibility", 1, arg_def, V_VOID, 0, resp_def);
|
|---|
| 849 | proto_add_oper(p, CONSOLE_CURSOR_VISIBILITY, o);
|
|---|
| 850 |
|
|---|
| 851 | proto_console = p;
|
|---|
| 852 | proto_register(SERVICE_CONSOLE, p);
|
|---|
| 853 | }
|
|---|
| 854 |
|
|---|
| 855 | static void print_syntax()
|
|---|
| 856 | {
|
|---|
| 857 | printf("Syntax:\n");
|
|---|
| 858 | printf("\ttrace [+<events>] <executable> [<arg1> [...]]\n");
|
|---|
| 859 | printf("or\ttrace [+<events>] -t <task_id>\n");
|
|---|
| 860 | printf("Events: (default is +tp)\n");
|
|---|
| 861 | printf("\n");
|
|---|
| 862 | printf("\tt ... Thread creation and termination\n");
|
|---|
| 863 | printf("\ts ... System calls\n");
|
|---|
| 864 | printf("\ti ... Low-level IPC\n");
|
|---|
| 865 | printf("\tp ... Protocol level\n");
|
|---|
| 866 | printf("\n");
|
|---|
| 867 | printf("Examples:\n");
|
|---|
| 868 | printf("\ttrace +s /app/tetris\n");
|
|---|
| 869 | printf("\ttrace +tsip -t 12\n");
|
|---|
| 870 | }
|
|---|
| 871 |
|
|---|
| 872 | static display_mask_t parse_display_mask(const char *text)
|
|---|
| 873 | {
|
|---|
| 874 | display_mask_t dm = 0;
|
|---|
| 875 | const char *c = text;
|
|---|
| 876 |
|
|---|
| 877 | while (*c) {
|
|---|
| 878 | switch (*c) {
|
|---|
| 879 | case 't':
|
|---|
| 880 | dm = dm | DM_THREAD;
|
|---|
| 881 | break;
|
|---|
| 882 | case 's':
|
|---|
| 883 | dm = dm | DM_SYSCALL;
|
|---|
| 884 | break;
|
|---|
| 885 | case 'i':
|
|---|
| 886 | dm = dm | DM_IPC;
|
|---|
| 887 | break;
|
|---|
| 888 | case 'p':
|
|---|
| 889 | dm = dm | DM_SYSTEM | DM_USER;
|
|---|
| 890 | break;
|
|---|
| 891 | default:
|
|---|
| 892 | printf("Unexpected event type '%c'.\n", *c);
|
|---|
| 893 | exit(1);
|
|---|
| 894 | }
|
|---|
| 895 |
|
|---|
| 896 | ++c;
|
|---|
| 897 | }
|
|---|
| 898 |
|
|---|
| 899 | return dm;
|
|---|
| 900 | }
|
|---|
| 901 |
|
|---|
| 902 | static int parse_args(int argc, char *argv[])
|
|---|
| 903 | {
|
|---|
| 904 | char *err_p;
|
|---|
| 905 |
|
|---|
| 906 | task_id = 0;
|
|---|
| 907 |
|
|---|
| 908 | --argc;
|
|---|
| 909 | ++argv;
|
|---|
| 910 |
|
|---|
| 911 | while (argc > 0) {
|
|---|
| 912 | char *arg = *argv;
|
|---|
| 913 | if (arg[0] == '+') {
|
|---|
| 914 | display_mask = parse_display_mask(&arg[1]);
|
|---|
| 915 | } else if (arg[0] == '-') {
|
|---|
| 916 | if (arg[1] == 't') {
|
|---|
| 917 | /* Trace an already running task */
|
|---|
| 918 | --argc;
|
|---|
| 919 | ++argv;
|
|---|
| 920 | task_id = strtol(*argv, &err_p, 10);
|
|---|
| 921 | task_ldr = NULL;
|
|---|
| 922 | task_wait_for = false;
|
|---|
| 923 | if (*err_p) {
|
|---|
| 924 | printf("Task ID syntax error\n");
|
|---|
| 925 | print_syntax();
|
|---|
| 926 | return -1;
|
|---|
| 927 | }
|
|---|
| 928 | } else {
|
|---|
| 929 | printf("Uknown option '%c'\n", arg[0]);
|
|---|
| 930 | print_syntax();
|
|---|
| 931 | return -1;
|
|---|
| 932 | }
|
|---|
| 933 | } else {
|
|---|
| 934 | break;
|
|---|
| 935 | }
|
|---|
| 936 |
|
|---|
| 937 | --argc;
|
|---|
| 938 | ++argv;
|
|---|
| 939 | }
|
|---|
| 940 |
|
|---|
| 941 | if (task_id != 0) {
|
|---|
| 942 | if (argc == 0)
|
|---|
| 943 | return 0;
|
|---|
| 944 | printf("Extra arguments\n");
|
|---|
| 945 | print_syntax();
|
|---|
| 946 | return -1;
|
|---|
| 947 | }
|
|---|
| 948 |
|
|---|
| 949 | if (argc < 1) {
|
|---|
| 950 | printf("Missing argument\n");
|
|---|
| 951 | print_syntax();
|
|---|
| 952 | return -1;
|
|---|
| 953 | }
|
|---|
| 954 |
|
|---|
| 955 | /* Preload the specified program file. */
|
|---|
| 956 | printf("Spawning '%s' with arguments:\n", *argv);
|
|---|
| 957 |
|
|---|
| 958 | char **cp = argv;
|
|---|
| 959 | while (*cp)
|
|---|
| 960 | printf("'%s'\n", *cp++);
|
|---|
| 961 |
|
|---|
| 962 | task_ldr = preload_task(*argv, argv, &task_id);
|
|---|
| 963 | task_wait_for = true;
|
|---|
| 964 |
|
|---|
| 965 | return 0;
|
|---|
| 966 | }
|
|---|
| 967 |
|
|---|
| 968 | int main(int argc, char *argv[])
|
|---|
| 969 | {
|
|---|
| 970 | int rc;
|
|---|
| 971 | task_exit_t texit;
|
|---|
| 972 | int retval;
|
|---|
| 973 |
|
|---|
| 974 | printf("System Call / IPC Tracer\n");
|
|---|
| 975 | printf("Controls: Q - Quit, P - Pause, R - Resume\n");
|
|---|
| 976 |
|
|---|
| 977 | display_mask = DM_THREAD | DM_SYSTEM | DM_USER;
|
|---|
| 978 |
|
|---|
| 979 | if (parse_args(argc, argv) < 0)
|
|---|
| 980 | return 1;
|
|---|
| 981 |
|
|---|
| 982 | main_init();
|
|---|
| 983 |
|
|---|
| 984 | rc = connect_task(task_id);
|
|---|
| 985 | if (rc < 0) {
|
|---|
| 986 | printf("Failed connecting to task %" PRIu64 ".\n", task_id);
|
|---|
| 987 | return 1;
|
|---|
| 988 | }
|
|---|
| 989 |
|
|---|
| 990 | printf("Connected to task %" PRIu64 ".\n", task_id);
|
|---|
| 991 |
|
|---|
| 992 | if (task_ldr != NULL)
|
|---|
| 993 | program_run();
|
|---|
| 994 |
|
|---|
| 995 | cev_fibril_start();
|
|---|
| 996 | trace_task(task_id);
|
|---|
| 997 |
|
|---|
| 998 | if (task_wait_for) {
|
|---|
| 999 | printf("Waiting for task to exit.\n");
|
|---|
| 1000 |
|
|---|
| 1001 | rc = task_wait(task_id, &texit, &retval);
|
|---|
| 1002 | if (rc != EOK) {
|
|---|
| 1003 | printf("Failed waiting for task.\n");
|
|---|
| 1004 | return -1;
|
|---|
| 1005 | }
|
|---|
| 1006 |
|
|---|
| 1007 | if (texit == TASK_EXIT_NORMAL) {
|
|---|
| 1008 | printf("Task exited normally, return value %d.\n",
|
|---|
| 1009 | retval);
|
|---|
| 1010 | } else {
|
|---|
| 1011 | printf("Task exited unexpectedly.\n");
|
|---|
| 1012 | }
|
|---|
| 1013 | }
|
|---|
| 1014 |
|
|---|
| 1015 | return 0;
|
|---|
| 1016 | }
|
|---|
| 1017 |
|
|---|
| 1018 | /** @}
|
|---|
| 1019 | */
|
|---|