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