bedbug.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * BedBug Functions
  3. */
  4. #include <common.h>
  5. #include <cli.h>
  6. #include <command.h>
  7. #include <console.h>
  8. #include <asm/global_data.h>
  9. #include <asm/ptrace.h>
  10. #include <linux/ctype.h>
  11. #include <net.h>
  12. #include <bedbug/type.h>
  13. #include <bedbug/bedbug.h>
  14. #include <bedbug/regs.h>
  15. #include <bedbug/ppc.h>
  16. DECLARE_GLOBAL_DATA_PTR;
  17. extern void show_regs __P ((struct pt_regs *));
  18. extern int run_command __P ((const char *, int));
  19. ulong dis_last_addr = 0; /* Last address disassembled */
  20. ulong dis_last_len = 20; /* Default disassembler length */
  21. CPU_DEBUG_CTX bug_ctx; /* Bedbug context structure */
  22. /* ======================================================================
  23. * U-Boot's puts function does not append a newline, so the bedbug stuff
  24. * will use this for the output of the dis/assembler.
  25. * ====================================================================== */
  26. int bedbug_puts (const char *str)
  27. {
  28. /* -------------------------------------------------- */
  29. printf ("%s\r\n", str);
  30. return 0;
  31. } /* bedbug_puts */
  32. /* ======================================================================
  33. * Initialize the bug_ctx structure used by the bedbug debugger. This is
  34. * specific to the CPU since each has different debug registers and
  35. * settings.
  36. * ====================================================================== */
  37. int bedbug_init(void)
  38. {
  39. /* -------------------------------------------------- */
  40. return 0;
  41. } /* bedbug_init */
  42. /* ======================================================================
  43. * Entry point from the interpreter to the disassembler. Repeated calls
  44. * will resume from the last disassembled address.
  45. * ====================================================================== */
  46. int do_bedbug_dis(struct cmd_tbl *cmdtp, int flag, int argc,
  47. char *const argv[])
  48. {
  49. ulong addr; /* Address to start disassembly from */
  50. ulong len; /* # of instructions to disassemble */
  51. /* -------------------------------------------------- */
  52. /* Setup to go from the last address if none is given */
  53. addr = dis_last_addr;
  54. len = dis_last_len;
  55. if (argc < 2)
  56. return CMD_RET_USAGE;
  57. if ((flag & CMD_FLAG_REPEAT) == 0) {
  58. /* New command */
  59. addr = hextoul(argv[1], NULL);
  60. /* If an extra param is given then it is the length */
  61. if (argc > 2)
  62. len = hextoul(argv[2], NULL);
  63. }
  64. /* Run the disassembler */
  65. disppc ((unsigned char *) addr, 0, len, bedbug_puts, F_RADHEX);
  66. dis_last_addr = addr + (len * 4);
  67. dis_last_len = len;
  68. return 0;
  69. } /* do_bedbug_dis */
  70. U_BOOT_CMD (ds, 3, 1, do_bedbug_dis,
  71. "disassemble memory",
  72. "ds <address> [# instructions]");
  73. /* ======================================================================
  74. * Entry point from the interpreter to the assembler. Assembles
  75. * instructions in consecutive memory locations until a '.' (period) is
  76. * entered on a line by itself.
  77. * ====================================================================== */
  78. int do_bedbug_asm(struct cmd_tbl *cmdtp, int flag, int argc,
  79. char *const argv[])
  80. {
  81. long mem_addr; /* Address to assemble into */
  82. unsigned long instr; /* Machine code for text */
  83. char prompt[15]; /* Prompt string for user input */
  84. int asm_err; /* Error code from the assembler */
  85. /* -------------------------------------------------- */
  86. int rcode = 0;
  87. if (argc < 2)
  88. return CMD_RET_USAGE;
  89. printf ("\nEnter '.' when done\n");
  90. mem_addr = hextoul(argv[1], NULL);
  91. while (1) {
  92. putc ('\n');
  93. disppc ((unsigned char *) mem_addr, 0, 1, bedbug_puts,
  94. F_RADHEX);
  95. sprintf (prompt, "%08lx: ", mem_addr);
  96. cli_readline(prompt);
  97. if (console_buffer[0] && strcmp (console_buffer, ".")) {
  98. if ((instr =
  99. asmppc (mem_addr, console_buffer,
  100. &asm_err)) != 0) {
  101. *(unsigned long *) mem_addr = instr;
  102. mem_addr += 4;
  103. } else {
  104. printf ("*** Error: %s ***\n",
  105. asm_error_str (asm_err));
  106. rcode = 1;
  107. }
  108. } else {
  109. break;
  110. }
  111. }
  112. return rcode;
  113. } /* do_bedbug_asm */
  114. U_BOOT_CMD (as, 2, 0, do_bedbug_asm,
  115. "assemble memory", "as <address>");
  116. /* ======================================================================
  117. * Used to set a break point from the interpreter. Simply calls into the
  118. * CPU-specific break point set routine.
  119. * ====================================================================== */
  120. int do_bedbug_break(struct cmd_tbl *cmdtp, int flag, int argc,
  121. char *const argv[])
  122. {
  123. /* -------------------------------------------------- */
  124. if (bug_ctx.do_break)
  125. (*bug_ctx.do_break) (cmdtp, flag, argc, argv);
  126. return 0;
  127. } /* do_bedbug_break */
  128. U_BOOT_CMD (break, 3, 0, do_bedbug_break,
  129. "set or clear a breakpoint",
  130. " - Set or clear a breakpoint\n"
  131. "break <address> - Break at an address\n"
  132. "break off <bp#> - Disable breakpoint.\n"
  133. "break show - List breakpoints.");
  134. /* ======================================================================
  135. * Called from the debug interrupt routine. Simply calls the CPU-specific
  136. * breakpoint handling routine.
  137. * ====================================================================== */
  138. void do_bedbug_breakpoint (struct pt_regs *regs)
  139. {
  140. /* -------------------------------------------------- */
  141. if (bug_ctx.break_isr)
  142. (*bug_ctx.break_isr) (regs);
  143. return;
  144. } /* do_bedbug_breakpoint */
  145. /* ======================================================================
  146. * Called from the CPU-specific breakpoint handling routine. Enter a
  147. * mini main loop until the stopped flag is cleared from the breakpoint
  148. * context.
  149. *
  150. * This handles the parts of the debugger that are common to all CPU's.
  151. * ====================================================================== */
  152. void bedbug_main_loop (unsigned long addr, struct pt_regs *regs)
  153. {
  154. int len; /* Length of command line */
  155. int flag; /* Command flags */
  156. int rc = 0; /* Result from run_command */
  157. char prompt_str[20]; /* Prompt string */
  158. static char lastcommand[CONFIG_SYS_CBSIZE] = { 0 }; /* previous command */
  159. /* -------------------------------------------------- */
  160. if (bug_ctx.clear)
  161. (*bug_ctx.clear) (bug_ctx.current_bp);
  162. printf ("Breakpoint %d: ", bug_ctx.current_bp);
  163. disppc ((unsigned char *) addr, 0, 1, bedbug_puts, F_RADHEX);
  164. bug_ctx.stopped = 1;
  165. bug_ctx.regs = regs;
  166. sprintf (prompt_str, "BEDBUG.%d =>", bug_ctx.current_bp);
  167. /* A miniature main loop */
  168. while (bug_ctx.stopped) {
  169. len = cli_readline(prompt_str);
  170. flag = 0; /* assume no special flags for now */
  171. if (len > 0)
  172. strcpy (lastcommand, console_buffer);
  173. else if (len == 0)
  174. flag |= CMD_FLAG_REPEAT;
  175. if (len == -1)
  176. printf ("<INTERRUPT>\n");
  177. else
  178. rc = run_command_repeatable(lastcommand, flag);
  179. if (rc <= 0) {
  180. /* invalid command or not repeatable, forget it */
  181. lastcommand[0] = 0;
  182. }
  183. }
  184. bug_ctx.regs = NULL;
  185. bug_ctx.current_bp = 0;
  186. return;
  187. } /* bedbug_main_loop */
  188. /* ======================================================================
  189. * Interpreter command to continue from a breakpoint. Just clears the
  190. * stopped flag in the context so that the breakpoint routine will
  191. * return.
  192. * ====================================================================== */
  193. int do_bedbug_continue(struct cmd_tbl *cmdtp, int flag, int argc,
  194. char *const argv[])
  195. {
  196. /* -------------------------------------------------- */
  197. if (!bug_ctx.stopped) {
  198. printf ("Not at a breakpoint\n");
  199. return 1;
  200. }
  201. bug_ctx.stopped = 0;
  202. return 0;
  203. } /* do_bedbug_continue */
  204. U_BOOT_CMD (continue, 1, 0, do_bedbug_continue,
  205. "continue from a breakpoint",
  206. "");
  207. /* ======================================================================
  208. * Interpreter command to continue to the next instruction, stepping into
  209. * subroutines. Works by calling the find_next_addr() routine to compute
  210. * the address passes control to the CPU-specific set breakpoint routine
  211. * for the current breakpoint number.
  212. * ====================================================================== */
  213. int do_bedbug_step(struct cmd_tbl *cmdtp, int flag, int argc,
  214. char *const argv[])
  215. {
  216. unsigned long addr; /* Address to stop at */
  217. /* -------------------------------------------------- */
  218. if (!bug_ctx.stopped) {
  219. printf ("Not at a breakpoint\n");
  220. return 1;
  221. }
  222. if (!find_next_address((unsigned char *) &addr, false, bug_ctx.regs))
  223. return 1;
  224. if (bug_ctx.set)
  225. (*bug_ctx.set) (bug_ctx.current_bp, addr);
  226. bug_ctx.stopped = 0;
  227. return 0;
  228. } /* do_bedbug_step */
  229. U_BOOT_CMD (step, 1, 1, do_bedbug_step,
  230. "single step execution.",
  231. "");
  232. /* ======================================================================
  233. * Interpreter command to continue to the next instruction, stepping over
  234. * subroutines. Works by calling the find_next_addr() routine to compute
  235. * the address passes control to the CPU-specific set breakpoint routine
  236. * for the current breakpoint number.
  237. * ====================================================================== */
  238. int do_bedbug_next(struct cmd_tbl *cmdtp, int flag, int argc,
  239. char *const argv[])
  240. {
  241. unsigned long addr; /* Address to stop at */
  242. /* -------------------------------------------------- */
  243. if (!bug_ctx.stopped) {
  244. printf ("Not at a breakpoint\n");
  245. return 1;
  246. }
  247. if (!find_next_address((unsigned char *) &addr, true, bug_ctx.regs))
  248. return 1;
  249. if (bug_ctx.set)
  250. (*bug_ctx.set) (bug_ctx.current_bp, addr);
  251. bug_ctx.stopped = 0;
  252. return 0;
  253. } /* do_bedbug_next */
  254. U_BOOT_CMD (next, 1, 1, do_bedbug_next,
  255. "single step execution, stepping over subroutines.",
  256. "");
  257. /* ======================================================================
  258. * Interpreter command to print the current stack. This assumes an EABI
  259. * architecture, so it starts with GPR R1 and works back up the stack.
  260. * ====================================================================== */
  261. int do_bedbug_stack(struct cmd_tbl *cmdtp, int flag, int argc,
  262. 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->ram_start + gd->ram_size;
  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(struct cmd_tbl *cmdtp, int flag, int argc,
  299. 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. */