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

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

Taskdump printing of fibril stacktraces.

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