Subversion Repositories HelenOS

Rev

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

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