vdso.c 7.1 KB

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