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