Subversion Repositories HelenOS

Rev

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

Rev 3031 Rev 3032
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 <console/klog.h>
51
#include <console/klog.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);
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);
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
    if (TASK->udebug.dt_state == UDEBUG_TS_BEGINNING) {
144
    /* Lock order OK, THREAD->udebug.lock is after TASK->udebug.lock */
-
 
145
    mutex_lock(&THREAD->udebug.lock);
145
        klog_printf("udebug_stoppable_begin");
146
    ASSERT(THREAD->udebug.stoppable == false);
146
        klog_printf(" - nsc := %d", nsc);
147
    THREAD->udebug.stoppable = true;
147
    }
-
 
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
        /* Lock order OK, THREAD->udebug.lock is after TASK->udebug.lock */
-
 
159
        mutex_lock(&THREAD->udebug.lock);
-
 
160
        ASSERT(THREAD->udebug.stoppable == false);
-
 
161
        THREAD->udebug.stoppable = true;
-
 
162
        mutex_unlock(&THREAD->udebug.lock);
-
 
163
 
-
 
164
        TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
158
        TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
165
        TASK->udebug.begin_call = NULL;
159
        TASK->udebug.begin_call = NULL;
166
        mutex_unlock(&TASK->udebug.lock);
-
 
167
 
160
 
168
        IPC_SET_RETVAL(db_call->data, 0);
161
        IPC_SET_RETVAL(db_call->data, 0);
169
        //klog_printf("udebug_stoppable_begin/ipc_answer");
-
 
170
        ipc_answer(&TASK->answerbox, db_call);     
162
        ipc_answer(&TASK->answerbox, db_call);     
171
 
163
 
172
    } else if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
164
    } else if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
173
        /*
165
        /*
174
         * Active debugging session
166
         * Active debugging session
175
         */
167
         */
176
 
168
 
177
        /* Lock order OK, THREAD->udebug.lock is after TASK->udebug.lock */
-
 
178
        mutex_lock(&THREAD->udebug.lock);
-
 
179
        ASSERT(THREAD->udebug.stoppable == false);
-
 
180
        THREAD->udebug.stoppable = true;
-
 
181
 
-
 
182
        if (THREAD->udebug.debug_active && THREAD->udebug.stop) {
169
        if (THREAD->udebug.debug_active && THREAD->udebug.stop) {
183
            /*
170
            /*
184
             * Thread was requested to stop - answer go call
171
             * Thread was requested to stop - answer go call
185
             */
172
             */
186
 
173
 
187
            /* Make sure nobody takes this call away from us */
174
            /* Make sure nobody takes this call away from us */
188
            go_call = THREAD->udebug.go_call;
175
            go_call = THREAD->udebug.go_call;
189
            THREAD->udebug.go_call = NULL;
176
            THREAD->udebug.go_call = NULL;
190
            ASSERT(go_call);
177
            ASSERT(go_call);
191
 
178
 
192
            IPC_SET_RETVAL(go_call->data, 0);
179
            IPC_SET_RETVAL(go_call->data, 0);
193
            IPC_SET_ARG1(go_call->data, UDEBUG_EVENT_STOP);
180
            IPC_SET_ARG1(go_call->data, UDEBUG_EVENT_STOP);
194
 
181
 
195
            THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
182
            THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
196
            mutex_unlock(&THREAD->udebug.lock);
-
 
197
 
183
 
198
                ipc_answer(&TASK->answerbox, go_call);
184
                ipc_answer(&TASK->answerbox, go_call);
199
 
-
 
200
                mutex_unlock(&TASK->udebug.lock);
-
 
201
        } else {
-
 
202
            /*
-
 
203
             * No stop request - nothing happens.
-
 
204
             */
-
 
205
            mutex_unlock(&THREAD->udebug.lock);
-
 
206
                mutex_unlock(&TASK->udebug.lock);
-
 
207
        }
185
        }
