Subversion Repositories HelenOS

Rev

Rev 3005 | 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 <string.h>
  38. #include <bool.h>
  39. #include <udebug.h>
  40. #include <sys/types.h>
  41.  
  42. #include "main.h"
  43. #include "cons.h"
  44. #include "dthread.h"
  45. #include "breakpoint.h"
  46. #include "include/arch.h"
  47. #include "cmd.h"
  48.  
  49. static void cmd_break(int argc, char *argv[]);
  50. static void cmd_ct(int argc, char *argv[]);
  51. static void cmd_dbreak(int argc, char *argv[]);
  52. static void cmd_go(int argc, char *argv[]);
  53. static void cmd_istep(int argc, char *argv[]);
  54. static void cmd_lbrk(int argc, char *argv[]);
  55. void        cmd_help(int argc, char *argv[]);
  56. static void cmd_memr(int argc, char *argv[]);
  57. static void cmd_pwt(int argc, char *argv[]);
  58. static void cmd_regs(int argc, char *argv[]);
  59. static void cmd_stop(int argc, char *argv[]);
  60. static void cmd_threads(int argc, char *argv[]);
  61. static void cmd_quit(int argc, char *argv[]);
  62.  
  63. volatile bool quit = false;
  64.  
  65. cmd_desc_t cmd_table[] = {
  66.     { 1,    "break",    cmd_break },
  67.     { 1,    "ct",       cmd_ct },
  68.     { 1,    "dbreak",   cmd_dbreak },
  69.     { 0,    "go",       cmd_go },
  70.     { 0,    "help",     cmd_help },
  71.     { 2,    "memr",     cmd_memr },
  72.     { 0,    "pwt",      cmd_pwt },
  73.     { 0,    "regs",     cmd_regs },
  74.     { 0 ,   "stop",     cmd_stop },
  75.     { 0,    "istep",    cmd_istep },
  76.     { 0,    "lbrk",     cmd_lbrk },
  77.     { 0,    "threads",  cmd_threads },
  78.     { 0,    "quit",     cmd_quit },
  79.     { -1,   NULL,       NULL }
  80. };
  81.  
  82. static void cmd_break(int argc, char *argv[])
  83. {
  84.     uintptr_t addr;
  85.  
  86.     (void)argc;
  87.     addr = strtoul(argv[1], NULL, 0);
  88.  
  89.     cons_printf("You requested a breakpoint at 0x%x\n", addr);
  90. //  arch_breakpoint_add(addr);
  91.     breakpoint_add(addr);
  92. }
  93.  
  94. static void cmd_ct(int argc, char *argv[])
  95. {
  96.     int tid;
  97.  
  98.     link_t *cur;
  99.     dthread_t *dt;
  100.  
  101.     (void)argc;
  102.     tid = strtoul(argv[1], NULL, 0);
  103.  
  104.     dt = NULL;
  105.     for (cur = dthreads.next; cur != &dthreads; cur = cur->next) {
  106.         dt = list_get_instance(cur, dthread_t, link);
  107.         if (dt->id == tid) break;
  108.     }
  109.    
  110.     if (dt->id == tid) {
  111.         cwt = dt;
  112.         cons_printf("changed working thread to: %d [hash 0x%x]\n",
  113.             cwt->id, cwt->hash);
  114.     } else {
  115.         cons_printf("no such thread\n");
  116.     }
  117. }
  118.  
  119. static void cmd_dbreak(int argc, char *argv[])
  120. {
  121.     int bid;
  122.  
  123.     (void)argc;
  124.     bid = strtoul(argv[1], NULL, 0);
  125.  
  126.     printf("remove breakpoint %d\n", bid);
  127.  
  128. //  arch_breakpoint_remove(bid);
  129.     breakpoint_remove(bid);
  130. }
  131.  
  132.  
  133. void cmd_go(int argc, char *argv[])
  134. {
  135.     link_t *cur;
  136.     dthread_t *dt;
  137.  
  138.     (void)argc; (void)argv;
  139.  
  140.     dt = NULL;
  141.     for (cur = dthreads.next; cur != &dthreads; cur = cur->next) {
  142.         dt = list_get_instance(cur, dthread_t, link);
  143.         dthread_resume(dt);
  144.     }  
  145. }
  146.  
  147. void cmd_help(int argc, char *argv[])
  148. {
  149.     int i;
  150.  
  151.     (void)argc; (void)argv;
  152.     i = 0;
  153.     while (cmd_table[i].name != NULL) {
  154.         cons_printf("%s\n", cmd_table[i].name);
  155.         ++i;
  156.     }
  157. }
  158.  
  159. void cmd_istep(int argc, char *argv[])
  160. {
  161.     (void)argc; (void)argv;
  162.  
  163.     arch_singlestep(cwt);
  164. }
  165.  
  166. static void cmd_lbrk(int argc, char *argv[])
  167. {
  168.     (void)argc; (void)argv;
  169.  
  170.     //arch_breakpoint_list();
  171.     breakpoint_list();
  172. }
  173.  
  174.  
  175. #define BYTES_PER_LINE 16
  176.  
  177. static void cmd_memr(int argc, char *argv[])
  178. {
  179.     uintptr_t addr;
  180.     size_t length;
  181.     uint8_t buf[BYTES_PER_LINE];
  182.     int to_read, i;
  183.     int rc;
  184.  
  185.     (void)argc;
  186.     addr = strtoul(argv[1], NULL, 0);
  187.     length = strtoul(argv[2], NULL, 0);
  188.  
  189.     while (length > 0) {
  190.         to_read = length < BYTES_PER_LINE ? length : BYTES_PER_LINE;
  191.  
  192.         rc = udebug_mem_read(app_phone, buf, addr, to_read);
  193.         if (rc < 0) {
  194.             cons_printf("error %d\n", rc);
  195.             return;
  196.         }
  197.  
  198.         cons_printf("0x%x:", addr);
  199.         for (i = 0; i < to_read; ++i) {
  200.             cons_printf(" %02x", buf[i]);
  201.         }
  202.         for (i = to_read; i < BYTES_PER_LINE; ++i) {
  203.             cons_printf("   ");
  204.         }
  205.  
  206.         putchar ('\t');
  207.  
  208.         for (i = 0; i < to_read; ++i) {
  209.             if (buf[i] >= 32 && buf[i] < 127)
  210.                 putchar(buf[i]);
  211.             else
  212.                 putchar('.');
  213.         }
  214.         cons_printf("\n");
  215.  
  216.         addr += to_read;
  217.         length -= to_read;
  218.     }
  219. }
  220.  
  221. void cmd_pwt(int argc, char *argv[])
  222. {
  223.     (void)argc; (void)argv;
  224.  
  225.     cons_printf("working thread: %d [hash 0x%x]\n", cwt->id, cwt->hash);
  226. }
  227.  
  228. void cmd_regs(int argc, char *argv[])
  229. {
  230.     (void)argc; (void)argv;
  231.  
  232.     if (cwt) arch_dump_regs(cwt->hash);
  233. }
  234.  
  235. void cmd_stop(int argc, char *argv[])
  236. {
  237.     link_t *cur;
  238.     dthread_t *dt;
  239.     int rc;
  240.  
  241.     (void)argc; (void)argv;
  242.  
  243.     dt = NULL;
  244.     for (cur = dthreads.next; cur != &dthreads; cur = cur->next) {
  245.         dt = list_get_instance(cur, dthread_t, link);
  246.         if (!dt->stopped) {
  247.             rc = udebug_stop(app_phone, dt->hash);
  248.             if (rc < 0) {
  249.                 printf("failed halting thread %d\n", dt->id);
  250.             }
  251.         }
  252.     }  
  253. }
  254.  
  255.  
  256. void cmd_threads(int argc, char *argv[])
  257. {
  258.     link_t *cur;
  259.     dthread_t *dt;
  260.  
  261.     (void)argc;
  262.  
  263.     dt = NULL;
  264.     for (cur = dthreads.next; cur != &dthreads; cur = cur->next) {
  265.         dt = list_get_instance(cur, dthread_t, link);
  266.         cons_printf("%d [hash 0x%x]\n", dt->id, dt->hash);
  267.     }  
  268. }
  269.  
  270.  
  271. static void cmd_quit(int argc, char *argv[])
  272. {
  273.     (void)argc; (void)argv;
  274.     quit = true;
  275. }
  276.  
  277. /** @}
  278.  */
  279.