Subversion Repositories HelenOS

Rev

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