208
    } else {
-
 
209
        /*
-
 
210
         * All other cases - nothing special happens.
-
 
211
         */
-
 
212
 
-
 
213
        /* Lock order OK, THREAD->udebug.lock is after TASK->udebug.lock */
-
 
214
        mutex_lock(&THREAD->udebug.lock);
-
 
215
        ASSERT(THREAD->udebug.stoppable == false);
-
 
216
        THREAD->udebug.stoppable = true;
-
 
217
        mutex_unlock(&THREAD->udebug.lock);
-
 
218
 
-
 
219
            mutex_unlock(&TASK->udebug.lock);
-
 
220
    }
186
    }
-
 
187
 
-
 
188
    mutex_unlock(&THREAD->udebug.lock);
-
 
189
        mutex_unlock(&TASK->udebug.lock);
221
}
190
}
222
 
191
 
223
void udebug_stoppable_end(void)
192
void udebug_stoppable_end(void)
224
{
193
{
225
    /* Early check for undebugged tasks */
194
    /* Early check for undebugged tasks */
226
    if (!udebug_thread_precheck()) {
195
    if (!udebug_thread_precheck()) {
227
        udebug_int_unlock();
196
        udebug_int_unlock();
228
        return;
197
        return;
229
    }
198
    }
230
 
199
 
231
restart:
200
restart:
232
    mutex_lock(&TASK->udebug.lock);
201
    mutex_lock(&TASK->udebug.lock);
233
 
-
 
234
    /* Lock order OK, THREAD->udebug.lock is after TASK->udebug.lock */
-
 
235
    mutex_lock(&THREAD->udebug.lock);
202
    mutex_lock(&THREAD->udebug.lock);
236
 
203
 
237
    if (THREAD->udebug.debug_active &&
204
    if (THREAD->udebug.debug_active &&
238
        THREAD->udebug.stop == true) {
205
        THREAD->udebug.stop == true) {
239
        TASK->udebug.begin_call = NULL;
206
        TASK->udebug.begin_call = NULL;
240
        mutex_unlock(&THREAD->udebug.lock);
207
        mutex_unlock(&THREAD->udebug.lock);
241
        mutex_unlock(&TASK->udebug.lock);
208
        mutex_unlock(&TASK->udebug.lock);
242
 
209
 
243
        udebug_wait_for_go(&THREAD->udebug.go_wq);
210
        udebug_wait_for_go(&THREAD->udebug.go_wq);
244
 
211
 
245
        goto restart;
212
        goto restart;
246
        /* must try again - have to lose stoppability atomically */
213
        /* must try again - have to lose stoppability atomically */
247
    } else {
214
    } else {
248
        ++TASK->udebug.not_stoppable_count;
215
        ++TASK->udebug.not_stoppable_count;
249
        ASSERT(THREAD->udebug.stoppable == true);
216
        ASSERT(THREAD->udebug.stoppable == true);
250
        THREAD->udebug.stoppable = false;
217
        THREAD->udebug.stoppable = false;
251
 
218
 
252
        mutex_unlock(&THREAD->udebug.lock);
219
        mutex_unlock(&THREAD->udebug.lock);
253
        mutex_unlock(&TASK->udebug.lock);
220
        mutex_unlock(&TASK->udebug.lock);
254
    }
221
    }
255
 
222
 
256
    udebug_int_unlock();
223
    udebug_int_unlock();
257
}
224
}
258
 
225
 
259
/** 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.
260
 *
227
 *
261
 * This function is called from clock(). Preemption is enabled.
228
 * This function is called from clock(). Preemption is enabled.
262
 * interrupts are disabled, but since this is called after
229
 * interrupts are disabled, but since this is called after
263
 * 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
264
 * not to allow arbitrary recursion or deadlock with the thread context.
231
 * not to allow arbitrary recursion or deadlock with the thread context.
265
 */
232
 */
