xref: /openbmc/linux/tools/perf/util/srcline.c (revision 0ada120c883d4f1f6aafd01cf0fbb10d8bbba015)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2fd20e811SArnaldo Carvalho de Melo #include <inttypes.h>
3f048d548SNamhyung Kim #include <stdio.h>
4f048d548SNamhyung Kim #include <stdlib.h>
5f048d548SNamhyung Kim #include <string.h>
6f048d548SNamhyung Kim 
7f048d548SNamhyung Kim #include <linux/kernel.h>
813c230abSArnaldo Carvalho de Melo #include <linux/string.h>
97f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
10f048d548SNamhyung Kim 
1186c98cabSNamhyung Kim #include "util/dso.h"
12f048d548SNamhyung Kim #include "util/debug.h"
13a64489c5SJin Yao #include "util/callchain.h"
14b10c78c5SJin Yao #include "util/symbol_conf.h"
15632a5cabSArnaldo Carvalho de Melo #include "srcline.h"
167285cf33SNamhyung Kim #include "string2.h"
1785c116a6SAndi Kleen #include "symbol.h"
1885c116a6SAndi Kleen 
19a9710ba0SAndi Kleen bool srcline_full_filename;
20a9710ba0SAndi Kleen 
215580338dSJin Yao static const char *dso__name(struct dso *dso)
225580338dSJin Yao {
235580338dSJin Yao 	const char *dso_name;
245580338dSJin Yao 
255580338dSJin Yao 	if (dso->symsrc_filename)
265580338dSJin Yao 		dso_name = dso->symsrc_filename;
275580338dSJin Yao 	else
285580338dSJin Yao 		dso_name = dso->long_name;
295580338dSJin Yao 
305580338dSJin Yao 	if (dso_name[0] == '[')
315580338dSJin Yao 		return NULL;
325580338dSJin Yao 
335580338dSJin Yao 	if (!strncmp(dso_name, "/tmp/perf-", 10))
345580338dSJin Yao 		return NULL;
355580338dSJin Yao 
365580338dSJin Yao 	return dso_name;
375580338dSJin Yao }
385580338dSJin Yao 
392be8832fSMilian Wolff static int inline_list__append(struct symbol *symbol, char *srcline,
402be8832fSMilian Wolff 			       struct inline_node *node)
41a64489c5SJin Yao {
42a64489c5SJin Yao 	struct inline_list *ilist;
43a64489c5SJin Yao 
44a64489c5SJin Yao 	ilist = zalloc(sizeof(*ilist));
45a64489c5SJin Yao 	if (ilist == NULL)
46a64489c5SJin Yao 		return -1;
47a64489c5SJin Yao 
48fea0cf84SMilian Wolff 	ilist->symbol = symbol;
492be8832fSMilian Wolff 	ilist->srcline = srcline;
50a64489c5SJin Yao 
5128071f51SMilian Wolff 	if (callchain_param.order == ORDER_CALLEE)
52a64489c5SJin Yao 		list_add_tail(&ilist->list, &node->val);
5328071f51SMilian Wolff 	else
5428071f51SMilian Wolff 		list_add(&ilist->list, &node->val);
55a64489c5SJin Yao 
56a64489c5SJin Yao 	return 0;
57a64489c5SJin Yao }
58a64489c5SJin Yao 
592be8832fSMilian Wolff /* basename version that takes a const input string */
602be8832fSMilian Wolff static const char *gnu_basename(const char *path)
612be8832fSMilian Wolff {
622be8832fSMilian Wolff 	const char *base = strrchr(path, '/');
632be8832fSMilian Wolff 
642be8832fSMilian Wolff 	return base ? base + 1 : path;
652be8832fSMilian Wolff }
662be8832fSMilian Wolff 
672be8832fSMilian Wolff static char *srcline_from_fileline(const char *file, unsigned int line)
682be8832fSMilian Wolff {
692be8832fSMilian Wolff 	char *srcline;
702be8832fSMilian Wolff 
712be8832fSMilian Wolff 	if (!file)
722be8832fSMilian Wolff 		return NULL;
732be8832fSMilian Wolff 
742be8832fSMilian Wolff 	if (!srcline_full_filename)
752be8832fSMilian Wolff 		file = gnu_basename(file);
762be8832fSMilian Wolff 
772be8832fSMilian Wolff 	if (asprintf(&srcline, "%s:%u", file, line) < 0)
782be8832fSMilian Wolff 		return NULL;
792be8832fSMilian Wolff 
802be8832fSMilian Wolff 	return srcline;
812be8832fSMilian Wolff }
822be8832fSMilian Wolff 
837285cf33SNamhyung Kim static struct symbol *new_inline_sym(struct dso *dso,
847285cf33SNamhyung Kim 				     struct symbol *base_sym,
857285cf33SNamhyung Kim 				     const char *funcname)
867285cf33SNamhyung Kim {
877285cf33SNamhyung Kim 	struct symbol *inline_sym;
887285cf33SNamhyung Kim 	char *demangled = NULL;
897285cf33SNamhyung Kim 
90d4046e8eSMilian Wolff 	if (!funcname)
91d4046e8eSMilian Wolff 		funcname = "??";
92d4046e8eSMilian Wolff 
937285cf33SNamhyung Kim 	if (dso) {
947285cf33SNamhyung Kim 		demangled = dso__demangle_sym(dso, 0, funcname);
957285cf33SNamhyung Kim 		if (demangled)
967285cf33SNamhyung Kim 			funcname = demangled;
977285cf33SNamhyung Kim 	}
987285cf33SNamhyung Kim 
997285cf33SNamhyung Kim 	if (base_sym && strcmp(funcname, base_sym->name) == 0) {
1007285cf33SNamhyung Kim 		/* reuse the real, existing symbol */
1017285cf33SNamhyung Kim 		inline_sym = base_sym;
1027285cf33SNamhyung Kim 		/* ensure that we don't alias an inlined symbol, which could
1037285cf33SNamhyung Kim 		 * lead to double frees in inline_node__delete
1047285cf33SNamhyung Kim 		 */
1057285cf33SNamhyung Kim 		assert(!base_sym->inlined);
1067285cf33SNamhyung Kim 	} else {
1077285cf33SNamhyung Kim 		/* create a fake symbol for the inline frame */
1087285cf33SNamhyung Kim 		inline_sym = symbol__new(base_sym ? base_sym->start : 0,
1097346195eSHe Kuang 					 base_sym ? (base_sym->end - base_sym->start) : 0,
1107285cf33SNamhyung Kim 					 base_sym ? base_sym->binding : 0,
111af30bffaSArnaldo Carvalho de Melo 					 base_sym ? base_sym->type : 0,
1127285cf33SNamhyung Kim 					 funcname);
1137285cf33SNamhyung Kim 		if (inline_sym)
1147285cf33SNamhyung Kim 			inline_sym->inlined = 1;
1157285cf33SNamhyung Kim 	}
1167285cf33SNamhyung Kim 
1177285cf33SNamhyung Kim 	free(demangled);
1187285cf33SNamhyung Kim 
1197285cf33SNamhyung Kim 	return inline_sym;
1207285cf33SNamhyung Kim }
1217285cf33SNamhyung Kim 
1222f48fcd8SRoberto Vitillo #ifdef HAVE_LIBBFD_SUPPORT
1232f48fcd8SRoberto Vitillo 
1242f48fcd8SRoberto Vitillo /*
1252f48fcd8SRoberto Vitillo  * Implement addr2line using libbfd.
1262f48fcd8SRoberto Vitillo  */
1272f48fcd8SRoberto Vitillo #define PACKAGE "perf"
1282f48fcd8SRoberto Vitillo #include <bfd.h>
1292f48fcd8SRoberto Vitillo 
1302f48fcd8SRoberto Vitillo struct a2l_data {
1312f48fcd8SRoberto Vitillo 	const char 	*input;
132ac931f87SWang Nan 	u64	 	addr;
1332f48fcd8SRoberto Vitillo 
1342f48fcd8SRoberto Vitillo 	bool 		found;
1352f48fcd8SRoberto Vitillo 	const char 	*filename;
1362f48fcd8SRoberto Vitillo 	const char 	*funcname;
1372f48fcd8SRoberto Vitillo 	unsigned 	line;
1382f48fcd8SRoberto Vitillo 
1392f48fcd8SRoberto Vitillo 	bfd 		*abfd;
1402f48fcd8SRoberto Vitillo 	asymbol 	**syms;
1412f48fcd8SRoberto Vitillo };
1422f48fcd8SRoberto Vitillo 
1432f48fcd8SRoberto Vitillo static int bfd_error(const char *string)
1442f48fcd8SRoberto Vitillo {
1452f48fcd8SRoberto Vitillo 	const char *errmsg;
1462f48fcd8SRoberto Vitillo 
1472f48fcd8SRoberto Vitillo 	errmsg = bfd_errmsg(bfd_get_error());
1482f48fcd8SRoberto Vitillo 	fflush(stdout);
1492f48fcd8SRoberto Vitillo 
1502f48fcd8SRoberto Vitillo 	if (string)
1512f48fcd8SRoberto Vitillo 		pr_debug("%s: %s\n", string, errmsg);
1522f48fcd8SRoberto Vitillo 	else
1532f48fcd8SRoberto Vitillo 		pr_debug("%s\n", errmsg);
1542f48fcd8SRoberto Vitillo 
1552f48fcd8SRoberto Vitillo 	return -1;
1562f48fcd8SRoberto Vitillo }
1572f48fcd8SRoberto Vitillo 
1582f48fcd8SRoberto Vitillo static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
1592f48fcd8SRoberto Vitillo {
1602f48fcd8SRoberto Vitillo 	long storage;
1612f48fcd8SRoberto Vitillo 	long symcount;
1622f48fcd8SRoberto Vitillo 	asymbol **syms;
1632f48fcd8SRoberto Vitillo 	bfd_boolean dynamic = FALSE;
1642f48fcd8SRoberto Vitillo 
1652f48fcd8SRoberto Vitillo 	if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
1662f48fcd8SRoberto Vitillo 		return bfd_error(bfd_get_filename(abfd));
1672f48fcd8SRoberto Vitillo 
1682f48fcd8SRoberto Vitillo 	storage = bfd_get_symtab_upper_bound(abfd);
1692f48fcd8SRoberto Vitillo 	if (storage == 0L) {
1702f48fcd8SRoberto Vitillo 		storage = bfd_get_dynamic_symtab_upper_bound(abfd);
1712f48fcd8SRoberto Vitillo 		dynamic = TRUE;
1722f48fcd8SRoberto Vitillo 	}
1732f48fcd8SRoberto Vitillo 	if (storage < 0L)
1742f48fcd8SRoberto Vitillo 		return bfd_error(bfd_get_filename(abfd));
1752f48fcd8SRoberto Vitillo 
1762f48fcd8SRoberto Vitillo 	syms = malloc(storage);
1772f48fcd8SRoberto Vitillo 	if (dynamic)
1782f48fcd8SRoberto Vitillo 		symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
1792f48fcd8SRoberto Vitillo 	else
1802f48fcd8SRoberto Vitillo 		symcount = bfd_canonicalize_symtab(abfd, syms);
1812f48fcd8SRoberto Vitillo 
1822f48fcd8SRoberto Vitillo 	if (symcount < 0) {
1832f48fcd8SRoberto Vitillo 		free(syms);
1842f48fcd8SRoberto Vitillo 		return bfd_error(bfd_get_filename(abfd));
1852f48fcd8SRoberto Vitillo 	}
1862f48fcd8SRoberto Vitillo 
1872f48fcd8SRoberto Vitillo 	a2l->syms = syms;
1882f48fcd8SRoberto Vitillo 	return 0;
1892f48fcd8SRoberto Vitillo }
1902f48fcd8SRoberto Vitillo 
1912f48fcd8SRoberto Vitillo static void find_address_in_section(bfd *abfd, asection *section, void *data)
1922f48fcd8SRoberto Vitillo {
1932f48fcd8SRoberto Vitillo 	bfd_vma pc, vma;
1942f48fcd8SRoberto Vitillo 	bfd_size_type size;
1952f48fcd8SRoberto Vitillo 	struct a2l_data *a2l = data;
196*0ada120cSChangbin Du 	flagword flags;
1972f48fcd8SRoberto Vitillo 
1982f48fcd8SRoberto Vitillo 	if (a2l->found)
1992f48fcd8SRoberto Vitillo 		return;
2002f48fcd8SRoberto Vitillo 
201*0ada120cSChangbin Du #ifdef bfd_get_section_flags
202*0ada120cSChangbin Du 	flags = bfd_get_section_flags(abfd, section);
203*0ada120cSChangbin Du #else
204*0ada120cSChangbin Du 	flags = bfd_section_flags(section);
205*0ada120cSChangbin Du #endif
206*0ada120cSChangbin Du 	if ((flags & SEC_ALLOC) == 0)
2072f48fcd8SRoberto Vitillo 		return;
2082f48fcd8SRoberto Vitillo 
2092f48fcd8SRoberto Vitillo 	pc = a2l->addr;
210*0ada120cSChangbin Du #ifdef bfd_get_section_vma
2112f48fcd8SRoberto Vitillo 	vma = bfd_get_section_vma(abfd, section);
212*0ada120cSChangbin Du #else
213*0ada120cSChangbin Du 	vma = bfd_section_vma(section);
214*0ada120cSChangbin Du #endif
215*0ada120cSChangbin Du #ifdef bfd_get_section_size
2162f48fcd8SRoberto Vitillo 	size = bfd_get_section_size(section);
217*0ada120cSChangbin Du #else
218*0ada120cSChangbin Du 	size = bfd_section_size(section);
219*0ada120cSChangbin Du #endif
2202f48fcd8SRoberto Vitillo 
2212f48fcd8SRoberto Vitillo 	if (pc < vma || pc >= vma + size)
2222f48fcd8SRoberto Vitillo 		return;
2232f48fcd8SRoberto Vitillo 
2242f48fcd8SRoberto Vitillo 	a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
2252f48fcd8SRoberto Vitillo 					   &a2l->filename, &a2l->funcname,
2262f48fcd8SRoberto Vitillo 					   &a2l->line);
227d964b1cdSMilian Wolff 
228d964b1cdSMilian Wolff 	if (a2l->filename && !strlen(a2l->filename))
229d964b1cdSMilian Wolff 		a2l->filename = NULL;
2302f48fcd8SRoberto Vitillo }
2312f48fcd8SRoberto Vitillo 
2322f48fcd8SRoberto Vitillo static struct a2l_data *addr2line_init(const char *path)
2332f48fcd8SRoberto Vitillo {
2342f48fcd8SRoberto Vitillo 	bfd *abfd;
2352f48fcd8SRoberto Vitillo 	struct a2l_data *a2l = NULL;
2362f48fcd8SRoberto Vitillo 
2372f48fcd8SRoberto Vitillo 	abfd = bfd_openr(path, NULL);
2382f48fcd8SRoberto Vitillo 	if (abfd == NULL)
2392f48fcd8SRoberto Vitillo 		return NULL;
2402f48fcd8SRoberto Vitillo 
2412f48fcd8SRoberto Vitillo 	if (!bfd_check_format(abfd, bfd_object))
2422f48fcd8SRoberto Vitillo 		goto out;
2432f48fcd8SRoberto Vitillo 
2442f48fcd8SRoberto Vitillo 	a2l = zalloc(sizeof(*a2l));
2452f48fcd8SRoberto Vitillo 	if (a2l == NULL)
2462f48fcd8SRoberto Vitillo 		goto out;
2472f48fcd8SRoberto Vitillo 
2482f48fcd8SRoberto Vitillo 	a2l->abfd = abfd;
2492f48fcd8SRoberto Vitillo 	a2l->input = strdup(path);
2502f48fcd8SRoberto Vitillo 	if (a2l->input == NULL)
2512f48fcd8SRoberto Vitillo 		goto out;
2522f48fcd8SRoberto Vitillo 
2532f48fcd8SRoberto Vitillo 	if (slurp_symtab(abfd, a2l))
2542f48fcd8SRoberto Vitillo 		goto out;
2552f48fcd8SRoberto Vitillo 
2562f48fcd8SRoberto Vitillo 	return a2l;
2572f48fcd8SRoberto Vitillo 
2582f48fcd8SRoberto Vitillo out:
2592f48fcd8SRoberto Vitillo 	if (a2l) {
2607d16c634SNamhyung Kim 		zfree((char **)&a2l->input);
2612f48fcd8SRoberto Vitillo 		free(a2l);
2622f48fcd8SRoberto Vitillo 	}
2632f48fcd8SRoberto Vitillo 	bfd_close(abfd);
2642f48fcd8SRoberto Vitillo 	return NULL;
2652f48fcd8SRoberto Vitillo }
2662f48fcd8SRoberto Vitillo 
2672f48fcd8SRoberto Vitillo static void addr2line_cleanup(struct a2l_data *a2l)
2682f48fcd8SRoberto Vitillo {
2692f48fcd8SRoberto Vitillo 	if (a2l->abfd)
2702f48fcd8SRoberto Vitillo 		bfd_close(a2l->abfd);
2717d16c634SNamhyung Kim 	zfree((char **)&a2l->input);
27274cf249dSArnaldo Carvalho de Melo 	zfree(&a2l->syms);
2732f48fcd8SRoberto Vitillo 	free(a2l);
2742f48fcd8SRoberto Vitillo }
2752f48fcd8SRoberto Vitillo 
2762f84b42bSAndi Kleen #define MAX_INLINE_NEST 1024
2772f84b42bSAndi Kleen 
2784d53b9d5SMilian Wolff static int inline_list__append_dso_a2l(struct dso *dso,
279fea0cf84SMilian Wolff 				       struct inline_node *node,
280fea0cf84SMilian Wolff 				       struct symbol *sym)
2814d53b9d5SMilian Wolff {
2824d53b9d5SMilian Wolff 	struct a2l_data *a2l = dso->a2l;
283fea0cf84SMilian Wolff 	struct symbol *inline_sym = new_inline_sym(dso, sym, a2l->funcname);
2842be8832fSMilian Wolff 	char *srcline = NULL;
2854d53b9d5SMilian Wolff 
2862be8832fSMilian Wolff 	if (a2l->filename)
2872be8832fSMilian Wolff 		srcline = srcline_from_fileline(a2l->filename, a2l->line);
2882be8832fSMilian Wolff 
2892be8832fSMilian Wolff 	return inline_list__append(inline_sym, srcline, node);
2904d53b9d5SMilian Wolff }
2914d53b9d5SMilian Wolff 
292ac931f87SWang Nan static int addr2line(const char *dso_name, u64 addr,
2932f84b42bSAndi Kleen 		     char **file, unsigned int *line, struct dso *dso,
294fea0cf84SMilian Wolff 		     bool unwind_inlines, struct inline_node *node,
295fea0cf84SMilian Wolff 		     struct symbol *sym)
2962f48fcd8SRoberto Vitillo {
2972f48fcd8SRoberto Vitillo 	int ret = 0;
298454ff00fSAdrian Hunter 	struct a2l_data *a2l = dso->a2l;
2992f48fcd8SRoberto Vitillo 
300454ff00fSAdrian Hunter 	if (!a2l) {
301454ff00fSAdrian Hunter 		dso->a2l = addr2line_init(dso_name);
302454ff00fSAdrian Hunter 		a2l = dso->a2l;
303454ff00fSAdrian Hunter 	}
304454ff00fSAdrian Hunter 
3052f48fcd8SRoberto Vitillo 	if (a2l == NULL) {
306b10c78c5SJin Yao 		if (!symbol_conf.disable_add2line_warn)
3072f48fcd8SRoberto Vitillo 			pr_warning("addr2line_init failed for %s\n", dso_name);
3082f48fcd8SRoberto Vitillo 		return 0;
3092f48fcd8SRoberto Vitillo 	}
3102f48fcd8SRoberto Vitillo 
3112f48fcd8SRoberto Vitillo 	a2l->addr = addr;
312454ff00fSAdrian Hunter 	a2l->found = false;
313454ff00fSAdrian Hunter 
3142f48fcd8SRoberto Vitillo 	bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
3152f48fcd8SRoberto Vitillo 
316b21cc978SMilian Wolff 	if (!a2l->found)
317b21cc978SMilian Wolff 		return 0;
318b21cc978SMilian Wolff 
319b21cc978SMilian Wolff 	if (unwind_inlines) {
3202f84b42bSAndi Kleen 		int cnt = 0;
3212f84b42bSAndi Kleen 
322fea0cf84SMilian Wolff 		if (node && inline_list__append_dso_a2l(dso, node, sym))
3234d53b9d5SMilian Wolff 			return 0;
3244d53b9d5SMilian Wolff 
3252f84b42bSAndi Kleen 		while (bfd_find_inliner_info(a2l->abfd, &a2l->filename,
3262f84b42bSAndi Kleen 					     &a2l->funcname, &a2l->line) &&
327a64489c5SJin Yao 		       cnt++ < MAX_INLINE_NEST) {
328a64489c5SJin Yao 
329d964b1cdSMilian Wolff 			if (a2l->filename && !strlen(a2l->filename))
330d964b1cdSMilian Wolff 				a2l->filename = NULL;
331d964b1cdSMilian Wolff 
332a64489c5SJin Yao 			if (node != NULL) {
333fea0cf84SMilian Wolff 				if (inline_list__append_dso_a2l(dso, node, sym))
334a64489c5SJin Yao 					return 0;
335b21cc978SMilian Wolff 				// found at least one inline frame
336b21cc978SMilian Wolff 				ret = 1;
337a64489c5SJin Yao 			}
338a64489c5SJin Yao 		}
3392f84b42bSAndi Kleen 	}
3402f84b42bSAndi Kleen 
341b21cc978SMilian Wolff 	if (file) {
342b21cc978SMilian Wolff 		*file = a2l->filename ? strdup(a2l->filename) : NULL;
343b21cc978SMilian Wolff 		ret = *file ? 1 : 0;
3442f48fcd8SRoberto Vitillo 	}
3452f48fcd8SRoberto Vitillo 
346b21cc978SMilian Wolff 	if (line)
347b21cc978SMilian Wolff 		*line = a2l->line;
348b21cc978SMilian Wolff 
3492f48fcd8SRoberto Vitillo 	return ret;
3502f48fcd8SRoberto Vitillo }
3512f48fcd8SRoberto Vitillo 
352454ff00fSAdrian Hunter void dso__free_a2l(struct dso *dso)
353454ff00fSAdrian Hunter {
354454ff00fSAdrian Hunter 	struct a2l_data *a2l = dso->a2l;
355454ff00fSAdrian Hunter 
356454ff00fSAdrian Hunter 	if (!a2l)
357454ff00fSAdrian Hunter 		return;
358454ff00fSAdrian Hunter 
359454ff00fSAdrian Hunter 	addr2line_cleanup(a2l);
360454ff00fSAdrian Hunter 
361454ff00fSAdrian Hunter 	dso->a2l = NULL;
362454ff00fSAdrian Hunter }
363454ff00fSAdrian Hunter 
364a64489c5SJin Yao static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
365fea0cf84SMilian Wolff 					struct dso *dso, struct symbol *sym)
366a64489c5SJin Yao {
367a64489c5SJin Yao 	struct inline_node *node;
368a64489c5SJin Yao 
369a64489c5SJin Yao 	node = zalloc(sizeof(*node));
370a64489c5SJin Yao 	if (node == NULL) {
371a64489c5SJin Yao 		perror("not enough memory for the inline node");
372a64489c5SJin Yao 		return NULL;
373a64489c5SJin Yao 	}
374a64489c5SJin Yao 
375a64489c5SJin Yao 	INIT_LIST_HEAD(&node->val);
376a64489c5SJin Yao 	node->addr = addr;
377a64489c5SJin Yao 
378b38775cfSMilian Wolff 	addr2line(dso_name, addr, NULL, NULL, dso, true, node, sym);
379a64489c5SJin Yao 	return node;
380a64489c5SJin Yao }
381a64489c5SJin Yao 
3822f48fcd8SRoberto Vitillo #else /* HAVE_LIBBFD_SUPPORT */
3832f48fcd8SRoberto Vitillo 
3845580338dSJin Yao static int filename_split(char *filename, unsigned int *line_nr)
3855580338dSJin Yao {
3865580338dSJin Yao 	char *sep;
3875580338dSJin Yao 
3885580338dSJin Yao 	sep = strchr(filename, '\n');
3895580338dSJin Yao 	if (sep)
3905580338dSJin Yao 		*sep = '\0';
3915580338dSJin Yao 
3925580338dSJin Yao 	if (!strcmp(filename, "??:0"))
3935580338dSJin Yao 		return 0;
3945580338dSJin Yao 
3955580338dSJin Yao 	sep = strchr(filename, ':');
3965580338dSJin Yao 	if (sep) {
3975580338dSJin Yao 		*sep++ = '\0';
3985580338dSJin Yao 		*line_nr = strtoul(sep, NULL, 0);
3995580338dSJin Yao 		return 1;
4005580338dSJin Yao 	}
4015580338dSJin Yao 
4025580338dSJin Yao 	return 0;
4035580338dSJin Yao }
4045580338dSJin Yao 
405ac931f87SWang Nan static int addr2line(const char *dso_name, u64 addr,
406454ff00fSAdrian Hunter 		     char **file, unsigned int *line_nr,
4072f84b42bSAndi Kleen 		     struct dso *dso __maybe_unused,
408a64489c5SJin Yao 		     bool unwind_inlines __maybe_unused,
409fea0cf84SMilian Wolff 		     struct inline_node *node __maybe_unused,
410fea0cf84SMilian Wolff 		     struct symbol *sym __maybe_unused)
411f048d548SNamhyung Kim {
412f048d548SNamhyung Kim 	FILE *fp;
413f048d548SNamhyung Kim 	char cmd[PATH_MAX];
414f048d548SNamhyung Kim 	char *filename = NULL;
415f048d548SNamhyung Kim 	size_t len;
416f048d548SNamhyung Kim 	int ret = 0;
417f048d548SNamhyung Kim 
418f048d548SNamhyung Kim 	scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64,
419f048d548SNamhyung Kim 		  dso_name, addr);
420f048d548SNamhyung Kim 
421f048d548SNamhyung Kim 	fp = popen(cmd, "r");
422f048d548SNamhyung Kim 	if (fp == NULL) {
423f048d548SNamhyung Kim 		pr_warning("popen failed for %s\n", dso_name);
424f048d548SNamhyung Kim 		return 0;
425f048d548SNamhyung Kim 	}
426f048d548SNamhyung Kim 
427f048d548SNamhyung Kim 	if (getline(&filename, &len, fp) < 0 || !len) {
428f048d548SNamhyung Kim 		pr_warning("addr2line has no output for %s\n", dso_name);
429f048d548SNamhyung Kim 		goto out;
430f048d548SNamhyung Kim 	}
431f048d548SNamhyung Kim 
4325580338dSJin Yao 	ret = filename_split(filename, line_nr);
4335580338dSJin Yao 	if (ret != 1) {
434f048d548SNamhyung Kim 		free(filename);
435f048d548SNamhyung Kim 		goto out;
436f048d548SNamhyung Kim 	}
437f048d548SNamhyung Kim 
438f048d548SNamhyung Kim 	*file = filename;
4395580338dSJin Yao 
440f048d548SNamhyung Kim out:
441f048d548SNamhyung Kim 	pclose(fp);
442f048d548SNamhyung Kim 	return ret;
443f048d548SNamhyung Kim }
444454ff00fSAdrian Hunter 
445454ff00fSAdrian Hunter void dso__free_a2l(struct dso *dso __maybe_unused)
446454ff00fSAdrian Hunter {
447454ff00fSAdrian Hunter }
448454ff00fSAdrian Hunter 
449a64489c5SJin Yao static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
450fea0cf84SMilian Wolff 					struct dso *dso __maybe_unused,
451fea0cf84SMilian Wolff 					struct symbol *sym)
452a64489c5SJin Yao {
453a64489c5SJin Yao 	FILE *fp;
454a64489c5SJin Yao 	char cmd[PATH_MAX];
455a64489c5SJin Yao 	struct inline_node *node;
456a64489c5SJin Yao 	char *filename = NULL;
4577285cf33SNamhyung Kim 	char *funcname = NULL;
4587285cf33SNamhyung Kim 	size_t filelen, funclen;
459a64489c5SJin Yao 	unsigned int line_nr = 0;
460a64489c5SJin Yao 
4617285cf33SNamhyung Kim 	scnprintf(cmd, sizeof(cmd), "addr2line -e %s -i -f %016"PRIx64,
462a64489c5SJin Yao 		  dso_name, addr);
463a64489c5SJin Yao 
464a64489c5SJin Yao 	fp = popen(cmd, "r");
465a64489c5SJin Yao 	if (fp == NULL) {
466a64489c5SJin Yao 		pr_err("popen failed for %s\n", dso_name);
467a64489c5SJin Yao 		return NULL;
468a64489c5SJin Yao 	}
469a64489c5SJin Yao 
470a64489c5SJin Yao 	node = zalloc(sizeof(*node));
471a64489c5SJin Yao 	if (node == NULL) {
472a64489c5SJin Yao 		perror("not enough memory for the inline node");
473a64489c5SJin Yao 		goto out;
474a64489c5SJin Yao 	}
475a64489c5SJin Yao 
476a64489c5SJin Yao 	INIT_LIST_HEAD(&node->val);
477a64489c5SJin Yao 	node->addr = addr;
478a64489c5SJin Yao 
4797285cf33SNamhyung Kim 	/* addr2line -f generates two lines for each inlined functions */
4807285cf33SNamhyung Kim 	while (getline(&funcname, &funclen, fp) != -1) {
4812be8832fSMilian Wolff 		char *srcline;
4827285cf33SNamhyung Kim 		struct symbol *inline_sym;
4837285cf33SNamhyung Kim 
48413c230abSArnaldo Carvalho de Melo 		strim(funcname);
4857285cf33SNamhyung Kim 
4867285cf33SNamhyung Kim 		if (getline(&filename, &filelen, fp) == -1)
4877285cf33SNamhyung Kim 			goto out;
488fea0cf84SMilian Wolff 
489b7b75a60SNamhyung Kim 		if (filename_split(filename, &line_nr) != 1)
490a64489c5SJin Yao 			goto out;
491a64489c5SJin Yao 
4922be8832fSMilian Wolff 		srcline = srcline_from_fileline(filename, line_nr);
4937285cf33SNamhyung Kim 		inline_sym = new_inline_sym(dso, sym, funcname);
4947285cf33SNamhyung Kim 
4957285cf33SNamhyung Kim 		if (inline_list__append(inline_sym, srcline, node) != 0) {
4967285cf33SNamhyung Kim 			free(srcline);
4977285cf33SNamhyung Kim 			if (inline_sym && inline_sym->inlined)
4987285cf33SNamhyung Kim 				symbol__delete(inline_sym);
499a64489c5SJin Yao 			goto out;
500a64489c5SJin Yao 		}
5017285cf33SNamhyung Kim 	}
502a64489c5SJin Yao 
503a64489c5SJin Yao out:
504a64489c5SJin Yao 	pclose(fp);
505b7b75a60SNamhyung Kim 	free(filename);
5067285cf33SNamhyung Kim 	free(funcname);
507a64489c5SJin Yao 
508a64489c5SJin Yao 	return node;
509a64489c5SJin Yao }
510a64489c5SJin Yao 
5112f48fcd8SRoberto Vitillo #endif /* HAVE_LIBBFD_SUPPORT */
512f048d548SNamhyung Kim 
513906049c8SAdrian Hunter /*
514906049c8SAdrian Hunter  * Number of addr2line failures (without success) before disabling it for that
515906049c8SAdrian Hunter  * dso.
516906049c8SAdrian Hunter  */
517906049c8SAdrian Hunter #define A2L_FAIL_LIMIT 123
518906049c8SAdrian Hunter 
5192f84b42bSAndi Kleen char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
520935f5a9dSJin Yao 		  bool show_sym, bool show_addr, bool unwind_inlines,
521935f5a9dSJin Yao 		  u64 ip)
522f048d548SNamhyung Kim {
523a949fffbSDavid Ahern 	char *file = NULL;
524a949fffbSDavid Ahern 	unsigned line = 0;
5252cc9d0efSNamhyung Kim 	char *srcline;
526bf4414aeSArnaldo Carvalho de Melo 	const char *dso_name;
527f048d548SNamhyung Kim 
5282cc9d0efSNamhyung Kim 	if (!dso->has_srcline)
52923f0981bSAndi Kleen 		goto out;
5302cc9d0efSNamhyung Kim 
5315580338dSJin Yao 	dso_name = dso__name(dso);
5325580338dSJin Yao 	if (dso_name == NULL)
53358d91a00SNamhyung Kim 		goto out;
53458d91a00SNamhyung Kim 
535fea0cf84SMilian Wolff 	if (!addr2line(dso_name, addr, &file, &line, dso,
536fea0cf84SMilian Wolff 		       unwind_inlines, NULL, sym))
53758d91a00SNamhyung Kim 		goto out;
538f048d548SNamhyung Kim 
5392be8832fSMilian Wolff 	srcline = srcline_from_fileline(file, line);
540906049c8SAdrian Hunter 	free(file);
5412be8832fSMilian Wolff 
5422be8832fSMilian Wolff 	if (!srcline)
543906049c8SAdrian Hunter 		goto out;
544906049c8SAdrian Hunter 
545906049c8SAdrian Hunter 	dso->a2l_fails = 0;
546f048d548SNamhyung Kim 
547f048d548SNamhyung Kim 	return srcline;
5482cc9d0efSNamhyung Kim 
5492cc9d0efSNamhyung Kim out:
550906049c8SAdrian Hunter 	if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
5512cc9d0efSNamhyung Kim 		dso->has_srcline = 0;
552454ff00fSAdrian Hunter 		dso__free_a2l(dso);
553906049c8SAdrian Hunter 	}
5545dfa210eSMilian Wolff 
5555dfa210eSMilian Wolff 	if (!show_addr)
5565dfa210eSMilian Wolff 		return (show_sym && sym) ?
5575dfa210eSMilian Wolff 			    strndup(sym->name, sym->namelen) : NULL;
5585dfa210eSMilian Wolff 
55985c116a6SAndi Kleen 	if (sym) {
560ac931f87SWang Nan 		if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
561935f5a9dSJin Yao 					ip - sym->start) < 0)
56285c116a6SAndi Kleen 			return SRCLINE_UNKNOWN;
563ac931f87SWang Nan 	} else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
5642cc9d0efSNamhyung Kim 		return SRCLINE_UNKNOWN;
56523f0981bSAndi Kleen 	return srcline;
566f048d548SNamhyung Kim }
567f048d548SNamhyung Kim 
568dd2e18e9SAndi Kleen /* Returns filename and fills in line number in line */
569dd2e18e9SAndi Kleen char *get_srcline_split(struct dso *dso, u64 addr, unsigned *line)
570dd2e18e9SAndi Kleen {
571dd2e18e9SAndi Kleen 	char *file = NULL;
572dd2e18e9SAndi Kleen 	const char *dso_name;
573dd2e18e9SAndi Kleen 
574dd2e18e9SAndi Kleen 	if (!dso->has_srcline)
575dd2e18e9SAndi Kleen 		goto out;
576dd2e18e9SAndi Kleen 
577dd2e18e9SAndi Kleen 	dso_name = dso__name(dso);
578dd2e18e9SAndi Kleen 	if (dso_name == NULL)
579dd2e18e9SAndi Kleen 		goto out;
580dd2e18e9SAndi Kleen 
581dd2e18e9SAndi Kleen 	if (!addr2line(dso_name, addr, &file, line, dso, true, NULL, NULL))
582dd2e18e9SAndi Kleen 		goto out;
583dd2e18e9SAndi Kleen 
584dd2e18e9SAndi Kleen 	dso->a2l_fails = 0;
585dd2e18e9SAndi Kleen 	return file;
586dd2e18e9SAndi Kleen 
587dd2e18e9SAndi Kleen out:
588dd2e18e9SAndi Kleen 	if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
589dd2e18e9SAndi Kleen 		dso->has_srcline = 0;
590dd2e18e9SAndi Kleen 		dso__free_a2l(dso);
591dd2e18e9SAndi Kleen 	}
592dd2e18e9SAndi Kleen 
593dd2e18e9SAndi Kleen 	return NULL;
594dd2e18e9SAndi Kleen }
595dd2e18e9SAndi Kleen 
596f048d548SNamhyung Kim void free_srcline(char *srcline)
597f048d548SNamhyung Kim {
598f048d548SNamhyung Kim 	if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
599f048d548SNamhyung Kim 		free(srcline);
600f048d548SNamhyung Kim }
6012f84b42bSAndi Kleen 
6022f84b42bSAndi Kleen char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
603935f5a9dSJin Yao 		  bool show_sym, bool show_addr, u64 ip)
6042f84b42bSAndi Kleen {
605935f5a9dSJin Yao 	return __get_srcline(dso, addr, sym, show_sym, show_addr, false, ip);
6062f84b42bSAndi Kleen }
607a64489c5SJin Yao 
60821ac9d54SMilian Wolff struct srcline_node {
60921ac9d54SMilian Wolff 	u64			addr;
61021ac9d54SMilian Wolff 	char			*srcline;
61121ac9d54SMilian Wolff 	struct rb_node		rb_node;
61221ac9d54SMilian Wolff };
61321ac9d54SMilian Wolff 
61455ecd631SDavidlohr Bueso void srcline__tree_insert(struct rb_root_cached *tree, u64 addr, char *srcline)
61521ac9d54SMilian Wolff {
61655ecd631SDavidlohr Bueso 	struct rb_node **p = &tree->rb_root.rb_node;
61721ac9d54SMilian Wolff 	struct rb_node *parent = NULL;
61821ac9d54SMilian Wolff 	struct srcline_node *i, *node;
61955ecd631SDavidlohr Bueso 	bool leftmost = true;
62021ac9d54SMilian Wolff 
62121ac9d54SMilian Wolff 	node = zalloc(sizeof(struct srcline_node));
62221ac9d54SMilian Wolff 	if (!node) {
62321ac9d54SMilian Wolff 		perror("not enough memory for the srcline node");
62421ac9d54SMilian Wolff 		return;
62521ac9d54SMilian Wolff 	}
62621ac9d54SMilian Wolff 
62721ac9d54SMilian Wolff 	node->addr = addr;
62821ac9d54SMilian Wolff 	node->srcline = srcline;
62921ac9d54SMilian Wolff 
63021ac9d54SMilian Wolff 	while (*p != NULL) {
63121ac9d54SMilian Wolff 		parent = *p;
63221ac9d54SMilian Wolff 		i = rb_entry(parent, struct srcline_node, rb_node);
63321ac9d54SMilian Wolff 		if (addr < i->addr)
63421ac9d54SMilian Wolff 			p = &(*p)->rb_left;
63555ecd631SDavidlohr Bueso 		else {
63621ac9d54SMilian Wolff 			p = &(*p)->rb_right;
63755ecd631SDavidlohr Bueso 			leftmost = false;
63855ecd631SDavidlohr Bueso 		}
63921ac9d54SMilian Wolff 	}
64021ac9d54SMilian Wolff 	rb_link_node(&node->rb_node, parent, p);
64155ecd631SDavidlohr Bueso 	rb_insert_color_cached(&node->rb_node, tree, leftmost);
64221ac9d54SMilian Wolff }
64321ac9d54SMilian Wolff 
64455ecd631SDavidlohr Bueso char *srcline__tree_find(struct rb_root_cached *tree, u64 addr)
64521ac9d54SMilian Wolff {
64655ecd631SDavidlohr Bueso 	struct rb_node *n = tree->rb_root.rb_node;
64721ac9d54SMilian Wolff 
64821ac9d54SMilian Wolff 	while (n) {
64921ac9d54SMilian Wolff 		struct srcline_node *i = rb_entry(n, struct srcline_node,
65021ac9d54SMilian Wolff 						  rb_node);
65121ac9d54SMilian Wolff 
65221ac9d54SMilian Wolff 		if (addr < i->addr)
65321ac9d54SMilian Wolff 			n = n->rb_left;
65421ac9d54SMilian Wolff 		else if (addr > i->addr)
65521ac9d54SMilian Wolff 			n = n->rb_right;
65621ac9d54SMilian Wolff 		else
65721ac9d54SMilian Wolff 			return i->srcline;
65821ac9d54SMilian Wolff 	}
65921ac9d54SMilian Wolff 
66021ac9d54SMilian Wolff 	return NULL;
66121ac9d54SMilian Wolff }
66221ac9d54SMilian Wolff 
66355ecd631SDavidlohr Bueso void srcline__tree_delete(struct rb_root_cached *tree)
66421ac9d54SMilian Wolff {
66521ac9d54SMilian Wolff 	struct srcline_node *pos;
66655ecd631SDavidlohr Bueso 	struct rb_node *next = rb_first_cached(tree);
66721ac9d54SMilian Wolff 
66821ac9d54SMilian Wolff 	while (next) {
66921ac9d54SMilian Wolff 		pos = rb_entry(next, struct srcline_node, rb_node);
67021ac9d54SMilian Wolff 		next = rb_next(&pos->rb_node);
67155ecd631SDavidlohr Bueso 		rb_erase_cached(&pos->rb_node, tree);
67221ac9d54SMilian Wolff 		free_srcline(pos->srcline);
67321ac9d54SMilian Wolff 		zfree(&pos);
67421ac9d54SMilian Wolff 	}
67521ac9d54SMilian Wolff }
67621ac9d54SMilian Wolff 
677fea0cf84SMilian Wolff struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr,
678fea0cf84SMilian Wolff 					    struct symbol *sym)
679a64489c5SJin Yao {
680a64489c5SJin Yao 	const char *dso_name;
681a64489c5SJin Yao 
682a64489c5SJin Yao 	dso_name = dso__name(dso);
683a64489c5SJin Yao 	if (dso_name == NULL)
684a64489c5SJin Yao 		return NULL;
685a64489c5SJin Yao 
686fea0cf84SMilian Wolff 	return addr2inlines(dso_name, addr, dso, sym);
687a64489c5SJin Yao }
688a64489c5SJin Yao 
689a64489c5SJin Yao void inline_node__delete(struct inline_node *node)
690a64489c5SJin Yao {
691a64489c5SJin Yao 	struct inline_list *ilist, *tmp;
692a64489c5SJin Yao 
693a64489c5SJin Yao 	list_for_each_entry_safe(ilist, tmp, &node->val, list) {
694a64489c5SJin Yao 		list_del_init(&ilist->list);
6952be8832fSMilian Wolff 		free_srcline(ilist->srcline);
696fea0cf84SMilian Wolff 		/* only the inlined symbols are owned by the list */
697fea0cf84SMilian Wolff 		if (ilist->symbol && ilist->symbol->inlined)
698fea0cf84SMilian Wolff 			symbol__delete(ilist->symbol);
699a64489c5SJin Yao 		free(ilist);
700a64489c5SJin Yao 	}
701a64489c5SJin Yao 
702a64489c5SJin Yao 	free(node);
703a64489c5SJin Yao }
70411ea2515SMilian Wolff 
70555ecd631SDavidlohr Bueso void inlines__tree_insert(struct rb_root_cached *tree,
70655ecd631SDavidlohr Bueso 			  struct inline_node *inlines)
70711ea2515SMilian Wolff {
70855ecd631SDavidlohr Bueso 	struct rb_node **p = &tree->rb_root.rb_node;
70911ea2515SMilian Wolff 	struct rb_node *parent = NULL;
71011ea2515SMilian Wolff 	const u64 addr = inlines->addr;
71111ea2515SMilian Wolff 	struct inline_node *i;
71255ecd631SDavidlohr Bueso 	bool leftmost = true;
71311ea2515SMilian Wolff 
71411ea2515SMilian Wolff 	while (*p != NULL) {
71511ea2515SMilian Wolff 		parent = *p;
71611ea2515SMilian Wolff 		i = rb_entry(parent, struct inline_node, rb_node);
71711ea2515SMilian Wolff 		if (addr < i->addr)
71811ea2515SMilian Wolff 			p = &(*p)->rb_left;
71955ecd631SDavidlohr Bueso 		else {
72011ea2515SMilian Wolff 			p = &(*p)->rb_right;
72155ecd631SDavidlohr Bueso 			leftmost = false;
72255ecd631SDavidlohr Bueso 		}
72311ea2515SMilian Wolff 	}
72411ea2515SMilian Wolff 	rb_link_node(&inlines->rb_node, parent, p);
72555ecd631SDavidlohr Bueso 	rb_insert_color_cached(&inlines->rb_node, tree, leftmost);
72611ea2515SMilian Wolff }
72711ea2515SMilian Wolff 
72855ecd631SDavidlohr Bueso struct inline_node *inlines__tree_find(struct rb_root_cached *tree, u64 addr)
72911ea2515SMilian Wolff {
73055ecd631SDavidlohr Bueso 	struct rb_node *n = tree->rb_root.rb_node;
73111ea2515SMilian Wolff 
73211ea2515SMilian Wolff 	while (n) {
73311ea2515SMilian Wolff 		struct inline_node *i = rb_entry(n, struct inline_node,
73411ea2515SMilian Wolff 						 rb_node);
73511ea2515SMilian Wolff 
73611ea2515SMilian Wolff 		if (addr < i->addr)
73711ea2515SMilian Wolff 			n = n->rb_left;
73811ea2515SMilian Wolff 		else if (addr > i->addr)
73911ea2515SMilian Wolff 			n = n->rb_right;
74011ea2515SMilian Wolff 		else
74111ea2515SMilian Wolff 			return i;
74211ea2515SMilian Wolff 	}
74311ea2515SMilian Wolff 
74411ea2515SMilian Wolff 	return NULL;
74511ea2515SMilian Wolff }
74611ea2515SMilian Wolff 
74755ecd631SDavidlohr Bueso void inlines__tree_delete(struct rb_root_cached *tree)
74811ea2515SMilian Wolff {
74911ea2515SMilian Wolff 	struct inline_node *pos;
75055ecd631SDavidlohr Bueso 	struct rb_node *next = rb_first_cached(tree);
75111ea2515SMilian Wolff 
75211ea2515SMilian Wolff 	while (next) {
75311ea2515SMilian Wolff 		pos = rb_entry(next, struct inline_node, rb_node);
75411ea2515SMilian Wolff 		next = rb_next(&pos->rb_node);
75555ecd631SDavidlohr Bueso 		rb_erase_cached(&pos->rb_node, tree);
75611ea2515SMilian Wolff 		inline_node__delete(pos);
75711ea2515SMilian Wolff 	}
75811ea2515SMilian Wolff }
759