Subversion Repositories HelenOS

Rev

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

Rev 3474 Rev 3674
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->stop = true;
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
}
103
 
78
 
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 196... Line 168...
196
    } else if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
168
    } else if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
197
        /*
169
        /*
198
         * Active debugging session
170
         * Active debugging session
199
         */
171
         */
200
 
172
 
201
        if (THREAD->udebug.debug_active && THREAD->udebug.stop) {
173
        if (THREAD->udebug.debug_active == true &&
-
 
174
            THREAD->udebug.go == false) {
202
            /*
175
            /*
203
             * Thread was requested to stop - answer go call
176
             * Thread was requested to stop - answer go call
204
             */
177
             */
205
 
178
 
206
            /* Make sure nobody takes this call away from us */
179
            /* Make sure nobody takes this call away from us */
Line 228... Line 201...
228
 */
201
 */
229
void udebug_stoppable_end(void)
202
void udebug_stoppable_end(void)
230
{
203
{
231
    /* Early check for undebugged tasks */
204
    /* Early check for undebugged tasks */
232
    if (!udebug_thread_precheck()) {
205
    if (!udebug_thread_precheck()) {
233
        udebug_int_unlock();
-
 
234
        return;
206
        return;
235
    }
207
    }
236
 
208
 
237
restart:
209
restart:
238
    mutex_lock(&TASK->udebug.lock);
210
    mutex_lock(&TASK->udebug.lock);
239
    mutex_lock(&THREAD->udebug.lock);
211
    mutex_lock(&THREAD->udebug.lock);
240
 
212
 
241
    if (THREAD->udebug.debug_active &&
213
    if (THREAD->udebug.debug_active &&
242
        THREAD->udebug.stop == true) {
214
        THREAD->udebug.go == false) {
243
        TASK->udebug.begin_call = NULL;
215
        TASK->udebug.begin_call = NULL;
244
        mutex_unlock(&THREAD->udebug.lock);
216
        mutex_unlock(&THREAD->udebug.lock);
245
        mutex_unlock(&TASK->udebug.lock);
217
        mutex_unlock(&TASK->udebug.lock);
246
 
218
 
247
        udebug_wait_for_go(&THREAD->udebug.go_wq);
219
        udebug_wait_for_go(&THREAD->udebug.go_wq);
248
 
220
 
249
        goto restart;
221
        goto restart;
250
        /* must try again - have to lose stoppability atomically */
222
        /* Must try again - have to lose stoppability atomically. */
251
    } else {
223
    } else {
252
        ++TASK->udebug.not_stoppable_count;
224
        ++TASK->udebug.not_stoppable_count;
253
        ASSERT(THREAD->udebug.stoppable == true);
225
        ASSERT(THREAD->udebug.stoppable == true);
254
        THREAD->udebug.stoppable = false;
226
        THREAD->udebug.stoppable = false;
255
 
227
 
256
        mutex_unlock(&THREAD->udebug.lock);
228
        mutex_unlock(&THREAD->udebug.lock);
257
        mutex_unlock(&TASK->udebug.lock);
229
        mutex_unlock(&TASK->udebug.lock);
258
    }
230
    }
259
 
-
 
260
    udebug_int_unlock();
-
 
261
}
231
}
262
 
232
 
263
/** 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.
264
 *
234
 *
265
 * This function is called from clock(). Preemption is enabled.
235
 * This function is called from clock().
266
 * interrupts are disabled, but since this is called after
-
 
267
 * being scheduled-in, we can enable them, if we're careful enough
-
 
268
 * not to allow arbitrary recursion or deadlock with the thread context.
-
 
269
 */
236
 */
270
void udebug_before_thread_runs(void)
237
void udebug_before_thread_runs(void)
271
{
238
{
272
    ipl_t ipl;
-
 
273
 
-
 
274
    return;
-
 
275
    ASSERT(!PREEMPTION_DISABLED);
-
 
276
 
-
 
277
    /*
-
 
278
     * Prevent agains re-entering, such as when preempted inside this
-
 
279
     * function.
-
 
280
     */
-
 
281
    if (atomic_get(&THREAD->udebug.int_lock) != 0)
-
 
282
        return;
-
 
283
 
-
 
284
    udebug_int_lock();
-
 
285
 
-
 
286
    ipl = interrupts_enable();
-
 
287
 
-
 
288
    /* Now we're free to do whatever we need (lock mutexes, sleep, etc.) */
-
 
289
 
-
 
290
    /* Check if we're supposed to stop */
239
    /* Check if we're supposed to stop */
291
    udebug_stoppable_begin();
240
    udebug_stoppable_begin();
292
    udebug_stoppable_end();
241
    udebug_stoppable_end();
293
 
-
 
294
    interrupts_restore(ipl);
-
 
295
 
-
 
296
    udebug_int_unlock();
-
 
297
}
242
}
298
 
243
 
299
/** Syscall event hook.
244
/** Syscall event hook.
300
 *
245
 *
301
 * 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 308... Line 253...
308
    call_t *call;
253
    call_t *call;
309
    udebug_event_t etype;
254
    udebug_event_t etype;
310
 
255
 
311
    etype = end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
256
    etype = end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
312
 
257
 
313
    udebug_int_lock();
-
 
314
 
-
 
315
    /* Early check for undebugged tasks */
258
    /* Early check for undebugged tasks */
316
    if (!udebug_thread_precheck()) {
259
    if (!udebug_thread_precheck()) {
317
        udebug_int_unlock();
-
 
318
        return;
260
        return;
319
    }
261
    }
320
 
262
 
321
    mutex_lock(&TASK->udebug.lock);
263
    mutex_lock(&TASK->udebug.lock);
322
    mutex_lock(&THREAD->udebug.lock);
264
    mutex_lock(&THREAD->udebug.lock);
323
 
265
 
324
    /* Must only generate events when in debugging session and have go */
266
    /* Must only generate events when in debugging session and is go. */
325
    if (THREAD->udebug.debug_active != true ||
267
    if (THREAD->udebug.debug_active != true ||
326
        THREAD->udebug.stop == true ||
268
        THREAD->udebug.go == false ||
327
        (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
269
        (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
328
        mutex_unlock(&THREAD->udebug.lock);
270
        mutex_unlock(&THREAD->udebug.lock);
329
        mutex_unlock(&TASK->udebug.lock);
271
        mutex_unlock(&TASK->udebug.lock);
330
        return;
272
        return;
331
    }
273
    }
Line 346... Line 288...
346
    THREAD->udebug.syscall_args[3] = a4;
288
    THREAD->udebug.syscall_args[3] = a4;
347
    THREAD->udebug.syscall_args[4] = a5;
289
    THREAD->udebug.syscall_args[4] = a5;
348
    THREAD->udebug.syscall_args[5] = a6;
290
    THREAD->udebug.syscall_args[5] = a6;
349
 
291
 
350
    /*
292
    /*
351
     * Make sure udebug.stop is true when going to sleep
293
     * Make sure udebug.go is false when going to sleep
352
     * in case we get woken up by DEBUG_END. (At which
294
     * in case we get woken up by DEBUG_END. (At which
353
     * point it must be back to the initial true value).
295
     * point it must be back to the initial true value).
354
     */
296
     */
355
    THREAD->udebug.stop = true;
297
    THREAD->udebug.go = false;
356
    THREAD->udebug.cur_event = etype;
298
    THREAD->udebug.cur_event = etype;
357
 
299
 
358
    ipc_answer(&TASK->answerbox, call);
300
    ipc_answer(&TASK->answerbox, call);
359
 
301
 
360
    mutex_unlock(&THREAD->udebug.lock);
302
    mutex_unlock(&THREAD->udebug.lock);
361
    mutex_unlock(&TASK->udebug.lock);
303
    mutex_unlock(&TASK->udebug.lock);
362
 
304
 
363
    udebug_wait_for_go(&THREAD->udebug.go_wq);
305
    udebug_wait_for_go(&THREAD->udebug.go_wq);
364
 
-
 
365
    udebug_int_unlock();
-
 
366
}
306
}
367
 
307
 
368
/** Thread-creation event hook.
308
/** Thread-creation event hook combined with attaching the thread.
369
 *
309
 *
370
 * 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
371
 * task. Generates a THREAD_B event.
318
 * and get a THREAD_B event for them.
372
 *
319
 *
373
 * @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
374
 *      thread is not executing yet.
321
 *      thread is not executing yet.
-
 
322
 * @param ta    Task to which the thread should be attached.
375
 */
323
 */
376
void udebug_thread_b_event(struct thread *t)
324
void udebug_thread_b_event_attach(struct thread *t, struct task *ta)
377
{
325
{
378
    call_t *call;
326
    call_t *call;
379
 
327
 
380
    udebug_int_lock();
-
 
381
 
-
 
382
    mutex_lock(&TASK->udebug.lock);
328
    mutex_lock(&TASK->udebug.lock);
383
    mutex_lock(&THREAD->udebug.lock);
329
    mutex_lock(&THREAD->udebug.lock);
384
 
330
 
-
 
331
    thread_attach(t, ta);
-
 
332
 
385
    LOG("udebug_thread_b_event\n");
333
    LOG("udebug_thread_b_event\n");
386
    LOG("- check state\n");
334
    LOG("- check state\n");
387
 
335
 
388
    /* Must only generate events when in debugging session */
336
    /* Must only generate events when in debugging session */
389
    if (THREAD->udebug.debug_active != true) {
337
    if (THREAD->udebug.debug_active != true) {
390
        LOG("- debug_active: %s, udebug.stop: %s\n",
338
        LOG("- debug_active: %s, udebug.go: %s\n",
391
            THREAD->udebug.debug_active ? "yes(+)" : "no(-)",
339
            THREAD->udebug.debug_active ? "yes(+)" : "no(-)",
392
            THREAD->udebug.stop ? "yes(-)" : "no(+)");
340
            THREAD->udebug.go ? "yes(-)" : "no(+)");
393
        mutex_unlock(&THREAD->udebug.lock);
341
        mutex_unlock(&THREAD->udebug.lock);
394
        mutex_unlock(&TASK->udebug.lock);
342
        mutex_unlock(&TASK->udebug.lock);
395
        return;
343
        return;
396
    }
344
    }
397
 
345
 
Line 402... Line 350...
402
    IPC_SET_RETVAL(call->data, 0);
350
    IPC_SET_RETVAL(call->data, 0);
403
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_B);
351
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_B);
404
    IPC_SET_ARG2(call->data, (unative_t)t);
352
    IPC_SET_ARG2(call->data, (unative_t)t);
405
 
353
 
406
    /*
354
    /*
407
     * Make sure udebug.stop is true when going to sleep
355
     * Make sure udebug.go is false when going to sleep
408
     * in case we get woken up by DEBUG_END. (At which
356
     * in case we get woken up by DEBUG_END. (At which
409
     * point it must be back to the initial true value).
357
     * point it must be back to the initial true value).
410
     */
358
     */
411
    THREAD->udebug.stop = true;
359
    THREAD->udebug.go = false;
412
    THREAD->udebug.cur_event = UDEBUG_EVENT_THREAD_B;
360
    THREAD->udebug.cur_event = UDEBUG_EVENT_THREAD_B;
413
 
361
 
414
    ipc_answer(&TASK->answerbox, call);
362
    ipc_answer(&TASK->answerbox, call);
415
 
363
 
416
    mutex_unlock(&THREAD->udebug.lock);
364
    mutex_unlock(&THREAD->udebug.lock);
417
    mutex_unlock(&TASK->udebug.lock);
365
    mutex_unlock(&TASK->udebug.lock);
418
 
366
 
419
    LOG("- sleep\n");
367
    LOG("- sleep\n");
420
    udebug_wait_for_go(&THREAD->udebug.go_wq);
368
    udebug_wait_for_go(&THREAD->udebug.go_wq);
421
 
-
 
422
    udebug_int_unlock();
-
 
423
}
369
}
424
 
