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 #if (CONFIG_COMMANDS & CFG_CMD_BEDBUG)
17 #define MAX(a,b) ((a) > (b) ? (a) : (b))
20 extern void show_regs __P((struct pt_regs*));
21 extern int run_command __P((const char*, int));
22 extern char console_buffer[];
24 ulong dis_last_addr = 0; /* Last address disassembled */
25 ulong dis_last_len = 20; /* Default disassembler length */
26 CPU_DEBUG_CTX bug_ctx; /* Bedbug context structure */
29 /* ======================================================================
30 * U-Boot's puts function does not append a newline, so the bedbug stuff
31 * will use this for the output of the dis/assembler.
32 * ====================================================================== */
34 int bedbug_puts(const char *str)
36 /* -------------------------------------------------- */
38 printf( "%s\r\n", str );
44 /* ======================================================================
45 * Initialize the bug_ctx structure used by the bedbug debugger. This is
46 * specific to the CPU since each has different debug registers and
48 * ====================================================================== */
50 void bedbug_init( void )
52 /* -------------------------------------------------- */
54 #if defined(CONFIG_4xx)
55 void bedbug405_init( void );
57 #elif defined(CONFIG_8xx)
58 void bedbug860_init( void );
62 #if defined(CONFIG_MPC824X) || defined(CONFIG_MPC8260)
63 /* Processors that are 603e core based */
64 void bedbug603e_init( void );
74 /* ======================================================================
75 * Entry point from the interpreter to the disassembler. Repeated calls
76 * will resume from the last disassembled address.
77 * ====================================================================== */
78 int do_bedbug_dis (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
80 ulong addr; /* Address to start disassembly from */
81 ulong len; /* # of instructions to disassemble */
82 /* -------------------------------------------------- */
84 /* Setup to go from the last address if none is given */
90 printf ("Usage:\n%s\n", cmdtp->usage);
94 if(( flag & CMD_FLAG_REPEAT ) == 0 )
97 addr = simple_strtoul( argv[1], NULL, 16 );
99 /* If an extra param is given then it is the length */
101 len = simple_strtoul( argv[2], NULL, 16 );
104 /* Run the disassembler */
105 disppc( (unsigned char *)addr, 0, len, bedbug_puts, F_RADHEX );
107 dis_last_addr = addr + (len * 4);
110 } /* do_bedbug_dis */
112 ds, 3, 1, do_bedbug_dis,
113 "ds - disassemble memory\n",
114 "ds <address> [# instructions]\n"
117 /* ======================================================================
118 * Entry point from the interpreter to the assembler. Assembles
119 * instructions in consecutive memory locations until a '.' (period) is
120 * entered on a line by itself.
121 * ====================================================================== */
122 int do_bedbug_asm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
124 long mem_addr; /* Address to assemble into */
125 unsigned long instr; /* Machine code for text */
126 char prompt[ 15 ]; /* Prompt string for user input */
127 int asm_err; /* Error code from the assembler*/
128 /* -------------------------------------------------- */
133 printf ("Usage:\n%s\n", cmdtp->usage);
137 printf( "\nEnter '.' when done\n" );
138 mem_addr = simple_strtoul( argv[ 1 ], NULL, 16 );
143 disppc( (unsigned char *)mem_addr, 0, 1, bedbug_puts, F_RADHEX );
145 sprintf( prompt, "%08lx: ", mem_addr );
148 if( console_buffer[ 0 ] && strcmp( console_buffer, "." ))
150 if(( instr = asmppc( mem_addr, console_buffer, &asm_err )) != 0 )
152 *(unsigned long *)mem_addr = instr;
157 printf( "*** Error: %s ***\n", asm_error_str( asm_err ));
167 } /* do_bedbug_asm */
169 as, 2, 0, do_bedbug_asm,
170 "as - assemble memory\n",
174 /* ======================================================================
175 * Used to set a break point from the interpreter. Simply calls into the
176 * CPU-specific break point set routine.
177 * ====================================================================== */
179 int do_bedbug_break (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
181 /* -------------------------------------------------- */
182 if( bug_ctx.do_break )
183 (*bug_ctx.do_break)( cmdtp, flag, argc, argv );
186 } /* do_bedbug_break */
188 break, 3, 0, do_bedbug_break,
189 "break - set or clear a breakpoint\n",
190 " - Set or clear a breakpoint\n"
191 "break <address> - Break at an address\n"
192 "break off <bp#> - Disable breakpoint.\n"
193 "break show - List breakpoints.\n"
196 /* ======================================================================
197 * Called from the debug interrupt routine. Simply calls the CPU-specific
198 * breakpoint handling routine.
199 * ====================================================================== */
201 void do_bedbug_breakpoint (struct pt_regs *regs)
203 /* -------------------------------------------------- */
205 if( bug_ctx.break_isr )
206 (*bug_ctx.break_isr)( regs );
209 } /* do_bedbug_breakpoint */
213 /* ======================================================================
214 * Called from the CPU-specific breakpoint handling routine. Enter a
215 * mini main loop until the stopped flag is cleared from the breakpoint
218 * This handles the parts of the debugger that are common to all CPU's.
219 * ====================================================================== */
221 void bedbug_main_loop( unsigned long addr, struct pt_regs *regs )
223 int len; /* Length of command line */
224 int flag; /* Command flags */
225 int rc = 0; /* Result from run_command*/
226 char prompt_str[ 20 ]; /* Prompt string */
227 static char lastcommand[ CFG_CBSIZE ] = {0}; /* previous command */
228 /* -------------------------------------------------- */
231 (*bug_ctx.clear)( bug_ctx.current_bp );
233 printf( "Breakpoint %d: ", bug_ctx.current_bp );
234 disppc( (unsigned char *)addr, 0, 1, bedbug_puts, F_RADHEX );
239 sprintf( prompt_str, "BEDBUG.%d =>", bug_ctx.current_bp );
241 /* A miniature main loop */
242 while( bug_ctx.stopped )
244 len = readline( prompt_str );
246 flag = 0; /* assume no special flags for now */
249 strcpy( lastcommand, console_buffer );
251 flag |= CMD_FLAG_REPEAT;
254 printf ("<INTERRUPT>\n");
256 rc = run_command( lastcommand, flag );
259 /* invalid command or not repeatable, forget it */
265 bug_ctx.current_bp = 0;
268 } /* bedbug_main_loop */
272 /* ======================================================================
273 * Interpreter command to continue from a breakpoint. Just clears the
274 * stopped flag in the context so that the breakpoint routine will
276 * ====================================================================== */
277 int do_bedbug_continue (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
280 /* -------------------------------------------------- */
282 if( ! bug_ctx.stopped )
284 printf( "Not at a breakpoint\n" );
290 } /* do_bedbug_continue */
292 continue, 1, 0, do_bedbug_continue,
293 "continue- continue from a breakpoint\n",
294 " - continue from a breakpoint.\n"
297 /* ======================================================================
298 * Interpreter command to continue to the next instruction, stepping into
299 * subroutines. Works by calling the find_next_addr() routine to compute
300 * the address passes control to the CPU-specific set breakpoint routine
301 * for the current breakpoint number.
302 * ====================================================================== */
303 int do_bedbug_step (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
305 unsigned long addr; /* Address to stop at */
306 /* -------------------------------------------------- */
308 if( ! bug_ctx.stopped )
310 printf( "Not at a breakpoint\n" );
314 if( !find_next_address( (unsigned char *)&addr, FALSE, bug_ctx.regs ))
318 (*bug_ctx.set)( bug_ctx.current_bp, addr );
322 } /* do_bedbug_step */
324 step, 1, 1, do_bedbug_step,
325 "step - single step execution.\n",
326 " - single step execution.\n"
329 /* ======================================================================
330 * Interpreter command to continue to the next instruction, stepping over
331 * subroutines. Works by calling the find_next_addr() routine to compute
332 * the address passes control to the CPU-specific set breakpoint routine
333 * for the current breakpoint number.
334 * ====================================================================== */
335 int do_bedbug_next (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
337 unsigned long addr; /* Address to stop at */
338 /* -------------------------------------------------- */
340 if( ! bug_ctx.stopped )
342 printf( "Not at a breakpoint\n" );
346 if( !find_next_address( (unsigned char *)&addr, TRUE, bug_ctx.regs ))
350 (*bug_ctx.set)( bug_ctx.current_bp, addr );
354 } /* do_bedbug_next */
356 next, 1, 1, do_bedbug_next,
357 "next - single step execution, stepping over subroutines.\n",
358 " - single step execution, stepping over subroutines.\n"
361 /* ======================================================================
362 * Interpreter command to print the current stack. This assumes an EABI
363 * architecture, so it starts with GPR R1 and works back up the stack.
364 * ====================================================================== */
365 int do_bedbug_stack (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
367 DECLARE_GLOBAL_DATA_PTR;
369 unsigned long sp; /* Stack pointer */
370 unsigned long func; /* LR from stack */
371 int depth; /* Stack iteration level */
372 int skip = 1; /* Flag to skip the first entry */
373 unsigned long top; /* Top of memory address */
374 /* -------------------------------------------------- */
376 if( ! bug_ctx.stopped )
378 printf( "Not at a breakpoint\n" );
382 top = gd->bd->bi_memstart + gd->bd->bi_memsize;
385 printf( "Depth PC\n" );
386 printf( "----- --------\n" );
387 printf( "%5d %08lx\n", depth++, bug_ctx.regs->nip );
389 sp = bug_ctx.regs->gpr[ 1 ];
390 func = *(unsigned long *)(sp+4);
392 while(( func < top ) && ( sp < top ))
395 printf( "%5d %08lx\n", depth++, func );
399 sp = *(unsigned long *)sp;
400 func = *(unsigned long *)(sp+4);
403 } /* do_bedbug_stack */
405 where, 1, 1, do_bedbug_stack,
406 "where - Print the running stack.\n",
407 " - Print the running stack.\n"
410 /* ======================================================================
411 * Interpreter command to dump the registers. Calls the CPU-specific
412 * show registers routine.
413 * ====================================================================== */
414 int do_bedbug_rdump (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
416 /* -------------------------------------------------- */
418 if( ! bug_ctx.stopped )
420 printf( "Not at a breakpoint\n" );
424 show_regs( bug_ctx.regs );
426 } /* do_bedbug_rdump */
428 rdump, 1, 1, do_bedbug_rdump,
429 "rdump - Show registers.\n",
430 " - Show registers.\n"
432 /* ====================================================================== */
433 #endif /* CFG_CMD_BEDBUG */
437 * Copyright (c) 2001 William L. Pitts
438 * All rights reserved.
440 * Redistribution and use in source and binary forms are freely
441 * permitted provided that the above copyright notice and this
442 * paragraph and the following disclaimer are duplicated in all
445 * This software is provided "AS IS" and without any express or
446 * implied warranties, including, without limitation, the implied
447 * warranties of merchantability and fitness for a particular