Subversion Repositories HelenOS

Rev

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

Rev 4377 Rev 4393
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 operations.
35
 * @brief   Udebug operations.
36
 *
36
 *
37
 * Udebug operations on tasks and threads are implemented here. The
37
 * Udebug operations on tasks and threads are implemented here. The
38
 * functions defined here are called from the udebug_ipc module
38
 * functions defined here are called from the udebug_ipc module
39
 * when servicing udebug IPC messages.
39
 * when servicing udebug IPC messages.
40
 */
40
 */
41
 
41
 
42
#include <debug.h>
42
#include <debug.h>
43
#include <proc/task.h>
43
#include <proc/task.h>
44
#include <proc/thread.h>
44
#include <proc/thread.h>
45
#include <arch.h>
45
#include <arch.h>
46
#include <errno.h>
46
#include <errno.h>
47
#include <print.h>
47
#include <print.h>
48
#include <syscall/copy.h>
48
#include <syscall/copy.h>
49
#include <ipc/ipc.h>
49
#include <ipc/ipc.h>
50
#include <udebug/udebug.h>
50
#include <udebug/udebug.h>
51
#include <udebug/udebug_ops.h>
51
#include <udebug/udebug_ops.h>
52
 
52
 
53
/**
53
/**
54
 * Prepare a thread for a debugging operation.
54
 * Prepare a thread for a debugging operation.
55
 *
55
 *
56
 * Simply put, return thread t with t->udebug.lock held,
56
 * Simply put, return thread t with t->udebug.lock held,
57
 * but only if it verifies all conditions.
57
 * but only if it verifies all conditions.
58
 *
58
 *
59
 * Specifically, verifies that thread t exists, is a userspace thread,
59
 * Specifically, verifies that thread t exists, is a userspace thread,
60
 * and belongs to the current task (TASK). Verifies, that the thread
60
 * and belongs to the current task (TASK). Verifies, that the thread
61
 * is (or is not) go according to being_go (typically false).
61
 * is (or is not) go according to being_go (typically false).
62
 * It also locks t->udebug.lock, making sure that t->udebug.active
62
 * It also locks t->udebug.lock, making sure that t->udebug.active
63
 * is true - that the thread is in a valid debugging session.
63
 * is true - that the thread is in a valid debugging session.
64
 *
64
 *
65
 * With this verified and the t->udebug.lock mutex held, it is ensured
65
 * With this verified and the t->udebug.lock mutex held, it is ensured
66
 * that the thread cannot leave the debugging session, let alone cease
66
 * that the thread cannot leave the debugging session, let alone cease
67
 * to exist.
67
 * to exist.
68
 *
68
 *
69
 * In this function, holding the TASK->udebug.lock mutex prevents the
69
 * In this function, holding the TASK->udebug.lock mutex prevents the
70
 * thread from leaving the debugging session, while relaxing from
70
 * thread from leaving the debugging session, while relaxing from
71
 * the t->lock spinlock to the t->udebug.lock mutex.
71
 * the t->lock spinlock to the t->udebug.lock mutex.
72
 *
72
 *
73
 * @param t     Pointer, need not at all be valid.
73
 * @param t     Pointer, need not at all be valid.
74
 * @param being_go  Required thread state.
74
 * @param being_go  Required thread state.
75
 *
75
 *
76
 * Returns EOK if all went well, or an error code otherwise.
76
 * Returns EOK if all went well, or an error code otherwise.
77
 */
77
 */
