Subversion Repositories HelenOS

Rev

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

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