bedbug.c 12 KB

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