78
static int _thread_op_begin(thread_t *t, bool being_go)
78
static int _thread_op_begin(thread_t *t, bool being_go)
79
{
79
{
80
    task_id_t taskid;
80
    task_id_t taskid;
81
    ipl_t ipl;
81
    ipl_t ipl;
82
 
82
 
83
    taskid = TASK->taskid;
83
    taskid = TASK->taskid;
84
 
84
 
85
    mutex_lock(&TASK->udebug.lock);
85
    mutex_lock(&TASK->udebug.lock);
86
 
86
 
87
    /* thread_exists() must be called with threads_lock held */
87
    /* thread_exists() must be called with threads_lock held */
88
    ipl = interrupts_disable();
88
    ipl = interrupts_disable();
89
    spinlock_lock(&threads_lock);
89
    spinlock_lock(&threads_lock);
90
 
90
 
91
    if (!thread_exists(t)) {
91
    if (!thread_exists(t)) {
92
        spinlock_unlock(&threads_lock);
92
        spinlock_unlock(&threads_lock);
93
        interrupts_restore(ipl);
93
        interrupts_restore(ipl);
94
        mutex_unlock(&TASK->udebug.lock);
94
        mutex_unlock(&TASK->udebug.lock);
95
        return ENOENT;
95
        return ENOENT;
96
    }
96
    }
97
 
97
 
98
    /* t->lock is enough to ensure the thread's existence */
98
    /* t->lock is enough to ensure the thread's existence */
99
    spinlock_lock(&t->lock);
99
    spinlock_lock(&t->lock);
100
    spinlock_unlock(&threads_lock);
100
    spinlock_unlock(&threads_lock);
101
 
101
 
102
    /* Verify that 't' is a userspace thread. */
102
    /* Verify that 't' is a userspace thread. */
103
    if ((t->flags & THREAD_FLAG_USPACE) == 0) {
103
    if ((t->flags & THREAD_FLAG_USPACE) == 0) {
104
        /* It's not, deny its existence */
104
        /* It's not, deny its existence */
105
        spinlock_unlock(&t->lock);
105
        spinlock_unlock(&t->lock);
106
        interrupts_restore(ipl);
106
        interrupts_restore(ipl);
107
        mutex_unlock(&TASK->udebug.lock);
107
        mutex_unlock(&TASK->udebug.lock);
108
        return ENOENT;
108
        return ENOENT;
109
    }
109
    }
110
 
110
 
111
    /* Verify debugging state. */
111
    /* Verify debugging state. */
112
    if (t->udebug.active != true) {
112
    if (t->udebug.active != true) {
113
        /* Not in debugging session or undesired GO state */
113
        /* Not in debugging session or undesired GO state */
114
        spinlock_unlock(&t->lock);
114
        spinlock_unlock(&t->lock);
115
        interrupts_restore(ipl);
115
        interrupts_restore(ipl);
116
        mutex_unlock(&TASK->udebug.lock);
116
        mutex_unlock(&TASK->udebug.lock);
117
        return ENOENT;
117
        return ENOENT;
118
    }
118
    }
119
 
119
 
120
    /*
120
    /*
121
     * Since the thread has active == true, TASK->udebug.lock
121
     * Since the thread has active == true, TASK->udebug.lock
122
     * is enough to ensure its existence and that active remains
122
     * is enough to ensure its existence and that active remains
123
     * true.
123
     * true.
124
     */
124
     */
125
    spinlock_unlock(&t->lock);
125
    spinlock_unlock(&t->lock);
126
    interrupts_restore(ipl);
126
    interrupts_restore(ipl);
127
 
127
 
128
    /* Only mutex TASK->udebug.lock left. */
128
    /* Only mutex TASK->udebug.lock left. */
129
   
129
   
130
    /* Now verify that the thread belongs to the current task. */
130
    /* Now verify that the thread belongs to the current task. */
131
    if (t->task != TASK) {
131
    if (t->task != TASK) {
132
        /* No such thread belonging this task*/
132
        /* No such thread belonging this task*/
133
        mutex_unlock(&TASK->udebug.lock);
133
        mutex_unlock(&TASK->udebug.lock);
134
        return ENOENT;
134
        return ENOENT;
135
    }
135
    }
136
 
136
 
137
    /*
137
    /*
138
     * Now we need to grab the thread's debug lock for synchronization
138
     * Now we need to grab the thread's debug lock for synchronization
139
     * of the threads stoppability/stop state.
139
     * of the threads stoppability/stop state.
140
     */
140
     */
141
    mutex_lock(&t->udebug.lock);
141
    mutex_lock(&t->udebug.lock);
142
 
142
 
143
    /* The big task mutex is no longer needed. */
143
    /* The big task mutex is no longer needed. */
144
    mutex_unlock(&TASK->udebug.lock);
144
    mutex_unlock(&TASK->udebug.lock);
145
 
145
 
146
    if (t->udebug.go != being_go) {
146
    if (t->udebug.go != being_go) {
147
        /* Not in debugging session or undesired GO state. */
147
        /* Not in debugging session or undesired GO state. */
148
        mutex_unlock(&t->udebug.lock);
148
        mutex_unlock(&t->udebug.lock);
149
        return EINVAL;
149
        return EINVAL;
150
    }
150
    }
151
 
151
 
152
    /* Only t->udebug.lock left. */
152
    /* Only t->udebug.lock left. */
153
 
153
 
154
    return EOK; /* All went well. */
154
    return EOK; /* All went well. */
155
}
155
}
156
 
156
 
157
/** End debugging operation on a thread. */
157
/** End debugging operation on a thread. */
158
static void _thread_op_end(thread_t *t)
158
static void _thread_op_end(thread_t *t)
159
{
159
{
160
    mutex_unlock(&t->udebug.lock);
160
    mutex_unlock(&t->udebug.lock);
161
}
161
}
162
 
162
 
