Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
2911 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
 
29
/** @addtogroup debug
30
 * @{
31
 */
32
/** @file
33
 */
34
 
35
#include <stdio.h>
36
#include <stdlib.h>
37
#include <unistd.h>
38
#include <syscall.h>
39
#include <ipc/ipc.h>
40
#include <fibril.h>
41
#include <errno.h>
42
#include <udebug.h>
43
#include <async.h>
44
#include <string.h>
45
 
46
#include "cmd.h"
2936 svoboda 47
#include "cons.h"
2938 svoboda 48
#include "dthread.h"
3005 svoboda 49
#include "breakpoint.h"
2923 svoboda 50
#include "include/arch.h"
2935 svoboda 51
#include "fib_synch.h"
2915 svoboda 52
#include "main.h"
2911 svoboda 53
 
54
void thread_debug_start(unsigned thread_hash);
55
 
2936 svoboda 56
#define IN_BUF_SIZE 64
57
static char in_buf[IN_BUF_SIZE];
2911 svoboda 58
 
59
#define MAX_ARGC 10
60
int cmd_argc;
61
char *cmd_argv[MAX_ARGC + 1];   /* need one spare field for cmd_split() */
62
 
2937 svoboda 63
 
2911 svoboda 64
int next_thread_id;
65
 
66
int app_phone;
67
volatile bool abort_debug;
68
 
69
volatile int paused;
70
 
2935 svoboda 71
fcv_t go_cv;
72
 
2938 svoboda 73
static void command_split(char *cmd_str)
2911 svoboda 74
{
75
    char *p = cmd_str;
76
 
77
    if (*p == '\0') {
78
        cmd_argc = 0;
79
        return;
80
    }
81
 
82
    cmd_argc = 1;
83
    cmd_argv[0] = p;
84
 
85
    while (*p != '\0') {
86
        if (*p == ' ') {
87
            cmd_argv[cmd_argc++] = p + 1;
88
            *p = '\0';
89
        }
90
        ++p;
91
    }
92
}
93
 
2938 svoboda 94
static void command_run(void)
2911 svoboda 95
{
96
    int i;
97
    int cmp_len;
98
    int len;
99
 
100
    int idx_found;
101
    int num_found;
102
 
103
    len = strlen(cmd_argv[0]);
104
    cmp_len = 1;
105
 
2946 svoboda 106
    /* Silence warnings */
107
    num_found = 0;
108
    idx_found = 0;
109
 
2911 svoboda 110
    while (cmp_len <= len + 1) {
111
 
112
        num_found = 0;
113
        i = 0;
114
        while (cmd_table[i].name != NULL) {
115
            if (strncmp(cmd_table[i].name, cmd_argv[0], cmp_len) == 0) {
116
                idx_found = i;
117
                ++num_found;
118
            }
119
            ++i;
120
        }
121
 
122
        if (num_found < 2) break;
123
 
2947 svoboda 124
        ++cmp_len;
2911 svoboda 125
    }
126
 
127
    if (num_found == 0) {
2936 svoboda 128
        cons_printf("Unknown command. Try one of:\n");
2911 svoboda 129
        cmd_help(0, NULL);
130
        return;
131
    }
132
 
133
    if (cmd_argc - 1 != cmd_table[idx_found].argc) {
2936 svoboda 134
        cons_printf("Command '%s' expects %d arguments\n",
2911 svoboda 135
        cmd_table[idx_found].name, cmd_table[idx_found].argc);
136
        return;
137
    }
138
 
139
    (*cmd_table[idx_found].proc)(cmd_argc, cmd_argv);
140
}
141
 
2938 svoboda 142
static void thread_stop(void)
2936 svoboda 143
{
2939 svoboda 144
    dthread_t *dt;
2940 svoboda 145
    uintptr_t pc;
2939 svoboda 146
 
147
    dt = dthread_get();
2940 svoboda 148
    pc = dthread_get_pc(dt);
149
    cons_printf("[thread %d] stopped at 0x%lx\n", dt->id, pc);
2939 svoboda 150
    dthread_stop_me();
151
    cons_printf("[thread %d] go\n", dt->id);
2936 svoboda 152
}
153
 
2935 svoboda 154
/*
155
 * Called by a fibril (from arch code) when a breakpoint is hit.
156
 */
3005 svoboda 157
void breakpoint_hit(breakpoint_t *b)
2935 svoboda 158
{
3005 svoboda 159
    dthread_t *dt;
160
 
161
    dt = dthread_get();
162
    cons_printf("Thread %d hit breakpoint %d at 0x%lx\n",
163
        dt->id, b->id, b->addr);
2936 svoboda 164
    thread_stop();
2935 svoboda 165
}
2911 svoboda 166
 
2942 svoboda 167
/*
168
 * Called by a fibril (from arch code) when a single instruction
169
 * in singlestep is executed
170
 */
