Subversion Repositories HelenOS

Rev

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

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