Subversion Repositories HelenOS

Rev

Rev 3601 | Rev 3613 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3601 Rev 3604
1
/*
1
/*
2
 * Copyright (c) 2008 Jiri Svoboda
2
 * Copyright (c) 2008 Jiri Svoboda
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup generic
29
/** @addtogroup generic
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
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.
72
 */
51
 */
73
void udebug_task_init(udebug_task_t *ut)
52
void udebug_task_init(udebug_task_t *ut)
74
{
53
{
75
    mutex_initialize(&ut->lock, MUTEX_PASSIVE);
54
    mutex_initialize(&ut->lock, MUTEX_PASSIVE);
76
    ut->dt_state = UDEBUG_TS_INACTIVE;
55
    ut->dt_state = UDEBUG_TS_INACTIVE;
77
    ut->begin_call = NULL;
56
    ut->begin_call = NULL;
78
    ut->not_stoppable_count = 0;
57
    ut->not_stoppable_count = 0;
79
    ut->evmask = 0;
58
    ut->evmask = 0;
80
}
59
}
81
 
60
 
82
/** Initialize udebug part of thread structure.
61
/** Initialize udebug part of thread structure.
83
 *
62
 *
84
 * Called as part of thread structure initialization.
63
 * Called as part of thread structure initialization.
85
 * @param ut    Pointer to the structure to initialize.
64
 * @param ut    Pointer to the structure to initialize.
86
 */
65
 */
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;
98
    ut->go = false;
72
    ut->go = false;
99
    ut->stoppable = true;
73
    ut->stoppable = true;
100
    ut->debug_active = false;
74
    ut->debug_active = false;
101
    ut->cur_event = 0; /* none */
75
    ut->cur_event = 0; /* none */
102
}
76
}
103
 
77
 
104
/** Wait for a GO message.
78
/** Wait for a GO message.
105
 *
79
 *
106
 * When a debugging event occurs in a thread or the thread is stopped,
80
 * When a debugging event occurs in a thread or the thread is stopped,
107
 * this function is called to block the thread until a GO message
81
 * this function is called to block the thread until a GO message
108
 * is received.
82
 * is received.
109
 *
83
 *
110
 * @param wq    The wait queue used by the thread to wait for GO messages.
84
 * @param wq    The wait queue used by the thread to wait for GO messages.
111
 */
85
 */
