Merge branch 'locking-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / tools / perf / util / vdso.c
blob0acb1ec0e2f08c0ead7aa14835725b3a20f2fd76
1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <unistd.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <stdlib.h>
10 #include <linux/kernel.h>
12 #include "vdso.h"
13 #include "util.h"
14 #include "symbol.h"
15 #include "machine.h"
16 #include "thread.h"
17 #include "linux/string.h"
18 #include "debug.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"
28 struct vdso_file {
29 bool found;
30 bool error;
31 char temp_file_name[sizeof(VDSO__TEMP_FILE_NAME)];
32 const char *dso_name;
33 const char *read_prog;
36 struct vdso_info {
37 struct vdso_file vdso;
38 #if BITS_PER_LONG == 64
39 struct vdso_file vdso32;
40 struct vdso_file vdsox32;
41 #endif
44 static struct vdso_info *vdso_info__new(void)
46 static const struct vdso_info vdso_info_init = {
47 .vdso = {
48 .temp_file_name = VDSO__TEMP_FILE_NAME,
49 .dso_name = DSO__NAME_VDSO,
51 #if BITS_PER_LONG == 64
52 .vdso32 = {
53 .temp_file_name = VDSO__TEMP_FILE_NAME,
54 .dso_name = DSO__NAME_VDSO32,
55 .read_prog = "perf-read-vdso32",
57 .vdsox32 = {
58 .temp_file_name = VDSO__TEMP_FILE_NAME,
59 .dso_name = DSO__NAME_VDSOX32,
60 .read_prog = "perf-read-vdsox32",
62 #endif
65 return memdup(&vdso_info_init, sizeof(vdso_info_init));
68 static char *get_file(struct vdso_file *vdso_file)
70 char *vdso = NULL;
71 char *buf = NULL;
72 void *start, *end;
73 size_t size;
74 int fd;
76 if (vdso_file->found)
77 return vdso_file->temp_file_name;
79 if (vdso_file->error || find_vdso_map(&start, &end))
80 return NULL;
82 size = end - start;
84 buf = memdup(start, size);
85 if (!buf)
86 return NULL;
88 fd = mkstemp(vdso_file->temp_file_name);
89 if (fd < 0)
90 goto out;
92 if (size == (size_t) write(fd, buf, size))
93 vdso = vdso_file->temp_file_name;
95 close(fd);
97 out:
98 free(buf);
100 vdso_file->found = (vdso != NULL);
101 vdso_file->error = !vdso_file->found;
102 return vdso;
105 void machine__exit_vdso(struct machine *machine)
107 struct vdso_info *vdso_info = machine->vdso_info;
109 if (!vdso_info)
110 return;
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);
119 #endif
121 zfree(&machine->vdso_info);
124 static struct dso *__machine__addnew_vdso(struct machine *machine, const char *short_name,
125 const char *long_name)
127 struct dso *dso;
129 dso = dso__new(short_name);
130 if (dso != NULL) {
131 __dsos__add(&machine->dsos, dso);
132 dso__set_long_name(dso, long_name, false);
135 return dso;
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;
142 struct map *map;
143 struct dso *dso;
145 map = map_groups__first(thread->mg, MAP__FUNCTION);
146 for (; map ; map = map_groups__next(map)) {
147 dso = map->dso;
148 if (!dso || dso->long_name[0] != '/')
149 continue;
150 dso_type = dso__type(dso, machine);
151 if (dso_type != DSO__TYPE_UNKNOWN)
152 break;
155 return dso_type;
158 #if BITS_PER_LONG == 64
160 static int vdso__do_copy_compat(FILE *f, int fd)
162 char buf[4096];
163 size_t count;
165 while (1) {
166 count = fread(buf, 1, sizeof(buf), f);
167 if (ferror(f))
168 return -errno;
169 if (feof(f))
170 break;
171 if (count && writen(fd, buf, count) != (ssize_t)count)
172 return -errno;
175 return 0;
178 static int vdso__copy_compat(const char *prog, int fd)
180 FILE *f;
181 int err;
183 f = popen(prog, "r");
184 if (!f)
185 return -errno;
187 err = vdso__do_copy_compat(f, fd);
189 if (pclose(f) == -1)
190 return -errno;
192 return err;
195 static int vdso__create_compat_file(const char *prog, char *temp_name)
197 int fd, err;
199 fd = mkstemp(temp_name);
200 if (fd < 0)
201 return -errno;
203 err = vdso__copy_compat(prog, fd);
205 if (close(fd) == -1)
206 return -errno;
208 return err;
211 static const char *vdso__get_compat_file(struct vdso_file *vdso_file)
213 int err;
215 if (vdso_file->found)
216 return vdso_file->temp_file_name;
218 if (vdso_file->error)
219 return NULL;
221 err = vdso__create_compat_file(vdso_file->read_prog,
222 vdso_file->temp_file_name);
223 if (err) {
224 pr_err("%s failed, error %d\n", vdso_file->read_prog, err);
225 vdso_file->error = true;
226 return NULL;
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;
238 struct dso *dso;
240 dso = __dsos__find(&machine->dsos, vdso_file->dso_name, true);
241 if (dso)
242 goto out;
244 file_name = vdso__get_compat_file(vdso_file);
245 if (!file_name)
246 goto out;
248 dso = __machine__addnew_vdso(machine, vdso_file->dso_name, file_name);
249 out:
250 return dso;
253 static int __machine__findnew_vdso_compat(struct machine *machine,
254 struct thread *thread,
255 struct vdso_info *vdso_info,
256 struct dso **dso)
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)
264 return 0;
265 #endif
266 #ifndef HAVE_PERF_READ_VDSOX32
267 if (dso_type == DSO__TYPE_X32BIT)
268 return 0;
269 #endif
271 switch (dso_type) {
272 case DSO__TYPE_32BIT:
273 *dso = __machine__findnew_compat(machine, &vdso_info->vdso32);
274 return 1;
275 case DSO__TYPE_X32BIT:
276 *dso = __machine__findnew_compat(machine, &vdso_info->vdsox32);
277 return 1;
278 case DSO__TYPE_UNKNOWN:
279 case DSO__TYPE_64BIT:
280 default:
281 return 0;
285 #endif
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);
294 switch (dso_type) {
295 case DSO__TYPE_32BIT:
296 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO32, true);
297 if (!dso) {
298 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO,
299 true);
300 if (dso && dso_type != dso__type(dso, machine))
301 dso = NULL;
303 break;
304 case DSO__TYPE_X32BIT:
305 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSOX32, true);
306 break;
307 case DSO__TYPE_64BIT:
308 case DSO__TYPE_UNKNOWN:
309 default:
310 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, true);
311 break;
314 return dso;
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;
328 if (!vdso_info)
329 goto out_unlock;
331 dso = machine__find_vdso(machine, thread);
332 if (dso)
333 goto out_unlock;
335 #if BITS_PER_LONG == 64
336 if (__machine__findnew_vdso_compat(machine, thread, vdso_info, &dso))
337 goto out_unlock;
338 #endif
340 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, true);
341 if (!dso) {
342 char *file;
344 file = get_file(&vdso_info->vdso);
345 if (file)
346 dso = __machine__addnew_vdso(machine, DSO__NAME_VDSO, file);
349 out_unlock:
350 dso__get(dso);
351 up_write(&machine->dsos.lock);
352 return dso;
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);