Rev 3005 | Rev 3093 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3005 | Rev 3012 | ||
---|---|---|---|
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 <udebug.h> |
38 | #include <udebug.h> |
39 | #include <kernel/arch/context_offset.h> |
- | |
40 | 39 | ||
41 | #include "../../../cons.h" |
40 | #include "../../../cons.h" |
42 | #include "../../../main.h" |
41 | #include "../../../main.h" |
43 | #include "../../../breakpoint.h" |
42 | #include "../../../breakpoint.h" |
44 | #include "../../../include/arch.h" |
43 | #include "../../../include/arch.h" |
45 | 44 | ||
46 | #define OPCODE_BREAK 0x0000000d |
45 | #define OPCODE_BREAK 0x0000000d |
47 | 46 | ||
48 | static unsigned buffer[1024]; |
47 | static istate_t istate; |
49 | 48 | ||
50 | int arch_breakpoint_set(breakpoint_t *b) |
49 | int arch_breakpoint_set(breakpoint_t *b) |
51 | { |
50 | { |
52 | uint32_t brkp; |
51 | uint32_t brkp; |
53 | int rc; |
52 | int rc; |
54 | 53 | ||
55 | rc = udebug_mem_read(app_phone, &b->arch.back, b->addr, |
54 | rc = udebug_mem_read(app_phone, &b->arch.back, b->addr, |
56 | sizeof(b->arch.back)); |
55 | sizeof(b->arch.back)); |
57 | if (rc < 0) return rc; |
56 | if (rc < 0) return rc; |
58 | 57 | ||
59 | cons_printf("udebug_mem_read() -> %d\n", rc); |
58 | cons_printf("udebug_mem_read() -> %d\n", rc); |
60 | brkp = OPCODE_BREAK; |
59 | brkp = OPCODE_BREAK; |
61 | rc = udebug_mem_write(app_phone, &brkp, b->addr, sizeof(brkp)); |
60 | rc = udebug_mem_write(app_phone, &brkp, b->addr, sizeof(brkp)); |
62 | cons_printf("udebug_mem_write() -> %d\n", rc); |
61 | cons_printf("udebug_mem_write() -> %d\n", rc); |
63 | if (rc < 0) return rc; |
62 | if (rc < 0) return rc; |
64 | 63 | ||
65 | return 0; |
64 | return 0; |
66 | } |
65 | } |
67 | 66 | ||
68 | int arch_breakpoint_remove(breakpoint_t *b) |
67 | int arch_breakpoint_remove(breakpoint_t *b) |
69 | { |
68 | { |
70 | int rc; |
69 | int rc; |
71 | 70 | ||
72 | if (b->active) { |
71 | if (b->active) { |
73 | rc = udebug_mem_write(app_phone, &b->arch.back, b->addr + 4, 1); |
72 | rc = udebug_mem_write(app_phone, &b->arch.back, b->addr + 4, 4); |
74 | if (rc < 0) { |
73 | if (rc < 0) { |
75 | cons_printf("error writing memory\n"); |
74 | cons_printf("error writing memory\n"); |
76 | return rc; |
75 | return rc; |
77 | } |
76 | } |
78 | active_bkpt = NULL; |
77 | active_bkpt = NULL; |
79 | } else { |
78 | } else { |
80 | rc = udebug_mem_write(app_phone, &b->arch.back, b->addr, 1); |
79 | rc = udebug_mem_write(app_phone, &b->arch.back, b->addr, 4); |
81 | if (rc < 0) { |
80 | if (rc < 0) { |
82 | cons_printf("error writing memory\n"); |
81 | cons_printf("error writing memory\n"); |
83 | return rc; |
82 | return rc; |
84 | } |
83 | } |
85 | } |
84 | } |
86 | 85 | ||
87 | return 0; |
86 | return 0; |
88 | 87 | ||
89 | } |
88 | } |
90 | 89 | ||
91 | void arch_event_breakpoint(thash_t thread_hash) |
90 | void arch_event_breakpoint(thash_t thread_hash) |
92 | { |
91 | { |
93 | breakpoint_t *b; |
92 | breakpoint_t *b; |
- | 93 | dthread_t *dt; |
|
94 | int rc; |
94 | int rc; |
95 | uint32_t epc; |
95 | uint32_t epc; |
96 | int brk_addr; |
96 | int brk_addr; |
97 | uint32_t brkp; |
97 | uint32_t brkp; |
98 | 98 | ||
99 | brkp = OPCODE_BREAK; |
99 | brkp = OPCODE_BREAK; |
100 | 100 | ||
- | 101 | cons_printf("arch_event_breakpoint\n"); |
|
- | 102 | ||
101 | rc = udebug_regs_read(app_phone, thread_hash, buffer); |
103 | rc = udebug_regs_read(app_phone, thread_hash, &istate); |
102 | cons_printf("udebug_regs_read -> %d\n", rc); |
104 | cons_printf("udebug_regs_read -> %d\n", rc); |
103 | epc = buffer[EOFFSET_EPC/sizeof(unsigned)]; |
105 | epc = istate_get_pc(&istate); |
104 | cons_printf("EPC was 0x%08x\n", epc); |
106 | cons_printf("EPC was 0x%08x\n", epc); |
105 | brk_addr = epc; |
107 | brk_addr = epc; |
106 | 108 | ||
107 | b = breakpoint_find_by_addr(brk_addr); |
109 | b = breakpoint_find_by_addr(brk_addr); |
108 | if (b != NULL) { |
110 | if (b != NULL) { |
- | 111 | cons_printf("move breakpoint\b"); |
|
109 | rc = udebug_mem_write(app_phone, &b->arch.back, brk_addr, 4); |
112 | rc = udebug_mem_write(app_phone, &b->arch.back, brk_addr, 4); |
110 | cons_printf("udebug_mem_write(phone, 0x%x, 0x%02x, 1) -> %d\n", brk_addr, b->arch.back, rc); |
- | |
111 | rc = udebug_mem_read(app_phone, &b->arch.back, brk_addr + 4, 4); |
113 | rc = udebug_mem_read(app_phone, &b->arch.back, brk_addr + 4, 4); |
112 | rc = udebug_mem_write(app_phone, &brkp, brk_addr + 4, 4); |
114 | rc = udebug_mem_write(app_phone, &brkp, brk_addr + 4, 4); |
113 | active_bkpt = b; |
115 | active_bkpt = b; |
- | 116 | b->active = true; |
|
114 | 117 | ||
- | 118 | cons_printf("breakpoint_hit...\n"); |
|
115 | breakpoint_hit(b); |
119 | breakpoint_hit(b); |
- | 120 | cons_printf("end_hit...\n"); |
|
- | 121 | return; |
|
116 | } |
122 | } |
117 | 123 | ||
118 | b = breakpoint_find_by_addr(brk_addr - 4); |
124 | b = breakpoint_find_by_addr(brk_addr - 4); |
119 | if (b != NULL && b->active) { |
125 | if (b != NULL && b->active) { |
120 | cons_printf("restoring breakpoint %d\n", b->id); |
126 | cons_printf("restoring breakpoint %d\n", b->id); |
121 | rc = udebug_mem_write(app_phone, &b->arch.back, brk_addr, 4); |
127 | rc = udebug_mem_write(app_phone, &b->arch.back, brk_addr, 4); |
122 | rc = udebug_mem_read(app_phone, &b->arch.back, brk_addr - 4, 4); |
128 | rc = udebug_mem_read(app_phone, &b->arch.back, brk_addr - 4, 4); |
123 | rc = udebug_mem_write(app_phone, &brkp, brk_addr - 4, 4); |
129 | rc = udebug_mem_write(app_phone, &brkp, brk_addr - 4, 4); |
124 | active_bkpt = NULL; |
130 | active_bkpt = NULL; |
- | 131 | ||
- | 132 | if (dt->arch.singlestep) { |
|
- | 133 | singlestep_hit(); |
|
- | 134 | ||
- | 135 | rc = udebug_mem_read(app_phone, &dt->arch.sstep_back, brk_addr + 4, 4); |
|
- | 136 | rc = udebug_mem_write(app_phone, &brkp, brk_addr + 4, 4); |
|
- | 137 | } |
|
- | 138 | return; |
|
- | 139 | } |
|
- | 140 | ||
- | 141 | dt = dthread_get(); |
|
- | 142 | ||
- | 143 | if (dt->arch.singlestep) { |
|
- | 144 | cons_printf("advance singlestep\n"); |
|
- | 145 | rc = udebug_mem_write(app_phone, &dt->arch.sstep_back, brk_addr, 4); |
|
- | 146 | rc = udebug_mem_read(app_phone, &dt->arch.sstep_back, brk_addr + 4, 4); |
|
- | 147 | rc = udebug_mem_write(app_phone, &brkp, brk_addr + 4, 4); |
|
- | 148 | ||
- | 149 | singlestep_hit(); |
|
- | 150 | ||
125 | return; |
151 | return; |
126 | } |
152 | } |
127 | 153 | ||
128 | cons_printf("Unrecognized breakpoint at 0x%lx\n", brk_addr); |
154 | cons_printf("Unrecognized breakpoint at 0x%lx\n", brk_addr); |
129 | } |
155 | } |
130 | 156 | ||
131 | void arch_event_trap(dthread_t *dt) |
157 | void arch_event_trap(dthread_t *dt) |
132 | { |
158 | { |
133 | /* Unused */ |
159 | /* Unused */ |
134 | (void)dt; |
160 | (void)dt; |
135 | } |
161 | } |
136 | 162 | ||
137 | void arch_dump_regs(thash_t thash) |
163 | void arch_dump_regs(thash_t thash) |
138 | { |
164 | { |
139 | } |
165 | } |
140 | 166 | ||
141 | void arch_set_singlestep(dthread_t *dt, int enable) |
167 | void arch_set_singlestep(dthread_t *dt, int enable) |
142 | { |
168 | { |
- | 169 | int rc; |
|
- | 170 | uint32_t epc; |
|
- | 171 | uint32_t brk; |
|
- | 172 | breakpoint_t *b1, *b2; |
|
- | 173 | ||
- | 174 | brk = OPCODE_BREAK; |
|
- | 175 | ||
- | 176 | cons_printf("arch_set_singlestep(dt, %d)\n", enable); |
|
- | 177 | rc = udebug_regs_read(app_phone, dt->hash, &istate); |
|
- | 178 | cons_printf("udebug_regs_read -> %d\n", rc); |
|
- | 179 | epc = istate_get_pc(&istate); |
|
- | 180 | cons_printf("EPC was 0x%08x\n", epc); |
|
- | 181 | ||
- | 182 | b1 = breakpoint_find_by_addr(epc - 4); |
|
- | 183 | b2 = breakpoint_find_by_addr(epc); |
|
- | 184 | ||
- | 185 | if (enable && !dt->arch.singlestep) { |
|
- | 186 | if (b1 && b1->active) { |
|
- | 187 | dt->arch.sstep_back = b1->arch.back; |
|
- | 188 | } else if (b2) { |
|
- | 189 | dt->arch.sstep_back = b2->arch.back; |
|
- | 190 | } else { |
|
- | 191 | cons_printf("initial set singlestep\b"); |
|
- | 192 | rc = udebug_mem_read(app_phone, &dt->arch.sstep_back, epc + 4, 4); |
|
- | 193 | rc = udebug_mem_write(app_phone, &brk, epc + 4, 4); |
|
- | 194 | if (rc < 0) { cons_printf("error writing mem\n"); return; } |
|
- | 195 | } |
|
- | 196 | } else if (!enable && dt->arch.singlestep) { |
|
- | 197 | if ((b1 && b1->active) || b2) { |
|
- | 198 | /* Do not remove BRK instruction */ |
|
- | 199 | } else { |
|
- | 200 | cons_printf("remove singlestep\b"); |
|
- | 201 | rc = udebug_mem_write(app_phone, &dt->arch.sstep_back, epc + 4, 4); |
|
- | 202 | } |
|
- | 203 | } |
|
- | 204 | dt->arch.singlestep = enable; |
|
143 | } |
205 | } |
144 | 206 | ||
145 | /** @} |
207 | /** @} |
146 | */ |
208 | */ |
147 | 209 |