Subversion Repositories HelenOS

Rev

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