1 // SPDX-License-Identifier: GPL-2.0
10 #include <linux/kernel.h>
14 #include <internal/lib.h>
19 #include "linux/string.h"
20 #include <linux/zalloc.h>
24 * Include definition of find_map() also used in perf-read-vdso.c for
25 * building perf-read-vdso32 and perf-read-vdsox32.
29 #define VDSO__TEMP_FILE_NAME "/tmp/perf-vdso.so-XXXXXX"
34 char temp_file_name[sizeof(VDSO__TEMP_FILE_NAME)];
36 const char *read_prog;
40 struct vdso_file vdso;
41 #if BITS_PER_LONG == 64
42 struct vdso_file vdso32;
43 struct vdso_file vdsox32;
47 static struct vdso_info *vdso_info__new(void)
49 static const struct vdso_info vdso_info_init = {
51 .temp_file_name = VDSO__TEMP_FILE_NAME,
52 .dso_name = DSO__NAME_VDSO,
54 #if BITS_PER_LONG == 64
56 .temp_file_name = VDSO__TEMP_FILE_NAME,
57 .dso_name = DSO__NAME_VDSO32,
58 .read_prog = "perf-read-vdso32",
61 .temp_file_name = VDSO__TEMP_FILE_NAME,
62 .dso_name = DSO__NAME_VDSOX32,
63 .read_prog = "perf-read-vdsox32",
68 return memdup(&vdso_info_init, sizeof(vdso_info_init));
71 static char *get_file(struct vdso_file *vdso_file)
80 return vdso_file->temp_file_name;
82 if (vdso_file->error || find_map(&start, &end, VDSO__MAP_NAME))
87 buf = memdup(start, size);
91 fd = mkstemp(vdso_file->temp_file_name);
95 if (size == (size_t) write(fd, buf, size))
96 vdso = vdso_file->temp_file_name;
103 vdso_file->found = (vdso != NULL);
104 vdso_file->error = !vdso_file->found;
108 void machine__exit_vdso(struct machine *machine)
110 struct vdso_info *vdso_info = machine->vdso_info;
115 if (vdso_info->vdso.found)
116 unlink(vdso_info->vdso.temp_file_name);
117 #if BITS_PER_LONG == 64
118 if (vdso_info->vdso32.found)
119 unlink(vdso_info->vdso32.temp_file_name);
120 if (vdso_info->vdsox32.found)
121 unlink(vdso_info->vdsox32.temp_file_name);
124 zfree(&machine->vdso_info);
127 static struct dso *__machine__addnew_vdso(struct machine *machine, const char *short_name,
128 const char *long_name)
132 dso = dso__new(short_name);
134 __dsos__add(&machine->dsos, dso);
135 dso__set_long_name(dso, long_name, false);
136 /* Put dso here because __dsos_add already got it */
143 static enum dso_type machine__thread_dso_type(struct machine *machine,
144 struct thread *thread)
146 enum dso_type dso_type = DSO__TYPE_UNKNOWN;
147 struct map_rb_node *rb_node;
149 maps__for_each_entry(thread__maps(thread), rb_node) {
150 struct dso *dso = map__dso(rb_node->map);
152 if (!dso || dso->long_name[0] != '/')
154 dso_type = dso__type(dso, machine);
155 if (dso_type != DSO__TYPE_UNKNOWN)
162 #if BITS_PER_LONG == 64
164 static int vdso__do_copy_compat(FILE *f, int fd)
170 count = fread(buf, 1, sizeof(buf), f);
175 if (count && writen(fd, buf, count) != (ssize_t)count)
182 static int vdso__copy_compat(const char *prog, int fd)
187 f = popen(prog, "r");
191 err = vdso__do_copy_compat(f, fd);
199 static int vdso__create_compat_file(const char *prog, char *temp_name)
203 fd = mkstemp(temp_name);
207 err = vdso__copy_compat(prog, fd);
215 static const char *vdso__get_compat_file(struct vdso_file *vdso_file)
219 if (vdso_file->found)
220 return vdso_file->temp_file_name;
222 if (vdso_file->error)
225 err = vdso__create_compat_file(vdso_file->read_prog,
226 vdso_file->temp_file_name);
228 pr_err("%s failed, error %d\n", vdso_file->read_prog, err);
229 vdso_file->error = true;
233 vdso_file->found = true;
235 return vdso_file->temp_file_name;
238 static struct dso *__machine__findnew_compat(struct machine *machine,
239 struct vdso_file *vdso_file)
241 const char *file_name;
244 dso = __dsos__find(&machine->dsos, vdso_file->dso_name, true);
248 file_name = vdso__get_compat_file(vdso_file);
252 dso = __machine__addnew_vdso(machine, vdso_file->dso_name, file_name);
257 static int __machine__findnew_vdso_compat(struct machine *machine,
258 struct thread *thread,
259 struct vdso_info *vdso_info,
262 enum dso_type dso_type;
264 dso_type = machine__thread_dso_type(machine, thread);
266 #ifndef HAVE_PERF_READ_VDSO32
267 if (dso_type == DSO__TYPE_32BIT)
270 #ifndef HAVE_PERF_READ_VDSOX32
271 if (dso_type == DSO__TYPE_X32BIT)
276 case DSO__TYPE_32BIT:
277 *dso = __machine__findnew_compat(machine, &vdso_info->vdso32);
279 case DSO__TYPE_X32BIT:
280 *dso = __machine__findnew_compat(machine, &vdso_info->vdsox32);
282 case DSO__TYPE_UNKNOWN:
283 case DSO__TYPE_64BIT:
291 static struct dso *machine__find_vdso(struct machine *machine,
292 struct thread *thread)
294 struct dso *dso = NULL;
295 enum dso_type dso_type;
297 dso_type = machine__thread_dso_type(machine, thread);
299 case DSO__TYPE_32BIT:
300 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO32, true);
302 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO,
304 if (dso && dso_type != dso__type(dso, machine))
308 case DSO__TYPE_X32BIT:
309 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSOX32, true);
311 case DSO__TYPE_64BIT:
312 case DSO__TYPE_UNKNOWN:
314 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, true);
321 struct dso *machine__findnew_vdso(struct machine *machine,
322 struct thread *thread)
324 struct vdso_info *vdso_info;
325 struct dso *dso = NULL;
327 down_write(&machine->dsos.lock);
328 if (!machine->vdso_info)
329 machine->vdso_info = vdso_info__new();
331 vdso_info = machine->vdso_info;
335 dso = machine__find_vdso(machine, thread);
339 #if BITS_PER_LONG == 64
340 if (__machine__findnew_vdso_compat(machine, thread, vdso_info, &dso))
344 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, true);
348 file = get_file(&vdso_info->vdso);
350 dso = __machine__addnew_vdso(machine, DSO__NAME_VDSO, file);
355 up_write(&machine->dsos.lock);
359 bool dso__is_vdso(struct dso *dso)
361 return !strcmp(dso->short_name, DSO__NAME_VDSO) ||
362 !strcmp(dso->short_name, DSO__NAME_VDSO32) ||
363 !strcmp(dso->short_name, DSO__NAME_VDSOX32);