source: mainline/kernel/generic/src/udebug/udebug.c@ 96b02eb9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 96b02eb9 was 96b02eb9, checked in by Martin Decky <martin@…>, 15 years ago

more unification of basic types

  • use sysarg_t and native_t (unsigned and signed variant) in both kernel and uspace
  • remove ipcarg_t in favour of sysarg_t

(no change in functionality)

  • Property mode set to 100644
File size: 13.6 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>
46
[7dc62af]47/** Initialize udebug part of task structure.
48 *
49 * Called as part of task structure initialization.
[da1bafb]50 * @param ut Pointer to the structure to initialize.
51 *
[7dc62af]52 */
[9a1b20c]53void udebug_task_init(udebug_task_t *ut)
54{
55 mutex_initialize(&ut->lock, MUTEX_PASSIVE);
56 ut->dt_state = UDEBUG_TS_INACTIVE;
57 ut->begin_call = NULL;
58 ut->not_stoppable_count = 0;
59 ut->evmask = 0;
60}
61
[7dc62af]62/** Initialize udebug part of thread structure.
63 *
64 * Called as part of thread structure initialization.
[da1bafb]65 *
66 * @param ut Pointer to the structure to initialize.
67 *
[7dc62af]68 */
[9a1b20c]69void udebug_thread_initialize(udebug_thread_t *ut)
70{
71 mutex_initialize(&ut->lock, MUTEX_PASSIVE);
72 waitq_initialize(&ut->go_wq);
[a074b4f]73 condvar_initialize(&ut->active_cv);
[da1bafb]74
[9a1b20c]75 ut->go_call = NULL;
[3ff2b54]76 ut->uspace_state = NULL;
[384c488]77 ut->go = false;
[9a1b20c]78 ut->stoppable = true;
[8af9950]79 ut->active = false;
[da1bafb]80 ut->cur_event = 0; /* None */
[9a1b20c]81}
82
[7dc62af]83/** Wait for a GO message.
84 *
85 * When a debugging event occurs in a thread or the thread is stopped,
86 * this function is called to block the thread until a GO message
87 * is received.
88 *
[da1bafb]89 * @param wq The wait queue used by the thread to wait for GO messages.
90 *
[7dc62af]91 */
[9a1b20c]92static void udebug_wait_for_go(waitq_t *wq)
93{
[da1bafb]94 ipl_t ipl = waitq_sleep_prepare(wq);
95
96 wq->missed_wakeups = 0; /* Enforce blocking. */
97 int rc = waitq_sleep_timeout_unsafe(wq, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
98
[9a1b20c]99 waitq_sleep_finish(wq, rc, ipl);
100}
101
[7dc62af]102/** Start of stoppable section.
103 *
[da1bafb]104 * A stoppable section is a section of code where if the thread can
105 * be stoped. In other words, if a STOP operation is issued, the thread
106 * is guaranteed not to execute any userspace instructions until the
107 * thread is resumed.
[7dc62af]108 *
109 * Having stoppable sections is better than having stopping points, since
110 * a thread can be stopped even when it is blocked indefinitely in a system
111 * call (whereas it would not reach any stopping point).
[da1bafb]112 *
[7dc62af]113 */
[9a1b20c]114void udebug_stoppable_begin(void)
115{
116 ASSERT(THREAD);
117 ASSERT(TASK);
[da1bafb]118
[9a1b20c]119 mutex_lock(&TASK->udebug.lock);
[da1bafb]120
121 int nsc = --TASK->udebug.not_stoppable_count;
122
[9a1b20c]123 /* Lock order OK, THREAD->udebug.lock is after TASK->udebug.lock */
124 mutex_lock(&THREAD->udebug.lock);
125 ASSERT(THREAD->udebug.stoppable == false);
126 THREAD->udebug.stoppable = true;
[da1bafb]127
128 if ((TASK->udebug.dt_state == UDEBUG_TS_BEGINNING) && (nsc == 0)) {
[9a1b20c]129 /*
130 * This was the last non-stoppable thread. Reply to
131 * DEBUG_BEGIN call.
[da1bafb]132 *
[9a1b20c]133 */
[da1bafb]134
135 call_t *db_call = TASK->udebug.begin_call;
[9a1b20c]136 ASSERT(db_call);
[da1bafb]137
[9a1b20c]138 TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
139 TASK->udebug.begin_call = NULL;
[da1bafb]140
[9a1b20c]141 IPC_SET_RETVAL(db_call->data, 0);
[da1bafb]142 ipc_answer(&TASK->answerbox, db_call);
[9a1b20c]143 } else if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
144 /*
145 * Active debugging session
146 */
[da1bafb]147
[8af9950]148 if (THREAD->udebug.active == true &&
[384c488]149 THREAD->udebug.go == false) {
[9a1b20c]150 /*
151 * Thread was requested to stop - answer go call
[da1bafb]152 *
[9a1b20c]153 */
[da1bafb]154
[9a1b20c]155 /* Make sure nobody takes this call away from us */
[da1bafb]156 call_t *go_call = THREAD->udebug.go_call;
[9a1b20c]157 THREAD->udebug.go_call = NULL;
158 ASSERT(go_call);
[da1bafb]159
[9a1b20c]160 IPC_SET_RETVAL(go_call->data, 0);
161 IPC_SET_ARG1(go_call->data, UDEBUG_EVENT_STOP);
[da1bafb]162
[9a1b20c]163 THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
[da1bafb]164 ipc_answer(&TASK->answerbox, go_call);
[9a1b20c]165 }
166 }
[da1bafb]167
[9a1b20c]168 mutex_unlock(&THREAD->udebug.lock);
169 mutex_unlock(&TASK->udebug.lock);
170}
171
[7dc62af]172/** End of a stoppable section.
173 *
174 * This is the point where the thread will block if it is stopped.
[da1bafb]175 * (As, by definition, a stopped thread must not leave its stoppable
176 * section).
177 *
[7dc62af]178 */
[9a1b20c]179void udebug_stoppable_end(void)
180{
181restart:
182 mutex_lock(&TASK->udebug.lock);
183 mutex_lock(&THREAD->udebug.lock);
[da1bafb]184
185 if ((THREAD->udebug.active) && (THREAD->udebug.go == false)) {
[9a1b20c]186 mutex_unlock(&THREAD->udebug.lock);
187 mutex_unlock(&TASK->udebug.lock);
[da1bafb]188
[9a1b20c]189 udebug_wait_for_go(&THREAD->udebug.go_wq);
[da1bafb]190
[9a1b20c]191 goto restart;
[1378b2b]192 /* Must try again - have to lose stoppability atomically. */
[9a1b20c]193 } else {
194 ++TASK->udebug.not_stoppable_count;
195 ASSERT(THREAD->udebug.stoppable == true);
196 THREAD->udebug.stoppable = false;
[da1bafb]197
[9a1b20c]198 mutex_unlock(&THREAD->udebug.lock);
199 mutex_unlock(&TASK->udebug.lock);
200 }
201}
202
203/** Upon being scheduled to run, check if the current thread should stop.
204 *
[3ff2b54]205 * This function is called from clock().
[da1bafb]206 *
[9a1b20c]207 */
208void udebug_before_thread_runs(void)
209{
210 /* Check if we're supposed to stop */
211 udebug_stoppable_begin();
212 udebug_stoppable_end();
213}
214
[7dc62af]215/** Syscall event hook.
216 *
217 * Must be called before and after servicing a system call. This generates
218 * a SYSCALL_B or SYSCALL_E event, depending on the value of @a end_variant.
[da1bafb]219 *
[7dc62af]220 */
[96b02eb9]221void udebug_syscall_event(sysarg_t a1, sysarg_t a2, sysarg_t a3,
222 sysarg_t a4, sysarg_t a5, sysarg_t a6, sysarg_t id, sysarg_t rc,
[9a1b20c]223 bool end_variant)
224{
[da1bafb]225 udebug_event_t etype =
226 end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
227
[9a1b20c]228 mutex_lock(&TASK->udebug.lock);
229 mutex_lock(&THREAD->udebug.lock);
[da1bafb]230
[1378b2b]231 /* Must only generate events when in debugging session and is go. */
[8af9950]232 if (THREAD->udebug.active != true || THREAD->udebug.go == false ||
[9a1b20c]233 (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
234 mutex_unlock(&THREAD->udebug.lock);
235 mutex_unlock(&TASK->udebug.lock);
236 return;
237 }
[da1bafb]238
[ae5aa90]239 /* Fill in the GO response. */
[da1bafb]240 call_t *call = THREAD->udebug.go_call;
[9a1b20c]241 THREAD->udebug.go_call = NULL;
[da1bafb]242
[9a1b20c]243 IPC_SET_RETVAL(call->data, 0);
244 IPC_SET_ARG1(call->data, etype);
245 IPC_SET_ARG2(call->data, id);
246 IPC_SET_ARG3(call->data, rc);
[da1bafb]247
[9a1b20c]248 THREAD->udebug.syscall_args[0] = a1;
249 THREAD->udebug.syscall_args[1] = a2;
250 THREAD->udebug.syscall_args[2] = a3;
251 THREAD->udebug.syscall_args[3] = a4;
252 THREAD->udebug.syscall_args[4] = a5;
253 THREAD->udebug.syscall_args[5] = a6;
[da1bafb]254
[9a1b20c]255 /*
[384c488]256 * Make sure udebug.go is false when going to sleep
[9a1b20c]257 * in case we get woken up by DEBUG_END. (At which
258 * point it must be back to the initial true value).
[da1bafb]259 *
[9a1b20c]260 */
[384c488]261 THREAD->udebug.go = false;
[9a1b20c]262 THREAD->udebug.cur_event = etype;
[da1bafb]263
[9a1b20c]264 ipc_answer(&TASK->answerbox, call);
[da1bafb]265
[9a1b20c]266 mutex_unlock(&THREAD->udebug.lock);
267 mutex_unlock(&TASK->udebug.lock);
[da1bafb]268
[9a1b20c]269 udebug_wait_for_go(&THREAD->udebug.go_wq);
270}
271
[13964ef]272/** Thread-creation event hook combined with attaching the thread.
[7dc62af]273 *
274 * Must be called when a new userspace thread is created in the debugged
[13964ef]275 * task. Generates a THREAD_B event. Also attaches the thread @a t
276 * to the task @a ta.
277 *
278 * This is necessary to avoid a race condition where the BEGIN and THREAD_READ
279 * requests would be handled inbetween attaching the thread and checking it
280 * for being in a debugging session to send the THREAD_B event. We could then
281 * either miss threads or get some threads both in the thread list
282 * and get a THREAD_B event for them.
[7dc62af]283 *
[da1bafb]284 * @param thread Structure of the thread being created. Not locked, as the
285 * thread is not executing yet.
286 * @param task Task to which the thread should be attached.
287 *
[7dc62af]288 */
[da1bafb]289void udebug_thread_b_event_attach(struct thread *thread, struct task *task)
[9a1b20c]290{
291 mutex_lock(&TASK->udebug.lock);
292 mutex_lock(&THREAD->udebug.lock);
[da1bafb]293
294 thread_attach(thread, task);
295
[ae5aa90]296 LOG("Check state");
[da1bafb]297
[9a1b20c]298 /* Must only generate events when in debugging session */
[8af9950]299 if (THREAD->udebug.active != true) {
[ae5aa90]300 LOG("udebug.active: %s, udebug.go: %s",
[da1bafb]301 THREAD->udebug.active ? "Yes(+)" : "No",
302 THREAD->udebug.go ? "Yes(-)" : "No");
303
[9a1b20c]304 mutex_unlock(&THREAD->udebug.lock);
305 mutex_unlock(&TASK->udebug.lock);
306 return;
307 }
[da1bafb]308
[ae5aa90]309 LOG("Trigger event");
[da1bafb]310
311 call_t *call = THREAD->udebug.go_call;
312
[9a1b20c]313 THREAD->udebug.go_call = NULL;
314 IPC_SET_RETVAL(call->data, 0);
315 IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_B);
[96b02eb9]316 IPC_SET_ARG2(call->data, (sysarg_t) thread);
[da1bafb]317
[9a1b20c]318 /*
[384c488]319 * Make sure udebug.go is false when going to sleep
[9a1b20c]320 * in case we get woken up by DEBUG_END. (At which
321 * point it must be back to the initial true value).
[da1bafb]322 *
[9a1b20c]323 */
[384c488]324 THREAD->udebug.go = false;
[9a1b20c]325 THREAD->udebug.cur_event = UDEBUG_EVENT_THREAD_B;
[da1bafb]326
[9a1b20c]327 ipc_answer(&TASK->answerbox, call);
[da1bafb]328
[9a1b20c]329 mutex_unlock(&THREAD->udebug.lock);
330 mutex_unlock(&TASK->udebug.lock);
[da1bafb]331
[ae5aa90]332 LOG("Wait for Go");
[9a1b20c]333 udebug_wait_for_go(&THREAD->udebug.go_wq);
334}
335
[7dc62af]336/** Thread-termination event hook.
337 *
338 * Must be called when the current thread is terminating.
339 * Generates a THREAD_E event.
[da1bafb]340 *
[7dc62af]341 */
[9a1b20c]342void udebug_thread_e_event(void)
343{
344 mutex_lock(&TASK->udebug.lock);
345 mutex_lock(&THREAD->udebug.lock);
[da1bafb]346
[ae5aa90]347 LOG("Check state");
[da1bafb]348
[1378b2b]349 /* Must only generate events when in debugging session. */
[8af9950]350 if (THREAD->udebug.active != true) {
[ae5aa90]351 LOG("udebug.active: %s, udebug.go: %s",
[da1bafb]352 THREAD->udebug.active ? "Yes" : "No",
353 THREAD->udebug.go ? "Yes" : "No");
354
[9a1b20c]355 mutex_unlock(&THREAD->udebug.lock);
356 mutex_unlock(&TASK->udebug.lock);
357 return;
358 }
[da1bafb]359
[ae5aa90]360 LOG("Trigger event");
[da1bafb]361
362 call_t *call = THREAD->udebug.go_call;
363
[9a1b20c]364 THREAD->udebug.go_call = NULL;
365 IPC_SET_RETVAL(call->data, 0);
366 IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_E);
[da1bafb]367
[1378b2b]368 /* Prevent any further debug activity in thread. */
[8af9950]369 THREAD->udebug.active = false;
[da1bafb]370 THREAD->udebug.cur_event = 0; /* None */
371 THREAD->udebug.go = false; /* Set to initial value */
372
[9a1b20c]373 ipc_answer(&TASK->answerbox, call);
[da1bafb]374
[9a1b20c]375 mutex_unlock(&THREAD->udebug.lock);
376 mutex_unlock(&TASK->udebug.lock);
[da1bafb]377
378 /*
[32e6c9c]379 * This event does not sleep - debugging has finished
380 * in this thread.
[da1bafb]381 *
[32e6c9c]382 */
[9a1b20c]383}
384
[da1bafb]385/** Terminate task debugging session.
[9a1b20c]386 *
[da1bafb]387 * Gracefully terminate the debugging session for a task. If the debugger
[7dc62af]388 * is still waiting for events on some threads, it will receive a
389 * FINISHED event for each of them.
390 *
[1d432f9]391 * @param task Task structure. task->udebug.lock must be already locked.
[da1bafb]392 *
393 * @return Zero on success or negative error code.
394 *
[9a1b20c]395 */
[da1bafb]396int udebug_task_cleanup(struct task *task)
[9a1b20c]397{
[1d432f9]398 ASSERT(mutex_locked(&task->udebug.lock));
399
[da1bafb]400 if ((task->udebug.dt_state != UDEBUG_TS_BEGINNING) &&
401 (task->udebug.dt_state != UDEBUG_TS_ACTIVE)) {
[9a1b20c]402 return EINVAL;
403 }
[da1bafb]404
405 LOG("Task %" PRIu64, task->taskid);
406
[9a1b20c]407 /* Finish debugging of all userspace threads */
[da1bafb]408 link_t *cur;
409 for (cur = task->th_head.next; cur != &task->th_head; cur = cur->next) {
410 thread_t *thread = list_get_instance(cur, thread_t, th_link);
411
412 mutex_lock(&thread->udebug.lock);
413 unsigned int flags = thread->flags;
414
[1378b2b]415 /* Only process userspace threads. */
[9a1b20c]416 if ((flags & THREAD_FLAG_USPACE) != 0) {
[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.