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 #if (CONFIG_COMMANDS & CFG_CMD_BEDBUG)
19 #define MAX(a,b) ((a) > (b) ? (a) : (b))
22 extern void show_regs
__P ((struct pt_regs
*));
23 extern int run_command
__P ((const char *, int));
24 extern char console_buffer
[];
26 ulong dis_last_addr
= 0; /* Last address disassembled */
27 ulong dis_last_len
= 20; /* Default disassembler length */
28 CPU_DEBUG_CTX bug_ctx
; /* Bedbug context structure */
31 /* ======================================================================
32 * U-Boot's puts function does not append a newline, so the bedbug stuff
33 * will use this for the output of the dis/assembler.
34 * ====================================================================== */
36 int bedbug_puts (const char *str
)
38 /* -------------------------------------------------- */
40 printf ("%s\r\n", str
);
46 /* ======================================================================
47 * Initialize the bug_ctx structure used by the bedbug debugger. This is
48 * specific to the CPU since each has different debug registers and
50 * ====================================================================== */
52 void bedbug_init (void)
54 /* -------------------------------------------------- */
56 #if defined(CONFIG_4xx)
57 void bedbug405_init (void);
60 #elif defined(CONFIG_8xx)
61 void bedbug860_init (void);
66 #if defined(CONFIG_MPC824X) || defined(CONFIG_MPC8260)
67 /* Processors that are 603e core based */
68 void bedbug603e_init (void);
78 /* ======================================================================
79 * Entry point from the interpreter to the disassembler. Repeated calls
80 * will resume from the last disassembled address.
81 * ====================================================================== */
82 int do_bedbug_dis (cmd_tbl_t
* cmdtp
, int flag
, int argc
, char *argv
[])
84 ulong addr
; /* Address to start disassembly from */
85 ulong len
; /* # of instructions to disassemble */
87 /* -------------------------------------------------- */
89 /* Setup to go from the last address if none is given */
94 printf ("Usage:\n%s\n", cmdtp
->usage
);
98 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
100 addr
= simple_strtoul (argv
[1], NULL
, 16);
102 /* If an extra param is given then it is the length */
104 len
= simple_strtoul (argv
[2], NULL
, 16);
107 /* Run the disassembler */
108 disppc ((unsigned char *) addr
, 0, len
, bedbug_puts
, F_RADHEX
);
110 dis_last_addr
= addr
+ (len
* 4);
113 } /* do_bedbug_dis */
115 U_BOOT_CMD (ds
, 3, 1, do_bedbug_dis
,
116 "ds - disassemble memory\n",
117 "ds <address> [# instructions]\n");
119 /* ======================================================================
120 * Entry point from the interpreter to the assembler. Assembles
121 * instructions in consecutive memory locations until a '.' (period) is
122 * entered on a line by itself.
123 * ====================================================================== */
124 int do_bedbug_asm (cmd_tbl_t
* cmdtp
, int flag
, int argc
, char *argv
[])
126 long mem_addr
; /* Address to assemble into */
127 unsigned long instr
; /* Machine code for text */
128 char prompt
[15]; /* Prompt string for user input */
129 int asm_err
; /* Error code from the assembler */
131 /* -------------------------------------------------- */
135 printf ("Usage:\n%s\n", cmdtp
->usage
);
139 printf ("\nEnter '.' when done\n");
140 mem_addr
= simple_strtoul (argv
[1], NULL
, 16);
144 disppc ((unsigned char *) mem_addr
, 0, 1, bedbug_puts
,
147 sprintf (prompt
, "%08lx: ", mem_addr
);
150 if (console_buffer
[0] && strcmp (console_buffer
, ".")) {
152 asmppc (mem_addr
, console_buffer
,
154 *(unsigned long *) mem_addr
= instr
;
157 printf ("*** Error: %s ***\n",
158 asm_error_str (asm_err
));
166 } /* do_bedbug_asm */
168 U_BOOT_CMD (as
, 2, 0, do_bedbug_asm
,
169 "as - assemble memory\n", "as <address>\n");
171 /* ======================================================================
172 * Used to set a break point from the interpreter. Simply calls into the
173 * CPU-specific break point set routine.
174 * ====================================================================== */
176 int do_bedbug_break (cmd_tbl_t
* cmdtp
, int flag
, int argc
, char *argv
[])
178 /* -------------------------------------------------- */
179 if (bug_ctx
.do_break
)
180 (*bug_ctx
.do_break
) (cmdtp
, flag
, argc
, argv
);
183 } /* do_bedbug_break */
185 U_BOOT_CMD (break, 3, 0, do_bedbug_break
,
186 "break - set or clear a breakpoint\n",
187 " - Set or clear a breakpoint\n"
188 "break <address> - Break at an address\n"
189 "break off <bp#> - Disable breakpoint.\n"
190 "break show - List breakpoints.\n");
192 /* ======================================================================
193 * Called from the debug interrupt routine. Simply calls the CPU-specific
194 * breakpoint handling routine.
195 * ====================================================================== */
197 void do_bedbug_breakpoint (struct pt_regs
*regs
)
199 /* -------------------------------------------------- */
201 if (bug_ctx
.break_isr
)
202 (*bug_ctx
.break_isr
) (regs
);
205 } /* do_bedbug_breakpoint */
209 /* ======================================================================
210 * Called from the CPU-specific breakpoint handling routine. Enter a
211 * mini main loop until the stopped flag is cleared from the breakpoint
214 * This handles the parts of the debugger that are common to all CPU's.
215 * ====================================================================== */
217 void bedbug_main_loop (unsigned long addr
, struct pt_regs
*regs
)
219 int len
; /* Length of command line */
220 int flag
; /* Command flags */
221 int rc
= 0; /* Result from run_command */
222 char prompt_str
[20]; /* Prompt string */
223 static char lastcommand
[CFG_CBSIZE
] = { 0 }; /* previous command */
224 /* -------------------------------------------------- */
227 (*bug_ctx
.clear
) (bug_ctx
.current_bp
);
229 printf ("Breakpoint %d: ", bug_ctx
.current_bp
);
230 disppc ((unsigned char *) addr
, 0, 1, bedbug_puts
, F_RADHEX
);
235 sprintf (prompt_str
, "BEDBUG.%d =>", bug_ctx
.current_bp
);
237 /* A miniature main loop */
238 while (bug_ctx
.stopped
) {
239 len
= readline (prompt_str
);
241 flag
= 0; /* assume no special flags for now */
244 strcpy (lastcommand
, console_buffer
);
246 flag
|= CMD_FLAG_REPEAT
;
249 printf ("<INTERRUPT>\n");
251 rc
= run_command (lastcommand
, flag
);
254 /* invalid command or not repeatable, forget it */
260 bug_ctx
.current_bp
= 0;
263 } /* bedbug_main_loop */
267 /* ======================================================================
268 * Interpreter command to continue from a breakpoint. Just clears the
269 * stopped flag in the context so that the breakpoint routine will
271 * ====================================================================== */
272 int do_bedbug_continue (cmd_tbl_t
* cmdtp
, int flag
, int argc
, char *argv
[])
274 /* -------------------------------------------------- */
276 if (!bug_ctx
.stopped
) {
277 printf ("Not at a breakpoint\n");
283 } /* do_bedbug_continue */
285 U_BOOT_CMD (continue, 1, 0, do_bedbug_continue
,
286 "continue- continue from a breakpoint\n",
287 " - continue from a breakpoint.\n");
289 /* ======================================================================
290 * Interpreter command to continue to the next instruction, stepping into
291 * subroutines. Works by calling the find_next_addr() routine to compute
292 * the address passes control to the CPU-specific set breakpoint routine
293 * for the current breakpoint number.
294 * ====================================================================== */
295 int do_bedbug_step (cmd_tbl_t
* cmdtp
, int flag
, int argc
, char *argv
[])
297 unsigned long addr
; /* Address to stop at */
299 /* -------------------------------------------------- */
301 if (!bug_ctx
.stopped
) {
302 printf ("Not at a breakpoint\n");
306 if (!find_next_address ((unsigned char *) &addr
, FALSE
, bug_ctx
.regs
))
310 (*bug_ctx
.set
) (bug_ctx
.current_bp
, addr
);
314 } /* do_bedbug_step */
316 U_BOOT_CMD (step
, 1, 1, do_bedbug_step
,
317 "step - single step execution.\n",
318 " - single step execution.\n");
320 /* ======================================================================
321 * Interpreter command to continue to the next instruction, stepping over
322 * subroutines. Works by calling the find_next_addr() routine to compute
323 * the address passes control to the CPU-specific set breakpoint routine
324 * for the current breakpoint number.
325 * ====================================================================== */
326 int do_bedbug_next (cmd_tbl_t
* cmdtp
, int flag
, int argc
, char *argv
[])
328 unsigned long addr
; /* Address to stop at */
330 /* -------------------------------------------------- */
332 if (!bug_ctx
.stopped
) {
333 printf ("Not at a breakpoint\n");
337 if (!find_next_address ((unsigned char *) &addr
, TRUE
, bug_ctx
.regs
))
341 (*bug_ctx
.set
) (bug_ctx
.current_bp
, addr
);
345 } /* do_bedbug_next */
347 U_BOOT_CMD (next
, 1, 1, do_bedbug_next
,
348 "next - single step execution, stepping over subroutines.\n",
349 " - single step execution, stepping over subroutines.\n");
351 /* ======================================================================
352 * Interpreter command to print the current stack. This assumes an EABI
353 * architecture, so it starts with GPR R1 and works back up the stack.
354 * ====================================================================== */
355 int do_bedbug_stack (cmd_tbl_t
* cmdtp
, int flag
, int argc
, char *argv
[])
357 unsigned long sp
; /* Stack pointer */
358 unsigned long func
; /* LR from stack */
359 int depth
; /* Stack iteration level */
360 int skip
= 1; /* Flag to skip the first entry */
361 unsigned long top
; /* Top of memory address */
363 /* -------------------------------------------------- */
365 if (!bug_ctx
.stopped
) {
366 printf ("Not at a breakpoint\n");
370 top
= gd
->bd
->bi_memstart
+ gd
->bd
->bi_memsize
;
373 printf ("Depth PC\n");
374 printf ("----- --------\n");
375 printf ("%5d %08lx\n", depth
++, bug_ctx
.regs
->nip
);
377 sp
= bug_ctx
.regs
->gpr
[1];
378 func
= *(unsigned long *) (sp
+ 4);
380 while ((func
< top
) && (sp
< top
)) {
382 printf ("%5d %08lx\n", depth
++, func
);
386 sp
= *(unsigned long *) sp
;
387 func
= *(unsigned long *) (sp
+ 4);
390 } /* do_bedbug_stack */
392 U_BOOT_CMD (where
, 1, 1, do_bedbug_stack
,
393 "where - Print the running stack.\n",
394 " - Print the running stack.\n");
396 /* ======================================================================
397 * Interpreter command to dump the registers. Calls the CPU-specific
398 * show registers routine.
399 * ====================================================================== */
400 int do_bedbug_rdump (cmd_tbl_t
* cmdtp
, int flag
, int argc
, char *argv
[])
402 /* -------------------------------------------------- */
404 if (!bug_ctx
.stopped
) {
405 printf ("Not at a breakpoint\n");
409 show_regs (bug_ctx
.regs
);
411 } /* do_bedbug_rdump */
413 U_BOOT_CMD (rdump
, 1, 1, do_bedbug_rdump
,
414 "rdump - Show registers.\n", " - Show registers.\n");
415 /* ====================================================================== */
416 #endif /* CFG_CMD_BEDBUG */
420 * Copyright (c) 2001 William L. Pitts
421 * All rights reserved.
423 * Redistribution and use in source and binary forms are freely
424 * permitted provided that the above copyright notice and this
425 * paragraph and the following disclaimer are duplicated in all
428 * This software is provided "AS IS" and without any express or
429 * implied warranties, including, without limitation, the implied
430 * warranties of merchantability and fitness for a particular