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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d5c1051 was d5c1051, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

"Obviously harmless" error handling tweaks.

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