171
void singlestep_hit(void)
172
{
173
    cons_printf("singlestep hit\n");
174
    thread_stop();
175
}
176
 
2938 svoboda 177
static int task_connect(int taskid)
2911 svoboda 178
{
179
    int rc;
2918 svoboda 180
    unsigned evmask;
2911 svoboda 181
 
2936 svoboda 182
    cons_printf("ipc_connect_kbox(%d)... ", taskid);
2911 svoboda 183
    rc = ipc_connect_kbox(taskid);
2936 svoboda 184
    cons_printf("-> %d\n", rc);
2911 svoboda 185
    app_phone = rc;
186
    if (rc < 0) return rc;
187
 
2936 svoboda 188
    cons_printf("udebug_begin()... ");
2911 svoboda 189
    rc = udebug_begin(app_phone);
2936 svoboda 190
    cons_printf("-> %d\n", rc);
2911 svoboda 191
    if (rc < 0) return rc;
192
 
2918 svoboda 193
    evmask = UDEBUG_EM_ALL & ~(UDEBUG_EM_SYSCALL_B | UDEBUG_EM_SYSCALL_E);
2936 svoboda 194
    cons_printf("udebug_set_evmask(0x%x)... ", evmask);
2918 svoboda 195
    rc = udebug_set_evmask(app_phone, evmask);
2936 svoboda 196
    cons_printf("-> %d\n", rc);
2911 svoboda 197
    if (rc < 0) return rc;
198
 
199
    return 0;
200
}
201
 
2940 svoboda 202
#define THASH_BUF_INIT_LENGTH 32
203
 
204
static int get_thread_list(thash_t **thash_buf_ptr, int *n)
2911 svoboda 205
{
206
    int rc;
2946 svoboda 207
    size_t tb_copied;
208
    size_t tb_needed;
2911 svoboda 209
    int i;
2940 svoboda 210
    size_t tb_size;
211
    thash_t *thash_buf;
2911 svoboda 212
 
2940 svoboda 213
    tb_size = THASH_BUF_INIT_LENGTH * sizeof(thash_t);
214
    thash_buf = malloc(tb_size);
215
 
2946 svoboda 216
    rc = udebug_thread_read(app_phone, thash_buf,
2940 svoboda 217
        tb_size, &tb_copied, &tb_needed);
2911 svoboda 218
    if (rc < 0) return rc;
219
 
2940 svoboda 220
    if (tb_needed > tb_size) {
221
        /* Larger buffer needed  */
2911 svoboda 222
 
2940 svoboda 223
        free(thash_buf);
224
 
225
        tb_size = tb_needed;
226
        thash_buf = malloc(tb_size);
227
 
228
        if (!thash_buf) {
229
            printf("malloc failed\n");
230
            exit(1);
231
        }
232
 
233
        /* Try again */
234
 
2946 svoboda 235
        rc = udebug_thread_read(app_phone, thash_buf,
2940 svoboda 236
            tb_size, &tb_copied, &tb_needed);
237
 
238
        if (rc < 0) return rc;
2911 svoboda 239
    }
240
 
2940 svoboda 241
    *n = tb_copied / sizeof(thash_t);
242
 
243
    cons_printf("thread hashes:");
244
 
245
    for (i = 0; i < *n; ++i) {
246
        cons_printf("0x%x\n", thash_buf[i]);
247
    }
248
 
249
    cons_printf("Total of %u threads\n", *n);
250
 
251
    *thash_buf_ptr = thash_buf;
252
 
253
    return 0;
2911 svoboda 254
}
255
 
2946 svoboda 256
static void event_thread_b(unsigned hash)
2911 svoboda 257
{
258
    async_serialize_start();
2936 svoboda 259
    cons_printf("new thread, hash 0x%x\n", hash);
2911 svoboda 260
    async_serialize_end();
261
 
262
    thread_debug_start(hash);
263
}
264
 
2923 svoboda 265
static void debug_event(thash_t thash, udebug_event_t ev_type, sysarg_t val0)
266
{
267
    switch (ev_type) {
268
    case UDEBUG_EVENT_STOP:
2936 svoboda 269
        cons_printf("stop event\n");
2939 svoboda 270
        thread_stop();
2923 svoboda 271
        break;
272
    case UDEBUG_EVENT_THREAD_B:
273
        event_thread_b(val0);
274
        break;
275
    case UDEBUG_EVENT_THREAD_E:
2936 svoboda 276
        cons_printf("thread 0x%x exited\n", val0);
2923 svoboda 277
        abort_debug = true;
278
        break;
279
    case UDEBUG_EVENT_BREAKPOINT:
280
        arch_event_breakpoint(thash);
281
        break;
282
    case UDEBUG_EVENT_TRAP:
2942 svoboda 283
        arch_event_trap(dthread_get());
2923 svoboda 284
        break;
285
    default:
2936 svoboda 286
        cons_printf("unknown event type %d\n", ev_type);
2923 svoboda 287
        break;
288
    }
289
}
290
 
