Subversion Repositories HelenOS

Rev

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