1 // SPDX-License-Identifier: GPL-2.0
10 #include <linux/kernel.h>
17 #include "linux/string.h"
21 * Include definition of find_vdso_map() also used in perf-read-vdso.c for
22 * building perf-read-vdso32 and perf-read-vdsox32.
24 #include "find-vdso-map.c"
26 #define VDSO__TEMP_FILE_NAME "/tmp/perf-vdso.so-XXXXXX"
31 char temp_file_name
[sizeof(VDSO__TEMP_FILE_NAME
)];
33 const char *read_prog
;
37 struct vdso_file vdso
;
38 #if BITS_PER_LONG == 64
39 struct vdso_file vdso32
;
40 struct vdso_file vdsox32
;
44 static struct vdso_info
*vdso_info__new(void)
46 static const struct vdso_info vdso_info_init
= {
48 .temp_file_name
= VDSO__TEMP_FILE_NAME
,
49 .dso_name
= DSO__NAME_VDSO
,
51 #if BITS_PER_LONG == 64
53 .temp_file_name
= VDSO__TEMP_FILE_NAME
,
54 .dso_name
= DSO__NAME_VDSO32
,
55 .read_prog
= "perf-read-vdso32",
58 .temp_file_name
= VDSO__TEMP_FILE_NAME
,
59 .dso_name
= DSO__NAME_VDSOX32
,
60 .read_prog
= "perf-read-vdsox32",
65 return memdup(&vdso_info_init
, sizeof(vdso_info_init
));
68 static char *get_file(struct vdso_file
*vdso_file
)
77 return vdso_file
->temp_file_name
;
79 if (vdso_file
->error
|| find_vdso_map(&start
, &end
))
84 buf
= memdup(start
, size
);
88 fd
= mkstemp(vdso_file
->temp_file_name
);
92 if (size
== (size_t) write(fd
, buf
, size
))
93 vdso
= vdso_file
->temp_file_name
;
100 vdso_file
->found
= (vdso
!= NULL
);
101 vdso_file
->error
= !vdso_file
->found
;
105 void machine__exit_vdso(struct machine
*machine
)
107 struct vdso_info
*vdso_info
= machine
->vdso_info
;
112 if (vdso_info
->vdso
.found
)
113 unlink(vdso_info
->vdso
.temp_file_name
);
114 #if BITS_PER_LONG == 64
115 if (vdso_info
->vdso32
.found
)
116 unlink(vdso_info
->vdso32
.temp_file_name
);
117 if (vdso_info
->vdsox32
.found
)
118 unlink(vdso_info
->vdsox32
.temp_file_name
);
121 zfree(&machine
->vdso_info
);
124 static struct dso
*__machine__addnew_vdso(struct machine
*machine
, const char *short_name
,
125 const char *long_name
)
129 dso
= dso__new(short_name
);
131 __dsos__add(&machine
->dsos
, dso
);
132 dso__set_long_name(dso
, long_name
, false);
138 static enum dso_type
machine__thread_dso_type(struct machine
*machine
,
139 struct thread
*thread
)
141 enum dso_type dso_type
= DSO__TYPE_UNKNOWN
;
145 map
= map_groups__first(thread
->mg
, MAP__FUNCTION
);
146 for (; map
; map
= map_groups__next(map
)) {
148 if (!dso
|| dso
->long_name
[0] != '/')
150 dso_type
= dso__type(dso
, machine
);
151 if (dso_type
!= DSO__TYPE_UNKNOWN
)
158 #if BITS_PER_LONG == 64
160 static int vdso__do_copy_compat(FILE *f
, int fd
)
166 count
= fread(buf
, 1, sizeof(buf
), f
);
171 if (count
&& writen(fd
, buf
, count
) != (ssize_t
)count
)
178 static int vdso__copy_compat(const char *prog
, int fd
)
183 f
= popen(prog
, "r");
187 err
= vdso__do_copy_compat(f
, fd
);
195 static int vdso__create_compat_file(const char *prog
, char *temp_name
)
199 fd
= mkstemp(temp_name
);
203 err
= vdso__copy_compat(prog
, fd
);
211 static const char *vdso__get_compat_file(struct vdso_file
*vdso_file
)
215 if (vdso_file
->found
)
216 return vdso_file
->temp_file_name
;
218 if (vdso_file
->error
)
221 err
= vdso__create_compat_file(vdso_file
->read_prog
,
222 vdso_file
->temp_file_name
);
224 pr_err("%s failed, error %d\n", vdso_file
->read_prog
, err
);
225 vdso_file
->error
= true;
229 vdso_file
->found
= true;
231 return vdso_file
->temp_file_name
;
234 static struct dso
*__machine__findnew_compat(struct machine
*machine
,
235 struct vdso_file
*vdso_file
)
237 const char *file_name
;
240 dso
= __dsos__find(&machine
->dsos
, vdso_file
->dso_name
, true);
244 file_name
= vdso__get_compat_file(vdso_file
);
248 dso
= __machine__addnew_vdso(machine
, vdso_file
->dso_name
, file_name
);
253 static int __machine__findnew_vdso_compat(struct machine
*machine
,
254 struct thread
*thread
,
255 struct vdso_info
*vdso_info
,
258 enum dso_type dso_type
;
260 dso_type
= machine__thread_dso_type(machine
, thread
);
262 #ifndef HAVE_PERF_READ_VDSO32
263 if (dso_type
== DSO__TYPE_32BIT
)
266 #ifndef HAVE_PERF_READ_VDSOX32
267 if (dso_type
== DSO__TYPE_X32BIT
)
272 case DSO__TYPE_32BIT
:
273 *dso
= __machine__findnew_compat(machine
, &vdso_info
->vdso32
);
275 case DSO__TYPE_X32BIT
:
276 *dso
= __machine__findnew_compat(machine
, &vdso_info
->vdsox32
);
278 case DSO__TYPE_UNKNOWN
:
279 case DSO__TYPE_64BIT
:
287 static struct dso
*machine__find_vdso(struct machine
*machine
,
288 struct thread
*thread
)
290 struct dso
*dso
= NULL
;
291 enum dso_type dso_type
;
293 dso_type
= machine__thread_dso_type(machine
, thread
);
295 case DSO__TYPE_32BIT
:
296 dso
= __dsos__find(&machine
->dsos
, DSO__NAME_VDSO32
, true);
298 dso
= __dsos__find(&machine
->dsos
, DSO__NAME_VDSO
,
300 if (dso
&& dso_type
!= dso__type(dso
, machine
))
304 case DSO__TYPE_X32BIT
:
305 dso
= __dsos__find(&machine
->dsos
, DSO__NAME_VDSOX32
, true);
307 case DSO__TYPE_64BIT
:
308 case DSO__TYPE_UNKNOWN
:
310 dso
= __dsos__find(&machine
->dsos
, DSO__NAME_VDSO
, true);
317 struct dso
*machine__findnew_vdso(struct machine
*machine
,
318 struct thread
*thread
)
320 struct vdso_info
*vdso_info
;
321 struct dso
*dso
= NULL
;
323 down_write(&machine
->dsos
.lock
);
324 if (!machine
->vdso_info
)
325 machine
->vdso_info
= vdso_info__new();
327 vdso_info
= machine
->vdso_info
;
331 dso
= machine__find_vdso(machine
, thread
);
335 #if BITS_PER_LONG == 64
336 if (__machine__findnew_vdso_compat(machine
, thread
, vdso_info
, &dso
))
340 dso
= __dsos__find(&machine
->dsos
, DSO__NAME_VDSO
, true);
344 file
= get_file(&vdso_info
->vdso
);
346 dso
= __machine__addnew_vdso(machine
, DSO__NAME_VDSO
, file
);
351 up_write(&machine
->dsos
.lock
);
355 bool dso__is_vdso(struct dso
*dso
)
357 return !strcmp(dso
->short_name
, DSO__NAME_VDSO
) ||
358 !strcmp(dso
->short_name
, DSO__NAME_VDSO32
) ||
359 !strcmp(dso
->short_name
, DSO__NAME_VDSOX32
);