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