Linux Audio

Check our new training course

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