2946 svoboda 291
static int debug_loop(void *dt_arg)
2911 svoboda 292
{
293
    int rc;
2923 svoboda 294
    udebug_event_t ev_type;
2911 svoboda 295
    unsigned val0, val1;
2938 svoboda 296
    dthread_t *dt;
2911 svoboda 297
 
2938 svoboda 298
    dt = (dthread_t *)dt_arg;
2911 svoboda 299
 
2938 svoboda 300
    cons_printf("debug_loop(%d)\n", dt->id);
2911 svoboda 301
 
302
    while (!abort_debug) {
303
 
304
        /* Run thread until an event occurs */
2938 svoboda 305
        rc = udebug_go(app_phone, dt->hash, &ev_type, &val0, &val1);
2911 svoboda 306
 
307
        if (ev_type == UDEBUG_EVENT_FINISHED) {
2938 svoboda 308
            cons_printf("thread %u debugging finished\n", dt->id);
2911 svoboda 309
            break;
310
        }
2939 svoboda 311
        if (rc >= 0) debug_event(dt->hash, ev_type, val0);
2911 svoboda 312
    }
313
 
2938 svoboda 314
    cons_printf("debug_loop(%d) exiting\n", dt->id);
2946 svoboda 315
    return 0;
2911 svoboda 316
}
317
 
2937 svoboda 318
void thread_debug_start(unsigned thash)
2911 svoboda 319
{
320
    fid_t fid;
2938 svoboda 321
    dthread_t *dt;
2911 svoboda 322
 
2938 svoboda 323
    dt = dthread_new(thash);
2911 svoboda 324
 
2938 svoboda 325
    fid = fibril_create(debug_loop, (void *)dt);
2911 svoboda 326
    if (fid == 0) {
2936 svoboda 327
        cons_printf("Warning: Failed creating fibril\n");
2911 svoboda 328
    }
2939 svoboda 329
    dt->fid = fid;
330
 
2911 svoboda 331
    fibril_add_ready(fid);
332
}
333
 
2946 svoboda 334
static void debug_active_task(void)
2911 svoboda 335
{
336
    int taskid;
337
    int i;
338
    int rc;
339
 
2940 svoboda 340
    thash_t *thash_buffer;
341
    int n_threads;
342
 
2936 svoboda 343
    cons_printf("Breakpoint Debugger\n");
344
    cons_printf("Press 'c' to connect\n");
2911 svoboda 345
    while ((i = getchar()) != 'c')
346
        putchar(i);
347
 
2935 svoboda 348
    taskid = 14;
2911 svoboda 349
    rc = task_connect(taskid);
350
    if (rc < 0) {
2936 svoboda 351
        cons_printf("Failed to connect to task %d\n", taskid);
2911 svoboda 352
        return;
353
    }
354
 
2936 svoboda 355
    cons_printf("Connected to task %d\n", taskid);
2911 svoboda 356
 
2940 svoboda 357
    rc = get_thread_list(&thash_buffer, &n_threads);
2911 svoboda 358
    if (rc < 0) {
2940 svoboda 359
        cons_printf("Failed to get thread list\n", rc);
2911 svoboda 360
        return;
361
    }
362
 
363
    abort_debug = false;
364
 
2940 svoboda 365
    for (i = 0; i < n_threads; i++) {
366
        thread_debug_start(thash_buffer[i]);
2911 svoboda 367
    }
368
 
369
    while (!quit) {
2936 svoboda 370
        cons_read_line(in_buf, IN_BUF_SIZE);
2911 svoboda 371
        command_split(in_buf);
372
        if (cmd_argc == 0) continue;
373
 
374
        command_run();
375
    }
376
 
2936 svoboda 377
    cons_printf("terminate debugging session...\n");
2911 svoboda 378
    abort_debug = true;
379
    udebug_end(app_phone);
380
    ipc_hangup(app_phone);
381
 
2936 svoboda 382
    cons_printf("done\n");
2911 svoboda 383
    return;
384
}
385
 
386
static void main_init(void)
387
{
388
    next_thread_id = 1;
389
    paused = 0;
2938 svoboda 390
 
391
    list_initialize(&dthreads);
3005 svoboda 392
    breakpoint_init();
2938 svoboda 393
    cwt = NULL;
2935 svoboda 394
 
395
    fcv_init(&go_cv);
2911 svoboda 396
}
397
 
398
int main(void)
399
{
400
    main_init();
401
 
402
    while (1) {
403
        debug_active_task();
404
    }
405
}
406
 
407
/** @}
408
 */