Subversion Repositories HelenOS

Rev

Rev 3742 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3438 svoboda 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
3457 svoboda 35
 * @brief   Udebug hooks and data structure management.
3438 svoboda 36
 *
3457 svoboda 37
 * Udebug is an interface that makes userspace debuggers possible.
3438 svoboda 38
 */
39
 
40
#include <synch/waitq.h>
3441 svoboda 41
#include <debug.h>
3438 svoboda 42
#include <udebug/udebug.h>
43
#include <errno.h>
44
#include <arch.h>
4073 rimsky 45
#include <arch/asm.h>
46
#include <print.h>
3438 svoboda 47
 
48
 
3457 svoboda 49
/** Initialize udebug part of task structure.
50
 *
51
 * Called as part of task structure initialization.
52
 * @param ut    Pointer to the structure to initialize.
53
 */
3438 svoboda 54
void udebug_task_init(udebug_task_t *ut)
55
{
56
    mutex_initialize(&ut->lock, MUTEX_PASSIVE);
57
    ut->dt_state = UDEBUG_TS_INACTIVE;
58
    ut->begin_call = NULL;
59
    ut->not_stoppable_count = 0;
60
    ut->evmask = 0;
61
}
62
 
3457 svoboda 63
/** Initialize udebug part of thread structure.
64
 *
65
 * Called as part of thread structure initialization.
66
 * @param ut    Pointer to the structure to initialize.
67
 */
3438 svoboda 68
void udebug_thread_initialize(udebug_thread_t *ut)
69
{
70
    mutex_initialize(&ut->lock, MUTEX_PASSIVE);
71
    waitq_initialize(&ut->go_wq);
72
 
73
    ut->go_call = NULL;
3665 rimsky 74
    ut->uspace_state = NULL;
3602 rimsky 75
    ut->go = false;
3438 svoboda 76
    ut->stoppable = true;
3742 rimsky 77
    ut->active = false;
3438 svoboda 78
    ut->cur_event = 0; /* none */
79
}
80
 
3457 svoboda 81
/** Wait for a GO message.
82
 *
83
 * When a debugging event occurs in a thread or the thread is stopped,
84
 * this function is called to block the thread until a GO message
85
 * is received.
86
 *
87
 * @param wq    The wait queue used by the thread to wait for GO messages.
88
 */
3438 svoboda 89
static void udebug_wait_for_go(waitq_t *wq)
90
{
91
    int rc;
92
    ipl_t ipl;
93
 
94
    ipl = waitq_sleep_prepare(wq);
95
 
96
    wq->missed_wakeups = 0; /* Enforce blocking. */
97
    rc = waitq_sleep_timeout_unsafe(wq, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
98
 
99
    waitq_sleep_finish(wq, rc, ipl);
100
}
101
 
102
/** Do a preliminary check that a debugging session is in progress.
103
 *
3457 svoboda 104
 * This only requires the THREAD->udebug.lock mutex (and not TASK->udebug.lock
105
 * mutex). For an undebugged task, this will never block (while there could be
106
 * collisions by different threads on the TASK mutex), thus improving SMP
107
 * perormance for undebugged tasks.
108
 *
109
 * @return  True if the thread was in a debugging session when the function
110
 *      checked, false otherwise.
3438 svoboda 111
 */
112
static bool udebug_thread_precheck(void)
113
{
114
    bool res;
115
 
116
    mutex_lock(&THREAD->udebug.lock);
3742 rimsky 117
    res = THREAD->udebug.active;
3438 svoboda 118
    mutex_unlock(&THREAD->udebug.lock);
119
 
120
    return res;
121
}
122
 
3457 svoboda 123
/** Start of stoppable section.
124
 *
125
 * A stoppable section is a section of code where if the thread can be stoped. In other words,
126
 * if a STOP operation is issued, the thread is guaranteed not to execute
127
 * any userspace instructions until the thread is resumed.
128
 *
129
 * Having stoppable sections is better than having stopping points, since
130
 * a thread can be stopped even when it is blocked indefinitely in a system
131
 * call (whereas it would not reach any stopping point).
132
 */
3438 svoboda 133
void udebug_stoppable_begin(void)
134
{
135
    int nsc;
136
    call_t *db_call, *go_call;
137
 
138
    ASSERT(THREAD);
139
    ASSERT(TASK);
140
 
141
    /* Early check for undebugged tasks */
142
    if (!udebug_thread_precheck()) {
143
        return;
144
    }
145
 
146
    mutex_lock(&TASK->udebug.lock);
147
 
148
    nsc = --TASK->udebug.not_stoppable_count;
149
 
150
    /* Lock order OK, THREAD->udebug.lock is after TASK->udebug.lock */
151
    mutex_lock(&THREAD->udebug.lock);
152
    ASSERT(THREAD->udebug.stoppable == false);
153
    THREAD->udebug.stoppable = true;
154
 
155
    if (TASK->udebug.dt_state == UDEBUG_TS_BEGINNING && nsc == 0) {
156
        /*
157
         * This was the last non-stoppable thread. Reply to
158
         * DEBUG_BEGIN call.
159
         */
160
 
161
        db_call = TASK->udebug.begin_call;
162
        ASSERT(db_call);
163
 
164
        TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
165
        TASK->udebug.begin_call = NULL;
166
 
167
        IPC_SET_RETVAL(db_call->data, 0);
168
        ipc_answer(&TASK->answerbox, db_call);     
169
 
170
    } else if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
171
        /*
172
         * Active debugging session
173
         */
174
 
3742 rimsky 175
        if (THREAD->udebug.active == true &&
3602 rimsky 176
            THREAD->udebug.go == false) {
3438 svoboda 177
            /*
178
             * Thread was requested to stop - answer go call
179
             */
180
 
181
            /* Make sure nobody takes this call away from us */
182
            go_call = THREAD->udebug.go_call;
183
            THREAD->udebug.go_call = NULL;
184
            ASSERT(go_call);
185
 
186
            IPC_SET_RETVAL(go_call->data, 0);
187
            IPC_SET_ARG1(go_call->data, UDEBUG_EVENT_STOP);
188
 
189
            THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
190
 
191
                ipc_answer(&TASK->answerbox, go_call);
192
        }
193
    }
194
 
195
    mutex_unlock(&THREAD->udebug.lock);
196
        mutex_unlock(&TASK->udebug.lock);
197
}
198
 
