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 | 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", (void *) 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 = (uintptr_t) 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("[nr] [count] [address ] [inprog] [oneshot] [funccall] [in symbol\n");
|
---|
263 |
|
---|
264 | for (i = 0; i < BKPOINTS_MAX; i++) {
|
---|
265 | if (breakpoints[i].address) {
|
---|
266 | const char *symbol = symtab_fmt_name_lookup(
|
---|
267 | breakpoints[i].address);
|
---|
268 |
|
---|
269 | printf("%-4u %7zu %p %-8s %-9s %-10s %s\n", i,
|
---|
270 | breakpoints[i].counter, (void *) breakpoints[i].address,
|
---|
271 | ((breakpoints[i].flags & BKPOINT_INPROG) ? "true" :
|
---|
272 | "false"), ((breakpoints[i].flags & BKPOINT_ONESHOT)
|
---|
273 | ? "true" : "false"), ((breakpoints[i].flags &
|
---|
274 | BKPOINT_FUNCCALL) ? "true" : "false"), symbol);
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | return 1;
|
---|
279 | }
|
---|
280 |
|
---|
281 | #endif /* CONFIG_KCONSOLE */
|
---|
282 |
|
---|
283 | /** Initialize debugger
|
---|
284 | *
|
---|
285 | */
|
---|
286 | void debugger_init()
|
---|
287 | {
|
---|
288 | unsigned int i;
|
---|
289 |
|
---|
290 | for (i = 0; i < BKPOINTS_MAX; i++)
|
---|
291 | breakpoints[i].address = (uintptr_t) NULL;
|
---|
292 |
|
---|
293 | #ifdef CONFIG_KCONSOLE
|
---|
294 | cmd_initialize(&bkpts_info);
|
---|
295 | if (!cmd_register(&bkpts_info))
|
---|
296 | printf("Cannot register command %s\n", bkpts_info.name);
|
---|
297 |
|
---|
298 | cmd_initialize(&delbkpt_info);
|
---|
299 | if (!cmd_register(&delbkpt_info))
|
---|
300 | printf("Cannot register command %s\n", delbkpt_info.name);
|
---|
301 |
|
---|
302 | cmd_initialize(&addbkpt_info);
|
---|
303 | if (!cmd_register(&addbkpt_info))
|
---|
304 | printf("Cannot register command %s\n", addbkpt_info.name);
|
---|
305 |
|
---|
306 | cmd_initialize(&addbkpte_info);
|
---|
307 | if (!cmd_register(&addbkpte_info))
|
---|
308 | printf("Cannot register command %s\n", addbkpte_info.name);
|
---|
309 | #endif /* CONFIG_KCONSOLE */
|
---|
310 | }
|
---|
311 |
|
---|
312 | /** Handle breakpoint
|
---|
313 | *
|
---|
314 | * Find breakpoint in breakpoint table.
|
---|
315 | * If found, call kconsole, set break on next instruction and reexecute.
|
---|
316 | * If we are on "next instruction", set it back on the first and reexecute.
|
---|
317 | * If breakpoint not found in breakpoint table, call kconsole and start
|
---|
318 | * next instruction.
|
---|
319 | *
|
---|
320 | */
|
---|
321 | void debugger_bpoint(istate_t *istate)
|
---|
322 | {
|
---|
323 | /* test branch delay slot */
|
---|
324 | if (cp0_cause_read() & 0x80000000)
|
---|
325 | panic("Breakpoint in branch delay slot not supported.");
|
---|
326 |
|
---|
327 | irq_spinlock_lock(&bkpoint_lock, false);
|
---|
328 |
|
---|
329 | bpinfo_t *cur = NULL;
|
---|
330 | uintptr_t fireaddr = istate->epc;
|
---|
331 | unsigned int i;
|
---|
332 |
|
---|
333 | for (i = 0; i < BKPOINTS_MAX; i++) {
|
---|
334 | /* Normal breakpoint */
|
---|
335 | if ((fireaddr == breakpoints[i].address) &&
|
---|
336 | (!(breakpoints[i].flags & BKPOINT_REINST))) {
|
---|
337 | cur = &breakpoints[i];
|
---|
338 | break;
|
---|
339 | }
|
---|
340 |
|
---|
341 | /* Reinst only breakpoint */
|
---|
342 | if ((breakpoints[i].flags & BKPOINT_REINST) &&
|
---|
343 | (fireaddr == breakpoints[i].address + sizeof(unative_t))) {
|
---|
344 | cur = &breakpoints[i];
|
---|
345 | break;
|
---|
346 | }
|
---|
347 | }
|
---|
348 |
|
---|
349 | if (cur) {
|
---|
350 | if (cur->flags & BKPOINT_REINST) {
|
---|
351 | /* Set breakpoint on first instruction */
|
---|
352 | ((uint32_t *) cur->address)[0] = 0x0d;
|
---|
353 | smc_coherence(((uint32_t *)cur->address)[0]);
|
---|
354 |
|
---|
355 | /* Return back the second */
|
---|
356 | ((uint32_t *) cur->address)[1] = cur->nextinstruction;
|
---|
357 | smc_coherence(((uint32_t *) cur->address)[1]);
|
---|
358 |
|
---|
359 | cur->flags &= ~BKPOINT_REINST;
|
---|
360 | irq_spinlock_unlock(&bkpoint_lock, false);
|
---|
361 | return;
|
---|
362 | }
|
---|
363 |
|
---|
364 | if (cur->flags & BKPOINT_INPROG)
|
---|
365 | printf("Warning: breakpoint recursion\n");
|
---|
366 |
|
---|
367 | if (!(cur->flags & BKPOINT_FUNCCALL)) {
|
---|
368 | printf("***Breakpoint %u: %p in %s.\n", i,
|
---|
369 | (void *) 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,
|
---|
385 | (void *) fireaddr,
|
---|
386 | symtab_fmt_name_lookup(fireaddr));
|
---|
387 |
|
---|
388 | /* Move on to next instruction */
|
---|
389 | istate->epc += 4;
|
---|
390 | }
|
---|
391 |
|
---|
392 | if (cur)
|
---|
393 | cur->counter++;
|
---|
394 |
|
---|
395 | if (cur && (cur->flags & BKPOINT_FUNCCALL)) {
|
---|
396 | /* Allow zero bkfunc, just for counting */
|
---|
397 | if (cur->bkfunc)
|
---|
398 | cur->bkfunc(cur, istate);
|
---|
399 | } else {
|
---|
400 | #ifdef CONFIG_KCONSOLE
|
---|
401 | /*
|
---|
402 | * This disables all other processors - we are not SMP,
|
---|
403 | * actually this gets us to cpu_halt, if scheduler() is run
|
---|
404 | * - we generally do not want scheduler to be run from debug,
|
---|
405 | * so this is a good idea
|
---|
406 | */
|
---|
407 | atomic_set(&haltstate, 1);
|
---|
408 | irq_spinlock_unlock(&bkpoint_lock, false);
|
---|
409 |
|
---|
410 | kconsole("debug", "Debug console ready.\n", false);
|
---|
411 |
|
---|
412 | irq_spinlock_lock(&bkpoint_lock, false);
|
---|
413 | atomic_set(&haltstate, 0);
|
---|
414 | #endif
|
---|
415 | }
|
---|
416 |
|
---|
417 | if ((cur) && (cur->address == fireaddr)
|
---|
418 | && ((cur->flags & BKPOINT_INPROG))) {
|
---|
419 | /* Remove one-shot breakpoint */
|
---|
420 | if ((cur->flags & BKPOINT_ONESHOT))
|
---|
421 | cur->address = (uintptr_t) NULL;
|
---|
422 |
|
---|
423 | /* Remove in-progress flag */
|
---|
424 | cur->flags &= ~BKPOINT_INPROG;
|
---|
425 | }
|
---|
426 |
|
---|
427 | irq_spinlock_unlock(&bkpoint_lock, false);
|
---|
428 | }
|
---|
429 |
|
---|
430 | /** @}
|
---|
431 | */
|
---|