2 * Kernel Debugger Architecture Independent Breakpoint Handler
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
8 * Copyright (c) 1999-2004 Silicon Graphics, Inc. All Rights Reserved.
9 * Copyright (c) 2009 Wind River Systems, Inc. All Rights Reserved.
12 #include <linux/string.h>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/kdb.h>
16 #include <linux/kgdb.h>
17 #include <linux/smp.h>
18 #include <linux/sched.h>
19 #include <linux/interrupt.h>
20 #include "kdb_private.h"
23 * Table of kdb_breakpoints
25 kdb_bp_t kdb_breakpoints[KDB_MAXBPT];
27 static void kdb_setsinglestep(struct pt_regs *regs)
29 KDB_STATE_SET(DOING_SS);
32 static char *kdb_rwtypes[] = {
34 "Instruction(Register)",
40 static char *kdb_bptype(kdb_bp_t *bp)
42 if (bp->bp_type < 0 || bp->bp_type > 4)
45 return kdb_rwtypes[bp->bp_type];
48 static int kdb_parsebp(int argc, const char **argv, int *nextargp, kdb_bp_t *bp)
50 int nextarg = *nextargp;
54 if ((argc + 1) != nextarg) {
55 if (strnicmp(argv[nextarg], "datar", sizeof("datar")) == 0)
56 bp->bp_type = BP_ACCESS_WATCHPOINT;
57 else if (strnicmp(argv[nextarg], "dataw", sizeof("dataw")) == 0)
58 bp->bp_type = BP_WRITE_WATCHPOINT;
59 else if (strnicmp(argv[nextarg], "inst", sizeof("inst")) == 0)
60 bp->bp_type = BP_HARDWARE_BREAKPOINT;
68 if ((argc + 1) != nextarg) {
71 diag = kdbgetularg((char *)argv[nextarg],
84 if ((argc + 1) != nextarg)
92 static int _kdb_bp_remove(kdb_bp_t *bp)
95 if (!bp->bp_installed)
98 ret = dbg_remove_sw_break(bp->bp_addr);
100 ret = arch_kgdb_ops.remove_hw_breakpoint(bp->bp_addr,
104 bp->bp_installed = 0;
108 static void kdb_handle_bp(struct pt_regs *regs, kdb_bp_t *bp)
111 kdb_printf("regs->ip = 0x%lx\n", instruction_pointer(regs));
116 kdb_setsinglestep(regs);
119 * Reset delay attribute
125 static int _kdb_bp_install(struct pt_regs *regs, kdb_bp_t *bp)
129 * Install the breakpoint, if it is not already installed.
133 kdb_printf("%s: bp_installed %d\n",
134 __func__, bp->bp_installed);
135 if (!KDB_STATE(SSBPT))
137 if (bp->bp_installed)
139 if (bp->bp_delay || (bp->bp_delayed && KDB_STATE(DOING_SS))) {
141 kdb_printf("%s: delayed bp\n", __func__);
142 kdb_handle_bp(regs, bp);
146 ret = dbg_set_sw_break(bp->bp_addr);
148 ret = arch_kgdb_ops.set_hw_breakpoint(bp->bp_addr,
152 bp->bp_installed = 1;
154 kdb_printf("%s: failed to set breakpoint at 0x%lx\n",
155 __func__, bp->bp_addr);
156 #ifdef CONFIG_DEBUG_RODATA
158 kdb_printf("Software breakpoints are unavailable.\n"
159 " Change the kernel CONFIG_DEBUG_RODATA=n\n"
160 " OR use hw breaks: help bph\n");
171 * Install kdb_breakpoints prior to returning from the
172 * kernel debugger. This allows the kdb_breakpoints to be set
173 * upon functions that are used internally by kdb, such as
174 * printk(). This function is only called once per kdb session.
176 void kdb_bp_install(struct pt_regs *regs)
180 for (i = 0; i < KDB_MAXBPT; i++) {
181 kdb_bp_t *bp = &kdb_breakpoints[i];
184 kdb_printf("%s: bp %d bp_enabled %d\n",
185 __func__, i, bp->bp_enabled);
188 _kdb_bp_install(regs, bp);
195 * Remove kdb_breakpoints upon entry to the kernel debugger.
207 void kdb_bp_remove(void)
211 for (i = KDB_MAXBPT - 1; i >= 0; i--) {
212 kdb_bp_t *bp = &kdb_breakpoints[i];
215 kdb_printf("%s: bp %d bp_enabled %d\n",
216 __func__, i, bp->bp_enabled);
227 * Internal function to format and print a breakpoint entry.
240 static void kdb_printbp(kdb_bp_t *bp, int i)
242 kdb_printf("%s ", kdb_bptype(bp));
243 kdb_printf("BP #%d at ", i);
244 kdb_symbol_print(bp->bp_addr, NULL, KDB_SP_DEFAULT);
247 kdb_printf("\n is enabled");
249 kdb_printf("\n is disabled");
251 kdb_printf("\taddr at %016lx, hardtype=%d installed=%d\n",
252 bp->bp_addr, bp->bp_type, bp->bp_installed);
260 * Handle the bp commands.
262 * [bp|bph] <addr-expression> [DATAR|DATAW]
265 * argc Count of arguments in argv
266 * argv Space delimited command line arguments
270 * Zero for success, a kdb diagnostic if failure.
275 * bp Set breakpoint on all cpus. Only use hardware assist if need.
276 * bph Set breakpoint on all cpus. Force hardware register
279 static int kdb_bp(int argc, const char **argv)
282 kdb_bp_t *bp, *bp_check;
284 char *symname = NULL;
287 kdb_bp_t template = {0};
291 * Display breakpoint table
293 for (bpno = 0, bp = kdb_breakpoints; bpno < KDB_MAXBPT;
297 kdb_printbp(bp, bpno);
304 diag = kdbgetaddrarg(argc, argv, &nextarg, &template.bp_addr,
308 if (!template.bp_addr)
312 * Find an empty bp structure to allocate
314 for (bpno = 0, bp = kdb_breakpoints; bpno < KDB_MAXBPT; bpno++, bp++) {
319 if (bpno == KDB_MAXBPT)
320 return KDB_TOOMANYBPT;
322 if (strcmp(argv[0], "bph") == 0) {
323 template.bp_type = BP_HARDWARE_BREAKPOINT;
324 diag = kdb_parsebp(argc, argv, &nextarg, &template);
328 template.bp_type = BP_BREAKPOINT;
332 * Check for clashing breakpoints.
334 * Note, in this design we can't have hardware breakpoints
335 * enabled for both read and write on the same address.
337 for (i = 0, bp_check = kdb_breakpoints; i < KDB_MAXBPT;
339 if (!bp_check->bp_free &&
340 bp_check->bp_addr == template.bp_addr) {
341 kdb_printf("You already have a breakpoint at "
342 kdb_bfd_vma_fmt0 "\n", template.bp_addr);
347 template.bp_enabled = 1;
350 * Actually allocate the breakpoint found earlier
355 kdb_printbp(bp, bpno);
363 * Handles the 'bc', 'be', and 'bd' commands
365 * [bd|bc|be] <breakpoint-number>
369 * argc Count of arguments in argv
370 * argv Space delimited command line arguments
374 * Zero for success, a kdb diagnostic for failure
379 static int kdb_bc(int argc, const char **argv)
383 int lowbp = KDB_MAXBPT;
389 int cmd; /* KDBCMD_B? */
394 if (strcmp(argv[0], "be") == 0)
396 else if (strcmp(argv[0], "bd") == 0)
404 if (strcmp(argv[1], "*") == 0) {
408 diag = kdbgetularg(argv[1], &addr);
413 * For addresses less than the maximum breakpoint number,
414 * assume that the breakpoint number is desired.
416 if (addr < KDB_MAXBPT) {
417 bp = &kdb_breakpoints[addr];
418 lowbp = highbp = addr;
421 for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT;
423 if (bp->bp_addr == addr) {
433 * Now operate on the set of breakpoints matching the input
434 * criteria (either '*' for all, or an individual breakpoint).
436 for (bp = &kdb_breakpoints[lowbp], i = lowbp;
448 kdb_printf("Breakpoint %d at "
449 kdb_bfd_vma_fmt " cleared\n",
459 kdb_printf("Breakpoint %d at "
460 kdb_bfd_vma_fmt " enabled",
471 kdb_printf("Breakpoint %d at "
472 kdb_bfd_vma_fmt " disabled\n",
477 if (bp->bp_delay && (cmd == KDBCMD_BC || cmd == KDBCMD_BD)) {
479 KDB_STATE_CLEAR(SSBPT);
483 return (!done) ? KDB_BPTNOTFOUND : 0;
489 * Process the 'ss' (Single Step) command.
494 * argc Argument count
495 * argv Argument vector
499 * KDB_CMD_SS for success, a kdb error if failure.
504 * Set the arch specific option to trigger a debug trap after the next
508 static int kdb_ss(int argc, const char **argv)
513 * Set trace flag and go.
515 KDB_STATE_SET(DOING_SS);
519 /* Initialize the breakpoint table and register breakpoint commands. */
521 void __init kdb_initbptab(void)
527 * First time initialization.
529 memset(&kdb_breakpoints, '\0', sizeof(kdb_breakpoints));
531 for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++)
534 kdb_register_repeat("bp", kdb_bp, "[<vaddr>]",
535 "Set/Display breakpoints", 0, KDB_REPEAT_NO_ARGS);
536 kdb_register_repeat("bl", kdb_bp, "[<vaddr>]",
537 "Display breakpoints", 0, KDB_REPEAT_NO_ARGS);
538 if (arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT)
539 kdb_register_repeat("bph", kdb_bp, "[<vaddr>]",
540 "[datar [length]|dataw [length]] Set hw brk", 0, KDB_REPEAT_NO_ARGS);
541 kdb_register_repeat("bc", kdb_bc, "<bpnum>",
542 "Clear Breakpoint", 0, KDB_REPEAT_NONE);
543 kdb_register_repeat("be", kdb_bc, "<bpnum>",
544 "Enable Breakpoint", 0, KDB_REPEAT_NONE);
545 kdb_register_repeat("bd", kdb_bc, "<bpnum>",
546 "Disable Breakpoint", 0, KDB_REPEAT_NONE);
548 kdb_register_repeat("ss", kdb_ss, "",
549 "Single Step", 1, KDB_REPEAT_NO_ARGS);
551 * Architecture dependent initialization.