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
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 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 <synch/waitq.h>
41#include <debug.h>
42#include <udebug/udebug.h>
43#include <errno.h>
44#include <print.h>
45#include <arch.h>
46
47/** Initialize udebug part of task structure.
48 *
49 * Called as part of task structure initialization.
50 * @param ut Pointer to the structure to initialize.
51 *
52 */
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
62/** Initialize udebug part of thread structure.
63 *
64 * Called as part of thread structure initialization.
65 *
66 * @param ut Pointer to the structure to initialize.
67 *
68 */
69void udebug_thread_initialize(udebug_thread_t *ut)
70{
71 mutex_initialize(&ut->lock, MUTEX_PASSIVE);
72 waitq_initialize(&ut->go_wq);
73 condvar_initialize(&ut->active_cv);
74
75 ut->go_call = NULL;
76 ut->uspace_state = NULL;
77 ut->go = false;
78 ut->stoppable = true;
79 ut->active = false;
80 ut->cur_event = 0; /* None */
81}
82
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 *
89 * @param wq The wait queue used by the thread to wait for GO messages.
90 *
91 */
92static void udebug_wait_for_go(waitq_t *wq)
93{
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
99 waitq_sleep_finish(wq, rc, ipl);
100}
101
102/** Start of stoppable section.
103 *
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.
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).
112 *
113 */
114void udebug_stoppable_begin(void)
115{
116 ASSERT(THREAD);
117 ASSERT(TASK);
118
119 mutex_lock(&TASK->udebug.lock);
120
121 int nsc = --TASK->udebug.not_stoppable_count;
122
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;
127
128 if ((TASK->udebug.dt_state == UDEBUG_TS_BEGINNING) && (nsc == 0)) {
129 /*
130 * This was the last non-stoppable thread. Reply to
131 * DEBUG_BEGIN call.
132 *
133 */
134
135 call_t *db_call = TASK->udebug.begin_call;
136 ASSERT(db_call);
137
138 TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
139 TASK->udebug.begin_call = NULL;
140
141 IPC_SET_RETVAL(db_call->data, 0);
142 ipc_answer(&TASK->answerbox, db_call);
143 } else if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
144 /*
145 * Active debugging session
146 */
147
148 if (THREAD->udebug.active == true &&
149 THREAD->udebug.go == false) {
150 /*
151 * Thread was requested to stop - answer go call
152 *
153 */
154
155 /* Make sure nobody takes this call away from us */
156 call_t *go_call = THREAD->udebug.go_call;
157 THREAD->udebug.go_call = NULL;
158 ASSERT(go_call);
159
160 IPC_SET_RETVAL(go_call->data, 0);
161 IPC_SET_ARG1(go_call->data, UDEBUG_EVENT_STOP);
162
163 THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
164 ipc_answer(&TASK->answerbox, go_call);
165 }
166 }
167
168 mutex_unlock(&THREAD->udebug.lock);
169 mutex_unlock(&TASK->udebug.lock);
170}
171
172/** End of a stoppable section.
173 *
174 * This is the point where the thread will block if it is stopped.
175 * (As, by definition, a stopped thread must not leave its stoppable
176 * section).
177 *
178 */
179void udebug_stoppable_end(void)
180{
181restart:
182 mutex_lock(&TASK->udebug.lock);
183 mutex_lock(&THREAD->udebug.lock);
184
185 if ((THREAD->udebug.active) && (THREAD->udebug.go == false)) {
186 mutex_unlock(&THREAD->udebug.lock);
187 mutex_unlock(&TASK->udebug.lock);
188
189 udebug_wait_for_go(&THREAD->udebug.go_wq);
190
191 goto restart;
192 /* Must try again - have to lose stoppability atomically. */
193 } else {
194 ++TASK->udebug.not_stoppable_count;
195 ASSERT(THREAD->udebug.stoppable == true);
196 THREAD->udebug.stoppable = false;
197
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 *
205 * This function is called from clock().
206 *
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
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.
219 *
220 */
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,
223 bool end_variant)
224{
225 udebug_event_t etype =
226 end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
227
228 mutex_lock(&TASK->udebug.lock);
229 mutex_lock(&THREAD->udebug.lock);
230
231 /* Must only generate events when in debugging session and is go. */
232 if (THREAD->udebug.active != true || THREAD->udebug.go == false ||
233 (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
234 mutex_unlock(&THREAD->udebug.lock);
235 mutex_unlock(&TASK->udebug.lock);
236 return;
237 }
238
239 /* Fill in the GO response. */
240 call_t *call = THREAD->udebug.go_call;
241 THREAD->udebug.go_call = NULL;
242
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);
247
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;
254
255 /*
256 * Make sure udebug.go is false when going to sleep
257 * in case we get woken up by DEBUG_END. (At which
258 * point it must be back to the initial true value).
259 *
260 */
261 THREAD->udebug.go = false;
262 THREAD->udebug.cur_event = etype;
263
264 ipc_answer(&TASK->answerbox, call);
265
266 mutex_unlock(&THREAD->udebug.lock);
267 mutex_unlock(&TASK->udebug.lock);
268
269 udebug_wait_for_go(&THREAD->udebug.go_wq);
270}
271
272/** Thread-creation event hook combined with attaching the thread.
273 *
274 * Must be called when a new userspace thread is created in the debugged
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.
283 *
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 *
288 */
289void udebug_thread_b_event_attach(struct thread *thread, struct task *task)
290{
291 mutex_lock(&TASK->udebug.lock);
292 mutex_lock(&THREAD->udebug.lock);
293
294 thread_attach(thread, task);
295
296 LOG("Check state");
297
298 /* Must only generate events when in debugging session */
299 if (THREAD->udebug.active != true) {
300 LOG("udebug.active: %s, udebug.go: %s",
301 THREAD->udebug.active ? "Yes(+)" : "No",
302 THREAD->udebug.go ? "Yes(-)" : "No");
303
304 mutex_unlock(&THREAD->udebug.lock);
305 mutex_unlock(&TASK->udebug.lock);
306 return;
307 }
308
309 LOG("Trigger event");
310
311 call_t *call = THREAD->udebug.go_call;
312
313 THREAD->udebug.go_call = NULL;
314 IPC_SET_RETVAL(call->data, 0);
315 IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_B);
316 IPC_SET_ARG2(call->data, (sysarg_t) thread);
317
318 /*
319 * Make sure udebug.go is false when going to sleep
320 * in case we get woken up by DEBUG_END. (At which
321 * point it must be back to the initial true value).
322 *
323 */
324 THREAD->udebug.go = false;
325 THREAD->udebug.cur_event = UDEBUG_EVENT_THREAD_B;
326
327 ipc_answer(&TASK->answerbox, call);
328
329 mutex_unlock(&THREAD->udebug.lock);
330 mutex_unlock(&TASK->udebug.lock);
331
332 LOG("Wait for Go");
333 udebug_wait_for_go(&THREAD->udebug.go_wq);
334}
335
336/** Thread-termination event hook.
337 *
338 * Must be called when the current thread is terminating.
339 * Generates a THREAD_E event.
340 *
341 */
342void udebug_thread_e_event(void)
343{
344 mutex_lock(&TASK->udebug.lock);
345 mutex_lock(&THREAD->udebug.lock);
346
347 LOG("Check state");
348
349 /* Must only generate events when in debugging session. */
350 if (THREAD->udebug.active != true) {
351 LOG("udebug.active: %s, udebug.go: %s",
352 THREAD->udebug.active ? "Yes" : "No",
353 THREAD->udebug.go ? "Yes" : "No");
354
355 mutex_unlock(&THREAD->udebug.lock);
356 mutex_unlock(&TASK->udebug.lock);
357 return;
358 }
359
360 LOG("Trigger event");
361
362 call_t *call = THREAD->udebug.go_call;
363
364 THREAD->udebug.go_call = NULL;
365 IPC_SET_RETVAL(call->data, 0);
366 IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_E);
367
368 /* Prevent any further debug activity in thread. */
369 THREAD->udebug.active = false;
370 THREAD->udebug.cur_event = 0; /* None */
371 THREAD->udebug.go = false; /* Set to initial value */
372
373 ipc_answer(&TASK->answerbox, call);
374
375 mutex_unlock(&THREAD->udebug.lock);
376 mutex_unlock(&TASK->udebug.lock);
377
378 /*
379 * This event does not sleep - debugging has finished
380 * in this thread.
381 *
382 */
383}
384
385/** Terminate task debugging session.
386 *
387 * Gracefully terminate the debugging session for a task. If the debugger
388 * is still waiting for events on some threads, it will receive a
389 * FINISHED event for each of them.
390 *
391 * @param task Task structure. task->udebug.lock must be already locked.
392 *
393 * @return Zero on success or negative error code.
394 *
395 */
396int udebug_task_cleanup(struct task *task)
397{
398 ASSERT(mutex_locked(&task->udebug.lock));
399
400 if ((task->udebug.dt_state != UDEBUG_TS_BEGINNING) &&
401 (task->udebug.dt_state != UDEBUG_TS_ACTIVE)) {
402 return EINVAL;
403 }
404
405 LOG("Task %" PRIu64, task->taskid);
406
407 /* Finish debugging of all userspace threads */
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
415 /* Only process userspace threads. */
416 if ((flags & THREAD_FLAG_USPACE) != 0) {
417 /* Prevent any further debug activity in thread. */
418 thread->udebug.active = false;
419 thread->udebug.cur_event = 0; /* None */
420
421 /* Is the thread still go? */
422 if (thread->udebug.go == true) {
423 /*
424 * Yes, so clear go. As active == false,
425 * this doesn't affect anything.
426 (
427 */
428 thread->udebug.go = false;
429
430 /* Answer GO call */
431 LOG("Answer GO call with EVENT_FINISHED.");
432
433 IPC_SET_RETVAL(thread->udebug.go_call->data, 0);
434 IPC_SET_ARG1(thread->udebug.go_call->data,
435 UDEBUG_EVENT_FINISHED);
436
437 ipc_answer(&task->answerbox, thread->udebug.go_call);
438 thread->udebug.go_call = NULL;
439 } else {
440 /*
441 * Debug_stop is already at initial value.
442 * Yet this means the thread needs waking up.
443 *
444 */
445
446 /*
447 * thread's lock must not be held when calling
448 * waitq_wakeup.
449 *
450 */
451 waitq_wakeup(&thread->udebug.go_wq, WAKEUP_FIRST);
452 }
453
454 mutex_unlock(&thread->udebug.lock);
455 condvar_broadcast(&thread->udebug.active_cv);
456 } else
457 mutex_unlock(&thread->udebug.lock);
458 }
459
460 task->udebug.dt_state = UDEBUG_TS_INACTIVE;
461 task->udebug.debugger = NULL;
462
463 return 0;
464}
465
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).
472 *
473 */
474void udebug_thread_fault(void)
475{
476 udebug_stoppable_begin();
477
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);
483
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);
489
490 udebug_stoppable_end();
491}
492
493/** @}
494 */
Note: See TracBrowser for help on using the repository browser.