Subversion Repositories HelenOS

Rev

Rev 3602 | Rev 3742 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3602 Rev 3665
Line 33... Line 33...
33
/**
33
/**
34
 * @file
34
 * @file
35
 * @brief   Udebug hooks and data structure management.
35
 * @brief   Udebug hooks and data structure management.
36
 *
36
 *
37
 * Udebug is an interface that makes userspace debuggers possible.
37
 * Udebug is an interface that makes userspace debuggers possible.
38
 *
-
 
39
 * Functions in this file are executed directly in each thread, which
-
 
40
 * may or may not be the subject of debugging. The udebug_stoppable_begin/end()
-
 
41
 * functions are also executed in the clock interrupt handler. To avoid
-
 
42
 * deadlock, functions in this file are protected from the interrupt
-
 
43
 * by locking the recursive lock THREAD->udebug.int_lock (just an atomic
-
 
44
 * variable). This prevents udebug_stoppable_begin/end() from being
-
 
45
 * executed in the interrupt handler (they are skipped).
-
 
46
 *
-
 
47
 * Functions in udebug_ops.c and udebug_ipc.c execute in different threads,
-
 
48
 * so they needn't be protected from the (preemptible) interrupt-initiated
-
 
49
 * code.
-
 
50
 */
38
 */
51
 
39
 
52
#include <synch/waitq.h>
40
#include <synch/waitq.h>
53
#include <debug.h>
41
#include <debug.h>
54
#include <udebug/udebug.h>
42
#include <udebug/udebug.h>
55
#include <errno.h>
43
#include <errno.h>
56
#include <arch.h>
44
#include <arch.h>
57
 
45
 
58
static inline void udebug_int_lock(void)
-
 
59
{
-
 
60
    atomic_inc(&THREAD->udebug.int_lock);
-
 
61
}
-
 
62
 
-
 
63
static inline void udebug_int_unlock(void)
-
 
64
{
-
 
65
    atomic_dec(&THREAD->udebug.int_lock);
-
 
66
}
-
 
67
 
46
 
68
/** Initialize udebug part of task structure.
47
/** Initialize udebug part of task structure.
69
 *
48
 *
70
 * Called as part of task structure initialization.
49
 * Called as part of task structure initialization.
71
 * @param ut    Pointer to the structure to initialize.
50
 * @param ut    Pointer to the structure to initialize.
Line 87... Line 66...
87
void udebug_thread_initialize(udebug_thread_t *ut)
66
void udebug_thread_initialize(udebug_thread_t *ut)
88
{
67
{
89
    mutex_initialize(&ut->lock, MUTEX_PASSIVE);
68
    mutex_initialize(&ut->lock, MUTEX_PASSIVE);
90
    waitq_initialize(&ut->go_wq);
69
    waitq_initialize(&ut->go_wq);
91
 
70
 
92
    /*
-
 
93
     * At the beginning the thread is stoppable, so int_lock be set, too.
-
 
94
     */
-
 
95
    atomic_set(&ut->int_lock, 1);
-
 
96
 
-
 
97
    ut->go_call = NULL;
71
    ut->go_call = NULL;
-
 
72
    ut->uspace_state = NULL;
98
    ut->go = false;
73
    ut->go = false;
99
    ut->stoppable = true;
74
    ut->stoppable = true;
100
    ut->debug_active = false;
75
    ut->debug_active = false;
101
    ut->cur_event = 0; /* none */
76
    ut->cur_event = 0; /* none */
102
}
77
}
Line 159... Line 134...
159
    call_t *db_call, *go_call;
134
    call_t *db_call, *go_call;
160
 
135
 
161
    ASSERT(THREAD);
136
    ASSERT(THREAD);
162
    ASSERT(TASK);
137
    ASSERT(TASK);
163
 
138
 
164
    udebug_int_lock();
-
 
165
 
-
 
166
    /* Early check for undebugged tasks */
139
    /* Early check for undebugged tasks */
167
    if (!udebug_thread_precheck()) {
140
    if (!udebug_thread_precheck()) {
168
        udebug_int_unlock();
-
 
169
        return;
141
        return;
170
    }
142
    }
171
 
143
 
172
    mutex_lock(&TASK->udebug.lock);
144
    mutex_lock(&TASK->udebug.lock);
173
 
145
 
Line 229... Line 201...
229
 */
201
 */
230
void udebug_stoppable_end(void)
202
void udebug_stoppable_end(void)
231
{
203
{
232
    /* Early check for undebugged tasks */
204
    /* Early check for undebugged tasks */
233
    if (!udebug_thread_precheck()) {
205
    if (!udebug_thread_precheck()) {
234
        udebug_int_unlock();
-
 
235
        return;
206
        return;
236
    }
207
    }
237
 
208
 
238
restart:
209
restart:
239
    mutex_lock(&TASK->udebug.lock);
210
    mutex_lock(&TASK->udebug.lock);
Line 255... Line 226...
255
        THREAD->udebug.stoppable = false;
226
        THREAD->udebug.stoppable = false;
256
 
227
 
257
        mutex_unlock(&THREAD->udebug.lock);
228
        mutex_unlock(&THREAD->udebug.lock);
258
        mutex_unlock(&TASK->udebug.lock);
229
        mutex_unlock(&TASK->udebug.lock);
259
    }
230
    }
