source: mainline/kernel/generic/src/udebug/udebug.c@ 207e8880

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 207e8880 was 1066041, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

preemption_disable: Turned functions into macros. Moved THREAD, AS, TASK, CPU into thread.h, as.h, task.h, cpu.h to fix the include hell that ensued.

  • Property mode set to 100644
File size: 13.5 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
29/** @addtogroup generic
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
[9a1b20c]40#include <synch/waitq.h>
[0108984a]41#include <debug.h>
[9a1b20c]42#include <udebug/udebug.h>
43#include <errno.h>
[1902113]44#include <print.h>
[9a1b20c]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);
[da1bafb]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{
[da1bafb]96 ipl_t ipl = waitq_sleep_prepare(wq);
97
98 wq->missed_wakeups = 0; /* Enforce blocking. */
99 int rc = waitq_sleep_timeout_unsafe(wq, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
100
[9a1b20c]101 waitq_sleep_finish(wq, rc, ipl);
102}
103
[7dc62af]104/** Start of stoppable section.
105 *
[da1bafb]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.
[7dc62af]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).
[da1bafb]114 *
[7dc62af]115 */
[9a1b20c]116void udebug_stoppable_begin(void)
117{
118 ASSERT(THREAD);
119 ASSERT(TASK);
[da1bafb]120
[9a1b20c]121 mutex_lock(&TASK->udebug.lock);
[da1bafb]122
123 int nsc = --TASK->udebug.not_stoppable_count;
124
[9a1b20c]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;
[da1bafb]129
130 if ((TASK->udebug.dt_state == UDEBUG_TS_BEGINNING) && (nsc == 0)) {
[9a1b20c]131 /*
132 * This was the last non-stoppable thread. Reply to
133 * DEBUG_BEGIN call.
[da1bafb]134 *
[9a1b20c]135 */
[da1bafb]136
137 call_t *db_call = TASK->udebug.begin_call;
[9a1b20c]138 ASSERT(db_call);
[da1bafb]139
[9a1b20c]140 TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
141 TASK->udebug.begin_call = NULL;
[da1bafb]142
[9a1b20c]143 IPC_SET_RETVAL(db_call->data, 0);
[da1bafb]144 ipc_answer(&TASK->answerbox, db_call);
[9a1b20c]145 } else if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
146 /*
147 * Active debugging session
148 */
[da1bafb]149
[8af9950]150 if (THREAD->udebug.active == true &&
[384c488]151 THREAD->udebug.go == false) {
[9a1b20c]152 /*
153 * Thread was requested to stop - answer go call
[da1bafb]154 *
[9a1b20c]155 */
[da1bafb]156
[9a1b20c]157 /* Make sure nobody takes this call away from us */
[da1bafb]158 call_t *go_call = THREAD->udebug.go_call;
[9a1b20c]159 THREAD->udebug.go_call = NULL;
160 ASSERT(go_call);
[da1bafb]161
[9a1b20c]162 IPC_SET_RETVAL(go_call->data, 0);
163 IPC_SET_ARG1(go_call->data, UDEBUG_EVENT_STOP);
[da1bafb]164
[9a1b20c]165 THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
[da1bafb]166 ipc_answer(&TASK->answerbox, go_call);
[9a1b20c]167 }
168 }
[da1bafb]169
[9a1b20c]170 mutex_unlock(&THREAD->udebug.lock);
171 mutex_unlock(&TASK->udebug.lock);
172}
173
[7dc62af]174/** End of a stoppable section.
175 *
176 * This is the point where the thread will block if it is stopped.
[da1bafb]177 * (As, by definition, a stopped thread must not leave its stoppable
178 * section).
179 *
[7dc62af]180 */
[9a1b20c]181void udebug_stoppable_end(void)
182{
183restart:
184 mutex_lock(&TASK->udebug.lock);
185 mutex_lock(&THREAD->udebug.lock);
[da1bafb]186
187 if ((THREAD->udebug.active) && (THREAD->udebug.go == false)) {
[9a1b20c]188 mutex_unlock(&THREAD->udebug.lock);
189 mutex_unlock(&TASK->udebug.lock);
[da1bafb]190
[9a1b20c]191 udebug_wait_for_go(&THREAD->udebug.go_wq);
[da1bafb]192
[9a1b20c]193 goto restart;
[1378b2b]194 /* Must try again - have to lose stoppability atomically. */
[9a1b20c]195 } else {
196 ++TASK->udebug.not_stoppable_count;
197 ASSERT(THREAD->udebug.stoppable == true);
198 THREAD->udebug.stoppable = false;
[da1bafb]199
[9a1b20c]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 *
[3ff2b54]207 * This function is called from clock().
[da1bafb]208 *
[9a1b20c]209 */
210void udebug_before_thread_runs(void)
211{
212 /* Check if we're supposed to stop */
213 udebug_stoppable_begin();
214 udebug_stoppable_end();
215}
216
[7dc62af]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.
[da1bafb]221 *
[7dc62af]222 */
[96b02eb9]223void 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,
[9a1b20c]225 bool end_variant)
226{
[da1bafb]227 udebug_event_t etype =
228 end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
229
[9a1b20c]230 mutex_lock(&TASK->udebug.lock);
231 mutex_lock(&THREAD->udebug.lock);
[da1bafb]232
[1378b2b]233 /* Must only generate events when in debugging session and is go. */
[8af9950]234 if (THREAD->udebug.active != true || THREAD->udebug.go == false ||
[9a1b20c]235 (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
236 mutex_unlock(&THREAD->udebug.lock);
237 mutex_unlock(&TASK->udebug.lock);
238 return;
239 }
[da1bafb]240
[ae5aa90]241 /* Fill in the GO response. */
[da1bafb]242 call_t *call = THREAD->udebug.go_call;
[9a1b20c]243 THREAD->udebug.go_call = NULL;
[da1bafb]244
[9a1b20c]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);
[da1bafb]249
[9a1b20c]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;
[da1bafb]256
[9a1b20c]257 /*
[384c488]258 * Make sure udebug.go is false when going to sleep
[9a1b20c]259 * in case we get woken up by DEBUG_END. (At which
260 * point it must be back to the initial true value).
[da1bafb]261 *
[9a1b20c]262 */
[384c488]263 THREAD->udebug.go = false;
[9a1b20c]264 THREAD->udebug.cur_event = etype;
[da1bafb]265
[9a1b20c]266 ipc_answer(&TASK->answerbox, call);
[da1bafb]267
[9a1b20c]268 mutex_unlock(&THREAD->udebug.lock);
269 mutex_unlock(&TASK->udebug.lock);
[da1bafb]270
[9a1b20c]271 udebug_wait_for_go(&THREAD->udebug.go_wq);
272}
273
[13964ef]274/** Thread-creation event hook combined with attaching the thread.
[7dc62af]275 *
276 * Must be called when a new userspace thread is created in the debugged
[13964ef]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.
[7dc62af]285 *
[da1bafb]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 *
[7dc62af]290 */
[da1bafb]291void udebug_thread_b_event_attach(struct thread *thread, struct task *task)
[9a1b20c]292{
293 mutex_lock(&TASK->udebug.lock);
294 mutex_lock(&THREAD->udebug.lock);
[da1bafb]295
296 thread_attach(thread, task);
297
[ae5aa90]298 LOG("Check state");
[da1bafb]299
[9a1b20c]300 /* Must only generate events when in debugging session */
[8af9950]301 if (THREAD->udebug.active != true) {
[ae5aa90]302 LOG("udebug.active: %s, udebug.go: %s",
[da1bafb]303 THREAD->udebug.active ? "Yes(+)" : "No",
304 THREAD->udebug.go ? "Yes(-)" : "No");
305
[9a1b20c]306 mutex_unlock(&THREAD->udebug.lock);
307 mutex_unlock(&TASK->udebug.lock);
308 return;
309 }
[da1bafb]310
[ae5aa90]311 LOG("Trigger event");
[da1bafb]312
313 call_t *call = THREAD->udebug.go_call;
314
[9a1b20c]315 THREAD->udebug.go_call = NULL;
316 IPC_SET_RETVAL(call->data, 0);
317 IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_B);
[96b02eb9]318 IPC_SET_ARG2(call->data, (sysarg_t) thread);
[da1bafb]319
[9a1b20c]320 /*
[384c488]321 * Make sure udebug.go is false when going to sleep
[9a1b20c]322 * in case we get woken up by DEBUG_END. (At which
323 * point it must be back to the initial true value).
[da1bafb]324 *
[9a1b20c]325 */
[384c488]326 THREAD->udebug.go = false;
[9a1b20c]327 THREAD->udebug.cur_event = UDEBUG_EVENT_THREAD_B;
[da1bafb]328
[9a1b20c]329 ipc_answer(&TASK->answerbox, call);
[da1bafb]330
[9a1b20c]331 mutex_unlock(&THREAD->udebug.lock);
332 mutex_unlock(&TASK->udebug.lock);
[da1bafb]333
[ae5aa90]334 LOG("Wait for Go");
[9a1b20c]335 udebug_wait_for_go(&THREAD->udebug.go_wq);
336}
337
[7dc62af]338/** Thread-termination event hook.
339 *
340 * Must be called when the current thread is terminating.
341 * Generates a THREAD_E event.
[da1bafb]342 *
[7dc62af]343 */
[9a1b20c]344void udebug_thread_e_event(void)
345{
346 mutex_lock(&TASK->udebug.lock);
347 mutex_lock(&THREAD->udebug.lock);
[da1bafb]348
[ae5aa90]349 LOG("Check state");
[da1bafb]350
[1378b2b]351 /* Must only generate events when in debugging session. */
[8af9950]352 if (THREAD->udebug.active != true) {
[ae5aa90]353 LOG("udebug.active: %s, udebug.go: %s",
[da1bafb]354 THREAD->udebug.active ? "Yes" : "No",
355 THREAD->udebug.go ? "Yes" : "No");
356
[9a1b20c]357 mutex_unlock(&THREAD->udebug.lock);
358 mutex_unlock(&TASK->udebug.lock);
359 return;
360 }
[da1bafb]361
[ae5aa90]362 LOG("Trigger event");
[da1bafb]363
364 call_t *call = THREAD->udebug.go_call;
365
[9a1b20c]366 THREAD->udebug.go_call = NULL;
367 IPC_SET_RETVAL(call->data, 0);
368 IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_E);
[da1bafb]369
[1378b2b]370 /* Prevent any further debug activity in thread. */
[8af9950]371 THREAD->udebug.active = false;
[da1bafb]372 THREAD->udebug.cur_event = 0; /* None */
373 THREAD->udebug.go = false; /* Set to initial value */
374
[9a1b20c]375 ipc_answer(&TASK->answerbox, call);
[da1bafb]376
[9a1b20c]377 mutex_unlock(&THREAD->udebug.lock);
378 mutex_unlock(&TASK->udebug.lock);
[da1bafb]379
380 /*
[32e6c9c]381 * This event does not sleep - debugging has finished
382 * in this thread.
[da1bafb]383 *
[32e6c9c]384 */
[9a1b20c]385}
386
[da1bafb]387/** Terminate task debugging session.
[9a1b20c]388 *
[da1bafb]389 * Gracefully terminate the debugging session for a task. If the debugger
[7dc62af]390 * is still waiting for events on some threads, it will receive a
391 * FINISHED event for each of them.
392 *
[1d432f9]393 * @param task Task structure. task->udebug.lock must be already locked.
[da1bafb]394 *
395 * @return Zero on success or negative error code.
396 *
[9a1b20c]397 */
[da1bafb]398int udebug_task_cleanup(struct task *task)
[9a1b20c]399{
[1d432f9]400 ASSERT(mutex_locked(&task->udebug.lock));
401
[da1bafb]402 if ((task->udebug.dt_state != UDEBUG_TS_BEGINNING) &&
403 (task->udebug.dt_state != UDEBUG_TS_ACTIVE)) {
[9a1b20c]404 return EINVAL;
405 }
[da1bafb]406
407 LOG("Task %" PRIu64, task->taskid);
408
[9a1b20c]409 /* Finish debugging of all userspace threads */
[55b77d9]410 list_foreach(task->threads, cur) {
[da1bafb]411 thread_t *thread = list_get_instance(cur, thread_t, th_link);
412
413 mutex_lock(&thread->udebug.lock);
414
[1378b2b]415 /* Only process userspace threads. */
[6eef3c4]416 if (thread->uspace) {
[1378b2b]417 /* Prevent any further debug activity in thread. */
[da1bafb]418 thread->udebug.active = false;
419 thread->udebug.cur_event = 0; /* None */
420
[1378b2b]421 /* Is the thread still go? */
[da1bafb]422 if (thread->udebug.go == true) {
[9a1b20c]423 /*
[da1bafb]424 * Yes, so clear go. As active == false,
[9a1b20c]425 * this doesn't affect anything.
[da1bafb]426 (
[9a1b20c]427 */
[da1bafb]428 thread->udebug.go = false;
429
[9a1b20c]430 /* Answer GO call */
[ae5aa90]431 LOG("Answer GO call with EVENT_FINISHED.");
[da1bafb]432
433 IPC_SET_RETVAL(thread->udebug.go_call->data, 0);
434 IPC_SET_ARG1(thread->udebug.go_call->data,
[3926f30]435 UDEBUG_EVENT_FINISHED);
[da1bafb]436
437 ipc_answer(&task->answerbox, thread->udebug.go_call);
438 thread->udebug.go_call = NULL;
[9a1b20c]439 } else {
440 /*
441 * Debug_stop is already at initial value.
442 * Yet this means the thread needs waking up.
[da1bafb]443 *
[9a1b20c]444 */
[da1bafb]445
[9a1b20c]446 /*
[da1bafb]447 * thread's lock must not be held when calling
[9a1b20c]448 * waitq_wakeup.
[da1bafb]449 *
[9a1b20c]450 */
[da1bafb]451 waitq_wakeup(&thread->udebug.go_wq, WAKEUP_FIRST);
[9a1b20c]452 }
[da1bafb]453
454 mutex_unlock(&thread->udebug.lock);
455 condvar_broadcast(&thread->udebug.active_cv);
456 } else
457 mutex_unlock(&thread->udebug.lock);
[9a1b20c]458 }
[da1bafb]459
460 task->udebug.dt_state = UDEBUG_TS_INACTIVE;
461 task->udebug.debugger = NULL;
462
[9a1b20c]463 return 0;
464}
465
[0d21b53]466/** Wait for debugger to handle a fault in this thread.
467 *
468 * When a thread faults and someone is subscribed to the FAULT kernel event,
469 * this function is called to wait for a debugging session to give userspace
470 * a chance to examine the faulting thead/task. When the debugging session
471 * is over, this function returns (so that thread/task cleanup can continue).
[da1bafb]472 *
[0d21b53]473 */
474void udebug_thread_fault(void)
475{
476 udebug_stoppable_begin();
[da1bafb]477
[0d21b53]478 /* Wait until a debugger attends to us. */
479 mutex_lock(&THREAD->udebug.lock);
480 while (!THREAD->udebug.active)
481 condvar_wait(&THREAD->udebug.active_cv, &THREAD->udebug.lock);
482 mutex_unlock(&THREAD->udebug.lock);
[da1bafb]483
[0d21b53]484 /* Make sure the debugging session is over before proceeding. */
485 mutex_lock(&THREAD->udebug.lock);
486 while (THREAD->udebug.active)
487 condvar_wait(&THREAD->udebug.active_cv, &THREAD->udebug.lock);
488 mutex_unlock(&THREAD->udebug.lock);
[da1bafb]489
[0d21b53]490 udebug_stoppable_end();
491}
[9a1b20c]492
493/** @}
494 */
Note: See TracBrowser for help on using the repository browser.