Subversion Repositories HelenOS

Rev

Rev 3016 | Rev 3026 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2894 svoboda 1
/*
2
 * Copyright (c) 2008 Jiri Svoboda
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
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
15
 *   derived from this software without specific prior written permission.
16
 *
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
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
2887 svoboda 29
/** @addtogroup generic
30
 * @{
31
 */
32
 
33
/**
34
 * @file
2894 svoboda 35
 * @brief   Udebug operations.
2887 svoboda 36
 */
37
 
38
#include <console/klog.h>
39
#include <proc/task.h>
40
#include <proc/thread.h>
41
#include <arch.h>
42
#include <errno.h>
43
#include <syscall/copy.h>
44
#include <ipc/ipc.h>
45
#include <udebug/udebug.h>
46
#include <udebug/udebug_ops.h>
47
 
48
/**
49
 * Prepare a thread for a debugging operation.
50
 *
3018 svoboda 51
 * Simply put, return thread t with t->udebug.lock held,
2887 svoboda 52
 * but only if it verifies all conditions.
53
 *
54
 * Specifically, verifies that thread t exists, is a userspace thread,
2898 svoboda 55
 * and belongs to the current task (TASK). Verifies, that the thread
56
 * has (or hasn't) go according to having_go (typically false).
3018 svoboda 57
 * It also locks t->udebug.lock, making sure that t->udebug.debug_active is true
2898 svoboda 58
 * - that the thread is in a valid debugging session.
2887 svoboda 59
 *
60
 * Returns EOK if all went well, or an error code otherwise.
61
 * Interrupts must be already disabled when calling this function.
62
 *
63
 * Note: This function sports complicated locking.
64
 */
2898 svoboda 65
static int _thread_op_begin(thread_t *t, bool having_go)
2887 svoboda 66
{
67
    int rc;
68
    task_id_t taskid;
69
 
70
    taskid = TASK->taskid;
71
 
72
    /* Must lock threads_lock to ensure continued existence of the thread */
73
    spinlock_lock(&threads_lock);
74
 
75
    if (!thread_exists(t)) {
76
        spinlock_unlock(&threads_lock);
77
        return ENOENT;
78
    }
79
 
3018 svoboda 80
    spinlock_lock(&t->udebug.lock);
2887 svoboda 81
    spinlock_lock(&t->lock);
82
 
83
    /* Now verify that it's the current task */
84
    if (t->task != TASK) {
85
        /* No such thread belonging to callee */
86
        rc = ENOENT;
87
        goto error_exit;
88
    }
89
 
90
    /* Verify that 't' is a userspace thread */
91
    if ((t->flags & THREAD_FLAG_USPACE) == 0) {
92
        /* It's not, deny its existence */
93
        rc = ENOENT;
94
        goto error_exit;
95
    }
96
 
3018 svoboda 97
    if ((t->udebug.debug_active != true) || (!t->udebug.stop != having_go)) {
2898 svoboda 98
        /* Not in debugging session or undesired GO state */
99
        rc = EINVAL;
2887 svoboda 100
        goto error_exit;
101
    }
102
 
103
    spinlock_unlock(&threads_lock);
104
    spinlock_unlock(&t->lock);
105
 
3018 svoboda 106
    /* Only t->udebug.lock left */
2887 svoboda 107
 
108
    return EOK; /* All went well */
109
 
110
 
111
    /* Executed when a check on the thread fails */
112
error_exit:
113
    spinlock_unlock(&t->lock);
3018 svoboda 114
    spinlock_unlock(&t->udebug.lock);
2887 svoboda 115
    spinlock_unlock(&threads_lock);
116
 
117
    /* No locks left here */
118
    return rc;  /* Some errors occured */
119
}
120
 
121
 
122
static void _thread_op_end(thread_t *t)
123
{
3018 svoboda 124
    spinlock_unlock(&t->udebug.lock);
2887 svoboda 125
}
126
 
127
/**
128
 * \return 0 (ok, but not done yet), 1 (done) or negative error code.
129
 */
