Subversion Repositories HelenOS

Rev

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

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