266
void udebug_before_thread_runs(void)
233
void udebug_before_thread_runs(void)
267
{
234
{
268
    ipl_t ipl;
235
    ipl_t ipl;
269
 
236
 
270
    ASSERT(!PREEMPTION_DISABLED);
237
    ASSERT(!PREEMPTION_DISABLED);
271
 
238
 
272
    /*
239
    /*
273
     * Prevent agains re-entering, such as when preempted inside this
240
     * Prevent agains re-entering, such as when preempted inside this
274
     * function.
241
     * function.
275
     */
242
     */
276
    if (atomic_get(&THREAD->udebug.int_lock) != 0)
243
    if (atomic_get(&THREAD->udebug.int_lock) != 0)
277
        return;
244
        return;
278
 
245
 
279
    udebug_int_lock();
246
    udebug_int_lock();
280
 
247
 
281
    ipl = interrupts_enable();
248
    ipl = interrupts_enable();
282
 
249
 
283
    /* Now we're free to do whatever we need (lock mutexes, sleep, etc.) */
250
    /* Now we're free to do whatever we need (lock mutexes, sleep, etc.) */
284
 
251
 
285
    /* Check if we're supposed to stop */
252
    /* Check if we're supposed to stop */
286
    udebug_stoppable_begin();
253
    udebug_stoppable_begin();
287
    udebug_stoppable_end();
254
    udebug_stoppable_end();
288
 
255
 
289
    interrupts_restore(ipl);
256
    interrupts_restore(ipl);
290
 
257
 
291
    udebug_int_unlock();
258
    udebug_int_unlock();
292
}
259
}
293
 
260
 
294
void udebug_syscall_event(unative_t a1, unative_t a2, unative_t a3,
261
void udebug_syscall_event(unative_t a1, unative_t a2, unative_t a3,
295
    unative_t a4, unative_t a5, unative_t a6, unative_t id, unative_t rc,
262
    unative_t a4, unative_t a5, unative_t a6, unative_t id, unative_t rc,
296
    bool end_variant)
263
    bool end_variant)
