1
2
3
4
5#include <common.h>
6#include <cli.h>
7#include <command.h>
8#include <console.h>
9#include <linux/ctype.h>
10#include <net.h>
11#include <bedbug/type.h>
12#include <bedbug/bedbug.h>
13#include <bedbug/regs.h>
14#include <bedbug/ppc.h>
15
16DECLARE_GLOBAL_DATA_PTR;
17
18extern void show_regs __P ((struct pt_regs *));
19extern int run_command __P ((const char *, int));
20
21ulong dis_last_addr = 0;
22ulong dis_last_len = 20;
23CPU_DEBUG_CTX bug_ctx;
24
25
26
27
28
29
30
31int bedbug_puts (const char *str)
32{
33
34
35 printf ("%s\r\n", str);
36 return 0;
37}
38
39
40
41
42
43
44
45
46
47void bedbug_init (void)
48{
49
50 return;
51}
52
53
54
55
56
57
58
59int do_bedbug_dis (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
60{
61 ulong addr;
62 ulong len;
63
64
65
66
67 addr = dis_last_addr;
68 len = dis_last_len;
69
70 if (argc < 2)
71 return CMD_RET_USAGE;
72
73 if ((flag & CMD_FLAG_REPEAT) == 0) {
74
75 addr = simple_strtoul (argv[1], NULL, 16);
76
77
78 if (argc > 2)
79 len = simple_strtoul (argv[2], NULL, 16);
80 }
81
82
83 disppc ((unsigned char *) addr, 0, len, bedbug_puts, F_RADHEX);
84
85 dis_last_addr = addr + (len * 4);
86 dis_last_len = len;
87 return 0;
88}
89
90U_BOOT_CMD (ds, 3, 1, do_bedbug_dis,
91 "disassemble memory",
92 "ds <address> [# instructions]");
93
94
95
96
97
98
99int do_bedbug_asm (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
100{
101 long mem_addr;
102 unsigned long instr;
103 char prompt[15];
104 int asm_err;
105
106
107 int rcode = 0;
108
109 if (argc < 2)
110 return CMD_RET_USAGE;
111
112 printf ("\nEnter '.' when done\n");
113 mem_addr = simple_strtoul (argv[1], NULL, 16);
114
115 while (1) {
116 putc ('\n');
117 disppc ((unsigned char *) mem_addr, 0, 1, bedbug_puts,
118 F_RADHEX);
119
120 sprintf (prompt, "%08lx: ", mem_addr);
121 cli_readline(prompt);
122
123 if (console_buffer[0] && strcmp (console_buffer, ".")) {
124 if ((instr =
125 asmppc (mem_addr, console_buffer,
126 &asm_err)) != 0) {
127 *(unsigned long *) mem_addr = instr;
128 mem_addr += 4;
129 } else {
130 printf ("*** Error: %s ***\n",
131 asm_error_str (asm_err));
132 rcode = 1;
133 }
134 } else {
135 break;
136 }
137 }
138 return rcode;
139}
140
141U_BOOT_CMD (as, 2, 0, do_bedbug_asm,
142 "assemble memory", "as <address>");
143
144
145
146
147
148
149int do_bedbug_break (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
150{
151
152 if (bug_ctx.do_break)
153 (*bug_ctx.do_break) (cmdtp, flag, argc, argv);
154 return 0;
155
156}
157
158U_BOOT_CMD (break, 3, 0, do_bedbug_break,
159 "set or clear a breakpoint",
160 " - Set or clear a breakpoint\n"
161 "break <address> - Break at an address\n"
162 "break off <bp#> - Disable breakpoint.\n"
163 "break show - List breakpoints.");
164
165
166
167
168
169
170void do_bedbug_breakpoint (struct pt_regs *regs)
171{
172
173
174 if (bug_ctx.break_isr)
175 (*bug_ctx.break_isr) (regs);
176
177 return;
178}
179
180
181
182
183
184
185
186
187
188
189
190void bedbug_main_loop (unsigned long addr, struct pt_regs *regs)
191{
192 int len;
193 int flag;
194 int rc = 0;
195 char prompt_str[20];
196 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0 };
197
198
199 if (bug_ctx.clear)
200 (*bug_ctx.clear) (bug_ctx.current_bp);
201
202 printf ("Breakpoint %d: ", bug_ctx.current_bp);
203 disppc ((unsigned char *) addr, 0, 1, bedbug_puts, F_RADHEX);
204
205 bug_ctx.stopped = 1;
206 bug_ctx.regs = regs;
207
208 sprintf (prompt_str, "BEDBUG.%d =>", bug_ctx.current_bp);
209
210
211 while (bug_ctx.stopped) {
212 len = cli_readline(prompt_str);
213
214 flag = 0;
215
216 if (len > 0)
217 strcpy (lastcommand, console_buffer);
218 else if (len == 0)
219 flag |= CMD_FLAG_REPEAT;
220
221 if (len == -1)
222 printf ("<INTERRUPT>\n");
223 else
224 rc = run_command_repeatable(lastcommand, flag);
225
226 if (rc <= 0) {
227
228 lastcommand[0] = 0;
229 }
230 }
231
232 bug_ctx.regs = NULL;
233 bug_ctx.current_bp = 0;
234
235 return;
236}
237
238
239
240
241
242
243
244
245int do_bedbug_continue (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
246{
247
248
249 if (!bug_ctx.stopped) {
250 printf ("Not at a breakpoint\n");
251 return 1;
252 }
253
254 bug_ctx.stopped = 0;
255 return 0;
256}
257
258U_BOOT_CMD (continue, 1, 0, do_bedbug_continue,
259 "continue from a breakpoint",
260 "");
261
262
263
264
265
266
267
268int do_bedbug_step (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
269{
270 unsigned long addr;
271
272
273
274 if (!bug_ctx.stopped) {
275 printf ("Not at a breakpoint\n");
276 return 1;
277 }
278
279 if (!find_next_address((unsigned char *) &addr, false, bug_ctx.regs))
280 return 1;
281
282 if (bug_ctx.set)
283 (*bug_ctx.set) (bug_ctx.current_bp, addr);
284
285 bug_ctx.stopped = 0;
286 return 0;
287}
288
289U_BOOT_CMD (step, 1, 1, do_bedbug_step,
290 "single step execution.",
291 "");
292
293
294
295
296
297
298
299int do_bedbug_next (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
300{
301 unsigned long addr;
302
303
304
305 if (!bug_ctx.stopped) {
306 printf ("Not at a breakpoint\n");
307 return 1;
308 }
309
310 if (!find_next_address((unsigned char *) &addr, true, bug_ctx.regs))
311 return 1;
312
313 if (bug_ctx.set)
314 (*bug_ctx.set) (bug_ctx.current_bp, addr);
315
316 bug_ctx.stopped = 0;
317 return 0;
318}
319
320U_BOOT_CMD (next, 1, 1, do_bedbug_next,
321 "single step execution, stepping over subroutines.",
322 "");
323
324
325
326
327
328int do_bedbug_stack (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
329{
330 unsigned long sp;
331 unsigned long func;
332 int depth;
333 int skip = 1;
334 unsigned long top;
335
336
337
338 if (!bug_ctx.stopped) {
339 printf ("Not at a breakpoint\n");
340 return 1;
341 }
342
343 top = gd->bd->bi_memstart + gd->bd->bi_memsize;
344 depth = 0;
345
346 printf ("Depth PC\n");
347 printf ("----- --------\n");
348 printf ("%5d %08lx\n", depth++, bug_ctx.regs->nip);
349
350 sp = bug_ctx.regs->gpr[1];
351 func = *(unsigned long *) (sp + 4);
352
353 while ((func < top) && (sp < top)) {
354 if (!skip)
355 printf ("%5d %08lx\n", depth++, func);
356 else
357 --skip;
358
359 sp = *(unsigned long *) sp;
360 func = *(unsigned long *) (sp + 4);
361 }
362 return 0;
363}
364
365U_BOOT_CMD (where, 1, 1, do_bedbug_stack,
366 "Print the running stack.",
367 "");
368
369
370
371
372
373int do_bedbug_rdump (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
374{
375
376
377 if (!bug_ctx.stopped) {
378 printf ("Not at a breakpoint\n");
379 return 1;
380 }
381
382 show_regs (bug_ctx.regs);
383 return 0;
384}
385
386U_BOOT_CMD (rdump, 1, 1, do_bedbug_rdump,
387 "Show registers.", "");
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405