Subversion Repositories HelenOS

Rev

Rev 2937 | Rev 2939 | 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 debug
  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. #include <string.h>
  45.  
  46. #include "cmd.h"
  47. #include "cons.h"
  48. #include "dthread.h"
  49. #include "include/arch.h"
  50. #include "fib_synch.h"
  51. #include "main.h"
  52.  
  53. void thread_debug_start(unsigned thread_hash);
  54.  
  55. #define IN_BUF_SIZE 64
  56. static char in_buf[IN_BUF_SIZE];
  57.  
  58. #define MAX_ARGC 10
  59. int cmd_argc;
  60. char *cmd_argv[MAX_ARGC + 1];   /* need one spare field for cmd_split() */
  61.  
  62. #define THBUF_SIZE 64
  63. thash_t thread_hash_buf[THBUF_SIZE];
  64.  
  65. int next_thread_id;
  66.  
  67. int app_phone;
  68. volatile bool abort_debug;
  69.  
  70. thash_t thash;
  71. volatile int paused;
  72.  
  73. breakpoint_t brk_list[MAX_BRKPTS];
  74. int lifted_brkpt;
  75.  
  76. fcv_t go_cv;
  77.  
  78. static void command_split(char *cmd_str)
  79. {
  80.     char *p = cmd_str;
  81.  
  82.     if (*p == '\0') {
  83.         cmd_argc = 0;
  84.         return;
  85.     }
  86.  
  87.     cmd_argc = 1;
  88.     cmd_argv[0] = p;
  89.  
  90.     while (*p != '\0') {
  91.         if (*p == ' ') {
  92.             cmd_argv[cmd_argc++] = p + 1;
  93.             *p = '\0';
  94.         }
  95.         ++p;
  96.     }
  97. }
  98.  
  99. static void command_run(void)
  100. {
  101.     int i;
  102.     int cmp_len;
  103.     int len;
  104.  
  105.     int idx_found;
  106.     int num_found;
  107.  
  108.     len = strlen(cmd_argv[0]);
  109.     cmp_len = 1;
  110.  
  111.     while (cmp_len <= len + 1) {
  112.  
  113.         num_found = 0;
  114.         i = 0;
  115.         while (cmd_table[i].name != NULL) {
  116.             if (strncmp(cmd_table[i].name, cmd_argv[0], cmp_len) == 0) {
  117.                 idx_found = i;
  118.                 ++num_found;
  119.             }
  120.             ++i;
  121.         }
  122.  
  123.         if (num_found < 2) break;
  124.  
  125.         --cmp_len;
  126.     }
  127.  
  128.     if (num_found == 0) {
  129.         cons_printf("Unknown command. Try one of:\n");
  130.         cmd_help(0, NULL);
  131.         return;
  132.     }
  133.  
  134.     if (cmd_argc - 1 != cmd_table[idx_found].argc) {
  135.         cons_printf("Command '%s' expects %d arguments\n",
  136.         cmd_table[idx_found].name, cmd_table[idx_found].argc);
  137.         return;
  138.     }
  139.  
  140.     (*cmd_table[idx_found].proc)(cmd_argc, cmd_argv);
  141. }
  142.  
  143. static void thread_stop(void)
  144. {
  145.     cons_printf("[t] stopped\n");
  146.     fcv_wait(&go_cv);
  147.     cons_printf("[t] go\n");
  148. }
  149.  
  150. /*
  151.  * Called by a fibril (from arch code) when a breakpoint is hit.
  152.  */
  153. void breakpoint_hit(void)
  154. {
  155.     cons_printf("breakpoint hit\n");
  156.     thread_stop();
  157. }
  158.  
  159. static int task_connect(int taskid)
  160. {
  161.     int rc;
  162.     unsigned evmask;
  163.  
  164.     cons_printf("ipc_connect_kbox(%d)... ", taskid);
  165.     rc = ipc_connect_kbox(taskid);
  166.     cons_printf("-> %d\n", rc);
  167.     app_phone = rc;
  168.     if (rc < 0) return rc;
  169.  
  170.     cons_printf("udebug_begin()... ");
  171.     rc = udebug_begin(app_phone);
  172.     cons_printf("-> %d\n", rc);
  173.     if (rc < 0) return rc;
  174.  
  175.     evmask = UDEBUG_EM_ALL & ~(UDEBUG_EM_SYSCALL_B | UDEBUG_EM_SYSCALL_E);
  176.     cons_printf("udebug_set_evmask(0x%x)... ", evmask);
  177.     rc = udebug_set_evmask(app_phone, evmask);
  178.     cons_printf("-> %d\n", rc);
  179.     if (rc < 0) return rc;
  180.  
  181.     return 0;
  182. }
  183.  
  184. static int get_thread_list(void)
  185. {
  186.     int rc;
  187.     int tb_copied;
  188.     int tb_needed;
  189.     int i;
  190.     int n;
  191.  
  192.     cons_printf("send IPC_M_DEBUG_THREAD_READ message\n");
  193.     rc = udebug_thread_read(app_phone, (unsigned)thread_hash_buf,
  194.         THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed);
  195.     cons_printf("-> %d\n", rc);
  196.     if (rc < 0) return rc;
  197.  
  198.     n = tb_copied / sizeof(unsigned);
  199.  
  200.     cons_printf("thread IDs:");
  201.     for (i=0; i<n; i++) {
  202.         cons_printf(" %u", thread_hash_buf[i]);
  203.     }
  204.     cons_printf("\ntotal of %u threads\n", tb_needed/sizeof(unsigned));
  205.  
  206.     return n;
  207. }
  208.  
  209. void event_thread_b(unsigned hash)
  210. {
  211.     async_serialize_start();
  212.     cons_printf("new thread, hash 0x%x\n", hash);
  213.     async_serialize_end();
  214.  
  215.     thread_debug_start(hash);
  216. }
  217.  
  218. static unsigned buffer[1024];
  219.  
  220. static void debug_event(thash_t thash, udebug_event_t ev_type, sysarg_t val0)
  221. {
  222.     switch (ev_type) {
  223.     case UDEBUG_EVENT_STOP:
  224.         cons_printf("stop event\n");
  225.         cons_printf("waiting for resume\n");
  226.         while (paused) {
  227.             usleep(1000000);
  228.             fibril_yield();
  229.             cons_printf(".");
  230.         }
  231.         cons_printf("resumed\n");
  232.         break;
  233.     case UDEBUG_EVENT_THREAD_B:
  234.         event_thread_b(val0);
  235.         break;
  236.     case UDEBUG_EVENT_THREAD_E:
  237.         cons_printf("thread 0x%x exited\n", val0);
  238.         abort_debug = true;
  239.         break;
  240.     case UDEBUG_EVENT_BREAKPOINT:
  241.         arch_event_breakpoint(thash);
  242.         break;
  243.     case UDEBUG_EVENT_TRAP:
  244.         arch_event_trap(thash);
  245.         break;
  246.     default:
  247.         cons_printf("unknown event type %d\n", ev_type);
  248.         break;
  249.     }
  250. }
  251.  
  252. void debug_loop(void *dt_arg)
  253. {
  254.     int rc;
  255.     udebug_event_t ev_type;
  256.     unsigned thread_buf_idx;
  257.     unsigned val0, val1;
  258.     dthread_t *dt;
  259.  
  260.     dt = (dthread_t *)dt_arg;
  261.  
  262.     cons_printf("debug_loop(%d)\n", dt->id);
  263.  
  264.     while (!abort_debug) {
  265.  
  266.         /* Run thread until an event occurs */
  267.         rc = udebug_go(app_phone, dt->hash, &ev_type, &val0, &val1);
  268.  
  269.         if (ev_type == UDEBUG_EVENT_FINISHED) {
  270.             cons_printf("thread %u debugging finished\n", dt->id);
  271.             break;
  272.         }
  273.         if (rc >= 0) debug_event(thash, ev_type, val0);
  274.     }
  275.  
  276.     cons_printf("debug_loop(%d) exiting\n", dt->id);
  277. }
  278.  
  279. void thread_debug_start(unsigned thash)
  280. {
  281.     fid_t fid;
  282.     dthread_t *dt;
  283.  
  284.     dt = dthread_new(thash);
  285.  
  286.     fid = fibril_create(debug_loop, (void *)dt);
  287.     if (fid == 0) {
  288.         cons_printf("Warning: Failed creating fibril\n");
  289.     }
  290.     fibril_add_ready(fid);
  291. }
  292.  
  293. void debug_active_task(void)
  294. {
  295.     int taskid;
  296.     int i;
  297.     int rc;
  298.     int c;
  299.  
  300.     cons_printf("Breakpoint Debugger\n");
  301.     cons_printf("Press 'c' to connect\n");
  302.     while ((i = getchar()) != 'c')
  303.         putchar(i);
  304.  
  305.     taskid = 14;
  306.     rc = task_connect(taskid);
  307.     if (rc < 0) {
  308.         cons_printf("Failed to connect to task %d\n", taskid);
  309.         return;
  310.     }
  311.  
  312.     cons_printf("Connected to task %d\n", taskid);
  313.  
  314.     rc = get_thread_list();
  315.     if (rc < 0) {
  316.         cons_printf("Failed to get thread list (error %d)\n", rc);
  317.         return;
  318.     }
  319.  
  320.     abort_debug = false;
  321.  
  322.     for (i = 0; i < rc; i++) {
  323.         thread_debug_start(thread_hash_buf[i]);
  324.     }
  325.  
  326.     while (!quit) {
  327.         cons_read_line(in_buf, IN_BUF_SIZE);
  328.         command_split(in_buf);
  329.         if (cmd_argc == 0) continue;
  330.  
  331.         command_run();
  332.     }
  333.  
  334.     cons_printf("terminate debugging session...\n");
  335.     abort_debug = true;
  336.     udebug_end(app_phone);
  337.     ipc_hangup(app_phone);
  338.  
  339.     cons_printf("done\n");
  340.     return;
  341. }
  342.  
  343. static void main_init(void)
  344. {
  345.     next_thread_id = 1;
  346.     paused = 0;
  347.    
  348.     list_initialize(&dthreads);
  349.     cwt = NULL;
  350.  
  351.     fcv_init(&go_cv);
  352. }
  353.  
  354. int main(void)
  355. {
  356.     main_init();
  357.  
  358.     while (1) {
  359.         debug_active_task();
  360.     }
  361. }
  362.  
  363. /** @}
  364.  */
  365.