Subversion Repositories HelenOS

Rev

Rev 2924 | Rev 2936 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2924 Rev 2935
1
/*
1
/*
2
 * Copyright (c) 2008 Jiri Svoboda
2
 * Copyright (c) 2008 Jiri Svoboda
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup debug
29
/** @addtogroup debug
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include <stdio.h>
35
#include <stdio.h>
36
#include <stdlib.h>
36
#include <stdlib.h>
37
#include <unistd.h>
37
#include <unistd.h>
38
#include <syscall.h>
38
#include <syscall.h>
39
#include <ipc/ipc.h>
39
#include <ipc/ipc.h>
40
#include <fibril.h>
40
#include <fibril.h>
41
#include <errno.h>
41
#include <errno.h>
42
#include <udebug.h>
42
#include <udebug.h>
43
#include <async.h>
43
#include <async.h>
44
#include <string.h>
44
#include <string.h>
45
 
45
 
46
#include "cmd.h"
46
#include "cmd.h"
47
#include "include/arch.h"
47
#include "include/arch.h"
-
 
48
#include "fib_synch.h"
48
#include "main.h"
49
#include "main.h"
49
 
50
 
50
void thread_debug_start(unsigned thread_hash);
51
void thread_debug_start(unsigned thread_hash);
51
 
52
 
52
#define INBUF_SIZE 64
53
#define INBUF_SIZE 64
53
char in_buf[INBUF_SIZE];
54
char in_buf[INBUF_SIZE];
54
 
55
 
55
#define MAX_ARGC 10
56
#define MAX_ARGC 10
56
int cmd_argc;
57
int cmd_argc;
57
char *cmd_argv[MAX_ARGC + 1];   /* need one spare field for cmd_split() */
58
char *cmd_argv[MAX_ARGC + 1];   /* need one spare field for cmd_split() */
58
 
59
 
59
#define THBUF_SIZE 64
60
#define THBUF_SIZE 64
60
thash_t thread_hash_buf[THBUF_SIZE];
61
thash_t thread_hash_buf[THBUF_SIZE];
61
unsigned n_threads;
62
unsigned n_threads;
62
 
63
 
63
int next_thread_id;
64
int next_thread_id;
64
 
65
 
65
int app_phone;
66
int app_phone;
66
volatile bool abort_debug;
67
volatile bool abort_debug;
67
 
68
 
68
thash_t thash;
69
thash_t thash;
69
volatile int paused;
70
volatile int paused;
70
 
71
 
71
breakpoint_t brk_list[MAX_BRKPTS];
72
breakpoint_t brk_list[MAX_BRKPTS];
72
int lifted_brkpt;
73
int lifted_brkpt;
73
 
74
 
-
 
75
fcv_t go_cv;
-
 
76
 
74
void read_line(char *buffer, int n)
77
void read_line(char *buffer, int n)
75
{
78
{
76
    char c;
79
    char c;
77
    int i;
80
    int i;
78
 
81
 
79
    i = 0;
82
    i = 0;
80
    while (i < n - 1) {
83
    while (i < n - 1) {
81
        c = getchar();
84
        c = getchar();
82
        if (c == '\n') break;
85
        if (c == '\n') break;
83
        if (c == '\b') {
86
        if (c == '\b') {
84
            if (i > 0) {
87
            if (i > 0) {
85
                putchar('\b');
88
                putchar('\b');
86
                --i;
89
                --i;
87
            }
90
            }
88
            continue;
91
            continue;
89
        }
92
        }
90
       
93
       
91
        putchar(c);
94
        putchar(c);
92
        buffer[i++] = c;
95
        buffer[i++] = c;
93
    }
96
    }
94
 
97
 
95
    putchar('\n');
98
    putchar('\n');
96
    buffer[i] = '\0';
99
    buffer[i] = '\0';
97
}
100
}
98
 
101
 
