Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Kernel Debugger Architecture Independent Breakpoint Handler
0003  *
0004  * This file is subject to the terms and conditions of the GNU General Public
0005  * License.  See the file "COPYING" in the main directory of this archive
0006  * for more details.
0007  *
0008  * Copyright (c) 1999-2004 Silicon Graphics, Inc.  All Rights Reserved.
0009  * Copyright (c) 2009 Wind River Systems, Inc.  All Rights Reserved.
0010  */
0011 
0012 #include <linux/string.h>
0013 #include <linux/kernel.h>
0014 #include <linux/init.h>
0015 #include <linux/kdb.h>
0016 #include <linux/kgdb.h>
0017 #include <linux/smp.h>
0018 #include <linux/sched.h>
0019 #include <linux/interrupt.h>
0020 #include "kdb_private.h"
0021 
0022 /*
0023  * Table of kdb_breakpoints
0024  */
0025 kdb_bp_t kdb_breakpoints[KDB_MAXBPT];
0026 
0027 static void kdb_setsinglestep(struct pt_regs *regs)
0028 {
0029     KDB_STATE_SET(DOING_SS);
0030 }
0031 
0032 static char *kdb_rwtypes[] = {
0033     "Instruction(i)",
0034     "Instruction(Register)",
0035     "Data Write",
0036     "I/O",
0037     "Data Access"
0038 };
0039 
0040 static char *kdb_bptype(kdb_bp_t *bp)
0041 {
0042     if (bp->bp_type < 0 || bp->bp_type > 4)
0043         return "";
0044 
0045     return kdb_rwtypes[bp->bp_type];
0046 }
0047 
0048 static int kdb_parsebp(int argc, const char **argv, int *nextargp, kdb_bp_t *bp)
0049 {
0050     int nextarg = *nextargp;
0051     int diag;
0052 
0053     bp->bph_length = 1;
0054     if ((argc + 1) != nextarg) {
0055         if (strncasecmp(argv[nextarg], "datar", sizeof("datar")) == 0)
0056             bp->bp_type = BP_ACCESS_WATCHPOINT;
0057         else if (strncasecmp(argv[nextarg], "dataw", sizeof("dataw")) == 0)
0058             bp->bp_type = BP_WRITE_WATCHPOINT;
0059         else if (strncasecmp(argv[nextarg], "inst", sizeof("inst")) == 0)
0060             bp->bp_type = BP_HARDWARE_BREAKPOINT;
0061         else
0062             return KDB_ARGCOUNT;
0063 
0064         bp->bph_length = 1;
0065 
0066         nextarg++;
0067 
0068         if ((argc + 1) != nextarg) {
0069             unsigned long len;
0070 
0071             diag = kdbgetularg((char *)argv[nextarg],
0072                        &len);
0073             if (diag)
0074                 return diag;
0075 
0076 
0077             if (len > 8)
0078                 return KDB_BADLENGTH;
0079 
0080             bp->bph_length = len;
0081             nextarg++;
0082         }
0083 
0084         if ((argc + 1) != nextarg)
0085             return KDB_ARGCOUNT;
0086     }
0087 
0088     *nextargp = nextarg;
0089     return 0;
0090 }
0091 
0092 static int _kdb_bp_remove(kdb_bp_t *bp)
0093 {
0094     int ret = 1;
0095     if (!bp->bp_installed)
0096         return ret;
0097     if (!bp->bp_type)
0098         ret = dbg_remove_sw_break(bp->bp_addr);
0099     else
0100         ret = arch_kgdb_ops.remove_hw_breakpoint(bp->bp_addr,
0101              bp->bph_length,
0102              bp->bp_type);
0103     if (ret == 0)
0104         bp->bp_installed = 0;
0105     return ret;
0106 }
0107 
0108 static void kdb_handle_bp(struct pt_regs *regs, kdb_bp_t *bp)
0109 {
0110     if (KDB_DEBUG(BP))
0111         kdb_printf("regs->ip = 0x%lx\n", instruction_pointer(regs));
0112 
0113     /*
0114      * Setup single step
0115      */
0116     kdb_setsinglestep(regs);
0117 
0118     /*
0119      * Reset delay attribute
0120      */
0121     bp->bp_delay = 0;
0122     bp->bp_delayed = 1;
0123 }
0124 
0125 static int _kdb_bp_install(struct pt_regs *regs, kdb_bp_t *bp)
0126 {
0127     int ret;
0128     /*
0129      * Install the breakpoint, if it is not already installed.
0130      */
0131 
0132     if (KDB_DEBUG(BP))
0133         kdb_printf("%s: bp_installed %d\n",
0134                __func__, bp->bp_installed);
0135     if (!KDB_STATE(SSBPT))
0136         bp->bp_delay = 0;
0137     if (bp->bp_installed)
0138         return 1;
0139     if (bp->bp_delay || (bp->bp_delayed && KDB_STATE(DOING_SS))) {
0140         if (KDB_DEBUG(BP))
0141             kdb_printf("%s: delayed bp\n", __func__);
0142         kdb_handle_bp(regs, bp);
0143         return 0;
0144     }
0145     if (!bp->bp_type)
0146         ret = dbg_set_sw_break(bp->bp_addr);
0147     else
0148         ret = arch_kgdb_ops.set_hw_breakpoint(bp->bp_addr,
0149              bp->bph_length,
0150              bp->bp_type);
0151     if (ret == 0) {
0152         bp->bp_installed = 1;
0153     } else {
0154         kdb_printf("%s: failed to set breakpoint at 0x%lx\n",
0155                __func__, bp->bp_addr);
0156         if (!bp->bp_type) {
0157             kdb_printf("Software breakpoints are unavailable.\n"
0158                    "  Boot the kernel with rodata=off\n"
0159                    "  OR use hw breaks: help bph\n");
0160         }
0161         return 1;
0162     }
0163     return 0;
0164 }
0165 
0166 /*
0167  * kdb_bp_install
0168  *
0169  *  Install kdb_breakpoints prior to returning from the
0170  *  kernel debugger.  This allows the kdb_breakpoints to be set
0171  *  upon functions that are used internally by kdb, such as
0172  *  printk().  This function is only called once per kdb session.
0173  */
0174 void kdb_bp_install(struct pt_regs *regs)
0175 {
0176     int i;
0177 
0178     for (i = 0; i < KDB_MAXBPT; i++) {
0179         kdb_bp_t *bp = &kdb_breakpoints[i];
0180 
0181         if (KDB_DEBUG(BP)) {
0182             kdb_printf("%s: bp %d bp_enabled %d\n",
0183                    __func__, i, bp->bp_enabled);
0184         }
0185         if (bp->bp_enabled)
0186             _kdb_bp_install(regs, bp);
0187     }
0188 }
0189 
0190 /*
0191  * kdb_bp_remove
0192  *
0193  *  Remove kdb_breakpoints upon entry to the kernel debugger.
0194  *
0195  * Parameters:
0196  *  None.
0197  * Outputs:
0198  *  None.
0199  * Returns:
0200  *  None.
0201  * Locking:
0202  *  None.
0203  * Remarks:
0204  */
0205 void kdb_bp_remove(void)
0206 {
0207     int i;
0208 
0209     for (i = KDB_MAXBPT - 1; i >= 0; i--) {
0210         kdb_bp_t *bp = &kdb_breakpoints[i];
0211 
0212         if (KDB_DEBUG(BP)) {
0213             kdb_printf("%s: bp %d bp_enabled %d\n",
0214                    __func__, i, bp->bp_enabled);
0215         }
0216         if (bp->bp_enabled)
0217             _kdb_bp_remove(bp);
0218     }
0219 }
0220 
0221 
0222 /*
0223  * kdb_printbp
0224  *
0225  *  Internal function to format and print a breakpoint entry.
0226  *
0227  * Parameters:
0228  *  None.
0229  * Outputs:
0230  *  None.
0231  * Returns:
0232  *  None.
0233  * Locking:
0234  *  None.
0235  * Remarks:
0236  */
0237 
0238 static void kdb_printbp(kdb_bp_t *bp, int i)
0239 {
0240     kdb_printf("%s ", kdb_bptype(bp));
0241     kdb_printf("BP #%d at ", i);
0242     kdb_symbol_print(bp->bp_addr, NULL, KDB_SP_DEFAULT);
0243 
0244     if (bp->bp_enabled)
0245         kdb_printf("\n    is enabled ");
0246     else
0247         kdb_printf("\n    is disabled");
0248 
0249     kdb_printf("  addr at %016lx, hardtype=%d installed=%d\n",
0250            bp->bp_addr, bp->bp_type, bp->bp_installed);
0251 
0252     kdb_printf("\n");
0253 }
0254 
0255 /*
0256  * kdb_bp
0257  *
0258  *  Handle the bp commands.
0259  *
0260  *  [bp|bph] <addr-expression> [DATAR|DATAW]
0261  *
0262  * Parameters:
0263  *  argc    Count of arguments in argv
0264  *  argv    Space delimited command line arguments
0265  * Outputs:
0266  *  None.
0267  * Returns:
0268  *  Zero for success, a kdb diagnostic if failure.
0269  * Locking:
0270  *  None.
0271  * Remarks:
0272  *
0273  *  bp  Set breakpoint on all cpus.  Only use hardware assist if need.
0274  *  bph Set breakpoint on all cpus.  Force hardware register
0275  */
0276 
0277 static int kdb_bp(int argc, const char **argv)
0278 {
0279     int i, bpno;
0280     kdb_bp_t *bp, *bp_check;
0281     int diag;
0282     char *symname = NULL;
0283     long offset = 0ul;
0284     int nextarg;
0285     kdb_bp_t template = {0};
0286 
0287     if (argc == 0) {
0288         /*
0289          * Display breakpoint table
0290          */
0291         for (bpno = 0, bp = kdb_breakpoints; bpno < KDB_MAXBPT;
0292              bpno++, bp++) {
0293             if (bp->bp_free)
0294                 continue;
0295             kdb_printbp(bp, bpno);
0296         }
0297 
0298         return 0;
0299     }
0300 
0301     nextarg = 1;
0302     diag = kdbgetaddrarg(argc, argv, &nextarg, &template.bp_addr,
0303                  &offset, &symname);
0304     if (diag)
0305         return diag;
0306     if (!template.bp_addr)
0307         return KDB_BADINT;
0308 
0309     /*
0310      * This check is redundant (since the breakpoint machinery should
0311      * be doing the same check during kdb_bp_install) but gives the
0312      * user immediate feedback.
0313      */
0314     diag = kgdb_validate_break_address(template.bp_addr);
0315     if (diag)
0316         return diag;
0317 
0318     /*
0319      * Find an empty bp structure to allocate
0320      */
0321     for (bpno = 0, bp = kdb_breakpoints; bpno < KDB_MAXBPT; bpno++, bp++) {
0322         if (bp->bp_free)
0323             break;
0324     }
0325 
0326     if (bpno == KDB_MAXBPT)
0327         return KDB_TOOMANYBPT;
0328 
0329     if (strcmp(argv[0], "bph") == 0) {
0330         template.bp_type = BP_HARDWARE_BREAKPOINT;
0331         diag = kdb_parsebp(argc, argv, &nextarg, &template);
0332         if (diag)
0333             return diag;
0334     } else {
0335         template.bp_type = BP_BREAKPOINT;
0336     }
0337 
0338     /*
0339      * Check for clashing breakpoints.
0340      *
0341      * Note, in this design we can't have hardware breakpoints
0342      * enabled for both read and write on the same address.
0343      */
0344     for (i = 0, bp_check = kdb_breakpoints; i < KDB_MAXBPT;
0345          i++, bp_check++) {
0346         if (!bp_check->bp_free &&
0347             bp_check->bp_addr == template.bp_addr) {
0348             kdb_printf("You already have a breakpoint at "
0349                    kdb_bfd_vma_fmt0 "\n", template.bp_addr);
0350             return KDB_DUPBPT;
0351         }
0352     }
0353 
0354     template.bp_enabled = 1;
0355 
0356     /*
0357      * Actually allocate the breakpoint found earlier
0358      */
0359     *bp = template;
0360     bp->bp_free = 0;
0361 
0362     kdb_printbp(bp, bpno);
0363 
0364     return 0;
0365 }
0366 
0367 /*
0368  * kdb_bc
0369  *
0370  *  Handles the 'bc', 'be', and 'bd' commands
0371  *
0372  *  [bd|bc|be] <breakpoint-number>
0373  *  [bd|bc|be] *
0374  *
0375  * Parameters:
0376  *  argc    Count of arguments in argv
0377  *  argv    Space delimited command line arguments
0378  * Outputs:
0379  *  None.
0380  * Returns:
0381  *  Zero for success, a kdb diagnostic for failure
0382  * Locking:
0383  *  None.
0384  * Remarks:
0385  */
0386 static int kdb_bc(int argc, const char **argv)
0387 {
0388     unsigned long addr;
0389     kdb_bp_t *bp = NULL;
0390     int lowbp = KDB_MAXBPT;
0391     int highbp = 0;
0392     int done = 0;
0393     int i;
0394     int diag = 0;
0395 
0396     int cmd;            /* KDBCMD_B? */
0397 #define KDBCMD_BC   0
0398 #define KDBCMD_BE   1
0399 #define KDBCMD_BD   2
0400 
0401     if (strcmp(argv[0], "be") == 0)
0402         cmd = KDBCMD_BE;
0403     else if (strcmp(argv[0], "bd") == 0)
0404         cmd = KDBCMD_BD;
0405     else
0406         cmd = KDBCMD_BC;
0407 
0408     if (argc != 1)
0409         return KDB_ARGCOUNT;
0410 
0411     if (strcmp(argv[1], "*") == 0) {
0412         lowbp = 0;
0413         highbp = KDB_MAXBPT;
0414     } else {
0415         diag = kdbgetularg(argv[1], &addr);
0416         if (diag)
0417             return diag;
0418 
0419         /*
0420          * For addresses less than the maximum breakpoint number,
0421          * assume that the breakpoint number is desired.
0422          */
0423         if (addr < KDB_MAXBPT) {
0424             lowbp = highbp = addr;
0425             highbp++;
0426         } else {
0427             for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT;
0428                 i++, bp++) {
0429                 if (bp->bp_addr == addr) {
0430                     lowbp = highbp = i;
0431                     highbp++;
0432                     break;
0433                 }
0434             }
0435         }
0436     }
0437 
0438     /*
0439      * Now operate on the set of breakpoints matching the input
0440      * criteria (either '*' for all, or an individual breakpoint).
0441      */
0442     for (bp = &kdb_breakpoints[lowbp], i = lowbp;
0443         i < highbp;
0444         i++, bp++) {
0445         if (bp->bp_free)
0446             continue;
0447 
0448         done++;
0449 
0450         switch (cmd) {
0451         case KDBCMD_BC:
0452             bp->bp_enabled = 0;
0453 
0454             kdb_printf("Breakpoint %d at "
0455                    kdb_bfd_vma_fmt " cleared\n",
0456                    i, bp->bp_addr);
0457 
0458             bp->bp_addr = 0;
0459             bp->bp_free = 1;
0460 
0461             break;
0462         case KDBCMD_BE:
0463             bp->bp_enabled = 1;
0464 
0465             kdb_printf("Breakpoint %d at "
0466                    kdb_bfd_vma_fmt " enabled",
0467                    i, bp->bp_addr);
0468 
0469             kdb_printf("\n");
0470             break;
0471         case KDBCMD_BD:
0472             if (!bp->bp_enabled)
0473                 break;
0474 
0475             bp->bp_enabled = 0;
0476 
0477             kdb_printf("Breakpoint %d at "
0478                    kdb_bfd_vma_fmt " disabled\n",
0479                    i, bp->bp_addr);
0480 
0481             break;
0482         }
0483         if (bp->bp_delay && (cmd == KDBCMD_BC || cmd == KDBCMD_BD)) {
0484             bp->bp_delay = 0;
0485             KDB_STATE_CLEAR(SSBPT);
0486         }
0487     }
0488 
0489     return (!done) ? KDB_BPTNOTFOUND : 0;
0490 }
0491 
0492 /*
0493  * kdb_ss
0494  *
0495  *  Process the 'ss' (Single Step) command.
0496  *
0497  *  ss
0498  *
0499  * Parameters:
0500  *  argc    Argument count
0501  *  argv    Argument vector
0502  * Outputs:
0503  *  None.
0504  * Returns:
0505  *  KDB_CMD_SS for success, a kdb error if failure.
0506  * Locking:
0507  *  None.
0508  * Remarks:
0509  *
0510  *  Set the arch specific option to trigger a debug trap after the next
0511  *  instruction.
0512  */
0513 
0514 static int kdb_ss(int argc, const char **argv)
0515 {
0516     if (argc != 0)
0517         return KDB_ARGCOUNT;
0518     /*
0519      * Set trace flag and go.
0520      */
0521     KDB_STATE_SET(DOING_SS);
0522     return KDB_CMD_SS;
0523 }
0524 
0525 static kdbtab_t bptab[] = {
0526     {   .name = "bp",
0527         .func = kdb_bp,
0528         .usage = "[<vaddr>]",
0529         .help = "Set/Display breakpoints",
0530         .flags = KDB_ENABLE_FLOW_CTRL | KDB_REPEAT_NO_ARGS,
0531     },
0532     {   .name = "bl",
0533         .func = kdb_bp,
0534         .usage = "[<vaddr>]",
0535         .help = "Display breakpoints",
0536         .flags = KDB_ENABLE_FLOW_CTRL | KDB_REPEAT_NO_ARGS,
0537     },
0538     {   .name = "bc",
0539         .func = kdb_bc,
0540         .usage = "<bpnum>",
0541         .help = "Clear Breakpoint",
0542         .flags = KDB_ENABLE_FLOW_CTRL,
0543     },
0544     {   .name = "be",
0545         .func = kdb_bc,
0546         .usage = "<bpnum>",
0547         .help = "Enable Breakpoint",
0548         .flags = KDB_ENABLE_FLOW_CTRL,
0549     },
0550     {   .name = "bd",
0551         .func = kdb_bc,
0552         .usage = "<bpnum>",
0553         .help = "Disable Breakpoint",
0554         .flags = KDB_ENABLE_FLOW_CTRL,
0555     },
0556     {   .name = "ss",
0557         .func = kdb_ss,
0558         .usage = "",
0559         .help = "Single Step",
0560         .minlen = 1,
0561         .flags = KDB_ENABLE_FLOW_CTRL | KDB_REPEAT_NO_ARGS,
0562     },
0563 };
0564 
0565 static kdbtab_t bphcmd = {
0566     .name = "bph",
0567     .func = kdb_bp,
0568     .usage = "[<vaddr>]",
0569     .help = "[datar [length]|dataw [length]]   Set hw brk",
0570     .flags = KDB_ENABLE_FLOW_CTRL | KDB_REPEAT_NO_ARGS,
0571 };
0572 
0573 /* Initialize the breakpoint table and register breakpoint commands. */
0574 
0575 void __init kdb_initbptab(void)
0576 {
0577     int i;
0578     kdb_bp_t *bp;
0579 
0580     /*
0581      * First time initialization.
0582      */
0583     memset(&kdb_breakpoints, '\0', sizeof(kdb_breakpoints));
0584 
0585     for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++)
0586         bp->bp_free = 1;
0587 
0588     kdb_register_table(bptab, ARRAY_SIZE(bptab));
0589     if (arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT)
0590         kdb_register_table(&bphcmd, 1);
0591 }