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