Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 1 | |
| 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 Hunter | 2a03068 | 2014-07-22 16:17:53 +0300 | [diff] [blame] | 14 | #include "machine.h" |
Adrian Hunter | f6832e17 | 2014-10-23 13:45:23 +0300 | [diff] [blame] | 15 | #include "thread.h" |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 16 | #include "linux/string.h" |
Jiri Olsa | 84f5d36 | 2014-07-14 23:46:48 +0200 | [diff] [blame] | 17 | #include "debug.h" |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 18 | |
Adrian Hunter | e477f3f | 2014-10-23 18:16:03 -0300 | [diff] [blame] | 19 | /* |
| 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 Hunter | 30f4f81 | 2014-07-22 16:17:54 +0300 | [diff] [blame] | 25 | #define VDSO__TEMP_FILE_NAME "/tmp/perf-vdso.so-XXXXXX" |
| 26 | |
| 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; |
Adrian Hunter | f6832e17 | 2014-10-23 13:45:23 +0300 | [diff] [blame] | 32 | const char *read_prog; |
Adrian Hunter | 30f4f81 | 2014-07-22 16:17:54 +0300 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | struct vdso_info { |
| 36 | struct vdso_file vdso; |
Adrian Hunter | f6832e17 | 2014-10-23 13:45:23 +0300 | [diff] [blame] | 37 | #if BITS_PER_LONG == 64 |
| 38 | struct vdso_file vdso32; |
| 39 | struct vdso_file vdsox32; |
| 40 | #endif |
Adrian Hunter | 30f4f81 | 2014-07-22 16:17:54 +0300 | [diff] [blame] | 41 | }; |
| 42 | |
Adrian Hunter | d027b64 | 2014-07-23 14:23:00 +0300 | [diff] [blame] | 43 | static 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 Hunter | 51682dc | 2014-07-22 16:17:57 +0300 | [diff] [blame] | 48 | .dso_name = DSO__NAME_VDSO, |
Adrian Hunter | d027b64 | 2014-07-23 14:23:00 +0300 | [diff] [blame] | 49 | }, |
Adrian Hunter | f6832e17 | 2014-10-23 13:45:23 +0300 | [diff] [blame] | 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", |
| 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 Hunter | d027b64 | 2014-07-23 14:23:00 +0300 | [diff] [blame] | 62 | }; |
Adrian Hunter | 30f4f81 | 2014-07-22 16:17:54 +0300 | [diff] [blame] | 63 | |
Adrian Hunter | d027b64 | 2014-07-23 14:23:00 +0300 | [diff] [blame] | 64 | return memdup(&vdso_info_init, sizeof(vdso_info_init)); |
| 65 | } |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 66 | |
Adrian Hunter | 30f4f81 | 2014-07-22 16:17:54 +0300 | [diff] [blame] | 67 | static char *get_file(struct vdso_file *vdso_file) |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 68 | { |
| 69 | char *vdso = NULL; |
| 70 | char *buf = NULL; |
| 71 | void *start, *end; |
| 72 | size_t size; |
| 73 | int fd; |
| 74 | |
Adrian Hunter | 30f4f81 | 2014-07-22 16:17:54 +0300 | [diff] [blame] | 75 | if (vdso_file->found) |
| 76 | return vdso_file->temp_file_name; |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 77 | |
Adrian Hunter | 30f4f81 | 2014-07-22 16:17:54 +0300 | [diff] [blame] | 78 | if (vdso_file->error || find_vdso_map(&start, &end)) |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 79 | return NULL; |
| 80 | |
| 81 | size = end - start; |
| 82 | |
| 83 | buf = memdup(start, size); |
| 84 | if (!buf) |
| 85 | return NULL; |
| 86 | |
Adrian Hunter | 30f4f81 | 2014-07-22 16:17:54 +0300 | [diff] [blame] | 87 | fd = mkstemp(vdso_file->temp_file_name); |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 88 | if (fd < 0) |
| 89 | goto out; |
| 90 | |
| 91 | if (size == (size_t) write(fd, buf, size)) |
Adrian Hunter | 30f4f81 | 2014-07-22 16:17:54 +0300 | [diff] [blame] | 92 | vdso = vdso_file->temp_file_name; |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 93 | |
| 94 | close(fd); |
| 95 | |
| 96 | out: |
| 97 | free(buf); |
| 98 | |
Adrian Hunter | 30f4f81 | 2014-07-22 16:17:54 +0300 | [diff] [blame] | 99 | vdso_file->found = (vdso != NULL); |
| 100 | vdso_file->error = !vdso_file->found; |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 101 | return vdso; |
| 102 | } |
| 103 | |
Arnaldo Carvalho de Melo | 9a4388c | 2015-05-29 11:54:08 -0300 | [diff] [blame] | 104 | void machine__exit_vdso(struct machine *machine) |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 105 | { |
Adrian Hunter | d027b64 | 2014-07-23 14:23:00 +0300 | [diff] [blame] | 106 | struct vdso_info *vdso_info = machine->vdso_info; |
| 107 | |
| 108 | if (!vdso_info) |
| 109 | return; |
| 110 | |
Adrian Hunter | 30f4f81 | 2014-07-22 16:17:54 +0300 | [diff] [blame] | 111 | if (vdso_info->vdso.found) |
| 112 | unlink(vdso_info->vdso.temp_file_name); |
Adrian Hunter | f6832e17 | 2014-10-23 13:45:23 +0300 | [diff] [blame] | 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 |
Adrian Hunter | d027b64 | 2014-07-23 14:23:00 +0300 | [diff] [blame] | 119 | |
| 120 | zfree(&machine->vdso_info); |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 121 | } |
| 122 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 123 | static struct dso *__machine__addnew_vdso(struct machine *machine, const char *short_name, |
| 124 | const char *long_name) |
Adrian Hunter | 4f71f2a | 2014-07-22 16:17:56 +0300 | [diff] [blame] | 125 | { |
| 126 | struct dso *dso; |
| 127 | |
| 128 | dso = dso__new(short_name); |
| 129 | if (dso != NULL) { |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 130 | __dsos__add(&machine->dsos, dso); |
Adrian Hunter | 4f71f2a | 2014-07-22 16:17:56 +0300 | [diff] [blame] | 131 | dso__set_long_name(dso, long_name, false); |
| 132 | } |
| 133 | |
| 134 | return dso; |
| 135 | } |
| 136 | |
Adrian Hunter | f6832e17 | 2014-10-23 13:45:23 +0300 | [diff] [blame] | 137 | static enum dso_type machine__thread_dso_type(struct machine *machine, |
| 138 | struct thread *thread) |
| 139 | { |
| 140 | enum dso_type dso_type = DSO__TYPE_UNKNOWN; |
| 141 | struct map *map; |
| 142 | struct dso *dso; |
| 143 | |
| 144 | map = map_groups__first(thread->mg, MAP__FUNCTION); |
| 145 | for (; map ; map = map_groups__next(map)) { |
| 146 | dso = map->dso; |
| 147 | if (!dso || dso->long_name[0] != '/') |
| 148 | continue; |
| 149 | dso_type = dso__type(dso, machine); |
| 150 | if (dso_type != DSO__TYPE_UNKNOWN) |
| 151 | break; |
| 152 | } |
| 153 | |
| 154 | return dso_type; |
| 155 | } |
| 156 | |
He Kuang | 76c588f | 2016-05-17 09:04:54 +0000 | [diff] [blame] | 157 | #if BITS_PER_LONG == 64 |
| 158 | |
Adrian Hunter | f6832e17 | 2014-10-23 13:45:23 +0300 | [diff] [blame] | 159 | static 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 | |
| 177 | static 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 | |
| 194 | static 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 | |
| 210 | static 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 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 233 | static struct dso *__machine__findnew_compat(struct machine *machine, |
| 234 | struct vdso_file *vdso_file) |
Adrian Hunter | f6832e17 | 2014-10-23 13:45:23 +0300 | [diff] [blame] | 235 | { |
| 236 | const char *file_name; |
| 237 | struct dso *dso; |
| 238 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 239 | dso = __dsos__find(&machine->dsos, vdso_file->dso_name, true); |
Adrian Hunter | f6832e17 | 2014-10-23 13:45:23 +0300 | [diff] [blame] | 240 | if (dso) |
Adrian Hunter | 6d545a6 | 2015-07-07 14:13:38 +0300 | [diff] [blame] | 241 | goto out; |
Adrian Hunter | f6832e17 | 2014-10-23 13:45:23 +0300 | [diff] [blame] | 242 | |
| 243 | file_name = vdso__get_compat_file(vdso_file); |
| 244 | if (!file_name) |
Adrian Hunter | 6d545a6 | 2015-07-07 14:13:38 +0300 | [diff] [blame] | 245 | goto out; |
Adrian Hunter | f6832e17 | 2014-10-23 13:45:23 +0300 | [diff] [blame] | 246 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 247 | dso = __machine__addnew_vdso(machine, vdso_file->dso_name, file_name); |
Adrian Hunter | 6d545a6 | 2015-07-07 14:13:38 +0300 | [diff] [blame] | 248 | out: |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 249 | return dso; |
Adrian Hunter | f6832e17 | 2014-10-23 13:45:23 +0300 | [diff] [blame] | 250 | } |
| 251 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 252 | static int __machine__findnew_vdso_compat(struct machine *machine, |
| 253 | struct thread *thread, |
| 254 | struct vdso_info *vdso_info, |
| 255 | struct dso **dso) |
Adrian Hunter | f6832e17 | 2014-10-23 13:45:23 +0300 | [diff] [blame] | 256 | { |
| 257 | enum dso_type dso_type; |
| 258 | |
| 259 | dso_type = machine__thread_dso_type(machine, thread); |
Adrian Hunter | 46b1fa8 | 2014-10-23 13:45:24 +0300 | [diff] [blame] | 260 | |
| 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 |
| 269 | |
Adrian Hunter | f6832e17 | 2014-10-23 13:45:23 +0300 | [diff] [blame] | 270 | switch (dso_type) { |
| 271 | case DSO__TYPE_32BIT: |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 272 | *dso = __machine__findnew_compat(machine, &vdso_info->vdso32); |
Adrian Hunter | f6832e17 | 2014-10-23 13:45:23 +0300 | [diff] [blame] | 273 | return 1; |
| 274 | case DSO__TYPE_X32BIT: |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 275 | *dso = __machine__findnew_compat(machine, &vdso_info->vdsox32); |
Adrian Hunter | f6832e17 | 2014-10-23 13:45:23 +0300 | [diff] [blame] | 276 | return 1; |
| 277 | case DSO__TYPE_UNKNOWN: |
| 278 | case DSO__TYPE_64BIT: |
| 279 | default: |
| 280 | return 0; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | #endif |
| 285 | |
He Kuang | 76c588f | 2016-05-17 09:04:54 +0000 | [diff] [blame] | 286 | static struct dso *machine__find_vdso(struct machine *machine, |
| 287 | struct thread *thread) |
| 288 | { |
| 289 | struct dso *dso = NULL; |
| 290 | enum dso_type dso_type; |
| 291 | |
| 292 | dso_type = machine__thread_dso_type(machine, thread); |
| 293 | switch (dso_type) { |
| 294 | case DSO__TYPE_32BIT: |
| 295 | dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO32, true); |
| 296 | if (!dso) { |
| 297 | dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, |
| 298 | true); |
| 299 | if (dso && dso_type != dso__type(dso, machine)) |
| 300 | dso = NULL; |
| 301 | } |
| 302 | break; |
| 303 | case DSO__TYPE_X32BIT: |
| 304 | dso = __dsos__find(&machine->dsos, DSO__NAME_VDSOX32, true); |
| 305 | break; |
| 306 | case DSO__TYPE_64BIT: |
| 307 | case DSO__TYPE_UNKNOWN: |
| 308 | default: |
| 309 | dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, true); |
| 310 | break; |
| 311 | } |
| 312 | |
| 313 | return dso; |
| 314 | } |
| 315 | |
Arnaldo Carvalho de Melo | 9a4388c | 2015-05-29 11:54:08 -0300 | [diff] [blame] | 316 | struct dso *machine__findnew_vdso(struct machine *machine, |
He Kuang | 76c588f | 2016-05-17 09:04:54 +0000 | [diff] [blame] | 317 | struct thread *thread) |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 318 | { |
Adrian Hunter | d027b64 | 2014-07-23 14:23:00 +0300 | [diff] [blame] | 319 | struct vdso_info *vdso_info; |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 320 | struct dso *dso = NULL; |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 321 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 322 | pthread_rwlock_wrlock(&machine->dsos.lock); |
Adrian Hunter | d027b64 | 2014-07-23 14:23:00 +0300 | [diff] [blame] | 323 | if (!machine->vdso_info) |
| 324 | machine->vdso_info = vdso_info__new(); |
| 325 | |
| 326 | vdso_info = machine->vdso_info; |
| 327 | if (!vdso_info) |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 328 | goto out_unlock; |
Adrian Hunter | d027b64 | 2014-07-23 14:23:00 +0300 | [diff] [blame] | 329 | |
He Kuang | 76c588f | 2016-05-17 09:04:54 +0000 | [diff] [blame] | 330 | dso = machine__find_vdso(machine, thread); |
| 331 | if (dso) |
| 332 | goto out_unlock; |
| 333 | |
Adrian Hunter | f6832e17 | 2014-10-23 13:45:23 +0300 | [diff] [blame] | 334 | #if BITS_PER_LONG == 64 |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 335 | if (__machine__findnew_vdso_compat(machine, thread, vdso_info, &dso)) |
| 336 | goto out_unlock; |
Adrian Hunter | f6832e17 | 2014-10-23 13:45:23 +0300 | [diff] [blame] | 337 | #endif |
| 338 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 339 | dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, true); |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 340 | if (!dso) { |
| 341 | char *file; |
| 342 | |
Adrian Hunter | 30f4f81 | 2014-07-22 16:17:54 +0300 | [diff] [blame] | 343 | file = get_file(&vdso_info->vdso); |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 344 | if (file) |
| 345 | dso = __machine__addnew_vdso(machine, DSO__NAME_VDSO, file); |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 346 | } |
| 347 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 348 | out_unlock: |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 349 | dso__get(dso); |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 350 | pthread_rwlock_unlock(&machine->dsos.lock); |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 351 | return dso; |
| 352 | } |
Adrian Hunter | 51682dc | 2014-07-22 16:17:57 +0300 | [diff] [blame] | 353 | |
| 354 | bool dso__is_vdso(struct dso *dso) |
| 355 | { |
Adrian Hunter | f6832e17 | 2014-10-23 13:45:23 +0300 | [diff] [blame] | 356 | return !strcmp(dso->short_name, DSO__NAME_VDSO) || |
| 357 | !strcmp(dso->short_name, DSO__NAME_VDSO32) || |
| 358 | !strcmp(dso->short_name, DSO__NAME_VDSOX32); |
Adrian Hunter | 51682dc | 2014-07-22 16:17:57 +0300 | [diff] [blame] | 359 | } |