source: mainline/kernel/arch/mips32/src/debugger.c@ f93ba6d5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f93ba6d5 was 53c8d12, checked in by Vojtech Horky <vojtechhorky@…>, 11 years ago

is_jump() is needed even without kernel console

  • Property mode set to 100644
File size: 11.3 KB
Line 
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 <log.h>
42#include <panic.h>
43#include <arch.h>
44#include <arch/cp0.h>
45#include <func.h>
46#include <symtab.h>
47
48bpinfo_t breakpoints[BKPOINTS_MAX];
49IRQ_SPINLOCK_STATIC_INITIALIZE(bkpoint_lock);
50
51#ifdef CONFIG_KCONSOLE
52
53static int cmd_print_breakpoints(cmd_arg_t *);
54static int cmd_del_breakpoint(cmd_arg_t *);
55static int cmd_add_breakpoint(cmd_arg_t *);
56
57static cmd_info_t bkpts_info = {
58 .name = "bkpts",
59 .description = "Print breakpoint table.",
60 .func = cmd_print_breakpoints,
61 .argc = 0,
62};
63
64static cmd_arg_t del_argv = {
65 .type = ARG_TYPE_INT
66};
67
68static cmd_info_t delbkpt_info = {
69 .name = "delbkpt",
70 .description = "Delete breakpoint.",
71 .func = cmd_del_breakpoint,
72 .argc = 1,
73 .argv = &del_argv
74};
75
76static cmd_arg_t add_argv = {
77 .type = ARG_TYPE_INT
78};
79
80static cmd_info_t addbkpt_info = {
81 .name = "addbkpt",
82 .description = "Add bkpoint (break on j/branch insts unsupported).",
83 .func = cmd_add_breakpoint,
84 .argc = 1,
85 .argv = &add_argv
86};
87
88static cmd_arg_t adde_argv[] = {
89 { .type = ARG_TYPE_INT },
90 { .type = ARG_TYPE_INT }
91};
92static cmd_info_t addbkpte_info = {
93 .name = "addbkpte",
94 .description = "Add bkpoint with a trigger function.",
95 .func = cmd_add_breakpoint,
96 .argc = 2,
97 .argv = adde_argv
98};
99#endif
100
101static struct {
102 uint32_t andmask;
103 uint32_t value;
104} jmpinstr[] = {
105 {0xf3ff0000, 0x41000000}, /* BCzF */
106 {0xf3ff0000, 0x41020000}, /* BCzFL */
107 {0xf3ff0000, 0x41010000}, /* BCzT */
108 {0xf3ff0000, 0x41030000}, /* BCzTL */
109 {0xfc000000, 0x10000000}, /* BEQ */
110 {0xfc000000, 0x50000000}, /* BEQL */
111 {0xfc1f0000, 0x04010000}, /* BEQL */
112 {0xfc1f0000, 0x04110000}, /* BGEZAL */
113 {0xfc1f0000, 0x04130000}, /* BGEZALL */
114 {0xfc1f0000, 0x04030000}, /* BGEZL */
115 {0xfc1f0000, 0x1c000000}, /* BGTZ */
116 {0xfc1f0000, 0x5c000000}, /* BGTZL */
117 {0xfc1f0000, 0x18000000}, /* BLEZ */
118 {0xfc1f0000, 0x58000000}, /* BLEZL */
119 {0xfc1f0000, 0x04000000}, /* BLTZ */
120 {0xfc1f0000, 0x04100000}, /* BLTZAL */
121 {0xfc1f0000, 0x04120000}, /* BLTZALL */
122 {0xfc1f0000, 0x04020000}, /* BLTZL */
123 {0xfc000000, 0x14000000}, /* BNE */
124 {0xfc000000, 0x54000000}, /* BNEL */
125 {0xfc000000, 0x08000000}, /* J */
126 {0xfc000000, 0x0c000000}, /* JAL */
127 {0xfc1f07ff, 0x00000009}, /* JALR */
128 {0, 0} /* end of table */
129};
130
131/** Test, if the given instruction is a jump or branch instruction
132 *
133 * @param instr Instruction code
134 *
135 * @return true if it is jump instruction, false otherwise
136 *
137 */
138bool is_jump(sysarg_t instr)
139{
140 unsigned int i;
141
142 for (i = 0; jmpinstr[i].andmask; i++) {
143 if ((instr & jmpinstr[i].andmask) == jmpinstr[i].value)
144 return true;
145 }
146
147 return false;
148}
149
150#ifdef CONFIG_KCONSOLE
151
152/** Add new breakpoint to table
153 *
154 */
155int cmd_add_breakpoint(cmd_arg_t *argv)
156{
157 if (argv->intval & 0x3) {
158 printf("Not aligned instruction, forgot to use &symbol?\n");
159 return 1;
160 }
161
162 irq_spinlock_lock(&bkpoint_lock, true);
163
164 /* Check, that the breakpoints do not conflict */
165 unsigned int i;
166 for (i = 0; i < BKPOINTS_MAX; i++) {
167 if (breakpoints[i].address == (uintptr_t) argv->intval) {
168 printf("Duplicate breakpoint %d.\n", i);
169 irq_spinlock_unlock(&bkpoint_lock, true);
170 return 0;
171 } else if ((breakpoints[i].address == (uintptr_t) argv->intval +
172 sizeof(sysarg_t)) || (breakpoints[i].address ==
173 (uintptr_t) argv->intval - sizeof(sysarg_t))) {
174 printf("Adjacent breakpoints not supported, conflict "
175 "with %d.\n", i);
176 irq_spinlock_unlock(&bkpoint_lock, true);
177 return 0;
178 }
179
180 }
181
182 bpinfo_t *cur = NULL;
183
184 for (i = 0; i < BKPOINTS_MAX; i++) {
185 if (!breakpoints[i].address) {
186 cur = &breakpoints[i];
187 break;
188 }
189 }
190
191 if (!cur) {
192 printf("Too many breakpoints.\n");
193 irq_spinlock_unlock(&bkpoint_lock, true);
194 return 0;
195 }
196
197 printf("Adding breakpoint on address %p\n", (void *) argv->intval);
198
199 cur->address = (uintptr_t) argv->intval;
200 cur->instruction = ((sysarg_t *) cur->address)[0];
201 cur->nextinstruction = ((sysarg_t *) cur->address)[1];
202 if (argv == &add_argv) {
203 cur->flags = 0;
204 } else { /* We are add extended */
205 cur->flags = BKPOINT_FUNCCALL;
206 cur->bkfunc = (void (*)(void *, istate_t *)) argv[1].intval;
207 }
208
209 if (is_jump(cur->instruction))
210 cur->flags |= BKPOINT_ONESHOT;
211
212 cur->counter = 0;
213
214 /* Set breakpoint */
215 *((sysarg_t *) cur->address) = 0x0d;
216 smc_coherence(cur->address);
217
218 irq_spinlock_unlock(&bkpoint_lock, true);
219
220 return 1;
221}
222
223/** Remove breakpoint from table
224 *
225 */
226int cmd_del_breakpoint(cmd_arg_t *argv)
227{
228 if (argv->intval > BKPOINTS_MAX) {
229 printf("Invalid breakpoint number.\n");
230 return 0;
231 }
232
233 irq_spinlock_lock(&bkpoint_lock, true);
234
235 bpinfo_t *cur = &breakpoints[argv->intval];
236 if (!cur->address) {
237 printf("Breakpoint does not exist.\n");
238 irq_spinlock_unlock(&bkpoint_lock, true);
239 return 0;
240 }
241
242 if ((cur->flags & BKPOINT_INPROG) && (cur->flags & BKPOINT_ONESHOT)) {
243 printf("Cannot remove one-shot breakpoint in-progress\n");
244 irq_spinlock_unlock(&bkpoint_lock, true);
245 return 0;
246 }
247
248 ((uint32_t *) cur->address)[0] = cur->instruction;
249 smc_coherence(((uint32_t *) cur->address)[0]);
250 ((uint32_t *) cur->address)[1] = cur->nextinstruction;
251 smc_coherence(((uint32_t *) cur->address)[1]);
252
253 cur->address = (uintptr_t) NULL;
254
255 irq_spinlock_unlock(&bkpoint_lock, true);
256 return 1;
257}
258
259/** Print table of active breakpoints
260 *
261 */
262int cmd_print_breakpoints(cmd_arg_t *argv)
263{
264 unsigned int i;
265
266 printf("[nr] [count] [address ] [inprog] [oneshot] [funccall] [in symbol\n");
267
268 for (i = 0; i < BKPOINTS_MAX; i++) {
269 if (breakpoints[i].address) {
270 const char *symbol = symtab_fmt_name_lookup(
271 breakpoints[i].address);
272
273 printf("%-4u %7zu %p %-8s %-9s %-10s %s\n", i,
274 breakpoints[i].counter, (void *) breakpoints[i].address,
275 ((breakpoints[i].flags & BKPOINT_INPROG) ? "true" :
276 "false"), ((breakpoints[i].flags & BKPOINT_ONESHOT)
277 ? "true" : "false"), ((breakpoints[i].flags &
278 BKPOINT_FUNCCALL) ? "true" : "false"), symbol);
279 }
280 }
281
282 return 1;
283}
284
285#endif /* CONFIG_KCONSOLE */
286
287/** Initialize debugger
288 *
289 */
290void debugger_init()
291{
292 unsigned int i;
293
294 for (i = 0; i < BKPOINTS_MAX; i++)
295 breakpoints[i].address = (uintptr_t) NULL;
296
297#ifdef CONFIG_KCONSOLE
298 cmd_initialize(&bkpts_info);
299 if (!cmd_register(&bkpts_info))
300 log(LF_OTHER, LVL_WARN, "Cannot register command %s",
301 bkpts_info.name);
302
303 cmd_initialize(&delbkpt_info);
304 if (!cmd_register(&delbkpt_info))
305 log(LF_OTHER, LVL_WARN, "Cannot register command %s",
306 delbkpt_info.name);
307
308 cmd_initialize(&addbkpt_info);
309 if (!cmd_register(&addbkpt_info))
310 log(LF_OTHER, LVL_WARN, "Cannot register command %s",
311 addbkpt_info.name);
312
313 cmd_initialize(&addbkpte_info);
314 if (!cmd_register(&addbkpte_info))
315 log(LF_OTHER, LVL_WARN, "Cannot register command %s",
316 addbkpte_info.name);
317#endif /* CONFIG_KCONSOLE */
318}
319
320/** Handle breakpoint
321 *
322 * Find breakpoint in breakpoint table.
323 * If found, call kconsole, set break on next instruction and reexecute.
324 * If we are on "next instruction", set it back on the first and reexecute.
325 * If breakpoint not found in breakpoint table, call kconsole and start
326 * next instruction.
327 *
328 */
329void debugger_bpoint(istate_t *istate)
330{
331 /* test branch delay slot */
332 if (cp0_cause_read() & 0x80000000)
333 panic("Breakpoint in branch delay slot not supported.");
334
335 irq_spinlock_lock(&bkpoint_lock, false);
336
337 bpinfo_t *cur = NULL;
338 uintptr_t fireaddr = istate->epc;
339 unsigned int i;
340
341 for (i = 0; i < BKPOINTS_MAX; i++) {
342 /* Normal breakpoint */
343 if ((fireaddr == breakpoints[i].address) &&
344 (!(breakpoints[i].flags & BKPOINT_REINST))) {
345 cur = &breakpoints[i];
346 break;
347 }
348
349 /* Reinst only breakpoint */
350 if ((breakpoints[i].flags & BKPOINT_REINST) &&
351 (fireaddr == breakpoints[i].address + sizeof(sysarg_t))) {
352 cur = &breakpoints[i];
353 break;
354 }
355 }
356
357 if (cur) {
358 if (cur->flags & BKPOINT_REINST) {
359 /* Set breakpoint on first instruction */
360 ((uint32_t *) cur->address)[0] = 0x0d;
361 smc_coherence(((uint32_t *)cur->address)[0]);
362
363 /* Return back the second */
364 ((uint32_t *) cur->address)[1] = cur->nextinstruction;
365 smc_coherence(((uint32_t *) cur->address)[1]);
366
367 cur->flags &= ~BKPOINT_REINST;
368 irq_spinlock_unlock(&bkpoint_lock, false);
369 return;
370 }
371
372 if (cur->flags & BKPOINT_INPROG)
373 printf("Warning: breakpoint recursion\n");
374
375 if (!(cur->flags & BKPOINT_FUNCCALL)) {
376 printf("***Breakpoint %u: %p in %s.\n", i,
377 (void *) fireaddr,
378 symtab_fmt_name_lookup(fireaddr));
379 }
380
381 /* Return first instruction back */
382 ((uint32_t *)cur->address)[0] = cur->instruction;
383 smc_coherence(cur->address);
384
385 if (! (cur->flags & BKPOINT_ONESHOT)) {
386 /* Set Breakpoint on next instruction */
387 ((uint32_t *)cur->address)[1] = 0x0d;
388 cur->flags |= BKPOINT_REINST;
389 }
390 cur->flags |= BKPOINT_INPROG;
391 } else {
392 printf("***Breakpoint %d: %p in %s.\n", i,
393 (void *) fireaddr,
394 symtab_fmt_name_lookup(fireaddr));
395
396 /* Move on to next instruction */
397 istate->epc += 4;
398 }
399
400 if (cur)
401 cur->counter++;
402
403 if (cur && (cur->flags & BKPOINT_FUNCCALL)) {
404 /* Allow zero bkfunc, just for counting */
405 if (cur->bkfunc)
406 cur->bkfunc(cur, istate);
407 } else {
408#ifdef CONFIG_KCONSOLE
409 /*
410 * This disables all other processors - we are not SMP,
411 * actually this gets us to cpu_halt, if scheduler() is run
412 * - we generally do not want scheduler to be run from debug,
413 * so this is a good idea
414 */
415 atomic_set(&haltstate, 1);
416 irq_spinlock_unlock(&bkpoint_lock, false);
417
418 kconsole("debug", "Debug console ready.\n", false);
419
420 irq_spinlock_lock(&bkpoint_lock, false);
421 atomic_set(&haltstate, 0);
422#endif
423 }
424
425 if ((cur) && (cur->address == fireaddr)
426 && ((cur->flags & BKPOINT_INPROG))) {
427 /* Remove one-shot breakpoint */
428 if ((cur->flags & BKPOINT_ONESHOT))
429 cur->address = (uintptr_t) NULL;
430
431 /* Remove in-progress flag */
432 cur->flags &= ~BKPOINT_INPROG;
433 }
434
435 irq_spinlock_unlock(&bkpoint_lock, false);
436}
437
438/** @}
439 */
Note: See TracBrowser for help on using the repository browser.