130
int udebug_begin(call_t *call)
131
{
132
    ipl_t ipl;
133
    int reply;
134
 
135
    thread_t *t;
136
    link_t *cur;
137
 
138
    klog_printf("udebug_begin()");
139
 
3016 svoboda 140
    mutex_lock(&TASK->udebug.lock);
2887 svoboda 141
    klog_printf("debugging task %llu", TASK->taskid);
142
 
3014 svoboda 143
    if (TASK->udebug.dt_state != UDEBUG_TS_INACTIVE) {
3016 svoboda 144
        mutex_unlock(&TASK->udebug.lock);
2887 svoboda 145
        klog_printf("udebug_begin(): busy error");
146
 
147
        return EBUSY;
148
    }
149
 
3014 svoboda 150
    TASK->udebug.dt_state = UDEBUG_TS_BEGINNING;
151
    TASK->udebug.begin_call = call;
152
    TASK->udebug.debugger = call->sender;
2887 svoboda 153
 
3014 svoboda 154
    if (TASK->udebug.not_stoppable_count == 0) {
155
        TASK->udebug.dt_state = UDEBUG_TS_ACTIVE;
156
        TASK->udebug.begin_call = NULL;
2887 svoboda 157
        reply = 1; /* immediate reply */
158
    } else {
159
        reply = 0; /* no reply */
160
    }
161
 
3018 svoboda 162
    /* Set udebug.debug_active on all of the task's userspace threads */
2887 svoboda 163
 
164
    for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
165
        t = list_get_instance(cur, thread_t, th_link);
166
 
3016 svoboda 167
        ipl = interrupts_disable();
3018 svoboda 168
        spinlock_lock(&t->udebug.lock);
2887 svoboda 169
        if ((t->flags & THREAD_FLAG_USPACE) != 0)
3018 svoboda 170
            t->udebug.debug_active = true;
171
        spinlock_unlock(&t->udebug.lock);
3016 svoboda 172
        interrupts_restore(ipl);
2887 svoboda 173
    }
174
 
3016 svoboda 175
    mutex_unlock(&TASK->udebug.lock);
2887 svoboda 176
 
177
    klog_printf("udebug_begin() done (%s)",
178
        reply ? "reply" : "stoppability wait");
179
 
180
    return reply;
181
}
182
 
183
int udebug_end(void)
184
{
185
    int rc;
186
 
187
    klog_printf("udebug_end()");
188
 
3016 svoboda 189
    mutex_lock(&TASK->udebug.lock);
190
    klog_printf("task %llu", TASK->taskid);
2887 svoboda 191
 
192
    rc = udebug_task_cleanup(TASK);
193
 
3016 svoboda 194
    mutex_unlock(&TASK->udebug.lock);
2887 svoboda 195
 
3016 svoboda 196
    return rc;
2887 svoboda 197
}
198
 
2899 svoboda 199
int udebug_set_evmask(udebug_evmask_t mask)
200
{
201
    klog_printf("udebug_set_mask()");
202
 
203
    klog_printf("debugging task %llu", TASK->taskid);
204
 
3016 svoboda 205
    mutex_lock(&TASK->udebug.lock);
2899 svoboda 206
 
3014 svoboda 207
    if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
3016 svoboda 208
        mutex_unlock(&TASK->udebug.lock);
2899 svoboda 209
        klog_printf("udebug_set_mask(): not active debuging session");
210
 
211
        return EINVAL;
212
    }
213
 
3014 svoboda 214
    TASK->udebug.evmask = mask;
2899 svoboda 215
 
3016 svoboda 216
    mutex_unlock(&TASK->udebug.lock);
2899 svoboda 217
 
218
    return 0;
219
}
220
 
221
 