163
/** Begin debugging the current task.
163
/** Begin debugging the current task.
164
 *
164
 *
165
 * Initiates a debugging session for the current task (and its threads).
165
 * Initiates a debugging session for the current task (and its threads).
166
 * When the debugging session has started a reply will be sent to the
166
 * When the debugging session has started a reply will be sent to the
167
 * UDEBUG_BEGIN call. This may happen immediately in this function if
167
 * UDEBUG_BEGIN call. This may happen immediately in this function if
168
 * all the threads in this task are stoppable at the moment and in this
168
 * all the threads in this task are stoppable at the moment and in this
169
 * case the function returns 1.
169
 * case the function returns 1.
170
 *
170
 *
171
 * Otherwise the function returns 0 and the reply will be sent as soon as
171
 * Otherwise the function returns 0 and the reply will be sent as soon as
172
 * all the threads become stoppable (i.e. they can be considered stopped).
172
 * all the threads become stoppable (i.e. they can be considered stopped).
173
 *
173
 *
174
 * @param call  The BEGIN call we are servicing.
174
 * @param call  The BEGIN call we are servicing.
175
 * @return  0 (OK, but not done yet), 1 (done) or negative error code.
175
 * @return  0 (OK, but not done yet), 1 (done) or negative error code.
176
 */
176
 */
177
int udebug_begin(call_t *call)
177
int udebug_begin(call_t *call)
178
{
178
{
179
    int reply;
179
    int reply;
180
 
180
 
181
    thread_t *t;
181
    thread_t *t;
182
    link_t *cur;
182
    link_t *cur;
183
 
183
 
184
    LOG("udebug_begin()\n");
184
    LOG("udebug_begin()\n");
185
 
185
 
186
    mutex_lock(&TASK->udebug.lock);
186
    mutex_lock(&TASK->udebug.lock);
187
    LOG("debugging task %llu\n", TASK->taskid);
187
    LOG("debugging task %llu\n", TASK->taskid);
188
 
188
 
189
    if (TASK->udebug.dt_state != UDEBUG_TS_INACTIVE) {
189
    if (TASK->udebug.dt_state != UDEBUG_TS_INACTIVE) {
190
        mutex_unlock(&TASK->udebug.lock);
190
        mutex_unlock(&TASK->udebug.lock);
191
        LOG("udebug_begin(): busy error\n");
191
        LOG("udebug_begin(): busy error\n");
192
 
192
 
193
        return EBUSY;
193
        return EBUSY;
194
    }
194
    }
195
 
195
 
196
    TASK->udebug.dt_state = UDEBUG_TS_BEGINNING;
196
    TASK->udebug.dt_state = UDEBUG_TS_BEGINNING;
197
    TASK->udebug.begin_call = call;
197
    TASK->udebug.begin_call = call;
198
    TASK->udebug.debugger = call->sender;
198
    TASK->udebug.debugger = call->sender;
199
 
199
 
200
    if (TASK->udebug.not_stoppable_count == 0) {
200
    if (TASK->udebug.not_stoppable_count == 0) {
201
        TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
201
        TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
202
        TASK->udebug.begin_call = NULL;
202
        TASK->udebug.begin_call = NULL;
203
        reply = 1; /* immediate reply */
203
        reply = 1; /* immediate reply */
204
    } else {
204
    } else {
205
        reply = 0; /* no reply */
205
        reply = 0; /* no reply */
206
    }
206
    }
207
   
207
   
208
    /* Set udebug.active on all of the task's userspace threads. */
208
    /* Set udebug.active on all of the task's userspace threads. */
209
 
209
 
210
    for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
210
    for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
211
        t = list_get_instance(cur, thread_t, th_link);
211
        t = list_get_instance(cur, thread_t, th_link);
212
 
212
 
213
        mutex_lock(&t->udebug.lock);
213
        mutex_lock(&t->udebug.lock);
214
        if ((t->flags & THREAD_FLAG_USPACE) != 0)
214
        if ((t->flags & THREAD_FLAG_USPACE) != 0)
215
            t->udebug.active = true;
215
            t->udebug.active = true;
216
        mutex_unlock(&t->udebug.lock);
216
        mutex_unlock(&t->udebug.lock);
217
    }
217
    }
218
 
218
 
219
    mutex_unlock(&TASK->udebug.lock);
219
    mutex_unlock(&TASK->udebug.lock);
220
 
220
 
221
    LOG("udebug_begin() done (%s)\n",
221
    LOG("udebug_begin() done (%s)\n",
222
        reply ? "reply" : "stoppability wait");
222
        reply ? "reply" : "stoppability wait");
223
 
223
 
224
    return reply;
224
    return reply;
225
}
225
}
226
 
226
 
227
/** Finish debugging the current task.
227
/** Finish debugging the current task.
228
 *
228
 *
229
 * Closes the debugging session for the current task.
229
 * Closes the debugging session for the current task.
230
 * @return Zero on success or negative error code.
230
 * @return Zero on success or negative error code.
231
 */
231
 */
232
int udebug_end(void)
232
int udebug_end(void)
233
{
233
{
234
    int rc;
234
    int rc;
235
 
235
 
236
    LOG("udebug_end()\n");
236
    LOG("udebug_end()\n");
237
 
237
 
238
    mutex_lock(&TASK->udebug.lock);
238
    mutex_lock(&TASK->udebug.lock);
239
    LOG("task %" PRIu64 "\n", TASK->taskid);
239
    LOG("task %" PRIu64 "\n", TASK->taskid);
240
 
240
 
241
    rc = udebug_task_cleanup(TASK);
241
    rc = udebug_task_cleanup(TASK);
242
 
242
 
243
    mutex_unlock(&TASK->udebug.lock);
243
    mutex_unlock(&TASK->udebug.lock);
244
 
244
 
245
    return rc;
245
    return rc;
246
}
246
}
247
 
