Subversion Repositories HelenOS

Rev

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