Subversion Repositories HelenOS

Rev

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

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