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