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