3457 svoboda 199
/** End of a stoppable section.
200
 *
201
 * This is the point where the thread will block if it is stopped.
202
 * (As, by definition, a stopped thread must not leave its stoppable section).
203
 */
3438 svoboda 204
void udebug_stoppable_end(void)
205
{
206
    /* Early check for undebugged tasks */
207
    if (!udebug_thread_precheck()) {
208
        return;
209
    }
210
 
211
restart:
212
    mutex_lock(&TASK->udebug.lock);
213
    mutex_lock(&THREAD->udebug.lock);
214
 
3742 rimsky 215
    if (THREAD->udebug.active && THREAD->udebug.go == false) {
3438 svoboda 216
        TASK->udebug.begin_call = NULL;
217
        mutex_unlock(&THREAD->udebug.lock);
218
        mutex_unlock(&TASK->udebug.lock);
219
 
220
        udebug_wait_for_go(&THREAD->udebug.go_wq);
221
 
222
        goto restart;
3602 rimsky 223
        /* Must try again - have to lose stoppability atomically. */
3438 svoboda 224
    } else {
225
        ++TASK->udebug.not_stoppable_count;
226
        ASSERT(THREAD->udebug.stoppable == true);
227
        THREAD->udebug.stoppable = false;
228
 
229
        mutex_unlock(&THREAD->udebug.lock);
230
        mutex_unlock(&TASK->udebug.lock);
231
    }
232
}
233
 
234
/** Upon being scheduled to run, check if the current thread should stop.
235
 *
3665 rimsky 236
 * This function is called from clock().
3438 svoboda 237
 */
238
void udebug_before_thread_runs(void)
239
{
240
    /* Check if we're supposed to stop */
241
    udebug_stoppable_begin();
242
    udebug_stoppable_end();
243
}
244
 
3457 svoboda 245
/** Syscall event hook.
246
 *
247
 * Must be called before and after servicing a system call. This generates
248
 * a SYSCALL_B or SYSCALL_E event, depending on the value of @a end_variant.
249
 */
3438 svoboda 250
void udebug_syscall_event(unative_t a1, unative_t a2, unative_t a3,
251
    unative_t a4, unative_t a5, unative_t a6, unative_t id, unative_t rc,
252
    bool end_variant)
