Subversion Repositories HelenOS

Rev

Rev 4393 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4393 Rev 4692
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("Debugging task %llu", TASK->taskid);
185
 
-
 
186
    mutex_lock(&TASK->udebug.lock);
185
    mutex_lock(&TASK->udebug.lock);
187
    LOG("debugging task %llu\n", TASK->taskid);
-
 
188
 
186
 
189
    if (TASK->udebug.dt_state != UDEBUG_TS_INACTIVE) {
187
    if (TASK->udebug.dt_state != UDEBUG_TS_INACTIVE) {
190
        mutex_unlock(&TASK->udebug.lock);
188
        mutex_unlock(&TASK->udebug.lock);
191
        LOG("udebug_begin(): busy error\n");
-
 
192
 
-
 
193
        return EBUSY;
189
        return EBUSY;
194
    }
190
    }
195
 
191
 
196
    TASK->udebug.dt_state = UDEBUG_TS_BEGINNING;
192
    TASK->udebug.dt_state = UDEBUG_TS_BEGINNING;
197
    TASK->udebug.begin_call = call;
193
    TASK->udebug.begin_call = call;
198
    TASK->udebug.debugger = call->sender;
194
    TASK->udebug.debugger = call->sender;
199
 
195
 
200
    if (TASK->udebug.not_stoppable_count == 0) {
196
    if (TASK->udebug.not_stoppable_count == 0) {
201
        TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
197
        TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
202
        TASK->udebug.begin_call = NULL;
198
        TASK->udebug.begin_call = NULL;
203
        reply = 1; /* immediate reply */
199
        reply = 1; /* immediate reply */
204
    } else {
200
    } else {
205
        reply = 0; /* no reply */
201
        reply = 0; /* no reply */
206
    }
202
    }
207
   
203
   
208
    /* Set udebug.active on all of the task's userspace threads. */
204
    /* Set udebug.active on all of the task's userspace threads. */
209
 
205
 
210
    for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
206
    for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
211
        t = list_get_instance(cur, thread_t, th_link);
207
        t = list_get_instance(cur, thread_t, th_link);
212
 
208
 
213
        mutex_lock(&t->udebug.lock);
209
        mutex_lock(&t->udebug.lock);
214
        if ((t->flags & THREAD_FLAG_USPACE) != 0)
210
        if ((t->flags & THREAD_FLAG_USPACE) != 0)
215
            t->udebug.active = true;
211
            t->udebug.active = true;
216
        mutex_unlock(&t->udebug.lock);
212
        mutex_unlock(&t->udebug.lock);
217
    }
213
    }
218
 
214
 
219
    mutex_unlock(&TASK->udebug.lock);
215
    mutex_unlock(&TASK->udebug.lock);
220
 
-
 
221
    LOG("udebug_begin() done (%s)\n",
-
 
222
        reply ? "reply" : "stoppability wait");
-
 
223
 
-
 
224
    return reply;
216
    return reply;
225
}
217
}
226
 
218
 
227
/** Finish debugging the current task.
219
/** Finish debugging the current task.
228
 *
220
 *
229
 * Closes the debugging session for the current task.
221
 * Closes the debugging session for the current task.
230
 * @return Zero on success or negative error code.
222
 * @return Zero on success or negative error code.
231
 */
223
 */
232
int udebug_end(void)
224
int udebug_end(void)
233
{
225
{
234
    int rc;
226
    int rc;
235
 
227
 
236
    LOG("udebug_end()\n");
228
    LOG("Task %" PRIu64, TASK->taskid);
237
 
229
 
238
    mutex_lock(&TASK->udebug.lock);
230
    mutex_lock(&TASK->udebug.lock);
239
    LOG("task %" PRIu64 "\n", TASK->taskid);
-
 
240
 
-
 
241
    rc = udebug_task_cleanup(TASK);
231
    rc = udebug_task_cleanup(TASK);
242
 
-
 
243
    mutex_unlock(&TASK->udebug.lock);
232
    mutex_unlock(&TASK->udebug.lock);
244
 
233
 
245
    return rc;
234
    return rc;
246
}
235
}
247
 
