Subversion Repositories HelenOS

Rev

Rev 2938 | Rev 2940 | 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
 
62
#define THBUF_SIZE 64
63
thash_t thread_hash_buf[THBUF_SIZE];
2937 svoboda 64
 
2911 svoboda 65
int next_thread_id;
66
 
67
int app_phone;
68
volatile bool abort_debug;
69
 
70
volatile int paused;
71
 
2922 svoboda 72
breakpoint_t brk_list[MAX_BRKPTS];
73
int lifted_brkpt;
74
 
2935 svoboda 75
fcv_t go_cv;
76
 
2938 svoboda 77
static void command_split(char *cmd_str)
2911 svoboda 78
{
79
    char *p = cmd_str;
80
 
81
    if (*p == '\0') {
82
        cmd_argc = 0;
83
        return;
84
    }
85
 
86
    cmd_argc = 1;
87
    cmd_argv[0] = p;
88
 
89
    while (*p != '\0') {
90
        if (*p == ' ') {
91
            cmd_argv[cmd_argc++] = p + 1;
92
            *p = '\0';
93
        }
94
        ++p;
95
    }
96
}
97
 
2938 svoboda 98
static void command_run(void)
2911 svoboda 99
{
100
    int i;
101
    int cmp_len;
102
    int len;
103
 
104
    int idx_found;
105
    int num_found;
106
 
107
    len = strlen(cmd_argv[0]);
108
    cmp_len = 1;
109
 
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
 
124
        --cmp_len;
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;
145
 
146
    dt = dthread_get();
147
    cons_printf("[thread %d] stopped\n", dt->id);
148
    dthread_stop_me();
149
    cons_printf("[thread %d] go\n", dt->id);
2936 svoboda 150
}
151
 
2935 svoboda 152
/*
153
 * Called by a fibril (from arch code) when a breakpoint is hit.
154
 */
155
void breakpoint_hit(void)
156
{
2936 svoboda 157
    cons_printf("breakpoint hit\n");
158
    thread_stop();
2935 svoboda 159
}
2911 svoboda 160
 
2938 svoboda 161
static int task_connect(int taskid)
2911 svoboda 162
{
163
    int rc;
2918 svoboda 164
    unsigned evmask;
2911 svoboda 165
 
2936 svoboda 166
    cons_printf("ipc_connect_kbox(%d)... ", taskid);
2911 svoboda 167
    rc = ipc_connect_kbox(taskid);
2936 svoboda 168
    cons_printf("-> %d\n", rc);
2911 svoboda 169
    app_phone = rc;
170
    if (rc < 0) return rc;
171
 
2936 svoboda 172
    cons_printf("udebug_begin()... ");
2911 svoboda 173
    rc = udebug_begin(app_phone);
2936 svoboda 174
    cons_printf("-> %d\n", rc);
2911 svoboda 175
    if (rc < 0) return rc;
176
 
2918 svoboda 177
    evmask = UDEBUG_EM_ALL & ~(UDEBUG_EM_SYSCALL_B | UDEBUG_EM_SYSCALL_E);
2936 svoboda 178
    cons_printf("udebug_set_evmask(0x%x)... ", evmask);
2918 svoboda 179
    rc = udebug_set_evmask(app_phone, evmask);
2936 svoboda 180
    cons_printf("-> %d\n", rc);
2911 svoboda 181
    if (rc < 0) return rc;
182
 
183
    return 0;
184
}
185
 
2938 svoboda 186
static int get_thread_list(void)
2911 svoboda 187
{
188
    int rc;
189
    int tb_copied;
190
    int tb_needed;
191
    int i;
2937 svoboda 192
    int n;
2911 svoboda 193
 
2936 svoboda 194
    cons_printf("send IPC_M_DEBUG_THREAD_READ message\n");
2911 svoboda 195
    rc = udebug_thread_read(app_phone, (unsigned)thread_hash_buf,
196
        THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed);
2936 svoboda 197
    cons_printf("-> %d\n", rc);
2911 svoboda 198
    if (rc < 0) return rc;
199
 
2937 svoboda 200
    n = tb_copied / sizeof(unsigned);
2911 svoboda 201
 
2936 svoboda 202
    cons_printf("thread IDs:");
2937 svoboda 203
    for (i=0; i<n; i++) {
2936 svoboda 204
        cons_printf(" %u", thread_hash_buf[i]);
2911 svoboda 205
    }
2936 svoboda 206
    cons_printf("\ntotal of %u threads\n", tb_needed/sizeof(unsigned));
2911 svoboda 207
 
2937 svoboda 208
    return n;
2911 svoboda 209
}
210
 
211
void event_thread_b(unsigned hash)
212
{
213
    async_serialize_start();
2936 svoboda 214
    cons_printf("new thread, hash 0x%x\n", hash);
2911 svoboda 215
    async_serialize_end();
216
 
217
    thread_debug_start(hash);
218
}
219
 
2922 svoboda 220
static unsigned buffer[1024];
221
 
