blob: 5c7dd796979d0d625df236c82c30b5e22f5ac7d9 [file] [log] [blame]
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +02001
2#include <unistd.h>
3#include <stdio.h>
4#include <string.h>
5#include <sys/types.h>
6#include <sys/stat.h>
7#include <fcntl.h>
8#include <stdlib.h>
9#include <linux/kernel.h>
10
11#include "vdso.h"
12#include "util.h"
13#include "symbol.h"
Adrian Hunter2a030682014-07-22 16:17:53 +030014#include "machine.h"
Adrian Hunterf6832e172014-10-23 13:45:23 +030015#include "thread.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020016#include "linux/string.h"
Jiri Olsa84f5d362014-07-14 23:46:48 +020017#include "debug.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020018
Adrian Huntere477f3f2014-10-23 18:16:03 -030019/*
20 * Include definition of find_vdso_map() also used in perf-read-vdso.c for
21 * building perf-read-vdso32 and perf-read-vdsox32.
22 */
23#include "find-vdso-map.c"
24
Adrian Hunter30f4f812014-07-22 16:17:54 +030025#define VDSO__TEMP_FILE_NAME "/tmp/perf-vdso.so-XXXXXX"
26
27struct vdso_file {
28 bool found;
29 bool error;
30 char temp_file_name[sizeof(VDSO__TEMP_FILE_NAME)];
31 const char *dso_name;
Adrian Hunterf6832e172014-10-23 13:45:23 +030032 const char *read_prog;
Adrian Hunter30f4f812014-07-22 16:17:54 +030033};
34
35struct vdso_info {
36 struct vdso_file vdso;
Adrian Hunterf6832e172014-10-23 13:45:23 +030037#if BITS_PER_LONG == 64
38 struct vdso_file vdso32;
39 struct vdso_file vdsox32;
40#endif
Adrian Hunter30f4f812014-07-22 16:17:54 +030041};
42
Adrian Hunterd027b642014-07-23 14:23:00 +030043static struct vdso_info *vdso_info__new(void)
44{
45 static const struct vdso_info vdso_info_init = {
46 .vdso = {
47 .temp_file_name = VDSO__TEMP_FILE_NAME,
Adrian Hunter51682dc2014-07-22 16:17:57 +030048 .dso_name = DSO__NAME_VDSO,
Adrian Hunterd027b642014-07-23 14:23:00 +030049 },
Adrian Hunterf6832e172014-10-23 13:45:23 +030050#if BITS_PER_LONG == 64
51 .vdso32 = {
52 .temp_file_name = VDSO__TEMP_FILE_NAME,
53 .dso_name = DSO__NAME_VDSO32,
54 .read_prog = "perf-read-vdso32",
55 },
56 .vdsox32 = {
57 .temp_file_name = VDSO__TEMP_FILE_NAME,
58 .dso_name = DSO__NAME_VDSOX32,
59 .read_prog = "perf-read-vdsox32",
60 },
61#endif
Adrian Hunterd027b642014-07-23 14:23:00 +030062 };
Adrian Hunter30f4f812014-07-22 16:17:54 +030063
Adrian Hunterd027b642014-07-23 14:23:00 +030064 return memdup(&vdso_info_init, sizeof(vdso_info_init));
65}
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020066
Adrian Hunter30f4f812014-07-22 16:17:54 +030067static char *get_file(struct vdso_file *vdso_file)
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020068{
69 char *vdso = NULL;
70 char *buf = NULL;
71 void *start, *end;
72 size_t size;
73 int fd;
74
Adrian Hunter30f4f812014-07-22 16:17:54 +030075 if (vdso_file->found)
76 return vdso_file->temp_file_name;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020077
Adrian Hunter30f4f812014-07-22 16:17:54 +030078 if (vdso_file->error || find_vdso_map(&start, &end))
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020079 return NULL;
80
81 size = end - start;
82
83 buf = memdup(start, size);
84 if (!buf)
85 return NULL;
86
Adrian Hunter30f4f812014-07-22 16:17:54 +030087 fd = mkstemp(vdso_file->temp_file_name);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020088 if (fd < 0)
89 goto out;
90
91 if (size == (size_t) write(fd, buf, size))
Adrian Hunter30f4f812014-07-22 16:17:54 +030092 vdso = vdso_file->temp_file_name;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020093
94 close(fd);
95
96 out:
97 free(buf);
98
Adrian Hunter30f4f812014-07-22 16:17:54 +030099 vdso_file->found = (vdso != NULL);
100 vdso_file->error = !vdso_file->found;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200101 return vdso;
102}
103
Adrian Hunterd027b642014-07-23 14:23:00 +0300104void vdso__exit(struct machine *machine)
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200105{
Adrian Hunterd027b642014-07-23 14:23:00 +0300106 struct vdso_info *vdso_info = machine->vdso_info;
107
108 if (!vdso_info)
109 return;
110
Adrian Hunter30f4f812014-07-22 16:17:54 +0300111 if (vdso_info->vdso.found)
112 unlink(vdso_info->vdso.temp_file_name);
Adrian Hunterf6832e172014-10-23 13:45:23 +0300113#if BITS_PER_LONG == 64
114 if (vdso_info->vdso32.found)
115 unlink(vdso_info->vdso32.temp_file_name);
116 if (vdso_info->vdsox32.found)
117 unlink(vdso_info->vdsox32.temp_file_name);
118#endif
Adrian Hunterd027b642014-07-23 14:23:00 +0300119
120 zfree(&machine->vdso_info);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200121}
122
Adrian Hunter4f71f2a2014-07-22 16:17:56 +0300123static struct dso *vdso__new(struct machine *machine, const char *short_name,
124 const char *long_name)
125{
126 struct dso *dso;
127
128 dso = dso__new(short_name);
129 if (dso != NULL) {
130 dsos__add(&machine->user_dsos, dso);
131 dso__set_long_name(dso, long_name, false);
132 }
133
134 return dso;
135}
136
Adrian Hunterf6832e172014-10-23 13:45:23 +0300137#if BITS_PER_LONG == 64
138
139static enum dso_type machine__thread_dso_type(struct machine *machine,
140 struct thread *thread)
141{
142 enum dso_type dso_type = DSO__TYPE_UNKNOWN;
143 struct map *map;
144 struct dso *dso;
145
146 map = map_groups__first(thread->mg, MAP__FUNCTION);
147 for (; map ; map = map_groups__next(map)) {
148 dso = map->dso;
149 if (!dso || dso->long_name[0] != '/')
150 continue;
151 dso_type = dso__type(dso, machine);
152 if (dso_type != DSO__TYPE_UNKNOWN)
153 break;
154 }
155
156 return dso_type;
157}
158
159static int vdso__do_copy_compat(FILE *f, int fd)
160{
161 char buf[4096];
162 size_t count;
163
164 while (1) {
165 count = fread(buf, 1, sizeof(buf), f);
166 if (ferror(f))
167 return -errno;
168 if (feof(f))
169 break;
170 if (count && writen(fd, buf, count) != (ssize_t)count)
171 return -errno;
172 }
173
174 return 0;
175}
176
177static int vdso__copy_compat(const char *prog, int fd)
178{
179 FILE *f;
180 int err;
181
182 f = popen(prog, "r");
183 if (!f)
184 return -errno;
185
186 err = vdso__do_copy_compat(f, fd);
187
188 if (pclose(f) == -1)
189 return -errno;
190
191 return err;
192}
193
194static int vdso__create_compat_file(const char *prog, char *temp_name)
195{
196 int fd, err;
197
198 fd = mkstemp(temp_name);
199 if (fd < 0)
200 return -errno;
201
202 err = vdso__copy_compat(prog, fd);
203
204 if (close(fd) == -1)
205 return -errno;
206
207 return err;
208}
209
210static const char *vdso__get_compat_file(struct vdso_file *vdso_file)
211{
212 int err;
213
214 if (vdso_file->found)
215 return vdso_file->temp_file_name;
216
217 if (vdso_file->error)
218 return NULL;
219
220 err = vdso__create_compat_file(vdso_file->read_prog,
221 vdso_file->temp_file_name);
222 if (err) {
223 pr_err("%s failed, error %d\n", vdso_file->read_prog, err);
224 vdso_file->error = true;
225 return NULL;
226 }
227
228 vdso_file->found = true;
229
230 return vdso_file->temp_file_name;
231}
232
233static struct dso *vdso__findnew_compat(struct machine *machine,
234 struct vdso_file *vdso_file)
235{
236 const char *file_name;
237 struct dso *dso;
238
239 dso = dsos__find(&machine->user_dsos, vdso_file->dso_name, true);
240 if (dso)
241 return dso;
242
243 file_name = vdso__get_compat_file(vdso_file);
244 if (!file_name)
245 return NULL;
246
247 return vdso__new(machine, vdso_file->dso_name, file_name);
248}
249
250static int vdso__dso_findnew_compat(struct machine *machine,
251 struct thread *thread,
252 struct vdso_info *vdso_info,
253 struct dso **dso)
254{
255 enum dso_type dso_type;
256
257 dso_type = machine__thread_dso_type(machine, thread);
Adrian Hunter46b1fa82014-10-23 13:45:24 +0300258
259#ifndef HAVE_PERF_READ_VDSO32
260 if (dso_type == DSO__TYPE_32BIT)
261 return 0;
262#endif
263#ifndef HAVE_PERF_READ_VDSOX32
264 if (dso_type == DSO__TYPE_X32BIT)
265 return 0;
266#endif
267
Adrian Hunterf6832e172014-10-23 13:45:23 +0300268 switch (dso_type) {
269 case DSO__TYPE_32BIT:
270 *dso = vdso__findnew_compat(machine, &vdso_info->vdso32);
271 return 1;
272 case DSO__TYPE_X32BIT:
273 *dso = vdso__findnew_compat(machine, &vdso_info->vdsox32);
274 return 1;
275 case DSO__TYPE_UNKNOWN:
276 case DSO__TYPE_64BIT:
277 default:
278 return 0;
279 }
280}
281
282#endif
283
Adrian Hunter5835edd2014-07-22 16:18:00 +0300284struct dso *vdso__dso_findnew(struct machine *machine,
285 struct thread *thread __maybe_unused)
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200286{
Adrian Hunterd027b642014-07-23 14:23:00 +0300287 struct vdso_info *vdso_info;
288 struct dso *dso;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200289
Adrian Hunterd027b642014-07-23 14:23:00 +0300290 if (!machine->vdso_info)
291 machine->vdso_info = vdso_info__new();
292
293 vdso_info = machine->vdso_info;
294 if (!vdso_info)
295 return NULL;
296
Adrian Hunterf6832e172014-10-23 13:45:23 +0300297#if BITS_PER_LONG == 64
298 if (vdso__dso_findnew_compat(machine, thread, vdso_info, &dso))
299 return dso;
300#endif
301
Adrian Hunter51682dc2014-07-22 16:17:57 +0300302 dso = dsos__find(&machine->user_dsos, DSO__NAME_VDSO, true);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200303 if (!dso) {
304 char *file;
305
Adrian Hunter30f4f812014-07-22 16:17:54 +0300306 file = get_file(&vdso_info->vdso);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200307 if (!file)
308 return NULL;
309
Adrian Hunter51682dc2014-07-22 16:17:57 +0300310 dso = vdso__new(machine, DSO__NAME_VDSO, file);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200311 }
312
313 return dso;
314}
Adrian Hunter51682dc2014-07-22 16:17:57 +0300315
316bool dso__is_vdso(struct dso *dso)
317{
Adrian Hunterf6832e172014-10-23 13:45:23 +0300318 return !strcmp(dso->short_name, DSO__NAME_VDSO) ||
319 !strcmp(dso->short_name, DSO__NAME_VDSO32) ||
320 !strcmp(dso->short_name, DSO__NAME_VDSOX32);
Adrian Hunter51682dc2014-07-22 16:17:57 +0300321}