Subversion Repositories HelenOS

Rev

Rev 2921 | Rev 3015 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2894 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
 
2801 svoboda 29
/** @addtogroup generic
30
 * @{
31
 */
32
 
33
/**
34
 * @file
2894 svoboda 35
 * @brief   Udebug.
2801 svoboda 36
 */
37
 
38
#include <synch/waitq.h>
39
#include <console/klog.h>
2813 svoboda 40
#include <udebug/udebug.h>
2870 svoboda 41
#include <errno.h>
2801 svoboda 42
#include <arch.h>
43
 
3014 svoboda 44
void udebug_task_init(udebug_task_t *ut)
45
{
46
    ut->dt_state = UDEBUG_TS_INACTIVE;
47
    ut->begin_call = NULL;
48
    ut->not_stoppable_count = 0;
49
    ut->evmask = 0;
50
}
51
 
2917 svoboda 52
static void udebug_wait_for_go(waitq_t *wq)
53
{
54
    int rc;
55
    ipl_t ipl;
56
 
57
    ipl = waitq_sleep_prepare(wq);
58
 
59
    wq->missed_wakeups = 0; /* Enforce blocking. */
60
    rc = waitq_sleep_timeout_unsafe(wq, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
61
 
62
    waitq_sleep_finish(wq, rc, ipl);
63
}
64
 
2804 svoboda 65
void udebug_stoppable_begin(void)
2801 svoboda 66
{
2804 svoboda 67
    int nsc;
2898 svoboda 68
    call_t *db_call, *go_call;
2823 svoboda 69
    ipl_t ipl;
2804 svoboda 70
 
2902 svoboda 71
    ASSERT(THREAD);
72
    ASSERT(TASK);
73
 
2823 svoboda 74
    ipl = interrupts_disable();
2804 svoboda 75
    spinlock_lock(&TASK->lock);
76
 
3014 svoboda 77
    nsc = --TASK->udebug.not_stoppable_count;
2804 svoboda 78
 
3014 svoboda 79
    if (TASK->udebug.dt_state == UDEBUG_TS_BEGINNING) {
2804 svoboda 80
        klog_printf("udebug_stoppable_begin");
81
        klog_printf(" - nsc := %d", nsc);
82
    }
83
 
3014 svoboda 84
    if (TASK->udebug.dt_state == UDEBUG_TS_BEGINNING && nsc == 0) {
2898 svoboda 85
        /*
86
         * This was the last non-stoppable thread. Reply to
87
         * DEBUG_BEGIN call.
88
         */
89
 
3014 svoboda 90
        db_call = TASK->udebug.begin_call;
2902 svoboda 91
        ASSERT(db_call);
92
 
2898 svoboda 93
        /* Lock order OK, THREAD->debug_lock is after TASK->lock */
94
        spinlock_lock(&THREAD->debug_lock);
95
        THREAD->debug_stoppable = true;
96
        spinlock_unlock(&THREAD->debug_lock);
97
 
3014 svoboda 98
        TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
99
        TASK->udebug.begin_call = NULL;
2804 svoboda 100
        spinlock_unlock(&TASK->lock);
2823 svoboda 101
        interrupts_restore(ipl);
2804 svoboda 102
 
103
        IPC_SET_RETVAL(db_call->data, 0);
2913 svoboda 104
        //klog_printf("udebug_stoppable_begin/ipc_answer");
2804 svoboda 105
        ipc_answer(&TASK->answerbox, db_call);     
2898 svoboda 106
 
3014 svoboda 107
    } else if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
2898 svoboda 108
        /*
109
         * Active debugging session
110
         */
111
 
112
        /* Lock order OK, THREAD->debug_lock is after TASK->lock */
113
        spinlock_lock(&THREAD->debug_lock);
114
        THREAD->debug_stoppable = true;
115
 
2902 svoboda 116
        if (THREAD->debug_active && THREAD->debug_stop) {
2898 svoboda 117
            /*
118
             * Thread was requested to stop - answer go call
119
             */
120
 
121
            /* Make sure nobody takes this call away from us */
122
            go_call = THREAD->debug_go_call;
123
            THREAD->debug_go_call = NULL;
2902 svoboda 124
            ASSERT(go_call);
2898 svoboda 125
 
126
            IPC_SET_RETVAL(go_call->data, 0);
127
            IPC_SET_ARG1(go_call->data, UDEBUG_EVENT_STOP);
128
 
129
            THREAD->cur_event = UDEBUG_EVENT_STOP;
130
            spinlock_unlock(&THREAD->debug_lock);
131
 
132
                ipc_answer(&TASK->answerbox, go_call);
133
 
134
                spinlock_unlock(&TASK->lock);
135
            interrupts_restore(ipl);
136
        } else {
137
            /*
138
             * No stop request - nothing happens.
139
             */
140
            spinlock_unlock(&THREAD->debug_lock);
141
                spinlock_unlock(&TASK->lock);
142
            interrupts_restore(ipl);
143
        }
2804 svoboda 144
    } else {
2898 svoboda 145
        /*
146
         * All other cases - nothing special happens.
147
         */
148
 
149
        /* Lock order OK, THREAD->debug_lock is after TASK->lock */
150
        spinlock_lock(&THREAD->debug_lock);
151
        THREAD->debug_stoppable = true;
152
        spinlock_unlock(&THREAD->debug_lock);
153
 
2804 svoboda 154
            spinlock_unlock(&TASK->lock);
2823 svoboda 155
        interrupts_restore(ipl);
2804 svoboda 156
    }
157
}
158
 
