1 | /*
|
---|
2 | * Copyright (c) 2005 Ondrej Palkovsky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup mips32debug
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <arch/debugger.h>
|
---|
36 | #include <arch/barrier.h>
|
---|
37 | #include <memstr.h>
|
---|
38 | #include <console/kconsole.h>
|
---|
39 | #include <console/cmd.h>
|
---|
40 | #include <print.h>
|
---|
41 | #include <panic.h>
|
---|
42 | #include <arch.h>
|
---|
43 | #include <arch/cp0.h>
|
---|
44 | #include <func.h>
|
---|
45 | #include <symtab.h>
|
---|
46 |
|
---|
47 | bpinfo_t breakpoints[BKPOINTS_MAX];
|
---|
48 | IRQ_SPINLOCK_STATIC_INITIALIZE(bkpoint_lock);
|
---|
49 |
|
---|
50 | #ifdef CONFIG_KCONSOLE
|
---|
51 |
|
---|
52 | static int cmd_print_breakpoints(cmd_arg_t *);
|
---|
53 | static int cmd_del_breakpoint(cmd_arg_t *);
|
---|
54 | static int cmd_add_breakpoint(cmd_arg_t *);
|
---|
55 |
|
---|
56 | static cmd_info_t bkpts_info = {
|
---|
57 | .name = "bkpts",
|
---|
58 | .description = "Print breakpoint table.",
|
---|
59 | .func = cmd_print_breakpoints,
|
---|
60 | .argc = 0,
|
---|
61 | };
|
---|
62 |
|
---|
63 | static cmd_arg_t del_argv = {
|
---|
64 | .type = ARG_TYPE_INT
|
---|
65 | };
|
---|
66 |
|
---|
67 | static cmd_info_t delbkpt_info = {
|
---|
68 | .name = "delbkpt",
|
---|
69 | .description = "Delete breakpoint.",
|
---|
70 | .func = cmd_del_breakpoint,
|
---|
71 | .argc = 1,
|
---|
72 | .argv = &del_argv
|
---|
73 | };
|
---|
74 |
|
---|
75 | static cmd_arg_t add_argv = {
|
---|
76 | .type = ARG_TYPE_INT
|
---|
77 | };
|
---|
78 |
|
---|
79 | static cmd_info_t addbkpt_info = {
|
---|
80 | .name = "addbkpt",
|
---|
81 | .description = "Add bkpoint (break on J/Branch insts unsupported).",
|
---|
82 | .func = cmd_add_breakpoint,
|
---|
83 | .argc = 1,
|
---|
84 | .argv = &add_argv
|
---|
85 | };
|
---|
86 |
|
---|
87 | static cmd_arg_t adde_argv[] = {
|
---|
88 | { .type = ARG_TYPE_INT },
|
---|
89 | { .type = ARG_TYPE_INT }
|
---|
90 | };
|
---|
91 | static cmd_info_t addbkpte_info = {
|
---|
92 | .name = "addbkpte",
|
---|
93 | .description = "Add bkpoint with a trigger function.",
|
---|
94 | .func = cmd_add_breakpoint,
|
---|
95 | .argc = 2,
|
---|
96 | .argv = adde_argv
|
---|
97 | };
|
---|
98 |
|
---|
99 | static struct {
|
---|
100 | uint32_t andmask;
|
---|
101 | uint32_t value;
|
---|
102 | } jmpinstr[] = {
|
---|
103 | {0xf3ff0000, 0x41000000}, /* BCzF */
|
---|
104 | {0xf3ff0000, 0x41020000}, /* BCzFL */
|
---|
105 | {0xf3ff0000, 0x41010000}, /* BCzT */
|
---|
106 | {0xf3ff0000, 0x41030000}, /* BCzTL */
|
---|
107 | {0xfc000000, 0x10000000}, /* BEQ */
|
---|
108 | {0xfc000000, 0x50000000}, /* BEQL */
|
---|
109 | {0xfc1f0000, 0x04010000}, /* BEQL */
|
---|
110 | {0xfc1f0000, 0x04110000}, /* BGEZAL */
|
---|
111 | {0xfc1f0000, 0x04130000}, /* BGEZALL */
|
---|
112 | {0xfc1f0000, 0x04030000}, /* BGEZL */
|
---|
113 | {0xfc1f0000, 0x1c000000}, /* BGTZ */
|
---|
114 | {0xfc1f0000, 0x5c000000}, /* BGTZL */
|
---|
115 | {0xfc1f0000, 0x18000000}, /* BLEZ */
|
---|
116 | {0xfc1f0000, 0x58000000}, /* BLEZL */
|
---|
117 | {0xfc1f0000, 0x04000000}, /* BLTZ */
|
---|
118 | {0xfc1f0000, 0x04100000}, /* BLTZAL */
|
---|
119 | {0xfc1f0000, 0x04120000}, /* BLTZALL */
|
---|
120 | {0xfc1f0000, 0x04020000}, /* BLTZL */
|
---|
121 | {0xfc000000, 0x14000000}, /* BNE */
|
---|
122 | {0xfc000000, 0x54000000}, /* BNEL */
|
---|
123 | {0xfc000000, 0x08000000}, /* J */
|
---|
124 | {0xfc000000, 0x0c000000}, /* JAL */
|
---|
125 | {0xfc1f07ff, 0x00000009}, /* JALR */
|
---|
126 | {0, 0} /* end of table */
|
---|
127 | };
|
---|
128 |
|
---|
129 | /** Test, if the given instruction is a jump or branch instruction
|
---|
130 | *
|
---|
131 | * @param instr Instruction code
|
---|
132 | *
|
---|
133 | * @return true if it is jump instruction, false otherwise
|
---|
134 | *
|
---|
135 | */
|
---|
136 | static bool is_jump(unative_t instr)
|
---|
137 | {
|
---|
138 | unsigned int i;
|
---|
139 |
|
---|
140 | for (i = 0; jmpinstr[i].andmask; i++) {
|
---|
141 | if ((instr & jmpinstr[i].andmask) == jmpinstr[i].value)
|
---|
142 | return true;
|
---|
143 | }
|
---|
144 |
|
---|
145 | return false;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /** Add new breakpoint to table
|
---|
149 | *
|
---|
150 | */
|
---|
151 | int cmd_add_breakpoint(cmd_arg_t *argv)
|
---|
152 | {
|
---|
153 | if (argv->intval & 0x3) {
|
---|
154 | printf("Not aligned instruction, forgot to use &symbol?\n");
|
---|
155 | return 1;
|
---|
156 | }
|
---|
157 |
|
---|
158 | irq_spinlock_lock(&bkpoint_lock, true);
|
---|
159 |
|
---|
160 | /* Check, that the breakpoints do not conflict */
|
---|
161 | unsigned int i;
|
---|
162 | for (i = 0; i < BKPOINTS_MAX; i++) {
|
---|
163 | if (breakpoints[i].address == (uintptr_t) argv->intval) {
|
---|
164 | printf("Duplicate breakpoint %d.\n", i);
|
---|
165 | irq_spinlock_unlock(&bkpoint_lock, true);
|
---|
166 | return 0;
|
---|
167 | } else if ((breakpoints[i].address == (uintptr_t) argv->intval +
|
---|
168 | sizeof(unative_t)) || (breakpoints[i].address ==
|
---|
169 | (uintptr_t) argv->intval - sizeof(unative_t))) {
|
---|
170 | printf("Adjacent breakpoints not supported, conflict "
|
---|
171 | "with %d.\n", i);
|
---|
172 | irq_spinlock_unlock(&bkpoint_lock, true);
|
---|
173 | return 0;
|
---|
174 | }
|
---|
175 |
|
---|
176 | }
|
---|
177 |
|
---|
178 | bpinfo_t *cur = NULL;
|
---|
179 |
|
---|
180 | for (i = 0; i < BKPOINTS_MAX; i++) {
|
---|
181 | if (!breakpoints[i].address) {
|
---|
182 | cur = &breakpoints[i];
|
---|
183 | break;
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | if (!cur) {
|
---|
188 | printf("Too many breakpoints.\n");
|
---|
189 | irq_spinlock_unlock(&bkpoint_lock, true);
|
---|
190 | return 0;
|
---|
191 | }
|
---|
192 |
|
---|
193 | printf("Adding breakpoint on address %p\n", argv->intval);
|
---|
194 |
|
---|
195 | cur->address = (uintptr_t) argv->intval;
|
---|
196 | cur->instruction = ((unative_t *) cur->address)[0];
|
---|
197 | cur->nextinstruction = ((unative_t *) cur->address)[1];
|
---|
198 | if (argv == &add_argv) {
|
---|
199 | cur->flags = 0;
|
---|
200 | } else { /* We are add extended */
|
---|
201 | cur->flags = BKPOINT_FUNCCALL;
|
---|
202 | cur->bkfunc = (void (*)(void *, istate_t *)) argv[1].intval;
|
---|
203 | }
|
---|
204 |
|
---|
205 | if (is_jump(cur->instruction))
|
---|
206 | cur->flags |= BKPOINT_ONESHOT;
|
---|
207 |
|
---|
208 | cur->counter = 0;
|
---|
209 |
|
---|
210 | /* Set breakpoint */
|
---|
211 | *((unative_t *) cur->address) = 0x0d;
|
---|
212 | smc_coherence(cur->address);
|
---|
213 |
|
---|
214 | irq_spinlock_unlock(&bkpoint_lock, true);
|
---|
215 |
|
---|
216 | return 1;
|
---|
217 | }
|
---|
218 |
|
---|
219 | /** Remove breakpoint from table
|
---|
220 | *
|
---|
221 | */
|
---|
222 | int cmd_del_breakpoint(cmd_arg_t *argv)
|
---|
223 | {
|
---|
224 | if (argv->intval > BKPOINTS_MAX) {
|
---|
225 | printf("Invalid breakpoint number.\n");
|
---|
226 | return 0;
|
---|
227 | }
|
---|
228 |
|
---|
229 | irq_spinlock_lock(&bkpoint_lock, true);
|
---|
230 |
|
---|
231 | bpinfo_t *cur = &breakpoints[argv->intval];
|
---|
232 | if (!cur->address) {
|
---|
233 | printf("Breakpoint does not exist.\n");
|
---|
234 | irq_spinlock_unlock(&bkpoint_lock, true);
|
---|
235 | return 0;
|
---|
236 | }
|
---|
237 |
|
---|
238 | if ((cur->flags & BKPOINT_INPROG) && (cur->flags & BKPOINT_ONESHOT)) {
|
---|
239 | printf("Cannot remove one-shot breakpoint in-progress\n");
|
---|
240 | irq_spinlock_unlock(&bkpoint_lock, true);
|
---|
241 | return 0;
|
---|
242 | }
|
---|
243 |
|
---|
244 | ((uint32_t *) cur->address)[0] = cur->instruction;
|
---|
245 | smc_coherence(((uint32_t *) cur->address)[0]);
|
---|
246 | ((uint32_t *) cur->address)[1] = cur->nextinstruction;
|
---|
247 | smc_coherence(((uint32_t *) cur->address)[1]);
|
---|
248 |
|
---|
249 | cur->address = NULL;
|
---|
250 |
|
---|
251 | irq_spinlock_unlock(&bkpoint_lock, true);
|
---|
252 | return 1;
|
---|
253 | }
|
---|
254 |
|
---|
255 | /** Print table of active breakpoints
|
---|
256 | *
|
---|
257 | */
|
---|
258 | int cmd_print_breakpoints(cmd_arg_t *argv)
|
---|
259 | {
|
---|
260 | unsigned int i;
|
---|
261 |
|
---|
262 | printf("# Count Address INPROG ONESHOT FUNCCALL In symbol\n");
|
---|
263 | printf("-- ----- ---------- ------ ------- -------- ---------\n");
|
---|
264 |
|
---|
265 | for (i = 0; i < BKPOINTS_MAX; i++) {
|
---|
266 | if (breakpoints[i].address) {
|
---|
267 | const char *symbol = symtab_fmt_name_lookup(
|
---|
268 | breakpoints[i].address);
|
---|
269 |
|
---|
270 | printf("%-2u %-5d %#10zx %-6s %-7s %-8s %s\n", i,
|
---|
271 | breakpoints[i].counter, breakpoints[i].address,
|
---|
272 | ((breakpoints[i].flags & BKPOINT_INPROG) ? "true" :
|
---|
273 | "false"), ((breakpoints[i].flags & BKPOINT_ONESHOT)
|
---|
274 | ? "true" : "false"), ((breakpoints[i].flags &
|
---|
275 | BKPOINT_FUNCCALL) ? "true" : "false"), symbol);
|
---|
276 | }
|
---|
277 | }
|
---|
278 |
|
---|
279 | return 1;
|
---|
280 | }
|
---|
281 |
|
---|
282 | #endif /* CONFIG_KCONSOLE */
|
---|
283 |
|
---|
284 | /** Initialize debugger
|
---|
285 | *
|
---|
286 | */
|
---|
287 | void debugger_init()
|
---|
288 | {
|
---|
289 | unsigned int i;
|
---|
290 |
|
---|
291 | for (i = 0; i < BKPOINTS_MAX; i++)
|
---|
292 | breakpoints[i].address = NULL;
|
---|
293 |
|
---|
294 | #ifdef CONFIG_KCONSOLE
|
---|
295 | cmd_initialize(&bkpts_info);
|
---|
296 | if (!cmd_register(&bkpts_info))
|
---|
297 | printf("Cannot register command %s\n", bkpts_info.name);
|
---|
298 |
|
---|
299 | cmd_initialize(&delbkpt_info);
|
---|
300 | if (!cmd_register(&delbkpt_info))
|
---|
301 | printf("Cannot register command %s\n", delbkpt_info.name);
|
---|
302 |
|
---|
303 | cmd_initialize(&addbkpt_info);
|
---|
304 | if (!cmd_register(&addbkpt_info))
|
---|
305 | printf("Cannot register command %s\n", addbkpt_info.name);
|
---|
306 |
|
---|
307 | cmd_initialize(&addbkpte_info);
|
---|
308 | if (!cmd_register(&addbkpte_info))
|
---|
309 | printf("Cannot register command %s\n", addbkpte_info.name);
|
---|
310 | #endif /* CONFIG_KCONSOLE */
|
---|
311 | }
|
---|
312 |
|
---|
313 | /** Handle breakpoint
|
---|
314 | *
|
---|
315 | * Find breakpoint in breakpoint table.
|
---|
316 | * If found, call kconsole, set break on next instruction and reexecute.
|
---|
317 | * If we are on "next instruction", set it back on the first and reexecute.
|
---|
318 | * If breakpoint not found in breakpoint table, call kconsole and start
|
---|
319 | * next instruction.
|
---|
320 | *
|
---|
321 | */
|
---|
322 | void debugger_bpoint(istate_t *istate)
|
---|
323 | {
|
---|
324 | /* test branch delay slot */
|
---|
325 | if (cp0_cause_read() & 0x80000000)
|
---|
326 | panic("Breakpoint in branch delay slot not supported.");
|
---|
327 |
|
---|
328 | irq_spinlock_lock(&bkpoint_lock, false);
|
---|
329 |
|
---|
330 | bpinfo_t *cur = NULL;
|
---|
331 | uintptr_t fireaddr = istate->epc;
|
---|
332 | unsigned int i;
|
---|
333 |
|
---|
334 | for (i = 0; i < BKPOINTS_MAX; i++) {
|
---|
335 | /* Normal breakpoint */
|
---|
336 | if ((fireaddr == breakpoints[i].address) &&
|
---|
337 | (!(breakpoints[i].flags & BKPOINT_REINST))) {
|
---|
338 | cur = &breakpoints[i];
|
---|
339 | break;
|
---|
340 | }
|
---|
341 |
|
---|
342 | /* Reinst only breakpoint */
|
---|
343 | if ((breakpoints[i].flags & BKPOINT_REINST) &&
|
---|
344 | (fireaddr == breakpoints[i].address + sizeof(unative_t))) {
|
---|
345 | cur = &breakpoints[i];
|
---|
346 | break;
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | if (cur) {
|
---|
351 | if (cur->flags & BKPOINT_REINST) {
|
---|
352 | /* Set breakpoint on first instruction */
|
---|
353 | ((uint32_t *) cur->address)[0] = 0x0d;
|
---|
354 | smc_coherence(((uint32_t *)cur->address)[0]);
|
---|
355 |
|
---|
356 | /* Return back the second */
|
---|
357 | ((uint32_t *) cur->address)[1] = cur->nextinstruction;
|
---|
358 | smc_coherence(((uint32_t *) cur->address)[1]);
|
---|
359 |
|
---|
360 | cur->flags &= ~BKPOINT_REINST;
|
---|
361 | irq_spinlock_unlock(&bkpoint_lock, false);
|
---|
362 | return;
|
---|
363 | }
|
---|
364 |
|
---|
365 | if (cur->flags & BKPOINT_INPROG)
|
---|
366 | printf("Warning: breakpoint recursion\n");
|
---|
367 |
|
---|
368 | if (!(cur->flags & BKPOINT_FUNCCALL)) {
|
---|
369 | printf("***Breakpoint %u: %p in %s.\n", i, fireaddr,
|
---|
370 | symtab_fmt_name_lookup(fireaddr));
|
---|
371 | }
|
---|
372 |
|
---|
373 | /* Return first instruction back */
|
---|
374 | ((uint32_t *)cur->address)[0] = cur->instruction;
|
---|
375 | smc_coherence(cur->address);
|
---|
376 |
|
---|
377 | if (! (cur->flags & BKPOINT_ONESHOT)) {
|
---|
378 | /* Set Breakpoint on next instruction */
|
---|
379 | ((uint32_t *)cur->address)[1] = 0x0d;
|
---|
380 | cur->flags |= BKPOINT_REINST;
|
---|
381 | }
|
---|
382 | cur->flags |= BKPOINT_INPROG;
|
---|
383 | } else {
|
---|
384 | printf("***Breakpoint %d: %p in %s.\n", i, fireaddr,
|
---|
385 | symtab_fmt_name_lookup(fireaddr));
|
---|
386 |
|
---|
387 | /* Move on to next instruction */
|
---|
388 | istate->epc += 4;
|
---|
389 | }
|
---|
390 |
|
---|
391 | if (cur)
|
---|
392 | cur->counter++;
|
---|
393 |
|
---|
394 | if (cur && (cur->flags & BKPOINT_FUNCCALL)) {
|
---|
395 | /* Allow zero bkfunc, just for counting */
|
---|
396 | if (cur->bkfunc)
|
---|
397 | cur->bkfunc(cur, istate);
|
---|
398 | } else {
|
---|
399 | #ifdef CONFIG_KCONSOLE
|
---|
400 | /*
|
---|
401 | * This disables all other processors - we are not SMP,
|
---|
402 | * actually this gets us to cpu_halt, if scheduler() is run
|
---|
403 | * - we generally do not want scheduler to be run from debug,
|
---|
404 | * so this is a good idea
|
---|
405 | */
|
---|
406 | atomic_set(&haltstate, 1);
|
---|
407 | irq_spinlock_unlock(&bkpoint_lock, false);
|
---|
408 |
|
---|
409 | kconsole("debug", "Debug console ready.\n", false);
|
---|
410 |
|
---|
411 | irq_spinlock_lock(&bkpoint_lock, false);
|
---|
412 | atomic_set(&haltstate, 0);
|
---|
413 | #endif
|
---|
414 | }
|
---|
415 |
|
---|
416 | if ((cur) && (cur->address == fireaddr)
|
---|
417 | && ((cur->flags & BKPOINT_INPROG))) {
|
---|
418 | /* Remove one-shot breakpoint */
|
---|
419 | if ((cur->flags & BKPOINT_ONESHOT))
|
---|
420 | cur->address = NULL;
|
---|
421 |
|
---|
422 | /* Remove in-progress flag */
|
---|
423 | cur->flags &= ~BKPOINT_INPROG;
|
---|
424 | }
|
---|
425 |
|
---|
426 | irq_spinlock_unlock(&bkpoint_lock, false);
|
---|
427 | }
|
---|
428 |
|
---|
429 | /** @}
|
---|
430 | */
|
---|