Subversion Repositories HelenOS

Rev

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

Rev 2921 Rev 3014
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
void udebug_task_init(udebug_task_t *ut)
-
 
45
{
-
 
46
    ut->dt_state = UDEBUG_TS_INACTIVE;
-
 
47
    ut->begin_call = NULL;
-
 
48
    ut->not_stoppable_count = 0;
-
 
49
    ut->evmask = 0;
-
 
50
}
-
 
51
 
44
static void udebug_wait_for_go(waitq_t *wq)
52
static void udebug_wait_for_go(waitq_t *wq)
45
{
53
{
46
    int rc;
54
    int rc;
47
    ipl_t ipl;
55
    ipl_t ipl;
48
 
56
 
49
    ipl = waitq_sleep_prepare(wq);
57
    ipl = waitq_sleep_prepare(wq);
50
 
58
 
51
    wq->missed_wakeups = 0; /* Enforce blocking. */
59
    wq->missed_wakeups = 0; /* Enforce blocking. */
52
    rc = waitq_sleep_timeout_unsafe(wq, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
60
    rc = waitq_sleep_timeout_unsafe(wq, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
53
 
61
 
54
    waitq_sleep_finish(wq, rc, ipl);
62
    waitq_sleep_finish(wq, rc, ipl);
55
}
63
}
56
 
64
 
57
void udebug_stoppable_begin(void)
65
void udebug_stoppable_begin(void)
58
{
66
{
59
    int nsc;
67
    int nsc;
60
    call_t *db_call, *go_call;
68
    call_t *db_call, *go_call;
61
    ipl_t ipl;
69
    ipl_t ipl;
62
 
70
 
63
    ASSERT(THREAD);
71
    ASSERT(THREAD);
64
    ASSERT(TASK);
72
    ASSERT(TASK);
65
 
73
 
66
    ipl = interrupts_disable();
74
    ipl = interrupts_disable();
67
    spinlock_lock(&TASK->lock);
75
    spinlock_lock(&TASK->lock);
68
 
76
 
69
    nsc = --TASK->not_stoppable_count;
77
    nsc = --TASK->udebug.not_stoppable_count;
70
 
78
 
71
    if (TASK->dt_state == UDEBUG_TS_BEGINNING) {
79
    if (TASK->udebug.dt_state == UDEBUG_TS_BEGINNING) {
72
        klog_printf("udebug_stoppable_begin");
80
        klog_printf("udebug_stoppable_begin");
73
        klog_printf(" - nsc := %d", nsc);
81
        klog_printf(" - nsc := %d", nsc);
74
    }
82
    }
75
 
83
 
76
    if (TASK->dt_state == UDEBUG_TS_BEGINNING && nsc == 0) {
84
    if (TASK->udebug.dt_state == UDEBUG_TS_BEGINNING && nsc == 0) {
77
        /*
85
        /*
78
         * This was the last non-stoppable thread. Reply to
86
         * This was the last non-stoppable thread. Reply to
79
         * DEBUG_BEGIN call.
87
         * DEBUG_BEGIN call.
80
         */
88
         */
81
 
89
 
82
        db_call = TASK->debug_begin_call;
90
        db_call = TASK->udebug.begin_call;
83
        ASSERT(db_call);
91
        ASSERT(db_call);
84
 
92
 
85
        /* Lock order OK, THREAD->debug_lock is after TASK->lock */
93
        /* Lock order OK, THREAD->debug_lock is after TASK->lock */
86
        spinlock_lock(&THREAD->debug_lock);
94
        spinlock_lock(&THREAD->debug_lock);
87
        THREAD->debug_stoppable = true;
95
        THREAD->debug_stoppable = true;
88
        spinlock_unlock(&THREAD->debug_lock);
96
        spinlock_unlock(&THREAD->debug_lock);
89
 
97
 
90
        TASK->dt_state = UDEBUG_TS_ACTIVE;
98
        TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
91
        TASK->debug_begin_call = NULL;
99
        TASK->udebug.begin_call = NULL;
92
        spinlock_unlock(&TASK->lock);
100
        spinlock_unlock(&TASK->lock);
93
        interrupts_restore(ipl);
101
        interrupts_restore(ipl);
94
 
102
 
95
        IPC_SET_RETVAL(db_call->data, 0);
103
        IPC_SET_RETVAL(db_call->data, 0);
96
        //klog_printf("udebug_stoppable_begin/ipc_answer");
104
        //klog_printf("udebug_stoppable_begin/ipc_answer");
97
        ipc_answer(&TASK->answerbox, db_call);     
105
        ipc_answer(&TASK->answerbox, db_call);     
98
 
106
 
99
    } else if (TASK->dt_state == UDEBUG_TS_ACTIVE) {
107
    } else if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
100
        /*
108
        /*
101
         * Active debugging session
109
         * Active debugging session
102
         */
110
         */
103
 
111
 
104
        /* Lock order OK, THREAD->debug_lock is after TASK->lock */
112
        /* Lock order OK, THREAD->debug_lock is after TASK->lock */
105
        spinlock_lock(&THREAD->debug_lock);
113
        spinlock_lock(&THREAD->debug_lock);
106
        THREAD->debug_stoppable = true;
114
        THREAD->debug_stoppable = true;
107
 
115
 
108
        if (THREAD->debug_active && THREAD->debug_stop) {
116
        if (THREAD->debug_active && THREAD->debug_stop) {
109
            /*
117
            /*
110
             * Thread was requested to stop - answer go call
118
             * Thread was requested to stop - answer go call
111
             */
119
             */
112
 
120
 
113
            /* Make sure nobody takes this call away from us */
121
            /* Make sure nobody takes this call away from us */
114
            go_call = THREAD->debug_go_call;
122
            go_call = THREAD->debug_go_call;
115
            THREAD->debug_go_call = NULL;
123
            THREAD->debug_go_call = NULL;
116
            ASSERT(go_call);
124
            ASSERT(go_call);
117
 
125
 
118
            IPC_SET_RETVAL(go_call->data, 0);
126
            IPC_SET_RETVAL(go_call->data, 0);
119
            IPC_SET_ARG1(go_call->data, UDEBUG_EVENT_STOP);
127
            IPC_SET_ARG1(go_call->data, UDEBUG_EVENT_STOP);
120
 
128
 
121
            THREAD->cur_event = UDEBUG_EVENT_STOP;
129
            THREAD->cur_event = UDEBUG_EVENT_STOP;
122
            spinlock_unlock(&THREAD->debug_lock);
130
            spinlock_unlock(&THREAD->debug_lock);
123
 
131
 
124
                ipc_answer(&TASK->answerbox, go_call);
132
                ipc_answer(&TASK->answerbox, go_call);
125
 
133
 
126
                spinlock_unlock(&TASK->lock);
134
                spinlock_unlock(&TASK->lock);
127
            interrupts_restore(ipl);
135
            interrupts_restore(ipl);
128
        } else {
136
        } else {
129
            /*
137
            /*
130
             * No stop request - nothing happens.
138
             * No stop request - nothing happens.
131
             */
139
             */
132
            spinlock_unlock(&THREAD->debug_lock);
140
            spinlock_unlock(&THREAD->debug_lock);
133
                spinlock_unlock(&TASK->lock);
141
                spinlock_unlock(&TASK->lock);
134
            interrupts_restore(ipl);
142
            interrupts_restore(ipl);
135
        }
143
        }
136
    } else {
144
    } else {
137
        /*
145
        /*
138
         * All other cases - nothing special happens.
146
         * All other cases - nothing special happens.
139
         */
147
         */
140
 
148
 
141
        /* Lock order OK, THREAD->debug_lock is after TASK->lock */
149
        /* Lock order OK, THREAD->debug_lock is after TASK->lock */
142
        spinlock_lock(&THREAD->debug_lock);
150
        spinlock_lock(&THREAD->debug_lock);
143
        THREAD->debug_stoppable = true;
151
        THREAD->debug_stoppable = true;
144
        spinlock_unlock(&THREAD->debug_lock);
152
        spinlock_unlock(&THREAD->debug_lock);
145
 
153
 
146
            spinlock_unlock(&TASK->lock);
154
            spinlock_unlock(&TASK->lock);
147
        interrupts_restore(ipl);
155
        interrupts_restore(ipl);
148
    }
156
    }
149
}
157
}
150
 
158
 
151
void udebug_stoppable_end(void)
159
void udebug_stoppable_end(void)
152
{
160
{
153
    ipl_t ipl;
161
    ipl_t ipl;
154
 
162
 
155
restart:
163
restart:
156
    ipl = interrupts_disable();
164
    ipl = interrupts_disable();
157
    spinlock_lock(&TASK->lock);
165
    spinlock_lock(&TASK->lock);
158
 
166
 
159
    /* Lock order OK, THREAD->debug_lock is after TASK->lock */
167
    /* Lock order OK, THREAD->debug_lock is after TASK->lock */
160
    spinlock_lock(&THREAD->debug_lock);
168
    spinlock_lock(&THREAD->debug_lock);
161
 
169
 
162
    if (TASK->dt_state == UDEBUG_TS_ACTIVE) {
170
    if (TASK->udebug.dt_state == UDEBUG_TS_ACTIVE) {
163
        //klog_printf("udebug_stoppable_end");
171
        //klog_printf("udebug_stoppable_end");
164
        //klog_printf("debug_stop=%d", THREAD->debug_stop);
172
        //klog_printf("debug_stop=%d", THREAD->debug_stop);
165
    }
173
    }
166
 
174
 
167
    if (THREAD->debug_active &&
175
    if (THREAD->debug_active &&
168
        THREAD->debug_stop == true) {
176
        THREAD->debug_stop == true) {
169
        TASK->debug_begin_call = NULL;
177
        TASK->udebug.begin_call = NULL;
170
        spinlock_unlock(&THREAD->debug_lock);
178
        spinlock_unlock(&THREAD->debug_lock);
171
        spinlock_unlock(&TASK->lock);
179
        spinlock_unlock(&TASK->lock);
172
        interrupts_restore(ipl);
180
        interrupts_restore(ipl);
173
 
181
 
174
        udebug_wait_for_go(&THREAD->go_wq);
182
        udebug_wait_for_go(&THREAD->go_wq);
175
 
183
 
176
        goto restart;
184
        goto restart;
177
        /* must try again - have to lose stoppability atomically */
185
        /* must try again - have to lose stoppability atomically */
178
    } else {
186
    } else {
179
        ++TASK->not_stoppable_count;
187
        ++TASK->udebug.not_stoppable_count;
180
        THREAD->debug_stoppable = false;
188
        THREAD->debug_stoppable = false;
181
 
189
 
182
        spinlock_unlock(&THREAD->debug_lock);
190
        spinlock_unlock(&THREAD->debug_lock);
183
        spinlock_unlock(&TASK->lock);
191
        spinlock_unlock(&TASK->lock);
184
        interrupts_restore(ipl);
192
        interrupts_restore(ipl);
185
    }
193
    }
186
}
194
}
187
 
195
 
188
void udebug_syscall_event(unative_t a1, unative_t a2, unative_t a3,
196
void udebug_syscall_event(unative_t a1, unative_t a2, unative_t a3,
189
    unative_t a4, unative_t a5, unative_t a6, unative_t id, unative_t rc,
197
    unative_t a4, unative_t a5, unative_t a6, unative_t id, unative_t rc,
190
    bool end_variant)
198
    bool end_variant)
191
{
199
{
192
    call_t *call;
200
    call_t *call;
193
    ipl_t ipl;
201
    ipl_t ipl;
194
    udebug_event_t etype;
202
    udebug_event_t etype;
195
 
203
 
196
    etype = end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
204
    etype = end_variant ? UDEBUG_EVENT_SYSCALL_E : UDEBUG_EVENT_SYSCALL_B;
197
 
205
 
198
    ipl = interrupts_disable();
206
    ipl = interrupts_disable();
199
    spinlock_lock(&THREAD->debug_lock);
207
    spinlock_lock(&THREAD->debug_lock);
200
 
208
 
201
    /* Must only generate events when in debugging session and have go */
209
    /* Must only generate events when in debugging session and have go */
202
    if (THREAD->debug_active != true ||
210
    if (THREAD->debug_active != true ||
203
        THREAD->debug_stop == true ||
211
        THREAD->debug_stop == true ||
204
        (TASK->debug_evmask & UDEBUG_EVMASK(etype)) == 0) {
212
        (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
205
        spinlock_unlock(&THREAD->debug_lock);
213
        spinlock_unlock(&THREAD->debug_lock);
206
        interrupts_restore(ipl);
214
        interrupts_restore(ipl);
207
        return;
215
        return;
208
    }
216
    }
209
 
217
 
210
    //klog_printf("udebug_syscall_event");
218
    //klog_printf("udebug_syscall_event");
211
    call = THREAD->debug_go_call;
219
    call = THREAD->debug_go_call;
212
    IPC_SET_RETVAL(call->data, 0);
220
    IPC_SET_RETVAL(call->data, 0);
213
    IPC_SET_ARG1(call->data, etype);
221
    IPC_SET_ARG1(call->data, etype);
214
    IPC_SET_ARG2(call->data, id);
222
    IPC_SET_ARG2(call->data, id);
215
    IPC_SET_ARG3(call->data, rc);
223
    IPC_SET_ARG3(call->data, rc);
216
    //klog_printf("udebug_syscall_event/ipc_answer");
224
    //klog_printf("udebug_syscall_event/ipc_answer");
217
 
225
 
218
    THREAD->syscall_args[0] = a1;
226
    THREAD->syscall_args[0] = a1;
219
    THREAD->syscall_args[1] = a2;
227
    THREAD->syscall_args[1] = a2;
220
    THREAD->syscall_args[2] = a3;
228
    THREAD->syscall_args[2] = a3;
221
    THREAD->syscall_args[3] = a4;
229
    THREAD->syscall_args[3] = a4;
222
    THREAD->syscall_args[4] = a5;
230
    THREAD->syscall_args[4] = a5;
223
    THREAD->syscall_args[5] = a6;
231
    THREAD->syscall_args[5] = a6;
224
 
232
 
225
    /*
233
    /*
226
     * Make sure debug_stop is true when going to sleep
234
     * Make sure debug_stop is true when going to sleep
227
     * in case we get woken up by DEBUG_END. (At which
235
     * in case we get woken up by DEBUG_END. (At which
228
     * point it must be back to the initial true value).
236
     * point it must be back to the initial true value).
229
     */
237
     */
230
    THREAD->debug_stop = true;
238
    THREAD->debug_stop = true;
231
 
239
 
232
    THREAD->cur_event = etype;
240
    THREAD->cur_event = etype;
233
    spinlock_unlock(&THREAD->debug_lock);
241
    spinlock_unlock(&THREAD->debug_lock);
234
 
242
 
235
    spinlock_lock(&TASK->lock);
243
    spinlock_lock(&TASK->lock);
236
    ipc_answer(&TASK->answerbox, THREAD->debug_go_call);
244
    ipc_answer(&TASK->answerbox, THREAD->debug_go_call);
237
    spinlock_unlock(&TASK->lock);
245
    spinlock_unlock(&TASK->lock);
238
    interrupts_restore(ipl);
246
    interrupts_restore(ipl);
239
 
247
 
240
    udebug_wait_for_go(&THREAD->go_wq);
248
    udebug_wait_for_go(&THREAD->go_wq);
241
}
249
}
242
 
250
 
243
void udebug_thread_b_event(struct thread *t)
251
void udebug_thread_b_event(struct thread *t)
244
{
252
{
245
    call_t *call;
253
    call_t *call;
246
    ipl_t ipl;
254
    ipl_t ipl;
247
 
255
 
248
    ipl = interrupts_disable();
256
    ipl = interrupts_disable();
249
    spinlock_lock(&THREAD->debug_lock);
257
    spinlock_lock(&THREAD->debug_lock);
250
 
258
 
251
    klog_printf("udebug_thread_b_event");
259
    klog_printf("udebug_thread_b_event");
252
    klog_printf("- check state");
260
    klog_printf("- check state");
253
 
261
 
254
    /* Must only generate events when in debugging session */
262
    /* Must only generate events when in debugging session */
255
    if (THREAD->debug_active != true) {
263
    if (THREAD->debug_active != true) {
256
        klog_printf("- debug_active: %s, debug_stop: %s",
264
        klog_printf("- debug_active: %s, debug_stop: %s",
257
            THREAD->debug_active ? "yes(+)" : "no(-)",
265
            THREAD->debug_active ? "yes(+)" : "no(-)",
258
            THREAD->debug_stop ? "yes(-)" : "no(+)");
266
            THREAD->debug_stop ? "yes(-)" : "no(+)");
259
        spinlock_unlock(&THREAD->debug_lock);
267
        spinlock_unlock(&THREAD->debug_lock);
260
        interrupts_restore(ipl);
268
        interrupts_restore(ipl);
261
        return;
269
        return;
262
    }
270
    }
263
 
271
 
264
    klog_printf("- trigger event");
272
    klog_printf("- trigger event");
265
 
273
 
266
    call = THREAD->debug_go_call;
274
    call = THREAD->debug_go_call;
267
    IPC_SET_RETVAL(call->data, 0);
275
    IPC_SET_RETVAL(call->data, 0);
268
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_B);
276
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_B);
269
    IPC_SET_ARG2(call->data, (unative_t)t);
277
    IPC_SET_ARG2(call->data, (unative_t)t);
270
 
278
 
271
    /*
279
    /*
272
     * Make sure debug_stop is true when going to sleep
280
     * Make sure debug_stop is true when going to sleep
273
     * in case we get woken up by DEBUG_END. (At which
281
     * in case we get woken up by DEBUG_END. (At which
274
     * point it must be back to the initial true value).
282
     * point it must be back to the initial true value).
275
     */
283
     */
276
    THREAD->debug_stop = true;
284
    THREAD->debug_stop = true;
277
 
285
 
278
    THREAD->cur_event = UDEBUG_EVENT_THREAD_B;
286
    THREAD->cur_event = UDEBUG_EVENT_THREAD_B;
279
    spinlock_unlock(&THREAD->debug_lock);
287
    spinlock_unlock(&THREAD->debug_lock);
280
 
288
 
281
    spinlock_lock(&TASK->lock);
289
    spinlock_lock(&TASK->lock);
282
    ipc_answer(&TASK->answerbox, THREAD->debug_go_call);
290
    ipc_answer(&TASK->answerbox, THREAD->debug_go_call);
283
    spinlock_unlock(&TASK->lock);
291
    spinlock_unlock(&TASK->lock);
284
 
292
 
285
    interrupts_restore(ipl);
293
    interrupts_restore(ipl);
286
    klog_printf("- sleep");
294
    klog_printf("- sleep");
287
    udebug_wait_for_go(&THREAD->go_wq);
295
    udebug_wait_for_go(&THREAD->go_wq);
288
}
296
}
289
 
