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