xref: /openbmc/linux/tools/perf/util/srcline.c (revision b21cc978)
1fd20e811SArnaldo Carvalho de Melo #include <inttypes.h>
2f048d548SNamhyung Kim #include <stdio.h>
3f048d548SNamhyung Kim #include <stdlib.h>
4f048d548SNamhyung Kim #include <string.h>
5f048d548SNamhyung Kim 
6f048d548SNamhyung Kim #include <linux/kernel.h>
7f048d548SNamhyung Kim 
886c98cabSNamhyung Kim #include "util/dso.h"
9f048d548SNamhyung Kim #include "util/util.h"
10f048d548SNamhyung Kim #include "util/debug.h"
11a64489c5SJin Yao #include "util/callchain.h"
12632a5cabSArnaldo Carvalho de Melo #include "srcline.h"
13f048d548SNamhyung Kim 
1485c116a6SAndi Kleen #include "symbol.h"
1585c116a6SAndi Kleen 
16a9710ba0SAndi Kleen bool srcline_full_filename;
17a9710ba0SAndi Kleen 
185580338dSJin Yao static const char *dso__name(struct dso *dso)
195580338dSJin Yao {
205580338dSJin Yao 	const char *dso_name;
215580338dSJin Yao 
225580338dSJin Yao 	if (dso->symsrc_filename)
235580338dSJin Yao 		dso_name = dso->symsrc_filename;
245580338dSJin Yao 	else
255580338dSJin Yao 		dso_name = dso->long_name;
265580338dSJin Yao 
275580338dSJin Yao 	if (dso_name[0] == '[')
285580338dSJin Yao 		return NULL;
295580338dSJin Yao 
305580338dSJin Yao 	if (!strncmp(dso_name, "/tmp/perf-", 10))
315580338dSJin Yao 		return NULL;
325580338dSJin Yao 
335580338dSJin Yao 	return dso_name;
345580338dSJin Yao }
355580338dSJin Yao 
36a64489c5SJin Yao static int inline_list__append(char *filename, char *funcname, int line_nr,
37a64489c5SJin Yao 			       struct inline_node *node, struct dso *dso)
38a64489c5SJin Yao {
39a64489c5SJin Yao 	struct inline_list *ilist;
40a64489c5SJin Yao 	char *demangled;
41a64489c5SJin Yao 
42a64489c5SJin Yao 	ilist = zalloc(sizeof(*ilist));
43a64489c5SJin Yao 	if (ilist == NULL)
44a64489c5SJin Yao 		return -1;
45a64489c5SJin Yao 
46a64489c5SJin Yao 	ilist->filename = filename;
47a64489c5SJin Yao 	ilist->line_nr = line_nr;
48a64489c5SJin Yao 
49a64489c5SJin Yao 	if (dso != NULL) {
50a64489c5SJin Yao 		demangled = dso__demangle_sym(dso, 0, funcname);
51a64489c5SJin Yao 		if (demangled == NULL) {
52a64489c5SJin Yao 			ilist->funcname = funcname;
53a64489c5SJin Yao 		} else {
54a64489c5SJin Yao 			ilist->funcname = demangled;
55a64489c5SJin Yao 			free(funcname);
56a64489c5SJin Yao 		}
57a64489c5SJin Yao 	}
58a64489c5SJin Yao 
59a64489c5SJin Yao 	list_add_tail(&ilist->list, &node->val);
60a64489c5SJin Yao 
61a64489c5SJin Yao 	return 0;
62a64489c5SJin Yao }
63a64489c5SJin Yao 
642f48fcd8SRoberto Vitillo #ifdef HAVE_LIBBFD_SUPPORT
652f48fcd8SRoberto Vitillo 
662f48fcd8SRoberto Vitillo /*
672f48fcd8SRoberto Vitillo  * Implement addr2line using libbfd.
682f48fcd8SRoberto Vitillo  */
692f48fcd8SRoberto Vitillo #define PACKAGE "perf"
702f48fcd8SRoberto Vitillo #include <bfd.h>
712f48fcd8SRoberto Vitillo 
722f48fcd8SRoberto Vitillo struct a2l_data {
732f48fcd8SRoberto Vitillo 	const char 	*input;
74ac931f87SWang Nan 	u64	 	addr;
752f48fcd8SRoberto Vitillo 
762f48fcd8SRoberto Vitillo 	bool 		found;
772f48fcd8SRoberto Vitillo 	const char 	*filename;
782f48fcd8SRoberto Vitillo 	const char 	*funcname;
792f48fcd8SRoberto Vitillo 	unsigned 	line;
802f48fcd8SRoberto Vitillo 
812f48fcd8SRoberto Vitillo 	bfd 		*abfd;
822f48fcd8SRoberto Vitillo 	asymbol 	**syms;
832f48fcd8SRoberto Vitillo };
842f48fcd8SRoberto Vitillo 
852f48fcd8SRoberto Vitillo static int bfd_error(const char *string)
862f48fcd8SRoberto Vitillo {
872f48fcd8SRoberto Vitillo 	const char *errmsg;
882f48fcd8SRoberto Vitillo 
892f48fcd8SRoberto Vitillo 	errmsg = bfd_errmsg(bfd_get_error());
902f48fcd8SRoberto Vitillo 	fflush(stdout);
912f48fcd8SRoberto Vitillo 
922f48fcd8SRoberto Vitillo 	if (string)
932f48fcd8SRoberto Vitillo 		pr_debug("%s: %s\n", string, errmsg);
942f48fcd8SRoberto Vitillo 	else
952f48fcd8SRoberto Vitillo 		pr_debug("%s\n", errmsg);
962f48fcd8SRoberto Vitillo 
972f48fcd8SRoberto Vitillo 	return -1;
982f48fcd8SRoberto Vitillo }
992f48fcd8SRoberto Vitillo 
1002f48fcd8SRoberto Vitillo static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
1012f48fcd8SRoberto Vitillo {
1022f48fcd8SRoberto Vitillo 	long storage;
1032f48fcd8SRoberto Vitillo 	long symcount;
1042f48fcd8SRoberto Vitillo 	asymbol **syms;
1052f48fcd8SRoberto Vitillo 	bfd_boolean dynamic = FALSE;
1062f48fcd8SRoberto Vitillo 
1072f48fcd8SRoberto Vitillo 	if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
1082f48fcd8SRoberto Vitillo 		return bfd_error(bfd_get_filename(abfd));
1092f48fcd8SRoberto Vitillo 
1102f48fcd8SRoberto Vitillo 	storage = bfd_get_symtab_upper_bound(abfd);
1112f48fcd8SRoberto Vitillo 	if (storage == 0L) {
1122f48fcd8SRoberto Vitillo 		storage = bfd_get_dynamic_symtab_upper_bound(abfd);
1132f48fcd8SRoberto Vitillo 		dynamic = TRUE;
1142f48fcd8SRoberto Vitillo 	}
1152f48fcd8SRoberto Vitillo 	if (storage < 0L)
1162f48fcd8SRoberto Vitillo 		return bfd_error(bfd_get_filename(abfd));
1172f48fcd8SRoberto Vitillo 
1182f48fcd8SRoberto Vitillo 	syms = malloc(storage);
1192f48fcd8SRoberto Vitillo 	if (dynamic)
1202f48fcd8SRoberto Vitillo 		symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
1212f48fcd8SRoberto Vitillo 	else
1222f48fcd8SRoberto Vitillo 		symcount = bfd_canonicalize_symtab(abfd, syms);
1232f48fcd8SRoberto Vitillo 
1242f48fcd8SRoberto Vitillo 	if (symcount < 0) {
1252f48fcd8SRoberto Vitillo 		free(syms);
1262f48fcd8SRoberto Vitillo 		return bfd_error(bfd_get_filename(abfd));
1272f48fcd8SRoberto Vitillo 	}
1282f48fcd8SRoberto Vitillo 
1292f48fcd8SRoberto Vitillo 	a2l->syms = syms;
1302f48fcd8SRoberto Vitillo 	return 0;
1312f48fcd8SRoberto Vitillo }
1322f48fcd8SRoberto Vitillo 
1332f48fcd8SRoberto Vitillo static void find_address_in_section(bfd *abfd, asection *section, void *data)
1342f48fcd8SRoberto Vitillo {
1352f48fcd8SRoberto Vitillo 	bfd_vma pc, vma;
1362f48fcd8SRoberto Vitillo 	bfd_size_type size;
1372f48fcd8SRoberto Vitillo 	struct a2l_data *a2l = data;
1382f48fcd8SRoberto Vitillo 
1392f48fcd8SRoberto Vitillo 	if (a2l->found)
1402f48fcd8SRoberto Vitillo 		return;
1412f48fcd8SRoberto Vitillo 
1422f48fcd8SRoberto Vitillo 	if ((bfd_get_section_flags(abfd, section) & SEC_ALLOC) == 0)
1432f48fcd8SRoberto Vitillo 		return;
1442f48fcd8SRoberto Vitillo 
1452f48fcd8SRoberto Vitillo 	pc = a2l->addr;
1462f48fcd8SRoberto Vitillo 	vma = bfd_get_section_vma(abfd, section);
1472f48fcd8SRoberto Vitillo 	size = bfd_get_section_size(section);
1482f48fcd8SRoberto Vitillo 
1492f48fcd8SRoberto Vitillo 	if (pc < vma || pc >= vma + size)
1502f48fcd8SRoberto Vitillo 		return;
1512f48fcd8SRoberto Vitillo 
1522f48fcd8SRoberto Vitillo 	a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
1532f48fcd8SRoberto Vitillo 					   &a2l->filename, &a2l->funcname,
1542f48fcd8SRoberto Vitillo 					   &a2l->line);
1552f48fcd8SRoberto Vitillo }
1562f48fcd8SRoberto Vitillo 
1572f48fcd8SRoberto Vitillo static struct a2l_data *addr2line_init(const char *path)
1582f48fcd8SRoberto Vitillo {
1592f48fcd8SRoberto Vitillo 	bfd *abfd;
1602f48fcd8SRoberto Vitillo 	struct a2l_data *a2l = NULL;
1612f48fcd8SRoberto Vitillo 
1622f48fcd8SRoberto Vitillo 	abfd = bfd_openr(path, NULL);
1632f48fcd8SRoberto Vitillo 	if (abfd == NULL)
1642f48fcd8SRoberto Vitillo 		return NULL;
1652f48fcd8SRoberto Vitillo 
1662f48fcd8SRoberto Vitillo 	if (!bfd_check_format(abfd, bfd_object))
1672f48fcd8SRoberto Vitillo 		goto out;
1682f48fcd8SRoberto Vitillo 
1692f48fcd8SRoberto Vitillo 	a2l = zalloc(sizeof(*a2l));
1702f48fcd8SRoberto Vitillo 	if (a2l == NULL)
1712f48fcd8SRoberto Vitillo 		goto out;
1722f48fcd8SRoberto Vitillo 
1732f48fcd8SRoberto Vitillo 	a2l->abfd = abfd;
1742f48fcd8SRoberto Vitillo 	a2l->input = strdup(path);
1752f48fcd8SRoberto Vitillo 	if (a2l->input == NULL)
1762f48fcd8SRoberto Vitillo 		goto out;
1772f48fcd8SRoberto Vitillo 
1782f48fcd8SRoberto Vitillo 	if (slurp_symtab(abfd, a2l))
1792f48fcd8SRoberto Vitillo 		goto out;
1802f48fcd8SRoberto Vitillo 
1812f48fcd8SRoberto Vitillo 	return a2l;
1822f48fcd8SRoberto Vitillo 
1832f48fcd8SRoberto Vitillo out:
1842f48fcd8SRoberto Vitillo 	if (a2l) {
1857d16c634SNamhyung Kim 		zfree((char **)&a2l->input);
1862f48fcd8SRoberto Vitillo 		free(a2l);
1872f48fcd8SRoberto Vitillo 	}
1882f48fcd8SRoberto Vitillo 	bfd_close(abfd);
1892f48fcd8SRoberto Vitillo 	return NULL;
1902f48fcd8SRoberto Vitillo }
1912f48fcd8SRoberto Vitillo 
1922f48fcd8SRoberto Vitillo static void addr2line_cleanup(struct a2l_data *a2l)
1932f48fcd8SRoberto Vitillo {
1942f48fcd8SRoberto Vitillo 	if (a2l->abfd)
1952f48fcd8SRoberto Vitillo 		bfd_close(a2l->abfd);
1967d16c634SNamhyung Kim 	zfree((char **)&a2l->input);
19774cf249dSArnaldo Carvalho de Melo 	zfree(&a2l->syms);
1982f48fcd8SRoberto Vitillo 	free(a2l);
1992f48fcd8SRoberto Vitillo }
2002f48fcd8SRoberto Vitillo 
2012f84b42bSAndi Kleen #define MAX_INLINE_NEST 1024
2022f84b42bSAndi Kleen 
203a64489c5SJin Yao static void inline_list__reverse(struct inline_node *node)
204a64489c5SJin Yao {
205a64489c5SJin Yao 	struct inline_list *ilist, *n;
206a64489c5SJin Yao 
207a64489c5SJin Yao 	list_for_each_entry_safe_reverse(ilist, n, &node->val, list)
208a64489c5SJin Yao 		list_move_tail(&ilist->list, &node->val);
209a64489c5SJin Yao }
210a64489c5SJin Yao 
211ac931f87SWang Nan static int addr2line(const char *dso_name, u64 addr,
2122f84b42bSAndi Kleen 		     char **file, unsigned int *line, struct dso *dso,
213a64489c5SJin Yao 		     bool unwind_inlines, struct inline_node *node)
2142f48fcd8SRoberto Vitillo {
2152f48fcd8SRoberto Vitillo 	int ret = 0;
216454ff00fSAdrian Hunter 	struct a2l_data *a2l = dso->a2l;
2172f48fcd8SRoberto Vitillo 
218454ff00fSAdrian Hunter 	if (!a2l) {
219454ff00fSAdrian Hunter 		dso->a2l = addr2line_init(dso_name);
220454ff00fSAdrian Hunter 		a2l = dso->a2l;
221454ff00fSAdrian Hunter 	}
222454ff00fSAdrian Hunter 
2232f48fcd8SRoberto Vitillo 	if (a2l == NULL) {
2242f48fcd8SRoberto Vitillo 		pr_warning("addr2line_init failed for %s\n", dso_name);
2252f48fcd8SRoberto Vitillo 		return 0;
2262f48fcd8SRoberto Vitillo 	}
2272f48fcd8SRoberto Vitillo 
2282f48fcd8SRoberto Vitillo 	a2l->addr = addr;
229454ff00fSAdrian Hunter 	a2l->found = false;
230454ff00fSAdrian Hunter 
2312f48fcd8SRoberto Vitillo 	bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
2322f48fcd8SRoberto Vitillo 
233b21cc978SMilian Wolff 	if (!a2l->found)
234b21cc978SMilian Wolff 		return 0;
235b21cc978SMilian Wolff 
236b21cc978SMilian Wolff 	if (unwind_inlines) {
2372f84b42bSAndi Kleen 		int cnt = 0;
2382f84b42bSAndi Kleen 
2392f84b42bSAndi Kleen 		while (bfd_find_inliner_info(a2l->abfd, &a2l->filename,
2402f84b42bSAndi Kleen 					     &a2l->funcname, &a2l->line) &&
241a64489c5SJin Yao 		       cnt++ < MAX_INLINE_NEST) {
242a64489c5SJin Yao 
243a64489c5SJin Yao 			if (node != NULL) {
244a64489c5SJin Yao 				if (inline_list__append(strdup(a2l->filename),
245a64489c5SJin Yao 							strdup(a2l->funcname),
246a64489c5SJin Yao 							a2l->line, node,
247a64489c5SJin Yao 							dso) != 0)
248a64489c5SJin Yao 					return 0;
249b21cc978SMilian Wolff 				// found at least one inline frame
250b21cc978SMilian Wolff 				ret = 1;
251a64489c5SJin Yao 			}
252a64489c5SJin Yao 		}
253a64489c5SJin Yao 
254a64489c5SJin Yao 		if ((node != NULL) &&
255a64489c5SJin Yao 		    (callchain_param.order != ORDER_CALLEE)) {
256a64489c5SJin Yao 			inline_list__reverse(node);
257a64489c5SJin Yao 		}
2582f84b42bSAndi Kleen 	}
2592f84b42bSAndi Kleen 
260b21cc978SMilian Wolff 	if (file) {
261b21cc978SMilian Wolff 		*file = a2l->filename ? strdup(a2l->filename) : NULL;
262b21cc978SMilian Wolff 		ret = *file ? 1 : 0;
2632f48fcd8SRoberto Vitillo 	}
2642f48fcd8SRoberto Vitillo 
265b21cc978SMilian Wolff 	if (line)
266b21cc978SMilian Wolff 		*line = a2l->line;
267b21cc978SMilian Wolff 
2682f48fcd8SRoberto Vitillo 	return ret;
2692f48fcd8SRoberto Vitillo }
2702f48fcd8SRoberto Vitillo 
271454ff00fSAdrian Hunter void dso__free_a2l(struct dso *dso)
272454ff00fSAdrian Hunter {
273454ff00fSAdrian Hunter 	struct a2l_data *a2l = dso->a2l;
274454ff00fSAdrian Hunter 
275454ff00fSAdrian Hunter 	if (!a2l)
276454ff00fSAdrian Hunter 		return;
277454ff00fSAdrian Hunter 
278454ff00fSAdrian Hunter 	addr2line_cleanup(a2l);
279454ff00fSAdrian Hunter 
280454ff00fSAdrian Hunter 	dso->a2l = NULL;
281454ff00fSAdrian Hunter }
282454ff00fSAdrian Hunter 
283a64489c5SJin Yao static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
284a64489c5SJin Yao 	struct dso *dso)
285a64489c5SJin Yao {
286a64489c5SJin Yao 	struct inline_node *node;
287a64489c5SJin Yao 
288a64489c5SJin Yao 	node = zalloc(sizeof(*node));
289a64489c5SJin Yao 	if (node == NULL) {
290a64489c5SJin Yao 		perror("not enough memory for the inline node");
291a64489c5SJin Yao 		return NULL;
292a64489c5SJin Yao 	}
293a64489c5SJin Yao 
294a64489c5SJin Yao 	INIT_LIST_HEAD(&node->val);
295a64489c5SJin Yao 	node->addr = addr;
296a64489c5SJin Yao 
297b21cc978SMilian Wolff 	if (!addr2line(dso_name, addr, NULL, NULL, dso, TRUE, node))
298a64489c5SJin Yao 		goto out_free_inline_node;
299a64489c5SJin Yao 
300a64489c5SJin Yao 	if (list_empty(&node->val))
301a64489c5SJin Yao 		goto out_free_inline_node;
302a64489c5SJin Yao 
303a64489c5SJin Yao 	return node;
304a64489c5SJin Yao 
305a64489c5SJin Yao out_free_inline_node:
306a64489c5SJin Yao 	inline_node__delete(node);
307a64489c5SJin Yao 	return NULL;
308a64489c5SJin Yao }
309a64489c5SJin Yao 
3102f48fcd8SRoberto Vitillo #else /* HAVE_LIBBFD_SUPPORT */
3112f48fcd8SRoberto Vitillo 
3125580338dSJin Yao static int filename_split(char *filename, unsigned int *line_nr)
3135580338dSJin Yao {
3145580338dSJin Yao 	char *sep;
3155580338dSJin Yao 
3165580338dSJin Yao 	sep = strchr(filename, '\n');
3175580338dSJin Yao 	if (sep)
3185580338dSJin Yao 		*sep = '\0';
3195580338dSJin Yao 
3205580338dSJin Yao 	if (!strcmp(filename, "??:0"))
3215580338dSJin Yao 		return 0;
3225580338dSJin Yao 
3235580338dSJin Yao 	sep = strchr(filename, ':');
3245580338dSJin Yao 	if (sep) {
3255580338dSJin Yao 		*sep++ = '\0';
3265580338dSJin Yao 		*line_nr = strtoul(sep, NULL, 0);
3275580338dSJin Yao 		return 1;
3285580338dSJin Yao 	}
3295580338dSJin Yao 
3305580338dSJin Yao 	return 0;
3315580338dSJin Yao }
3325580338dSJin Yao 
333ac931f87SWang Nan static int addr2line(const char *dso_name, u64 addr,
334454ff00fSAdrian Hunter 		     char **file, unsigned int *line_nr,
3352f84b42bSAndi Kleen 		     struct dso *dso __maybe_unused,
336a64489c5SJin Yao 		     bool unwind_inlines __maybe_unused,
337a64489c5SJin Yao 		     struct inline_node *node __maybe_unused)
338f048d548SNamhyung Kim {
339f048d548SNamhyung Kim 	FILE *fp;
340f048d548SNamhyung Kim 	char cmd[PATH_MAX];
341f048d548SNamhyung Kim 	char *filename = NULL;
342f048d548SNamhyung Kim 	size_t len;
343f048d548SNamhyung Kim 	int ret = 0;
344f048d548SNamhyung Kim 
345f048d548SNamhyung Kim 	scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64,
346f048d548SNamhyung Kim 		  dso_name, addr);
347f048d548SNamhyung Kim 
348f048d548SNamhyung Kim 	fp = popen(cmd, "r");
349f048d548SNamhyung Kim 	if (fp == NULL) {
350f048d548SNamhyung Kim 		pr_warning("popen failed for %s\n", dso_name);
351f048d548SNamhyung Kim 		return 0;
352f048d548SNamhyung Kim 	}
353f048d548SNamhyung Kim 
354f048d548SNamhyung Kim 	if (getline(&filename, &len, fp) < 0 || !len) {
355f048d548SNamhyung Kim 		pr_warning("addr2line has no output for %s\n", dso_name);
356f048d548SNamhyung Kim 		goto out;
357f048d548SNamhyung Kim 	}
358f048d548SNamhyung Kim 
3595580338dSJin Yao 	ret = filename_split(filename, line_nr);
3605580338dSJin Yao 	if (ret != 1) {
361f048d548SNamhyung Kim 		free(filename);
362f048d548SNamhyung Kim 		goto out;
363f048d548SNamhyung Kim 	}
364f048d548SNamhyung Kim 
365f048d548SNamhyung Kim 	*file = filename;
3665580338dSJin Yao 
367f048d548SNamhyung Kim out:
368f048d548SNamhyung Kim 	pclose(fp);
369f048d548SNamhyung Kim 	return ret;
370f048d548SNamhyung Kim }
371454ff00fSAdrian Hunter 
372454ff00fSAdrian Hunter void dso__free_a2l(struct dso *dso __maybe_unused)
373454ff00fSAdrian Hunter {
374454ff00fSAdrian Hunter }
375454ff00fSAdrian Hunter 
376a64489c5SJin Yao static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
377a64489c5SJin Yao 	struct dso *dso __maybe_unused)
378a64489c5SJin Yao {
379a64489c5SJin Yao 	FILE *fp;
380a64489c5SJin Yao 	char cmd[PATH_MAX];
381a64489c5SJin Yao 	struct inline_node *node;
382a64489c5SJin Yao 	char *filename = NULL;
383a64489c5SJin Yao 	size_t len;
384a64489c5SJin Yao 	unsigned int line_nr = 0;
385a64489c5SJin Yao 
386a64489c5SJin Yao 	scnprintf(cmd, sizeof(cmd), "addr2line -e %s -i %016"PRIx64,
387a64489c5SJin Yao 		  dso_name, addr);
388a64489c5SJin Yao 
389a64489c5SJin Yao 	fp = popen(cmd, "r");
390a64489c5SJin Yao 	if (fp == NULL) {
391a64489c5SJin Yao 		pr_err("popen failed for %s\n", dso_name);
392a64489c5SJin Yao 		return NULL;
393a64489c5SJin Yao 	}
394a64489c5SJin Yao 
395a64489c5SJin Yao 	node = zalloc(sizeof(*node));
396a64489c5SJin Yao 	if (node == NULL) {
397a64489c5SJin Yao 		perror("not enough memory for the inline node");
398a64489c5SJin Yao 		goto out;
399a64489c5SJin Yao 	}
400a64489c5SJin Yao 
401a64489c5SJin Yao 	INIT_LIST_HEAD(&node->val);
402a64489c5SJin Yao 	node->addr = addr;
403a64489c5SJin Yao 
404a64489c5SJin Yao 	while (getline(&filename, &len, fp) != -1) {
405a64489c5SJin Yao 		if (filename_split(filename, &line_nr) != 1) {
406a64489c5SJin Yao 			free(filename);
407a64489c5SJin Yao 			goto out;
408a64489c5SJin Yao 		}
409a64489c5SJin Yao 
410a64489c5SJin Yao 		if (inline_list__append(filename, NULL, line_nr, node,
411a64489c5SJin Yao 					NULL) != 0)
412a64489c5SJin Yao 			goto out;
413a64489c5SJin Yao 
414a64489c5SJin Yao 		filename = NULL;
415a64489c5SJin Yao 	}
416a64489c5SJin Yao 
417a64489c5SJin Yao out:
418a64489c5SJin Yao 	pclose(fp);
419a64489c5SJin Yao 
420a64489c5SJin Yao 	if (list_empty(&node->val)) {
421a64489c5SJin Yao 		inline_node__delete(node);
422a64489c5SJin Yao 		return NULL;
423a64489c5SJin Yao 	}
424a64489c5SJin Yao 
425a64489c5SJin Yao 	return node;
426a64489c5SJin Yao }
427a64489c5SJin Yao 
4282f48fcd8SRoberto Vitillo #endif /* HAVE_LIBBFD_SUPPORT */
429f048d548SNamhyung Kim 
430906049c8SAdrian Hunter /*
431906049c8SAdrian Hunter  * Number of addr2line failures (without success) before disabling it for that
432906049c8SAdrian Hunter  * dso.
433906049c8SAdrian Hunter  */
434906049c8SAdrian Hunter #define A2L_FAIL_LIMIT 123
435906049c8SAdrian Hunter 
4362f84b42bSAndi Kleen char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
4375dfa210eSMilian Wolff 		  bool show_sym, bool show_addr, bool unwind_inlines)
438f048d548SNamhyung Kim {
439a949fffbSDavid Ahern 	char *file = NULL;
440a949fffbSDavid Ahern 	unsigned line = 0;
4412cc9d0efSNamhyung Kim 	char *srcline;
442bf4414aeSArnaldo Carvalho de Melo 	const char *dso_name;
443f048d548SNamhyung Kim 
4442cc9d0efSNamhyung Kim 	if (!dso->has_srcline)
44523f0981bSAndi Kleen 		goto out;
4462cc9d0efSNamhyung Kim 
4475580338dSJin Yao 	dso_name = dso__name(dso);
4485580338dSJin Yao 	if (dso_name == NULL)
44958d91a00SNamhyung Kim 		goto out;
45058d91a00SNamhyung Kim 
451a64489c5SJin Yao 	if (!addr2line(dso_name, addr, &file, &line, dso, unwind_inlines, NULL))
45258d91a00SNamhyung Kim 		goto out;
453f048d548SNamhyung Kim 
454a9710ba0SAndi Kleen 	if (asprintf(&srcline, "%s:%u",
455a9710ba0SAndi Kleen 				srcline_full_filename ? file : basename(file),
456a9710ba0SAndi Kleen 				line) < 0) {
457906049c8SAdrian Hunter 		free(file);
458906049c8SAdrian Hunter 		goto out;
459906049c8SAdrian Hunter 	}
460906049c8SAdrian Hunter 
461906049c8SAdrian Hunter 	dso->a2l_fails = 0;
462f048d548SNamhyung Kim 
463f048d548SNamhyung Kim 	free(file);
464f048d548SNamhyung Kim 	return srcline;
4652cc9d0efSNamhyung Kim 
4662cc9d0efSNamhyung Kim out:
467906049c8SAdrian Hunter 	if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
4682cc9d0efSNamhyung Kim 		dso->has_srcline = 0;
469454ff00fSAdrian Hunter 		dso__free_a2l(dso);
470906049c8SAdrian Hunter 	}
4715dfa210eSMilian Wolff 
4725dfa210eSMilian Wolff 	if (!show_addr)
4735dfa210eSMilian Wolff 		return (show_sym && sym) ?
4745dfa210eSMilian Wolff 			    strndup(sym->name, sym->namelen) : NULL;
4755dfa210eSMilian Wolff 
47685c116a6SAndi Kleen 	if (sym) {
477ac931f87SWang Nan 		if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
47885c116a6SAndi Kleen 					addr - sym->start) < 0)
47985c116a6SAndi Kleen 			return SRCLINE_UNKNOWN;
480ac931f87SWang Nan 	} else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
4812cc9d0efSNamhyung Kim 		return SRCLINE_UNKNOWN;
48223f0981bSAndi Kleen 	return srcline;
483f048d548SNamhyung Kim }
484f048d548SNamhyung Kim 
485f048d548SNamhyung Kim void free_srcline(char *srcline)
486f048d548SNamhyung Kim {
487f048d548SNamhyung Kim 	if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
488f048d548SNamhyung Kim 		free(srcline);
489f048d548SNamhyung Kim }
4902f84b42bSAndi Kleen 
4912f84b42bSAndi Kleen char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
4925dfa210eSMilian Wolff 		  bool show_sym, bool show_addr)
4932f84b42bSAndi Kleen {
4945dfa210eSMilian Wolff 	return __get_srcline(dso, addr, sym, show_sym, show_addr, false);
4952f84b42bSAndi Kleen }
496a64489c5SJin Yao 
497a64489c5SJin Yao struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr)
498a64489c5SJin Yao {
499a64489c5SJin Yao 	const char *dso_name;
500a64489c5SJin Yao 
501a64489c5SJin Yao 	dso_name = dso__name(dso);
502a64489c5SJin Yao 	if (dso_name == NULL)
503a64489c5SJin Yao 		return NULL;
504a64489c5SJin Yao 
505a64489c5SJin Yao 	return addr2inlines(dso_name, addr, dso);
506a64489c5SJin Yao }
507a64489c5SJin Yao 
508a64489c5SJin Yao void inline_node__delete(struct inline_node *node)
509a64489c5SJin Yao {
510a64489c5SJin Yao 	struct inline_list *ilist, *tmp;
511a64489c5SJin Yao 
512a64489c5SJin Yao 	list_for_each_entry_safe(ilist, tmp, &node->val, list) {
513a64489c5SJin Yao 		list_del_init(&ilist->list);
514a64489c5SJin Yao 		zfree(&ilist->filename);
515a64489c5SJin Yao 		zfree(&ilist->funcname);
516a64489c5SJin Yao 		free(ilist);
517a64489c5SJin Yao 	}
518a64489c5SJin Yao 
519a64489c5SJin Yao 	free(node);
520a64489c5SJin Yao }
521