source: mainline/uspace/app/taskdump/taskdump.c@ 3e6a98c5

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

Standards-compliant boolean type.

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