253
{
254
    call_t *call;
255
    udebug_event_t etype;
256
 
257
    etype = end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
258
 
259
    /* Early check for undebugged tasks */
260
    if (!udebug_thread_precheck()) {
261
        return;
262
    }
263
 
264
    mutex_lock(&TASK->udebug.lock);
265
    mutex_lock(&THREAD->udebug.lock);
266
 
3602 rimsky 267
    /* Must only generate events when in debugging session and is go. */
3742 rimsky 268
    if (THREAD->udebug.active != true || THREAD->udebug.go == false ||
3438 svoboda 269
        (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
270
        mutex_unlock(&THREAD->udebug.lock);
271
        mutex_unlock(&TASK->udebug.lock);
272
        return;
273
    }
274
 
275
    //printf("udebug_syscall_event\n");
276
    call = THREAD->udebug.go_call;
277
    THREAD->udebug.go_call = NULL;
278
 
279
    IPC_SET_RETVAL(call->data, 0);
280
    IPC_SET_ARG1(call->data, etype);
281
    IPC_SET_ARG2(call->data, id);
282
    IPC_SET_ARG3(call->data, rc);
283
    //printf("udebug_syscall_event/ipc_answer\n");
284
 
285
    THREAD->udebug.syscall_args[0] = a1;
286
    THREAD->udebug.syscall_args[1] = a2;
287
    THREAD->udebug.syscall_args[2] = a3;
288
    THREAD->udebug.syscall_args[3] = a4;
289
    THREAD->udebug.syscall_args[4] = a5;
290
    THREAD->udebug.syscall_args[5] = a6;
291
 
292
    /*
3602 rimsky 293
     * Make sure udebug.go is false when going to sleep
3438 svoboda 294
     * in case we get woken up by DEBUG_END. (At which
295
     * point it must be back to the initial true value).
296
     */
3602 rimsky 297
    THREAD->udebug.go = false;
3438 svoboda 298
    THREAD->udebug.cur_event = etype;
299
 
300
    ipc_answer(&TASK->answerbox, call);
301
 
302
    mutex_unlock(&THREAD->udebug.lock);
303
    mutex_unlock(&TASK->udebug.lock);
304
 
305
    udebug_wait_for_go(&THREAD->udebug.go_wq);
306
}
307
 
3665 rimsky 308
/** Thread-creation event hook combined with attaching the thread.
3457 svoboda 309
 *
310
 * Must be called when a new userspace thread is created in the debugged
3665 rimsky 311
 * task. Generates a THREAD_B event. Also attaches the thread @a t
312
 * to the task @a ta.
3457 svoboda 313
 *
3665 rimsky 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
318
 * and get a THREAD_B event for them.
319
 *
3457 svoboda 320
 * @param t Structure of the thread being created. Not locked, as the
321
 *      thread is not executing yet.
3665 rimsky 322
 * @param ta    Task to which the thread should be attached.
3457 svoboda 323
 */
3665 rimsky 324
void udebug_thread_b_event_attach(struct thread *t, struct task *ta)
3438 svoboda 325
{
326
    call_t *call;
327
 
328
    mutex_lock(&TASK->udebug.lock);
329
    mutex_lock(&THREAD->udebug.lock);
330
 
3665 rimsky 331
    thread_attach(t, ta);
332
 
3441 svoboda 333
    LOG("udebug_thread_b_event\n");
334
    LOG("- check state\n");
3438 svoboda 335
 
336
    /* Must only generate events when in debugging session */
3742 rimsky 337
    if (THREAD->udebug.active != true) {
338
        LOG("- udebug.active: %s, udebug.go: %s\n",
339
            THREAD->udebug.active ? "yes(+)" : "no(-)",
3602 rimsky 340
            THREAD->udebug.go ? "yes(-)" : "no(+)");
3438 svoboda 341
        mutex_unlock(&THREAD->udebug.lock);
342
        mutex_unlock(&TASK->udebug.lock);
343
        return;
344
    }
345
 
3441 svoboda 346
    LOG("- trigger event\n");
3438 svoboda 347
 
348
    call = THREAD->udebug.go_call;
349
    THREAD->udebug.go_call = NULL;
350
    IPC_SET_RETVAL(call->data, 0);
351
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_B);
352
    IPC_SET_ARG2(call->data, (unative_t)t);
353
 
354
    /*
3602 rimsky 355
     * Make sure udebug.go is false when going to sleep
3438 svoboda 356
     * in case we get woken up by DEBUG_END. (At which
357
     * point it must be back to the initial true value).
358
     */
3602 rimsky 359
    THREAD->udebug.go = false;
3438 svoboda 360
    THREAD->udebug.cur_event = UDEBUG_EVENT_THREAD_B;
361
 
362
    ipc_answer(&TASK->answerbox, call);
363
 
364
    mutex_unlock(&THREAD->udebug.lock);
365
    mutex_unlock(&TASK->udebug.lock);
366
 
3441 svoboda 367
    LOG("- sleep\n");
3438 svoboda 368
    udebug_wait_for_go(&THREAD->udebug.go_wq);
369
}
370
 
3457 svoboda 371
/** Thread-termination event hook.
372
 *
373
 * Must be called when the current thread is terminating.
374
 * Generates a THREAD_E event.
375
 */
