Rev 2936 | Rev 2942 | 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 | |||
48 | #define ISTATE_OFF_EIP 12 |
||
49 | #define ISTATE_OFF_EFLAGS 14 |
||
50 | |||
51 | void arch_breakpoint_add(uintptr_t addr) |
||
52 | { |
||
53 | char brkp[1]; |
||
54 | int rc; |
||
55 | breakpoint_t *brk; |
||
56 | int i; |
||
57 | |||
58 | brk = NULL; |
||
59 | for (i = 0; i < MAX_BRKPTS; i++) |
||
60 | if (brk_list[i].set == 0) brk = brk_list+i; |
||
61 | |||
62 | if (!brk) { |
||
2936 | svoboda | 63 | cons_printf("too many breakpoints\n"); |
2923 | svoboda | 64 | return; |
65 | } |
||
66 | |||
67 | rc = udebug_mem_read(app_phone, &brk->arch.back, addr, 1); |
||
2936 | svoboda | 68 | cons_printf("udebug_mem_read() -> %d\n", rc); |
2923 | svoboda | 69 | brkp[0] = OPCODE_INT3; |
70 | rc = udebug_mem_write(app_phone, brkp, addr, 1); |
||
2936 | svoboda | 71 | cons_printf("udebug_mem_write() -> %d\n", rc); |
2923 | svoboda | 72 | |
73 | brk->addr = addr; |
||
74 | brk->set = 1; |
||
75 | } |
||
76 | |||
2941 | svoboda | 77 | static istate_t istate; |
2923 | svoboda | 78 | static breakpoint_t *lifted_brkpt; |
79 | |||
80 | void arch_event_breakpoint(thash_t thread_hash) |
||
81 | { |
||
82 | int rc; |
||
83 | |||
2941 | svoboda | 84 | rc = udebug_regs_read(app_phone, thread_hash, &istate); |
2936 | svoboda | 85 | cons_printf("udebug_regs_read -> %d\n", rc); |
2941 | svoboda | 86 | cons_printf("EIP was 0x%08x\n", istate.eip); |
87 | int brk_addr = istate.eip - 1; |
||
2923 | svoboda | 88 | int bi; |
89 | for (bi = 0; bi < MAX_BRKPTS; bi++) { |
||
90 | if (brk_list[bi].set && brk_list[bi].addr == brk_addr) |
||
91 | break; |
||
92 | } |
||
2924 | svoboda | 93 | |
2923 | svoboda | 94 | if (bi < MAX_BRKPTS) { |
2936 | svoboda | 95 | cons_printf("breakpoint %d hit\n", bi); |
2935 | svoboda | 96 | breakpoint_hit(); |
2924 | svoboda | 97 | |
2941 | svoboda | 98 | istate.eip = brk_addr; |
99 | istate.eflags |= 0x0100; /* trap flag */ |
||
100 | cons_printf("setting EIP to 0x%08x\n", istate.eip); |
||
101 | rc = udebug_regs_write(app_phone, thread_hash, &istate); |
||
2923 | svoboda | 102 | rc = udebug_mem_write(app_phone, &brk_list[bi].arch.back, brk_addr, 1); |
2936 | svoboda | 103 | cons_printf("udebug_mem_write(phone, 0x%x, 0x%02x, 1) -> %d\n", brk_addr, brk_list[bi].arch.back, rc); |
2923 | svoboda | 104 | lifted_brkpt = &brk_list[bi]; |
105 | } else { |
||
2936 | svoboda | 106 | cons_printf("unrecognized breakpoint at 0x%x\n", brk_addr); |
2923 | svoboda | 107 | } |
108 | } |
||
109 | |||
110 | void arch_event_trap(thash_t thread_hash) |
||
111 | { |
||
112 | unsigned char brkinstr[1]; |
||
113 | int rc; |
||
114 | |||
2936 | svoboda | 115 | cons_printf("trap event\n"); |
2924 | svoboda | 116 | |
2923 | svoboda | 117 | breakpoint_t *lb = lifted_brkpt; |
118 | brkinstr[0] = OPCODE_INT3; |
||
119 | rc = udebug_mem_write(app_phone, brkinstr, lb->addr, 1); |
||
2936 | svoboda | 120 | cons_printf("restore breakpoint -> %d\n", rc); |
2923 | svoboda | 121 | |
2941 | svoboda | 122 | rc = udebug_regs_read(app_phone, thread_hash, &istate); |
2936 | svoboda | 123 | cons_printf("udebug_regs_read -> %d\n", rc); |
2941 | svoboda | 124 | istate.eflags &= ~0x0100; /* trap flag */ |
125 | rc = udebug_regs_write(app_phone, thread_hash, &istate); |
||
2923 | svoboda | 126 | } |
127 | |||
2941 | svoboda | 128 | void arch_dump_regs(thash_t thash) |
129 | { |
||
130 | int rc; |
||
131 | |||
132 | printf("arch_dump_regs...\n"); |
||
133 | |||
134 | rc = udebug_regs_read(app_phone, thash, &istate); |
||
135 | if (rc < 0) { cons_printf("Error reading regs\n"); return; } |
||
136 | |||
137 | cons_printf( |
||
138 | "eip:%08x eflags:%08x eax:%08x ebx:%08x ecx:%08x edx:%08x\n" |
||
139 | "esi:%08x edi:%08x cs:%04x ds:%04x es:%04x fs:%04x gs:%04x\n", |
||
140 | istate.eip, istate.eflags, istate.eax, istate.ebx, |
||
141 | istate.ecx, istate.edx, istate.esi, istate.edi, istate.cs, |
||
142 | istate.ds, istate.es, istate.fs, istate.gs); |
||
143 | } |
||
144 | |||
2923 | svoboda | 145 | /** @} |
146 | */ |