260
 
-
 
261
    udebug_int_unlock();
-
 
262
}
231
}
263
 
232
 
264
/** Upon being scheduled to run, check if the current thread should stop.
233
/** Upon being scheduled to run, check if the current thread should stop.
265
 *
234
 *
266
 * This function is called from clock(). Preemption is enabled.
235
 * This function is called from clock().
267
 * interrupts are disabled, but since this is called after
-
 
268
 * being scheduled-in, we can enable them, if we're careful enough
-
 
269
 * not to allow arbitrary recursion or deadlock with the thread context.
-
 
270
 */
236
 */
271
void udebug_before_thread_runs(void)
237
void udebug_before_thread_runs(void)
272
{
238
{
273
    ipl_t ipl;
-
 
274
 
-
 
275
    return;
-
 
276
    ASSERT(!PREEMPTION_DISABLED);
-
 
277
 
-
 
278
    /*
-
 
279
     * Prevent agains re-entering, such as when preempted inside this
-
 
280
     * function.
-
 
281
     */
-
 
282
    if (atomic_get(&THREAD->udebug.int_lock) != 0)
-
 
283
        return;
-
 
284
 
-
 
285
    udebug_int_lock();
-
 
286
 
-
 
287
    ipl = interrupts_enable();
-
 
288
 
-
 
289
    /* Now we're free to do whatever we need (lock mutexes, sleep, etc.) */
-
 
290
 
-
 
291
    /* Check if we're supposed to stop */
239
    /* Check if we're supposed to stop */
292
    udebug_stoppable_begin();
240
    udebug_stoppable_begin();
293
    udebug_stoppable_end();
241
    udebug_stoppable_end();
294
 
-
 
295
    interrupts_restore(ipl);
-
 
296
 
-
 
297
    udebug_int_unlock();
-
 
298
}
242
}
299
 
243
 
300
/** Syscall event hook.
244
/** Syscall event hook.
301
 *
245
 *
302
 * Must be called before and after servicing a system call. This generates
246
 * Must be called before and after servicing a system call. This generates
Line 309... Line 253...
309
    call_t *call;
253
    call_t *call;
310
    udebug_event_t etype;
254
    udebug_event_t etype;
311
 
255
 
312
    etype = end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
256
    etype = end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
313
 
257
 
314
    udebug_int_lock();
-
 
315
 
-
 
316
    /* Early check for undebugged tasks */
258
    /* Early check for undebugged tasks */
317
    if (!udebug_thread_precheck()) {
259
    if (!udebug_thread_precheck()) {
318
        udebug_int_unlock();
-
 
319
        return;
260
        return;
320
    }
261
    }
321
 
262
 
322
    mutex_lock(&TASK->udebug.lock);
263
    mutex_lock(&TASK->udebug.lock);
323
    mutex_lock(&THREAD->udebug.lock);
264
    mutex_lock(&THREAD->udebug.lock);
Line 360... Line 301...
360
 
301
 
361
    mutex_unlock(&THREAD->udebug.lock);
302
    mutex_unlock(&THREAD->udebug.lock);
362
    mutex_unlock(&TASK->udebug.lock);
303
    mutex_unlock(&TASK->udebug.lock);
363
 
304
 
364
    udebug_wait_for_go(&THREAD->udebug.go_wq);
305
    udebug_wait_for_go(&THREAD->udebug.go_wq);
365
 
-
 
366
    udebug_int_unlock();
-
 
367
}
306
}
368
 
307
 
369
/** Thread-creation event hook.
308
/** Thread-creation event hook combined with attaching the thread.
370
 *
309
 *
371
 * Must be called when a new userspace thread is created in the debugged
310
 * Must be called when a new userspace thread is created in the debugged
-
 
311
 * task. Generates a THREAD_B event. Also attaches the thread @a t
-
 
312
 * to the task @a ta.
-
 
313
 *
-
 
314
 * This is necessary to avoid a race condition where the BEGIN and THREAD_READ
-
 
315
 * requests would be handled inbetween attaching the thread and checking it
-
 
316
 * for being in a debugging session to send the THREAD_B event. We could then
-
 
317
 * either miss threads or get some threads both in the thread list
372
 * task. Generates a THREAD_B event.
318
 * and get a THREAD_B event for them.
373
 *
319
 *
374
 * @param t Structure of the thread being created. Not locked, as the
320
 * @param t Structure of the thread being created. Not locked, as the
375
 *      thread is not executing yet.
321
 *      thread is not executing yet.
-
 
322
 * @param ta    Task to which the thread should be attached.
376
 */
323
 */