247
 
248
/** Set the event mask.
248
/** Set the event mask.
249
 *
249
 *
250
 * Sets the event mask that determines which events are enabled.
250
 * Sets the event mask that determines which events are enabled.
251
 *
251
 *
252
 * @param mask  Or combination of events that should be enabled.
252
 * @param mask  Or combination of events that should be enabled.
253
 * @return  Zero on success or negative error code.
253
 * @return  Zero on success or negative error code.
254
 */
254
 */
255
int udebug_set_evmask(udebug_evmask_t mask)
255
int udebug_set_evmask(udebug_evmask_t mask)
256
{
256
{
257
    LOG("udebug_set_mask()\n");
257
    LOG("udebug_set_mask()\n");
258
 
258
 
259
    mutex_lock(&TASK->udebug.lock);
259
    mutex_lock(&TASK->udebug.lock);
260
 
260
 
261
    if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
261
    if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
262
        mutex_unlock(&TASK->udebug.lock);
262
        mutex_unlock(&TASK->udebug.lock);
263
        LOG("udebug_set_mask(): not active debuging session\n");
263
        LOG("udebug_set_mask(): not active debuging session\n");
264
 
264
 
265
        return EINVAL;
265
        return EINVAL;
266
    }
266
    }
267
 
267
 
268
    TASK->udebug.evmask = mask;
268
    TASK->udebug.evmask = mask;
269
 
269
 
270
    mutex_unlock(&TASK->udebug.lock);
270
    mutex_unlock(&TASK->udebug.lock);
271
 
271
 
272
    return 0;
272
    return 0;
273
}
273
}
274
 
274
 
275
/** Give thread GO.
275
/** Give thread GO.
276
 *
276
 *
277
 * Upon recieving a go message, the thread is given GO. Being GO
277
 * Upon recieving a go message, the thread is given GO. Being GO
278
 * means the thread is allowed to execute userspace code (until
278
 * means the thread is allowed to execute userspace code (until
279
 * a debugging event or STOP occurs, at which point the thread loses GO.
279
 * a debugging event or STOP occurs, at which point the thread loses GO.
280
 *
280
 *
281
 * @param t The thread to operate on (unlocked and need not be valid).
281
 * @param t The thread to operate on (unlocked and need not be valid).
282
 * @param call  The GO call that we are servicing.
282
 * @param call  The GO call that we are servicing.
283
 */
283
 */
284
int udebug_go(thread_t *t, call_t *call)
284
int udebug_go(thread_t *t, call_t *call)
285
{
285
{
286
    int rc;
286
    int rc;
287
 
287
 
288
    /* On success, this will lock t->udebug.lock. */
288
    /* On success, this will lock t->udebug.lock. */
289
    rc = _thread_op_begin(t, false);
289
    rc = _thread_op_begin(t, false);
290
    if (rc != EOK) {
290
    if (rc != EOK) {
291
        return rc;
291
        return rc;
292
    }
292
    }
293
 
293
 
294
    t->udebug.go_call = call;
294
    t->udebug.go_call = call;
295
    t->udebug.go = true;
295
    t->udebug.go = true;
296
    t->udebug.cur_event = 0;    /* none */
296
    t->udebug.cur_event = 0;    /* none */
297
 
297
 
298
    /*
298
    /*
299
     * Neither t's lock nor threads_lock may be held during wakeup.
299
     * Neither t's lock nor threads_lock may be held during wakeup.
300
     */
300
     */
301
    waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST);
301
    waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST);
302
 
302
 
303
    _thread_op_end(t);
303
    _thread_op_end(t);
304
 
304
 
305
    return 0;
305
    return 0;
306
}
306
}
307
 
307
 
308
/** Stop a thread (i.e. take its GO away)
308
/** Stop a thread (i.e. take its GO away)
309
 *
309
 *
310
 * Generates a STOP event as soon as the thread becomes stoppable (i.e.
310
 * Generates a STOP event as soon as the thread becomes stoppable (i.e.
311
 * can be considered stopped).
311
 * can be considered stopped).
312
 *
312
 *
313
 * @param t The thread to operate on (unlocked and need not be valid).
313
 * @param t The thread to operate on (unlocked and need not be valid).
314
 * @param call  The GO call that we are servicing.
314
 * @param call  The GO call that we are servicing.
315
 */
315
 */
