1 | /*
|
---|
2 | * Copyright (c) 2008 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
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
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
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
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
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
|
---|
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
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup kernel_generic
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file
|
---|
35 | * @brief Udebug hooks and data structure management.
|
---|
36 | *
|
---|
37 | * Udebug is an interface that makes userspace debuggers possible.
|
---|
38 | */
|
---|
39 |
|
---|
40 | #include <assert.h>
|
---|
41 | #include <debug.h>
|
---|
42 | #include <synch/waitq.h>
|
---|
43 | #include <udebug/udebug.h>
|
---|
44 | #include <errno.h>
|
---|
45 | #include <arch.h>
|
---|
46 | #include <proc/task.h>
|
---|
47 | #include <proc/thread.h>
|
---|
48 |
|
---|
49 | /** Initialize udebug part of task structure.
|
---|
50 | *
|
---|
51 | * Called as part of task structure initialization.
|
---|
52 | * @param ut Pointer to the structure to initialize.
|
---|
53 | *
|
---|
54 | */
|
---|
55 | void udebug_task_init(udebug_task_t *ut)
|
---|
56 | {
|
---|
57 | mutex_initialize(&ut->lock, MUTEX_PASSIVE);
|
---|
58 | ut->dt_state = UDEBUG_TS_INACTIVE;
|
---|
59 | ut->begin_call = NULL;
|
---|
60 | ut->not_stoppable_count = 0;
|
---|
61 | ut->evmask = 0;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /** Initialize udebug part of thread structure.
|
---|
65 | *
|
---|
66 | * Called as part of thread structure initialization.
|
---|
67 | *
|
---|
68 | * @param ut Pointer to the structure to initialize.
|
---|
69 | *
|
---|
70 | */
|
---|
71 | void udebug_thread_initialize(udebug_thread_t *ut)
|
---|
72 | {
|
---|
73 | mutex_initialize(&ut->lock, MUTEX_PASSIVE);
|
---|
74 | waitq_initialize(&ut->go_wq);
|
---|
75 | condvar_initialize(&ut->active_cv);
|
---|
76 |
|
---|
77 | ut->go_call = NULL;
|
---|
78 | ut->uspace_state = NULL;
|
---|
79 | ut->go = false;
|
---|
80 | ut->stoppable = true;
|
---|
81 | ut->active = false;
|
---|
82 | ut->cur_event = 0; /* None */
|
---|
83 | }
|
---|
84 |
|
---|
85 | /** Wait for a GO message.
|
---|
86 | *
|
---|
87 | * When a debugging event occurs in a thread or the thread is stopped,
|
---|
88 | * this function is called to block the thread until a GO message
|
---|
89 | * is received.
|
---|
90 | *
|
---|
91 | * @param wq The wait queue used by the thread to wait for GO messages.
|
---|
92 | *
|
---|
93 | */
|
---|
94 | static void udebug_wait_for_go(waitq_t *wq)
|
---|
95 | {
|
---|
96 | ipl_t ipl = waitq_sleep_prepare(wq);
|
---|
97 |
|
---|
98 | wq->missed_wakeups = 0; /* Enforce blocking. */
|
---|
99 | bool blocked;
|
---|
100 | (void) waitq_sleep_timeout_unsafe(wq, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE, &blocked);
|
---|
101 | waitq_sleep_finish(wq, blocked, ipl);
|
---|
102 | }
|
---|
103 |
|
---|
104 | /** Start of stoppable section.
|
---|
105 | *
|
---|
106 | * A stoppable section is a section of code where if the thread can
|
---|
107 | * be stoped. In other words, if a STOP operation is issued, the thread
|
---|
108 | * is guaranteed not to execute any userspace instructions until the
|
---|
109 | * thread is resumed.
|
---|
110 | *
|
---|
111 | * Having stoppable sections is better than having stopping points, since
|
---|
112 | * a thread can be stopped even when it is blocked indefinitely in a system
|
---|
113 | * call (whereas it would not reach any stopping point).
|
---|
114 | *
|
---|
115 | */
|
---|
116 | void udebug_stoppable_begin(void)
|
---|
117 | {
|
---|
118 | assert(THREAD);
|
---|
119 | assert(TASK);
|
---|
120 |
|
---|
121 | mutex_lock(&TASK->udebug.lock);
|
---|
122 |
|
---|
123 | int nsc = --TASK->udebug.not_stoppable_count;
|
---|
124 |
|
---|
125 | /* Lock order OK, THREAD->udebug.lock is after TASK->udebug.lock */
|
---|
126 | mutex_lock(&THREAD->udebug.lock);
|
---|
127 | assert(THREAD->udebug.stoppable == false);
|
---|
128 | THREAD->udebug.stoppable = true;
|
---|
129 |
|
---|
130 | if ((TASK->udebug.dt_state == UDEBUG_TS_BEGINNING) && (nsc == 0)) {
|
---|
131 | /*
|
---|
132 | * This was the last non-stoppable thread. Reply to
|
---|
133 | * DEBUG_BEGIN call.
|
---|
134 | *
|
---|
135 | */
|
---|
136 |
|
---|
137 | call_t *db_call = TASK->udebug.begin_call;
|
---|
138 | assert(db_call);
|
---|
139 |
|
---|
140 | TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
|
---|
141 | TASK->udebug.begin_call = NULL;
|
---|
142 |
|
---|
143 | ipc_set_retval(&db_call->data, 0);
|
---|
144 | ipc_answer(&TASK->answerbox, db_call);
|
---|
145 | } else if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
|
---|
146 | /*
|
---|
147 | * Active debugging session
|
---|
148 | */
|
---|
149 |
|
---|
150 | if (THREAD->udebug.active == true &&
|
---|
151 | THREAD->udebug.go == false) {
|
---|
152 | /*
|
---|
153 | * Thread was requested to stop - answer go call
|
---|
154 | *
|
---|
155 | */
|
---|
156 |
|
---|
157 | /* Make sure nobody takes this call away from us */
|
---|
158 | call_t *go_call = THREAD->udebug.go_call;
|
---|
159 | THREAD->udebug.go_call = NULL;
|
---|
160 | assert(go_call);
|
---|
161 |
|
---|
162 | ipc_set_retval(&go_call->data, 0);
|
---|
163 | ipc_set_arg1(&go_call->data, UDEBUG_EVENT_STOP);
|
---|
164 |
|
---|
165 | THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
|
---|
166 | ipc_answer(&TASK->answerbox, go_call);
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | mutex_unlock(&THREAD->udebug.lock);
|
---|
171 | mutex_unlock(&TASK->udebug.lock);
|
---|
172 | }
|
---|
173 |
|
---|
174 | /** End of a stoppable section.
|
---|
175 | *
|
---|
176 | * This is the point where the thread will block if it is stopped.
|
---|
177 | * (As, by definition, a stopped thread must not leave its stoppable
|
---|
178 | * section).
|
---|
179 | *
|
---|
180 | */
|
---|
181 | void udebug_stoppable_end(void)
|
---|
182 | {
|
---|
183 | restart:
|
---|
184 | mutex_lock(&TASK->udebug.lock);
|
---|
185 | mutex_lock(&THREAD->udebug.lock);
|
---|
186 |
|
---|
187 | if ((THREAD->udebug.active) && (THREAD->udebug.go == false)) {
|
---|
188 | mutex_unlock(&THREAD->udebug.lock);
|
---|
189 | mutex_unlock(&TASK->udebug.lock);
|
---|
190 |
|
---|
191 | udebug_wait_for_go(&THREAD->udebug.go_wq);
|
---|
192 |
|
---|
193 | goto restart;
|
---|
194 | /* Must try again - have to lose stoppability atomically. */
|
---|
195 | } else {
|
---|
196 | ++TASK->udebug.not_stoppable_count;
|
---|
197 | assert(THREAD->udebug.stoppable == true);
|
---|
198 | THREAD->udebug.stoppable = false;
|
---|
199 |
|
---|
200 | mutex_unlock(&THREAD->udebug.lock);
|
---|
201 | mutex_unlock(&TASK->udebug.lock);
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | /** Upon being scheduled to run, check if the current thread should stop.
|
---|
206 | *
|
---|
207 | * This function is called from clock().
|
---|
208 | *
|
---|
209 | */
|
---|
210 | void udebug_before_thread_runs(void)
|
---|
211 | {
|
---|
212 | /* Check if we're supposed to stop */
|
---|
213 | udebug_stoppable_begin();
|
---|
214 | udebug_stoppable_end();
|
---|
215 | }
|
---|
216 |
|
---|
217 | /** Syscall event hook.
|
---|
218 | *
|
---|
219 | * Must be called before and after servicing a system call. This generates
|
---|
220 | * a SYSCALL_B or SYSCALL_E event, depending on the value of @a end_variant.
|
---|
221 | *
|
---|
222 | */
|
---|
223 | void udebug_syscall_event(sysarg_t a1, sysarg_t a2, sysarg_t a3,
|
---|
224 | sysarg_t a4, sysarg_t a5, sysarg_t a6, sysarg_t id, sysarg_t rc,
|
---|
225 | bool end_variant)
|
---|
226 | {
|
---|
227 | udebug_event_t etype =
|
---|
228 | end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
|
---|
229 |
|
---|
230 | mutex_lock(&TASK->udebug.lock);
|
---|
231 | mutex_lock(&THREAD->udebug.lock);
|
---|
232 |
|
---|
233 | /* Must only generate events when in debugging session and is go. */
|
---|
234 | if (THREAD->udebug.active != true || THREAD->udebug.go == false ||
|
---|
235 | (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
|
---|
236 | mutex_unlock(&THREAD->udebug.lock);
|
---|
237 | mutex_unlock(&TASK->udebug.lock);
|
---|
238 | return;
|
---|
239 | }
|
---|
240 |
|
---|
241 | /* Fill in the GO response. */
|
---|
242 | call_t *call = THREAD->udebug.go_call;
|
---|
243 | THREAD->udebug.go_call = NULL;
|
---|
244 |
|
---|
245 | ipc_set_retval(&call->data, 0);
|
---|
246 | ipc_set_arg1(&call->data, etype);
|
---|
247 | ipc_set_arg2(&call->data, id);
|
---|
248 | ipc_set_arg3(&call->data, rc);
|
---|
249 |
|
---|
250 | THREAD->udebug.syscall_args[0] = a1;
|
---|
251 | THREAD->udebug.syscall_args[1] = a2;
|
---|
252 | THREAD->udebug.syscall_args[2] = a3;
|
---|
253 | THREAD->udebug.syscall_args[3] = a4;
|
---|
254 | THREAD->udebug.syscall_args[4] = a5;
|
---|
255 | THREAD->udebug.syscall_args[5] = a6;
|
---|
256 |
|
---|
257 | /*
|
---|
258 | * Make sure udebug.go is false when going to sleep
|
---|
259 | * in case we get woken up by DEBUG_END. (At which
|
---|
260 | * point it must be back to the initial true value).
|
---|
261 | *
|
---|
262 | */
|
---|
263 | THREAD->udebug.go = false;
|
---|
264 | THREAD->udebug.cur_event = etype;
|
---|
265 |
|
---|
266 | ipc_answer(&TASK->answerbox, call);
|
---|
267 |
|
---|
268 | mutex_unlock(&THREAD->udebug.lock);
|
---|
269 | mutex_unlock(&TASK->udebug.lock);
|
---|
270 |
|
---|
271 | udebug_wait_for_go(&THREAD->udebug.go_wq);
|
---|
272 | }
|
---|
273 |
|
---|
274 | /** Thread-creation event hook combined with attaching the thread.
|
---|
275 | *
|
---|
276 | * Must be called when a new userspace thread is created in the debugged
|
---|
277 | * task. Generates a THREAD_B event. Also attaches the thread @a t
|
---|
278 | * to the task @a ta.
|
---|
279 | *
|
---|
280 | * This is necessary to avoid a race condition where the BEGIN and THREAD_READ
|
---|
281 | * requests would be handled inbetween attaching the thread and checking it
|
---|
282 | * for being in a debugging session to send the THREAD_B event. We could then
|
---|
283 | * either miss threads or get some threads both in the thread list
|
---|
284 | * and get a THREAD_B event for them.
|
---|
285 | *
|
---|
286 | * @param thread Structure of the thread being created. Not locked, as the
|
---|
287 | * thread is not executing yet.
|
---|
288 | * @param task Task to which the thread should be attached.
|
---|
289 | *
|
---|
290 | */
|
---|
291 | void udebug_thread_b_event_attach(struct thread *thread, struct task *task)
|
---|
292 | {
|
---|
293 | mutex_lock(&TASK->udebug.lock);
|
---|
294 | mutex_lock(&THREAD->udebug.lock);
|
---|
295 |
|
---|
296 | thread_attach(thread, task);
|
---|
297 |
|
---|
298 | LOG("Check state");
|
---|
299 |
|
---|
300 | /* Must only generate events when in debugging session */
|
---|
301 | if (THREAD->udebug.active != true) {
|
---|
302 | LOG("udebug.active: %s, udebug.go: %s",
|
---|
303 | THREAD->udebug.active ? "Yes(+)" : "No",
|
---|
304 | THREAD->udebug.go ? "Yes(-)" : "No");
|
---|
305 |
|
---|
306 | mutex_unlock(&THREAD->udebug.lock);
|
---|
307 | mutex_unlock(&TASK->udebug.lock);
|
---|
308 | return;
|
---|
309 | }
|
---|
310 |
|
---|
311 | LOG("Trigger event");
|
---|
312 |
|
---|
313 | call_t *call = THREAD->udebug.go_call;
|
---|
314 |
|
---|
315 | THREAD->udebug.go_call = NULL;
|
---|
316 | ipc_set_retval(&call->data, 0);
|
---|
317 | ipc_set_arg1(&call->data, UDEBUG_EVENT_THREAD_B);
|
---|
318 | ipc_set_arg2(&call->data, (sysarg_t) thread);
|
---|
319 |
|
---|
320 | /*
|
---|
321 | * Make sure udebug.go is false when going to sleep
|
---|
322 | * in case we get woken up by DEBUG_END. (At which
|
---|
323 | * point it must be back to the initial true value).
|
---|
324 | *
|
---|
325 | */
|
---|
326 | THREAD->udebug.go = false;
|
---|
327 | THREAD->udebug.cur_event = UDEBUG_EVENT_THREAD_B;
|
---|
328 |
|
---|
329 | ipc_answer(&TASK->answerbox, call);
|
---|
330 |
|
---|
331 | mutex_unlock(&THREAD->udebug.lock);
|
---|
332 | mutex_unlock(&TASK->udebug.lock);
|
---|
333 |
|
---|
334 | LOG("Wait for Go");
|
---|
335 | udebug_wait_for_go(&THREAD->udebug.go_wq);
|
---|
336 | }
|
---|
337 |
|
---|
338 | /** Thread-termination event hook.
|
---|
339 | *
|
---|
340 | * Must be called when the current thread is terminating.
|
---|
341 | * Generates a THREAD_E event.
|
---|
342 | *
|
---|
343 | */
|
---|
344 | void udebug_thread_e_event(void)
|
---|
345 | {
|
---|
346 | mutex_lock(&TASK->udebug.lock);
|
---|
347 | mutex_lock(&THREAD->udebug.lock);
|
---|
348 |
|
---|
349 | LOG("Check state");
|
---|
350 |
|
---|
351 | /* Must only generate events when in debugging session. */
|
---|
352 | if (THREAD->udebug.active != true) {
|
---|
353 | LOG("udebug.active: %s, udebug.go: %s",
|
---|
354 | THREAD->udebug.active ? "Yes" : "No",
|
---|
355 | THREAD->udebug.go ? "Yes" : "No");
|
---|
356 |
|
---|
357 | mutex_unlock(&THREAD->udebug.lock);
|
---|
358 | mutex_unlock(&TASK->udebug.lock);
|
---|
359 | return;
|
---|
360 | }
|
---|
361 |
|
---|
362 | LOG("Trigger event");
|
---|
363 |
|
---|
364 | call_t *call = THREAD->udebug.go_call;
|
---|
365 |
|
---|
366 | THREAD->udebug.go_call = NULL;
|
---|
367 | ipc_set_retval(&call->data, 0);
|
---|
368 | ipc_set_arg1(&call->data, UDEBUG_EVENT_THREAD_E);
|
---|
369 |
|
---|
370 | /* Prevent any further debug activity in thread. */
|
---|
371 | THREAD->udebug.active = false;
|
---|
372 | THREAD->udebug.cur_event = 0; /* None */
|
---|
373 | THREAD->udebug.go = false; /* Set to initial value */
|
---|
374 |
|
---|
375 | ipc_answer(&TASK->answerbox, call);
|
---|
376 |
|
---|
377 | mutex_unlock(&THREAD->udebug.lock);
|
---|
378 | mutex_unlock(&TASK->udebug.lock);
|
---|
379 |
|
---|
380 | /*
|
---|
381 | * This event does not sleep - debugging has finished
|
---|
382 | * in this thread.
|
---|
383 | *
|
---|
384 | */
|
---|
385 | }
|
---|
386 |
|
---|
387 | /** Terminate task debugging session.
|
---|
388 | *
|
---|
389 | * Gracefully terminate the debugging session for a task. If the debugger
|
---|
390 | * is still waiting for events on some threads, it will receive a
|
---|
391 | * FINISHED event for each of them.
|
---|
392 | *
|
---|
393 | * @param task Task structure. task->udebug.lock must be already locked.
|
---|
394 | *
|
---|
395 | * @return Zero on success or an error code.
|
---|
396 | *
|
---|
397 | */
|
---|
398 | errno_t udebug_task_cleanup(struct task *task)
|
---|
399 | {
|
---|
400 | assert(mutex_locked(&task->udebug.lock));
|
---|
401 |
|
---|
402 | if ((task->udebug.dt_state != UDEBUG_TS_BEGINNING) &&
|
---|
403 | (task->udebug.dt_state != UDEBUG_TS_ACTIVE)) {
|
---|
404 | return EINVAL;
|
---|
405 | }
|
---|
406 |
|
---|
407 | LOG("Task %" PRIu64, task->taskid);
|
---|
408 |
|
---|
409 | /* Finish debugging of all userspace threads */
|
---|
410 | list_foreach(task->threads, th_link, thread_t, thread) {
|
---|
411 | mutex_lock(&thread->udebug.lock);
|
---|
412 |
|
---|
413 | /* Only process userspace threads. */
|
---|
414 | if (thread->uspace) {
|
---|
415 | /* Prevent any further debug activity in thread. */
|
---|
416 | thread->udebug.active = false;
|
---|
417 | thread->udebug.cur_event = 0; /* None */
|
---|
418 |
|
---|
419 | /* Is the thread still go? */
|
---|
420 | if (thread->udebug.go == true) {
|
---|
421 | /*
|
---|
422 | * Yes, so clear go. As active == false,
|
---|
423 | * this doesn't affect anything.
|
---|
424 | */
|
---|
425 | thread->udebug.go = false;
|
---|
426 |
|
---|
427 | /* Answer GO call */
|
---|
428 | LOG("Answer GO call with EVENT_FINISHED.");
|
---|
429 |
|
---|
430 | ipc_set_retval(&thread->udebug.go_call->data, 0);
|
---|
431 | ipc_set_arg1(&thread->udebug.go_call->data,
|
---|
432 | UDEBUG_EVENT_FINISHED);
|
---|
433 |
|
---|
434 | ipc_answer(&task->answerbox, thread->udebug.go_call);
|
---|
435 | thread->udebug.go_call = NULL;
|
---|
436 | } else {
|
---|
437 | /*
|
---|
438 | * Debug_stop is already at initial value.
|
---|
439 | * Yet this means the thread needs waking up.
|
---|
440 | *
|
---|
441 | */
|
---|
442 |
|
---|
443 | /*
|
---|
444 | * thread's lock must not be held when calling
|
---|
445 | * waitq_wakeup.
|
---|
446 | *
|
---|
447 | */
|
---|
448 | waitq_wakeup(&thread->udebug.go_wq, WAKEUP_FIRST);
|
---|
449 | }
|
---|
450 |
|
---|
451 | mutex_unlock(&thread->udebug.lock);
|
---|
452 | condvar_broadcast(&thread->udebug.active_cv);
|
---|
453 | } else
|
---|
454 | mutex_unlock(&thread->udebug.lock);
|
---|
455 | }
|
---|
456 |
|
---|
457 | task->udebug.dt_state = UDEBUG_TS_INACTIVE;
|
---|
458 | task->udebug.debugger = NULL;
|
---|
459 |
|
---|
460 | return 0;
|
---|
461 | }
|
---|
462 |
|
---|
463 | /** Wait for debugger to handle a fault in this thread.
|
---|
464 | *
|
---|
465 | * When a thread faults and someone is subscribed to the FAULT kernel event,
|
---|
466 | * this function is called to wait for a debugging session to give userspace
|
---|
467 | * a chance to examine the faulting thead/task. When the debugging session
|
---|
468 | * is over, this function returns (so that thread/task cleanup can continue).
|
---|
469 | *
|
---|
470 | */
|
---|
471 | void udebug_thread_fault(void)
|
---|
472 | {
|
---|
473 | udebug_stoppable_begin();
|
---|
474 |
|
---|
475 | /* Wait until a debugger attends to us. */
|
---|
476 | mutex_lock(&THREAD->udebug.lock);
|
---|
477 | while (!THREAD->udebug.active)
|
---|
478 | condvar_wait(&THREAD->udebug.active_cv, &THREAD->udebug.lock);
|
---|
479 | mutex_unlock(&THREAD->udebug.lock);
|
---|
480 |
|
---|
481 | /*
|
---|
482 | * This is where we will typically block until a post-mortem debugger
|
---|
483 | * terminates the debugging session.
|
---|
484 | */
|
---|
485 | udebug_stoppable_end();
|
---|
486 | }
|
---|
487 |
|
---|
488 | /** @}
|
---|
489 | */
|
---|