blob: 75245f081b600c2dd6a22d288c3430a78965c2b1 [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
31static struct vdso_info vdso_info_ = {
32 .vdso = {
33 .temp_file_name = VDSO__TEMP_FILE_NAME,
34 .dso_name = VDSO__MAP_NAME,
35 },
36};
37
38static struct vdso_info *vdso_info = &vdso_info_;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020039
40static int find_vdso_map(void **start, void **end)
41{
42 FILE *maps;
43 char line[128];
44 int found = 0;
45
46 maps = fopen("/proc/self/maps", "r");
47 if (!maps) {
48 pr_err("vdso: cannot open maps\n");
49 return -1;
50 }
51
52 while (!found && fgets(line, sizeof(line), maps)) {
53 int m = -1;
54
55 /* We care only about private r-x mappings. */
56 if (2 != sscanf(line, "%p-%p r-xp %*x %*x:%*x %*u %n",
57 start, end, &m))
58 continue;
59 if (m < 0)
60 continue;
61
62 if (!strncmp(&line[m], VDSO__MAP_NAME,
63 sizeof(VDSO__MAP_NAME) - 1))
64 found = 1;
65 }
66
67 fclose(maps);
68 return !found;
69}
70
Adrian Hunter30f4f812014-07-22 16:17:54 +030071static char *get_file(struct vdso_file *vdso_file)
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020072{
73 char *vdso = NULL;
74 char *buf = NULL;
75 void *start, *end;
76 size_t size;
77 int fd;
78
Adrian Hunter30f4f812014-07-22 16:17:54 +030079 if (vdso_file->found)
80 return vdso_file->temp_file_name;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020081
Adrian Hunter30f4f812014-07-22 16:17:54 +030082 if (vdso_file->error || find_vdso_map(&start, &end))
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020083 return NULL;
84
85 size = end - start;
86
87 buf = memdup(start, size);
88 if (!buf)
89 return NULL;
90
Adrian Hunter30f4f812014-07-22 16:17:54 +030091 fd = mkstemp(vdso_file->temp_file_name);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020092 if (fd < 0)
93 goto out;
94
95 if (size == (size_t) write(fd, buf, size))
Adrian Hunter30f4f812014-07-22 16:17:54 +030096 vdso = vdso_file->temp_file_name;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020097
98 close(fd);
99
100 out:
101 free(buf);
102
Adrian Hunter30f4f812014-07-22 16:17:54 +0300103 vdso_file->found = (vdso != NULL);
104 vdso_file->error = !vdso_file->found;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200105 return vdso;
106}
107
108void vdso__exit(void)
109{
Adrian Hunter30f4f812014-07-22 16:17:54 +0300110 if (vdso_info->vdso.found)
111 unlink(vdso_info->vdso.temp_file_name);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200112}
113
Adrian Hunter2a030682014-07-22 16:17:53 +0300114struct dso *vdso__dso_findnew(struct machine *machine)
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200115{
Adrian Hunter2a030682014-07-22 16:17:53 +0300116 struct dso *dso = dsos__find(&machine->user_dsos, VDSO__MAP_NAME, true);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200117
118 if (!dso) {
119 char *file;
120
Adrian Hunter30f4f812014-07-22 16:17:54 +0300121 file = get_file(&vdso_info->vdso);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200122 if (!file)
123 return NULL;
124
125 dso = dso__new(VDSO__MAP_NAME);
126 if (dso != NULL) {
Adrian Hunter2a030682014-07-22 16:17:53 +0300127 dsos__add(&machine->user_dsos, dso);
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -0300128 dso__set_long_name(dso, file, false);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200129 }
130 }
131
132 return dso;
133}