236
 
248
/** Set the event mask.
237
/** Set the event mask.
249
 *
238
 *
250
 * Sets the event mask that determines which events are enabled.
239
 * Sets the event mask that determines which events are enabled.
251
 *
240
 *
252
 * @param mask  Or combination of events that should be enabled.
241
 * @param mask  Or combination of events that should be enabled.
253
 * @return  Zero on success or negative error code.
242
 * @return  Zero on success or negative error code.
254
 */
243
 */
255
int udebug_set_evmask(udebug_evmask_t mask)
244
int udebug_set_evmask(udebug_evmask_t mask)
256
{
245
{
257
    LOG("udebug_set_mask()\n");
246
    LOG("mask = 0x%x", mask);
258
 
247
 
259
    mutex_lock(&TASK->udebug.lock);
248
    mutex_lock(&TASK->udebug.lock);
260
 
249
 
261
    if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
250
    if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
262
        mutex_unlock(&TASK->udebug.lock);
251
        mutex_unlock(&TASK->udebug.lock);
263
        LOG("udebug_set_mask(): not active debuging session\n");
-
 
264
 
-
 
265
        return EINVAL;
252
        return EINVAL;
266
    }
253
    }
267
 
254
 
268
    TASK->udebug.evmask = mask;
255
    TASK->udebug.evmask = mask;
269
 
-
 
270
    mutex_unlock(&TASK->udebug.lock);
256
    mutex_unlock(&TASK->udebug.lock);
271
 
257
 
272
    return 0;
258
    return 0;
273
}
259
}
274
 
260
 
275
/** Give thread GO.
261
/** Give thread GO.
276
 *
262
 *
277
 * Upon recieving a go message, the thread is given GO. Being GO
263
 * Upon recieving a go message, the thread is given GO. Being GO
278
 * means the thread is allowed to execute userspace code (until
264
 * means the thread is allowed to execute userspace code (until
279
 * a debugging event or STOP occurs, at which point the thread loses GO.
265
 * a debugging event or STOP occurs, at which point the thread loses GO.
280
 *
266
 *
281
 * @param t The thread to operate on (unlocked and need not be valid).
267
 * @param t The thread to operate on (unlocked and need not be valid).
282
 * @param call  The GO call that we are servicing.
268
 * @param call  The GO call that we are servicing.
283
 */
269
 */
284
int udebug_go(thread_t *t, call_t *call)
270
int udebug_go(thread_t *t, call_t *call)
285
{
271
{
286
    int rc;
272
    int rc;
287
 
273
 
288
    /* On success, this will lock t->udebug.lock. */
274
    /* On success, this will lock t->udebug.lock. */
289
    rc = _thread_op_begin(t, false);
275
    rc = _thread_op_begin(t, false);
290
    if (rc != EOK) {
276
    if (rc != EOK) {
291
        return rc;
277
        return rc;
292
    }
278
    }
293
 
279
 
294
    t->udebug.go_call = call;
280
    t->udebug.go_call = call;
295
    t->udebug.go = true;
281
    t->udebug.go = true;
296
    t->udebug.cur_event = 0;    /* none */
282
    t->udebug.cur_event = 0;    /* none */
297
 
283
 
298
    /*
284
    /*
299
     * Neither t's lock nor threads_lock may be held during wakeup.
285
     * Neither t's lock nor threads_lock may be held during wakeup.
300
     */
286
     */
301
    waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST);
287
    waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST);
302
 
288
 
303
    _thread_op_end(t);
289
    _thread_op_end(t);
304
 
290
 
305
    return 0;
291
    return 0;
306
}
292
}
307
 
293
 
