1 // SPDX-License-Identifier: GPL-2.0
10 #include <linux/kernel.h>
18 #include "linux/string.h"
22 * Include definition of find_map() also used in perf-read-vdso.c for
23 * building perf-read-vdso32 and perf-read-vdsox32.
27 #define VDSO__TEMP_FILE_NAME "/tmp/perf-vdso.so-XXXXXX"
32 char temp_file_name
[sizeof(VDSO__TEMP_FILE_NAME
)];
34 const char *read_prog
;
38 struct vdso_file vdso
;
39 #if BITS_PER_LONG == 64
40 struct vdso_file vdso32
;
41 struct vdso_file vdsox32
;
45 static struct vdso_info
*vdso_info__new(void)
47 static const struct vdso_info vdso_info_init
= {
49 .temp_file_name
= VDSO__TEMP_FILE_NAME
,
50 .dso_name
= DSO__NAME_VDSO
,
52 #if BITS_PER_LONG == 64
54 .temp_file_name
= VDSO__TEMP_FILE_NAME
,
55 .dso_name
= DSO__NAME_VDSO32
,
56 .read_prog
= "perf-read-vdso32",
59 .temp_file_name
= VDSO__TEMP_FILE_NAME
,
60 .dso_name
= DSO__NAME_VDSOX32
,
61 .read_prog
= "perf-read-vdsox32",
66 return memdup(&vdso_info_init
, sizeof(vdso_info_init
));
69 static char *get_file(struct vdso_file
*vdso_file
)
78 return vdso_file
->temp_file_name
;
80 if (vdso_file
->error
|| find_map(&start
, &end
, VDSO__MAP_NAME
))
85 buf
= memdup(start
, size
);
89 fd
= mkstemp(vdso_file
->temp_file_name
);
93 if (size
== (size_t) write(fd
, buf
, size
))
94 vdso
= vdso_file
->temp_file_name
;
101 vdso_file
->found
= (vdso
!= NULL
);
102 vdso_file
->error
= !vdso_file
->found
;
106 void machine__exit_vdso(struct machine
*machine
)
108 struct vdso_info
*vdso_info
= machine
->vdso_info
;
113 if (vdso_info
->vdso
.found
)
114 unlink(vdso_info
->vdso
.temp_file_name
);
115 #if BITS_PER_LONG == 64
116 if (vdso_info
->vdso32
.found
)
117 unlink(vdso_info
->vdso32
.temp_file_name
);
118 if (vdso_info
->vdsox32
.found
)
119 unlink(vdso_info
->vdsox32
.temp_file_name
);
122 zfree(&machine
->vdso_info
);
125 static struct dso
*__machine__addnew_vdso(struct machine
*machine
, const char *short_name
,
126 const char *long_name
)
130 dso
= dso__new(short_name
);
132 __dsos__add(&machine
->dsos
, dso
);
133 dso__set_long_name(dso
, long_name
, false);
139 static enum dso_type
machine__thread_dso_type(struct machine
*machine
,
140 struct thread
*thread
)
142 enum dso_type dso_type
= DSO__TYPE_UNKNOWN
;
143 struct map
*map
= map_groups__first(thread
->mg
);
145 for (; map
; map
= map_groups__next(map
)) {
146 struct dso
*dso
= map
->dso
;
147 if (!dso
|| dso
->long_name
[0] != '/')
149 dso_type
= dso__type(dso
, machine
);
150 if (dso_type
!= DSO__TYPE_UNKNOWN
)
157 #if BITS_PER_LONG == 64
159 static int vdso__do_copy_compat(FILE *f
, int fd
)
165 count
= fread(buf
, 1, sizeof(buf
), f
);
170 if (count
&& writen(fd
, buf
, count
) != (ssize_t
)count
)
177 static int vdso__copy_compat(const char *prog
, int fd
)
182 f
= popen(prog
, "r");
186 err
= vdso__do_copy_compat(f
, fd
);
194 static int vdso__create_compat_file(const char *prog
, char *temp_name
)
198 fd
= mkstemp(temp_name
);
202 err
= vdso__copy_compat(prog
, fd
);
210 static const char *vdso__get_compat_file(struct vdso_file
*vdso_file
)
214 if (vdso_file
->found
)
215 return vdso_file
->temp_file_name
;
217 if (vdso_file
->error
)
220 err
= vdso__create_compat_file(vdso_file
->read_prog
,
221 vdso_file
->temp_file_name
);
223 pr_err("%s failed, error %d\n", vdso_file
->read_prog
, err
);
224 vdso_file
->error
= true;
228 vdso_file
->found
= true;
230 return vdso_file
->temp_file_name
;
233 static struct dso
*__machine__findnew_compat(struct machine
*machine
,
234 struct vdso_file
*vdso_file
)
236 const char *file_name
;
239 dso
= __dsos__find(&machine
->dsos
, vdso_file
->dso_name
, true);
243 file_name
= vdso__get_compat_file(vdso_file
);
247 dso
= __machine__addnew_vdso(machine
, vdso_file
->dso_name
, file_name
);
252 static int __machine__findnew_vdso_compat(struct machine
*machine
,
253 struct thread
*thread
,
254 struct vdso_info
*vdso_info
,
257 enum dso_type dso_type
;
259 dso_type
= machine__thread_dso_type(machine
, thread
);
261 #ifndef HAVE_PERF_READ_VDSO32
262 if (dso_type
== DSO__TYPE_32BIT
)
265 #ifndef HAVE_PERF_READ_VDSOX32
266 if (dso_type
== DSO__TYPE_X32BIT
)
271 case DSO__TYPE_32BIT
:
272 *dso
= __machine__findnew_compat(machine
, &vdso_info
->vdso32
);
274 case DSO__TYPE_X32BIT
:
275 *dso
= __machine__findnew_compat(machine
, &vdso_info
->vdsox32
);
277 case DSO__TYPE_UNKNOWN
:
278 case DSO__TYPE_64BIT
:
286 static struct dso
*machine__find_vdso(struct machine
*machine
,
287 struct thread
*thread
)
289 struct dso
*dso
= NULL
;
290 enum dso_type dso_type
;
292 dso_type
= machine__thread_dso_type(machine
, thread
);
294 case DSO__TYPE_32BIT
:
295 dso
= __dsos__find(&machine
->dsos
, DSO__NAME_VDSO32
, true);
297 dso
= __dsos__find(&machine
->dsos
, DSO__NAME_VDSO
,
299 if (dso
&& dso_type
!= dso__type(dso
, machine
))
303 case DSO__TYPE_X32BIT
:
304 dso
= __dsos__find(&machine
->dsos
, DSO__NAME_VDSOX32
, true);
306 case DSO__TYPE_64BIT
:
307 case DSO__TYPE_UNKNOWN
:
309 dso
= __dsos__find(&machine
->dsos
, DSO__NAME_VDSO
, true);
316 struct dso
*machine__findnew_vdso(struct machine
*machine
,
317 struct thread
*thread
)
319 struct vdso_info
*vdso_info
;
320 struct dso
*dso
= NULL
;
322 down_write(&machine
->dsos
.lock
);
323 if (!machine
->vdso_info
)
324 machine
->vdso_info
= vdso_info__new();
326 vdso_info
= machine
->vdso_info
;
330 dso
= machine__find_vdso(machine
, thread
);
334 #if BITS_PER_LONG == 64
335 if (__machine__findnew_vdso_compat(machine
, thread
, vdso_info
, &dso
))
339 dso
= __dsos__find(&machine
->dsos
, DSO__NAME_VDSO
, true);
343 file
= get_file(&vdso_info
->vdso
);
345 dso
= __machine__addnew_vdso(machine
, DSO__NAME_VDSO
, file
);
350 up_write(&machine
->dsos
.lock
);
354 bool dso__is_vdso(struct dso
*dso
)
356 return !strcmp(dso
->short_name
, DSO__NAME_VDSO
) ||
357 !strcmp(dso
->short_name
, DSO__NAME_VDSO32
) ||
358 !strcmp(dso
->short_name
, DSO__NAME_VDSOX32
);