source: mainline/uspace/app/taskdump/taskdump.c@ 202f57b

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

Fix warnings in taskdump.c.

  • Property mode set to 100644
File size: 9.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 <stdio.h>
36#include <stdlib.h>
37#include <unistd.h>
38#include <ipc/ipc.h>
39#include <errno.h>
40#include <udebug.h>
41#include <task.h>
42#include <as.h>
43#include <sys/types.h>
44#include <sys/typefmt.h>
45#include <libarch/istate.h>
46#include <macros.h>
47#include <assert.h>
48#include <bool.h>
49
50#include <symtab.h>
51#include <elf_core.h>
52#include <stacktrace.h>
53
54#define LINE_BYTES 16
55
56static int phoneid;
57static task_id_t task_id;
58static bool write_core_file;
59static char *core_file_name;
60static char *app_name;
61static symtab_t *app_symtab;
62
63static int connect_task(task_id_t task_id);
64static int parse_args(int argc, char *argv[]);
65static void print_syntax(void);
66static int threads_dump(void);
67static int thread_dump(uintptr_t thash);
68static int areas_dump(void);
69static int td_read_uintptr(void *arg, uintptr_t addr, uintptr_t *value);
70
71static void autoload_syms(void);
72static char *get_app_task_name(void);
73static char *fmt_sym_address(uintptr_t addr);
74
75int main(int argc, char *argv[])
76{
77 int rc;
78
79 printf("Task Dump Utility\n");
80 write_core_file = false;
81
82 if (parse_args(argc, argv) < 0)
83 return 1;
84
85 rc = connect_task(task_id);
86 if (rc < 0) {
87 printf("Failed connecting to task %" PRIdTASKID ".\n", task_id);
88 return 1;
89 }
90
91 app_name = get_app_task_name();
92 app_symtab = NULL;
93
94 printf("Dumping task '%s' (task ID %" PRIdTASKID ").\n", app_name, task_id);
95 autoload_syms();
96 putchar('\n');
97
98 rc = threads_dump();
99 if (rc < 0)
100 printf("Failed dumping threads.\n");
101
102 rc = areas_dump();
103 if (rc < 0)
104 printf("Failed dumping address space areas.\n");
105
106 udebug_end(phoneid);
107 ipc_hangup(phoneid);
108
109 return 0;
110}
111
112static int connect_task(task_id_t task_id)
113{
114 int rc;
115
116 rc = ipc_connect_kbox(task_id);
117
118 if (rc == ENOTSUP) {
119 printf("You do not have userspace debugging support "
120 "compiled in the kernel.\n");
121 printf("Compile kernel with 'Support for userspace debuggers' "
122 "(CONFIG_UDEBUG) enabled.\n");
123 return rc;
124 }
125
126 if (rc < 0) {
127 printf("Error connecting\n");
128 printf("ipc_connect_task(%" PRIdTASKID ") -> %d ", task_id, rc);
129 return rc;
130 }
131
132 phoneid = rc;
133
134 rc = udebug_begin(phoneid);
135 if (rc < 0) {
136 printf("udebug_begin() -> %d\n", rc);
137 return rc;
138 }
139
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 '%s'\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(phoneid, &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(phoneid, 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(" [%d] hash: %p\n", 1+i, 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(phoneid, &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(phoneid, 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(" [%d] flags: %c%c%c%c base: %p size: %p\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 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 rc = elf_core_save(core_file_name, ainfo_buf, n_areas, phoneid);
299 if (rc != EOK) {
300 printf("Failed writing core file.\n");
301 return EIO;
302 }
303 }
304
305 free(ainfo_buf);
306
307 return 0;
308}
309
310static int thread_dump(uintptr_t thash)
311{
312 istate_t istate;
313 uintptr_t pc, fp, nfp;
314 stacktrace_t st;
315 char *sym_pc;
316 int rc;
317
318 rc = udebug_regs_read(phoneid, thash, &istate);
319 if (rc < 0) {
320 printf("Failed reading registers (%d).\n", rc);
321 return EIO;
322 }
323
324 pc = istate_get_pc(&istate);
325 fp = istate_get_fp(&istate);
326
327 sym_pc = fmt_sym_address(pc);
328 printf("Thread %p crashed at %s. FP = %p\n", thash, sym_pc, fp);
329 free(sym_pc);
330
331 st.op_arg = NULL;
332 st.read_uintptr = td_read_uintptr;
333
334 while (stacktrace_fp_valid(&st, fp)) {
335 sym_pc = fmt_sym_address(pc);
336 printf(" %p: %s\n", fp, sym_pc);
337 free(sym_pc);
338
339 rc = stacktrace_ra_get(&st, fp, &pc);
340 if (rc != EOK)
341 return rc;
342
343 rc = stacktrace_fp_prev(&st, fp, &nfp);
344 if (rc != EOK)
345 return rc;
346
347 fp = nfp;
348 }
349
350 return EOK;
351}
352
353static int td_read_uintptr(void *arg, uintptr_t addr, uintptr_t *value)
354{
355 uintptr_t data;
356 int rc;
357
358 (void) arg;
359
360 rc = udebug_mem_read(phoneid, &data, addr, sizeof(data));
361 if (rc < 0) {
362 printf("Warning: udebug_mem_read() failed.\n");
363 return rc;
364 }
365
366 *value = data;
367 return EOK;
368}
369
370/** Attempt to find the right executable file and load the symbol table. */
371static void autoload_syms(void)
372{
373 char *file_name;
374 int rc;
375
376 assert(app_name != NULL);
377 assert(app_symtab == NULL);
378
379 rc = asprintf(&file_name, "/app/%s", app_name);
380 if (rc < 0) {
381 printf("Memory allocation failure.\n");
382 exit(1);
383 }
384
385 rc = symtab_load(file_name, &app_symtab);
386 if (rc == EOK) {
387 printf("Loaded symbol table from %s\n", file_name);
388 free(file_name);
389 return;
390 }
391
392 free(file_name);
393
394 rc = asprintf(&file_name, "/srv/%s", app_name);
395 if (rc < 0) {
396 printf("Memory allocation failure.\n");
397 exit(1);
398 }
399
400 rc = symtab_load(file_name, &app_symtab);
401 if (rc == EOK) {
402 printf("Loaded symbol table from %s\n", file_name);
403 free(file_name);
404 return;
405 }
406
407 free(file_name);
408 printf("Failed autoloading symbol table.\n");
409}
410
411static char *get_app_task_name(void)
412{
413 char dummy_buf;
414 size_t copied, needed, name_size;
415 char *name;
416 int rc;
417
418 rc = udebug_name_read(phoneid, &dummy_buf, 0, &copied, &needed);
419 if (rc < 0)
420 return NULL;
421
422 name_size = needed;
423 name = malloc(name_size + 1);
424 rc = udebug_name_read(phoneid, name, name_size, &copied, &needed);
425 if (rc < 0) {
426 free(name);
427 return NULL;
428 }
429
430 assert(copied == name_size);
431 assert(copied == needed);
432 name[copied] = '\0';
433
434 return name;
435}
436
437/** Format address in symbolic form.
438 *
439 * Formats address as <symbol_name>+<offset> (<address>), if possible,
440 * otherwise as <address>.
441 *
442 * @param addr Address to format.
443 * @return Newly allocated string, address in symbolic form.
444 */
445static char *fmt_sym_address(uintptr_t addr)
446{
447 char *name;
448 size_t offs;
449 int rc;
450 char *str;
451
452 if (app_symtab != NULL) {
453 rc = symtab_addr_to_name(app_symtab, addr, &name, &offs);
454 } else {
455 rc = ENOTSUP;
456 }
457
458 if (rc == EOK) {
459 rc = asprintf(&str, "%p (%s+%p)", addr, name, offs);
460 } else {
461 rc = asprintf(&str, "%p", addr);
462 }
463
464 if (rc < 0) {
465 printf("Memory allocation error.\n");
466 exit(1);
467 }
468
469 return str;
470}
471
472/** @}
473 */
Note: See TracBrowser for help on using the repository browser.