316
int udebug_stop(thread_t *t, call_t *call)
316
int udebug_stop(thread_t *t, call_t *call)
317
{
317
{
318
    int rc;
318
    int rc;
319
 
319
 
320
    LOG("udebug_stop()\n");
320
    LOG("udebug_stop()\n");
321
 
321
 
322
    /*
322
    /*
323
     * On success, this will lock t->udebug.lock. Note that this makes sure
323
     * On success, this will lock t->udebug.lock. Note that this makes sure
324
     * the thread is not stopped.
324
     * the thread is not stopped.
325
     */
325
     */
326
    rc = _thread_op_begin(t, true);
326
    rc = _thread_op_begin(t, true);
327
    if (rc != EOK) {
327
    if (rc != EOK) {
328
        return rc;
328
        return rc;
329
    }
329
    }
330
 
330
 
331
    /* Take GO away from the thread. */
331
    /* Take GO away from the thread. */
332
    t->udebug.go = false;
332
    t->udebug.go = false;
333
 
333
 
334
    if (t->udebug.stoppable != true) {
334
    if (t->udebug.stoppable != true) {
335
        /* Answer will be sent when the thread becomes stoppable. */
335
        /* Answer will be sent when the thread becomes stoppable. */
336
        _thread_op_end(t);
336
        _thread_op_end(t);
337
        return 0;
337
        return 0;
338
    }
338
    }
339
 
339
 
340
    /*
340
    /*
341
     * Answer GO call.
341
     * Answer GO call.
342
     */
342
     */
343
    LOG("udebug_stop - answering go call\n");
343
    LOG("udebug_stop - answering go call\n");
344
 
344
 
345
    /* Make sure nobody takes this call away from us. */
345
    /* Make sure nobody takes this call away from us. */
346
    call = t->udebug.go_call;
346
    call = t->udebug.go_call;
347
    t->udebug.go_call = NULL;
347
    t->udebug.go_call = NULL;
348
 
348
 
349
    IPC_SET_RETVAL(call->data, 0);
349
    IPC_SET_RETVAL(call->data, 0);
350
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_STOP);
350
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_STOP);
351
    LOG("udebug_stop/ipc_answer\n");
351
    LOG("udebug_stop/ipc_answer\n");
352
 
352
 
353
    THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
353
    THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
354
 
354
 
355
    _thread_op_end(t);
355
    _thread_op_end(t);
356
 
356
 
357
    mutex_lock(&TASK->udebug.lock);
357
    mutex_lock(&TASK->udebug.lock);
358
    ipc_answer(&TASK->answerbox, call);
358
    ipc_answer(&TASK->answerbox, call);
359
    mutex_unlock(&TASK->udebug.lock);
359
    mutex_unlock(&TASK->udebug.lock);
360
 
360
 
361
    LOG("udebog_stop/done\n");
361
    LOG("udebog_stop/done\n");
362
    return 0;
362
    return 0;
363
}
363
}
364
 
364
 
365
/** Read the list of userspace threads in the current task.
365
/** Read the list of userspace threads in the current task.
366
 *
366
 *
367
 * The list takes the form of a sequence of thread hashes (i.e. the pointers
367
 * The list takes the form of a sequence of thread hashes (i.e. the pointers
368
 * to thread structures). A buffer of size @a buf_size is allocated and
368
 * to thread structures). A buffer of size @a buf_size is allocated and
369
 * a pointer to it written to @a buffer. The sequence of hashes is written
369
 * a pointer to it written to @a buffer. The sequence of hashes is written
370
 * into this buffer.
370
 * into this buffer.
371
 *
371
 *
372
 * If the sequence is longer than @a buf_size bytes, only as much hashes
372
 * If the sequence is longer than @a buf_size bytes, only as much hashes
373
 * as can fit are copied. The number of thread hashes copied is stored
373
 * as can fit are copied. The number of thread hashes copied is stored
374
 * in @a n.
374
 * in @a n.
375
 *
375
 *
376
 * The rationale for having @a buf_size is that this function is only
376
 * The rationale for having @a buf_size is that this function is only
377
 * used for servicing the THREAD_READ message, which always specifies
377
 * used for servicing the THREAD_READ message, which always specifies
378
 * a maximum size for the userspace buffer.
378
 * a maximum size for the userspace buffer.
379
 *
379
 *
380
 * @param buffer    The buffer for storing thread hashes.
380
 * @param buffer    The buffer for storing thread hashes.
381
 * @param buf_size  Buffer size in bytes.
381
 * @param buf_size  Buffer size in bytes.
382
 * @param n     The actual number of hashes copied will be stored here.
382
 * @param n     The actual number of hashes copied will be stored here.
383
 */
383
 */
