source: mainline/uspace/app/taskdump/taskdump.c@ bca408b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bca408b was 3698e44, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Add ability to determine task name and load symbol table from the binary executable. Resolve symbol names in stack traces when dumping.

  • Property mode set to 100644
File size: 10.8 KB
Line 
1/*
2 * Copyright (c) 2010 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 taskdump
30 * @{
31 */
32/** @file
33 */
34
35#include <stdio.h>
36#include <stdlib.h>
37#include <unistd.h>
38#include <ipc/ipc.h>
39#include <errno.h>
40#include <udebug.h>
41#include <task.h>
42#include <kernel/mm/as.h>
43#include <libarch/istate.h>
44#include <macros.h>
45#include <assert.h>
46#include <bool.h>
47
48#include <symtab.h>
49#include <stacktrace.h>
50
51#define LINE_BYTES 16
52
53#define DBUF_SIZE 4096
54static uint8_t data_buf[DBUF_SIZE];
55
56static int phoneid;
57static task_id_t task_id;
58static bool dump_memory;
59static char *app_name;
60static symtab_t *app_symtab;
61
62static int connect_task(task_id_t task_id);
63static int parse_args(int argc, char *argv[]);
64static void print_syntax(void);
65static int threads_dump(void);
66static int thread_dump(uintptr_t thash);
67static int areas_dump(void);
68static int area_dump(as_area_info_t *area);
69static void hex_dump(uintptr_t addr, void *buffer, size_t size);
70static int td_read_uintptr(void *arg, uintptr_t addr, uintptr_t *value);
71
72static void autoload_syms(void);
73static char *get_app_task_name(void);
74static char *fmt_sym_address(uintptr_t addr);
75
76int main(int argc, char *argv[])
77{
78 int rc;
79
80 /*
81 * FIXME: The stdio module cannot currently detect whether we are
82 * writing to a console or file. This workaround make file output
83 * faster.
84 */
85 setvbuf(stdout, NULL, _IOFBF, 32768);
86
87 printf("Task Dump Utility\n");
88 dump_memory = false;
89
90 if (parse_args(argc, argv) < 0)
91 return 1;
92
93 rc = connect_task(task_id);
94 if (rc < 0) {
95 printf("Failed connecting to task %lld.\n", task_id);
96 return 1;
97 }
98
99 app_name = get_app_task_name();
100 app_symtab = NULL;
101
102 printf("Dumping task '%s' (task ID %lld).\n", app_name, task_id);
103 autoload_syms();
104 putchar('\n');
105
106 rc = threads_dump();
107 if (rc < 0)
108 printf("Failed dumping threads.\n");
109
110 rc = areas_dump();
111 if (rc < 0)
112 printf("Failed dumping address space areas.\n");
113
114 udebug_end(phoneid);
115 ipc_hangup(phoneid);
116
117 return 0;
118}
119
120static int connect_task(task_id_t task_id)
121{
122 int rc;
123
124 rc = ipc_connect_kbox(task_id);
125
126 if (rc == ENOTSUP) {
127 printf("You do not have userspace debugging support "
128 "compiled in the kernel.\n");
129 printf("Compile kernel with 'Support for userspace debuggers' "
130 "(CONFIG_UDEBUG) enabled.\n");
131 return rc;
132 }
133
134 if (rc < 0) {
135 printf("Error connecting\n");
136 printf("ipc_connect_task(%lld) -> %d ", task_id, rc);
137 return rc;
138 }
139
140 phoneid = rc;
141
142 rc = udebug_begin(phoneid);
143 if (rc < 0) {
144 printf("udebug_begin() -> %d\n", rc);
145 return rc;
146 }
147
148 return 0;
149}
150
151static int parse_args(int argc, char *argv[])
152{
153 char *arg;
154 char *err_p;
155
156 task_id = 0;
157
158 --argc; ++argv;
159
160 while (argc > 0) {
161 arg = *argv;
162 if (arg[0] == '-') {
163 if (arg[1] == 't' && arg[2] == '\0') {
164 /* Task ID */
165 --argc; ++argv;
166 task_id = strtol(*argv, &err_p, 10);
167 if (*err_p) {
168 printf("Task ID syntax error\n");
169 print_syntax();
170 return -1;
171 }
172 } else if (arg[1] == 'm' && arg[2] == '\0') {
173 dump_memory = true;
174 } else {
175 printf("Uknown option '%s'\n", arg[0]);
176 print_syntax();
177 return -1;
178 }
179 } else {
180 break;
181 }
182
183 --argc; ++argv;
184 }
185
186 if (task_id == 0) {
187 printf("Missing task ID argument\n");
188 print_syntax();
189 return -1;
190 }
191
192 if (argc != 0) {
193 printf("Extra arguments\n");
194 print_syntax();
195 return -1;
196 }
197
198 return 0;
199}
200
201static void print_syntax(void)
202{
203 printf("Syntax: taskdump [-m] -t <task_id>\n");
204 printf("\t-m\tDump memory area contents.\n");
205 printf("\t-t <task_id>\tWhich task to dump.\n");
206}
207
208static int threads_dump(void)
209{
210 uintptr_t *thash_buf;
211 uintptr_t dummy_buf;
212 size_t buf_size, n_threads;
213
214 size_t copied;
215 size_t needed;
216 size_t i;
217 int rc;
218
219 /* TODO: See why NULL does not work. */
220 rc = udebug_thread_read(phoneid, &dummy_buf, 0, &copied, &needed);
221 if (rc < 0) {
222 printf("udebug_thread_read() -> %d\n", rc);
223 return rc;
224 }
225
226 if (needed == 0) {
227 printf("No threads.\n\n");
228 return 0;
229 }
230
231 buf_size = needed;
232 thash_buf = malloc(buf_size);
233
234 rc = udebug_thread_read(phoneid, thash_buf, buf_size, &copied, &needed);
235 if (rc < 0) {
236 printf("udebug_thread_read() -> %d\n", rc);
237 return rc;
238 }
239
240 assert(copied == buf_size);
241 assert(needed == buf_size);
242
243 n_threads = copied / sizeof(uintptr_t);
244
245 printf("Threads:\n");
246 for (i = 0; i < n_threads; i++) {
247 printf(" [%d] hash: 0x%lx\n", 1+i, thash_buf[i]);
248
249 thread_dump(thash_buf[i]);
250 }
251 putchar('\n');
252
253 free(thash_buf);
254
255 return 0;
256}
257
258static int areas_dump(void)
259{
260 as_area_info_t *ainfo_buf;
261 as_area_info_t dummy_buf;
262 size_t buf_size, n_areas;
263
264 size_t copied;
265 size_t needed;
266 size_t i;
267 int rc;
268
269 rc = udebug_areas_read(phoneid, &dummy_buf, 0, &copied, &needed);
270 if (rc < 0) {
271 printf("udebug_areas_read() -> %d\n", rc);
272 return rc;
273 }
274
275 buf_size = needed;
276 ainfo_buf = malloc(buf_size);
277
278 rc = udebug_areas_read(phoneid, ainfo_buf, buf_size, &copied, &needed);
279 if (rc < 0) {
280 printf("udebug_areas_read() -> %d\n", rc);
281 return rc;
282 }
283
284 assert(copied == buf_size);
285 assert(needed == buf_size);
286
287 n_areas = copied / sizeof(as_area_info_t);
288
289 printf("Address space areas:\n");
290 for (i = 0; i < n_areas; i++) {
291 printf(" [%d] flags: %c%c%c%c base: 0x%lx size: 0x%lx\n", 1+i,
292 (ainfo_buf[i].flags & AS_AREA_READ) ? 'R' : '-',
293 (ainfo_buf[i].flags & AS_AREA_WRITE) ? 'W' : '-',
294 (ainfo_buf[i].flags & AS_AREA_EXEC) ? 'X' : '-',
295 (ainfo_buf[i].flags & AS_AREA_CACHEABLE) ? 'C' : '-',
296 ainfo_buf[i].start_addr, ainfo_buf[i].size);
297
298 if (dump_memory) {
299 putchar('\n');
300 area_dump(&ainfo_buf[i]);
301 putchar('\n');
302 }
303 }
304
305 putchar('\n');
306
307 free(ainfo_buf);
308
309 return 0;
310}
311
312static int thread_dump(uintptr_t thash)
313{
314 istate_t istate;
315 uintptr_t pc, fp, nfp;
316 stacktrace_t st;
317 char *sym_pc;
318 int rc;
319
320 rc = udebug_regs_read(phoneid, thash, &istate);
321 if (rc < 0) {
322 printf("Failed reading registers (%d).\n", rc);
323 return EIO;
324 }
325
326 pc = istate_get_pc(&istate);
327 fp = istate_get_fp(&istate);
328
329 printf("Thread 0x%lx crashed at PC 0x%lx. FP 0x%lx\n", thash, pc, fp);
330
331 st.op_arg = NULL;
332 st.read_uintptr = td_read_uintptr;
333
334 while (stacktrace_fp_valid(&st, fp)) {
335 sym_pc = fmt_sym_address(pc);
336 printf(" %p: %s()\n", fp, sym_pc);
337 free(sym_pc);
338
339 rc = stacktrace_ra_get(&st, fp, &pc);
340 if (rc != EOK)
341 return rc;
342
343 rc = stacktrace_fp_prev(&st, fp, &nfp);
344 if (rc != EOK)
345 return rc;
346
347 fp = nfp;
348 }
349
350 return EOK;
351}
352
353static int area_dump(as_area_info_t *area)
354{
355 size_t to_copy;
356 size_t total;
357 uintptr_t addr;
358 int rc;
359
360 addr = area->start_addr;
361 total = 0;
362
363 while (total < area->size) {
364 to_copy = min(area->size - total, DBUF_SIZE);
365 rc = udebug_mem_read(phoneid, data_buf, addr, to_copy);
366 if (rc < 0) {
367 printf("udebug_mem_read() failed.\n");
368 return rc;
369 }
370
371 hex_dump(addr, data_buf, to_copy);
372
373 addr += to_copy;
374 total += to_copy;
375 }
376
377 return EOK;
378}
379
380static void hex_dump(uintptr_t addr, void *buffer, size_t size)
381{
382 uint8_t *data = (uint8_t *) buffer;
383 uint8_t b;
384 size_t pos, i;
385
386 assert(addr % LINE_BYTES == 0);
387 assert(size % LINE_BYTES == 0);
388
389 pos = 0;
390
391 while (pos < size) {
392 printf("%08x:", addr + pos);
393 for (i = 0; i < LINE_BYTES; ++i) {
394 if (i % 4 == 0) putchar(' ');
395 printf(" %02x", data[pos + i]);
396 }
397 putchar('\t');
398
399 for (i = 0; i < LINE_BYTES; ++i) {
400 b = data[pos + i];
401 if (b >= 32 && b < 127) {
402 putchar(b);
403 } else {
404 putchar(' ');
405 }
406 }
407 putchar('\n');
408 pos += LINE_BYTES;
409 }
410}
411
412static int td_read_uintptr(void *arg, uintptr_t addr, uintptr_t *value)
413{
414 uintptr_t data;
415 int rc;
416
417 (void) arg;
418
419 rc = udebug_mem_read(phoneid, &data, addr, sizeof(data));
420 if (rc < 0) {
421 printf("Warning: udebug_mem_read() failed.\n");
422 return rc;
423 }
424
425 *value = data;
426 return EOK;
427}
428
429/** Attempt to find the right executable file and load the symbol table. */
430static void autoload_syms(void)
431{
432 char *file_name;
433 int rc;
434
435 assert(app_name != NULL);
436 assert(app_symtab == NULL);
437
438 rc = asprintf(&file_name, "/app/%s", app_name);
439 if (rc < 0) {
440 printf("Memory allocation failure.\n");
441 exit(1);
442 }
443
444 rc = symtab_load(file_name, &app_symtab);
445 if (rc == EOK) {
446 printf("Loaded symbol table from %s\n", file_name);
447 free(file_name);
448 return;
449 }
450
451 free(file_name);
452
453 rc = asprintf(&file_name, "/srv/%s", app_name);
454 if (rc < 0) {
455 printf("Memory allocation failure.\n");
456 exit(1);
457 }
458
459 rc = symtab_load("/srv/xyz", &app_symtab);
460 if (rc == EOK) {
461 printf("Loaded symbol table from %s\n", file_name);
462 free(file_name);
463 return;
464 }
465
466 free(file_name);
467 printf("Failed autoloading symbol table.\n");
468}
469
470static char *get_app_task_name(void)
471{
472 char dummy_buf;
473 size_t copied, needed, name_size;
474 char *name;
475 int rc;
476
477 rc = udebug_name_read(phoneid, &dummy_buf, 0, &copied, &needed);
478 if (rc < 0)
479 return NULL;
480
481 name_size = needed;
482 name = malloc(name_size + 1);
483 rc = udebug_name_read(phoneid, name, name_size, &copied, &needed);
484 if (rc < 0) {
485 free(name);
486 return NULL;
487 }
488
489 assert(copied == name_size);
490 assert(copied == needed);
491 name[copied] = '\0';
492
493 return name;
494}
495
496/** Format address in symbolic form.
497 *
498 * Formats address as <symbol_name>+<offset> (<address>), if possible,
499 * otherwise as <address>.
500 *
501 * @param addr Address to format.
502 * @return Newly allocated string, address in symbolic form.
503 */
504static char *fmt_sym_address(uintptr_t addr)
505{
506 char *name;
507 size_t offs;
508 int rc;
509 char *str;
510
511 if (app_symtab != NULL) {
512 rc = symtab_addr_to_name(app_symtab, addr, &name, &offs);
513 } else {
514 rc = ENOTSUP;
515 }
516
517 if (rc == EOK) {
518 rc = asprintf(&str, "(%p) %s+%p", addr, name, offs);
519 } else {
520 rc = asprintf(&str, "%p", addr);
521 }
522
523 if (rc < 0) {
524 printf("Memory allocation error.\n");
525 exit(1);
526 }
527
528 return str;
529}
530
531/** @}
532 */
Note: See TracBrowser for help on using the repository browser.