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
RevLine 
[336db295]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
[ffa2c8ef]35#include <async.h>
[5baf209]36#include <elf/elf_linux.h>
[c1b979a]37#include <fibrildump.h>
[336db295]38#include <stdio.h>
39#include <stdlib.h>
[8d2dd7f2]40#include <stddef.h>
41#include <stdbool.h>
[9af1c61]42#include <str_error.h>
[336db295]43#include <errno.h>
44#include <udebug.h>
45#include <task.h>
[dafa2d04]46#include <as.h>
[80487bc5]47#include <libarch/istate.h>
[336db295]48#include <macros.h>
49#include <assert.h>
50
[3698e44]51#include <symtab.h>
[dafa2d04]52#include <elf_core.h>
[e515b21a]53#include <stacktrace.h>
[c1b979a]54#include <taskdump.h>
[e515b21a]55
[336db295]56#define LINE_BYTES 16
57
[79ae36dd]58static async_sess_t *sess;
[336db295]59static task_id_t task_id;
[dafa2d04]60static bool write_core_file;
61static char *core_file_name;
[3698e44]62static char *app_name;
63static symtab_t *app_symtab;
[336db295]64
65static int connect_task(task_id_t task_id);
66static int parse_args(int argc, char *argv[]);
[e515b21a]67static void print_syntax(void);
[336db295]68static int threads_dump(void);
[80487bc5]69static int thread_dump(uintptr_t thash);
[336db295]70static int areas_dump(void);
[e515b21a]71static int td_read_uintptr(void *arg, uintptr_t addr, uintptr_t *value);
[336db295]72
[3698e44]73static void autoload_syms(void);
74static char *get_app_task_name(void);
75static char *fmt_sym_address(uintptr_t addr);
76
[5baf209]77static istate_t reg_state;
78
[c1b979a]79static stacktrace_ops_t td_stacktrace_ops = {
80 .read_uintptr = td_read_uintptr
81};
82
[336db295]83int main(int argc, char *argv[])
84{
85 int rc;
86
87 printf("Task Dump Utility\n");
[dafa2d04]88 write_core_file = false;
[336db295]89
90 if (parse_args(argc, argv) < 0)
91 return 1;
92
93 rc = connect_task(task_id);
[d5c1051]94 if (rc != EOK) {
[7e752b2]95 printf("Failed connecting to task %" PRIu64 ".\n", task_id);
[336db295]96 return 1;
97 }
98
[3698e44]99 app_name = get_app_task_name();
100 app_symtab = NULL;
101
[7e752b2]102 printf("Dumping task '%s' (task ID %" PRIu64 ").\n", app_name, task_id);
[3698e44]103 autoload_syms();
104 putchar('\n');
[336db295]105
106 rc = threads_dump();
[d5c1051]107 if (rc != EOK)
[336db295]108 printf("Failed dumping threads.\n");
109
110 rc = areas_dump();
[d5c1051]111 if (rc != EOK)
[336db295]112 printf("Failed dumping address space areas.\n");
113
[c1b979a]114 rc = fibrils_dump(app_symtab, sess);
[d5c1051]115 if (rc != EOK)
[c1b979a]116 printf("Failed dumping fibrils.\n");
117
[79ae36dd]118 udebug_end(sess);
119 async_hangup(sess);
[336db295]120
121 return 0;
122}
123
124static int connect_task(task_id_t task_id)
125{
[79ae36dd]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
[336db295]137 printf("Error connecting\n");
[9af1c61]138 printf("async_connect_kbox(%" PRIu64 ") -> %s", task_id, str_error_name(errno));
[79ae36dd]139 return errno;
[336db295]140 }
[79ae36dd]141
142 int rc = udebug_begin(ksess);
[d5c1051]143 if (rc != EOK) {
[c1694b6b]144 printf("udebug_begin() -> %s\n", str_error_name(rc));
[336db295]145 return rc;
146 }
[79ae36dd]147
148 sess = ksess;
[336db295]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 }
[dafa2d04]173 } else if (arg[1] == 'c' && arg[2] == '\0') {
174 write_core_file = true;
175
176 --argc; ++argv;
177 core_file_name = *argv;
[336db295]178 } else {
[7e752b2]179 printf("Uknown option '%c'\n", arg[0]);
[336db295]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
[e515b21a]205static void print_syntax(void)
[336db295]206{
[dafa2d04]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");
[336db295]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. */
[79ae36dd]224 rc = udebug_thread_read(sess, &dummy_buf, 0, &copied, &needed);
[d5c1051]225 if (rc != EOK) {
[c1694b6b]226 printf("udebug_thread_read() -> %s\n", str_error_name(rc));
[336db295]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
[79ae36dd]238 rc = udebug_thread_read(sess, thash_buf, buf_size, &copied, &needed);
[d5c1051]239 if (rc != EOK) {
[c1694b6b]240 printf("udebug_thread_read() -> %s\n", str_error_name(rc));
[336db295]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++) {
[7e752b2]251 printf(" [%zu] hash: %p\n", 1 + i, (void *) thash_buf[i]);
[80487bc5]252
253 thread_dump(thash_buf[i]);
[336db295]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
[79ae36dd]273 rc = udebug_areas_read(sess, &dummy_buf, 0, &copied, &needed);
[d5c1051]274 if (rc != EOK) {
[c1694b6b]275 printf("udebug_areas_read() -> %s\n", str_error_name(rc));
[336db295]276 return rc;
277 }
278
279 buf_size = needed;
280 ainfo_buf = malloc(buf_size);
281
[79ae36dd]282 rc = udebug_areas_read(sess, ainfo_buf, buf_size, &copied, &needed);
[d5c1051]283 if (rc != EOK) {
[c1694b6b]284 printf("udebug_areas_read() -> %s\n", str_error_name(rc));
[336db295]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++) {
[7e752b2]295 printf(" [%zu] flags: %c%c%c%c base: %p size: %zu\n", 1 + i,
[336db295]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' : '-',
[7e752b2]300 (void *) ainfo_buf[i].start_addr, ainfo_buf[i].size);
[336db295]301 }
302
303 putchar('\n');
304
[dafa2d04]305 if (write_core_file) {
306 printf("Writing core file '%s'\n", core_file_name);
[5baf209]307
308 rc = elf_core_save(core_file_name, ainfo_buf, n_areas, sess,
309 &reg_state);
310
[dafa2d04]311 if (rc != EOK) {
312 printf("Failed writing core file.\n");
313 return EIO;
314 }
315 }
316
[336db295]317 free(ainfo_buf);
318
319 return 0;
320}
321
[c1b979a]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
[80487bc5]351static int thread_dump(uintptr_t thash)
352{
353 istate_t istate;
[c1b979a]354 uintptr_t pc, fp;
[3698e44]355 char *sym_pc;
[80487bc5]356 int rc;
357
[79ae36dd]358 rc = udebug_regs_read(sess, thash, &istate);
[d5c1051]359 if (rc != EOK) {
[c1694b6b]360 printf("Failed reading registers: %s.\n", str_error_name(rc));
[80487bc5]361 return EIO;
362 }
363
364 pc = istate_get_pc(&istate);
365 fp = istate_get_fp(&istate);
366
[5baf209]367 /* Save register state for dumping to core file later. */
368 reg_state = istate;
369
[196a1439]370 sym_pc = fmt_sym_address(pc);
[63594c0]371 printf("Thread %p: PC = %s. FP = %p\n", (void *) thash,
[7e752b2]372 sym_pc, (void *) fp);
[196a1439]373 free(sym_pc);
[e515b21a]374
[c1b979a]375 (void) td_stacktrace(fp, pc);
[80487bc5]376
377 return EOK;
378}
379
[e515b21a]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
[79ae36dd]387 rc = udebug_mem_read(sess, &data, addr, sizeof(data));
[d5c1051]388 if (rc != EOK) {
[e515b21a]389 printf("Warning: udebug_mem_read() failed.\n");
390 return rc;
391 }
392
393 *value = data;
394 return EOK;
395}
396
[3698e44]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;
[d5c1051]402 int ret;
[3698e44]403
404 assert(app_name != NULL);
405 assert(app_symtab == NULL);
406
[d5c1051]407 ret = asprintf(&file_name, "/app/%s", app_name);
408 if (ret < 0) {
[3698e44]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
[d5c1051]422 ret = asprintf(&file_name, "/srv/%s", app_name);
423 if (ret < 0) {
[3698e44]424 printf("Memory allocation failure.\n");
425 exit(1);
426 }
427
[196a1439]428 rc = symtab_load(file_name, &app_symtab);
[3698e44]429 if (rc == EOK) {
430 printf("Loaded symbol table from %s\n", file_name);
431 free(file_name);
432 return;
433 }
434
[d5c1051]435 ret = asprintf(&file_name, "/drv/%s/%s", app_name, app_name);
436 if (ret < 0) {
[6a343bdf]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
[3698e44]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
[79ae36dd]459 rc = udebug_name_read(sess, &dummy_buf, 0, &copied, &needed);
[d5c1051]460 if (rc != EOK)
[3698e44]461 return NULL;
462
463 name_size = needed;
464 name = malloc(name_size + 1);
[79ae36dd]465 rc = udebug_name_read(sess, name, name_size, &copied, &needed);
[d5c1051]466 if (rc != EOK) {
[3698e44]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;
[d5c1051]491 int ret;
[3698e44]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) {
[d5c1051]501 ret = asprintf(&str, "%p (%s+%zu)", (void *) addr, name, offs);
[3698e44]502 } else {
[d5c1051]503 ret = asprintf(&str, "%p", (void *) addr);
[3698e44]504 }
505
[d5c1051]506 if (ret < 0) {
[3698e44]507 printf("Memory allocation error.\n");
508 exit(1);
509 }
510
511 return str;
512}
513
[336db295]514/** @}
515 */
Note: See TracBrowser for help on using the repository browser.