Subversion Repositories HelenOS

Rev

Rev 3442 | Rev 3452 | 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.  
  45. // Temporary: service and method names
  46. #include "proto.h"
  47. #include <ipc/services.h>
  48. #include "../../srv/vfs/vfs.h"
  49. #include "../../srv/console/console.h"
  50.  
  51. #include "syscalls.h"
  52. #include "ipcp.h"
  53. #include "errors.h"
  54. #include "trace.h"
  55.  
  56. #define THBUF_SIZE 64
  57. unsigned thread_hash_buf[THBUF_SIZE];
  58. unsigned n_threads;
  59.  
  60. int next_thread_id;
  61.  
  62. int phoneid;
  63. int abort_trace;
  64.  
  65. unsigned thash;
  66. volatile int paused;
  67.  
  68. void thread_trace_start(unsigned thread_hash);
  69.  
  70. static proto_t *proto_console;
  71. static task_id_t task_id;
  72.  
  73. /** Combination of events/data to print. */
  74. display_mask_t display_mask;
  75.  
  76. static int task_connect(task_id_t task_id)
  77. {
  78.     int rc;
  79.  
  80.     rc = ipc_connect_kbox(task_id);
  81.  
  82.     if (rc == ENOTSUP) {
  83.         printf("You do not have userspace debugging support "
  84.             "compiled in the kernel.\n");
  85.         printf("Compile kernel with 'Support for userspace debuggers' "
  86.             "(CONFIG_UDEBUG) enabled.\n");
  87.         return rc;
  88.     }
  89.  
  90.     if (rc < 0) {
  91.         printf("Error connecting\n");
  92.         printf("ipc_connect_task(%lld) -> %d ", task_id, rc);
  93.         return rc;
  94.     }
  95.  
  96.     phoneid = rc;
  97.  
  98.     rc = udebug_begin(phoneid);
  99.     if (rc < 0) {
  100.         printf("udebug_begin() -> %d\n", rc);
  101.         return rc;
  102.     }
  103.  
  104.     rc = udebug_set_evmask(phoneid, UDEBUG_EM_ALL);
  105.     if (rc < 0) {
  106.         printf("udebug_set_evmask(0x%x) -> %d\n ", UDEBUG_EM_ALL, rc);
  107.         return rc;
  108.     }
  109.  
  110.     return 0;
  111. }
  112.  
  113. static int get_thread_list(void)
  114. {
  115.     int rc;
  116.     size_t tb_copied;
  117.     size_t tb_needed;
  118.     int i;
  119.  
  120.     rc = udebug_thread_read(phoneid, thread_hash_buf,
  121.         THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed);
  122.     if (rc < 0) {
  123.         printf("udebug_thread_read() -> %d\n", rc);
  124.         return rc;
  125.     }
  126.  
  127.     n_threads = tb_copied / sizeof(unsigned);
  128.  
  129.     printf("Threads:");
  130.     for (i = 0; i < n_threads; i++) {
  131.         printf(" [%d] (hash 0x%u)", 1+i, thread_hash_buf[i]);
  132.     }
  133.     printf("\ntotal of %u threads\n", tb_needed/sizeof(unsigned));
  134.  
  135.     return 0;
  136. }
  137.  
  138. static void print_sc_retval(int retval, rv_type_t rv_type)
  139. {
  140.     printf (" -> ");
  141.     if (rv_type == RV_INTEGER) {
  142.         printf("%d", retval);
  143.     } else if (rv_type == RV_HASH) {
  144.         printf("0x%08x", retval);
  145.     } else if (rv_type == RV_ERRNO) {
  146.         if (retval >= -15 && retval <= 0) {
  147.             printf("%d %s (%s)", retval,
  148.                 err_desc[retval].name,
  149.                 err_desc[retval].desc);
  150.         } else {
  151.             printf("%d", retval);
  152.         }
  153.     } else if (rv_type == RV_INT_ERRNO) {
  154.         if (retval >= -15 && retval < 0) {
  155.             printf("%d %s (%s)", retval,
  156.                 err_desc[retval].name,
  157.                 err_desc[retval].desc);
  158.         } else {
  159.             printf("%d", retval);
  160.         }
  161.     }
  162.     putchar('\n');
  163. }
  164.  
  165. static void print_sc_args(unsigned *sc_args, int n)
  166. {
  167.     int i;
  168.  
  169.     putchar('(');
  170.     if (n > 0) printf("%d", sc_args[0]);
  171.     for (i=1; i<n; i++) {
  172.         printf(", %d", sc_args[i]);
  173.     }
  174.     putchar(')');
  175. }
  176.  
  177. static void sc_ipc_call_async_fast(unsigned *sc_args, int sc_rc)
  178. {
  179.     ipc_call_t call;
  180.     int phoneid;
  181.    
  182.     if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
  183.         return;
  184.  
  185.     phoneid = sc_args[0];
  186.  
  187.     IPC_SET_METHOD(call, sc_args[1]);
  188.     IPC_SET_ARG1(call, sc_args[2]);
  189.     IPC_SET_ARG2(call, sc_args[3]);
  190.     IPC_SET_ARG3(call, sc_args[4]);
  191.     IPC_SET_ARG4(call, sc_args[5]);
  192.     IPC_SET_ARG5(call, 0);
  193.  
  194.     ipcp_call_out(phoneid, &call, sc_rc);
  195. }
  196.  
  197. static void sc_ipc_call_async_slow(unsigned *sc_args, int sc_rc)
  198. {
  199.     ipc_call_t call;
  200.     int rc;
  201.  
  202.     if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
  203.         return;
  204.  
  205.     memset(&call, 0, sizeof(call));
  206.     rc = udebug_mem_read(phoneid, &call.args, sc_args[1], sizeof(call.args));
  207.  
  208.     if (rc >= 0) {
  209.         ipcp_call_out(sc_args[0], &call, sc_rc);
  210.     }
  211. }
  212.  
  213. static void sc_ipc_call_sync_fast(unsigned *sc_args)
  214. {
  215.     ipc_call_t question, reply;
  216.     int rc;
  217.     int phoneidx;
  218.  
  219. //  printf("sc_ipc_call_sync_fast()\n");
  220.     phoneidx = sc_args[0];
  221.  
  222.     IPC_SET_METHOD(question, sc_args[1]);
  223.     IPC_SET_ARG1(question, sc_args[2]);
  224.     IPC_SET_ARG2(question, sc_args[3]);
  225.     IPC_SET_ARG3(question, sc_args[4]);
  226.     IPC_SET_ARG4(question, 0);
  227.     IPC_SET_ARG5(question, 0);
  228.  
  229. //  printf("memset\n");
  230.     memset(&reply, 0, sizeof(reply));
  231. //  printf("udebug_mem_read(phone=%d, buffer_ptr=%u, src_addr=%d, n=%d\n",
  232. //      phoneid, &reply.args, sc_args[5], sizeof(reply.args));
  233.     rc = udebug_mem_read(phoneid, &reply.args, sc_args[5], sizeof(reply.args));
  234. //  printf("dmr->%d\n", rc);
  235.     if (rc < 0) return;
  236.  
  237. //  printf("call ipc_call_sync\n");
  238.     ipcp_call_sync(phoneidx, &question, &reply);
  239. }
  240.  
  241. static void sc_ipc_call_sync_slow(unsigned *sc_args)
  242. {
  243.     ipc_call_t question, reply;
  244.     int rc;
  245.  
  246.     memset(&question, 0, sizeof(question));
  247.     rc = udebug_mem_read(phoneid, &question.args, sc_args[1], sizeof(question.args));
  248.     printf("dmr->%d\n", rc);
  249.     if (rc < 0) return;
  250.  
  251.     memset(&reply, 0, sizeof(reply));
  252.     rc = udebug_mem_read(phoneid, &reply.args, sc_args[2], sizeof(reply.args));
  253.     printf("dmr->%d\n", rc);
  254.     if (rc < 0) return;
  255.  
  256.     ipcp_call_sync(sc_args[0], &question, &reply);
  257. }
  258.  
  259. static void sc_ipc_wait(unsigned *sc_args, int sc_rc)
  260. {
  261.     ipc_call_t call;
  262.     int rc;
  263.  
  264.     if (sc_rc == 0) return;
  265.  
  266.     memset(&call, 0, sizeof(call));
  267.     rc = udebug_mem_read(phoneid, &call, sc_args[0], sizeof(call));
  268. //  printf("udebug_mem_read(phone %d, dest %d, app-mem src %d, size %d -> %d\n",
  269. //      phoneid, (int)&call, sc_args[0], sizeof(call), rc);
  270.  
  271.     if (rc >= 0) {
  272.         ipcp_call_in(&call, sc_rc);
  273.     }
  274. }
  275.  
  276. static void event_syscall_b(unsigned thread_id, unsigned thread_hash,  unsigned sc_id, int sc_rc)
  277. {
  278.     unsigned sc_args[6];
  279.     int rc;
  280.  
  281.     /* Read syscall arguments */
  282.     rc = udebug_args_read(phoneid, thread_hash, sc_args);
  283.  
  284.     async_serialize_start();
  285.  
  286. //  printf("[%d] ", thread_id);
  287.  
  288.     if (rc < 0) {
  289.         printf("error\n");
  290.         async_serialize_end();
  291.         return;
  292.     }
  293.  
  294.     if ((display_mask & DM_SYSCALL) != 0) {
  295.         /* Print syscall name and arguments */
  296.         printf("%s", syscall_desc[sc_id].name);
  297.         print_sc_args(sc_args, syscall_desc[sc_id].n_args);
  298.     }
  299.  
  300.     async_serialize_end();
  301. }
  302.  
  303. static void event_syscall_e(unsigned thread_id, unsigned thread_hash,  unsigned sc_id, int sc_rc)
  304. {
  305.     unsigned sc_args[6];
  306.     int rv_type;
  307.     int rc;
  308.  
  309.     /* Read syscall arguments */
  310.     rc = udebug_args_read(phoneid, thread_hash, sc_args);
  311.  
  312.     async_serialize_start();
  313.  
  314. //  printf("[%d] ", thread_id);
  315.  
  316.     if (rc < 0) {
  317.         printf("error\n");
  318.         async_serialize_end();
  319.         return;
  320.     }
  321.  
  322.     if ((display_mask & DM_SYSCALL) != 0) {
  323.         /* Print syscall return value */
  324.         rv_type = syscall_desc[sc_id].rv_type;
  325.         print_sc_retval(sc_rc, rv_type);
  326.     }
  327.  
  328.     switch (sc_id) {
  329.     case SYS_IPC_CALL_ASYNC_FAST:
  330.         sc_ipc_call_async_fast(sc_args, sc_rc);
  331.         break;
  332.     case SYS_IPC_CALL_ASYNC_SLOW:
  333.         sc_ipc_call_async_slow(sc_args, sc_rc);
  334.         break;
  335.     case SYS_IPC_CALL_SYNC_FAST:
  336.         sc_ipc_call_sync_fast(sc_args);
  337.         break;
  338.     case SYS_IPC_CALL_SYNC_SLOW:
  339.         sc_ipc_call_sync_slow(sc_args);
  340.         break;
  341.     case SYS_IPC_WAIT:
  342.         sc_ipc_wait(sc_args, sc_rc);
  343.         break;
  344.     default:
  345.         break;
  346.     }
  347.  
  348.     async_serialize_end();
  349. }
  350.  
  351. static void event_thread_b(unsigned hash)
  352. {
  353.     async_serialize_start();
  354.     printf("New thread, hash 0x%x\n", hash);
  355.     async_serialize_end();
  356.  
  357.     thread_trace_start(hash);
  358. }
  359.  
  360. static int trace_loop(void *thread_hash_arg)
  361. {
  362.     int rc;
  363.     unsigned ev_type;
  364.     unsigned thread_hash;
  365.     unsigned thread_id;
  366.     unsigned val0, val1;
  367.  
  368.     thread_hash = (unsigned)thread_hash_arg;
  369.     thread_id = next_thread_id++;
  370.  
  371.     printf("Start tracing thread [%d] (hash 0x%x)\n", thread_id, thread_hash);
  372.  
  373.     while (!abort_trace) {
  374.  
  375.         /* Run thread until an event occurs */
  376.         rc = udebug_go(phoneid, thread_hash,
  377.             &ev_type, &val0, &val1);
  378.  
  379. //      printf("rc = %d, ev_type=%d\n", rc, ev_type);
  380.         if (ev_type == UDEBUG_EVENT_FINISHED) {
  381.             /* Done tracing this thread */
  382.             break;
  383.         }
  384.  
  385.         if (rc >= 0) {
  386.             switch (ev_type) {
  387.             case UDEBUG_EVENT_SYSCALL_B:
  388.                 event_syscall_b(thread_id, thread_hash, val0, (int)val1);
  389.                 break;
  390.             case UDEBUG_EVENT_SYSCALL_E:
  391.                 event_syscall_e(thread_id, thread_hash, val0, (int)val1);
  392.                 break;
  393.             case UDEBUG_EVENT_STOP:
  394.                 printf("Stop event\n");
  395.                 printf("Waiting for resume\n");
  396.                 while (paused) {
  397.                     usleep(1000000);
  398.                     fibril_yield();
  399.                     printf(".");
  400.                 }
  401.                 printf("Resumed\n");
  402.                 break;
  403.             case UDEBUG_EVENT_THREAD_B:
  404.                 event_thread_b(val0);
  405.                 break;
  406.             case UDEBUG_EVENT_THREAD_E:
  407.                 printf("Thread 0x%x exited\n", val0);
  408.                 abort_trace = 1;
  409.                 break;
  410.             default:
  411.                 printf("Unknown event type %d\n", ev_type);
  412.                 break;
  413.             }
  414.         }
  415.  
  416.     }
  417.  
  418.     printf("Finished tracing thread [%d]\n", thread_id);
  419.     return 0;
  420. }
  421.  
  422. void thread_trace_start(unsigned thread_hash)
  423. {
  424.     fid_t fid;
  425.  
  426.     thash = thread_hash;
  427.  
  428.     fid = fibril_create(trace_loop, (void *)thread_hash);
  429.     if (fid == 0) {
  430.         printf("Warning: Failed creating fibril\n");
  431.     }
  432.     fibril_add_ready(fid);
  433. }
  434.  
  435. static void trace_active_task(task_id_t task_id)
  436. {
  437.     int i;
  438.     int rc;
  439.     int c;
  440.  
  441.     rc = task_connect(task_id);
  442.     if (rc < 0) {
  443.         printf("Failed to connect to task %lld\n", task_id);
  444.         return;
  445.     }
  446.  
  447.     printf("Connected to task %lld\n", task_id);
  448.  
  449.     ipcp_init();
  450.  
  451.     /*
  452.      * User apps now typically have console on phone 3.
  453.      * (Phones 1 and 2 are used by the loader).
  454.      */
  455.     ipcp_connection_set(3, 0, proto_console);
  456.  
  457.     rc = get_thread_list();
  458.     if (rc < 0) {
  459.         printf("Failed to get thread list (error %d)\n", rc);
  460.         return;
  461.     }
  462.  
  463.     abort_trace = 0;
  464.  
  465.     for (i = 0; i < n_threads; i++) {
  466.         thread_trace_start(thread_hash_buf[i]);
  467.     }
  468.  
  469.     while(1) {
  470.         c = getchar();
  471.         if (c == 'q') break;
  472.         if (c == 'p') {
  473.             paused = 1;
  474.             rc = udebug_stop(phoneid, thash);
  475.             printf("stop -> %d\n", rc);
  476.         }
  477.         if (c == 'r') {
  478.             paused = 0;
  479.         }
  480.     }
  481.  
  482.     printf("\nTerminate debugging session...\n");
  483.     abort_trace = 1;
  484.     udebug_end(phoneid);
  485.     ipc_hangup(phoneid);
  486.  
  487.     ipcp_cleanup();
  488.  
  489.     printf("Done\n");
  490.     return;
  491. }
  492.  
  493. static void main_init(void)
  494. {
  495.     proto_t *p;
  496.     oper_t *o;
  497.  
  498.     next_thread_id = 1;
  499.     paused = 0;
  500.  
  501.     proto_init();
  502.  
  503.     p = proto_new("vfs");
  504.     o = oper_new("read");
  505.     proto_add_oper(p, VFS_READ, o);
  506.     o = oper_new("write");
  507.     proto_add_oper(p, VFS_WRITE, o);
  508.     o = oper_new("truncate");
  509.     proto_add_oper(p, VFS_TRUNCATE, o);
  510.     o = oper_new("mount");
  511.     proto_add_oper(p, VFS_MOUNT, o);
  512.     o = oper_new("unmount");
  513.     proto_add_oper(p, VFS_UNMOUNT, o);
  514.  
  515.     proto_register(SERVICE_VFS, p);
  516.  
  517.     p = proto_new("console");
  518.     o = oper_new("getchar");
  519.     proto_add_oper(p, CONSOLE_GETCHAR, o);
  520.     o = oper_new("putchar");
  521.     proto_add_oper(p, CONSOLE_PUTCHAR, o);
  522.     o = oper_new("clear");
  523.     proto_add_oper(p, CONSOLE_CLEAR, o);
  524.     o = oper_new("goto");
  525.     proto_add_oper(p, CONSOLE_GOTO, o);
  526.     o = oper_new("getsize");
  527.     proto_add_oper(p, CONSOLE_GETSIZE, o);
  528.     o = oper_new("flush");
  529.     proto_add_oper(p, CONSOLE_FLUSH, o);
  530.     o = oper_new("set_style");
  531.     proto_add_oper(p, CONSOLE_SET_STYLE, o);
  532.     o = oper_new("cursor_visibility");
  533.     proto_add_oper(p, CONSOLE_CURSOR_VISIBILITY, o);
  534.     o = oper_new("flush");
  535.     proto_add_oper(p, CONSOLE_FLUSH, o);
  536.  
  537.     proto_console = p;
  538.     proto_register(SERVICE_CONSOLE, p);
  539. }
  540.  
  541. static void print_syntax()
  542. {
  543.     printf("Syntax: trace [+<events>] <task_id>\n");
  544.     printf("Events: (default is +tp)\n");
  545.     printf("\n");
  546.     printf("\tt ... Thread creation and termination\n");
  547.     printf("\ts ... System calls\n");
  548.     printf("\ti ... Low-level IPC\n");
  549.     printf("\tp ... Protocol level\n");
  550.     printf("\n");
  551.     printf("Example: trace +tsip 12\n");
  552. }
  553.  
  554. static display_mask_t parse_display_mask(char *text)
  555. {
  556.     display_mask_t dm;
  557.     char *c;
  558.  
  559.     c = text;
  560.  
  561.     while (*c) {
  562.         switch (*c) {
  563.         case 't': dm = dm | DM_THREAD; break;
  564.         case 's': dm = dm | DM_SYSCALL; break;
  565.         case 'i': dm = dm | DM_IPC; break;
  566.         case 'p': dm = dm | DM_SYSTEM | DM_USER; break;
  567.         default:
  568.             printf("Unexpected event type '%c'\n", *c);
  569.             exit(1);
  570.         }
  571.  
  572.         ++c;
  573.     }
  574.  
  575.     return dm;
  576. }
  577.  
  578. static int parse_args(int argc, char *argv[])
  579. {
  580.     char *arg;
  581.     char *err_p;
  582.  
  583.     --argc; ++argv;
  584.  
  585.     while (argc > 1) {
  586.         arg = *argv;
  587.         if (arg[0] == '+') {
  588.             display_mask = parse_display_mask(&arg[1]);
  589.         } else {
  590.             printf("Unexpected argument '%s'\n", arg);
  591.             print_syntax();
  592.             return -1;
  593.         }
  594.  
  595.         --argc; ++argv;
  596.     }
  597.  
  598.     if (argc != 1) {
  599.         printf("Missing argument\n");
  600.         print_syntax();
  601.         return 1;
  602.     }
  603.  
  604.     task_id = strtol(*argv, &err_p, 10);
  605.  
  606.     if (*err_p) {
  607.         printf("Task ID syntax error\n");
  608.         print_syntax();
  609.         return -1;
  610.     }
  611.  
  612.     return 0;
  613. }
  614.  
  615. int main(int argc, char *argv[])
  616. {
  617.     printf("System Call / IPC Tracer\n");
  618.  
  619.     display_mask = DM_THREAD | DM_SYSTEM | DM_USER;
  620.  
  621.     if (parse_args(argc, argv) < 0)
  622.         return 1;
  623.  
  624.     main_init();
  625.     trace_active_task(task_id);
  626.  
  627.     return 0;
  628. }
  629.  
  630. /** @}
  631.  */
  632.