112
static void udebug_wait_for_go(waitq_t *wq)
86
static void udebug_wait_for_go(waitq_t *wq)
113
{
87
{
114
    int rc;
88
    int rc;
115
    ipl_t ipl;
89
    ipl_t ipl;
116
 
90
 
117
    ipl = waitq_sleep_prepare(wq);
91
    ipl = waitq_sleep_prepare(wq);
118
 
92
 
119
    wq->missed_wakeups = 0; /* Enforce blocking. */
93
    wq->missed_wakeups = 0; /* Enforce blocking. */
120
    rc = waitq_sleep_timeout_unsafe(wq, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
94
    rc = waitq_sleep_timeout_unsafe(wq, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
121
 
95
 
122
    waitq_sleep_finish(wq, rc, ipl);
96
    waitq_sleep_finish(wq, rc, ipl);
123
}
97
}
124
 
98
 
125
/** Do a preliminary check that a debugging session is in progress.
99
/** Do a preliminary check that a debugging session is in progress.
126
 *
100
 *
127
 * This only requires the THREAD->udebug.lock mutex (and not TASK->udebug.lock
101
 * This only requires the THREAD->udebug.lock mutex (and not TASK->udebug.lock
128
 * mutex). For an undebugged task, this will never block (while there could be
102
 * mutex). For an undebugged task, this will never block (while there could be
129
 * collisions by different threads on the TASK mutex), thus improving SMP
103
 * collisions by different threads on the TASK mutex), thus improving SMP
130
 * perormance for undebugged tasks.
104
 * perormance for undebugged tasks.
131
 *
105
 *
132
 * @return  True if the thread was in a debugging session when the function
106
 * @return  True if the thread was in a debugging session when the function
133
 *      checked, false otherwise.
107
 *      checked, false otherwise.
134
 */
108
 */
135
static bool udebug_thread_precheck(void)
109
static bool udebug_thread_precheck(void)
136
{
110
{
137
    bool res;
111
    bool res;
138
 
112
 
139
    mutex_lock(&THREAD->udebug.lock);
113
    mutex_lock(&THREAD->udebug.lock);
140
    res = THREAD->udebug.debug_active;
114
    res = THREAD->udebug.debug_active;
141
    mutex_unlock(&THREAD->udebug.lock);
115
    mutex_unlock(&THREAD->udebug.lock);
142
 
116
 
143
    return res;
117
    return res;
144
}
118
}
145
 
119
 
146
/** Start of stoppable section.
120
/** Start of stoppable section.
147
 *
121
 *
148
 * A stoppable section is a section of code where if the thread can be stoped. In other words,
122
 * A stoppable section is a section of code where if the thread can be stoped. In other words,
149
 * if a STOP operation is issued, the thread is guaranteed not to execute
123
 * if a STOP operation is issued, the thread is guaranteed not to execute
150
 * any userspace instructions until the thread is resumed.
124
 * any userspace instructions until the thread is resumed.
151
 *
125
 *
152
 * Having stoppable sections is better than having stopping points, since
126
 * Having stoppable sections is better than having stopping points, since
153
 * a thread can be stopped even when it is blocked indefinitely in a system
127
 * a thread can be stopped even when it is blocked indefinitely in a system
154
 * call (whereas it would not reach any stopping point).
128
 * call (whereas it would not reach any stopping point).
155
 */
129
 */
156
void udebug_stoppable_begin(void)
130
void udebug_stoppable_begin(void)
157
{
131
{
158
    int nsc;
132
    int nsc;
159
    call_t *db_call, *go_call;
133
    call_t *db_call, *go_call;
160
 
134
 
161
    ASSERT(THREAD);
135
    ASSERT(THREAD);
162
    ASSERT(TASK);
136
    ASSERT(TASK);
163
 
137
 
164
    udebug_int_lock();
-
 
165
 
-
 
166
    /* Early check for undebugged tasks */
138
    /* Early check for undebugged tasks */
167
    if (!udebug_thread_precheck()) {
139
    if (!udebug_thread_precheck()) {
168
        udebug_int_unlock();
-
 
169
        return;
140
        return;
170
    }
141
    }
171
 
142
 
172
    mutex_lock(&TASK->udebug.lock);
143
    mutex_lock(&TASK->udebug.lock);
173
 
144
 
174
    nsc = --TASK->udebug.not_stoppable_count;
145
    nsc = --TASK->udebug.not_stoppable_count;
175
 
146
 
176
    /* Lock order OK, THREAD->udebug.lock is after TASK->udebug.lock */
147
    /* Lock order OK, THREAD->udebug.lock is after TASK->udebug.lock */
177
    mutex_lock(&THREAD->udebug.lock);
148
    mutex_lock(&THREAD->udebug.lock);
178
    ASSERT(THREAD->udebug.stoppable == false);
149
    ASSERT(THREAD->udebug.stoppable == false);
179
    THREAD->udebug.stoppable = true;
150
    THREAD->udebug.stoppable = true;
180
 
151
 
181
    if (TASK->udebug.dt_state == UDEBUG_TS_BEGINNING && nsc == 0) {
152
    if (TASK->udebug.dt_state == UDEBUG_TS_BEGINNING && nsc == 0) {
182
        /*
153
        /*
183
         * This was the last non-stoppable thread. Reply to
154
         * This was the last non-stoppable thread. Reply to
184
         * DEBUG_BEGIN call.
155
         * DEBUG_BEGIN call.
185
         */
156
         */
186
 
157
 
187
        db_call = TASK->udebug.begin_call;
158
        db_call = TASK->udebug.begin_call;
188
        ASSERT(db_call);
159
        ASSERT(db_call);
189
 
160
 
190
        TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
161
        TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
191
        TASK->udebug.begin_call = NULL;
162
        TASK->udebug.begin_call = NULL;
192
 
163
 
193
        IPC_SET_RETVAL(db_call->data, 0);
164
        IPC_SET_RETVAL(db_call->data, 0);
194
        ipc_answer(&TASK->answerbox, db_call);     
165
        ipc_answer(&TASK->answerbox, db_call);     
195
 
166
 
196
    } else if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
167
    } else if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
197
        /*
168
        /*
198
         * Active debugging session
169
         * Active debugging session
199
         */
170
         */
200
 
171
 
201
        if (THREAD->udebug.debug_active == true &&
172
        if (THREAD->udebug.debug_active == true &&
202
            THREAD->udebug.go == false) {
173
            THREAD->udebug.go == false) {
203
            /*
174
            /*
204
             * Thread was requested to stop - answer go call
175
             * Thread was requested to stop - answer go call
205
             */
176
             */
206
 
177
 
207
            /* Make sure nobody takes this call away from us */
178
            /* Make sure nobody takes this call away from us */
208
            go_call = THREAD->udebug.go_call;
179
            go_call = THREAD->udebug.go_call;
209
            THREAD->udebug.go_call = NULL;
180
            THREAD->udebug.go_call = NULL;
210
            ASSERT(go_call);
181
            ASSERT(go_call);
211
 
182
 
212
            IPC_SET_RETVAL(go_call->data, 0);
183
            IPC_SET_RETVAL(go_call->data, 0);
213
            IPC_SET_ARG1(go_call->data, UDEBUG_EVENT_STOP);
184
            IPC_SET_ARG1(go_call->data, UDEBUG_EVENT_STOP);
214
 
185
 
215
            THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
186
            THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
216
 
187
 
217
                ipc_answer(&TASK->answerbox, go_call);
188
                ipc_answer(&TASK->answerbox, go_call);
218
        }
189
        }
219
    }
190
    }
220
 
191
 
221
    mutex_unlock(&THREAD->udebug.lock);
192
    mutex_unlock(&THREAD->udebug.lock);
222
        mutex_unlock(&TASK->udebug.lock);
193
        mutex_unlock(&TASK->udebug.lock);
223
}
194
}
224
 
