Subversion Repositories HelenOS

Rev

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