Subversion Repositories HelenOS

Rev

Rev 2940 | Rev 2946 | 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.  
  63. int next_thread_id;
  64.  
  65. int app_phone;
  66. volatile bool abort_debug;
  67.  
  68. volatile int paused;
  69.  
  70. breakpoint_t brk_list[MAX_BRKPTS];
  71. int lifted_brkpt;
  72.  
  73. fcv_t go_cv;
  74.  
  75. static void command_split(char *cmd_str)
  76. {
  77.     char *p = cmd_str;
  78.  
  79.     if (*p == '\0') {
  80.         cmd_argc = 0;
  81.         return;
  82.     }
  83.  
  84.     cmd_argc = 1;
  85.     cmd_argv[0] = p;
  86.  
  87.     while (*p != '\0') {
  88.         if (*p == ' ') {
  89.             cmd_argv[cmd_argc++] = p + 1;
  90.             *p = '\0';
  91.         }
  92.         ++p;
  93.     }
  94. }
  95.  
  96. static void command_run(void)
  97. {
  98.     int i;
  99.     int cmp_len;
  100.     int len;
  101.  
  102.     int idx_found;
  103.     int num_found;
  104.  
  105.     len = strlen(cmd_argv[0]);
  106.     cmp_len = 1;
  107.  
  108.     while (cmp_len <= len + 1) {
  109.  
  110.         num_found = 0;
  111.         i = 0;
  112.         while (cmd_table[i].name != NULL) {
  113.             if (strncmp(cmd_table[i].name, cmd_argv[0], cmp_len) == 0) {
  114.                 idx_found = i;
  115.                 ++num_found;
  116.             }
  117.             ++i;
  118.         }
  119.  
  120.         if (num_found < 2) break;
  121.  
  122.         --cmp_len;
  123.     }
  124.  
  125.     if (num_found == 0) {
  126.         cons_printf("Unknown command. Try one of:\n");
  127.         cmd_help(0, NULL);
  128.         return;
  129.     }
  130.  
  131.     if (cmd_argc - 1 != cmd_table[idx_found].argc) {
  132.         cons_printf("Command '%s' expects %d arguments\n",
  133.         cmd_table[idx_found].name, cmd_table[idx_found].argc);
  134.         return;
  135.     }
  136.  
  137.     (*cmd_table[idx_found].proc)(cmd_argc, cmd_argv);
  138. }
  139.  
  140. static void thread_stop(void)
  141. {
  142.     dthread_t *dt;
  143.     uintptr_t pc;
  144.  
  145.     dt = dthread_get();
  146.     pc = dthread_get_pc(dt);
  147.     cons_printf("[thread %d] stopped at 0x%lx\n", dt->id, pc);
  148.     dthread_stop_me();
  149.     cons_printf("[thread %d] go\n", dt->id);
  150. }
  151.  
  152. /*
  153.  * Called by a fibril (from arch code) when a breakpoint is hit.
  154.  */
  155. void breakpoint_hit(void)
  156. {
  157.     cons_printf("breakpoint hit\n");
  158.     thread_stop();
  159. }
  160.  
  161. /*
  162.  * Called by a fibril (from arch code) when a single instruction
  163.  * in singlestep is executed
  164.  */
  165. void singlestep_hit(void)
  166. {
  167.     cons_printf("singlestep hit\n");
  168.     thread_stop();
  169. }
  170.  
  171. static int task_connect(int taskid)
  172. {
  173.     int rc;
  174.     unsigned evmask;
  175.  
  176.     cons_printf("ipc_connect_kbox(%d)... ", taskid);
  177.     rc = ipc_connect_kbox(taskid);
  178.     cons_printf("-> %d\n", rc);
  179.     app_phone = rc;
  180.     if (rc < 0) return rc;
  181.  
  182.     cons_printf("udebug_begin()... ");
  183.     rc = udebug_begin(app_phone);
  184.     cons_printf("-> %d\n", rc);
  185.     if (rc < 0) return rc;
  186.  
  187.     evmask = UDEBUG_EM_ALL & ~(UDEBUG_EM_SYSCALL_B | UDEBUG_EM_SYSCALL_E);
  188.     cons_printf("udebug_set_evmask(0x%x)... ", evmask);
  189.     rc = udebug_set_evmask(app_phone, evmask);
  190.     cons_printf("-> %d\n", rc);
  191.     if (rc < 0) return rc;
  192.  
  193.     return 0;
  194. }
  195.  
  196. #define THASH_BUF_INIT_LENGTH 32
  197.  
  198. static int get_thread_list(thash_t **thash_buf_ptr, int *n)
  199. {
  200.     int rc;
  201.     int tb_copied;
  202.     int tb_needed;
  203.     int i;
  204.     size_t tb_size;
  205.     thash_t *thash_buf;
  206.  
  207.     tb_size = THASH_BUF_INIT_LENGTH * sizeof(thash_t);
  208.     thash_buf = malloc(tb_size);
  209.  
  210.     rc = udebug_thread_read(app_phone, (sysarg_t)thash_buf,
  211.         tb_size, &tb_copied, &tb_needed);
  212.     if (rc < 0) return rc;
  213.  
  214.     if (tb_needed > tb_size) {
  215.         /* Larger buffer needed  */
  216.  
  217.         free(thash_buf);
  218.  
  219.         tb_size = tb_needed;
  220.         thash_buf = malloc(tb_size);
  221.  
  222.         if (!thash_buf) {
  223.             printf("malloc failed\n");
  224.             exit(1);
  225.         }
  226.  
  227.         /* Try again */
  228.        
  229.         rc = udebug_thread_read(app_phone, (sysarg_t)thash_buf,
  230.             tb_size, &tb_copied, &tb_needed);
  231.  
  232.         if (rc < 0) return rc;
  233.     }
  234.  
  235.     *n = tb_copied / sizeof(thash_t);
  236.  
  237.     cons_printf("thread hashes:");
  238.  
  239.     for (i = 0; i < *n; ++i) {
  240.         cons_printf("0x%x\n", thash_buf[i]);
  241.     }
  242.  
  243.     cons_printf("Total of %u threads\n", *n);
  244.  
  245.     *thash_buf_ptr = thash_buf;
  246.  
  247.     return 0;
  248. }
  249.  
  250. void event_thread_b(unsigned hash)
  251. {
  252.     async_serialize_start();
  253.     cons_printf("new thread, hash 0x%x\n", hash);
  254.     async_serialize_end();
  255.  
  256.     thread_debug_start(hash);
  257. }
  258.  
  259. static unsigned buffer[1024];
  260.  
  261. static void debug_event(thash_t thash, udebug_event_t ev_type, sysarg_t val0)
  262. {
  263.     switch (ev_type) {
  264.     case UDEBUG_EVENT_STOP:
  265.         cons_printf("stop event\n");
  266.         thread_stop();
  267.         break;
  268.     case UDEBUG_EVENT_THREAD_B:
  269.         event_thread_b(val0);
  270.         break;
  271.     case UDEBUG_EVENT_THREAD_E:
  272.         cons_printf("thread 0x%x exited\n", val0);
  273.         abort_debug = true;
  274.         break;
  275.     case UDEBUG_EVENT_BREAKPOINT:
  276.         arch_event_breakpoint(thash);
  277.         break;
  278.     case UDEBUG_EVENT_TRAP:
  279.         arch_event_trap(dthread_get());
  280.         break;
  281.     default:
  282.         cons_printf("unknown event type %d\n", ev_type);
  283.         break;
  284.     }
  285. }
  286.  
  287. void debug_loop(void *dt_arg)
  288. {
  289.     int rc;
  290.     udebug_event_t ev_type;
  291.     unsigned thread_buf_idx;
  292.     unsigned val0, val1;
  293.     dthread_t *dt;
  294.  
  295.     dt = (dthread_t *)dt_arg;
  296.  
  297.     cons_printf("debug_loop(%d)\n", dt->id);
  298.  
  299.     while (!abort_debug) {
  300.  
  301.         /* Run thread until an event occurs */
  302.         rc = udebug_go(app_phone, dt->hash, &ev_type, &val0, &val1);
  303.  
  304.         if (ev_type == UDEBUG_EVENT_FINISHED) {
  305.             cons_printf("thread %u debugging finished\n", dt->id);
  306.             break;
  307.         }
  308.         if (rc >= 0) debug_event(dt->hash, ev_type, val0);
  309.     }
  310.  
  311.     cons_printf("debug_loop(%d) exiting\n", dt->id);
  312. }
  313.  
  314. void thread_debug_start(unsigned thash)
  315. {
  316.     fid_t fid;
  317.     dthread_t *dt;
  318.  
  319.     dt = dthread_new(thash);
  320.  
  321.     fid = fibril_create(debug_loop, (void *)dt);
  322.     if (fid == 0) {
  323.         cons_printf("Warning: Failed creating fibril\n");
  324.     }
  325.     dt->fid = fid;
  326.  
  327.     fibril_add_ready(fid);
  328. }
  329.  
  330. void debug_active_task(void)
  331. {
  332.     int taskid;
  333.     int i;
  334.     int rc;
  335.     int c;
  336.  
  337.     thash_t *thash_buffer;
  338.     int n_threads;
  339.  
  340.     cons_printf("Breakpoint Debugger\n");
  341.     cons_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.         cons_printf("Failed to connect to task %d\n", taskid);
  349.         return;
  350.     }
  351.  
  352.     cons_printf("Connected to task %d\n", taskid);
  353.  
  354.     rc = get_thread_list(&thash_buffer, &n_threads);
  355.     if (rc < 0) {
  356.         cons_printf("Failed to get thread list\n", rc);
  357.         return;
  358.     }
  359.  
  360.     abort_debug = false;
  361.  
  362.     for (i = 0; i < n_threads; i++) {
  363.         thread_debug_start(thash_buffer[i]);
  364.     }
  365.  
  366.     while (!quit) {
  367.         cons_read_line(in_buf, IN_BUF_SIZE);
  368.         command_split(in_buf);
  369.         if (cmd_argc == 0) continue;
  370.  
  371.         command_run();
  372.     }
  373.  
  374.     cons_printf("terminate debugging session...\n");
  375.     abort_debug = true;
  376.     udebug_end(app_phone);
  377.     ipc_hangup(app_phone);
  378.  
  379.     cons_printf("done\n");
  380.     return;
  381. }
  382.  
  383. static void main_init(void)
  384. {
  385.     next_thread_id = 1;
  386.     paused = 0;
  387.    
  388.     list_initialize(&dthreads);
  389.     cwt = NULL;
  390.  
  391.     fcv_init(&go_cv);
  392. }
  393.  
  394. int main(void)
  395. {
  396.     main_init();
  397.  
  398.     while (1) {
  399.         debug_active_task();
  400.     }
  401. }
  402.  
  403. /** @}
  404.  */
  405.