1 // SPDX-License-Identifier: GPL-2.0 2 #include <inttypes.h> 3 #include <signal.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 #include <sys/types.h> 8 9 #include <linux/kernel.h> 10 #include <linux/string.h> 11 #include <linux/zalloc.h> 12 13 #include <api/io.h> 14 15 #include "util/dso.h" 16 #include "util/debug.h" 17 #include "util/callchain.h" 18 #include "util/symbol_conf.h" 19 #include "srcline.h" 20 #include "string2.h" 21 #include "symbol.h" 22 #include "subcmd/run-command.h" 23 24 bool srcline_full_filename; 25 26 char *srcline__unknown = (char *)"??:0"; 27 28 static const char *dso__name(struct dso *dso) 29 { 30 const char *dso_name; 31 32 if (dso->symsrc_filename) 33 dso_name = dso->symsrc_filename; 34 else 35 dso_name = dso->long_name; 36 37 if (dso_name[0] == '[') 38 return NULL; 39 40 if (!strncmp(dso_name, "/tmp/perf-", 10)) 41 return NULL; 42 43 return dso_name; 44 } 45 46 static int inline_list__append(struct symbol *symbol, char *srcline, 47 struct inline_node *node) 48 { 49 struct inline_list *ilist; 50 51 ilist = zalloc(sizeof(*ilist)); 52 if (ilist == NULL) 53 return -1; 54 55 ilist->symbol = symbol; 56 ilist->srcline = srcline; 57 58 if (callchain_param.order == ORDER_CALLEE) 59 list_add_tail(&ilist->list, &node->val); 60 else 61 list_add(&ilist->list, &node->val); 62 63 return 0; 64 } 65 66 /* basename version that takes a const input string */ 67 static const char *gnu_basename(const char *path) 68 { 69 const char *base = strrchr(path, '/'); 70 71 return base ? base + 1 : path; 72 } 73 74 static char *srcline_from_fileline(const char *file, unsigned int line) 75 { 76 char *srcline; 77 78 if (!file) 79 return NULL; 80 81 if (!srcline_full_filename) 82 file = gnu_basename(file); 83 84 if (asprintf(&srcline, "%s:%u", file, line) < 0) 85 return NULL; 86 87 return srcline; 88 } 89 90 static struct symbol *new_inline_sym(struct dso *dso, 91 struct symbol *base_sym, 92 const char *funcname) 93 { 94 struct symbol *inline_sym; 95 char *demangled = NULL; 96 97 if (!funcname) 98 funcname = "??"; 99 100 if (dso) { 101 demangled = dso__demangle_sym(dso, 0, funcname); 102 if (demangled) 103 funcname = demangled; 104 } 105 106 if (base_sym && strcmp(funcname, base_sym->name) == 0) { 107 /* reuse the real, existing symbol */ 108 inline_sym = base_sym; 109 /* ensure that we don't alias an inlined symbol, which could 110 * lead to double frees in inline_node__delete 111 */ 112 assert(!base_sym->inlined); 113 } else { 114 /* create a fake symbol for the inline frame */ 115 inline_sym = symbol__new(base_sym ? base_sym->start : 0, 116 base_sym ? (base_sym->end - base_sym->start) : 0, 117 base_sym ? base_sym->binding : 0, 118 base_sym ? base_sym->type : 0, 119 funcname); 120 if (inline_sym) 121 inline_sym->inlined = 1; 122 } 123 124 free(demangled); 125 126 return inline_sym; 127 } 128 129 #define MAX_INLINE_NEST 1024 130 131 #ifdef HAVE_LIBBFD_SUPPORT 132 133 /* 134 * Implement addr2line using libbfd. 135 */ 136 #define PACKAGE "perf" 137 #include <bfd.h> 138 139 struct a2l_data { 140 const char *input; 141 u64 addr; 142 143 bool found; 144 const char *filename; 145 const char *funcname; 146 unsigned line; 147 148 bfd *abfd; 149 asymbol **syms; 150 }; 151 152 static int bfd_error(const char *string) 153 { 154 const char *errmsg; 155 156 errmsg = bfd_errmsg(bfd_get_error()); 157 fflush(stdout); 158 159 if (string) 160 pr_debug("%s: %s\n", string, errmsg); 161 else 162 pr_debug("%s\n", errmsg); 163 164 return -1; 165 } 166 167 static int slurp_symtab(bfd *abfd, struct a2l_data *a2l) 168 { 169 long storage; 170 long symcount; 171 asymbol **syms; 172 bfd_boolean dynamic = FALSE; 173 174 if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0) 175 return bfd_error(bfd_get_filename(abfd)); 176 177 storage = bfd_get_symtab_upper_bound(abfd); 178 if (storage == 0L) { 179 storage = bfd_get_dynamic_symtab_upper_bound(abfd); 180 dynamic = TRUE; 181 } 182 if (storage < 0L) 183 return bfd_error(bfd_get_filename(abfd)); 184 185 syms = malloc(storage); 186 if (dynamic) 187 symcount = bfd_canonicalize_dynamic_symtab(abfd, syms); 188 else 189 symcount = bfd_canonicalize_symtab(abfd, syms); 190 191 if (symcount < 0) { 192 free(syms); 193 return bfd_error(bfd_get_filename(abfd)); 194 } 195 196 a2l->syms = syms; 197 return 0; 198 } 199 200 static void find_address_in_section(bfd *abfd, asection *section, void *data) 201 { 202 bfd_vma pc, vma; 203 bfd_size_type size; 204 struct a2l_data *a2l = data; 205 flagword flags; 206 207 if (a2l->found) 208 return; 209 210 #ifdef bfd_get_section_flags 211 flags = bfd_get_section_flags(abfd, section); 212 #else 213 flags = bfd_section_flags(section); 214 #endif 215 if ((flags & SEC_ALLOC) == 0) 216 return; 217 218 pc = a2l->addr; 219 #ifdef bfd_get_section_vma 220 vma = bfd_get_section_vma(abfd, section); 221 #else 222 vma = bfd_section_vma(section); 223 #endif 224 #ifdef bfd_get_section_size 225 size = bfd_get_section_size(section); 226 #else 227 size = bfd_section_size(section); 228 #endif 229 230 if (pc < vma || pc >= vma + size) 231 return; 232 233 a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma, 234 &a2l->filename, &a2l->funcname, 235 &a2l->line); 236 237 if (a2l->filename && !strlen(a2l->filename)) 238 a2l->filename = NULL; 239 } 240 241 static struct a2l_data *addr2line_init(const char *path) 242 { 243 bfd *abfd; 244 struct a2l_data *a2l = NULL; 245 246 abfd = bfd_openr(path, NULL); 247 if (abfd == NULL) 248 return NULL; 249 250 if (!bfd_check_format(abfd, bfd_object)) 251 goto out; 252 253 a2l = zalloc(sizeof(*a2l)); 254 if (a2l == NULL) 255 goto out; 256 257 a2l->abfd = abfd; 258 a2l->input = strdup(path); 259 if (a2l->input == NULL) 260 goto out; 261 262 if (slurp_symtab(abfd, a2l)) 263 goto out; 264 265 return a2l; 266 267 out: 268 if (a2l) { 269 zfree((char **)&a2l->input); 270 free(a2l); 271 } 272 bfd_close(abfd); 273 return NULL; 274 } 275 276 static void addr2line_cleanup(struct a2l_data *a2l) 277 { 278 if (a2l->abfd) 279 bfd_close(a2l->abfd); 280 zfree((char **)&a2l->input); 281 zfree(&a2l->syms); 282 free(a2l); 283 } 284 285 static int inline_list__append_dso_a2l(struct dso *dso, 286 struct inline_node *node, 287 struct symbol *sym) 288 { 289 struct a2l_data *a2l = dso->a2l; 290 struct symbol *inline_sym = new_inline_sym(dso, sym, a2l->funcname); 291 char *srcline = NULL; 292 293 if (a2l->filename) 294 srcline = srcline_from_fileline(a2l->filename, a2l->line); 295 296 return inline_list__append(inline_sym, srcline, node); 297 } 298 299 static int addr2line(const char *dso_name, u64 addr, 300 char **file, unsigned int *line, struct dso *dso, 301 bool unwind_inlines, struct inline_node *node, 302 struct symbol *sym) 303 { 304 int ret = 0; 305 struct a2l_data *a2l = dso->a2l; 306 307 if (!a2l) { 308 dso->a2l = addr2line_init(dso_name); 309 a2l = dso->a2l; 310 } 311 312 if (a2l == NULL) { 313 if (!symbol_conf.disable_add2line_warn) 314 pr_warning("addr2line_init failed for %s\n", dso_name); 315 return 0; 316 } 317 318 a2l->addr = addr; 319 a2l->found = false; 320 321 bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l); 322 323 if (!a2l->found) 324 return 0; 325 326 if (unwind_inlines) { 327 int cnt = 0; 328 329 if (node && inline_list__append_dso_a2l(dso, node, sym)) 330 return 0; 331 332 while (bfd_find_inliner_info(a2l->abfd, &a2l->filename, 333 &a2l->funcname, &a2l->line) && 334 cnt++ < MAX_INLINE_NEST) { 335 336 if (a2l->filename && !strlen(a2l->filename)) 337 a2l->filename = NULL; 338 339 if (node != NULL) { 340 if (inline_list__append_dso_a2l(dso, node, sym)) 341 return 0; 342 // found at least one inline frame 343 ret = 1; 344 } 345 } 346 } 347 348 if (file) { 349 *file = a2l->filename ? strdup(a2l->filename) : NULL; 350 ret = *file ? 1 : 0; 351 } 352 353 if (line) 354 *line = a2l->line; 355 356 return ret; 357 } 358 359 void dso__free_a2l(struct dso *dso) 360 { 361 struct a2l_data *a2l = dso->a2l; 362 363 if (!a2l) 364 return; 365 366 addr2line_cleanup(a2l); 367 368 dso->a2l = NULL; 369 } 370 371 #else /* HAVE_LIBBFD_SUPPORT */ 372 373 static int filename_split(char *filename, unsigned int *line_nr) 374 { 375 char *sep; 376 377 sep = strchr(filename, '\n'); 378 if (sep) 379 *sep = '\0'; 380 381 if (!strcmp(filename, "??:0")) 382 return 0; 383 384 sep = strchr(filename, ':'); 385 if (sep) { 386 *sep++ = '\0'; 387 *line_nr = strtoul(sep, NULL, 0); 388 return 1; 389 } 390 391 return 0; 392 } 393 394 static void addr2line_subprocess_cleanup(struct child_process *a2l) 395 { 396 if (a2l->pid != -1) { 397 kill(a2l->pid, SIGKILL); 398 finish_command(a2l); /* ignore result, we don't care */ 399 a2l->pid = -1; 400 } 401 402 free(a2l); 403 } 404 405 static struct child_process *addr2line_subprocess_init(const char *addr2line_path, 406 const char *binary_path) 407 { 408 const char *argv[] = { 409 addr2line_path ?: "addr2line", 410 "-e", binary_path, 411 "-i", "-f", NULL 412 }; 413 struct child_process *a2l = zalloc(sizeof(*a2l)); 414 int start_command_status = 0; 415 416 if (a2l == NULL) { 417 pr_err("Failed to allocate memory for addr2line"); 418 return NULL; 419 } 420 421 a2l->pid = -1; 422 a2l->in = -1; 423 a2l->out = -1; 424 a2l->no_stderr = 1; 425 426 a2l->argv = argv; 427 start_command_status = start_command(a2l); 428 a2l->argv = NULL; /* it's not used after start_command; avoid dangling pointers */ 429 430 if (start_command_status != 0) { 431 pr_warning("could not start addr2line (%s) for %s: start_command return code %d\n", 432 addr2line_path, binary_path, start_command_status); 433 addr2line_subprocess_cleanup(a2l); 434 return NULL; 435 } 436 437 return a2l; 438 } 439 440 enum a2l_style { 441 BROKEN, 442 GNU_BINUTILS, 443 LLVM, 444 }; 445 446 static enum a2l_style addr2line_configure(struct child_process *a2l) 447 { 448 static bool cached; 449 static enum a2l_style style; 450 451 if (!cached) { 452 char buf[128]; 453 struct io io; 454 int ch; 455 456 if (write(a2l->in, ",\n", 2) != 2) 457 return BROKEN; 458 459 io__init(&io, a2l->out, buf, sizeof(buf)); 460 ch = io__get_char(&io); 461 if (ch == ',') { 462 style = LLVM; 463 cached = true; 464 } else if (ch == '?') { 465 style = GNU_BINUTILS; 466 cached = true; 467 } else { 468 style = BROKEN; 469 } 470 do { 471 ch = io__get_char(&io); 472 } while (ch > 0 && ch != '\n'); 473 if (style == GNU_BINUTILS) { 474 do { 475 ch = io__get_char(&io); 476 } while (ch > 0 && ch != '\n'); 477 } 478 /* Ignore SIGPIPE in the event addr2line exits. */ 479 signal(SIGPIPE, SIG_IGN); 480 } 481 return style; 482 } 483 484 static int read_addr2line_record(struct io *io, 485 enum a2l_style style, 486 char **function, 487 char **filename, 488 unsigned int *line_nr) 489 { 490 /* 491 * Returns: 492 * -1 ==> error 493 * 0 ==> sentinel (or other ill-formed) record read 494 * 1 ==> a genuine record read 495 */ 496 char *line = NULL; 497 size_t line_len = 0; 498 unsigned int dummy_line_nr = 0; 499 int ret = -1; 500 501 if (function != NULL) 502 zfree(function); 503 504 if (filename != NULL) 505 zfree(filename); 506 507 if (line_nr != NULL) 508 *line_nr = 0; 509 510 if (io__getline(io, &line, &line_len) < 0 || !line_len) 511 goto error; 512 513 if (style == LLVM && line_len == 2 && line[0] == ',') { 514 zfree(&line); 515 return 0; 516 } 517 518 if (function != NULL) 519 *function = strdup(strim(line)); 520 521 zfree(&line); 522 line_len = 0; 523 524 if (io__getline(io, &line, &line_len) < 0 || !line_len) 525 goto error; 526 527 if (filename_split(line, line_nr == NULL ? &dummy_line_nr : line_nr) == 0 && 528 style == GNU_BINUTILS) { 529 ret = 0; 530 goto error; 531 } 532 533 if (filename != NULL) 534 *filename = strdup(line); 535 536 zfree(&line); 537 line_len = 0; 538 539 return 1; 540 541 error: 542 free(line); 543 if (function != NULL) 544 zfree(function); 545 if (filename != NULL) 546 zfree(filename); 547 return ret; 548 } 549 550 static int inline_list__append_record(struct dso *dso, 551 struct inline_node *node, 552 struct symbol *sym, 553 const char *function, 554 const char *filename, 555 unsigned int line_nr) 556 { 557 struct symbol *inline_sym = new_inline_sym(dso, sym, function); 558 559 return inline_list__append(inline_sym, srcline_from_fileline(filename, line_nr), node); 560 } 561 562 static int addr2line(const char *dso_name, u64 addr, 563 char **file, unsigned int *line_nr, 564 struct dso *dso, 565 bool unwind_inlines, 566 struct inline_node *node, 567 struct symbol *sym __maybe_unused) 568 { 569 struct child_process *a2l = dso->a2l; 570 char *record_function = NULL; 571 char *record_filename = NULL; 572 unsigned int record_line_nr = 0; 573 int record_status = -1; 574 int ret = 0; 575 size_t inline_count = 0; 576 int len; 577 char buf[128]; 578 ssize_t written; 579 struct io io; 580 enum a2l_style a2l_style; 581 582 if (!a2l) { 583 if (!filename__has_section(dso_name, ".debug_line")) 584 goto out; 585 586 dso->a2l = addr2line_subprocess_init(symbol_conf.addr2line_path, 587 dso_name); 588 a2l = dso->a2l; 589 } 590 591 if (a2l == NULL) { 592 if (!symbol_conf.disable_add2line_warn) 593 pr_warning("%s %s: addr2line_subprocess_init failed\n", __func__, dso_name); 594 goto out; 595 } 596 a2l_style = addr2line_configure(a2l); 597 if (a2l_style == BROKEN) { 598 if (!symbol_conf.disable_add2line_warn) 599 pr_warning("%s: addr2line configuration failed\n", __func__); 600 goto out; 601 } 602 603 /* 604 * Send our request and then *deliberately* send something that can't be interpreted as 605 * a valid address to ask addr2line about (namely, ","). This causes addr2line to first 606 * write out the answer to our request, in an unbounded/unknown number of records, and 607 * then to write out the lines "??" and "??:0", for GNU binutils, or "," for 608 * llvm-addr2line, so that we can detect when it has finished giving us anything 609 * useful. With GNU binutils, we have to be careful about the first record, though, 610 * because it may be genuinely unknown, in which case we'll get two sets of "??"/"??:0" 611 * lines. 612 */ 613 len = snprintf(buf, sizeof(buf), "%016"PRIx64"\n,\n", addr); 614 written = len > 0 ? write(a2l->in, buf, len) : -1; 615 if (written != len) { 616 if (!symbol_conf.disable_add2line_warn) 617 pr_warning("%s %s: could not send request\n", __func__, dso_name); 618 goto out; 619 } 620 io__init(&io, a2l->out, buf, sizeof(buf)); 621 622 switch (read_addr2line_record(&io, a2l_style, 623 &record_function, &record_filename, &record_line_nr)) { 624 case -1: 625 if (!symbol_conf.disable_add2line_warn) 626 pr_warning("%s %s: could not read first record\n", __func__, dso_name); 627 goto out; 628 case 0: 629 /* 630 * The first record was invalid, so return failure, but first read another 631 * record, since we asked a junk question and have to clear the answer out. 632 */ 633 switch (read_addr2line_record(&io, a2l_style, NULL, NULL, NULL)) { 634 case -1: 635 if (!symbol_conf.disable_add2line_warn) 636 pr_warning("%s %s: could not read delimiter record\n", 637 __func__, dso_name); 638 break; 639 case 0: 640 /* As expected. */ 641 break; 642 default: 643 if (!symbol_conf.disable_add2line_warn) 644 pr_warning("%s %s: unexpected record instead of sentinel", 645 __func__, dso_name); 646 break; 647 } 648 goto out; 649 default: 650 break; 651 } 652 653 if (file) { 654 *file = strdup(record_filename); 655 ret = 1; 656 } 657 if (line_nr) 658 *line_nr = record_line_nr; 659 660 if (unwind_inlines) { 661 if (node && inline_list__append_record(dso, node, sym, 662 record_function, 663 record_filename, 664 record_line_nr)) { 665 ret = 0; 666 goto out; 667 } 668 } 669 670 /* We have to read the records even if we don't care about the inline info. */ 671 while ((record_status = read_addr2line_record(&io, 672 a2l_style, 673 &record_function, 674 &record_filename, 675 &record_line_nr)) == 1) { 676 if (unwind_inlines && node && inline_count++ < MAX_INLINE_NEST) { 677 if (inline_list__append_record(dso, node, sym, 678 record_function, 679 record_filename, 680 record_line_nr)) { 681 ret = 0; 682 goto out; 683 } 684 ret = 1; /* found at least one inline frame */ 685 } 686 } 687 688 out: 689 free(record_function); 690 free(record_filename); 691 return ret; 692 } 693 694 void dso__free_a2l(struct dso *dso) 695 { 696 struct child_process *a2l = dso->a2l; 697 698 if (!a2l) 699 return; 700 701 addr2line_subprocess_cleanup(a2l); 702 703 dso->a2l = NULL; 704 } 705 706 #endif /* HAVE_LIBBFD_SUPPORT */ 707 708 static struct inline_node *addr2inlines(const char *dso_name, u64 addr, 709 struct dso *dso, struct symbol *sym) 710 { 711 struct inline_node *node; 712 713 node = zalloc(sizeof(*node)); 714 if (node == NULL) { 715 perror("not enough memory for the inline node"); 716 return NULL; 717 } 718 719 INIT_LIST_HEAD(&node->val); 720 node->addr = addr; 721 722 addr2line(dso_name, addr, NULL, NULL, dso, true, node, sym); 723 return node; 724 } 725 726 /* 727 * Number of addr2line failures (without success) before disabling it for that 728 * dso. 729 */ 730 #define A2L_FAIL_LIMIT 123 731 732 char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym, 733 bool show_sym, bool show_addr, bool unwind_inlines, 734 u64 ip) 735 { 736 char *file = NULL; 737 unsigned line = 0; 738 char *srcline; 739 const char *dso_name; 740 741 if (!dso->has_srcline) 742 goto out; 743 744 dso_name = dso__name(dso); 745 if (dso_name == NULL) 746 goto out; 747 748 if (!addr2line(dso_name, addr, &file, &line, dso, 749 unwind_inlines, NULL, sym)) 750 goto out; 751 752 srcline = srcline_from_fileline(file, line); 753 free(file); 754 755 if (!srcline) 756 goto out; 757 758 dso->a2l_fails = 0; 759 760 return srcline; 761 762 out: 763 if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) { 764 dso->has_srcline = 0; 765 dso__free_a2l(dso); 766 } 767 768 if (!show_addr) 769 return (show_sym && sym) ? 770 strndup(sym->name, sym->namelen) : SRCLINE_UNKNOWN; 771 772 if (sym) { 773 if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "", 774 ip - sym->start) < 0) 775 return SRCLINE_UNKNOWN; 776 } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0) 777 return SRCLINE_UNKNOWN; 778 return srcline; 779 } 780 781 /* Returns filename and fills in line number in line */ 782 char *get_srcline_split(struct dso *dso, u64 addr, unsigned *line) 783 { 784 char *file = NULL; 785 const char *dso_name; 786 787 if (!dso->has_srcline) 788 goto out; 789 790 dso_name = dso__name(dso); 791 if (dso_name == NULL) 792 goto out; 793 794 if (!addr2line(dso_name, addr, &file, line, dso, true, NULL, NULL)) 795 goto out; 796 797 dso->a2l_fails = 0; 798 return file; 799 800 out: 801 if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) { 802 dso->has_srcline = 0; 803 dso__free_a2l(dso); 804 } 805 806 return NULL; 807 } 808 809 void zfree_srcline(char **srcline) 810 { 811 if (*srcline == NULL) 812 return; 813 814 if (*srcline != SRCLINE_UNKNOWN) 815 free(*srcline); 816 817 *srcline = NULL; 818 } 819 820 char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym, 821 bool show_sym, bool show_addr, u64 ip) 822 { 823 return __get_srcline(dso, addr, sym, show_sym, show_addr, false, ip); 824 } 825 826 struct srcline_node { 827 u64 addr; 828 char *srcline; 829 struct rb_node rb_node; 830 }; 831 832 void srcline__tree_insert(struct rb_root_cached *tree, u64 addr, char *srcline) 833 { 834 struct rb_node **p = &tree->rb_root.rb_node; 835 struct rb_node *parent = NULL; 836 struct srcline_node *i, *node; 837 bool leftmost = true; 838 839 node = zalloc(sizeof(struct srcline_node)); 840 if (!node) { 841 perror("not enough memory for the srcline node"); 842 return; 843 } 844 845 node->addr = addr; 846 node->srcline = srcline; 847 848 while (*p != NULL) { 849 parent = *p; 850 i = rb_entry(parent, struct srcline_node, rb_node); 851 if (addr < i->addr) 852 p = &(*p)->rb_left; 853 else { 854 p = &(*p)->rb_right; 855 leftmost = false; 856 } 857 } 858 rb_link_node(&node->rb_node, parent, p); 859 rb_insert_color_cached(&node->rb_node, tree, leftmost); 860 } 861 862 char *srcline__tree_find(struct rb_root_cached *tree, u64 addr) 863 { 864 struct rb_node *n = tree->rb_root.rb_node; 865 866 while (n) { 867 struct srcline_node *i = rb_entry(n, struct srcline_node, 868 rb_node); 869 870 if (addr < i->addr) 871 n = n->rb_left; 872 else if (addr > i->addr) 873 n = n->rb_right; 874 else 875 return i->srcline; 876 } 877 878 return NULL; 879 } 880 881 void srcline__tree_delete(struct rb_root_cached *tree) 882 { 883 struct srcline_node *pos; 884 struct rb_node *next = rb_first_cached(tree); 885 886 while (next) { 887 pos = rb_entry(next, struct srcline_node, rb_node); 888 next = rb_next(&pos->rb_node); 889 rb_erase_cached(&pos->rb_node, tree); 890 zfree_srcline(&pos->srcline); 891 zfree(&pos); 892 } 893 } 894 895 struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr, 896 struct symbol *sym) 897 { 898 const char *dso_name; 899 900 dso_name = dso__name(dso); 901 if (dso_name == NULL) 902 return NULL; 903 904 return addr2inlines(dso_name, addr, dso, sym); 905 } 906 907 void inline_node__delete(struct inline_node *node) 908 { 909 struct inline_list *ilist, *tmp; 910 911 list_for_each_entry_safe(ilist, tmp, &node->val, list) { 912 list_del_init(&ilist->list); 913 zfree_srcline(&ilist->srcline); 914 /* only the inlined symbols are owned by the list */ 915 if (ilist->symbol && ilist->symbol->inlined) 916 symbol__delete(ilist->symbol); 917 free(ilist); 918 } 919 920 free(node); 921 } 922 923 void inlines__tree_insert(struct rb_root_cached *tree, 924 struct inline_node *inlines) 925 { 926 struct rb_node **p = &tree->rb_root.rb_node; 927 struct rb_node *parent = NULL; 928 const u64 addr = inlines->addr; 929 struct inline_node *i; 930 bool leftmost = true; 931 932 while (*p != NULL) { 933 parent = *p; 934 i = rb_entry(parent, struct inline_node, rb_node); 935 if (addr < i->addr) 936 p = &(*p)->rb_left; 937 else { 938 p = &(*p)->rb_right; 939 leftmost = false; 940 } 941 } 942 rb_link_node(&inlines->rb_node, parent, p); 943 rb_insert_color_cached(&inlines->rb_node, tree, leftmost); 944 } 945 946 struct inline_node *inlines__tree_find(struct rb_root_cached *tree, u64 addr) 947 { 948 struct rb_node *n = tree->rb_root.rb_node; 949 950 while (n) { 951 struct inline_node *i = rb_entry(n, struct inline_node, 952 rb_node); 953 954 if (addr < i->addr) 955 n = n->rb_left; 956 else if (addr > i->addr) 957 n = n->rb_right; 958 else 959 return i; 960 } 961 962 return NULL; 963 } 964 965 void inlines__tree_delete(struct rb_root_cached *tree) 966 { 967 struct inline_node *pos; 968 struct rb_node *next = rb_first_cached(tree); 969 970 while (next) { 971 pos = rb_entry(next, struct inline_node, rb_node); 972 next = rb_next(&pos->rb_node); 973 rb_erase_cached(&pos->rb_node, tree); 974 inline_node__delete(pos); 975 } 976 } 977