99
void command_split(char *cmd_str)
102
void command_split(char *cmd_str)
100
{
103
{
101
    char *p = cmd_str;
104
    char *p = cmd_str;
102
 
105
 
103
    if (*p == '\0') {
106
    if (*p == '\0') {
104
        cmd_argc = 0;
107
        cmd_argc = 0;
105
        return;
108
        return;
106
    }
109
    }
107
 
110
 
108
    cmd_argc = 1;
111
    cmd_argc = 1;
109
    cmd_argv[0] = p;
112
    cmd_argv[0] = p;
110
 
113
 
111
    while (*p != '\0') {
114
    while (*p != '\0') {
112
        if (*p == ' ') {
115
        if (*p == ' ') {
113
            cmd_argv[cmd_argc++] = p + 1;
116
            cmd_argv[cmd_argc++] = p + 1;
114
            *p = '\0';
117
            *p = '\0';
115
        }
118
        }
116
        ++p;
119
        ++p;
117
    }
120
    }
118
}
121
}
119
 
122
 
120
void command_run(void)
123
void command_run(void)
121
{
124
{
122
    int i;
125
    int i;
123
    int cmp_len;
126
    int cmp_len;
124
    int len;
127
    int len;
125
 
128
 
126
    int idx_found;
129
    int idx_found;
127
    int num_found;
130
    int num_found;
128
 
131
 
129
    len = strlen(cmd_argv[0]);
132
    len = strlen(cmd_argv[0]);
130
    cmp_len = 1;
133
    cmp_len = 1;
131
 
134
 
132
    while (cmp_len <= len + 1) {
135
    while (cmp_len <= len + 1) {
133
 
136
 
134
        num_found = 0;
137
        num_found = 0;
135
        i = 0;
138
        i = 0;
136
        while (cmd_table[i].name != NULL) {
139
        while (cmd_table[i].name != NULL) {
137
            if (strncmp(cmd_table[i].name, cmd_argv[0], cmp_len) == 0) {
140
            if (strncmp(cmd_table[i].name, cmd_argv[0], cmp_len) == 0) {
138
                idx_found = i;
141
                idx_found = i;
139
                ++num_found;
142
                ++num_found;
140
            }
143
            }
141
            ++i;
144
            ++i;
142
        }
145
        }
143
 
146
 
144
        if (num_found < 2) break;
147
        if (num_found < 2) break;
145
 
148
 
146
        --cmp_len;
149
        --cmp_len;
147
    }
150
    }
148
 
151
 
149
    if (num_found == 0) {
152
    if (num_found == 0) {
150
        printf("Unknown command. Try one of:\n");
153
        printf("Unknown command. Try one of:\n");
151
        cmd_help(0, NULL);
154
        cmd_help(0, NULL);
152
        return;
155
        return;
153
    }
156
    }
154
 
157
 
155
    if (cmd_argc - 1 != cmd_table[idx_found].argc) {
158
    if (cmd_argc - 1 != cmd_table[idx_found].argc) {
156
        printf("Command '%s' expects %d arguments\n",
159
        printf("Command '%s' expects %d arguments\n",
157
        cmd_table[idx_found].name, cmd_table[idx_found].argc);
160
        cmd_table[idx_found].name, cmd_table[idx_found].argc);
158
        return;
161
        return;
159
    }
162
    }
160
 
163
 
161
    (*cmd_table[idx_found].proc)(cmd_argc, cmd_argv);
164
    (*cmd_table[idx_found].proc)(cmd_argc, cmd_argv);
162
}
165
}
163
 
166
 
-
 
167
/*
-
 
168
 * Called by a fibril (from arch code) when a breakpoint is hit.
-
 
169
 */
-
 
170
void breakpoint_hit(void)
-
 
171
{
-
 
172
    fcv_wait(&go_cv);
-
 
173
}
164
 
174
 
