vdso.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "linux/string.h"
  14. #include "debug.h"
  15. #define VDSO__TEMP_FILE_NAME "/tmp/perf-vdso.so-XXXXXX"
  16. struct vdso_file {
  17. bool found;
  18. bool error;
  19. char temp_file_name[sizeof(VDSO__TEMP_FILE_NAME)];
  20. const char *dso_name;
  21. };
  22. struct vdso_info {
  23. struct vdso_file vdso;
  24. };
  25. static struct vdso_info *vdso_info__new(void)
  26. {
  27. static const struct vdso_info vdso_info_init = {
  28. .vdso = {
  29. .temp_file_name = VDSO__TEMP_FILE_NAME,
  30. .dso_name = DSO__NAME_VDSO,
  31. },
  32. };
  33. return memdup(&vdso_info_init, sizeof(vdso_info_init));
  34. }
  35. static int find_vdso_map(void **start, void **end)
  36. {
  37. FILE *maps;
  38. char line[128];
  39. int found = 0;
  40. maps = fopen("/proc/self/maps", "r");
  41. if (!maps) {
  42. pr_err("vdso: cannot open maps\n");
  43. return -1;
  44. }
  45. while (!found && fgets(line, sizeof(line), maps)) {
  46. int m = -1;
  47. /* We care only about private r-x mappings. */
  48. if (2 != sscanf(line, "%p-%p r-xp %*x %*x:%*x %*u %n",
  49. start, end, &m))
  50. continue;
  51. if (m < 0)
  52. continue;
  53. if (!strncmp(&line[m], VDSO__MAP_NAME,
  54. sizeof(VDSO__MAP_NAME) - 1))
  55. found = 1;
  56. }
  57. fclose(maps);
  58. return !found;
  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 vdso__exit(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. zfree(&machine->vdso_info);
  95. }
  96. static struct dso *vdso__new(struct machine *machine, const char *short_name,
  97. const char *long_name)
  98. {
  99. struct dso *dso;
  100. dso = dso__new(short_name);
  101. if (dso != NULL) {
  102. dsos__add(&machine->user_dsos, dso);
  103. dso__set_long_name(dso, long_name, false);
  104. }
  105. return dso;
  106. }
  107. struct dso *vdso__dso_findnew(struct machine *machine,
  108. struct thread *thread __maybe_unused)
  109. {
  110. struct vdso_info *vdso_info;
  111. struct dso *dso;
  112. if (!machine->vdso_info)
  113. machine->vdso_info = vdso_info__new();
  114. vdso_info = machine->vdso_info;
  115. if (!vdso_info)
  116. return NULL;
  117. dso = dsos__find(&machine->user_dsos, DSO__NAME_VDSO, true);
  118. if (!dso) {
  119. char *file;
  120. file = get_file(&vdso_info->vdso);
  121. if (!file)
  122. return NULL;
  123. dso = vdso__new(machine, DSO__NAME_VDSO, file);
  124. }
  125. return dso;
  126. }
  127. bool dso__is_vdso(struct dso *dso)
  128. {
  129. return !strcmp(dso->short_name, DSO__NAME_VDSO);
  130. }