159
void udebug_stoppable_end(void)
160
{
2823 svoboda 161
    ipl_t ipl;
162
 
2804 svoboda 163
restart:
2823 svoboda 164
    ipl = interrupts_disable();
2804 svoboda 165
    spinlock_lock(&TASK->lock);
166
 
2898 svoboda 167
    /* Lock order OK, THREAD->debug_lock is after TASK->lock */
168
    spinlock_lock(&THREAD->debug_lock);
169
 
3014 svoboda 170
    if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
2913 svoboda 171
        //klog_printf("udebug_stoppable_end");
172
        //klog_printf("debug_stop=%d", THREAD->debug_stop);
2898 svoboda 173
    }
174
 
2902 svoboda 175
    if (THREAD->debug_active &&
2825 svoboda 176
        THREAD->debug_stop == true) {
3014 svoboda 177
        TASK->udebug.begin_call = NULL;
2898 svoboda 178
        spinlock_unlock(&THREAD->debug_lock);
2804 svoboda 179
        spinlock_unlock(&TASK->lock);
2823 svoboda 180
        interrupts_restore(ipl);
181
 
2917 svoboda 182
        udebug_wait_for_go(&THREAD->go_wq);
183
 
2804 svoboda 184
        goto restart;
185
        /* must try again - have to lose stoppability atomically */
186
    } else {
3014 svoboda 187
        ++TASK->udebug.not_stoppable_count;
2898 svoboda 188
        THREAD->debug_stoppable = false;
189
 
190
        spinlock_unlock(&THREAD->debug_lock);
2804 svoboda 191
        spinlock_unlock(&TASK->lock);
2823 svoboda 192
        interrupts_restore(ipl);
2801 svoboda 193
    }
194
}
195
 
2805 svoboda 196
void udebug_syscall_event(unative_t a1, unative_t a2, unative_t a3,
2901 svoboda 197
    unative_t a4, unative_t a5, unative_t a6, unative_t id, unative_t rc,
198
    bool end_variant)
