srcline.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <linux/kernel.h>
  5. #include "util/dso.h"
  6. #include "util/util.h"
  7. #include "util/debug.h"
  8. #include "symbol.h"
  9. #ifdef HAVE_LIBBFD_SUPPORT
  10. /*
  11. * Implement addr2line using libbfd.
  12. */
  13. #define PACKAGE "perf"
  14. #include <bfd.h>
  15. struct a2l_data {
  16. const char *input;
  17. u64 addr;
  18. bool found;
  19. const char *filename;
  20. const char *funcname;
  21. unsigned line;
  22. bfd *abfd;
  23. asymbol **syms;
  24. };
  25. static int bfd_error(const char *string)
  26. {
  27. const char *errmsg;
  28. errmsg = bfd_errmsg(bfd_get_error());
  29. fflush(stdout);
  30. if (string)
  31. pr_debug("%s: %s\n", string, errmsg);
  32. else
  33. pr_debug("%s\n", errmsg);
  34. return -1;
  35. }
  36. static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
  37. {
  38. long storage;
  39. long symcount;
  40. asymbol **syms;
  41. bfd_boolean dynamic = FALSE;
  42. if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
  43. return bfd_error(bfd_get_filename(abfd));
  44. storage = bfd_get_symtab_upper_bound(abfd);
  45. if (storage == 0L) {
  46. storage = bfd_get_dynamic_symtab_upper_bound(abfd);
  47. dynamic = TRUE;
  48. }
  49. if (storage < 0L)
  50. return bfd_error(bfd_get_filename(abfd));
  51. syms = malloc(storage);
  52. if (dynamic)
  53. symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
  54. else
  55. symcount = bfd_canonicalize_symtab(abfd, syms);
  56. if (symcount < 0) {
  57. free(syms);
  58. return bfd_error(bfd_get_filename(abfd));
  59. }
  60. a2l->syms = syms;
  61. return 0;
  62. }
  63. static void find_address_in_section(bfd *abfd, asection *section, void *data)
  64. {
  65. bfd_vma pc, vma;
  66. bfd_size_type size;
  67. struct a2l_data *a2l = data;
  68. if (a2l->found)
  69. return;
  70. if ((bfd_get_section_flags(abfd, section) & SEC_ALLOC) == 0)
  71. return;
  72. pc = a2l->addr;
  73. vma = bfd_get_section_vma(abfd, section);
  74. size = bfd_get_section_size(section);
  75. if (pc < vma || pc >= vma + size)
  76. return;
  77. a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
  78. &a2l->filename, &a2l->funcname,
  79. &a2l->line);
  80. }
  81. static struct a2l_data *addr2line_init(const char *path)
  82. {
  83. bfd *abfd;
  84. struct a2l_data *a2l = NULL;
  85. abfd = bfd_openr(path, NULL);
  86. if (abfd == NULL)
  87. return NULL;
  88. if (!bfd_check_format(abfd, bfd_object))
  89. goto out;
  90. a2l = zalloc(sizeof(*a2l));
  91. if (a2l == NULL)
  92. goto out;
  93. a2l->abfd = abfd;
  94. a2l->input = strdup(path);
  95. if (a2l->input == NULL)
  96. goto out;
  97. if (slurp_symtab(abfd, a2l))
  98. goto out;
  99. return a2l;
  100. out:
  101. if (a2l) {
  102. zfree((char **)&a2l->input);
  103. free(a2l);
  104. }
  105. bfd_close(abfd);
  106. return NULL;
  107. }
  108. static void addr2line_cleanup(struct a2l_data *a2l)
  109. {
  110. if (a2l->abfd)
  111. bfd_close(a2l->abfd);
  112. zfree((char **)&a2l->input);
  113. zfree(&a2l->syms);
  114. free(a2l);
  115. }
  116. static int addr2line(const char *dso_name, u64 addr,
  117. char **file, unsigned int *line, struct dso *dso)
  118. {
  119. int ret = 0;
  120. struct a2l_data *a2l = dso->a2l;
  121. if (!a2l) {
  122. dso->a2l = addr2line_init(dso_name);
  123. a2l = dso->a2l;
  124. }
  125. if (a2l == NULL) {
  126. pr_warning("addr2line_init failed for %s\n", dso_name);
  127. return 0;
  128. }
  129. a2l->addr = addr;
  130. a2l->found = false;
  131. bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
  132. if (a2l->found && a2l->filename) {
  133. *file = strdup(a2l->filename);
  134. *line = a2l->line;
  135. if (*file)
  136. ret = 1;
  137. }
  138. return ret;
  139. }
  140. void dso__free_a2l(struct dso *dso)
  141. {
  142. struct a2l_data *a2l = dso->a2l;
  143. if (!a2l)
  144. return;
  145. addr2line_cleanup(a2l);
  146. dso->a2l = NULL;
  147. }
  148. #else /* HAVE_LIBBFD_SUPPORT */
  149. static int addr2line(const char *dso_name, u64 addr,
  150. char **file, unsigned int *line_nr,
  151. struct dso *dso __maybe_unused)
  152. {
  153. FILE *fp;
  154. char cmd[PATH_MAX];
  155. char *filename = NULL;
  156. size_t len;
  157. char *sep;
  158. int ret = 0;
  159. scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64,
  160. dso_name, addr);
  161. fp = popen(cmd, "r");
  162. if (fp == NULL) {
  163. pr_warning("popen failed for %s\n", dso_name);
  164. return 0;
  165. }
  166. if (getline(&filename, &len, fp) < 0 || !len) {
  167. pr_warning("addr2line has no output for %s\n", dso_name);
  168. goto out;
  169. }
  170. sep = strchr(filename, '\n');
  171. if (sep)
  172. *sep = '\0';
  173. if (!strcmp(filename, "??:0")) {
  174. pr_debug("no debugging info in %s\n", dso_name);
  175. free(filename);
  176. goto out;
  177. }
  178. sep = strchr(filename, ':');
  179. if (sep) {
  180. *sep++ = '\0';
  181. *file = filename;
  182. *line_nr = strtoul(sep, NULL, 0);
  183. ret = 1;
  184. }
  185. out:
  186. pclose(fp);
  187. return ret;
  188. }
  189. void dso__free_a2l(struct dso *dso __maybe_unused)
  190. {
  191. }
  192. #endif /* HAVE_LIBBFD_SUPPORT */
  193. /*
  194. * Number of addr2line failures (without success) before disabling it for that
  195. * dso.
  196. */
  197. #define A2L_FAIL_LIMIT 123
  198. char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym,
  199. bool show_sym)
  200. {
  201. char *file = NULL;
  202. unsigned line = 0;
  203. char *srcline;
  204. const char *dso_name;
  205. if (!dso->has_srcline)
  206. goto out;
  207. if (dso->symsrc_filename)
  208. dso_name = dso->symsrc_filename;
  209. else
  210. dso_name = dso->long_name;
  211. if (dso_name[0] == '[')
  212. goto out;
  213. if (!strncmp(dso_name, "/tmp/perf-", 10))
  214. goto out;
  215. if (!addr2line(dso_name, addr, &file, &line, dso))
  216. goto out;
  217. if (asprintf(&srcline, "%s:%u", basename(file), line) < 0) {
  218. free(file);
  219. goto out;
  220. }
  221. dso->a2l_fails = 0;
  222. free(file);
  223. return srcline;
  224. out:
  225. if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
  226. dso->has_srcline = 0;
  227. dso__free_a2l(dso);
  228. }
  229. if (sym) {
  230. if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "",
  231. addr - sym->start) < 0)
  232. return SRCLINE_UNKNOWN;
  233. } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0)
  234. return SRCLINE_UNKNOWN;
  235. return srcline;
  236. }
  237. void free_srcline(char *srcline)
  238. {
  239. if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
  240. free(srcline);
  241. }