Subversion Repositories HelenOS-historic

Rev

Rev 1196 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (C) 2005 Ondrej Palkovsky
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29.  /** @addtogroup mips32debug mips32
  30.  * @ingroup debug
  31.  * @{
  32.  */
  33. /** @file
  34.  */
  35.  
  36. #include <arch/debugger.h>
  37. #include <memstr.h>
  38. #include <console/kconsole.h>
  39. #include <console/cmd.h>
  40. #include <symtab.h>
  41. #include <print.h>
  42. #include <panic.h>
  43. #include <arch.h>
  44. #include <arch/cp0.h>
  45. #include <func.h>
  46.  
  47. bpinfo_t breakpoints[BKPOINTS_MAX];
  48. SPINLOCK_INITIALIZE(bkpoint_lock);
  49.  
  50. static int cmd_print_breakpoints(cmd_arg_t *argv);
  51. static cmd_info_t bkpts_info = {
  52.     .name = "bkpts",
  53.     .description = "Print breakpoint table.",
  54.     .func = cmd_print_breakpoints,
  55.     .argc = 0,
  56. };
  57.  
  58. static int cmd_del_breakpoint(cmd_arg_t *argv);
  59. static cmd_arg_t del_argv = {
  60.     .type = ARG_TYPE_INT
  61. };
  62. static cmd_info_t delbkpt_info = {
  63.     .name = "delbkpt",
  64.     .description = "delbkpt <number> - Delete breakpoint.",
  65.     .func = cmd_del_breakpoint,
  66.     .argc = 1,
  67.     .argv = &del_argv
  68. };
  69.  
  70. static int cmd_add_breakpoint(cmd_arg_t *argv);
  71. static cmd_arg_t add_argv = {
  72.     .type = ARG_TYPE_INT
  73. };
  74. static cmd_info_t addbkpt_info = {
  75.     .name = "addbkpt",
  76.     .description = "addbkpt <&symbol> - new bkpoint. Break on J/Branch insts unsupported.",
  77.     .func = cmd_add_breakpoint,
  78.     .argc = 1,
  79.     .argv = &add_argv
  80. };
  81.  
  82. static cmd_arg_t adde_argv[] = {
  83.     { .type = ARG_TYPE_INT },
  84.     { .type = ARG_TYPE_INT }
  85. };
  86. static cmd_info_t addbkpte_info = {
  87.     .name = "addbkpte",
  88.     .description = "addebkpte <&symbol> <&func> - new bkpoint. Call func(or Nothing if 0).",
  89.     .func = cmd_add_breakpoint,
  90.     .argc = 2,
  91.     .argv = adde_argv
  92. };
  93.  
  94. static struct {
  95.     __u32 andmask;
  96.     __u32 value;
  97. }jmpinstr[] = {
  98.     {0xf3ff0000, 0x41000000}, /* BCzF */
  99.     {0xf3ff0000, 0x41020000}, /* BCzFL */
  100.     {0xf3ff0000, 0x41010000}, /* BCzT */
  101.     {0xf3ff0000, 0x41030000}, /* BCzTL */
  102.     {0xfc000000, 0x10000000}, /* BEQ */
  103.     {0xfc000000, 0x50000000}, /* BEQL */
  104.     {0xfc1f0000, 0x04010000}, /* BEQL */
  105.     {0xfc1f0000, 0x04110000}, /* BGEZAL */
  106.     {0xfc1f0000, 0x04130000}, /* BGEZALL */
  107.     {0xfc1f0000, 0x04030000}, /* BGEZL */
  108.     {0xfc1f0000, 0x1c000000}, /* BGTZ */
  109.     {0xfc1f0000, 0x5c000000}, /* BGTZL */
  110.     {0xfc1f0000, 0x18000000}, /* BLEZ */
  111.     {0xfc1f0000, 0x58000000}, /* BLEZL */
  112.     {0xfc1f0000, 0x04000000}, /* BLTZ */
  113.     {0xfc1f0000, 0x04100000}, /* BLTZAL */
  114.     {0xfc1f0000, 0x04120000}, /* BLTZALL */
  115.     {0xfc1f0000, 0x04020000}, /* BLTZL */
  116.     {0xfc000000, 0x14000000}, /* BNE */
  117.     {0xfc000000, 0x54000000}, /* BNEL */
  118.     {0xfc000000, 0x08000000}, /* J */
  119.     {0xfc000000, 0x0c000000}, /* JAL */
  120.     {0xfc1f07ff, 0x00000009}, /* JALR */
  121.     {0,0} /* EndOfTable */
  122. };
  123.  
  124. /** Test, if the given instruction is a jump or branch instruction
  125.  *
  126.  * @param instr Instruction code
  127.  * @return true - it is jump instruction, false otherwise
  128.  */
  129. static bool is_jump(__native instr)
  130. {
  131.     int i;
  132.  
  133.     for (i=0;jmpinstr[i].andmask;i++) {
  134.         if ((instr & jmpinstr[i].andmask) == jmpinstr[i].value)
  135.             return true;
  136.     }
  137.  
  138.     return false;
  139. }
  140.  
  141. /** Add new breakpoint to table */
  142. int cmd_add_breakpoint(cmd_arg_t *argv)
  143. {
  144.     bpinfo_t *cur = NULL;
  145.     ipl_t ipl;
  146.     int i;
  147.  
  148.     if (argv->intval & 0x3) {
  149.         printf("Not aligned instruction, forgot to use &symbol?\n");
  150.         return 1;
  151.     }
  152.     ipl = interrupts_disable();
  153.     spinlock_lock(&bkpoint_lock);
  154.  
  155.     /* Check, that the breakpoints do not conflict */
  156.     for (i=0; i<BKPOINTS_MAX; i++) {
  157.         if (breakpoints[i].address == (__address)argv->intval) {
  158.             printf("Duplicate breakpoint %d.\n", i);
  159.             spinlock_unlock(&bkpoints_lock);
  160.             return 0;
  161.         } else if (breakpoints[i].address == (__address)argv->intval + sizeof(__native) || \
  162.                breakpoints[i].address == (__address)argv->intval - sizeof(__native)) {
  163.             printf("Adjacent breakpoints not supported, conflict with %d.\n", i);
  164.             spinlock_unlock(&bkpoints_lock);
  165.             return 0;
  166.         }
  167.            
  168.     }
  169.  
  170.     for (i=0; i<BKPOINTS_MAX; i++)
  171.         if (!breakpoints[i].address) {
  172.             cur = &breakpoints[i];
  173.             break;
  174.         }
  175.     if (!cur) {
  176.         printf("Too many breakpoints.\n");
  177.         spinlock_unlock(&bkpoint_lock);
  178.         interrupts_restore(ipl);
  179.         return 0;
  180.     }
  181.     cur->address = (__address) argv->intval;
  182.     printf("Adding breakpoint on address: %p\n", argv->intval);
  183.     cur->instruction = ((__native *)cur->address)[0];
  184.     cur->nextinstruction = ((__native *)cur->address)[1];
  185.     if (argv == &add_argv) {
  186.         cur->flags = 0;
  187.     } else { /* We are add extended */
  188.         cur->flags = BKPOINT_FUNCCALL;
  189.         cur->bkfunc =   (void (*)(void *, istate_t *)) argv[1].intval;
  190.     }
  191.     if (is_jump(cur->instruction))
  192.         cur->flags |= BKPOINT_ONESHOT;
  193.     cur->counter = 0;
  194.  
  195.     /* Set breakpoint */
  196.     *((__native *)cur->address) = 0x0d;
  197.  
  198.     spinlock_unlock(&bkpoint_lock);
  199.     interrupts_restore(ipl);
  200.  
  201.     return 1;
  202. }
  203.  
  204.  
  205.  
  206. /** Remove breakpoint from table */
  207. int cmd_del_breakpoint(cmd_arg_t *argv)
  208. {
  209.     bpinfo_t *cur;
  210.     ipl_t ipl;
  211.  
  212.     if (argv->intval < 0 || argv->intval > BKPOINTS_MAX) {
  213.         printf("Invalid breakpoint number.\n");
  214.         return 0;
  215.     }
  216.     ipl = interrupts_disable();
  217.     spinlock_lock(&bkpoint_lock);
  218.  
  219.     cur = &breakpoints[argv->intval];
  220.     if (!cur->address) {
  221.         printf("Breakpoint does not exist.\n");
  222.         spinlock_unlock(&bkpoint_lock);
  223.         interrupts_restore(ipl);
  224.         return 0;
  225.     }
  226.     if ((cur->flags & BKPOINT_INPROG) && (cur->flags & BKPOINT_ONESHOT)) {
  227.         printf("Cannot remove one-shot breakpoint in-progress\n");
  228.         spinlock_unlock(&bkpoint_lock);
  229.         interrupts_restore(ipl);
  230.         return 0;
  231.     }
  232.     ((__u32 *)cur->address)[0] = cur->instruction;
  233.     ((__u32 *)cur->address)[1] = cur->nextinstruction;
  234.  
  235.     cur->address = NULL;
  236.  
  237.     spinlock_unlock(&bkpoint_lock);
  238.     interrupts_restore(ipl);
  239.     return 1;
  240. }
  241.  
  242. /** Print table of active breakpoints */
  243. int cmd_print_breakpoints(cmd_arg_t *argv)
  244. {
  245.     int i;
  246.     char *symbol;
  247.  
  248.     printf("Breakpoint table.\n");
  249.     for (i=0; i < BKPOINTS_MAX; i++)
  250.         if (breakpoints[i].address) {
  251.             symbol = get_symtab_entry(breakpoints[i].address);
  252.             printf("%d. %p in %s\n",i,
  253.                    breakpoints[i].address, symbol);
  254.             printf("     Count(%d) ", breakpoints[i].counter);
  255.             if (breakpoints[i].flags & BKPOINT_INPROG)
  256.                 printf("INPROG ");
  257.             if (breakpoints[i].flags & BKPOINT_ONESHOT)
  258.                 printf("ONESHOT ");
  259.             if (breakpoints[i].flags & BKPOINT_FUNCCALL)
  260.                 printf("FUNCCALL ");
  261.             printf("\n");
  262.         }
  263.     return 1;
  264. }
  265.  
  266. /** Initialize debugger */
  267. void debugger_init()
  268. {
  269.     int i;
  270.  
  271.     for (i=0; i<BKPOINTS_MAX; i++)
  272.         breakpoints[i].address = NULL;
  273.    
  274.     cmd_initialize(&bkpts_info);
  275.     if (!cmd_register(&bkpts_info))
  276.         panic("could not register command %s\n", bkpts_info.name);
  277.  
  278.     cmd_initialize(&delbkpt_info);
  279.     if (!cmd_register(&delbkpt_info))
  280.         panic("could not register command %s\n", delbkpt_info.name);
  281.  
  282.     cmd_initialize(&addbkpt_info);
  283.     if (!cmd_register(&addbkpt_info))
  284.         panic("could not register command %s\n", addbkpt_info.name);
  285.  
  286.     cmd_initialize(&addbkpte_info);
  287.     if (!cmd_register(&addbkpte_info))
  288.         panic("could not register command %s\n", addbkpte_info.name);
  289. }
  290.  
  291. /** Handle breakpoint
  292.  *
  293.  * Find breakpoint in breakpoint table.
  294.  * If found, call kconsole, set break on next instruction and reexecute.
  295.  * If we are on "next instruction", set it back on the first and reexecute.
  296.  * If breakpoint not found in breakpoint table, call kconsole and start
  297.  * next instruction.
  298.  */
  299. void debugger_bpoint(istate_t *istate)
  300. {
  301.     bpinfo_t *cur = NULL;
  302.     __address fireaddr = istate->epc;
  303.     int i;
  304.  
  305.     /* test branch delay slot */
  306.     if (cp0_cause_read() & 0x80000000)
  307.         panic("Breakpoint in branch delay slot not supported.\n");
  308.  
  309.     spinlock_lock(&bkpoint_lock);
  310.     for (i=0; i<BKPOINTS_MAX; i++) {
  311.         /* Normal breakpoint */
  312.         if (fireaddr == breakpoints[i].address \
  313.             && !(breakpoints[i].flags & BKPOINT_REINST)) {
  314.             cur = &breakpoints[i];
  315.             break;
  316.         }
  317.         /* Reinst only breakpoint */
  318.         if ((breakpoints[i].flags & BKPOINT_REINST) \
  319.             && (fireaddr ==breakpoints[i].address+sizeof(__native))) {
  320.             cur = &breakpoints[i];
  321.             break;
  322.         }
  323.     }
  324.     if (cur) {
  325.         if (cur->flags & BKPOINT_REINST) {
  326.             /* Set breakpoint on first instruction */
  327.             ((__u32 *)cur->address)[0] = 0x0d;
  328.             /* Return back the second */
  329.             ((__u32 *)cur->address)[1] = cur->nextinstruction;
  330.             cur->flags &= ~BKPOINT_REINST;
  331.             spinlock_unlock(&bkpoint_lock);
  332.             return;
  333.         }
  334.         if (cur->flags & BKPOINT_INPROG)
  335.             printf("Warning: breakpoint recursion\n");
  336.        
  337.         if (!(cur->flags & BKPOINT_FUNCCALL))
  338.             printf("***Breakpoint %d: %p in %s.\n", i,
  339.                    fireaddr, get_symtab_entry(istate->epc));
  340.  
  341.         /* Return first instruction back */
  342.         ((__u32 *)cur->address)[0] = cur->instruction;
  343.  
  344.         if (! (cur->flags & BKPOINT_ONESHOT)) {
  345.             /* Set Breakpoint on next instruction */
  346.             ((__u32 *)cur->address)[1] = 0x0d;
  347.             cur->flags |= BKPOINT_REINST;
  348.         }
  349.         cur->flags |= BKPOINT_INPROG;
  350.     } else {
  351.         printf("***Breakpoint %p in %s.\n", fireaddr,
  352.                get_symtab_entry(fireaddr));
  353.         /* Move on to next instruction */
  354.         istate->epc += 4;
  355.     }
  356.     if (cur)
  357.         cur->counter++;
  358.     if (cur && (cur->flags & BKPOINT_FUNCCALL)) {
  359.         /* Allow zero bkfunc, just for counting */
  360.         if (cur->bkfunc)
  361.             cur->bkfunc(cur, istate);
  362.     } else {
  363.         printf("***Type 'exit' to exit kconsole.\n");
  364.         /* This disables all other processors - we are not SMP,
  365.          * actually this gets us to cpu_halt, if scheduler() is run
  366.          * - we generally do not want scheduler to be run from debug,
  367.          *   so this is a good idea
  368.          */
  369.         atomic_set(&haltstate,1);
  370.         spinlock_unlock(&bkpoint_lock);
  371.  
  372.         kconsole("debug");
  373.  
  374.         spinlock_lock(&bkpoint_lock);
  375.         atomic_set(&haltstate,0);
  376.     }
  377.     if (cur && cur->address == fireaddr && (cur->flags & BKPOINT_INPROG)) {
  378.         /* Remove one-shot breakpoint */
  379.         if ((cur->flags & BKPOINT_ONESHOT))
  380.             cur->address = NULL;
  381.         /* Remove in-progress flag */
  382.         cur->flags &= ~BKPOINT_INPROG;
  383.     }
  384.     spinlock_unlock(&bkpoint_lock);
  385. }
  386.  
  387.  /** @}
  388.  */
  389.  
  390.