Subversion Repositories HelenOS

Rev

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

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