308
/** Stop a thread (i.e. take its GO away)
294
/** Stop a thread (i.e. take its GO away)
309
 *
295
 *
310
 * Generates a STOP event as soon as the thread becomes stoppable (i.e.
296
 * Generates a STOP event as soon as the thread becomes stoppable (i.e.
311
 * can be considered stopped).
297
 * can be considered stopped).
312
 *
298
 *
313
 * @param t The thread to operate on (unlocked and need not be valid).
299
 * @param t The thread to operate on (unlocked and need not be valid).
314
 * @param call  The GO call that we are servicing.
300
 * @param call  The GO call that we are servicing.
315
 */
301
 */
316
int udebug_stop(thread_t *t, call_t *call)
302
int udebug_stop(thread_t *t, call_t *call)
317
{
303
{
318
    int rc;
304
    int rc;
319
 
305
 
320
    LOG("udebug_stop()\n");
306
    LOG("udebug_stop()");
321
 
307
 
322
    /*
308
    /*
323
     * On success, this will lock t->udebug.lock. Note that this makes sure
309
     * On success, this will lock t->udebug.lock. Note that this makes sure
324
     * the thread is not stopped.
310
     * the thread is not stopped.
325
     */
311
     */
326
    rc = _thread_op_begin(t, true);
312
    rc = _thread_op_begin(t, true);
327
    if (rc != EOK) {
313
    if (rc != EOK) {
328
        return rc;
314
        return rc;
329
    }
315
    }
330
 
316
 
331
    /* Take GO away from the thread. */
317
    /* Take GO away from the thread. */
332
    t->udebug.go = false;
318
    t->udebug.go = false;
333
 
319
 
334
    if (t->udebug.stoppable != true) {
320
    if (t->udebug.stoppable != true) {
335
        /* Answer will be sent when the thread becomes stoppable. */
321
        /* Answer will be sent when the thread becomes stoppable. */
336
        _thread_op_end(t);
322
        _thread_op_end(t);
337
        return 0;
323
        return 0;
338
    }
324
    }
339
 
325
 
340
    /*
326
    /*
341
     * Answer GO call.
327
     * Answer GO call.
342
     */
328
     */
343
    LOG("udebug_stop - answering go call\n");
-
 
344
 
329
 
345
    /* Make sure nobody takes this call away from us. */
330
    /* Make sure nobody takes this call away from us. */
346
    call = t->udebug.go_call;
331
    call = t->udebug.go_call;
347
    t->udebug.go_call = NULL;
332
    t->udebug.go_call = NULL;
348
 
333
 
349
    IPC_SET_RETVAL(call->data, 0);
334
    IPC_SET_RETVAL(call->data, 0);
350
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_STOP);
335
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_STOP);
351
    LOG("udebug_stop/ipc_answer\n");
-
 
352
 
336
 
353
    THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
337
    THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
354
 
338
 
355
    _thread_op_end(t);
339
    _thread_op_end(t);
356
 
340
 
357
    mutex_lock(&TASK->udebug.lock);
341
    mutex_lock(&TASK->udebug.lock);
358
    ipc_answer(&TASK->answerbox, call);
342
    ipc_answer(&TASK->answerbox, call);
359
    mutex_unlock(&TASK->udebug.lock);
343
    mutex_unlock(&TASK->udebug.lock);
360
 
344
 
361
    LOG("udebog_stop/done\n");
-
 
362
    return 0;
345
    return 0;
363
}
346
}
364
 
347
 
