Rev 3005 | Rev 3108 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3005 | Rev 3093 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Jiri Svoboda |
2 | * Copyright (c) 2008 Jiri Svoboda |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup debug |
29 | /** @addtogroup debug |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | /** @file |
32 | /** @file |
33 | */ |
33 | */ |
34 | 34 | ||
35 | #include <stdio.h> |
35 | #include <stdio.h> |
36 | #include <stdlib.h> |
36 | #include <stdlib.h> |
37 | #include <sys/types.h> |
37 | #include <sys/types.h> |
- | 38 | #include <bool.h> |
|
38 | #include <udebug.h> |
39 | #include <udebug.h> |
39 | 40 | ||
40 | #include <kernel/arch/context_offset.h> |
41 | #include <kernel/arch/context_offset.h> |
41 | 42 | ||
42 | #include "../../../cons.h" |
43 | #include "../../../cons.h" |
43 | #include "../../../main.h" |
44 | #include "../../../main.h" |
44 | #include "../../../include/arch.h" |
45 | #include "../../../include/arch.h" |
45 | 46 | ||
46 | #define OPCODE_INT3 0xCC |
47 | #define OPCODE_INT3 0xCC |
47 | 48 | ||
- | 49 | static int _set_trap_flag(dthread_t *dt, bool enable) |
|
- | 50 | { |
|
- | 51 | static istate_t istate; |
|
- | 52 | int rc; |
|
- | 53 | ||
- | 54 | rc = udebug_regs_read(app_phone, dt->hash, &istate); |
|
- | 55 | if (rc < 0) { printf("regs read failed\n"); return; } |
|
- | 56 | ||
- | 57 | if (enable) istate.eflags |= 0x0100; /* trap flag */ |
|
- | 58 | else if (!active_bkpt) istate.eflags &= ~0x0100; /* trap flag */ |
|
- | 59 | ||
- | 60 | rc = udebug_regs_write(app_phone, dt->hash, &istate); |
|
- | 61 | if (rc < 0) { printf("regs write failed\n"); return; } |
|
- | 62 | ||
- | 63 | return 0; |
|
- | 64 | } |
|
- | 65 | ||
48 | int arch_breakpoint_set(breakpoint_t *b) |
66 | int arch_breakpoint_set(breakpoint_t *b) |
49 | { |
67 | { |
50 | char brkp[1]; |
68 | char brkp[1]; |
51 | int rc; |
69 | int rc; |
52 | 70 | ||
53 | rc = udebug_mem_read(app_phone, &b->arch.back, b->addr, 1); |
71 | rc = udebug_mem_read(app_phone, &b->arch.back, b->addr, 1); |
54 | cons_printf("udebug_mem_read() -> %d\n", rc); |
72 | cons_printf("udebug_mem_read() -> %d\n", rc); |
55 | if (rc < 0) return rc; |
73 | if (rc < 0) return rc; |
56 | 74 | ||
57 | brkp[0] = OPCODE_INT3; |
75 | brkp[0] = OPCODE_INT3; |
58 | rc = udebug_mem_write(app_phone, brkp, b->addr, 1); |
76 | rc = udebug_mem_write(app_phone, brkp, b->addr, 1); |
59 | if (rc < 0) return rc; |
77 | if (rc < 0) return rc; |
60 | 78 | ||
61 | cons_printf("udebug_mem_write() -> %d\n", rc); |
79 | cons_printf("udebug_mem_write() -> %d\n", rc); |
62 | return 0; |
80 | return 0; |
63 | } |
81 | } |
64 | 82 | ||
65 | int arch_breakpoint_remove(breakpoint_t *b) |
83 | int arch_breakpoint_remove(breakpoint_t *b) |
66 | { |
84 | { |
67 | int rc; |
85 | int rc; |
68 | 86 | ||
69 | if (b->active) { |
87 | if (b->active) { |
70 | active_bkpt = NULL; |
88 | active_bkpt = NULL; |
71 | } else { |
89 | } else { |
72 | rc = udebug_mem_write(app_phone, &b->arch.back, b->addr, 1); |
90 | rc = udebug_mem_write(app_phone, &b->arch.back, b->addr, 1); |
73 | if (rc < 0) { |
91 | if (rc < 0) { |
74 | cons_printf("error writing mem\n"); |
92 | cons_printf("error writing mem\n"); |
75 | return rc; |
93 | return rc; |
76 | } |
94 | } |
77 | } |
95 | } |
78 | 96 | ||
79 | return 0; |
97 | return 0; |
80 | } |
98 | } |
81 | 99 | ||
82 | void arch_event_breakpoint(thash_t thread_hash) |
100 | void arch_event_breakpoint(thash_t thread_hash) |
83 | { |
101 | { |
84 | static istate_t istate; |
102 | static istate_t istate; |
85 | breakpoint_t *b; |
103 | breakpoint_t *b; |
86 | int rc; |
104 | int rc; |
87 | 105 | ||
88 | rc = udebug_regs_read(app_phone, thread_hash, &istate); |
106 | rc = udebug_regs_read(app_phone, thread_hash, &istate); |
89 | // cons_printf("udebug_regs_read -> %d\n", rc); |
107 | // cons_printf("udebug_regs_read -> %d\n", rc); |
90 | // cons_printf("EIP was 0x%08x\n", istate.eip); |
108 | // cons_printf("EIP was 0x%08x\n", istate.eip); |
91 | int brk_addr = istate.eip - 1; |
109 | int brk_addr = istate.eip - 1; |
92 | 110 | ||
93 | b = breakpoint_find_by_addr(brk_addr); |
111 | b = breakpoint_find_by_addr(brk_addr); |
94 | if (!b) { |
112 | if (!b) { |
95 | cons_printf("unrecognized breakpoint at 0x%x\n", brk_addr); |
113 | cons_printf("unrecognized breakpoint at 0x%x\n", brk_addr); |
96 | return; |
114 | return; |
97 | } |
115 | } |
98 | 116 | ||
99 | istate.eip = brk_addr; |
117 | istate.eip = brk_addr; |
100 | istate.eflags |= 0x0100; /* trap flag */ |
118 | istate.eflags |= 0x0100; /* trap flag */ |
101 | 119 | ||
102 | rc = udebug_regs_write(app_phone, thread_hash, &istate); |
120 | rc = udebug_regs_write(app_phone, thread_hash, &istate); |
103 | if (rc < 0) { cons_printf("error writing regs\n"); return; } |
121 | if (rc < 0) { cons_printf("error writing regs\n"); return; } |
104 | rc = udebug_mem_write(app_phone, &b->arch.back, brk_addr, 1); |
122 | rc = udebug_mem_write(app_phone, &b->arch.back, brk_addr, 1); |
105 | if (rc < 0) { cons_printf("error writing mem\n"); return; } |
123 | if (rc < 0) { cons_printf("error writing mem\n"); return; } |
106 | // cons_printf("udebug_mem_write(phone, 0x%x, 0x%02x, 1) -> %d\n", brk_addr, brk_list[bi].arch.back, rc); |
124 | // cons_printf("udebug_mem_write(phone, 0x%x, 0x%02x, 1) -> %d\n", brk_addr, brk_list[bi].arch.back, rc); |
107 | 125 | ||
108 | b->active = true; |
126 | b->active = true; |
109 | active_bkpt = b; |
127 | active_bkpt = b; |
110 | 128 | ||
111 | breakpoint_hit(b); |
129 | breakpoint_hit(b); |
112 | } |
130 | } |
113 | 131 | ||
114 | void arch_event_trap(dthread_t *dt) |
132 | void arch_event_trap(dthread_t *dt) |
115 | { |
133 | { |
116 | breakpoint_t *b; |
134 | breakpoint_t *b; |
117 | static istate_t istate; |
135 | static istate_t istate; |
118 | unsigned char brkinstr[1]; |
136 | unsigned char brkinstr[1]; |
119 | int rc; |
137 | int rc; |
120 | 138 | ||
121 | // cons_printf("trap event\n"); |
139 | // cons_printf("trap event\n"); |
122 | b = active_bkpt; |
140 | b = active_bkpt; |
123 | 141 | ||
124 | if (b) { |
142 | if (b) { |
125 | brkinstr[0] = OPCODE_INT3; |
143 | brkinstr[0] = OPCODE_INT3; |
126 | rc = udebug_mem_write(app_phone, brkinstr, b->addr, 1); |
144 | rc = udebug_mem_write(app_phone, brkinstr, b->addr, 1); |
127 | // cons_printf("restore breakpoint -> %d\n", rc); |
145 | // cons_printf("restore breakpoint -> %d\n", rc); |
128 | active_bkpt = NULL; |
146 | active_bkpt = NULL; |
129 | } |
147 | } |
130 | 148 | ||
131 | if (!dt->arch.singlestep) { |
- | |
132 | rc = udebug_regs_read(app_phone, dt->hash, &istate); |
149 | rc = _set_trap_flag(dt, false); |
133 | // cons_printf("udebug_regs_read -> %d\n", rc); |
- | |
134 | istate.eflags &= ~0x0100; /* trap flag */ |
150 | dt->arch.singlestep = false; |
135 | rc = udebug_regs_write(app_phone, dt->hash, &istate); |
- | |
136 | } else { |
151 | |
137 | // printf("ss-hit\n"); |
- | |
138 | singlestep_hit(); |
152 | singlestep_hit(); |
139 | } |
- | |
140 | } |
153 | } |
141 | 154 | ||
142 | void arch_dump_regs(thash_t thash) |
155 | void arch_dump_regs(thash_t thash) |
143 | { |
156 | { |
144 | static istate_t istate; |
157 | static istate_t istate; |
145 | int rc; |
158 | int rc; |
146 | 159 | ||
147 | rc = udebug_regs_read(app_phone, thash, &istate); |
160 | rc = udebug_regs_read(app_phone, thash, &istate); |
148 | if (rc < 0) { cons_printf("Error reading regs\n"); return; } |
161 | if (rc < 0) { cons_printf("Error reading regs\n"); return; } |
149 | 162 | ||
150 | cons_printf( |
163 | cons_printf( |
151 | "eip:%08x eflags:%08x eax:%08x ebx:%08x ecx:%08x edx:%08x\n" |
164 | "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", |
165 | "esi:%08x edi:%08x cs:%04x ds:%04x es:%04x fs:%04x gs:%04x\n", |
153 | istate.eip, istate.eflags, istate.eax, istate.ebx, |
166 | istate.eip, istate.eflags, istate.eax, istate.ebx, |
154 | istate.ecx, istate.edx, istate.esi, istate.edi, istate.cs, |
167 | istate.ecx, istate.edx, istate.esi, istate.edi, istate.cs, |
155 | istate.ds, istate.es, istate.fs, istate.gs); |
168 | istate.ds, istate.es, istate.fs, istate.gs); |
156 | } |
169 | } |
157 | 170 | ||
158 | void arch_set_singlestep(dthread_t *dt, int enable) |
171 | void arch_singlestep(dthread_t *dt) |
159 | { |
172 | { |
160 | static istate_t istate; |
- | |
161 | int rc; |
173 | int rc; |
162 | 174 | ||
163 | rc = udebug_regs_read(app_phone, dt->hash, &istate); |
175 | rc = _set_trap_flag(dt, true); |
164 | if (rc < 0) { printf("regs read failed\n"); return; } |
176 | if (rc != 0) return; |
165 | - | ||
166 | if (enable) istate.eflags |= 0x0100; /* trap flag */ |
- | |
167 | else if (!active_bkpt) istate.eflags &= ~0x0100; /* trap flag */ |
- | |
168 | - | ||
169 | rc = udebug_regs_write(app_phone, dt->hash, &istate); |
- | |
170 | if (rc < 0) { printf("regs write failed\n"); return; } |
- | |
171 | 177 | ||
172 | dt->arch.singlestep = enable; |
178 | dthread_resume(dt); |
173 | } |
179 | } |
174 | 180 | ||
175 | /** @} |
181 | /** @} |
176 | */ |
182 | */ |
177 | 183 |