Subversion Repositories HelenOS

Rev

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