Subversion Repositories HelenOS

Rev

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