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

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

Let taskdump save ELF core files. (Only memory, no register state yet.)

  • Property mode set to 100644
File size: 11.0 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 <as.h>
43#include <sys/types.h>
44#include <sys/typefmt.h>
45#include <libarch/istate.h>
46#include <macros.h>
47#include <assert.h>
48#include <bool.h>
49
50#include <symtab.h>
51#include <elf_core.h>
52#include <stacktrace.h>
53
54#define LINE_BYTES 16
55
56#define DBUF_SIZE 4096
57static uint8_t data_buf[DBUF_SIZE];
58
59static int phoneid;
60static task_id_t task_id;
61static bool write_core_file;
62static char *core_file_name;
63static char *app_name;
64static symtab_t *app_symtab;
65
66static int connect_task(task_id_t task_id);
67static int parse_args(int argc, char *argv[]);
68static void print_syntax(void);
69static int threads_dump(void);
70static int thread_dump(uintptr_t thash);
71static int areas_dump(void);
72static int area_dump(as_area_info_t *area);
73static void hex_dump(uintptr_t addr, void *buffer, size_t size);
74static int td_read_uintptr(void *arg, uintptr_t addr, uintptr_t *value);
75
76static void autoload_syms(void);
77static char *get_app_task_name(void);
78static char *fmt_sym_address(uintptr_t addr);
79
80int main(int argc, char *argv[])
81{
82 int rc;
83
84 printf("Task Dump Utility\n");
85 write_core_file = false;
86
87 if (parse_args(argc, argv) < 0)
88 return 1;
89
90 rc = connect_task(task_id);
91 if (rc < 0) {
92 printf("Failed connecting to task %" PRIdTASKID ".\n", task_id);
93 return 1;
94 }
95
96 app_name = get_app_task_name();
97 app_symtab = NULL;
98
99 printf("Dumping task '%s' (task ID %" PRIdTASKID ").\n", app_name, task_id);
100 autoload_syms();
101 putchar('\n');
102
103 rc = threads_dump();
104 if (rc < 0)
105 printf("Failed dumping threads.\n");
106
107 rc = areas_dump();
108 if (rc < 0)
109 printf("Failed dumping address space areas.\n");
110
111 udebug_end(phoneid);
112 ipc_hangup(phoneid);
113
114 return 0;
115}
116
117static int connect_task(task_id_t task_id)
118{
119 int rc;
120
121 rc = ipc_connect_kbox(task_id);
122
123 if (rc == ENOTSUP) {
124 printf("You do not have userspace debugging support "
125 "compiled in the kernel.\n");
126 printf("Compile kernel with 'Support for userspace debuggers' "
127 "(CONFIG_UDEBUG) enabled.\n");
128 return rc;
129 }
130
131 if (rc < 0) {
132 printf("Error connecting\n");
133 printf("ipc_connect_task(%" PRIdTASKID ") -> %d ", task_id, rc);
134 return rc;
135 }
136
137 phoneid = rc;
138
139 rc = udebug_begin(phoneid);
140 if (rc < 0) {
141 printf("udebug_begin() -> %d\n", rc);
142 return rc;
143 }
144
145 return 0;
146}
147
148static int parse_args(int argc, char *argv[])
149{
150 char *arg;
151 char *err_p;
152
153 task_id = 0;
154
155 --argc; ++argv;
156
157 while (argc > 0) {
158 arg = *argv;
159 if (arg[0] == '-') {
160 if (arg[1] == 't' && arg[2] == '\0') {
161 /* Task ID */
162 --argc; ++argv;
163 task_id = strtol(*argv, &err_p, 10);
164 if (*err_p) {
165 printf("Task ID syntax error\n");
166 print_syntax();
167 return -1;
168 }
169 } else if (arg[1] == 'c' && arg[2] == '\0') {
170 write_core_file = true;
171
172 --argc; ++argv;
173 core_file_name = *argv;
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 [-c <core_file>] -t <task_id>\n");
204 printf("\t-c <core_file_id>\tName of core file to write.\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: %p\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: %p size: %p\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
299 putchar('\n');
300
301 if (write_core_file) {
302 printf("Writing core file '%s'\n", core_file_name);
303 rc = elf_core_save(core_file_name, ainfo_buf, n_areas, phoneid);
304 if (rc != EOK) {
305 printf("Failed writing core file.\n");
306 return EIO;
307 }
308 }
309
310 free(ainfo_buf);
311
312 return 0;
313}
314
315static int thread_dump(uintptr_t thash)
316{
317 istate_t istate;
318 uintptr_t pc, fp, nfp;
319 stacktrace_t st;
320 char *sym_pc;
321 int rc;
322
323 rc = udebug_regs_read(phoneid, thash, &istate);
324 if (rc < 0) {
325 printf("Failed reading registers (%d).\n", rc);
326 return EIO;
327 }
328
329 pc = istate_get_pc(&istate);
330 fp = istate_get_fp(&istate);
331
332 sym_pc = fmt_sym_address(pc);
333 printf("Thread %p crashed at %s. FP = %p\n", thash, sym_pc, fp);
334 free(sym_pc);
335
336 st.op_arg = NULL;
337 st.read_uintptr = td_read_uintptr;
338
339 while (stacktrace_fp_valid(&st, fp)) {
340 sym_pc = fmt_sym_address(pc);
341 printf(" %p: %s\n", fp, sym_pc);
342 free(sym_pc);
343
344 rc = stacktrace_ra_get(&st, fp, &pc);
345 if (rc != EOK)
346 return rc;
347
348 rc = stacktrace_fp_prev(&st, fp, &nfp);
349 if (rc != EOK)
350 return rc;
351
352 fp = nfp;
353 }
354
355 return EOK;
356}
357
358static int area_dump(as_area_info_t *area)
359{
360 size_t to_copy;
361 size_t total;
362 uintptr_t addr;
363 int rc;
364
365 addr = area->start_addr;
366 total = 0;
367
368 while (total < area->size) {
369 to_copy = min(area->size - total, DBUF_SIZE);
370 rc = udebug_mem_read(phoneid, data_buf, addr, to_copy);
371 if (rc < 0) {
372 printf("udebug_mem_read() failed.\n");
373 return rc;
374 }
375
376 hex_dump(addr, data_buf, to_copy);
377
378 addr += to_copy;
379 total += to_copy;
380 }
381
382 return EOK;
383}
384
385static void hex_dump(uintptr_t addr, void *buffer, size_t size)
386{
387 uint8_t *data = (uint8_t *) buffer;
388 uint8_t b;
389 size_t pos, i;
390
391 assert(addr % LINE_BYTES == 0);
392 assert(size % LINE_BYTES == 0);
393
394 pos = 0;
395
396 while (pos < size) {
397 printf("%08lx:", addr + pos);
398 for (i = 0; i < LINE_BYTES; ++i) {
399 if (i % 4 == 0) putchar(' ');
400 printf(" %02x", data[pos + i]);
401 }
402 putchar('\t');
403
404 for (i = 0; i < LINE_BYTES; ++i) {
405 b = data[pos + i];
406 if (b >= 32 && b < 127) {
407 putchar(b);
408 } else {
409 putchar(' ');
410 }
411 }
412 putchar('\n');
413 pos += LINE_BYTES;
414 }
415}
416
417static int td_read_uintptr(void *arg, uintptr_t addr, uintptr_t *value)
418{
419 uintptr_t data;
420 int rc;
421
422 (void) arg;
423
424 rc = udebug_mem_read(phoneid, &data, addr, sizeof(data));
425 if (rc < 0) {
426 printf("Warning: udebug_mem_read() failed.\n");
427 return rc;
428 }
429
430 *value = data;
431 return EOK;
432}
433
434/** Attempt to find the right executable file and load the symbol table. */
435static void autoload_syms(void)
436{
437 char *file_name;
438 int rc;
439
440 assert(app_name != NULL);
441 assert(app_symtab == NULL);
442
443 rc = asprintf(&file_name, "/app/%s", app_name);
444 if (rc < 0) {
445 printf("Memory allocation failure.\n");
446 exit(1);
447 }
448
449 rc = symtab_load(file_name, &app_symtab);
450 if (rc == EOK) {
451 printf("Loaded symbol table from %s\n", file_name);
452 free(file_name);
453 return;
454 }
455
456 free(file_name);
457
458 rc = asprintf(&file_name, "/srv/%s", app_name);
459 if (rc < 0) {
460 printf("Memory allocation failure.\n");
461 exit(1);
462 }
463
464 rc = symtab_load(file_name, &app_symtab);
465 if (rc == EOK) {
466 printf("Loaded symbol table from %s\n", file_name);
467 free(file_name);
468 return;
469 }
470
471 free(file_name);
472 printf("Failed autoloading symbol table.\n");
473}
474
475static char *get_app_task_name(void)
476{
477 char dummy_buf;
478 size_t copied, needed, name_size;
479 char *name;
480 int rc;
481
482 rc = udebug_name_read(phoneid, &dummy_buf, 0, &copied, &needed);
483 if (rc < 0)
484 return NULL;
485
486 name_size = needed;
487 name = malloc(name_size + 1);
488 rc = udebug_name_read(phoneid, name, name_size, &copied, &needed);
489 if (rc < 0) {
490 free(name);
491 return NULL;
492 }
493
494 assert(copied == name_size);
495 assert(copied == needed);
496 name[copied] = '\0';
497
498 return name;
499}
500
501/** Format address in symbolic form.
502 *
503 * Formats address as <symbol_name>+<offset> (<address>), if possible,
504 * otherwise as <address>.
505 *
506 * @param addr Address to format.
507 * @return Newly allocated string, address in symbolic form.
508 */
509static char *fmt_sym_address(uintptr_t addr)
510{
511 char *name;
512 size_t offs;
513 int rc;
514 char *str;
515
516 if (app_symtab != NULL) {
517 rc = symtab_addr_to_name(app_symtab, addr, &name, &offs);
518 } else {
519 rc = ENOTSUP;
520 }
521
522 if (rc == EOK) {
523 rc = asprintf(&str, "%p (%s+%p)", addr, name, offs);
524 } else {
525 rc = asprintf(&str, "%p", addr);
526 }
527
528 if (rc < 0) {
529 printf("Memory allocation error.\n");
530 exit(1);
531 }
532
533 return str;
534}
535
536/** @}
537 */
Note: See TracBrowser for help on using the repository browser.