vdso.c 7.2 KB

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