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 112f48fcd8SRoberto Vitillo #ifdef HAVE_LIBBFD_SUPPORT 122f48fcd8SRoberto Vitillo 132f48fcd8SRoberto Vitillo /* 142f48fcd8SRoberto Vitillo * Implement addr2line using libbfd. 152f48fcd8SRoberto Vitillo */ 162f48fcd8SRoberto Vitillo #define PACKAGE "perf" 172f48fcd8SRoberto Vitillo #include <bfd.h> 182f48fcd8SRoberto Vitillo 192f48fcd8SRoberto Vitillo struct a2l_data { 202f48fcd8SRoberto Vitillo const char *input; 212f48fcd8SRoberto Vitillo unsigned long addr; 222f48fcd8SRoberto Vitillo 232f48fcd8SRoberto Vitillo bool found; 242f48fcd8SRoberto Vitillo const char *filename; 252f48fcd8SRoberto Vitillo const char *funcname; 262f48fcd8SRoberto Vitillo unsigned line; 272f48fcd8SRoberto Vitillo 282f48fcd8SRoberto Vitillo bfd *abfd; 292f48fcd8SRoberto Vitillo asymbol **syms; 302f48fcd8SRoberto Vitillo }; 312f48fcd8SRoberto Vitillo 322f48fcd8SRoberto Vitillo static int bfd_error(const char *string) 332f48fcd8SRoberto Vitillo { 342f48fcd8SRoberto Vitillo const char *errmsg; 352f48fcd8SRoberto Vitillo 362f48fcd8SRoberto Vitillo errmsg = bfd_errmsg(bfd_get_error()); 372f48fcd8SRoberto Vitillo fflush(stdout); 382f48fcd8SRoberto Vitillo 392f48fcd8SRoberto Vitillo if (string) 402f48fcd8SRoberto Vitillo pr_debug("%s: %s\n", string, errmsg); 412f48fcd8SRoberto Vitillo else 422f48fcd8SRoberto Vitillo pr_debug("%s\n", errmsg); 432f48fcd8SRoberto Vitillo 442f48fcd8SRoberto Vitillo return -1; 452f48fcd8SRoberto Vitillo } 462f48fcd8SRoberto Vitillo 472f48fcd8SRoberto Vitillo static int slurp_symtab(bfd *abfd, struct a2l_data *a2l) 482f48fcd8SRoberto Vitillo { 492f48fcd8SRoberto Vitillo long storage; 502f48fcd8SRoberto Vitillo long symcount; 512f48fcd8SRoberto Vitillo asymbol **syms; 522f48fcd8SRoberto Vitillo bfd_boolean dynamic = FALSE; 532f48fcd8SRoberto Vitillo 542f48fcd8SRoberto Vitillo if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0) 552f48fcd8SRoberto Vitillo return bfd_error(bfd_get_filename(abfd)); 562f48fcd8SRoberto Vitillo 572f48fcd8SRoberto Vitillo storage = bfd_get_symtab_upper_bound(abfd); 582f48fcd8SRoberto Vitillo if (storage == 0L) { 592f48fcd8SRoberto Vitillo storage = bfd_get_dynamic_symtab_upper_bound(abfd); 602f48fcd8SRoberto Vitillo dynamic = TRUE; 612f48fcd8SRoberto Vitillo } 622f48fcd8SRoberto Vitillo if (storage < 0L) 632f48fcd8SRoberto Vitillo return bfd_error(bfd_get_filename(abfd)); 642f48fcd8SRoberto Vitillo 652f48fcd8SRoberto Vitillo syms = malloc(storage); 662f48fcd8SRoberto Vitillo if (dynamic) 672f48fcd8SRoberto Vitillo symcount = bfd_canonicalize_dynamic_symtab(abfd, syms); 682f48fcd8SRoberto Vitillo else 692f48fcd8SRoberto Vitillo symcount = bfd_canonicalize_symtab(abfd, syms); 702f48fcd8SRoberto Vitillo 712f48fcd8SRoberto Vitillo if (symcount < 0) { 722f48fcd8SRoberto Vitillo free(syms); 732f48fcd8SRoberto Vitillo return bfd_error(bfd_get_filename(abfd)); 742f48fcd8SRoberto Vitillo } 752f48fcd8SRoberto Vitillo 762f48fcd8SRoberto Vitillo a2l->syms = syms; 772f48fcd8SRoberto Vitillo return 0; 782f48fcd8SRoberto Vitillo } 792f48fcd8SRoberto Vitillo 802f48fcd8SRoberto Vitillo static void find_address_in_section(bfd *abfd, asection *section, void *data) 812f48fcd8SRoberto Vitillo { 822f48fcd8SRoberto Vitillo bfd_vma pc, vma; 832f48fcd8SRoberto Vitillo bfd_size_type size; 842f48fcd8SRoberto Vitillo struct a2l_data *a2l = data; 852f48fcd8SRoberto Vitillo 862f48fcd8SRoberto Vitillo if (a2l->found) 872f48fcd8SRoberto Vitillo return; 882f48fcd8SRoberto Vitillo 892f48fcd8SRoberto Vitillo if ((bfd_get_section_flags(abfd, section) & SEC_ALLOC) == 0) 902f48fcd8SRoberto Vitillo return; 912f48fcd8SRoberto Vitillo 922f48fcd8SRoberto Vitillo pc = a2l->addr; 932f48fcd8SRoberto Vitillo vma = bfd_get_section_vma(abfd, section); 942f48fcd8SRoberto Vitillo size = bfd_get_section_size(section); 952f48fcd8SRoberto Vitillo 962f48fcd8SRoberto Vitillo if (pc < vma || pc >= vma + size) 972f48fcd8SRoberto Vitillo return; 982f48fcd8SRoberto Vitillo 992f48fcd8SRoberto Vitillo a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma, 1002f48fcd8SRoberto Vitillo &a2l->filename, &a2l->funcname, 1012f48fcd8SRoberto Vitillo &a2l->line); 1022f48fcd8SRoberto Vitillo } 1032f48fcd8SRoberto Vitillo 1042f48fcd8SRoberto Vitillo static struct a2l_data *addr2line_init(const char *path) 1052f48fcd8SRoberto Vitillo { 1062f48fcd8SRoberto Vitillo bfd *abfd; 1072f48fcd8SRoberto Vitillo struct a2l_data *a2l = NULL; 1082f48fcd8SRoberto Vitillo 1092f48fcd8SRoberto Vitillo abfd = bfd_openr(path, NULL); 1102f48fcd8SRoberto Vitillo if (abfd == NULL) 1112f48fcd8SRoberto Vitillo return NULL; 1122f48fcd8SRoberto Vitillo 1132f48fcd8SRoberto Vitillo if (!bfd_check_format(abfd, bfd_object)) 1142f48fcd8SRoberto Vitillo goto out; 1152f48fcd8SRoberto Vitillo 1162f48fcd8SRoberto Vitillo a2l = zalloc(sizeof(*a2l)); 1172f48fcd8SRoberto Vitillo if (a2l == NULL) 1182f48fcd8SRoberto Vitillo goto out; 1192f48fcd8SRoberto Vitillo 1202f48fcd8SRoberto Vitillo a2l->abfd = abfd; 1212f48fcd8SRoberto Vitillo a2l->input = strdup(path); 1222f48fcd8SRoberto Vitillo if (a2l->input == NULL) 1232f48fcd8SRoberto Vitillo goto out; 1242f48fcd8SRoberto Vitillo 1252f48fcd8SRoberto Vitillo if (slurp_symtab(abfd, a2l)) 1262f48fcd8SRoberto Vitillo goto out; 1272f48fcd8SRoberto Vitillo 1282f48fcd8SRoberto Vitillo return a2l; 1292f48fcd8SRoberto Vitillo 1302f48fcd8SRoberto Vitillo out: 1312f48fcd8SRoberto Vitillo if (a2l) { 1327d16c634SNamhyung Kim zfree((char **)&a2l->input); 1332f48fcd8SRoberto Vitillo free(a2l); 1342f48fcd8SRoberto Vitillo } 1352f48fcd8SRoberto Vitillo bfd_close(abfd); 1362f48fcd8SRoberto Vitillo return NULL; 1372f48fcd8SRoberto Vitillo } 1382f48fcd8SRoberto Vitillo 1392f48fcd8SRoberto Vitillo static void addr2line_cleanup(struct a2l_data *a2l) 1402f48fcd8SRoberto Vitillo { 1412f48fcd8SRoberto Vitillo if (a2l->abfd) 1422f48fcd8SRoberto Vitillo bfd_close(a2l->abfd); 1437d16c634SNamhyung Kim zfree((char **)&a2l->input); 14474cf249dSArnaldo Carvalho de Melo zfree(&a2l->syms); 1452f48fcd8SRoberto Vitillo free(a2l); 1462f48fcd8SRoberto Vitillo } 1472f48fcd8SRoberto Vitillo 1482f48fcd8SRoberto Vitillo static int addr2line(const char *dso_name, unsigned long addr, 149454ff00fSAdrian Hunter char **file, unsigned int *line, struct dso *dso) 1502f48fcd8SRoberto Vitillo { 1512f48fcd8SRoberto Vitillo int ret = 0; 152454ff00fSAdrian Hunter struct a2l_data *a2l = dso->a2l; 1532f48fcd8SRoberto Vitillo 154454ff00fSAdrian Hunter if (!a2l) { 155454ff00fSAdrian Hunter dso->a2l = addr2line_init(dso_name); 156454ff00fSAdrian Hunter a2l = dso->a2l; 157454ff00fSAdrian Hunter } 158454ff00fSAdrian Hunter 1592f48fcd8SRoberto Vitillo if (a2l == NULL) { 1602f48fcd8SRoberto Vitillo pr_warning("addr2line_init failed for %s\n", dso_name); 1612f48fcd8SRoberto Vitillo return 0; 1622f48fcd8SRoberto Vitillo } 1632f48fcd8SRoberto Vitillo 1642f48fcd8SRoberto Vitillo a2l->addr = addr; 165454ff00fSAdrian Hunter a2l->found = false; 166454ff00fSAdrian Hunter 1672f48fcd8SRoberto Vitillo bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l); 1682f48fcd8SRoberto Vitillo 1692f48fcd8SRoberto Vitillo if (a2l->found && a2l->filename) { 1702f48fcd8SRoberto Vitillo *file = strdup(a2l->filename); 1712f48fcd8SRoberto Vitillo *line = a2l->line; 1722f48fcd8SRoberto Vitillo 1732f48fcd8SRoberto Vitillo if (*file) 1742f48fcd8SRoberto Vitillo ret = 1; 1752f48fcd8SRoberto Vitillo } 1762f48fcd8SRoberto Vitillo 1772f48fcd8SRoberto Vitillo return ret; 1782f48fcd8SRoberto Vitillo } 1792f48fcd8SRoberto Vitillo 180454ff00fSAdrian Hunter void dso__free_a2l(struct dso *dso) 181454ff00fSAdrian Hunter { 182454ff00fSAdrian Hunter struct a2l_data *a2l = dso->a2l; 183454ff00fSAdrian Hunter 184454ff00fSAdrian Hunter if (!a2l) 185454ff00fSAdrian Hunter return; 186454ff00fSAdrian Hunter 187454ff00fSAdrian Hunter addr2line_cleanup(a2l); 188454ff00fSAdrian Hunter 189454ff00fSAdrian Hunter dso->a2l = NULL; 190454ff00fSAdrian Hunter } 191454ff00fSAdrian Hunter 1922f48fcd8SRoberto Vitillo #else /* HAVE_LIBBFD_SUPPORT */ 1932f48fcd8SRoberto Vitillo 194f048d548SNamhyung Kim static int addr2line(const char *dso_name, unsigned long addr, 195454ff00fSAdrian Hunter char **file, unsigned int *line_nr, 196454ff00fSAdrian Hunter struct dso *dso __maybe_unused) 197f048d548SNamhyung Kim { 198f048d548SNamhyung Kim FILE *fp; 199f048d548SNamhyung Kim char cmd[PATH_MAX]; 200f048d548SNamhyung Kim char *filename = NULL; 201f048d548SNamhyung Kim size_t len; 202f048d548SNamhyung Kim char *sep; 203f048d548SNamhyung Kim int ret = 0; 204f048d548SNamhyung Kim 205f048d548SNamhyung Kim scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64, 206f048d548SNamhyung Kim dso_name, addr); 207f048d548SNamhyung Kim 208f048d548SNamhyung Kim fp = popen(cmd, "r"); 209f048d548SNamhyung Kim if (fp == NULL) { 210f048d548SNamhyung Kim pr_warning("popen failed for %s\n", dso_name); 211f048d548SNamhyung Kim return 0; 212f048d548SNamhyung Kim } 213f048d548SNamhyung Kim 214f048d548SNamhyung Kim if (getline(&filename, &len, fp) < 0 || !len) { 215f048d548SNamhyung Kim pr_warning("addr2line has no output for %s\n", dso_name); 216f048d548SNamhyung Kim goto out; 217f048d548SNamhyung Kim } 218f048d548SNamhyung Kim 219f048d548SNamhyung Kim sep = strchr(filename, '\n'); 220f048d548SNamhyung Kim if (sep) 221f048d548SNamhyung Kim *sep = '\0'; 222f048d548SNamhyung Kim 223f048d548SNamhyung Kim if (!strcmp(filename, "??:0")) { 224f048d548SNamhyung Kim pr_debug("no debugging info in %s\n", dso_name); 225f048d548SNamhyung Kim free(filename); 226f048d548SNamhyung Kim goto out; 227f048d548SNamhyung Kim } 228f048d548SNamhyung Kim 229f048d548SNamhyung Kim sep = strchr(filename, ':'); 230f048d548SNamhyung Kim if (sep) { 231f048d548SNamhyung Kim *sep++ = '\0'; 232f048d548SNamhyung Kim *file = filename; 233f048d548SNamhyung Kim *line_nr = strtoul(sep, NULL, 0); 234f048d548SNamhyung Kim ret = 1; 235f048d548SNamhyung Kim } 236f048d548SNamhyung Kim out: 237f048d548SNamhyung Kim pclose(fp); 238f048d548SNamhyung Kim return ret; 239f048d548SNamhyung Kim } 240454ff00fSAdrian Hunter 241454ff00fSAdrian Hunter void dso__free_a2l(struct dso *dso __maybe_unused) 242454ff00fSAdrian Hunter { 243454ff00fSAdrian Hunter } 244454ff00fSAdrian Hunter 2452f48fcd8SRoberto Vitillo #endif /* HAVE_LIBBFD_SUPPORT */ 246f048d548SNamhyung Kim 247906049c8SAdrian Hunter /* 248906049c8SAdrian Hunter * Number of addr2line failures (without success) before disabling it for that 249906049c8SAdrian Hunter * dso. 250906049c8SAdrian Hunter */ 251906049c8SAdrian Hunter #define A2L_FAIL_LIMIT 123 252906049c8SAdrian Hunter 25386c98cabSNamhyung Kim char *get_srcline(struct dso *dso, unsigned long addr) 254f048d548SNamhyung Kim { 255a949fffbSDavid Ahern char *file = NULL; 256a949fffbSDavid Ahern unsigned line = 0; 2572cc9d0efSNamhyung Kim char *srcline; 258bf4414aeSArnaldo Carvalho de Melo const char *dso_name; 259f048d548SNamhyung Kim 2602cc9d0efSNamhyung Kim if (!dso->has_srcline) 2612cc9d0efSNamhyung Kim return SRCLINE_UNKNOWN; 2622cc9d0efSNamhyung Kim 2630058aef6SAdrian Hunter if (dso->symsrc_filename) 2640058aef6SAdrian Hunter dso_name = dso->symsrc_filename; 2650058aef6SAdrian Hunter else 2660058aef6SAdrian Hunter dso_name = dso->long_name; 2670058aef6SAdrian Hunter 26858d91a00SNamhyung Kim if (dso_name[0] == '[') 26958d91a00SNamhyung Kim goto out; 27058d91a00SNamhyung Kim 27158d91a00SNamhyung Kim if (!strncmp(dso_name, "/tmp/perf-", 10)) 27258d91a00SNamhyung Kim goto out; 27358d91a00SNamhyung Kim 274454ff00fSAdrian Hunter if (!addr2line(dso_name, addr, &file, &line, dso)) 27558d91a00SNamhyung Kim goto out; 276f048d548SNamhyung Kim 277*2de21768SAndi Kleen if (asprintf(&srcline, "%s:%u", basename(file), line) < 0) { 278906049c8SAdrian Hunter free(file); 279906049c8SAdrian Hunter goto out; 280906049c8SAdrian Hunter } 281906049c8SAdrian Hunter 282906049c8SAdrian Hunter dso->a2l_fails = 0; 283f048d548SNamhyung Kim 284f048d548SNamhyung Kim free(file); 285f048d548SNamhyung Kim return srcline; 2862cc9d0efSNamhyung Kim 2872cc9d0efSNamhyung Kim out: 288906049c8SAdrian Hunter if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) { 2892cc9d0efSNamhyung Kim dso->has_srcline = 0; 290454ff00fSAdrian Hunter dso__free_a2l(dso); 291906049c8SAdrian Hunter } 2922cc9d0efSNamhyung Kim return SRCLINE_UNKNOWN; 293f048d548SNamhyung Kim } 294f048d548SNamhyung Kim 295f048d548SNamhyung Kim void free_srcline(char *srcline) 296f048d548SNamhyung Kim { 297f048d548SNamhyung Kim if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0) 298f048d548SNamhyung Kim free(srcline); 299f048d548SNamhyung Kim } 300