source: mainline/uspace/app/taskdump/taskdump.c@ 196a1439

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

Slightly improve taskdump output.

  • 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 sym_pc = fmt_sym_address(pc);
330 printf("Thread 0x%lx crashed at %s. FP = 0x%lx\n", thash, sym_pc, fp);
331 free(sym_pc);
332
333 st.op_arg = NULL;
334 st.read_uintptr = td_read_uintptr;
335
336 while (stacktrace_fp_valid(&st, fp)) {
337 sym_pc = fmt_sym_address(pc);
338 printf(" %p: %s\n", fp, sym_pc);
339 free(sym_pc);
340
341 rc = stacktrace_ra_get(&st, fp, &pc);
342 if (rc != EOK)
343 return rc;
344
345 rc = stacktrace_fp_prev(&st, fp, &nfp);
346 if (rc != EOK)
347 return rc;
348
349 fp = nfp;
350 }
351
352 return EOK;
353}
354
355static int area_dump(as_area_info_t *area)
356{
357 size_t to_copy;
358 size_t total;
359 uintptr_t addr;
360 int rc;
361
362 addr = area->start_addr;
363 total = 0;
364
365 while (total < area->size) {
366 to_copy = min(area->size - total, DBUF_SIZE);
367 rc = udebug_mem_read(phoneid, data_buf, addr, to_copy);
368 if (rc < 0) {
369 printf("udebug_mem_read() failed.\n");
370 return rc;
371 }
372
373 hex_dump(addr, data_buf, to_copy);
374
375 addr += to_copy;
376 total += to_copy;
377 }
378
379 return EOK;
380}
381
382static void hex_dump(uintptr_t addr, void *buffer, size_t size)
383{
384 uint8_t *data = (uint8_t *) buffer;
385 uint8_t b;
386 size_t pos, i;
387
388 assert(addr % LINE_BYTES == 0);
389 assert(size % LINE_BYTES == 0);
390
391 pos = 0;
392
393 while (pos < size) {
394 printf("%08lx:", addr + pos);
395 for (i = 0; i < LINE_BYTES; ++i) {
396 if (i % 4 == 0) putchar(' ');
397 printf(" %02x", data[pos + i]);
398 }
399 putchar('\t');
400
401 for (i = 0; i < LINE_BYTES; ++i) {
402 b = data[pos + i];
403 if (b >= 32 && b < 127) {
404 putchar(b);
405 } else {
406 putchar(' ');
407 }
408 }
409 putchar('\n');
410 pos += LINE_BYTES;
411 }
412}
413
414static int td_read_uintptr(void *arg, uintptr_t addr, uintptr_t *value)
415{
416 uintptr_t data;
417 int rc;
418
419 (void) arg;
420
421 rc = udebug_mem_read(phoneid, &data, addr, sizeof(data));
422 if (rc < 0) {
423 printf("Warning: udebug_mem_read() failed.\n");
424 return rc;
425 }
426
427 *value = data;
428 return EOK;
429}
430
431/** Attempt to find the right executable file and load the symbol table. */
432static void autoload_syms(void)
433{
434 char *file_name;
435 int rc;
436
437 assert(app_name != NULL);
438 assert(app_symtab == NULL);
439
440 rc = asprintf(&file_name, "/app/%s", app_name);
441 if (rc < 0) {
442 printf("Memory allocation failure.\n");
443 exit(1);
444 }
445
446 rc = symtab_load(file_name, &app_symtab);
447 if (rc == EOK) {
448 printf("Loaded symbol table from %s\n", file_name);
449 free(file_name);
450 return;
451 }
452
453 free(file_name);
454
455 rc = asprintf(&file_name, "/srv/%s", app_name);
456 if (rc < 0) {
457 printf("Memory allocation failure.\n");
458 exit(1);
459 }
460
461 rc = symtab_load(file_name, &app_symtab);
462 if (rc == EOK) {
463 printf("Loaded symbol table from %s\n", file_name);
464 free(file_name);
465 return;
466 }
467
468 free(file_name);
469 printf("Failed autoloading symbol table.\n");
470}
471
472static char *get_app_task_name(void)
473{
474 char dummy_buf;
475 size_t copied, needed, name_size;
476 char *name;
477 int rc;
478
479 rc = udebug_name_read(phoneid, &dummy_buf, 0, &copied, &needed);
480 if (rc < 0)
481 return NULL;
482
483 name_size = needed;
484 name = malloc(name_size + 1);
485 rc = udebug_name_read(phoneid, name, name_size, &copied, &needed);
486 if (rc < 0) {
487 free(name);
488 return NULL;
489 }
490
491 assert(copied == name_size);
492 assert(copied == needed);
493 name[copied] = '\0';
494
495 return name;
496}
497
498/** Format address in symbolic form.
499 *
500 * Formats address as <symbol_name>+<offset> (<address>), if possible,
501 * otherwise as <address>.
502 *
503 * @param addr Address to format.
504 * @return Newly allocated string, address in symbolic form.
505 */
506static char *fmt_sym_address(uintptr_t addr)
507{
508 char *name;
509 size_t offs;
510 int rc;
511 char *str;
512
513 if (app_symtab != NULL) {
514 rc = symtab_addr_to_name(app_symtab, addr, &name, &offs);
515 } else {
516 rc = ENOTSUP;
517 }
518
519 if (rc == EOK) {
520 rc = asprintf(&str, "%p (%s+%p)", addr, name, offs);
521 } else {
522 rc = asprintf(&str, "%p", addr);
523 }
524
525 if (rc < 0) {
526 printf("Memory allocation error.\n");
527 exit(1);
528 }
529
530 return str;
531}
532
533/** @}
534 */
Note: See TracBrowser for help on using the repository browser.