Subversion Repositories HelenOS

Rev

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

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