Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-btrfs-devel.git] / kernel / debug / kdb / kdb_bp.c
blob20059ef4459a4ff293428337d9936ff438a8eb96
1 /*
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
6 * for more details.
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[] = {
33 "Instruction(i)",
34 "Instruction(Register)",
35 "Data Write",
36 "I/O",
37 "Data Access"
40 static char *kdb_bptype(kdb_bp_t *bp)
42 if (bp->bp_type < 0 || bp->bp_type > 4)
43 return "";
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;
51 int diag;
53 bp->bph_length = 1;
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;
61 else
62 return KDB_ARGCOUNT;
64 bp->bph_length = 1;
66 nextarg++;
68 if ((argc + 1) != nextarg) {
69 unsigned long len;
71 diag = kdbgetularg((char *)argv[nextarg],
72 &len);
73 if (diag)
74 return diag;
77 if (len > 8)
78 return KDB_BADLENGTH;
80 bp->bph_length = len;
81 nextarg++;
84 if ((argc + 1) != nextarg)
85 return KDB_ARGCOUNT;
88 *nextargp = nextarg;
89 return 0;
92 static int _kdb_bp_remove(kdb_bp_t *bp)
94 int ret = 1;
95 if (!bp->bp_installed)
96 return ret;
97 if (!bp->bp_type)
98 ret = dbg_remove_sw_break(bp->bp_addr);
99 else
100 ret = arch_kgdb_ops.remove_hw_breakpoint(bp->bp_addr,
101 bp->bph_length,
102 bp->bp_type);
103 if (ret == 0)
104 bp->bp_installed = 0;
105 return ret;
108 static void kdb_handle_bp(struct pt_regs *regs, kdb_bp_t *bp)
110 if (KDB_DEBUG(BP))
111 kdb_printf("regs->ip = 0x%lx\n", instruction_pointer(regs));
114 * Setup single step
116 kdb_setsinglestep(regs);
119 * Reset delay attribute
121 bp->bp_delay = 0;
122 bp->bp_delayed = 1;
125 static int _kdb_bp_install(struct pt_regs *regs, kdb_bp_t *bp)
127 int ret;
129 * Install the breakpoint, if it is not already installed.
132 if (KDB_DEBUG(BP))
133 kdb_printf("%s: bp_installed %d\n",
134 __func__, bp->bp_installed);
135 if (!KDB_STATE(SSBPT))
136 bp->bp_delay = 0;
137 if (bp->bp_installed)
138 return 1;
139 if (bp->bp_delay || (bp->bp_delayed && KDB_STATE(DOING_SS))) {
140 if (KDB_DEBUG(BP))
141 kdb_printf("%s: delayed bp\n", __func__);
142 kdb_handle_bp(regs, bp);
143 return 0;
145 if (!bp->bp_type)
146 ret = dbg_set_sw_break(bp->bp_addr);
147 else
148 ret = arch_kgdb_ops.set_hw_breakpoint(bp->bp_addr,
149 bp->bph_length,
150 bp->bp_type);
151 if (ret == 0) {
152 bp->bp_installed = 1;
153 } else {
154 kdb_printf("%s: failed to set breakpoint at 0x%lx\n",
155 __func__, bp->bp_addr);
156 return 1;
158 return 0;
162 * kdb_bp_install
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)
171 int i;
173 for (i = 0; i < KDB_MAXBPT; i++) {
174 kdb_bp_t *bp = &kdb_breakpoints[i];
176 if (KDB_DEBUG(BP)) {
177 kdb_printf("%s: bp %d bp_enabled %d\n",
178 __func__, i, bp->bp_enabled);
180 if (bp->bp_enabled)
181 _kdb_bp_install(regs, bp);
186 * kdb_bp_remove
188 * Remove kdb_breakpoints upon entry to the kernel debugger.
190 * Parameters:
191 * None.
192 * Outputs:
193 * None.
194 * Returns:
195 * None.
196 * Locking:
197 * None.
198 * Remarks:
200 void kdb_bp_remove(void)
202 int i;
204 for (i = KDB_MAXBPT - 1; i >= 0; i--) {
205 kdb_bp_t *bp = &kdb_breakpoints[i];
207 if (KDB_DEBUG(BP)) {
208 kdb_printf("%s: bp %d bp_enabled %d\n",
209 __func__, i, bp->bp_enabled);
211 if (bp->bp_enabled)
212 _kdb_bp_remove(bp);
218 * kdb_printbp
220 * Internal function to format and print a breakpoint entry.
222 * Parameters:
223 * None.
224 * Outputs:
225 * None.
226 * Returns:
227 * None.
228 * Locking:
229 * None.
230 * Remarks:
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);
239 if (bp->bp_enabled)
240 kdb_printf("\n is enabled");
241 else
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);
247 kdb_printf("\n");
251 * kdb_bp
253 * Handle the bp commands.
255 * [bp|bph] <addr-expression> [DATAR|DATAW]
257 * Parameters:
258 * argc Count of arguments in argv
259 * argv Space delimited command line arguments
260 * Outputs:
261 * None.
262 * Returns:
263 * Zero for success, a kdb diagnostic if failure.
264 * Locking:
265 * None.
266 * Remarks:
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)
274 int i, bpno;
275 kdb_bp_t *bp, *bp_check;
276 int diag;
277 char *symname = NULL;
278 long offset = 0ul;
279 int nextarg;
280 kdb_bp_t template = {0};
282 if (argc == 0) {
284 * Display breakpoint table
286 for (bpno = 0, bp = kdb_breakpoints; bpno < KDB_MAXBPT;
287 bpno++, bp++) {
288 if (bp->bp_free)
289 continue;
290 kdb_printbp(bp, bpno);
293 return 0;
296 nextarg = 1;
297 diag = kdbgetaddrarg(argc, argv, &nextarg, &template.bp_addr,
298 &offset, &symname);
299 if (diag)
300 return diag;
301 if (!template.bp_addr)
302 return KDB_BADINT;
305 * Find an empty bp structure to allocate
307 for (bpno = 0, bp = kdb_breakpoints; bpno < KDB_MAXBPT; bpno++, bp++) {
308 if (bp->bp_free)
309 break;
312 if (bpno == KDB_MAXBPT)
313 return KDB_TOOMANYBPT;
315 if (strcmp(argv[0], "bph") == 0) {
316 template.bp_type = BP_HARDWARE_BREAKPOINT;
317 diag = kdb_parsebp(argc, argv, &nextarg, &template);
318 if (diag)
319 return diag;
320 } else {
321 template.bp_type = BP_BREAKPOINT;
325 * Check for clashing breakpoints.
327 * Note, in this design we can't have hardware breakpoints
328 * enabled for both read and write on the same address.
330 for (i = 0, bp_check = kdb_breakpoints; i < KDB_MAXBPT;
331 i++, bp_check++) {
332 if (!bp_check->bp_free &&
333 bp_check->bp_addr == template.bp_addr) {
334 kdb_printf("You already have a breakpoint at "
335 kdb_bfd_vma_fmt0 "\n", template.bp_addr);
336 return KDB_DUPBPT;
340 template.bp_enabled = 1;
343 * Actually allocate the breakpoint found earlier
345 *bp = template;
346 bp->bp_free = 0;
348 kdb_printbp(bp, bpno);
350 return 0;
354 * kdb_bc
356 * Handles the 'bc', 'be', and 'bd' commands
358 * [bd|bc|be] <breakpoint-number>
359 * [bd|bc|be] *
361 * Parameters:
362 * argc Count of arguments in argv
363 * argv Space delimited command line arguments
364 * Outputs:
365 * None.
366 * Returns:
367 * Zero for success, a kdb diagnostic for failure
368 * Locking:
369 * None.
370 * Remarks:
372 static int kdb_bc(int argc, const char **argv)
374 unsigned long addr;
375 kdb_bp_t *bp = NULL;
376 int lowbp = KDB_MAXBPT;
377 int highbp = 0;
378 int done = 0;
379 int i;
380 int diag = 0;
382 int cmd; /* KDBCMD_B? */
383 #define KDBCMD_BC 0
384 #define KDBCMD_BE 1
385 #define KDBCMD_BD 2
387 if (strcmp(argv[0], "be") == 0)
388 cmd = KDBCMD_BE;
389 else if (strcmp(argv[0], "bd") == 0)
390 cmd = KDBCMD_BD;
391 else
392 cmd = KDBCMD_BC;
394 if (argc != 1)
395 return KDB_ARGCOUNT;
397 if (strcmp(argv[1], "*") == 0) {
398 lowbp = 0;
399 highbp = KDB_MAXBPT;
400 } else {
401 diag = kdbgetularg(argv[1], &addr);
402 if (diag)
403 return diag;
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;
412 highbp++;
413 } else {
414 for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT;
415 i++, bp++) {
416 if (bp->bp_addr == addr) {
417 lowbp = highbp = i;
418 highbp++;
419 break;
426 * Now operate on the set of breakpoints matching the input
427 * criteria (either '*' for all, or an individual breakpoint).
429 for (bp = &kdb_breakpoints[lowbp], i = lowbp;
430 i < highbp;
431 i++, bp++) {
432 if (bp->bp_free)
433 continue;
435 done++;
437 switch (cmd) {
438 case KDBCMD_BC:
439 bp->bp_enabled = 0;
441 kdb_printf("Breakpoint %d at "
442 kdb_bfd_vma_fmt " cleared\n",
443 i, bp->bp_addr);
445 bp->bp_addr = 0;
446 bp->bp_free = 1;
448 break;
449 case KDBCMD_BE:
450 bp->bp_enabled = 1;
452 kdb_printf("Breakpoint %d at "
453 kdb_bfd_vma_fmt " enabled",
454 i, bp->bp_addr);
456 kdb_printf("\n");
457 break;
458 case KDBCMD_BD:
459 if (!bp->bp_enabled)
460 break;
462 bp->bp_enabled = 0;
464 kdb_printf("Breakpoint %d at "
465 kdb_bfd_vma_fmt " disabled\n",
466 i, bp->bp_addr);
468 break;
470 if (bp->bp_delay && (cmd == KDBCMD_BC || cmd == KDBCMD_BD)) {
471 bp->bp_delay = 0;
472 KDB_STATE_CLEAR(SSBPT);
476 return (!done) ? KDB_BPTNOTFOUND : 0;
480 * kdb_ss
482 * Process the 'ss' (Single Step) and 'ssb' (Single Step to Branch)
483 * commands.
485 * ss
486 * ssb
488 * Parameters:
489 * argc Argument count
490 * argv Argument vector
491 * Outputs:
492 * None.
493 * Returns:
494 * KDB_CMD_SS[B] for success, a kdb error if failure.
495 * Locking:
496 * None.
497 * Remarks:
499 * Set the arch specific option to trigger a debug trap after the next
500 * instruction.
502 * For 'ssb', set the trace flag in the debug trap handler
503 * after printing the current insn and return directly without
504 * invoking the kdb command processor, until a branch instruction
505 * is encountered.
508 static int kdb_ss(int argc, const char **argv)
510 int ssb = 0;
512 ssb = (strcmp(argv[0], "ssb") == 0);
513 if (argc != 0)
514 return KDB_ARGCOUNT;
516 * Set trace flag and go.
518 KDB_STATE_SET(DOING_SS);
519 if (ssb) {
520 KDB_STATE_SET(DOING_SSB);
521 return KDB_CMD_SSB;
523 return KDB_CMD_SS;
526 /* Initialize the breakpoint table and register breakpoint commands. */
528 void __init kdb_initbptab(void)
530 int i;
531 kdb_bp_t *bp;
534 * First time initialization.
536 memset(&kdb_breakpoints, '\0', sizeof(kdb_breakpoints));
538 for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++)
539 bp->bp_free = 1;
541 kdb_register_repeat("bp", kdb_bp, "[<vaddr>]",
542 "Set/Display breakpoints", 0, KDB_REPEAT_NO_ARGS);
543 kdb_register_repeat("bl", kdb_bp, "[<vaddr>]",
544 "Display breakpoints", 0, KDB_REPEAT_NO_ARGS);
545 if (arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT)
546 kdb_register_repeat("bph", kdb_bp, "[<vaddr>]",
547 "[datar [length]|dataw [length]] Set hw brk", 0, KDB_REPEAT_NO_ARGS);
548 kdb_register_repeat("bc", kdb_bc, "<bpnum>",
549 "Clear Breakpoint", 0, KDB_REPEAT_NONE);
550 kdb_register_repeat("be", kdb_bc, "<bpnum>",
551 "Enable Breakpoint", 0, KDB_REPEAT_NONE);
552 kdb_register_repeat("bd", kdb_bc, "<bpnum>",
553 "Disable Breakpoint", 0, KDB_REPEAT_NONE);
555 kdb_register_repeat("ss", kdb_ss, "",
556 "Single Step", 1, KDB_REPEAT_NO_ARGS);
557 kdb_register_repeat("ssb", kdb_ss, "",
558 "Single step to branch/call", 0, KDB_REPEAT_NO_ARGS);
560 * Architecture dependent initialization.