297
{
264
{
298
    call_t *call;
265
    call_t *call;
299
    udebug_event_t etype;
266
    udebug_event_t etype;
300
 
267
 
301
    etype = end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
268
    etype = end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
302
 
269
 
303
    udebug_int_lock();
270
    udebug_int_lock();
304
 
271
 
305
    /* Early check for undebugged tasks */
272
    /* Early check for undebugged tasks */
306
    if (!udebug_thread_precheck()) {
273
    if (!udebug_thread_precheck()) {
307
        udebug_int_unlock();
274
        udebug_int_unlock();
308
        return;
275
        return;
309
    }
276
    }
310
 
277
 
311
    mutex_lock(&TASK->udebug.lock);
278
    mutex_lock(&TASK->udebug.lock);
312
    mutex_lock(&THREAD->udebug.lock);
279
    mutex_lock(&THREAD->udebug.lock);
313
 
280
 
314
    /* Must only generate events when in debugging session and have go */
281
    /* Must only generate events when in debugging session and have go */
315
    if (THREAD->udebug.debug_active != true ||
282
    if (THREAD->udebug.debug_active != true ||
316
        THREAD->udebug.stop == true ||
283
        THREAD->udebug.stop == true ||
317
        (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
284
        (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
318
        mutex_unlock(&THREAD->udebug.lock);
285
        mutex_unlock(&THREAD->udebug.lock);
319
        mutex_unlock(&TASK->udebug.lock);
286
        mutex_unlock(&TASK->udebug.lock);
320
        return;
287
        return;
321
    }
288
    }
322
 
289
 
323
    //klog_printf("udebug_syscall_event");
290
    //klog_printf("udebug_syscall_event");
324
    call = THREAD->udebug.go_call;
291
    call = THREAD->udebug.go_call;
325
    THREAD->udebug.go_call = NULL;
292
    THREAD->udebug.go_call = NULL;
326
 
293
 
327
    IPC_SET_RETVAL(call->data, 0);
294
    IPC_SET_RETVAL(call->data, 0);
328
    IPC_SET_ARG1(call->data, etype);
295
    IPC_SET_ARG1(call->data, etype);
329
    IPC_SET_ARG2(call->data, id);
296
    IPC_SET_ARG2(call->data, id);
330
    IPC_SET_ARG3(call->data, rc);
297
    IPC_SET_ARG3(call->data, rc);
331
    //klog_printf("udebug_syscall_event/ipc_answer");
298
    //klog_printf("udebug_syscall_event/ipc_answer");
332
 
299
 
333
    THREAD->udebug.syscall_args[0] = a1;
300
    THREAD->udebug.syscall_args[0] = a1;
334
    THREAD->udebug.syscall_args[1] = a2;
301
    THREAD->udebug.syscall_args[1] = a2;
335
    THREAD->udebug.syscall_args[2] = a3;
302
    THREAD->udebug.syscall_args[2] = a3;
336
    THREAD->udebug.syscall_args[3] = a4;
303
    THREAD->udebug.syscall_args[3] = a4;
337
    THREAD->udebug.syscall_args[4] = a5;
304
    THREAD->udebug.syscall_args[4] = a5;
338
    THREAD->udebug.syscall_args[5] = a6;
305
    THREAD->udebug.syscall_args[5] = a6;
339
 
306
 
340
    /*
307
    /*
341
     * Make sure udebug.stop is true when going to sleep
308
     * Make sure udebug.stop is true when going to sleep
342
     * in case we get woken up by DEBUG_END. (At which
309
     * in case we get woken up by DEBUG_END. (At which
343
     * point it must be back to the initial true value).
310
     * point it must be back to the initial true value).
344
     */
311
     */
345
    THREAD->udebug.stop = true;
312
    THREAD->udebug.stop = true;
346
 
-
 
347
    THREAD->udebug.cur_event = etype;
313
    THREAD->udebug.cur_event = etype;
348
    mutex_unlock(&THREAD->udebug.lock);
-
 
349
 
314
 
350
    ipc_answer(&TASK->answerbox, call);
315
    ipc_answer(&TASK->answerbox, call);
351
 
316
 
-
 
317
    mutex_unlock(&THREAD->udebug.lock);
352
    mutex_unlock(&TASK->udebug.lock);
318
    mutex_unlock(&TASK->udebug.lock);
353
 
319
 
354
    udebug_wait_for_go(&THREAD->udebug.go_wq);
320
    udebug_wait_for_go(&THREAD->udebug.go_wq);
355
 
321
 
356
    udebug_int_unlock();
322
    udebug_int_unlock();
357
}
323
}
358
 
324
 
359
void udebug_thread_b_event(struct thread *t)
325
void udebug_thread_b_event(struct thread *t)
360
{
326
{
361
    call_t *call;
327
    call_t *call;
362
 
328
 
363
    udebug_int_lock();
329
    udebug_int_lock();
364
 
330
 
365
    mutex_lock(&TASK->udebug.lock);
331
    mutex_lock(&TASK->udebug.lock);
366
    mutex_lock(&THREAD->udebug.lock);
332
    mutex_lock(&THREAD->udebug.lock);
367
 
333
 
368
    klog_printf("udebug_thread_b_event");
334
    klog_printf("udebug_thread_b_event");
369
    klog_printf("- check state");
335
    klog_printf("- check state");
370
 
336
 
371
    /* Must only generate events when in debugging session */
337
    /* Must only generate events when in debugging session */
372
    if (THREAD->udebug.debug_active != true) {
338
    if (THREAD->udebug.debug_active != true) {
373
        klog_printf("- debug_active: %s, udebug.stop: %s",
339
        klog_printf("- debug_active: %s, udebug.stop: %s",
374
            THREAD->udebug.debug_active ? "yes(+)" : "no(-)",
340
            THREAD->udebug.debug_active ? "yes(+)" : "no(-)",
375
            THREAD->udebug.stop ? "yes(-)" : "no(+)");
341
            THREAD->udebug.stop ? "yes(-)" : "no(+)");
376
        mutex_unlock(&THREAD->udebug.lock);
342
        mutex_unlock(&THREAD->udebug.lock);
377
        mutex_unlock(&TASK->udebug.lock);
343
        mutex_unlock(&TASK->udebug.lock);
378
        return;
344
        return;
379
    }
345
    }
380
 
346
 
381
    klog_printf("- trigger event");
347
    klog_printf("- trigger event");
382
 
348
 
383
    call = THREAD->udebug.go_call;
349
    call = THREAD->udebug.go_call;
384
    THREAD->udebug.go_call = NULL;
350
    THREAD->udebug.go_call = NULL;
385
    IPC_SET_RETVAL(call->data, 0);
351
    IPC_SET_RETVAL(call->data, 0);
386
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_B);
352
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_B);
387
    IPC_SET_ARG2(call->data, (unative_t)t);
353
    IPC_SET_ARG2(call->data, (unative_t)t);
388
 
354
 
389
    /*
355
    /*
390
     * Make sure udebug.stop is true when going to sleep
356
     * Make sure udebug.stop is true when going to sleep
391
     * in case we get woken up by DEBUG_END. (At which
357
     * in case we get woken up by DEBUG_END. (At which
392
     * point it must be back to the initial true value).
358
     * point it must be back to the initial true value).
393
     */
359
     */
394
    THREAD->udebug.stop = true;
360
    THREAD->udebug.stop = true;
395
 
-
 
396
    THREAD->udebug.cur_event = UDEBUG_EVENT_THREAD_B;
361
    THREAD->udebug.cur_event = UDEBUG_EVENT_THREAD_B;
397
    mutex_unlock(&THREAD->udebug.lock);
-
 
398
 
362
 
399
    ipc_answer(&TASK->answerbox, call);
363
    ipc_answer(&TASK->answerbox, call);
400
 
364
 
-
 
365
    mutex_unlock(&THREAD->udebug.lock);
401
    mutex_unlock(&TASK->udebug.lock);
366
    mutex_unlock(&TASK->udebug.lock);
402
 
367
 
403
    klog_printf("- sleep");
368
    klog_printf("- sleep");
404
    udebug_wait_for_go(&THREAD->udebug.go_wq);
369
    udebug_wait_for_go(&THREAD->udebug.go_wq);
405
 
370
 
406
    udebug_int_unlock();
371
    udebug_int_unlock();
407
}
372
}
408
 
