blob: 75b74d50b0690887c4abf4eb569208b4dd95b114 [file] [log] [blame]
wdenk47d1a6e2002-11-03 00:01:44 +00001/*
2 * BedBug Functions
3 */
4
5#include <common.h>
6#include <command.h>
7#include <linux/ctype.h>
8#include <net.h>
9
10#include <cmd_bedbug.h>
11#include <bedbug/bedbug.h>
12#include <bedbug/regs.h>
13#include <bedbug/ppc.h>
14#include <elf.h>
15
16#if (CONFIG_COMMANDS & CFG_CMD_BEDBUG)
17
18#ifndef MAX
19#define MAX(a,b) ((a) > (b) ? (a) : (b))
20#endif
21
22extern void show_regs __P((struct pt_regs*));
23extern int run_command __P((const char*, int));
24extern char console_buffer[];
25
26ulong dis_last_addr = 0; /* Last address disassembled */
27ulong dis_last_len = 20; /* Default disassembler length */
28CPU_DEBUG_CTX bug_ctx; /* Bedbug context structure */
29
30
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 * ====================================================================== */
35
36int bedbug_puts(const char *str)
37{
38 /* -------------------------------------------------- */
39
40 printf( "%s\r\n", str );
41 return 0;
42} /* bedbug_puts */
43
44
45
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
49 * settings.
50 * ====================================================================== */
51
52void bedbug_init( void )
53{
54 /* -------------------------------------------------- */
55
56#if defined(CONFIG_4xx)
57 void bedbug405_init( void );
58 bedbug405_init();
59#elif defined(CONFIG_MPC860)
60 void bedbug860_init( void );
61 bedbug860_init();
62#endif
63
64#if defined(CONFIG_MPC824X) || defined(CONFIG_MPC8260)
65 /* Processors that are 603e core based */
66 void bedbug603e_init( void );
67
68 bedbug603e_init();
69#endif
70
71 return;
72} /* bedbug_init */
73
74
75
76/* ======================================================================
77 * Entry point from the interpreter to the disassembler. Repeated calls
78 * will resume from the last disassembled address.
79 * ====================================================================== */
80int do_bedbug_dis (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
81{
82 ulong addr; /* Address to start disassembly from */
83 ulong len; /* # of instructions to disassemble */
84 /* -------------------------------------------------- */
85
86 /* Setup to go from the last address if none is given */
87 addr = dis_last_addr;
88 len = dis_last_len;
89
90 if (argc < 2)
91 {
92 printf ("Usage:\n%s\n", cmdtp->usage);
93 return 1;
94 }
95
96 if(( flag & CMD_FLAG_REPEAT ) == 0 )
97 {
98 /* New command */
99 addr = simple_strtoul( argv[1], NULL, 16 );
100
101 /* If an extra param is given then it is the length */
102 if( argc > 2 )
103 len = simple_strtoul( argv[2], NULL, 16 );
104 }
105
106 /* Run the disassembler */
107 disppc( (unsigned char *)addr, 0, len, bedbug_puts, F_RADHEX );
108
109 dis_last_addr = addr + (len * 4);
110 dis_last_len = len;
111 return 0;
112} /* do_bedbug_dis */
113
114
115
116/* ======================================================================
117 * Entry point from the interpreter to the assembler. Assembles
118 * instructions in consecutive memory locations until a '.' (period) is
119 * entered on a line by itself.
120 * ====================================================================== */
121int do_bedbug_asm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
122{
123 long mem_addr; /* Address to assemble into */
124 unsigned long instr; /* Machine code for text */
125 char prompt[ 15 ]; /* Prompt string for user input */
126 int asm_err; /* Error code from the assembler*/
127 /* -------------------------------------------------- */
128 int rcode = 0;
129
130 if (argc < 2)
131 {
132 printf ("Usage:\n%s\n", cmdtp->usage);
133 return 1;
134 }
135
136 printf( "\nEnter '.' when done\n" );
137 mem_addr = simple_strtoul( argv[ 1 ], NULL, 16 );
138
139 while( 1 )
140 {
141 putc( '\n' );
142 disppc( (unsigned char *)mem_addr, 0, 1, bedbug_puts, F_RADHEX );
143
144 sprintf( prompt, "%08lx: ", mem_addr );
145 readline( prompt );
146
147 if( console_buffer[ 0 ] && strcmp( console_buffer, "." ))
148 {
149 if(( instr = asmppc( mem_addr, console_buffer, &asm_err )) != 0 )
150 {
151 *(unsigned long *)mem_addr = instr;
152 mem_addr += 4;
153 }
154 else
155 {
156 printf( "*** Error: %s ***\n", asm_error_str( asm_err ));
157 rcode = 1;
158 }
159 }
160 else
161 {
162 break;
163 }
164 }
165 return rcode;
166} /* do_bedbug_asm */
167
168
169
170/* ======================================================================
171 * Used to set a break point from the interpreter. Simply calls into the
172 * CPU-specific break point set routine.
173 * ====================================================================== */
174
175int do_bedbug_break (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
176{
177 /* -------------------------------------------------- */
178 if( bug_ctx.do_break )
179 (*bug_ctx.do_break)( cmdtp, flag, argc, argv );
180 return 0;
181
182} /* do_bedbug_break */
183
184
185
186/* ======================================================================
187 * Called from the debug interrupt routine. Simply calls the CPU-specific
188 * breakpoint handling routine.
189 * ====================================================================== */
190
191void do_bedbug_breakpoint (struct pt_regs *regs)
192{
193 /* -------------------------------------------------- */
194
195 if( bug_ctx.break_isr )
196 (*bug_ctx.break_isr)( regs );
197
198 return;
199} /* do_bedbug_breakpoint */
200
201
202
203/* ======================================================================
204 * Called from the CPU-specific breakpoint handling routine. Enter a
205 * mini main loop until the stopped flag is cleared from the breakpoint
206 * context.
207 *
208 * This handles the parts of the debugger that are common to all CPU's.
209 * ====================================================================== */
210
211void bedbug_main_loop( unsigned long addr, struct pt_regs *regs )
212{
213 int len; /* Length of command line */
214 int flag; /* Command flags */
215 int rc = 0; /* Result from run_command*/
216 char prompt_str[ 20 ]; /* Prompt string */
217 static char lastcommand[ CFG_CBSIZE ] = {0}; /* previous command */
218 /* -------------------------------------------------- */
219
220 if( bug_ctx.clear )
221 (*bug_ctx.clear)( bug_ctx.current_bp );
222
223 printf( "Breakpoint %d: ", bug_ctx.current_bp );
224 disppc( (unsigned char *)addr, 0, 1, bedbug_puts, F_RADHEX );
225
226 bug_ctx.stopped = 1;
227 bug_ctx.regs = regs;
228
229 sprintf( prompt_str, "BEDBUG.%d =>", bug_ctx.current_bp );
230
231 /* A miniature main loop */
232 while( bug_ctx.stopped )
233 {
234 len = readline( prompt_str );
235
236 flag = 0; /* assume no special flags for now */
237
238 if (len > 0)
239 strcpy( lastcommand, console_buffer );
240 else if( len == 0 )
241 flag |= CMD_FLAG_REPEAT;
242
243 if (len == -1)
244 printf ("<INTERRUPT>\n");
245 else
246 rc = run_command( lastcommand, flag );
247
248 if (rc <= 0) {
249 /* invalid command or not repeatable, forget it */
250 lastcommand[0] = 0;
251 }
252 }
253
254 bug_ctx.regs = NULL;
255 bug_ctx.current_bp = 0;
256
257 return;
258} /* bedbug_main_loop */
259
260
261
262/* ======================================================================
263 * Interpreter command to continue from a breakpoint. Just clears the
264 * stopped flag in the context so that the breakpoint routine will
265 * return.
266 * ====================================================================== */
267int do_bedbug_continue (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
268
269{
270 /* -------------------------------------------------- */
271
272 if( ! bug_ctx.stopped )
273 {
274 printf( "Not at a breakpoint\n" );
275 return 1;
276 }
277
278 bug_ctx.stopped = 0;
279 return 0;
280} /* do_bedbug_continue */
281
282
283
284/* ======================================================================
285 * Interpreter command to continue to the next instruction, stepping into
286 * subroutines. Works by calling the find_next_addr() routine to compute
287 * the address passes control to the CPU-specific set breakpoint routine
288 * for the current breakpoint number.
289 * ====================================================================== */
290int do_bedbug_step (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
291{
292 unsigned long addr; /* Address to stop at */
293 /* -------------------------------------------------- */
294
295 if( ! bug_ctx.stopped )
296 {
297 printf( "Not at a breakpoint\n" );
298 return 1;
299 }
300
301 if( !find_next_address( (unsigned char *)&addr, FALSE, bug_ctx.regs ))
302 return 1;
303
304 if( bug_ctx.set )
305 (*bug_ctx.set)( bug_ctx.current_bp, addr );
306
307 bug_ctx.stopped = 0;
308 return 0;
309} /* do_bedbug_step */
310
311
312
313/* ======================================================================
314 * Interpreter command to continue to the next instruction, stepping over
315 * subroutines. Works by calling the find_next_addr() routine to compute
316 * the address passes control to the CPU-specific set breakpoint routine
317 * for the current breakpoint number.
318 * ====================================================================== */
319int do_bedbug_next (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
320{
321 unsigned long addr; /* Address to stop at */
322 /* -------------------------------------------------- */
323
324 if( ! bug_ctx.stopped )
325 {
326 printf( "Not at a breakpoint\n" );
327 return 1;
328 }
329
330 if( !find_next_address( (unsigned char *)&addr, TRUE, bug_ctx.regs ))
331 return 1;
332
333 if( bug_ctx.set )
334 (*bug_ctx.set)( bug_ctx.current_bp, addr );
335
336 bug_ctx.stopped = 0;
337 return 0;
338} /* do_bedbug_next */
339
340
341
342/* ======================================================================
343 * Interpreter command to print the current stack. This assumes an EABI
344 * architecture, so it starts with GPR R1 and works back up the stack.
345 * ====================================================================== */
346int do_bedbug_stack (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
347{
348 DECLARE_GLOBAL_DATA_PTR;
349
350 unsigned long sp; /* Stack pointer */
351 unsigned long func; /* LR from stack */
352 int depth; /* Stack iteration level */
353 int skip = 1; /* Flag to skip the first entry */
354 unsigned long top; /* Top of memory address */
355 /* -------------------------------------------------- */
356
357 if( ! bug_ctx.stopped )
358 {
359 printf( "Not at a breakpoint\n" );
360 return 1;
361 }
362
363 top = gd->bd->bi_memstart + gd->bd->bi_memsize;
364 depth = 0;
365
366 printf( "Depth PC\n" );
367 printf( "----- --------\n" );
368 printf( "%5d %08lx\n", depth++, bug_ctx.regs->nip );
369
370 sp = bug_ctx.regs->gpr[ 1 ];
371 func = *(unsigned long *)(sp+4);
372
373 while(( func < top ) && ( sp < top ))
374 {
375 if( !skip )
376 printf( "%5d %08lx\n", depth++, func );
377 else
378 --skip;
379
380 sp = *(unsigned long *)sp;
381 func = *(unsigned long *)(sp+4);
382 }
383 return 0;
384} /* do_bedbug_stack */
385
386
387
388/* ======================================================================
389 * Interpreter command to dump the registers. Calls the CPU-specific
390 * show registers routine.
391 * ====================================================================== */
392int do_bedbug_rdump (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
393{
394 /* -------------------------------------------------- */
395
396 if( ! bug_ctx.stopped )
397 {
398 printf( "Not at a breakpoint\n" );
399 return 1;
400 }
401
402 show_regs( bug_ctx.regs );
403 return 0;
404} /* do_bedbug_rdump */
405
406
407/* ====================================================================== */
408#endif /* CFG_CMD_BEDBUG */
409
410
411/*
412 * Copyright (c) 2001 William L. Pitts
413 * All rights reserved.
414 *
415 * Redistribution and use in source and binary forms are freely
416 * permitted provided that the above copyright notice and this
417 * paragraph and the following disclaimer are duplicated in all
418 * such forms.
419 *
420 * This software is provided "AS IS" and without any express or
421 * implied warranties, including, without limitation, the implied
422 * warranties of merchantability and fitness for a particular
423 * purpose.
424 */