297
 
290
void udebug_thread_e_event(void)
298
void udebug_thread_e_event(void)
291
{
299
{
292
    call_t *call;
300
    call_t *call;
293
    ipl_t ipl;
301
    ipl_t ipl;
294
 
302
 
295
    ipl = interrupts_disable();
303
    ipl = interrupts_disable();
296
    spinlock_lock(&THREAD->debug_lock);
304
    spinlock_lock(&THREAD->debug_lock);
297
 
305
 
298
    klog_printf("udebug_thread_e_event");
306
    klog_printf("udebug_thread_e_event");
299
    klog_printf("- check state");
307
    klog_printf("- check state");
300
 
308
 
301
    /* Must only generate events when in debugging session */
309
    /* Must only generate events when in debugging session */
302
    if (THREAD->debug_active != true) {
310
    if (THREAD->debug_active != true) {
303
        klog_printf("- debug_active: %s, debug_stop: %s",
311
        klog_printf("- debug_active: %s, debug_stop: %s",
304
            THREAD->debug_active ? "yes(+)" : "no(-)",
312
            THREAD->debug_active ? "yes(+)" : "no(-)",
305
            THREAD->debug_stop ? "yes(-)" : "no(+)");
313
            THREAD->debug_stop ? "yes(-)" : "no(+)");
306
        spinlock_unlock(&THREAD->debug_lock);
314
        spinlock_unlock(&THREAD->debug_lock);
307
        interrupts_restore(ipl);
315
        interrupts_restore(ipl);
308
        return;
316
        return;
309
    }
317
    }
310
 
318
 
311
    klog_printf("- trigger event");
319
    klog_printf("- trigger event");
312
 
320
 
313
    call = THREAD->debug_go_call;
321
    call = THREAD->debug_go_call;
314
    IPC_SET_RETVAL(call->data, 0);
322
    IPC_SET_RETVAL(call->data, 0);
315
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_E);
323
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_THREAD_E);
316
 
