Rev 2947 | Rev 3093 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2923 | svoboda | 1 | /* |
| 2 | * Copyright (c) 2008 Jiri Svoboda |
||
| 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 debug |
||
| 30 | * @{ |
||
| 31 | */ |
||
| 32 | /** @file |
||
| 33 | */ |
||
| 34 | |||
| 35 | #include <stdio.h> |
||
| 36 | #include <stdlib.h> |
||
| 37 | #include <sys/types.h> |
||
| 38 | #include <udebug.h> |
||
| 39 | |||
| 2941 | svoboda | 40 | #include <kernel/arch/context_offset.h> |
| 41 | |||
| 2936 | svoboda | 42 | #include "../../../cons.h" |
| 2923 | svoboda | 43 | #include "../../../main.h" |
| 44 | #include "../../../include/arch.h" |
||
| 45 | |||
| 46 | #define OPCODE_INT3 0xCC |
||
| 47 | |||
| 3005 | svoboda | 48 | int arch_breakpoint_set(breakpoint_t *b) |
| 2923 | svoboda | 49 | { |
| 50 | char brkp[1]; |
||
| 51 | int rc; |
||
| 52 | |||
| 3005 | svoboda | 53 | rc = udebug_mem_read(app_phone, &b->arch.back, b->addr, 1); |
| 54 | cons_printf("udebug_mem_read() -> %d\n", rc); |
||
| 55 | if (rc < 0) return rc; |
||
| 2923 | svoboda | 56 | |
| 3005 | svoboda | 57 | brkp[0] = OPCODE_INT3; |
| 58 | rc = udebug_mem_write(app_phone, brkp, b->addr, 1); |
||
| 59 | if (rc < 0) return rc; |
||
| 2923 | svoboda | 60 | |
| 2936 | svoboda | 61 | cons_printf("udebug_mem_write() -> %d\n", rc); |
| 3005 | svoboda | 62 | return 0; |
| 2923 | svoboda | 63 | } |
| 64 | |||
| 3005 | svoboda | 65 | int arch_breakpoint_remove(breakpoint_t *b) |
| 2943 | svoboda | 66 | { |
| 67 | int rc; |
||
| 2923 | svoboda | 68 | |
| 3005 | svoboda | 69 | if (b->active) { |
| 70 | active_bkpt = NULL; |
||
| 2943 | svoboda | 71 | } else { |
| 3005 | svoboda | 72 | rc = udebug_mem_write(app_phone, &b->arch.back, b->addr, 1); |
| 73 | if (rc < 0) { |
||
| 74 | cons_printf("error writing mem\n"); |
||
| 75 | return rc; |
||
| 76 | } |
||
| 2943 | svoboda | 77 | } |
| 78 | |||
| 3005 | svoboda | 79 | return 0; |
| 2943 | svoboda | 80 | } |
| 81 | |||
| 2923 | svoboda | 82 | void arch_event_breakpoint(thash_t thread_hash) |
| 83 | { |
||
| 2942 | svoboda | 84 | static istate_t istate; |
| 3005 | svoboda | 85 | breakpoint_t *b; |
| 2923 | svoboda | 86 | int rc; |
| 87 | |||
| 2941 | svoboda | 88 | rc = udebug_regs_read(app_phone, thread_hash, &istate); |
| 2942 | svoboda | 89 | // cons_printf("udebug_regs_read -> %d\n", rc); |
| 90 | // cons_printf("EIP was 0x%08x\n", istate.eip); |
||
| 2941 | svoboda | 91 | int brk_addr = istate.eip - 1; |
| 3005 | svoboda | 92 | |
| 93 | b = breakpoint_find_by_addr(brk_addr); |
||
| 94 | if (!b) { |
||
| 95 | cons_printf("unrecognized breakpoint at 0x%x\n", brk_addr); |
||
| 96 | return; |
||
| 2923 | svoboda | 97 | } |
| 2924 | svoboda | 98 | |
| 3005 | svoboda | 99 | istate.eip = brk_addr; |
| 100 | istate.eflags |= 0x0100; /* trap flag */ |
||
| 2924 | svoboda | 101 | |
| 3005 | svoboda | 102 | rc = udebug_regs_write(app_phone, thread_hash, &istate); |
| 103 | if (rc < 0) { cons_printf("error writing regs\n"); return; } |
||
| 104 | rc = udebug_mem_write(app_phone, &b->arch.back, brk_addr, 1); |
||
| 105 | if (rc < 0) { cons_printf("error writing mem\n"); return; } |
||
| 2942 | svoboda | 106 | // cons_printf("udebug_mem_write(phone, 0x%x, 0x%02x, 1) -> %d\n", brk_addr, brk_list[bi].arch.back, rc); |
| 107 | |||
| 3005 | svoboda | 108 | b->active = true; |
| 109 | active_bkpt = b; |
||
| 110 | |||
| 111 | breakpoint_hit(b); |
||
| 2923 | svoboda | 112 | } |
| 113 | |||
| 2942 | svoboda | 114 | void arch_event_trap(dthread_t *dt) |
| 2923 | svoboda | 115 | { |
| 3005 | svoboda | 116 | breakpoint_t *b; |
| 2942 | svoboda | 117 | static istate_t istate; |
| 2923 | svoboda | 118 | unsigned char brkinstr[1]; |
| 119 | int rc; |
||
| 120 | |||
| 2942 | svoboda | 121 | // cons_printf("trap event\n"); |
| 3005 | svoboda | 122 | b = active_bkpt; |
| 123 | |||
| 124 | if (b) { |
||
| 2942 | svoboda | 125 | brkinstr[0] = OPCODE_INT3; |
| 3005 | svoboda | 126 | rc = udebug_mem_write(app_phone, brkinstr, b->addr, 1); |
| 2942 | svoboda | 127 | // cons_printf("restore breakpoint -> %d\n", rc); |
| 3005 | svoboda | 128 | active_bkpt = NULL; |
| 2942 | svoboda | 129 | } |
| 2923 | svoboda | 130 | |
| 2942 | svoboda | 131 | if (!dt->arch.singlestep) { |
| 132 | rc = udebug_regs_read(app_phone, dt->hash, &istate); |
||
| 133 | // cons_printf("udebug_regs_read -> %d\n", rc); |
||
| 134 | istate.eflags &= ~0x0100; /* trap flag */ |
||
| 135 | rc = udebug_regs_write(app_phone, dt->hash, &istate); |
||
| 136 | } else { |
||
| 137 | // printf("ss-hit\n"); |
||
| 138 | singlestep_hit(); |
||
| 139 | } |
||
| 2923 | svoboda | 140 | } |
| 141 | |||
| 2941 | svoboda | 142 | void arch_dump_regs(thash_t thash) |
| 143 | { |
||
| 2942 | svoboda | 144 | static istate_t istate; |
| 2941 | svoboda | 145 | int rc; |
| 146 | |||
| 147 | rc = udebug_regs_read(app_phone, thash, &istate); |
||
| 148 | if (rc < 0) { cons_printf("Error reading regs\n"); return; } |
||
| 149 | |||
| 150 | cons_printf( |
||
| 151 | "eip:%08x eflags:%08x eax:%08x ebx:%08x ecx:%08x edx:%08x\n" |
||
| 152 | "esi:%08x edi:%08x cs:%04x ds:%04x es:%04x fs:%04x gs:%04x\n", |
||
| 153 | istate.eip, istate.eflags, istate.eax, istate.ebx, |
||
| 154 | istate.ecx, istate.edx, istate.esi, istate.edi, istate.cs, |
||
| 155 | istate.ds, istate.es, istate.fs, istate.gs); |
||
| 156 | } |
||
| 157 | |||
| 2942 | svoboda | 158 | void arch_set_singlestep(dthread_t *dt, int enable) |
| 159 | { |
||
| 160 | static istate_t istate; |
||
| 161 | int rc; |
||
| 162 | |||
| 163 | rc = udebug_regs_read(app_phone, dt->hash, &istate); |
||
| 164 | if (rc < 0) { printf("regs read failed\n"); return; } |
||
| 165 | |||
| 166 | if (enable) istate.eflags |= 0x0100; /* trap flag */ |
||
| 3005 | svoboda | 167 | else if (!active_bkpt) istate.eflags &= ~0x0100; /* trap flag */ |
| 2942 | svoboda | 168 | |
| 169 | rc = udebug_regs_write(app_phone, dt->hash, &istate); |
||
| 170 | if (rc < 0) { printf("regs write failed\n"); return; } |
||
| 171 | |||
| 172 | dt->arch.singlestep = enable; |
||
| 173 | } |
||
| 174 | |||
| 2923 | svoboda | 175 | /** @} |
| 176 | */ |