3438 svoboda 376
void udebug_thread_e_event(void)
377
{
378
    call_t *call;
379
 
380
    mutex_lock(&TASK->udebug.lock);
381
    mutex_lock(&THREAD->udebug.lock);
382
 
3441 svoboda 383
    LOG("udebug_thread_e_event\n");
384
    LOG("- check state\n");
3438 svoboda 385
 
3602 rimsky 386
    /* Must only generate events when in debugging session. */
3742 rimsky 387
    if (THREAD->udebug.active != true) {
388
/*      printf("- udebug.active: %s, udebug.go: %s\n",
389
            THREAD->udebug.active ? "yes(+)" : "no(-)",
3602 rimsky 390
            THREAD->udebug.go ? "yes(-)" : "no(+)");*/
3438 svoboda 391
        mutex_unlock(&THREAD->udebug.lock);
392
        mutex_unlock(&TASK->udebug.lock);
393
        return;
394
    }
395
 
3441 svoboda 396
    LOG("- trigger event\n");
3438 svoboda 397
 
398
    call = THREAD->udebug.go_call;
399
    THREAD->udebug.go_call = NULL;
400
    IPC_SET_RETVAL(call->data, 0);
401
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_E);
402
 
3602 rimsky 403
    /* Prevent any further debug activity in thread. */
3742 rimsky 404
    THREAD->udebug.active = false;
3438 svoboda 405
    THREAD->udebug.cur_event = 0;       /* none */
3602 rimsky 406
    THREAD->udebug.go = false;  /* set to initial value */
3438 svoboda 407
 
408
    ipc_answer(&TASK->answerbox, call);
409
 
410
    mutex_unlock(&THREAD->udebug.lock);
411
    mutex_unlock(&TASK->udebug.lock);
412
 
3665 rimsky 413
    /*
414
     * This event does not sleep - debugging has finished
415
     * in this thread.
416
     */
3438 svoboda 417
}
418
 
419
/**
420
 * Terminate task debugging session.
421
 *
3457 svoboda 422
 * Gracefully terminates the debugging session for a task. If the debugger
423
 * is still waiting for events on some threads, it will receive a
424
 * FINISHED event for each of them.
425
 *
426
 * @param ta    Task structure. ta->udebug.lock must be already locked.
427
 * @return  Zero on success or negative error code.
3438 svoboda 428
 */
429
int udebug_task_cleanup(struct task *ta)
430
{
431
    thread_t *t;
432
    link_t *cur;
433
    int flags;
434
    ipl_t ipl;
435
 
3441 svoboda 436
    LOG("udebug_task_cleanup()\n");
437
    LOG("task %" PRIu64 "\n", ta->taskid);
3438 svoboda 438
 
439
    if (ta->udebug.dt_state != UDEBUG_TS_BEGINNING &&
440
        ta->udebug.dt_state != UDEBUG_TS_ACTIVE) {
3441 svoboda 441
        LOG("udebug_task_cleanup(): task not being debugged\n");
3438 svoboda 442
        return EINVAL;
443
    }
444
 
445
    /* Finish debugging of all userspace threads */
446
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
447
        t = list_get_instance(cur, thread_t, th_link);
448
 
449
        mutex_lock(&t->udebug.lock);
450
 
451
        ipl = interrupts_disable();
452
        spinlock_lock(&t->lock);
453
 
454
        flags = t->flags;
455
 
456
        spinlock_unlock(&t->lock);
457
        interrupts_restore(ipl);
458
 
3602 rimsky 459
        /* Only process userspace threads. */
3438 svoboda 460
        if ((flags & THREAD_FLAG_USPACE) != 0) {
3602 rimsky 461
            /* Prevent any further debug activity in thread. */
3742 rimsky 462
            t->udebug.active = false;
3438 svoboda 463
            t->udebug.cur_event = 0;    /* none */
464
 
3602 rimsky 465
            /* Is the thread still go? */
466
            if (t->udebug.go == true) {
3438 svoboda 467
                /*
3742 rimsky 468
                * Yes, so clear go. As active == false,
3438 svoboda 469
                 * this doesn't affect anything.
470
                 */
3602 rimsky 471
                t->udebug.go = false;  
3438 svoboda 472
 
473
                /* Answer GO call */
3441 svoboda 474
                LOG("answer GO call with EVENT_FINISHED\n");
3438 svoboda 475
                IPC_SET_RETVAL(t->udebug.go_call->data, 0);
3468 svoboda 476
                IPC_SET_ARG1(t->udebug.go_call->data,
477
                    UDEBUG_EVENT_FINISHED);
3438 svoboda 478
 
479
                ipc_answer(&ta->answerbox, t->udebug.go_call);
480
                t->udebug.go_call = NULL;
481
            } else {
482
                /*
483
                 * Debug_stop is already at initial value.
484
                 * Yet this means the thread needs waking up.
485
                 */
486
 
487
                /*
488
                 * t's lock must not be held when calling
489
                 * waitq_wakeup.
490
                 */
491
                waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST);
492
            }
493
        }
494
        mutex_unlock(&t->udebug.lock);
495
    }
496
 
497
    ta->udebug.dt_state = UDEBUG_TS_INACTIVE;
498
    ta->udebug.debugger = NULL;
499
 
500
    return 0;
501
}
502
 
503
 
504
/** @}
505
 */