source: mainline/uspace/app/trace/trace.c@ 24345a5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 24345a5 was 24345a5, checked in by Jiri Svoboda <jirik.svoboda@…>, 17 years ago

Set meaningful names for loaded programs. Now 'tasks' kconsole command is much less obscure.

  • Property mode set to 100644
File size: 16.7 KB
RevLine 
[9a1b20c]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 <ipc/ipc.h>
39#include <fibril.h>
40#include <errno.h>
41#include <udebug.h>
42#include <async.h>
[47e0a05b]43#include <task.h>
[4470e26]44#include <loader/loader.h>
[9a1b20c]45
[f2ef7fd]46#include <libc.h>
47
[9a1b20c]48// Temporary: service and method names
49#include "proto.h"
50#include <ipc/services.h>
51#include "../../srv/vfs/vfs.h"
52#include "../../srv/console/console.h"
53
54#include "syscalls.h"
55#include "ipcp.h"
56#include "errors.h"
[1643855]57#include "trace.h"
[9a1b20c]58
59#define THBUF_SIZE 64
[a5c3f73]60uintptr_t thread_hash_buf[THBUF_SIZE];
61int n_threads;
[9a1b20c]62
63int next_thread_id;
64
65int phoneid;
66int abort_trace;
67
[a5c3f73]68uintptr_t thash;
[9a1b20c]69volatile int paused;
70
[a5c3f73]71void thread_trace_start(uintptr_t thread_hash);
[9a1b20c]72
73static proto_t *proto_console;
[1643855]74static task_id_t task_id;
[4470e26]75static loader_t *task_ldr;
[1643855]76
77/** Combination of events/data to print. */
78display_mask_t display_mask;
[9a1b20c]79
[4470e26]80static int program_run_fibril(void *arg);
81
82static void program_run(void)
83{
84 fid_t fid;
85
86 fid = fibril_create(program_run_fibril, NULL);
87 if (fid == 0) {
88 printf("Error creating fibril\n");
89 exit(1);
90 }
91
92 fibril_add_ready(fid);
93}
94
95static int program_run_fibril(void *arg)
96{
97 int rc;
98
99 /*
100 * This must be done in background as it will block until
101 * we let the task reply to this call.
102 */
103 rc = loader_run(task_ldr);
104 if (rc != 0) {
105 printf("Error running program\n");
106 exit(1);
107 }
108
109 free(task_ldr);
110 task_ldr = NULL;
111
112 printf("program_run_fibril exiting\n");
113 return 0;
114}
115
116
117static int connect_task(task_id_t task_id)
[9a1b20c]118{
119 int rc;
120
121 rc = ipc_connect_kbox(task_id);
[fb9b0b0]122
123 if (rc == ENOTSUP) {
124 printf("You do not have userspace debugging support "
125 "compiled in the kernel.\n");
126 printf("Compile kernel with 'Support for userspace debuggers' "
127 "(CONFIG_UDEBUG) enabled.\n");
[c9a29d6]128 return rc;
129 }
130
131 if (rc < 0) {
132 printf("Error connecting\n");
133 printf("ipc_connect_task(%lld) -> %d ", task_id, rc);
134 return rc;
[fb9b0b0]135 }
136
[9a1b20c]137 phoneid = rc;
138
139 rc = udebug_begin(phoneid);
[c9a29d6]140 if (rc < 0) {
141 printf("udebug_begin() -> %d\n", rc);
142 return rc;
143 }
[9a1b20c]144
145 rc = udebug_set_evmask(phoneid, UDEBUG_EM_ALL);
[c9a29d6]146 if (rc < 0) {
147 printf("udebug_set_evmask(0x%x) -> %d\n ", UDEBUG_EM_ALL, rc);
148 return rc;
149 }
[9a1b20c]150
151 return 0;
152}
153
154static int get_thread_list(void)
155{
156 int rc;
157 size_t tb_copied;
158 size_t tb_needed;
159 int i;
160
161 rc = udebug_thread_read(phoneid, thread_hash_buf,
162 THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed);
[c9a29d6]163 if (rc < 0) {
164 printf("udebug_thread_read() -> %d\n", rc);
165 return rc;
166 }
[9a1b20c]167
[a5c3f73]168 n_threads = tb_copied / sizeof(uintptr_t);
[9a1b20c]169
[c9a29d6]170 printf("Threads:");
171 for (i = 0; i < n_threads; i++) {
[a5c3f73]172 printf(" [%d] (hash 0x%lx)", 1+i, thread_hash_buf[i]);
[9a1b20c]173 }
[a5c3f73]174 printf("\ntotal of %u threads\n", tb_needed / sizeof(uintptr_t));
[9a1b20c]175
176 return 0;
177}
178
[356acd0]179void val_print(sysarg_t val, val_type_t v_type)
[9a1b20c]180{
[abf3564]181 switch (v_type) {
182 case V_VOID:
183 printf("<void>");
184 break;
185
186 case V_INTEGER:
[356acd0]187 printf("%ld", val);
[abf3564]188 break;
189
190 case V_HASH:
[4470e26]191 case V_PTR:
[356acd0]192 printf("0x%08lx", val);
[abf3564]193 break;
194
195 case V_ERRNO:
196 if (val >= -15 && val <= 0) {
[356acd0]197 printf("%ld %s (%s)", val,
[abf3564]198 err_desc[-val].name,
199 err_desc[-val].desc);
[9a1b20c]200 } else {
[356acd0]201 printf("%ld", val);
[9a1b20c]202 }
[abf3564]203 break;
204 case V_INT_ERRNO:
205 if (val >= -15 && val < 0) {
[356acd0]206 printf("%ld %s (%s)", val,
[abf3564]207 err_desc[-val].name,
208 err_desc[-val].desc);
209 } else {
[356acd0]210 printf("%ld", val);
[abf3564]211 }
212 break;
213
214 case V_CHAR:
215 if (val >= 0x20 && val < 0x7f) {
216 printf("'%c'", val);
[9a1b20c]217 } else {
[abf3564]218 switch (val) {
219 case '\a': printf("'\\a'"); break;
220 case '\b': printf("'\\b'"); break;
221 case '\n': printf("'\\n'"); break;
222 case '\r': printf("'\\r'"); break;
223 case '\t': printf("'\\t'"); break;
224 case '\\': printf("'\\\\'"); break;
[356acd0]225 default: printf("'\\x%02lX'", val); break;
[abf3564]226 }
[9a1b20c]227 }
[abf3564]228 break;
[9a1b20c]229 }
[abf3564]230}
231
232
[356acd0]233static void print_sc_retval(sysarg_t retval, val_type_t val_type)
[abf3564]234{
235 printf(" -> ");
236 val_print(retval, val_type);
[9a1b20c]237 putchar('\n');
238}
239
[a5c3f73]240static void print_sc_args(sysarg_t *sc_args, int n)
[9a1b20c]241{
242 int i;
243
244 putchar('(');
[a5c3f73]245 if (n > 0) printf("%ld", sc_args[0]);
[abf3564]246 for (i = 1; i < n; i++) {
[a5c3f73]247 printf(", %ld", sc_args[i]);
[9a1b20c]248 }
249 putchar(')');
250}
251
[a5c3f73]252static void sc_ipc_call_async_fast(sysarg_t *sc_args, sysarg_t sc_rc)
[9a1b20c]253{
254 ipc_call_t call;
[a5c3f73]255 ipcarg_t phoneid;
[9a1b20c]256
257 if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
258 return;
259
260 phoneid = sc_args[0];
261
262 IPC_SET_METHOD(call, sc_args[1]);
263 IPC_SET_ARG1(call, sc_args[2]);
264 IPC_SET_ARG2(call, sc_args[3]);
265 IPC_SET_ARG3(call, sc_args[4]);
266 IPC_SET_ARG4(call, sc_args[5]);
267 IPC_SET_ARG5(call, 0);
268
269 ipcp_call_out(phoneid, &call, sc_rc);
270}
271
[a5c3f73]272static void sc_ipc_call_async_slow(sysarg_t *sc_args, sysarg_t sc_rc)
[9a1b20c]273{
274 ipc_call_t call;
275 int rc;
276
277 if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
278 return;
279
280 memset(&call, 0, sizeof(call));
281 rc = udebug_mem_read(phoneid, &call.args, sc_args[1], sizeof(call.args));
282
283 if (rc >= 0) {
284 ipcp_call_out(sc_args[0], &call, sc_rc);
285 }
286}
287
[a5c3f73]288static void sc_ipc_call_sync_fast(sysarg_t *sc_args)
[9a1b20c]289{
290 ipc_call_t question, reply;
291 int rc;
292 int phoneidx;
293
294// printf("sc_ipc_call_sync_fast()\n");
295 phoneidx = sc_args[0];
296
297 IPC_SET_METHOD(question, sc_args[1]);
298 IPC_SET_ARG1(question, sc_args[2]);
299 IPC_SET_ARG2(question, sc_args[3]);
300 IPC_SET_ARG3(question, sc_args[4]);
301 IPC_SET_ARG4(question, 0);
302 IPC_SET_ARG5(question, 0);
303
304// printf("memset\n");
305 memset(&reply, 0, sizeof(reply));
306// printf("udebug_mem_read(phone=%d, buffer_ptr=%u, src_addr=%d, n=%d\n",
307// phoneid, &reply.args, sc_args[5], sizeof(reply.args));
308 rc = udebug_mem_read(phoneid, &reply.args, sc_args[5], sizeof(reply.args));
309// printf("dmr->%d\n", rc);
310 if (rc < 0) return;
311
312// printf("call ipc_call_sync\n");
313 ipcp_call_sync(phoneidx, &question, &reply);
314}
315
[a5c3f73]316static void sc_ipc_call_sync_slow(sysarg_t *sc_args)
[9a1b20c]317{
318 ipc_call_t question, reply;
319 int rc;
320
321 memset(&question, 0, sizeof(question));
322 rc = udebug_mem_read(phoneid, &question.args, sc_args[1], sizeof(question.args));
323 printf("dmr->%d\n", rc);
324 if (rc < 0) return;
325
326 memset(&reply, 0, sizeof(reply));
327 rc = udebug_mem_read(phoneid, &reply.args, sc_args[2], sizeof(reply.args));
328 printf("dmr->%d\n", rc);
329 if (rc < 0) return;
330
331 ipcp_call_sync(sc_args[0], &question, &reply);
332}
333
[a5c3f73]334static void sc_ipc_wait(sysarg_t *sc_args, int sc_rc)
[9a1b20c]335{
336 ipc_call_t call;
337 int rc;
338
339 if (sc_rc == 0) return;
340
341 memset(&call, 0, sizeof(call));
342 rc = udebug_mem_read(phoneid, &call, sc_args[0], sizeof(call));
343// printf("udebug_mem_read(phone %d, dest %d, app-mem src %d, size %d -> %d\n",
344// phoneid, (int)&call, sc_args[0], sizeof(call), rc);
345
346 if (rc >= 0) {
347 ipcp_call_in(&call, sc_rc);
348 }
349}
350
[a5c3f73]351static void event_syscall_b(unsigned thread_id, uintptr_t thread_hash,
352 unsigned sc_id, sysarg_t sc_rc)
[9a1b20c]353{
[a5c3f73]354 sysarg_t sc_args[6];
[9a1b20c]355 int rc;
356
357 /* Read syscall arguments */
358 rc = udebug_args_read(phoneid, thread_hash, sc_args);
359
360 async_serialize_start();
361
362// printf("[%d] ", thread_id);
363
364 if (rc < 0) {
365 printf("error\n");
366 async_serialize_end();
367 return;
368 }
369
[1643855]370 if ((display_mask & DM_SYSCALL) != 0) {
371 /* Print syscall name and arguments */
372 printf("%s", syscall_desc[sc_id].name);
373 print_sc_args(sc_args, syscall_desc[sc_id].n_args);
374 }
[9a1b20c]375
376 async_serialize_end();
377}
378
[a5c3f73]379static void event_syscall_e(unsigned thread_id, uintptr_t thread_hash,
380 unsigned sc_id, sysarg_t sc_rc)
[9a1b20c]381{
[a5c3f73]382 sysarg_t sc_args[6];
[9a1b20c]383 int rv_type;
384 int rc;
385
386 /* Read syscall arguments */
387 rc = udebug_args_read(phoneid, thread_hash, sc_args);
388
389 async_serialize_start();
390
391// printf("[%d] ", thread_id);
392
393 if (rc < 0) {
394 printf("error\n");
395 async_serialize_end();
396 return;
397 }
398
[1643855]399 if ((display_mask & DM_SYSCALL) != 0) {
400 /* Print syscall return value */
401 rv_type = syscall_desc[sc_id].rv_type;
402 print_sc_retval(sc_rc, rv_type);
403 }
[9a1b20c]404
405 switch (sc_id) {
406 case SYS_IPC_CALL_ASYNC_FAST:
407 sc_ipc_call_async_fast(sc_args, sc_rc);
408 break;
409 case SYS_IPC_CALL_ASYNC_SLOW:
410 sc_ipc_call_async_slow(sc_args, sc_rc);
411 break;
412 case SYS_IPC_CALL_SYNC_FAST:
413 sc_ipc_call_sync_fast(sc_args);
414 break;
415 case SYS_IPC_CALL_SYNC_SLOW:
416 sc_ipc_call_sync_slow(sc_args);
417 break;
418 case SYS_IPC_WAIT:
419 sc_ipc_wait(sc_args, sc_rc);
420 break;
421 default:
422 break;
423 }
424
425 async_serialize_end();
426}
427
[a5c3f73]428static void event_thread_b(uintptr_t hash)
[9a1b20c]429{
430 async_serialize_start();
[a5c3f73]431 printf("New thread, hash 0x%lx\n", hash);
[9a1b20c]432 async_serialize_end();
433
434 thread_trace_start(hash);
435}
436
437static int trace_loop(void *thread_hash_arg)
438{
439 int rc;
440 unsigned ev_type;
[a5c3f73]441 uintptr_t thread_hash;
[9a1b20c]442 unsigned thread_id;
[a5c3f73]443 sysarg_t val0, val1;
[9a1b20c]444
[a5c3f73]445 thread_hash = (uintptr_t)thread_hash_arg;
[9a1b20c]446 thread_id = next_thread_id++;
447
[a5c3f73]448 printf("Start tracing thread [%d] (hash 0x%lx)\n", thread_id, thread_hash);
[9a1b20c]449
450 while (!abort_trace) {
451
452 /* Run thread until an event occurs */
453 rc = udebug_go(phoneid, thread_hash,
454 &ev_type, &val0, &val1);
455
456// printf("rc = %d, ev_type=%d\n", rc, ev_type);
457 if (ev_type == UDEBUG_EVENT_FINISHED) {
[c9a29d6]458 /* Done tracing this thread */
[9a1b20c]459 break;
460 }
461
462 if (rc >= 0) {
463 switch (ev_type) {
464 case UDEBUG_EVENT_SYSCALL_B:
465 event_syscall_b(thread_id, thread_hash, val0, (int)val1);
466 break;
467 case UDEBUG_EVENT_SYSCALL_E:
468 event_syscall_e(thread_id, thread_hash, val0, (int)val1);
469 break;
470 case UDEBUG_EVENT_STOP:
[c9a29d6]471 printf("Stop event\n");
472 printf("Waiting for resume\n");
[9a1b20c]473 while (paused) {
474 usleep(1000000);
475 fibril_yield();
476 printf(".");
477 }
[c9a29d6]478 printf("Resumed\n");
[9a1b20c]479 break;
480 case UDEBUG_EVENT_THREAD_B:
481 event_thread_b(val0);
482 break;
483 case UDEBUG_EVENT_THREAD_E:
[a5c3f73]484 printf("Thread 0x%lx exited\n", val0);
[9a1b20c]485 abort_trace = 1;
486 break;
487 default:
[c9a29d6]488 printf("Unknown event type %d\n", ev_type);
[9a1b20c]489 break;
490 }
491 }
492
493 }
494
[c9a29d6]495 printf("Finished tracing thread [%d]\n", thread_id);
[9a1b20c]496 return 0;
497}
498
[a5c3f73]499void thread_trace_start(uintptr_t thread_hash)
[9a1b20c]500{
501 fid_t fid;
502
503 thash = thread_hash;
504
505 fid = fibril_create(trace_loop, (void *)thread_hash);
506 if (fid == 0) {
507 printf("Warning: Failed creating fibril\n");
508 }
509 fibril_add_ready(fid);
510}
511
[4470e26]512static loader_t *preload_task(const char *path, char *const argv[],
513 task_id_t *task_id)
[9a1b20c]514{
[4470e26]515 loader_t *ldr;
[9a1b20c]516 int rc;
517
[4470e26]518 /* Spawn a program loader */
[24345a5]519 ldr = loader_spawn(path);
[4470e26]520 if (ldr == NULL)
521 return 0;
522
523 /* Get task ID. */
524 rc = loader_get_task_id(ldr, task_id);
525 if (rc != EOK)
526 goto error;
527
528 /* Send program pathname */
529 rc = loader_set_pathname(ldr, path);
530 if (rc != EOK)
531 goto error;
532
533 /* Send arguments */
534 rc = loader_set_args(ldr, argv);
535 if (rc != EOK)
536 goto error;
537
538 /* Load the program. */
539 rc = loader_load_program(ldr);
540 if (rc != EOK)
541 goto error;
542
543 /* Success */
544 return ldr;
545
546 /* Error exit */
547error:
548 loader_abort(ldr);
549 free(ldr);
550 return NULL;
551}
[9a1b20c]552
[4470e26]553static void trace_task(task_id_t task_id)
554{
555 int i;
556 int rc;
557 int c;
[9a1b20c]558
559 ipcp_init();
[c9a29d6]560
561 /*
562 * User apps now typically have console on phone 3.
563 * (Phones 1 and 2 are used by the loader).
564 */
565 ipcp_connection_set(3, 0, proto_console);
[9a1b20c]566
567 rc = get_thread_list();
568 if (rc < 0) {
569 printf("Failed to get thread list (error %d)\n", rc);
570 return;
571 }
572
573 abort_trace = 0;
574
575 for (i = 0; i < n_threads; i++) {
576 thread_trace_start(thread_hash_buf[i]);
577 }
578
579 while(1) {
580 c = getchar();
581 if (c == 'q') break;
582 if (c == 'p') {
583 paused = 1;
584 rc = udebug_stop(phoneid, thash);
585 printf("stop -> %d\n", rc);
586 }
587 if (c == 'r') {
588 paused = 0;
589 }
590 }
591
[c9a29d6]592 printf("\nTerminate debugging session...\n");
[9a1b20c]593 abort_trace = 1;
594 udebug_end(phoneid);
595 ipc_hangup(phoneid);
596
597 ipcp_cleanup();
598
[c9a29d6]599 printf("Done\n");
[9a1b20c]600 return;
601}
602
603static void main_init(void)
604{
605 proto_t *p;
606 oper_t *o;
607
[abf3564]608 val_type_t arg_def[OPER_MAX_ARGS] = {
609 V_INTEGER,
610 V_INTEGER,
611 V_INTEGER,
612 V_INTEGER,
613 V_INTEGER
614 };
615
616 val_type_t resp_def[OPER_MAX_ARGS] = {
617 V_INTEGER,
618 V_INTEGER,
619 V_INTEGER,
620 V_INTEGER,
621 V_INTEGER
622 };
623
[9a1b20c]624 next_thread_id = 1;
625 paused = 0;
626
627 proto_init();
628
629 p = proto_new("vfs");
[abf3564]630 o = oper_new("read", 1, arg_def, V_ERRNO, 1, resp_def);
[9a1b20c]631 proto_add_oper(p, VFS_READ, o);
[abf3564]632 o = oper_new("write", 1, arg_def, V_ERRNO, 1, resp_def);
[9a1b20c]633 proto_add_oper(p, VFS_WRITE, o);
[abf3564]634 o = oper_new("truncate", 5, arg_def, V_ERRNO, 0, resp_def);
[9a1b20c]635 proto_add_oper(p, VFS_TRUNCATE, o);
[abf3564]636 o = oper_new("mount", 2, arg_def, V_ERRNO, 0, resp_def);
[9a1b20c]637 proto_add_oper(p, VFS_MOUNT, o);
[abf3564]638/* o = oper_new("unmount", 0, arg_def);
639 proto_add_oper(p, VFS_UNMOUNT, o);*/
[9a1b20c]640
641 proto_register(SERVICE_VFS, p);
642
643 p = proto_new("console");
[abf3564]644 resp_def[0] = V_CHAR;
645 o = oper_new("getchar", 0, arg_def, V_INTEGER, 2, resp_def);
[9a1b20c]646 proto_add_oper(p, CONSOLE_GETCHAR, o);
[abf3564]647
648 arg_def[0] = V_CHAR;
649 o = oper_new("putchar", 1, arg_def, V_VOID, 0, resp_def);
[9a1b20c]650 proto_add_oper(p, CONSOLE_PUTCHAR, o);
[abf3564]651 o = oper_new("clear", 0, arg_def, V_VOID, 0, resp_def);
[9a1b20c]652 proto_add_oper(p, CONSOLE_CLEAR, o);
[abf3564]653
654 arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER;
655 o = oper_new("goto", 2, arg_def, V_VOID, 0, resp_def);
[9a1b20c]656 proto_add_oper(p, CONSOLE_GOTO, o);
[abf3564]657
658 resp_def[0] = V_INTEGER; resp_def[1] = V_INTEGER;
659 o = oper_new("getsize", 0, arg_def, V_INTEGER, 2, resp_def);
[9a1b20c]660 proto_add_oper(p, CONSOLE_GETSIZE, o);
[abf3564]661 o = oper_new("flush", 0, arg_def, V_VOID, 0, resp_def);
[9a1b20c]662 proto_add_oper(p, CONSOLE_FLUSH, o);
[abf3564]663
664 arg_def[0] = V_INTEGER; arg_def[1] = V_INTEGER;
665 o = oper_new("set_style", 2, arg_def, V_INTEGER, 0, resp_def);
[9a1b20c]666 proto_add_oper(p, CONSOLE_SET_STYLE, o);
[abf3564]667 o = oper_new("cursor_visibility", 1, arg_def, V_VOID, 0, resp_def);
[9a1b20c]668 proto_add_oper(p, CONSOLE_CURSOR_VISIBILITY, o);
669
670 proto_console = p;
671 proto_register(SERVICE_CONSOLE, p);
672}
673
674static void print_syntax()
675{
[47e0a05b]676 printf("Syntax:\n");
677 printf("\ttrace [+<events>] <executable> [<arg1> [...]]\n");
678 printf("or\ttrace [+<events>] -t <task_id>\n");
[1643855]679 printf("Events: (default is +tp)\n");
680 printf("\n");
681 printf("\tt ... Thread creation and termination\n");
682 printf("\ts ... System calls\n");
683 printf("\ti ... Low-level IPC\n");
684 printf("\tp ... Protocol level\n");
685 printf("\n");
[47e0a05b]686 printf("Examples:\n");
687 printf("\ttrace +s /app/tetris\n");
688 printf("\ttrace +tsip -t 12\n");
[9a1b20c]689}
690
[1643855]691static display_mask_t parse_display_mask(char *text)
[9a1b20c]692{
[1643855]693 display_mask_t dm;
694 char *c;
695
696 c = text;
697
698 while (*c) {
699 switch (*c) {
700 case 't': dm = dm | DM_THREAD; break;
701 case 's': dm = dm | DM_SYSCALL; break;
702 case 'i': dm = dm | DM_IPC; break;
703 case 'p': dm = dm | DM_SYSTEM | DM_USER; break;
704 default:
705 printf("Unexpected event type '%c'\n", *c);
706 exit(1);
707 }
708
709 ++c;
710 }
711
712 return dm;
713}
714
715static int parse_args(int argc, char *argv[])
716{
717 char *arg;
[9a1b20c]718 char *err_p;
719
[47e0a05b]720 task_id = 0;
721
[1643855]722 --argc; ++argv;
723
[47e0a05b]724 while (argc > 0) {
[1643855]725 arg = *argv;
726 if (arg[0] == '+') {
727 display_mask = parse_display_mask(&arg[1]);
[47e0a05b]728 } else if (arg[0] == '-') {
729 if (arg[1] == 't') {
730 /* Trace an already running task */
731 --argc; ++argv;
732 task_id = strtol(*argv, &err_p, 10);
[4470e26]733 task_ldr = NULL;
[47e0a05b]734 if (*err_p) {
735 printf("Task ID syntax error\n");
736 print_syntax();
737 return -1;
738 }
739 } else {
740 printf("Uknown option '%s'\n", arg[0]);
741 print_syntax();
742 return -1;
743 }
[1643855]744 } else {
[47e0a05b]745 break;
[1643855]746 }
747
748 --argc; ++argv;
749 }
750
[47e0a05b]751 if (task_id != 0) {
[a5c3f73]752 if (argc == 0) return 0;
[47e0a05b]753 printf("Extra arguments\n");
[9a1b20c]754 print_syntax();
[47e0a05b]755 return -1;
[9a1b20c]756 }
757
[47e0a05b]758 if (argc < 1) {
759 printf("Missing argument\n");
[9a1b20c]760 print_syntax();
[1643855]761 return -1;
[9a1b20c]762 }
763
[4470e26]764 /* Preload the specified program file. */
[47e0a05b]765 printf("Spawning '%s' with arguments:\n", *argv);
766 {
767 char **cp = argv;
768 while (*cp) printf("'%s'\n", *cp++);
769 }
[4470e26]770 task_ldr = preload_task(*argv, argv, &task_id);
[47e0a05b]771
[1643855]772 return 0;
773}
774
775int main(int argc, char *argv[])
776{
[4470e26]777 int rc;
778
[1643855]779 printf("System Call / IPC Tracer\n");
780
781 display_mask = DM_THREAD | DM_SYSTEM | DM_USER;
782
783 if (parse_args(argc, argv) < 0)
784 return 1;
785
[9a1b20c]786 main_init();
[4470e26]787
788 rc = connect_task(task_id);
789 if (rc < 0) {
790 printf("Failed connecting to task %lld\n", task_id);
791 return 1;
792 }
793
794 printf("Connected to task %lld\n", task_id);
795
796 if (task_ldr != NULL) {
797 program_run();
798 }
799
800 trace_task(task_id);
[1643855]801
802 return 0;
[9a1b20c]803}
804
805/** @}
806 */
Note: See TracBrowser for help on using the repository browser.