324
 
317
    /* Prevent any further debug activity in thread */
325
    /* Prevent any further debug activity in thread */
318
    THREAD->debug_active = false;
326
    THREAD->debug_active = false;
319
    THREAD->cur_event = 0;      /* none */
327
    THREAD->cur_event = 0;      /* none */
320
    THREAD->debug_stop = true;  /* set to initial value */
328
    THREAD->debug_stop = true;  /* set to initial value */
321
    spinlock_unlock(&THREAD->debug_lock);
329
    spinlock_unlock(&THREAD->debug_lock);
322
 
330
 
323
    spinlock_lock(&TASK->lock);
331
    spinlock_lock(&TASK->lock);
324
    ipc_answer(&TASK->answerbox, THREAD->debug_go_call);
332
    ipc_answer(&TASK->answerbox, THREAD->debug_go_call);
325
    spinlock_unlock(&TASK->lock);
333
    spinlock_unlock(&TASK->lock);
326
 
334
 
327
    interrupts_restore(ipl);
335
    interrupts_restore(ipl);
328
 
336
 
329
    /* This event does not sleep - debugging has finished in this thread */
337
    /* This event does not sleep - debugging has finished in this thread */
330
}
338
}
331
 
339
 
332
static void breakpoint_trap_event(uintptr_t addr, udebug_event_t etype)
340
static void breakpoint_trap_event(uintptr_t addr, udebug_event_t etype)
333
{
341
{
334
    call_t *call;
342
    call_t *call;
335
    ipl_t ipl;
343
    ipl_t ipl;
336
 
344
 
337
    ipl = interrupts_disable();
345
    ipl = interrupts_disable();
338
    spinlock_lock(&THREAD->debug_lock);
346
    spinlock_lock(&THREAD->debug_lock);
339
 
347
 
340
    /* Must only generate events when in debugging session and have go */
348
    /* Must only generate events when in debugging session and have go */
341
    if (THREAD->debug_active != true ||
349
    if (THREAD->debug_active != true ||
342
        THREAD->debug_stop == true ||
350
        THREAD->debug_stop == true ||
343
        (TASK->debug_evmask & UDEBUG_EVMASK(etype)) == 0) {
351
        (TASK->udebug.evmask & UDEBUG_EVMASK(etype)) == 0) {
344
        spinlock_unlock(&THREAD->debug_lock);
352
        spinlock_unlock(&THREAD->debug_lock);
345
        interrupts_restore(ipl);
353
        interrupts_restore(ipl);
346
        return;
354
        return;
347
    }
355
    }
348
 
356
 
349
    klog_printf("udebug_breakpoint/trap_event");
357
    klog_printf("udebug_breakpoint/trap_event");
350
    call = THREAD->debug_go_call;
358
    call = THREAD->debug_go_call;
351
    IPC_SET_RETVAL(call->data, 0);
359
    IPC_SET_RETVAL(call->data, 0);
352
    IPC_SET_ARG1(call->data, etype);
360
    IPC_SET_ARG1(call->data, etype);
353
    IPC_SET_ARG2(call->data, addr);
361
    IPC_SET_ARG2(call->data, addr);
354
 
362
 
355
    /*
363
    /*
356
     * Make sure debug_stop is true when going to sleep
364
     * Make sure debug_stop is true when going to sleep
357
     * in case we get woken up by DEBUG_END. (At which
365
     * in case we get woken up by DEBUG_END. (At which
358
     * point it must be back to the initial true value).
366
     * point it must be back to the initial true value).
359
     */
367
     */
360
    THREAD->debug_stop = true;
368
    THREAD->debug_stop = true;
361
 
369
 
362
    THREAD->cur_event = etype;
370
    THREAD->cur_event = etype;
363
    spinlock_unlock(&THREAD->debug_lock);
371
    spinlock_unlock(&THREAD->debug_lock);
364
    klog_printf("- send answer");
372
    klog_printf("- send answer");
365
 
373
 
366
    spinlock_lock(&TASK->lock);
374
    spinlock_lock(&TASK->lock);
367
    ipc_answer(&TASK->answerbox, THREAD->debug_go_call);
375
    ipc_answer(&TASK->answerbox, THREAD->debug_go_call);
368
    spinlock_unlock(&TASK->lock);
376
    spinlock_unlock(&TASK->lock);
369
    interrupts_restore(ipl);
377
    interrupts_restore(ipl);
370
 
378
 
371
    udebug_wait_for_go(&THREAD->go_wq);
379
    udebug_wait_for_go(&THREAD->go_wq);
372
}
380
}
373
 