195
 
225
/** End of a stoppable section.
196
/** End of a stoppable section.
226
 *
197
 *
227
 * This is the point where the thread will block if it is stopped.
198
 * This is the point where the thread will block if it is stopped.
228
 * (As, by definition, a stopped thread must not leave its stoppable section).
199
 * (As, by definition, a stopped thread must not leave its stoppable section).
229
 */
200
 */
230
void udebug_stoppable_end(void)
201
void udebug_stoppable_end(void)
231
{
202
{
232
    /* Early check for undebugged tasks */
203
    /* Early check for undebugged tasks */
233
    if (!udebug_thread_precheck()) {
204
    if (!udebug_thread_precheck()) {
234
        udebug_int_unlock();
-
 
235
        return;
205
        return;
236
    }
206
    }
237
 
207
 
238
restart:
208
restart:
239
    mutex_lock(&TASK->udebug.lock);
209
    mutex_lock(&TASK->udebug.lock);
240
    mutex_lock(&THREAD->udebug.lock);
210
    mutex_lock(&THREAD->udebug.lock);
241
 
211
 
242
    if (THREAD->udebug.debug_active &&
212
    if (THREAD->udebug.debug_active &&
243
        THREAD->udebug.go == false) {
213
        THREAD->udebug.go == false) {
244
        TASK->udebug.begin_call = NULL;
214
        TASK->udebug.begin_call = NULL;
245
        mutex_unlock(&THREAD->udebug.lock);
215
        mutex_unlock(&THREAD->udebug.lock);
246
        mutex_unlock(&TASK->udebug.lock);
216
        mutex_unlock(&TASK->udebug.lock);
247
 
217
 
248
        udebug_wait_for_go(&THREAD->udebug.go_wq);
218
        udebug_wait_for_go(&THREAD->udebug.go_wq);
249
 
219
 
250
        goto restart;
220
        goto restart;
251
        /* Must try again - have to lose stoppability atomically. */
221
        /* Must try again - have to lose stoppability atomically. */
252
    } else {
222
    } else {
253
        ++TASK->udebug.not_stoppable_count;
223
        ++TASK->udebug.not_stoppable_count;
254
        ASSERT(THREAD->udebug.stoppable == true);
224
        ASSERT(THREAD->udebug.stoppable == true);
255
        THREAD->udebug.stoppable = false;
225
        THREAD->udebug.stoppable = false;
256
 
226
 
257
        mutex_unlock(&THREAD->udebug.lock);
227
        mutex_unlock(&THREAD->udebug.lock);
258
        mutex_unlock(&TASK->udebug.lock);
228
        mutex_unlock(&TASK->udebug.lock);
259
    }
229
    }
260
 
-
 
261
    udebug_int_unlock();
-
 
262
}
230
}
263
 
