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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since fc10e1b was e3306d04, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Convert atomic_t to atomic_size_t (4): Use atomic_store instead of atomic_set

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