srcline.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. free((void *)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. free((void *)a2l->input);
  112. free(a2l->syms);
  113. free(a2l);
  114. }
  115. static int addr2line(const char *dso_name, unsigned long addr,
  116. char **file, unsigned int *line)
  117. {
  118. int ret = 0;
  119. struct a2l_data *a2l;
  120. a2l = addr2line_init(dso_name);
  121. if (a2l == NULL) {
  122. pr_warning("addr2line_init failed for %s\n", dso_name);
  123. return 0;
  124. }
  125. a2l->addr = addr;
  126. bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
  127. if (a2l->found && a2l->filename) {
  128. *file = strdup(a2l->filename);
  129. *line = a2l->line;
  130. if (*file)
  131. ret = 1;
  132. }
  133. addr2line_cleanup(a2l);
  134. return ret;
  135. }
  136. #else /* HAVE_LIBBFD_SUPPORT */
  137. static int addr2line(const char *dso_name, unsigned long addr,
  138. char **file, unsigned int *line_nr)
  139. {
  140. FILE *fp;
  141. char cmd[PATH_MAX];
  142. char *filename = NULL;
  143. size_t len;
  144. char *sep;
  145. int ret = 0;
  146. scnprintf(cmd, sizeof(cmd), "addr2line -e %s %016"PRIx64,
  147. dso_name, addr);
  148. fp = popen(cmd, "r");
  149. if (fp == NULL) {
  150. pr_warning("popen failed for %s\n", dso_name);
  151. return 0;
  152. }
  153. if (getline(&filename, &len, fp) < 0 || !len) {
  154. pr_warning("addr2line has no output for %s\n", dso_name);
  155. goto out;
  156. }
  157. sep = strchr(filename, '\n');
  158. if (sep)
  159. *sep = '\0';
  160. if (!strcmp(filename, "??:0")) {
  161. pr_debug("no debugging info in %s\n", dso_name);
  162. free(filename);
  163. goto out;
  164. }
  165. sep = strchr(filename, ':');
  166. if (sep) {
  167. *sep++ = '\0';
  168. *file = filename;
  169. *line_nr = strtoul(sep, NULL, 0);
  170. ret = 1;
  171. }
  172. out:
  173. pclose(fp);
  174. return ret;
  175. }
  176. #endif /* HAVE_LIBBFD_SUPPORT */
  177. char *get_srcline(struct dso *dso, unsigned long addr)
  178. {
  179. char *file = NULL;
  180. unsigned line = 0;
  181. char *srcline;
  182. char *dso_name = dso->long_name;
  183. size_t size;
  184. if (!dso->has_srcline)
  185. return SRCLINE_UNKNOWN;
  186. if (dso_name[0] == '[')
  187. goto out;
  188. if (!strncmp(dso_name, "/tmp/perf-", 10))
  189. goto out;
  190. if (!addr2line(dso_name, addr, &file, &line))
  191. goto out;
  192. /* just calculate actual length */
  193. size = snprintf(NULL, 0, "%s:%u", file, line) + 1;
  194. srcline = malloc(size);
  195. if (srcline)
  196. snprintf(srcline, size, "%s:%u", file, line);
  197. else
  198. srcline = SRCLINE_UNKNOWN;
  199. free(file);
  200. return srcline;
  201. out:
  202. dso->has_srcline = 0;
  203. return SRCLINE_UNKNOWN;
  204. }
  205. void free_srcline(char *srcline)
  206. {
  207. if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0)
  208. free(srcline);
  209. }