Subversion Repositories HelenOS

Rev

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