2887 svoboda 222
int udebug_go(thread_t *t, call_t *call)
223
{
224
    ipl_t ipl;
225
    int rc;
226
 
2913 svoboda 227
//  klog_printf("udebug_go()");
2887 svoboda 228
 
229
    ipl = interrupts_disable();
230
 
3018 svoboda 231
    /* On success, this will lock t->udebug.lock */
2898 svoboda 232
    rc = _thread_op_begin(t, false);
2887 svoboda 233
    if (rc != EOK) {
234
        interrupts_restore(ipl);
235
        return rc;
236
    }
237
 
3018 svoboda 238
    t->udebug.go_call = call;
239
    t->udebug.stop = false;
240
    t->udebug.cur_event = 0;    /* none */
2887 svoboda 241
 
242
    /*
243
     * Neither t's lock nor threads_lock may be held during wakeup
244
     */
3018 svoboda 245
    waitq_wakeup(&t->udebug.go_wq, WAKEUP_FIRST);
2887 svoboda 246
 
247
    _thread_op_end(t);
248
    interrupts_restore(ipl);
249
 
250
    return 0;
251
}
252
 
2898 svoboda 253
int udebug_stop(thread_t *t, call_t *call)
254
{
255
    ipl_t ipl;
256
    int rc;
2887 svoboda 257
 
2898 svoboda 258
    klog_printf("udebug_stop()");
3016 svoboda 259
    mutex_lock(&TASK->udebug.lock);
2898 svoboda 260
 
261
    ipl = interrupts_disable();
262
 
263
    /*
3018 svoboda 264
     * On success, this will lock t->udebug.lock. Note that this makes sure
2898 svoboda 265
     * the thread is not stopped.
266
     */
267
    rc = _thread_op_begin(t, true);
268
    if (rc != EOK) {
269
        interrupts_restore(ipl);
270
        return rc;
271
    }
272
 
273
    /* Take GO away from the thread */
3018 svoboda 274
    t->udebug.stop = true;
2898 svoboda 275
 
3018 svoboda 276
    if (!t->udebug.stoppable) {
2898 svoboda 277
        /* Answer will be sent when the thread becomes stoppable */
278
        _thread_op_end(t);
279
        interrupts_restore(ipl);
280
        return 0;
281
    }
282
 
283
    /*
284
     * Answer GO call
285
     */
286
    klog_printf("udebug_stop - answering go call");
287
 
288
    /* Make sure nobody takes this call away from us */
3018 svoboda 289
    call = t->udebug.go_call;
290
    t->udebug.go_call = NULL;
2898 svoboda 291
 
292
    IPC_SET_RETVAL(call->data, 0);
293
    IPC_SET_ARG1(call->data, UDEBUG_EVENT_STOP);
294
    klog_printf("udebug_stop/ipc_answer");
295
 
3018 svoboda 296
    THREAD->udebug.cur_event = UDEBUG_EVENT_STOP;
3016 svoboda 297
 
2898 svoboda 298
    _thread_op_end(t);
3016 svoboda 299
    interrupts_restore(ipl);
2898 svoboda 300
 
301
    ipc_answer(&TASK->answerbox, call);
3016 svoboda 302
    mutex_unlock(&TASK->udebug.lock);
2898 svoboda 303
 
304
    klog_printf("udebog_stop/done");
305
    return 0;
306
}
307
 
2897 svoboda 308
int udebug_thread_read(void **buffer, size_t buf_size, size_t *n)
2887 svoboda 309
{
310
    thread_t *t;
311
    link_t *cur;
312
    unative_t tid;
2897 svoboda 313
    unsigned copied_ids;
2887 svoboda 314
    ipl_t ipl;
315
    unative_t *id_buffer;
316
    int flags;
2897 svoboda 317
    size_t max_ids;
2887 svoboda 318
 
319
    klog_printf("udebug_thread_read()");
320
 
2897 svoboda 321
    /* Allocate a buffer to hold thread IDs */
322
    id_buffer = malloc(buf_size, 0);
323
 
3016 svoboda 324
    mutex_lock(&TASK->udebug.lock);
2887 svoboda 325
 
326
    /* Verify task state */
3014 svoboda 327
    if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
3016 svoboda 328
        mutex_unlock(&TASK->udebug.lock);
2887 svoboda 329
        return EINVAL;
330
    }