365
/** Read the list of userspace threads in the current task.
348
/** Read the list of userspace threads in the current task.
366
 *
349
 *
367
 * The list takes the form of a sequence of thread hashes (i.e. the pointers
350
 * 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
351
 * 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
352
 * a pointer to it written to @a buffer. The sequence of hashes is written
370
 * into this buffer.
353
 * into this buffer.
371
 *
354
 *
372
 * If the sequence is longer than @a buf_size bytes, only as much hashes
355
 * 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
356
 * as can fit are copied. The number of thread hashes copied is stored
374
 * in @a n.
357
 * in @a n.
375
 *
358
 *
376
 * The rationale for having @a buf_size is that this function is only
359
 * The rationale for having @a buf_size is that this function is only
377
 * used for servicing the THREAD_READ message, which always specifies
360
 * used for servicing the THREAD_READ message, which always specifies
378
 * a maximum size for the userspace buffer.
361
 * a maximum size for the userspace buffer.
379
 *
362
 *
380
 * @param buffer    The buffer for storing thread hashes.
363
 * @param buffer    The buffer for storing thread hashes.
381
 * @param buf_size  Buffer size in bytes.
364
 * @param buf_size  Buffer size in bytes.
382
 * @param n     The actual number of hashes copied will be stored here.
365
 * @param n     The actual number of hashes copied will be stored here.
383
 */
366
 */
384
int udebug_thread_read(void **buffer, size_t buf_size, size_t *n)
367
int udebug_thread_read(void **buffer, size_t buf_size, size_t *n)
385
{
368
{
386
    thread_t *t;
369
    thread_t *t;
387
    link_t *cur;
370
    link_t *cur;
388
    unative_t tid;
371
    unative_t tid;
389
    unsigned copied_ids;
372
    unsigned copied_ids;
390
    ipl_t ipl;
373
    ipl_t ipl;
391
    unative_t *id_buffer;
374
    unative_t *id_buffer;
392
    int flags;
375
    int flags;
393
    size_t max_ids;
376
    size_t max_ids;
394
 
377
 
395
    LOG("udebug_thread_read()\n");
378
    LOG("udebug_thread_read()");
396
 
379
 
397
    /* Allocate a buffer to hold thread IDs */
380
    /* Allocate a buffer to hold thread IDs */
398
    id_buffer = malloc(buf_size, 0);
381
    id_buffer = malloc(buf_size, 0);
399
 
382
 
400
    mutex_lock(&TASK->udebug.lock);
383
    mutex_lock(&TASK->udebug.lock);
401
 
384
 
402
    /* Verify task state */
385
    /* Verify task state */
403
    if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
386
    if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
404
        mutex_unlock(&TASK->udebug.lock);
387
        mutex_unlock(&TASK->udebug.lock);
405
        return EINVAL;
388
        return EINVAL;
406
    }
389
    }
407
 
390
 
408
    ipl = interrupts_disable();
391
    ipl = interrupts_disable();
409
    spinlock_lock(&TASK->lock);
392
    spinlock_lock(&TASK->lock);
410
    /* Copy down the thread IDs */
393
    /* Copy down the thread IDs */
411
 
394
 
412
    max_ids = buf_size / sizeof(unative_t);
395
    max_ids = buf_size / sizeof(unative_t);
413
    copied_ids = 0;
396
    copied_ids = 0;
414
 
397
 
415
    /* FIXME: make sure the thread isn't past debug shutdown... */
398
    /* FIXME: make sure the thread isn't past debug shutdown... */
416
    for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
399
    for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
417
        /* Do not write past end of buffer */
400
        /* Do not write past end of buffer */
418
        if (copied_ids >= max_ids) break;
401
        if (copied_ids >= max_ids) break;
419
 
402
 
420
        t = list_get_instance(cur, thread_t, th_link);
403
        t = list_get_instance(cur, thread_t, th_link);
421
 
404
 
422
        spinlock_lock(&t->lock);
405
        spinlock_lock(&t->lock);
423
        flags = t->flags;
406
        flags = t->flags;
424
        spinlock_unlock(&t->lock);
407
        spinlock_unlock(&t->lock);
425
 
408
 
426
        /* Not interested in kernel threads. */
409
        /* Not interested in kernel threads. */
427
        if ((flags & THREAD_FLAG_USPACE) != 0) {
410
        if ((flags & THREAD_FLAG_USPACE) != 0) {
428
            /* Using thread struct pointer as identification hash */
411
            /* Using thread struct pointer as identification hash */
429
            tid = (unative_t) t;
412
            tid = (unative_t) t;
430
            id_buffer[copied_ids++] = tid;
413
            id_buffer[copied_ids++] = tid;
431
        }
414
        }
