blob: adca69384fcc044eeea0a1a51580e8a29dea7058 [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"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020015#include "linux/string.h"
Jiri Olsa84f5d362014-07-14 23:46:48 +020016#include "debug.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020017
Adrian Hunter30f4f812014-07-22 16:17:54 +030018#define VDSO__TEMP_FILE_NAME "/tmp/perf-vdso.so-XXXXXX"
19
20struct vdso_file {
21 bool found;
22 bool error;
23 char temp_file_name[sizeof(VDSO__TEMP_FILE_NAME)];
24 const char *dso_name;
25};
26
27struct vdso_info {
28 struct vdso_file vdso;
29};
30
Adrian Hunterd027b642014-07-23 14:23:00 +030031static struct vdso_info *vdso_info__new(void)
32{
33 static const struct vdso_info vdso_info_init = {
34 .vdso = {
35 .temp_file_name = VDSO__TEMP_FILE_NAME,
Adrian Hunter51682dc2014-07-22 16:17:57 +030036 .dso_name = DSO__NAME_VDSO,
Adrian Hunterd027b642014-07-23 14:23:00 +030037 },
38 };
Adrian Hunter30f4f812014-07-22 16:17:54 +030039
Adrian Hunterd027b642014-07-23 14:23:00 +030040 return memdup(&vdso_info_init, sizeof(vdso_info_init));
41}
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020042
43static int find_vdso_map(void **start, void **end)
44{
45 FILE *maps;
46 char line[128];
47 int found = 0;
48
49 maps = fopen("/proc/self/maps", "r");
50 if (!maps) {
51 pr_err("vdso: cannot open maps\n");
52 return -1;
53 }
54
55 while (!found && fgets(line, sizeof(line), maps)) {
56 int m = -1;
57
58 /* We care only about private r-x mappings. */
59 if (2 != sscanf(line, "%p-%p r-xp %*x %*x:%*x %*u %n",
60 start, end, &m))
61 continue;
62 if (m < 0)
63 continue;
64
65 if (!strncmp(&line[m], VDSO__MAP_NAME,
66 sizeof(VDSO__MAP_NAME) - 1))
67 found = 1;
68 }
69
70 fclose(maps);
71 return !found;
72}
73
Adrian Hunter30f4f812014-07-22 16:17:54 +030074static char *get_file(struct vdso_file *vdso_file)
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020075{
76 char *vdso = NULL;
77 char *buf = NULL;
78 void *start, *end;
79 size_t size;
80 int fd;
81
Adrian Hunter30f4f812014-07-22 16:17:54 +030082 if (vdso_file->found)
83 return vdso_file->temp_file_name;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020084
Adrian Hunter30f4f812014-07-22 16:17:54 +030085 if (vdso_file->error || find_vdso_map(&start, &end))
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020086 return NULL;
87
88 size = end - start;
89
90 buf = memdup(start, size);
91 if (!buf)
92 return NULL;
93
Adrian Hunter30f4f812014-07-22 16:17:54 +030094 fd = mkstemp(vdso_file->temp_file_name);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020095 if (fd < 0)
96 goto out;
97
98 if (size == (size_t) write(fd, buf, size))
Adrian Hunter30f4f812014-07-22 16:17:54 +030099 vdso = vdso_file->temp_file_name;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200100
101 close(fd);
102
103 out:
104 free(buf);
105
Adrian Hunter30f4f812014-07-22 16:17:54 +0300106 vdso_file->found = (vdso != NULL);
107 vdso_file->error = !vdso_file->found;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200108 return vdso;
109}
110
Adrian Hunterd027b642014-07-23 14:23:00 +0300111void vdso__exit(struct machine *machine)
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200112{
Adrian Hunterd027b642014-07-23 14:23:00 +0300113 struct vdso_info *vdso_info = machine->vdso_info;
114
115 if (!vdso_info)
116 return;
117
Adrian Hunter30f4f812014-07-22 16:17:54 +0300118 if (vdso_info->vdso.found)
119 unlink(vdso_info->vdso.temp_file_name);
Adrian Hunterd027b642014-07-23 14:23:00 +0300120
121 zfree(&machine->vdso_info);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200122}
123
Adrian Hunter4f71f2a2014-07-22 16:17:56 +0300124static struct dso *vdso__new(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->user_dsos, dso);
132 dso__set_long_name(dso, long_name, false);
133 }
134
135 return dso;
136}
137
Adrian Hunter5835edd2014-07-22 16:18:00 +0300138struct dso *vdso__dso_findnew(struct machine *machine,
139 struct thread *thread __maybe_unused)
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200140{
Adrian Hunterd027b642014-07-23 14:23:00 +0300141 struct vdso_info *vdso_info;
142 struct dso *dso;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200143
Adrian Hunterd027b642014-07-23 14:23:00 +0300144 if (!machine->vdso_info)
145 machine->vdso_info = vdso_info__new();
146
147 vdso_info = machine->vdso_info;
148 if (!vdso_info)
149 return NULL;
150
Adrian Hunter51682dc2014-07-22 16:17:57 +0300151 dso = dsos__find(&machine->user_dsos, DSO__NAME_VDSO, true);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200152 if (!dso) {
153 char *file;
154
Adrian Hunter30f4f812014-07-22 16:17:54 +0300155 file = get_file(&vdso_info->vdso);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200156 if (!file)
157 return NULL;
158
Adrian Hunter51682dc2014-07-22 16:17:57 +0300159 dso = vdso__new(machine, DSO__NAME_VDSO, file);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200160 }
161
162 return dso;
163}
Adrian Hunter51682dc2014-07-22 16:17:57 +0300164
165bool dso__is_vdso(struct dso *dso)
166{
167 return !strcmp(dso->short_name, DSO__NAME_VDSO);
168}