cmd_bedbug.c 13 KB

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