source: mainline/uspace/app/taskdump/taskdump.c@ 1ad52de

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

Obtain userspace state in case of system call.

  • Property mode set to 100644
File size: 9.9 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
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>
[dafa2d04]42#include <as.h>
[1ccafee]43#include <sys/types.h>
44#include <sys/typefmt.h>
[80487bc5]45#include <libarch/istate.h>
[336db295]46#include <macros.h>
47#include <assert.h>
48#include <bool.h>
49
[3698e44]50#include <symtab.h>
[dafa2d04]51#include <elf_core.h>
[e515b21a]52#include <stacktrace.h>
53
[336db295]54#define LINE_BYTES 16
55
56static int phoneid;
57static task_id_t task_id;
[dafa2d04]58static bool write_core_file;
59static char *core_file_name;
[3698e44]60static char *app_name;
61static symtab_t *app_symtab;
[336db295]62
63static int connect_task(task_id_t task_id);
64static int parse_args(int argc, char *argv[]);
[e515b21a]65static void print_syntax(void);
[336db295]66static int threads_dump(void);
[80487bc5]67static int thread_dump(uintptr_t thash);
[336db295]68static int areas_dump(void);
[e515b21a]69static int td_read_uintptr(void *arg, uintptr_t addr, uintptr_t *value);
[336db295]70
[3698e44]71static void autoload_syms(void);
72static char *get_app_task_name(void);
73static char *fmt_sym_address(uintptr_t addr);
74
[336db295]75int main(int argc, char *argv[])
76{
77 int rc;
78
79 printf("Task Dump Utility\n");
[dafa2d04]80 write_core_file = false;
[336db295]81
82 if (parse_args(argc, argv) < 0)
83 return 1;
84
85 rc = connect_task(task_id);
86 if (rc < 0) {
[7e752b2]87 printf("Failed connecting to task %" PRIu64 ".\n", task_id);
[336db295]88 return 1;
89 }
90
[3698e44]91 app_name = get_app_task_name();
92 app_symtab = NULL;
93
[7e752b2]94 printf("Dumping task '%s' (task ID %" PRIu64 ").\n", app_name, task_id);
[3698e44]95 autoload_syms();
96 putchar('\n');
[336db295]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");
[7e752b2]128 printf("ipc_connect_task(%" PRIu64 ") -> %d ", task_id, rc);
[336db295]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 }
[dafa2d04]164 } else if (arg[1] == 'c' && arg[2] == '\0') {
165 write_core_file = true;
166
167 --argc; ++argv;
168 core_file_name = *argv;
[336db295]169 } else {
[7e752b2]170 printf("Uknown option '%c'\n", arg[0]);
[336db295]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
[e515b21a]196static void print_syntax(void)
[336db295]197{
[dafa2d04]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");
[336db295]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++) {
[7e752b2]242 printf(" [%zu] hash: %p\n", 1 + i, (void *) thash_buf[i]);
[80487bc5]243
244 thread_dump(thash_buf[i]);
[336db295]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++) {
[7e752b2]286 printf(" [%zu] flags: %c%c%c%c base: %p size: %zu\n", 1 + i,
[336db295]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' : '-',
[7e752b2]291 (void *) ainfo_buf[i].start_addr, ainfo_buf[i].size);
[336db295]292 }
293
294 putchar('\n');
295
[dafa2d04]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
[336db295]305 free(ainfo_buf);
306
307 return 0;
308}
309
[80487bc5]310static int thread_dump(uintptr_t thash)
311{
312 istate_t istate;
[e515b21a]313 uintptr_t pc, fp, nfp;
314 stacktrace_t st;
[3698e44]315 char *sym_pc;
[80487bc5]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
[196a1439]327 sym_pc = fmt_sym_address(pc);
[63594c0]328 printf("Thread %p: PC = %s. FP = %p\n", (void *) thash,
[7e752b2]329 sym_pc, (void *) fp);
[196a1439]330 free(sym_pc);
[e515b21a]331
332 st.op_arg = NULL;
333 st.read_uintptr = td_read_uintptr;
334
335 while (stacktrace_fp_valid(&st, fp)) {
[3698e44]336 sym_pc = fmt_sym_address(pc);
[7e752b2]337 printf(" %p: %s\n", (void *) fp, sym_pc);
[3698e44]338 free(sym_pc);
[e515b21a]339
340 rc = stacktrace_ra_get(&st, fp, &pc);
341 if (rc != EOK)
342 return rc;
343
344 rc = stacktrace_fp_prev(&st, fp, &nfp);
345 if (rc != EOK)
346 return rc;
347
348 fp = nfp;
349 }
[80487bc5]350
351 return EOK;
352}
353
[e515b21a]354static int td_read_uintptr(void *arg, uintptr_t addr, uintptr_t *value)
355{
356 uintptr_t data;
357 int rc;
358
359 (void) arg;
360
361 rc = udebug_mem_read(phoneid, &data, addr, sizeof(data));
362 if (rc < 0) {
363 printf("Warning: udebug_mem_read() failed.\n");
364 return rc;
365 }
366
367 *value = data;
368 return EOK;
369}
370
[3698e44]371/** Attempt to find the right executable file and load the symbol table. */
372static void autoload_syms(void)
373{
374 char *file_name;
375 int rc;
376
377 assert(app_name != NULL);
378 assert(app_symtab == NULL);
379
380 rc = asprintf(&file_name, "/app/%s", app_name);
381 if (rc < 0) {
382 printf("Memory allocation failure.\n");
383 exit(1);
384 }
385
386 rc = symtab_load(file_name, &app_symtab);
387 if (rc == EOK) {
388 printf("Loaded symbol table from %s\n", file_name);
389 free(file_name);
390 return;
391 }
392
393 free(file_name);
394
395 rc = asprintf(&file_name, "/srv/%s", app_name);
396 if (rc < 0) {
397 printf("Memory allocation failure.\n");
398 exit(1);
399 }
400
[196a1439]401 rc = symtab_load(file_name, &app_symtab);
[3698e44]402 if (rc == EOK) {
403 printf("Loaded symbol table from %s\n", file_name);
404 free(file_name);
405 return;
406 }
407
408 free(file_name);
409 printf("Failed autoloading symbol table.\n");
410}
411
412static char *get_app_task_name(void)
413{
414 char dummy_buf;
415 size_t copied, needed, name_size;
416 char *name;
417 int rc;
418
419 rc = udebug_name_read(phoneid, &dummy_buf, 0, &copied, &needed);
420 if (rc < 0)
421 return NULL;
422
423 name_size = needed;
424 name = malloc(name_size + 1);
425 rc = udebug_name_read(phoneid, name, name_size, &copied, &needed);
426 if (rc < 0) {
427 free(name);
428 return NULL;
429 }
430
431 assert(copied == name_size);
432 assert(copied == needed);
433 name[copied] = '\0';
434
435 return name;
436}
437
438/** Format address in symbolic form.
439 *
440 * Formats address as <symbol_name>+<offset> (<address>), if possible,
441 * otherwise as <address>.
442 *
443 * @param addr Address to format.
444 * @return Newly allocated string, address in symbolic form.
445 */
446static char *fmt_sym_address(uintptr_t addr)
447{
448 char *name;
449 size_t offs;
450 int rc;
451 char *str;
452
453 if (app_symtab != NULL) {
454 rc = symtab_addr_to_name(app_symtab, addr, &name, &offs);
455 } else {
456 rc = ENOTSUP;
457 }
458
459 if (rc == EOK) {
[7e752b2]460 rc = asprintf(&str, "%p (%s+%zu)", (void *) addr, name, offs);
[3698e44]461 } else {
[7e752b2]462 rc = asprintf(&str, "%p", (void *) addr);
[3698e44]463 }
464
465 if (rc < 0) {
466 printf("Memory allocation error.\n");
467 exit(1);
468 }
469
470 return str;
471}
472
[336db295]473/** @}
474 */
Note: See TracBrowser for help on using the repository browser.