xref: /openbmc/linux/tools/perf/util/srcline.c (revision ac931f87a647ca156f65a4c00e7297165e4fa2d8)
1f048d548SNamhyung Kim #include <stdio.h>
2f048d548SNamhyung Kim #include <stdlib.h>
3f048d548SNamhyung Kim #include <string.h>
4f048d548SNamhyung Kim 
5f048d548SNamhyung Kim #include <linux/kernel.h>
6f048d548SNamhyung Kim 
786c98cabSNamhyung Kim #include "util/dso.h"
8f048d548SNamhyung Kim #include "util/util.h"
9f048d548SNamhyung Kim #include "util/debug.h"
10f048d548SNamhyung Kim 
1185c116a6SAndi Kleen #include "symbol.h"
1285c116a6SAndi Kleen 
132f48fcd8SRoberto Vitillo #ifdef HAVE_LIBBFD_SUPPORT
142f48fcd8SRoberto Vitillo 
152f48fcd8SRoberto Vitillo /*
162f48fcd8SRoberto Vitillo  * Implement addr2line using libbfd.
172f48fcd8SRoberto Vitillo  */
182f48fcd8SRoberto Vitillo #define PACKAGE "perf"
192f48fcd8SRoberto Vitillo #include <bfd.h>
202f48fcd8SRoberto Vitillo 
212f48fcd8SRoberto Vitillo struct a2l_data {
222f48fcd8SRoberto Vitillo 	const char 	*input;
23*ac931f87SWang Nan 	u64	 	addr;
242f48fcd8SRoberto Vitillo 
252f48fcd8SRoberto Vitillo 	bool 		found;
262f48fcd8SRoberto Vitillo 	const char 	*filename;
272f48fcd8SRoberto Vitillo 	const char 	*funcname;
282f48fcd8SRoberto Vitillo 	unsigned 	line;
292f48fcd8SRoberto Vitillo 
302f48fcd8SRoberto Vitillo 	bfd 		*abfd;
312f48fcd8SRoberto Vitillo 	asymbol 	**syms;
322f48fcd8SRoberto Vitillo };
332f48fcd8SRoberto Vitillo 
342f48fcd8SRoberto Vitillo static int bfd_error(const char *string)
352f48fcd8SRoberto Vitillo {
362f48fcd8SRoberto Vitillo 	const char *errmsg;
372f48fcd8SRoberto Vitillo 
382f48fcd8SRoberto Vitillo 	errmsg = bfd_errmsg(bfd_get_error());
392f48fcd8SRoberto Vitillo 	fflush(stdout);
402f48fcd8SRoberto Vitillo 
412f48fcd8SRoberto Vitillo 	if (string)
422f48fcd8SRoberto Vitillo 		pr_debug("%s: %s\n", string, errmsg);
432f48fcd8SRoberto Vitillo 	else
442f48fcd8SRoberto Vitillo 		pr_debug("%s\n", errmsg);
452f48fcd8SRoberto Vitillo 
462f48fcd8SRoberto Vitillo 	return -1;
472f48fcd8SRoberto Vitillo }
482f48fcd8SRoberto Vitillo 
492f48fcd8SRoberto Vitillo static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
502f48fcd8SRoberto Vitillo {
512f48fcd8SRoberto Vitillo 	long storage;
522f48fcd8SRoberto Vitillo 	long symcount;
532f48fcd8SRoberto Vitillo 	asymbol **syms;
542f48fcd8SRoberto Vitillo 	bfd_boolean dynamic = FALSE;
552f48fcd8SRoberto Vitillo 
562f48fcd8SRoberto Vitillo 	if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
572f48fcd8SRoberto Vitillo 		return bfd_error(bfd_get_filename(abfd));
582f48fcd8SRoberto Vitillo 
592f48fcd8SRoberto Vitillo 	storage = bfd_get_symtab_upper_bound(abfd);
602f48fcd8SRoberto Vitillo 	if (storage == 0L) {
612f48fcd8SRoberto Vitillo 		storage = bfd_get_dynamic_symtab_upper_bound(abfd);
622f48fcd8SRoberto Vitillo 		dynamic = TRUE;
632f48fcd8SRoberto Vitillo 	}
642f48fcd8SRoberto Vitillo 	if (storage < 0L)
652f48fcd8SRoberto Vitillo 		return bfd_error(bfd_get_filename(abfd));
662f48fcd8SRoberto Vitillo 
672f48fcd8SRoberto Vitillo 	syms = malloc(storage);
682f48fcd8SRoberto Vitillo 	if (dynamic)
692f48fcd8SRoberto Vitillo 		symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
702f48fcd8SRoberto Vitillo 	else
712f48fcd8SRoberto Vitillo 		symcount = bfd_canonicalize_symtab(abfd, syms);
722f48fcd8SRoberto Vitillo 
732f48fcd8SRoberto Vitillo 	if (symcount < 0) {
742f48fcd8SRoberto Vitillo 		free(syms);
752f48fcd8SRoberto Vitillo 		return bfd_error(bfd_get_filename(abfd));
762f48fcd8SRoberto Vitillo 	}
772f48fcd8SRoberto Vitillo 
782f48fcd8SRoberto Vitillo 	a2l->syms = syms;
792f48fcd8SRoberto Vitillo 	return 0;
802f48fcd8SRoberto Vitillo }
812f48fcd8SRoberto Vitillo 
822f48fcd8SRoberto Vitillo static void find_address_in_section(bfd *abfd, asection *section, void *data)
832f48fcd8SRoberto Vitillo {
842f48fcd8SRoberto Vitillo 	bfd_vma pc, vma;
852f48fcd8SRoberto Vitillo 	bfd_size_type size;
862f48fcd8SRoberto Vitillo 	struct a2l_data *a2l = data;
872f48fcd8SRoberto Vitillo 
882f48fcd8SRoberto Vitillo 	if (a2l->found)
892f48fcd8SRoberto Vitillo 		return;
902f48fcd8SRoberto Vitillo 
912f48fcd8SRoberto Vitillo 	if ((bfd_get_section_flags(abfd, section) & SEC_ALLOC) == 0)
922f48fcd8SRoberto Vitillo 		return;
932f48fcd8SRoberto Vitillo 
942f48fcd8SRoberto Vitillo 	pc = a2l->addr;
952f48fcd8SRoberto Vitillo 	vma = bfd_get_section_vma(abfd, section);
962f48fcd8SRoberto Vitillo 	size = bfd_get_section_size(section);
972f48fcd8SRoberto Vitillo 
982f48fcd8SRoberto Vitillo 	if (pc < vma || pc >= vma + size)
992f48fcd8SRoberto Vitillo 		return;
1002f48fcd8SRoberto Vitillo 
1012f48fcd8SRoberto Vitillo 	a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
1022f48fcd8SRoberto Vitillo 					   &a2l->filename, &a2l->funcname,
1032f48fcd8SRoberto Vitillo 					   &a2l->line);
1042f48fcd8SRoberto Vitillo }
1052f48fcd8SRoberto Vitillo 
1062f48fcd8SRoberto Vitillo static struct a2l_data *addr2line_init(const char *path)
1072f48fcd8SRoberto Vitillo {
1082f48fcd8SRoberto Vitillo 	bfd *abfd;
1092f48fcd8SRoberto Vitillo 	struct a2l_data *a2l = NULL;
1102f48fcd8SRoberto Vitillo 
1112f48fcd8SRoberto Vitillo 	abfd = bfd_openr(path, NULL);
1122f48fcd8SRoberto Vitillo 	if (abfd == NULL)
1132f48fcd8SRoberto Vitillo 		return NULL;
1142f48fcd8SRoberto Vitillo 
1152f48fcd8SRoberto Vitillo 	if (!bfd_check_format(abfd, bfd_object))
1162f48fcd8SRoberto Vitillo 		goto out;
1172f48fcd8SRoberto Vitillo 
1182f48fcd8SRoberto Vitillo 	a2l = zalloc(sizeof(*a2l));
1192f48fcd8SRoberto Vitillo 	if (a2l == NULL)
1202f48fcd8SRoberto Vitillo 		goto out;
1212f48fcd8SRoberto Vitillo 
1222f48fcd8SRoberto Vitillo 	a2l->abfd = abfd;
1232f48fcd8SRoberto Vitillo 	a2l->input = strdup(path);
1242f48fcd8SRoberto Vitillo 	if (a2l->input == NULL)
1252f48fcd8SRoberto Vitillo 		goto out;
1262f48fcd8SRoberto Vitillo 
1272f48fcd8SRoberto Vitillo 	if (slurp_symtab(abfd, a2l))
1282f48fcd8SRoberto Vitillo 		goto out;
1292f48fcd8SRoberto Vitillo 
1302f48fcd8SRoberto Vitillo 	return a2l;
1312f48fcd8SRoberto Vitillo 
1322f48fcd8SRoberto Vitillo out:
1332f48fcd8SRoberto Vitillo 	if (a2l) {
1347d16c634SNamhyung Kim 		zfree((char **)&a2l->input);
1352f48fcd8SRoberto Vitillo 		free(a2l);
1362f48fcd8SRoberto Vitillo 	}
1372f48fcd8SRoberto Vitillo 	bfd_close(abfd);
1382f48fcd8SRoberto Vitillo 	return NULL;
1392f48fcd8SRoberto Vitillo }
1402f48fcd8SRoberto Vitillo 
1412f48fcd8SRoberto Vitillo static void addr2line_cleanup(struct a2l_data *a2l)
1422f48fcd8SRoberto Vitillo {
1432f48fcd8SRoberto Vitillo 	if (a2l->abfd)
1442f48fcd8SRoberto Vitillo 		bfd_close(a2l->abfd);
1457d16c634SNamhyung Kim 	zfree((char **)&a2l->input);
14674cf249dSArnaldo Carvalho de Melo 	zfree(&a2l->syms);
1472f48fcd8SRoberto Vitillo 	free(a2l);
1482f48fcd8SRoberto Vitillo }
1492f48fcd8SRoberto Vitillo 
150*ac931f87SWang Nan static int addr2line(const char *dso_name, u64 addr,
151454ff00fSAdrian Hunter 		     char **file, unsigned int *line, struct dso *dso)
1522f48fcd8SRoberto Vitillo {
1532f48fcd8SRoberto Vitillo 	int ret = 0;
154454ff00fSAdrian Hunter 	struct a2l_data *a2l = dso->a2l;
1552f48fcd8SRoberto Vitillo 
156454ff00fSAdrian Hunter 	if (!a2l) {
157454ff00fSAdrian Hunter 		dso->a2l = addr2line_init(dso_name);
158454ff00fSAdrian Hunter 		a2l = dso->a2l;
159454ff00fSAdrian Hunter 	}
160454ff00fSAdrian Hunter 
1612f48fcd8SRoberto Vitillo 	if (a2l == NULL) {
1622f48fcd8SRoberto Vitillo 		pr_warning("addr2line_init failed for %s\n", dso_name);
1632f48fcd8SRoberto Vitillo 		return 0;
1642f48fcd8SRoberto Vitillo 	}
1652f48fcd8SRoberto Vitillo 
1662f48fcd8SRoberto Vitillo 	a2l->addr = addr;
167454ff00fSAdrian Hunter 	a2l->found = false;
168454ff00fSAdrian Hunter 
1692f48fcd8SRoberto Vitillo 	bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
1702f48fcd8SRoberto Vitillo 
1712f48fcd8SRoberto Vitillo 	if (a2l->found && a2l->filename) {
1722f48fcd8SRoberto Vitillo 		*file = strdup(a2l->filename);
1732f48fcd8SRoberto Vitillo 		*line = a2l->line;
1742f48fcd8SRoberto Vitillo 
1752f48fcd8SRoberto Vitillo 		if (*file)
1762f48fcd8SRoberto Vitillo 			ret = 1;
1772f48fcd8SRoberto Vitillo 	}
1782f48fcd8SRoberto Vitillo 
1792f48fcd8SRoberto Vitillo 	return ret;
1802f48fcd8SRoberto Vitillo }
1812f48fcd8SRoberto Vitillo 
182454ff00fSAdrian Hunter void dso__free_a2l(struct dso *dso)
183454ff00fSAdrian Hunter {
184454ff00fSAdrian Hunter 	struct a2l_data *a2l = dso->a2l;
185454ff00fSAdrian Hunter 
186454ff00fSAdrian Hunter 	if (!a2l)
187454ff00fSAdrian Hunter 		return;
188454ff00fSAdrian Hunter 
189454ff00fSAdrian Hunter 	addr2line_cleanup(a2l);
190454ff00fSAdrian Hunter 
191454ff00fSAdrian Hunter 	dso->a2l = NULL;
192454ff00fSAdrian Hunter }
193454ff00fSAdrian Hunter 
1942f48fcd8SRoberto Vitillo #else /* HAVE_LIBBFD_SUPPORT */
1952f48fcd8SRoberto Vitillo 
196*ac931f87SWang Nan static int addr2line(const char *dso_name, u64 addr,
197454ff00fSAdrian Hunter 		     char **file, unsigned int *line_nr,
198454ff00fSAdrian Hunter 		     struct dso *dso __maybe_unused)
199f048d548SNamhyung Kim {
200f048d548SNamhyung Kim 	FILE *fp;
201f048d548SNamhyung Kim 	char cmd[PATH_MAX];
202f048d548SNamhyung Kim 	char *filename = NULL;
203f048d548SNamhyung Kim 	size_t len;
204f048d548SNamhyung Kim 	char *sep;
205f048d548SNamhyung Kim 	int ret = 0;
206f048d548SNamhyung Kim 
207f048d548SNamhyung Kim 	scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64,
208f048d548SNamhyung Kim 		  dso_name, addr);
209f048d548SNamhyung Kim 
210f048d548SNamhyung Kim 	fp = popen(cmd, "r");
211f048d548SNamhyung Kim 	if (fp == NULL) {
212f048d548SNamhyung Kim 		pr_warning("popen failed for %s\n", dso_name);
213f048d548SNamhyung Kim 		return 0;
214f048d548SNamhyung Kim 	}
215f048d548SNamhyung Kim 
216f048d548SNamhyung Kim 	if (getline(&filename, &len, fp) < 0 || !len) {
217f048d548SNamhyung Kim 		pr_warning("addr2line has no output for %s\n", dso_name);
218f048d548SNamhyung Kim 		goto out;
219f048d548SNamhyung Kim 	}
220f048d548SNamhyung Kim 
221f048d548SNamhyung Kim 	sep = strchr(filename, '\n');
222f048d548SNamhyung Kim 	if (sep)
223f048d548SNamhyung Kim 		*sep = '\0';
224f048d548SNamhyung Kim 
225f048d548SNamhyung Kim 	if (!strcmp(filename, "??:0")) {
226f048d548SNamhyung Kim 		pr_debug("no debugging info in %s\n", dso_name);
227f048d548SNamhyung Kim 		free(filename);
228f048d548SNamhyung Kim 		goto out;
229f048d548SNamhyung Kim 	}
230f048d548SNamhyung Kim 
231f048d548SNamhyung Kim 	sep = strchr(filename, ':');
232f048d548SNamhyung Kim 	if (sep) {
233f048d548SNamhyung Kim 		*sep++ = '\0';
234f048d548SNamhyung Kim 		*file = filename;
235f048d548SNamhyung Kim 		*line_nr = strtoul(sep, NULL, 0);
236f048d548SNamhyung Kim 		ret = 1;
237f048d548SNamhyung Kim 	}
238f048d548SNamhyung Kim out:
239f048d548SNamhyung Kim 	pclose(fp);
240f048d548SNamhyung Kim 	return ret;
241f048d548SNamhyung Kim }
242454ff00fSAdrian Hunter 
243454ff00fSAdrian Hunter void dso__free_a2l(struct dso *dso __maybe_unused)
244454ff00fSAdrian Hunter {
245454ff00fSAdrian Hunter }
246454ff00fSAdrian Hunter 
2472f48fcd8SRoberto Vitillo #endif /* HAVE_LIBBFD_SUPPORT */
248f048d548SNamhyung Kim 
249906049c8SAdrian Hunter /*
250906049c8SAdrian Hunter  * Number of addr2line failures (without success) before disabling it for that
251906049c8SAdrian Hunter  * dso.
252906049c8SAdrian Hunter  */
253906049c8SAdrian Hunter #define A2L_FAIL_LIMIT 123
254906049c8SAdrian Hunter 
255*ac931f87SWang Nan char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
25685c116a6SAndi Kleen 		  bool show_sym)
257f048d548SNamhyung Kim {
258a949fffbSDavid Ahern 	char *file = NULL;
259a949fffbSDavid Ahern 	unsigned line = 0;
2602cc9d0efSNamhyung Kim 	char *srcline;
261bf4414aeSArnaldo Carvalho de Melo 	const char *dso_name;
262f048d548SNamhyung Kim 
2632cc9d0efSNamhyung Kim 	if (!dso->has_srcline)
26423f0981bSAndi Kleen 		goto out;
2652cc9d0efSNamhyung Kim 
2660058aef6SAdrian Hunter 	if (dso->symsrc_filename)
2670058aef6SAdrian Hunter 		dso_name = dso->symsrc_filename;
2680058aef6SAdrian Hunter 	else
2690058aef6SAdrian Hunter 		dso_name = dso->long_name;
2700058aef6SAdrian Hunter 
27158d91a00SNamhyung Kim 	if (dso_name[0] == '[')
27258d91a00SNamhyung Kim 		goto out;
27358d91a00SNamhyung Kim 
27458d91a00SNamhyung Kim 	if (!strncmp(dso_name, "/tmp/perf-", 10))
27558d91a00SNamhyung Kim 		goto out;
27658d91a00SNamhyung Kim 
277454ff00fSAdrian Hunter 	if (!addr2line(dso_name, addr, &file, &line, dso))
27858d91a00SNamhyung Kim 		goto out;
279f048d548SNamhyung Kim 
2802de21768SAndi Kleen 	if (asprintf(&srcline, "%s:%u", basename(file), line) < 0) {
281906049c8SAdrian Hunter 		free(file);
282906049c8SAdrian Hunter 		goto out;
283906049c8SAdrian Hunter 	}
284906049c8SAdrian Hunter 
285906049c8SAdrian Hunter 	dso->a2l_fails = 0;
286f048d548SNamhyung Kim 
287f048d548SNamhyung Kim 	free(file);
288f048d548SNamhyung Kim 	return srcline;
2892cc9d0efSNamhyung Kim 
2902cc9d0efSNamhyung Kim out:
291906049c8SAdrian Hunter 	if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
2922cc9d0efSNamhyung Kim 		dso->has_srcline = 0;
293454ff00fSAdrian Hunter 		dso__free_a2l(dso);
294906049c8SAdrian Hunter 	}
29585c116a6SAndi Kleen 	if (sym) {
296*ac931f87SWang Nan 		if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
29785c116a6SAndi Kleen 					addr - sym->start) < 0)
29885c116a6SAndi Kleen 			return SRCLINE_UNKNOWN;
299*ac931f87SWang Nan 	} else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
3002cc9d0efSNamhyung Kim 		return SRCLINE_UNKNOWN;
30123f0981bSAndi Kleen 	return srcline;
302f048d548SNamhyung Kim }
303f048d548SNamhyung Kim 
304f048d548SNamhyung Kim void free_srcline(char *srcline)
305f048d548SNamhyung Kim {
306f048d548SNamhyung Kim 	if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
307f048d548SNamhyung Kim 		free(srcline);
308f048d548SNamhyung Kim }
309