Rev 3623 | Rev 4377 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3623 | Rev 3684 | ||
---|---|---|---|
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 generic |
29 | /** @addtogroup generic |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file |
34 | * @file |
35 | * @brief Udebug hooks and data structure management. |
35 | * @brief Udebug hooks and data structure management. |
36 | * |
36 | * |
37 | * Udebug is an interface that makes userspace debuggers possible. |
37 | * Udebug is an interface that makes userspace debuggers possible. |
38 | */ |
38 | */ |
39 | 39 | ||
40 | #include <synch/waitq.h> |
40 | #include <synch/waitq.h> |
41 | #include <debug.h> |
41 | #include <debug.h> |
42 | #include <udebug/udebug.h> |
42 | #include <udebug/udebug.h> |
43 | #include <errno.h> |
43 | #include <errno.h> |
44 | #include <arch.h> |
44 | #include <arch.h> |
45 | 45 | ||
46 | 46 | ||
47 | /** Initialize udebug part of task structure. |
47 | /** Initialize udebug part of task structure. |
48 | * |
48 | * |
49 | * Called as part of task structure initialization. |
49 | * Called as part of task structure initialization. |
50 | * @param ut Pointer to the structure to initialize. |
50 | * @param ut Pointer to the structure to initialize. |
51 | */ |
51 | */ |
52 | void udebug_task_init(udebug_task_t *ut) |
52 | void udebug_task_init(udebug_task_t *ut) |
53 | { |
53 | { |
54 | mutex_initialize(&ut->lock, MUTEX_PASSIVE); |
54 | mutex_initialize(&ut->lock, MUTEX_PASSIVE); |
55 | ut->dt_state = UDEBUG_TS_INACTIVE; |
55 | ut->dt_state = UDEBUG_TS_INACTIVE; |
56 | ut->begin_call = NULL; |
56 | ut->begin_call = NULL; |
57 | ut->not_stoppable_count = 0; |
57 | ut->not_stoppable_count = 0; |
58 | ut->evmask = 0; |
58 | ut->evmask = 0; |
59 | } |
59 | } |
60 | 60 | ||
61 | /** Initialize udebug part of thread structure. |
61 | /** Initialize udebug part of thread structure. |
62 | * |
62 | * |
63 | * Called as part of thread structure initialization. |
63 | * Called as part of thread structure initialization. |
64 | * @param ut Pointer to the structure to initialize. |
64 | * @param ut Pointer to the structure to initialize. |
65 | */ |
65 | */ |
66 | void udebug_thread_initialize(udebug_thread_t *ut) |
66 | void udebug_thread_initialize(udebug_thread_t *ut) |
67 | { |
67 | { |
68 | mutex_initialize(&ut->lock, MUTEX_PASSIVE); |
68 | mutex_initialize(&ut->lock, MUTEX_PASSIVE); |
69 | waitq_initialize(&ut->go_wq); |
69 | waitq_initialize(&ut->go_wq); |
70 | 70 | ||
71 | ut->go_call = NULL; |
71 | ut->go_call = NULL; |
72 | ut->uspace_state = NULL; |
72 | ut->uspace_state = NULL; |
73 | ut->go = false; |
73 | ut->go = false; |
74 | ut->stoppable = true; |
74 | ut->stoppable = true; |
75 | ut->debug_active = false; |
75 | ut->active = false; |
76 | ut->cur_event = 0; /* none */ |
76 | ut->cur_event = 0; /* none */ |
77 | } |
77 | } |
78 | 78 | ||
79 | /** Wait for a GO message. |
79 | /** Wait for a GO message. |
80 | * |
80 | * |
81 | * When a debugging event occurs in a thread or the thread is stopped, |
81 | * When a debugging event occurs in a thread or the thread is stopped, |
82 | * this function is called to block the thread until a GO message |
82 | * this function is called to block the thread until a GO message |
83 | * is received. |
83 | * is received. |
84 | * |
84 | * |
85 | * @param wq The wait queue used by the thread to wait for GO messages. |
85 | * @param wq The wait queue used by the thread to wait for GO messages. |
86 | */ |
86 | */ |
87 | static void udebug_wait_for_go(waitq_t *wq) |
87 | static void udebug_wait_for_go(waitq_t *wq) |
88 | { |
88 | { |
89 | int rc; |
89 | int rc; |
90 | ipl_t ipl; |
90 | ipl_t ipl; |
91 | 91 | ||
92 | ipl = waitq_sleep_prepare(wq); |
92 | ipl = waitq_sleep_prepare(wq); |
93 | 93 | ||
94 | wq->missed_wakeups = 0; /* Enforce blocking. */ |
94 | wq->missed_wakeups = 0; /* Enforce blocking. */ |
95 | rc = waitq_sleep_timeout_unsafe(wq, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE); |
95 | rc = waitq_sleep_timeout_unsafe(wq, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE); |
96 | 96 | ||
97 | waitq_sleep_finish(wq, rc, ipl); |
97 | waitq_sleep_finish(wq, rc, ipl); |
98 | } |
98 | } |
99 | 99 | ||
100 | /** Do a preliminary check that a debugging session is in progress. |
100 | /** Do a preliminary check that a debugging session is in progress. |
101 | * |
101 | * |
102 | * This only requires the THREAD->udebug.lock mutex (and not TASK->udebug.lock |
102 | * This only requires the THREAD->udebug.lock mutex (and not TASK->udebug.lock |
103 | * mutex). For an undebugged task, this will never block (while there could be |
103 | * mutex). For an undebugged task, this will never block (while there could be |
104 | * collisions by different threads on the TASK mutex), thus improving SMP |
104 | * collisions by different threads on the TASK mutex), thus improving SMP |
105 | * perormance for undebugged tasks. |
105 | * perormance for undebugged tasks. |
106 | * |
106 | * |
107 | * @return True if the thread was in a debugging session when the function |
107 | * @return True if the thread was in a debugging session when the function |
108 | * checked, false otherwise. |
108 | * checked, false otherwise. |
109 | */ |
109 | */ |
110 | static bool udebug_thread_precheck(void) |
110 | static bool udebug_thread_precheck(void) |
111 | { |
111 | { |
112 | bool res; |
112 | bool res; |
113 | 113 | ||
114 | mutex_lock(&THREAD->udebug.lock); |
114 | mutex_lock(&THREAD->udebug.lock); |
115 | res = THREAD->udebug.debug_active; |
115 | res = THREAD->udebug.active; |
116 | mutex_unlock(&THREAD->udebug.lock); |
116 | mutex_unlock(&THREAD->udebug.lock); |
117 | 117 | ||
118 | return res; |
118 | return res; |
119 | } |
119 | } |
120 | 120 | ||
121 | /** Start of stoppable section. |
121 | /** Start of stoppable section. |
122 | * |
122 | * |
123 | * A stoppable section is a section of code where if the thread can be stoped. In other words, |
123 | * A stoppable section is a section of code where if the thread can be stoped. In other words, |
124 | * if a STOP operation is issued, the thread is guaranteed not to execute |
124 | * if a STOP operation is issued, the thread is guaranteed not to execute |
125 | * any userspace instructions until the thread is resumed. |
125 | * any userspace instructions until the thread is resumed. |
126 | * |
126 | * |
127 | * Having stoppable sections is better than having stopping points, since |
127 | * Having stoppable sections is better than having stopping points, since |
128 | * a thread can be stopped even when it is blocked indefinitely in a system |
128 | * a thread can be stopped even when it is blocked indefinitely in a system |
129 | * call (whereas it would not reach any stopping point). |
129 | * call (whereas it would not reach any stopping point). |
130 | */ |
130 | */ |
131 | void udebug_stoppable_begin(void) |
131 | void udebug_stoppable_begin(void) |
132 | { |
132 | { |
133 | int nsc; |
133 | int nsc; |
134 | call_t *db_call, *go_call; |
134 | call_t *db_call, *go_call; |
135 | 135 | ||
136 | ASSERT(THREAD); |
136 | ASSERT(THREAD); |
137 | ASSERT(TASK); |
137 | ASSERT(TASK); |
138 | 138 | ||
139 | /* Early check for undebugged tasks */ |
139 | /* Early check for undebugged tasks */ |
140 | if (!udebug_thread_precheck()) { |
140 | if (!udebug_thread_precheck()) { |
141 | return; |
141 | return; |
142 | } |
142 | } |
143 | 143 | ||
144 | mutex_lock(&TASK->udebug.lock); |
144 | mutex_lock(&TASK->udebug.lock); |
145 | 145 | ||
146 | nsc = --TASK->udebug.not_stoppable_count; |
146 | nsc = --TASK->udebug.not_stoppable_count; |
147 | 147 | ||
148 | /* Lock order OK, THREAD->udebug.lock is after TASK->udebug.lock */ |
148 | /* Lock order OK, THREAD->udebug.lock is after TASK->udebug.lock */ |
149 | mutex_lock(&THREAD->udebug.lock); |
149 | mutex_lock(&THREAD->udebug.lock); |
150 | ASSERT(THREAD->udebug.stoppable == false); |
150 | ASSERT(THREAD->udebug.stoppable == false); |
151 | THREAD->udebug.stoppable = true; |
151 | THREAD->udebug.stoppable = true; |
152 | 152 | ||
153 | if (TASK->udebug.dt_state == UDEBUG_TS_BEGINNING && nsc == 0) { |
153 | if (TASK->udebug.dt_state == UDEBUG_TS_BEGINNING && nsc == 0) { |
154 | /* |
154 | /* |
155 | * This was the last non-stoppable thread. Reply to |
155 | * This was the last non-stoppable thread. Reply to |
156 | * DEBUG_BEGIN call. |
156 | * DEBUG_BEGIN call. |
157 | */ |
157 | */ |
158 | 158 | ||
159 | db_call = TASK->udebug.begin_call; |
159 | db_call = TASK->udebug.begin_call; |
160 | ASSERT(db_call); |
160 | ASSERT(db_call); |
161 | 161 | ||
162 | TASK->udebug.dt_state = UDEBUG_TS_ACTIVE; |
162 | TASK->udebug.dt_state = UDEBUG_TS_ACTIVE; |
163 | TASK->udebug.begin_call = NULL; |
163 | TASK->udebug.begin_call = NULL; |
164 | 164 | ||
165 | IPC_SET_RETVAL(db_call->data, 0); |
165 | IPC_SET_RETVAL(db_call->data, 0); |
166 | ipc_answer(&TASK->answerbox, db_call); |
166 | ipc_answer(&TASK->answerbox, db_call); |
167 | 167 | ||
168 | } else if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) { |
168 | } else if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) { |
169 | /* |
169 | /* |
170 | * Active debugging session |
170 | * Active debugging session |
171 | */ |
171 | */ |
172 | 172 | ||
173 | if (THREAD->udebug.debug_active == true && |
173 | if (THREAD->udebug.active == true && |
174 | THREAD->udebug.go == false) { |
174 | THREAD->udebug.go == false) { |
175 | /* |
175 | /* |
176 | * Thread was requested to stop - answer go call |
176 | * Thread was requested to stop - answer go call |
177 | */ |
177 | */ |
178 | 178 | ||
179 | /* Make sure nobody takes this call away from us */ |
179 | /* Make sure nobody takes this call away from us */ |
180 | go_call = THREAD->udebug.go_call; |
180 | go_call = THREAD->udebug.go_call; |
181 | THREAD->udebug.go_call = NULL; |
181 | THREAD->udebug.go_call = NULL; |
182 | ASSERT(go_call); |
182 | ASSERT(go_call); |
183 | 183 | ||
184 | IPC_SET_RETVAL(go_call->data, 0); |
184 | IPC_SET_RETVAL(go_call->data, 0); |
185 | IPC_SET_ARG1(go_call->data, UDEBUG_EVENT_STOP); |
185 | IPC_SET_ARG1(go_call->data, UDEBUG_EVENT_STOP); |
186 | 186 | ||
187 | THREAD->udebug.cur_event = UDEBUG_EVENT_STOP; |
187 | THREAD->udebug.cur_event = UDEBUG_EVENT_STOP; |
188 | 188 | ||
189 | ipc_answer(&TASK->answerbox, go_call); |
189 | ipc_answer(&TASK->answerbox, go_call); |
190 | } |
190 | } |
191 | } |
191 | } |
192 | 192 | ||
193 | mutex_unlock(&THREAD->udebug.lock); |
193 | mutex_unlock(&THREAD->udebug.lock); |
194 | mutex_unlock(&TASK->udebug.lock); |
194 | mutex_unlock(&TASK->udebug.lock); |
195 | } |
195 | } |
196 | 196 | ||
197 | /** End of a stoppable section. |
197 | /** End of a stoppable section. |
198 | * |
198 | * |
199 | * This is the point where the thread will block if it is stopped. |
199 | * This is the point where the thread will block if it is stopped. |
200 | * (As, by definition, a stopped thread must not leave its stoppable section). |
200 | * (As, by definition, a stopped thread must not leave its stoppable section). |
201 | */ |
201 | */ |
202 | void udebug_stoppable_end(void) |
202 | void udebug_stoppable_end(void) |
203 | { |
203 | { |
204 | /* Early check for undebugged tasks */ |
204 | /* Early check for undebugged tasks */ |
205 | if (!udebug_thread_precheck()) { |
205 | if (!udebug_thread_precheck()) { |
206 | return; |
206 | return; |
207 | } |
207 | } |
208 | 208 | ||
209 | restart: |
209 | restart: |
210 | mutex_lock(&TASK->udebug.lock); |
210 | mutex_lock(&TASK->udebug.lock); |
211 | mutex_lock(&THREAD->udebug.lock); |
211 | mutex_lock(&THREAD->udebug.lock); |
212 | 212 | ||
213 | if (THREAD->udebug.debug_active && |
- | |
214 | THREAD->udebug.go == false) { |
213 | if (THREAD->udebug.active && THREAD->udebug.go == false) { |
215 | TASK->udebug.begin_call = NULL; |
214 | TASK->udebug.begin_call = NULL; |
216 | mutex_unlock(&THREAD->udebug.lock); |
215 | mutex_unlock(&THREAD->udebug.lock); |
217 | mutex_unlock(&TASK->udebug.lock); |
216 | mutex_unlock(&TASK->udebug.lock); |
218 | 217 | ||
219 | udebug_wait_for_go(&THREAD->udebug.go_wq); |
218 | udebug_wait_for_go(&THREAD->udebug.go_wq); |
220 | 219 | ||
221 | goto restart; |
220 | goto restart; |
222 | /* Must try again - have to lose stoppability atomically. */ |
221 | /* Must try again - have to lose stoppability atomically. */ |
223 | } else { |
222 | } else { |
224 | ++TASK->udebug.not_stoppable_count; |
223 | ++TASK->udebug.not_stoppable_count; |
225 | ASSERT(THREAD->udebug.stoppable == true); |
224 | ASSERT(THREAD->udebug.stoppable == true); |
226 | THREAD->udebug.stoppable = false; |
225 | THREAD->udebug.stoppable = false; |
227 | 226 | ||
228 | mutex_unlock(&THREAD->udebug.lock); |
227 | mutex_unlock(&THREAD->udebug.lock); |
229 | mutex_unlock(&TASK->udebug.lock); |
228 | mutex_unlock(&TASK->udebug.lock); |
230 | } |
229 | } |
231 | } |
230 | } |
232 | 231 | ||
233 | /** Upon being scheduled to run, check if the current thread should stop. |
232 | /** Upon being scheduled to run, check if the current thread should stop. |
234 | * |
233 | * |
235 | * This function is called from clock(). |
234 | * This function is called from clock(). |
236 | */ |
235 | */ |
237 | void udebug_before_thread_runs(void) |
236 | void udebug_before_thread_runs(void) |
238 | { |
237 | { |
239 | /* Check if we are supposed to stop. */ |
238 | /* Check if we are supposed to stop. */ |
240 | udebug_stoppable_begin(); |
239 | udebug_stoppable_begin(); |
241 | udebug_stoppable_end(); |
240 | udebug_stoppable_end(); |
242 | } |
241 | } |
243 | 242 | ||
244 | /** Syscall event hook. |
243 | /** Syscall event hook. |
245 | * |
244 | * |
246 | * Must be called before and after servicing a system call. This generates |
245 | * Must be called before and after servicing a system call. This generates |
247 | * a SYSCALL_B or SYSCALL_E event, depending on the value of @a end_variant. |
246 | * a SYSCALL_B or SYSCALL_E event, depending on the value of @a end_variant. |
248 | */ |
247 | */ |
249 | void udebug_syscall_event(unative_t a1, unative_t a2, unative_t a3, |
248 | void udebug_syscall_event(unative_t a1, unative_t a2, unative_t a3, |
250 | unative_t a4, unative_t a5, unative_t a6, unative_t id, unative_t rc, |
249 | unative_t a4, unative_t a5, unative_t a6, unative_t id, unative_t rc, |
251 | bool end_variant) |
250 | bool end_variant) |
252 | { |
251 | { |
253 | call_t *call; |
252 | call_t *call; |
254 | udebug_event_t etype; |
253 | udebug_event_t etype; |
255 | 254 | ||
256 | etype = end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B; |
255 | etype = end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B; |
257 | 256 | ||
258 | /* Early check for undebugged tasks */ |
257 | /* Early check for undebugged tasks */ |
259 | if (!udebug_thread_precheck()) { |
258 | if (!udebug_thread_precheck()) { |
260 | return; |
259 | return; |
261 | } |
260 | } |
262 | 261 | ||
263 | mutex_lock(&TASK->udebug.lock); |
262 | mutex_lock(&TASK->udebug.lock); |
264 | mutex_lock(&THREAD->udebug.lock); |
263 | mutex_lock(&THREAD->udebug.lock); |
265 | 264 | ||
266 | /* Must only generate events when in debugging session and is go. */ |
265 | /* Must only generate events when in debugging session and is go. */ |
267 | if (THREAD->udebug.debug_active != true || |
- | |
268 | THREAD->udebug.go == false || |
266 | if (THREAD->udebug.active != true || THREAD->udebug.go == false || |
269 | (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) { |
267 | (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) { |
270 | mutex_unlock(&THREAD->udebug.lock); |
268 | mutex_unlock(&THREAD->udebug.lock); |
271 | mutex_unlock(&TASK->udebug.lock); |
269 | mutex_unlock(&TASK->udebug.lock); |
272 | return; |
270 | return; |
273 | } |
271 | } |
274 | 272 | ||
275 | //printf("udebug_syscall_event\n"); |
273 | //printf("udebug_syscall_event\n"); |
276 | call = THREAD->udebug.go_call; |
274 | call = THREAD->udebug.go_call; |
277 | THREAD->udebug.go_call = NULL; |
275 | THREAD->udebug.go_call = NULL; |
278 | 276 | ||
279 | IPC_SET_RETVAL(call->data, 0); |
277 | IPC_SET_RETVAL(call->data, 0); |
280 | IPC_SET_ARG1(call->data, etype); |
278 | IPC_SET_ARG1(call->data, etype); |
281 | IPC_SET_ARG2(call->data, id); |
279 | IPC_SET_ARG2(call->data, id); |
282 | IPC_SET_ARG3(call->data, rc); |
280 | IPC_SET_ARG3(call->data, rc); |
283 | //printf("udebug_syscall_event/ipc_answer\n"); |
281 | //printf("udebug_syscall_event/ipc_answer\n"); |
284 | 282 | ||
285 | THREAD->udebug.syscall_args[0] = a1; |
283 | THREAD->udebug.syscall_args[0] = a1; |
286 | THREAD->udebug.syscall_args[1] = a2; |
284 | THREAD->udebug.syscall_args[1] = a2; |
287 | THREAD->udebug.syscall_args[2] = a3; |
285 | THREAD->udebug.syscall_args[2] = a3; |
288 | THREAD->udebug.syscall_args[3] = a4; |
286 | THREAD->udebug.syscall_args[3] = a4; |
289 | THREAD->udebug.syscall_args[4] = a5; |
287 | THREAD->udebug.syscall_args[4] = a5; |
290 | THREAD->udebug.syscall_args[5] = a6; |
288 | THREAD->udebug.syscall_args[5] = a6; |
291 | 289 | ||
292 | /* |
290 | /* |
293 | * Make sure udebug.go is false when going to sleep |
291 | * Make sure udebug.go is false when going to sleep |
294 | * in case we get woken up by DEBUG_END. (At which |
292 | * in case we get woken up by DEBUG_END. (At which |
295 | * point it must be back to the initial true value). |
293 | * point it must be back to the initial true value). |
296 | */ |
294 | */ |
297 | THREAD->udebug.go = false; |
295 | THREAD->udebug.go = false; |
298 | THREAD->udebug.cur_event = etype; |
296 | THREAD->udebug.cur_event = etype; |
299 | 297 | ||
300 | ipc_answer(&TASK->answerbox, call); |
298 | ipc_answer(&TASK->answerbox, call); |
301 | 299 | ||
302 | mutex_unlock(&THREAD->udebug.lock); |
300 | mutex_unlock(&THREAD->udebug.lock); |
303 | mutex_unlock(&TASK->udebug.lock); |
301 | mutex_unlock(&TASK->udebug.lock); |
304 | 302 | ||
305 | udebug_wait_for_go(&THREAD->udebug.go_wq); |
303 | udebug_wait_for_go(&THREAD->udebug.go_wq); |
306 | } |
304 | } |
307 | 305 | ||
308 | /** Thread-creation event hook combined with attaching the thread. |
306 | /** Thread-creation event hook combined with attaching the thread. |
309 | * |
307 | * |
310 | * Must be called when a new userspace thread is created in the debugged |
308 | * Must be called when a new userspace thread is created in the debugged |
311 | * task. Generates a THREAD_B event. Also attaches the thread @a t |
309 | * task. Generates a THREAD_B event. Also attaches the thread @a t |
312 | * to the task @a ta. |
310 | * to the task @a ta. |
313 | * |
311 | * |
314 | * This is necessary to avoid a race condition where the BEGIN and THREAD_READ |
312 | * This is necessary to avoid a race condition where the BEGIN and THREAD_READ |
315 | * requests would be handled inbetween attaching the thread and checking it |
313 | * requests would be handled inbetween attaching the thread and checking it |
316 | * for being in a debugging session to send the THREAD_B event. We could then |
314 | * for being in a debugging session to send the THREAD_B event. We could then |
317 | * either miss threads or get some threads both in the thread list |
315 | * either miss threads or get some threads both in the thread list |
318 | * and get a THREAD_B event for them. |
316 | * and get a THREAD_B event for them. |
319 | * |
317 | * |
320 | * @param t Structure of the thread being created. Not locked, as the |
318 | * @param t Structure of the thread being created. Not locked, as the |
321 | * thread is not executing yet. |
319 | * thread is not executing yet. |
322 | * @param ta Task to which the thread should be attached. |
320 | * @param ta Task to which the thread should be attached. |
323 | */ |
321 | */ |
324 | void udebug_thread_b_event_attach(struct thread *t, struct task *ta) |
322 | void udebug_thread_b_event_attach(struct thread *t, struct task *ta) |
325 | { |
323 | { |
326 | call_t *call; |
324 | call_t *call; |
327 | 325 | ||
328 | mutex_lock(&TASK->udebug.lock); |
326 | mutex_lock(&TASK->udebug.lock); |
329 | mutex_lock(&THREAD->udebug.lock); |
327 | mutex_lock(&THREAD->udebug.lock); |
330 | 328 | ||
331 | thread_attach(t, ta); |
329 | thread_attach(t, ta); |
332 | 330 | ||
333 | LOG("udebug_thread_b_event\n"); |
331 | LOG("udebug_thread_b_event\n"); |
334 | LOG("- check state\n"); |
332 | LOG("- check state\n"); |
335 | 333 | ||
336 | /* Must only generate events when in debugging session */ |
334 | /* Must only generate events when in debugging session */ |
337 | if (THREAD->udebug.debug_active != true) { |
335 | if (THREAD->udebug.active != true) { |
338 | LOG("- debug_active: %s, udebug.go: %s\n", |
336 | LOG("- udebug.active: %s, udebug.go: %s\n", |
339 | THREAD->udebug.debug_active ? "yes(+)" : "no(-)", |
337 | THREAD->udebug.active ? "yes(+)" : "no(-)", |
340 | THREAD->udebug.go ? "yes(-)" : "no(+)"); |
338 | THREAD->udebug.go ? "yes(-)" : "no(+)"); |
341 | mutex_unlock(&THREAD->udebug.lock); |
339 | mutex_unlock(&THREAD->udebug.lock); |
342 | mutex_unlock(&TASK->udebug.lock); |
340 | mutex_unlock(&TASK->udebug.lock); |
343 | return; |
341 | return; |
344 | } |
342 | } |
345 | 343 | ||
346 | LOG("- trigger event\n"); |
344 | LOG("- trigger event\n"); |
347 | 345 | ||
348 | call = THREAD->udebug.go_call; |
346 | call = THREAD->udebug.go_call; |
349 | THREAD->udebug.go_call = NULL; |
347 | THREAD->udebug.go_call = NULL; |
350 | IPC_SET_RETVAL(call->data, 0); |
348 | IPC_SET_RETVAL(call->data, 0); |
351 | IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_B); |
349 | IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_B); |
352 | IPC_SET_ARG2(call->data, (unative_t)t); |
350 | IPC_SET_ARG2(call->data, (unative_t)t); |
353 | 351 | ||
354 | /* |
352 | /* |
355 | * Make sure udebug.go is false when going to sleep |
353 | * Make sure udebug.go is false when going to sleep |
356 | * in case we get woken up by DEBUG_END. (At which |
354 | * in case we get woken up by DEBUG_END. (At which |
357 | * point it must be back to the initial true value). |
355 | * point it must be back to the initial true value). |
358 | */ |
356 | */ |
359 | THREAD->udebug.go = false; |
357 | THREAD->udebug.go = false; |
360 | THREAD->udebug.cur_event = UDEBUG_EVENT_THREAD_B; |
358 | THREAD->udebug.cur_event = UDEBUG_EVENT_THREAD_B; |
361 | 359 | ||
362 | ipc_answer(&TASK->answerbox, call); |
360 | ipc_answer(&TASK->answerbox, call); |
363 | 361 | ||
364 | mutex_unlock(&THREAD->udebug.lock); |
362 | mutex_unlock(&THREAD->udebug.lock); |
365 | mutex_unlock(&TASK->udebug.lock); |
363 | mutex_unlock(&TASK->udebug.lock); |
366 | 364 | ||
367 | LOG("- sleep\n"); |
365 | LOG("- sleep\n"); |
368 | udebug_wait_for_go(&THREAD->udebug.go_wq); |
366 | udebug_wait_for_go(&THREAD->udebug.go_wq); |
369 | } |
367 | } |
370 | 368 | ||
371 | /** Thread-termination event hook. |
369 | /** Thread-termination event hook. |
372 | * |
370 | * |
373 | * Must be called when the current thread is terminating. |
371 | * Must be called when the current thread is terminating. |
374 | * Generates a THREAD_E event. |
372 | * Generates a THREAD_E event. |
375 | */ |
373 | */ |
376 | void udebug_thread_e_event(void) |
374 | void udebug_thread_e_event(void) |
377 | { |
375 | { |
378 | call_t *call; |
376 | call_t *call; |
379 | 377 | ||
380 | mutex_lock(&TASK->udebug.lock); |
378 | mutex_lock(&TASK->udebug.lock); |
381 | mutex_lock(&THREAD->udebug.lock); |
379 | mutex_lock(&THREAD->udebug.lock); |
382 | 380 | ||
383 | LOG("udebug_thread_e_event\n"); |
381 | LOG("udebug_thread_e_event\n"); |
384 | LOG("- check state\n"); |
382 | LOG("- check state\n"); |
385 | 383 | ||
386 | /* Must only generate events when in debugging session. */ |
384 | /* Must only generate events when in debugging session. */ |
387 | if (THREAD->udebug.debug_active != true) { |
385 | if (THREAD->udebug.active != true) { |
388 | /* printf("- debug_active: %s, udebug.go: %s\n", |
386 | /* printf("- udebug.active: %s, udebug.go: %s\n", |
389 | THREAD->udebug.debug_active ? "yes(+)" : "no(-)", |
387 | THREAD->udebug.active ? "yes(+)" : "no(-)", |
390 | THREAD->udebug.go ? "yes(-)" : "no(+)");*/ |
388 | THREAD->udebug.go ? "yes(-)" : "no(+)");*/ |
391 | mutex_unlock(&THREAD->udebug.lock); |
389 | mutex_unlock(&THREAD->udebug.lock); |
392 | mutex_unlock(&TASK->udebug.lock); |
390 | mutex_unlock(&TASK->udebug.lock); |
393 | return; |
391 | return; |
394 | } |
392 | } |
395 | 393 | ||
396 | LOG("- trigger event\n"); |
394 | LOG("- trigger event\n"); |
397 | 395 | ||
398 | call = THREAD->udebug.go_call; |
396 | call = THREAD->udebug.go_call; |
399 | THREAD->udebug.go_call = NULL; |
397 | THREAD->udebug.go_call = NULL; |
400 | IPC_SET_RETVAL(call->data, 0); |
398 | IPC_SET_RETVAL(call->data, 0); |
401 | IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_E); |
399 | IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_E); |
402 | 400 | ||
403 | /* Prevent any further debug activity in thread. */ |
401 | /* Prevent any further debug activity in thread. */ |
404 | THREAD->udebug.debug_active = false; |
402 | THREAD->udebug.active = false; |
405 | THREAD->udebug.cur_event = 0; /* none */ |
403 | THREAD->udebug.cur_event = 0; /* none */ |
406 | THREAD->udebug.go = false; /* set to initial value */ |
404 | THREAD->udebug.go = false; /* set to initial value */ |
407 | 405 | ||
408 | ipc_answer(&TASK->answerbox, call); |
406 | ipc_answer(&TASK->answerbox, call); |
409 | 407 | ||
410 | mutex_unlock(&THREAD->udebug.lock); |
408 | mutex_unlock(&THREAD->udebug.lock); |
411 | mutex_unlock(&TASK->udebug.lock); |
409 | mutex_unlock(&TASK->udebug.lock); |
412 | 410 | ||
413 | /* |
411 | /* |
414 | * This event does not sleep - debugging has finished |
412 | * This event does not sleep - debugging has finished |
415 | * in this thread. |
413 | * in this thread. |
416 | */ |
414 | */ |
417 | } |
415 | } |
418 | 416 | ||
419 | static void breakpoint_trap_event(uintptr_t addr, udebug_event_t etype) |
417 | static void breakpoint_trap_event(uintptr_t addr, udebug_event_t etype) |
420 | { |
418 | { |
421 | call_t *call; |
419 | call_t *call; |
422 | 420 | ||
423 | mutex_lock(&TASK->udebug.lock); |
421 | mutex_lock(&TASK->udebug.lock); |
424 | mutex_lock(&THREAD->udebug.lock); |
422 | mutex_lock(&THREAD->udebug.lock); |
425 | 423 | ||
426 | /* Must only generate events when in debugging session and have go. */ |
424 | /* Must only generate events when in debugging session and have go. */ |
427 | if (THREAD->udebug.debug_active != true || |
- | |
428 | THREAD->udebug.go == false) { |
425 | if (THREAD->udebug.active != true || THREAD->udebug.go == false) { |
429 | mutex_unlock(&THREAD->udebug.lock); |
426 | mutex_unlock(&THREAD->udebug.lock); |
430 | mutex_unlock(&TASK->udebug.lock); |
427 | mutex_unlock(&TASK->udebug.lock); |
431 | return; |
428 | return; |
432 | } |
429 | } |
433 | 430 | ||
434 | /* Verify that the event is enabled */ |
431 | /* Verify that the event is enabled */ |
435 | if ((TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) { |
432 | if ((TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) { |
436 | mutex_unlock(&THREAD->udebug.lock); |
433 | mutex_unlock(&THREAD->udebug.lock); |
437 | mutex_unlock(&TASK->udebug.lock); |
434 | mutex_unlock(&TASK->udebug.lock); |
438 | return; |
435 | return; |
439 | } |
436 | } |
440 | 437 | ||
441 | LOG("udebug_breakpoint/trap_event\n"); |
438 | LOG("udebug_breakpoint/trap_event\n"); |
442 | call = THREAD->udebug.go_call; |
439 | call = THREAD->udebug.go_call; |
443 | THREAD->udebug.go_call = NULL; |
440 | THREAD->udebug.go_call = NULL; |
444 | 441 | ||
445 | IPC_SET_RETVAL(call->data, 0); |
442 | IPC_SET_RETVAL(call->data, 0); |
446 | IPC_SET_ARG1(call->data, etype); |
443 | IPC_SET_ARG1(call->data, etype); |
447 | IPC_SET_ARG2(call->data, addr); |
444 | IPC_SET_ARG2(call->data, addr); |
448 | 445 | ||
449 | /* |
446 | /* |
450 | * Make sure udebug.go is false when going to sleep |
447 | * Make sure udebug.go is false when going to sleep |
451 | * in case we get woken up by DEBUG_END. (At which |
448 | * in case we get woken up by DEBUG_END. (At which |
452 | * point it must be back to the initial true value). |
449 | * point it must be back to the initial true value). |
453 | */ |
450 | */ |
454 | THREAD->udebug.go = false; |
451 | THREAD->udebug.go = false; |
455 | THREAD->udebug.cur_event = etype; |
452 | THREAD->udebug.cur_event = etype; |
456 | 453 | ||
457 | LOG("- send answer\n"); |
454 | LOG("- send answer\n"); |
458 | ipc_answer(&TASK->answerbox, call); |
455 | ipc_answer(&TASK->answerbox, call); |
459 | 456 | ||
460 | mutex_unlock(&THREAD->udebug.lock); |
457 | mutex_unlock(&THREAD->udebug.lock); |
461 | mutex_unlock(&TASK->udebug.lock); |
458 | mutex_unlock(&TASK->udebug.lock); |
462 | 459 | ||
463 | udebug_wait_for_go(&THREAD->udebug.go_wq); |
460 | udebug_wait_for_go(&THREAD->udebug.go_wq); |
464 | } |
461 | } |
465 | 462 | ||
466 | void udebug_breakpoint_event(uintptr_t addr) |
463 | void udebug_breakpoint_event(uintptr_t addr) |
467 | { |
464 | { |
468 | breakpoint_trap_event(addr, UDEBUG_EVENT_BREAKPOINT); |
465 | breakpoint_trap_event(addr, UDEBUG_EVENT_BREAKPOINT); |
469 | } |
466 | } |
470 | 467 | ||
471 | void udebug_trap_event(uintptr_t addr) |
468 | void udebug_trap_event(uintptr_t addr) |
472 | { |
469 | { |
473 | breakpoint_trap_event(addr, UDEBUG_EVENT_TRAP); |
470 | breakpoint_trap_event(addr, UDEBUG_EVENT_TRAP); |
474 | } |
471 | } |
475 | 472 | ||
476 | /** |
473 | /** |
477 | * Terminate task debugging session. |
474 | * Terminate task debugging session. |
478 | * |
475 | * |
479 | * Gracefully terminates the debugging session for a task. If the debugger |
476 | * Gracefully terminates the debugging session for a task. If the debugger |
480 | * is still waiting for events on some threads, it will receive a |
477 | * is still waiting for events on some threads, it will receive a |
481 | * FINISHED event for each of them. |
478 | * FINISHED event for each of them. |
482 | * |
479 | * |
483 | * @param ta Task structure. ta->udebug.lock must be already locked. |
480 | * @param ta Task structure. ta->udebug.lock must be already locked. |
484 | * @return Zero on success or negative error code. |
481 | * @return Zero on success or negative error code. |
485 | */ |
482 | */ |
486 | int udebug_task_cleanup(struct task *ta) |
483 | int udebug_task_cleanup(struct task *ta) |
487 | { |
484 | { |
488 | thread_t *t; |
485 | thread_t *t; |
489 | link_t *cur; |
486 | link_t *cur; |
490 | int flags; |
487 | int flags; |
491 | ipl_t ipl; |
488 | ipl_t ipl; |
492 | 489 | ||
493 | LOG("udebug_task_cleanup()\n"); |
490 | LOG("udebug_task_cleanup()\n"); |
494 | LOG("task %" PRIu64 "\n", ta->taskid); |
491 | LOG("task %" PRIu64 "\n", ta->taskid); |
495 | 492 | ||
496 | if (ta->udebug.dt_state != UDEBUG_TS_BEGINNING && |
493 | if (ta->udebug.dt_state != UDEBUG_TS_BEGINNING && |
497 | ta->udebug.dt_state != UDEBUG_TS_ACTIVE) { |
494 | ta->udebug.dt_state != UDEBUG_TS_ACTIVE) { |
498 | LOG("udebug_task_cleanup(): task not being debugged\n"); |
495 | LOG("udebug_task_cleanup(): task not being debugged\n"); |
499 | return EINVAL; |
496 | return EINVAL; |
500 | } |
497 | } |
501 | 498 | ||
502 | /* Finish debugging of all userspace threads */ |
499 | /* Finish debugging of all userspace threads */ |
503 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) { |
500 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) { |
504 | t = list_get_instance(cur, thread_t, th_link); |
501 | t = list_get_instance(cur, thread_t, th_link); |
505 | 502 | ||
506 | mutex_lock(&t->udebug.lock); |
503 | mutex_lock(&t->udebug.lock); |
507 | 504 | ||
508 | ipl = interrupts_disable(); |
505 | ipl = interrupts_disable(); |
509 | spinlock_lock(&t->lock); |
506 | spinlock_lock(&t->lock); |
510 | 507 | ||
511 | flags = t->flags; |
508 | flags = t->flags; |
512 | 509 | ||
513 | spinlock_unlock(&t->lock); |
510 | spinlock_unlock(&t->lock); |
514 | interrupts_restore(ipl); |
511 | interrupts_restore(ipl); |
515 | 512 | ||
516 | /* Only process userspace threads. */ |
513 | /* Only process userspace threads. */ |
517 | if ((flags & THREAD_FLAG_USPACE) != 0) { |
514 | if ((flags & THREAD_FLAG_USPACE) != 0) { |
518 | /* Prevent any further debug activity in thread. */ |
515 | /* Prevent any further debug activity in thread. */ |
519 | t->udebug.debug_active = false; |
516 | t->udebug.active = false; |
520 | t->udebug.cur_event = 0; /* none */ |
517 | t->udebug.cur_event = 0; /* none */ |
521 | 518 | ||
522 | /* Is the thread still go? */ |
519 | /* Is the thread still go? */ |
523 | if (t->udebug.go == true) { |
520 | if (t->udebug.go == true) { |
524 | /* |
521 | /* |
525 | * Yes, so clear go. As debug_active == false, |
522 | * Yes, so clear go. As active == false, |
526 | * this doesn't affect anything. |
523 | * this doesn't affect anything. |
527 | */ |
524 | */ |
528 | t->udebug.go = false; |
525 | t->udebug.go = false; |
529 | 526 | ||
530 | /* Answer GO call */ |
527 | /* Answer GO call */ |
531 | LOG("answer GO call with EVENT_FINISHED\n"); |
528 | LOG("answer GO call with EVENT_FINISHED\n"); |
532 | IPC_SET_RETVAL(t->udebug.go_call->data, 0); |
529 | IPC_SET_RETVAL(t->udebug.go_call->data, 0); |
533 | IPC_SET_ARG1(t->udebug.go_call->data, |
530 | IPC_SET_ARG1(t->udebug.go_call->data, |
534 | UDEBUG_EVENT_FINISHED); |
531 | UDEBUG_EVENT_FINISHED); |
535 | 532 | ||
536 | ipc_answer(&ta->answerbox, t->udebug.go_call); |
533 | ipc_answer(&ta->answerbox, t->udebug.go_call); |
537 | t->udebug.go_call = NULL; |
534 | t->udebug.go_call = NULL; |
538 | } else { |
535 | } else { |
539 | /* |
536 | /* |
540 | * Debug_stop is already at initial value. |
537 | * Debug_stop is already at initial value. |
541 | * Yet this means the thread needs waking up. |
538 | * Yet this means the thread needs waking up. |
542 | */ |
539 | */ |
543 | 540 | ||
544 | /* |
541 | /* |
545 | * t's lock must not be held when calling |
542 | * t's lock must not be held when calling |
546 | * waitq_wakeup. |
543 | * waitq_wakeup. |
547 | */ |
544 | */ |
548 | waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST); |
545 | waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST); |
549 | } |
546 | } |
550 | } |
547 | } |
551 | mutex_unlock(&t->udebug.lock); |
548 | mutex_unlock(&t->udebug.lock); |
552 | } |
549 | } |
553 | 550 | ||
554 | ta->udebug.dt_state = UDEBUG_TS_INACTIVE; |
551 | ta->udebug.dt_state = UDEBUG_TS_INACTIVE; |
555 | ta->udebug.debugger = NULL; |
552 | ta->udebug.debugger = NULL; |
556 | 553 | ||
557 | return 0; |
554 | return 0; |
558 | } |
555 | } |
559 | 556 | ||
560 | 557 | ||
561 | /** @} |
558 | /** @} |
562 | */ |
559 | */ |
563 | 560 |