2801 svoboda 199
{
2805 svoboda 200
    call_t *call;
2823 svoboda 201
    ipl_t ipl;
2901 svoboda 202
    udebug_event_t etype;
2805 svoboda 203
 
2901 svoboda 204
    etype = end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
205
 
2823 svoboda 206
    ipl = interrupts_disable();
2848 svoboda 207
    spinlock_lock(&THREAD->debug_lock);
2823 svoboda 208
 
2854 svoboda 209
    /* Must only generate events when in debugging session and have go */
2867 svoboda 210
    if (THREAD->debug_active != true ||
2899 svoboda 211
        THREAD->debug_stop == true ||
3014 svoboda 212
        (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
2867 svoboda 213
        spinlock_unlock(&THREAD->debug_lock);
214
        interrupts_restore(ipl);
215
        return;
216
    }
2804 svoboda 217
 
2913 svoboda 218
    //klog_printf("udebug_syscall_event");
2867 svoboda 219
    call = THREAD->debug_go_call;
220
    IPC_SET_RETVAL(call->data, 0);
2901 svoboda 221
    IPC_SET_ARG1(call->data, etype);
2867 svoboda 222
    IPC_SET_ARG2(call->data, id);
223
    IPC_SET_ARG3(call->data, rc);
2913 svoboda 224
    //klog_printf("udebug_syscall_event/ipc_answer");
2805 svoboda 225
 
2867 svoboda 226
    THREAD->syscall_args[0] = a1;
227
    THREAD->syscall_args[1] = a2;
228
    THREAD->syscall_args[2] = a3;
229
    THREAD->syscall_args[3] = a4;
230
    THREAD->syscall_args[4] = a5;
231
    THREAD->syscall_args[5] = a6;
2866 svoboda 232
 
2867 svoboda 233
    /*
234
     * Make sure debug_stop is true when going to sleep
235
     * in case we get woken up by DEBUG_END. (At which
236
     * point it must be back to the initial true value).
237
     */
238
    THREAD->debug_stop = true;
2825 svoboda 239
 
2901 svoboda 240
    THREAD->cur_event = etype;
2867 svoboda 241
    spinlock_unlock(&THREAD->debug_lock);
2834 svoboda 242
 
2867 svoboda 243
    spinlock_lock(&TASK->lock);
244
    ipc_answer(&TASK->answerbox, THREAD->debug_go_call);
245
    spinlock_unlock(&TASK->lock);
246
    interrupts_restore(ipl);
247
 
2917 svoboda 248
    udebug_wait_for_go(&THREAD->go_wq);
2867 svoboda 249
}
250
 
2903 svoboda 251
void udebug_thread_b_event(struct thread *t)
2867 svoboda 252
{
253
    call_t *call;
254
    ipl_t ipl;
255
 
256
    ipl = interrupts_disable();
257
    spinlock_lock(&THREAD->debug_lock);
258
 
2903 svoboda 259
    klog_printf("udebug_thread_b_event");
2867 svoboda 260
    klog_printf("- check state");
261
 
262
    /* Must only generate events when in debugging session */
263
    if (THREAD->debug_active != true) {
264
        klog_printf("- debug_active: %s, debug_stop: %s",
265
            THREAD->debug_active ? "yes(+)" : "no(-)",
266
            THREAD->debug_stop ? "yes(-)" : "no(+)");
2848 svoboda 267
        spinlock_unlock(&THREAD->debug_lock);
2823 svoboda 268
        interrupts_restore(ipl);
2867 svoboda 269
        return;
2801 svoboda 270
    }
2867 svoboda 271
 
272
    klog_printf("- trigger event");
273
 
274
    call = THREAD->debug_go_call;
275
    IPC_SET_RETVAL(call->data, 0);
2903 svoboda 276
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_B);
2867 svoboda 277
    IPC_SET_ARG2(call->data, (unative_t)t);
278
 
279
    /*
280
     * Make sure debug_stop is true when going to sleep
281
     * in case we get woken up by DEBUG_END. (At which
282
     * point it must be back to the initial true value).
283
     */
284
    THREAD->debug_stop = true;
285
 
2903 svoboda 286
    THREAD->cur_event = UDEBUG_EVENT_THREAD_B;
2867 svoboda 287
    spinlock_unlock(&THREAD->debug_lock);
288
 
289
    spinlock_lock(&TASK->lock);
290
    ipc_answer(&TASK->answerbox, THREAD->debug_go_call);
291
    spinlock_unlock(&TASK->lock);
292
 
293
    interrupts_restore(ipl);
294
    klog_printf("- sleep");
2917 svoboda 295
    udebug_wait_for_go(&THREAD->go_wq);
2801 svoboda 296
}
297
 
2903 svoboda 298
void udebug_thread_e_event(void)
299
{
300
    call_t *call;
301
    ipl_t ipl;
302
 
303
    ipl = interrupts_disable();
304
    spinlock_lock(&THREAD->debug_lock);
305
 
306
    klog_printf("udebug_thread_e_event");
307
    klog_printf("- check state");
308
 
309
    /* Must only generate events when in debugging session */
310
    if (THREAD->debug_active != true) {
311
        klog_printf("- debug_active: %s, debug_stop: %s",
312
            THREAD->debug_active ? "yes(+)" : "no(-)",
313
            THREAD->debug_stop ? "yes(-)" : "no(+)");
314
        spinlock_unlock(&THREAD->debug_lock);
315
        interrupts_restore(ipl);
316
        return;
317
    }
318
 
319
    klog_printf("- trigger event");
320
 
321
    call = THREAD->debug_go_call;
322
    IPC_SET_RETVAL(call->data, 0);
323
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_E);
324
 
2908 svoboda 325
    /* Prevent any further debug activity in thread */
326
    THREAD->debug_active = false;
327
    THREAD->cur_event = 0;      /* none */
328
    THREAD->debug_stop = true;  /* set to initial value */
2903 svoboda 329
    spinlock_unlock(&THREAD->debug_lock);
330
 
331
    spinlock_lock(&TASK->lock);
332
    ipc_answer(&TASK->answerbox, THREAD->debug_go_call);
333
    spinlock_unlock(&TASK->lock);
334
 
335
    interrupts_restore(ipl);
336
 
2908 svoboda 337
    /* This event does not sleep - debugging has finished in this thread */
2903 svoboda 338
}
339
 
