srcline.c 5.2 KB

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