165
int task_connect(int taskid)
175
int task_connect(int taskid)
166
{
176
{
167
    int rc;
177
    int rc;
168
    unsigned evmask;
178
    unsigned evmask;
169
 
179
 
170
    printf("ipc_connect_kbox(%d)... ", taskid);
180
    printf("ipc_connect_kbox(%d)... ", taskid);
171
    rc = ipc_connect_kbox(taskid);
181
    rc = ipc_connect_kbox(taskid);
172
    printf("-> %d\n", rc);
182
    printf("-> %d\n", rc);
173
    app_phone = rc;
183
    app_phone = rc;
174
    if (rc < 0) return rc;
184
    if (rc < 0) return rc;
175
 
185
 
176
    printf("udebug_begin()... ");
186
    printf("udebug_begin()... ");
177
    rc = udebug_begin(app_phone);
187
    rc = udebug_begin(app_phone);
178
    printf("-> %d\n", rc);
188
    printf("-> %d\n", rc);
179
    if (rc < 0) return rc;
189
    if (rc < 0) return rc;
180
 
190
 
181
    evmask = UDEBUG_EM_ALL & ~(UDEBUG_EM_SYSCALL_B | UDEBUG_EM_SYSCALL_E);
191
    evmask = UDEBUG_EM_ALL & ~(UDEBUG_EM_SYSCALL_B | UDEBUG_EM_SYSCALL_E);
182
    printf("udebug_set_evmask(0x%x)... ", evmask);
192
    printf("udebug_set_evmask(0x%x)... ", evmask);
183
    rc = udebug_set_evmask(app_phone, evmask);
193
    rc = udebug_set_evmask(app_phone, evmask);
184
    printf("-> %d\n", rc);
194
    printf("-> %d\n", rc);
185
    if (rc < 0) return rc;
195
    if (rc < 0) return rc;
186
 
196
 
187
    return 0;
197
    return 0;
188
}
198
}
189
 
199
 
190
int get_thread_list(void)
200
int get_thread_list(void)
191
{
201
{
192
    int rc;
202
    int rc;
193
    int tb_copied;
203
    int tb_copied;
194
    int tb_needed;
204
    int tb_needed;
195
    int i;
205
    int i;
196
 
206
 
197
    printf("send IPC_M_DEBUG_THREAD_READ message\n");
207
    printf("send IPC_M_DEBUG_THREAD_READ message\n");
198
    rc = udebug_thread_read(app_phone, (unsigned)thread_hash_buf,
208
    rc = udebug_thread_read(app_phone, (unsigned)thread_hash_buf,
199
        THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed);
209
        THBUF_SIZE*sizeof(unsigned), &tb_copied, &tb_needed);
200
    printf("-> %d\n", rc);
210
    printf("-> %d\n", rc);
201
    if (rc < 0) return rc;
211
    if (rc < 0) return rc;
202
 
212
 
203
    n_threads = tb_copied / sizeof(unsigned);
213
    n_threads = tb_copied / sizeof(unsigned);
204
 
214
 
205
    printf("thread IDs:");
215
    printf("thread IDs:");
206
    for (i=0; i<n_threads; i++) {
216
    for (i=0; i<n_threads; i++) {
207
        printf(" %u", thread_hash_buf[i]);
217
        printf(" %u", thread_hash_buf[i]);
208
    }
218
    }
209
    printf("\ntotal of %u threads\n", tb_needed/sizeof(unsigned));
219
    printf("\ntotal of %u threads\n", tb_needed/sizeof(unsigned));
210
 
220
 
211
    return 0;
221
    return 0;
212
}
222
}
213
 
223
 
214
void event_thread_b(unsigned hash)
224
void event_thread_b(unsigned hash)
215
{
225
{
216
    async_serialize_start();
226
    async_serialize_start();
217
    printf("new thread, hash 0x%x\n", hash);
227
    printf("new thread, hash 0x%x\n", hash);
218
    async_serialize_end();
228
    async_serialize_end();
219
 
229
 
220
    thread_debug_start(hash);
230
    thread_debug_start(hash);
221
}
231
}
222
 
232
 
223
static unsigned buffer[1024];
233
static unsigned buffer[1024];
224
 
234
 
