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