cmd_bedbug.c 12 KB

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