9 #include <linux/ctype.h>
11 #include <bedbug/type.h>
12 #include <bedbug/bedbug.h>
13 #include <bedbug/regs.h>
14 #include <bedbug/ppc.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 extern void show_regs __P ((struct pt_regs *));
19 extern int run_command __P ((const char *, int));
21 ulong dis_last_addr = 0; /* Last address disassembled */
22 ulong dis_last_len = 20; /* Default disassembler length */
23 CPU_DEBUG_CTX bug_ctx; /* Bedbug context structure */
26 /* ======================================================================
27 * U-Boot's puts function does not append a newline, so the bedbug stuff
28 * will use this for the output of the dis/assembler.
29 * ====================================================================== */
31 int bedbug_puts (const char *str)
33 /* -------------------------------------------------- */
35 printf ("%s\r\n", str);
41 /* ======================================================================
42 * Initialize the bug_ctx structure used by the bedbug debugger. This is
43 * specific to the CPU since each has different debug registers and
45 * ====================================================================== */
47 void bedbug_init (void)
49 /* -------------------------------------------------- */
51 #if defined(CONFIG_4xx)
52 void bedbug405_init (void);
57 #if defined(CONFIG_MPC824X) || defined(CONFIG_MPC8260)
58 /* Processors that are 603e core based */
59 void bedbug603e_init (void);
69 /* ======================================================================
70 * Entry point from the interpreter to the disassembler. Repeated calls
71 * will resume from the last disassembled address.
72 * ====================================================================== */
73 int do_bedbug_dis (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
75 ulong addr; /* Address to start disassembly from */
76 ulong len; /* # of instructions to disassemble */
78 /* -------------------------------------------------- */
80 /* Setup to go from the last address if none is given */
87 if ((flag & CMD_FLAG_REPEAT) == 0) {
89 addr = simple_strtoul (argv[1], NULL, 16);
91 /* If an extra param is given then it is the length */
93 len = simple_strtoul (argv[2], NULL, 16);
96 /* Run the disassembler */
97 disppc ((unsigned char *) addr, 0, len, bedbug_puts, F_RADHEX);
99 dis_last_addr = addr + (len * 4);
102 } /* do_bedbug_dis */
104 U_BOOT_CMD (ds, 3, 1, do_bedbug_dis,
105 "disassemble memory",
106 "ds <address> [# instructions]");
108 /* ======================================================================
109 * Entry point from the interpreter to the assembler. Assembles
110 * instructions in consecutive memory locations until a '.' (period) is
111 * entered on a line by itself.
112 * ====================================================================== */
113 int do_bedbug_asm (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
115 long mem_addr; /* Address to assemble into */
116 unsigned long instr; /* Machine code for text */
117 char prompt[15]; /* Prompt string for user input */
118 int asm_err; /* Error code from the assembler */
120 /* -------------------------------------------------- */
124 return CMD_RET_USAGE;
126 printf ("\nEnter '.' when done\n");
127 mem_addr = simple_strtoul (argv[1], NULL, 16);
131 disppc ((unsigned char *) mem_addr, 0, 1, bedbug_puts,
134 sprintf (prompt, "%08lx: ", mem_addr);
135 cli_readline(prompt);
137 if (console_buffer[0] && strcmp (console_buffer, ".")) {
139 asmppc (mem_addr, console_buffer,
141 *(unsigned long *) mem_addr = instr;
144 printf ("*** Error: %s ***\n",
145 asm_error_str (asm_err));
153 } /* do_bedbug_asm */
155 U_BOOT_CMD (as, 2, 0, do_bedbug_asm,
156 "assemble memory", "as <address>");
158 /* ======================================================================
159 * Used to set a break point from the interpreter. Simply calls into the
160 * CPU-specific break point set routine.
161 * ====================================================================== */
163 int do_bedbug_break (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
165 /* -------------------------------------------------- */
166 if (bug_ctx.do_break)
167 (*bug_ctx.do_break) (cmdtp, flag, argc, argv);
170 } /* do_bedbug_break */
172 U_BOOT_CMD (break, 3, 0, do_bedbug_break,
173 "set or clear a breakpoint",
174 " - Set or clear a breakpoint\n"
175 "break <address> - Break at an address\n"
176 "break off <bp#> - Disable breakpoint.\n"
177 "break show - List breakpoints.");
179 /* ======================================================================
180 * Called from the debug interrupt routine. Simply calls the CPU-specific
181 * breakpoint handling routine.
182 * ====================================================================== */
184 void do_bedbug_breakpoint (struct pt_regs *regs)
186 /* -------------------------------------------------- */
188 if (bug_ctx.break_isr)
189 (*bug_ctx.break_isr) (regs);
192 } /* do_bedbug_breakpoint */
196 /* ======================================================================
197 * Called from the CPU-specific breakpoint handling routine. Enter a
198 * mini main loop until the stopped flag is cleared from the breakpoint
201 * This handles the parts of the debugger that are common to all CPU's.
202 * ====================================================================== */
204 void bedbug_main_loop (unsigned long addr, struct pt_regs *regs)
206 int len; /* Length of command line */
207 int flag; /* Command flags */
208 int rc = 0; /* Result from run_command */
209 char prompt_str[20]; /* Prompt string */
210 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0 }; /* previous command */
211 /* -------------------------------------------------- */
214 (*bug_ctx.clear) (bug_ctx.current_bp);
216 printf ("Breakpoint %d: ", bug_ctx.current_bp);
217 disppc ((unsigned char *) addr, 0, 1, bedbug_puts, F_RADHEX);
222 sprintf (prompt_str, "BEDBUG.%d =>", bug_ctx.current_bp);
224 /* A miniature main loop */
225 while (bug_ctx.stopped) {
226 len = cli_readline(prompt_str);
228 flag = 0; /* assume no special flags for now */
231 strcpy (lastcommand, console_buffer);
233 flag |= CMD_FLAG_REPEAT;
236 printf ("<INTERRUPT>\n");
238 rc = run_command_repeatable(lastcommand, flag);
241 /* invalid command or not repeatable, forget it */
247 bug_ctx.current_bp = 0;
250 } /* bedbug_main_loop */
254 /* ======================================================================
255 * Interpreter command to continue from a breakpoint. Just clears the
256 * stopped flag in the context so that the breakpoint routine will
258 * ====================================================================== */
259 int do_bedbug_continue (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
261 /* -------------------------------------------------- */
263 if (!bug_ctx.stopped) {
264 printf ("Not at a breakpoint\n");
270 } /* do_bedbug_continue */
272 U_BOOT_CMD (continue, 1, 0, do_bedbug_continue,
273 "continue from a breakpoint",
276 /* ======================================================================
277 * Interpreter command to continue to the next instruction, stepping into
278 * subroutines. Works by calling the find_next_addr() routine to compute
279 * the address passes control to the CPU-specific set breakpoint routine
280 * for the current breakpoint number.
281 * ====================================================================== */
282 int do_bedbug_step (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
284 unsigned long addr; /* Address to stop at */
286 /* -------------------------------------------------- */
288 if (!bug_ctx.stopped) {
289 printf ("Not at a breakpoint\n");
293 if (!find_next_address((unsigned char *) &addr, false, bug_ctx.regs))
297 (*bug_ctx.set) (bug_ctx.current_bp, addr);
301 } /* do_bedbug_step */
303 U_BOOT_CMD (step, 1, 1, do_bedbug_step,
304 "single step execution.",
307 /* ======================================================================
308 * Interpreter command to continue to the next instruction, stepping over
309 * subroutines. Works by calling the find_next_addr() routine to compute
310 * the address passes control to the CPU-specific set breakpoint routine
311 * for the current breakpoint number.
312 * ====================================================================== */
313 int do_bedbug_next (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
315 unsigned long addr; /* Address to stop at */
317 /* -------------------------------------------------- */
319 if (!bug_ctx.stopped) {
320 printf ("Not at a breakpoint\n");
324 if (!find_next_address((unsigned char *) &addr, true, bug_ctx.regs))
328 (*bug_ctx.set) (bug_ctx.current_bp, addr);
332 } /* do_bedbug_next */
334 U_BOOT_CMD (next, 1, 1, do_bedbug_next,
335 "single step execution, stepping over subroutines.",
338 /* ======================================================================
339 * Interpreter command to print the current stack. This assumes an EABI
340 * architecture, so it starts with GPR R1 and works back up the stack.
341 * ====================================================================== */
342 int do_bedbug_stack (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
344 unsigned long sp; /* Stack pointer */
345 unsigned long func; /* LR from stack */
346 int depth; /* Stack iteration level */
347 int skip = 1; /* Flag to skip the first entry */
348 unsigned long top; /* Top of memory address */
350 /* -------------------------------------------------- */
352 if (!bug_ctx.stopped) {
353 printf ("Not at a breakpoint\n");
357 top = gd->bd->bi_memstart + gd->bd->bi_memsize;
360 printf ("Depth PC\n");
361 printf ("----- --------\n");
362 printf ("%5d %08lx\n", depth++, bug_ctx.regs->nip);
364 sp = bug_ctx.regs->gpr[1];
365 func = *(unsigned long *) (sp + 4);
367 while ((func < top) && (sp < top)) {
369 printf ("%5d %08lx\n", depth++, func);
373 sp = *(unsigned long *) sp;
374 func = *(unsigned long *) (sp + 4);
377 } /* do_bedbug_stack */
379 U_BOOT_CMD (where, 1, 1, do_bedbug_stack,
380 "Print the running stack.",
383 /* ======================================================================
384 * Interpreter command to dump the registers. Calls the CPU-specific
385 * show registers routine.
386 * ====================================================================== */
387 int do_bedbug_rdump (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
389 /* -------------------------------------------------- */
391 if (!bug_ctx.stopped) {
392 printf ("Not at a breakpoint\n");
396 show_regs (bug_ctx.regs);
398 } /* do_bedbug_rdump */
400 U_BOOT_CMD (rdump, 1, 1, do_bedbug_rdump,
401 "Show registers.", "");
402 /* ====================================================================== */
406 * Copyright (c) 2001 William L. Pitts
407 * All rights reserved.
409 * Redistribution and use in source and binary forms are freely
410 * permitted provided that the above copyright notice and this
411 * paragraph and the following disclaimer are duplicated in all
414 * This software is provided "AS IS" and without any express or
415 * implied warranties, including, without limitation, the implied
416 * warranties of merchantability and fitness for a particular