231
 
264
/** Upon being scheduled to run, check if the current thread should stop.
232
/** Upon being scheduled to run, check if the current thread should stop.
265
 *
233
 *
266
 * This function is called from clock(). Preemption is enabled.
234
 * This function is called from clock(). Preemption is enabled.
267
 * interrupts are disabled, but since this is called after
235
 * interrupts are disabled, but since this is called after
268
 * being scheduled-in, we can enable them, if we're careful enough
236
 * being scheduled-in, we can enable them, if we're careful enough
269
 * not to allow arbitrary recursion or deadlock with the thread context.
237
 * not to allow arbitrary recursion or deadlock with the thread context.
270
 */
238
 */
271
void udebug_before_thread_runs(void)
239
void udebug_before_thread_runs(void)
272
{
240
{
273
    ipl_t ipl;
241
    ipl_t ipl;
274
 
242
 
275
    return;
243
    return;
276
    ASSERT(!PREEMPTION_DISABLED);
-
 
277
 
-
 
278
    /*
-
 
279
     * Prevent agains re-entering, such as when preempted inside this
-
 
280
     * function.
-
 
281
     */
-
 
282
    if (atomic_get(&THREAD->udebug.int_lock) != 0)
-
 
283
        return;
-
 
284
 
-
 
285
    udebug_int_lock();
-
 
286
 
244
 
287
    ipl = interrupts_enable();
245
    ipl = interrupts_enable();
288
 
246
 
289
    /* Now we're free to do whatever we need (lock mutexes, sleep, etc.) */
247
    /* Now we're free to do whatever we need (lock mutexes, sleep, etc.) */
290
 
248
 
291
    /* Check if we're supposed to stop */
249
    /* Check if we're supposed to stop */
292
    udebug_stoppable_begin();
250
    udebug_stoppable_begin();
293
    udebug_stoppable_end();
251
    udebug_stoppable_end();
294
 
252
 
295
    interrupts_restore(ipl);
253
    interrupts_restore(ipl);
296
 
-
 
297
    udebug_int_unlock();
-
 
298
}
254
}
299
 
255
 
300
/** Syscall event hook.
256
/** Syscall event hook.
301
 *
257
 *
302
 * Must be called before and after servicing a system call. This generates
258
 * Must be called before and after servicing a system call. This generates
303
 * a SYSCALL_B or SYSCALL_E event, depending on the value of @a end_variant.
259
 * a SYSCALL_B or SYSCALL_E event, depending on the value of @a end_variant.
304
 */
260
 */
305
void udebug_syscall_event(unative_t a1, unative_t a2, unative_t a3,
261
void udebug_syscall_event(unative_t a1, unative_t a2, unative_t a3,
306
    unative_t a4, unative_t a5, unative_t a6, unative_t id, unative_t rc,
262
    unative_t a4, unative_t a5, unative_t a6, unative_t id, unative_t rc,
307
    bool end_variant)
263
    bool end_variant)
