source: mainline/uspace/app/taskdump/taskdump.c@ 82d515e9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 82d515e9 was 7354b5e, checked in by Jakub Jermar <jakub@…>, 8 years ago

Remove sys/typefmt.h

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