vdso.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <stdlib.h>
  8. #include <linux/kernel.h>
  9. #include "vdso.h"
  10. #include "util.h"
  11. #include "symbol.h"
  12. #include "machine.h"
  13. #include "thread.h"
  14. #include "linux/string.h"
  15. #include "debug.h"
  16. /*
  17. * Include definition of find_vdso_map() also used in perf-read-vdso.c for
  18. * building perf-read-vdso32 and perf-read-vdsox32.
  19. */
  20. #include "find-vdso-map.c"
  21. #define VDSO__TEMP_FILE_NAME "/tmp/perf-vdso.so-XXXXXX"
  22. struct vdso_file {
  23. bool found;
  24. bool error;
  25. char temp_file_name[sizeof(VDSO__TEMP_FILE_NAME)];
  26. const char *dso_name;
  27. const char *read_prog;
  28. };
  29. struct vdso_info {
  30. struct vdso_file vdso;
  31. #if BITS_PER_LONG == 64
  32. struct vdso_file vdso32;
  33. struct vdso_file vdsox32;
  34. #endif
  35. };
  36. static struct vdso_info *vdso_info__new(void)
  37. {
  38. static const struct vdso_info vdso_info_init = {
  39. .vdso = {
  40. .temp_file_name = VDSO__TEMP_FILE_NAME,
  41. .dso_name = DSO__NAME_VDSO,
  42. },
  43. #if BITS_PER_LONG == 64
  44. .vdso32 = {
  45. .temp_file_name = VDSO__TEMP_FILE_NAME,
  46. .dso_name = DSO__NAME_VDSO32,
  47. .read_prog = "perf-read-vdso32",
  48. },
  49. .vdsox32 = {
  50. .temp_file_name = VDSO__TEMP_FILE_NAME,
  51. .dso_name = DSO__NAME_VDSOX32,
  52. .read_prog = "perf-read-vdsox32",
  53. },
  54. #endif
  55. };
  56. return memdup(&vdso_info_init, sizeof(vdso_info_init));
  57. }
  58. static char *get_file(struct vdso_file *vdso_file)
  59. {
  60. char *vdso = NULL;
  61. char *buf = NULL;
  62. void *start, *end;
  63. size_t size;
  64. int fd;
  65. if (vdso_file->found)
  66. return vdso_file->temp_file_name;
  67. if (vdso_file->error || find_vdso_map(&start, &end))
  68. return NULL;
  69. size = end - start;
  70. buf = memdup(start, size);
  71. if (!buf)
  72. return NULL;
  73. fd = mkstemp(vdso_file->temp_file_name);
  74. if (fd < 0)
  75. goto out;
  76. if (size == (size_t) write(fd, buf, size))
  77. vdso = vdso_file->temp_file_name;
  78. close(fd);
  79. out:
  80. free(buf);
  81. vdso_file->found = (vdso != NULL);
  82. vdso_file->error = !vdso_file->found;
  83. return vdso;
  84. }
  85. void machine__exit_vdso(struct machine *machine)
  86. {
  87. struct vdso_info *vdso_info = machine->vdso_info;
  88. if (!vdso_info)
  89. return;
  90. if (vdso_info->vdso.found)
  91. unlink(vdso_info->vdso.temp_file_name);
  92. #if BITS_PER_LONG == 64
  93. if (vdso_info->vdso32.found)
  94. unlink(vdso_info->vdso32.temp_file_name);
  95. if (vdso_info->vdsox32.found)
  96. unlink(vdso_info->vdsox32.temp_file_name);
  97. #endif
  98. zfree(&machine->vdso_info);
  99. }
  100. static struct dso *__machine__addnew_vdso(struct machine *machine, const char *short_name,
  101. const char *long_name)
  102. {
  103. struct dso *dso;
  104. dso = dso__new(short_name);
  105. if (dso != NULL) {
  106. __dsos__add(&machine->dsos, dso);
  107. dso__set_long_name(dso, long_name, false);
  108. }
  109. return dso;
  110. }
  111. #if BITS_PER_LONG == 64
  112. static enum dso_type machine__thread_dso_type(struct machine *machine,
  113. struct thread *thread)
  114. {
  115. enum dso_type dso_type = DSO__TYPE_UNKNOWN;
  116. struct map *map;
  117. struct dso *dso;
  118. map = map_groups__first(thread->mg, MAP__FUNCTION);
  119. for (; map ; map = map_groups__next(map)) {
  120. dso = map->dso;
  121. if (!dso || dso->long_name[0] != '/')
  122. continue;
  123. dso_type = dso__type(dso, machine);
  124. if (dso_type != DSO__TYPE_UNKNOWN)
  125. break;
  126. }
  127. return dso_type;
  128. }
  129. static int vdso__do_copy_compat(FILE *f, int fd)
  130. {
  131. char buf[4096];
  132. size_t count;
  133. while (1) {
  134. count = fread(buf, 1, sizeof(buf), f);
  135. if (ferror(f))
  136. return -errno;
  137. if (feof(f))
  138. break;
  139. if (count && writen(fd, buf, count) != (ssize_t)count)
  140. return -errno;
  141. }
  142. return 0;
  143. }
  144. static int vdso__copy_compat(const char *prog, int fd)
  145. {
  146. FILE *f;
  147. int err;
  148. f = popen(prog, "r");
  149. if (!f)
  150. return -errno;
  151. err = vdso__do_copy_compat(f, fd);
  152. if (pclose(f) == -1)
  153. return -errno;
  154. return err;
  155. }
  156. static int vdso__create_compat_file(const char *prog, char *temp_name)
  157. {
  158. int fd, err;
  159. fd = mkstemp(temp_name);
  160. if (fd < 0)
  161. return -errno;
  162. err = vdso__copy_compat(prog, fd);
  163. if (close(fd) == -1)
  164. return -errno;
  165. return err;
  166. }
  167. static const char *vdso__get_compat_file(struct vdso_file *vdso_file)
  168. {
  169. int err;
  170. if (vdso_file->found)
  171. return vdso_file->temp_file_name;
  172. if (vdso_file->error)
  173. return NULL;
  174. err = vdso__create_compat_file(vdso_file->read_prog,
  175. vdso_file->temp_file_name);
  176. if (err) {
  177. pr_err("%s failed, error %d\n", vdso_file->read_prog, err);
  178. vdso_file->error = true;
  179. return NULL;
  180. }
  181. vdso_file->found = true;
  182. return vdso_file->temp_file_name;
  183. }
  184. static struct dso *__machine__findnew_compat(struct machine *machine,
  185. struct vdso_file *vdso_file)
  186. {
  187. const char *file_name;
  188. struct dso *dso;
  189. dso = __dsos__find(&machine->dsos, vdso_file->dso_name, true);
  190. if (dso)
  191. goto out;
  192. file_name = vdso__get_compat_file(vdso_file);
  193. if (!file_name)
  194. goto out;
  195. dso = __machine__addnew_vdso(machine, vdso_file->dso_name, file_name);
  196. out:
  197. return dso;
  198. }
  199. static int __machine__findnew_vdso_compat(struct machine *machine,
  200. struct thread *thread,
  201. struct vdso_info *vdso_info,
  202. struct dso **dso)
  203. {
  204. enum dso_type dso_type;
  205. dso_type = machine__thread_dso_type(machine, thread);
  206. #ifndef HAVE_PERF_READ_VDSO32
  207. if (dso_type == DSO__TYPE_32BIT)
  208. return 0;
  209. #endif
  210. #ifndef HAVE_PERF_READ_VDSOX32
  211. if (dso_type == DSO__TYPE_X32BIT)
  212. return 0;
  213. #endif
  214. switch (dso_type) {
  215. case DSO__TYPE_32BIT:
  216. *dso = __machine__findnew_compat(machine, &vdso_info->vdso32);
  217. return 1;
  218. case DSO__TYPE_X32BIT:
  219. *dso = __machine__findnew_compat(machine, &vdso_info->vdsox32);
  220. return 1;
  221. case DSO__TYPE_UNKNOWN:
  222. case DSO__TYPE_64BIT:
  223. default:
  224. return 0;
  225. }
  226. }
  227. #endif
  228. struct dso *machine__findnew_vdso(struct machine *machine,
  229. struct thread *thread __maybe_unused)
  230. {
  231. struct vdso_info *vdso_info;
  232. struct dso *dso = NULL;
  233. pthread_rwlock_wrlock(&machine->dsos.lock);
  234. if (!machine->vdso_info)
  235. machine->vdso_info = vdso_info__new();
  236. vdso_info = machine->vdso_info;
  237. if (!vdso_info)
  238. goto out_unlock;
  239. #if BITS_PER_LONG == 64
  240. if (__machine__findnew_vdso_compat(machine, thread, vdso_info, &dso))
  241. goto out_unlock;
  242. #endif
  243. dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, true);
  244. if (!dso) {
  245. char *file;
  246. file = get_file(&vdso_info->vdso);
  247. if (file)
  248. dso = __machine__addnew_vdso(machine, DSO__NAME_VDSO, file);
  249. }
  250. out_unlock:
  251. dso__get(dso);
  252. pthread_rwlock_unlock(&machine->dsos.lock);
  253. return dso;
  254. }
  255. bool dso__is_vdso(struct dso *dso)
  256. {
  257. return !strcmp(dso->short_name, DSO__NAME_VDSO) ||
  258. !strcmp(dso->short_name, DSO__NAME_VDSO32) ||
  259. !strcmp(dso->short_name, DSO__NAME_VDSOX32);
  260. }