Rev 3127 | Rev 3471 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3127 | Rev 3424 | ||
---|---|---|---|
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 <console/klog.h> |
38 | #include <print.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 | klog_printf("udebug_begin()"); |
165 | printf("udebug_begin()\n"); |
166 | 166 | ||
167 | mutex_lock(&TASK->udebug.lock); |
167 | mutex_lock(&TASK->udebug.lock); |
168 | klog_printf("debugging task %llu", TASK->taskid); |
168 | printf("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 | klog_printf("udebug_begin(): busy error"); |
172 | printf("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 | klog_printf("udebug_begin() done (%s)", |
202 | printf("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 | klog_printf("udebug_end()"); |
212 | printf("udebug_end()\n"); |
213 | 213 | ||
214 | mutex_lock(&TASK->udebug.lock); |
214 | mutex_lock(&TASK->udebug.lock); |
215 | klog_printf("task %llu", TASK->taskid); |
215 | printf("task %llu\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 | klog_printf("udebug_set_mask()"); |
226 | printf("udebug_set_mask()\n"); |
227 | 227 | ||
228 | klog_printf("debugging task %llu", TASK->taskid); |
228 | printf("debugging task %llu\n", TASK->taskid); |
229 | 229 | ||
230 | mutex_lock(&TASK->udebug.lock); |
230 | mutex_lock(&TASK->udebug.lock); |
231 | 231 | ||
232 | if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) { |
232 | if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) { |
233 | mutex_unlock(&TASK->udebug.lock); |
233 | mutex_unlock(&TASK->udebug.lock); |
234 | klog_printf("udebug_set_mask(): not active debuging session"); |
234 | printf("udebug_set_mask(): not active debuging session\n"); |
235 | 235 | ||
236 | return EINVAL; |
236 | return EINVAL; |
237 | } |
237 | } |
238 | 238 | ||
239 | TASK->udebug.evmask = mask; |
239 | TASK->udebug.evmask = mask; |
240 | 240 | ||
241 | mutex_unlock(&TASK->udebug.lock); |
241 | mutex_unlock(&TASK->udebug.lock); |
242 | 242 | ||
243 | return 0; |
243 | return 0; |
244 | } |
244 | } |
245 | 245 | ||
246 | 246 | ||
247 | int udebug_go(thread_t *t, call_t *call) |
247 | int udebug_go(thread_t *t, call_t *call) |
248 | { |
248 | { |
249 | int rc; |
249 | int rc; |
250 | 250 | ||
251 | // klog_printf("udebug_go()"); |
251 | // printf("udebug_go()\n"); |
252 | 252 | ||
253 | /* On success, this will lock t->udebug.lock */ |
253 | /* On success, this will lock t->udebug.lock */ |
254 | rc = _thread_op_begin(t, false); |
254 | rc = _thread_op_begin(t, false); |
255 | if (rc != EOK) { |
255 | if (rc != EOK) { |
256 | return rc; |
256 | return rc; |
257 | } |
257 | } |
258 | 258 | ||
259 | t->udebug.go_call = call; |
259 | t->udebug.go_call = call; |
260 | t->udebug.stop = false; |
260 | t->udebug.stop = false; |
261 | t->udebug.cur_event = 0; /* none */ |
261 | t->udebug.cur_event = 0; /* none */ |
262 | 262 | ||
263 | /* |
263 | /* |
264 | * Neither t's lock nor threads_lock may be held during wakeup |
264 | * Neither t's lock nor threads_lock may be held during wakeup |
265 | */ |
265 | */ |
266 | waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST); |
266 | waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST); |
267 | 267 | ||
268 | _thread_op_end(t); |
268 | _thread_op_end(t); |
269 | 269 | ||
270 | return 0; |
270 | return 0; |
271 | } |
271 | } |
272 | 272 | ||
273 | int udebug_stop(thread_t *t, call_t *call) |
273 | int udebug_stop(thread_t *t, call_t *call) |
274 | { |
274 | { |
275 | int rc; |
275 | int rc; |
276 | 276 | ||
277 | klog_printf("udebug_stop()"); |
277 | printf("udebug_stop()\n"); |
278 | mutex_lock(&TASK->udebug.lock); |
278 | mutex_lock(&TASK->udebug.lock); |
279 | 279 | ||
280 | /* |
280 | /* |
281 | * On success, this will lock t->udebug.lock. Note that this makes sure |
281 | * On success, this will lock t->udebug.lock. Note that this makes sure |
282 | * the thread is not stopped. |
282 | * the thread is not stopped. |
283 | */ |
283 | */ |
284 | rc = _thread_op_begin(t, true); |
284 | rc = _thread_op_begin(t, true); |
285 | if (rc != EOK) { |
285 | if (rc != EOK) { |
286 | return rc; |
286 | return rc; |
287 | } |
287 | } |
288 | 288 | ||
289 | /* Take GO away from the thread */ |
289 | /* Take GO away from the thread */ |
290 | t->udebug.stop = true; |
290 | t->udebug.stop = true; |
291 | 291 | ||
292 | if (!t->udebug.stoppable) { |
292 | if (!t->udebug.stoppable) { |
293 | /* Answer will be sent when the thread becomes stoppable */ |
293 | /* Answer will be sent when the thread becomes stoppable */ |
294 | _thread_op_end(t); |
294 | _thread_op_end(t); |
295 | return 0; |
295 | return 0; |
296 | } |
296 | } |
297 | 297 | ||
298 | /* |
298 | /* |
299 | * Answer GO call |
299 | * Answer GO call |
300 | */ |
300 | */ |
301 | klog_printf("udebug_stop - answering go call"); |
301 | printf("udebug_stop - answering go call\n"); |
302 | 302 | ||
303 | /* Make sure nobody takes this call away from us */ |
303 | /* Make sure nobody takes this call away from us */ |
304 | call = t->udebug.go_call; |
304 | call = t->udebug.go_call; |
305 | t->udebug.go_call = NULL; |
305 | t->udebug.go_call = NULL; |
306 | 306 | ||
307 | IPC_SET_RETVAL(call->data, 0); |
307 | IPC_SET_RETVAL(call->data, 0); |
308 | IPC_SET_ARG1(call->data, UDEBUG_EVENT_STOP); |
308 | IPC_SET_ARG1(call->data, UDEBUG_EVENT_STOP); |
309 | klog_printf("udebug_stop/ipc_answer"); |
309 | printf("udebug_stop/ipc_answer\n"); |
310 | 310 | ||
311 | THREAD->udebug.cur_event = UDEBUG_EVENT_STOP; |
311 | THREAD->udebug.cur_event = UDEBUG_EVENT_STOP; |
312 | 312 | ||
313 | _thread_op_end(t); |
313 | _thread_op_end(t); |
314 | 314 | ||
315 | ipc_answer(&TASK->answerbox, call); |
315 | ipc_answer(&TASK->answerbox, call); |
316 | mutex_unlock(&TASK->udebug.lock); |
316 | mutex_unlock(&TASK->udebug.lock); |
317 | 317 | ||
318 | klog_printf("udebog_stop/done"); |
318 | printf("udebog_stop/done\n"); |
319 | return 0; |
319 | return 0; |
320 | } |
320 | } |
321 | 321 | ||
322 | int udebug_thread_read(void **buffer, size_t buf_size, size_t *n) |
322 | int udebug_thread_read(void **buffer, size_t buf_size, size_t *n) |
323 | { |
323 | { |
324 | thread_t *t; |
324 | thread_t *t; |
325 | link_t *cur; |
325 | link_t *cur; |
326 | unative_t tid; |
326 | unative_t tid; |
327 | unsigned copied_ids; |
327 | unsigned copied_ids; |
328 | ipl_t ipl; |
328 | ipl_t ipl; |
329 | unative_t *id_buffer; |
329 | unative_t *id_buffer; |
330 | int flags; |
330 | int flags; |
331 | size_t max_ids; |
331 | size_t max_ids; |
332 | 332 | ||
333 | klog_printf("udebug_thread_read()"); |
333 | printf("udebug_thread_read()\n"); |
334 | 334 | ||
335 | /* Allocate a buffer to hold thread IDs */ |
335 | /* Allocate a buffer to hold thread IDs */ |
336 | id_buffer = malloc(buf_size, 0); |
336 | id_buffer = malloc(buf_size, 0); |
337 | 337 | ||
338 | mutex_lock(&TASK->udebug.lock); |
338 | mutex_lock(&TASK->udebug.lock); |
339 | 339 | ||
340 | /* Verify task state */ |
340 | /* Verify task state */ |
341 | if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) { |
341 | if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) { |
342 | mutex_unlock(&TASK->udebug.lock); |
342 | mutex_unlock(&TASK->udebug.lock); |
343 | return EINVAL; |
343 | return EINVAL; |
344 | } |
344 | } |
345 | 345 | ||
346 | ipl = interrupts_disable(); |
346 | ipl = interrupts_disable(); |
347 | spinlock_lock(&TASK->lock); |
347 | spinlock_lock(&TASK->lock); |
348 | /* Copy down the thread IDs */ |
348 | /* Copy down the thread IDs */ |
349 | 349 | ||
350 | max_ids = buf_size / sizeof(unative_t); |
350 | max_ids = buf_size / sizeof(unative_t); |
351 | copied_ids = 0; |
351 | copied_ids = 0; |
352 | 352 | ||
353 | /* FIXME: make sure the thread isn't past debug shutdown... */ |
353 | /* FIXME: make sure the thread isn't past debug shutdown... */ |
354 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
354 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
355 | /* Do not write past end of buffer */ |
355 | /* Do not write past end of buffer */ |
356 | if (copied_ids >= max_ids) break; |
356 | if (copied_ids >= max_ids) break; |
357 | 357 | ||
358 | t = list_get_instance(cur, thread_t, th_link); |
358 | t = list_get_instance(cur, thread_t, th_link); |
359 | 359 | ||
360 | spinlock_lock(&t->lock); |
360 | spinlock_lock(&t->lock); |
361 | flags = t->flags; |
361 | flags = t->flags; |
362 | spinlock_unlock(&t->lock); |
362 | spinlock_unlock(&t->lock); |
363 | 363 | ||
364 | /* Not interested in kernel threads */ |
364 | /* Not interested in kernel threads */ |
365 | if ((flags & THREAD_FLAG_USPACE) != 0) { |
365 | if ((flags & THREAD_FLAG_USPACE) != 0) { |
366 | /* Using thread struct pointer as identification hash */ |
366 | /* Using thread struct pointer as identification hash */ |
367 | tid = (unative_t) t; |
367 | tid = (unative_t) t; |
368 | id_buffer[copied_ids++] = tid; |
368 | id_buffer[copied_ids++] = tid; |
369 | } |
369 | } |
370 | } |
370 | } |
371 | 371 | ||
372 | spinlock_unlock(&TASK->lock); |
372 | spinlock_unlock(&TASK->lock); |
373 | interrupts_restore(ipl); |
373 | interrupts_restore(ipl); |
374 | 374 | ||
375 | mutex_unlock(&TASK->udebug.lock); |
375 | mutex_unlock(&TASK->udebug.lock); |
376 | 376 | ||
377 | *buffer = id_buffer; |
377 | *buffer = id_buffer; |
378 | *n = copied_ids * sizeof(unative_t); |
378 | *n = copied_ids * sizeof(unative_t); |
379 | 379 | ||
380 | return 0; |
380 | return 0; |
381 | } |
381 | } |
382 | 382 | ||
383 | int udebug_args_read(thread_t *t, void **buffer) |
383 | int udebug_args_read(thread_t *t, void **buffer) |
384 | { |
384 | { |
385 | int rc; |
385 | int rc; |
386 | unative_t *arg_buffer; |
386 | unative_t *arg_buffer; |
387 | 387 | ||
388 | // klog_printf("udebug_args_read()"); |
388 | // printf("udebug_args_read()\n"); |
389 | 389 | ||
390 | /* Prepare a buffer to hold the arguments */ |
390 | /* Prepare a buffer to hold the arguments */ |
391 | arg_buffer = malloc(6 * sizeof(unative_t), 0); |
391 | arg_buffer = malloc(6 * sizeof(unative_t), 0); |
392 | 392 | ||
393 | /* On success, this will lock t->udebug.lock */ |
393 | /* On success, this will lock t->udebug.lock */ |
394 | rc = _thread_op_begin(t, false); |
394 | rc = _thread_op_begin(t, false); |
395 | if (rc != EOK) { |
395 | if (rc != EOK) { |
396 | return rc; |
396 | return rc; |
397 | } |
397 | } |
398 | 398 | ||
399 | /* Additionally we need to verify that we are inside a syscall */ |
399 | /* Additionally we need to verify that we are inside a syscall */ |
400 | if (t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_B && |
400 | if (t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_B && |
401 | t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_E) { |
401 | t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_E) { |
402 | _thread_op_end(t); |
402 | _thread_op_end(t); |
403 | return EINVAL; |
403 | return EINVAL; |
404 | } |
404 | } |
405 | 405 | ||
406 | /* Copy to a local buffer before releasing the lock */ |
406 | /* Copy to a local buffer before releasing the lock */ |
407 | memcpy(arg_buffer, t->udebug.syscall_args, 6 * sizeof(unative_t)); |
407 | memcpy(arg_buffer, t->udebug.syscall_args, 6 * sizeof(unative_t)); |
408 | 408 | ||
409 | _thread_op_end(t); |
409 | _thread_op_end(t); |
410 | 410 | ||
411 | *buffer = arg_buffer; |
411 | *buffer = arg_buffer; |
412 | return 0; |
412 | return 0; |
413 | } |
413 | } |
414 | 414 | ||
415 | int udebug_regs_read(thread_t *t, void *buffer) |
415 | int udebug_regs_read(thread_t *t, void *buffer) |
416 | { |
416 | { |
417 | istate_t *state; |
417 | istate_t *state; |
418 | int rc; |
418 | int rc; |
419 | 419 | ||
420 | // klog_printf("udebug_regs_read()"); |
420 | // printf("udebug_regs_read()\n"); |
421 | 421 | ||
422 | /* On success, this will lock t->udebug.lock */ |
422 | /* On success, this will lock t->udebug.lock */ |
423 | rc = _thread_op_begin(t, false); |
423 | rc = _thread_op_begin(t, false); |
424 | if (rc != EOK) { |
424 | if (rc != EOK) { |
425 | return rc; |
425 | return rc; |
426 | } |
426 | } |
427 | 427 | ||
428 | state = t->udebug.uspace_state; |
428 | state = t->udebug.uspace_state; |
429 | if (state == NULL) { |
429 | if (state == NULL) { |
430 | _thread_op_end(t); |
430 | _thread_op_end(t); |
431 | klog_printf("udebug_regs_read() - istate not available"); |
431 | printf("udebug_regs_read() - istate not available\n"); |
432 | return EBUSY; |
432 | return EBUSY; |
433 | } |
433 | } |
434 | 434 | ||
435 | /* Copy to the allocated buffer */ |
435 | /* Copy to the allocated buffer */ |
436 | memcpy(buffer, state, sizeof(istate_t)); |
436 | memcpy(buffer, state, sizeof(istate_t)); |
437 | 437 | ||
438 | _thread_op_end(t); |
438 | _thread_op_end(t); |
439 | 439 | ||
440 | return 0; |
440 | return 0; |
441 | } |
441 | } |
442 | 442 | ||
443 | int udebug_regs_write(thread_t *t, void *buffer) |
443 | int udebug_regs_write(thread_t *t, void *buffer) |
444 | { |
444 | { |
445 | int rc; |
445 | int rc; |
446 | istate_t *state; |
446 | istate_t *state; |
447 | 447 | ||
448 | klog_printf("udebug_regs_write()"); |
448 | printf("udebug_regs_write()\n"); |
449 | 449 | ||
450 | /* Try to change the thread's uspace_state */ |
450 | /* Try to change the thread's uspace_state */ |
451 | 451 | ||
452 | /* On success, this will lock t->udebug.lock */ |
452 | /* On success, this will lock t->udebug.lock */ |
453 | rc = _thread_op_begin(t, false); |
453 | rc = _thread_op_begin(t, false); |
454 | if (rc != EOK) { |
454 | if (rc != EOK) { |
455 | klog_printf("error locking thread"); |
455 | printf("error locking thread\n"); |
456 | return rc; |
456 | return rc; |
457 | } |
457 | } |
458 | 458 | ||
459 | state = t->udebug.uspace_state; |
459 | state = t->udebug.uspace_state; |
460 | if (state == NULL) { |
460 | if (state == NULL) { |
461 | _thread_op_end(t); |
461 | _thread_op_end(t); |
462 | klog_printf("udebug_regs_write() - istate not available"); |
462 | printf("udebug_regs_write() - istate not available\n"); |
463 | return EBUSY; |
463 | return EBUSY; |
464 | } |
464 | } |
465 | 465 | ||
466 | memcpy(t->udebug.uspace_state, buffer, sizeof(istate_t)); |
466 | memcpy(t->udebug.uspace_state, buffer, sizeof(istate_t)); |
467 | 467 | ||
468 | _thread_op_end(t); |
468 | _thread_op_end(t); |
469 | 469 | ||
470 | return 0; |
470 | return 0; |
471 | } |
471 | } |
472 | 472 | ||
473 | 473 | ||
474 | int udebug_mem_read(unative_t uspace_addr, size_t n, void **buffer) |
474 | int udebug_mem_read(unative_t uspace_addr, size_t n, void **buffer) |
475 | { |
475 | { |
476 | void *data_buffer; |
476 | void *data_buffer; |
477 | int rc; |
477 | int rc; |
478 | 478 | ||
479 | /* Verify task state */ |
479 | /* Verify task state */ |
480 | mutex_lock(&TASK->udebug.lock); |
480 | mutex_lock(&TASK->udebug.lock); |
481 | 481 | ||
482 | if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) { |
482 | if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) { |
483 | mutex_unlock(&TASK->udebug.lock); |
483 | mutex_unlock(&TASK->udebug.lock); |
484 | return EBUSY; |
484 | return EBUSY; |
485 | } |
485 | } |
486 | 486 | ||
487 | data_buffer = malloc(n, 0); |
487 | data_buffer = malloc(n, 0); |
488 | 488 | ||
489 | // klog_printf("udebug_mem_read: src=%u, size=%u", uspace_addr, n); |
489 | // printf("udebug_mem_read: src=%u, size=%u\n", uspace_addr, n); |
490 | 490 | ||
491 | /* NOTE: this is not strictly from a syscall... but that shouldn't |
491 | /* NOTE: this is not strictly from a syscall... but that shouldn't |
492 | * be a problem */ |
492 | * be a problem */ |
493 | rc = copy_from_uspace(data_buffer, (void *)uspace_addr, n); |
493 | rc = copy_from_uspace(data_buffer, (void *)uspace_addr, n); |
494 | mutex_unlock(&TASK->udebug.lock); |
494 | mutex_unlock(&TASK->udebug.lock); |
495 | 495 | ||
496 | if (rc != 0) return rc; |
496 | if (rc != 0) return rc; |
497 | 497 | ||
498 | *buffer = data_buffer; |
498 | *buffer = data_buffer; |
499 | return 0; |
499 | return 0; |
500 | } |
500 | } |
501 | 501 | ||
502 | int udebug_mem_write(unative_t uspace_addr, void *data, size_t n) |
502 | int udebug_mem_write(unative_t uspace_addr, void *data, size_t n) |
503 | { |
503 | { |
504 | int rc; |
504 | int rc; |
505 | 505 | ||
506 | klog_printf("udebug_mem_write()"); |
506 | printf("udebug_mem_write()\n"); |
507 | 507 | ||
508 | /* n must be positive */ |
508 | /* n must be positive */ |
509 | if (n < 1) |
509 | if (n < 1) |
510 | return EINVAL; |
510 | return EINVAL; |
511 | 511 | ||
512 | /* Verify task state */ |
512 | /* Verify task state */ |
513 | mutex_lock(&TASK->udebug.lock); |
513 | mutex_lock(&TASK->udebug.lock); |
514 | 514 | ||
515 | if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) { |
515 | if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) { |
516 | mutex_unlock(&TASK->udebug.lock); |
516 | mutex_unlock(&TASK->udebug.lock); |
517 | return EBUSY; |
517 | return EBUSY; |
518 | } |
518 | } |
519 | 519 | ||
520 | klog_printf("dst=%u, size=%u", uspace_addr, n); |
520 | printf("dst=%u, size=%u\n", uspace_addr, n); |
521 | 521 | ||
522 | /* NOTE: this is not strictly from a syscall... but that shouldn't |
522 | /* NOTE: this is not strictly from a syscall... but that shouldn't |
523 | * be a problem */ |
523 | * be a problem */ |
524 | // rc = copy_to_uspace((void *)uspace_addr, data, n); |
524 | // rc = copy_to_uspace((void *)uspace_addr, data, n); |
525 | // if (rc) return rc; |
525 | // if (rc) return rc; |
526 | 526 | ||
527 | rc = as_debug_write(uspace_addr, data, n); |
527 | rc = as_debug_write(uspace_addr, data, n); |
528 | 528 | ||
529 | klog_printf("rc=%d\n", rc); |
529 | printf("rc=%d\n", rc); |
530 | 530 | ||
531 | mutex_unlock(&TASK->udebug.lock); |
531 | mutex_unlock(&TASK->udebug.lock); |
532 | 532 | ||
533 | return rc; |
533 | return rc; |
534 | } |
534 | } |
535 | 535 | ||
536 | /** @} |
536 | /** @} |
537 | */ |
537 | */ |
538 | 538 |