Drop obsoleted patch.
[linux-flexiantxendom0-3.2.10.git] / 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  */
10
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>
19
20 /*
21  * Table of kdb_breakpoints
22  */
23 kdb_bp_t kdb_breakpoints[KDB_MAXBPT];
24
25 /*
26  * kdb_bp_install_global
27  *
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
31  *      printk().
32  *
33  * Parameters:
34  *      regs    Execution frame.
35  * Outputs:
36  *      None.
37  * Returns:
38  *      None.
39  * Locking:
40  *      None.
41  * Remarks:
42  *
43  *      This function is only called once per kdb session.
44  */
45
46 void
47 kdb_bp_install_global(struct pt_regs *regs)
48 {
49         int i;
50
51         for(i=0; i<KDB_MAXBPT; i++) {
52                 if (KDB_DEBUG(BP)) {
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);
55                 }
56                 if (kdb_breakpoints[i].bp_enabled
57                  && kdb_breakpoints[i].bp_global) {
58                         kdba_installbp(regs, &kdb_breakpoints[i]);
59                 }
60         }
61 }
62
63 /*
64  * kdb_bp_install_local
65  *
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
69  *      printk().
70  *
71  * Parameters:
72  *      regs    Execution frame.
73  * Outputs:
74  *      None.
75  * Returns:
76  *      None.
77  * Locking:
78  *      None.
79  * Remarks:
80  *
81  *      This function is called once per processor.
82  */
83
84 void
85 kdb_bp_install_local(struct pt_regs *regs)
86 {
87         int i;
88
89         for(i=0; i<KDB_MAXBPT; i++) {
90                 if (KDB_DEBUG(BP)) {
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);
94                 }
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]);
99                 }
100         }
101 }
102
103 /*
104  * kdb_bp_remove_global
105  *
106  *      Remove global kdb_breakpoints upon entry to the kernel debugger.
107  *
108  * Parameters:
109  *      None.
110  * Outputs:
111  *      None.
112  * Returns:
113  *      None.
114  * Locking:
115  *      None.
116  * Remarks:
117  */
118
119 void
120 kdb_bp_remove_global(void)
121 {
122         int i;
123
124         for(i=KDB_MAXBPT-1; i>=0; i--) {
125                 if (KDB_DEBUG(BP)) {
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);
128                 }
129                 if (kdb_breakpoints[i].bp_enabled
130                  && kdb_breakpoints[i].bp_global) {
131                         kdba_removebp(&kdb_breakpoints[i]);
132                 }
133         }
134 }
135
136
137 /*
138  * kdb_bp_remove_local
139  *
140  *      Remove local kdb_breakpoints upon entry to the kernel debugger.
141  *
142  * Parameters:
143  *      None.
144  * Outputs:
145  *      None.
146  * Returns:
147  *      None.
148  * Locking:
149  *      None.
150  * Remarks:
151  */
152
153 void
154 kdb_bp_remove_local(void)
155 {
156         int i;
157
158         for(i=KDB_MAXBPT-1; i>=0; i--) {
159                 if (KDB_DEBUG(BP)) {
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);
163                 }
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]);
168                 }
169         }
170 }
171
172 /*
173  * kdb_printbp
174  *
175  *      Internal function to format and print a breakpoint entry.
176  *
177  * Parameters:
178  *      None.
179  * Outputs:
180  *      None.
181  * Returns:
182  *      None.
183  * Locking:
184  *      None.
185  * Remarks:
186  */
187
188 static void
189 kdb_printbp(kdb_bp_t *bp, int i)
190 {
191         if (bp->bp_forcehw) {
192                 kdb_printf("Forced ");
193         }
194
195         if (!bp->bp_template.bph_free) {
196                 kdb_printf("%s ", kdba_bptype(&bp->bp_template));
197         } else {
198                 kdb_printf("Instruction(i) ");
199         }
200
201         kdb_printf("BP #%d at ", i);
202         kdb_symbol_print(bp->bp_addr, NULL, KDB_SP_DEFAULT);
203
204         if (bp->bp_enabled) {
205                 kdba_printbp(bp);
206                 if (bp->bp_global)
207                         kdb_printf(" globally");
208                 else
209                         kdb_printf(" on cpu %d", bp->bp_cpu);
210                 if (bp->bp_adjust)
211                         kdb_printf(" adjust %d", bp->bp_adjust);
212         } else {
213                 kdb_printf("\n    is disabled");
214         }
215
216         kdb_printf("\n");
217 }
218
219 /*
220  * kdb_bp
221  *
222  *      Handle the bp, and bpa commands.
223  *
224  *      [bp|bpa|bph] <addr-expression> [DATAR|DATAW|IO [length]]
225  *
226  * Parameters:
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
231  * Outputs:
232  *      None.
233  * Returns:
234  *      Zero for success, a kdb diagnostic if failure.
235  * Locking:
236  *      None.
237  * Remarks:
238  *
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
243  */
244
245 static int
246 kdb_bp(int argc, const char **argv, const char **envp, struct pt_regs *regs)
247 {
248         int i, bpno;
249         kdb_bp_t *bp, *bp_check;
250         int diag;
251         int free;
252         kdb_machreg_t addr;
253         char *symname = NULL;
254         long offset = 0ul;
255         int nextarg;
256         int hardware;
257         int global;
258
259         if (argc == 0) {
260                 /*
261                  * Display breakpoint table
262                  */
263                 for(bpno=0,bp=kdb_breakpoints; bpno<KDB_MAXBPT; bpno++, bp++) {
264                         if (bp->bp_free) continue;
265
266                         kdb_printbp(bp, bpno);
267                 }
268
269                 return 0;
270         }
271
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));
276
277         nextarg = 1;
278         diag = kdbgetaddrarg(argc, argv, &nextarg, &addr, &offset, &symname, regs);
279         if (diag)
280                 return diag;
281         if (!addr)
282                 return KDB_BADINT;
283
284         /*
285          * Allocate a new bp structure
286          */
287         free = KDB_MAXBPT;
288         for(bpno=0,bp=kdb_breakpoints; bpno<KDB_MAXBPT; bpno++,bp++) {
289                 if (bp->bp_free) {
290                         break;
291                 }
292         }
293
294         if (bpno == KDB_MAXBPT)
295                 return KDB_TOOMANYBPT;
296
297         memset(bp, 0, sizeof(*bp));
298         bp->bp_free = 1;
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);
303                         return KDB_DUPBPT;
304                 }
305         }
306         bp->bp_addr = addr;
307         bp->bp_free = 0;
308
309         bp->bp_forcehw = hardware;
310         if (KDB_DEBUG(BP))
311                 kdb_printf("kdb_bp: forcehw is %d hardware is %d\n", bp->bp_forcehw, hardware);
312
313         /*
314          * Handle architecture dependent parsing
315          */
316         diag = kdba_parsebp(argc, argv, &nextarg, bp);
317         if (diag) {
318                 return diag;
319         }
320
321         bp->bp_enabled = 1;
322         bp->bp_global = 1;      /* Most breakpoints are global */
323
324         if (hardware && !global) {
325                 bp->bp_global = 0;
326                 bp->bp_cpu = smp_processor_id();
327         }
328
329         /*
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.
334          */
335         if (!bp->bp_template.bph_free) {
336                 bp->bp_hard = kdba_allocbp(&bp->bp_template, &diag);
337                 if (diag) {
338                         bp->bp_enabled = 0;
339                         return diag;
340                 }
341                 bp->bp_hardtype = 1;
342         }
343
344         kdb_printbp(bp, bpno);
345
346         return 0;
347 }
348
349 /*
350  * kdb_bc
351  *
352  *      Handles the 'bc', 'be', and 'bd' commands
353  *
354  *      [bd|bc|be] <breakpoint-number>
355  *      [bd|bc|be] *
356  *
357  * Parameters:
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
362  * Outputs:
363  *      None.
364  * Returns:
365  *      Zero for success, a kdb diagnostic for failure
366  * Locking:
367  *      None.
368  * Remarks:
369  */
370
371 #define KDBCMD_BC       0
372 #define KDBCMD_BE       1
373 #define KDBCMD_BD       2
374
375 static int
376 kdb_bc(int argc, const char **argv, const char **envp, struct pt_regs *regs)
377 {
378         kdb_machreg_t addr;
379         kdb_bp_t *bp = 0;
380         int lowbp = KDB_MAXBPT;
381         int highbp = 0;
382         int done = 0;
383         int i;
384         int diag;
385         int cmd;                        /* KDBCMD_B? */
386
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;
393
394         if (argc != 1)
395                 return KDB_ARGCOUNT;
396
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;
404
405                 /*
406                  * For addresses less than the maximum breakpoint number,
407                  * assume that the breakpoint number is desired.
408                  */
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; i++, bp++) {
415                                 if (bp->bp_addr == addr) {
416                                         lowbp = highbp = i;
417                                         highbp++;
418                                         break;
419                                 }
420                         }
421                 }
422         }
423
424         /*
425          * Now operate on the set of breakpoints matching the input
426          * criteria (either '*' for all, or an individual breakpoint).
427          */
428         for(bp=&kdb_breakpoints[lowbp], i=lowbp;
429             i < highbp;
430             i++, bp++) {
431                 if (bp->bp_free)
432                         continue;
433
434                 done++;
435
436                 switch (cmd) {
437                 case KDBCMD_BC:
438                         if (bp->bp_hardtype) {
439                                 kdba_freebp(bp->bp_hard);
440                                 bp->bp_hard = 0;
441                                 bp->bp_hardtype = 0;
442                         }
443
444                         bp->bp_enabled = 0;
445                         bp->bp_global = 0;
446
447                         kdb_printf("Breakpoint %d at " kdb_bfd_vma_fmt " cleared\n",
448                                 i, bp->bp_addr);
449
450                         bp->bp_addr = 0;
451                         bp->bp_free = 1;
452
453                         break;
454                 case KDBCMD_BE:
455                         /*
456                          * Allocate a hardware breakpoint.  If one is not
457                          * available, don't enable the breakpoint.
458                          */
459                         if (!bp->bp_template.bph_free
460                          && !bp->bp_hardtype) {
461                                 bp->bp_hard = kdba_allocbp(&bp->bp_template, &diag);
462                                 if (diag) {
463                                         bp->bp_enabled = 0;
464                                         return diag;
465                                 }
466                                 bp->bp_hardtype = 1;
467                         }
468
469                         bp->bp_enabled = 1;
470
471                         kdb_printf("Breakpoint %d at " kdb_bfd_vma_fmt " enabled",
472                                 i, bp->bp_addr);
473
474                         kdb_printf("\n");
475                         break;
476                 case KDBCMD_BD:
477                         if (!bp->bp_enabled) {
478                                 return 0;
479                         }
480
481                         /*
482                          * Since this breakpoint is now disabled, we can
483                          * give up the hardware register which is allocated
484                          * to it.
485                          */
486                         if (bp->bp_hardtype) {
487                                 kdba_freebp(bp->bp_hard);
488                                 bp->bp_hard = 0;
489                                 bp->bp_hardtype = 0;
490                         }
491
492                         bp->bp_enabled = 0;
493
494                         kdb_printf("Breakpoint %d at " kdb_bfd_vma_fmt " disabled\n",
495                                 i, bp->bp_addr);
496
497                         break;
498                 }
499                 if (bp->bp_delay && (cmd == KDBCMD_BC || cmd == KDBCMD_BD)) {
500                         bp->bp_delay = 0;
501                         KDB_STATE_CLEAR(SSBPT);
502                 }
503         }
504
505         return (!done)?KDB_BPTNOTFOUND:0;
506 }
507
508 /*
509  * kdb_ss
510  *
511  *      Process the 'ss' (Single Step) and 'ssb' (Single Step to Branch)
512  *      commands.
513  *
514  *      ss
515  *      ssb
516  *
517  * Parameters:
518  *      argc    Argument count
519  *      argv    Argument vector
520  *      envp    Environment vector
521  *      regs    Registers at time of entry to kernel debugger
522  * Outputs:
523  *      None.
524  * Returns:
525  *      KDB_CMD_SS[B] for success, a kdb error if failure.
526  * Locking:
527  *      None.
528  * Remarks:
529  *
530  *      Set the arch specific option to trigger a debug trap after the next
531  *      instruction.
532  *
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
536  *      is encountered.
537  */
538
539 static int
540 kdb_ss(int argc, const char **argv, const char **envp, struct pt_regs *regs)
541 {
542         int ssb = 0;
543
544         ssb = (strcmp(argv[0], "ssb") == 0);
545         if (argc != 0)
546                 return KDB_ARGCOUNT;
547
548         if (!regs) {
549                 kdb_printf("%s: pt_regs not available\n", __FUNCTION__);
550                 return KDB_BADREG;
551         }
552
553         /*
554          * Set trace flag and go.
555          */
556         KDB_STATE_SET(DOING_SS);
557         if (ssb)
558                 KDB_STATE_SET(DOING_SSB);
559
560         kdba_setsinglestep(regs);               /* Enable single step */
561
562         if (ssb)
563                 return KDB_CMD_SSB;
564         return KDB_CMD_SS;
565 }
566
567 /*
568  * kdb_initbptab
569  *
570  *      Initialize the breakpoint table.  Register breakpoint commands.
571  *
572  * Parameters:
573  *      None.
574  * Outputs:
575  *      None.
576  * Returns:
577  *      None.
578  * Locking:
579  *      None.
580  * Remarks:
581  */
582
583 void __init
584 kdb_initbptab(void)
585 {
586         int i;
587         kdb_bp_t *bp;
588
589         /*
590          * First time initialization.
591          */
592         memset(&kdb_breakpoints, '\0', sizeof(kdb_breakpoints));
593
594         for (i=0, bp=kdb_breakpoints; i<KDB_MAXBPT; i++, bp++) {
595                 bp->bp_free = 1;
596                 /*
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.
601                  *
602                  * The rest of the template is reserved to the architecture
603                  * dependent code and _must_ not be touched by the architecture
604                  * independent code.
605                  */
606                 bp->bp_template.bph_free = 1;
607         }
608
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);
617
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);
620         /*
621          * Architecture dependent initialization.
622          */
623         kdba_initbp();
624 }