Subversion Repositories HelenOS

Rev

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