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" 10*a64489c5SJin Yao #include "util/callchain.h" 11f048d548SNamhyung Kim 1285c116a6SAndi Kleen #include "symbol.h" 1385c116a6SAndi Kleen 14a9710ba0SAndi Kleen bool srcline_full_filename; 15a9710ba0SAndi Kleen 165580338dSJin Yao static const char *dso__name(struct dso *dso) 175580338dSJin Yao { 185580338dSJin Yao const char *dso_name; 195580338dSJin Yao 205580338dSJin Yao if (dso->symsrc_filename) 215580338dSJin Yao dso_name = dso->symsrc_filename; 225580338dSJin Yao else 235580338dSJin Yao dso_name = dso->long_name; 245580338dSJin Yao 255580338dSJin Yao if (dso_name[0] == '[') 265580338dSJin Yao return NULL; 275580338dSJin Yao 285580338dSJin Yao if (!strncmp(dso_name, "/tmp/perf-", 10)) 295580338dSJin Yao return NULL; 305580338dSJin Yao 315580338dSJin Yao return dso_name; 325580338dSJin Yao } 335580338dSJin Yao 34*a64489c5SJin Yao static int inline_list__append(char *filename, char *funcname, int line_nr, 35*a64489c5SJin Yao struct inline_node *node, struct dso *dso) 36*a64489c5SJin Yao { 37*a64489c5SJin Yao struct inline_list *ilist; 38*a64489c5SJin Yao char *demangled; 39*a64489c5SJin Yao 40*a64489c5SJin Yao ilist = zalloc(sizeof(*ilist)); 41*a64489c5SJin Yao if (ilist == NULL) 42*a64489c5SJin Yao return -1; 43*a64489c5SJin Yao 44*a64489c5SJin Yao ilist->filename = filename; 45*a64489c5SJin Yao ilist->line_nr = line_nr; 46*a64489c5SJin Yao 47*a64489c5SJin Yao if (dso != NULL) { 48*a64489c5SJin Yao demangled = dso__demangle_sym(dso, 0, funcname); 49*a64489c5SJin Yao if (demangled == NULL) { 50*a64489c5SJin Yao ilist->funcname = funcname; 51*a64489c5SJin Yao } else { 52*a64489c5SJin Yao ilist->funcname = demangled; 53*a64489c5SJin Yao free(funcname); 54*a64489c5SJin Yao } 55*a64489c5SJin Yao } 56*a64489c5SJin Yao 57*a64489c5SJin Yao list_add_tail(&ilist->list, &node->val); 58*a64489c5SJin Yao 59*a64489c5SJin Yao return 0; 60*a64489c5SJin Yao } 61*a64489c5SJin Yao 622f48fcd8SRoberto Vitillo #ifdef HAVE_LIBBFD_SUPPORT 632f48fcd8SRoberto Vitillo 642f48fcd8SRoberto Vitillo /* 652f48fcd8SRoberto Vitillo * Implement addr2line using libbfd. 662f48fcd8SRoberto Vitillo */ 672f48fcd8SRoberto Vitillo #define PACKAGE "perf" 682f48fcd8SRoberto Vitillo #include <bfd.h> 692f48fcd8SRoberto Vitillo 702f48fcd8SRoberto Vitillo struct a2l_data { 712f48fcd8SRoberto Vitillo const char *input; 72ac931f87SWang Nan u64 addr; 732f48fcd8SRoberto Vitillo 742f48fcd8SRoberto Vitillo bool found; 752f48fcd8SRoberto Vitillo const char *filename; 762f48fcd8SRoberto Vitillo const char *funcname; 772f48fcd8SRoberto Vitillo unsigned line; 782f48fcd8SRoberto Vitillo 792f48fcd8SRoberto Vitillo bfd *abfd; 802f48fcd8SRoberto Vitillo asymbol **syms; 812f48fcd8SRoberto Vitillo }; 822f48fcd8SRoberto Vitillo 832f48fcd8SRoberto Vitillo static int bfd_error(const char *string) 842f48fcd8SRoberto Vitillo { 852f48fcd8SRoberto Vitillo const char *errmsg; 862f48fcd8SRoberto Vitillo 872f48fcd8SRoberto Vitillo errmsg = bfd_errmsg(bfd_get_error()); 882f48fcd8SRoberto Vitillo fflush(stdout); 892f48fcd8SRoberto Vitillo 902f48fcd8SRoberto Vitillo if (string) 912f48fcd8SRoberto Vitillo pr_debug("%s: %s\n", string, errmsg); 922f48fcd8SRoberto Vitillo else 932f48fcd8SRoberto Vitillo pr_debug("%s\n", errmsg); 942f48fcd8SRoberto Vitillo 952f48fcd8SRoberto Vitillo return -1; 962f48fcd8SRoberto Vitillo } 972f48fcd8SRoberto Vitillo 982f48fcd8SRoberto Vitillo static int slurp_symtab(bfd *abfd, struct a2l_data *a2l) 992f48fcd8SRoberto Vitillo { 1002f48fcd8SRoberto Vitillo long storage; 1012f48fcd8SRoberto Vitillo long symcount; 1022f48fcd8SRoberto Vitillo asymbol **syms; 1032f48fcd8SRoberto Vitillo bfd_boolean dynamic = FALSE; 1042f48fcd8SRoberto Vitillo 1052f48fcd8SRoberto Vitillo if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0) 1062f48fcd8SRoberto Vitillo return bfd_error(bfd_get_filename(abfd)); 1072f48fcd8SRoberto Vitillo 1082f48fcd8SRoberto Vitillo storage = bfd_get_symtab_upper_bound(abfd); 1092f48fcd8SRoberto Vitillo if (storage == 0L) { 1102f48fcd8SRoberto Vitillo storage = bfd_get_dynamic_symtab_upper_bound(abfd); 1112f48fcd8SRoberto Vitillo dynamic = TRUE; 1122f48fcd8SRoberto Vitillo } 1132f48fcd8SRoberto Vitillo if (storage < 0L) 1142f48fcd8SRoberto Vitillo return bfd_error(bfd_get_filename(abfd)); 1152f48fcd8SRoberto Vitillo 1162f48fcd8SRoberto Vitillo syms = malloc(storage); 1172f48fcd8SRoberto Vitillo if (dynamic) 1182f48fcd8SRoberto Vitillo symcount = bfd_canonicalize_dynamic_symtab(abfd, syms); 1192f48fcd8SRoberto Vitillo else 1202f48fcd8SRoberto Vitillo symcount = bfd_canonicalize_symtab(abfd, syms); 1212f48fcd8SRoberto Vitillo 1222f48fcd8SRoberto Vitillo if (symcount < 0) { 1232f48fcd8SRoberto Vitillo free(syms); 1242f48fcd8SRoberto Vitillo return bfd_error(bfd_get_filename(abfd)); 1252f48fcd8SRoberto Vitillo } 1262f48fcd8SRoberto Vitillo 1272f48fcd8SRoberto Vitillo a2l->syms = syms; 1282f48fcd8SRoberto Vitillo return 0; 1292f48fcd8SRoberto Vitillo } 1302f48fcd8SRoberto Vitillo 1312f48fcd8SRoberto Vitillo static void find_address_in_section(bfd *abfd, asection *section, void *data) 1322f48fcd8SRoberto Vitillo { 1332f48fcd8SRoberto Vitillo bfd_vma pc, vma; 1342f48fcd8SRoberto Vitillo bfd_size_type size; 1352f48fcd8SRoberto Vitillo struct a2l_data *a2l = data; 1362f48fcd8SRoberto Vitillo 1372f48fcd8SRoberto Vitillo if (a2l->found) 1382f48fcd8SRoberto Vitillo return; 1392f48fcd8SRoberto Vitillo 1402f48fcd8SRoberto Vitillo if ((bfd_get_section_flags(abfd, section) & SEC_ALLOC) == 0) 1412f48fcd8SRoberto Vitillo return; 1422f48fcd8SRoberto Vitillo 1432f48fcd8SRoberto Vitillo pc = a2l->addr; 1442f48fcd8SRoberto Vitillo vma = bfd_get_section_vma(abfd, section); 1452f48fcd8SRoberto Vitillo size = bfd_get_section_size(section); 1462f48fcd8SRoberto Vitillo 1472f48fcd8SRoberto Vitillo if (pc < vma || pc >= vma + size) 1482f48fcd8SRoberto Vitillo return; 1492f48fcd8SRoberto Vitillo 1502f48fcd8SRoberto Vitillo a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma, 1512f48fcd8SRoberto Vitillo &a2l->filename, &a2l->funcname, 1522f48fcd8SRoberto Vitillo &a2l->line); 1532f48fcd8SRoberto Vitillo } 1542f48fcd8SRoberto Vitillo 1552f48fcd8SRoberto Vitillo static struct a2l_data *addr2line_init(const char *path) 1562f48fcd8SRoberto Vitillo { 1572f48fcd8SRoberto Vitillo bfd *abfd; 1582f48fcd8SRoberto Vitillo struct a2l_data *a2l = NULL; 1592f48fcd8SRoberto Vitillo 1602f48fcd8SRoberto Vitillo abfd = bfd_openr(path, NULL); 1612f48fcd8SRoberto Vitillo if (abfd == NULL) 1622f48fcd8SRoberto Vitillo return NULL; 1632f48fcd8SRoberto Vitillo 1642f48fcd8SRoberto Vitillo if (!bfd_check_format(abfd, bfd_object)) 1652f48fcd8SRoberto Vitillo goto out; 1662f48fcd8SRoberto Vitillo 1672f48fcd8SRoberto Vitillo a2l = zalloc(sizeof(*a2l)); 1682f48fcd8SRoberto Vitillo if (a2l == NULL) 1692f48fcd8SRoberto Vitillo goto out; 1702f48fcd8SRoberto Vitillo 1712f48fcd8SRoberto Vitillo a2l->abfd = abfd; 1722f48fcd8SRoberto Vitillo a2l->input = strdup(path); 1732f48fcd8SRoberto Vitillo if (a2l->input == NULL) 1742f48fcd8SRoberto Vitillo goto out; 1752f48fcd8SRoberto Vitillo 1762f48fcd8SRoberto Vitillo if (slurp_symtab(abfd, a2l)) 1772f48fcd8SRoberto Vitillo goto out; 1782f48fcd8SRoberto Vitillo 1792f48fcd8SRoberto Vitillo return a2l; 1802f48fcd8SRoberto Vitillo 1812f48fcd8SRoberto Vitillo out: 1822f48fcd8SRoberto Vitillo if (a2l) { 1837d16c634SNamhyung Kim zfree((char **)&a2l->input); 1842f48fcd8SRoberto Vitillo free(a2l); 1852f48fcd8SRoberto Vitillo } 1862f48fcd8SRoberto Vitillo bfd_close(abfd); 1872f48fcd8SRoberto Vitillo return NULL; 1882f48fcd8SRoberto Vitillo } 1892f48fcd8SRoberto Vitillo 1902f48fcd8SRoberto Vitillo static void addr2line_cleanup(struct a2l_data *a2l) 1912f48fcd8SRoberto Vitillo { 1922f48fcd8SRoberto Vitillo if (a2l->abfd) 1932f48fcd8SRoberto Vitillo bfd_close(a2l->abfd); 1947d16c634SNamhyung Kim zfree((char **)&a2l->input); 19574cf249dSArnaldo Carvalho de Melo zfree(&a2l->syms); 1962f48fcd8SRoberto Vitillo free(a2l); 1972f48fcd8SRoberto Vitillo } 1982f48fcd8SRoberto Vitillo 1992f84b42bSAndi Kleen #define MAX_INLINE_NEST 1024 2002f84b42bSAndi Kleen 201*a64489c5SJin Yao static void inline_list__reverse(struct inline_node *node) 202*a64489c5SJin Yao { 203*a64489c5SJin Yao struct inline_list *ilist, *n; 204*a64489c5SJin Yao 205*a64489c5SJin Yao list_for_each_entry_safe_reverse(ilist, n, &node->val, list) 206*a64489c5SJin Yao list_move_tail(&ilist->list, &node->val); 207*a64489c5SJin Yao } 208*a64489c5SJin Yao 209ac931f87SWang Nan static int addr2line(const char *dso_name, u64 addr, 2102f84b42bSAndi Kleen char **file, unsigned int *line, struct dso *dso, 211*a64489c5SJin Yao bool unwind_inlines, struct inline_node *node) 2122f48fcd8SRoberto Vitillo { 2132f48fcd8SRoberto Vitillo int ret = 0; 214454ff00fSAdrian Hunter struct a2l_data *a2l = dso->a2l; 2152f48fcd8SRoberto Vitillo 216454ff00fSAdrian Hunter if (!a2l) { 217454ff00fSAdrian Hunter dso->a2l = addr2line_init(dso_name); 218454ff00fSAdrian Hunter a2l = dso->a2l; 219454ff00fSAdrian Hunter } 220454ff00fSAdrian Hunter 2212f48fcd8SRoberto Vitillo if (a2l == NULL) { 2222f48fcd8SRoberto Vitillo pr_warning("addr2line_init failed for %s\n", dso_name); 2232f48fcd8SRoberto Vitillo return 0; 2242f48fcd8SRoberto Vitillo } 2252f48fcd8SRoberto Vitillo 2262f48fcd8SRoberto Vitillo a2l->addr = addr; 227454ff00fSAdrian Hunter a2l->found = false; 228454ff00fSAdrian Hunter 2292f48fcd8SRoberto Vitillo bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l); 2302f48fcd8SRoberto Vitillo 2312f84b42bSAndi Kleen if (a2l->found && unwind_inlines) { 2322f84b42bSAndi Kleen int cnt = 0; 2332f84b42bSAndi Kleen 2342f84b42bSAndi Kleen while (bfd_find_inliner_info(a2l->abfd, &a2l->filename, 2352f84b42bSAndi Kleen &a2l->funcname, &a2l->line) && 236*a64489c5SJin Yao cnt++ < MAX_INLINE_NEST) { 237*a64489c5SJin Yao 238*a64489c5SJin Yao if (node != NULL) { 239*a64489c5SJin Yao if (inline_list__append(strdup(a2l->filename), 240*a64489c5SJin Yao strdup(a2l->funcname), 241*a64489c5SJin Yao a2l->line, node, 242*a64489c5SJin Yao dso) != 0) 243*a64489c5SJin Yao return 0; 244*a64489c5SJin Yao } 245*a64489c5SJin Yao } 246*a64489c5SJin Yao 247*a64489c5SJin Yao if ((node != NULL) && 248*a64489c5SJin Yao (callchain_param.order != ORDER_CALLEE)) { 249*a64489c5SJin Yao inline_list__reverse(node); 250*a64489c5SJin Yao } 2512f84b42bSAndi Kleen } 2522f84b42bSAndi Kleen 2532f48fcd8SRoberto Vitillo if (a2l->found && a2l->filename) { 2542f48fcd8SRoberto Vitillo *file = strdup(a2l->filename); 2552f48fcd8SRoberto Vitillo *line = a2l->line; 2562f48fcd8SRoberto Vitillo 2572f48fcd8SRoberto Vitillo if (*file) 2582f48fcd8SRoberto Vitillo ret = 1; 2592f48fcd8SRoberto Vitillo } 2602f48fcd8SRoberto Vitillo 2612f48fcd8SRoberto Vitillo return ret; 2622f48fcd8SRoberto Vitillo } 2632f48fcd8SRoberto Vitillo 264454ff00fSAdrian Hunter void dso__free_a2l(struct dso *dso) 265454ff00fSAdrian Hunter { 266454ff00fSAdrian Hunter struct a2l_data *a2l = dso->a2l; 267454ff00fSAdrian Hunter 268454ff00fSAdrian Hunter if (!a2l) 269454ff00fSAdrian Hunter return; 270454ff00fSAdrian Hunter 271454ff00fSAdrian Hunter addr2line_cleanup(a2l); 272454ff00fSAdrian Hunter 273454ff00fSAdrian Hunter dso->a2l = NULL; 274454ff00fSAdrian Hunter } 275454ff00fSAdrian Hunter 276*a64489c5SJin Yao static struct inline_node *addr2inlines(const char *dso_name, u64 addr, 277*a64489c5SJin Yao struct dso *dso) 278*a64489c5SJin Yao { 279*a64489c5SJin Yao char *file = NULL; 280*a64489c5SJin Yao unsigned int line = 0; 281*a64489c5SJin Yao struct inline_node *node; 282*a64489c5SJin Yao 283*a64489c5SJin Yao node = zalloc(sizeof(*node)); 284*a64489c5SJin Yao if (node == NULL) { 285*a64489c5SJin Yao perror("not enough memory for the inline node"); 286*a64489c5SJin Yao return NULL; 287*a64489c5SJin Yao } 288*a64489c5SJin Yao 289*a64489c5SJin Yao INIT_LIST_HEAD(&node->val); 290*a64489c5SJin Yao node->addr = addr; 291*a64489c5SJin Yao 292*a64489c5SJin Yao if (!addr2line(dso_name, addr, &file, &line, dso, TRUE, node)) 293*a64489c5SJin Yao goto out_free_inline_node; 294*a64489c5SJin Yao 295*a64489c5SJin Yao if (list_empty(&node->val)) 296*a64489c5SJin Yao goto out_free_inline_node; 297*a64489c5SJin Yao 298*a64489c5SJin Yao return node; 299*a64489c5SJin Yao 300*a64489c5SJin Yao out_free_inline_node: 301*a64489c5SJin Yao inline_node__delete(node); 302*a64489c5SJin Yao return NULL; 303*a64489c5SJin Yao } 304*a64489c5SJin Yao 3052f48fcd8SRoberto Vitillo #else /* HAVE_LIBBFD_SUPPORT */ 3062f48fcd8SRoberto Vitillo 3075580338dSJin Yao static int filename_split(char *filename, unsigned int *line_nr) 3085580338dSJin Yao { 3095580338dSJin Yao char *sep; 3105580338dSJin Yao 3115580338dSJin Yao sep = strchr(filename, '\n'); 3125580338dSJin Yao if (sep) 3135580338dSJin Yao *sep = '\0'; 3145580338dSJin Yao 3155580338dSJin Yao if (!strcmp(filename, "??:0")) 3165580338dSJin Yao return 0; 3175580338dSJin Yao 3185580338dSJin Yao sep = strchr(filename, ':'); 3195580338dSJin Yao if (sep) { 3205580338dSJin Yao *sep++ = '\0'; 3215580338dSJin Yao *line_nr = strtoul(sep, NULL, 0); 3225580338dSJin Yao return 1; 3235580338dSJin Yao } 3245580338dSJin Yao 3255580338dSJin Yao return 0; 3265580338dSJin Yao } 3275580338dSJin Yao 328ac931f87SWang Nan static int addr2line(const char *dso_name, u64 addr, 329454ff00fSAdrian Hunter char **file, unsigned int *line_nr, 3302f84b42bSAndi Kleen struct dso *dso __maybe_unused, 331*a64489c5SJin Yao bool unwind_inlines __maybe_unused, 332*a64489c5SJin Yao struct inline_node *node __maybe_unused) 333f048d548SNamhyung Kim { 334f048d548SNamhyung Kim FILE *fp; 335f048d548SNamhyung Kim char cmd[PATH_MAX]; 336f048d548SNamhyung Kim char *filename = NULL; 337f048d548SNamhyung Kim size_t len; 338f048d548SNamhyung Kim int ret = 0; 339f048d548SNamhyung Kim 340f048d548SNamhyung Kim scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64, 341f048d548SNamhyung Kim dso_name, addr); 342f048d548SNamhyung Kim 343f048d548SNamhyung Kim fp = popen(cmd, "r"); 344f048d548SNamhyung Kim if (fp == NULL) { 345f048d548SNamhyung Kim pr_warning("popen failed for %s\n", dso_name); 346f048d548SNamhyung Kim return 0; 347f048d548SNamhyung Kim } 348f048d548SNamhyung Kim 349f048d548SNamhyung Kim if (getline(&filename, &len, fp) < 0 || !len) { 350f048d548SNamhyung Kim pr_warning("addr2line has no output for %s\n", dso_name); 351f048d548SNamhyung Kim goto out; 352f048d548SNamhyung Kim } 353f048d548SNamhyung Kim 3545580338dSJin Yao ret = filename_split(filename, line_nr); 3555580338dSJin Yao if (ret != 1) { 356f048d548SNamhyung Kim free(filename); 357f048d548SNamhyung Kim goto out; 358f048d548SNamhyung Kim } 359f048d548SNamhyung Kim 360f048d548SNamhyung Kim *file = filename; 3615580338dSJin Yao 362f048d548SNamhyung Kim out: 363f048d548SNamhyung Kim pclose(fp); 364f048d548SNamhyung Kim return ret; 365f048d548SNamhyung Kim } 366454ff00fSAdrian Hunter 367454ff00fSAdrian Hunter void dso__free_a2l(struct dso *dso __maybe_unused) 368454ff00fSAdrian Hunter { 369454ff00fSAdrian Hunter } 370454ff00fSAdrian Hunter 371*a64489c5SJin Yao static struct inline_node *addr2inlines(const char *dso_name, u64 addr, 372*a64489c5SJin Yao struct dso *dso __maybe_unused) 373*a64489c5SJin Yao { 374*a64489c5SJin Yao FILE *fp; 375*a64489c5SJin Yao char cmd[PATH_MAX]; 376*a64489c5SJin Yao struct inline_node *node; 377*a64489c5SJin Yao char *filename = NULL; 378*a64489c5SJin Yao size_t len; 379*a64489c5SJin Yao unsigned int line_nr = 0; 380*a64489c5SJin Yao 381*a64489c5SJin Yao scnprintf(cmd, sizeof(cmd), "addr2line -e %s -i %016"PRIx64, 382*a64489c5SJin Yao dso_name, addr); 383*a64489c5SJin Yao 384*a64489c5SJin Yao fp = popen(cmd, "r"); 385*a64489c5SJin Yao if (fp == NULL) { 386*a64489c5SJin Yao pr_err("popen failed for %s\n", dso_name); 387*a64489c5SJin Yao return NULL; 388*a64489c5SJin Yao } 389*a64489c5SJin Yao 390*a64489c5SJin Yao node = zalloc(sizeof(*node)); 391*a64489c5SJin Yao if (node == NULL) { 392*a64489c5SJin Yao perror("not enough memory for the inline node"); 393*a64489c5SJin Yao goto out; 394*a64489c5SJin Yao } 395*a64489c5SJin Yao 396*a64489c5SJin Yao INIT_LIST_HEAD(&node->val); 397*a64489c5SJin Yao node->addr = addr; 398*a64489c5SJin Yao 399*a64489c5SJin Yao while (getline(&filename, &len, fp) != -1) { 400*a64489c5SJin Yao if (filename_split(filename, &line_nr) != 1) { 401*a64489c5SJin Yao free(filename); 402*a64489c5SJin Yao goto out; 403*a64489c5SJin Yao } 404*a64489c5SJin Yao 405*a64489c5SJin Yao if (inline_list__append(filename, NULL, line_nr, node, 406*a64489c5SJin Yao NULL) != 0) 407*a64489c5SJin Yao goto out; 408*a64489c5SJin Yao 409*a64489c5SJin Yao filename = NULL; 410*a64489c5SJin Yao } 411*a64489c5SJin Yao 412*a64489c5SJin Yao out: 413*a64489c5SJin Yao pclose(fp); 414*a64489c5SJin Yao 415*a64489c5SJin Yao if (list_empty(&node->val)) { 416*a64489c5SJin Yao inline_node__delete(node); 417*a64489c5SJin Yao return NULL; 418*a64489c5SJin Yao } 419*a64489c5SJin Yao 420*a64489c5SJin Yao return node; 421*a64489c5SJin Yao } 422*a64489c5SJin Yao 4232f48fcd8SRoberto Vitillo #endif /* HAVE_LIBBFD_SUPPORT */ 424f048d548SNamhyung Kim 425906049c8SAdrian Hunter /* 426906049c8SAdrian Hunter * Number of addr2line failures (without success) before disabling it for that 427906049c8SAdrian Hunter * dso. 428906049c8SAdrian Hunter */ 429906049c8SAdrian Hunter #define A2L_FAIL_LIMIT 123 430906049c8SAdrian Hunter 4312f84b42bSAndi Kleen char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym, 4322f84b42bSAndi Kleen bool show_sym, bool unwind_inlines) 433f048d548SNamhyung Kim { 434a949fffbSDavid Ahern char *file = NULL; 435a949fffbSDavid Ahern unsigned line = 0; 4362cc9d0efSNamhyung Kim char *srcline; 437bf4414aeSArnaldo Carvalho de Melo const char *dso_name; 438f048d548SNamhyung Kim 4392cc9d0efSNamhyung Kim if (!dso->has_srcline) 44023f0981bSAndi Kleen goto out; 4412cc9d0efSNamhyung Kim 4425580338dSJin Yao dso_name = dso__name(dso); 4435580338dSJin Yao if (dso_name == NULL) 44458d91a00SNamhyung Kim goto out; 44558d91a00SNamhyung Kim 446*a64489c5SJin Yao if (!addr2line(dso_name, addr, &file, &line, dso, unwind_inlines, NULL)) 44758d91a00SNamhyung Kim goto out; 448f048d548SNamhyung Kim 449a9710ba0SAndi Kleen if (asprintf(&srcline, "%s:%u", 450a9710ba0SAndi Kleen srcline_full_filename ? file : basename(file), 451a9710ba0SAndi Kleen line) < 0) { 452906049c8SAdrian Hunter free(file); 453906049c8SAdrian Hunter goto out; 454906049c8SAdrian Hunter } 455906049c8SAdrian Hunter 456906049c8SAdrian Hunter dso->a2l_fails = 0; 457f048d548SNamhyung Kim 458f048d548SNamhyung Kim free(file); 459f048d548SNamhyung Kim return srcline; 4602cc9d0efSNamhyung Kim 4612cc9d0efSNamhyung Kim out: 462906049c8SAdrian Hunter if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) { 4632cc9d0efSNamhyung Kim dso->has_srcline = 0; 464454ff00fSAdrian Hunter dso__free_a2l(dso); 465906049c8SAdrian Hunter } 46685c116a6SAndi Kleen if (sym) { 467ac931f87SWang Nan if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "", 46885c116a6SAndi Kleen addr - sym->start) < 0) 46985c116a6SAndi Kleen return SRCLINE_UNKNOWN; 470ac931f87SWang Nan } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0) 4712cc9d0efSNamhyung Kim return SRCLINE_UNKNOWN; 47223f0981bSAndi Kleen return srcline; 473f048d548SNamhyung Kim } 474f048d548SNamhyung Kim 475f048d548SNamhyung Kim void free_srcline(char *srcline) 476f048d548SNamhyung Kim { 477f048d548SNamhyung Kim if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0) 478f048d548SNamhyung Kim free(srcline); 479f048d548SNamhyung Kim } 4802f84b42bSAndi Kleen 4812f84b42bSAndi Kleen char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym, 4822f84b42bSAndi Kleen bool show_sym) 4832f84b42bSAndi Kleen { 4842f84b42bSAndi Kleen return __get_srcline(dso, addr, sym, show_sym, false); 4852f84b42bSAndi Kleen } 486*a64489c5SJin Yao 487*a64489c5SJin Yao struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr) 488*a64489c5SJin Yao { 489*a64489c5SJin Yao const char *dso_name; 490*a64489c5SJin Yao 491*a64489c5SJin Yao dso_name = dso__name(dso); 492*a64489c5SJin Yao if (dso_name == NULL) 493*a64489c5SJin Yao return NULL; 494*a64489c5SJin Yao 495*a64489c5SJin Yao return addr2inlines(dso_name, addr, dso); 496*a64489c5SJin Yao } 497*a64489c5SJin Yao 498*a64489c5SJin Yao void inline_node__delete(struct inline_node *node) 499*a64489c5SJin Yao { 500*a64489c5SJin Yao struct inline_list *ilist, *tmp; 501*a64489c5SJin Yao 502*a64489c5SJin Yao list_for_each_entry_safe(ilist, tmp, &node->val, list) { 503*a64489c5SJin Yao list_del_init(&ilist->list); 504*a64489c5SJin Yao zfree(&ilist->filename); 505*a64489c5SJin Yao zfree(&ilist->funcname); 506*a64489c5SJin Yao free(ilist); 507*a64489c5SJin Yao } 508*a64489c5SJin Yao 509*a64489c5SJin Yao free(node); 510*a64489c5SJin Yao } 511