source: mainline/uspace/app/taskdump/taskdump.c@ 984a9ba

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

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

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