cmd_bedbug.c 12 KB

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