308
{
264
{
309
    call_t *call;
265
    call_t *call;
310
    udebug_event_t etype;
266
    udebug_event_t etype;
311
 
267
 
312
    etype = end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
268
    etype = end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
313
 
269
 
314
    udebug_int_lock();
-
 
315
 
-
 
316
    /* Early check for undebugged tasks */
270
    /* Early check for undebugged tasks */
317
    if (!udebug_thread_precheck()) {
271
    if (!udebug_thread_precheck()) {
318
        udebug_int_unlock();
-
 
319
        return;
272
        return;
320
    }
273
    }
321
 
274
 
322
    mutex_lock(&TASK->udebug.lock);
275
    mutex_lock(&TASK->udebug.lock);
323
    mutex_lock(&THREAD->udebug.lock);
276
    mutex_lock(&THREAD->udebug.lock);
324
 
277
 
325
    /* Must only generate events when in debugging session and is go. */
278
    /* Must only generate events when in debugging session and is go. */
326
    if (THREAD->udebug.debug_active != true ||
279
    if (THREAD->udebug.debug_active != true ||
327
        THREAD->udebug.go == false ||
280
        THREAD->udebug.go == false ||
328
        (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
281
        (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
329
        mutex_unlock(&THREAD->udebug.lock);
282
        mutex_unlock(&THREAD->udebug.lock);
330
        mutex_unlock(&TASK->udebug.lock);
283
        mutex_unlock(&TASK->udebug.lock);
331
        return;
284
        return;
332
    }
285
    }
333
 
286
 
334
    //printf("udebug_syscall_event\n");
287
    //printf("udebug_syscall_event\n");
335
    call = THREAD->udebug.go_call;
288
    call = THREAD->udebug.go_call;
336
    THREAD->udebug.go_call = NULL;
289
    THREAD->udebug.go_call = NULL;
337
 
290
 
338
    IPC_SET_RETVAL(call->data, 0);
291
    IPC_SET_RETVAL(call->data, 0);
339
    IPC_SET_ARG1(call->data, etype);
292
    IPC_SET_ARG1(call->data, etype);
340
    IPC_SET_ARG2(call->data, id);
293
    IPC_SET_ARG2(call->data, id);
341
    IPC_SET_ARG3(call->data, rc);
294
    IPC_SET_ARG3(call->data, rc);
342
    //printf("udebug_syscall_event/ipc_answer\n");
295
    //printf("udebug_syscall_event/ipc_answer\n");
343
 
296
 
344
    THREAD->udebug.syscall_args[0] = a1;
297
    THREAD->udebug.syscall_args[0] = a1;
345
    THREAD->udebug.syscall_args[1] = a2;
298
    THREAD->udebug.syscall_args[1] = a2;
346
    THREAD->udebug.syscall_args[2] = a3;
299
    THREAD->udebug.syscall_args[2] = a3;
347
    THREAD->udebug.syscall_args[3] = a4;
300
    THREAD->udebug.syscall_args[3] = a4;
348
    THREAD->udebug.syscall_args[4] = a5;
301
    THREAD->udebug.syscall_args[4] = a5;
349
    THREAD->udebug.syscall_args[5] = a6;
302
    THREAD->udebug.syscall_args[5] = a6;
350
 
303
 
351
    /*
304
    /*
352
     * Make sure udebug.go is false when going to sleep
305
     * Make sure udebug.go is false when going to sleep
353
     * in case we get woken up by DEBUG_END. (At which
306
     * in case we get woken up by DEBUG_END. (At which
354
     * point it must be back to the initial true value).
307
     * point it must be back to the initial true value).
355
     */
308
     */
356
    THREAD->udebug.go = false;
309
    THREAD->udebug.go = false;
357
    THREAD->udebug.cur_event = etype;
310
    THREAD->udebug.cur_event = etype;
358
 
311
 
359
    ipc_answer(&TASK->answerbox, call);
312
    ipc_answer(&TASK->answerbox, call);
360
 
313
 
361
    mutex_unlock(&THREAD->udebug.lock);
314
    mutex_unlock(&THREAD->udebug.lock);
362
    mutex_unlock(&TASK->udebug.lock);
315
    mutex_unlock(&TASK->udebug.lock);
363
 
316
 
364
    udebug_wait_for_go(&THREAD->udebug.go_wq);
317
    udebug_wait_for_go(&THREAD->udebug.go_wq);
365
 
-
 
366
    udebug_int_unlock();
-
 
367
}
318
}
368
 
319
 
369
/** Thread-creation event hook.
320
/** Thread-creation event hook.
370
 *
321
 *
371
 * Must be called when a new userspace thread is created in the debugged
322
 * Must be called when a new userspace thread is created in the debugged
372
 * task. Generates a THREAD_B event.
323
 * task. Generates a THREAD_B event.
373
 *
324
 *
374
 * @param t Structure of the thread being created. Not locked, as the
325
 * @param t Structure of the thread being created. Not locked, as the
375
 *      thread is not executing yet.
326
 *      thread is not executing yet.
376
 */
327
 */
377
void udebug_thread_b_event(struct thread *t)
328
void udebug_thread_b_event(struct thread *t)
378
{
329
{
379
    call_t *call;
330
    call_t *call;
380
 
331
 
381
    udebug_int_lock();
-
 
382
 
-
 
383
    mutex_lock(&TASK->udebug.lock);
332
    mutex_lock(&TASK->udebug.lock);
384
    mutex_lock(&THREAD->udebug.lock);
333
    mutex_lock(&THREAD->udebug.lock);
385
 
334
 
386
    LOG("udebug_thread_b_event\n");
335
    LOG("udebug_thread_b_event\n");
387
    LOG("- check state\n");
336
    LOG("- check state\n");
388
 
337
 
389
    /* Must only generate events when in debugging session */
338
    /* Must only generate events when in debugging session */
390
    if (THREAD->udebug.debug_active != true) {
339
    if (THREAD->udebug.debug_active != true) {
391
        LOG("- debug_active: %s, udebug.go: %s\n",
340
        LOG("- debug_active: %s, udebug.go: %s\n",
392
            THREAD->udebug.debug_active ? "yes(+)" : "no(-)",
341
            THREAD->udebug.debug_active ? "yes(+)" : "no(-)",
393
            THREAD->udebug.go ? "yes(-)" : "no(+)");
342
            THREAD->udebug.go ? "yes(-)" : "no(+)");
394
        mutex_unlock(&THREAD->udebug.lock);
343
        mutex_unlock(&THREAD->udebug.lock);
395
        mutex_unlock(&TASK->udebug.lock);
344
        mutex_unlock(&TASK->udebug.lock);
396
        return;
345
        return;
397
    }
346
    }
398
 
347
 
399
    LOG("- trigger event\n");
348
    LOG("- trigger event\n");
400
 
349
 
401
    call = THREAD->udebug.go_call;
350
    call = THREAD->udebug.go_call;
402
    THREAD->udebug.go_call = NULL;
351
    THREAD->udebug.go_call = NULL;
403
    IPC_SET_RETVAL(call->data, 0);
352
    IPC_SET_RETVAL(call->data, 0);
404
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_B);
353
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_B);
405
    IPC_SET_ARG2(call->data, (unative_t)t);
354
    IPC_SET_ARG2(call->data, (unative_t)t);
406
 
355
 
407
    /*
356
    /*
408
     * Make sure udebug.go is false when going to sleep
357
     * Make sure udebug.go is false when going to sleep
409
     * in case we get woken up by DEBUG_END. (At which
358
     * in case we get woken up by DEBUG_END. (At which
410
     * point it must be back to the initial true value).
359
     * point it must be back to the initial true value).
411
     */
360
     */
412
    THREAD->udebug.go = false;
361
    THREAD->udebug.go = false;
413
    THREAD->udebug.cur_event = UDEBUG_EVENT_THREAD_B;
362
    THREAD->udebug.cur_event = UDEBUG_EVENT_THREAD_B;
414
 
363
 
415
    ipc_answer(&TASK->answerbox, call);
364
    ipc_answer(&TASK->answerbox, call);
416
 
365
 
417
    mutex_unlock(&THREAD->udebug.lock);
366
    mutex_unlock(&THREAD->udebug.lock);
418
    mutex_unlock(&TASK->udebug.lock);
367
    mutex_unlock(&TASK->udebug.lock);
419
 
368
 
420
    LOG("- sleep\n");
369
    LOG("- sleep\n");
421
    udebug_wait_for_go(&THREAD->udebug.go_wq);
370
    udebug_wait_for_go(&THREAD->udebug.go_wq);
422
 
-
 
423
    udebug_int_unlock();
-
 
424
}
371
}
425
 
