bcma: claim only 14e4:4365 PCI Dell card with SoftMAC BCM43142
[linux/fpc-iii.git] / tools / perf / util / vdso.c
blob44d440da15dcf73f97a81518c97a33be5c32b89b
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>
11 #include "vdso.h"
12 #include "util.h"
13 #include "symbol.h"
14 #include "machine.h"
15 #include "thread.h"
16 #include "linux/string.h"
17 #include "debug.h"
20 * Include definition of find_vdso_map() also used in perf-read-vdso.c for
21 * building perf-read-vdso32 and perf-read-vdsox32.
23 #include "find-vdso-map.c"
25 #define VDSO__TEMP_FILE_NAME "/tmp/perf-vdso.so-XXXXXX"
27 struct vdso_file {
28 bool found;
29 bool error;
30 char temp_file_name[sizeof(VDSO__TEMP_FILE_NAME)];
31 const char *dso_name;
32 const char *read_prog;
35 struct vdso_info {
36 struct vdso_file vdso;
37 #if BITS_PER_LONG == 64
38 struct vdso_file vdso32;
39 struct vdso_file vdsox32;
40 #endif
43 static struct vdso_info *vdso_info__new(void)
45 static const struct vdso_info vdso_info_init = {
46 .vdso = {
47 .temp_file_name = VDSO__TEMP_FILE_NAME,
48 .dso_name = DSO__NAME_VDSO,
50 #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",
56 .vdsox32 = {
57 .temp_file_name = VDSO__TEMP_FILE_NAME,
58 .dso_name = DSO__NAME_VDSOX32,
59 .read_prog = "perf-read-vdsox32",
61 #endif
64 return memdup(&vdso_info_init, sizeof(vdso_info_init));
67 static char *get_file(struct vdso_file *vdso_file)
69 char *vdso = NULL;
70 char *buf = NULL;
71 void *start, *end;
72 size_t size;
73 int fd;
75 if (vdso_file->found)
76 return vdso_file->temp_file_name;
78 if (vdso_file->error || find_vdso_map(&start, &end))
79 return NULL;
81 size = end - start;
83 buf = memdup(start, size);
84 if (!buf)
85 return NULL;
87 fd = mkstemp(vdso_file->temp_file_name);
88 if (fd < 0)
89 goto out;
91 if (size == (size_t) write(fd, buf, size))
92 vdso = vdso_file->temp_file_name;
94 close(fd);
96 out:
97 free(buf);
99 vdso_file->found = (vdso != NULL);
100 vdso_file->error = !vdso_file->found;
101 return vdso;
104 void machine__exit_vdso(struct machine *machine)
106 struct vdso_info *vdso_info = machine->vdso_info;
108 if (!vdso_info)
109 return;
111 if (vdso_info->vdso.found)
112 unlink(vdso_info->vdso.temp_file_name);
113 #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
120 zfree(&machine->vdso_info);
123 static struct dso *__machine__addnew_vdso(struct machine *machine, const char *short_name,
124 const char *long_name)
126 struct dso *dso;
128 dso = dso__new(short_name);
129 if (dso != NULL) {
130 __dsos__add(&machine->dsos, dso);
131 dso__set_long_name(dso, long_name, false);
134 return dso;
137 #if BITS_PER_LONG == 64
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;
144 struct dso *dso;
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;
156 return dso_type;
159 static int vdso__do_copy_compat(FILE *f, int fd)
161 char buf[4096];
162 size_t count;
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;
174 return 0;
177 static int vdso__copy_compat(const char *prog, int fd)
179 FILE *f;
180 int err;
182 f = popen(prog, "r");
183 if (!f)
184 return -errno;
186 err = vdso__do_copy_compat(f, fd);
188 if (pclose(f) == -1)
189 return -errno;
191 return err;
194 static int vdso__create_compat_file(const char *prog, char *temp_name)
196 int fd, err;
198 fd = mkstemp(temp_name);
199 if (fd < 0)
200 return -errno;
202 err = vdso__copy_compat(prog, fd);
204 if (close(fd) == -1)
205 return -errno;
207 return err;
210 static const char *vdso__get_compat_file(struct vdso_file *vdso_file)
212 int err;
214 if (vdso_file->found)
215 return vdso_file->temp_file_name;
217 if (vdso_file->error)
218 return NULL;
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;
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;
237 struct dso *dso;
239 dso = __dsos__find(&machine->dsos, vdso_file->dso_name, true);
240 if (dso)
241 goto out;
243 file_name = vdso__get_compat_file(vdso_file);
244 if (!file_name)
245 goto out;
247 dso = __machine__addnew_vdso(machine, vdso_file->dso_name, file_name);
248 out:
249 return dso;
252 static int __machine__findnew_vdso_compat(struct machine *machine,
253 struct thread *thread,
254 struct vdso_info *vdso_info,
255 struct dso **dso)
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)
263 return 0;
264 #endif
265 #ifndef HAVE_PERF_READ_VDSOX32
266 if (dso_type == DSO__TYPE_X32BIT)
267 return 0;
268 #endif
270 switch (dso_type) {
271 case DSO__TYPE_32BIT:
272 *dso = __machine__findnew_compat(machine, &vdso_info->vdso32);
273 return 1;
274 case DSO__TYPE_X32BIT:
275 *dso = __machine__findnew_compat(machine, &vdso_info->vdsox32);
276 return 1;
277 case DSO__TYPE_UNKNOWN:
278 case DSO__TYPE_64BIT:
279 default:
280 return 0;
284 #endif
286 struct dso *machine__findnew_vdso(struct machine *machine,
287 struct thread *thread __maybe_unused)
289 struct vdso_info *vdso_info;
290 struct dso *dso = NULL;
292 pthread_rwlock_wrlock(&machine->dsos.lock);
293 if (!machine->vdso_info)
294 machine->vdso_info = vdso_info__new();
296 vdso_info = machine->vdso_info;
297 if (!vdso_info)
298 goto out_unlock;
300 #if BITS_PER_LONG == 64
301 if (__machine__findnew_vdso_compat(machine, thread, vdso_info, &dso))
302 goto out_unlock;
303 #endif
305 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, true);
306 if (!dso) {
307 char *file;
309 file = get_file(&vdso_info->vdso);
310 if (file)
311 dso = __machine__addnew_vdso(machine, DSO__NAME_VDSO, file);
314 out_unlock:
315 dso__get(dso);
316 pthread_rwlock_unlock(&machine->dsos.lock);
317 return dso;
320 bool dso__is_vdso(struct dso *dso)
322 return !strcmp(dso->short_name, DSO__NAME_VDSO) ||
323 !strcmp(dso->short_name, DSO__NAME_VDSO32) ||
324 !strcmp(dso->short_name, DSO__NAME_VDSOX32);