370
 
425
/** Thread-termination event hook.
371
/** Thread-termination event hook.
426
 *
372
 *
427
 * Must be called when the current thread is terminating.
373
 * Must be called when the current thread is terminating.
Line 429... Line 375...
429
 */
375
 */
430
void udebug_thread_e_event(void)
376
void udebug_thread_e_event(void)
431
{
377
{
432
    call_t *call;
378
    call_t *call;
433
 
379
 
434
    udebug_int_lock();
-
 
435
 
-
 
436
    mutex_lock(&TASK->udebug.lock);
380
    mutex_lock(&TASK->udebug.lock);
437
    mutex_lock(&THREAD->udebug.lock);
381
    mutex_lock(&THREAD->udebug.lock);
438
 
382
 
439
    LOG("udebug_thread_e_event\n");
383
    LOG("udebug_thread_e_event\n");
440
    LOG("- check state\n");
384
    LOG("- check state\n");
441
 
385
 
442
    /* Must only generate events when in debugging session */
386
    /* Must only generate events when in debugging session. */
443
    if (THREAD->udebug.debug_active != true) {
387
    if (THREAD->udebug.debug_active != true) {
444
/*      printf("- debug_active: %s, udebug.stop: %s\n",
388
/*      printf("- debug_active: %s, udebug.go: %s\n",
445
            THREAD->udebug.debug_active ? "yes(+)" : "no(-)",
389
            THREAD->udebug.debug_active ? "yes(+)" : "no(-)",
446
            THREAD->udebug.stop ? "yes(-)" : "no(+)");*/
390
            THREAD->udebug.go ? "yes(-)" : "no(+)");*/
447
        mutex_unlock(&THREAD->udebug.lock);
391
        mutex_unlock(&THREAD->udebug.lock);
448
        mutex_unlock(&TASK->udebug.lock);
392
        mutex_unlock(&TASK->udebug.lock);
449
        return;
393
        return;
450
    }
394
    }
451
 
395
 
Line 454... Line 398...
454
    call = THREAD->udebug.go_call;
398
    call = THREAD->udebug.go_call;
455
    THREAD->udebug.go_call = NULL;
399
    THREAD->udebug.go_call = NULL;
456
    IPC_SET_RETVAL(call->data, 0);
400
    IPC_SET_RETVAL(call->data, 0);
457
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_E);
401
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_E);
458
 
