source: mainline/kernel/generic/src/udebug/udebug.c

Last change on this file was 111b9b9, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 2 years ago

Reimplement waitq using thread_wait/wakeup

This adds a few functions to the thread API which can be
summarized as "stop running until woken up by others".
The ordering and context-switching concerns are thus yeeted
to this abstraction and waitq only deals with maintaining
the queues. Overall, this makes the control flow in waitq
much easier to navigate.

  • Property mode set to 100644
File size: 13.1 KB
Line 
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 */
55void 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 */
71void 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 */
94static void udebug_wait_for_go(waitq_t *wq)
95{
96 waitq_sleep(wq);
97}
98
99/** Start of stoppable section.
100 *
101 * A stoppable section is a section of code where if the thread can
102 * be stoped. In other words, if a STOP operation is issued, the thread
103 * is guaranteed not to execute any userspace instructions until the
104 * thread is resumed.
105 *
106 * Having stoppable sections is better than having stopping points, since
107 * a thread can be stopped even when it is blocked indefinitely in a system
108 * call (whereas it would not reach any stopping point).
109 *
110 */
111void udebug_stoppable_begin(void)
112{
113 assert(THREAD);
114 assert(TASK);
115
116 mutex_lock(&TASK->udebug.lock);
117
118 int nsc = --TASK->udebug.not_stoppable_count;
119
120 /* Lock order OK, THREAD->udebug.lock is after TASK->udebug.lock */
121 mutex_lock(&THREAD->udebug.lock);
122 assert(THREAD->udebug.stoppable == false);
123 THREAD->udebug.stoppable = true;
124
125 if ((TASK->udebug.dt_state == UDEBUG_TS_BEGINNING) && (nsc == 0)) {
126 /*
127 * This was the last non-stoppable thread. Reply to
128 * DEBUG_BEGIN call.
129 *
130 */
131
132 call_t *db_call = TASK->udebug.begin_call;
133 assert(db_call);
134
135 TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
136 TASK->udebug.begin_call = NULL;
137
138 ipc_set_retval(&db_call->data, 0);
139 ipc_answer(&TASK->answerbox, db_call);
140 } else if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
141 /*
142 * Active debugging session
143 */
144
145 if (THREAD->udebug.active == true &&
146 THREAD->udebug.go == false) {
147 /*
148 * Thread was requested to stop - answer go call
149 *
150 */
151
152 /* Make sure nobody takes this call away from us */
153 call_t *go_call = THREAD->udebug.go_call;
154 THREAD->udebug.go_call = NULL;
155 assert(go_call);
156
157 ipc_set_retval(&go_call->data, 0);
158 ipc_set_arg1(&go_call->data, UDEBUG_EVENT_STOP);
159
160 THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
161 ipc_answer(&TASK->answerbox, go_call);
162 }
163 }
164
165 mutex_unlock(&THREAD->udebug.lock);
166 mutex_unlock(&TASK->udebug.lock);
167}
168
169/** End of a stoppable section.
170 *
171 * This is the point where the thread will block if it is stopped.
172 * (As, by definition, a stopped thread must not leave its stoppable
173 * section).
174 *
175 */
176void udebug_stoppable_end(void)
177{
178restart:
179 mutex_lock(&TASK->udebug.lock);
180 mutex_lock(&THREAD->udebug.lock);
181
182 if ((THREAD->udebug.active) && (THREAD->udebug.go == false)) {
183 mutex_unlock(&THREAD->udebug.lock);
184 mutex_unlock(&TASK->udebug.lock);
185
186 udebug_wait_for_go(&THREAD->udebug.go_wq);
187
188 goto restart;
189 /* Must try again - have to lose stoppability atomically. */
190 } else {
191 ++TASK->udebug.not_stoppable_count;
192 assert(THREAD->udebug.stoppable == true);
193 THREAD->udebug.stoppable = false;
194
195 mutex_unlock(&THREAD->udebug.lock);
196 mutex_unlock(&TASK->udebug.lock);
197 }
198}
199
200/** Upon being scheduled to run, check if the current thread should stop.
201 *
202 * This function is called from clock().
203 *
204 */
205void udebug_before_thread_runs(void)
206{
207 /* Check if we're supposed to stop */
208 udebug_stoppable_begin();
209 udebug_stoppable_end();
210}
211
212/** Syscall event hook.
213 *
214 * Must be called before and after servicing a system call. This generates
215 * a SYSCALL_B or SYSCALL_E event, depending on the value of @a end_variant.
216 *
217 */
218void udebug_syscall_event(sysarg_t a1, sysarg_t a2, sysarg_t a3,
219 sysarg_t a4, sysarg_t a5, sysarg_t a6, sysarg_t id, sysarg_t rc,
220 bool end_variant)
221{
222 udebug_event_t etype =
223 end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
224
225 mutex_lock(&TASK->udebug.lock);
226 mutex_lock(&THREAD->udebug.lock);
227
228 /* Must only generate events when in debugging session and is go. */
229 if (THREAD->udebug.active != true || THREAD->udebug.go == false ||
230 (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
231 mutex_unlock(&THREAD->udebug.lock);
232 mutex_unlock(&TASK->udebug.lock);
233 return;
234 }
235
236 /* Fill in the GO response. */
237 call_t *call = THREAD->udebug.go_call;
238 THREAD->udebug.go_call = NULL;
239
240 ipc_set_retval(&call->data, 0);
241 ipc_set_arg1(&call->data, etype);
242 ipc_set_arg2(&call->data, id);
243 ipc_set_arg3(&call->data, rc);
244
245 THREAD->udebug.syscall_args[0] = a1;
246 THREAD->udebug.syscall_args[1] = a2;
247 THREAD->udebug.syscall_args[2] = a3;
248 THREAD->udebug.syscall_args[3] = a4;
249 THREAD->udebug.syscall_args[4] = a5;
250 THREAD->udebug.syscall_args[5] = a6;
251
252 /*
253 * Make sure udebug.go is false when going to sleep
254 * in case we get woken up by DEBUG_END. (At which
255 * point it must be back to the initial true value).
256 *
257 */
258 THREAD->udebug.go = false;
259 THREAD->udebug.cur_event = etype;
260
261 ipc_answer(&TASK->answerbox, call);
262
263 mutex_unlock(&THREAD->udebug.lock);
264 mutex_unlock(&TASK->udebug.lock);
265
266 udebug_wait_for_go(&THREAD->udebug.go_wq);
267}
268
269/** Thread-creation event hook combined with attaching the thread.
270 *
271 * Must be called when a new userspace thread is created in the debugged
272 * task. Generates a THREAD_B event. Also attaches the thread @a t
273 * to the task @a ta.
274 *
275 * This is necessary to avoid a race condition where the BEGIN and THREAD_READ
276 * requests would be handled inbetween attaching the thread and checking it
277 * for being in a debugging session to send the THREAD_B event. We could then
278 * either miss threads or get some threads both in the thread list
279 * and get a THREAD_B event for them.
280 *
281 * @param thread Structure of the thread being created. Not locked, as the
282 * thread is not executing yet.
283 * @param task Task to which the thread should be attached.
284 *
285 */
286void udebug_thread_b_event_attach(struct thread *thread, struct task *task)
287{
288 mutex_lock(&TASK->udebug.lock);
289 mutex_lock(&THREAD->udebug.lock);
290
291 thread_attach(thread, task);
292
293 LOG("Check state");
294
295 /* Must only generate events when in debugging session */
296 if (THREAD->udebug.active != true) {
297 LOG("udebug.active: %s, udebug.go: %s",
298 THREAD->udebug.active ? "Yes(+)" : "No",
299 THREAD->udebug.go ? "Yes(-)" : "No");
300
301 mutex_unlock(&THREAD->udebug.lock);
302 mutex_unlock(&TASK->udebug.lock);
303 return;
304 }
305
306 LOG("Trigger event");
307
308 call_t *call = THREAD->udebug.go_call;
309
310 THREAD->udebug.go_call = NULL;
311 ipc_set_retval(&call->data, 0);
312 ipc_set_arg1(&call->data, UDEBUG_EVENT_THREAD_B);
313 ipc_set_arg2(&call->data, (sysarg_t) thread);
314
315 /*
316 * Make sure udebug.go is false when going to sleep
317 * in case we get woken up by DEBUG_END. (At which
318 * point it must be back to the initial true value).
319 *
320 */
321 THREAD->udebug.go = false;
322 THREAD->udebug.cur_event = UDEBUG_EVENT_THREAD_B;
323
324 ipc_answer(&TASK->answerbox, call);
325
326 mutex_unlock(&THREAD->udebug.lock);
327 mutex_unlock(&TASK->udebug.lock);
328
329 LOG("Wait for Go");
330 udebug_wait_for_go(&THREAD->udebug.go_wq);
331}
332
333/** Thread-termination event hook.
334 *
335 * Must be called when the current thread is terminating.
336 * Generates a THREAD_E event.
337 *
338 */
339void udebug_thread_e_event(void)
340{
341 mutex_lock(&TASK->udebug.lock);
342 mutex_lock(&THREAD->udebug.lock);
343
344 LOG("Check state");
345
346 /* Must only generate events when in debugging session. */
347 if (THREAD->udebug.active != true) {
348 LOG("udebug.active: %s, udebug.go: %s",
349 THREAD->udebug.active ? "Yes" : "No",
350 THREAD->udebug.go ? "Yes" : "No");
351
352 mutex_unlock(&THREAD->udebug.lock);
353 mutex_unlock(&TASK->udebug.lock);
354 return;
355 }
356
357 LOG("Trigger event");
358
359 call_t *call = THREAD->udebug.go_call;
360
361 THREAD->udebug.go_call = NULL;
362 ipc_set_retval(&call->data, 0);
363 ipc_set_arg1(&call->data, UDEBUG_EVENT_THREAD_E);
364
365 /* Prevent any further debug activity in thread. */
366 THREAD->udebug.active = false;
367 THREAD->udebug.cur_event = 0; /* None */
368 THREAD->udebug.go = false; /* Set to initial value */
369
370 ipc_answer(&TASK->answerbox, call);
371
372 mutex_unlock(&THREAD->udebug.lock);
373 mutex_unlock(&TASK->udebug.lock);
374
375 /*
376 * This event does not sleep - debugging has finished
377 * in this thread.
378 *
379 */
380}
381
382/** Terminate task debugging session.
383 *
384 * Gracefully terminate the debugging session for a task. If the debugger
385 * is still waiting for events on some threads, it will receive a
386 * FINISHED event for each of them.
387 *
388 * @param task Task structure. task->udebug.lock must be already locked.
389 *
390 * @return Zero on success or an error code.
391 *
392 */
393errno_t udebug_task_cleanup(struct task *task)
394{
395 assert(mutex_locked(&task->udebug.lock));
396
397 if ((task->udebug.dt_state != UDEBUG_TS_BEGINNING) &&
398 (task->udebug.dt_state != UDEBUG_TS_ACTIVE)) {
399 return EINVAL;
400 }
401
402 LOG("Task %" PRIu64, task->taskid);
403
404 /* Finish debugging of all userspace threads */
405 list_foreach(task->threads, th_link, thread_t, thread) {
406 mutex_lock(&thread->udebug.lock);
407
408 /* Only process userspace threads. */
409 if (thread->uspace) {
410 /* Prevent any further debug activity in thread. */
411 thread->udebug.active = false;
412 thread->udebug.cur_event = 0; /* None */
413
414 /* Is the thread still go? */
415 if (thread->udebug.go == true) {
416 /*
417 * Yes, so clear go. As active == false,
418 * this doesn't affect anything.
419 */
420 thread->udebug.go = false;
421
422 /* Answer GO call */
423 LOG("Answer GO call with EVENT_FINISHED.");
424
425 ipc_set_retval(&thread->udebug.go_call->data, 0);
426 ipc_set_arg1(&thread->udebug.go_call->data,
427 UDEBUG_EVENT_FINISHED);
428
429 ipc_answer(&task->answerbox, thread->udebug.go_call);
430 thread->udebug.go_call = NULL;
431 } else {
432 /*
433 * Debug_stop is already at initial value.
434 * Yet this means the thread needs waking up.
435 *
436 */
437
438 /*
439 * thread's lock must not be held when calling
440 * waitq_close.
441 *
442 */
443 waitq_close(&thread->udebug.go_wq);
444 }
445
446 mutex_unlock(&thread->udebug.lock);
447 condvar_broadcast(&thread->udebug.active_cv);
448 } else
449 mutex_unlock(&thread->udebug.lock);
450 }
451
452 task->udebug.dt_state = UDEBUG_TS_INACTIVE;
453 task->udebug.debugger = NULL;
454
455 return 0;
456}
457
458/** Wait for debugger to handle a fault in this thread.
459 *
460 * When a thread faults and someone is subscribed to the FAULT kernel event,
461 * this function is called to wait for a debugging session to give userspace
462 * a chance to examine the faulting thead/task. When the debugging session
463 * is over, this function returns (so that thread/task cleanup can continue).
464 *
465 */
466void udebug_thread_fault(void)
467{
468 udebug_stoppable_begin();
469
470 /* Wait until a debugger attends to us. */
471 mutex_lock(&THREAD->udebug.lock);
472 while (!THREAD->udebug.active)
473 condvar_wait(&THREAD->udebug.active_cv, &THREAD->udebug.lock);
474 mutex_unlock(&THREAD->udebug.lock);
475
476 /*
477 * This is where we will typically block until a post-mortem debugger
478 * terminates the debugging session.
479 */
480 udebug_stoppable_end();
481}
482
483/** @}
484 */
Note: See TracBrowser for help on using the repository browser.