Subversion Repositories HelenOS

Rev

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