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