225
static void debug_event(thash_t thash, udebug_event_t ev_type, sysarg_t val0)
235
static void debug_event(thash_t thash, udebug_event_t ev_type, sysarg_t val0)
226
{
236
{
227
    switch (ev_type) {
237
    switch (ev_type) {
228
    case UDEBUG_EVENT_STOP:
238
    case UDEBUG_EVENT_STOP:
229
        printf("stop event\n");
239
        printf("stop event\n");
230
        printf("waiting for resume\n");
240
        printf("waiting for resume\n");
231
        while (paused) {
241
        while (paused) {
232
            usleep(1000000);
242
            usleep(1000000);
233
            fibril_yield();
243
            fibril_yield();
234
            printf(".");
244
            printf(".");
235
        }
245
        }
236
        printf("resumed\n");
246
        printf("resumed\n");
237
        break;
247
        break;
238
    case UDEBUG_EVENT_THREAD_B:
248
    case UDEBUG_EVENT_THREAD_B:
239
        event_thread_b(val0);
249
        event_thread_b(val0);
240
        break;
250
        break;
241
    case UDEBUG_EVENT_THREAD_E:
251
    case UDEBUG_EVENT_THREAD_E:
242
        printf("thread 0x%x exited\n", val0);
252
        printf("thread 0x%x exited\n", val0);
243
        abort_debug = true;
253
        abort_debug = true;
244
        break;
254
        break;
245
    case UDEBUG_EVENT_BREAKPOINT:
255
    case UDEBUG_EVENT_BREAKPOINT:
246
        arch_event_breakpoint(thash);
256
        arch_event_breakpoint(thash);
247
        break;
257
        break;
248
    case UDEBUG_EVENT_TRAP:
258
    case UDEBUG_EVENT_TRAP:
249
        arch_event_trap(thash);
259
        arch_event_trap(thash);
250
        break;
260
        break;
251
    default:
261
    default:
252
        printf("unknown event type %d\n", ev_type);
262
        printf("unknown event type %d\n", ev_type);
253
        break;
263
        break;
254
    }
264
    }
255
}
265
}
256
 
266
 
257
void debug_loop(void *thread_hash_arg)
267
void debug_loop(void *thread_hash_arg)
258
{
268
{
259
    int rc;
269
    int rc;
260
    udebug_event_t ev_type;
270
    udebug_event_t ev_type;
261
    unsigned thread_hash;
271
    unsigned thread_hash;
262
    unsigned thread_id;
272
    unsigned thread_id;
263
    unsigned val0, val1;
273
    unsigned val0, val1;
264
 
274
 
265
    thread_hash = (unsigned)thread_hash_arg;
275
    thread_hash = (unsigned)thread_hash_arg;
266
    thread_id = next_thread_id++;
276
    thread_id = next_thread_id++;
267
 
277
 
268
    printf("debug_loop(%d)\n", thread_id); 
278
    printf("debug_loop(%d)\n", thread_id); 
269
 
279
 
270
    while (!abort_debug) {
280
    while (!abort_debug) {
271
 
281
 
272
        printf("go\n");
282
        printf("[t%d] go...\n", thread_id);
273
        /* Run thread until an event occurs */
283
        /* Run thread until an event occurs */
274
        rc = udebug_go(app_phone, thread_hash,
284
        rc = udebug_go(app_phone, thread_hash,
275
            &ev_type, &val0, &val1);
285
            &ev_type, &val0, &val1);
-
 
286
        printf("[t%d] stopped\n", thread_id);
276
 
287
 
277
        if (ev_type == UDEBUG_EVENT_FINISHED) {
288
        if (ev_type == UDEBUG_EVENT_FINISHED) {
278
            printf("thread %u debugging finished\n", thread_id);
289
            printf("thread %u debugging finished\n", thread_id);
279
            break;
290
            break;
280
        }
291
        }
281
 
-
 
282
        if (rc >= 0) debug_event(thread_hash, ev_type, val0);
292
        if (rc >= 0) debug_event(thread_hash, ev_type, val0);
283
    }
293
    }
284
 
294
 
285
    printf("debug_loop(%d) exiting\n", thread_id);
295
    printf("debug_loop(%d) exiting\n", thread_id);
286
}
296
}
287
 
297
 
