| 1 | /* |
| 2 | * so-info.c |
| 3 | * |
| 4 | * Babeltrace - Executable and Shared Object Debug Info Reader |
| 5 | * |
| 6 | * Copyright 2015 Antoine Busque <abusque@efficios.com> |
| 7 | * |
| 8 | * Author: Antoine Busque <abusque@efficios.com> |
| 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | * of this software and associated documentation files (the "Software"), to deal |
| 12 | * in the Software without restriction, including without limitation the rights |
| 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | * copies of the Software, and to permit persons to whom the Software is |
| 15 | * furnished to do so, subject to the following conditions: |
| 16 | * |
| 17 | * The above copyright notice and this permission notice shall be included in |
| 18 | * all copies or substantial portions of the Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | * SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | #include <fcntl.h> |
| 30 | #include <math.h> |
| 31 | #include <libgen.h> |
| 32 | #include <stdio.h> |
| 33 | #include <inttypes.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include <unistd.h> |
| 37 | #include <dwarf.h> |
| 38 | #include <glib.h> |
| 39 | #include <babeltrace/dwarf.h> |
| 40 | #include <babeltrace/so-info.h> |
| 41 | #include <babeltrace/crc32.h> |
| 42 | #include <babeltrace/babeltrace-internal.h> |
| 43 | #include <babeltrace/utils.h> |
| 44 | |
| 45 | /* |
| 46 | * An address printed in hex is at most 20 bytes (16 for 64-bits + |
| 47 | * leading 0x + optional leading '+' if addr is an offset + null |
| 48 | * character). |
| 49 | */ |
| 50 | #define ADDR_STR_LEN 20 |
| 51 | |
| 52 | BT_HIDDEN |
| 53 | int so_info_init(void) |
| 54 | { |
| 55 | int ret = 0; |
| 56 | |
| 57 | if (elf_version(EV_CURRENT) == EV_NONE) { |
| 58 | fprintf(stderr, "ELF library initialization failed: %s\n", |
| 59 | elf_errmsg(-1)); |
| 60 | ret = -1; |
| 61 | } |
| 62 | |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | BT_HIDDEN |
| 67 | struct so_info *so_info_create(const char *path, uint64_t low_addr, |
| 68 | uint64_t memsz, bool is_pic) |
| 69 | { |
| 70 | struct so_info *so = NULL; |
| 71 | GElf_Ehdr *ehdr = NULL; |
| 72 | |
| 73 | if (!path) { |
| 74 | goto error; |
| 75 | } |
| 76 | |
| 77 | so = g_new0(struct so_info, 1); |
| 78 | if (!so) { |
| 79 | goto error; |
| 80 | } |
| 81 | |
| 82 | so->elf_path = strdup(path); |
| 83 | if (!so->elf_path) { |
| 84 | goto error; |
| 85 | } |
| 86 | |
| 87 | so->elf_fd = open(path, O_RDONLY); |
| 88 | if (so->elf_fd < 0) { |
| 89 | fprintf(stderr, "Failed to open %s\n", path); |
| 90 | goto error; |
| 91 | } |
| 92 | |
| 93 | so->elf_file = elf_begin(so->elf_fd, ELF_C_READ, NULL); |
| 94 | if (!so->elf_file) { |
| 95 | fprintf(stderr, "elf_begin failed: %s\n", elf_errmsg(-1)); |
| 96 | goto error; |
| 97 | } |
| 98 | |
| 99 | if (elf_kind(so->elf_file) != ELF_K_ELF) { |
| 100 | fprintf(stderr, "Error: %s is not an ELF object\n", |
| 101 | so->elf_path); |
| 102 | goto error; |
| 103 | } |
| 104 | |
| 105 | ehdr = g_new0(GElf_Ehdr, 1); |
| 106 | if (!ehdr) { |
| 107 | goto error; |
| 108 | } |
| 109 | |
| 110 | if (!gelf_getehdr(so->elf_file, ehdr)) { |
| 111 | fprintf(stderr, "Error: couldn't get ehdr for %s\n", |
| 112 | so->elf_path); |
| 113 | goto error; |
| 114 | } |
| 115 | |
| 116 | so->is_pic = is_pic; |
| 117 | so->memsz = memsz; |
| 118 | so->low_addr = low_addr; |
| 119 | so->high_addr = so->low_addr + so->memsz; |
| 120 | |
| 121 | g_free(ehdr); |
| 122 | return so; |
| 123 | |
| 124 | error: |
| 125 | g_free(ehdr); |
| 126 | so_info_destroy(so); |
| 127 | return NULL; |
| 128 | } |
| 129 | |
| 130 | BT_HIDDEN |
| 131 | void so_info_destroy(struct so_info *so) |
| 132 | { |
| 133 | if (!so) { |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | dwarf_end(so->dwarf_info); |
| 138 | |
| 139 | free(so->elf_path); |
| 140 | free(so->dwarf_path); |
| 141 | free(so->build_id); |
| 142 | free(so->dbg_link_filename); |
| 143 | |
| 144 | elf_end(so->elf_file); |
| 145 | |
| 146 | close(so->elf_fd); |
| 147 | close(so->dwarf_fd); |
| 148 | |
| 149 | g_free(so); |
| 150 | } |
| 151 | |
| 152 | BT_HIDDEN |
| 153 | int so_info_set_build_id(struct so_info *so, uint8_t *build_id, |
| 154 | size_t build_id_len) |
| 155 | { |
| 156 | if (!so || !build_id) { |
| 157 | goto error; |
| 158 | } |
| 159 | |
| 160 | so->build_id = malloc(build_id_len); |
| 161 | if (!so->build_id) { |
| 162 | goto error; |
| 163 | } |
| 164 | |
| 165 | memcpy(so->build_id, build_id, build_id_len); |
| 166 | so->build_id_len = build_id_len; |
| 167 | |
| 168 | /* |
| 169 | * Reset the is_elf_only flag in case it had been set |
| 170 | * previously, because we might find separate debug info using |
| 171 | * the new build id information. |
| 172 | */ |
| 173 | so->is_elf_only = false; |
| 174 | |
| 175 | return 0; |
| 176 | |
| 177 | error: |
| 178 | |
| 179 | return -1; |
| 180 | } |
| 181 | |
| 182 | BT_HIDDEN |
| 183 | int so_info_set_debug_link(struct so_info *so, char *filename, uint32_t crc) |
| 184 | { |
| 185 | if (!so || !filename) { |
| 186 | goto error; |
| 187 | } |
| 188 | |
| 189 | so->dbg_link_filename = strdup(filename); |
| 190 | if (!so->dbg_link_filename) { |
| 191 | goto error; |
| 192 | } |
| 193 | |
| 194 | so->dbg_link_crc = crc; |
| 195 | |
| 196 | /* |
| 197 | * Reset the is_elf_only flag in case it had been set |
| 198 | * previously, because we might find separate debug info using |
| 199 | * the new build id information. |
| 200 | */ |
| 201 | so->is_elf_only = false; |
| 202 | |
| 203 | return 0; |
| 204 | |
| 205 | error: |
| 206 | |
| 207 | return -1; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Tries to read DWARF info from the location given by path, and |
| 212 | * attach it to the given so_info instance if it exists. |
| 213 | * |
| 214 | * @param so so_info instance for which to set DWARF info |
| 215 | * @param path Presumed location of the DWARF info |
| 216 | * @returns 0 on success, -1 on failure |
| 217 | */ |
| 218 | static |
| 219 | int so_info_set_dwarf_info_from_path(struct so_info *so, char *path) |
| 220 | { |
| 221 | int fd = -1, ret = 0; |
| 222 | struct bt_dwarf_cu *cu = NULL; |
| 223 | Dwarf *dwarf_info = NULL; |
| 224 | |
| 225 | if (!so || !path) { |
| 226 | goto error; |
| 227 | } |
| 228 | |
| 229 | fd = open(path, O_RDONLY); |
| 230 | if (fd < 0) { |
| 231 | goto error; |
| 232 | } |
| 233 | |
| 234 | dwarf_info = dwarf_begin(fd, DWARF_C_READ); |
| 235 | if (!dwarf_info) { |
| 236 | goto error; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Check if the dwarf info has any CU. If not, the SO's object |
| 241 | * file contains no DWARF info. |
| 242 | */ |
| 243 | cu = bt_dwarf_cu_create(dwarf_info); |
| 244 | if (!cu) { |
| 245 | goto error; |
| 246 | } |
| 247 | |
| 248 | ret = bt_dwarf_cu_next(cu); |
| 249 | if (ret) { |
| 250 | goto error; |
| 251 | } |
| 252 | |
| 253 | so->dwarf_fd = fd; |
| 254 | so->dwarf_path = strdup(path); |
| 255 | if (!so->dwarf_path) { |
| 256 | goto error; |
| 257 | } |
| 258 | so->dwarf_info = dwarf_info; |
| 259 | free(cu); |
| 260 | |
| 261 | return 0; |
| 262 | |
| 263 | error: |
| 264 | close(fd); |
| 265 | dwarf_end(dwarf_info); |
| 266 | g_free(dwarf_info); |
| 267 | free(cu); |
| 268 | |
| 269 | return -1; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Try to set the dwarf_info for a given so_info instance via the |
| 274 | * build ID method. |
| 275 | * |
| 276 | * @param so so_info instance for which to retrieve the |
| 277 | * DWARF info via build ID |
| 278 | * @returns 0 on success (i.e. dwarf_info set), -1 on failure |
| 279 | */ |
| 280 | static |
| 281 | int so_info_set_dwarf_info_build_id(struct so_info *so) |
| 282 | { |
| 283 | int i = 0, ret = 0, dbg_dir_trailing_slash = 0; |
| 284 | char *path = NULL, *build_id_file = NULL; |
| 285 | const char *dbg_dir = NULL; |
| 286 | size_t build_id_file_len, path_len; |
| 287 | |
| 288 | if (!so || !so->build_id) { |
| 289 | goto error; |
| 290 | } |
| 291 | |
| 292 | dbg_dir = opt_debug_info_dir ? : DEFAULT_DEBUG_DIR; |
| 293 | |
| 294 | dbg_dir_trailing_slash = dbg_dir[strlen(dbg_dir) - 1] == '/'; |
| 295 | |
| 296 | /* 2 characters per byte printed in hex, +2 for '/' and '\0' */ |
| 297 | build_id_file_len = (2 * so->build_id_len) + 2; |
| 298 | build_id_file = malloc(build_id_file_len); |
| 299 | if (!build_id_file) { |
| 300 | goto error; |
| 301 | } |
| 302 | |
| 303 | snprintf(build_id_file, 4, "%02x/", so->build_id[0]); |
| 304 | for (i = 1; i < so->build_id_len; ++i) { |
| 305 | int path_idx = 3 + 2 * (i - 1); |
| 306 | |
| 307 | snprintf(&build_id_file[path_idx], 3, "%02x", so->build_id[i]); |
| 308 | } |
| 309 | |
| 310 | path_len = strlen(dbg_dir) + strlen(BUILD_ID_SUBDIR) + |
| 311 | strlen(build_id_file) + strlen(BUILD_ID_SUFFIX) + 1; |
| 312 | if (!dbg_dir_trailing_slash) { |
| 313 | path_len += 1; |
| 314 | } |
| 315 | |
| 316 | path = malloc(path_len); |
| 317 | if (!path) { |
| 318 | goto error; |
| 319 | } |
| 320 | |
| 321 | strcpy(path, dbg_dir); |
| 322 | if (!dbg_dir_trailing_slash) { |
| 323 | strcat(path, "/"); |
| 324 | } |
| 325 | strcat(path, BUILD_ID_SUBDIR); |
| 326 | strcat(path, build_id_file); |
| 327 | strcat(path, BUILD_ID_SUFFIX); |
| 328 | |
| 329 | ret = so_info_set_dwarf_info_from_path(so, path); |
| 330 | if (ret) { |
| 331 | goto error; |
| 332 | } |
| 333 | |
| 334 | goto end; |
| 335 | |
| 336 | error: |
| 337 | ret = -1; |
| 338 | end: |
| 339 | free(build_id_file); |
| 340 | free(path); |
| 341 | |
| 342 | return ret; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Tests whether the file located at path exists and has the expected |
| 347 | * checksum. |
| 348 | * |
| 349 | * This predicate is used when looking up separate debug info via the |
| 350 | * GNU debuglink method. The expected crc can be found .gnu_debuglink |
| 351 | * section in the original ELF file, along with the filename for the |
| 352 | * file containing the debug info. |
| 353 | * |
| 354 | * @param path Full path at which to look for the debug file |
| 355 | * @param crc Expected checksum for the debug file |
| 356 | * @returns 1 if the file exists and has the correct checksum, |
| 357 | * 0 otherwise |
| 358 | */ |
| 359 | static |
| 360 | int is_valid_debug_file(char *path, uint32_t crc) |
| 361 | { |
| 362 | int ret = 0, fd = -1; |
| 363 | uint32_t _crc = 0; |
| 364 | |
| 365 | if (!path) { |
| 366 | goto end; |
| 367 | } |
| 368 | |
| 369 | fd = open(path, O_RDONLY); |
| 370 | if (fd < 0) { |
| 371 | goto end; |
| 372 | } |
| 373 | |
| 374 | ret = crc32(fd, &_crc); |
| 375 | if (ret) { |
| 376 | ret = 0; |
| 377 | goto end; |
| 378 | } |
| 379 | |
| 380 | ret = (crc == _crc); |
| 381 | |
| 382 | end: |
| 383 | close(fd); |
| 384 | return ret; |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Try to set the dwarf_info for a given so_info instance via the |
| 389 | * build ID method. |
| 390 | * |
| 391 | * @param so so_info instance for which to retrieve the |
| 392 | * DWARF info via debug link |
| 393 | * @returns 0 on success (i.e. dwarf_info set), -1 on failure |
| 394 | */ |
| 395 | static |
| 396 | int so_info_set_dwarf_info_debug_link(struct so_info *so) |
| 397 | { |
| 398 | int ret = 0; |
| 399 | const char *dbg_dir = NULL; |
| 400 | char *dir_name = NULL, *so_dir = NULL, *path = NULL; |
| 401 | size_t max_path_len = 0; |
| 402 | |
| 403 | if (!so || !so->dbg_link_filename) { |
| 404 | goto error; |
| 405 | } |
| 406 | |
| 407 | dbg_dir = opt_debug_info_dir ? : DEFAULT_DEBUG_DIR; |
| 408 | |
| 409 | dir_name = dirname(so->elf_path); |
| 410 | if (!dir_name) { |
| 411 | goto error; |
| 412 | } |
| 413 | |
| 414 | /* so_dir is just dir_name with a trailing slash */ |
| 415 | so_dir = malloc(strlen(dir_name) + 2); |
| 416 | if (!so_dir) { |
| 417 | goto error; |
| 418 | } |
| 419 | |
| 420 | strcpy(so_dir, dir_name); |
| 421 | strcat(so_dir, "/"); |
| 422 | |
| 423 | max_path_len = strlen(dbg_dir) + strlen(so_dir) + |
| 424 | strlen(DEBUG_SUBDIR) + strlen(so->dbg_link_filename) |
| 425 | + 1; |
| 426 | path = malloc(max_path_len); |
| 427 | if (!path) { |
| 428 | goto error; |
| 429 | } |
| 430 | |
| 431 | /* First look in the SO's dir */ |
| 432 | strcpy(path, so_dir); |
| 433 | strcat(path, so->dbg_link_filename); |
| 434 | |
| 435 | if (is_valid_debug_file(path, so->dbg_link_crc)) { |
| 436 | goto found; |
| 437 | } |
| 438 | |
| 439 | /* If not found, look in .debug subdir */ |
| 440 | strcpy(path, so_dir); |
| 441 | strcat(path, DEBUG_SUBDIR); |
| 442 | strcat(path, so->dbg_link_filename); |
| 443 | |
| 444 | if (is_valid_debug_file(path, so->dbg_link_crc)) { |
| 445 | goto found; |
| 446 | } |
| 447 | |
| 448 | /* Lastly, look under the global debug directory */ |
| 449 | strcpy(path, dbg_dir); |
| 450 | strcat(path, so_dir); |
| 451 | strcat(path, so->dbg_link_filename); |
| 452 | |
| 453 | if (is_valid_debug_file(path, so->dbg_link_crc)) { |
| 454 | goto found; |
| 455 | } |
| 456 | |
| 457 | error: |
| 458 | ret = -1; |
| 459 | end: |
| 460 | free(path); |
| 461 | free(so_dir); |
| 462 | |
| 463 | return ret; |
| 464 | |
| 465 | found: |
| 466 | ret = so_info_set_dwarf_info_from_path(so, path); |
| 467 | if (ret) { |
| 468 | goto error; |
| 469 | } |
| 470 | |
| 471 | goto end; |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Initialize the DWARF info for a given executable. |
| 476 | * |
| 477 | * @param so so_info instance |
| 478 | * @returns 0 on success, -1 on failure |
| 479 | */ |
| 480 | static |
| 481 | int so_info_set_dwarf_info(struct so_info *so) |
| 482 | { |
| 483 | int ret = 0; |
| 484 | |
| 485 | if (!so) { |
| 486 | goto error; |
| 487 | } |
| 488 | |
| 489 | /* First try to set the DWARF info from the ELF file */ |
| 490 | ret = so_info_set_dwarf_info_from_path(so, so->elf_path); |
| 491 | if (!ret) { |
| 492 | goto end; |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | * If that fails, try to find separate debug info via build ID |
| 497 | * and debug link. |
| 498 | */ |
| 499 | ret = so_info_set_dwarf_info_build_id(so); |
| 500 | if (!ret) { |
| 501 | goto end; |
| 502 | } |
| 503 | |
| 504 | ret = so_info_set_dwarf_info_debug_link(so); |
| 505 | if (!ret) { |
| 506 | goto end; |
| 507 | } |
| 508 | |
| 509 | error: |
| 510 | ret = -1; |
| 511 | end: |
| 512 | return ret; |
| 513 | } |
| 514 | |
| 515 | BT_HIDDEN |
| 516 | void source_location_destroy(struct source_location *src_loc) |
| 517 | { |
| 518 | if (!src_loc) { |
| 519 | return; |
| 520 | } |
| 521 | |
| 522 | free(src_loc->filename); |
| 523 | g_free(src_loc); |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * Try to find the symbol closest to an address within a given ELF |
| 528 | * section. |
| 529 | * |
| 530 | * Only function symbols are taken into account. The symbol's address |
| 531 | * must precede `addr`. A symbol with a closer address might exist |
| 532 | * after `addr` but is irrelevant because it cannot encompass `addr`. |
| 533 | * |
| 534 | * On success, if found, the out parameters `sym` and `shdr` are |
| 535 | * set. On failure or if none are found, they remain unchanged. |
| 536 | * |
| 537 | * @param scn ELF section in which to look for the address |
| 538 | * @param addr Virtual memory address for which to find the |
| 539 | * nearest function symbol |
| 540 | * @param sym Out parameter, the nearest function symbol |
| 541 | * @param shdr Out parameter, the section header for scn |
| 542 | * @returns 0 on success, -1 on failure |
| 543 | */ |
| 544 | static |
| 545 | int so_info_get_nearest_symbol_from_section(Elf_Scn *scn, uint64_t addr, |
| 546 | GElf_Sym **sym, GElf_Shdr **shdr) |
| 547 | { |
| 548 | int i; |
| 549 | size_t symbol_count; |
| 550 | Elf_Data *data = NULL; |
| 551 | GElf_Shdr *_shdr = NULL; |
| 552 | GElf_Sym *nearest_sym = NULL; |
| 553 | |
| 554 | if (!scn || !sym || !shdr) { |
| 555 | goto error; |
| 556 | } |
| 557 | |
| 558 | _shdr = g_new0(GElf_Shdr, 1); |
| 559 | if (!_shdr) { |
| 560 | goto error; |
| 561 | } |
| 562 | |
| 563 | _shdr = gelf_getshdr(scn, _shdr); |
| 564 | if (!_shdr) { |
| 565 | goto error; |
| 566 | } |
| 567 | |
| 568 | if (_shdr->sh_type != SHT_SYMTAB) { |
| 569 | /* |
| 570 | * We are only interested in symbol table (symtab) |
| 571 | * sections, skip this one. |
| 572 | */ |
| 573 | goto end; |
| 574 | } |
| 575 | |
| 576 | data = elf_getdata(scn, NULL); |
| 577 | if (!data) { |
| 578 | goto error; |
| 579 | } |
| 580 | |
| 581 | symbol_count = _shdr->sh_size / _shdr->sh_entsize; |
| 582 | |
| 583 | for (i = 0; i < symbol_count; ++i) { |
| 584 | GElf_Sym *cur_sym = NULL; |
| 585 | |
| 586 | cur_sym = g_new0(GElf_Sym, 1); |
| 587 | if (!cur_sym) { |
| 588 | goto error; |
| 589 | } |
| 590 | cur_sym = gelf_getsym(data, i, cur_sym); |
| 591 | if (!cur_sym) { |
| 592 | goto error; |
| 593 | } |
| 594 | if (GELF_ST_TYPE(cur_sym->st_info) != STT_FUNC) { |
| 595 | /* We're only interested in the functions. */ |
| 596 | g_free(cur_sym); |
| 597 | continue; |
| 598 | } |
| 599 | |
| 600 | if (cur_sym->st_value <= addr && |
| 601 | (!nearest_sym || |
| 602 | cur_sym->st_value > nearest_sym->st_value)) { |
| 603 | g_free(nearest_sym); |
| 604 | nearest_sym = cur_sym; |
| 605 | } else { |
| 606 | g_free(cur_sym); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | end: |
| 611 | if (nearest_sym) { |
| 612 | *sym = nearest_sym; |
| 613 | *shdr = _shdr; |
| 614 | } else { |
| 615 | g_free(_shdr); |
| 616 | } |
| 617 | |
| 618 | return 0; |
| 619 | |
| 620 | error: |
| 621 | g_free(nearest_sym); |
| 622 | g_free(_shdr); |
| 623 | return -1; |
| 624 | } |
| 625 | |
| 626 | /** |
| 627 | * Get the name of the function containing a given address within an |
| 628 | * executable using ELF symbols. |
| 629 | * |
| 630 | * The function name is in fact the name of the nearest ELF symbol, |
| 631 | * followed by the offset in bytes between the address and the symbol |
| 632 | * (in hex), separated by a '+' character. |
| 633 | * |
| 634 | * If found, the out parameter `func_name` is set on success. On failure, |
| 635 | * it remains unchanged. |
| 636 | * |
| 637 | * @param so so_info instance for the executable containing |
| 638 | * the address |
| 639 | * @param addr Virtual memory address for which to find the |
| 640 | * function name |
| 641 | * @param func_name Out parameter, the function name |
| 642 | * @returns 0 on success, -1 on failure |
| 643 | */ |
| 644 | static |
| 645 | int so_info_lookup_elf_function_name(struct so_info *so, uint64_t addr, |
| 646 | char **func_name) |
| 647 | { |
| 648 | /* |
| 649 | * TODO (possible optimisation): if an ELF has no symtab |
| 650 | * section, it has been stripped. Therefore, it would be wise |
| 651 | * to store a flag indicating the stripped status after the |
| 652 | * first iteration to prevent subsequent ones. |
| 653 | */ |
| 654 | int ret = 0; |
| 655 | Elf_Scn *scn = NULL; |
| 656 | GElf_Sym *sym = NULL; |
| 657 | GElf_Shdr *shdr = NULL; |
| 658 | char *sym_name = NULL; |
| 659 | char *_func_name = NULL; |
| 660 | char offset_str[ADDR_STR_LEN]; |
| 661 | |
| 662 | scn = elf_nextscn(so->elf_file, scn); |
| 663 | if (!scn) { |
| 664 | goto error; |
| 665 | } |
| 666 | |
| 667 | while (scn && !sym) { |
| 668 | ret = so_info_get_nearest_symbol_from_section( |
| 669 | scn, addr, &sym, &shdr); |
| 670 | if (ret) { |
| 671 | goto error; |
| 672 | } |
| 673 | |
| 674 | scn = elf_nextscn(so->elf_file, scn); |
| 675 | } |
| 676 | |
| 677 | if (sym) { |
| 678 | sym_name = elf_strptr(so->elf_file, shdr->sh_link, |
| 679 | sym->st_name); |
| 680 | if (!sym_name) { |
| 681 | goto error; |
| 682 | } |
| 683 | |
| 684 | snprintf(offset_str, ADDR_STR_LEN, "+%#0" PRIx64, |
| 685 | addr - sym->st_value); |
| 686 | _func_name = malloc(strlen(sym_name) + ADDR_STR_LEN); |
| 687 | if (!_func_name) { |
| 688 | goto error; |
| 689 | } |
| 690 | |
| 691 | strcpy(_func_name, sym_name); |
| 692 | strcat(_func_name, offset_str); |
| 693 | *func_name = _func_name; |
| 694 | } |
| 695 | |
| 696 | g_free(shdr); |
| 697 | g_free(sym); |
| 698 | return 0; |
| 699 | |
| 700 | error: |
| 701 | g_free(shdr); |
| 702 | g_free(sym); |
| 703 | free(_func_name); |
| 704 | return -1; |
| 705 | } |
| 706 | |
| 707 | /** |
| 708 | * Get the name of the function containing a given address within a |
| 709 | * given compile unit (CU). |
| 710 | * |
| 711 | * If found, the out parameter `func_name` is set on success. On |
| 712 | * failure, it remains unchanged. |
| 713 | * |
| 714 | * @param cu bt_dwarf_cu instance which may contain the address |
| 715 | * @param addr Virtual memory address for which to find the |
| 716 | * function name |
| 717 | * @param func_name Out parameter, the function name |
| 718 | * @returns 0 on success, -1 on failure |
| 719 | */ |
| 720 | static |
| 721 | int so_info_lookup_cu_function_name(struct bt_dwarf_cu *cu, uint64_t addr, |
| 722 | char **func_name) |
| 723 | { |
| 724 | int ret = 0, found = 0; |
| 725 | char *_func_name = NULL; |
| 726 | struct bt_dwarf_die *die = NULL; |
| 727 | |
| 728 | if (!cu || !func_name) { |
| 729 | goto error; |
| 730 | } |
| 731 | |
| 732 | die = bt_dwarf_die_create(cu); |
| 733 | if (!die) { |
| 734 | goto error; |
| 735 | } |
| 736 | |
| 737 | while (bt_dwarf_die_next(die) == 0) { |
| 738 | int tag; |
| 739 | |
| 740 | ret = bt_dwarf_die_get_tag(die, &tag); |
| 741 | if (ret) { |
| 742 | goto error; |
| 743 | } |
| 744 | |
| 745 | if (tag == DW_TAG_subprogram) { |
| 746 | ret = bt_dwarf_die_contains_addr(die, addr, &found); |
| 747 | if (ret) { |
| 748 | goto error; |
| 749 | } |
| 750 | |
| 751 | if (found) { |
| 752 | break; |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | if (found) { |
| 758 | ret = bt_dwarf_die_get_name(die, &_func_name); |
| 759 | if (ret) { |
| 760 | goto error; |
| 761 | } |
| 762 | |
| 763 | *func_name = _func_name; |
| 764 | } |
| 765 | |
| 766 | bt_dwarf_die_destroy(die); |
| 767 | return 0; |
| 768 | |
| 769 | error: |
| 770 | bt_dwarf_die_destroy(die); |
| 771 | return -1; |
| 772 | } |
| 773 | |
| 774 | /** |
| 775 | * Get the name of the function containing a given address within an |
| 776 | * executable using DWARF debug info. |
| 777 | * |
| 778 | * If found, the out parameter `func_name` is set on success. On |
| 779 | * failure, it remains unchanged. |
| 780 | * |
| 781 | * @param so so_info instance for the executable containing |
| 782 | * the address |
| 783 | * @param addr Virtual memory address for which to find the |
| 784 | * function name |
| 785 | * @param func_name Out parameter, the function name |
| 786 | * @returns 0 on success, -1 on failure |
| 787 | */ |
| 788 | static |
| 789 | int so_info_lookup_dwarf_function_name(struct so_info *so, uint64_t addr, |
| 790 | char **func_name) |
| 791 | { |
| 792 | int ret = 0; |
| 793 | char *_func_name = NULL; |
| 794 | struct bt_dwarf_cu *cu = NULL; |
| 795 | |
| 796 | if (!so || !func_name) { |
| 797 | goto error; |
| 798 | } |
| 799 | |
| 800 | cu = bt_dwarf_cu_create(so->dwarf_info); |
| 801 | if (!cu) { |
| 802 | goto error; |
| 803 | } |
| 804 | |
| 805 | while (bt_dwarf_cu_next(cu) == 0) { |
| 806 | ret = so_info_lookup_cu_function_name(cu, addr, &_func_name); |
| 807 | if (ret) { |
| 808 | goto error; |
| 809 | } |
| 810 | |
| 811 | if (_func_name) { |
| 812 | break; |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | if (_func_name) { |
| 817 | *func_name = _func_name; |
| 818 | } |
| 819 | |
| 820 | bt_dwarf_cu_destroy(cu); |
| 821 | return 0; |
| 822 | |
| 823 | error: |
| 824 | bt_dwarf_cu_destroy(cu); |
| 825 | return -1; |
| 826 | } |
| 827 | |
| 828 | BT_HIDDEN |
| 829 | int so_info_lookup_function_name(struct so_info *so, uint64_t ip, |
| 830 | char **func_name) |
| 831 | { |
| 832 | int ret = 0; |
| 833 | char *_func_name = NULL; |
| 834 | uint64_t relative_addr; |
| 835 | |
| 836 | if (!so || !func_name) { |
| 837 | goto error; |
| 838 | } |
| 839 | |
| 840 | /* Set DWARF info if it hasn't been accessed yet. */ |
| 841 | if (!so->dwarf_info && !so->is_elf_only) { |
| 842 | ret = so_info_set_dwarf_info(so); |
| 843 | if (ret) { |
| 844 | /* Failed to set DWARF info, fallback to ELF. */ |
| 845 | so->is_elf_only = true; |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | if (!so_info_has_address(so, ip)) { |
| 850 | goto error; |
| 851 | } |
| 852 | |
| 853 | relative_addr = ip - so->low_addr; |
| 854 | /* |
| 855 | * Addresses in ELF and DWARF are relative to base address for |
| 856 | * PIC, so make the address argument relative too if needed. |
| 857 | */ |
| 858 | if (so->is_elf_only) { |
| 859 | ret = so_info_lookup_elf_function_name(so, |
| 860 | so->is_pic ? relative_addr : ip, |
| 861 | &_func_name); |
| 862 | } else { |
| 863 | ret = so_info_lookup_dwarf_function_name(so, |
| 864 | so->is_pic ? relative_addr : ip, |
| 865 | &_func_name); |
| 866 | } |
| 867 | |
| 868 | if (ret) { |
| 869 | goto error; |
| 870 | } |
| 871 | |
| 872 | if (!_func_name) { |
| 873 | /* |
| 874 | * Can't map to a function; fallback to a generic output of the |
| 875 | * form binary+/@address. |
| 876 | * |
| 877 | * FIXME check position independence flag. |
| 878 | */ |
| 879 | const char *binary_name = get_filename_from_path(so->elf_path); |
| 880 | |
| 881 | ret = asprintf(&_func_name, "%s+%#0" PRIx64, binary_name, |
| 882 | relative_addr); |
| 883 | if (!_func_name) { |
| 884 | goto error; |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | *func_name = _func_name; |
| 889 | return 0; |
| 890 | |
| 891 | error: |
| 892 | return -1; |
| 893 | } |
| 894 | |
| 895 | /** |
| 896 | * Predicate used to determine whether the children of a given DIE |
| 897 | * contain a specific address. |
| 898 | * |
| 899 | * More specifically, the parameter `die` is expected to be a |
| 900 | * subprogram (function) DIE, and this predicate tells whether any |
| 901 | * subroutines are inlined within this function and would contain |
| 902 | * `addr`. |
| 903 | * |
| 904 | * Do note that this function advances the position of `die`. If the |
| 905 | * address is found within one of its children, `die` will be pointing |
| 906 | * to that child upon returning from the function, allowing to extract |
| 907 | * the information deemed necessary. |
| 908 | * |
| 909 | * @param die The parent DIE in whose children the address will be |
| 910 | * looked for |
| 911 | * @param addr The address for which to look for in the DIEs |
| 912 | * @returns Returns 1 if the address was found, 0 if not |
| 913 | */ |
| 914 | static |
| 915 | int so_info_child_die_has_address(struct bt_dwarf_die *die, uint64_t addr) |
| 916 | { |
| 917 | int ret = 0, contains = 0; |
| 918 | |
| 919 | if (!die) { |
| 920 | goto error; |
| 921 | } |
| 922 | |
| 923 | ret = bt_dwarf_die_child(die); |
| 924 | if (ret) { |
| 925 | goto error; |
| 926 | } |
| 927 | |
| 928 | do { |
| 929 | int tag; |
| 930 | |
| 931 | ret = bt_dwarf_die_get_tag(die, &tag); |
| 932 | if (ret) { |
| 933 | goto error; |
| 934 | } |
| 935 | |
| 936 | if (tag == DW_TAG_inlined_subroutine) { |
| 937 | ret = bt_dwarf_die_contains_addr(die, addr, &contains); |
| 938 | if (ret) { |
| 939 | goto error; |
| 940 | } |
| 941 | |
| 942 | if (contains) { |
| 943 | ret = 1; |
| 944 | goto end; |
| 945 | } |
| 946 | } |
| 947 | } while (bt_dwarf_die_next(die) == 0); |
| 948 | |
| 949 | end: |
| 950 | return ret; |
| 951 | |
| 952 | error: |
| 953 | ret = 0; |
| 954 | goto end; |
| 955 | } |
| 956 | |
| 957 | /** |
| 958 | * Lookup the source location for a given address within a CU, making |
| 959 | * the assumption that it is contained within an inline routine in a |
| 960 | * function. |
| 961 | * |
| 962 | * @param cu bt_dwarf_cu instance in which to look for the address |
| 963 | * @param addr The address for which to look for |
| 964 | * @param src_loc Out parameter, the source location (filename and |
| 965 | * line number) for the address |
| 966 | * @returns 0 on success, -1 on failure |
| 967 | */ |
| 968 | static |
| 969 | int so_info_lookup_cu_src_loc_inl(struct bt_dwarf_cu *cu, uint64_t addr, |
| 970 | struct source_location **src_loc) |
| 971 | { |
| 972 | int ret = 0, found = 0; |
| 973 | struct bt_dwarf_die *die = NULL; |
| 974 | struct source_location *_src_loc = NULL; |
| 975 | |
| 976 | if (!cu || !src_loc) { |
| 977 | goto error; |
| 978 | } |
| 979 | |
| 980 | die = bt_dwarf_die_create(cu); |
| 981 | if (!die) { |
| 982 | goto error; |
| 983 | } |
| 984 | |
| 985 | while (bt_dwarf_die_next(die) == 0) { |
| 986 | int tag; |
| 987 | |
| 988 | ret = bt_dwarf_die_get_tag(die, &tag); |
| 989 | if (ret) { |
| 990 | goto error; |
| 991 | } |
| 992 | |
| 993 | if (tag == DW_TAG_subprogram) { |
| 994 | int contains = 0; |
| 995 | |
| 996 | ret = bt_dwarf_die_contains_addr(die, addr, &contains); |
| 997 | if (ret) { |
| 998 | goto error; |
| 999 | } |
| 1000 | |
| 1001 | if (contains) { |
| 1002 | /* |
| 1003 | * Try to find an inlined subroutine |
| 1004 | * child of this DIE containing addr. |
| 1005 | */ |
| 1006 | found = so_info_child_die_has_address( |
| 1007 | die, addr); |
| 1008 | goto end; |
| 1009 | } |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | end: |
| 1014 | if (found) { |
| 1015 | char *filename = NULL; |
| 1016 | uint64_t line_no; |
| 1017 | |
| 1018 | _src_loc = g_new0(struct source_location, 1); |
| 1019 | if (!_src_loc) { |
| 1020 | goto error; |
| 1021 | } |
| 1022 | |
| 1023 | ret = bt_dwarf_die_get_call_file(die, &filename); |
| 1024 | if (ret) { |
| 1025 | goto error; |
| 1026 | } |
| 1027 | ret = bt_dwarf_die_get_call_line(die, &line_no); |
| 1028 | if (ret) { |
| 1029 | free(filename); |
| 1030 | goto error; |
| 1031 | } |
| 1032 | |
| 1033 | _src_loc->filename = filename; |
| 1034 | _src_loc->line_no = line_no; |
| 1035 | *src_loc = _src_loc; |
| 1036 | } |
| 1037 | |
| 1038 | bt_dwarf_die_destroy(die); |
| 1039 | return 0; |
| 1040 | |
| 1041 | error: |
| 1042 | source_location_destroy(_src_loc); |
| 1043 | bt_dwarf_die_destroy(die); |
| 1044 | return -1; |
| 1045 | } |
| 1046 | |
| 1047 | /** |
| 1048 | * Lookup the source location for a given address within a CU, |
| 1049 | * assuming that it is contained within an inlined function. |
| 1050 | * |
| 1051 | * A source location can be found regardless of inlining status for |
| 1052 | * this method, but in the case of an inlined function, the returned |
| 1053 | * source location will point not to the callsite but rather to the |
| 1054 | * definition site of the inline function. |
| 1055 | * |
| 1056 | * @param cu bt_dwarf_cu instance in which to look for the address |
| 1057 | * @param addr The address for which to look for |
| 1058 | * @param src_loc Out parameter, the source location (filename and |
| 1059 | * line number) for the address |
| 1060 | * @returns 0 on success, -1 on failure |
| 1061 | */ |
| 1062 | static |
| 1063 | int so_info_lookup_cu_src_loc_no_inl(struct bt_dwarf_cu *cu, uint64_t addr, |
| 1064 | struct source_location **src_loc) |
| 1065 | { |
| 1066 | struct source_location *_src_loc = NULL; |
| 1067 | struct bt_dwarf_die *die = NULL; |
| 1068 | const char *filename = NULL; |
| 1069 | Dwarf_Line *line = NULL; |
| 1070 | Dwarf_Addr line_addr; |
| 1071 | int ret, line_no; |
| 1072 | |
| 1073 | if (!cu || !src_loc) { |
| 1074 | goto error; |
| 1075 | } |
| 1076 | |
| 1077 | die = bt_dwarf_die_create(cu); |
| 1078 | if (!die) { |
| 1079 | goto error; |
| 1080 | } |
| 1081 | |
| 1082 | line = dwarf_getsrc_die(die->dwarf_die, addr); |
| 1083 | if (!line) { |
| 1084 | goto error; |
| 1085 | } |
| 1086 | |
| 1087 | ret = dwarf_lineaddr(line, &line_addr); |
| 1088 | if (ret) { |
| 1089 | goto error; |
| 1090 | } |
| 1091 | |
| 1092 | filename = dwarf_linesrc(line, NULL, NULL); |
| 1093 | if (!filename) { |
| 1094 | goto error; |
| 1095 | } |
| 1096 | |
| 1097 | if (addr == line_addr) { |
| 1098 | _src_loc = g_new0(struct source_location, 1); |
| 1099 | if (!_src_loc) { |
| 1100 | goto error; |
| 1101 | } |
| 1102 | |
| 1103 | ret = dwarf_lineno(line, &line_no); |
| 1104 | if (ret) { |
| 1105 | goto error; |
| 1106 | } |
| 1107 | |
| 1108 | _src_loc->line_no = line_no; |
| 1109 | _src_loc->filename = strdup(filename); |
| 1110 | } |
| 1111 | |
| 1112 | bt_dwarf_die_destroy(die); |
| 1113 | |
| 1114 | if (_src_loc) { |
| 1115 | *src_loc = _src_loc; |
| 1116 | } |
| 1117 | |
| 1118 | return 0; |
| 1119 | |
| 1120 | error: |
| 1121 | source_location_destroy(_src_loc); |
| 1122 | bt_dwarf_die_destroy(die); |
| 1123 | return -1; |
| 1124 | } |
| 1125 | |
| 1126 | /** |
| 1127 | * Get the source location (file name and line number) for a given |
| 1128 | * address within a compile unit (CU). |
| 1129 | * |
| 1130 | * On success, the out parameter `src_loc` is set if found. On |
| 1131 | * failure, it remains unchanged. |
| 1132 | * |
| 1133 | * @param so bt_dwarf_cu instance for the compile unit which |
| 1134 | * may contain the address |
| 1135 | * @param addr Virtual memory address for which to find the |
| 1136 | * source location |
| 1137 | * @param src_loc Out parameter, the source location |
| 1138 | * @returns 0 on success, -1 on failure |
| 1139 | */ |
| 1140 | static |
| 1141 | int so_info_lookup_cu_src_loc(struct bt_dwarf_cu *cu, uint64_t addr, |
| 1142 | struct source_location **src_loc) |
| 1143 | { |
| 1144 | int ret = 0; |
| 1145 | struct source_location *_src_loc = NULL; |
| 1146 | |
| 1147 | if (!cu || !src_loc) { |
| 1148 | goto error; |
| 1149 | } |
| 1150 | |
| 1151 | ret = so_info_lookup_cu_src_loc_inl(cu, addr, &_src_loc); |
| 1152 | if (ret) { |
| 1153 | goto error; |
| 1154 | } |
| 1155 | |
| 1156 | if (_src_loc) { |
| 1157 | goto end; |
| 1158 | } |
| 1159 | |
| 1160 | ret = so_info_lookup_cu_src_loc_no_inl(cu, addr, &_src_loc); |
| 1161 | if (ret) { |
| 1162 | goto error; |
| 1163 | } |
| 1164 | |
| 1165 | if (_src_loc) { |
| 1166 | goto end; |
| 1167 | } |
| 1168 | |
| 1169 | end: |
| 1170 | if (_src_loc) { |
| 1171 | *src_loc = _src_loc; |
| 1172 | } |
| 1173 | |
| 1174 | return 0; |
| 1175 | |
| 1176 | error: |
| 1177 | source_location_destroy(_src_loc); |
| 1178 | return -1; |
| 1179 | } |
| 1180 | |
| 1181 | BT_HIDDEN |
| 1182 | int so_info_lookup_source_location(struct so_info *so, uint64_t addr, |
| 1183 | struct source_location **src_loc) |
| 1184 | { |
| 1185 | struct bt_dwarf_cu *cu = NULL; |
| 1186 | struct source_location *_src_loc = NULL; |
| 1187 | |
| 1188 | if (!so || !src_loc) { |
| 1189 | goto error; |
| 1190 | } |
| 1191 | |
| 1192 | /* Set DWARF info if it hasn't been accessed yet. */ |
| 1193 | if (!so->dwarf_info && !so->is_elf_only) { |
| 1194 | if (so_info_set_dwarf_info(so)) { |
| 1195 | /* Failed to set DWARF info. */ |
| 1196 | so->is_elf_only = true; |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | if (so->is_elf_only) { |
| 1201 | /* We cannot lookup source location without DWARF info. */ |
| 1202 | goto error; |
| 1203 | } |
| 1204 | |
| 1205 | if (!so_info_has_address(so, addr)) { |
| 1206 | goto error; |
| 1207 | } |
| 1208 | |
| 1209 | /* |
| 1210 | * Addresses in ELF and DWARF are relative to base address for |
| 1211 | * PIC, so make the address argument relative too if needed. |
| 1212 | */ |
| 1213 | if (so->is_pic) { |
| 1214 | addr -= so->low_addr; |
| 1215 | } |
| 1216 | |
| 1217 | cu = bt_dwarf_cu_create(so->dwarf_info); |
| 1218 | if (!cu) { |
| 1219 | goto error; |
| 1220 | } |
| 1221 | |
| 1222 | while (bt_dwarf_cu_next(cu) == 0) { |
| 1223 | int ret; |
| 1224 | |
| 1225 | ret = so_info_lookup_cu_src_loc(cu, addr, &_src_loc); |
| 1226 | if (ret) { |
| 1227 | goto error; |
| 1228 | } |
| 1229 | |
| 1230 | if (_src_loc) { |
| 1231 | break; |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | bt_dwarf_cu_destroy(cu); |
| 1236 | if (_src_loc) { |
| 1237 | *src_loc = _src_loc; |
| 1238 | } |
| 1239 | |
| 1240 | return 0; |
| 1241 | |
| 1242 | error: |
| 1243 | source_location_destroy(_src_loc); |
| 1244 | bt_dwarf_cu_destroy(cu); |
| 1245 | return -1; |
| 1246 | } |