331
 
3016 svoboda 332
    ipl = interrupts_disable();
333
    spinlock_lock(&TASK->lock);
2897 svoboda 334
    /* Copy down the thread IDs */
2887 svoboda 335
 
2897 svoboda 336
    max_ids = buf_size / sizeof(unative_t);
337
    copied_ids = 0;
338
 
2887 svoboda 339
    for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
2897 svoboda 340
        /* Do not write past end of buffer */
341
        if (copied_ids >= max_ids) break;
2887 svoboda 342
 
343
        t = list_get_instance(cur, thread_t, th_link);
344
 
345
        spinlock_lock(&t->lock);
346
        flags = t->flags;
347
        spinlock_unlock(&t->lock);
348
 
349
        /* Not interested in kernel threads */
350
        if ((flags & THREAD_FLAG_USPACE) != 0) {
2897 svoboda 351
            /* Using thread struct pointer as identification hash */
2887 svoboda 352
            tid = (unative_t) t;
353
            id_buffer[copied_ids++] = tid;
354
        }
355
    }
356
 
357
    spinlock_unlock(&TASK->lock);
358
    interrupts_restore(ipl);
359
 
3016 svoboda 360
    mutex_unlock(&TASK->udebug.lock);
361
 
2887 svoboda 362
    *buffer = id_buffer;
363
    *n = copied_ids * sizeof(unative_t);
364
 
365
    return 0;
366
}
367
 
368
int udebug_args_read(thread_t *t, void **buffer)
369
{
370
    int rc;
371
    ipl_t ipl;
372
    unative_t *arg_buffer;
373
 
374
    klog_printf("udebug_args_read()");
375
 
2896 svoboda 376
    /* Prepare a buffer to hold the arguments */
377
    arg_buffer = malloc(6 * sizeof(unative_t), 0);
378
 
2887 svoboda 379
    ipl = interrupts_disable();
380
 
3018 svoboda 381
    /* On success, this will lock t->udebug.lock */
2898 svoboda 382
    rc = _thread_op_begin(t, false);
2887 svoboda 383
    if (rc != EOK) {
384
        interrupts_restore(ipl);
385
        return rc;
386
    }
387
 
388
    /* Additionally we need to verify that we are inside a syscall */
3018 svoboda 389
    if (t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_B &&
390
        t->udebug.cur_event != UDEBUG_EVENT_SYSCALL_E) {
2887 svoboda 391
        _thread_op_end(t);
392
        interrupts_restore(ipl);
393
 
394
        return EINVAL;
395
    }
396
 
397
    /* Copy to a local buffer before releasing the lock */
3018 svoboda 398
    memcpy(arg_buffer, t->udebug.syscall_args, 6 * sizeof(unative_t));
2887 svoboda 399
 
400
    _thread_op_end(t);
401
    interrupts_restore(ipl);
402
 
403
    *buffer = arg_buffer;
404
    return 0;
405
}
406
 
2919 svoboda 407
int udebug_regs_read(thread_t *t, void *buffer)
2887 svoboda 408
{
409
    istate_t *state;
410
    int rc;
411
    ipl_t ipl;
412
 
413
    klog_printf("udebug_regs_read()");
414
 
415
    ipl = interrupts_disable();
416
 
3018 svoboda 417
    /* On success, this will lock t->udebug.lock */
2898 svoboda 418
    rc = _thread_op_begin(t, false);
2887 svoboda 419
    if (rc != EOK) {
420
        interrupts_restore(ipl);
421
        return rc;
422
    }
423
 
3018 svoboda 424
    state = t->udebug.uspace_state;
2887 svoboda 425
    if (state == NULL) {
426
        _thread_op_end(t);
427
        interrupts_restore(ipl);
428
        klog_printf("udebug_regs_read() - istate not available");
429
        return EBUSY;
430
    }
431
 
2896 svoboda 432
    /* Copy to the allocated buffer */
2919 svoboda 433
    memcpy(buffer, state, sizeof(istate_t));
2887 svoboda 434
 
435
    _thread_op_end(t);
436
    interrupts_restore(ipl);
437
 
438
    return 0;
439
}
440
 
