Subversion Repositories HelenOS

Rev

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

  1. /** @addtogroup sctrace
  2.  * @{
  3.  */
  4. /** @file
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <syscall.h>
  11. #include <ipc/ipc.h>
  12. #include <fibril.h>
  13. #include <errno.h>
  14. #include <udebug.h>
  15. #include <async.h>
  16.  
  17. // Temporary: service and method names
  18. #include "proto.h"
  19. #include <ipc/services.h>
  20. #include "../../srv/vfs/vfs.h"
  21. #include "../../srv/console/console.h"
  22.  
  23. #include "syscalls.h"
  24. #include "ipcp.h"
  25. #include "errors.h"
  26. #include "debug_api.h"
  27.  
  28. #define THBUF_SIZE 64
  29. unsigned thread_hash_buf[THBUF_SIZE];
  30. unsigned n_threads;
  31.  
  32. int next_thread_id;
  33.  
  34. int phoneid;
  35. int abort_trace;
  36.  
  37. void thread_trace_start(unsigned thread_hash);
  38.  
  39. static proto_t *proto_console;
  40.  
  41. int task_connect(int taskid)
  42. {
  43.     int rc;
  44.  
  45.     printf("ipc_connect_task(%d)...\n", taskid);
  46.     rc = ipc_connect_kbox(taskid);
  47.     printf("-> %d\n", rc);
  48.     phoneid = rc;
  49.     if (rc < 0) return rc;
  50.  
  51.     printf("debug_begin()\n");
  52.     rc = debug_begin(phoneid);
  53.     printf("-> %d\n", rc);
  54.     if (rc < 0) return rc;
  55.  
  56.     return 0;
  57. }
  58.  
  59. int get_thread_list(void)
  60. {
  61.     int rc;
  62.     int tb_copied;
  63.     int tb_needed;
  64.     int i;
  65.  
  66.  
  67.     printf("send IPC_M_DEBUG_THREAD_READ message\n");
  68.     rc = debug_thread_read(phoneid, (unsigned)thread_hash_buf,
  69.         THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed);
  70.     printf("-> %d\n", rc);
  71.     if (rc < 0) return rc;
  72.  
  73.     n_threads = tb_copied / sizeof(unsigned);
  74.  
  75.     printf("thread IDs:");
  76.     for (i=0; i<n_threads; i++) {
  77.         printf(" %u", thread_hash_buf[i]);
  78.     }
  79.     printf("\ntotal of %u threads\n", tb_needed/sizeof(unsigned));
  80.  
  81.     return 0;
  82. }
  83.  
  84. void print_sc_retval(int retval, rv_type_t rv_type)
  85. {
  86.     printf (" -> ");
  87.     if (rv_type == RV_INTEGER) {
  88.         printf("%d", retval);
  89.     } else if (rv_type == RV_HASH) {
  90.         printf("0x%08x", retval);
  91.     } else if (rv_type == RV_ERRNO) {
  92.         if (retval >= -15 && retval <= 0) {
  93.             printf("%d %s (%s)", retval,
  94.                 err_desc[retval].name,
  95.                 err_desc[retval].desc);
  96.         } else {
  97.             printf("%d", retval);
  98.         }
  99.     } else if (rv_type == RV_INT_ERRNO) {
  100.         if (retval >= -15 && retval < 0) {
  101.             printf("%d %s (%s)", retval,
  102.                 err_desc[retval].name,
  103.                 err_desc[retval].desc);
  104.         } else {
  105.             printf("%d", retval);
  106.         }
  107.     }
  108.     putchar('\n');
  109. }
  110.  
  111. void print_sc_args(unsigned *sc_args, int n)
  112. {
  113.     int i;
  114.  
  115.     putchar('(');
  116.     if (n > 0) printf("%d", sc_args[0]);
  117.     for (i=1; i<n; i++) {
  118.         printf(", %d", sc_args[i]);
  119.     }
  120.     putchar(')');
  121. }
  122.  
  123. void sc_ipc_call_async_fast(unsigned *sc_args, int sc_rc)
  124. {
  125.     ipc_call_t call;
  126.     int phoneid;
  127.    
  128.     if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
  129.         return;
  130.  
  131.     phoneid = sc_args[0];
  132.  
  133.     IPC_SET_METHOD(call, sc_args[1]);
  134.     IPC_SET_ARG1(call, sc_args[2]);
  135.     IPC_SET_ARG2(call, sc_args[3]);
  136.     IPC_SET_ARG3(call, sc_args[4]);
  137.     IPC_SET_ARG4(call, sc_args[5]);
  138.     IPC_SET_ARG5(call, 0);
  139.  
  140.     ipcp_call_out(phoneid, &call, sc_rc);
  141. }
  142.  
  143. void sc_ipc_call_async_slow(unsigned *sc_args, int sc_rc)
  144. {
  145.     ipc_call_t call;
  146.     int rc;
  147.  
  148.     if (sc_rc == IPC_CALLRET_FATAL || sc_rc == IPC_CALLRET_TEMPORARY)
  149.         return;
  150.  
  151.     memset(&call, 0, sizeof(call));
  152.     rc = debug_mem_read(phoneid, &call.args, sc_args[1], sizeof(call.args));
  153.  
  154.     if (rc >= 0) {
  155.         ipcp_call_out(sc_args[0], &call, sc_rc);
  156.     }
  157. }
  158.  
  159. void sc_ipc_call_sync_fast(unsigned *sc_args)
  160. {
  161.     ipc_call_t question, reply;
  162.     int rc;
  163.     int phoneidx;
  164.  
  165. //  printf("sc_ipc_call_sync_fast()\n");
  166.     phoneidx = sc_args[0];
  167.  
  168.     IPC_SET_METHOD(question, sc_args[1]);
  169.     IPC_SET_ARG1(question, sc_args[2]);
  170.     IPC_SET_ARG2(question, sc_args[3]);
  171.     IPC_SET_ARG3(question, sc_args[4]);
  172.     IPC_SET_ARG4(question, 0);
  173.     IPC_SET_ARG5(question, 0);
  174.  
  175. //  printf("memset\n");
  176.     memset(&reply, 0, sizeof(reply));
  177. //  printf("debug_mem_read(phone=%d, buffer_ptr=%u, src_addr=%d, n=%d\n",
  178. //      phoneid, &reply.args, sc_args[5], sizeof(reply.args));
  179.     rc = debug_mem_read(phoneid, &reply.args, sc_args[5], sizeof(reply.args));
  180. //  printf("dmr->%d\n", rc);
  181.     if (rc < 0) return;
  182.  
  183. //  printf("call ipc_call_sync\n");
  184.     ipcp_call_sync(phoneidx, &question, &reply);
  185. }
  186.  
  187. void sc_ipc_call_sync_slow(unsigned *sc_args)
  188. {
  189.     ipc_call_t question, reply;
  190.     int rc;
  191.  
  192.     memset(&question, 0, sizeof(question));
  193.     rc = debug_mem_read(phoneid, &question.args, sc_args[1], sizeof(question.args));
  194.     printf("dmr->%d\n", rc);
  195.     if (rc < 0) return;
  196.  
  197.     memset(&reply, 0, sizeof(reply));
  198.     rc = debug_mem_read(phoneid, &reply.args, sc_args[2], sizeof(reply.args));
  199.     printf("dmr->%d\n", rc);
  200.     if (rc < 0) return;
  201.  
  202.     ipcp_call_sync(sc_args[0], &question, &reply);
  203. }
  204.  
  205. void sc_ipc_wait(unsigned *sc_args, int sc_rc)
  206. {
  207.     ipc_call_t call;
  208.     int rc;
  209.  
  210.     if (sc_rc == 0) return 0;
  211.  
  212.     memset(&call, 0, sizeof(call));
  213.     rc = debug_mem_read(phoneid, &call, sc_args[0], sizeof(call));
  214. //  printf("debug_mem_read(phone %d, dest %d, app-mem src %d, size %d -> %d\n",
  215. //      phoneid, (int)&call, sc_args[0], sizeof(call), rc);
  216.  
  217.     if (rc >= 0) {
  218.         ipcp_call_in(&call, sc_rc);
  219.     }
  220. }
  221.  
  222. void event_syscall(unsigned thread_id, unsigned thread_hash,  unsigned sc_id, int sc_rc)
  223. {
  224.     unsigned sc_args[6];
  225.     int rv_type;
  226.     int rc;
  227.  
  228.     /* Read syscall arguments */
  229.     rc = debug_args_read(phoneid, thread_hash, sc_args);
  230.  
  231.     async_serialize_start();
  232.  
  233. //  printf("[%d] ", thread_id);
  234.  
  235.     if (rc < 0) {
  236.         printf("error\n");
  237.         async_serialize_end();
  238.         return;
  239.     }
  240.  
  241.     /* Print syscall name, id and arguments */
  242.     printf("%s", syscall_desc[sc_id].name);
  243.     print_sc_args(sc_args, syscall_desc[sc_id].n_args);
  244.     rv_type = syscall_desc[sc_id].rv_type;
  245.     print_sc_retval(sc_rc, rv_type);
  246.  
  247.     switch (sc_id) {
  248.     case SYS_IPC_CALL_ASYNC_FAST:
  249.         sc_ipc_call_async_fast(sc_args, sc_rc);
  250.         break;
  251.     case SYS_IPC_CALL_ASYNC_SLOW:
  252.         sc_ipc_call_async_slow(sc_args, sc_rc);
  253.         break;
  254.     case SYS_IPC_CALL_SYNC_FAST:
  255.         sc_ipc_call_sync_fast(sc_args);
  256.         break;
  257.     case SYS_IPC_CALL_SYNC_SLOW:
  258.         sc_ipc_call_sync_slow(sc_args);
  259.         break;
  260.     case SYS_IPC_WAIT:
  261.         sc_ipc_wait(sc_args, sc_rc);
  262.         break;
  263.     default:
  264.         break;
  265.     }
  266.  
  267.     async_serialize_end();
  268. }
  269.  
  270. void event_new_thread(unsigned hash)
  271. {
  272.     async_serialize_start();
  273.     printf("new thread, hash 0x%x\n", hash);
  274.     async_serialize_end();
  275.  
  276.     thread_trace_start(hash);
  277. }
  278.  
  279. void trace_loop(void *thread_hash_arg)
  280. {
  281.     int rc;
  282.     unsigned ev_type;
  283.     unsigned thread_hash;
  284.     unsigned thread_id;
  285.     unsigned val0, val1;
  286.  
  287.     thread_hash = (unsigned)thread_hash_arg;
  288.     thread_id = next_thread_id++;
  289.  
  290.     printf("trace_loop(%d)\n", thread_id); 
  291.  
  292.     while (!abort_trace) {
  293.  
  294.         /* Run thread until an event occurs */
  295.         rc = debug_go(phoneid, thread_hash,
  296.             &ev_type, &val0, &val1);
  297.  
  298. //      printf("rc = %d, ev_type=%d\n", rc, ev_type);
  299.         if (ev_type == UDEBUG_EVENT_FINISHED) {
  300.             printf("thread %u debugging finished\n", thread_id);
  301.             break;
  302.         }
  303.  
  304.         if (rc >= 0) {
  305.             switch (ev_type) {
  306.             case UDEBUG_EVENT_SYSCALL:
  307.                 event_syscall(thread_id, thread_hash, val0, (int)val1);
  308.                 break;
  309.             case UDEBUG_EVENT_NEW_THREAD:
  310.                 event_new_thread(val0);
  311.                 break;
  312.             default:
  313.                 printf("unknown event type %d\n", ev_type);
  314.                 break;
  315.             }
  316.         }
  317.  
  318.     }
  319.  
  320.     printf("trace_loop(%d) exiting\n", thread_id);
  321. }
  322.  
  323. void thread_trace_start(unsigned thread_hash)
  324. {
  325.     fid_t fid;
  326.  
  327.     fid = fibril_create(trace_loop, (void *)thread_hash);
  328.     if (fid == 0) {
  329.         printf("Warning: Failed creating fibril\n");
  330.     }
  331.     fibril_add_ready(fid);
  332. }
  333.  
  334. void trace_active_task(void)
  335. {
  336.     int taskid;
  337.     int i;
  338.     int rc;
  339.  
  340.     printf("Syscall Tracer\n");
  341.     printf("Press 'c' to connect\n");
  342.     while ((i = getchar()) != 'c')
  343.         putchar(i);
  344.  
  345.     taskid = 14;
  346.     rc = task_connect(taskid);
  347.     if (rc < 0) {
  348.         printf("Failed to connect to task %d\n", taskid);
  349.         return;
  350.     }
  351.  
  352.     printf("Connected to task %d\n", taskid);
  353.  
  354.     ipcp_init();
  355.     ipcp_connection_set(1, 0, proto_console);
  356.  
  357.     rc = get_thread_list();
  358.     if (rc < 0) {
  359.         printf("Failed to get thread list (error %d)\n", rc);
  360.         return;
  361.     }
  362.  
  363.     abort_trace = 0;
  364.  
  365.     for (i = 0; i < n_threads; i++) {
  366.         thread_trace_start(thread_hash_buf[i]);
  367.     }
  368.  
  369.     getchar();
  370.  
  371.     printf("terminate debugging session...\n");
  372.     abort_trace = 1;
  373.     debug_end(phoneid);
  374.     ipc_hangup(phoneid);
  375.  
  376.     ipcp_cleanup();
  377.  
  378.     printf("done\n");
  379.     return;
  380. }
  381.  
  382. static void main_init(void)
  383. {
  384.     proto_t *p;
  385.     oper_t *o;
  386.  
  387.     next_thread_id = 1;
  388.  
  389.     proto_init();
  390.  
  391.     p = proto_new("vfs");
  392.     o = oper_new("read");
  393.     proto_add_oper(p, VFS_READ, o);
  394.     o = oper_new("write");
  395.     proto_add_oper(p, VFS_WRITE, o);
  396.     o = oper_new("truncate");
  397.     proto_add_oper(p, VFS_TRUNCATE, o);
  398.     o = oper_new("mount");
  399.     proto_add_oper(p, VFS_MOUNT, o);
  400.     o = oper_new("unmount");
  401.     proto_add_oper(p, VFS_UNMOUNT, o);
  402.  
  403.     proto_register(SERVICE_VFS, p);
  404.  
  405.     p = proto_new("console");
  406.     o = oper_new("getchar");
  407.     proto_add_oper(p, CONSOLE_GETCHAR, o);
  408.     o = oper_new("putchar");
  409.     proto_add_oper(p, CONSOLE_PUTCHAR, o);
  410.     o = oper_new("clear");
  411.     proto_add_oper(p, CONSOLE_CLEAR, o);
  412.     o = oper_new("goto");
  413.     proto_add_oper(p, CONSOLE_GOTO, o);
  414.     o = oper_new("getsize");
  415.     proto_add_oper(p, CONSOLE_GETSIZE, o);
  416.     o = oper_new("flush");
  417.     proto_add_oper(p, CONSOLE_FLUSH, o);
  418.     o = oper_new("set_style");
  419.     proto_add_oper(p, CONSOLE_SET_STYLE, o);
  420.     o = oper_new("flush");
  421.     proto_add_oper(p, CONSOLE_CURSOR_VISIBILITY, o);
  422.     o = oper_new("cursor_visibility");
  423.     proto_add_oper(p, CONSOLE_FLUSH, o);
  424.  
  425.     proto_console = p;
  426.     proto_register(SERVICE_CONSOLE, p);
  427. }
  428.  
  429. int main(void)
  430. {
  431.     main_init();
  432.  
  433.     while (1) {
  434.         trace_active_task();
  435.     }
  436. }
  437.  
  438. /** @}
  439.  */
  440.