432
    }
415
    }
433
 
416
 
434
    spinlock_unlock(&TASK->lock);
417
    spinlock_unlock(&TASK->lock);
435
    interrupts_restore(ipl);
418
    interrupts_restore(ipl);
436
 
419
 
437
    mutex_unlock(&TASK->udebug.lock);
420
    mutex_unlock(&TASK->udebug.lock);
438
 
421
 
439
    *buffer = id_buffer;
422
    *buffer = id_buffer;
440
    *n = copied_ids * sizeof(unative_t);
423
    *n = copied_ids * sizeof(unative_t);
441
 
424
 
442
    return 0;
425
    return 0;
443
}
426
}
444
 
427
 
445
/** Read the arguments of a system call.
428
/** Read the arguments of a system call.
446
 *
429
 *
447
 * The arguments of the system call being being executed are copied
430
 * 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.
431
 * 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
432
 * The size of the buffer is exactly such that it can hold the maximum number
450
 * of system-call arguments.
433
 * of system-call arguments.
451
 *
434
 *
452
 * Unless the thread is currently blocked in a SYSCALL_B or SYSCALL_E event,
435
 * Unless the thread is currently blocked in a SYSCALL_B or SYSCALL_E event,
453
 * this function will fail with an EINVAL error code.
436
 * this function will fail with an EINVAL error code.
454
 *
437
 *
455
 * @param buffer    The buffer for storing thread hashes.
438
 * @param buffer    The buffer for storing thread hashes.
456
 */
439
 */
457
int udebug_args_read(thread_t *t, void **buffer)
440
int udebug_args_read(thread_t *t, void **buffer)
458
{
441
{
459
    int rc;
442
    int rc;
460
    unative_t *arg_buffer;
443
    unative_t *arg_buffer;
461
 
444
 
462
    /* Prepare a buffer to hold the arguments. */
445
    /* Prepare a buffer to hold the arguments. */
463
    arg_buffer = malloc(6 * sizeof(unative_t), 0);
446
    arg_buffer = malloc(6 * sizeof(unative_t), 0);
464
 
447
 
465
    /* On success, this will lock t->udebug.lock. */
448
    /* On success, this will lock t->udebug.lock. */
466
    rc = _thread_op_begin(t, false);
449
    rc = _thread_op_begin(t, false);
467
    if (rc != EOK) {
450
    if (rc != EOK) {
468
        return rc;
451
        return rc;
469
    }
452
    }
470
 
453
 
471
    /* Additionally we need to verify that we are inside a syscall. */
454
    /* Additionally we need to verify that we are inside a syscall. */
472
    if (t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_B &&
455
    if (t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_B &&
473
        t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_E) {
456
        t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_E) {
474
        _thread_op_end(t);
457
        _thread_op_end(t);
475
        return EINVAL;
458
        return EINVAL;
476
    }
459
    }
477
 
460
 
478
    /* Copy to a local buffer before releasing the lock. */
461
    /* Copy to a local buffer before releasing the lock. */
479
    memcpy(arg_buffer, t->udebug.syscall_args, 6 * sizeof(unative_t));
462
    memcpy(arg_buffer, t->udebug.syscall_args, 6 * sizeof(unative_t));
480
 
463
 
481
    _thread_op_end(t);
464
    _thread_op_end(t);
482
 
465
 
483
    *buffer = arg_buffer;
466
    *buffer = arg_buffer;
484
    return 0;
467
    return 0;
485
}
468
}
486
 
469
 
