Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | |
| 5 | #include <linux/kernel.h> |
| 6 | |
Namhyung Kim | 86c98ca | 2013-09-11 14:09:30 +0900 | [diff] [blame] | 7 | #include "util/dso.h" |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 8 | #include "util/util.h" |
| 9 | #include "util/debug.h" |
| 10 | |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 11 | #ifdef HAVE_LIBBFD_SUPPORT |
| 12 | |
| 13 | /* |
| 14 | * Implement addr2line using libbfd. |
| 15 | */ |
| 16 | #define PACKAGE "perf" |
| 17 | #include <bfd.h> |
| 18 | |
| 19 | struct a2l_data { |
| 20 | const char *input; |
| 21 | unsigned long addr; |
| 22 | |
| 23 | bool found; |
| 24 | const char *filename; |
| 25 | const char *funcname; |
| 26 | unsigned line; |
| 27 | |
| 28 | bfd *abfd; |
| 29 | asymbol **syms; |
| 30 | }; |
| 31 | |
| 32 | static int bfd_error(const char *string) |
| 33 | { |
| 34 | const char *errmsg; |
| 35 | |
| 36 | errmsg = bfd_errmsg(bfd_get_error()); |
| 37 | fflush(stdout); |
| 38 | |
| 39 | if (string) |
| 40 | pr_debug("%s: %s\n", string, errmsg); |
| 41 | else |
| 42 | pr_debug("%s\n", errmsg); |
| 43 | |
| 44 | return -1; |
| 45 | } |
| 46 | |
| 47 | static int slurp_symtab(bfd *abfd, struct a2l_data *a2l) |
| 48 | { |
| 49 | long storage; |
| 50 | long symcount; |
| 51 | asymbol **syms; |
| 52 | bfd_boolean dynamic = FALSE; |
| 53 | |
| 54 | if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0) |
| 55 | return bfd_error(bfd_get_filename(abfd)); |
| 56 | |
| 57 | storage = bfd_get_symtab_upper_bound(abfd); |
| 58 | if (storage == 0L) { |
| 59 | storage = bfd_get_dynamic_symtab_upper_bound(abfd); |
| 60 | dynamic = TRUE; |
| 61 | } |
| 62 | if (storage < 0L) |
| 63 | return bfd_error(bfd_get_filename(abfd)); |
| 64 | |
| 65 | syms = malloc(storage); |
| 66 | if (dynamic) |
| 67 | symcount = bfd_canonicalize_dynamic_symtab(abfd, syms); |
| 68 | else |
| 69 | symcount = bfd_canonicalize_symtab(abfd, syms); |
| 70 | |
| 71 | if (symcount < 0) { |
| 72 | free(syms); |
| 73 | return bfd_error(bfd_get_filename(abfd)); |
| 74 | } |
| 75 | |
| 76 | a2l->syms = syms; |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static void find_address_in_section(bfd *abfd, asection *section, void *data) |
| 81 | { |
| 82 | bfd_vma pc, vma; |
| 83 | bfd_size_type size; |
| 84 | struct a2l_data *a2l = data; |
| 85 | |
| 86 | if (a2l->found) |
| 87 | return; |
| 88 | |
| 89 | if ((bfd_get_section_flags(abfd, section) & SEC_ALLOC) == 0) |
| 90 | return; |
| 91 | |
| 92 | pc = a2l->addr; |
| 93 | vma = bfd_get_section_vma(abfd, section); |
| 94 | size = bfd_get_section_size(section); |
| 95 | |
| 96 | if (pc < vma || pc >= vma + size) |
| 97 | return; |
| 98 | |
| 99 | a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma, |
| 100 | &a2l->filename, &a2l->funcname, |
| 101 | &a2l->line); |
| 102 | } |
| 103 | |
| 104 | static struct a2l_data *addr2line_init(const char *path) |
| 105 | { |
| 106 | bfd *abfd; |
| 107 | struct a2l_data *a2l = NULL; |
| 108 | |
| 109 | abfd = bfd_openr(path, NULL); |
| 110 | if (abfd == NULL) |
| 111 | return NULL; |
| 112 | |
| 113 | if (!bfd_check_format(abfd, bfd_object)) |
| 114 | goto out; |
| 115 | |
| 116 | a2l = zalloc(sizeof(*a2l)); |
| 117 | if (a2l == NULL) |
| 118 | goto out; |
| 119 | |
| 120 | a2l->abfd = abfd; |
| 121 | a2l->input = strdup(path); |
| 122 | if (a2l->input == NULL) |
| 123 | goto out; |
| 124 | |
| 125 | if (slurp_symtab(abfd, a2l)) |
| 126 | goto out; |
| 127 | |
| 128 | return a2l; |
| 129 | |
| 130 | out: |
| 131 | if (a2l) { |
Namhyung Kim | 7d16c63 | 2014-01-09 23:07:59 +0900 | [diff] [blame] | 132 | zfree((char **)&a2l->input); |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 133 | free(a2l); |
| 134 | } |
| 135 | bfd_close(abfd); |
| 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | static void addr2line_cleanup(struct a2l_data *a2l) |
| 140 | { |
| 141 | if (a2l->abfd) |
| 142 | bfd_close(a2l->abfd); |
Namhyung Kim | 7d16c63 | 2014-01-09 23:07:59 +0900 | [diff] [blame] | 143 | zfree((char **)&a2l->input); |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 144 | zfree(&a2l->syms); |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 145 | free(a2l); |
| 146 | } |
| 147 | |
| 148 | static int addr2line(const char *dso_name, unsigned long addr, |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 149 | char **file, unsigned int *line, struct dso *dso) |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 150 | { |
| 151 | int ret = 0; |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 152 | struct a2l_data *a2l = dso->a2l; |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 153 | |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 154 | if (!a2l) { |
| 155 | dso->a2l = addr2line_init(dso_name); |
| 156 | a2l = dso->a2l; |
| 157 | } |
| 158 | |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 159 | if (a2l == NULL) { |
| 160 | pr_warning("addr2line_init failed for %s\n", dso_name); |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | a2l->addr = addr; |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 165 | a2l->found = false; |
| 166 | |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 167 | bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l); |
| 168 | |
| 169 | if (a2l->found && a2l->filename) { |
| 170 | *file = strdup(a2l->filename); |
| 171 | *line = a2l->line; |
| 172 | |
| 173 | if (*file) |
| 174 | ret = 1; |
| 175 | } |
| 176 | |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 177 | return ret; |
| 178 | } |
| 179 | |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 180 | void dso__free_a2l(struct dso *dso) |
| 181 | { |
| 182 | struct a2l_data *a2l = dso->a2l; |
| 183 | |
| 184 | if (!a2l) |
| 185 | return; |
| 186 | |
| 187 | addr2line_cleanup(a2l); |
| 188 | |
| 189 | dso->a2l = NULL; |
| 190 | } |
| 191 | |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 192 | #else /* HAVE_LIBBFD_SUPPORT */ |
| 193 | |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 194 | static int addr2line(const char *dso_name, unsigned long addr, |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 195 | char **file, unsigned int *line_nr, |
| 196 | struct dso *dso __maybe_unused) |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 197 | { |
| 198 | FILE *fp; |
| 199 | char cmd[PATH_MAX]; |
| 200 | char *filename = NULL; |
| 201 | size_t len; |
| 202 | char *sep; |
| 203 | int ret = 0; |
| 204 | |
| 205 | scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64, |
| 206 | dso_name, addr); |
| 207 | |
| 208 | fp = popen(cmd, "r"); |
| 209 | if (fp == NULL) { |
| 210 | pr_warning("popen failed for %s\n", dso_name); |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | if (getline(&filename, &len, fp) < 0 || !len) { |
| 215 | pr_warning("addr2line has no output for %s\n", dso_name); |
| 216 | goto out; |
| 217 | } |
| 218 | |
| 219 | sep = strchr(filename, '\n'); |
| 220 | if (sep) |
| 221 | *sep = '\0'; |
| 222 | |
| 223 | if (!strcmp(filename, "??:0")) { |
| 224 | pr_debug("no debugging info in %s\n", dso_name); |
| 225 | free(filename); |
| 226 | goto out; |
| 227 | } |
| 228 | |
| 229 | sep = strchr(filename, ':'); |
| 230 | if (sep) { |
| 231 | *sep++ = '\0'; |
| 232 | *file = filename; |
| 233 | *line_nr = strtoul(sep, NULL, 0); |
| 234 | ret = 1; |
| 235 | } |
| 236 | out: |
| 237 | pclose(fp); |
| 238 | return ret; |
| 239 | } |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 240 | |
| 241 | void dso__free_a2l(struct dso *dso __maybe_unused) |
| 242 | { |
| 243 | } |
| 244 | |
Roberto Vitillo | 2f48fcd | 2013-09-11 14:09:32 +0900 | [diff] [blame] | 245 | #endif /* HAVE_LIBBFD_SUPPORT */ |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 246 | |
Adrian Hunter | 906049c8 | 2013-12-03 09:23:10 +0200 | [diff] [blame] | 247 | /* |
| 248 | * Number of addr2line failures (without success) before disabling it for that |
| 249 | * dso. |
| 250 | */ |
| 251 | #define A2L_FAIL_LIMIT 123 |
| 252 | |
Namhyung Kim | 86c98ca | 2013-09-11 14:09:30 +0900 | [diff] [blame] | 253 | char *get_srcline(struct dso *dso, unsigned long addr) |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 254 | { |
David Ahern | a949fff | 2013-10-09 21:51:31 -0600 | [diff] [blame] | 255 | char *file = NULL; |
| 256 | unsigned line = 0; |
Namhyung Kim | 2cc9d0e | 2013-09-11 14:09:31 +0900 | [diff] [blame] | 257 | char *srcline; |
Arnaldo Carvalho de Melo | bf4414a | 2013-12-10 15:19:23 -0300 | [diff] [blame] | 258 | const char *dso_name; |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 259 | |
Namhyung Kim | 2cc9d0e | 2013-09-11 14:09:31 +0900 | [diff] [blame] | 260 | if (!dso->has_srcline) |
| 261 | return SRCLINE_UNKNOWN; |
| 262 | |
Adrian Hunter | 0058aef | 2013-12-03 09:23:08 +0200 | [diff] [blame] | 263 | if (dso->symsrc_filename) |
| 264 | dso_name = dso->symsrc_filename; |
| 265 | else |
| 266 | dso_name = dso->long_name; |
| 267 | |
Namhyung Kim | 58d91a0 | 2013-09-11 14:09:29 +0900 | [diff] [blame] | 268 | if (dso_name[0] == '[') |
| 269 | goto out; |
| 270 | |
| 271 | if (!strncmp(dso_name, "/tmp/perf-", 10)) |
| 272 | goto out; |
| 273 | |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 274 | if (!addr2line(dso_name, addr, &file, &line, dso)) |
Namhyung Kim | 58d91a0 | 2013-09-11 14:09:29 +0900 | [diff] [blame] | 275 | goto out; |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 276 | |
Adrian Hunter | 906049c8 | 2013-12-03 09:23:10 +0200 | [diff] [blame] | 277 | if (asprintf(&srcline, "%s:%u", file, line) < 0) { |
| 278 | free(file); |
| 279 | goto out; |
| 280 | } |
| 281 | |
| 282 | dso->a2l_fails = 0; |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 283 | |
| 284 | free(file); |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 285 | return srcline; |
Namhyung Kim | 2cc9d0e | 2013-09-11 14:09:31 +0900 | [diff] [blame] | 286 | |
| 287 | out: |
Adrian Hunter | 906049c8 | 2013-12-03 09:23:10 +0200 | [diff] [blame] | 288 | if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) { |
| 289 | dso->has_srcline = 0; |
| 290 | dso__free_a2l(dso); |
| 291 | } |
Namhyung Kim | 2cc9d0e | 2013-09-11 14:09:31 +0900 | [diff] [blame] | 292 | return SRCLINE_UNKNOWN; |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | void free_srcline(char *srcline) |
| 296 | { |
| 297 | if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0) |
| 298 | free(srcline); |
| 299 | } |