2921 svoboda 340
static void breakpoint_trap_event(uintptr_t addr, udebug_event_t etype)
2918 svoboda 341
{
342
    call_t *call;
343
    ipl_t ipl;
2903 svoboda 344
 
2918 svoboda 345
    ipl = interrupts_disable();
346
    spinlock_lock(&THREAD->debug_lock);
347
 
348
    /* Must only generate events when in debugging session and have go */
349
    if (THREAD->debug_active != true ||
350
        THREAD->debug_stop == true ||
3014 svoboda 351
        (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
2918 svoboda 352
        spinlock_unlock(&THREAD->debug_lock);
353
        interrupts_restore(ipl);
354
        return;
355
    }
356
 
2921 svoboda 357
    klog_printf("udebug_breakpoint/trap_event");
2918 svoboda 358
    call = THREAD->debug_go_call;
359
    IPC_SET_RETVAL(call->data, 0);
360
    IPC_SET_ARG1(call->data, etype);
361
    IPC_SET_ARG2(call->data, addr);
362
 
363
    /*
364
     * Make sure debug_stop is true when going to sleep
365
     * in case we get woken up by DEBUG_END. (At which
366
     * point it must be back to the initial true value).
367
     */
368
    THREAD->debug_stop = true;
369
 
370
    THREAD->cur_event = etype;
371
    spinlock_unlock(&THREAD->debug_lock);
372
    klog_printf("- send answer");
373
 
374
    spinlock_lock(&TASK->lock);
375
    ipc_answer(&TASK->answerbox, THREAD->debug_go_call);
376
    spinlock_unlock(&TASK->lock);
377
    interrupts_restore(ipl);
378
 
379
    udebug_wait_for_go(&THREAD->go_wq);
380
}
381
 
2921 svoboda 382
void udebug_breakpoint_event(uintptr_t addr)
383
{
384
    breakpoint_trap_event(addr, UDEBUG_EVENT_BREAKPOINT);
385
}
2918 svoboda 386
 
2921 svoboda 387
void udebug_trap_event(uintptr_t addr)
388
{
389
    breakpoint_trap_event(addr, UDEBUG_EVENT_TRAP);
390
}
391
 
2870 svoboda 392
/**
393
 * Terminate task debugging session.
394
 *
395
 * \param ta Must be already locked and interrupts must be disabled.
396
 * \return Zero on success or negative error code.
397
 */
398
int udebug_task_cleanup(struct task *ta)
399
{
400
    thread_t *t;
401
    link_t *cur;
402
    int flags;
2867 svoboda 403
 
2870 svoboda 404
    klog_printf("udebug_task_cleanup()");
405
    klog_printf("task %llu", ta->taskid);
406
 
3014 svoboda 407
    if (ta->udebug.dt_state == UDEBUG_TS_BEGINNING &&
408
        ta->udebug.dt_state != UDEBUG_TS_ACTIVE) {
2870 svoboda 409
        klog_printf("udebug_task_cleanup(): task not being debugged");
410
        return EINVAL;
411
    }
412
 
413
    /* Finish debugging of all userspace threads */
414
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
415
        t = list_get_instance(cur, thread_t, th_link);
416
 
417
        spinlock_lock(&t->debug_lock);
418
        spinlock_lock(&t->lock);
419
 
420
        flags = t->flags;
421
 
422
        spinlock_unlock(&t->lock);
423
 
424
        /* Only process userspace threads */
425
        if ((flags & THREAD_FLAG_USPACE) != 0) {
426
            /* Prevent any further debug activity in thread */
427
            t->debug_active = false;
428
            t->cur_event = 0;   /* none */
429
 
430
            /* Still has go? */
431
            if (t->debug_stop == false) {
432
                /*
433
                * Yes, so clear go. As debug_active == false,
434
                 * this doesn't affect anything.
435
                 */
436
                t->debug_stop = true;  
437
 
438
                /* Answer GO call */
439
                klog_printf("answer GO call with EVENT_FINISHED");
440
                IPC_SET_RETVAL(t->debug_go_call->data, 0);
441
                IPC_SET_ARG1(t->debug_go_call->data, UDEBUG_EVENT_FINISHED);
442
                ipc_answer(&ta->answerbox, t->debug_go_call);
443
            } else {
444
                /*
445
                 * Debug_stop is already at initial value.
446
                 * Yet this means the thread needs waking up.
447
                 */
448
 
449
                /*
450
                 * t's lock must not be held when calling
451
                 * waitq_wakeup.
452
                 */
453
                waitq_wakeup(&t->go_wq, WAKEUP_FIRST);
454
            }
455
        }
456
        spinlock_unlock(&t->debug_lock);
457
    }
458
 
3014 svoboda 459
    ta->udebug.dt_state = UDEBUG_TS_INACTIVE;
460
    ta->udebug.debugger = NULL;
2870 svoboda 461
 
462
    return 0;
463
}
464
 
465
 
2801 svoboda 466
/** @}
467
 */