487
int udebug_regs_read(thread_t *t, void *buffer)
470
int udebug_regs_read(thread_t *t, void *buffer)
488
{
471
{
489
    istate_t *state;
472
    istate_t *state;
490
    int rc;
473
    int rc;
491
 
474
 
492
    LOG("udebug_regs_read()");
475
    LOG("udebug_regs_read()");
493
 
476
 
494
    /* On success, this will lock t->udebug.lock */
477
    /* On success, this will lock t->udebug.lock */
495
    rc = _thread_op_begin(t, false);
478
    rc = _thread_op_begin(t, false);
496
    if (rc != EOK) {
479
    if (rc != EOK) {
497
        return rc;
480
        return rc;
498
    }
481
    }
499
 
482
 
500
    state = t->udebug.uspace_state;
483
    state = t->udebug.uspace_state;
501
    if (state == NULL) {
484
    if (state == NULL) {
502
        _thread_op_end(t);
485
        _thread_op_end(t);
503
        LOG("udebug_regs_read() - istate not available\n");
486
        LOG("udebug_regs_read() - istate not available\n");
504
        return EBUSY;
487
        return EBUSY;
505
    }
488
    }
506
 
489
 
507
    /* Copy to the allocated buffer */
490
    /* Copy to the allocated buffer */
508
    memcpy(buffer, state, sizeof(istate_t));
491
    memcpy(buffer, state, sizeof(istate_t));
509
 
492
 
510
    _thread_op_end(t);
493
    _thread_op_end(t);
511
 
494
 
512
    LOG("pc = 0x%" PRIp, istate_get_pc((istate_t *) buffer));
495
    LOG("pc = 0x%" PRIp, istate_get_pc((istate_t *) buffer));
513
 
496
 
514
    return 0;
497
    return 0;
515
}
498
}
516
 
499
 
517
int udebug_regs_write(thread_t *t, void *buffer)
500
int udebug_regs_write(thread_t *t, void *buffer)
518
{
501
{
519
    int rc;
502
    int rc;
520
    istate_t *state;
503
    istate_t *state;
521
 
504
 
522
    LOG("udebug_regs_write()\n");
505
    LOG("udebug_regs_write()\n");
523
 
506
 
524
    /* Try to change the thread's uspace_state */
507
    /* Try to change the thread's uspace_state */
525
 
508
 
526
    /* On success, this will lock t->udebug.lock */
509
    /* On success, this will lock t->udebug.lock */
527
    rc = _thread_op_begin(t, false);
510
    rc = _thread_op_begin(t, false);
528
    if (rc != EOK) {
511
    if (rc != EOK) {
529
        LOG("error locking thread\n");
512
        LOG("error locking thread\n");
530
        return rc;
513
        return rc;
531
    }
514
    }
532
 
515
 
533
    state = t->udebug.uspace_state;
516
    state = t->udebug.uspace_state;
534
    if (state == NULL) {
517
    if (state == NULL) {
535
        _thread_op_end(t);
518
        _thread_op_end(t);
536
        LOG("udebug_regs_write() - istate not available\n");
519
        LOG("udebug_regs_write() - istate not available\n");
537
        return EBUSY;
520
        return EBUSY;
538
    }
521
    }
539
 
522
 
540
    memcpy(t->udebug.uspace_state, buffer, sizeof(istate_t));
523
    memcpy(t->udebug.uspace_state, buffer, sizeof(istate_t));
541
 
524
 
542
    _thread_op_end(t);
525
    _thread_op_end(t);
543
 
526
 
544
    return 0;
527
    return 0;
545
}
528
}
546
 
529
 
547
/** Read the memory of the debugged task.
530
/** Read the memory of the debugged task.
548
 *
531
 *
549
 * Reads @a n bytes from the address space of the debugged task, starting
532
 * Reads @a n bytes from the address space of the debugged task, starting
550
 * from @a uspace_addr. The bytes are copied into an allocated buffer
533
 * from @a uspace_addr. The bytes are copied into an allocated buffer
551
 * and a pointer to it is written into @a buffer.
534
 * and a pointer to it is written into @a buffer.
552
 *
535
 *
553
 * @param uspace_addr   Address from where to start reading.
536
 * @param uspace_addr   Address from where to start reading.
554
 * @param n     Number of bytes to read.
537
 * @param n     Number of bytes to read.
555
 * @param buffer    For storing a pointer to the allocated buffer.
538
 * @param buffer    For storing a pointer to the allocated buffer.
556
 */
