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.
11 #include <linux/string.h>
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/kdb.h>
15 #include <linux/kdbprivate.h>
16 #include <linux/smp.h>
17 #include <linux/sched.h>
18 #include <asm/system.h>
21 * Table of kdb_breakpoints
23 kdb_bp_t kdb_breakpoints[KDB_MAXBPT];
26 * kdb_bp_install_global
28 * Install global kdb_breakpoints prior to returning from the
29 * kernel debugger. This allows the kdb_breakpoints to be set
30 * upon functions that are used internally by kdb, such as
34 * regs Execution frame.
43 * This function is only called once per kdb session.
47 kdb_bp_install_global(struct pt_regs *regs)
51 for(i=0; i<KDB_MAXBPT; i++) {
53 kdb_printf("kdb_bp_install_global bp %d bp_enabled %d bp_global %d\n",
54 i, kdb_breakpoints[i].bp_enabled, kdb_breakpoints[i].bp_global);
56 if (kdb_breakpoints[i].bp_enabled
57 && kdb_breakpoints[i].bp_global) {
58 kdba_installbp(regs, &kdb_breakpoints[i]);
64 * kdb_bp_install_local
66 * Install local kdb_breakpoints prior to returning from the
67 * kernel debugger. This allows the kdb_breakpoints to be set
68 * upon functions that are used internally by kdb, such as
72 * regs Execution frame.
81 * This function is called once per processor.
85 kdb_bp_install_local(struct pt_regs *regs)
89 for(i=0; i<KDB_MAXBPT; i++) {
91 kdb_printf("kdb_bp_install_local bp %d bp_enabled %d bp_global %d cpu %d bp_cpu %d\n",
92 i, kdb_breakpoints[i].bp_enabled, kdb_breakpoints[i].bp_global,
93 smp_processor_id(), kdb_breakpoints[i].bp_cpu);
95 if (kdb_breakpoints[i].bp_enabled
96 && kdb_breakpoints[i].bp_cpu == smp_processor_id()
97 && !kdb_breakpoints[i].bp_global){
98 kdba_installbp(regs, &kdb_breakpoints[i]);
104 * kdb_bp_remove_global
106 * Remove global kdb_breakpoints upon entry to the kernel debugger.
120 kdb_bp_remove_global(void)
124 for(i=KDB_MAXBPT-1; i>=0; i--) {
126 kdb_printf("kdb_bp_remove_global bp %d bp_enabled %d bp_global %d\n",
127 i, kdb_breakpoints[i].bp_enabled, kdb_breakpoints[i].bp_global);
129 if (kdb_breakpoints[i].bp_enabled
130 && kdb_breakpoints[i].bp_global) {
131 kdba_removebp(&kdb_breakpoints[i]);
138 * kdb_bp_remove_local
140 * Remove local kdb_breakpoints upon entry to the kernel debugger.
154 kdb_bp_remove_local(void)
158 for(i=KDB_MAXBPT-1; i>=0; i--) {
160 kdb_printf("kdb_bp_remove_local bp %d bp_enabled %d bp_global %d cpu %d bp_cpu %d\n",
161 i, kdb_breakpoints[i].bp_enabled, kdb_breakpoints[i].bp_global,
162 smp_processor_id(), kdb_breakpoints[i].bp_cpu);
164 if (kdb_breakpoints[i].bp_enabled
165 && kdb_breakpoints[i].bp_cpu == smp_processor_id()
166 && !kdb_breakpoints[i].bp_global){
167 kdba_removebp(&kdb_breakpoints[i]);
175 * Internal function to format and print a breakpoint entry.
189 kdb_printbp(kdb_bp_t *bp, int i)
191 if (bp->bp_forcehw) {
192 kdb_printf("Forced ");
195 if (!bp->bp_template.bph_free) {
196 kdb_printf("%s ", kdba_bptype(&bp->bp_template));
198 kdb_printf("Instruction(i) ");
201 kdb_printf("BP #%d at ", i);
202 kdb_symbol_print(bp->bp_addr, NULL, KDB_SP_DEFAULT);
204 if (bp->bp_enabled) {
207 kdb_printf(" globally");
209 kdb_printf(" on cpu %d", bp->bp_cpu);
211 kdb_printf(" adjust %d", bp->bp_adjust);
213 kdb_printf("\n is disabled");
222 * Handle the bp, and bpa commands.
224 * [bp|bpa|bph] <addr-expression> [DATAR|DATAW|IO [length]]
227 * argc Count of arguments in argv
228 * argv Space delimited command line arguments
229 * envp Environment value
230 * regs Exception frame at entry to kernel debugger
234 * Zero for success, a kdb diagnostic if failure.
239 * bp Set breakpoint. Only use hardware assist if necessary.
240 * bpa Set breakpoint on all cpus, only use hardware regs if necessary
241 * bph Set breakpoint - force hardware register
242 * bpha Set breakpoint on all cpus, force hardware register
246 kdb_bp(int argc, const char **argv, const char **envp, struct pt_regs *regs)
249 kdb_bp_t *bp, *bp_check;
253 char *symname = NULL;
261 * Display breakpoint table
263 for(bpno=0,bp=kdb_breakpoints; bpno<KDB_MAXBPT; bpno++, bp++) {
264 if (bp->bp_free) continue;
266 kdb_printbp(bp, bpno);
272 global = ((strcmp(argv[0], "bpa") == 0)
273 || (strcmp(argv[0], "bpha") == 0));
274 hardware = ((strcmp(argv[0], "bph") == 0)
275 || (strcmp(argv[0], "bpha") == 0));
278 diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, &symname, regs);
285 * Allocate a new bp structure
288 for(bpno=0,bp=kdb_breakpoints; bpno<KDB_MAXBPT; bpno++,bp++) {
294 if (bpno == KDB_MAXBPT)
295 return KDB_TOOMANYBPT;
297 memset(bp, 0, sizeof(*bp));
299 kdba_check_pc(&addr);
300 for(i=0,bp_check=kdb_breakpoints; i<KDB_MAXBPT; i++,bp_check++) {
301 if (!bp_check->bp_free && bp_check->bp_addr == addr) {
302 kdb_printf("You already have a breakpoint at " kdb_bfd_vma_fmt0 "\n", addr);
309 bp->bp_forcehw = hardware;
311 kdb_printf("kdb_bp: forcehw is %d hardware is %d\n", bp->bp_forcehw, hardware);
314 * Handle architecture dependent parsing
316 diag = kdba_parsebp(argc, argv, &nextarg, bp);
322 bp->bp_global = 1; /* Most breakpoints are global */
324 if (hardware && !global) {
326 bp->bp_cpu = smp_processor_id();
330 * Allocate a hardware breakpoint. If one is not available,
331 * disable the breakpoint, but leave it in the breakpoint
332 * table. When the breakpoint is re-enabled (via 'be'), we'll
333 * attempt to allocate a hardware register for it.
335 if (!bp->bp_template.bph_free) {
336 bp->bp_hard = kdba_allocbp(&bp->bp_template, &diag);
344 kdb_printbp(bp, bpno);
352 * Handles the 'bc', 'be', and 'bd' commands
354 * [bd|bc|be] <breakpoint-number>
358 * argc Count of arguments in argv
359 * argv Space delimited command line arguments
360 * envp Environment value
361 * regs Exception frame at entry to kernel debugger
365 * Zero for success, a kdb diagnostic for failure
376 kdb_bc(int argc, const char **argv, const char **envp, struct pt_regs *regs)
380 int lowbp = KDB_MAXBPT;
385 int cmd; /* KDBCMD_B? */
387 if (strcmp(argv[0], "be") == 0) {
389 } else if (strcmp(argv[0], "bd") == 0) {
397 if (strcmp(argv[1], "*") == 0) {
401 diag = kdbgetularg(argv[1], &addr);
406 * For addresses less than the maximum breakpoint number,
407 * assume that the breakpoint number is desired.
409 if (addr < KDB_MAXBPT) {
410 bp = &kdb_breakpoints[addr];
411 lowbp = highbp = addr;
414 for(i=0, bp=kdb_breakpoints; i<KDB_MAXBPT; i++, bp++) {
415 if (bp->bp_addr == addr) {
425 * Now operate on the set of breakpoints matching the input
426 * criteria (either '*' for all, or an individual breakpoint).
428 for(bp=&kdb_breakpoints[lowbp], i=lowbp;
438 if (bp->bp_hardtype) {
439 kdba_freebp(bp->bp_hard);
447 kdb_printf("Breakpoint %d at " kdb_bfd_vma_fmt " cleared\n",
456 * Allocate a hardware breakpoint. If one is not
457 * available, don't enable the breakpoint.
459 if (!bp->bp_template.bph_free
460 && !bp->bp_hardtype) {
461 bp->bp_hard = kdba_allocbp(&bp->bp_template, &diag);
471 kdb_printf("Breakpoint %d at " kdb_bfd_vma_fmt " enabled",
477 if (!bp->bp_enabled) {
482 * Since this breakpoint is now disabled, we can
483 * give up the hardware register which is allocated
486 if (bp->bp_hardtype) {
487 kdba_freebp(bp->bp_hard);
494 kdb_printf("Breakpoint %d at " kdb_bfd_vma_fmt " disabled\n",
499 if (bp->bp_delay && (cmd == KDBCMD_BC || cmd == KDBCMD_BD)) {
501 KDB_STATE_CLEAR(SSBPT);
505 return (!done)?KDB_BPTNOTFOUND:0;
511 * Process the 'ss' (Single Step) and 'ssb' (Single Step to Branch)
518 * argc Argument count
519 * argv Argument vector
520 * envp Environment vector
521 * regs Registers at time of entry to kernel debugger
525 * KDB_CMD_SS[B] for success, a kdb error if failure.
530 * Set the arch specific option to trigger a debug trap after the next
533 * For 'ssb', set the trace flag in the debug trap handler
534 * after printing the current insn and return directly without
535 * invoking the kdb command processor, until a branch instruction
540 kdb_ss(int argc, const char **argv, const char **envp, struct pt_regs *regs)
544 ssb = (strcmp(argv[0], "ssb") == 0);
549 kdb_printf("%s: pt_regs not available\n", __FUNCTION__);
554 * Set trace flag and go.
556 KDB_STATE_SET(DOING_SS);
558 KDB_STATE_SET(DOING_SSB);
560 kdba_setsinglestep(regs); /* Enable single step */
570 * Initialize the breakpoint table. Register breakpoint commands.
590 * First time initialization.
592 memset(&kdb_breakpoints, '\0', sizeof(kdb_breakpoints));
594 for (i=0, bp=kdb_breakpoints; i<KDB_MAXBPT; i++, bp++) {
597 * The bph_free flag is architecturally required. It
598 * is set by architecture-dependent code to false (zero)
599 * in the event a hardware breakpoint register is required
600 * for this breakpoint.
602 * The rest of the template is reserved to the architecture
603 * dependent code and _must_ not be touched by the architecture
606 bp->bp_template.bph_free = 1;
609 kdb_register_repeat("bp", kdb_bp, "[<vaddr>]", "Set/Display breakpoints", 0, KDB_REPEAT_NO_ARGS);
610 kdb_register_repeat("bl", kdb_bp, "[<vaddr>]", "Display breakpoints", 0, KDB_REPEAT_NO_ARGS);
611 kdb_register_repeat("bpa", kdb_bp, "[<vaddr>]", "Set/Display global breakpoints", 0, KDB_REPEAT_NO_ARGS);
612 kdb_register_repeat("bph", kdb_bp, "[<vaddr>]", "Set hardware breakpoint", 0, KDB_REPEAT_NO_ARGS);
613 kdb_register_repeat("bpha", kdb_bp, "[<vaddr>]", "Set global hardware breakpoint", 0, KDB_REPEAT_NO_ARGS);
614 kdb_register_repeat("bc", kdb_bc, "<bpnum>", "Clear Breakpoint", 0, KDB_REPEAT_NONE);
615 kdb_register_repeat("be", kdb_bc, "<bpnum>", "Enable Breakpoint", 0, KDB_REPEAT_NONE);
616 kdb_register_repeat("bd", kdb_bc, "<bpnum>", "Disable Breakpoint", 0, KDB_REPEAT_NONE);
618 kdb_register_repeat("ss", kdb_ss, "", "Single Step", 1, KDB_REPEAT_NO_ARGS);
619 kdb_register_repeat("ssb", kdb_ss, "", "Single step to branch/call", 0, KDB_REPEAT_NO_ARGS);
621 * Architecture dependent initialization.