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
RevLine 
[9a1b20c]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
[174156fd]29/** @addtogroup kernel_generic
[9a1b20c]30 * @{
31 */
32
33/**
34 * @file
[da1bafb]35 * @brief Udebug hooks and data structure management.
[7dc62af]36 *
37 * Udebug is an interface that makes userspace debuggers possible.
[9a1b20c]38 */
[da1bafb]39
[63e27ef]40#include <assert.h>
[0108984a]41#include <debug.h>
[63e27ef]42#include <synch/waitq.h>
[9a1b20c]43#include <udebug/udebug.h>
44#include <errno.h>
45#include <arch.h>
[1066041]46#include <proc/task.h>
47#include <proc/thread.h>
[9a1b20c]48
[7dc62af]49/** Initialize udebug part of task structure.
50 *
51 * Called as part of task structure initialization.
[da1bafb]52 * @param ut Pointer to the structure to initialize.
53 *
[7dc62af]54 */
[9a1b20c]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
[7dc62af]64/** Initialize udebug part of thread structure.
65 *
66 * Called as part of thread structure initialization.
[da1bafb]67 *
68 * @param ut Pointer to the structure to initialize.
69 *
[7dc62af]70 */
[9a1b20c]71void udebug_thread_initialize(udebug_thread_t *ut)
72{
73 mutex_initialize(&ut->lock, MUTEX_PASSIVE);
74 waitq_initialize(&ut->go_wq);
[a074b4f]75 condvar_initialize(&ut->active_cv);
[a35b458]76
[9a1b20c]77 ut->go_call = NULL;
[3ff2b54]78 ut->uspace_state = NULL;
[384c488]79 ut->go = false;
[9a1b20c]80 ut->stoppable = true;
[8af9950]81 ut->active = false;
[da1bafb]82 ut->cur_event = 0; /* None */
[9a1b20c]83}
84
[7dc62af]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 *
[da1bafb]91 * @param wq The wait queue used by the thread to wait for GO messages.
92 *
[7dc62af]93 */
[9a1b20c]94static void udebug_wait_for_go(waitq_t *wq)
95{
[07700ed]96 waitq_sleep(wq);
[9a1b20c]97}
98
[7dc62af]99/** Start of stoppable section.
100 *
[da1bafb]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.
[7dc62af]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).
[da1bafb]109 *
[7dc62af]110 */
[9a1b20c]111void udebug_stoppable_begin(void)
112{
[63e27ef]113 assert(THREAD);
114 assert(TASK);
[a35b458]115
[9a1b20c]116 mutex_lock(&TASK->udebug.lock);
[a35b458]117
[da1bafb]118 int nsc = --TASK->udebug.not_stoppable_count;
[a35b458]119
[9a1b20c]120 /* Lock order OK, THREAD->udebug.lock is after TASK->udebug.lock */
121 mutex_lock(&THREAD->udebug.lock);
[63e27ef]122 assert(THREAD->udebug.stoppable == false);
[9a1b20c]123 THREAD->udebug.stoppable = true;
[a35b458]124
[da1bafb]125 if ((TASK->udebug.dt_state == UDEBUG_TS_BEGINNING) && (nsc == 0)) {
[9a1b20c]126 /*
127 * This was the last non-stoppable thread. Reply to
128 * DEBUG_BEGIN call.
[da1bafb]129 *
[9a1b20c]130 */
[a35b458]131
[da1bafb]132 call_t *db_call = TASK->udebug.begin_call;
[63e27ef]133 assert(db_call);
[a35b458]134
[9a1b20c]135 TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
136 TASK->udebug.begin_call = NULL;
[a35b458]137
[fafb8e5]138 ipc_set_retval(&db_call->data, 0);
[da1bafb]139 ipc_answer(&TASK->answerbox, db_call);
[9a1b20c]140 } else if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
141 /*
142 * Active debugging session
143 */
[a35b458]144
[8af9950]145 if (THREAD->udebug.active == true &&
[384c488]146 THREAD->udebug.go == false) {
[9a1b20c]147 /*
148 * Thread was requested to stop - answer go call
[da1bafb]149 *
[9a1b20c]150 */
[a35b458]151
[9a1b20c]152 /* Make sure nobody takes this call away from us */
[da1bafb]153 call_t *go_call = THREAD->udebug.go_call;
[9a1b20c]154 THREAD->udebug.go_call = NULL;
[63e27ef]155 assert(go_call);
[a35b458]156
[fafb8e5]157 ipc_set_retval(&go_call->data, 0);
158 ipc_set_arg1(&go_call->data, UDEBUG_EVENT_STOP);
[a35b458]159
[9a1b20c]160 THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
[da1bafb]161 ipc_answer(&TASK->answerbox, go_call);
[9a1b20c]162 }
163 }
[a35b458]164
[9a1b20c]165 mutex_unlock(&THREAD->udebug.lock);
[3bacee1]166 mutex_unlock(&TASK->udebug.lock);
[9a1b20c]167}
168
[7dc62af]169/** End of a stoppable section.
170 *
171 * This is the point where the thread will block if it is stopped.
[da1bafb]172 * (As, by definition, a stopped thread must not leave its stoppable
173 * section).
174 *
[7dc62af]175 */
[9a1b20c]176void udebug_stoppable_end(void)
177{
178restart:
179 mutex_lock(&TASK->udebug.lock);
180 mutex_lock(&THREAD->udebug.lock);
[a35b458]181
[da1bafb]182 if ((THREAD->udebug.active) && (THREAD->udebug.go == false)) {
[9a1b20c]183 mutex_unlock(&THREAD->udebug.lock);
184 mutex_unlock(&TASK->udebug.lock);
[a35b458]185
[9a1b20c]186 udebug_wait_for_go(&THREAD->udebug.go_wq);
[a35b458]187
[9a1b20c]188 goto restart;
[1378b2b]189 /* Must try again - have to lose stoppability atomically. */
[9a1b20c]190 } else {
191 ++TASK->udebug.not_stoppable_count;
[63e27ef]192 assert(THREAD->udebug.stoppable == true);
[9a1b20c]193 THREAD->udebug.stoppable = false;
[a35b458]194
[9a1b20c]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 *
[3ff2b54]202 * This function is called from clock().
[da1bafb]203 *
[9a1b20c]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
[7dc62af]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.
[da1bafb]216 *
[7dc62af]217 */
[96b02eb9]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,
[9a1b20c]220 bool end_variant)
221{
[da1bafb]222 udebug_event_t etype =
223 end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
[a35b458]224
[9a1b20c]225 mutex_lock(&TASK->udebug.lock);
226 mutex_lock(&THREAD->udebug.lock);
[a35b458]227
[1378b2b]228 /* Must only generate events when in debugging session and is go. */
[8af9950]229 if (THREAD->udebug.active != true || THREAD->udebug.go == false ||
[9a1b20c]230 (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
231 mutex_unlock(&THREAD->udebug.lock);
232 mutex_unlock(&TASK->udebug.lock);
233 return;
234 }
[a35b458]235
[ae5aa90]236 /* Fill in the GO response. */
[da1bafb]237 call_t *call = THREAD->udebug.go_call;
[9a1b20c]238 THREAD->udebug.go_call = NULL;
[a35b458]239
[fafb8e5]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);
[a35b458]244
[9a1b20c]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;
[a35b458]251
[9a1b20c]252 /*
[384c488]253 * Make sure udebug.go is false when going to sleep
[9a1b20c]254 * in case we get woken up by DEBUG_END. (At which
255 * point it must be back to the initial true value).
[da1bafb]256 *
[9a1b20c]257 */
[384c488]258 THREAD->udebug.go = false;
[9a1b20c]259 THREAD->udebug.cur_event = etype;
[a35b458]260
[9a1b20c]261 ipc_answer(&TASK->answerbox, call);
[a35b458]262
[9a1b20c]263 mutex_unlock(&THREAD->udebug.lock);
264 mutex_unlock(&TASK->udebug.lock);
[a35b458]265
[9a1b20c]266 udebug_wait_for_go(&THREAD->udebug.go_wq);
267}
268
[13964ef]269/** Thread-creation event hook combined with attaching the thread.
[7dc62af]270 *
271 * Must be called when a new userspace thread is created in the debugged
[13964ef]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.
[7dc62af]280 *
[da1bafb]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 *
[7dc62af]285 */
[da1bafb]286void udebug_thread_b_event_attach(struct thread *thread, struct task *task)
[9a1b20c]287{
288 mutex_lock(&TASK->udebug.lock);
289 mutex_lock(&THREAD->udebug.lock);
[a35b458]290
[da1bafb]291 thread_attach(thread, task);
[a35b458]292
[ae5aa90]293 LOG("Check state");
[a35b458]294
[9a1b20c]295 /* Must only generate events when in debugging session */
[8af9950]296 if (THREAD->udebug.active != true) {
[ae5aa90]297 LOG("udebug.active: %s, udebug.go: %s",
[da1bafb]298 THREAD->udebug.active ? "Yes(+)" : "No",
299 THREAD->udebug.go ? "Yes(-)" : "No");
[a35b458]300
[9a1b20c]301 mutex_unlock(&THREAD->udebug.lock);
302 mutex_unlock(&TASK->udebug.lock);
303 return;
304 }
[a35b458]305
[ae5aa90]306 LOG("Trigger event");
[a35b458]307
[da1bafb]308 call_t *call = THREAD->udebug.go_call;
[a35b458]309
[9a1b20c]310 THREAD->udebug.go_call = NULL;
[fafb8e5]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);
[a35b458]314
[9a1b20c]315 /*
[384c488]316 * Make sure udebug.go is false when going to sleep
[9a1b20c]317 * in case we get woken up by DEBUG_END. (At which
318 * point it must be back to the initial true value).
[da1bafb]319 *
[9a1b20c]320 */
[384c488]321 THREAD->udebug.go = false;
[9a1b20c]322 THREAD->udebug.cur_event = UDEBUG_EVENT_THREAD_B;
[a35b458]323
[9a1b20c]324 ipc_answer(&TASK->answerbox, call);
[a35b458]325
[9a1b20c]326 mutex_unlock(&THREAD->udebug.lock);
327 mutex_unlock(&TASK->udebug.lock);
[a35b458]328
[ae5aa90]329 LOG("Wait for Go");
[9a1b20c]330 udebug_wait_for_go(&THREAD->udebug.go_wq);
331}
332
[7dc62af]333/** Thread-termination event hook.
334 *
335 * Must be called when the current thread is terminating.
336 * Generates a THREAD_E event.
[da1bafb]337 *
[7dc62af]338 */
[9a1b20c]339void udebug_thread_e_event(void)
340{
341 mutex_lock(&TASK->udebug.lock);
342 mutex_lock(&THREAD->udebug.lock);
[a35b458]343
[ae5aa90]344 LOG("Check state");
[a35b458]345
[1378b2b]346 /* Must only generate events when in debugging session. */
[8af9950]347 if (THREAD->udebug.active != true) {
[ae5aa90]348 LOG("udebug.active: %s, udebug.go: %s",
[da1bafb]349 THREAD->udebug.active ? "Yes" : "No",
350 THREAD->udebug.go ? "Yes" : "No");
[a35b458]351
[9a1b20c]352 mutex_unlock(&THREAD->udebug.lock);
353 mutex_unlock(&TASK->udebug.lock);
354 return;
355 }
[a35b458]356
[ae5aa90]357 LOG("Trigger event");
[a35b458]358
[da1bafb]359 call_t *call = THREAD->udebug.go_call;
[a35b458]360
[9a1b20c]361 THREAD->udebug.go_call = NULL;
[fafb8e5]362 ipc_set_retval(&call->data, 0);
363 ipc_set_arg1(&call->data, UDEBUG_EVENT_THREAD_E);
[a35b458]364
[1378b2b]365 /* Prevent any further debug activity in thread. */
[8af9950]366 THREAD->udebug.active = false;
[da1bafb]367 THREAD->udebug.cur_event = 0; /* None */
368 THREAD->udebug.go = false; /* Set to initial value */
[a35b458]369
[9a1b20c]370 ipc_answer(&TASK->answerbox, call);
[a35b458]371
[9a1b20c]372 mutex_unlock(&THREAD->udebug.lock);
373 mutex_unlock(&TASK->udebug.lock);
[a35b458]374
[da1bafb]375 /*
[32e6c9c]376 * This event does not sleep - debugging has finished
377 * in this thread.
[da1bafb]378 *
[32e6c9c]379 */
[9a1b20c]380}
381
[da1bafb]382/** Terminate task debugging session.
[9a1b20c]383 *
[da1bafb]384 * Gracefully terminate the debugging session for a task. If the debugger
[7dc62af]385 * is still waiting for events on some threads, it will receive a
386 * FINISHED event for each of them.
387 *
[1d432f9]388 * @param task Task structure. task->udebug.lock must be already locked.
[da1bafb]389 *
[cde999a]390 * @return Zero on success or an error code.
[da1bafb]391 *
[9a1b20c]392 */
[b7fd2a0]393errno_t udebug_task_cleanup(struct task *task)
[9a1b20c]394{
[63e27ef]395 assert(mutex_locked(&task->udebug.lock));
[1d432f9]396
[da1bafb]397 if ((task->udebug.dt_state != UDEBUG_TS_BEGINNING) &&
398 (task->udebug.dt_state != UDEBUG_TS_ACTIVE)) {
[9a1b20c]399 return EINVAL;
400 }
[a35b458]401
[da1bafb]402 LOG("Task %" PRIu64, task->taskid);
[a35b458]403
[9a1b20c]404 /* Finish debugging of all userspace threads */
[feeac0d]405 list_foreach(task->threads, th_link, thread_t, thread) {
[da1bafb]406 mutex_lock(&thread->udebug.lock);
[a35b458]407
[1378b2b]408 /* Only process userspace threads. */
[6eef3c4]409 if (thread->uspace) {
[1378b2b]410 /* Prevent any further debug activity in thread. */
[da1bafb]411 thread->udebug.active = false;
412 thread->udebug.cur_event = 0; /* None */
[a35b458]413
[1378b2b]414 /* Is the thread still go? */
[da1bafb]415 if (thread->udebug.go == true) {
[9a1b20c]416 /*
[da1bafb]417 * Yes, so clear go. As active == false,
[9a1b20c]418 * this doesn't affect anything.
419 */
[da1bafb]420 thread->udebug.go = false;
[a35b458]421
[9a1b20c]422 /* Answer GO call */
[ae5aa90]423 LOG("Answer GO call with EVENT_FINISHED.");
[a35b458]424
[fafb8e5]425 ipc_set_retval(&thread->udebug.go_call->data, 0);
426 ipc_set_arg1(&thread->udebug.go_call->data,
[3926f30]427 UDEBUG_EVENT_FINISHED);
[a35b458]428
[da1bafb]429 ipc_answer(&task->answerbox, thread->udebug.go_call);
430 thread->udebug.go_call = NULL;
[9a1b20c]431 } else {
432 /*
433 * Debug_stop is already at initial value.
434 * Yet this means the thread needs waking up.
[da1bafb]435 *
[9a1b20c]436 */
[a35b458]437
[9a1b20c]438 /*
[da1bafb]439 * thread's lock must not be held when calling
[111b9b9]440 * waitq_close.
[da1bafb]441 *
[9a1b20c]442 */
[111b9b9]443 waitq_close(&thread->udebug.go_wq);
[9a1b20c]444 }
[a35b458]445
[da1bafb]446 mutex_unlock(&thread->udebug.lock);
447 condvar_broadcast(&thread->udebug.active_cv);
448 } else
449 mutex_unlock(&thread->udebug.lock);
[9a1b20c]450 }
[a35b458]451
[da1bafb]452 task->udebug.dt_state = UDEBUG_TS_INACTIVE;
453 task->udebug.debugger = NULL;
[a35b458]454
[9a1b20c]455 return 0;
456}
457
[0d21b53]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).
[da1bafb]464 *
[0d21b53]465 */
466void udebug_thread_fault(void)
467{
468 udebug_stoppable_begin();
[a35b458]469
[0d21b53]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);
[a35b458]475
[5667dca]476 /*
477 * This is where we will typically block until a post-mortem debugger
478 * terminates the debugging session.
479 */
[0d21b53]480 udebug_stoppable_end();
481}
[9a1b20c]482
483/** @}
484 */
Note: See TracBrowser for help on using the repository browser.