372
 
426
/** Thread-termination event hook.
373
/** Thread-termination event hook.
427
 *
374
 *
428
 * Must be called when the current thread is terminating.
375
 * Must be called when the current thread is terminating.
429
 * Generates a THREAD_E event.
376
 * Generates a THREAD_E event.
430
 */
377
 */
431
void udebug_thread_e_event(void)
378
void udebug_thread_e_event(void)
432
{
379
{
433
    call_t *call;
380
    call_t *call;
434
 
381
 
435
    udebug_int_lock();
-
 
436
 
-
 
437
    mutex_lock(&TASK->udebug.lock);
382
    mutex_lock(&TASK->udebug.lock);
438
    mutex_lock(&THREAD->udebug.lock);
383
    mutex_lock(&THREAD->udebug.lock);
439
 
384
 
440
    LOG("udebug_thread_e_event\n");
385
    LOG("udebug_thread_e_event\n");
441
    LOG("- check state\n");
386
    LOG("- check state\n");
442
 
387
 
443
    /* Must only generate events when in debugging session. */
388
    /* Must only generate events when in debugging session. */
444
    if (THREAD->udebug.debug_active != true) {
389
    if (THREAD->udebug.debug_active != true) {
445
/*      printf("- debug_active: %s, udebug.go: %s\n",
390
/*      printf("- debug_active: %s, udebug.go: %s\n",
446
            THREAD->udebug.debug_active ? "yes(+)" : "no(-)",
391
            THREAD->udebug.debug_active ? "yes(+)" : "no(-)",
447
            THREAD->udebug.go ? "yes(-)" : "no(+)");*/
392
            THREAD->udebug.go ? "yes(-)" : "no(+)");*/
448
        mutex_unlock(&THREAD->udebug.lock);
393
        mutex_unlock(&THREAD->udebug.lock);
449
        mutex_unlock(&TASK->udebug.lock);
394
        mutex_unlock(&TASK->udebug.lock);
450
        return;
395
        return;
451
    }
396
    }
452
 
397
 
453
    LOG("- trigger event\n");
398
    LOG("- trigger event\n");
454
 
399
 
455
    call = THREAD->udebug.go_call;
400
    call = THREAD->udebug.go_call;
456
    THREAD->udebug.go_call = NULL;
401
    THREAD->udebug.go_call = NULL;
457
    IPC_SET_RETVAL(call->data, 0);
402
    IPC_SET_RETVAL(call->data, 0);
458
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_E);
403
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_E);
459
 
