Subversion Repositories HelenOS

Rev

Rev 3742 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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