384
int udebug_thread_read(void **buffer, size_t buf_size, size_t *n)
384
int udebug_thread_read(void **buffer, size_t buf_size, size_t *n)
385
{
385
{
386
    thread_t *t;
386
    thread_t *t;
387
    link_t *cur;
387
    link_t *cur;
388
    unative_t tid;
388
    unative_t tid;
389
    unsigned copied_ids;
389
    unsigned copied_ids;
390
    ipl_t ipl;
390
    ipl_t ipl;
391
    unative_t *id_buffer;
391
    unative_t *id_buffer;
392
    int flags;
392
    int flags;
393
    size_t max_ids;
393
    size_t max_ids;
394
 
394
 
395
    LOG("udebug_thread_read()\n");
395
    LOG("udebug_thread_read()\n");
396
 
396
 
397
    /* Allocate a buffer to hold thread IDs */
397
    /* Allocate a buffer to hold thread IDs */
398
    id_buffer = malloc(buf_size, 0);
398
    id_buffer = malloc(buf_size, 0);
399
 
399
 
400
    mutex_lock(&TASK->udebug.lock);
400
    mutex_lock(&TASK->udebug.lock);
401
 
401
 
402
    /* Verify task state */
402
    /* Verify task state */
403
    if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
403
    if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
404
        mutex_unlock(&TASK->udebug.lock);
404
        mutex_unlock(&TASK->udebug.lock);
405
        return EINVAL;
405
        return EINVAL;
406
    }
406
    }
407
 
407
 
408
    ipl = interrupts_disable();
408
    ipl = interrupts_disable();
409
    spinlock_lock(&TASK->lock);
409
    spinlock_lock(&TASK->lock);
410
    /* Copy down the thread IDs */
410
    /* Copy down the thread IDs */
411
 
411
 
412
    max_ids = buf_size / sizeof(unative_t);
412
    max_ids = buf_size / sizeof(unative_t);
413
    copied_ids = 0;
413
    copied_ids = 0;
414
 
414
 
415
    /* FIXME: make sure the thread isn't past debug shutdown... */
415
    /* FIXME: make sure the thread isn't past debug shutdown... */
416
    for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
416
    for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
417
        /* Do not write past end of buffer */
417
        /* Do not write past end of buffer */
418
        if (copied_ids >= max_ids) break;
418
        if (copied_ids >= max_ids) break;
419
 
419
 
420
        t = list_get_instance(cur, thread_t, th_link);
420
        t = list_get_instance(cur, thread_t, th_link);
421
 
421
 
422
        spinlock_lock(&t->lock);
422
        spinlock_lock(&t->lock);
423
        flags = t->flags;
423
        flags = t->flags;
424
        spinlock_unlock(&t->lock);
424
        spinlock_unlock(&t->lock);
425
 
425
 
426
        /* Not interested in kernel threads. */
426
        /* Not interested in kernel threads. */
427
        if ((flags & THREAD_FLAG_USPACE) != 0) {
427
        if ((flags & THREAD_FLAG_USPACE) != 0) {
428
            /* Using thread struct pointer as identification hash */
428
            /* Using thread struct pointer as identification hash */
429
            tid = (unative_t) t;
429
            tid = (unative_t) t;
430
            id_buffer[copied_ids++] = tid;
430
            id_buffer[copied_ids++] = tid;
431
        }
431
        }
432
    }
432
    }
433
 
433
 
434
    spinlock_unlock(&TASK->lock);
434
    spinlock_unlock(&TASK->lock);
435
    interrupts_restore(ipl);
435
    interrupts_restore(ipl);
436
 
436
 
437
    mutex_unlock(&TASK->udebug.lock);
437
    mutex_unlock(&TASK->udebug.lock);
438
 
438
 
439
    *buffer = id_buffer;
439
    *buffer = id_buffer;
440
    *n = copied_ids * sizeof(unative_t);
440
    *n = copied_ids * sizeof(unative_t);
441
 
441
 
442
    return 0;
442
    return 0;
443
}
443
}
444
 
444
 
445
/** Read the arguments of a system call.
445
/** Read the arguments of a system call.
446
 *
446
 *
447
 * The arguments of the system call being being executed are copied
447
 * The arguments of the system call being being executed are copied
448
 * to an allocated buffer and a pointer to it is written to @a buffer.
448
 * to an allocated buffer and a pointer to it is written to @a buffer.
449
 * The size of the buffer is exactly such that it can hold the maximum number
449
 * The size of the buffer is exactly such that it can hold the maximum number
450
 * of system-call arguments.
450
 * of system-call arguments.
451
 *
451
 *
452
 * Unless the thread is currently blocked in a SYSCALL_B or SYSCALL_E event,
452
 * Unless the thread is currently blocked in a SYSCALL_B or SYSCALL_E event,
453
 * this function will fail with an EINVAL error code.
453
 * this function will fail with an EINVAL error code.
454
 *
454
 *
455
 * @param buffer    The buffer for storing thread hashes.
455
 * @param buffer    The buffer for storing thread hashes.
456
 */
456
 */
