| 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 | async_sess_t *sess; | 
|---|
| 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 kbd_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 | async_sess_t *ksess = async_connect_kbox(task_id); | 
|---|
| 149 |  | 
|---|
| 150 | if (!ksess) { | 
|---|
| 151 | if (errno == ENOTSUP) { | 
|---|
| 152 | printf("You do not have userspace debugging support " | 
|---|
| 153 | "compiled in the kernel.\n"); | 
|---|
| 154 | printf("Compile kernel with 'Support for userspace debuggers' " | 
|---|
| 155 | "(CONFIG_UDEBUG) enabled.\n"); | 
|---|
| 156 | return errno; | 
|---|
| 157 | } | 
|---|
| 158 |  | 
|---|
| 159 | printf("Error connecting\n"); | 
|---|
| 160 | printf("ipc_connect_task(%" PRIu64 ") -> %d ", task_id, errno); | 
|---|
| 161 | return errno; | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | int rc = udebug_begin(ksess); | 
|---|
| 165 | if (rc < 0) { | 
|---|
| 166 | printf("udebug_begin() -> %d\n", rc); | 
|---|
| 167 | return rc; | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | rc = udebug_set_evmask(ksess, UDEBUG_EM_ALL); | 
|---|
| 171 | if (rc < 0) { | 
|---|
| 172 | printf("udebug_set_evmask(0x%x) -> %d\n ", UDEBUG_EM_ALL, rc); | 
|---|
| 173 | return rc; | 
|---|
| 174 | } | 
|---|
| 175 |  | 
|---|
| 176 | sess = ksess; | 
|---|
| 177 | return 0; | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | static int get_thread_list(void) | 
|---|
| 181 | { | 
|---|
| 182 | int rc; | 
|---|
| 183 | size_t tb_copied; | 
|---|
| 184 | size_t tb_needed; | 
|---|
| 185 | int i; | 
|---|
| 186 |  | 
|---|
| 187 | rc = udebug_thread_read(sess, thread_hash_buf, | 
|---|
| 188 | THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed); | 
|---|
| 189 | if (rc < 0) { | 
|---|
| 190 | printf("udebug_thread_read() -> %d\n", rc); | 
|---|
| 191 | return rc; | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 194 | n_threads = tb_copied / sizeof(uintptr_t); | 
|---|
| 195 |  | 
|---|
| 196 | printf("Threads:"); | 
|---|
| 197 | for (i = 0; i < n_threads; i++) { | 
|---|
| 198 | printf(" [%d] (hash %p)", 1 + i, (void *) thread_hash_buf[i]); | 
|---|
| 199 | } | 
|---|
| 200 | printf("\ntotal of %zu threads\n", tb_needed / sizeof(uintptr_t)); | 
|---|
| 201 |  | 
|---|
| 202 | return 0; | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | void val_print(sysarg_t val, val_type_t v_type) | 
|---|
| 206 | { | 
|---|
| 207 | long sval; | 
|---|
| 208 |  | 
|---|
| 209 | sval = (long) val; | 
|---|
| 210 |  | 
|---|
| 211 | switch (v_type) { | 
|---|
| 212 | case V_VOID: | 
|---|
| 213 | printf("<void>"); | 
|---|
| 214 | break; | 
|---|
| 215 |  | 
|---|
| 216 | case V_INTEGER: | 
|---|
| 217 | printf("%ld", sval); | 
|---|
| 218 | break; | 
|---|
| 219 |  | 
|---|
| 220 | case V_HASH: | 
|---|
| 221 | case V_PTR: | 
|---|
| 222 | printf("%p", (void *) val); | 
|---|
| 223 | break; | 
|---|
| 224 |  | 
|---|
| 225 | case V_ERRNO: | 
|---|
| 226 | if (sval >= -15 && sval <= 0) { | 
|---|
| 227 | printf("%ld %s (%s)", sval, | 
|---|
| 228 | err_desc[-sval].name, | 
|---|
| 229 | err_desc[-sval].desc); | 
|---|
| 230 | } else { | 
|---|
| 231 | printf("%ld", sval); | 
|---|
| 232 | } | 
|---|
| 233 | break; | 
|---|
| 234 | case V_INT_ERRNO: | 
|---|
| 235 | if (sval >= -15 && sval < 0) { | 
|---|
| 236 | printf("%ld %s (%s)", sval, | 
|---|
| 237 | err_desc[-sval].name, | 
|---|
| 238 | err_desc[-sval].desc); | 
|---|
| 239 | } else { | 
|---|
| 240 | printf("%ld", sval); | 
|---|
| 241 | } | 
|---|
| 242 | break; | 
|---|
| 243 |  | 
|---|
| 244 | case V_CHAR: | 
|---|
| 245 | if (sval >= 0x20 && sval < 0x7f) { | 
|---|
| 246 | printf("'%c'", (char) sval); | 
|---|
| 247 | } else { | 
|---|
| 248 | switch (sval) { | 
|---|
| 249 | case '\a': printf("'\\a'"); break; | 
|---|
| 250 | case '\b': printf("'\\b'"); break; | 
|---|
| 251 | case '\n': printf("'\\n'"); break; | 
|---|
| 252 | case '\r': printf("'\\r'"); break; | 
|---|
| 253 | case '\t': printf("'\\t'"); break; | 
|---|
| 254 | case '\\': printf("'\\\\'"); break; | 
|---|
| 255 | default: printf("'\\x%02" PRIxn "'", val); break; | 
|---|
| 256 | } | 
|---|
| 257 | } | 
|---|
| 258 | break; | 
|---|
| 259 | } | 
|---|
| 260 | } | 
|---|
| 261 |  | 
|---|
| 262 |  | 
|---|
| 263 | static void print_sc_retval(sysarg_t retval, val_type_t val_type) | 
|---|
| 264 | { | 
|---|
| 265 | printf(" -> "); | 
|---|
| 266 | val_print(retval, val_type); | 
|---|
| 267 | putchar('\n'); | 
|---|
| 268 | } | 
|---|
| 269 |  | 
|---|
| 270 | static void print_sc_args(sysarg_t *sc_args, int n) | 
|---|
| 271 | { | 
|---|
| 272 | int i; | 
|---|
| 273 |  | 
|---|
| 274 | putchar('('); | 
|---|
| 275 | if (n > 0) printf("%" PRIun, sc_args[0]); | 
|---|
| 276 | for (i = 1; i < n; i++) { | 
|---|
| 277 | printf(", %" PRIun, sc_args[i]); | 
|---|
| 278 | } | 
|---|
| 279 | putchar(')'); | 
|---|
| 280 | } | 
|---|
| 281 |  | 
|---|
| 282 | static void sc_ipc_call_async_fast(sysarg_t *sc_args, sysarg_t sc_rc) | 
|---|
| 283 | { | 
|---|
| 284 | ipc_call_t call; | 
|---|
| 285 | sysarg_t phoneid; | 
|---|
| 286 |  | 
|---|
| 287 | if (sc_rc == (sysarg_t) IPC_CALLRET_FATAL || | 
|---|
| 288 | sc_rc == (sysarg_t) IPC_CALLRET_TEMPORARY) | 
|---|
| 289 | return; | 
|---|
| 290 |  | 
|---|
| 291 | phoneid = sc_args[0]; | 
|---|
| 292 |  | 
|---|
| 293 | IPC_SET_IMETHOD(call, sc_args[1]); | 
|---|
| 294 | IPC_SET_ARG1(call, sc_args[2]); | 
|---|
| 295 | IPC_SET_ARG2(call, sc_args[3]); | 
|---|
| 296 | IPC_SET_ARG3(call, sc_args[4]); | 
|---|
| 297 | IPC_SET_ARG4(call, sc_args[5]); | 
|---|
| 298 | IPC_SET_ARG5(call, 0); | 
|---|
| 299 |  | 
|---|
| 300 | ipcp_call_out(phoneid, &call, sc_rc); | 
|---|
| 301 | } | 
|---|
| 302 |  | 
|---|
| 303 | static void sc_ipc_call_async_slow(sysarg_t *sc_args, sysarg_t sc_rc) | 
|---|
| 304 | { | 
|---|
| 305 | ipc_call_t call; | 
|---|
| 306 | int rc; | 
|---|
| 307 |  | 
|---|
| 308 | if (sc_rc == (sysarg_t) IPC_CALLRET_FATAL || | 
|---|
| 309 | sc_rc == (sysarg_t) IPC_CALLRET_TEMPORARY) | 
|---|
| 310 | return; | 
|---|
| 311 |  | 
|---|
| 312 | memset(&call, 0, sizeof(call)); | 
|---|
| 313 | rc = udebug_mem_read(sess, &call.args, sc_args[1], sizeof(call.args)); | 
|---|
| 314 |  | 
|---|
| 315 | if (rc >= 0) { | 
|---|
| 316 | ipcp_call_out(sc_args[0], &call, sc_rc); | 
|---|
| 317 | } | 
|---|
| 318 | } | 
|---|
| 319 |  | 
|---|
| 320 | static void sc_ipc_call_sync_fast(sysarg_t *sc_args) | 
|---|
| 321 | { | 
|---|
| 322 | ipc_call_t question, reply; | 
|---|
| 323 | int rc; | 
|---|
| 324 | int phoneid; | 
|---|
| 325 |  | 
|---|
| 326 | phoneid = sc_args[0]; | 
|---|
| 327 |  | 
|---|
| 328 | IPC_SET_IMETHOD(question, sc_args[1]); | 
|---|
| 329 | IPC_SET_ARG1(question, sc_args[2]); | 
|---|
| 330 | IPC_SET_ARG2(question, sc_args[3]); | 
|---|
| 331 | IPC_SET_ARG3(question, sc_args[4]); | 
|---|
| 332 | IPC_SET_ARG4(question, 0); | 
|---|
| 333 | IPC_SET_ARG5(question, 0); | 
|---|
| 334 |  | 
|---|
| 335 | memset(&reply, 0, sizeof(reply)); | 
|---|
| 336 | rc = udebug_mem_read(sess, &reply.args, sc_args[5], sizeof(reply.args)); | 
|---|
| 337 | if (rc < 0) | 
|---|
| 338 | return; | 
|---|
| 339 |  | 
|---|
| 340 | ipcp_call_sync(phoneid, &question, &reply); | 
|---|
| 341 | } | 
|---|
| 342 |  | 
|---|
| 343 | static void sc_ipc_call_sync_slow_b(unsigned thread_id, sysarg_t *sc_args) | 
|---|
| 344 | { | 
|---|
| 345 | ipc_call_t question; | 
|---|
| 346 | int rc; | 
|---|
| 347 |  | 
|---|
| 348 | memset(&question, 0, sizeof(question)); | 
|---|
| 349 | rc = udebug_mem_read(sess, &question.args, sc_args[1], | 
|---|
| 350 | sizeof(question.args)); | 
|---|
| 351 |  | 
|---|
| 352 | if (rc < 0) { | 
|---|
| 353 | printf("Error: mem_read->%d\n", rc); | 
|---|
| 354 | return; | 
|---|
| 355 | } | 
|---|
| 356 |  | 
|---|
| 357 | thread_ipc_req[thread_id] = question; | 
|---|
| 358 | } | 
|---|
| 359 |  | 
|---|
| 360 | static void sc_ipc_call_sync_slow_e(unsigned thread_id, sysarg_t *sc_args) | 
|---|
| 361 | { | 
|---|
| 362 | ipc_call_t reply; | 
|---|
| 363 | int rc; | 
|---|
| 364 |  | 
|---|
| 365 | memset(&reply, 0, sizeof(reply)); | 
|---|
| 366 | rc = udebug_mem_read(sess, &reply.args, sc_args[2], | 
|---|
| 367 | sizeof(reply.args)); | 
|---|
| 368 |  | 
|---|
| 369 | if (rc < 0) { | 
|---|
| 370 | printf("Error: mem_read->%d\n", rc); | 
|---|
| 371 | return; | 
|---|
| 372 | } | 
|---|
| 373 |  | 
|---|
| 374 | ipcp_call_sync(sc_args[0], &thread_ipc_req[thread_id], &reply); | 
|---|
| 375 | } | 
|---|
| 376 |  | 
|---|
| 377 | static void sc_ipc_wait(sysarg_t *sc_args, int sc_rc) | 
|---|
| 378 | { | 
|---|
| 379 | ipc_call_t call; | 
|---|
| 380 | int rc; | 
|---|
| 381 |  | 
|---|
| 382 | if (sc_rc == 0) return; | 
|---|
| 383 |  | 
|---|
| 384 | memset(&call, 0, sizeof(call)); | 
|---|
| 385 | rc = udebug_mem_read(sess, &call, sc_args[0], sizeof(call)); | 
|---|
| 386 |  | 
|---|
| 387 | if (rc >= 0) | 
|---|
| 388 | ipcp_call_in(&call, sc_rc); | 
|---|
| 389 | } | 
|---|
| 390 |  | 
|---|
| 391 | static void event_syscall_b(unsigned thread_id, uintptr_t thread_hash, | 
|---|
| 392 | unsigned sc_id, sysarg_t sc_rc) | 
|---|
| 393 | { | 
|---|
| 394 | sysarg_t sc_args[6]; | 
|---|
| 395 | int rc; | 
|---|
| 396 |  | 
|---|
| 397 | /* Read syscall arguments */ | 
|---|
| 398 | rc = udebug_args_read(sess, thread_hash, sc_args); | 
|---|
| 399 |  | 
|---|
| 400 | if (rc < 0) { | 
|---|
| 401 | printf("error\n"); | 
|---|
| 402 | return; | 
|---|
| 403 | } | 
|---|
| 404 |  | 
|---|
| 405 | if ((display_mask & DM_SYSCALL) != 0) { | 
|---|
| 406 | /* Print syscall name and arguments */ | 
|---|
| 407 | printf("%s", syscall_desc[sc_id].name); | 
|---|
| 408 | print_sc_args(sc_args, syscall_desc[sc_id].n_args); | 
|---|
| 409 | } | 
|---|
| 410 |  | 
|---|
| 411 | switch (sc_id) { | 
|---|
| 412 | case SYS_IPC_CALL_SYNC_SLOW: | 
|---|
| 413 | sc_ipc_call_sync_slow_b(thread_id, sc_args); | 
|---|
| 414 | break; | 
|---|
| 415 | default: | 
|---|
| 416 | break; | 
|---|
| 417 | } | 
|---|
| 418 | } | 
|---|
| 419 |  | 
|---|
| 420 | static void event_syscall_e(unsigned thread_id, uintptr_t thread_hash, | 
|---|
| 421 | unsigned sc_id, sysarg_t sc_rc) | 
|---|
| 422 | { | 
|---|
| 423 | sysarg_t sc_args[6]; | 
|---|
| 424 | int rv_type; | 
|---|
| 425 | int rc; | 
|---|
| 426 |  | 
|---|
| 427 | /* Read syscall arguments */ | 
|---|
| 428 | rc = udebug_args_read(sess, thread_hash, sc_args); | 
|---|
| 429 |  | 
|---|
| 430 | //      printf("[%d] ", thread_id); | 
|---|
| 431 |  | 
|---|
| 432 | if (rc < 0) { | 
|---|
| 433 | printf("error\n"); | 
|---|
| 434 | return; | 
|---|
| 435 | } | 
|---|
| 436 |  | 
|---|
| 437 | if ((display_mask & DM_SYSCALL) != 0) { | 
|---|
| 438 | /* Print syscall return value */ | 
|---|
| 439 | rv_type = syscall_desc[sc_id].rv_type; | 
|---|
| 440 | print_sc_retval(sc_rc, rv_type); | 
|---|
| 441 | } | 
|---|
| 442 |  | 
|---|
| 443 | switch (sc_id) { | 
|---|
| 444 | case SYS_IPC_CALL_ASYNC_FAST: | 
|---|
| 445 | sc_ipc_call_async_fast(sc_args, sc_rc); | 
|---|
| 446 | break; | 
|---|
| 447 | case SYS_IPC_CALL_ASYNC_SLOW: | 
|---|
| 448 | sc_ipc_call_async_slow(sc_args, sc_rc); | 
|---|
| 449 | break; | 
|---|
| 450 | case SYS_IPC_CALL_SYNC_FAST: | 
|---|
| 451 | sc_ipc_call_sync_fast(sc_args); | 
|---|
| 452 | break; | 
|---|
| 453 | case SYS_IPC_CALL_SYNC_SLOW: | 
|---|
| 454 | sc_ipc_call_sync_slow_e(thread_id, sc_args); | 
|---|
| 455 | break; | 
|---|
| 456 | case SYS_IPC_WAIT: | 
|---|
| 457 | sc_ipc_wait(sc_args, sc_rc); | 
|---|
| 458 | break; | 
|---|
| 459 | default: | 
|---|
| 460 | break; | 
|---|
| 461 | } | 
|---|
| 462 | } | 
|---|
| 463 |  | 
|---|
| 464 | static void event_thread_b(uintptr_t hash) | 
|---|
| 465 | { | 
|---|
| 466 | printf("New thread, hash %p\n", (void *) hash); | 
|---|
| 467 | thread_trace_start(hash); | 
|---|
| 468 | } | 
|---|
| 469 |  | 
|---|
| 470 | static int trace_loop(void *thread_hash_arg) | 
|---|
| 471 | { | 
|---|
| 472 | int rc; | 
|---|
| 473 | unsigned ev_type; | 
|---|
| 474 | uintptr_t thread_hash; | 
|---|
| 475 | unsigned thread_id; | 
|---|
| 476 | sysarg_t val0, val1; | 
|---|
| 477 |  | 
|---|
| 478 | thread_hash = (uintptr_t)thread_hash_arg; | 
|---|
| 479 | thread_id = next_thread_id++; | 
|---|
| 480 | if (thread_id >= THBUF_SIZE) { | 
|---|
| 481 | printf("Too many threads.\n"); | 
|---|
| 482 | return ELIMIT; | 
|---|
| 483 | } | 
|---|
| 484 |  | 
|---|
| 485 | printf("Start tracing thread [%u] (hash %p).\n", | 
|---|
| 486 | thread_id, (void *) thread_hash); | 
|---|
| 487 |  | 
|---|
| 488 | while (!abort_trace) { | 
|---|
| 489 |  | 
|---|
| 490 | fibril_mutex_lock(&state_lock); | 
|---|
| 491 | if (paused) { | 
|---|
| 492 | printf("Thread [%u] paused. Press R to resume.\n", | 
|---|
| 493 | thread_id); | 
|---|
| 494 |  | 
|---|
| 495 | while (paused) | 
|---|
| 496 | fibril_condvar_wait(&state_cv, &state_lock); | 
|---|
| 497 |  | 
|---|
| 498 | printf("Thread [%u] resumed.\n", thread_id); | 
|---|
| 499 | } | 
|---|
| 500 | fibril_mutex_unlock(&state_lock); | 
|---|
| 501 |  | 
|---|
| 502 | /* Run thread until an event occurs */ | 
|---|
| 503 | rc = udebug_go(sess, thread_hash, | 
|---|
| 504 | &ev_type, &val0, &val1); | 
|---|
| 505 |  | 
|---|
| 506 | //              printf("rc = %d, ev_type=%d\n", rc, ev_type); | 
|---|
| 507 | if (ev_type == UDEBUG_EVENT_FINISHED) { | 
|---|
| 508 | /* Done tracing this thread */ | 
|---|
| 509 | break; | 
|---|
| 510 | } | 
|---|
| 511 |  | 
|---|
| 512 | if (rc >= 0) { | 
|---|
| 513 | switch (ev_type) { | 
|---|
| 514 | case UDEBUG_EVENT_SYSCALL_B: | 
|---|
| 515 | event_syscall_b(thread_id, thread_hash, val0, (int)val1); | 
|---|
| 516 | break; | 
|---|
| 517 | case UDEBUG_EVENT_SYSCALL_E: | 
|---|
| 518 | event_syscall_e(thread_id, thread_hash, val0, (int)val1); | 
|---|
| 519 | break; | 
|---|
| 520 | case UDEBUG_EVENT_STOP: | 
|---|
| 521 | printf("Stop event\n"); | 
|---|
| 522 | fibril_mutex_lock(&state_lock); | 
|---|
| 523 | paused = true; | 
|---|
| 524 | fibril_mutex_unlock(&state_lock); | 
|---|
| 525 | break; | 
|---|
| 526 | case UDEBUG_EVENT_THREAD_B: | 
|---|
| 527 | event_thread_b(val0); | 
|---|
| 528 | break; | 
|---|
| 529 | case UDEBUG_EVENT_THREAD_E: | 
|---|
| 530 | printf("Thread %" PRIun " exited.\n", val0); | 
|---|
| 531 | fibril_mutex_lock(&state_lock); | 
|---|
| 532 | abort_trace = true; | 
|---|
| 533 | fibril_condvar_broadcast(&state_cv); | 
|---|
| 534 | fibril_mutex_unlock(&state_lock); | 
|---|
| 535 | break; | 
|---|
| 536 | default: | 
|---|
| 537 | printf("Unknown event type %d.\n", ev_type); | 
|---|
| 538 | break; | 
|---|
| 539 | } | 
|---|
| 540 | } | 
|---|
| 541 |  | 
|---|
| 542 | } | 
|---|
| 543 |  | 
|---|
| 544 | printf("Finished tracing thread [%d].\n", thread_id); | 
|---|
| 545 | return 0; | 
|---|
| 546 | } | 
|---|
| 547 |  | 
|---|
| 548 | void thread_trace_start(uintptr_t thread_hash) | 
|---|
| 549 | { | 
|---|
| 550 | fid_t fid; | 
|---|
| 551 |  | 
|---|
| 552 | thash = thread_hash; | 
|---|
| 553 |  | 
|---|
| 554 | fid = fibril_create(trace_loop, (void *)thread_hash); | 
|---|
| 555 | if (fid == 0) { | 
|---|
| 556 | printf("Warning: Failed creating fibril\n"); | 
|---|
| 557 | } | 
|---|
| 558 | fibril_add_ready(fid); | 
|---|
| 559 | } | 
|---|
| 560 |  | 
|---|
| 561 | static loader_t *preload_task(const char *path, char **argv, | 
|---|
| 562 | task_id_t *task_id) | 
|---|
| 563 | { | 
|---|
| 564 | loader_t *ldr; | 
|---|
| 565 | int rc; | 
|---|
| 566 |  | 
|---|
| 567 | /* Spawn a program loader */ | 
|---|
| 568 | ldr = loader_connect(); | 
|---|
| 569 | if (ldr == NULL) | 
|---|
| 570 | return 0; | 
|---|
| 571 |  | 
|---|
| 572 | /* Get task ID. */ | 
|---|
| 573 | rc = loader_get_task_id(ldr, task_id); | 
|---|
| 574 | if (rc != EOK) | 
|---|
| 575 | goto error; | 
|---|
| 576 |  | 
|---|
| 577 | /* Send program pathname */ | 
|---|
| 578 | rc = loader_set_pathname(ldr, path); | 
|---|
| 579 | if (rc != EOK) | 
|---|
| 580 | goto error; | 
|---|
| 581 |  | 
|---|
| 582 | /* Send arguments */ | 
|---|
| 583 | rc = loader_set_args(ldr, (const char **) argv); | 
|---|
| 584 | if (rc != EOK) | 
|---|
| 585 | goto error; | 
|---|
| 586 |  | 
|---|
| 587 | /* Send default files */ | 
|---|
| 588 | fdi_node_t *files[4]; | 
|---|
| 589 | fdi_node_t stdin_node; | 
|---|
| 590 | fdi_node_t stdout_node; | 
|---|
| 591 | fdi_node_t stderr_node; | 
|---|
| 592 |  | 
|---|
| 593 | if ((stdin != NULL) && (fnode(stdin, &stdin_node) == EOK)) | 
|---|
| 594 | files[0] = &stdin_node; | 
|---|
| 595 | else | 
|---|
| 596 | files[0] = NULL; | 
|---|
| 597 |  | 
|---|
| 598 | if ((stdout != NULL) && (fnode(stdout, &stdout_node) == EOK)) | 
|---|
| 599 | files[1] = &stdout_node; | 
|---|
| 600 | else | 
|---|
| 601 | files[1] = NULL; | 
|---|
| 602 |  | 
|---|
| 603 | if ((stderr != NULL) && (fnode(stderr, &stderr_node) == EOK)) | 
|---|
| 604 | files[2] = &stderr_node; | 
|---|
| 605 | else | 
|---|
| 606 | files[2] = NULL; | 
|---|
| 607 |  | 
|---|
| 608 | files[3] = NULL; | 
|---|
| 609 |  | 
|---|
| 610 | rc = loader_set_files(ldr, files); | 
|---|
| 611 | if (rc != EOK) | 
|---|
| 612 | goto error; | 
|---|
| 613 |  | 
|---|
| 614 | /* Load the program. */ | 
|---|
| 615 | rc = loader_load_program(ldr); | 
|---|
| 616 | if (rc != EOK) | 
|---|
| 617 | goto error; | 
|---|
| 618 |  | 
|---|
| 619 | /* Success */ | 
|---|
| 620 | return ldr; | 
|---|
| 621 |  | 
|---|
| 622 | /* Error exit */ | 
|---|
| 623 | error: | 
|---|
| 624 | loader_abort(ldr); | 
|---|
| 625 | free(ldr); | 
|---|
| 626 | return NULL; | 
|---|
| 627 | } | 
|---|
| 628 |  | 
|---|
| 629 | static int cev_fibril(void *arg) | 
|---|
| 630 | { | 
|---|
| 631 | (void) arg; | 
|---|
| 632 |  | 
|---|
| 633 | console_ctrl_t *console = console_init(stdin, stdout); | 
|---|
| 634 |  | 
|---|
| 635 | while (true) { | 
|---|
| 636 | fibril_mutex_lock(&state_lock); | 
|---|
| 637 | while (cev_valid) | 
|---|
| 638 | fibril_condvar_wait(&state_cv, &state_lock); | 
|---|
| 639 | fibril_mutex_unlock(&state_lock); | 
|---|
| 640 |  | 
|---|
| 641 | if (!console_get_kbd_event(console, &cev)) | 
|---|
| 642 | return -1; | 
|---|
| 643 |  | 
|---|
| 644 | fibril_mutex_lock(&state_lock); | 
|---|
| 645 | cev_valid = true; | 
|---|
| 646 | fibril_condvar_broadcast(&state_cv); | 
|---|
| 647 | fibril_mutex_unlock(&state_lock); | 
|---|
| 648 | } | 
|---|
| 649 | } | 
|---|
| 650 |  | 
|---|
| 651 | static void trace_task(task_id_t task_id) | 
|---|
| 652 | { | 
|---|
| 653 | kbd_event_t ev; | 
|---|
| 654 | bool done; | 
|---|
| 655 | int i; | 
|---|
| 656 | int rc; | 
|---|
| 657 |  | 
|---|
| 658 | ipcp_init(); | 
|---|
| 659 |  | 
|---|
| 660 | /* | 
|---|
| 661 | * User apps now typically have console on phone 3. | 
|---|
| 662 | * (Phones 1 and 2 are used by the loader). | 
|---|
| 663 | */ | 
|---|
| 664 | ipcp_connection_set(3, 0, proto_console); | 
|---|
| 665 |  | 
|---|
| 666 | rc = get_thread_list(); | 
|---|
| 667 | if (rc < 0) { | 
|---|
| 668 | printf("Failed to get thread list (error %d)\n", rc); | 
|---|
| 669 | return; | 
|---|
| 670 | } | 
|---|
| 671 |  | 
|---|
| 672 | abort_trace = false; | 
|---|
| 673 |  | 
|---|
| 674 | for (i = 0; i < n_threads; i++) { | 
|---|
| 675 | thread_trace_start(thread_hash_buf[i]); | 
|---|
| 676 | } | 
|---|
| 677 |  | 
|---|
| 678 | done = false; | 
|---|
| 679 |  | 
|---|
| 680 | while (!done) { | 
|---|
| 681 | fibril_mutex_lock(&state_lock); | 
|---|
| 682 | while (!cev_valid && !abort_trace) | 
|---|
| 683 | fibril_condvar_wait(&state_cv, &state_lock); | 
|---|
| 684 | fibril_mutex_unlock(&state_lock); | 
|---|
| 685 |  | 
|---|
| 686 | ev = cev; | 
|---|
| 687 |  | 
|---|
| 688 | fibril_mutex_lock(&state_lock); | 
|---|
| 689 | cev_valid = false; | 
|---|
| 690 | fibril_condvar_broadcast(&state_cv); | 
|---|
| 691 | fibril_mutex_unlock(&state_lock); | 
|---|
| 692 |  | 
|---|
| 693 | if (abort_trace) | 
|---|
| 694 | break; | 
|---|
| 695 |  | 
|---|
| 696 | if (ev.type != KEY_PRESS) | 
|---|
| 697 | continue; | 
|---|
| 698 |  | 
|---|
| 699 | switch (ev.key) { | 
|---|
| 700 | case KC_Q: | 
|---|
| 701 | done = true; | 
|---|
| 702 | break; | 
|---|
| 703 | case KC_P: | 
|---|
| 704 | printf("Pause...\n"); | 
|---|
| 705 | rc = udebug_stop(sess, thash); | 
|---|
| 706 | if (rc != EOK) | 
|---|
| 707 | printf("Error: stop -> %d\n", rc); | 
|---|
| 708 | break; | 
|---|
| 709 | case KC_R: | 
|---|
| 710 | fibril_mutex_lock(&state_lock); | 
|---|
| 711 | paused = false; | 
|---|
| 712 | fibril_condvar_broadcast(&state_cv); | 
|---|
| 713 | fibril_mutex_unlock(&state_lock); | 
|---|
| 714 | printf("Resume...\n"); | 
|---|
| 715 | break; | 
|---|
| 716 | } | 
|---|
| 717 | } | 
|---|
| 718 |  | 
|---|
| 719 | printf("\nTerminate debugging session...\n"); | 
|---|
| 720 | abort_trace = true; | 
|---|
| 721 | udebug_end(sess); | 
|---|
| 722 | async_hangup(sess); | 
|---|
| 723 |  | 
|---|
| 724 | ipcp_cleanup(); | 
|---|
| 725 |  | 
|---|
| 726 | printf("Done\n"); | 
|---|
| 727 | return; | 
|---|
| 728 | } | 
|---|
| 729 |  | 
|---|
| 730 | static void main_init(void) | 
|---|
| 731 | { | 
|---|
| 732 | proto_t *p; | 
|---|
| 733 | oper_t *o; | 
|---|
| 734 |  | 
|---|
| 735 | val_type_t arg_def[OPER_MAX_ARGS] = { | 
|---|
| 736 | V_INTEGER, | 
|---|
| 737 | V_INTEGER, | 
|---|
| 738 | V_INTEGER, | 
|---|
| 739 | V_INTEGER, | 
|---|
| 740 | V_INTEGER | 
|---|
| 741 | }; | 
|---|
| 742 |  | 
|---|
| 743 | val_type_t resp_def[OPER_MAX_ARGS] = { | 
|---|
| 744 | V_INTEGER, | 
|---|
| 745 | V_INTEGER, | 
|---|
| 746 | V_INTEGER, | 
|---|
| 747 | V_INTEGER, | 
|---|
| 748 | V_INTEGER | 
|---|
| 749 | }; | 
|---|
| 750 |  | 
|---|
| 751 | next_thread_id = 1; | 
|---|
| 752 | paused = false; | 
|---|
| 753 | cev_valid = false; | 
|---|
| 754 |  | 
|---|
| 755 | fibril_mutex_initialize(&state_lock); | 
|---|
| 756 | fibril_condvar_initialize(&state_cv); | 
|---|
| 757 |  | 
|---|
| 758 | proto_init(); | 
|---|
| 759 |  | 
|---|
| 760 | p = proto_new("vfs"); | 
|---|
| 761 | o = oper_new("open", 2, arg_def, V_INT_ERRNO, 0, resp_def); | 
|---|
| 762 | proto_add_oper(p, VFS_IN_OPEN, o); | 
|---|
| 763 | o = oper_new("open_node", 4, arg_def, V_INT_ERRNO, 0, resp_def); | 
|---|
| 764 | proto_add_oper(p, VFS_IN_OPEN_NODE, o); | 
|---|
| 765 | o = oper_new("read", 1, arg_def, V_ERRNO, 1, resp_def); | 
|---|
| 766 | proto_add_oper(p, VFS_IN_READ, o); | 
|---|
| 767 | o = oper_new("write", 1, arg_def, V_ERRNO, 1, resp_def); | 
|---|
| 768 | proto_add_oper(p, VFS_IN_WRITE, o); | 
|---|
| 769 | o = oper_new("seek", 3, arg_def, V_ERRNO, 0, resp_def); | 
|---|
| 770 | proto_add_oper(p, VFS_IN_SEEK, o); | 
|---|
| 771 | o = oper_new("truncate", 5, arg_def, V_ERRNO, 0, resp_def); | 
|---|
| 772 | proto_add_oper(p, VFS_IN_TRUNCATE, o); | 
|---|
| 773 | o = oper_new("fstat", 1, arg_def, V_ERRNO, 0, resp_def); | 
|---|
| 774 | proto_add_oper(p, VFS_IN_FSTAT, o); | 
|---|
| 775 | o = oper_new("close", 1, arg_def, V_ERRNO, 0, resp_def); | 
|---|
| 776 | proto_add_oper(p, VFS_IN_CLOSE, o); | 
|---|
| 777 | o = oper_new("mount", 2, arg_def, V_ERRNO, 0, resp_def); | 
|---|
| 778 | proto_add_oper(p, VFS_IN_MOUNT, o); | 
|---|
| 779 | /*      o = oper_new("unmount", 0, arg_def); | 
|---|
| 780 | proto_add_oper(p, VFS_IN_UNMOUNT, o);*/ | 
|---|
| 781 | o = oper_new("sync", 1, arg_def, V_ERRNO, 0, resp_def); | 
|---|
| 782 | proto_add_oper(p, VFS_IN_SYNC, o); | 
|---|
| 783 | o = oper_new("mkdir", 1, arg_def, V_ERRNO, 0, resp_def); | 
|---|
| 784 | proto_add_oper(p, VFS_IN_MKDIR, o); | 
|---|
| 785 | o = oper_new("unlink", 0, arg_def, V_ERRNO, 0, resp_def); | 
|---|
| 786 | proto_add_oper(p, VFS_IN_UNLINK, o); | 
|---|
| 787 | o = oper_new("rename", 0, arg_def, V_ERRNO, 0, resp_def); | 
|---|
| 788 | proto_add_oper(p, VFS_IN_RENAME, o); | 
|---|
| 789 | o = oper_new("stat", 0, arg_def, V_ERRNO, 0, resp_def); | 
|---|
| 790 | proto_add_oper(p, VFS_IN_STAT, o); | 
|---|
| 791 |  | 
|---|
| 792 | proto_register(SERVICE_VFS, p); | 
|---|
| 793 |  | 
|---|
| 794 | p = proto_new("console"); | 
|---|
| 795 |  | 
|---|
| 796 | o = oper_new("write", 1, arg_def, V_ERRNO, 1, resp_def); | 
|---|
| 797 | proto_add_oper(p, VFS_IN_WRITE, o); | 
|---|
| 798 |  | 
|---|
| 799 | resp_def[0] = V_INTEGER; resp_def[1] = V_INTEGER; | 
|---|
| 800 | resp_def[2] = V_INTEGER; resp_def[3] = V_CHAR; | 
|---|
| 801 | o = oper_new("getkey", 0, arg_def, V_ERRNO, 4, resp_def); | 
|---|
| 802 |  | 
|---|
| 803 | arg_def[0] = V_CHAR; | 
|---|
| 804 | o = oper_new("clear", 0, arg_def, V_VOID, 0, resp_def); | 
|---|
| 805 | proto_add_oper(p, CONSOLE_CLEAR, o); | 
|---|
| 806 |  | 
|---|
| 807 | arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER; | 
|---|
| 808 | o = oper_new("goto", 2, arg_def, V_VOID, 0, resp_def); | 
|---|
| 809 | proto_add_oper(p, CONSOLE_GOTO, o); | 
|---|
| 810 |  | 
|---|
| 811 | resp_def[0] = V_INTEGER; resp_def[1] = V_INTEGER; | 
|---|
| 812 | o = oper_new("getsize", 0, arg_def, V_INTEGER, 2, resp_def); | 
|---|
| 813 | proto_add_oper(p, CONSOLE_GET_SIZE, o); | 
|---|
| 814 |  | 
|---|
| 815 | arg_def[0] = V_INTEGER; | 
|---|
| 816 | o = oper_new("set_style", 1, arg_def, V_VOID, 0, resp_def); | 
|---|
| 817 | proto_add_oper(p, CONSOLE_SET_STYLE, o); | 
|---|
| 818 | arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER; arg_def[2] = V_INTEGER; | 
|---|
| 819 | o = oper_new("set_color", 3, arg_def, V_VOID, 0, resp_def); | 
|---|
| 820 | proto_add_oper(p, CONSOLE_SET_COLOR, o); | 
|---|
| 821 | arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER; | 
|---|
| 822 | o = oper_new("set_rgb_color", 2, arg_def, V_VOID, 0, resp_def); | 
|---|
| 823 | proto_add_oper(p, CONSOLE_SET_RGB_COLOR, o); | 
|---|
| 824 | o = oper_new("cursor_visibility", 1, arg_def, V_VOID, 0, resp_def); | 
|---|
| 825 | proto_add_oper(p, CONSOLE_CURSOR_VISIBILITY, o); | 
|---|
| 826 |  | 
|---|
| 827 | proto_console = p; | 
|---|
| 828 | proto_register(SERVICE_CONSOLE, p); | 
|---|
| 829 | } | 
|---|
| 830 |  | 
|---|
| 831 | static void print_syntax() | 
|---|
| 832 | { | 
|---|
| 833 | printf("Syntax:\n"); | 
|---|
| 834 | printf("\ttrace [+<events>] <executable> [<arg1> [...]]\n"); | 
|---|
| 835 | printf("or\ttrace [+<events>] -t <task_id>\n"); | 
|---|
| 836 | printf("Events: (default is +tp)\n"); | 
|---|
| 837 | printf("\n"); | 
|---|
| 838 | printf("\tt ... Thread creation and termination\n"); | 
|---|
| 839 | printf("\ts ... System calls\n"); | 
|---|
| 840 | printf("\ti ... Low-level IPC\n"); | 
|---|
| 841 | printf("\tp ... Protocol level\n"); | 
|---|
| 842 | printf("\n"); | 
|---|
| 843 | printf("Examples:\n"); | 
|---|
| 844 | printf("\ttrace +s /app/tetris\n"); | 
|---|
| 845 | printf("\ttrace +tsip -t 12\n"); | 
|---|
| 846 | } | 
|---|
| 847 |  | 
|---|
| 848 | static display_mask_t parse_display_mask(const char *text) | 
|---|
| 849 | { | 
|---|
| 850 | display_mask_t dm = 0; | 
|---|
| 851 | const char *c = text; | 
|---|
| 852 |  | 
|---|
| 853 | while (*c) { | 
|---|
| 854 | switch (*c) { | 
|---|
| 855 | case 't': | 
|---|
| 856 | dm = dm | DM_THREAD; | 
|---|
| 857 | break; | 
|---|
| 858 | case 's': | 
|---|
| 859 | dm = dm | DM_SYSCALL; | 
|---|
| 860 | break; | 
|---|
| 861 | case 'i': | 
|---|
| 862 | dm = dm | DM_IPC; | 
|---|
| 863 | break; | 
|---|
| 864 | case 'p': | 
|---|
| 865 | dm = dm | DM_SYSTEM | DM_USER; | 
|---|
| 866 | break; | 
|---|
| 867 | default: | 
|---|
| 868 | printf("Unexpected event type '%c'.\n", *c); | 
|---|
| 869 | exit(1); | 
|---|
| 870 | } | 
|---|
| 871 |  | 
|---|
| 872 | ++c; | 
|---|
| 873 | } | 
|---|
| 874 |  | 
|---|
| 875 | return dm; | 
|---|
| 876 | } | 
|---|
| 877 |  | 
|---|
| 878 | static int parse_args(int argc, char *argv[]) | 
|---|
| 879 | { | 
|---|
| 880 | char *err_p; | 
|---|
| 881 |  | 
|---|
| 882 | task_id = 0; | 
|---|
| 883 |  | 
|---|
| 884 | --argc; | 
|---|
| 885 | ++argv; | 
|---|
| 886 |  | 
|---|
| 887 | while (argc > 0) { | 
|---|
| 888 | char *arg = *argv; | 
|---|
| 889 | if (arg[0] == '+') { | 
|---|
| 890 | display_mask = parse_display_mask(&arg[1]); | 
|---|
| 891 | } else if (arg[0] == '-') { | 
|---|
| 892 | if (arg[1] == 't') { | 
|---|
| 893 | /* Trace an already running task */ | 
|---|
| 894 | --argc; | 
|---|
| 895 | ++argv; | 
|---|
| 896 | task_id = strtol(*argv, &err_p, 10); | 
|---|
| 897 | task_ldr = NULL; | 
|---|
| 898 | task_wait_for = false; | 
|---|
| 899 | if (*err_p) { | 
|---|
| 900 | printf("Task ID syntax error\n"); | 
|---|
| 901 | print_syntax(); | 
|---|
| 902 | return -1; | 
|---|
| 903 | } | 
|---|
| 904 | } else { | 
|---|
| 905 | printf("Uknown option '%c'\n", arg[0]); | 
|---|
| 906 | print_syntax(); | 
|---|
| 907 | return -1; | 
|---|
| 908 | } | 
|---|
| 909 | } else { | 
|---|
| 910 | break; | 
|---|
| 911 | } | 
|---|
| 912 |  | 
|---|
| 913 | --argc; | 
|---|
| 914 | ++argv; | 
|---|
| 915 | } | 
|---|
| 916 |  | 
|---|
| 917 | if (task_id != 0) { | 
|---|
| 918 | if (argc == 0) | 
|---|
| 919 | return 0; | 
|---|
| 920 | printf("Extra arguments\n"); | 
|---|
| 921 | print_syntax(); | 
|---|
| 922 | return -1; | 
|---|
| 923 | } | 
|---|
| 924 |  | 
|---|
| 925 | if (argc < 1) { | 
|---|
| 926 | printf("Missing argument\n"); | 
|---|
| 927 | print_syntax(); | 
|---|
| 928 | return -1; | 
|---|
| 929 | } | 
|---|
| 930 |  | 
|---|
| 931 | /* Preload the specified program file. */ | 
|---|
| 932 | printf("Spawning '%s' with arguments:\n", *argv); | 
|---|
| 933 |  | 
|---|
| 934 | char **cp = argv; | 
|---|
| 935 | while (*cp) | 
|---|
| 936 | printf("'%s'\n", *cp++); | 
|---|
| 937 |  | 
|---|
| 938 | task_ldr = preload_task(*argv, argv, &task_id); | 
|---|
| 939 | task_wait_for = true; | 
|---|
| 940 |  | 
|---|
| 941 | return 0; | 
|---|
| 942 | } | 
|---|
| 943 |  | 
|---|
| 944 | int main(int argc, char *argv[]) | 
|---|
| 945 | { | 
|---|
| 946 | int rc; | 
|---|
| 947 | task_exit_t texit; | 
|---|
| 948 | int retval; | 
|---|
| 949 |  | 
|---|
| 950 | printf("System Call / IPC Tracer\n"); | 
|---|
| 951 | printf("Controls: Q - Quit, P - Pause, R - Resume\n"); | 
|---|
| 952 |  | 
|---|
| 953 | display_mask = DM_THREAD | DM_SYSTEM | DM_USER; | 
|---|
| 954 |  | 
|---|
| 955 | if (parse_args(argc, argv) < 0) | 
|---|
| 956 | return 1; | 
|---|
| 957 |  | 
|---|
| 958 | main_init(); | 
|---|
| 959 |  | 
|---|
| 960 | rc = connect_task(task_id); | 
|---|
| 961 | if (rc < 0) { | 
|---|
| 962 | printf("Failed connecting to task %" PRIu64 ".\n", task_id); | 
|---|
| 963 | return 1; | 
|---|
| 964 | } | 
|---|
| 965 |  | 
|---|
| 966 | printf("Connected to task %" PRIu64 ".\n", task_id); | 
|---|
| 967 |  | 
|---|
| 968 | if (task_ldr != NULL) | 
|---|
| 969 | program_run(); | 
|---|
| 970 |  | 
|---|
| 971 | cev_fibril_start(); | 
|---|
| 972 | trace_task(task_id); | 
|---|
| 973 |  | 
|---|
| 974 | if (task_wait_for) { | 
|---|
| 975 | printf("Waiting for task to exit.\n"); | 
|---|
| 976 |  | 
|---|
| 977 | rc = task_wait(task_id, &texit, &retval); | 
|---|
| 978 | if (rc != EOK) { | 
|---|
| 979 | printf("Failed waiting for task.\n"); | 
|---|
| 980 | return -1; | 
|---|
| 981 | } | 
|---|
| 982 |  | 
|---|
| 983 | if (texit == TASK_EXIT_NORMAL) { | 
|---|
| 984 | printf("Task exited normally, return value %d.\n", | 
|---|
| 985 | retval); | 
|---|
| 986 | } else { | 
|---|
| 987 | printf("Task exited unexpectedly.\n"); | 
|---|
| 988 | } | 
|---|
| 989 | } | 
|---|
| 990 |  | 
|---|
| 991 | return 0; | 
|---|
| 992 | } | 
|---|
| 993 |  | 
|---|
| 994 | /** @} | 
|---|
| 995 | */ | 
|---|