7 #include <linux/ctype.h>
9 #include <bedbug/type.h>
10 #include <bedbug/bedbug.h>
11 #include <bedbug/regs.h>
12 #include <bedbug/ppc.h>
14 DECLARE_GLOBAL_DATA_PTR;
16 extern void show_regs __P ((struct pt_regs *));
17 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 */
24 /* ======================================================================
25 * U-Boot's puts function does not append a newline, so the bedbug stuff
26 * will use this for the output of the dis/assembler.
27 * ====================================================================== */
29 int bedbug_puts (const char *str)
31 /* -------------------------------------------------- */
33 printf ("%s\r\n", str);
39 /* ======================================================================
40 * Initialize the bug_ctx structure used by the bedbug debugger. This is
41 * specific to the CPU since each has different debug registers and
43 * ====================================================================== */
45 void bedbug_init (void)
47 /* -------------------------------------------------- */
49 #if defined(CONFIG_4xx)
50 void bedbug405_init (void);
53 #elif defined(CONFIG_8xx)
54 void bedbug860_init (void);
59 #if defined(CONFIG_MPC824X) || defined(CONFIG_MPC8260)
60 /* Processors that are 603e core based */
61 void bedbug603e_init (void);
71 /* ======================================================================
72 * Entry point from the interpreter to the disassembler. Repeated calls
73 * will resume from the last disassembled address.
74 * ====================================================================== */
75 int do_bedbug_dis (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
77 ulong addr; /* Address to start disassembly from */
78 ulong len; /* # of instructions to disassemble */
80 /* -------------------------------------------------- */
82 /* Setup to go from the last address if none is given */
87 return cmd_usage(cmdtp);
89 if ((flag & CMD_FLAG_REPEAT) == 0) {
91 addr = simple_strtoul (argv[1], NULL, 16);
93 /* If an extra param is given then it is the length */
95 len = simple_strtoul (argv[2], NULL, 16);
98 /* Run the disassembler */
99 disppc ((unsigned char *) addr, 0, len, bedbug_puts, F_RADHEX);
101 dis_last_addr = addr + (len * 4);
104 } /* do_bedbug_dis */
106 U_BOOT_CMD (ds, 3, 1, do_bedbug_dis,
107 "disassemble memory",
108 "ds <address> [# instructions]");
110 /* ======================================================================
111 * Entry point from the interpreter to the assembler. Assembles
112 * instructions in consecutive memory locations until a '.' (period) is
113 * entered on a line by itself.
114 * ====================================================================== */
115 int do_bedbug_asm (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
117 long mem_addr; /* Address to assemble into */
118 unsigned long instr; /* Machine code for text */
119 char prompt[15]; /* Prompt string for user input */
120 int asm_err; /* Error code from the assembler */
122 /* -------------------------------------------------- */
126 return cmd_usage(cmdtp);
128 printf ("\nEnter '.' when done\n");
129 mem_addr = simple_strtoul (argv[1], NULL, 16);
133 disppc ((unsigned char *) mem_addr, 0, 1, bedbug_puts,
136 sprintf (prompt, "%08lx: ", mem_addr);
139 if (console_buffer[0] && strcmp (console_buffer, ".")) {
141 asmppc (mem_addr, console_buffer,
143 *(unsigned long *) mem_addr = instr;
146 printf ("*** Error: %s ***\n",
147 asm_error_str (asm_err));
155 } /* do_bedbug_asm */
157 U_BOOT_CMD (as, 2, 0, do_bedbug_asm,
158 "assemble memory", "as <address>");
160 /* ======================================================================
161 * Used to set a break point from the interpreter. Simply calls into the
162 * CPU-specific break point set routine.
163 * ====================================================================== */
165 int do_bedbug_break (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
167 /* -------------------------------------------------- */
168 if (bug_ctx.do_break)
169 (*bug_ctx.do_break) (cmdtp, flag, argc, argv);
172 } /* do_bedbug_break */
174 U_BOOT_CMD (break, 3, 0, do_bedbug_break,
175 "set or clear a breakpoint",
176 " - Set or clear a breakpoint\n"
177 "break <address> - Break at an address\n"
178 "break off <bp#> - Disable breakpoint.\n"
179 "break show - List breakpoints.");
181 /* ======================================================================
182 * Called from the debug interrupt routine. Simply calls the CPU-specific
183 * breakpoint handling routine.
184 * ====================================================================== */
186 void do_bedbug_breakpoint (struct pt_regs *regs)
188 /* -------------------------------------------------- */
190 if (bug_ctx.break_isr)
191 (*bug_ctx.break_isr) (regs);
194 } /* do_bedbug_breakpoint */
198 /* ======================================================================
199 * Called from the CPU-specific breakpoint handling routine. Enter a
200 * mini main loop until the stopped flag is cleared from the breakpoint
203 * This handles the parts of the debugger that are common to all CPU's.
204 * ====================================================================== */
206 void bedbug_main_loop (unsigned long addr, struct pt_regs *regs)
208 int len; /* Length of command line */
209 int flag; /* Command flags */
210 int rc = 0; /* Result from run_command */
211 char prompt_str[20]; /* Prompt string */
212 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0 }; /* previous command */
213 /* -------------------------------------------------- */
216 (*bug_ctx.clear) (bug_ctx.current_bp);
218 printf ("Breakpoint %d: ", bug_ctx.current_bp);
219 disppc ((unsigned char *) addr, 0, 1, bedbug_puts, F_RADHEX);
224 sprintf (prompt_str, "BEDBUG.%d =>", bug_ctx.current_bp);
226 /* A miniature main loop */
227 while (bug_ctx.stopped) {
228 len = readline (prompt_str);
230 flag = 0; /* assume no special flags for now */
233 strcpy (lastcommand, console_buffer);
235 flag |= CMD_FLAG_REPEAT;
238 printf ("<INTERRUPT>\n");
240 rc = run_command (lastcommand, flag);
243 /* invalid command or not repeatable, forget it */
249 bug_ctx.current_bp = 0;
252 } /* bedbug_main_loop */
256 /* ======================================================================
257 * Interpreter command to continue from a breakpoint. Just clears the
258 * stopped flag in the context so that the breakpoint routine will
260 * ====================================================================== */
261 int do_bedbug_continue (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
263 /* -------------------------------------------------- */
265 if (!bug_ctx.stopped) {
266 printf ("Not at a breakpoint\n");
272 } /* do_bedbug_continue */
274 U_BOOT_CMD (continue, 1, 0, do_bedbug_continue,
275 "continue from a breakpoint",
278 /* ======================================================================
279 * Interpreter command to continue to the next instruction, stepping into
280 * subroutines. Works by calling the find_next_addr() routine to compute
281 * the address passes control to the CPU-specific set breakpoint routine
282 * for the current breakpoint number.
283 * ====================================================================== */
284 int do_bedbug_step (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
286 unsigned long addr; /* Address to stop at */
288 /* -------------------------------------------------- */
290 if (!bug_ctx.stopped) {
291 printf ("Not at a breakpoint\n");
295 if (!find_next_address ((unsigned char *) &addr, FALSE, bug_ctx.regs))
299 (*bug_ctx.set) (bug_ctx.current_bp, addr);
303 } /* do_bedbug_step */
305 U_BOOT_CMD (step, 1, 1, do_bedbug_step,
306 "single step execution.",
309 /* ======================================================================
310 * Interpreter command to continue to the next instruction, stepping over
311 * subroutines. Works by calling the find_next_addr() routine to compute
312 * the address passes control to the CPU-specific set breakpoint routine
313 * for the current breakpoint number.
314 * ====================================================================== */
315 int do_bedbug_next (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
317 unsigned long addr; /* Address to stop at */
319 /* -------------------------------------------------- */
321 if (!bug_ctx.stopped) {
322 printf ("Not at a breakpoint\n");
326 if (!find_next_address ((unsigned char *) &addr, TRUE, bug_ctx.regs))
330 (*bug_ctx.set) (bug_ctx.current_bp, addr);
334 } /* do_bedbug_next */
336 U_BOOT_CMD (next, 1, 1, do_bedbug_next,
337 "single step execution, stepping over subroutines.",
340 /* ======================================================================
341 * Interpreter command to print the current stack. This assumes an EABI
342 * architecture, so it starts with GPR R1 and works back up the stack.
343 * ====================================================================== */
344 int do_bedbug_stack (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
346 unsigned long sp; /* Stack pointer */
347 unsigned long func; /* LR from stack */
348 int depth; /* Stack iteration level */
349 int skip = 1; /* Flag to skip the first entry */
350 unsigned long top; /* Top of memory address */
352 /* -------------------------------------------------- */
354 if (!bug_ctx.stopped) {
355 printf ("Not at a breakpoint\n");
359 top = gd->bd->bi_memstart + gd->bd->bi_memsize;
362 printf ("Depth PC\n");
363 printf ("----- --------\n");
364 printf ("%5d %08lx\n", depth++, bug_ctx.regs->nip);
366 sp = bug_ctx.regs->gpr[1];
367 func = *(unsigned long *) (sp + 4);
369 while ((func < top) && (sp < top)) {
371 printf ("%5d %08lx\n", depth++, func);
375 sp = *(unsigned long *) sp;
376 func = *(unsigned long *) (sp + 4);
379 } /* do_bedbug_stack */
381 U_BOOT_CMD (where, 1, 1, do_bedbug_stack,
382 "Print the running stack.",
385 /* ======================================================================
386 * Interpreter command to dump the registers. Calls the CPU-specific
387 * show registers routine.
388 * ====================================================================== */
389 int do_bedbug_rdump (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
391 /* -------------------------------------------------- */
393 if (!bug_ctx.stopped) {
394 printf ("Not at a breakpoint\n");
398 show_regs (bug_ctx.regs);
400 } /* do_bedbug_rdump */
402 U_BOOT_CMD (rdump, 1, 1, do_bedbug_rdump,
403 "Show registers.", "");
404 /* ====================================================================== */
408 * Copyright (c) 2001 William L. Pitts
409 * All rights reserved.
411 * Redistribution and use in source and binary forms are freely
412 * permitted provided that the above copyright notice and this
413 * paragraph and the following disclaimer are duplicated in all
416 * This software is provided "AS IS" and without any express or
417 * implied warranties, including, without limitation, the implied
418 * warranties of merchantability and fitness for a particular