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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8b863a62 was b2fa1204, checked in by Martin Sucha <sucha14@…>, 12 years ago

Cherrypick usage of kernel logger

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