Rev 3438 | Rev 3457 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3438 | Rev 3441 | ||
---|---|---|---|
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 operations. |
35 | * @brief Udebug operations. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <print.h> |
38 | #include <debug.h> |
39 | #include <proc/task.h> |
39 | #include <proc/task.h> |
40 | #include <proc/thread.h> |
40 | #include <proc/thread.h> |
41 | #include <arch.h> |
41 | #include <arch.h> |
42 | #include <errno.h> |
42 | #include <errno.h> |
43 | #include <syscall/copy.h> |
43 | #include <syscall/copy.h> |
44 | #include <ipc/ipc.h> |
44 | #include <ipc/ipc.h> |
45 | #include <udebug/udebug.h> |
45 | #include <udebug/udebug.h> |
46 | #include <udebug/udebug_ops.h> |
46 | #include <udebug/udebug_ops.h> |
47 | 47 | ||
48 | /** |
48 | /** |
49 | * Prepare a thread for a debugging operation. |
49 | * Prepare a thread for a debugging operation. |
50 | * |
50 | * |
51 | * Simply put, return thread t with t->udebug.lock held, |
51 | * Simply put, return thread t with t->udebug.lock held, |
52 | * but only if it verifies all conditions. |
52 | * but only if it verifies all conditions. |
53 | * |
53 | * |
54 | * Specifically, verifies that thread t exists, is a userspace thread, |
54 | * Specifically, verifies that thread t exists, is a userspace thread, |
55 | * and belongs to the current task (TASK). Verifies, that the thread |
55 | * and belongs to the current task (TASK). Verifies, that the thread |
56 | * has (or hasn't) go according to having_go (typically false). |
56 | * has (or hasn't) go according to having_go (typically false). |
57 | * It also locks t->udebug.lock, making sure that t->udebug.debug_active |
57 | * It also locks t->udebug.lock, making sure that t->udebug.debug_active |
58 | * is true - that the thread is in a valid debugging session. |
58 | * is true - that the thread is in a valid debugging session. |
59 | * |
59 | * |
60 | * With this verified and the t->udebug.lock mutex held, it is ensured |
60 | * With this verified and the t->udebug.lock mutex held, it is ensured |
61 | * that the thread cannot leave the debugging session, let alone cease |
61 | * that the thread cannot leave the debugging session, let alone cease |
62 | * to exist. |
62 | * to exist. |
63 | * |
63 | * |
64 | * In this function, holding the TASK->udebug.lock mutex prevents the |
64 | * In this function, holding the TASK->udebug.lock mutex prevents the |
65 | * thread from leaving the debugging session, while relaxing from |
65 | * thread from leaving the debugging session, while relaxing from |
66 | * the t->lock spinlock to the t->udebug.lock mutex. |
66 | * the t->lock spinlock to the t->udebug.lock mutex. |
67 | * |
67 | * |
68 | * Returns EOK if all went well, or an error code otherwise. |
68 | * Returns EOK if all went well, or an error code otherwise. |
69 | */ |
69 | */ |
70 | static int _thread_op_begin(thread_t *t, bool having_go) |
70 | static int _thread_op_begin(thread_t *t, bool having_go) |
71 | { |
71 | { |
72 | task_id_t taskid; |
72 | task_id_t taskid; |
73 | ipl_t ipl; |
73 | ipl_t ipl; |
74 | 74 | ||
75 | taskid = TASK->taskid; |
75 | taskid = TASK->taskid; |
76 | 76 | ||
77 | mutex_lock(&TASK->udebug.lock); |
77 | mutex_lock(&TASK->udebug.lock); |
78 | 78 | ||
79 | /* thread_exists() must be called with threads_lock held */ |
79 | /* thread_exists() must be called with threads_lock held */ |
80 | ipl = interrupts_disable(); |
80 | ipl = interrupts_disable(); |
81 | spinlock_lock(&threads_lock); |
81 | spinlock_lock(&threads_lock); |
82 | 82 | ||
83 | if (!thread_exists(t)) { |
83 | if (!thread_exists(t)) { |
84 | spinlock_unlock(&threads_lock); |
84 | spinlock_unlock(&threads_lock); |
85 | interrupts_restore(ipl); |
85 | interrupts_restore(ipl); |
86 | mutex_unlock(&TASK->udebug.lock); |
86 | mutex_unlock(&TASK->udebug.lock); |
87 | return ENOENT; |
87 | return ENOENT; |
88 | } |
88 | } |
89 | 89 | ||
90 | /* t->lock is enough to ensure the thread's existence */ |
90 | /* t->lock is enough to ensure the thread's existence */ |
91 | spinlock_lock(&t->lock); |
91 | spinlock_lock(&t->lock); |
92 | spinlock_unlock(&threads_lock); |
92 | spinlock_unlock(&threads_lock); |
93 | 93 | ||
94 | /* Verify that 't' is a userspace thread */ |
94 | /* Verify that 't' is a userspace thread */ |
95 | if ((t->flags & THREAD_FLAG_USPACE) == 0) { |
95 | if ((t->flags & THREAD_FLAG_USPACE) == 0) { |
96 | /* It's not, deny its existence */ |
96 | /* It's not, deny its existence */ |
97 | spinlock_unlock(&t->lock); |
97 | spinlock_unlock(&t->lock); |
98 | interrupts_restore(ipl); |
98 | interrupts_restore(ipl); |
99 | mutex_unlock(&TASK->udebug.lock); |
99 | mutex_unlock(&TASK->udebug.lock); |
100 | return ENOENT; |
100 | return ENOENT; |
101 | } |
101 | } |
102 | 102 | ||
103 | /* Verify debugging state */ |
103 | /* Verify debugging state */ |
104 | if (t->udebug.debug_active != true) { |
104 | if (t->udebug.debug_active != true) { |
105 | /* Not in debugging session or undesired GO state */ |
105 | /* Not in debugging session or undesired GO state */ |
106 | spinlock_unlock(&t->lock); |
106 | spinlock_unlock(&t->lock); |
107 | interrupts_restore(ipl); |
107 | interrupts_restore(ipl); |
108 | mutex_unlock(&TASK->udebug.lock); |
108 | mutex_unlock(&TASK->udebug.lock); |
109 | return ENOENT; |
109 | return ENOENT; |
110 | } |
110 | } |
111 | 111 | ||
112 | /* |
112 | /* |
113 | * Since the thread has debug_active == true, TASK->udebug.lock |
113 | * Since the thread has debug_active == true, TASK->udebug.lock |
114 | * is enough to ensure its existence and that debug_active remains |
114 | * is enough to ensure its existence and that debug_active remains |
115 | * true. |
115 | * true. |
116 | */ |
116 | */ |
117 | spinlock_unlock(&t->lock); |
117 | spinlock_unlock(&t->lock); |
118 | interrupts_restore(ipl); |
118 | interrupts_restore(ipl); |
119 | 119 | ||
120 | /* Only mutex TASK->udebug.lock left */ |
120 | /* Only mutex TASK->udebug.lock left */ |
121 | 121 | ||
122 | /* Now verify that the thread belongs to the current task */ |
122 | /* Now verify that the thread belongs to the current task */ |
123 | if (t->task != TASK) { |
123 | if (t->task != TASK) { |
124 | /* No such thread belonging this task*/ |
124 | /* No such thread belonging this task*/ |
125 | mutex_unlock(&TASK->udebug.lock); |
125 | mutex_unlock(&TASK->udebug.lock); |
126 | return ENOENT; |
126 | return ENOENT; |
127 | } |
127 | } |
128 | 128 | ||
129 | /* |
129 | /* |
130 | * Now we need to grab the thread's debug lock for synchronization |
130 | * Now we need to grab the thread's debug lock for synchronization |
131 | * of the threads stoppability/stop state. |
131 | * of the threads stoppability/stop state. |
132 | */ |
132 | */ |
133 | mutex_lock(&t->udebug.lock); |
133 | mutex_lock(&t->udebug.lock); |
134 | 134 | ||
135 | /* The big task mutex is no longer needed */ |
135 | /* The big task mutex is no longer needed */ |
136 | mutex_unlock(&TASK->udebug.lock); |
136 | mutex_unlock(&TASK->udebug.lock); |
137 | 137 | ||
138 | if (!t->udebug.stop != having_go) { |
138 | if (!t->udebug.stop != having_go) { |
139 | /* Not in debugging session or undesired GO state */ |
139 | /* Not in debugging session or undesired GO state */ |
140 | mutex_unlock(&t->udebug.lock); |
140 | mutex_unlock(&t->udebug.lock); |
141 | return EINVAL; |
141 | return EINVAL; |
142 | } |
142 | } |
143 | 143 | ||
144 | /* Only t->udebug.lock left */ |
144 | /* Only t->udebug.lock left */ |
145 | 145 | ||
146 | return EOK; /* All went well */ |
146 | return EOK; /* All went well */ |
147 | } |
147 | } |
148 | 148 | ||
149 | 149 | ||
150 | static void _thread_op_end(thread_t *t) |
150 | static void _thread_op_end(thread_t *t) |
151 | { |
151 | { |
152 | mutex_unlock(&t->udebug.lock); |
152 | mutex_unlock(&t->udebug.lock); |
153 | } |
153 | } |
154 | 154 | ||
155 | /** |
155 | /** |
156 | * \return 0 (ok, but not done yet), 1 (done) or negative error code. |
156 | * \return 0 (ok, but not done yet), 1 (done) or negative error code. |
157 | */ |
157 | */ |
158 | int udebug_begin(call_t *call) |
158 | int udebug_begin(call_t *call) |
159 | { |
159 | { |
160 | int reply; |
160 | int reply; |
161 | 161 | ||
162 | thread_t *t; |
162 | thread_t *t; |
163 | link_t *cur; |
163 | link_t *cur; |
164 | 164 | ||
165 | printf("udebug_begin()\n"); |
165 | LOG("udebug_begin()\n"); |
166 | 166 | ||
167 | mutex_lock(&TASK->udebug.lock); |
167 | mutex_lock(&TASK->udebug.lock); |
168 | printf("debugging task %llu\n", TASK->taskid); |
168 | LOG("debugging task %llu\n", TASK->taskid); |
169 | 169 | ||
170 | if (TASK->udebug.dt_state != UDEBUG_TS_INACTIVE) { |
170 | if (TASK->udebug.dt_state != UDEBUG_TS_INACTIVE) { |
171 | mutex_unlock(&TASK->udebug.lock); |
171 | mutex_unlock(&TASK->udebug.lock); |
172 | printf("udebug_begin(): busy error\n"); |
172 | LOG("udebug_begin(): busy error\n"); |
173 | 173 | ||
174 | return EBUSY; |
174 | return EBUSY; |
175 | } |
175 | } |
176 | 176 | ||
177 | TASK->udebug.dt_state = UDEBUG_TS_BEGINNING; |
177 | TASK->udebug.dt_state = UDEBUG_TS_BEGINNING; |
178 | TASK->udebug.begin_call = call; |
178 | TASK->udebug.begin_call = call; |
179 | TASK->udebug.debugger = call->sender; |
179 | TASK->udebug.debugger = call->sender; |
180 | 180 | ||
181 | if (TASK->udebug.not_stoppable_count == 0) { |
181 | if (TASK->udebug.not_stoppable_count == 0) { |
182 | TASK->udebug.dt_state = UDEBUG_TS_ACTIVE; |
182 | TASK->udebug.dt_state = UDEBUG_TS_ACTIVE; |
183 | TASK->udebug.begin_call = NULL; |
183 | TASK->udebug.begin_call = NULL; |
184 | reply = 1; /* immediate reply */ |
184 | reply = 1; /* immediate reply */ |
185 | } else { |
185 | } else { |
186 | reply = 0; /* no reply */ |
186 | reply = 0; /* no reply */ |
187 | } |
187 | } |
188 | 188 | ||
189 | /* Set udebug.debug_active on all of the task's userspace threads */ |
189 | /* Set udebug.debug_active on all of the task's userspace threads */ |
190 | 190 | ||
191 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
191 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
192 | t = list_get_instance(cur, thread_t, th_link); |
192 | t = list_get_instance(cur, thread_t, th_link); |
193 | 193 | ||
194 | mutex_lock(&t->udebug.lock); |
194 | mutex_lock(&t->udebug.lock); |
195 | if ((t->flags & THREAD_FLAG_USPACE) != 0) |
195 | if ((t->flags & THREAD_FLAG_USPACE) != 0) |
196 | t->udebug.debug_active = true; |
196 | t->udebug.debug_active = true; |
197 | mutex_unlock(&t->udebug.lock); |
197 | mutex_unlock(&t->udebug.lock); |
198 | } |
198 | } |
199 | 199 | ||
200 | mutex_unlock(&TASK->udebug.lock); |
200 | mutex_unlock(&TASK->udebug.lock); |
201 | 201 | ||
202 | printf("udebug_begin() done (%s)\n", |
202 | LOG("udebug_begin() done (%s)\n", |
203 | reply ? "reply" : "stoppability wait"); |
203 | reply ? "reply" : "stoppability wait"); |
204 | 204 | ||
205 | return reply; |
205 | return reply; |
206 | } |
206 | } |
207 | 207 | ||
208 | int udebug_end(void) |
208 | int udebug_end(void) |
209 | { |
209 | { |
210 | int rc; |
210 | int rc; |
211 | 211 | ||
212 | printf("udebug_end()\n"); |
212 | LOG("udebug_end()\n"); |
213 | 213 | ||
214 | mutex_lock(&TASK->udebug.lock); |
214 | mutex_lock(&TASK->udebug.lock); |
215 | printf("task %llu\n", TASK->taskid); |
215 | LOG("task %" PRIu64 "\n", TASK->taskid); |
216 | 216 | ||
217 | rc = udebug_task_cleanup(TASK); |
217 | rc = udebug_task_cleanup(TASK); |
218 | 218 | ||
219 | mutex_unlock(&TASK->udebug.lock); |
219 | mutex_unlock(&TASK->udebug.lock); |
220 | 220 | ||
221 | return rc; |
221 | return rc; |
222 | } |
222 | } |
223 | 223 | ||
224 | int udebug_set_evmask(udebug_evmask_t mask) |
224 | int udebug_set_evmask(udebug_evmask_t mask) |
225 | { |
225 | { |
226 | printf("udebug_set_mask()\n"); |
226 | LOG("udebug_set_mask()\n"); |
227 | - | ||
228 | printf("debugging task %llu\n", TASK->taskid); |
- | |
229 | 227 | ||
230 | mutex_lock(&TASK->udebug.lock); |
228 | mutex_lock(&TASK->udebug.lock); |
231 | 229 | ||
232 | if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) { |
230 | if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) { |
233 | mutex_unlock(&TASK->udebug.lock); |
231 | mutex_unlock(&TASK->udebug.lock); |
234 | printf("udebug_set_mask(): not active debuging session\n"); |
232 | LOG("udebug_set_mask(): not active debuging session\n"); |
235 | 233 | ||
236 | return EINVAL; |
234 | return EINVAL; |
237 | } |
235 | } |
238 | 236 | ||
239 | TASK->udebug.evmask = mask; |
237 | TASK->udebug.evmask = mask; |
240 | 238 | ||
241 | mutex_unlock(&TASK->udebug.lock); |
239 | mutex_unlock(&TASK->udebug.lock); |
242 | 240 | ||
243 | return 0; |
241 | return 0; |
244 | } |
242 | } |
245 | 243 | ||
246 | 244 | ||
247 | int udebug_go(thread_t *t, call_t *call) |
245 | int udebug_go(thread_t *t, call_t *call) |
248 | { |
246 | { |
249 | int rc; |
247 | int rc; |
250 | 248 | ||
251 | // printf("udebug_go()\n"); |
- | |
252 | - | ||
253 | /* On success, this will lock t->udebug.lock */ |
249 | /* On success, this will lock t->udebug.lock */ |
254 | rc = _thread_op_begin(t, false); |
250 | rc = _thread_op_begin(t, false); |
255 | if (rc != EOK) { |
251 | if (rc != EOK) { |
256 | return rc; |
252 | return rc; |
257 | } |
253 | } |
258 | 254 | ||
259 | t->udebug.go_call = call; |
255 | t->udebug.go_call = call; |
260 | t->udebug.stop = false; |
256 | t->udebug.stop = false; |
261 | t->udebug.cur_event = 0; /* none */ |
257 | t->udebug.cur_event = 0; /* none */ |
262 | 258 | ||
263 | /* |
259 | /* |
264 | * Neither t's lock nor threads_lock may be held during wakeup |
260 | * Neither t's lock nor threads_lock may be held during wakeup |
265 | */ |
261 | */ |
266 | waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST); |
262 | waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST); |
267 | 263 | ||
268 | _thread_op_end(t); |
264 | _thread_op_end(t); |
269 | 265 | ||
270 | return 0; |
266 | return 0; |
271 | } |
267 | } |
272 | 268 | ||
273 | int udebug_stop(thread_t *t, call_t *call) |
269 | int udebug_stop(thread_t *t, call_t *call) |
274 | { |
270 | { |
275 | int rc; |
271 | int rc; |
276 | 272 | ||
277 | printf("udebug_stop()\n"); |
273 | LOG("udebug_stop()\n"); |
278 | mutex_lock(&TASK->udebug.lock); |
274 | mutex_lock(&TASK->udebug.lock); |
279 | 275 | ||
280 | /* |
276 | /* |
281 | * On success, this will lock t->udebug.lock. Note that this makes sure |
277 | * On success, this will lock t->udebug.lock. Note that this makes sure |
282 | * the thread is not stopped. |
278 | * the thread is not stopped. |
283 | */ |
279 | */ |
284 | rc = _thread_op_begin(t, true); |
280 | rc = _thread_op_begin(t, true); |
285 | if (rc != EOK) { |
281 | if (rc != EOK) { |
286 | return rc; |
282 | return rc; |
287 | } |
283 | } |
288 | 284 | ||
289 | /* Take GO away from the thread */ |
285 | /* Take GO away from the thread */ |
290 | t->udebug.stop = true; |
286 | t->udebug.stop = true; |
291 | 287 | ||
292 | if (!t->udebug.stoppable) { |
288 | if (!t->udebug.stoppable) { |
293 | /* Answer will be sent when the thread becomes stoppable */ |
289 | /* Answer will be sent when the thread becomes stoppable */ |
294 | _thread_op_end(t); |
290 | _thread_op_end(t); |
295 | return 0; |
291 | return 0; |
296 | } |
292 | } |
297 | 293 | ||
298 | /* |
294 | /* |
299 | * Answer GO call |
295 | * Answer GO call |
300 | */ |
296 | */ |
301 | printf("udebug_stop - answering go call\n"); |
297 | LOG("udebug_stop - answering go call\n"); |
302 | 298 | ||
303 | /* Make sure nobody takes this call away from us */ |
299 | /* Make sure nobody takes this call away from us */ |
304 | call = t->udebug.go_call; |
300 | call = t->udebug.go_call; |
305 | t->udebug.go_call = NULL; |
301 | t->udebug.go_call = NULL; |
306 | 302 | ||
307 | IPC_SET_RETVAL(call->data, 0); |
303 | IPC_SET_RETVAL(call->data, 0); |
308 | IPC_SET_ARG1(call->data, UDEBUG_EVENT_STOP); |
304 | IPC_SET_ARG1(call->data, UDEBUG_EVENT_STOP); |
309 | printf("udebug_stop/ipc_answer\n"); |
305 | LOG("udebug_stop/ipc_answer\n"); |
310 | 306 | ||
311 | THREAD->udebug.cur_event = UDEBUG_EVENT_STOP; |
307 | THREAD->udebug.cur_event = UDEBUG_EVENT_STOP; |
312 | 308 | ||
313 | _thread_op_end(t); |
309 | _thread_op_end(t); |
314 | 310 | ||
315 | ipc_answer(&TASK->answerbox, call); |
311 | ipc_answer(&TASK->answerbox, call); |
316 | mutex_unlock(&TASK->udebug.lock); |
312 | mutex_unlock(&TASK->udebug.lock); |
317 | 313 | ||
318 | printf("udebog_stop/done\n"); |
314 | LOG("udebog_stop/done\n"); |
319 | return 0; |
315 | return 0; |
320 | } |
316 | } |
321 | 317 | ||
322 | int udebug_thread_read(void **buffer, size_t buf_size, size_t *n) |
318 | int udebug_thread_read(void **buffer, size_t buf_size, size_t *n) |
323 | { |
319 | { |
324 | thread_t *t; |
320 | thread_t *t; |
325 | link_t *cur; |
321 | link_t *cur; |
326 | unative_t tid; |
322 | unative_t tid; |
327 | unsigned copied_ids; |
323 | unsigned copied_ids; |
328 | ipl_t ipl; |
324 | ipl_t ipl; |
329 | unative_t *id_buffer; |
325 | unative_t *id_buffer; |
330 | int flags; |
326 | int flags; |
331 | size_t max_ids; |
327 | size_t max_ids; |
332 | 328 | ||
333 | printf("udebug_thread_read()\n"); |
329 | LOG("udebug_thread_read()\n"); |
334 | 330 | ||
335 | /* Allocate a buffer to hold thread IDs */ |
331 | /* Allocate a buffer to hold thread IDs */ |
336 | id_buffer = malloc(buf_size, 0); |
332 | id_buffer = malloc(buf_size, 0); |
337 | 333 | ||
338 | mutex_lock(&TASK->udebug.lock); |
334 | mutex_lock(&TASK->udebug.lock); |
339 | 335 | ||
340 | /* Verify task state */ |
336 | /* Verify task state */ |
341 | if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) { |
337 | if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) { |
342 | mutex_unlock(&TASK->udebug.lock); |
338 | mutex_unlock(&TASK->udebug.lock); |
343 | return EINVAL; |
339 | return EINVAL; |
344 | } |
340 | } |
345 | 341 | ||
346 | ipl = interrupts_disable(); |
342 | ipl = interrupts_disable(); |
347 | spinlock_lock(&TASK->lock); |
343 | spinlock_lock(&TASK->lock); |
348 | /* Copy down the thread IDs */ |
344 | /* Copy down the thread IDs */ |
349 | 345 | ||
350 | max_ids = buf_size / sizeof(unative_t); |
346 | max_ids = buf_size / sizeof(unative_t); |
351 | copied_ids = 0; |
347 | copied_ids = 0; |
352 | 348 | ||
353 | /* FIXME: make sure the thread isn't past debug shutdown... */ |
349 | /* FIXME: make sure the thread isn't past debug shutdown... */ |
354 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
350 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
355 | /* Do not write past end of buffer */ |
351 | /* Do not write past end of buffer */ |
356 | if (copied_ids >= max_ids) break; |
352 | if (copied_ids >= max_ids) break; |
357 | 353 | ||
358 | t = list_get_instance(cur, thread_t, th_link); |
354 | t = list_get_instance(cur, thread_t, th_link); |
359 | 355 | ||
360 | spinlock_lock(&t->lock); |
356 | spinlock_lock(&t->lock); |
361 | flags = t->flags; |
357 | flags = t->flags; |
362 | spinlock_unlock(&t->lock); |
358 | spinlock_unlock(&t->lock); |
363 | 359 | ||
364 | /* Not interested in kernel threads */ |
360 | /* Not interested in kernel threads */ |
365 | if ((flags & THREAD_FLAG_USPACE) != 0) { |
361 | if ((flags & THREAD_FLAG_USPACE) != 0) { |
366 | /* Using thread struct pointer as identification hash */ |
362 | /* Using thread struct pointer as identification hash */ |
367 | tid = (unative_t) t; |
363 | tid = (unative_t) t; |
368 | id_buffer[copied_ids++] = tid; |
364 | id_buffer[copied_ids++] = tid; |
369 | } |
365 | } |
370 | } |
366 | } |
371 | 367 | ||
372 | spinlock_unlock(&TASK->lock); |
368 | spinlock_unlock(&TASK->lock); |
373 | interrupts_restore(ipl); |
369 | interrupts_restore(ipl); |
374 | 370 | ||
375 | mutex_unlock(&TASK->udebug.lock); |
371 | mutex_unlock(&TASK->udebug.lock); |
376 | 372 | ||
377 | *buffer = id_buffer; |
373 | *buffer = id_buffer; |
378 | *n = copied_ids * sizeof(unative_t); |
374 | *n = copied_ids * sizeof(unative_t); |
379 | 375 | ||
380 | return 0; |
376 | return 0; |
381 | } |
377 | } |
382 | 378 | ||
383 | int udebug_args_read(thread_t *t, void **buffer) |
379 | int udebug_args_read(thread_t *t, void **buffer) |
384 | { |
380 | { |
385 | int rc; |
381 | int rc; |
386 | unative_t *arg_buffer; |
382 | unative_t *arg_buffer; |
387 | 383 | ||
388 | // printf("udebug_args_read()\n"); |
- | |
389 | - | ||
390 | /* Prepare a buffer to hold the arguments */ |
384 | /* Prepare a buffer to hold the arguments */ |
391 | arg_buffer = malloc(6 * sizeof(unative_t), 0); |
385 | arg_buffer = malloc(6 * sizeof(unative_t), 0); |
392 | 386 | ||
393 | /* On success, this will lock t->udebug.lock */ |
387 | /* On success, this will lock t->udebug.lock */ |
394 | rc = _thread_op_begin(t, false); |
388 | rc = _thread_op_begin(t, false); |
395 | if (rc != EOK) { |
389 | if (rc != EOK) { |
396 | return rc; |
390 | return rc; |
397 | } |
391 | } |
398 | 392 | ||
399 | /* Additionally we need to verify that we are inside a syscall */ |
393 | /* Additionally we need to verify that we are inside a syscall */ |
400 | if (t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_B && |
394 | if (t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_B && |
401 | t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_E) { |
395 | t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_E) { |
402 | _thread_op_end(t); |
396 | _thread_op_end(t); |
403 | return EINVAL; |
397 | return EINVAL; |
404 | } |
398 | } |
405 | 399 | ||
406 | /* Copy to a local buffer before releasing the lock */ |
400 | /* Copy to a local buffer before releasing the lock */ |
407 | memcpy(arg_buffer, t->udebug.syscall_args, 6 * sizeof(unative_t)); |
401 | memcpy(arg_buffer, t->udebug.syscall_args, 6 * sizeof(unative_t)); |
408 | 402 | ||
409 | _thread_op_end(t); |
403 | _thread_op_end(t); |
410 | 404 | ||
411 | *buffer = arg_buffer; |
405 | *buffer = arg_buffer; |
412 | return 0; |
406 | return 0; |
413 | } |
407 | } |
414 | 408 | ||
415 | int udebug_mem_read(unative_t uspace_addr, size_t n, void **buffer) |
409 | int udebug_mem_read(unative_t uspace_addr, size_t n, void **buffer) |
416 | { |
410 | { |
417 | void *data_buffer; |
411 | void *data_buffer; |
418 | int rc; |
412 | int rc; |
419 | 413 | ||
420 | /* Verify task state */ |
414 | /* Verify task state */ |
421 | mutex_lock(&TASK->udebug.lock); |
415 | mutex_lock(&TASK->udebug.lock); |
422 | 416 | ||
423 | if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) { |
417 | if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) { |
424 | mutex_unlock(&TASK->udebug.lock); |
418 | mutex_unlock(&TASK->udebug.lock); |
425 | return EBUSY; |
419 | return EBUSY; |
426 | } |
420 | } |
427 | 421 | ||
428 | data_buffer = malloc(n, 0); |
422 | data_buffer = malloc(n, 0); |
429 | 423 | ||
430 | // printf("udebug_mem_read: src=%u, size=%u\n", uspace_addr, n); |
- | |
431 | - | ||
432 | /* NOTE: this is not strictly from a syscall... but that shouldn't |
424 | /* NOTE: this is not strictly from a syscall... but that shouldn't |
433 | * be a problem */ |
425 | * be a problem */ |
434 | rc = copy_from_uspace(data_buffer, (void *)uspace_addr, n); |
426 | rc = copy_from_uspace(data_buffer, (void *)uspace_addr, n); |
435 | mutex_unlock(&TASK->udebug.lock); |
427 | mutex_unlock(&TASK->udebug.lock); |
436 | 428 | ||
437 | if (rc != 0) return rc; |
429 | if (rc != 0) return rc; |
438 | 430 | ||
439 | *buffer = data_buffer; |
431 | *buffer = data_buffer; |
440 | return 0; |
432 | return 0; |
441 | } |
433 | } |
442 | 434 | ||
443 | /** @} |
435 | /** @} |
444 | */ |
436 | */ |
445 | 437 |