441
int udebug_regs_write(thread_t *t, void *buffer)
442
{
443
    int rc;
444
    istate_t *state;
445
    ipl_t ipl;
446
 
447
    klog_printf("udebug_regs_write()");
448
 
449
    /* Try to change the thread's uspace_state */
450
 
451
    ipl = interrupts_disable();
452
 
3018 svoboda 453
    /* On success, this will lock t->udebug.lock */
2898 svoboda 454
    rc = _thread_op_begin(t, false);
2887 svoboda 455
    if (rc != EOK) {
2919 svoboda 456
        klog_printf("error locking thread");
2887 svoboda 457
        interrupts_restore(ipl);
458
        return rc;
459
    }
460
 
3018 svoboda 461
    state = t->udebug.uspace_state;
2887 svoboda 462
    if (state == NULL) {
463
        _thread_op_end(t);
464
        interrupts_restore(ipl);
465
        klog_printf("udebug_regs_write() - istate not available");
466
 
467
        return EBUSY;
468
    }
469
 
3018 svoboda 470
    memcpy(t->udebug.uspace_state, buffer, sizeof(istate_t));
2887 svoboda 471
 
472
    _thread_op_end(t);
473
    interrupts_restore(ipl);
474
 
475
    return 0;
476
}
477
 
478
 
479
int udebug_mem_read(unative_t uspace_addr, size_t n, void **buffer)
480
{
481
    void *data_buffer;
482
    int rc;
483
 
3016 svoboda 484
    /* Verify task state */
485
    mutex_lock(&TASK->udebug.lock);
2887 svoboda 486
 
3016 svoboda 487
    if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
488
        mutex_unlock(&TASK->udebug.lock);
489
        return EBUSY;
490
    }
491
 
2896 svoboda 492
    data_buffer = malloc(n, 0);
493
 
3016 svoboda 494
//  klog_printf("udebug_mem_read: src=%u, size=%u", uspace_addr, n);
2887 svoboda 495
 
496
    /* NOTE: this is not strictly from a syscall... but that shouldn't
497
     * be a problem */
498
    rc = copy_from_uspace(data_buffer, (void *)uspace_addr, n);
3016 svoboda 499
    mutex_unlock(&TASK->udebug.lock);
2887 svoboda 500
 
3016 svoboda 501
    if (rc != 0) return rc;
502
 
2887 svoboda 503
    *buffer = data_buffer;
504
    return 0;
505
}
506
 
507
int udebug_mem_write(unative_t uspace_addr, void *data, size_t n)
508
{
509
    int rc;
510
 
511
    klog_printf("udebug_mem_write()");
512
 
513
    /* Verify task state */
3016 svoboda 514
    mutex_lock(&TASK->udebug.lock);
2887 svoboda 515
 
3016 svoboda 516
    if (TASK->udebug.dt_state != UDEBUG_TS_ACTIVE) {
517
        mutex_unlock(&TASK->udebug.lock);
2887 svoboda 518
        return EBUSY;
3016 svoboda 519
    }
2887 svoboda 520
 
521
    klog_printf("dst=%u, size=%u", uspace_addr, n);
522
 
523
    /* NOTE: this is not strictly from a syscall... but that shouldn't
524
     * be a problem */
3013 svoboda 525
//  rc = copy_to_uspace((void *)uspace_addr, data, n);
526
//  if (rc) return rc;
2887 svoboda 527
 
3013 svoboda 528
    rc = as_debug_write(uspace_addr, data, n);
529
 
3016 svoboda 530
    mutex_unlock(&TASK->udebug.lock);
531
 
3013 svoboda 532
    return rc;
2887 svoboda 533
}
534
 
535
/** @}
536
 */