373
 
409
void udebug_thread_e_event(void)
374
void udebug_thread_e_event(void)
410
{
375
{
411
    call_t *call;
376
    call_t *call;
412
 
377
 
413
    udebug_int_lock();
378
    udebug_int_lock();
414
 
379
 
415
    mutex_lock(&TASK->udebug.lock);
380
    mutex_lock(&TASK->udebug.lock);
416
    mutex_lock(&THREAD->udebug.lock);
381
    mutex_lock(&THREAD->udebug.lock);
417
 
382
 
418
    klog_printf("udebug_thread_e_event");
383
    klog_printf("udebug_thread_e_event");
419
    klog_printf("- check state");
384
    klog_printf("- check state");
420
 
385
 
421
    /* Must only generate events when in debugging session */
386
    /* Must only generate events when in debugging session */
422
    if (THREAD->udebug.debug_active != true) {
387
    if (THREAD->udebug.debug_active != true) {
423
        klog_printf("- debug_active: %s, udebug.stop: %s",
388
        klog_printf("- debug_active: %s, udebug.stop: %s",
424
            THREAD->udebug.debug_active ? "yes(+)" : "no(-)",
389
            THREAD->udebug.debug_active ? "yes(+)" : "no(-)",
425
            THREAD->udebug.stop ? "yes(-)" : "no(+)");
390
            THREAD->udebug.stop ? "yes(-)" : "no(+)");
426
        mutex_unlock(&THREAD->udebug.lock);
391
        mutex_unlock(&THREAD->udebug.lock);
427
        mutex_unlock(&TASK->udebug.lock);
392
        mutex_unlock(&TASK->udebug.lock);
428
        return;
393
        return;
429
    }
394
    }
430
 
395
 
431
    klog_printf("- trigger event");
396
    klog_printf("- trigger event");
432
 
397
 
433
    call = THREAD->udebug.go_call;
398
    call = THREAD->udebug.go_call;
434
    THREAD->udebug.go_call = NULL;
399
    THREAD->udebug.go_call = NULL;
435
    IPC_SET_RETVAL(call->data, 0);
400
    IPC_SET_RETVAL(call->data, 0);
436
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_E);
401
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_E);
437
 