402
 
459
    /* Prevent any further debug activity in thread */
403
    /* Prevent any further debug activity in thread. */
460
    THREAD->udebug.debug_active = false;
404
    THREAD->udebug.debug_active = false;
461
    THREAD->udebug.cur_event = 0;       /* none */
405
    THREAD->udebug.cur_event = 0;       /* none */
462
    THREAD->udebug.stop = true; /* set to initial value */
406
    THREAD->udebug.go = false;  /* set to initial value */
463
 
407
 
464
    ipc_answer(&TASK->answerbox, call);
408
    ipc_answer(&TASK->answerbox, call);
465
 
409
 
466
    mutex_unlock(&THREAD->udebug.lock);
410
    mutex_unlock(&THREAD->udebug.lock);
467
    mutex_unlock(&TASK->udebug.lock);
411
    mutex_unlock(&TASK->udebug.lock);
468
 
412
 
469
    /* Leave int_lock enabled */
413
    /*
470
    /* 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
     */
471
}
417
}
472
 
418
 
473
/**
419
/**
474
 * Terminate task debugging session.
420
 * Terminate task debugging session.
475
 *
421
 *
Line 488... Line 434...
488
    ipl_t ipl;
434
    ipl_t ipl;
489
 
435
 
490
    LOG("udebug_task_cleanup()\n");
436
    LOG("udebug_task_cleanup()\n");
491
    LOG("task %" PRIu64 "\n", ta->taskid);
437
    LOG("task %" PRIu64 "\n", ta->taskid);
492
 
438
 
493
    udebug_int_lock();
-
 
494
 
-
 
495
    if (ta->udebug.dt_state != UDEBUG_TS_BEGINNING &&
439
    if (ta->udebug.dt_state != UDEBUG_TS_BEGINNING &&
496
        ta->udebug.dt_state != UDEBUG_TS_ACTIVE) {
440
        ta->udebug.dt_state != UDEBUG_TS_ACTIVE) {
497
        LOG("udebug_task_cleanup(): task not being debugged\n");
441
        LOG("udebug_task_cleanup(): task not being debugged\n");
498
        return EINVAL;
442
        return EINVAL;
499
    }
443
    }
Line 510... Line 454...
510
        flags = t->flags;
454
        flags = t->flags;
511
 
455
 
512
        spinlock_unlock(&t->lock);
456
        spinlock_unlock(&t->lock);
513
        interrupts_restore(ipl);
457
        interrupts_restore(ipl);
514
 
458
 
515
        /* Only process userspace threads */
