9 #include <linux/kernel.h>
15 #include "linux/string.h"
18 #define VDSO__TEMP_FILE_NAME "/tmp/perf-vdso.so-XXXXXX"
23 char temp_file_name[sizeof(VDSO__TEMP_FILE_NAME)];
28 struct vdso_file vdso;
31 static struct vdso_info *vdso_info__new(void)
33 static const struct vdso_info vdso_info_init = {
35 .temp_file_name = VDSO__TEMP_FILE_NAME,
36 .dso_name = DSO__NAME_VDSO,
40 return memdup(&vdso_info_init, sizeof(vdso_info_init));
43 static int find_vdso_map(void **start, void **end)
49 maps = fopen("/proc/self/maps", "r");
51 pr_err("vdso: cannot open maps\n");
55 while (!found && fgets(line, sizeof(line), maps)) {
58 /* We care only about private r-x mappings. */
59 if (2 != sscanf(line, "%p-%p r-xp %*x %*x:%*x %*u %n",
65 if (!strncmp(&line[m], VDSO__MAP_NAME,
66 sizeof(VDSO__MAP_NAME) - 1))
74 static char *get_file(struct vdso_file *vdso_file)
83 return vdso_file->temp_file_name;
85 if (vdso_file->error || find_vdso_map(&start, &end))
90 buf = memdup(start, size);
94 fd = mkstemp(vdso_file->temp_file_name);
98 if (size == (size_t) write(fd, buf, size))
99 vdso = vdso_file->temp_file_name;
106 vdso_file->found = (vdso != NULL);
107 vdso_file->error = !vdso_file->found;
111 void vdso__exit(struct machine *machine)
113 struct vdso_info *vdso_info = machine->vdso_info;
118 if (vdso_info->vdso.found)
119 unlink(vdso_info->vdso.temp_file_name);
121 zfree(&machine->vdso_info);
124 static struct dso *vdso__new(struct machine *machine, const char *short_name,
125 const char *long_name)
129 dso = dso__new(short_name);
131 dsos__add(&machine->user_dsos, dso);
132 dso__set_long_name(dso, long_name, false);
138 struct dso *vdso__dso_findnew(struct machine *machine,
139 struct thread *thread __maybe_unused)
141 struct vdso_info *vdso_info;
144 if (!machine->vdso_info)
145 machine->vdso_info = vdso_info__new();
147 vdso_info = machine->vdso_info;
151 dso = dsos__find(&machine->user_dsos, DSO__NAME_VDSO, true);
155 file = get_file(&vdso_info->vdso);
159 dso = vdso__new(machine, DSO__NAME_VDSO, file);
165 bool dso__is_vdso(struct dso *dso)
167 return !strcmp(dso->short_name, DSO__NAME_VDSO);