288
void thread_debug_start(unsigned thread_hash)
298
void thread_debug_start(unsigned thread_hash)
289
{
299
{
290
    fid_t fid;
300
    fid_t fid;
291
 
301
 
292
    thash = thread_hash;
302
    thash = thread_hash;
293
 
303
 
294
    fid = fibril_create(debug_loop, (void *)thread_hash);
304
    fid = fibril_create(debug_loop, (void *)thread_hash);
295
    if (fid == 0) {
305
    if (fid == 0) {
296
        printf("Warning: Failed creating fibril\n");
306
        printf("Warning: Failed creating fibril\n");
297
    }
307
    }
298
    fibril_add_ready(fid);
308
    fibril_add_ready(fid);
299
}
309
}
300
 
310
 
301
void debug_active_task(void)
311
void debug_active_task(void)
302
{
312
{
303
    int taskid;
313
    int taskid;
304
    int i;
314
    int i;
305
    int rc;
315
    int rc;
306
    int c;
316
    int c;
307
 
317
 
308
    printf("Breakpoint Debugger\n");
318
    printf("Breakpoint Debugger\n");
309
    printf("Press 'c' to connect\n");
319
    printf("Press 'c' to connect\n");
310
    while ((i = getchar()) != 'c')
320
    while ((i = getchar()) != 'c')
311
        putchar(i);
321
        putchar(i);
312
 
322
 
313
    taskid = 13;
323
    taskid = 14;
314
    rc = task_connect(taskid);
324
    rc = task_connect(taskid);
315
    if (rc < 0) {
325
    if (rc < 0) {
316
        printf("Failed to connect to task %d\n", taskid);
326
        printf("Failed to connect to task %d\n", taskid);
317
        return;
327
        return;
318
    }
328
    }
319
 
329
 
320
    printf("Connected to task %d\n", taskid);
330
    printf("Connected to task %d\n", taskid);
321
 
331
 
322
    rc = get_thread_list();
332
    rc = get_thread_list();
323
    if (rc < 0) {
333
    if (rc < 0) {
324
        printf("Failed to get thread list (error %d)\n", rc);
334
        printf("Failed to get thread list (error %d)\n", rc);
325
        return;
335
        return;
326
    }
336
    }
327
 
337
 
328
    abort_debug = false;
338
    abort_debug = false;
329
 
339
 
330
    for (i = 0; i < n_threads; i++) {
340
    for (i = 0; i < n_threads; i++) {
331
        thread_debug_start(thread_hash_buf[i]);
341
        thread_debug_start(thread_hash_buf[i]);
332
    }
342
    }
333
 
343
 
334
    while (!quit) {
344
    while (!quit) {
335
        printf("> ");
345
        printf("> ");
336
        read_line(in_buf, INBUF_SIZE);
346
        read_line(in_buf, INBUF_SIZE);
337
        command_split(in_buf);
347
        command_split(in_buf);
338
        if (cmd_argc == 0) continue;
348
        if (cmd_argc == 0) continue;
339
 
349
 
340
        command_run();
350
        command_run();
341
    }
351
    }
342
 
352
 
343
    printf("terminate debugging session...\n");
353
    printf("terminate debugging session...\n");
344
    abort_debug = true;
354
    abort_debug = true;
345
    udebug_end(app_phone);
355
    udebug_end(app_phone);
346
    ipc_hangup(app_phone);
356
    ipc_hangup(app_phone);
347
 
357
 
348
    printf("done\n");
358
    printf("done\n");
349
    return;
359
    return;
350
}
360
}
351
 
361
 
352
static void main_init(void)
362
static void main_init(void)
353
{
363
{
354
    next_thread_id = 1;
364
    next_thread_id = 1;
355
    paused = 0;
365
    paused = 0;
-
 
366
 
-
 
367
    fcv_init(&go_cv);
356
}
368
}
357
 
369
 
358
int main(void)
370
int main(void)
359
{
371
{
360
    main_init();
372
    main_init();
361
 
373
 
362
    while (1) {
374
    while (1) {
363
        debug_active_task();
375
        debug_active_task();
364
    }
376
    }
365
}
377
}
366
 
378
 
367
/** @}
379
/** @}
368
 */
380
 */
369
 
381