459
        /* Only process userspace threads. */
516
        if ((flags & THREAD_FLAG_USPACE) != 0) {
460
        if ((flags & THREAD_FLAG_USPACE) != 0) {
517
            /* Prevent any further debug activity in thread */
461
            /* Prevent any further debug activity in thread. */
518
            t->udebug.debug_active = false;
462
            t->udebug.debug_active = false;
519
            t->udebug.cur_event = 0;    /* none */
463
            t->udebug.cur_event = 0;    /* none */
520
 
464
 
521
            /* Still has go? */
465
            /* Is the thread still go? */
522
            if (t->udebug.stop == false) {
466
            if (t->udebug.go == true) {
523
                /*
467
                /*
524
                * Yes, so clear go. As debug_active == false,
468
                * Yes, so clear go. As debug_active == false,
525
                 * this doesn't affect anything.
469
                 * this doesn't affect anything.
526
                 */
470
                 */
527
                t->udebug.stop = true; 
471
                t->udebug.go = false;  
528
 
472
 
529
                /* Answer GO call */
473
                /* Answer GO call */
530
                LOG("answer GO call with EVENT_FINISHED\n");
474
                LOG("answer GO call with EVENT_FINISHED\n");
531
                IPC_SET_RETVAL(t->udebug.go_call->data, 0);
475
                IPC_SET_RETVAL(t->udebug.go_call->data, 0);
532
                IPC_SET_ARG1(t->udebug.go_call->data,
476
                IPC_SET_ARG1(t->udebug.go_call->data,
Line 551... Line 495...
551
    }
495
    }
552
 
496
 
553
    ta->udebug.dt_state = UDEBUG_TS_INACTIVE;
497
    ta->udebug.dt_state = UDEBUG_TS_INACTIVE;
554
    ta->udebug.debugger = NULL;
498
    ta->udebug.debugger = NULL;
555
 
499
 
556
    udebug_int_unlock();
-
 
557
 
-
 
558
    return 0;
500
    return 0;
559
}
501
}
560
 
502
 
561
 
503
 
562
/** @}
504
/** @}