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);
43 /* ======================================================================
44 * Initialize the bug_ctx structure used by the bedbug debugger. This is
45 * specific to the CPU since each has different debug registers and
47 * ====================================================================== */
51 /* -------------------------------------------------- */
57 /* ======================================================================
58 * Entry point from the interpreter to the disassembler. Repeated calls
59 * will resume from the last disassembled address.
60 * ====================================================================== */
61 int do_bedbug_dis(struct cmd_tbl *cmdtp, int flag, int argc,
64 ulong addr; /* Address to start disassembly from */
65 ulong len; /* # of instructions to disassemble */
67 /* -------------------------------------------------- */
69 /* Setup to go from the last address if none is given */
76 if ((flag & CMD_FLAG_REPEAT) == 0) {
78 addr = hextoul(argv[1], NULL);
80 /* If an extra param is given then it is the length */
82 len = hextoul(argv[2], NULL);
85 /* Run the disassembler */
86 disppc ((unsigned char *) addr, 0, len, bedbug_puts, F_RADHEX);
88 dis_last_addr = addr + (len * 4);
93 U_BOOT_CMD (ds, 3, 1, do_bedbug_dis,
95 "ds <address> [# instructions]");
97 /* ======================================================================
98 * Entry point from the interpreter to the assembler. Assembles
99 * instructions in consecutive memory locations until a '.' (period) is
100 * entered on a line by itself.
101 * ====================================================================== */
102 int do_bedbug_asm(struct cmd_tbl *cmdtp, int flag, int argc,
105 long mem_addr; /* Address to assemble into */
106 unsigned long instr; /* Machine code for text */
107 char prompt[15]; /* Prompt string for user input */
108 int asm_err; /* Error code from the assembler */
110 /* -------------------------------------------------- */
114 return CMD_RET_USAGE;
116 printf ("\nEnter '.' when done\n");
117 mem_addr = hextoul(argv[1], NULL);
121 disppc ((unsigned char *) mem_addr, 0, 1, bedbug_puts,
124 sprintf (prompt, "%08lx: ", mem_addr);
125 cli_readline(prompt);
127 if (console_buffer[0] && strcmp (console_buffer, ".")) {
129 asmppc (mem_addr, console_buffer,
131 *(unsigned long *) mem_addr = instr;
134 printf ("*** Error: %s ***\n",
135 asm_error_str (asm_err));
143 } /* do_bedbug_asm */
145 U_BOOT_CMD (as, 2, 0, do_bedbug_asm,
146 "assemble memory", "as <address>");
148 /* ======================================================================
149 * Used to set a break point from the interpreter. Simply calls into the
150 * CPU-specific break point set routine.
151 * ====================================================================== */
153 int do_bedbug_break(struct cmd_tbl *cmdtp, int flag, int argc,
156 /* -------------------------------------------------- */
157 if (bug_ctx.do_break)
158 (*bug_ctx.do_break) (cmdtp, flag, argc, argv);
161 } /* do_bedbug_break */
163 U_BOOT_CMD (break, 3, 0, do_bedbug_break,
164 "set or clear a breakpoint",
165 " - Set or clear a breakpoint\n"
166 "break <address> - Break at an address\n"
167 "break off <bp#> - Disable breakpoint.\n"
168 "break show - List breakpoints.");
170 /* ======================================================================
171 * Called from the debug interrupt routine. Simply calls the CPU-specific
172 * breakpoint handling routine.
173 * ====================================================================== */
175 void do_bedbug_breakpoint (struct pt_regs *regs)
177 /* -------------------------------------------------- */
179 if (bug_ctx.break_isr)
180 (*bug_ctx.break_isr) (regs);
183 } /* do_bedbug_breakpoint */
187 /* ======================================================================
188 * Called from the CPU-specific breakpoint handling routine. Enter a
189 * mini main loop until the stopped flag is cleared from the breakpoint
192 * This handles the parts of the debugger that are common to all CPU's.
193 * ====================================================================== */
195 void bedbug_main_loop (unsigned long addr, struct pt_regs *regs)
197 int len; /* Length of command line */
198 int flag; /* Command flags */
199 int rc = 0; /* Result from run_command */
200 char prompt_str[20]; /* Prompt string */
201 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0 }; /* previous command */
202 /* -------------------------------------------------- */
205 (*bug_ctx.clear) (bug_ctx.current_bp);
207 printf ("Breakpoint %d: ", bug_ctx.current_bp);
208 disppc ((unsigned char *) addr, 0, 1, bedbug_puts, F_RADHEX);
213 sprintf (prompt_str, "BEDBUG.%d =>", bug_ctx.current_bp);
215 /* A miniature main loop */
216 while (bug_ctx.stopped) {
217 len = cli_readline(prompt_str);
219 flag = 0; /* assume no special flags for now */
222 strcpy (lastcommand, console_buffer);
224 flag |= CMD_FLAG_REPEAT;
227 printf ("<INTERRUPT>\n");
229 rc = run_command_repeatable(lastcommand, flag);
232 /* invalid command or not repeatable, forget it */
238 bug_ctx.current_bp = 0;
241 } /* bedbug_main_loop */
245 /* ======================================================================
246 * Interpreter command to continue from a breakpoint. Just clears the
247 * stopped flag in the context so that the breakpoint routine will
249 * ====================================================================== */
250 int do_bedbug_continue(struct cmd_tbl *cmdtp, int flag, int argc,
253 /* -------------------------------------------------- */
255 if (!bug_ctx.stopped) {
256 printf ("Not at a breakpoint\n");
262 } /* do_bedbug_continue */
264 U_BOOT_CMD (continue, 1, 0, do_bedbug_continue,
265 "continue from a breakpoint",
268 /* ======================================================================
269 * Interpreter command to continue to the next instruction, stepping into
270 * subroutines. Works by calling the find_next_addr() routine to compute
271 * the address passes control to the CPU-specific set breakpoint routine
272 * for the current breakpoint number.
273 * ====================================================================== */
274 int do_bedbug_step(struct cmd_tbl *cmdtp, int flag, int argc,
277 unsigned long addr; /* Address to stop at */
279 /* -------------------------------------------------- */
281 if (!bug_ctx.stopped) {
282 printf ("Not at a breakpoint\n");
286 if (!find_next_address((unsigned char *) &addr, false, bug_ctx.regs))
290 (*bug_ctx.set) (bug_ctx.current_bp, addr);
294 } /* do_bedbug_step */
296 U_BOOT_CMD (step, 1, 1, do_bedbug_step,
297 "single step execution.",
300 /* ======================================================================
301 * Interpreter command to continue to the next instruction, stepping over
302 * subroutines. Works by calling the find_next_addr() routine to compute
303 * the address passes control to the CPU-specific set breakpoint routine
304 * for the current breakpoint number.
305 * ====================================================================== */
306 int do_bedbug_next(struct cmd_tbl *cmdtp, int flag, int argc,
309 unsigned long addr; /* Address to stop at */
311 /* -------------------------------------------------- */
313 if (!bug_ctx.stopped) {
314 printf ("Not at a breakpoint\n");
318 if (!find_next_address((unsigned char *) &addr, true, bug_ctx.regs))
322 (*bug_ctx.set) (bug_ctx.current_bp, addr);
326 } /* do_bedbug_next */
328 U_BOOT_CMD (next, 1, 1, do_bedbug_next,
329 "single step execution, stepping over subroutines.",
332 /* ======================================================================
333 * Interpreter command to print the current stack. This assumes an EABI
334 * architecture, so it starts with GPR R1 and works back up the stack.
335 * ====================================================================== */
336 int do_bedbug_stack(struct cmd_tbl *cmdtp, int flag, int argc,
339 unsigned long sp; /* Stack pointer */
340 unsigned long func; /* LR from stack */
341 int depth; /* Stack iteration level */
342 int skip = 1; /* Flag to skip the first entry */
343 unsigned long top; /* Top of memory address */
345 /* -------------------------------------------------- */
347 if (!bug_ctx.stopped) {
348 printf ("Not at a breakpoint\n");
352 top = gd->ram_start + gd->ram_size;
355 printf ("Depth PC\n");
356 printf ("----- --------\n");
357 printf ("%5d %08lx\n", depth++, bug_ctx.regs->nip);
359 sp = bug_ctx.regs->gpr[1];
360 func = *(unsigned long *) (sp + 4);
362 while ((func < top) && (sp < top)) {
364 printf ("%5d %08lx\n", depth++, func);
368 sp = *(unsigned long *) sp;
369 func = *(unsigned long *) (sp + 4);
372 } /* do_bedbug_stack */
374 U_BOOT_CMD (where, 1, 1, do_bedbug_stack,
375 "Print the running stack.",
378 /* ======================================================================
379 * Interpreter command to dump the registers. Calls the CPU-specific
380 * show registers routine.
381 * ====================================================================== */
382 int do_bedbug_rdump(struct cmd_tbl *cmdtp, int flag, int argc,
385 /* -------------------------------------------------- */
387 if (!bug_ctx.stopped) {
388 printf ("Not at a breakpoint\n");
392 show_regs (bug_ctx.regs);
394 } /* do_bedbug_rdump */
396 U_BOOT_CMD (rdump, 1, 1, do_bedbug_rdump,
397 "Show registers.", "");
398 /* ====================================================================== */
402 * Copyright (c) 2001 William L. Pitts
403 * All rights reserved.
405 * Redistribution and use in source and binary forms are freely
406 * permitted provided that the above copyright notice and this
407 * paragraph and the following disclaimer are duplicated in all
410 * This software is provided "AS IS" and without any express or
411 * implied warranties, including, without limitation, the implied
412 * warranties of merchantability and fitness for a particular