Subversion Repositories HelenOS

Rev

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

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