2923 svoboda 222
static void debug_event(thash_t thash, udebug_event_t ev_type, sysarg_t val0)
223
{
224
    switch (ev_type) {
225
    case UDEBUG_EVENT_STOP:
2936 svoboda 226
        cons_printf("stop event\n");
2939 svoboda 227
        thread_stop();
2923 svoboda 228
        break;
229
    case UDEBUG_EVENT_THREAD_B:
230
        event_thread_b(val0);
231
        break;
232
    case UDEBUG_EVENT_THREAD_E:
2936 svoboda 233
        cons_printf("thread 0x%x exited\n", val0);
2923 svoboda 234
        abort_debug = true;
235
        break;
236
    case UDEBUG_EVENT_BREAKPOINT:
237
        arch_event_breakpoint(thash);
238
        break;
239
    case UDEBUG_EVENT_TRAP:
240
        arch_event_trap(thash);
241
        break;
242
    default:
2936 svoboda 243
        cons_printf("unknown event type %d\n", ev_type);
2923 svoboda 244
        break;
245
    }
246
}
247
 
2938 svoboda 248
void debug_loop(void *dt_arg)
2911 svoboda 249
{
250
    int rc;
2923 svoboda 251
    udebug_event_t ev_type;
2937 svoboda 252
    unsigned thread_buf_idx;
2911 svoboda 253
    unsigned val0, val1;
2938 svoboda 254
    dthread_t *dt;
2911 svoboda 255
 
2938 svoboda 256
    dt = (dthread_t *)dt_arg;
2911 svoboda 257
 
2938 svoboda 258
    cons_printf("debug_loop(%d)\n", dt->id);
2911 svoboda 259
 
260
    while (!abort_debug) {
261
 
262
        /* Run thread until an event occurs */
2938 svoboda 263
        rc = udebug_go(app_phone, dt->hash, &ev_type, &val0, &val1);
2911 svoboda 264
 
265
        if (ev_type == UDEBUG_EVENT_FINISHED) {
2938 svoboda 266
            cons_printf("thread %u debugging finished\n", dt->id);
2911 svoboda 267
            break;
268
        }
2939 svoboda 269
        if (rc >= 0) debug_event(dt->hash, ev_type, val0);
2911 svoboda 270
    }
271
 
2938 svoboda 272
    cons_printf("debug_loop(%d) exiting\n", dt->id);
2911 svoboda 273
}
274
 
2937 svoboda 275
void thread_debug_start(unsigned thash)
2911 svoboda 276
{
277
    fid_t fid;
2938 svoboda 278
    dthread_t *dt;
2911 svoboda 279
 
2938 svoboda 280
    dt = dthread_new(thash);
2911 svoboda 281
 
2938 svoboda 282
    fid = fibril_create(debug_loop, (void *)dt);
2911 svoboda 283
    if (fid == 0) {
2936 svoboda 284
        cons_printf("Warning: Failed creating fibril\n");
2911 svoboda 285
    }
2939 svoboda 286
    dt->fid = fid;
287
 
2911 svoboda 288
    fibril_add_ready(fid);
289
}
290
 
291
void debug_active_task(void)
292
{
293
    int taskid;
294
    int i;
295
    int rc;
296
    int c;
297
 
2936 svoboda 298
    cons_printf("Breakpoint Debugger\n");
299
    cons_printf("Press 'c' to connect\n");
2911 svoboda 300
    while ((i = getchar()) != 'c')
301
        putchar(i);
302
 
2935 svoboda 303
    taskid = 14;
2911 svoboda 304
    rc = task_connect(taskid);
305
    if (rc < 0) {
2936 svoboda 306
        cons_printf("Failed to connect to task %d\n", taskid);
2911 svoboda 307
        return;
308
    }
309
 
2936 svoboda 310
    cons_printf("Connected to task %d\n", taskid);
2911 svoboda 311
 
312
    rc = get_thread_list();
313
    if (rc < 0) {
2936 svoboda 314
        cons_printf("Failed to get thread list (error %d)\n", rc);
2911 svoboda 315
        return;
316
    }
317
 
318
    abort_debug = false;
319
 
2937 svoboda 320
    for (i = 0; i < rc; i++) {
2911 svoboda 321
        thread_debug_start(thread_hash_buf[i]);
322
    }
323
 
324
    while (!quit) {
2936 svoboda 325
        cons_read_line(in_buf, IN_BUF_SIZE);
2911 svoboda 326
        command_split(in_buf);
327
        if (cmd_argc == 0) continue;
328
 
329
        command_run();
330
    }
331
 
2936 svoboda 332
    cons_printf("terminate debugging session...\n");
2911 svoboda 333
    abort_debug = true;
334
    udebug_end(app_phone);
335
    ipc_hangup(app_phone);
336
 
2936 svoboda 337
    cons_printf("done\n");
2911 svoboda 338
    return;
339
}
340
 
341
static void main_init(void)
342
{
343
    next_thread_id = 1;
344
    paused = 0;
2938 svoboda 345
 
346
    list_initialize(&dthreads);
347
    cwt = NULL;
2935 svoboda 348
 
349
    fcv_init(&go_cv);
2911 svoboda 350
}
351
 
352
int main(void)
353
{
354
    main_init();
355
 
356
    while (1) {
357
        debug_active_task();
358
    }
359
}
360
 
361
/** @}
362
 */