Subversion Repositories HelenOS

Rev

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