Subversion Repositories HelenOS

Rev

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