Subversion Repositories HelenOS

Rev

Rev 3005 | Rev 3018 | 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 "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. static void command_split(char *cmd_str)
  71. {
  72.     char *p = cmd_str;
  73.  
  74.     if (*p == '\0') {
  75.         cmd_argc = 0;
  76.         return;
  77.     }
  78.  
  79.     cmd_argc = 1;
  80.     cmd_argv[0] = p;
  81.  
  82.     while (*p != '\0') {
  83.         if (*p == ' ') {
  84.             cmd_argv[cmd_argc++] = p + 1;
  85.             *p = '\0';
  86.         }
  87.         ++p;
  88.     }
  89. }
  90.  
  91. static void command_run(void)
  92. {
  93.     int i;
  94.     int cmp_len;
  95.     int len;
  96.  
  97.     int idx_found;
  98.     int num_found;
  99.  
  100.     len = strlen(cmd_argv[0]);
  101.     cmp_len = 1;
  102.  
  103.     /* Silence warnings */
  104.     num_found = 0;
  105.     idx_found = 0;
  106.  
  107.     while (cmp_len <= len + 1) {
  108.  
  109.         num_found = 0;
  110.         i = 0;
  111.         while (cmd_table[i].name != NULL) {
  112.             if (strncmp(cmd_table[i].name, cmd_argv[0], cmp_len) == 0) {
  113.                 idx_found = i;
  114.                 ++num_found;
  115.             }
  116.             ++i;
  117.         }
  118.  
  119.         if (num_found < 2) break;
  120.  
  121.         ++cmp_len;
  122.     }
  123.  
  124.     if (num_found == 0) {
  125.         cons_printf("Unknown command. Try one of:\n");
  126.         cmd_help(0, NULL);
  127.         return;
  128.     }
  129.  
  130.     if (cmd_argc - 1 != cmd_table[idx_found].argc) {
  131.         cons_printf("Command '%s' expects %d arguments\n",
  132.         cmd_table[idx_found].name, cmd_table[idx_found].argc);
  133.         return;
  134.     }
  135.  
  136.     (*cmd_table[idx_found].proc)(cmd_argc, cmd_argv);
  137. }
  138.  
  139. static void thread_stop(void)
  140. {
  141.     dthread_t *dt;
  142.     uintptr_t pc;
  143.  
  144.     dt = dthread_get();
  145.     pc = dthread_get_pc(dt);
  146.     cons_printf("[thread %d] stopped at 0x%lx\n", dt->id, pc);
  147.     dthread_stop_me();
  148.     cons_printf("[thread %d] go\n", dt->id);
  149. }
  150.  
  151. /*
  152.  * Called by a fibril (from arch code) when a breakpoint is hit.
  153.  */
  154. void breakpoint_hit(breakpoint_t *b)
  155. {
  156.     dthread_t *dt;
  157.  
  158.     dt = dthread_get();
  159.     cons_printf("Thread %d hit breakpoint %d at 0x%lx\n",
  160.         dt->id, b->id, b->addr);
  161.     thread_stop();
  162. }
  163.  
  164. /*
  165.  * Called by a fibril (from arch code) when a single instruction
  166.  * in singlestep is executed
  167.  */
  168. void singlestep_hit(void)
  169. {
  170.     cons_printf("singlestep hit\n");
  171.     thread_stop();
  172. }
  173.  
  174. static int task_connect(int taskid)
  175. {
  176.     int rc;
  177.     unsigned evmask;
  178.  
  179.     cons_printf("ipc_connect_kbox(%d)... ", taskid);
  180.     rc = ipc_connect_kbox(taskid);
  181.     cons_printf("-> %d\n", rc);
  182.     app_phone = rc;
  183.     if (rc < 0) return rc;
  184.  
  185.     cons_printf("udebug_begin()... ");
  186.     rc = udebug_begin(app_phone);
  187.     cons_printf("-> %d\n", rc);
  188.     if (rc < 0) return rc;
  189.  
  190.     evmask = UDEBUG_EM_ALL & ~(UDEBUG_EM_SYSCALL_B | UDEBUG_EM_SYSCALL_E);
  191.     cons_printf("udebug_set_evmask(0x%x)... ", evmask);
  192.     rc = udebug_set_evmask(app_phone, evmask);
  193.     cons_printf("-> %d\n", rc);
  194.     if (rc < 0) return rc;
  195.  
  196.     return 0;
  197. }
  198.  
  199. #define THASH_BUF_INIT_LENGTH 32
  200.  
  201. static int get_thread_list(thash_t **thash_buf_ptr, int *n)
  202. {
  203.     int rc;
  204.     size_t tb_copied;
  205.     size_t tb_needed;
  206.     int i;
  207.     size_t tb_size;
  208.     thash_t *thash_buf;
  209.  
  210.     tb_size = THASH_BUF_INIT_LENGTH * sizeof(thash_t);
  211.     thash_buf = malloc(tb_size);
  212.  
  213.     rc = udebug_thread_read(app_phone, thash_buf,
  214.         tb_size, &tb_copied, &tb_needed);
  215.     if (rc < 0) return rc;
  216.  
  217.     if (tb_needed > tb_size) {
  218.         /* Larger buffer needed  */
  219.  
  220.         free(thash_buf);
  221.  
  222.         tb_size = tb_needed;
  223.         thash_buf = malloc(tb_size);
  224.  
  225.         if (!thash_buf) {
  226.             printf("malloc failed\n");
  227.             exit(1);
  228.         }
  229.  
  230.         /* Try again */
  231.        
  232.         rc = udebug_thread_read(app_phone, thash_buf,
  233.             tb_size, &tb_copied, &tb_needed);
  234.  
  235.         if (rc < 0) return rc;
  236.     }
  237.  
  238.     *n = tb_copied / sizeof(thash_t);
  239.  
  240.     cons_printf("thread hashes:");
  241.  
  242.     for (i = 0; i < *n; ++i) {
  243.         cons_printf("0x%x\n", thash_buf[i]);
  244.     }
  245.  
  246.     cons_printf("Total of %u threads\n", *n);
  247.  
  248.     *thash_buf_ptr = thash_buf;
  249.  
  250.     return 0;
  251. }
  252.  
  253. static void event_thread_b(unsigned hash)
  254. {
  255.     async_serialize_start();
  256.     cons_printf("new thread, hash 0x%x\n", hash);
  257.     async_serialize_end();
  258.  
  259.     thread_debug_start(hash);
  260. }
  261.  
  262. static void debug_event(thash_t thash, udebug_event_t ev_type, sysarg_t val0)
  263. {
  264.     switch (ev_type) {
  265.     case UDEBUG_EVENT_STOP:
  266.         cons_printf("stop event\n");
  267.         thread_stop();
  268.         break;
  269.     case UDEBUG_EVENT_THREAD_B:
  270.         event_thread_b(val0);
  271.         break;
  272.     case UDEBUG_EVENT_THREAD_E:
  273.         cons_printf("thread 0x%x exited\n", val0);
  274.         abort_debug = true;
  275.         break;
  276.     case UDEBUG_EVENT_BREAKPOINT:
  277.         arch_event_breakpoint(thash);
  278.         break;
  279.     case UDEBUG_EVENT_TRAP:
  280.         arch_event_trap(dthread_get());
  281.         break;
  282.     default:
  283.         cons_printf("unknown event type %d\n", ev_type);
  284.         break;
  285.     }
  286. }
  287.  
  288. static int debug_loop(void *dt_arg)
  289. {
  290.     int rc;
  291.     udebug_event_t ev_type;
  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.     return 0;
  313. }
  314.  
  315. void thread_debug_start(unsigned thash)
  316. {
  317.     fid_t fid;
  318.     dthread_t *dt;
  319.  
  320.     dt = dthread_new(thash);
  321.  
  322.     fid = fibril_create(debug_loop, (void *)dt);
  323.     if (fid == 0) {
  324.         cons_printf("Warning: Failed creating fibril\n");
  325.     }
  326.     dt->fid = fid;
  327.  
  328.     fibril_add_ready(fid);
  329. }
  330.  
  331. static void debug_active_task(void)
  332. {
  333.     int taskid;
  334.     int i;
  335.     int rc;
  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.     breakpoint_init();
  390.     cwt = NULL;
  391. }
  392.  
  393. int main(void)
  394. {
  395.     main_init();
  396.  
  397.     while (1) {
  398.         debug_active_task();
  399.     }
  400. }
  401.  
  402. /** @}
  403.  */
  404.