Subversion Repositories HelenOS

Rev

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

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