bedbug.c 12 KB

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