cmd_bedbug.c 12 KB

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