bedbug.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * BedBug Functions
  3. */
  4. #include <common.h>
  5. #include <cli.h>
  6. #include <command.h>
  7. #include <console.h>
  8. #include <linux/ctype.h>
  9. #include <net.h>
  10. #include <bedbug/type.h>
  11. #include <bedbug/bedbug.h>
  12. #include <bedbug/regs.h>
  13. #include <bedbug/ppc.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. extern void show_regs __P ((struct pt_regs *));
  16. extern int run_command __P ((const char *, int));
  17. ulong dis_last_addr = 0; /* Last address disassembled */
  18. ulong dis_last_len = 20; /* Default disassembler length */
  19. CPU_DEBUG_CTX bug_ctx; /* Bedbug context structure */
  20. /* ======================================================================
  21. * U-Boot's puts function does not append a newline, so the bedbug stuff
  22. * will use this for the output of the dis/assembler.
  23. * ====================================================================== */
  24. int bedbug_puts (const char *str)
  25. {
  26. /* -------------------------------------------------- */
  27. printf ("%s\r\n", str);
  28. return 0;
  29. } /* bedbug_puts */
  30. /* ======================================================================
  31. * Initialize the bug_ctx structure used by the bedbug debugger. This is
  32. * specific to the CPU since each has different debug registers and
  33. * settings.
  34. * ====================================================================== */
  35. void bedbug_init (void)
  36. {
  37. /* -------------------------------------------------- */
  38. return;
  39. } /* bedbug_init */
  40. /* ======================================================================
  41. * Entry point from the interpreter to the disassembler. Repeated calls
  42. * will resume from the last disassembled address.
  43. * ====================================================================== */
  44. int do_bedbug_dis (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  45. {
  46. ulong addr; /* Address to start disassembly from */
  47. ulong len; /* # of instructions to disassemble */
  48. /* -------------------------------------------------- */
  49. /* Setup to go from the last address if none is given */
  50. addr = dis_last_addr;
  51. len = dis_last_len;
  52. if (argc < 2)
  53. return CMD_RET_USAGE;
  54. if ((flag & CMD_FLAG_REPEAT) == 0) {
  55. /* New command */
  56. addr = simple_strtoul (argv[1], NULL, 16);
  57. /* If an extra param is given then it is the length */
  58. if (argc > 2)
  59. len = simple_strtoul (argv[2], NULL, 16);
  60. }
  61. /* Run the disassembler */
  62. disppc ((unsigned char *) addr, 0, len, bedbug_puts, F_RADHEX);
  63. dis_last_addr = addr + (len * 4);
  64. dis_last_len = len;
  65. return 0;
  66. } /* do_bedbug_dis */
  67. U_BOOT_CMD (ds, 3, 1, do_bedbug_dis,
  68. "disassemble memory",
  69. "ds <address> [# instructions]");
  70. /* ======================================================================
  71. * Entry point from the interpreter to the assembler. Assembles
  72. * instructions in consecutive memory locations until a '.' (period) is
  73. * entered on a line by itself.
  74. * ====================================================================== */
  75. int do_bedbug_asm (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  76. {
  77. long mem_addr; /* Address to assemble into */
  78. unsigned long instr; /* Machine code for text */
  79. char prompt[15]; /* Prompt string for user input */
  80. int asm_err; /* Error code from the assembler */
  81. /* -------------------------------------------------- */
  82. int rcode = 0;
  83. if (argc < 2)
  84. return CMD_RET_USAGE;
  85. printf ("\nEnter '.' when done\n");
  86. mem_addr = simple_strtoul (argv[1], NULL, 16);
  87. while (1) {
  88. putc ('\n');
  89. disppc ((unsigned char *) mem_addr, 0, 1, bedbug_puts,
  90. F_RADHEX);
  91. sprintf (prompt, "%08lx: ", mem_addr);
  92. cli_readline(prompt);
  93. if (console_buffer[0] && strcmp (console_buffer, ".")) {
  94. if ((instr =
  95. asmppc (mem_addr, console_buffer,
  96. &asm_err)) != 0) {
  97. *(unsigned long *) mem_addr = instr;
  98. mem_addr += 4;
  99. } else {
  100. printf ("*** Error: %s ***\n",
  101. asm_error_str (asm_err));
  102. rcode = 1;
  103. }
  104. } else {
  105. break;
  106. }
  107. }
  108. return rcode;
  109. } /* do_bedbug_asm */
  110. U_BOOT_CMD (as, 2, 0, do_bedbug_asm,
  111. "assemble memory", "as <address>");
  112. /* ======================================================================
  113. * Used to set a break point from the interpreter. Simply calls into the
  114. * CPU-specific break point set routine.
  115. * ====================================================================== */
  116. int do_bedbug_break (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  117. {
  118. /* -------------------------------------------------- */
  119. if (bug_ctx.do_break)
  120. (*bug_ctx.do_break) (cmdtp, flag, argc, argv);
  121. return 0;
  122. } /* do_bedbug_break */
  123. U_BOOT_CMD (break, 3, 0, do_bedbug_break,
  124. "set or clear a breakpoint",
  125. " - Set or clear a breakpoint\n"
  126. "break <address> - Break at an address\n"
  127. "break off <bp#> - Disable breakpoint.\n"
  128. "break show - List breakpoints.");
  129. /* ======================================================================
  130. * Called from the debug interrupt routine. Simply calls the CPU-specific
  131. * breakpoint handling routine.
  132. * ====================================================================== */
  133. void do_bedbug_breakpoint (struct pt_regs *regs)
  134. {
  135. /* -------------------------------------------------- */
  136. if (bug_ctx.break_isr)
  137. (*bug_ctx.break_isr) (regs);
  138. return;
  139. } /* do_bedbug_breakpoint */
  140. /* ======================================================================
  141. * Called from the CPU-specific breakpoint handling routine. Enter a
  142. * mini main loop until the stopped flag is cleared from the breakpoint
  143. * context.
  144. *
  145. * This handles the parts of the debugger that are common to all CPU's.
  146. * ====================================================================== */
  147. void bedbug_main_loop (unsigned long addr, struct pt_regs *regs)
  148. {
  149. int len; /* Length of command line */
  150. int flag; /* Command flags */
  151. int rc = 0; /* Result from run_command */
  152. char prompt_str[20]; /* Prompt string */
  153. static char lastcommand[CONFIG_SYS_CBSIZE] = { 0 }; /* previous command */
  154. /* -------------------------------------------------- */
  155. if (bug_ctx.clear)
  156. (*bug_ctx.clear) (bug_ctx.current_bp);
  157. printf ("Breakpoint %d: ", bug_ctx.current_bp);
  158. disppc ((unsigned char *) addr, 0, 1, bedbug_puts, F_RADHEX);
  159. bug_ctx.stopped = 1;
  160. bug_ctx.regs = regs;
  161. sprintf (prompt_str, "BEDBUG.%d =>", bug_ctx.current_bp);
  162. /* A miniature main loop */
  163. while (bug_ctx.stopped) {
  164. len = cli_readline(prompt_str);
  165. flag = 0; /* assume no special flags for now */
  166. if (len > 0)
  167. strcpy (lastcommand, console_buffer);
  168. else if (len == 0)
  169. flag |= CMD_FLAG_REPEAT;
  170. if (len == -1)
  171. printf ("<INTERRUPT>\n");
  172. else
  173. rc = run_command_repeatable(lastcommand, flag);
  174. if (rc <= 0) {
  175. /* invalid command or not repeatable, forget it */
  176. lastcommand[0] = 0;
  177. }
  178. }
  179. bug_ctx.regs = NULL;
  180. bug_ctx.current_bp = 0;
  181. return;
  182. } /* bedbug_main_loop */
  183. /* ======================================================================
  184. * Interpreter command to continue from a breakpoint. Just clears the
  185. * stopped flag in the context so that the breakpoint routine will
  186. * return.
  187. * ====================================================================== */
  188. int do_bedbug_continue (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  189. {
  190. /* -------------------------------------------------- */
  191. if (!bug_ctx.stopped) {
  192. printf ("Not at a breakpoint\n");
  193. return 1;
  194. }
  195. bug_ctx.stopped = 0;
  196. return 0;
  197. } /* do_bedbug_continue */
  198. U_BOOT_CMD (continue, 1, 0, do_bedbug_continue,
  199. "continue from a breakpoint",
  200. "");
  201. /* ======================================================================
  202. * Interpreter command to continue to the next instruction, stepping into
  203. * subroutines. Works by calling the find_next_addr() routine to compute
  204. * the address passes control to the CPU-specific set breakpoint routine
  205. * for the current breakpoint number.
  206. * ====================================================================== */
  207. int do_bedbug_step (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  208. {
  209. unsigned long addr; /* Address to stop at */
  210. /* -------------------------------------------------- */
  211. if (!bug_ctx.stopped) {
  212. printf ("Not at a breakpoint\n");
  213. return 1;
  214. }
  215. if (!find_next_address((unsigned char *) &addr, false, bug_ctx.regs))
  216. return 1;
  217. if (bug_ctx.set)
  218. (*bug_ctx.set) (bug_ctx.current_bp, addr);
  219. bug_ctx.stopped = 0;
  220. return 0;
  221. } /* do_bedbug_step */
  222. U_BOOT_CMD (step, 1, 1, do_bedbug_step,
  223. "single step execution.",
  224. "");
  225. /* ======================================================================
  226. * Interpreter command to continue to the next instruction, stepping over
  227. * subroutines. Works by calling the find_next_addr() routine to compute
  228. * the address passes control to the CPU-specific set breakpoint routine
  229. * for the current breakpoint number.
  230. * ====================================================================== */
  231. int do_bedbug_next (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  232. {
  233. unsigned long addr; /* Address to stop at */
  234. /* -------------------------------------------------- */
  235. if (!bug_ctx.stopped) {
  236. printf ("Not at a breakpoint\n");
  237. return 1;
  238. }
  239. if (!find_next_address((unsigned char *) &addr, true, bug_ctx.regs))
  240. return 1;
  241. if (bug_ctx.set)
  242. (*bug_ctx.set) (bug_ctx.current_bp, addr);
  243. bug_ctx.stopped = 0;
  244. return 0;
  245. } /* do_bedbug_next */
  246. U_BOOT_CMD (next, 1, 1, do_bedbug_next,
  247. "single step execution, stepping over subroutines.",
  248. "");
  249. /* ======================================================================
  250. * Interpreter command to print the current stack. This assumes an EABI
  251. * architecture, so it starts with GPR R1 and works back up the stack.
  252. * ====================================================================== */
  253. int do_bedbug_stack (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  254. {
  255. unsigned long sp; /* Stack pointer */
  256. unsigned long func; /* LR from stack */
  257. int depth; /* Stack iteration level */
  258. int skip = 1; /* Flag to skip the first entry */
  259. unsigned long top; /* Top of memory address */
  260. /* -------------------------------------------------- */
  261. if (!bug_ctx.stopped) {
  262. printf ("Not at a breakpoint\n");
  263. return 1;
  264. }
  265. top = gd->bd->bi_memstart + gd->bd->bi_memsize;
  266. depth = 0;
  267. printf ("Depth PC\n");
  268. printf ("----- --------\n");
  269. printf ("%5d %08lx\n", depth++, bug_ctx.regs->nip);
  270. sp = bug_ctx.regs->gpr[1];
  271. func = *(unsigned long *) (sp + 4);
  272. while ((func < top) && (sp < top)) {
  273. if (!skip)
  274. printf ("%5d %08lx\n", depth++, func);
  275. else
  276. --skip;
  277. sp = *(unsigned long *) sp;
  278. func = *(unsigned long *) (sp + 4);
  279. }
  280. return 0;
  281. } /* do_bedbug_stack */
  282. U_BOOT_CMD (where, 1, 1, do_bedbug_stack,
  283. "Print the running stack.",
  284. "");
  285. /* ======================================================================
  286. * Interpreter command to dump the registers. Calls the CPU-specific
  287. * show registers routine.
  288. * ====================================================================== */
  289. int do_bedbug_rdump (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  290. {
  291. /* -------------------------------------------------- */
  292. if (!bug_ctx.stopped) {
  293. printf ("Not at a breakpoint\n");
  294. return 1;
  295. }
  296. show_regs (bug_ctx.regs);
  297. return 0;
  298. } /* do_bedbug_rdump */
  299. U_BOOT_CMD (rdump, 1, 1, do_bedbug_rdump,
  300. "Show registers.", "");
  301. /* ====================================================================== */
  302. /*
  303. * Copyright (c) 2001 William L. Pitts
  304. * All rights reserved.
  305. *
  306. * Redistribution and use in source and binary forms are freely
  307. * permitted provided that the above copyright notice and this
  308. * paragraph and the following disclaimer are duplicated in all
  309. * such forms.
  310. *
  311. * This software is provided "AS IS" and without any express or
  312. * implied warranties, including, without limitation, the implied
  313. * warranties of merchantability and fitness for a particular
  314. * purpose.
  315. */