Subversion Repositories HelenOS

Rev

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