9 #include <asm/global_data.h>
10 #include <asm/ptrace.h>
11 #include <linux/ctype.h>
13 #include <bedbug/type.h>
14 #include <bedbug/bedbug.h>
15 #include <bedbug/regs.h>
16 #include <bedbug/ppc.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 extern void show_regs __P ((struct pt_regs *));
21 extern int run_command __P ((const char *, int));
23 ulong dis_last_addr = 0; /* Last address disassembled */
24 ulong dis_last_len = 20; /* Default disassembler length */
25 CPU_DEBUG_CTX bug_ctx; /* Bedbug context structure */
28 /* ======================================================================
29 * U-Boot's puts function does not append a newline, so the bedbug stuff
30 * will use this for the output of the dis/assembler.
31 * ====================================================================== */
33 int bedbug_puts (const char *str)
35 /* -------------------------------------------------- */
37 printf ("%s\r\n", str);
42 /* ======================================================================
43 * Initialize the bug_ctx structure used by the bedbug debugger. This is
44 * specific to the CPU since each has different debug registers and
46 * ====================================================================== */
50 /* -------------------------------------------------- */
55 /* ======================================================================
56 * Entry point from the interpreter to the disassembler. Repeated calls
57 * will resume from the last disassembled address.
58 * ====================================================================== */
59 int do_bedbug_dis(struct cmd_tbl *cmdtp, int flag, int argc,
62 ulong addr; /* Address to start disassembly from */
63 ulong len; /* # of instructions to disassemble */
65 /* -------------------------------------------------- */
67 /* Setup to go from the last address if none is given */
74 if ((flag & CMD_FLAG_REPEAT) == 0) {
76 addr = hextoul(argv[1], NULL);
78 /* If an extra param is given then it is the length */
80 len = hextoul(argv[2], NULL);
83 /* Run the disassembler */
84 disppc ((unsigned char *) addr, 0, len, bedbug_puts, F_RADHEX);
86 dis_last_addr = addr + (len * 4);
91 U_BOOT_CMD (ds, 3, 1, do_bedbug_dis,
93 "ds <address> [# instructions]");
95 /* ======================================================================
96 * Entry point from the interpreter to the assembler. Assembles
97 * instructions in consecutive memory locations until a '.' (period) is
98 * entered on a line by itself.
99 * ====================================================================== */
100 int do_bedbug_asm(struct cmd_tbl *cmdtp, int flag, int argc,
103 long mem_addr; /* Address to assemble into */
104 unsigned long instr; /* Machine code for text */
105 char prompt[15]; /* Prompt string for user input */
106 int asm_err; /* Error code from the assembler */
108 /* -------------------------------------------------- */
112 return CMD_RET_USAGE;
114 printf ("\nEnter '.' when done\n");
115 mem_addr = hextoul(argv[1], NULL);
119 disppc ((unsigned char *) mem_addr, 0, 1, bedbug_puts,
122 sprintf (prompt, "%08lx: ", mem_addr);
123 cli_readline(prompt);
125 if (console_buffer[0] && strcmp (console_buffer, ".")) {
127 asmppc (mem_addr, console_buffer,
129 *(unsigned long *) mem_addr = instr;
132 printf ("*** Error: %s ***\n",
133 asm_error_str (asm_err));
141 } /* do_bedbug_asm */
143 U_BOOT_CMD (as, 2, 0, do_bedbug_asm,
144 "assemble memory", "as <address>");
146 /* ======================================================================
147 * Used to set a break point from the interpreter. Simply calls into the
148 * CPU-specific break point set routine.
149 * ====================================================================== */
151 int do_bedbug_break(struct cmd_tbl *cmdtp, int flag, int argc,
154 /* -------------------------------------------------- */
155 if (bug_ctx.do_break)
156 (*bug_ctx.do_break) (cmdtp, flag, argc, argv);
159 } /* do_bedbug_break */
161 U_BOOT_CMD (break, 3, 0, do_bedbug_break,
162 "set or clear a breakpoint",
163 " - Set or clear a breakpoint\n"
164 "break <address> - Break at an address\n"
165 "break off <bp#> - Disable breakpoint.\n"
166 "break show - List breakpoints.");
168 /* ======================================================================
169 * Called from the debug interrupt routine. Simply calls the CPU-specific
170 * breakpoint handling routine.
171 * ====================================================================== */
173 void do_bedbug_breakpoint (struct pt_regs *regs)
175 /* -------------------------------------------------- */
177 if (bug_ctx.break_isr)
178 (*bug_ctx.break_isr) (regs);
181 } /* do_bedbug_breakpoint */
184 /* ======================================================================
185 * Called from the CPU-specific breakpoint handling routine. Enter a
186 * mini main loop until the stopped flag is cleared from the breakpoint
189 * This handles the parts of the debugger that are common to all CPU's.
190 * ====================================================================== */
192 void bedbug_main_loop (unsigned long addr, struct pt_regs *regs)
194 int len; /* Length of command line */
195 int flag; /* Command flags */
196 int rc = 0; /* Result from run_command */
197 char prompt_str[20]; /* Prompt string */
198 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0 }; /* previous command */
199 /* -------------------------------------------------- */
202 (*bug_ctx.clear) (bug_ctx.current_bp);
204 printf ("Breakpoint %d: ", bug_ctx.current_bp);
205 disppc ((unsigned char *) addr, 0, 1, bedbug_puts, F_RADHEX);
210 sprintf (prompt_str, "BEDBUG.%d =>", bug_ctx.current_bp);
212 /* A miniature main loop */
213 while (bug_ctx.stopped) {
214 len = cli_readline(prompt_str);
216 flag = 0; /* assume no special flags for now */
219 strcpy (lastcommand, console_buffer);
221 flag |= CMD_FLAG_REPEAT;
224 printf ("<INTERRUPT>\n");
226 rc = run_command_repeatable(lastcommand, flag);
229 /* invalid command or not repeatable, forget it */
235 bug_ctx.current_bp = 0;
238 } /* bedbug_main_loop */
241 /* ======================================================================
242 * Interpreter command to continue from a breakpoint. Just clears the
243 * stopped flag in the context so that the breakpoint routine will
245 * ====================================================================== */
246 int do_bedbug_continue(struct cmd_tbl *cmdtp, int flag, int argc,
249 /* -------------------------------------------------- */
251 if (!bug_ctx.stopped) {
252 printf ("Not at a breakpoint\n");
258 } /* do_bedbug_continue */
260 U_BOOT_CMD (continue, 1, 0, do_bedbug_continue,
261 "continue from a breakpoint",
264 /* ======================================================================
265 * Interpreter command to continue to the next instruction, stepping into
266 * subroutines. Works by calling the find_next_addr() routine to compute
267 * the address passes control to the CPU-specific set breakpoint routine
268 * for the current breakpoint number.
269 * ====================================================================== */
270 int do_bedbug_step(struct cmd_tbl *cmdtp, int flag, int argc,
273 unsigned long addr; /* Address to stop at */
275 /* -------------------------------------------------- */
277 if (!bug_ctx.stopped) {
278 printf ("Not at a breakpoint\n");
282 if (!find_next_address((unsigned char *) &addr, false, bug_ctx.regs))
286 (*bug_ctx.set) (bug_ctx.current_bp, addr);
290 } /* do_bedbug_step */
292 U_BOOT_CMD (step, 1, 1, do_bedbug_step,
293 "single step execution.",
296 /* ======================================================================
297 * Interpreter command to continue to the next instruction, stepping over
298 * subroutines. Works by calling the find_next_addr() routine to compute
299 * the address passes control to the CPU-specific set breakpoint routine
300 * for the current breakpoint number.
301 * ====================================================================== */
302 int do_bedbug_next(struct cmd_tbl *cmdtp, int flag, int argc,
305 unsigned long addr; /* Address to stop at */
307 /* -------------------------------------------------- */
309 if (!bug_ctx.stopped) {
310 printf ("Not at a breakpoint\n");
314 if (!find_next_address((unsigned char *) &addr, true, bug_ctx.regs))
318 (*bug_ctx.set) (bug_ctx.current_bp, addr);
322 } /* do_bedbug_next */
324 U_BOOT_CMD (next, 1, 1, do_bedbug_next,
325 "single step execution, stepping over subroutines.",
328 /* ======================================================================
329 * Interpreter command to print the current stack. This assumes an EABI
330 * architecture, so it starts with GPR R1 and works back up the stack.
331 * ====================================================================== */
332 int do_bedbug_stack(struct cmd_tbl *cmdtp, int flag, int argc,
335 unsigned long sp; /* Stack pointer */
336 unsigned long func; /* LR from stack */
337 int depth; /* Stack iteration level */
338 int skip = 1; /* Flag to skip the first entry */
339 unsigned long top; /* Top of memory address */
341 /* -------------------------------------------------- */
343 if (!bug_ctx.stopped) {
344 printf ("Not at a breakpoint\n");
348 top = gd->ram_start + gd->ram_size;
351 printf ("Depth PC\n");
352 printf ("----- --------\n");
353 printf ("%5d %08lx\n", depth++, bug_ctx.regs->nip);
355 sp = bug_ctx.regs->gpr[1];
356 func = *(unsigned long *) (sp + 4);
358 while ((func < top) && (sp < top)) {
360 printf ("%5d %08lx\n", depth++, func);
364 sp = *(unsigned long *) sp;
365 func = *(unsigned long *) (sp + 4);
368 } /* do_bedbug_stack */
370 U_BOOT_CMD (where, 1, 1, do_bedbug_stack,
371 "Print the running stack.",
374 /* ======================================================================
375 * Interpreter command to dump the registers. Calls the CPU-specific
376 * show registers routine.
377 * ====================================================================== */
378 int do_bedbug_rdump(struct cmd_tbl *cmdtp, int flag, int argc,
381 /* -------------------------------------------------- */
383 if (!bug_ctx.stopped) {
384 printf ("Not at a breakpoint\n");
388 show_regs (bug_ctx.regs);
390 } /* do_bedbug_rdump */
392 U_BOOT_CMD (rdump, 1, 1, do_bedbug_rdump,
393 "Show registers.", "");
394 /* ====================================================================== */
398 * Copyright (c) 2001 William L. Pitts
399 * All rights reserved.
401 * Redistribution and use in source and binary forms are freely
402 * permitted provided that the above copyright notice and this
403 * paragraph and the following disclaimer are duplicated in all
406 * This software is provided "AS IS" and without any express or
407 * implied warranties, including, without limitation, the implied
408 * warranties of merchantability and fitness for a particular