Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <inttypes.h>
0003 #include <signal.h>
0004 #include <stdio.h>
0005 #include <stdlib.h>
0006 #include <string.h>
0007 #include <sys/types.h>
0008 
0009 #include <linux/kernel.h>
0010 #include <linux/string.h>
0011 #include <linux/zalloc.h>
0012 
0013 #include "util/dso.h"
0014 #include "util/debug.h"
0015 #include "util/callchain.h"
0016 #include "util/symbol_conf.h"
0017 #include "srcline.h"
0018 #include "string2.h"
0019 #include "symbol.h"
0020 #include "subcmd/run-command.h"
0021 
0022 bool srcline_full_filename;
0023 
0024 static const char *dso__name(struct dso *dso)
0025 {
0026     const char *dso_name;
0027 
0028     if (dso->symsrc_filename)
0029         dso_name = dso->symsrc_filename;
0030     else
0031         dso_name = dso->long_name;
0032 
0033     if (dso_name[0] == '[')
0034         return NULL;
0035 
0036     if (!strncmp(dso_name, "/tmp/perf-", 10))
0037         return NULL;
0038 
0039     return dso_name;
0040 }
0041 
0042 static int inline_list__append(struct symbol *symbol, char *srcline,
0043                    struct inline_node *node)
0044 {
0045     struct inline_list *ilist;
0046 
0047     ilist = zalloc(sizeof(*ilist));
0048     if (ilist == NULL)
0049         return -1;
0050 
0051     ilist->symbol = symbol;
0052     ilist->srcline = srcline;
0053 
0054     if (callchain_param.order == ORDER_CALLEE)
0055         list_add_tail(&ilist->list, &node->val);
0056     else
0057         list_add(&ilist->list, &node->val);
0058 
0059     return 0;
0060 }
0061 
0062 /* basename version that takes a const input string */
0063 static const char *gnu_basename(const char *path)
0064 {
0065     const char *base = strrchr(path, '/');
0066 
0067     return base ? base + 1 : path;
0068 }
0069 
0070 static char *srcline_from_fileline(const char *file, unsigned int line)
0071 {
0072     char *srcline;
0073 
0074     if (!file)
0075         return NULL;
0076 
0077     if (!srcline_full_filename)
0078         file = gnu_basename(file);
0079 
0080     if (asprintf(&srcline, "%s:%u", file, line) < 0)
0081         return NULL;
0082 
0083     return srcline;
0084 }
0085 
0086 static struct symbol *new_inline_sym(struct dso *dso,
0087                      struct symbol *base_sym,
0088                      const char *funcname)
0089 {
0090     struct symbol *inline_sym;
0091     char *demangled = NULL;
0092 
0093     if (!funcname)
0094         funcname = "??";
0095 
0096     if (dso) {
0097         demangled = dso__demangle_sym(dso, 0, funcname);
0098         if (demangled)
0099             funcname = demangled;
0100     }
0101 
0102     if (base_sym && strcmp(funcname, base_sym->name) == 0) {
0103         /* reuse the real, existing symbol */
0104         inline_sym = base_sym;
0105         /* ensure that we don't alias an inlined symbol, which could
0106          * lead to double frees in inline_node__delete
0107          */
0108         assert(!base_sym->inlined);
0109     } else {
0110         /* create a fake symbol for the inline frame */
0111         inline_sym = symbol__new(base_sym ? base_sym->start : 0,
0112                      base_sym ? (base_sym->end - base_sym->start) : 0,
0113                      base_sym ? base_sym->binding : 0,
0114                      base_sym ? base_sym->type : 0,
0115                      funcname);
0116         if (inline_sym)
0117             inline_sym->inlined = 1;
0118     }
0119 
0120     free(demangled);
0121 
0122     return inline_sym;
0123 }
0124 
0125 #define MAX_INLINE_NEST 1024
0126 
0127 #ifdef HAVE_LIBBFD_SUPPORT
0128 
0129 /*
0130  * Implement addr2line using libbfd.
0131  */
0132 #define PACKAGE "perf"
0133 #include <bfd.h>
0134 
0135 struct a2l_data {
0136     const char  *input;
0137     u64     addr;
0138 
0139     bool        found;
0140     const char  *filename;
0141     const char  *funcname;
0142     unsigned    line;
0143 
0144     bfd         *abfd;
0145     asymbol     **syms;
0146 };
0147 
0148 static int bfd_error(const char *string)
0149 {
0150     const char *errmsg;
0151 
0152     errmsg = bfd_errmsg(bfd_get_error());
0153     fflush(stdout);
0154 
0155     if (string)
0156         pr_debug("%s: %s\n", string, errmsg);
0157     else
0158         pr_debug("%s\n", errmsg);
0159 
0160     return -1;
0161 }
0162 
0163 static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
0164 {
0165     long storage;
0166     long symcount;
0167     asymbol **syms;
0168     bfd_boolean dynamic = FALSE;
0169 
0170     if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
0171         return bfd_error(bfd_get_filename(abfd));
0172 
0173     storage = bfd_get_symtab_upper_bound(abfd);
0174     if (storage == 0L) {
0175         storage = bfd_get_dynamic_symtab_upper_bound(abfd);
0176         dynamic = TRUE;
0177     }
0178     if (storage < 0L)
0179         return bfd_error(bfd_get_filename(abfd));
0180 
0181     syms = malloc(storage);
0182     if (dynamic)
0183         symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
0184     else
0185         symcount = bfd_canonicalize_symtab(abfd, syms);
0186 
0187     if (symcount < 0) {
0188         free(syms);
0189         return bfd_error(bfd_get_filename(abfd));
0190     }
0191 
0192     a2l->syms = syms;
0193     return 0;
0194 }
0195 
0196 static void find_address_in_section(bfd *abfd, asection *section, void *data)
0197 {
0198     bfd_vma pc, vma;
0199     bfd_size_type size;
0200     struct a2l_data *a2l = data;
0201     flagword flags;
0202 
0203     if (a2l->found)
0204         return;
0205 
0206 #ifdef bfd_get_section_flags
0207     flags = bfd_get_section_flags(abfd, section);
0208 #else
0209     flags = bfd_section_flags(section);
0210 #endif
0211     if ((flags & SEC_ALLOC) == 0)
0212         return;
0213 
0214     pc = a2l->addr;
0215 #ifdef bfd_get_section_vma
0216     vma = bfd_get_section_vma(abfd, section);
0217 #else
0218     vma = bfd_section_vma(section);
0219 #endif
0220 #ifdef bfd_get_section_size
0221     size = bfd_get_section_size(section);
0222 #else
0223     size = bfd_section_size(section);
0224 #endif
0225 
0226     if (pc < vma || pc >= vma + size)
0227         return;
0228 
0229     a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
0230                        &a2l->filename, &a2l->funcname,
0231                        &a2l->line);
0232 
0233     if (a2l->filename && !strlen(a2l->filename))
0234         a2l->filename = NULL;
0235 }
0236 
0237 static struct a2l_data *addr2line_init(const char *path)
0238 {
0239     bfd *abfd;
0240     struct a2l_data *a2l = NULL;
0241 
0242     abfd = bfd_openr(path, NULL);
0243     if (abfd == NULL)
0244         return NULL;
0245 
0246     if (!bfd_check_format(abfd, bfd_object))
0247         goto out;
0248 
0249     a2l = zalloc(sizeof(*a2l));
0250     if (a2l == NULL)
0251         goto out;
0252 
0253     a2l->abfd = abfd;
0254     a2l->input = strdup(path);
0255     if (a2l->input == NULL)
0256         goto out;
0257 
0258     if (slurp_symtab(abfd, a2l))
0259         goto out;
0260 
0261     return a2l;
0262 
0263 out:
0264     if (a2l) {
0265         zfree((char **)&a2l->input);
0266         free(a2l);
0267     }
0268     bfd_close(abfd);
0269     return NULL;
0270 }
0271 
0272 static void addr2line_cleanup(struct a2l_data *a2l)
0273 {
0274     if (a2l->abfd)
0275         bfd_close(a2l->abfd);
0276     zfree((char **)&a2l->input);
0277     zfree(&a2l->syms);
0278     free(a2l);
0279 }
0280 
0281 static int inline_list__append_dso_a2l(struct dso *dso,
0282                        struct inline_node *node,
0283                        struct symbol *sym)
0284 {
0285     struct a2l_data *a2l = dso->a2l;
0286     struct symbol *inline_sym = new_inline_sym(dso, sym, a2l->funcname);
0287     char *srcline = NULL;
0288 
0289     if (a2l->filename)
0290         srcline = srcline_from_fileline(a2l->filename, a2l->line);
0291 
0292     return inline_list__append(inline_sym, srcline, node);
0293 }
0294 
0295 static int addr2line(const char *dso_name, u64 addr,
0296              char **file, unsigned int *line, struct dso *dso,
0297              bool unwind_inlines, struct inline_node *node,
0298              struct symbol *sym)
0299 {
0300     int ret = 0;
0301     struct a2l_data *a2l = dso->a2l;
0302 
0303     if (!a2l) {
0304         dso->a2l = addr2line_init(dso_name);
0305         a2l = dso->a2l;
0306     }
0307 
0308     if (a2l == NULL) {
0309         if (!symbol_conf.disable_add2line_warn)
0310             pr_warning("addr2line_init failed for %s\n", dso_name);
0311         return 0;
0312     }
0313 
0314     a2l->addr = addr;
0315     a2l->found = false;
0316 
0317     bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
0318 
0319     if (!a2l->found)
0320         return 0;
0321 
0322     if (unwind_inlines) {
0323         int cnt = 0;
0324 
0325         if (node && inline_list__append_dso_a2l(dso, node, sym))
0326             return 0;
0327 
0328         while (bfd_find_inliner_info(a2l->abfd, &a2l->filename,
0329                          &a2l->funcname, &a2l->line) &&
0330                cnt++ < MAX_INLINE_NEST) {
0331 
0332             if (a2l->filename && !strlen(a2l->filename))
0333                 a2l->filename = NULL;
0334 
0335             if (node != NULL) {
0336                 if (inline_list__append_dso_a2l(dso, node, sym))
0337                     return 0;
0338                 // found at least one inline frame
0339                 ret = 1;
0340             }
0341         }
0342     }
0343 
0344     if (file) {
0345         *file = a2l->filename ? strdup(a2l->filename) : NULL;
0346         ret = *file ? 1 : 0;
0347     }
0348 
0349     if (line)
0350         *line = a2l->line;
0351 
0352     return ret;
0353 }
0354 
0355 void dso__free_a2l(struct dso *dso)
0356 {
0357     struct a2l_data *a2l = dso->a2l;
0358 
0359     if (!a2l)
0360         return;
0361 
0362     addr2line_cleanup(a2l);
0363 
0364     dso->a2l = NULL;
0365 }
0366 
0367 #else /* HAVE_LIBBFD_SUPPORT */
0368 
0369 struct a2l_subprocess {
0370     struct child_process addr2line;
0371     FILE *to_child;
0372     FILE *from_child;
0373 };
0374 
0375 static int filename_split(char *filename, unsigned int *line_nr)
0376 {
0377     char *sep;
0378 
0379     sep = strchr(filename, '\n');
0380     if (sep)
0381         *sep = '\0';
0382 
0383     if (!strcmp(filename, "??:0"))
0384         return 0;
0385 
0386     sep = strchr(filename, ':');
0387     if (sep) {
0388         *sep++ = '\0';
0389         *line_nr = strtoul(sep, NULL, 0);
0390         return 1;
0391     }
0392 
0393     return 0;
0394 }
0395 
0396 static void addr2line_subprocess_cleanup(struct a2l_subprocess *a2l)
0397 {
0398     if (a2l->addr2line.pid != -1) {
0399         kill(a2l->addr2line.pid, SIGKILL);
0400         finish_command(&a2l->addr2line); /* ignore result, we don't care */
0401         a2l->addr2line.pid = -1;
0402     }
0403 
0404     if (a2l->to_child != NULL) {
0405         fclose(a2l->to_child);
0406         a2l->to_child = NULL;
0407     }
0408 
0409     if (a2l->from_child != NULL) {
0410         fclose(a2l->from_child);
0411         a2l->from_child = NULL;
0412     }
0413 
0414     free(a2l);
0415 }
0416 
0417 static struct a2l_subprocess *addr2line_subprocess_init(const char *path)
0418 {
0419     const char *argv[] = { "addr2line", "-e", path, "-i", "-f", NULL };
0420     struct a2l_subprocess *a2l = zalloc(sizeof(*a2l));
0421     int start_command_status = 0;
0422 
0423     if (a2l == NULL)
0424         goto out;
0425 
0426     a2l->to_child = NULL;
0427     a2l->from_child = NULL;
0428 
0429     a2l->addr2line.pid = -1;
0430     a2l->addr2line.in = -1;
0431     a2l->addr2line.out = -1;
0432     a2l->addr2line.no_stderr = 1;
0433 
0434     a2l->addr2line.argv = argv;
0435     start_command_status = start_command(&a2l->addr2line);
0436     a2l->addr2line.argv = NULL; /* it's not used after start_command; avoid dangling pointers */
0437 
0438     if (start_command_status != 0) {
0439         pr_warning("could not start addr2line for %s: start_command return code %d\n",
0440                path,
0441                start_command_status);
0442         goto out;
0443     }
0444 
0445     a2l->to_child = fdopen(a2l->addr2line.in, "w");
0446     if (a2l->to_child == NULL) {
0447         pr_warning("could not open write-stream to addr2line of %s\n", path);
0448         goto out;
0449     }
0450 
0451     a2l->from_child = fdopen(a2l->addr2line.out, "r");
0452     if (a2l->from_child == NULL) {
0453         pr_warning("could not open read-stream from addr2line of %s\n", path);
0454         goto out;
0455     }
0456 
0457     return a2l;
0458 
0459 out:
0460     if (a2l)
0461         addr2line_subprocess_cleanup(a2l);
0462 
0463     return NULL;
0464 }
0465 
0466 static int read_addr2line_record(struct a2l_subprocess *a2l,
0467                  char **function,
0468                  char **filename,
0469                  unsigned int *line_nr)
0470 {
0471     /*
0472      * Returns:
0473      * -1 ==> error
0474      * 0 ==> sentinel (or other ill-formed) record read
0475      * 1 ==> a genuine record read
0476      */
0477     char *line = NULL;
0478     size_t line_len = 0;
0479     unsigned int dummy_line_nr = 0;
0480     int ret = -1;
0481 
0482     if (function != NULL)
0483         zfree(function);
0484 
0485     if (filename != NULL)
0486         zfree(filename);
0487 
0488     if (line_nr != NULL)
0489         *line_nr = 0;
0490 
0491     if (getline(&line, &line_len, a2l->from_child) < 0 || !line_len)
0492         goto error;
0493 
0494     if (function != NULL)
0495         *function = strdup(strim(line));
0496 
0497     zfree(&line);
0498     line_len = 0;
0499 
0500     if (getline(&line, &line_len, a2l->from_child) < 0 || !line_len)
0501         goto error;
0502 
0503     if (filename_split(line, line_nr == NULL ? &dummy_line_nr : line_nr) == 0) {
0504         ret = 0;
0505         goto error;
0506     }
0507 
0508     if (filename != NULL)
0509         *filename = strdup(line);
0510 
0511     zfree(&line);
0512     line_len = 0;
0513 
0514     return 1;
0515 
0516 error:
0517     free(line);
0518     if (function != NULL)
0519         zfree(function);
0520     if (filename != NULL)
0521         zfree(filename);
0522     return ret;
0523 }
0524 
0525 static int inline_list__append_record(struct dso *dso,
0526                       struct inline_node *node,
0527                       struct symbol *sym,
0528                       const char *function,
0529                       const char *filename,
0530                       unsigned int line_nr)
0531 {
0532     struct symbol *inline_sym = new_inline_sym(dso, sym, function);
0533 
0534     return inline_list__append(inline_sym, srcline_from_fileline(filename, line_nr), node);
0535 }
0536 
0537 static int addr2line(const char *dso_name, u64 addr,
0538              char **file, unsigned int *line_nr,
0539              struct dso *dso,
0540              bool unwind_inlines,
0541              struct inline_node *node,
0542              struct symbol *sym __maybe_unused)
0543 {
0544     struct a2l_subprocess *a2l = dso->a2l;
0545     char *record_function = NULL;
0546     char *record_filename = NULL;
0547     unsigned int record_line_nr = 0;
0548     int record_status = -1;
0549     int ret = 0;
0550     size_t inline_count = 0;
0551 
0552     if (!a2l) {
0553         dso->a2l = addr2line_subprocess_init(dso_name);
0554         a2l = dso->a2l;
0555     }
0556 
0557     if (a2l == NULL) {
0558         if (!symbol_conf.disable_add2line_warn)
0559             pr_warning("%s %s: addr2line_subprocess_init failed\n", __func__, dso_name);
0560         goto out;
0561     }
0562 
0563     /*
0564      * Send our request and then *deliberately* send something that can't be interpreted as
0565      * a valid address to ask addr2line about (namely, ","). This causes addr2line to first
0566      * write out the answer to our request, in an unbounded/unknown number of records, and
0567      * then to write out the lines "??" and "??:0", so that we can detect when it has
0568      * finished giving us anything useful. We have to be careful about the first record,
0569      * though, because it may be genuinely unknown, in which case we'll get two sets of
0570      * "??"/"??:0" lines.
0571      */
0572     if (fprintf(a2l->to_child, "%016"PRIx64"\n,\n", addr) < 0 || fflush(a2l->to_child) != 0) {
0573         pr_warning("%s %s: could not send request\n", __func__, dso_name);
0574         goto out;
0575     }
0576 
0577     switch (read_addr2line_record(a2l, &record_function, &record_filename, &record_line_nr)) {
0578     case -1:
0579         pr_warning("%s %s: could not read first record\n", __func__, dso_name);
0580         goto out;
0581     case 0:
0582         /*
0583          * The first record was invalid, so return failure, but first read another
0584          * record, since we asked a junk question and have to clear the answer out.
0585          */
0586         switch (read_addr2line_record(a2l, NULL, NULL, NULL)) {
0587         case -1:
0588             pr_warning("%s %s: could not read delimiter record\n", __func__, dso_name);
0589             break;
0590         case 0:
0591             /* As expected. */
0592             break;
0593         default:
0594             pr_warning("%s %s: unexpected record instead of sentinel",
0595                    __func__, dso_name);
0596             break;
0597         }
0598         goto out;
0599     default:
0600         break;
0601     }
0602 
0603     if (file) {
0604         *file = strdup(record_filename);
0605         ret = 1;
0606     }
0607     if (line_nr)
0608         *line_nr = record_line_nr;
0609 
0610     if (unwind_inlines) {
0611         if (node && inline_list__append_record(dso, node, sym,
0612                                record_function,
0613                                record_filename,
0614                                record_line_nr)) {
0615             ret = 0;
0616             goto out;
0617         }
0618     }
0619 
0620     /* We have to read the records even if we don't care about the inline info. */
0621     while ((record_status = read_addr2line_record(a2l,
0622                               &record_function,
0623                               &record_filename,
0624                               &record_line_nr)) == 1) {
0625         if (unwind_inlines && node && inline_count++ < MAX_INLINE_NEST) {
0626             if (inline_list__append_record(dso, node, sym,
0627                                record_function,
0628                                record_filename,
0629                                record_line_nr)) {
0630                 ret = 0;
0631                 goto out;
0632             }
0633             ret = 1; /* found at least one inline frame */
0634         }
0635     }
0636 
0637 out:
0638     free(record_function);
0639     free(record_filename);
0640     return ret;
0641 }
0642 
0643 void dso__free_a2l(struct dso *dso)
0644 {
0645     struct a2l_subprocess *a2l = dso->a2l;
0646 
0647     if (!a2l)
0648         return;
0649 
0650     addr2line_subprocess_cleanup(a2l);
0651 
0652     dso->a2l = NULL;
0653 }
0654 
0655 #endif /* HAVE_LIBBFD_SUPPORT */
0656 
0657 static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
0658                     struct dso *dso, struct symbol *sym)
0659 {
0660     struct inline_node *node;
0661 
0662     node = zalloc(sizeof(*node));
0663     if (node == NULL) {
0664         perror("not enough memory for the inline node");
0665         return NULL;
0666     }
0667 
0668     INIT_LIST_HEAD(&node->val);
0669     node->addr = addr;
0670 
0671     addr2line(dso_name, addr, NULL, NULL, dso, true, node, sym);
0672     return node;
0673 }
0674 
0675 /*
0676  * Number of addr2line failures (without success) before disabling it for that
0677  * dso.
0678  */
0679 #define A2L_FAIL_LIMIT 123
0680 
0681 char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
0682           bool show_sym, bool show_addr, bool unwind_inlines,
0683           u64 ip)
0684 {
0685     char *file = NULL;
0686     unsigned line = 0;
0687     char *srcline;
0688     const char *dso_name;
0689 
0690     if (!dso->has_srcline)
0691         goto out;
0692 
0693     dso_name = dso__name(dso);
0694     if (dso_name == NULL)
0695         goto out;
0696 
0697     if (!addr2line(dso_name, addr, &file, &line, dso,
0698                unwind_inlines, NULL, sym))
0699         goto out;
0700 
0701     srcline = srcline_from_fileline(file, line);
0702     free(file);
0703 
0704     if (!srcline)
0705         goto out;
0706 
0707     dso->a2l_fails = 0;
0708 
0709     return srcline;
0710 
0711 out:
0712     if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
0713         dso->has_srcline = 0;
0714         dso__free_a2l(dso);
0715     }
0716 
0717     if (!show_addr)
0718         return (show_sym && sym) ?
0719                 strndup(sym->name, sym->namelen) : NULL;
0720 
0721     if (sym) {
0722         if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
0723                     ip - sym->start) < 0)
0724             return SRCLINE_UNKNOWN;
0725     } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
0726         return SRCLINE_UNKNOWN;
0727     return srcline;
0728 }
0729 
0730 /* Returns filename and fills in line number in line */
0731 char *get_srcline_split(struct dso *dso, u64 addr, unsigned *line)
0732 {
0733     char *file = NULL;
0734     const char *dso_name;
0735 
0736     if (!dso->has_srcline)
0737         goto out;
0738 
0739     dso_name = dso__name(dso);
0740     if (dso_name == NULL)
0741         goto out;
0742 
0743     if (!addr2line(dso_name, addr, &file, line, dso, true, NULL, NULL))
0744         goto out;
0745 
0746     dso->a2l_fails = 0;
0747     return file;
0748 
0749 out:
0750     if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
0751         dso->has_srcline = 0;
0752         dso__free_a2l(dso);
0753     }
0754 
0755     return NULL;
0756 }
0757 
0758 void free_srcline(char *srcline)
0759 {
0760     if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
0761         free(srcline);
0762 }
0763 
0764 char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
0765           bool show_sym, bool show_addr, u64 ip)
0766 {
0767     return __get_srcline(dso, addr, sym, show_sym, show_addr, false, ip);
0768 }
0769 
0770 struct srcline_node {
0771     u64         addr;
0772     char            *srcline;
0773     struct rb_node      rb_node;
0774 };
0775 
0776 void srcline__tree_insert(struct rb_root_cached *tree, u64 addr, char *srcline)
0777 {
0778     struct rb_node **p = &tree->rb_root.rb_node;
0779     struct rb_node *parent = NULL;
0780     struct srcline_node *i, *node;
0781     bool leftmost = true;
0782 
0783     node = zalloc(sizeof(struct srcline_node));
0784     if (!node) {
0785         perror("not enough memory for the srcline node");
0786         return;
0787     }
0788 
0789     node->addr = addr;
0790     node->srcline = srcline;
0791 
0792     while (*p != NULL) {
0793         parent = *p;
0794         i = rb_entry(parent, struct srcline_node, rb_node);
0795         if (addr < i->addr)
0796             p = &(*p)->rb_left;
0797         else {
0798             p = &(*p)->rb_right;
0799             leftmost = false;
0800         }
0801     }
0802     rb_link_node(&node->rb_node, parent, p);
0803     rb_insert_color_cached(&node->rb_node, tree, leftmost);
0804 }
0805 
0806 char *srcline__tree_find(struct rb_root_cached *tree, u64 addr)
0807 {
0808     struct rb_node *n = tree->rb_root.rb_node;
0809 
0810     while (n) {
0811         struct srcline_node *i = rb_entry(n, struct srcline_node,
0812                           rb_node);
0813 
0814         if (addr < i->addr)
0815             n = n->rb_left;
0816         else if (addr > i->addr)
0817             n = n->rb_right;
0818         else
0819             return i->srcline;
0820     }
0821 
0822     return NULL;
0823 }
0824 
0825 void srcline__tree_delete(struct rb_root_cached *tree)
0826 {
0827     struct srcline_node *pos;
0828     struct rb_node *next = rb_first_cached(tree);
0829 
0830     while (next) {
0831         pos = rb_entry(next, struct srcline_node, rb_node);
0832         next = rb_next(&pos->rb_node);
0833         rb_erase_cached(&pos->rb_node, tree);
0834         free_srcline(pos->srcline);
0835         zfree(&pos);
0836     }
0837 }
0838 
0839 struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr,
0840                         struct symbol *sym)
0841 {
0842     const char *dso_name;
0843 
0844     dso_name = dso__name(dso);
0845     if (dso_name == NULL)
0846         return NULL;
0847 
0848     return addr2inlines(dso_name, addr, dso, sym);
0849 }
0850 
0851 void inline_node__delete(struct inline_node *node)
0852 {
0853     struct inline_list *ilist, *tmp;
0854 
0855     list_for_each_entry_safe(ilist, tmp, &node->val, list) {
0856         list_del_init(&ilist->list);
0857         free_srcline(ilist->srcline);
0858         /* only the inlined symbols are owned by the list */
0859         if (ilist->symbol && ilist->symbol->inlined)
0860             symbol__delete(ilist->symbol);
0861         free(ilist);
0862     }
0863 
0864     free(node);
0865 }
0866 
0867 void inlines__tree_insert(struct rb_root_cached *tree,
0868               struct inline_node *inlines)
0869 {
0870     struct rb_node **p = &tree->rb_root.rb_node;
0871     struct rb_node *parent = NULL;
0872     const u64 addr = inlines->addr;
0873     struct inline_node *i;
0874     bool leftmost = true;
0875 
0876     while (*p != NULL) {
0877         parent = *p;
0878         i = rb_entry(parent, struct inline_node, rb_node);
0879         if (addr < i->addr)
0880             p = &(*p)->rb_left;
0881         else {
0882             p = &(*p)->rb_right;
0883             leftmost = false;
0884         }
0885     }
0886     rb_link_node(&inlines->rb_node, parent, p);
0887     rb_insert_color_cached(&inlines->rb_node, tree, leftmost);
0888 }
0889 
0890 struct inline_node *inlines__tree_find(struct rb_root_cached *tree, u64 addr)
0891 {
0892     struct rb_node *n = tree->rb_root.rb_node;
0893 
0894     while (n) {
0895         struct inline_node *i = rb_entry(n, struct inline_node,
0896                          rb_node);
0897 
0898         if (addr < i->addr)
0899             n = n->rb_left;
0900         else if (addr > i->addr)
0901             n = n->rb_right;
0902         else
0903             return i;
0904     }
0905 
0906     return NULL;
0907 }
0908 
0909 void inlines__tree_delete(struct rb_root_cached *tree)
0910 {
0911     struct inline_node *pos;
0912     struct rb_node *next = rb_first_cached(tree);
0913 
0914     while (next) {
0915         pos = rb_entry(next, struct inline_node, rb_node);
0916         next = rb_next(&pos->rb_node);
0917         rb_erase_cached(&pos->rb_node, tree);
0918         inline_node__delete(pos);
0919     }
0920 }