539
 */
557
int udebug_mem_read(unative_t uspace_addr, size_t n, void **buffer)
540
int udebug_mem_read(unative_t uspace_addr, size_t n, void **buffer)
558
{
541
{
559
    void *data_buffer;
542
    void *data_buffer;
560
    int rc;
543
    int rc;
561
 
544
 
562
    /* Verify task state */
545
    /* Verify task state */
563
    mutex_lock(&TASK->udebug.lock);
546
    mutex_lock(&TASK->udebug.lock);
564
 
547
 
565
    if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
548
    if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
566
        mutex_unlock(&TASK->udebug.lock);
549
        mutex_unlock(&TASK->udebug.lock);
567
        return EBUSY;
550
        return EBUSY;
568
    }
551
    }
569
 
552
 
570
    data_buffer = malloc(n, 0);
553
    data_buffer = malloc(n, 0);
571
 
554
 
572
    /* NOTE: this is not strictly from a syscall... but that shouldn't
555
    /* NOTE: this is not strictly from a syscall... but that shouldn't
573
     * be a problem */
556
     * be a problem */
574
    rc = copy_from_uspace(data_buffer, (void *)uspace_addr, n);
557
    rc = copy_from_uspace(data_buffer, (void *)uspace_addr, n);
575
    mutex_unlock(&TASK->udebug.lock);
558
    mutex_unlock(&TASK->udebug.lock);
576
 
559
 
577
    if (rc != 0) return rc;
560
    if (rc != 0) return rc;
578
 
561
 
579
    *buffer = data_buffer;
562
    *buffer = data_buffer;
580
    return 0;
563
    return 0;
581
}
564
}
582
 
565
 
583
int udebug_mem_write(unative_t uspace_addr, void *data, size_t n)
566
int udebug_mem_write(unative_t uspace_addr, void *data, size_t n)
584
{
567
{
585
    int rc;
568
    int rc;
586
 
569
 
587
    LOG("udebug_mem_write()\n");
570
    LOG("udebug_mem_write()\n");
588
 
571
 
589
    /* n must be positive */
572
    /* n must be positive */
590
    if (n < 1)
573
    if (n < 1)
591
        return EINVAL;
574
        return EINVAL;
592
 
575
 
593
    /* Verify task state */
576
    /* Verify task state */
594
    mutex_lock(&TASK->udebug.lock);
577
    mutex_lock(&TASK->udebug.lock);
595
 
578
 
596
    if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
579
    if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
597
        mutex_unlock(&TASK->udebug.lock);
580
        mutex_unlock(&TASK->udebug.lock);
598
        return EBUSY;
581
        return EBUSY;
599
    }
582
    }
600
   
583
   
601
    LOG("dst=%u, size=%u\n", uspace_addr, n);
584
    LOG("dst=%u, size=%u\n", uspace_addr, n);
602
 
585
 
603
    /* NOTE: this is not strictly from a syscall... but that shouldn't
586
    /* NOTE: this is not strictly from a syscall... but that shouldn't
604
     * be a problem */
587
     * be a problem */
605
//  rc = copy_to_uspace((void *)uspace_addr, data, n);
588
//  rc = copy_to_uspace((void *)uspace_addr, data, n);
606
//  if (rc) return rc;
589
//  if (rc) return rc;
607
 
590
 
608
    rc = as_debug_write(uspace_addr, data, n);
591
    rc = as_debug_write(uspace_addr, data, n);
609
   
592
   
610
    LOG("rc=%d\n", rc);
593
    LOG("rc=%d\n", rc);
611
 
594
 
612
    mutex_unlock(&TASK->udebug.lock);
595
    mutex_unlock(&TASK->udebug.lock);
613
 
596
 
614
    return rc;
597
    return rc;
615
}
598
}
616
 
599
 
617
/** @}
600
/** @}
618
 */
601
 */
619
 
602