xref: /openbmc/linux/tools/perf/util/srcline.c (revision 2c4b928074839425b64155cb3f6d4e0e5e67e835)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2fd20e811SArnaldo Carvalho de Melo #include <inttypes.h>
3be8ecc57STony Garnock-Jones #include <signal.h>
4f048d548SNamhyung Kim #include <stdio.h>
5f048d548SNamhyung Kim #include <stdlib.h>
6f048d548SNamhyung Kim #include <string.h>
7be8ecc57STony Garnock-Jones #include <sys/types.h>
8f048d548SNamhyung Kim 
9f048d548SNamhyung Kim #include <linux/kernel.h>
1013c230abSArnaldo Carvalho de Melo #include <linux/string.h>
117f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
12f048d548SNamhyung Kim 
13b3801e79SIan Rogers #include <api/io.h>
14b3801e79SIan Rogers 
1586c98cabSNamhyung Kim #include "util/dso.h"
16f048d548SNamhyung Kim #include "util/debug.h"
17a64489c5SJin Yao #include "util/callchain.h"
18b10c78c5SJin Yao #include "util/symbol_conf.h"
19632a5cabSArnaldo Carvalho de Melo #include "srcline.h"
207285cf33SNamhyung Kim #include "string2.h"
2185c116a6SAndi Kleen #include "symbol.h"
22be8ecc57STony Garnock-Jones #include "subcmd/run-command.h"
2385c116a6SAndi Kleen 
24a9710ba0SAndi Kleen bool srcline_full_filename;
25a9710ba0SAndi Kleen 
265580338dSJin Yao static const char *dso__name(struct dso *dso)
275580338dSJin Yao {
285580338dSJin Yao 	const char *dso_name;
295580338dSJin Yao 
305580338dSJin Yao 	if (dso->symsrc_filename)
315580338dSJin Yao 		dso_name = dso->symsrc_filename;
325580338dSJin Yao 	else
335580338dSJin Yao 		dso_name = dso->long_name;
345580338dSJin Yao 
355580338dSJin Yao 	if (dso_name[0] == '[')
365580338dSJin Yao 		return NULL;
375580338dSJin Yao 
385580338dSJin Yao 	if (!strncmp(dso_name, "/tmp/perf-", 10))
395580338dSJin Yao 		return NULL;
405580338dSJin Yao 
415580338dSJin Yao 	return dso_name;
425580338dSJin Yao }
435580338dSJin Yao 
442be8832fSMilian Wolff static int inline_list__append(struct symbol *symbol, char *srcline,
452be8832fSMilian Wolff 			       struct inline_node *node)
46a64489c5SJin Yao {
47a64489c5SJin Yao 	struct inline_list *ilist;
48a64489c5SJin Yao 
49a64489c5SJin Yao 	ilist = zalloc(sizeof(*ilist));
50a64489c5SJin Yao 	if (ilist == NULL)
51a64489c5SJin Yao 		return -1;
52a64489c5SJin Yao 
53fea0cf84SMilian Wolff 	ilist->symbol = symbol;
542be8832fSMilian Wolff 	ilist->srcline = srcline;
55a64489c5SJin Yao 
5628071f51SMilian Wolff 	if (callchain_param.order == ORDER_CALLEE)
57a64489c5SJin Yao 		list_add_tail(&ilist->list, &node->val);
5828071f51SMilian Wolff 	else
5928071f51SMilian Wolff 		list_add(&ilist->list, &node->val);
60a64489c5SJin Yao 
61a64489c5SJin Yao 	return 0;
62a64489c5SJin Yao }
63a64489c5SJin Yao 
642be8832fSMilian Wolff /* basename version that takes a const input string */
652be8832fSMilian Wolff static const char *gnu_basename(const char *path)
662be8832fSMilian Wolff {
672be8832fSMilian Wolff 	const char *base = strrchr(path, '/');
682be8832fSMilian Wolff 
692be8832fSMilian Wolff 	return base ? base + 1 : path;
702be8832fSMilian Wolff }
712be8832fSMilian Wolff 
722be8832fSMilian Wolff static char *srcline_from_fileline(const char *file, unsigned int line)
732be8832fSMilian Wolff {
742be8832fSMilian Wolff 	char *srcline;
752be8832fSMilian Wolff 
762be8832fSMilian Wolff 	if (!file)
772be8832fSMilian Wolff 		return NULL;
782be8832fSMilian Wolff 
792be8832fSMilian Wolff 	if (!srcline_full_filename)
802be8832fSMilian Wolff 		file = gnu_basename(file);
812be8832fSMilian Wolff 
822be8832fSMilian Wolff 	if (asprintf(&srcline, "%s:%u", file, line) < 0)
832be8832fSMilian Wolff 		return NULL;
842be8832fSMilian Wolff 
852be8832fSMilian Wolff 	return srcline;
862be8832fSMilian Wolff }
872be8832fSMilian Wolff 
887285cf33SNamhyung Kim static struct symbol *new_inline_sym(struct dso *dso,
897285cf33SNamhyung Kim 				     struct symbol *base_sym,
907285cf33SNamhyung Kim 				     const char *funcname)
917285cf33SNamhyung Kim {
927285cf33SNamhyung Kim 	struct symbol *inline_sym;
937285cf33SNamhyung Kim 	char *demangled = NULL;
947285cf33SNamhyung Kim 
95d4046e8eSMilian Wolff 	if (!funcname)
96d4046e8eSMilian Wolff 		funcname = "??";
97d4046e8eSMilian Wolff 
987285cf33SNamhyung Kim 	if (dso) {
997285cf33SNamhyung Kim 		demangled = dso__demangle_sym(dso, 0, funcname);
1007285cf33SNamhyung Kim 		if (demangled)
1017285cf33SNamhyung Kim 			funcname = demangled;
1027285cf33SNamhyung Kim 	}
1037285cf33SNamhyung Kim 
1047285cf33SNamhyung Kim 	if (base_sym && strcmp(funcname, base_sym->name) == 0) {
1057285cf33SNamhyung Kim 		/* reuse the real, existing symbol */
1067285cf33SNamhyung Kim 		inline_sym = base_sym;
1077285cf33SNamhyung Kim 		/* ensure that we don't alias an inlined symbol, which could
1087285cf33SNamhyung Kim 		 * lead to double frees in inline_node__delete
1097285cf33SNamhyung Kim 		 */
1107285cf33SNamhyung Kim 		assert(!base_sym->inlined);
1117285cf33SNamhyung Kim 	} else {
1127285cf33SNamhyung Kim 		/* create a fake symbol for the inline frame */
1137285cf33SNamhyung Kim 		inline_sym = symbol__new(base_sym ? base_sym->start : 0,
1147346195eSHe Kuang 					 base_sym ? (base_sym->end - base_sym->start) : 0,
1157285cf33SNamhyung Kim 					 base_sym ? base_sym->binding : 0,
116af30bffaSArnaldo Carvalho de Melo 					 base_sym ? base_sym->type : 0,
1177285cf33SNamhyung Kim 					 funcname);
1187285cf33SNamhyung Kim 		if (inline_sym)
1197285cf33SNamhyung Kim 			inline_sym->inlined = 1;
1207285cf33SNamhyung Kim 	}
1217285cf33SNamhyung Kim 
1227285cf33SNamhyung Kim 	free(demangled);
1237285cf33SNamhyung Kim 
1247285cf33SNamhyung Kim 	return inline_sym;
1257285cf33SNamhyung Kim }
1267285cf33SNamhyung Kim 
127be8ecc57STony Garnock-Jones #define MAX_INLINE_NEST 1024
128be8ecc57STony Garnock-Jones 
1292f48fcd8SRoberto Vitillo #ifdef HAVE_LIBBFD_SUPPORT
1302f48fcd8SRoberto Vitillo 
1312f48fcd8SRoberto Vitillo /*
1322f48fcd8SRoberto Vitillo  * Implement addr2line using libbfd.
1332f48fcd8SRoberto Vitillo  */
1342f48fcd8SRoberto Vitillo #define PACKAGE "perf"
1352f48fcd8SRoberto Vitillo #include <bfd.h>
1362f48fcd8SRoberto Vitillo 
1372f48fcd8SRoberto Vitillo struct a2l_data {
1382f48fcd8SRoberto Vitillo 	const char 	*input;
139ac931f87SWang Nan 	u64	 	addr;
1402f48fcd8SRoberto Vitillo 
1412f48fcd8SRoberto Vitillo 	bool 		found;
1422f48fcd8SRoberto Vitillo 	const char 	*filename;
1432f48fcd8SRoberto Vitillo 	const char 	*funcname;
1442f48fcd8SRoberto Vitillo 	unsigned 	line;
1452f48fcd8SRoberto Vitillo 
1462f48fcd8SRoberto Vitillo 	bfd 		*abfd;
1472f48fcd8SRoberto Vitillo 	asymbol 	**syms;
1482f48fcd8SRoberto Vitillo };
1492f48fcd8SRoberto Vitillo 
1502f48fcd8SRoberto Vitillo static int bfd_error(const char *string)
1512f48fcd8SRoberto Vitillo {
1522f48fcd8SRoberto Vitillo 	const char *errmsg;
1532f48fcd8SRoberto Vitillo 
1542f48fcd8SRoberto Vitillo 	errmsg = bfd_errmsg(bfd_get_error());
1552f48fcd8SRoberto Vitillo 	fflush(stdout);
1562f48fcd8SRoberto Vitillo 
1572f48fcd8SRoberto Vitillo 	if (string)
1582f48fcd8SRoberto Vitillo 		pr_debug("%s: %s\n", string, errmsg);
1592f48fcd8SRoberto Vitillo 	else
1602f48fcd8SRoberto Vitillo 		pr_debug("%s\n", errmsg);
1612f48fcd8SRoberto Vitillo 
1622f48fcd8SRoberto Vitillo 	return -1;
1632f48fcd8SRoberto Vitillo }
1642f48fcd8SRoberto Vitillo 
1652f48fcd8SRoberto Vitillo static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
1662f48fcd8SRoberto Vitillo {
1672f48fcd8SRoberto Vitillo 	long storage;
1682f48fcd8SRoberto Vitillo 	long symcount;
1692f48fcd8SRoberto Vitillo 	asymbol **syms;
1702f48fcd8SRoberto Vitillo 	bfd_boolean dynamic = FALSE;
1712f48fcd8SRoberto Vitillo 
1722f48fcd8SRoberto Vitillo 	if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
1732f48fcd8SRoberto Vitillo 		return bfd_error(bfd_get_filename(abfd));
1742f48fcd8SRoberto Vitillo 
1752f48fcd8SRoberto Vitillo 	storage = bfd_get_symtab_upper_bound(abfd);
1762f48fcd8SRoberto Vitillo 	if (storage == 0L) {
1772f48fcd8SRoberto Vitillo 		storage = bfd_get_dynamic_symtab_upper_bound(abfd);
1782f48fcd8SRoberto Vitillo 		dynamic = TRUE;
1792f48fcd8SRoberto Vitillo 	}
1802f48fcd8SRoberto Vitillo 	if (storage < 0L)
1812f48fcd8SRoberto Vitillo 		return bfd_error(bfd_get_filename(abfd));
1822f48fcd8SRoberto Vitillo 
1832f48fcd8SRoberto Vitillo 	syms = malloc(storage);
1842f48fcd8SRoberto Vitillo 	if (dynamic)
1852f48fcd8SRoberto Vitillo 		symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
1862f48fcd8SRoberto Vitillo 	else
1872f48fcd8SRoberto Vitillo 		symcount = bfd_canonicalize_symtab(abfd, syms);
1882f48fcd8SRoberto Vitillo 
1892f48fcd8SRoberto Vitillo 	if (symcount < 0) {
1902f48fcd8SRoberto Vitillo 		free(syms);
1912f48fcd8SRoberto Vitillo 		return bfd_error(bfd_get_filename(abfd));
1922f48fcd8SRoberto Vitillo 	}
1932f48fcd8SRoberto Vitillo 
1942f48fcd8SRoberto Vitillo 	a2l->syms = syms;
1952f48fcd8SRoberto Vitillo 	return 0;
1962f48fcd8SRoberto Vitillo }
1972f48fcd8SRoberto Vitillo 
1982f48fcd8SRoberto Vitillo static void find_address_in_section(bfd *abfd, asection *section, void *data)
1992f48fcd8SRoberto Vitillo {
2002f48fcd8SRoberto Vitillo 	bfd_vma pc, vma;
2012f48fcd8SRoberto Vitillo 	bfd_size_type size;
2022f48fcd8SRoberto Vitillo 	struct a2l_data *a2l = data;
2030ada120cSChangbin Du 	flagword flags;
2042f48fcd8SRoberto Vitillo 
2052f48fcd8SRoberto Vitillo 	if (a2l->found)
2062f48fcd8SRoberto Vitillo 		return;
2072f48fcd8SRoberto Vitillo 
2080ada120cSChangbin Du #ifdef bfd_get_section_flags
2090ada120cSChangbin Du 	flags = bfd_get_section_flags(abfd, section);
2100ada120cSChangbin Du #else
2110ada120cSChangbin Du 	flags = bfd_section_flags(section);
2120ada120cSChangbin Du #endif
2130ada120cSChangbin Du 	if ((flags & SEC_ALLOC) == 0)
2142f48fcd8SRoberto Vitillo 		return;
2152f48fcd8SRoberto Vitillo 
2162f48fcd8SRoberto Vitillo 	pc = a2l->addr;
2170ada120cSChangbin Du #ifdef bfd_get_section_vma
2182f48fcd8SRoberto Vitillo 	vma = bfd_get_section_vma(abfd, section);
2190ada120cSChangbin Du #else
2200ada120cSChangbin Du 	vma = bfd_section_vma(section);
2210ada120cSChangbin Du #endif
2220ada120cSChangbin Du #ifdef bfd_get_section_size
2232f48fcd8SRoberto Vitillo 	size = bfd_get_section_size(section);
2240ada120cSChangbin Du #else
2250ada120cSChangbin Du 	size = bfd_section_size(section);
2260ada120cSChangbin Du #endif
2272f48fcd8SRoberto Vitillo 
2282f48fcd8SRoberto Vitillo 	if (pc < vma || pc >= vma + size)
2292f48fcd8SRoberto Vitillo 		return;
2302f48fcd8SRoberto Vitillo 
2312f48fcd8SRoberto Vitillo 	a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
2322f48fcd8SRoberto Vitillo 					   &a2l->filename, &a2l->funcname,
2332f48fcd8SRoberto Vitillo 					   &a2l->line);
234d964b1cdSMilian Wolff 
235d964b1cdSMilian Wolff 	if (a2l->filename && !strlen(a2l->filename))
236d964b1cdSMilian Wolff 		a2l->filename = NULL;
2372f48fcd8SRoberto Vitillo }
2382f48fcd8SRoberto Vitillo 
2392f48fcd8SRoberto Vitillo static struct a2l_data *addr2line_init(const char *path)
2402f48fcd8SRoberto Vitillo {
2412f48fcd8SRoberto Vitillo 	bfd *abfd;
2422f48fcd8SRoberto Vitillo 	struct a2l_data *a2l = NULL;
2432f48fcd8SRoberto Vitillo 
2442f48fcd8SRoberto Vitillo 	abfd = bfd_openr(path, NULL);
2452f48fcd8SRoberto Vitillo 	if (abfd == NULL)
2462f48fcd8SRoberto Vitillo 		return NULL;
2472f48fcd8SRoberto Vitillo 
2482f48fcd8SRoberto Vitillo 	if (!bfd_check_format(abfd, bfd_object))
2492f48fcd8SRoberto Vitillo 		goto out;
2502f48fcd8SRoberto Vitillo 
2512f48fcd8SRoberto Vitillo 	a2l = zalloc(sizeof(*a2l));
2522f48fcd8SRoberto Vitillo 	if (a2l == NULL)
2532f48fcd8SRoberto Vitillo 		goto out;
2542f48fcd8SRoberto Vitillo 
2552f48fcd8SRoberto Vitillo 	a2l->abfd = abfd;
2562f48fcd8SRoberto Vitillo 	a2l->input = strdup(path);
2572f48fcd8SRoberto Vitillo 	if (a2l->input == NULL)
2582f48fcd8SRoberto Vitillo 		goto out;
2592f48fcd8SRoberto Vitillo 
2602f48fcd8SRoberto Vitillo 	if (slurp_symtab(abfd, a2l))
2612f48fcd8SRoberto Vitillo 		goto out;
2622f48fcd8SRoberto Vitillo 
2632f48fcd8SRoberto Vitillo 	return a2l;
2642f48fcd8SRoberto Vitillo 
2652f48fcd8SRoberto Vitillo out:
2662f48fcd8SRoberto Vitillo 	if (a2l) {
2677d16c634SNamhyung Kim 		zfree((char **)&a2l->input);
2682f48fcd8SRoberto Vitillo 		free(a2l);
2692f48fcd8SRoberto Vitillo 	}
2702f48fcd8SRoberto Vitillo 	bfd_close(abfd);
2712f48fcd8SRoberto Vitillo 	return NULL;
2722f48fcd8SRoberto Vitillo }
2732f48fcd8SRoberto Vitillo 
2742f48fcd8SRoberto Vitillo static void addr2line_cleanup(struct a2l_data *a2l)
2752f48fcd8SRoberto Vitillo {
2762f48fcd8SRoberto Vitillo 	if (a2l->abfd)
2772f48fcd8SRoberto Vitillo 		bfd_close(a2l->abfd);
2787d16c634SNamhyung Kim 	zfree((char **)&a2l->input);
27974cf249dSArnaldo Carvalho de Melo 	zfree(&a2l->syms);
2802f48fcd8SRoberto Vitillo 	free(a2l);
2812f48fcd8SRoberto Vitillo }
2822f48fcd8SRoberto Vitillo 
2834d53b9d5SMilian Wolff static int inline_list__append_dso_a2l(struct dso *dso,
284fea0cf84SMilian Wolff 				       struct inline_node *node,
285fea0cf84SMilian Wolff 				       struct symbol *sym)
2864d53b9d5SMilian Wolff {
2874d53b9d5SMilian Wolff 	struct a2l_data *a2l = dso->a2l;
288fea0cf84SMilian Wolff 	struct symbol *inline_sym = new_inline_sym(dso, sym, a2l->funcname);
2892be8832fSMilian Wolff 	char *srcline = NULL;
2904d53b9d5SMilian Wolff 
2912be8832fSMilian Wolff 	if (a2l->filename)
2922be8832fSMilian Wolff 		srcline = srcline_from_fileline(a2l->filename, a2l->line);
2932be8832fSMilian Wolff 
2942be8832fSMilian Wolff 	return inline_list__append(inline_sym, srcline, node);
2954d53b9d5SMilian Wolff }
2964d53b9d5SMilian Wolff 
297ac931f87SWang Nan static int addr2line(const char *dso_name, u64 addr,
2982f84b42bSAndi Kleen 		     char **file, unsigned int *line, struct dso *dso,
299fea0cf84SMilian Wolff 		     bool unwind_inlines, struct inline_node *node,
300fea0cf84SMilian Wolff 		     struct symbol *sym)
3012f48fcd8SRoberto Vitillo {
3022f48fcd8SRoberto Vitillo 	int ret = 0;
303454ff00fSAdrian Hunter 	struct a2l_data *a2l = dso->a2l;
3042f48fcd8SRoberto Vitillo 
305454ff00fSAdrian Hunter 	if (!a2l) {
306454ff00fSAdrian Hunter 		dso->a2l = addr2line_init(dso_name);
307454ff00fSAdrian Hunter 		a2l = dso->a2l;
308454ff00fSAdrian Hunter 	}
309454ff00fSAdrian Hunter 
3102f48fcd8SRoberto Vitillo 	if (a2l == NULL) {
311b10c78c5SJin Yao 		if (!symbol_conf.disable_add2line_warn)
3122f48fcd8SRoberto Vitillo 			pr_warning("addr2line_init failed for %s\n", dso_name);
3132f48fcd8SRoberto Vitillo 		return 0;
3142f48fcd8SRoberto Vitillo 	}
3152f48fcd8SRoberto Vitillo 
3162f48fcd8SRoberto Vitillo 	a2l->addr = addr;
317454ff00fSAdrian Hunter 	a2l->found = false;
318454ff00fSAdrian Hunter 
3192f48fcd8SRoberto Vitillo 	bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
3202f48fcd8SRoberto Vitillo 
321b21cc978SMilian Wolff 	if (!a2l->found)
322b21cc978SMilian Wolff 		return 0;
323b21cc978SMilian Wolff 
324b21cc978SMilian Wolff 	if (unwind_inlines) {
3252f84b42bSAndi Kleen 		int cnt = 0;
3262f84b42bSAndi Kleen 
327fea0cf84SMilian Wolff 		if (node && inline_list__append_dso_a2l(dso, node, sym))
3284d53b9d5SMilian Wolff 			return 0;
3294d53b9d5SMilian Wolff 
3302f84b42bSAndi Kleen 		while (bfd_find_inliner_info(a2l->abfd, &a2l->filename,
3312f84b42bSAndi Kleen 					     &a2l->funcname, &a2l->line) &&
332a64489c5SJin Yao 		       cnt++ < MAX_INLINE_NEST) {
333a64489c5SJin Yao 
334d964b1cdSMilian Wolff 			if (a2l->filename && !strlen(a2l->filename))
335d964b1cdSMilian Wolff 				a2l->filename = NULL;
336d964b1cdSMilian Wolff 
337a64489c5SJin Yao 			if (node != NULL) {
338fea0cf84SMilian Wolff 				if (inline_list__append_dso_a2l(dso, node, sym))
339a64489c5SJin Yao 					return 0;
340b21cc978SMilian Wolff 				// found at least one inline frame
341b21cc978SMilian Wolff 				ret = 1;
342a64489c5SJin Yao 			}
343a64489c5SJin Yao 		}
3442f84b42bSAndi Kleen 	}
3452f84b42bSAndi Kleen 
346b21cc978SMilian Wolff 	if (file) {
347b21cc978SMilian Wolff 		*file = a2l->filename ? strdup(a2l->filename) : NULL;
348b21cc978SMilian Wolff 		ret = *file ? 1 : 0;
3492f48fcd8SRoberto Vitillo 	}
3502f48fcd8SRoberto Vitillo 
351b21cc978SMilian Wolff 	if (line)
352b21cc978SMilian Wolff 		*line = a2l->line;
353b21cc978SMilian Wolff 
3542f48fcd8SRoberto Vitillo 	return ret;
3552f48fcd8SRoberto Vitillo }
3562f48fcd8SRoberto Vitillo 
357454ff00fSAdrian Hunter void dso__free_a2l(struct dso *dso)
358454ff00fSAdrian Hunter {
359454ff00fSAdrian Hunter 	struct a2l_data *a2l = dso->a2l;
360454ff00fSAdrian Hunter 
361454ff00fSAdrian Hunter 	if (!a2l)
362454ff00fSAdrian Hunter 		return;
363454ff00fSAdrian Hunter 
364454ff00fSAdrian Hunter 	addr2line_cleanup(a2l);
365454ff00fSAdrian Hunter 
366454ff00fSAdrian Hunter 	dso->a2l = NULL;
367454ff00fSAdrian Hunter }
368454ff00fSAdrian Hunter 
3692f48fcd8SRoberto Vitillo #else /* HAVE_LIBBFD_SUPPORT */
3702f48fcd8SRoberto Vitillo 
3715580338dSJin Yao static int filename_split(char *filename, unsigned int *line_nr)
3725580338dSJin Yao {
3735580338dSJin Yao 	char *sep;
3745580338dSJin Yao 
3755580338dSJin Yao 	sep = strchr(filename, '\n');
3765580338dSJin Yao 	if (sep)
3775580338dSJin Yao 		*sep = '\0';
3785580338dSJin Yao 
3795580338dSJin Yao 	if (!strcmp(filename, "??:0"))
3805580338dSJin Yao 		return 0;
3815580338dSJin Yao 
3825580338dSJin Yao 	sep = strchr(filename, ':');
3835580338dSJin Yao 	if (sep) {
3845580338dSJin Yao 		*sep++ = '\0';
3855580338dSJin Yao 		*line_nr = strtoul(sep, NULL, 0);
3865580338dSJin Yao 		return 1;
3875580338dSJin Yao 	}
3885580338dSJin Yao 
3895580338dSJin Yao 	return 0;
3905580338dSJin Yao }
3915580338dSJin Yao 
392b3801e79SIan Rogers static void addr2line_subprocess_cleanup(struct child_process *a2l)
393f048d548SNamhyung Kim {
394b3801e79SIan Rogers 	if (a2l->pid != -1) {
395b3801e79SIan Rogers 		kill(a2l->pid, SIGKILL);
396b3801e79SIan Rogers 		finish_command(a2l); /* ignore result, we don't care */
397b3801e79SIan Rogers 		a2l->pid = -1;
398be8ecc57STony Garnock-Jones 	}
399be8ecc57STony Garnock-Jones 
400be8ecc57STony Garnock-Jones 	free(a2l);
401be8ecc57STony Garnock-Jones }
402be8ecc57STony Garnock-Jones 
403b3801e79SIan Rogers static struct child_process *addr2line_subprocess_init(const char *addr2line_path,
40457594454SIan Rogers 							const char *binary_path)
405be8ecc57STony Garnock-Jones {
40657594454SIan Rogers 	const char *argv[] = {
40757594454SIan Rogers 		addr2line_path ?: "addr2line",
40857594454SIan Rogers 		"-e", binary_path,
40957594454SIan Rogers 		"-i", "-f", NULL
41057594454SIan Rogers 	};
411b3801e79SIan Rogers 	struct child_process *a2l = zalloc(sizeof(*a2l));
412be8ecc57STony Garnock-Jones 	int start_command_status = 0;
413be8ecc57STony Garnock-Jones 
414b3801e79SIan Rogers 	if (a2l == NULL) {
415b3801e79SIan Rogers 		pr_err("Failed to allocate memory for addr2line");
416b3801e79SIan Rogers 		return NULL;
417b3801e79SIan Rogers 	}
418be8ecc57STony Garnock-Jones 
419b3801e79SIan Rogers 	a2l->pid = -1;
420b3801e79SIan Rogers 	a2l->in = -1;
421b3801e79SIan Rogers 	a2l->out = -1;
422b3801e79SIan Rogers 	a2l->no_stderr = 1;
423be8ecc57STony Garnock-Jones 
424b3801e79SIan Rogers 	a2l->argv = argv;
425b3801e79SIan Rogers 	start_command_status = start_command(a2l);
426b3801e79SIan Rogers 	a2l->argv = NULL; /* it's not used after start_command; avoid dangling pointers */
427be8ecc57STony Garnock-Jones 
428be8ecc57STony Garnock-Jones 	if (start_command_status != 0) {
42957594454SIan Rogers 		pr_warning("could not start addr2line (%s) for %s: start_command return code %d\n",
43057594454SIan Rogers 			addr2line_path, binary_path, start_command_status);
431be8ecc57STony Garnock-Jones 		addr2line_subprocess_cleanup(a2l);
432be8ecc57STony Garnock-Jones 		return NULL;
433be8ecc57STony Garnock-Jones 	}
434be8ecc57STony Garnock-Jones 
435b3801e79SIan Rogers 	return a2l;
436b3801e79SIan Rogers }
437b3801e79SIan Rogers 
438*2c4b9280SIan Rogers enum a2l_style {
439*2c4b9280SIan Rogers 	BROKEN,
440*2c4b9280SIan Rogers 	GNU_BINUTILS,
441*2c4b9280SIan Rogers 	LLVM,
442*2c4b9280SIan Rogers };
443*2c4b9280SIan Rogers 
444*2c4b9280SIan Rogers static enum a2l_style addr2line_configure(struct child_process *a2l)
445*2c4b9280SIan Rogers {
446*2c4b9280SIan Rogers 	static bool cached;
447*2c4b9280SIan Rogers 	static enum a2l_style style;
448*2c4b9280SIan Rogers 
449*2c4b9280SIan Rogers 	if (!cached) {
450*2c4b9280SIan Rogers 		char buf[128];
451*2c4b9280SIan Rogers 		struct io io;
452*2c4b9280SIan Rogers 		int ch;
453*2c4b9280SIan Rogers 
454*2c4b9280SIan Rogers 		if (write(a2l->in, ",\n", 2) != 2)
455*2c4b9280SIan Rogers 			return BROKEN;
456*2c4b9280SIan Rogers 
457*2c4b9280SIan Rogers 		io__init(&io, a2l->out, buf, sizeof(buf));
458*2c4b9280SIan Rogers 		ch = io__get_char(&io);
459*2c4b9280SIan Rogers 		if (ch == ',') {
460*2c4b9280SIan Rogers 			style = LLVM;
461*2c4b9280SIan Rogers 			cached = true;
462*2c4b9280SIan Rogers 		} else if (ch == '?') {
463*2c4b9280SIan Rogers 			style = GNU_BINUTILS;
464*2c4b9280SIan Rogers 			cached = true;
465*2c4b9280SIan Rogers 		} else {
466*2c4b9280SIan Rogers 			style = BROKEN;
467*2c4b9280SIan Rogers 		}
468*2c4b9280SIan Rogers 		do {
469*2c4b9280SIan Rogers 			ch = io__get_char(&io);
470*2c4b9280SIan Rogers 		} while (ch > 0 && ch != '\n');
471*2c4b9280SIan Rogers 		if (style == GNU_BINUTILS) {
472*2c4b9280SIan Rogers 			do {
473*2c4b9280SIan Rogers 				ch = io__get_char(&io);
474*2c4b9280SIan Rogers 			} while (ch > 0 && ch != '\n');
475*2c4b9280SIan Rogers 		}
476*2c4b9280SIan Rogers 	}
477*2c4b9280SIan Rogers 	return style;
478*2c4b9280SIan Rogers }
479*2c4b9280SIan Rogers 
480b3801e79SIan Rogers static int read_addr2line_record(struct io *io,
481*2c4b9280SIan Rogers 				 enum a2l_style style,
482be8ecc57STony Garnock-Jones 				 char **function,
483be8ecc57STony Garnock-Jones 				 char **filename,
484be8ecc57STony Garnock-Jones 				 unsigned int *line_nr)
485be8ecc57STony Garnock-Jones {
486be8ecc57STony Garnock-Jones 	/*
487be8ecc57STony Garnock-Jones 	 * Returns:
488be8ecc57STony Garnock-Jones 	 * -1 ==> error
489be8ecc57STony Garnock-Jones 	 * 0 ==> sentinel (or other ill-formed) record read
490be8ecc57STony Garnock-Jones 	 * 1 ==> a genuine record read
491be8ecc57STony Garnock-Jones 	 */
492be8ecc57STony Garnock-Jones 	char *line = NULL;
493be8ecc57STony Garnock-Jones 	size_t line_len = 0;
494be8ecc57STony Garnock-Jones 	unsigned int dummy_line_nr = 0;
495be8ecc57STony Garnock-Jones 	int ret = -1;
496be8ecc57STony Garnock-Jones 
497be8ecc57STony Garnock-Jones 	if (function != NULL)
498be8ecc57STony Garnock-Jones 		zfree(function);
499be8ecc57STony Garnock-Jones 
500be8ecc57STony Garnock-Jones 	if (filename != NULL)
501be8ecc57STony Garnock-Jones 		zfree(filename);
502be8ecc57STony Garnock-Jones 
503be8ecc57STony Garnock-Jones 	if (line_nr != NULL)
504be8ecc57STony Garnock-Jones 		*line_nr = 0;
505be8ecc57STony Garnock-Jones 
506b3801e79SIan Rogers 	if (io__getline(io, &line, &line_len) < 0 || !line_len)
507be8ecc57STony Garnock-Jones 		goto error;
508*2c4b9280SIan Rogers 
509*2c4b9280SIan Rogers 	if (style == LLVM && line_len == 2 && line[0] == ',') {
510*2c4b9280SIan Rogers 		zfree(&line);
511*2c4b9280SIan Rogers 		return 0;
512*2c4b9280SIan Rogers 	}
513*2c4b9280SIan Rogers 
514be8ecc57STony Garnock-Jones 	if (function != NULL)
515be8ecc57STony Garnock-Jones 		*function = strdup(strim(line));
516be8ecc57STony Garnock-Jones 
517be8ecc57STony Garnock-Jones 	zfree(&line);
518be8ecc57STony Garnock-Jones 	line_len = 0;
519be8ecc57STony Garnock-Jones 
520b3801e79SIan Rogers 	if (io__getline(io, &line, &line_len) < 0 || !line_len)
521be8ecc57STony Garnock-Jones 		goto error;
522be8ecc57STony Garnock-Jones 
523*2c4b9280SIan Rogers 	if (filename_split(line, line_nr == NULL ? &dummy_line_nr : line_nr) == 0 &&
524*2c4b9280SIan Rogers 	    style == GNU_BINUTILS) {
525be8ecc57STony Garnock-Jones 		ret = 0;
526be8ecc57STony Garnock-Jones 		goto error;
527be8ecc57STony Garnock-Jones 	}
528be8ecc57STony Garnock-Jones 
529be8ecc57STony Garnock-Jones 	if (filename != NULL)
530be8ecc57STony Garnock-Jones 		*filename = strdup(line);
531be8ecc57STony Garnock-Jones 
532be8ecc57STony Garnock-Jones 	zfree(&line);
533be8ecc57STony Garnock-Jones 	line_len = 0;
534be8ecc57STony Garnock-Jones 
535be8ecc57STony Garnock-Jones 	return 1;
536be8ecc57STony Garnock-Jones 
537be8ecc57STony Garnock-Jones error:
538be8ecc57STony Garnock-Jones 	free(line);
539be8ecc57STony Garnock-Jones 	if (function != NULL)
540be8ecc57STony Garnock-Jones 		zfree(function);
541be8ecc57STony Garnock-Jones 	if (filename != NULL)
542be8ecc57STony Garnock-Jones 		zfree(filename);
543f048d548SNamhyung Kim 	return ret;
544f048d548SNamhyung Kim }
545454ff00fSAdrian Hunter 
546be8ecc57STony Garnock-Jones static int inline_list__append_record(struct dso *dso,
547be8ecc57STony Garnock-Jones 				      struct inline_node *node,
548be8ecc57STony Garnock-Jones 				      struct symbol *sym,
549be8ecc57STony Garnock-Jones 				      const char *function,
550be8ecc57STony Garnock-Jones 				      const char *filename,
551be8ecc57STony Garnock-Jones 				      unsigned int line_nr)
552454ff00fSAdrian Hunter {
553be8ecc57STony Garnock-Jones 	struct symbol *inline_sym = new_inline_sym(dso, sym, function);
554be8ecc57STony Garnock-Jones 
555be8ecc57STony Garnock-Jones 	return inline_list__append(inline_sym, srcline_from_fileline(filename, line_nr), node);
556454ff00fSAdrian Hunter }
557454ff00fSAdrian Hunter 
558be8ecc57STony Garnock-Jones static int addr2line(const char *dso_name, u64 addr,
559be8ecc57STony Garnock-Jones 		     char **file, unsigned int *line_nr,
560be8ecc57STony Garnock-Jones 		     struct dso *dso,
561be8ecc57STony Garnock-Jones 		     bool unwind_inlines,
562be8ecc57STony Garnock-Jones 		     struct inline_node *node,
563be8ecc57STony Garnock-Jones 		     struct symbol *sym __maybe_unused)
564be8ecc57STony Garnock-Jones {
565b3801e79SIan Rogers 	struct child_process *a2l = dso->a2l;
566be8ecc57STony Garnock-Jones 	char *record_function = NULL;
567be8ecc57STony Garnock-Jones 	char *record_filename = NULL;
568be8ecc57STony Garnock-Jones 	unsigned int record_line_nr = 0;
569be8ecc57STony Garnock-Jones 	int record_status = -1;
570be8ecc57STony Garnock-Jones 	int ret = 0;
571be8ecc57STony Garnock-Jones 	size_t inline_count = 0;
572b3801e79SIan Rogers 	int len;
573b3801e79SIan Rogers 	char buf[128];
574b3801e79SIan Rogers 	ssize_t written;
575b3801e79SIan Rogers 	struct io io;
576*2c4b9280SIan Rogers 	enum a2l_style a2l_style;
577be8ecc57STony Garnock-Jones 
578be8ecc57STony Garnock-Jones 	if (!a2l) {
5793b27222dSNamhyung Kim 		if (!filename__has_section(dso_name, ".debug_line"))
5803b27222dSNamhyung Kim 			goto out;
5813b27222dSNamhyung Kim 
58257594454SIan Rogers 		dso->a2l = addr2line_subprocess_init(symbol_conf.addr2line_path,
58357594454SIan Rogers 						     dso_name);
584be8ecc57STony Garnock-Jones 		a2l = dso->a2l;
585be8ecc57STony Garnock-Jones 	}
586be8ecc57STony Garnock-Jones 
587be8ecc57STony Garnock-Jones 	if (a2l == NULL) {
588be8ecc57STony Garnock-Jones 		if (!symbol_conf.disable_add2line_warn)
589be8ecc57STony Garnock-Jones 			pr_warning("%s %s: addr2line_subprocess_init failed\n", __func__, dso_name);
590be8ecc57STony Garnock-Jones 		goto out;
591be8ecc57STony Garnock-Jones 	}
592*2c4b9280SIan Rogers 	a2l_style = addr2line_configure(a2l);
593*2c4b9280SIan Rogers 	if (a2l_style == BROKEN) {
594*2c4b9280SIan Rogers 		if (!symbol_conf.disable_add2line_warn)
595*2c4b9280SIan Rogers 			pr_warning("%s: addr2line configuration failed\n", __func__);
596*2c4b9280SIan Rogers 		goto out;
597*2c4b9280SIan Rogers 	}
598be8ecc57STony Garnock-Jones 
599be8ecc57STony Garnock-Jones 	/*
600be8ecc57STony Garnock-Jones 	 * Send our request and then *deliberately* send something that can't be interpreted as
601be8ecc57STony Garnock-Jones 	 * a valid address to ask addr2line about (namely, ","). This causes addr2line to first
602be8ecc57STony Garnock-Jones 	 * write out the answer to our request, in an unbounded/unknown number of records, and
603*2c4b9280SIan Rogers 	 * then to write out the lines "??" and "??:0", for GNU binutils, or "," for
604*2c4b9280SIan Rogers 	 * llvm-addr2line, so that we can detect when it has finished giving us anything
605*2c4b9280SIan Rogers 	 * useful. With GNU binutils, we have to be careful about the first record, though,
606*2c4b9280SIan Rogers 	 * because it may be genuinely unknown, in which case we'll get two sets of "??"/"??:0"
607*2c4b9280SIan Rogers 	 * lines.
608be8ecc57STony Garnock-Jones 	 */
609b3801e79SIan Rogers 	len = snprintf(buf, sizeof(buf), "%016"PRIx64"\n,\n", addr);
610b3801e79SIan Rogers 	written = len > 0 ? write(a2l->in, buf, len) : -1;
611b3801e79SIan Rogers 	if (written != len) {
612d5e33ce0SNamhyung Kim 		if (!symbol_conf.disable_add2line_warn)
613be8ecc57STony Garnock-Jones 			pr_warning("%s %s: could not send request\n", __func__, dso_name);
614be8ecc57STony Garnock-Jones 		goto out;
615be8ecc57STony Garnock-Jones 	}
616b3801e79SIan Rogers 	io__init(&io, a2l->out, buf, sizeof(buf));
617be8ecc57STony Garnock-Jones 
618*2c4b9280SIan Rogers 	switch (read_addr2line_record(&io, a2l_style,
619*2c4b9280SIan Rogers 				      &record_function, &record_filename, &record_line_nr)) {
620be8ecc57STony Garnock-Jones 	case -1:
621d5e33ce0SNamhyung Kim 		if (!symbol_conf.disable_add2line_warn)
622be8ecc57STony Garnock-Jones 			pr_warning("%s %s: could not read first record\n", __func__, dso_name);
623be8ecc57STony Garnock-Jones 		goto out;
624be8ecc57STony Garnock-Jones 	case 0:
625be8ecc57STony Garnock-Jones 		/*
626be8ecc57STony Garnock-Jones 		 * The first record was invalid, so return failure, but first read another
627be8ecc57STony Garnock-Jones 		 * record, since we asked a junk question and have to clear the answer out.
628be8ecc57STony Garnock-Jones 		 */
629*2c4b9280SIan Rogers 		switch (read_addr2line_record(&io, a2l_style, NULL, NULL, NULL)) {
630be8ecc57STony Garnock-Jones 		case -1:
631d5e33ce0SNamhyung Kim 			if (!symbol_conf.disable_add2line_warn)
632d5e33ce0SNamhyung Kim 				pr_warning("%s %s: could not read delimiter record\n",
633d5e33ce0SNamhyung Kim 					   __func__, dso_name);
634be8ecc57STony Garnock-Jones 			break;
635be8ecc57STony Garnock-Jones 		case 0:
636be8ecc57STony Garnock-Jones 			/* As expected. */
637be8ecc57STony Garnock-Jones 			break;
638be8ecc57STony Garnock-Jones 		default:
639d5e33ce0SNamhyung Kim 			if (!symbol_conf.disable_add2line_warn)
640be8ecc57STony Garnock-Jones 				pr_warning("%s %s: unexpected record instead of sentinel",
641be8ecc57STony Garnock-Jones 					   __func__, dso_name);
642be8ecc57STony Garnock-Jones 			break;
643be8ecc57STony Garnock-Jones 		}
644be8ecc57STony Garnock-Jones 		goto out;
645be8ecc57STony Garnock-Jones 	default:
646be8ecc57STony Garnock-Jones 		break;
647be8ecc57STony Garnock-Jones 	}
648be8ecc57STony Garnock-Jones 
649be8ecc57STony Garnock-Jones 	if (file) {
650be8ecc57STony Garnock-Jones 		*file = strdup(record_filename);
651be8ecc57STony Garnock-Jones 		ret = 1;
652be8ecc57STony Garnock-Jones 	}
653be8ecc57STony Garnock-Jones 	if (line_nr)
654be8ecc57STony Garnock-Jones 		*line_nr = record_line_nr;
655be8ecc57STony Garnock-Jones 
656be8ecc57STony Garnock-Jones 	if (unwind_inlines) {
657be8ecc57STony Garnock-Jones 		if (node && inline_list__append_record(dso, node, sym,
658be8ecc57STony Garnock-Jones 						       record_function,
659be8ecc57STony Garnock-Jones 						       record_filename,
660be8ecc57STony Garnock-Jones 						       record_line_nr)) {
661be8ecc57STony Garnock-Jones 			ret = 0;
662be8ecc57STony Garnock-Jones 			goto out;
663be8ecc57STony Garnock-Jones 		}
664be8ecc57STony Garnock-Jones 	}
665be8ecc57STony Garnock-Jones 
666be8ecc57STony Garnock-Jones 	/* We have to read the records even if we don't care about the inline info. */
667b3801e79SIan Rogers 	while ((record_status = read_addr2line_record(&io,
668*2c4b9280SIan Rogers 						      a2l_style,
669be8ecc57STony Garnock-Jones 						      &record_function,
670be8ecc57STony Garnock-Jones 						      &record_filename,
671be8ecc57STony Garnock-Jones 						      &record_line_nr)) == 1) {
672be8ecc57STony Garnock-Jones 		if (unwind_inlines && node && inline_count++ < MAX_INLINE_NEST) {
673be8ecc57STony Garnock-Jones 			if (inline_list__append_record(dso, node, sym,
674be8ecc57STony Garnock-Jones 						       record_function,
675be8ecc57STony Garnock-Jones 						       record_filename,
676be8ecc57STony Garnock-Jones 						       record_line_nr)) {
677be8ecc57STony Garnock-Jones 				ret = 0;
678be8ecc57STony Garnock-Jones 				goto out;
679be8ecc57STony Garnock-Jones 			}
680be8ecc57STony Garnock-Jones 			ret = 1; /* found at least one inline frame */
681be8ecc57STony Garnock-Jones 		}
682be8ecc57STony Garnock-Jones 	}
683be8ecc57STony Garnock-Jones 
684be8ecc57STony Garnock-Jones out:
685be8ecc57STony Garnock-Jones 	free(record_function);
686be8ecc57STony Garnock-Jones 	free(record_filename);
687be8ecc57STony Garnock-Jones 	return ret;
688be8ecc57STony Garnock-Jones }
689be8ecc57STony Garnock-Jones 
690be8ecc57STony Garnock-Jones void dso__free_a2l(struct dso *dso)
691be8ecc57STony Garnock-Jones {
692b3801e79SIan Rogers 	struct child_process *a2l = dso->a2l;
693be8ecc57STony Garnock-Jones 
694be8ecc57STony Garnock-Jones 	if (!a2l)
695be8ecc57STony Garnock-Jones 		return;
696be8ecc57STony Garnock-Jones 
697be8ecc57STony Garnock-Jones 	addr2line_subprocess_cleanup(a2l);
698be8ecc57STony Garnock-Jones 
699be8ecc57STony Garnock-Jones 	dso->a2l = NULL;
700be8ecc57STony Garnock-Jones }
701be8ecc57STony Garnock-Jones 
702be8ecc57STony Garnock-Jones #endif /* HAVE_LIBBFD_SUPPORT */
703be8ecc57STony Garnock-Jones 
704a64489c5SJin Yao static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
705be8ecc57STony Garnock-Jones 					struct dso *dso, struct symbol *sym)
706a64489c5SJin Yao {
707a64489c5SJin Yao 	struct inline_node *node;
708a64489c5SJin Yao 
709a64489c5SJin Yao 	node = zalloc(sizeof(*node));
710a64489c5SJin Yao 	if (node == NULL) {
711a64489c5SJin Yao 		perror("not enough memory for the inline node");
712be8ecc57STony Garnock-Jones 		return NULL;
713a64489c5SJin Yao 	}
714a64489c5SJin Yao 
715a64489c5SJin Yao 	INIT_LIST_HEAD(&node->val);
716a64489c5SJin Yao 	node->addr = addr;
717a64489c5SJin Yao 
718be8ecc57STony Garnock-Jones 	addr2line(dso_name, addr, NULL, NULL, dso, true, node, sym);
719a64489c5SJin Yao 	return node;
720a64489c5SJin Yao }
721a64489c5SJin Yao 
722906049c8SAdrian Hunter /*
723906049c8SAdrian Hunter  * Number of addr2line failures (without success) before disabling it for that
724906049c8SAdrian Hunter  * dso.
725906049c8SAdrian Hunter  */
726906049c8SAdrian Hunter #define A2L_FAIL_LIMIT 123
727906049c8SAdrian Hunter 
7282f84b42bSAndi Kleen char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
729935f5a9dSJin Yao 		  bool show_sym, bool show_addr, bool unwind_inlines,
730935f5a9dSJin Yao 		  u64 ip)
731f048d548SNamhyung Kim {
732a949fffbSDavid Ahern 	char *file = NULL;
733a949fffbSDavid Ahern 	unsigned line = 0;
7342cc9d0efSNamhyung Kim 	char *srcline;
735bf4414aeSArnaldo Carvalho de Melo 	const char *dso_name;
736f048d548SNamhyung Kim 
7372cc9d0efSNamhyung Kim 	if (!dso->has_srcline)
73823f0981bSAndi Kleen 		goto out;
7392cc9d0efSNamhyung Kim 
7405580338dSJin Yao 	dso_name = dso__name(dso);
7415580338dSJin Yao 	if (dso_name == NULL)
74258d91a00SNamhyung Kim 		goto out;
74358d91a00SNamhyung Kim 
744fea0cf84SMilian Wolff 	if (!addr2line(dso_name, addr, &file, &line, dso,
745fea0cf84SMilian Wolff 		       unwind_inlines, NULL, sym))
74658d91a00SNamhyung Kim 		goto out;
747f048d548SNamhyung Kim 
7482be8832fSMilian Wolff 	srcline = srcline_from_fileline(file, line);
749906049c8SAdrian Hunter 	free(file);
7502be8832fSMilian Wolff 
7512be8832fSMilian Wolff 	if (!srcline)
752906049c8SAdrian Hunter 		goto out;
753906049c8SAdrian Hunter 
754906049c8SAdrian Hunter 	dso->a2l_fails = 0;
755f048d548SNamhyung Kim 
756f048d548SNamhyung Kim 	return srcline;
7572cc9d0efSNamhyung Kim 
7582cc9d0efSNamhyung Kim out:
759906049c8SAdrian Hunter 	if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
7602cc9d0efSNamhyung Kim 		dso->has_srcline = 0;
761454ff00fSAdrian Hunter 		dso__free_a2l(dso);
762906049c8SAdrian Hunter 	}
7635dfa210eSMilian Wolff 
7645dfa210eSMilian Wolff 	if (!show_addr)
7655dfa210eSMilian Wolff 		return (show_sym && sym) ?
766ea335ef3SNamhyung Kim 			    strndup(sym->name, sym->namelen) : SRCLINE_UNKNOWN;
7675dfa210eSMilian Wolff 
76885c116a6SAndi Kleen 	if (sym) {
769ac931f87SWang Nan 		if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
770935f5a9dSJin Yao 					ip - sym->start) < 0)
77185c116a6SAndi Kleen 			return SRCLINE_UNKNOWN;
772ac931f87SWang Nan 	} else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
7732cc9d0efSNamhyung Kim 		return SRCLINE_UNKNOWN;
77423f0981bSAndi Kleen 	return srcline;
775f048d548SNamhyung Kim }
776f048d548SNamhyung Kim 
777dd2e18e9SAndi Kleen /* Returns filename and fills in line number in line */
778dd2e18e9SAndi Kleen char *get_srcline_split(struct dso *dso, u64 addr, unsigned *line)
779dd2e18e9SAndi Kleen {
780dd2e18e9SAndi Kleen 	char *file = NULL;
781dd2e18e9SAndi Kleen 	const char *dso_name;
782dd2e18e9SAndi Kleen 
783dd2e18e9SAndi Kleen 	if (!dso->has_srcline)
784dd2e18e9SAndi Kleen 		goto out;
785dd2e18e9SAndi Kleen 
786dd2e18e9SAndi Kleen 	dso_name = dso__name(dso);
787dd2e18e9SAndi Kleen 	if (dso_name == NULL)
788dd2e18e9SAndi Kleen 		goto out;
789dd2e18e9SAndi Kleen 
790dd2e18e9SAndi Kleen 	if (!addr2line(dso_name, addr, &file, line, dso, true, NULL, NULL))
791dd2e18e9SAndi Kleen 		goto out;
792dd2e18e9SAndi Kleen 
793dd2e18e9SAndi Kleen 	dso->a2l_fails = 0;
794dd2e18e9SAndi Kleen 	return file;
795dd2e18e9SAndi Kleen 
796dd2e18e9SAndi Kleen out:
797dd2e18e9SAndi Kleen 	if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
798dd2e18e9SAndi Kleen 		dso->has_srcline = 0;
799dd2e18e9SAndi Kleen 		dso__free_a2l(dso);
800dd2e18e9SAndi Kleen 	}
801dd2e18e9SAndi Kleen 
802dd2e18e9SAndi Kleen 	return NULL;
803dd2e18e9SAndi Kleen }
804dd2e18e9SAndi Kleen 
805f048d548SNamhyung Kim void free_srcline(char *srcline)
806f048d548SNamhyung Kim {
807f048d548SNamhyung Kim 	if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
808f048d548SNamhyung Kim 		free(srcline);
809f048d548SNamhyung Kim }
8102f84b42bSAndi Kleen 
8112f84b42bSAndi Kleen char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
812935f5a9dSJin Yao 		  bool show_sym, bool show_addr, u64 ip)
8132f84b42bSAndi Kleen {
814935f5a9dSJin Yao 	return __get_srcline(dso, addr, sym, show_sym, show_addr, false, ip);
8152f84b42bSAndi Kleen }
816a64489c5SJin Yao 
81721ac9d54SMilian Wolff struct srcline_node {
81821ac9d54SMilian Wolff 	u64			addr;
81921ac9d54SMilian Wolff 	char			*srcline;
82021ac9d54SMilian Wolff 	struct rb_node		rb_node;
82121ac9d54SMilian Wolff };
82221ac9d54SMilian Wolff 
82355ecd631SDavidlohr Bueso void srcline__tree_insert(struct rb_root_cached *tree, u64 addr, char *srcline)
82421ac9d54SMilian Wolff {
82555ecd631SDavidlohr Bueso 	struct rb_node **p = &tree->rb_root.rb_node;
82621ac9d54SMilian Wolff 	struct rb_node *parent = NULL;
82721ac9d54SMilian Wolff 	struct srcline_node *i, *node;
82855ecd631SDavidlohr Bueso 	bool leftmost = true;
82921ac9d54SMilian Wolff 
83021ac9d54SMilian Wolff 	node = zalloc(sizeof(struct srcline_node));
83121ac9d54SMilian Wolff 	if (!node) {
83221ac9d54SMilian Wolff 		perror("not enough memory for the srcline node");
83321ac9d54SMilian Wolff 		return;
83421ac9d54SMilian Wolff 	}
83521ac9d54SMilian Wolff 
83621ac9d54SMilian Wolff 	node->addr = addr;
83721ac9d54SMilian Wolff 	node->srcline = srcline;
83821ac9d54SMilian Wolff 
83921ac9d54SMilian Wolff 	while (*p != NULL) {
84021ac9d54SMilian Wolff 		parent = *p;
84121ac9d54SMilian Wolff 		i = rb_entry(parent, struct srcline_node, rb_node);
84221ac9d54SMilian Wolff 		if (addr < i->addr)
84321ac9d54SMilian Wolff 			p = &(*p)->rb_left;
84455ecd631SDavidlohr Bueso 		else {
84521ac9d54SMilian Wolff 			p = &(*p)->rb_right;
84655ecd631SDavidlohr Bueso 			leftmost = false;
84755ecd631SDavidlohr Bueso 		}
84821ac9d54SMilian Wolff 	}
84921ac9d54SMilian Wolff 	rb_link_node(&node->rb_node, parent, p);
85055ecd631SDavidlohr Bueso 	rb_insert_color_cached(&node->rb_node, tree, leftmost);
85121ac9d54SMilian Wolff }
85221ac9d54SMilian Wolff 
85355ecd631SDavidlohr Bueso char *srcline__tree_find(struct rb_root_cached *tree, u64 addr)
85421ac9d54SMilian Wolff {
85555ecd631SDavidlohr Bueso 	struct rb_node *n = tree->rb_root.rb_node;
85621ac9d54SMilian Wolff 
85721ac9d54SMilian Wolff 	while (n) {
85821ac9d54SMilian Wolff 		struct srcline_node *i = rb_entry(n, struct srcline_node,
85921ac9d54SMilian Wolff 						  rb_node);
86021ac9d54SMilian Wolff 
86121ac9d54SMilian Wolff 		if (addr < i->addr)
86221ac9d54SMilian Wolff 			n = n->rb_left;
86321ac9d54SMilian Wolff 		else if (addr > i->addr)
86421ac9d54SMilian Wolff 			n = n->rb_right;
86521ac9d54SMilian Wolff 		else
86621ac9d54SMilian Wolff 			return i->srcline;
86721ac9d54SMilian Wolff 	}
86821ac9d54SMilian Wolff 
86921ac9d54SMilian Wolff 	return NULL;
87021ac9d54SMilian Wolff }
87121ac9d54SMilian Wolff 
87255ecd631SDavidlohr Bueso void srcline__tree_delete(struct rb_root_cached *tree)
87321ac9d54SMilian Wolff {
87421ac9d54SMilian Wolff 	struct srcline_node *pos;
87555ecd631SDavidlohr Bueso 	struct rb_node *next = rb_first_cached(tree);
87621ac9d54SMilian Wolff 
87721ac9d54SMilian Wolff 	while (next) {
87821ac9d54SMilian Wolff 		pos = rb_entry(next, struct srcline_node, rb_node);
87921ac9d54SMilian Wolff 		next = rb_next(&pos->rb_node);
88055ecd631SDavidlohr Bueso 		rb_erase_cached(&pos->rb_node, tree);
88121ac9d54SMilian Wolff 		free_srcline(pos->srcline);
88221ac9d54SMilian Wolff 		zfree(&pos);
88321ac9d54SMilian Wolff 	}
88421ac9d54SMilian Wolff }
88521ac9d54SMilian Wolff 
886fea0cf84SMilian Wolff struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr,
887fea0cf84SMilian Wolff 					    struct symbol *sym)
888a64489c5SJin Yao {
889a64489c5SJin Yao 	const char *dso_name;
890a64489c5SJin Yao 
891a64489c5SJin Yao 	dso_name = dso__name(dso);
892a64489c5SJin Yao 	if (dso_name == NULL)
893a64489c5SJin Yao 		return NULL;
894a64489c5SJin Yao 
895fea0cf84SMilian Wolff 	return addr2inlines(dso_name, addr, dso, sym);
896a64489c5SJin Yao }
897a64489c5SJin Yao 
898a64489c5SJin Yao void inline_node__delete(struct inline_node *node)
899a64489c5SJin Yao {
900a64489c5SJin Yao 	struct inline_list *ilist, *tmp;
901a64489c5SJin Yao 
902a64489c5SJin Yao 	list_for_each_entry_safe(ilist, tmp, &node->val, list) {
903a64489c5SJin Yao 		list_del_init(&ilist->list);
9042be8832fSMilian Wolff 		free_srcline(ilist->srcline);
905fea0cf84SMilian Wolff 		/* only the inlined symbols are owned by the list */
906fea0cf84SMilian Wolff 		if (ilist->symbol && ilist->symbol->inlined)
907fea0cf84SMilian Wolff 			symbol__delete(ilist->symbol);
908a64489c5SJin Yao 		free(ilist);
909a64489c5SJin Yao 	}
910a64489c5SJin Yao 
911a64489c5SJin Yao 	free(node);
912a64489c5SJin Yao }
91311ea2515SMilian Wolff 
91455ecd631SDavidlohr Bueso void inlines__tree_insert(struct rb_root_cached *tree,
91555ecd631SDavidlohr Bueso 			  struct inline_node *inlines)
91611ea2515SMilian Wolff {
91755ecd631SDavidlohr Bueso 	struct rb_node **p = &tree->rb_root.rb_node;
91811ea2515SMilian Wolff 	struct rb_node *parent = NULL;
91911ea2515SMilian Wolff 	const u64 addr = inlines->addr;
92011ea2515SMilian Wolff 	struct inline_node *i;
92155ecd631SDavidlohr Bueso 	bool leftmost = true;
92211ea2515SMilian Wolff 
92311ea2515SMilian Wolff 	while (*p != NULL) {
92411ea2515SMilian Wolff 		parent = *p;
92511ea2515SMilian Wolff 		i = rb_entry(parent, struct inline_node, rb_node);
92611ea2515SMilian Wolff 		if (addr < i->addr)
92711ea2515SMilian Wolff 			p = &(*p)->rb_left;
92855ecd631SDavidlohr Bueso 		else {
92911ea2515SMilian Wolff 			p = &(*p)->rb_right;
93055ecd631SDavidlohr Bueso 			leftmost = false;
93155ecd631SDavidlohr Bueso 		}
93211ea2515SMilian Wolff 	}
93311ea2515SMilian Wolff 	rb_link_node(&inlines->rb_node, parent, p);
93455ecd631SDavidlohr Bueso 	rb_insert_color_cached(&inlines->rb_node, tree, leftmost);
93511ea2515SMilian Wolff }
93611ea2515SMilian Wolff 
93755ecd631SDavidlohr Bueso struct inline_node *inlines__tree_find(struct rb_root_cached *tree, u64 addr)
93811ea2515SMilian Wolff {
93955ecd631SDavidlohr Bueso 	struct rb_node *n = tree->rb_root.rb_node;
94011ea2515SMilian Wolff 
94111ea2515SMilian Wolff 	while (n) {
94211ea2515SMilian Wolff 		struct inline_node *i = rb_entry(n, struct inline_node,
94311ea2515SMilian Wolff 						 rb_node);
94411ea2515SMilian Wolff 
94511ea2515SMilian Wolff 		if (addr < i->addr)
94611ea2515SMilian Wolff 			n = n->rb_left;
94711ea2515SMilian Wolff 		else if (addr > i->addr)
94811ea2515SMilian Wolff 			n = n->rb_right;
94911ea2515SMilian Wolff 		else
95011ea2515SMilian Wolff 			return i;
95111ea2515SMilian Wolff 	}
95211ea2515SMilian Wolff 
95311ea2515SMilian Wolff 	return NULL;
95411ea2515SMilian Wolff }
95511ea2515SMilian Wolff 
95655ecd631SDavidlohr Bueso void inlines__tree_delete(struct rb_root_cached *tree)
95711ea2515SMilian Wolff {
95811ea2515SMilian Wolff 	struct inline_node *pos;
95955ecd631SDavidlohr Bueso 	struct rb_node *next = rb_first_cached(tree);
96011ea2515SMilian Wolff 
96111ea2515SMilian Wolff 	while (next) {
96211ea2515SMilian Wolff 		pos = rb_entry(next, struct inline_node, rb_node);
96311ea2515SMilian Wolff 		next = rb_next(&pos->rb_node);
96455ecd631SDavidlohr Bueso 		rb_erase_cached(&pos->rb_node, tree);
96511ea2515SMilian Wolff 		inline_node__delete(pos);
96611ea2515SMilian Wolff 	}
96711ea2515SMilian Wolff }
968