Subversion Repositories HelenOS

Rev

Rev 3436 | Rev 3472 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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