457
int udebug_args_read(thread_t *t, void **buffer)
457
int udebug_args_read(thread_t *t, void **buffer)
458
{
458
{
459
    int rc;
459
    int rc;
460
    unative_t *arg_buffer;
460
    unative_t *arg_buffer;
461
 
461
 
462
    /* Prepare a buffer to hold the arguments. */
462
    /* Prepare a buffer to hold the arguments. */
463
    arg_buffer = malloc(6 * sizeof(unative_t), 0);
463
    arg_buffer = malloc(6 * sizeof(unative_t), 0);
464
 
464
 
465
    /* On success, this will lock t->udebug.lock. */
465
    /* On success, this will lock t->udebug.lock. */
466
    rc = _thread_op_begin(t, false);
466
    rc = _thread_op_begin(t, false);
467
    if (rc != EOK) {
467
    if (rc != EOK) {
468
        return rc;
468
        return rc;
469
    }
469
    }
470
 
470
 
471
    /* Additionally we need to verify that we are inside a syscall. */
471
    /* Additionally we need to verify that we are inside a syscall. */
472
    if (t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_B &&
472
    if (t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_B &&
473
        t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_E) {
473
        t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_E) {
474
        _thread_op_end(t);
474
        _thread_op_end(t);
475
        return EINVAL;
475
        return EINVAL;
476
    }
476
    }
477
 
477
 
478
    /* Copy to a local buffer before releasing the lock. */
478
    /* Copy to a local buffer before releasing the lock. */
479
    memcpy(arg_buffer, t->udebug.syscall_args, 6 * sizeof(unative_t));
479
    memcpy(arg_buffer, t->udebug.syscall_args, 6 * sizeof(unative_t));
480
 
480
 
481
    _thread_op_end(t);
481
    _thread_op_end(t);
482
 
482
 
483
    *buffer = arg_buffer;
483
    *buffer = arg_buffer;
484
    return 0;
484
    return 0;
485
}
485
}
486
 
486
 
487
int udebug_regs_read(thread_t *t, void *buffer)
487
int udebug_regs_read(thread_t *t, void *buffer)
488
{
488
{
489
    istate_t *state;
489
    istate_t *state;
490
    int rc;
490
    int rc;
491
 
491
 
492
//  printf("udebug_regs_read()\n");
492
    LOG("udebug_regs_read()");
493
 
493
 
494
    /* On success, this will lock t->udebug.lock */
494
    /* On success, this will lock t->udebug.lock */
495
    rc = _thread_op_begin(t, false);
495
    rc = _thread_op_begin(t, false);
496
    if (rc != EOK) {
496
    if (rc != EOK) {
497
        return rc;
497
        return rc;
498
    }
498
    }
499
 
499
 
500
    state = t->udebug.uspace_state;
500
    state = t->udebug.uspace_state;
501
    if (state == NULL) {
501
    if (state == NULL) {
502
        _thread_op_end(t);
502
        _thread_op_end(t);
503
        LOG("udebug_regs_read() - istate not available\n");
503
        LOG("udebug_regs_read() - istate not available\n");
504
        return EBUSY;
504
        return EBUSY;
505
    }
505
    }
506
 
506
 
507
    /* Copy to the allocated buffer */
507
    /* Copy to the allocated buffer */
508
    memcpy(buffer, state, sizeof(istate_t));
508
    memcpy(buffer, state, sizeof(istate_t));
509
 
509
 
510
    _thread_op_end(t);
510
    _thread_op_end(t);
511
 
511
 
-
 
512
    LOG("pc = 0x%" PRIp, istate_get_pc((istate_t *) buffer));
-
 
513
 
512
    return 0;
514
    return 0;
513
}
515
}
514
 
516
 
515
int udebug_regs_write(thread_t *t, void *buffer)
517
int udebug_regs_write(thread_t *t, void *buffer)
516
{
518
{
517
    int rc;
519
    int rc;
518
    istate_t *state;
520
    istate_t *state;
519
 
521
 
520
    LOG("udebug_regs_write()\n");
522
    LOG("udebug_regs_write()\n");
521
 
523
 
522
    /* Try to change the thread's uspace_state */
524
    /* Try to change the thread's uspace_state */
523
 
525
 
524
    /* On success, this will lock t->udebug.lock */
526
    /* On success, this will lock t->udebug.lock */
525
    rc = _thread_op_begin(t, false);
527
    rc = _thread_op_begin(t, false);
526
    if (rc != EOK) {
528
    if (rc != EOK) {
527
        LOG("error locking thread\n");
529
        LOG("error locking thread\n");
528
        return rc;
530
        return rc;
529
    }
531
    }
530
 
532
 
531
    state = t->udebug.uspace_state;
533
    state = t->udebug.uspace_state;
532
    if (state == NULL) {
534
    if (state == NULL) {
533
        _thread_op_end(t);
535
        _thread_op_end(t);
534
        LOG("udebug_regs_write() - istate not available\n");
536
        LOG("udebug_regs_write() - istate not available\n");
535
        return EBUSY;
537
        return EBUSY;
536
    }
538
    }
537
 
539
 
538
    memcpy(t->udebug.uspace_state, buffer, sizeof(istate_t));
540
    memcpy(t->udebug.uspace_state, buffer, sizeof(istate_t));
539
 
541
 
540
    _thread_op_end(t);
542
    _thread_op_end(t);
541
 
543
 
542
    return 0;
544
    return 0;
543
}
545
}
544
 