404
 
460
    /* Prevent any further debug activity in thread. */
405
    /* Prevent any further debug activity in thread. */
461
    THREAD->udebug.debug_active = false;
406
    THREAD->udebug.debug_active = false;
462
    THREAD->udebug.cur_event = 0;       /* none */
407
    THREAD->udebug.cur_event = 0;       /* none */
463
    THREAD->udebug.go = false;  /* set to initial value */
408
    THREAD->udebug.go = false;  /* set to initial value */
464
 
409
 
465
    ipc_answer(&TASK->answerbox, call);
410
    ipc_answer(&TASK->answerbox, call);
466
 
411
 
467
    mutex_unlock(&THREAD->udebug.lock);
412
    mutex_unlock(&THREAD->udebug.lock);
468
    mutex_unlock(&TASK->udebug.lock);
413
    mutex_unlock(&TASK->udebug.lock);
469
 
414
 
470
    /* Leave int_lock enabled. */
415
    /*
471
    /* This event does not sleep - debugging has finished in this thread. */
416
     * This event does not sleep - debugging has finished
-
 
417
     * in this thread.
-
 
418
     */
472
}
419
}
473
 
420
 
474
/**
421
/**
475
 * Terminate task debugging session.
422
 * Terminate task debugging session.
476
 *
423
 *
477
 * Gracefully terminates the debugging session for a task. If the debugger
424
 * Gracefully terminates the debugging session for a task. If the debugger
478
 * is still waiting for events on some threads, it will receive a
425
 * is still waiting for events on some threads, it will receive a
479
 * FINISHED event for each of them.
426
 * FINISHED event for each of them.
480
 *
427
 *
481
 * @param ta    Task structure. ta->udebug.lock must be already locked.
428
 * @param ta    Task structure. ta->udebug.lock must be already locked.
482
 * @return  Zero on success or negative error code.
429
 * @return  Zero on success or negative error code.
483
 */
430
 */
