Subversion Repositories HelenOS

Rev

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