bedbug.c 12 KB

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