484
int udebug_task_cleanup(struct task *ta)
431
int udebug_task_cleanup(struct task *ta)
485
{
432
{
486
    thread_t *t;
433
    thread_t *t;
487
    link_t *cur;
434
    link_t *cur;
488
    int flags;
435
    int flags;
489
    ipl_t ipl;
436
    ipl_t ipl;
490
 
437
 
491
    LOG("udebug_task_cleanup()\n");
438
    LOG("udebug_task_cleanup()\n");
492
    LOG("task %" PRIu64 "\n", ta->taskid);
439
    LOG("task %" PRIu64 "\n", ta->taskid);
493
 
440
 
494
    udebug_int_lock();
-
 
495
 
-
 
496
    if (ta->udebug.dt_state != UDEBUG_TS_BEGINNING &&
441
    if (ta->udebug.dt_state != UDEBUG_TS_BEGINNING &&
497
        ta->udebug.dt_state != UDEBUG_TS_ACTIVE) {
442
        ta->udebug.dt_state != UDEBUG_TS_ACTIVE) {
498
        LOG("udebug_task_cleanup(): task not being debugged\n");
443
        LOG("udebug_task_cleanup(): task not being debugged\n");
499
        return EINVAL;
444
        return EINVAL;
500
    }
445
    }
501
 
446
 
502
    /* Finish debugging of all userspace threads */
447
    /* Finish debugging of all userspace threads */
503
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
448
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
504
        t = list_get_instance(cur, thread_t, th_link);
449
        t = list_get_instance(cur, thread_t, th_link);
505
 
450
 
506
        mutex_lock(&t->udebug.lock);
451
        mutex_lock(&t->udebug.lock);
507
 
452
 
508
        ipl = interrupts_disable();
453
        ipl = interrupts_disable();
509
        spinlock_lock(&t->lock);
454
        spinlock_lock(&t->lock);
510
 
455
 
511
        flags = t->flags;
456
        flags = t->flags;
512
 
457
 
513
        spinlock_unlock(&t->lock);
458
        spinlock_unlock(&t->lock);
514
        interrupts_restore(ipl);
459
        interrupts_restore(ipl);
515
 
460
 
516
        /* Only process userspace threads. */
461
        /* Only process userspace threads. */
517
        if ((flags & THREAD_FLAG_USPACE) != 0) {
462
        if ((flags & THREAD_FLAG_USPACE) != 0) {
518
            /* Prevent any further debug activity in thread. */
463
            /* Prevent any further debug activity in thread. */
519
            t->udebug.debug_active = false;
464
            t->udebug.debug_active = false;
520
            t->udebug.cur_event = 0;    /* none */
465
            t->udebug.cur_event = 0;    /* none */
521
 
466
 
522
            /* Is the thread still go? */
467
            /* Is the thread still go? */
523
            if (t->udebug.go == true) {
468
            if (t->udebug.go == true) {
524
                /*
469
                /*
525
                * Yes, so clear go. As debug_active == false,
470
                * Yes, so clear go. As debug_active == false,
526
                 * this doesn't affect anything.
471
                 * this doesn't affect anything.
527
                 */
472
                 */
528
                t->udebug.go = false;  
473
                t->udebug.go = false;  
529
 
474
 
530
                /* Answer GO call */
475
                /* Answer GO call */
531
                LOG("answer GO call with EVENT_FINISHED\n");
476
                LOG("answer GO call with EVENT_FINISHED\n");
532
                IPC_SET_RETVAL(t->udebug.go_call->data, 0);
477
                IPC_SET_RETVAL(t->udebug.go_call->data, 0);
533
                IPC_SET_ARG1(t->udebug.go_call->data,
478
                IPC_SET_ARG1(t->udebug.go_call->data,
534
                    UDEBUG_EVENT_FINISHED);
479
                    UDEBUG_EVENT_FINISHED);
535
 
480
 
536
                ipc_answer(&ta->answerbox, t->udebug.go_call);
481
                ipc_answer(&ta->answerbox, t->udebug.go_call);
537
                t->udebug.go_call = NULL;
482
                t->udebug.go_call = NULL;
538
            } else {
483
            } else {
539
                /*
484
                /*
540
                 * Debug_stop is already at initial value.
485
                 * Debug_stop is already at initial value.
541
                 * Yet this means the thread needs waking up.
486
                 * Yet this means the thread needs waking up.
542
                 */
487
                 */
543
 
488
 
544
                /*
489
                /*
545
                 * t's lock must not be held when calling
490
                 * t's lock must not be held when calling
546
                 * waitq_wakeup.
491
                 * waitq_wakeup.
547
                 */
492
                 */
548
                waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST);
493
                waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST);
549
            }
494
            }
550
        }
495
        }
551
        mutex_unlock(&t->udebug.lock);
496
        mutex_unlock(&t->udebug.lock);
552
    }
497
    }
553
 
498
 
554
    ta->udebug.dt_state = UDEBUG_TS_INACTIVE;
499
    ta->udebug.dt_state = UDEBUG_TS_INACTIVE;
555
    ta->udebug.debugger = NULL;
500
    ta->udebug.debugger = NULL;
556
 
501
 
557
    udebug_int_unlock();
-
 
558
 
-
 
559
    return 0;
502
    return 0;
560
}
503
}
561
 
504
 
562
 
505
 
563
/** @}
506
/** @}
564
 */
507
 */
565
 
508