Loading...
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>
11
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"
19
20/*
21 * Include definition of find_vdso_map() also used in perf-read-vdso.c for
22 * building perf-read-vdso32 and perf-read-vdsox32.
23 */
24#include "find-vdso-map.c"
25
26#define VDSO__TEMP_FILE_NAME "/tmp/perf-vdso.so-XXXXXX"
27
28struct 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;
34};
35
36struct 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
42};
43
44static struct vdso_info *vdso_info__new(void)
45{
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,
50 },
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",
56 },
57 .vdsox32 = {
58 .temp_file_name = VDSO__TEMP_FILE_NAME,
59 .dso_name = DSO__NAME_VDSOX32,
60 .read_prog = "perf-read-vdsox32",
61 },
62#endif
63 };
64
65 return memdup(&vdso_info_init, sizeof(vdso_info_init));
66}
67
68static char *get_file(struct vdso_file *vdso_file)
69{
70 char *vdso = NULL;
71 char *buf = NULL;
72 void *start, *end;
73 size_t size;
74 int fd;
75
76 if (vdso_file->found)
77 return vdso_file->temp_file_name;
78
79 if (vdso_file->error || find_vdso_map(&start, &end))
80 return NULL;
81
82 size = end - start;
83
84 buf = memdup(start, size);
85 if (!buf)
86 return NULL;
87
88 fd = mkstemp(vdso_file->temp_file_name);
89 if (fd < 0)
90 goto out;
91
92 if (size == (size_t) write(fd, buf, size))
93 vdso = vdso_file->temp_file_name;
94
95 close(fd);
96
97 out:
98 free(buf);
99
100 vdso_file->found = (vdso != NULL);
101 vdso_file->error = !vdso_file->found;
102 return vdso;
103}
104
105void machine__exit_vdso(struct machine *machine)
106{
107 struct vdso_info *vdso_info = machine->vdso_info;
108
109 if (!vdso_info)
110 return;
111
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
120
121 zfree(&machine->vdso_info);
122}
123
124static struct dso *__machine__addnew_vdso(struct machine *machine, const char *short_name,
125 const char *long_name)
126{
127 struct dso *dso;
128
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);
133 }
134
135 return dso;
136}
137
138static enum dso_type machine__thread_dso_type(struct machine *machine,
139 struct thread *thread)
140{
141 enum dso_type dso_type = DSO__TYPE_UNKNOWN;
142 struct map *map;
143 struct dso *dso;
144
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;
153 }
154
155 return dso_type;
156}
157
158#if BITS_PER_LONG == 64
159
160static int vdso__do_copy_compat(FILE *f, int fd)
161{
162 char buf[4096];
163 size_t count;
164
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;
173 }
174
175 return 0;
176}
177
178static int vdso__copy_compat(const char *prog, int fd)
179{
180 FILE *f;
181 int err;
182
183 f = popen(prog, "r");
184 if (!f)
185 return -errno;
186
187 err = vdso__do_copy_compat(f, fd);
188
189 if (pclose(f) == -1)
190 return -errno;
191
192 return err;
193}
194
195static int vdso__create_compat_file(const char *prog, char *temp_name)
196{
197 int fd, err;
198
199 fd = mkstemp(temp_name);
200 if (fd < 0)
201 return -errno;
202
203 err = vdso__copy_compat(prog, fd);
204
205 if (close(fd) == -1)
206 return -errno;
207
208 return err;
209}
210
211static const char *vdso__get_compat_file(struct vdso_file *vdso_file)
212{
213 int err;
214
215 if (vdso_file->found)
216 return vdso_file->temp_file_name;
217
218 if (vdso_file->error)
219 return NULL;
220
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;
227 }
228
229 vdso_file->found = true;
230
231 return vdso_file->temp_file_name;
232}
233
234static struct dso *__machine__findnew_compat(struct machine *machine,
235 struct vdso_file *vdso_file)
236{
237 const char *file_name;
238 struct dso *dso;
239
240 dso = __dsos__find(&machine->dsos, vdso_file->dso_name, true);
241 if (dso)
242 goto out;
243
244 file_name = vdso__get_compat_file(vdso_file);
245 if (!file_name)
246 goto out;
247
248 dso = __machine__addnew_vdso(machine, vdso_file->dso_name, file_name);
249out:
250 return dso;
251}
252
253static int __machine__findnew_vdso_compat(struct machine *machine,
254 struct thread *thread,
255 struct vdso_info *vdso_info,
256 struct dso **dso)
257{
258 enum dso_type dso_type;
259
260 dso_type = machine__thread_dso_type(machine, thread);
261
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
270
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;
282 }
283}
284
285#endif
286
287static struct dso *machine__find_vdso(struct machine *machine,
288 struct thread *thread)
289{
290 struct dso *dso = NULL;
291 enum dso_type dso_type;
292
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;
302 }
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;
312 }
313
314 return dso;
315}
316
317struct dso *machine__findnew_vdso(struct machine *machine,
318 struct thread *thread)
319{
320 struct vdso_info *vdso_info;
321 struct dso *dso = NULL;
322
323 down_write(&machine->dsos.lock);
324 if (!machine->vdso_info)
325 machine->vdso_info = vdso_info__new();
326
327 vdso_info = machine->vdso_info;
328 if (!vdso_info)
329 goto out_unlock;
330
331 dso = machine__find_vdso(machine, thread);
332 if (dso)
333 goto out_unlock;
334
335#if BITS_PER_LONG == 64
336 if (__machine__findnew_vdso_compat(machine, thread, vdso_info, &dso))
337 goto out_unlock;
338#endif
339
340 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, true);
341 if (!dso) {
342 char *file;
343
344 file = get_file(&vdso_info->vdso);
345 if (file)
346 dso = __machine__addnew_vdso(machine, DSO__NAME_VDSO, file);
347 }
348
349out_unlock:
350 dso__get(dso);
351 up_write(&machine->dsos.lock);
352 return dso;
353}
354
355bool dso__is_vdso(struct dso *dso)
356{
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);
360}
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>
11
12#include "vdso.h"
13#include "dso.h"
14#include <internal/lib.h>
15#include "map.h"
16#include "symbol.h"
17#include "machine.h"
18#include "thread.h"
19#include "linux/string.h"
20#include <linux/zalloc.h>
21#include "debug.h"
22
23/*
24 * Include definition of find_map() also used in perf-read-vdso.c for
25 * building perf-read-vdso32 and perf-read-vdsox32.
26 */
27#include "find-map.c"
28
29#define VDSO__TEMP_FILE_NAME "/tmp/perf-vdso.so-XXXXXX"
30
31struct vdso_file {
32 bool found;
33 bool error;
34 char temp_file_name[sizeof(VDSO__TEMP_FILE_NAME)];
35 const char *dso_name;
36 const char *read_prog;
37};
38
39struct vdso_info {
40 struct vdso_file vdso;
41#if BITS_PER_LONG == 64
42 struct vdso_file vdso32;
43 struct vdso_file vdsox32;
44#endif
45};
46
47static struct vdso_info *vdso_info__new(void)
48{
49 static const struct vdso_info vdso_info_init = {
50 .vdso = {
51 .temp_file_name = VDSO__TEMP_FILE_NAME,
52 .dso_name = DSO__NAME_VDSO,
53 },
54#if BITS_PER_LONG == 64
55 .vdso32 = {
56 .temp_file_name = VDSO__TEMP_FILE_NAME,
57 .dso_name = DSO__NAME_VDSO32,
58 .read_prog = "perf-read-vdso32",
59 },
60 .vdsox32 = {
61 .temp_file_name = VDSO__TEMP_FILE_NAME,
62 .dso_name = DSO__NAME_VDSOX32,
63 .read_prog = "perf-read-vdsox32",
64 },
65#endif
66 };
67
68 return memdup(&vdso_info_init, sizeof(vdso_info_init));
69}
70
71static char *get_file(struct vdso_file *vdso_file)
72{
73 char *vdso = NULL;
74 char *buf = NULL;
75 void *start, *end;
76 size_t size;
77 int fd;
78
79 if (vdso_file->found)
80 return vdso_file->temp_file_name;
81
82 if (vdso_file->error || find_map(&start, &end, VDSO__MAP_NAME))
83 return NULL;
84
85 size = end - start;
86
87 buf = memdup(start, size);
88 if (!buf)
89 return NULL;
90
91 fd = mkstemp(vdso_file->temp_file_name);
92 if (fd < 0)
93 goto out;
94
95 if (size == (size_t) write(fd, buf, size))
96 vdso = vdso_file->temp_file_name;
97
98 close(fd);
99
100 out:
101 free(buf);
102
103 vdso_file->found = (vdso != NULL);
104 vdso_file->error = !vdso_file->found;
105 return vdso;
106}
107
108void machine__exit_vdso(struct machine *machine)
109{
110 struct vdso_info *vdso_info = machine->vdso_info;
111
112 if (!vdso_info)
113 return;
114
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);
122#endif
123
124 zfree(&machine->vdso_info);
125}
126
127static struct dso *__machine__addnew_vdso(struct machine *machine, const char *short_name,
128 const char *long_name)
129{
130 struct dso *dso;
131
132 dso = dso__new(short_name);
133 if (dso != NULL) {
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 */
137 dso__put(dso);
138 }
139
140 return dso;
141}
142
143struct machine__thread_dso_type_maps_cb_args {
144 struct machine *machine;
145 enum dso_type dso_type;
146};
147
148static int machine__thread_dso_type_maps_cb(struct map *map, void *data)
149{
150 struct machine__thread_dso_type_maps_cb_args *args = data;
151 struct dso *dso = map__dso(map);
152
153 if (!dso || dso->long_name[0] != '/')
154 return 0;
155
156 args->dso_type = dso__type(dso, args->machine);
157 return (args->dso_type != DSO__TYPE_UNKNOWN) ? 1 : 0;
158}
159
160static enum dso_type machine__thread_dso_type(struct machine *machine,
161 struct thread *thread)
162{
163 struct machine__thread_dso_type_maps_cb_args args = {
164 .machine = machine,
165 .dso_type = DSO__TYPE_UNKNOWN,
166 };
167
168 maps__for_each_map(thread__maps(thread), machine__thread_dso_type_maps_cb, &args);
169
170 return args.dso_type;
171}
172
173#if BITS_PER_LONG == 64
174
175static int vdso__do_copy_compat(FILE *f, int fd)
176{
177 char buf[4096];
178 size_t count;
179
180 while (1) {
181 count = fread(buf, 1, sizeof(buf), f);
182 if (ferror(f))
183 return -errno;
184 if (feof(f))
185 break;
186 if (count && writen(fd, buf, count) != (ssize_t)count)
187 return -errno;
188 }
189
190 return 0;
191}
192
193static int vdso__copy_compat(const char *prog, int fd)
194{
195 FILE *f;
196 int err;
197
198 f = popen(prog, "r");
199 if (!f)
200 return -errno;
201
202 err = vdso__do_copy_compat(f, fd);
203
204 if (pclose(f) == -1)
205 return -errno;
206
207 return err;
208}
209
210static int vdso__create_compat_file(const char *prog, char *temp_name)
211{
212 int fd, err;
213
214 fd = mkstemp(temp_name);
215 if (fd < 0)
216 return -errno;
217
218 err = vdso__copy_compat(prog, fd);
219
220 if (close(fd) == -1)
221 return -errno;
222
223 return err;
224}
225
226static const char *vdso__get_compat_file(struct vdso_file *vdso_file)
227{
228 int err;
229
230 if (vdso_file->found)
231 return vdso_file->temp_file_name;
232
233 if (vdso_file->error)
234 return NULL;
235
236 err = vdso__create_compat_file(vdso_file->read_prog,
237 vdso_file->temp_file_name);
238 if (err) {
239 pr_err("%s failed, error %d\n", vdso_file->read_prog, err);
240 vdso_file->error = true;
241 return NULL;
242 }
243
244 vdso_file->found = true;
245
246 return vdso_file->temp_file_name;
247}
248
249static struct dso *__machine__findnew_compat(struct machine *machine,
250 struct vdso_file *vdso_file)
251{
252 const char *file_name;
253 struct dso *dso;
254
255 dso = __dsos__find(&machine->dsos, vdso_file->dso_name, true);
256 if (dso)
257 goto out;
258
259 file_name = vdso__get_compat_file(vdso_file);
260 if (!file_name)
261 goto out;
262
263 dso = __machine__addnew_vdso(machine, vdso_file->dso_name, file_name);
264out:
265 return dso;
266}
267
268static int __machine__findnew_vdso_compat(struct machine *machine,
269 struct thread *thread,
270 struct vdso_info *vdso_info,
271 struct dso **dso)
272{
273 enum dso_type dso_type;
274
275 dso_type = machine__thread_dso_type(machine, thread);
276
277#ifndef HAVE_PERF_READ_VDSO32
278 if (dso_type == DSO__TYPE_32BIT)
279 return 0;
280#endif
281#ifndef HAVE_PERF_READ_VDSOX32
282 if (dso_type == DSO__TYPE_X32BIT)
283 return 0;
284#endif
285
286 switch (dso_type) {
287 case DSO__TYPE_32BIT:
288 *dso = __machine__findnew_compat(machine, &vdso_info->vdso32);
289 return 1;
290 case DSO__TYPE_X32BIT:
291 *dso = __machine__findnew_compat(machine, &vdso_info->vdsox32);
292 return 1;
293 case DSO__TYPE_UNKNOWN:
294 case DSO__TYPE_64BIT:
295 default:
296 return 0;
297 }
298}
299
300#endif
301
302static struct dso *machine__find_vdso(struct machine *machine,
303 struct thread *thread)
304{
305 struct dso *dso = NULL;
306 enum dso_type dso_type;
307
308 dso_type = machine__thread_dso_type(machine, thread);
309 switch (dso_type) {
310 case DSO__TYPE_32BIT:
311 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO32, true);
312 if (!dso) {
313 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO,
314 true);
315 if (dso && dso_type != dso__type(dso, machine))
316 dso = NULL;
317 }
318 break;
319 case DSO__TYPE_X32BIT:
320 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSOX32, true);
321 break;
322 case DSO__TYPE_64BIT:
323 case DSO__TYPE_UNKNOWN:
324 default:
325 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, true);
326 break;
327 }
328
329 return dso;
330}
331
332struct dso *machine__findnew_vdso(struct machine *machine,
333 struct thread *thread)
334{
335 struct vdso_info *vdso_info;
336 struct dso *dso = NULL;
337
338 down_write(&machine->dsos.lock);
339 if (!machine->vdso_info)
340 machine->vdso_info = vdso_info__new();
341
342 vdso_info = machine->vdso_info;
343 if (!vdso_info)
344 goto out_unlock;
345
346 dso = machine__find_vdso(machine, thread);
347 if (dso)
348 goto out_unlock;
349
350#if BITS_PER_LONG == 64
351 if (__machine__findnew_vdso_compat(machine, thread, vdso_info, &dso))
352 goto out_unlock;
353#endif
354
355 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, true);
356 if (!dso) {
357 char *file;
358
359 file = get_file(&vdso_info->vdso);
360 if (file)
361 dso = __machine__addnew_vdso(machine, DSO__NAME_VDSO, file);
362 }
363
364out_unlock:
365 dso__get(dso);
366 up_write(&machine->dsos.lock);
367 return dso;
368}
369
370bool dso__is_vdso(struct dso *dso)
371{
372 return !strcmp(dso->short_name, DSO__NAME_VDSO) ||
373 !strcmp(dso->short_name, DSO__NAME_VDSO32) ||
374 !strcmp(dso->short_name, DSO__NAME_VDSOX32);
375}