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
);
164 * Install kdb_breakpoints prior to returning from the
165 * kernel debugger. This allows the kdb_breakpoints to be set
166 * upon functions that are used internally by kdb, such as
167 * printk(). This function is only called once per kdb session.
169 void kdb_bp_install(struct pt_regs
*regs
)
173 for (i
= 0; i
< KDB_MAXBPT
; i
++) {
174 kdb_bp_t
*bp
= &kdb_breakpoints
[i
];
177 kdb_printf("%s: bp %d bp_enabled %d\n",
178 __func__
, i
, bp
->bp_enabled
);
181 _kdb_bp_install(regs
, bp
);
188 * Remove kdb_breakpoints upon entry to the kernel debugger.
200 void kdb_bp_remove(void)
204 for (i
= KDB_MAXBPT
- 1; i
>= 0; i
--) {
205 kdb_bp_t
*bp
= &kdb_breakpoints
[i
];
208 kdb_printf("%s: bp %d bp_enabled %d\n",
209 __func__
, i
, bp
->bp_enabled
);
220 * Internal function to format and print a breakpoint entry.
233 static void kdb_printbp(kdb_bp_t
*bp
, int i
)
235 kdb_printf("%s ", kdb_bptype(bp
));
236 kdb_printf("BP #%d at ", i
);
237 kdb_symbol_print(bp
->bp_addr
, NULL
, KDB_SP_DEFAULT
);
240 kdb_printf("\n is enabled");
242 kdb_printf("\n is disabled");
244 kdb_printf("\taddr at %016lx, hardtype=%d installed=%d\n",
245 bp
->bp_addr
, bp
->bp_type
, bp
->bp_installed
);
253 * Handle the bp commands.
255 * [bp|bph] <addr-expression> [DATAR|DATAW]
258 * argc Count of arguments in argv
259 * argv Space delimited command line arguments
263 * Zero for success, a kdb diagnostic if failure.
268 * bp Set breakpoint on all cpus. Only use hardware assist if need.
269 * bph Set breakpoint on all cpus. Force hardware register
272 static int kdb_bp(int argc
, const char **argv
)
275 kdb_bp_t
*bp
, *bp_check
;
278 char *symname
= NULL
;
281 kdb_bp_t
template = {0};
285 * Display breakpoint table
287 for (bpno
= 0, bp
= kdb_breakpoints
; bpno
< KDB_MAXBPT
;
291 kdb_printbp(bp
, bpno
);
298 diag
= kdbgetaddrarg(argc
, argv
, &nextarg
, &template.bp_addr
,
302 if (!template.bp_addr
)
306 * Find an empty bp structure to allocate
309 for (bpno
= 0, bp
= kdb_breakpoints
; bpno
< KDB_MAXBPT
; bpno
++, bp
++) {
314 if (bpno
== KDB_MAXBPT
)
315 return KDB_TOOMANYBPT
;
317 if (strcmp(argv
[0], "bph") == 0) {
318 template.bp_type
= BP_HARDWARE_BREAKPOINT
;
319 diag
= kdb_parsebp(argc
, argv
, &nextarg
, &template);
323 template.bp_type
= BP_BREAKPOINT
;
327 * Check for clashing breakpoints.
329 * Note, in this design we can't have hardware breakpoints
330 * enabled for both read and write on the same address.
332 for (i
= 0, bp_check
= kdb_breakpoints
; i
< KDB_MAXBPT
;
334 if (!bp_check
->bp_free
&&
335 bp_check
->bp_addr
== template.bp_addr
) {
336 kdb_printf("You already have a breakpoint at "
337 kdb_bfd_vma_fmt0
"\n", template.bp_addr
);
342 template.bp_enabled
= 1;
345 * Actually allocate the breakpoint found earlier
350 kdb_printbp(bp
, bpno
);
358 * Handles the 'bc', 'be', and 'bd' commands
360 * [bd|bc|be] <breakpoint-number>
364 * argc Count of arguments in argv
365 * argv Space delimited command line arguments
369 * Zero for success, a kdb diagnostic for failure
374 static int kdb_bc(int argc
, const char **argv
)
378 int lowbp
= KDB_MAXBPT
;
384 int cmd
; /* KDBCMD_B? */
389 if (strcmp(argv
[0], "be") == 0)
391 else if (strcmp(argv
[0], "bd") == 0)
399 if (strcmp(argv
[1], "*") == 0) {
403 diag
= kdbgetularg(argv
[1], &addr
);
408 * For addresses less than the maximum breakpoint number,
409 * assume that the breakpoint number is desired.
411 if (addr
< KDB_MAXBPT
) {
412 bp
= &kdb_breakpoints
[addr
];
413 lowbp
= highbp
= addr
;
416 for (i
= 0, bp
= kdb_breakpoints
; i
< KDB_MAXBPT
;
418 if (bp
->bp_addr
== addr
) {
428 * Now operate on the set of breakpoints matching the input
429 * criteria (either '*' for all, or an individual breakpoint).
431 for (bp
= &kdb_breakpoints
[lowbp
], i
= lowbp
;
443 kdb_printf("Breakpoint %d at "
444 kdb_bfd_vma_fmt
" cleared\n",
454 kdb_printf("Breakpoint %d at "
455 kdb_bfd_vma_fmt
" enabled",
466 kdb_printf("Breakpoint %d at "
467 kdb_bfd_vma_fmt
" disabled\n",
472 if (bp
->bp_delay
&& (cmd
== KDBCMD_BC
|| cmd
== KDBCMD_BD
)) {
474 KDB_STATE_CLEAR(SSBPT
);
478 return (!done
) ? KDB_BPTNOTFOUND
: 0;
484 * Process the 'ss' (Single Step) and 'ssb' (Single Step to Branch)
491 * argc Argument count
492 * argv Argument vector
496 * KDB_CMD_SS[B] for success, a kdb error if failure.
501 * Set the arch specific option to trigger a debug trap after the next
504 * For 'ssb', set the trace flag in the debug trap handler
505 * after printing the current insn and return directly without
506 * invoking the kdb command processor, until a branch instruction
510 static int kdb_ss(int argc
, const char **argv
)
514 ssb
= (strcmp(argv
[0], "ssb") == 0);
518 * Set trace flag and go.
520 KDB_STATE_SET(DOING_SS
);
522 KDB_STATE_SET(DOING_SSB
);
528 /* Initialize the breakpoint table and register breakpoint commands. */
530 void __init
kdb_initbptab(void)
536 * First time initialization.
538 memset(&kdb_breakpoints
, '\0', sizeof(kdb_breakpoints
));
540 for (i
= 0, bp
= kdb_breakpoints
; i
< KDB_MAXBPT
; i
++, bp
++)
543 kdb_register_repeat("bp", kdb_bp
, "[<vaddr>]",
544 "Set/Display breakpoints", 0, KDB_REPEAT_NO_ARGS
);
545 kdb_register_repeat("bl", kdb_bp
, "[<vaddr>]",
546 "Display breakpoints", 0, KDB_REPEAT_NO_ARGS
);
547 if (arch_kgdb_ops
.flags
& KGDB_HW_BREAKPOINT
)
548 kdb_register_repeat("bph", kdb_bp
, "[<vaddr>]",
549 "[datar [length]|dataw [length]] Set hw brk", 0, KDB_REPEAT_NO_ARGS
);
550 kdb_register_repeat("bc", kdb_bc
, "<bpnum>",
551 "Clear Breakpoint", 0, KDB_REPEAT_NONE
);
552 kdb_register_repeat("be", kdb_bc
, "<bpnum>",
553 "Enable Breakpoint", 0, KDB_REPEAT_NONE
);
554 kdb_register_repeat("bd", kdb_bc
, "<bpnum>",
555 "Disable Breakpoint", 0, KDB_REPEAT_NONE
);
557 kdb_register_repeat("ss", kdb_ss
, "",
558 "Single Step", 1, KDB_REPEAT_NO_ARGS
);
559 kdb_register_repeat("ssb", kdb_ss
, "",
560 "Single step to branch/call", 0, KDB_REPEAT_NO_ARGS
);
562 * Architecture dependent initialization.