srcline.c 5.9 KB

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