Subversion Repositories HelenOS

Rev

Rev 2911 | 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.  
  165.     printf("ipc_connect_kbox(%d)... ", taskid);
  166.     rc = ipc_connect_kbox(taskid);
  167.     printf("-> %d\n", rc);
  168.     app_phone = rc;
  169.     if (rc < 0) return rc;
  170.  
  171.     printf("udebug_begin()... ");
  172.     rc = udebug_begin(app_phone);
  173.     printf("-> %d\n", rc);
  174.     if (rc < 0) return rc;
  175.  
  176.     printf("udebug_set_evmask(0x%x)... ", UDEBUG_EM_ALL);
  177.     rc = udebug_set_evmask(app_phone, UDEBUG_EM_ALL);
  178.     printf("-> %d\n", rc);
  179.     if (rc < 0) return rc;
  180.  
  181.     return 0;
  182. }
  183.  
  184. int get_thread_list(void)
  185. {
  186.     int rc;
  187.     int tb_copied;
  188.     int tb_needed;
  189.     int i;
  190.  
  191.     printf("send IPC_M_DEBUG_THREAD_READ message\n");
  192.     rc = udebug_thread_read(app_phone, (unsigned)thread_hash_buf,
  193.         THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed);
  194.     printf("-> %d\n", rc);
  195.     if (rc < 0) return rc;
  196.  
  197.     n_threads = tb_copied / sizeof(unsigned);
  198.  
  199.     printf("thread IDs:");
  200.     for (i=0; i<n_threads; i++) {
  201.         printf(" %u", thread_hash_buf[i]);
  202.     }
  203.     printf("\ntotal of %u threads\n", tb_needed/sizeof(unsigned));
  204.  
  205.     return 0;
  206. }
  207.  
  208. void event_thread_b(unsigned hash)
  209. {
  210.     async_serialize_start();
  211.     printf("new thread, hash 0x%x\n", hash);
  212.     async_serialize_end();
  213.  
  214.     thread_debug_start(hash);
  215. }
  216.  
  217. void debug_loop(void *thread_hash_arg)
  218. {
  219.     int rc;
  220.     unsigned ev_type;
  221.     unsigned thread_hash;
  222.     unsigned thread_id;
  223.     unsigned val0, val1;
  224.  
  225.     thread_hash = (unsigned)thread_hash_arg;
  226.     thread_id = next_thread_id++;
  227.  
  228.     printf("debug_loop(%d)\n", thread_id); 
  229.  
  230.     while (!abort_debug) {
  231.  
  232.         /* Run thread until an event occurs */
  233.         rc = udebug_go(app_phone, thread_hash,
  234.             &ev_type, &val0, &val1);
  235.  
  236. //      printf("rc = %d, ev_type=%d\n", rc, ev_type);
  237.         if (ev_type == UDEBUG_EVENT_FINISHED) {
  238.             printf("thread %u debugging finished\n", thread_id);
  239.             break;
  240.         }
  241.  
  242.         if (rc >= 0) {
  243.             switch (ev_type) {
  244.             case UDEBUG_EVENT_STOP:
  245.                 printf("stop event\n");
  246.                 printf("waiting for resume\n");
  247.                 while (paused) {
  248.                     usleep(1000000);
  249.                     fibril_yield();
  250.                     printf(".");
  251.                 }
  252.                 printf("resumed\n");
  253.                 break;
  254.             case UDEBUG_EVENT_THREAD_B:
  255.                 event_thread_b(val0);
  256.                 break;
  257.             case UDEBUG_EVENT_THREAD_E:
  258.                 printf("thread 0x%x exited\n", val0);
  259.                 abort_debug = true;
  260.                 break;
  261.             default:
  262.                 printf("unknown event type %d\n", ev_type);
  263.                 break;
  264.             }
  265.         }
  266.  
  267.     }
  268.  
  269.     printf("debug_loop(%d) exiting\n", thread_id);
  270. }
  271.  
  272. void thread_debug_start(unsigned thread_hash)
  273. {
  274.     fid_t fid;
  275.  
  276.     thash = thread_hash;
  277.  
  278.     fid = fibril_create(debug_loop, (void *)thread_hash);
  279.     if (fid == 0) {
  280.         printf("Warning: Failed creating fibril\n");
  281.     }
  282.     fibril_add_ready(fid);
  283. }
  284.  
  285. void debug_active_task(void)
  286. {
  287.     int taskid;
  288.     int i;
  289.     int rc;
  290.     int c;
  291.  
  292.     printf("Breakpoint Debugger\n");
  293.     printf("Press 'c' to connect\n");
  294.     while ((i = getchar()) != 'c')
  295.         putchar(i);
  296.  
  297.     taskid = 14;
  298.     rc = task_connect(taskid);
  299.     if (rc < 0) {
  300.         printf("Failed to connect to task %d\n", taskid);
  301.         return;
  302.     }
  303.  
  304.     printf("Connected to task %d\n", taskid);
  305.  
  306.     rc = get_thread_list();
  307.     if (rc < 0) {
  308.         printf("Failed to get thread list (error %d)\n", rc);
  309.         return;
  310.     }
  311.  
  312.     abort_debug = false;
  313.  
  314.     for (i = 0; i < n_threads; i++) {
  315.         thread_debug_start(thread_hash_buf[i]);
  316.     }
  317.  
  318.     while (!quit) {
  319.         printf("> ");
  320.         read_line(in_buf, INBUF_SIZE);
  321.         command_split(in_buf);
  322.         if (cmd_argc == 0) continue;
  323.  
  324.         command_run();
  325.     }
  326.  
  327.     printf("terminate debugging session...\n");
  328.     abort_debug = true;
  329.     udebug_end(app_phone);
  330.     ipc_hangup(app_phone);
  331.  
  332.     printf("done\n");
  333.     return;
  334. }
  335.  
  336. static void main_init(void)
  337. {
  338.     next_thread_id = 1;
  339.     paused = 0;
  340. }
  341.  
  342. int main(void)
  343. {
  344.     main_init();
  345.  
  346.     while (1) {
  347.         debug_active_task();
  348.     }
  349. }
  350.  
  351. /** @}
  352.  */
  353.