Subversion Repositories HelenOS

Rev

Rev 2903 | Go to most recent revision | Blame | 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 sctrace
  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.  
  55. #define THBUF_SIZE 64
  56. unsigned thread_hash_buf[THBUF_SIZE];
  57. unsigned n_threads;
  58.  
  59. int next_thread_id;
  60.  
  61. int phoneid;
  62. int abort_trace;
  63.  
  64. unsigned thash;
  65. volatile int paused;
  66.  
  67. void thread_trace_start(unsigned thread_hash);
  68.  
  69. static proto_t *proto_console;
  70.  
  71. int task_connect(int taskid)
  72. {
  73.     int rc;
  74.  
  75.     printf("ipc_connect_task(%d)... ", taskid);
  76.     rc = ipc_connect_kbox(taskid);
  77.     printf("-> %d\n", rc);
  78.     phoneid = rc;
  79.     if (rc < 0) return rc;
  80.  
  81.     printf("udebug_begin()... ");
  82.     rc = udebug_begin(phoneid);
  83.     printf("-> %d\n", rc);
  84.     if (rc < 0) return rc;
  85.  
  86.     printf("udebug_set_evmask(0x%x)... ", UDEBUG_EM_ALL);
  87.     rc = udebug_set_evmask(phoneid, UDEBUG_EM_ALL);
  88.     printf("-> %d\n", rc);
  89.     if (rc < 0) return rc;
  90.  
  91.     return 0;
  92. }
  93.  
  94. int get_thread_list(void)
  95. {
  96.     int rc;
  97.     int tb_copied;
  98.     int tb_needed;
  99.     int i;
  100.  
  101.  
  102.     printf("send IPC_M_DEBUG_THREAD_READ message\n");
  103.     rc = udebug_thread_read(phoneid, (unsigned)thread_hash_buf,
  104.         THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed);
  105.     printf("-> %d\n", rc);
  106.     if (rc < 0) return rc;
  107.  
  108.     n_threads = tb_copied / sizeof(unsigned);
  109.  
  110.     printf("thread IDs:");
  111.     for (i=0; i<n_threads; i++) {
  112.         printf(" %u", thread_hash_buf[i]);
  113.     }
  114.     printf("\ntotal of %u threads\n", tb_needed/sizeof(unsigned));
  115.  
  116.     return 0;
  117. }
  118.  
  119. void print_sc_retval(int retval, rv_type_t rv_type)
  120. {
  121.     printf (" -> ");
  122.     if (rv_type == RV_INTEGER) {
  123.         printf("%d", retval);
  124.     } else if (rv_type == RV_HASH) {
  125.         printf("0x%08x", retval);
  126.     } else if (rv_type == RV_ERRNO) {
  127.         if (retval >= -15 && retval <= 0) {
  128.             printf("%d %s (%s)", retval,
  129.                 err_desc[retval].name,
  130.                 err_desc[retval].desc);
  131.         } else {
  132.             printf("%d", retval);
  133.         }
  134.     } else if (rv_type == RV_INT_ERRNO) {
  135.         if (retval >= -15 && retval < 0) {
  136.             printf("%d %s (%s)", retval,
  137.                 err_desc[retval].name,
  138.                 err_desc[retval].desc);
  139.         } else {
  140.             printf("%d", retval);
  141.         }
  142.     }
  143.     putchar('\n');
  144. }
  145.  
  146. void print_sc_args(unsigned *sc_args, int n)
  147. {
  148.     int i;
  149.  
  150.     putchar('(');
  151.     if (n > 0) printf("%d", sc_args[0]);
  152.     for (i=1; i<n; i++) {
  153.         printf(", %d", sc_args[i]);
  154.     }
  155.     putchar(')');
  156. }
  157.  
  158. void sc_ipc_call_async_fast(unsigned *sc_args, int sc_rc)
  159. {
  160.     ipc_call_t call;
  161.     int phoneid;
  162.    
  163.     if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
  164.         return;
  165.  
  166.     phoneid = sc_args[0];
  167.  
  168.     IPC_SET_METHOD(call, sc_args[1]);
  169.     IPC_SET_ARG1(call, sc_args[2]);
  170.     IPC_SET_ARG2(call, sc_args[3]);
  171.     IPC_SET_ARG3(call, sc_args[4]);
  172.     IPC_SET_ARG4(call, sc_args[5]);
  173.     IPC_SET_ARG5(call, 0);
  174.  
  175.     ipcp_call_out(phoneid, &call, sc_rc);
  176. }
  177.  
  178. void sc_ipc_call_async_slow(unsigned *sc_args, int sc_rc)
  179. {
  180.     ipc_call_t call;
  181.     int rc;
  182.  
  183.     if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
  184.         return;
  185.  
  186.     memset(&call, 0, sizeof(call));
  187.     rc = udebug_mem_read(phoneid, &call.args, sc_args[1], sizeof(call.args));
  188.  
  189.     if (rc >= 0) {
  190.         ipcp_call_out(sc_args[0], &call, sc_rc);
  191.     }
  192. }
  193.  
  194. void sc_ipc_call_sync_fast(unsigned *sc_args)
  195. {
  196.     ipc_call_t question, reply;
  197.     int rc;
  198.     int phoneidx;
  199.  
  200. //  printf("sc_ipc_call_sync_fast()\n");
  201.     phoneidx = sc_args[0];
  202.  
  203.     IPC_SET_METHOD(question, sc_args[1]);
  204.     IPC_SET_ARG1(question, sc_args[2]);
  205.     IPC_SET_ARG2(question, sc_args[3]);
  206.     IPC_SET_ARG3(question, sc_args[4]);
  207.     IPC_SET_ARG4(question, 0);
  208.     IPC_SET_ARG5(question, 0);
  209.  
  210. //  printf("memset\n");
  211.     memset(&reply, 0, sizeof(reply));
  212. //  printf("udebug_mem_read(phone=%d, buffer_ptr=%u, src_addr=%d, n=%d\n",
  213. //      phoneid, &reply.args, sc_args[5], sizeof(reply.args));
  214.     rc = udebug_mem_read(phoneid, &reply.args, sc_args[5], sizeof(reply.args));
  215. //  printf("dmr->%d\n", rc);
  216.     if (rc < 0) return;
  217.  
  218. //  printf("call ipc_call_sync\n");
  219.     ipcp_call_sync(phoneidx, &question, &reply);
  220. }
  221.  
  222. void sc_ipc_call_sync_slow(unsigned *sc_args)
  223. {
  224.     ipc_call_t question, reply;
  225.     int rc;
  226.  
  227.     memset(&question, 0, sizeof(question));
  228.     rc = udebug_mem_read(phoneid, &question.args, sc_args[1], sizeof(question.args));
  229.     printf("dmr->%d\n", rc);
  230.     if (rc < 0) return;
  231.  
  232.     memset(&reply, 0, sizeof(reply));
  233.     rc = udebug_mem_read(phoneid, &reply.args, sc_args[2], sizeof(reply.args));
  234.     printf("dmr->%d\n", rc);
  235.     if (rc < 0) return;
  236.  
  237.     ipcp_call_sync(sc_args[0], &question, &reply);
  238. }
  239.  
  240. void sc_ipc_wait(unsigned *sc_args, int sc_rc)
  241. {
  242.     ipc_call_t call;
  243.     int rc;
  244.  
  245.     if (sc_rc == 0) return 0;
  246.  
  247.     memset(&call, 0, sizeof(call));
  248.     rc = udebug_mem_read(phoneid, &call, sc_args[0], sizeof(call));
  249. //  printf("udebug_mem_read(phone %d, dest %d, app-mem src %d, size %d -> %d\n",
  250. //      phoneid, (int)&call, sc_args[0], sizeof(call), rc);
  251.  
  252.     if (rc >= 0) {
  253.         ipcp_call_in(&call, sc_rc);
  254.     }
  255. }
  256.  
  257. void event_syscall_b(unsigned thread_id, unsigned thread_hash,  unsigned sc_id, int sc_rc)
  258. {
  259.     unsigned sc_args[6];
  260.     int rv_type;
  261.     int rc;
  262.  
  263.     /* Read syscall arguments */
  264.     rc = udebug_args_read(phoneid, thread_hash, sc_args);
  265.  
  266.     async_serialize_start();
  267.  
  268. //  printf("[%d] ", thread_id);
  269.  
  270.     if (rc < 0) {
  271.         printf("error\n");
  272.         async_serialize_end();
  273.         return;
  274.     }
  275.  
  276.     /* Print syscall name, id and arguments */
  277.     printf("%s", syscall_desc[sc_id].name);
  278.     print_sc_args(sc_args, syscall_desc[sc_id].n_args);
  279.  
  280.     async_serialize_end();
  281. }
  282.  
  283. void event_syscall_e(unsigned thread_id, unsigned thread_hash,  unsigned sc_id, int sc_rc)
  284. {
  285.     unsigned sc_args[6];
  286.     int rv_type;
  287.     int rc;
  288.  
  289.     /* Read syscall arguments */
  290.     rc = udebug_args_read(phoneid, thread_hash, sc_args);
  291.  
  292.     async_serialize_start();
  293.  
  294. //  printf("[%d] ", thread_id);
  295.  
  296.     if (rc < 0) {
  297.         printf("error\n");
  298.         async_serialize_end();
  299.         return;
  300.     }
  301.  
  302.     rv_type = syscall_desc[sc_id].rv_type;
  303.     print_sc_retval(sc_rc, rv_type);
  304.  
  305.     switch (sc_id) {
  306.     case SYS_IPC_CALL_ASYNC_FAST:
  307.         sc_ipc_call_async_fast(sc_args, sc_rc);
  308.         break;
  309.     case SYS_IPC_CALL_ASYNC_SLOW:
  310.         sc_ipc_call_async_slow(sc_args, sc_rc);
  311.         break;
  312.     case SYS_IPC_CALL_SYNC_FAST:
  313.         sc_ipc_call_sync_fast(sc_args);
  314.         break;
  315.     case SYS_IPC_CALL_SYNC_SLOW:
  316.         sc_ipc_call_sync_slow(sc_args);
  317.         break;
  318.     case SYS_IPC_WAIT:
  319.         sc_ipc_wait(sc_args, sc_rc);
  320.         break;
  321.     default:
  322.         break;
  323.     }
  324.  
  325.     async_serialize_end();
  326. }
  327.  
  328. void event_thread_b(unsigned hash)
  329. {
  330.     async_serialize_start();
  331.     printf("new thread, hash 0x%x\n", hash);
  332.     async_serialize_end();
  333.  
  334.     thread_trace_start(hash);
  335. }
  336.  
  337. void trace_loop(void *thread_hash_arg)
  338. {
  339.     int rc;
  340.     unsigned ev_type;
  341.     unsigned thread_hash;
  342.     unsigned thread_id;
  343.     unsigned val0, val1;
  344.  
  345.     thread_hash = (unsigned)thread_hash_arg;
  346.     thread_id = next_thread_id++;
  347.  
  348.     printf("trace_loop(%d)\n", thread_id); 
  349.  
  350.     while (!abort_trace) {
  351.  
  352.         /* Run thread until an event occurs */
  353.         rc = udebug_go(phoneid, thread_hash,
  354.             &ev_type, &val0, &val1);
  355.  
  356. //      printf("rc = %d, ev_type=%d\n", rc, ev_type);
  357.         if (ev_type == UDEBUG_EVENT_FINISHED) {
  358.             printf("thread %u debugging finished\n", thread_id);
  359.             break;
  360.         }
  361.  
  362.         if (rc >= 0) {
  363.             switch (ev_type) {
  364.             case UDEBUG_EVENT_SYSCALL_B:
  365.                 event_syscall_b(thread_id, thread_hash, val0, (int)val1);
  366.                 break;
  367.             case UDEBUG_EVENT_SYSCALL_E:
  368.                 event_syscall_e(thread_id, thread_hash, val0, (int)val1);
  369.                 break;
  370.             case UDEBUG_EVENT_STOP:
  371.                 printf("stop event\n");
  372.                 printf("waiting for resume\n");
  373.                 while (paused) {
  374.                     usleep(1000000);
  375.                     fibril_yield();
  376.                     printf(".");
  377.                 }
  378.                 printf("resumed\n");
  379.                 break;
  380.             case UDEBUG_EVENT_THREAD_B:
  381.                 event_thread_b(val0);
  382.                 break;
  383.             case UDEBUG_EVENT_THREAD_E:
  384.                 printf("thread 0x%x exited\n", val0);
  385.                 abort_trace = 1;
  386.                 break;
  387.             default:
  388.                 printf("unknown event type %d\n", ev_type);
  389.                 break;
  390.             }
  391.         }
  392.  
  393.     }
  394.  
  395.     printf("trace_loop(%d) exiting\n", thread_id);
  396. }
  397.  
  398. void thread_trace_start(unsigned thread_hash)
  399. {
  400.     fid_t fid;
  401.  
  402.     thash = thread_hash;
  403.  
  404.     fid = fibril_create(trace_loop, (void *)thread_hash);
  405.     if (fid == 0) {
  406.         printf("Warning: Failed creating fibril\n");
  407.     }
  408.     fibril_add_ready(fid);
  409. }
  410.  
  411. void trace_active_task(void)
  412. {
  413.     int taskid;
  414.     int i;
  415.     int rc;
  416.     int c;
  417.  
  418.     printf("Syscall Tracer\n");
  419.     printf("Press 'c' to connect\n");
  420.     while ((i = getchar()) != 'c')
  421.         putchar(i);
  422.  
  423.     taskid = 14;
  424.     rc = task_connect(taskid);
  425.     if (rc < 0) {
  426.         printf("Failed to connect to task %d\n", taskid);
  427.         return;
  428.     }
  429.  
  430.     printf("Connected to task %d\n", taskid);
  431.  
  432.     ipcp_init();
  433.     ipcp_connection_set(1, 0, proto_console);
  434.  
  435.     rc = get_thread_list();
  436.     if (rc < 0) {
  437.         printf("Failed to get thread list (error %d)\n", rc);
  438.         return;
  439.     }
  440.  
  441.     abort_trace = 0;
  442.  
  443.     for (i = 0; i < n_threads; i++) {
  444.         thread_trace_start(thread_hash_buf[i]);
  445.     }
  446.  
  447.     while(1) {
  448.         c = getchar();
  449.         if (c == 'q') break;
  450.         if (c == 'p') {
  451.             paused = 1;
  452.             rc = udebug_stop(phoneid, thash);
  453.             printf("stop -> %d\n", rc);
  454.         }
  455.         if (c == 'r') {
  456.             paused = 0;
  457.         }
  458.     }
  459.  
  460.     printf("terminate debugging session...\n");
  461.     abort_trace = 1;
  462.     udebug_end(phoneid);
  463.     ipc_hangup(phoneid);
  464.  
  465.     ipcp_cleanup();
  466.  
  467.     printf("done\n");
  468.     return;
  469. }
  470.  
  471. static void main_init(void)
  472. {
  473.     proto_t *p;
  474.     oper_t *o;
  475.  
  476.     next_thread_id = 1;
  477.     paused = 0;
  478.  
  479.     proto_init();
  480.  
  481.     p = proto_new("vfs");
  482.     o = oper_new("read");
  483.     proto_add_oper(p, VFS_READ, o);
  484.     o = oper_new("write");
  485.     proto_add_oper(p, VFS_WRITE, o);
  486.     o = oper_new("truncate");
  487.     proto_add_oper(p, VFS_TRUNCATE, o);
  488.     o = oper_new("mount");
  489.     proto_add_oper(p, VFS_MOUNT, o);
  490.     o = oper_new("unmount");
  491.     proto_add_oper(p, VFS_UNMOUNT, o);
  492.  
  493.     proto_register(SERVICE_VFS, p);
  494.  
  495.     p = proto_new("console");
  496.     o = oper_new("getchar");
  497.     proto_add_oper(p, CONSOLE_GETCHAR, o);
  498.     o = oper_new("putchar");
  499.     proto_add_oper(p, CONSOLE_PUTCHAR, o);
  500.     o = oper_new("clear");
  501.     proto_add_oper(p, CONSOLE_CLEAR, o);
  502.     o = oper_new("goto");
  503.     proto_add_oper(p, CONSOLE_GOTO, o);
  504.     o = oper_new("getsize");
  505.     proto_add_oper(p, CONSOLE_GETSIZE, o);
  506.     o = oper_new("flush");
  507.     proto_add_oper(p, CONSOLE_FLUSH, o);
  508.     o = oper_new("set_style");
  509.     proto_add_oper(p, CONSOLE_SET_STYLE, o);
  510.     o = oper_new("flush");
  511.     proto_add_oper(p, CONSOLE_CURSOR_VISIBILITY, o);
  512.     o = oper_new("cursor_visibility");
  513.     proto_add_oper(p, CONSOLE_FLUSH, o);
  514.  
  515.     proto_console = p;
  516.     proto_register(SERVICE_CONSOLE, p);
  517. }
  518.  
  519. int main(void)
  520. {
  521.     main_init();
  522.  
  523.     while (1) {
  524.         trace_active_task();
  525.     }
  526. }
  527.  
  528. /** @}
  529.  */
  530.