377
void udebug_thread_b_event(struct thread *t)
324
void udebug_thread_b_event_attach(struct thread *t, struct task *ta)
378
{
325
{
379
    call_t *call;
326
    call_t *call;
380
 
327
 
381
    udebug_int_lock();
-
 
382
 
-
 
383
    mutex_lock(&TASK->udebug.lock);
328
    mutex_lock(&TASK->udebug.lock);
384
    mutex_lock(&THREAD->udebug.lock);
329
    mutex_lock(&THREAD->udebug.lock);
385
 
330
 
-
 
331
    thread_attach(t, ta);
-
 
332
 
386
    LOG("udebug_thread_b_event\n");
333
    LOG("udebug_thread_b_event\n");
387
    LOG("- check state\n");
334
    LOG("- check state\n");
388
 
335
 
389
    /* Must only generate events when in debugging session */
336
    /* Must only generate events when in debugging session */
390
    if (THREAD->udebug.debug_active != true) {
337
    if (THREAD->udebug.debug_active != true) {
Line 417... Line 364...
417
    mutex_unlock(&THREAD->udebug.lock);
364
    mutex_unlock(&THREAD->udebug.lock);
418
    mutex_unlock(&TASK->udebug.lock);
365
    mutex_unlock(&TASK->udebug.lock);
419
 
366
 
420
    LOG("- sleep\n");
367
    LOG("- sleep\n");
421
    udebug_wait_for_go(&THREAD->udebug.go_wq);
368
    udebug_wait_for_go(&THREAD->udebug.go_wq);
422
 
-
 
423
    udebug_int_unlock();
-
 
424
}
369
}
425
 
370
 
426
/** Thread-termination event hook.
371
/** Thread-termination event hook.
427
 *
372
 *
428
 * Must be called when the current thread is terminating.
373
 * Must be called when the current thread is terminating.
Line 430... Line 375...
430
 */
375
 */
431
void udebug_thread_e_event(void)
376
void udebug_thread_e_event(void)
432
{
377
{
433
    call_t *call;
378
    call_t *call;
434
 
379
 
435
    udebug_int_lock();
-
 
436
 
-
 
437
    mutex_lock(&TASK->udebug.lock);
380
    mutex_lock(&TASK->udebug.lock);
438
    mutex_lock(&THREAD->udebug.lock);
381
    mutex_lock(&THREAD->udebug.lock);
439
 
382
 
440
    LOG("udebug_thread_e_event\n");
383
    LOG("udebug_thread_e_event\n");
441
    LOG("- check state\n");
384
    LOG("- check state\n");
Line 465... Line 408...
465
    ipc_answer(&TASK->answerbox, call);
408
    ipc_answer(&TASK->answerbox, call);
466
 
409
 
467
    mutex_unlock(&THREAD->udebug.lock);
410
    mutex_unlock(&THREAD->udebug.lock);
468
    mutex_unlock(&TASK->udebug.lock);
411
    mutex_unlock(&TASK->udebug.lock);
469
 
412
 
470
    /* Leave int_lock enabled. */
413
    /*
471
    /* This event does not sleep - debugging has finished in this thread. */
414
     * This event does not sleep - debugging has finished
-
 
415
     * in this thread.
-
 
416
     */
472
}
417
}
473
 
418
 
474
/**
419
/**
475
 * Terminate task debugging session.
420
 * Terminate task debugging session.
476
 *
421
 *
Line 489... Line 434...
489
    ipl_t ipl;
434
    ipl_t ipl;
490
 
435
 
491
    LOG("udebug_task_cleanup()\n");
436
    LOG("udebug_task_cleanup()\n");
492
    LOG("task %" PRIu64 "\n", ta->taskid);
437
    LOG("task %" PRIu64 "\n", ta->taskid);
493
 
438
 
494
    udebug_int_lock();
-
 
495
 
-
 
496
    if (ta->udebug.dt_state != UDEBUG_TS_BEGINNING &&
439
    if (ta->udebug.dt_state != UDEBUG_TS_BEGINNING &&
497
        ta->udebug.dt_state != UDEBUG_TS_ACTIVE) {
440
        ta->udebug.dt_state != UDEBUG_TS_ACTIVE) {
498
        LOG("udebug_task_cleanup(): task not being debugged\n");
441
        LOG("udebug_task_cleanup(): task not being debugged\n");
499
        return EINVAL;
442
        return EINVAL;
500
    }
443
    }
Line 552... Line 495...
552
    }
495
    }
553
 
496
 
554
    ta->udebug.dt_state = UDEBUG_TS_INACTIVE;
497
    ta->udebug.dt_state = UDEBUG_TS_INACTIVE;
555
    ta->udebug.debugger = NULL;
498
    ta->udebug.debugger = NULL;
556
 
499
 
557
    udebug_int_unlock();
-
 
558
 
-
 
559
    return 0;
500
    return 0;
560
}
501
}
561
 
502
 
562
 
503
 
563
/** @}
504
/** @}