546
 
545
/** Read the memory of the debugged task.
547
/** Read the memory of the debugged task.
546
 *
548
 *
547
 * Reads @a n bytes from the address space of the debugged task, starting
549
 * Reads @a n bytes from the address space of the debugged task, starting
548
 * from @a uspace_addr. The bytes are copied into an allocated buffer
550
 * from @a uspace_addr. The bytes are copied into an allocated buffer
549
 * and a pointer to it is written into @a buffer.
551
 * and a pointer to it is written into @a buffer.
550
 *
552
 *
551
 * @param uspace_addr   Address from where to start reading.
553
 * @param uspace_addr   Address from where to start reading.
552
 * @param n     Number of bytes to read.
554
 * @param n     Number of bytes to read.
553
 * @param buffer    For storing a pointer to the allocated buffer.
555
 * @param buffer    For storing a pointer to the allocated buffer.
554
 */
556
 */
555
int udebug_mem_read(unative_t uspace_addr, size_t n, void **buffer)
557
int udebug_mem_read(unative_t uspace_addr, size_t n, void **buffer)
556
{
558
{
557
    void *data_buffer;
559
    void *data_buffer;
558
    int rc;
560
    int rc;
559
 
561
 
560
    /* Verify task state */
562
    /* Verify task state */
561
    mutex_lock(&TASK->udebug.lock);
563
    mutex_lock(&TASK->udebug.lock);
562
 
564
 
563
    if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
565
    if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
564
        mutex_unlock(&TASK->udebug.lock);
566
        mutex_unlock(&TASK->udebug.lock);
565
        return EBUSY;
567
        return EBUSY;
566
    }
568
    }
567
 
569
 
568
    data_buffer = malloc(n, 0);
570
    data_buffer = malloc(n, 0);
569
 
571
 
570
    /* NOTE: this is not strictly from a syscall... but that shouldn't
572
    /* NOTE: this is not strictly from a syscall... but that shouldn't
571
     * be a problem */
573
     * be a problem */
572
    rc = copy_from_uspace(data_buffer, (void *)uspace_addr, n);
574
    rc = copy_from_uspace(data_buffer, (void *)uspace_addr, n);
573
    mutex_unlock(&TASK->udebug.lock);
575
    mutex_unlock(&TASK->udebug.lock);
574
 
576
 
575
    if (rc != 0) return rc;
577
    if (rc != 0) return rc;
576
 
578
 
577
    *buffer = data_buffer;
579
    *buffer = data_buffer;
578
    return 0;
580
    return 0;
579
}
581
}
580
 
582
 
581
int udebug_mem_write(unative_t uspace_addr, void *data, size_t n)
583
int udebug_mem_write(unative_t uspace_addr, void *data, size_t n)
582
{
584
{
583
    int rc;
585
    int rc;
584
 
586
 
585
    LOG("udebug_mem_write()\n");
587
    LOG("udebug_mem_write()\n");
586
 
588
 
587
    /* n must be positive */
589
    /* n must be positive */
588
    if (n < 1)
590
    if (n < 1)
589
        return EINVAL;
591
        return EINVAL;
590
 
592
 
591
    /* Verify task state */
593
    /* Verify task state */
592
    mutex_lock(&TASK->udebug.lock);
594
    mutex_lock(&TASK->udebug.lock);
593
 
595
 
594
    if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
596
    if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
595
        mutex_unlock(&TASK->udebug.lock);
597
        mutex_unlock(&TASK->udebug.lock);
596
        return EBUSY;
598
        return EBUSY;
597
    }
599
    }
598
   
600
   
599
    LOG("dst=%u, size=%u\n", uspace_addr, n);
601
    LOG("dst=%u, size=%u\n", uspace_addr, n);
600
 
602
 
601
    /* NOTE: this is not strictly from a syscall... but that shouldn't
603
    /* NOTE: this is not strictly from a syscall... but that shouldn't
602
     * be a problem */
604
     * be a problem */
603
//  rc = copy_to_uspace((void *)uspace_addr, data, n);
605
//  rc = copy_to_uspace((void *)uspace_addr, data, n);
604
//  if (rc) return rc;
606
//  if (rc) return rc;
605
 
607
 
606
    rc = as_debug_write(uspace_addr, data, n);
608
    rc = as_debug_write(uspace_addr, data, n);
607
   
609
   
608
    LOG("rc=%d\n", rc);
610
    LOG("rc=%d\n", rc);
609
 
611
 
610
    mutex_unlock(&TASK->udebug.lock);
612
    mutex_unlock(&TASK->udebug.lock);
611
 
613
 
612
    return rc;
614
    return rc;
613
}
615
}
614
 
616
 
615
/** @}
617
/** @}
616
 */
618
 */
617
 
619