Rev 4380 | 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> |
||
3536 | svoboda | 38 | #include <libarch/syscall.h> |
2911 | svoboda | 39 | #include <ipc/ipc.h> |
40 | #include <fibril.h> |
||
3472 | svoboda | 41 | #include <loader/loader.h> |
2911 | svoboda | 42 | #include <errno.h> |
43 | #include <udebug.h> |
||
44 | #include <async.h> |
||
45 | #include <string.h> |
||
46 | |||
47 | #include "cmd.h" |
||
2936 | svoboda | 48 | #include "cons.h" |
2938 | svoboda | 49 | #include "dthread.h" |
3005 | svoboda | 50 | #include "breakpoint.h" |
2923 | svoboda | 51 | #include "include/arch.h" |
2915 | svoboda | 52 | #include "main.h" |
2911 | svoboda | 53 | |
4390 | svoboda | 54 | void thread_debug_start(thash_t thread_hash); |
2911 | svoboda | 55 | |
2936 | svoboda | 56 | #define IN_BUF_SIZE 64 |
57 | static char in_buf[IN_BUF_SIZE]; |
||
2911 | svoboda | 58 | |
59 | #define MAX_ARGC 10 |
||
60 | int cmd_argc; |
||
61 | char *cmd_argv[MAX_ARGC + 1]; /* need one spare field for cmd_split() */ |
||
62 | |||
2937 | svoboda | 63 | |
2911 | svoboda | 64 | int next_thread_id; |
65 | |||
66 | int app_phone; |
||
67 | volatile bool abort_debug; |
||
68 | |||
69 | volatile int paused; |
||
70 | |||
3472 | svoboda | 71 | static task_id_t task_id; |
72 | static loader_t *task_ldr; |
||
73 | |||
74 | static int program_run_fibril(void *arg); |
||
75 | |||
76 | static void program_run(void) |
||
77 | { |
||
78 | fid_t fid; |
||
79 | |||
80 | fid = fibril_create(program_run_fibril, NULL); |
||
81 | if (fid == 0) { |
||
82 | printf("Error creating fibril\n"); |
||
83 | exit(1); |
||
84 | } |
||
85 | |||
86 | fibril_add_ready(fid); |
||
87 | } |
||
88 | |||
89 | static int program_run_fibril(void *arg) |
||
90 | { |
||
91 | int rc; |
||
92 | |||
93 | /* |
||
94 | * This must be done in background as it will block until |
||
95 | * we let the task reply to this call. |
||
96 | */ |
||
97 | rc = loader_run(task_ldr); |
||
98 | if (rc != 0) { |
||
99 | printf("Error running program\n"); |
||
100 | exit(1); |
||
101 | } |
||
102 | |||
103 | free(task_ldr); |
||
104 | task_ldr = NULL; |
||
105 | |||
106 | return 0; |
||
107 | } |
||
108 | |||
109 | static loader_t *preload_task(const char *path, char *const argv[], |
||
110 | task_id_t *task_id) |
||
111 | { |
||
112 | loader_t *ldr; |
||
113 | int rc; |
||
114 | |||
115 | /* Spawn a program loader */ |
||
4380 | svoboda | 116 | ldr = loader_connect(); |
3472 | svoboda | 117 | if (ldr == NULL) |
118 | return 0; |
||
119 | |||
120 | /* Get task ID. */ |
||
121 | rc = loader_get_task_id(ldr, task_id); |
||
122 | if (rc != EOK) |
||
123 | goto error; |
||
124 | |||
125 | /* Send program pathname */ |
||
126 | rc = loader_set_pathname(ldr, path); |
||
127 | if (rc != EOK) |
||
128 | goto error; |
||
129 | |||
130 | /* Send arguments */ |
||
131 | rc = loader_set_args(ldr, argv); |
||
132 | if (rc != EOK) |
||
133 | goto error; |
||
134 | |||
135 | /* Load the program. */ |
||
136 | rc = loader_load_program(ldr); |
||
137 | if (rc != EOK) |
||
138 | goto error; |
||
139 | |||
140 | /* Success */ |
||
141 | return ldr; |
||
142 | |||
143 | /* Error exit */ |
||
144 | error: |
||
145 | loader_abort(ldr); |
||
146 | free(ldr); |
||
147 | return NULL; |
||
148 | } |
||
149 | |||
150 | |||
2938 | svoboda | 151 | static void command_split(char *cmd_str) |
2911 | svoboda | 152 | { |
153 | char *p = cmd_str; |
||
154 | |||
155 | if (*p == '\0') { |
||
156 | cmd_argc = 0; |
||
157 | return; |
||
158 | } |
||
159 | |||
160 | cmd_argc = 1; |
||
161 | cmd_argv[0] = p; |
||
162 | |||
163 | while (*p != '\0') { |
||
164 | if (*p == ' ') { |
||
165 | cmd_argv[cmd_argc++] = p + 1; |
||
166 | *p = '\0'; |
||
167 | } |
||
168 | ++p; |
||
169 | } |
||
170 | } |
||
171 | |||
2938 | svoboda | 172 | static void command_run(void) |
2911 | svoboda | 173 | { |
174 | int i; |
||
175 | int cmp_len; |
||
176 | int len; |
||
177 | |||
178 | int idx_found; |
||
179 | int num_found; |
||
180 | |||
4377 | svoboda | 181 | len = str_length(cmd_argv[0]); |
2911 | svoboda | 182 | cmp_len = 1; |
183 | |||
2946 | svoboda | 184 | /* Silence warnings */ |
185 | num_found = 0; |
||
186 | idx_found = 0; |
||
187 | |||
2911 | svoboda | 188 | while (cmp_len <= len + 1) { |
189 | |||
190 | num_found = 0; |
||
191 | i = 0; |
||
192 | while (cmd_table[i].name != NULL) { |
||
4377 | svoboda | 193 | if (str_lcmp(cmd_table[i].name, cmd_argv[0], cmp_len) == 0) { |
2911 | svoboda | 194 | idx_found = i; |
195 | ++num_found; |
||
196 | } |
||
197 | ++i; |
||
198 | } |
||
199 | |||
200 | if (num_found < 2) break; |
||
201 | |||
2947 | svoboda | 202 | ++cmp_len; |
2911 | svoboda | 203 | } |
204 | |||
205 | if (num_found == 0) { |
||
2936 | svoboda | 206 | cons_printf("Unknown command. Try one of:\n"); |
2911 | svoboda | 207 | cmd_help(0, NULL); |
208 | return; |
||
209 | } |
||
210 | |||
211 | if (cmd_argc - 1 != cmd_table[idx_found].argc) { |
||
2936 | svoboda | 212 | cons_printf("Command '%s' expects %d arguments\n", |
2911 | svoboda | 213 | cmd_table[idx_found].name, cmd_table[idx_found].argc); |
214 | return; |
||
215 | } |
||
216 | |||
217 | (*cmd_table[idx_found].proc)(cmd_argc, cmd_argv); |
||
218 | } |
||
219 | |||
2938 | svoboda | 220 | static void thread_stop(void) |
2936 | svoboda | 221 | { |
2939 | svoboda | 222 | dthread_t *dt; |
2940 | svoboda | 223 | uintptr_t pc; |
2939 | svoboda | 224 | |
225 | dt = dthread_get(); |
||
2940 | svoboda | 226 | pc = dthread_get_pc(dt); |
227 | cons_printf("[thread %d] stopped at 0x%lx\n", dt->id, pc); |
||
2939 | svoboda | 228 | dthread_stop_me(); |
229 | cons_printf("[thread %d] go\n", dt->id); |
||
2936 | svoboda | 230 | } |
231 | |||
2935 | svoboda | 232 | /* |
233 | * Called by a fibril (from arch code) when a breakpoint is hit. |
||
234 | */ |
||
3005 | svoboda | 235 | void breakpoint_hit(breakpoint_t *b) |
2935 | svoboda | 236 | { |
3005 | svoboda | 237 | dthread_t *dt; |
238 | |||
239 | dt = dthread_get(); |
||
240 | cons_printf("Thread %d hit breakpoint %d at 0x%lx\n", |
||
241 | dt->id, b->id, b->addr); |
||
2936 | svoboda | 242 | thread_stop(); |
2935 | svoboda | 243 | } |
2911 | svoboda | 244 | |
2942 | svoboda | 245 | /* |
246 | * Called by a fibril (from arch code) when a single instruction |
||
247 | * in singlestep is executed |
||
248 | */ |
||
249 | void singlestep_hit(void) |
||
250 | { |
||
251 | cons_printf("singlestep hit\n"); |
||
252 | thread_stop(); |
||
253 | } |
||
254 | |||
3472 | svoboda | 255 | static int connect_task(task_id_t task_id) |
2911 | svoboda | 256 | { |
257 | int rc; |
||
2918 | svoboda | 258 | unsigned evmask; |
2911 | svoboda | 259 | |
3429 | svoboda | 260 | cons_printf("ipc_connect_kbox(%;;d)... ", task_id); |
261 | rc = ipc_connect_kbox(task_id); |
||
2936 | svoboda | 262 | cons_printf("-> %d\n", rc); |
2911 | svoboda | 263 | app_phone = rc; |
264 | if (rc < 0) return rc; |
||
265 | |||
2936 | svoboda | 266 | cons_printf("udebug_begin()... "); |
2911 | svoboda | 267 | rc = udebug_begin(app_phone); |
2936 | svoboda | 268 | cons_printf("-> %d\n", rc); |
2911 | svoboda | 269 | if (rc < 0) return rc; |
270 | |||
2918 | svoboda | 271 | evmask = UDEBUG_EM_ALL & ~(UDEBUG_EM_SYSCALL_B | UDEBUG_EM_SYSCALL_E); |
2936 | svoboda | 272 | cons_printf("udebug_set_evmask(0x%x)... ", evmask); |
2918 | svoboda | 273 | rc = udebug_set_evmask(app_phone, evmask); |
2936 | svoboda | 274 | cons_printf("-> %d\n", rc); |
2911 | svoboda | 275 | if (rc < 0) return rc; |
276 | |||
277 | return 0; |
||
278 | } |
||
279 | |||
2940 | svoboda | 280 | #define THASH_BUF_INIT_LENGTH 32 |
281 | |||
282 | static int get_thread_list(thash_t **thash_buf_ptr, int *n) |
||
2911 | svoboda | 283 | { |
284 | int rc; |
||
2946 | svoboda | 285 | size_t tb_copied; |
286 | size_t tb_needed; |
||
2911 | svoboda | 287 | int i; |
2940 | svoboda | 288 | size_t tb_size; |
289 | thash_t *thash_buf; |
||
2911 | svoboda | 290 | |
2940 | svoboda | 291 | tb_size = THASH_BUF_INIT_LENGTH * sizeof(thash_t); |
292 | thash_buf = malloc(tb_size); |
||
293 | |||
2946 | svoboda | 294 | rc = udebug_thread_read(app_phone, thash_buf, |
2940 | svoboda | 295 | tb_size, &tb_copied, &tb_needed); |
2911 | svoboda | 296 | if (rc < 0) return rc; |
297 | |||
2940 | svoboda | 298 | if (tb_needed > tb_size) { |
299 | /* Larger buffer needed */ |
||
2911 | svoboda | 300 | |
2940 | svoboda | 301 | free(thash_buf); |
302 | |||
303 | tb_size = tb_needed; |
||
304 | thash_buf = malloc(tb_size); |
||
305 | |||
306 | if (!thash_buf) { |
||
307 | printf("malloc failed\n"); |
||
308 | exit(1); |
||
309 | } |
||
310 | |||
311 | /* Try again */ |
||
312 | |||
2946 | svoboda | 313 | rc = udebug_thread_read(app_phone, thash_buf, |
2940 | svoboda | 314 | tb_size, &tb_copied, &tb_needed); |
315 | |||
316 | if (rc < 0) return rc; |
||
2911 | svoboda | 317 | } |
318 | |||
2940 | svoboda | 319 | *n = tb_copied / sizeof(thash_t); |
320 | |||
321 | cons_printf("thread hashes:"); |
||
322 | |||
323 | for (i = 0; i < *n; ++i) { |
||
4390 | svoboda | 324 | cons_printf("0x%lx\n", thash_buf[i]); |
2940 | svoboda | 325 | } |
326 | |||
327 | cons_printf("Total of %u threads\n", *n); |
||
328 | |||
329 | *thash_buf_ptr = thash_buf; |
||
330 | |||
331 | return 0; |
||
2911 | svoboda | 332 | } |
333 | |||
4390 | svoboda | 334 | static void event_thread_b(thash_t hash) |
2911 | svoboda | 335 | { |
336 | async_serialize_start(); |
||
4390 | svoboda | 337 | cons_printf("new thread, hash 0x%lx\n", hash); |
2911 | svoboda | 338 | async_serialize_end(); |
339 | |||
340 | thread_debug_start(hash); |
||
341 | } |
||
342 | |||
2923 | svoboda | 343 | static void debug_event(thash_t thash, udebug_event_t ev_type, sysarg_t val0) |
344 | { |
||
345 | switch (ev_type) { |
||
346 | case UDEBUG_EVENT_STOP: |
||
2936 | svoboda | 347 | cons_printf("stop event\n"); |
2939 | svoboda | 348 | thread_stop(); |
2923 | svoboda | 349 | break; |
350 | case UDEBUG_EVENT_THREAD_B: |
||
351 | event_thread_b(val0); |
||
352 | break; |
||
353 | case UDEBUG_EVENT_THREAD_E: |
||
2936 | svoboda | 354 | cons_printf("thread 0x%x exited\n", val0); |
2923 | svoboda | 355 | abort_debug = true; |
356 | break; |
||
357 | case UDEBUG_EVENT_BREAKPOINT: |
||
358 | arch_event_breakpoint(thash); |
||
359 | break; |
||
360 | case UDEBUG_EVENT_TRAP: |
||
2942 | svoboda | 361 | arch_event_trap(dthread_get()); |
2923 | svoboda | 362 | break; |
363 | default: |
||
2936 | svoboda | 364 | cons_printf("unknown event type %d\n", ev_type); |
2923 | svoboda | 365 | break; |
366 | } |
||
367 | } |
||
368 | |||
2946 | svoboda | 369 | static int debug_loop(void *dt_arg) |
2911 | svoboda | 370 | { |
371 | int rc; |
||
2923 | svoboda | 372 | udebug_event_t ev_type; |
4390 | svoboda | 373 | sysarg_t val0, val1; |
2938 | svoboda | 374 | dthread_t *dt; |
2911 | svoboda | 375 | |
2938 | svoboda | 376 | dt = (dthread_t *)dt_arg; |
2911 | svoboda | 377 | |
2938 | svoboda | 378 | cons_printf("debug_loop(%d)\n", dt->id); |
2911 | svoboda | 379 | |
380 | while (!abort_debug) { |
||
381 | /* Run thread until an event occurs */ |
||
2938 | svoboda | 382 | rc = udebug_go(app_phone, dt->hash, &ev_type, &val0, &val1); |
4390 | svoboda | 383 | if (rc < 0) { |
384 | cons_printf("Failed resuming thread (%d).\n", rc); |
||
385 | return rc; |
||
386 | } |
||
2911 | svoboda | 387 | |
388 | if (ev_type == UDEBUG_EVENT_FINISHED) { |
||
2938 | svoboda | 389 | cons_printf("thread %u debugging finished\n", dt->id); |
2911 | svoboda | 390 | break; |
391 | } |
||
4390 | svoboda | 392 | debug_event(dt->hash, ev_type, val0); |
2911 | svoboda | 393 | } |
394 | |||
2938 | svoboda | 395 | cons_printf("debug_loop(%d) exiting\n", dt->id); |
2946 | svoboda | 396 | return 0; |
2911 | svoboda | 397 | } |
398 | |||
4390 | svoboda | 399 | void thread_debug_start(thash_t thash) |
2911 | svoboda | 400 | { |
401 | fid_t fid; |
||
2938 | svoboda | 402 | dthread_t *dt; |
2911 | svoboda | 403 | |
2938 | svoboda | 404 | dt = dthread_new(thash); |
2911 | svoboda | 405 | |
2938 | svoboda | 406 | fid = fibril_create(debug_loop, (void *)dt); |
2911 | svoboda | 407 | if (fid == 0) { |
2936 | svoboda | 408 | cons_printf("Warning: Failed creating fibril\n"); |
2911 | svoboda | 409 | } |
2939 | svoboda | 410 | dt->fid = fid; |
411 | |||
2911 | svoboda | 412 | fibril_add_ready(fid); |
413 | } |
||
414 | |||
3472 | svoboda | 415 | static void debug_task(task_id_t task_id) |
2911 | svoboda | 416 | { |
417 | int i; |
||
418 | int rc; |
||
419 | |||
2940 | svoboda | 420 | thash_t *thash_buffer; |
421 | int n_threads; |
||
422 | |||
423 | rc = get_thread_list(&thash_buffer, &n_threads); |
||
2911 | svoboda | 424 | if (rc < 0) { |
2940 | svoboda | 425 | cons_printf("Failed to get thread list\n", rc); |
2911 | svoboda | 426 | return; |
427 | } |
||
428 | |||
429 | abort_debug = false; |
||
430 | |||
2940 | svoboda | 431 | for (i = 0; i < n_threads; i++) { |
432 | thread_debug_start(thash_buffer[i]); |
||
2911 | svoboda | 433 | } |
434 | |||
435 | while (!quit) { |
||
2936 | svoboda | 436 | cons_read_line(in_buf, IN_BUF_SIZE); |
2911 | svoboda | 437 | command_split(in_buf); |
438 | if (cmd_argc == 0) continue; |
||
439 | |||
440 | command_run(); |
||
441 | } |
||
442 | |||
2936 | svoboda | 443 | cons_printf("terminate debugging session...\n"); |
2911 | svoboda | 444 | abort_debug = true; |
445 | udebug_end(app_phone); |
||
446 | ipc_hangup(app_phone); |
||
447 | |||
2936 | svoboda | 448 | cons_printf("done\n"); |
2911 | svoboda | 449 | return; |
450 | } |
||
451 | |||
452 | static void main_init(void) |
||
453 | { |
||
454 | next_thread_id = 1; |
||
455 | paused = 0; |
||
2938 | svoboda | 456 | |
457 | list_initialize(&dthreads); |
||
3005 | svoboda | 458 | breakpoint_init(); |
2938 | svoboda | 459 | cwt = NULL; |
2911 | svoboda | 460 | } |
461 | |||
3429 | svoboda | 462 | static void print_syntax() |
2911 | svoboda | 463 | { |
3472 | svoboda | 464 | printf("Syntax:\n"); |
465 | printf("\tdebug <executable> [<arg1> [...]]\n"); |
||
466 | printf("or\tdebug -t <task_id>\n"); |
||
3429 | svoboda | 467 | } |
2911 | svoboda | 468 | |
3472 | svoboda | 469 | static int parse_args(int argc, char *argv[]) |
3429 | svoboda | 470 | { |
3472 | svoboda | 471 | char *arg; |
3429 | svoboda | 472 | char *err_p; |
473 | |||
3472 | svoboda | 474 | task_id = 0; |
475 | |||
476 | --argc; ++argv; |
||
477 | |||
478 | while (argc > 0) { |
||
479 | arg = *argv; |
||
480 | if (arg[0] == '-') { |
||
481 | if (arg[1] == 't') { |
||
482 | /* Trace an already running task */ |
||
483 | --argc; ++argv; |
||
484 | task_id = strtol(*argv, &err_p, 10); |
||
485 | task_ldr = NULL; |
||
486 | if (*err_p) { |
||
487 | printf("Task ID syntax error\n"); |
||
488 | print_syntax(); |
||
489 | return -1; |
||
490 | } |
||
491 | } else { |
||
492 | printf("Uknown option '%s'\n", arg[0]); |
||
493 | print_syntax(); |
||
494 | return -1; |
||
495 | } |
||
496 | } else { |
||
497 | break; |
||
498 | } |
||
499 | |||
500 | --argc; ++argv; |
||
501 | } |
||
502 | |||
503 | if (task_id != 0) { |
||
504 | if (argc == 0) return 0; |
||
505 | printf("Extra arguments\n"); |
||
3429 | svoboda | 506 | print_syntax(); |
3472 | svoboda | 507 | return -1; |
2911 | svoboda | 508 | } |
3429 | svoboda | 509 | |
3472 | svoboda | 510 | if (argc < 1) { |
511 | printf("Missing argument\n"); |
||
512 | print_syntax(); |
||
513 | return -1; |
||
514 | } |
||
3429 | svoboda | 515 | |
3472 | svoboda | 516 | /* Preload the specified program file. */ |
517 | printf("Spawning '%s' with arguments:\n", *argv); |
||
518 | { |
||
519 | char **cp = argv; |
||
520 | while (*cp) printf("'%s'\n", *cp++); |
||
521 | } |
||
522 | task_ldr = preload_task(*argv, argv, &task_id); |
||
523 | |||
524 | return 0; |
||
525 | } |
||
526 | |||
527 | int main(int argc, char *argv[]) |
||
528 | { |
||
529 | int rc; |
||
530 | |||
531 | cons_printf("Breakpoint Debugger\n"); |
||
532 | |||
533 | main_init(); |
||
534 | |||
535 | if (parse_args(argc, argv) < 0) |
||
3429 | svoboda | 536 | return 1; |
3472 | svoboda | 537 | |
538 | rc = connect_task(task_id); |
||
539 | if (rc < 0) { |
||
540 | printf("Failed connecting to task %lld\n", task_id); |
||
541 | return 1; |
||
3429 | svoboda | 542 | } |
543 | |||
3472 | svoboda | 544 | cons_printf("Connected to task %lld\n", task_id); |
545 | |||
546 | if (task_ldr != NULL) { |
||
547 | program_run(); |
||
548 | } |
||
549 | |||
550 | debug_task(task_id); |
||
551 | |||
552 | return 0; |
||
2911 | svoboda | 553 | } |
554 | |||
555 | /** @} |
||
556 | */ |