402
 
438
    /* Prevent any further debug activity in thread */
403
    /* Prevent any further debug activity in thread */
439
    THREAD->udebug.debug_active = false;
404
    THREAD->udebug.debug_active = false;
440
    THREAD->udebug.cur_event = 0;       /* none */
405
    THREAD->udebug.cur_event = 0;       /* none */
441
    THREAD->udebug.stop = true; /* set to initial value */
406
    THREAD->udebug.stop = true; /* set to initial value */
442
 
407
 
443
    mutex_unlock(&THREAD->udebug.lock);
-
 
444
 
-
 
445
    ipc_answer(&TASK->answerbox, call);
408
    ipc_answer(&TASK->answerbox, call);
446
 
409
 
-
 
410
    mutex_unlock(&THREAD->udebug.lock);
447
    mutex_unlock(&TASK->udebug.lock);
411
    mutex_unlock(&TASK->udebug.lock);
448
 
412
 
449
    /* Leave int_lock enabled */
413
    /* Leave int_lock enabled */
450
    /* This event does not sleep - debugging has finished in this thread */
414
    /* This event does not sleep - debugging has finished in this thread */
451
}
415
}
452
 
416
 
453
static void breakpoint_trap_event(uintptr_t addr, udebug_event_t etype)
417
static void breakpoint_trap_event(uintptr_t addr, udebug_event_t etype)
454
{
418
{
455
    call_t *call;
419
    call_t *call;
456
 
420
 
457
    udebug_int_lock();
421
    udebug_int_lock();
458
 
422
 
459
    mutex_lock(&TASK->udebug.lock);
423
    mutex_lock(&TASK->udebug.lock);
460
    mutex_lock(&THREAD->udebug.lock);
424
    mutex_lock(&THREAD->udebug.lock);
461
 
425
 
462
    /* Must only generate events when in debugging session and have go */
426
    /* Must only generate events when in debugging session and have go */
463
    if (THREAD->udebug.debug_active != true ||
427
    if (THREAD->udebug.debug_active != true ||
464
        THREAD->udebug.stop == true) {
428
        THREAD->udebug.stop == true) {
465
        mutex_unlock(&THREAD->udebug.lock);
429
        mutex_unlock(&THREAD->udebug.lock);
466
        mutex_unlock(&TASK->udebug.lock);
430
        mutex_unlock(&TASK->udebug.lock);
467
        udebug_int_unlock();
431
        udebug_int_unlock();
468
        return;
432
        return;
469
    }
433
    }
470
 
434
 
471
    /* Verify that the event is enabled */
435
    /* Verify that the event is enabled */
472
    if ((TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
436
    if ((TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
473
        mutex_unlock(&THREAD->udebug.lock);
437
        mutex_unlock(&THREAD->udebug.lock);
474
        mutex_unlock(&TASK->udebug.lock);
438
        mutex_unlock(&TASK->udebug.lock);
475
        udebug_int_unlock();
439
        udebug_int_unlock();
476
        return;
440
        return;
477
    }
441
    }
478
 
442
 
479
    klog_printf("udebug_breakpoint/trap_event");
443
    klog_printf("udebug_breakpoint/trap_event");
480
    call = THREAD->udebug.go_call;
444
    call = THREAD->udebug.go_call;
481
    THREAD->udebug.go_call = NULL;
445
    THREAD->udebug.go_call = NULL;
482
 
446
 
483
    IPC_SET_RETVAL(call->data, 0);
447
    IPC_SET_RETVAL(call->data, 0);
484
    IPC_SET_ARG1(call->data, etype);
448
    IPC_SET_ARG1(call->data, etype);
485
    IPC_SET_ARG2(call->data, addr);
449
    IPC_SET_ARG2(call->data, addr);
486
 
450
 
487
    /*
451
    /*
488
     * Make sure udebug.stop is true when going to sleep
452
     * Make sure udebug.stop is true when going to sleep
489
     * in case we get woken up by DEBUG_END. (At which
453
     * in case we get woken up by DEBUG_END. (At which
490
     * point it must be back to the initial true value).
454
     * point it must be back to the initial true value).
491
     */
455
     */
492
    THREAD->udebug.stop = true;
456
    THREAD->udebug.stop = true;
493
 
-
 
494
    THREAD->udebug.cur_event = etype;
457
    THREAD->udebug.cur_event = etype;
495
    mutex_unlock(&THREAD->udebug.lock);
-
 
496
 
458
 
497
    klog_printf("- send answer");
459
    klog_printf("- send answer");
498
 
-
 
499
    ipc_answer(&TASK->answerbox, call);
460
    ipc_answer(&TASK->answerbox, call);
-
 
461
 
-
 
462
    mutex_unlock(&THREAD->udebug.lock);
500
    mutex_unlock(&TASK->udebug.lock);
463
    mutex_unlock(&TASK->udebug.lock);
501
 
464
 
502
    udebug_wait_for_go(&THREAD->udebug.go_wq);
465
    udebug_wait_for_go(&THREAD->udebug.go_wq);
503
 
466
 
504
    udebug_int_unlock();
467
    udebug_int_unlock();
505
}
468
}
506
 
469
 
507
void udebug_breakpoint_event(uintptr_t addr)
470
void udebug_breakpoint_event(uintptr_t addr)
508
{
471
{
509
    breakpoint_trap_event(addr, UDEBUG_EVENT_BREAKPOINT);
472
    breakpoint_trap_event(addr, UDEBUG_EVENT_BREAKPOINT);
510
}
473
}
511
 
474
 
512
void udebug_trap_event(uintptr_t addr)
475
void udebug_trap_event(uintptr_t addr)
513
{
476
{
514
    breakpoint_trap_event(addr, UDEBUG_EVENT_TRAP);
477
    breakpoint_trap_event(addr, UDEBUG_EVENT_TRAP);
515
}
478
}
516
 
479
 
517
/**
480
/**
518
 * Terminate task debugging session.
481
 * Terminate task debugging session.
519
 *
482
 *
520
 * \param ta->udebug.lock must be already locked.
483
 * \param ta->udebug.lock must be already locked.
521
 * \return Zero on success or negative error code.
484
 * \return Zero on success or negative error code.
522
 */
485
 */
523
int udebug_task_cleanup(struct task *ta)
486
int udebug_task_cleanup(struct task *ta)
524
{
487
{
525
    thread_t *t;
488
    thread_t *t;
526
    link_t *cur;
489
    link_t *cur;
527
    int flags;
490
    int flags;
528
    ipl_t ipl;
491
    ipl_t ipl;
529
 
492
 
530
    klog_printf("udebug_task_cleanup()");
493
    klog_printf("udebug_task_cleanup()");
531
    klog_printf("task %llu", ta->taskid);
494
    klog_printf("task %llu", ta->taskid);
532
 
495
 
533
    udebug_int_lock();
496
    udebug_int_lock();
534
 
497
 
535
    if (ta->udebug.dt_state == UDEBUG_TS_BEGINNING &&
498
    if (ta->udebug.dt_state != UDEBUG_TS_BEGINNING &&
536
        ta->udebug.dt_state != UDEBUG_TS_ACTIVE) {
499
        ta->udebug.dt_state != UDEBUG_TS_ACTIVE) {
537
        klog_printf("udebug_task_cleanup(): task not being debugged");
500
        klog_printf("udebug_task_cleanup(): task not being debugged");
538
        return EINVAL;
501
        return EINVAL;
539
    }
502
    }
540
 
503
 
541
    /* Finish debugging of all userspace threads */
504
    /* Finish debugging of all userspace threads */
542
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
505
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
543
        t = list_get_instance(cur, thread_t, th_link);
506
        t = list_get_instance(cur, thread_t, th_link);
544
 
507
 
545
        mutex_lock(&t->udebug.lock);
508
        mutex_lock(&t->udebug.lock);
546
 
509
 
547
        ipl = interrupts_disable();
510
        ipl = interrupts_disable();
548
        spinlock_lock(&t->lock);
511
        spinlock_lock(&t->lock);
549
 
512
 
550
        flags = t->flags;
513
        flags = t->flags;
551
 
514
 
552
        spinlock_unlock(&t->lock);
515
        spinlock_unlock(&t->lock);
553
        interrupts_restore(ipl);
516
        interrupts_restore(ipl);
554
 
517
 
555
        /* Only process userspace threads */
518
        /* Only process userspace threads */
556
        if ((flags & THREAD_FLAG_USPACE) != 0) {
519
        if ((flags & THREAD_FLAG_USPACE) != 0) {
557
            /* Prevent any further debug activity in thread */
520
            /* Prevent any further debug activity in thread */
558
            t->udebug.debug_active = false;
521
            t->udebug.debug_active = false;
559
            t->udebug.cur_event = 0;    /* none */
522
            t->udebug.cur_event = 0;    /* none */
560
 
523
 
561
            /* Still has go? */
524
            /* Still has go? */
562
            if (t->udebug.stop == false) {
525
            if (t->udebug.stop == false) {
563
                /*
526
                /*
564
                * Yes, so clear go. As debug_active == false,
527
                * Yes, so clear go. As debug_active == false,
565
                 * this doesn't affect anything.
528
                 * this doesn't affect anything.
566
                 */
529
                 */
567
                t->udebug.stop = true; 
530
                t->udebug.stop = true; 
568
 
531
 
569
                /* Answer GO call */
532
                /* Answer GO call */
570
                klog_printf("answer GO call with EVENT_FINISHED");
533
                klog_printf("answer GO call with EVENT_FINISHED");
571
                IPC_SET_RETVAL(t->udebug.go_call->data, 0);
534
                IPC_SET_RETVAL(t->udebug.go_call->data, 0);
572
                IPC_SET_ARG1(t->udebug.go_call->data, UDEBUG_EVENT_FINISHED);
535
                IPC_SET_ARG1(t->udebug.go_call->data, UDEBUG_EVENT_FINISHED);
573
 
536
 
574
                ipc_answer(&ta->answerbox, t->udebug.go_call);
537
                ipc_answer(&ta->answerbox, t->udebug.go_call);
575
                t->udebug.go_call = NULL;
538
                t->udebug.go_call = NULL;
576
            } else {
539
            } else {
577
                /*
540
                /*
578
                 * Debug_stop is already at initial value.
541
                 * Debug_stop is already at initial value.
579
                 * Yet this means the thread needs waking up.
542
                 * Yet this means the thread needs waking up.
580
                 */
543
                 */
581
 
544
 
582
                /*
545
                /*
583
                 * t's lock must not be held when calling
546
                 * t's lock must not be held when calling
584
                 * waitq_wakeup.
547
                 * waitq_wakeup.
585
                 */
548
                 */
586
                waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST);
549
                waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST);
587
            }
550
            }
588
        }
551
        }
589
        mutex_unlock(&t->udebug.lock);
552
        mutex_unlock(&t->udebug.lock);
590
    }
553
    }
591
 
554
 
592
    ta->udebug.dt_state = UDEBUG_TS_INACTIVE;
555
    ta->udebug.dt_state = UDEBUG_TS_INACTIVE;
593
    ta->udebug.debugger = NULL;
556
    ta->udebug.debugger = NULL;
594
 
557
 
595
    udebug_int_unlock();
558
    udebug_int_unlock();
596
 
559
 
597
    return 0;
560
    return 0;
598
}
561
}
599
 
562
 
600
 
563
 
601
/** @}
564
/** @}
602
 */
565
 */
603
 
566