xref: /openbmc/linux/tools/perf/util/srcline.c (revision 701677b9)
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. */
25 int addr2line_timeout_ms = 1 * 1000;
26 bool srcline_full_filename;
27 
28 char *srcline__unknown = (char *)"??:0";
29 
30 static 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 
48 static 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 */
69 static const char *gnu_basename(const char *path)
70 {
71 	const char *base = strrchr(path, '/');
72 
73 	return base ? base + 1 : path;
74 }
75 
76 static 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 
92 static 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 
141 struct 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 
154 static 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 
169 static 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 
202 static 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 
243 static 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 
269 out:
270 	if (a2l) {
271 		zfree((char **)&a2l->input);
272 		free(a2l);
273 	}
274 	bfd_close(abfd);
275 	return NULL;
276 }
277 
278 static 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 
287 static 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 
301 static 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 
361 void 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 
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 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 	}
403 
404 	free(a2l);
405 }
406 
407 static struct child_process *addr2line_subprocess_init(const char *addr2line_path,
408 							const char *binary_path)
409 {
410 	const char *argv[] = {
411 		addr2line_path ?: "addr2line",
412 		"-e", binary_path,
413 		"-a", "-i", "-f", NULL
414 	};
415 	struct child_process *a2l = zalloc(sizeof(*a2l));
416 	int start_command_status = 0;
417 
418 	if (a2l == NULL) {
419 		pr_err("Failed to allocate memory for addr2line");
420 		return NULL;
421 	}
422 
423 	a2l->pid = -1;
424 	a2l->in = -1;
425 	a2l->out = -1;
426 	a2l->no_stderr = 1;
427 
428 	a2l->argv = argv;
429 	start_command_status = start_command(a2l);
430 	a2l->argv = NULL; /* it's not used after start_command; avoid dangling pointers */
431 
432 	if (start_command_status != 0) {
433 		pr_warning("could not start addr2line (%s) for %s: start_command return code %d\n",
434 			addr2line_path, binary_path, start_command_status);
435 		addr2line_subprocess_cleanup(a2l);
436 		return NULL;
437 	}
438 
439 	return a2l;
440 }
441 
442 enum a2l_style {
443 	BROKEN,
444 	GNU_BINUTILS,
445 	LLVM,
446 };
447 
448 static enum a2l_style addr2line_configure(struct child_process *a2l, const char *dso_name)
449 {
450 	static bool cached;
451 	static enum a2l_style style;
452 
453 	if (!cached) {
454 		char buf[128];
455 		struct io io;
456 		int ch;
457 		int lines;
458 
459 		if (write(a2l->in, ",\n", 2) != 2)
460 			return BROKEN;
461 
462 		io__init(&io, a2l->out, buf, sizeof(buf));
463 		ch = io__get_char(&io);
464 		if (ch == ',') {
465 			style = LLVM;
466 			cached = true;
467 			lines = 1;
468 		} else if (ch == '0') {
469 			style = GNU_BINUTILS;
470 			cached = true;
471 			lines = 3;
472 		} else {
473 			if (!symbol_conf.disable_add2line_warn) {
474 				char *output = NULL;
475 				size_t output_len;
476 
477 				io__getline(&io, &output, &output_len);
478 				pr_warning("%s %s: addr2line configuration failed\n",
479 					   __func__, dso_name);
480 				pr_warning("\t%c%s", ch, output);
481 			}
482 			return BROKEN;
483 		}
484 		while (lines) {
485 			ch = io__get_char(&io);
486 			if (ch <= 0)
487 				break;
488 			if (ch == '\n')
489 				lines--;
490 		}
491 		/* Ignore SIGPIPE in the event addr2line exits. */
492 		signal(SIGPIPE, SIG_IGN);
493 	}
494 	return style;
495 }
496 
497 static int read_addr2line_record(struct io *io,
498 				 enum a2l_style style,
499 				 char **function,
500 				 char **filename,
501 				 unsigned int *line_nr)
502 {
503 	/*
504 	 * Returns:
505 	 * -1 ==> error
506 	 * 0 ==> sentinel (or other ill-formed) record read
507 	 * 1 ==> a genuine record read
508 	 */
509 	char *line = NULL;
510 	size_t line_len = 0;
511 	unsigned int dummy_line_nr = 0;
512 	int ret = -1;
513 
514 	if (function != NULL)
515 		zfree(function);
516 
517 	if (filename != NULL)
518 		zfree(filename);
519 
520 	if (line_nr != NULL)
521 		*line_nr = 0;
522 
523 	/*
524 	 * Read the first line. Without an error this will be either an address
525 	 * like 0x1234 or for llvm-addr2line the sentinal ',' character.
526 	 */
527 	if (io__getline(io, &line, &line_len) < 0 || !line_len)
528 		goto error;
529 
530 	if (style == LLVM) {
531 		if (line_len == 2 && line[0] == ',') {
532 			zfree(&line);
533 			return 0;
534 		}
535 	} else {
536 		int zero_count = 0, non_zero_count = 0;
537 
538 		/* The address should always start 0x. */
539 		if (line_len < 2 || line[0] != '0' || line[1] != 'x')
540 			goto error;
541 
542 		for (size_t i = 2; i < line_len; i++) {
543 			if (line[i] == '0')
544 				zero_count++;
545 			else if (line[i] != '\n')
546 				non_zero_count++;
547 		}
548 		if (!non_zero_count) {
549 			int ch;
550 
551 			if (!zero_count) {
552 				/* Line was erroneous just '0x'. */
553 				goto error;
554 			}
555 			/*
556 			 * Line was 0x0..0, the sentinel for binutils. Remove
557 			 * the function and filename lines.
558 			 */
559 			zfree(&line);
560 			do {
561 				ch = io__get_char(io);
562 			} while (ch > 0 && ch != '\n');
563 			do {
564 				ch = io__get_char(io);
565 			} while (ch > 0 && ch != '\n');
566 			return 0;
567 		}
568 	}
569 
570 	/* Read the second function name line. */
571 	if (io__getline(io, &line, &line_len) < 0 || !line_len)
572 		goto error;
573 
574 	if (function != NULL)
575 		*function = strdup(strim(line));
576 
577 	zfree(&line);
578 	line_len = 0;
579 
580 	/* Read the third filename and line number line. */
581 	if (io__getline(io, &line, &line_len) < 0 || !line_len)
582 		goto error;
583 
584 	if (filename_split(line, line_nr == NULL ? &dummy_line_nr : line_nr) == 0 &&
585 	    style == GNU_BINUTILS) {
586 		ret = 0;
587 		goto error;
588 	}
589 
590 	if (filename != NULL)
591 		*filename = strdup(line);
592 
593 	zfree(&line);
594 	line_len = 0;
595 
596 	return 1;
597 
598 error:
599 	free(line);
600 	if (function != NULL)
601 		zfree(function);
602 	if (filename != NULL)
603 		zfree(filename);
604 	return ret;
605 }
606 
607 static int inline_list__append_record(struct dso *dso,
608 				      struct inline_node *node,
609 				      struct symbol *sym,
610 				      const char *function,
611 				      const char *filename,
612 				      unsigned int line_nr)
613 {
614 	struct symbol *inline_sym = new_inline_sym(dso, sym, function);
615 
616 	return inline_list__append(inline_sym, srcline_from_fileline(filename, line_nr), node);
617 }
618 
619 static int addr2line(const char *dso_name, u64 addr,
620 		     char **file, unsigned int *line_nr,
621 		     struct dso *dso,
622 		     bool unwind_inlines,
623 		     struct inline_node *node,
624 		     struct symbol *sym __maybe_unused)
625 {
626 	struct child_process *a2l = dso->a2l;
627 	char *record_function = NULL;
628 	char *record_filename = NULL;
629 	unsigned int record_line_nr = 0;
630 	int record_status = -1;
631 	int ret = 0;
632 	size_t inline_count = 0;
633 	int len;
634 	char buf[128];
635 	ssize_t written;
636 	struct io io = { .eof = false };
637 	enum a2l_style a2l_style;
638 
639 	if (!a2l) {
640 		if (!filename__has_section(dso_name, ".debug_line"))
641 			goto out;
642 
643 		dso->a2l = addr2line_subprocess_init(symbol_conf.addr2line_path,
644 						     dso_name);
645 		a2l = dso->a2l;
646 	}
647 
648 	if (a2l == NULL) {
649 		if (!symbol_conf.disable_add2line_warn)
650 			pr_warning("%s %s: addr2line_subprocess_init failed\n", __func__, dso_name);
651 		goto out;
652 	}
653 	a2l_style = addr2line_configure(a2l, dso_name);
654 	if (a2l_style == BROKEN)
655 		goto out;
656 
657 	/*
658 	 * Send our request and then *deliberately* send something that can't be interpreted as
659 	 * a valid address to ask addr2line about (namely, ","). This causes addr2line to first
660 	 * write out the answer to our request, in an unbounded/unknown number of records, and
661 	 * then to write out the lines "??" and "??:0", for GNU binutils, or "," for
662 	 * llvm-addr2line, so that we can detect when it has finished giving us anything
663 	 * useful. With GNU binutils, we have to be careful about the first record, though,
664 	 * because it may be genuinely unknown, in which case we'll get two sets of "??"/"??:0"
665 	 * lines.
666 	 */
667 	len = snprintf(buf, sizeof(buf), "%016"PRIx64"\n,\n", addr);
668 	written = len > 0 ? write(a2l->in, buf, len) : -1;
669 	if (written != len) {
670 		if (!symbol_conf.disable_add2line_warn)
671 			pr_warning("%s %s: could not send request\n", __func__, dso_name);
672 		goto out;
673 	}
674 	io__init(&io, a2l->out, buf, sizeof(buf));
675 	io.timeout_ms = addr2line_timeout_ms;
676 	switch (read_addr2line_record(&io, a2l_style,
677 				      &record_function, &record_filename, &record_line_nr)) {
678 	case -1:
679 		if (!symbol_conf.disable_add2line_warn)
680 			pr_warning("%s %s: could not read first record\n", __func__, dso_name);
681 		goto out;
682 	case 0:
683 		/*
684 		 * The first record was invalid, so return failure, but first
685 		 * read another record, since we sent a sentinel ',' for the
686 		 * sake of detected the last inlined function.
687 		 */
688 		switch (read_addr2line_record(&io, a2l_style, NULL, NULL, NULL)) {
689 		case -1:
690 			if (!symbol_conf.disable_add2line_warn)
691 				pr_warning("%s %s: could not read delimiter record\n",
692 					   __func__, dso_name);
693 			break;
694 		case 0:
695 			/* As expected. */
696 			break;
697 		default:
698 			if (!symbol_conf.disable_add2line_warn)
699 				pr_warning("%s %s: unexpected record instead of sentinel",
700 					   __func__, dso_name);
701 			break;
702 		}
703 		goto out;
704 	default:
705 		break;
706 	}
707 
708 	if (file) {
709 		*file = strdup(record_filename);
710 		ret = 1;
711 	}
712 	if (line_nr)
713 		*line_nr = record_line_nr;
714 
715 	if (unwind_inlines) {
716 		if (node && inline_list__append_record(dso, node, sym,
717 						       record_function,
718 						       record_filename,
719 						       record_line_nr)) {
720 			ret = 0;
721 			goto out;
722 		}
723 	}
724 
725 	/* We have to read the records even if we don't care about the inline info. */
726 	while ((record_status = read_addr2line_record(&io,
727 						      a2l_style,
728 						      &record_function,
729 						      &record_filename,
730 						      &record_line_nr)) == 1) {
731 		if (unwind_inlines && node && inline_count++ < MAX_INLINE_NEST) {
732 			if (inline_list__append_record(dso, node, sym,
733 						       record_function,
734 						       record_filename,
735 						       record_line_nr)) {
736 				ret = 0;
737 				goto out;
738 			}
739 			ret = 1; /* found at least one inline frame */
740 		}
741 	}
742 
743 out:
744 	free(record_function);
745 	free(record_filename);
746 	if (io.eof) {
747 		dso->a2l = NULL;
748 		addr2line_subprocess_cleanup(a2l);
749 	}
750 	return ret;
751 }
752 
753 void dso__free_a2l(struct dso *dso)
754 {
755 	struct child_process *a2l = dso->a2l;
756 
757 	if (!a2l)
758 		return;
759 
760 	addr2line_subprocess_cleanup(a2l);
761 
762 	dso->a2l = NULL;
763 }
764 
765 #endif /* HAVE_LIBBFD_SUPPORT */
766 
767 static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
768 					struct dso *dso, struct symbol *sym)
769 {
770 	struct inline_node *node;
771 
772 	node = zalloc(sizeof(*node));
773 	if (node == NULL) {
774 		perror("not enough memory for the inline node");
775 		return NULL;
776 	}
777 
778 	INIT_LIST_HEAD(&node->val);
779 	node->addr = addr;
780 
781 	addr2line(dso_name, addr, NULL, NULL, dso, true, node, sym);
782 	return node;
783 }
784 
785 /*
786  * Number of addr2line failures (without success) before disabling it for that
787  * dso.
788  */
789 #define A2L_FAIL_LIMIT 123
790 
791 char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
792 		  bool show_sym, bool show_addr, bool unwind_inlines,
793 		  u64 ip)
794 {
795 	char *file = NULL;
796 	unsigned line = 0;
797 	char *srcline;
798 	const char *dso_name;
799 
800 	if (!dso->has_srcline)
801 		goto out;
802 
803 	dso_name = dso__name(dso);
804 	if (dso_name == NULL)
805 		goto out;
806 
807 	if (!addr2line(dso_name, addr, &file, &line, dso,
808 		       unwind_inlines, NULL, sym))
809 		goto out;
810 
811 	srcline = srcline_from_fileline(file, line);
812 	free(file);
813 
814 	if (!srcline)
815 		goto out;
816 
817 	dso->a2l_fails = 0;
818 
819 	return srcline;
820 
821 out:
822 	if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
823 		dso->has_srcline = 0;
824 		dso__free_a2l(dso);
825 	}
826 
827 	if (!show_addr)
828 		return (show_sym && sym) ?
829 			    strndup(sym->name, sym->namelen) : SRCLINE_UNKNOWN;
830 
831 	if (sym) {
832 		if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
833 					ip - sym->start) < 0)
834 			return SRCLINE_UNKNOWN;
835 	} else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
836 		return SRCLINE_UNKNOWN;
837 	return srcline;
838 }
839 
840 /* Returns filename and fills in line number in line */
841 char *get_srcline_split(struct dso *dso, u64 addr, unsigned *line)
842 {
843 	char *file = NULL;
844 	const char *dso_name;
845 
846 	if (!dso->has_srcline)
847 		goto out;
848 
849 	dso_name = dso__name(dso);
850 	if (dso_name == NULL)
851 		goto out;
852 
853 	if (!addr2line(dso_name, addr, &file, line, dso, true, NULL, NULL))
854 		goto out;
855 
856 	dso->a2l_fails = 0;
857 	return file;
858 
859 out:
860 	if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
861 		dso->has_srcline = 0;
862 		dso__free_a2l(dso);
863 	}
864 
865 	return NULL;
866 }
867 
868 void zfree_srcline(char **srcline)
869 {
870 	if (*srcline == NULL)
871 		return;
872 
873 	if (*srcline != SRCLINE_UNKNOWN)
874 		free(*srcline);
875 
876 	*srcline = NULL;
877 }
878 
879 char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
880 		  bool show_sym, bool show_addr, u64 ip)
881 {
882 	return __get_srcline(dso, addr, sym, show_sym, show_addr, false, ip);
883 }
884 
885 struct srcline_node {
886 	u64			addr;
887 	char			*srcline;
888 	struct rb_node		rb_node;
889 };
890 
891 void srcline__tree_insert(struct rb_root_cached *tree, u64 addr, char *srcline)
892 {
893 	struct rb_node **p = &tree->rb_root.rb_node;
894 	struct rb_node *parent = NULL;
895 	struct srcline_node *i, *node;
896 	bool leftmost = true;
897 
898 	node = zalloc(sizeof(struct srcline_node));
899 	if (!node) {
900 		perror("not enough memory for the srcline node");
901 		return;
902 	}
903 
904 	node->addr = addr;
905 	node->srcline = srcline;
906 
907 	while (*p != NULL) {
908 		parent = *p;
909 		i = rb_entry(parent, struct srcline_node, rb_node);
910 		if (addr < i->addr)
911 			p = &(*p)->rb_left;
912 		else {
913 			p = &(*p)->rb_right;
914 			leftmost = false;
915 		}
916 	}
917 	rb_link_node(&node->rb_node, parent, p);
918 	rb_insert_color_cached(&node->rb_node, tree, leftmost);
919 }
920 
921 char *srcline__tree_find(struct rb_root_cached *tree, u64 addr)
922 {
923 	struct rb_node *n = tree->rb_root.rb_node;
924 
925 	while (n) {
926 		struct srcline_node *i = rb_entry(n, struct srcline_node,
927 						  rb_node);
928 
929 		if (addr < i->addr)
930 			n = n->rb_left;
931 		else if (addr > i->addr)
932 			n = n->rb_right;
933 		else
934 			return i->srcline;
935 	}
936 
937 	return NULL;
938 }
939 
940 void srcline__tree_delete(struct rb_root_cached *tree)
941 {
942 	struct srcline_node *pos;
943 	struct rb_node *next = rb_first_cached(tree);
944 
945 	while (next) {
946 		pos = rb_entry(next, struct srcline_node, rb_node);
947 		next = rb_next(&pos->rb_node);
948 		rb_erase_cached(&pos->rb_node, tree);
949 		zfree_srcline(&pos->srcline);
950 		zfree(&pos);
951 	}
952 }
953 
954 struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr,
955 					    struct symbol *sym)
956 {
957 	const char *dso_name;
958 
959 	dso_name = dso__name(dso);
960 	if (dso_name == NULL)
961 		return NULL;
962 
963 	return addr2inlines(dso_name, addr, dso, sym);
964 }
965 
966 void inline_node__delete(struct inline_node *node)
967 {
968 	struct inline_list *ilist, *tmp;
969 
970 	list_for_each_entry_safe(ilist, tmp, &node->val, list) {
971 		list_del_init(&ilist->list);
972 		zfree_srcline(&ilist->srcline);
973 		/* only the inlined symbols are owned by the list */
974 		if (ilist->symbol && ilist->symbol->inlined)
975 			symbol__delete(ilist->symbol);
976 		free(ilist);
977 	}
978 
979 	free(node);
980 }
981 
982 void inlines__tree_insert(struct rb_root_cached *tree,
983 			  struct inline_node *inlines)
984 {
985 	struct rb_node **p = &tree->rb_root.rb_node;
986 	struct rb_node *parent = NULL;
987 	const u64 addr = inlines->addr;
988 	struct inline_node *i;
989 	bool leftmost = true;
990 
991 	while (*p != NULL) {
992 		parent = *p;
993 		i = rb_entry(parent, struct inline_node, rb_node);
994 		if (addr < i->addr)
995 			p = &(*p)->rb_left;
996 		else {
997 			p = &(*p)->rb_right;
998 			leftmost = false;
999 		}
1000 	}
1001 	rb_link_node(&inlines->rb_node, parent, p);
1002 	rb_insert_color_cached(&inlines->rb_node, tree, leftmost);
1003 }
1004 
1005 struct inline_node *inlines__tree_find(struct rb_root_cached *tree, u64 addr)
1006 {
1007 	struct rb_node *n = tree->rb_root.rb_node;
1008 
1009 	while (n) {
1010 		struct inline_node *i = rb_entry(n, struct inline_node,
1011 						 rb_node);
1012 
1013 		if (addr < i->addr)
1014 			n = n->rb_left;
1015 		else if (addr > i->addr)
1016 			n = n->rb_right;
1017 		else
1018 			return i;
1019 	}
1020 
1021 	return NULL;
1022 }
1023 
1024 void inlines__tree_delete(struct rb_root_cached *tree)
1025 {
1026 	struct inline_node *pos;
1027 	struct rb_node *next = rb_first_cached(tree);
1028 
1029 	while (next) {
1030 		pos = rb_entry(next, struct inline_node, rb_node);
1031 		next = rb_next(&pos->rb_node);
1032 		rb_erase_cached(&pos->rb_node, tree);
1033 		inline_node__delete(pos);
1034 	}
1035 }
1036