381
 
374
void udebug_breakpoint_event(uintptr_t addr)
382
void udebug_breakpoint_event(uintptr_t addr)
375
{
383
{
376
    breakpoint_trap_event(addr, UDEBUG_EVENT_BREAKPOINT);
384
    breakpoint_trap_event(addr, UDEBUG_EVENT_BREAKPOINT);
377
}
385
}
378
 
386
 
379
void udebug_trap_event(uintptr_t addr)
387
void udebug_trap_event(uintptr_t addr)
380
{
388
{
381
    breakpoint_trap_event(addr, UDEBUG_EVENT_TRAP);
389
    breakpoint_trap_event(addr, UDEBUG_EVENT_TRAP);
382
}
390
}
383
 
391
 
384
/**
392
/**
385
 * Terminate task debugging session.
393
 * Terminate task debugging session.
386
 *
394
 *
387
 * \param ta Must be already locked and interrupts must be disabled.
395
 * \param ta Must be already locked and interrupts must be disabled.
388
 * \return Zero on success or negative error code.
396
 * \return Zero on success or negative error code.
389
 */
397
 */
390
int udebug_task_cleanup(struct task *ta)
398
int udebug_task_cleanup(struct task *ta)
391
{
399
{
392
    thread_t *t;
400
    thread_t *t;
393
    link_t *cur;
401
    link_t *cur;
394
    int flags;
402
    int flags;
395
 
403
 
396
    klog_printf("udebug_task_cleanup()");
404
    klog_printf("udebug_task_cleanup()");
397
    klog_printf("task %llu", ta->taskid);
405
    klog_printf("task %llu", ta->taskid);
398
 
406
 
399
    if (ta->dt_state == UDEBUG_TS_BEGINNING &&
407
    if (ta->udebug.dt_state == UDEBUG_TS_BEGINNING &&
400
        ta->dt_state != UDEBUG_TS_ACTIVE) {
408
        ta->udebug.dt_state != UDEBUG_TS_ACTIVE) {
401
        klog_printf("udebug_task_cleanup(): task not being debugged");
409
        klog_printf("udebug_task_cleanup(): task not being debugged");
402
        return EINVAL;
410
        return EINVAL;
403
    }
411
    }
404
 
412
 
405
    /* Finish debugging of all userspace threads */
413
    /* Finish debugging of all userspace threads */
406
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
414
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
407
        t = list_get_instance(cur, thread_t, th_link);
415
        t = list_get_instance(cur, thread_t, th_link);
408
 
416
 
409
        spinlock_lock(&t->debug_lock);
417
        spinlock_lock(&t->debug_lock);
410
        spinlock_lock(&t->lock);
418
        spinlock_lock(&t->lock);
411
 
419
 
412
        flags = t->flags;
420
        flags = t->flags;
413
 
421
 
414
        spinlock_unlock(&t->lock);
422
        spinlock_unlock(&t->lock);
415
 
423
 
416
        /* Only process userspace threads */
424
        /* Only process userspace threads */
417
        if ((flags & THREAD_FLAG_USPACE) != 0) {
425
        if ((flags & THREAD_FLAG_USPACE) != 0) {
418
            /* Prevent any further debug activity in thread */
426
            /* Prevent any further debug activity in thread */
419
            t->debug_active = false;
427
            t->debug_active = false;
420
            t->cur_event = 0;   /* none */
428
            t->cur_event = 0;   /* none */
421
 
429
 
422
            /* Still has go? */
430
            /* Still has go? */
423
            if (t->debug_stop == false) {
431
            if (t->debug_stop == false) {
424
                /*
432
                /*
425
                * Yes, so clear go. As debug_active == false,
433
                * Yes, so clear go. As debug_active == false,
426
                 * this doesn't affect anything.
434
                 * this doesn't affect anything.
427
                 */
435
                 */
428
                t->debug_stop = true;  
436
                t->debug_stop = true;  
429
 
437
 
430
                /* Answer GO call */
438
                /* Answer GO call */
431
                klog_printf("answer GO call with EVENT_FINISHED");
439
                klog_printf("answer GO call with EVENT_FINISHED");
432
                IPC_SET_RETVAL(t->debug_go_call->data, 0);
440
                IPC_SET_RETVAL(t->debug_go_call->data, 0);
433
                IPC_SET_ARG1(t->debug_go_call->data, UDEBUG_EVENT_FINISHED);
441
                IPC_SET_ARG1(t->debug_go_call->data, UDEBUG_EVENT_FINISHED);
434
                ipc_answer(&ta->answerbox, t->debug_go_call);
442
                ipc_answer(&ta->answerbox, t->debug_go_call);
435
            } else {
443
            } else {
436
                /*
444
                /*
437
                 * Debug_stop is already at initial value.
445
                 * Debug_stop is already at initial value.
438
                 * Yet this means the thread needs waking up.
446
                 * Yet this means the thread needs waking up.
439
                 */
447
                 */
440
 
448
 
441
                /*
449
                /*
442
                 * t's lock must not be held when calling
450
                 * t's lock must not be held when calling
443
                 * waitq_wakeup.
451
                 * waitq_wakeup.
444
                 */
452
                 */
445
                waitq_wakeup(&t->go_wq, WAKEUP_FIRST);
453
                waitq_wakeup(&t->go_wq, WAKEUP_FIRST);
446
            }
454
            }
447
        }
455
        }
448
        spinlock_unlock(&t->debug_lock);
456
        spinlock_unlock(&t->debug_lock);
449
    }
457
    }
450
 
458
 
451
    ta->dt_state = UDEBUG_TS_INACTIVE;
459
    ta->udebug.dt_state = UDEBUG_TS_INACTIVE;
452
    ta->debugger = NULL;
460
    ta->udebug.debugger = NULL;
453
 
461
 
454
    return 0;
462
    return 0;
455
}
463
}
456
 
464
 
457
 
465
 
458
/** @}
466
/** @}
459
 */
467
 */
460
 
468