75bd9b3ebbb7cf501800115a531e86e7c30c5a28
[linux-2.6.git] / kernel / debug / kdb / kdb_bp.c
1 /*
2  * Kernel Debugger Architecture Independent Breakpoint Handler
3  *
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.
7  *
8  * Copyright (c) 1999-2004 Silicon Graphics, Inc.  All Rights Reserved.
9  * Copyright (c) 2009 Wind River Systems, Inc.  All Rights Reserved.
10  */
11
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"
21
22 /*
23  * Table of kdb_breakpoints
24  */
25 kdb_bp_t kdb_breakpoints[KDB_MAXBPT];
26
27 static void kdb_setsinglestep(struct pt_regs *regs)
28 {
29         KDB_STATE_SET(DOING_SS);
30 }
31
32 static char *kdb_rwtypes[] = {
33         "Instruction(i)",
34         "Instruction(Register)",
35         "Data Write",
36         "I/O",
37         "Data Access"
38 };
39
40 static char *kdb_bptype(kdb_bp_t *bp)
41 {
42         if (bp->bp_type < 0 || bp->bp_type > 4)
43                 return "";
44
45         return kdb_rwtypes[bp->bp_type];
46 }
47
48 static int kdb_parsebp(int argc, const char **argv, int *nextargp, kdb_bp_t *bp)
49 {
50         int nextarg = *nextargp;
51         int diag;
52
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;
63
64                 bp->bph_length = 1;
65
66                 nextarg++;
67
68                 if ((argc + 1) != nextarg) {
69                         unsigned long len;
70
71                         diag = kdbgetularg((char *)argv[nextarg],
72                                            &len);
73                         if (diag)
74                                 return diag;
75
76
77                         if (len > 8)
78                                 return KDB_BADLENGTH;
79
80                         bp->bph_length = len;
81                         nextarg++;
82                 }
83
84                 if ((argc + 1) != nextarg)
85                         return KDB_ARGCOUNT;
86         }
87
88         *nextargp = nextarg;
89         return 0;
90 }
91
92 static int _kdb_bp_remove(kdb_bp_t *bp)
93 {
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;
106 }
107
108 static void kdb_handle_bp(struct pt_regs *regs, kdb_bp_t *bp)
109 {
110         if (KDB_DEBUG(BP))
111                 kdb_printf("regs->ip = 0x%lx\n", instruction_pointer(regs));
112
113         /*
114          * Setup single step
115          */
116         kdb_setsinglestep(regs);
117
118         /*
119          * Reset delay attribute
120          */
121         bp->bp_delay = 0;
122         bp->bp_delayed = 1;
123 }
124
125 static int _kdb_bp_install(struct pt_regs *regs, kdb_bp_t *bp)
126 {
127         int ret;
128         /*
129          * Install the breakpoint, if it is not already installed.
130          */
131
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;
144         }
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;
157         }
158         return 0;
159 }
160
161 /*
162  * kdb_bp_install
163  *
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.
168  */
169 void kdb_bp_install(struct pt_regs *regs)
170 {
171         int i;
172
173         for (i = 0; i < KDB_MAXBPT; i++) {
174                 kdb_bp_t *bp = &kdb_breakpoints[i];
175
176                 if (KDB_DEBUG(BP)) {
177                         kdb_printf("%s: bp %d bp_enabled %d\n",
178                                    __func__, i, bp->bp_enabled);
179                 }
180                 if (bp->bp_enabled)
181                         _kdb_bp_install(regs, bp);
182         }
183 }
184
185 /*
186  * kdb_bp_remove
187  *
188  *      Remove kdb_breakpoints upon entry to the kernel debugger.
189  *
190  * Parameters:
191  *      None.
192  * Outputs:
193  *      None.
194  * Returns:
195  *      None.
196  * Locking:
197  *      None.
198  * Remarks:
199  */
200 void kdb_bp_remove(void)
201 {
202         int i;
203
204         for (i = KDB_MAXBPT - 1; i >= 0; i--) {
205                 kdb_bp_t *bp = &kdb_breakpoints[i];
206
207                 if (KDB_DEBUG(BP)) {
208                         kdb_printf("%s: bp %d bp_enabled %d\n",
209                                    __func__, i, bp->bp_enabled);
210                 }
211                 if (bp->bp_enabled)
212                         _kdb_bp_remove(bp);
213         }
214 }
215
216
217 /*
218  * kdb_printbp
219  *
220  *      Internal function to format and print a breakpoint entry.
221  *
222  * Parameters:
223  *      None.
224  * Outputs:
225  *      None.
226  * Returns:
227  *      None.
228  * Locking:
229  *      None.
230  * Remarks:
231  */
232
233 static void kdb_printbp(kdb_bp_t *bp, int i)
234 {
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);
238
239         if (bp->bp_enabled)
240                 kdb_printf("\n    is enabled");
241         else
242                 kdb_printf("\n    is disabled");
243
244         kdb_printf("\taddr at %016lx, hardtype=%d installed=%d\n",
245                    bp->bp_addr, bp->bp_type, bp->bp_installed);
246
247         kdb_printf("\n");
248 }
249
250 /*
251  * kdb_bp
252  *
253  *      Handle the bp commands.
254  *
255  *      [bp|bph] <addr-expression> [DATAR|DATAW]
256  *
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:
267  *
268  *      bp      Set breakpoint on all cpus.  Only use hardware assist if need.
269  *      bph     Set breakpoint on all cpus.  Force hardware register
270  */
271
272 static int kdb_bp(int argc, const char **argv)
273 {
274         int i, bpno;
275         kdb_bp_t *bp, *bp_check;
276         int diag;
277         int free;
278         char *symname = NULL;
279         long offset = 0ul;
280         int nextarg;
281         kdb_bp_t template = {0};
282
283         if (argc == 0) {
284                 /*
285                  * Display breakpoint table
286                  */
287                 for (bpno = 0, bp = kdb_breakpoints; bpno < KDB_MAXBPT;
288                      bpno++, bp++) {
289                         if (bp->bp_free)
290                                 continue;
291                         kdb_printbp(bp, bpno);
292                 }
293
294                 return 0;
295         }
296
297         nextarg = 1;
298         diag = kdbgetaddrarg(argc, argv, &nextarg, &template.bp_addr,
299                              &offset, &symname);
300         if (diag)
301                 return diag;
302         if (!template.bp_addr)
303                 return KDB_BADINT;
304
305         /*
306          * Find an empty bp structure to allocate
307          */
308         free = KDB_MAXBPT;
309         for (bpno = 0, bp = kdb_breakpoints; bpno < KDB_MAXBPT; bpno++, bp++) {
310                 if (bp->bp_free)
311                         break;
312         }
313
314         if (bpno == KDB_MAXBPT)
315                 return KDB_TOOMANYBPT;
316
317         if (strcmp(argv[0], "bph") == 0) {
318                 template.bp_type = BP_HARDWARE_BREAKPOINT;
319                 diag = kdb_parsebp(argc, argv, &nextarg, &template);
320                 if (diag)
321                         return diag;
322         } else {
323                 template.bp_type = BP_BREAKPOINT;
324         }
325
326         /*
327          * Check for clashing breakpoints.
328          *
329          * Note, in this design we can't have hardware breakpoints
330          * enabled for both read and write on the same address.
331          */
332         for (i = 0, bp_check = kdb_breakpoints; i < KDB_MAXBPT;
333              i++, bp_check++) {
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);
338                         return KDB_DUPBPT;
339                 }
340         }
341
342         template.bp_enabled = 1;
343
344         /*
345          * Actually allocate the breakpoint found earlier
346          */
347         *bp = template;
348         bp->bp_free = 0;
349
350         kdb_printbp(bp, bpno);
351
352         return 0;
353 }
354
355 /*
356  * kdb_bc
357  *
358  *      Handles the 'bc', 'be', and 'bd' commands
359  *
360  *      [bd|bc|be] <breakpoint-number>
361  *      [bd|bc|be] *
362  *
363  * Parameters:
364  *      argc    Count of arguments in argv
365  *      argv    Space delimited command line arguments
366  * Outputs:
367  *      None.
368  * Returns:
369  *      Zero for success, a kdb diagnostic for failure
370  * Locking:
371  *      None.
372  * Remarks:
373  */
374 static int kdb_bc(int argc, const char **argv)
375 {
376         unsigned long addr;
377         kdb_bp_t *bp = NULL;
378         int lowbp = KDB_MAXBPT;
379         int highbp = 0;
380         int done = 0;
381         int i;
382         int diag = 0;
383
384         int cmd;                        /* KDBCMD_B? */
385 #define KDBCMD_BC       0
386 #define KDBCMD_BE       1
387 #define KDBCMD_BD       2
388
389         if (strcmp(argv[0], "be") == 0)
390                 cmd = KDBCMD_BE;
391         else if (strcmp(argv[0], "bd") == 0)
392                 cmd = KDBCMD_BD;
393         else
394                 cmd = KDBCMD_BC;
395
396         if (argc != 1)
397                 return KDB_ARGCOUNT;
398
399         if (strcmp(argv[1], "*") == 0) {
400                 lowbp = 0;
401                 highbp = KDB_MAXBPT;
402         } else {
403                 diag = kdbgetularg(argv[1], &addr);
404                 if (diag)
405                         return diag;
406
407                 /*
408                  * For addresses less than the maximum breakpoint number,
409                  * assume that the breakpoint number is desired.
410                  */
411                 if (addr < KDB_MAXBPT) {
412                         bp = &kdb_breakpoints[addr];
413                         lowbp = highbp = addr;
414                         highbp++;
415                 } else {
416                         for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT;
417                             i++, bp++) {
418                                 if (bp->bp_addr == addr) {
419                                         lowbp = highbp = i;
420                                         highbp++;
421                                         break;
422                                 }
423                         }
424                 }
425         }
426
427         /*
428          * Now operate on the set of breakpoints matching the input
429          * criteria (either '*' for all, or an individual breakpoint).
430          */
431         for (bp = &kdb_breakpoints[lowbp], i = lowbp;
432             i < highbp;
433             i++, bp++) {
434                 if (bp->bp_free)
435                         continue;
436
437                 done++;
438
439                 switch (cmd) {
440                 case KDBCMD_BC:
441                         bp->bp_enabled = 0;
442
443                         kdb_printf("Breakpoint %d at "
444                                    kdb_bfd_vma_fmt " cleared\n",
445                                    i, bp->bp_addr);
446
447                         bp->bp_addr = 0;
448                         bp->bp_free = 1;
449
450                         break;
451                 case KDBCMD_BE:
452                         bp->bp_enabled = 1;
453
454                         kdb_printf("Breakpoint %d at "
455                                    kdb_bfd_vma_fmt " enabled",
456                                    i, bp->bp_addr);
457
458                         kdb_printf("\n");
459                         break;
460                 case KDBCMD_BD:
461                         if (!bp->bp_enabled)
462                                 break;
463
464                         bp->bp_enabled = 0;
465
466                         kdb_printf("Breakpoint %d at "
467                                    kdb_bfd_vma_fmt " disabled\n",
468                                    i, bp->bp_addr);
469
470                         break;
471                 }
472                 if (bp->bp_delay && (cmd == KDBCMD_BC || cmd == KDBCMD_BD)) {
473                         bp->bp_delay = 0;
474                         KDB_STATE_CLEAR(SSBPT);
475                 }
476         }
477
478         return (!done) ? KDB_BPTNOTFOUND : 0;
479 }
480
481 /*
482  * kdb_ss
483  *
484  *      Process the 'ss' (Single Step) and 'ssb' (Single Step to Branch)
485  *      commands.
486  *
487  *      ss
488  *      ssb
489  *
490  * Parameters:
491  *      argc    Argument count
492  *      argv    Argument vector
493  * Outputs:
494  *      None.
495  * Returns:
496  *      KDB_CMD_SS[B] for success, a kdb error if failure.
497  * Locking:
498  *      None.
499  * Remarks:
500  *
501  *      Set the arch specific option to trigger a debug trap after the next
502  *      instruction.
503  *
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
507  *      is encountered.
508  */
509
510 static int kdb_ss(int argc, const char **argv)
511 {
512         int ssb = 0;
513
514         ssb = (strcmp(argv[0], "ssb") == 0);
515         if (argc != 0)
516                 return KDB_ARGCOUNT;
517         /*
518          * Set trace flag and go.
519          */
520         KDB_STATE_SET(DOING_SS);
521         if (ssb) {
522                 KDB_STATE_SET(DOING_SSB);
523                 return KDB_CMD_SSB;
524         }
525         return KDB_CMD_SS;
526 }
527
528 /* Initialize the breakpoint table and register breakpoint commands. */
529
530 void __init kdb_initbptab(void)
531 {
532         int i;
533         kdb_bp_t *bp;
534
535         /*
536          * First time initialization.
537          */
538         memset(&kdb_breakpoints, '\0', sizeof(kdb_breakpoints));
539
540         for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++)
541                 bp->bp_free = 1;
542
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);
556
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);
561         /*
562          * Architecture dependent initialization.
563          */
564 }