0001
0002 #include <errno.h>
0003 #include <unistd.h>
0004 #include <stdio.h>
0005 #include <string.h>
0006 #include <sys/types.h>
0007 #include <sys/stat.h>
0008 #include <fcntl.h>
0009 #include <stdlib.h>
0010 #include <linux/kernel.h>
0011
0012 #include "vdso.h"
0013 #include "dso.h"
0014 #include <internal/lib.h>
0015 #include "map.h"
0016 #include "symbol.h"
0017 #include "machine.h"
0018 #include "thread.h"
0019 #include "linux/string.h"
0020 #include <linux/zalloc.h>
0021 #include "debug.h"
0022
0023
0024
0025
0026
0027 #include "find-map.c"
0028
0029 #define VDSO__TEMP_FILE_NAME "/tmp/perf-vdso.so-XXXXXX"
0030
0031 struct vdso_file {
0032 bool found;
0033 bool error;
0034 char temp_file_name[sizeof(VDSO__TEMP_FILE_NAME)];
0035 const char *dso_name;
0036 const char *read_prog;
0037 };
0038
0039 struct vdso_info {
0040 struct vdso_file vdso;
0041 #if BITS_PER_LONG == 64
0042 struct vdso_file vdso32;
0043 struct vdso_file vdsox32;
0044 #endif
0045 };
0046
0047 static struct vdso_info *vdso_info__new(void)
0048 {
0049 static const struct vdso_info vdso_info_init = {
0050 .vdso = {
0051 .temp_file_name = VDSO__TEMP_FILE_NAME,
0052 .dso_name = DSO__NAME_VDSO,
0053 },
0054 #if BITS_PER_LONG == 64
0055 .vdso32 = {
0056 .temp_file_name = VDSO__TEMP_FILE_NAME,
0057 .dso_name = DSO__NAME_VDSO32,
0058 .read_prog = "perf-read-vdso32",
0059 },
0060 .vdsox32 = {
0061 .temp_file_name = VDSO__TEMP_FILE_NAME,
0062 .dso_name = DSO__NAME_VDSOX32,
0063 .read_prog = "perf-read-vdsox32",
0064 },
0065 #endif
0066 };
0067
0068 return memdup(&vdso_info_init, sizeof(vdso_info_init));
0069 }
0070
0071 static char *get_file(struct vdso_file *vdso_file)
0072 {
0073 char *vdso = NULL;
0074 char *buf = NULL;
0075 void *start, *end;
0076 size_t size;
0077 int fd;
0078
0079 if (vdso_file->found)
0080 return vdso_file->temp_file_name;
0081
0082 if (vdso_file->error || find_map(&start, &end, VDSO__MAP_NAME))
0083 return NULL;
0084
0085 size = end - start;
0086
0087 buf = memdup(start, size);
0088 if (!buf)
0089 return NULL;
0090
0091 fd = mkstemp(vdso_file->temp_file_name);
0092 if (fd < 0)
0093 goto out;
0094
0095 if (size == (size_t) write(fd, buf, size))
0096 vdso = vdso_file->temp_file_name;
0097
0098 close(fd);
0099
0100 out:
0101 free(buf);
0102
0103 vdso_file->found = (vdso != NULL);
0104 vdso_file->error = !vdso_file->found;
0105 return vdso;
0106 }
0107
0108 void machine__exit_vdso(struct machine *machine)
0109 {
0110 struct vdso_info *vdso_info = machine->vdso_info;
0111
0112 if (!vdso_info)
0113 return;
0114
0115 if (vdso_info->vdso.found)
0116 unlink(vdso_info->vdso.temp_file_name);
0117 #if BITS_PER_LONG == 64
0118 if (vdso_info->vdso32.found)
0119 unlink(vdso_info->vdso32.temp_file_name);
0120 if (vdso_info->vdsox32.found)
0121 unlink(vdso_info->vdsox32.temp_file_name);
0122 #endif
0123
0124 zfree(&machine->vdso_info);
0125 }
0126
0127 static struct dso *__machine__addnew_vdso(struct machine *machine, const char *short_name,
0128 const char *long_name)
0129 {
0130 struct dso *dso;
0131
0132 dso = dso__new(short_name);
0133 if (dso != NULL) {
0134 __dsos__add(&machine->dsos, dso);
0135 dso__set_long_name(dso, long_name, false);
0136
0137 dso__put(dso);
0138 }
0139
0140 return dso;
0141 }
0142
0143 static enum dso_type machine__thread_dso_type(struct machine *machine,
0144 struct thread *thread)
0145 {
0146 enum dso_type dso_type = DSO__TYPE_UNKNOWN;
0147 struct map *map;
0148
0149 maps__for_each_entry(thread->maps, map) {
0150 struct dso *dso = map->dso;
0151 if (!dso || dso->long_name[0] != '/')
0152 continue;
0153 dso_type = dso__type(dso, machine);
0154 if (dso_type != DSO__TYPE_UNKNOWN)
0155 break;
0156 }
0157
0158 return dso_type;
0159 }
0160
0161 #if BITS_PER_LONG == 64
0162
0163 static int vdso__do_copy_compat(FILE *f, int fd)
0164 {
0165 char buf[4096];
0166 size_t count;
0167
0168 while (1) {
0169 count = fread(buf, 1, sizeof(buf), f);
0170 if (ferror(f))
0171 return -errno;
0172 if (feof(f))
0173 break;
0174 if (count && writen(fd, buf, count) != (ssize_t)count)
0175 return -errno;
0176 }
0177
0178 return 0;
0179 }
0180
0181 static int vdso__copy_compat(const char *prog, int fd)
0182 {
0183 FILE *f;
0184 int err;
0185
0186 f = popen(prog, "r");
0187 if (!f)
0188 return -errno;
0189
0190 err = vdso__do_copy_compat(f, fd);
0191
0192 if (pclose(f) == -1)
0193 return -errno;
0194
0195 return err;
0196 }
0197
0198 static int vdso__create_compat_file(const char *prog, char *temp_name)
0199 {
0200 int fd, err;
0201
0202 fd = mkstemp(temp_name);
0203 if (fd < 0)
0204 return -errno;
0205
0206 err = vdso__copy_compat(prog, fd);
0207
0208 if (close(fd) == -1)
0209 return -errno;
0210
0211 return err;
0212 }
0213
0214 static const char *vdso__get_compat_file(struct vdso_file *vdso_file)
0215 {
0216 int err;
0217
0218 if (vdso_file->found)
0219 return vdso_file->temp_file_name;
0220
0221 if (vdso_file->error)
0222 return NULL;
0223
0224 err = vdso__create_compat_file(vdso_file->read_prog,
0225 vdso_file->temp_file_name);
0226 if (err) {
0227 pr_err("%s failed, error %d\n", vdso_file->read_prog, err);
0228 vdso_file->error = true;
0229 return NULL;
0230 }
0231
0232 vdso_file->found = true;
0233
0234 return vdso_file->temp_file_name;
0235 }
0236
0237 static struct dso *__machine__findnew_compat(struct machine *machine,
0238 struct vdso_file *vdso_file)
0239 {
0240 const char *file_name;
0241 struct dso *dso;
0242
0243 dso = __dsos__find(&machine->dsos, vdso_file->dso_name, true);
0244 if (dso)
0245 goto out;
0246
0247 file_name = vdso__get_compat_file(vdso_file);
0248 if (!file_name)
0249 goto out;
0250
0251 dso = __machine__addnew_vdso(machine, vdso_file->dso_name, file_name);
0252 out:
0253 return dso;
0254 }
0255
0256 static int __machine__findnew_vdso_compat(struct machine *machine,
0257 struct thread *thread,
0258 struct vdso_info *vdso_info,
0259 struct dso **dso)
0260 {
0261 enum dso_type dso_type;
0262
0263 dso_type = machine__thread_dso_type(machine, thread);
0264
0265 #ifndef HAVE_PERF_READ_VDSO32
0266 if (dso_type == DSO__TYPE_32BIT)
0267 return 0;
0268 #endif
0269 #ifndef HAVE_PERF_READ_VDSOX32
0270 if (dso_type == DSO__TYPE_X32BIT)
0271 return 0;
0272 #endif
0273
0274 switch (dso_type) {
0275 case DSO__TYPE_32BIT:
0276 *dso = __machine__findnew_compat(machine, &vdso_info->vdso32);
0277 return 1;
0278 case DSO__TYPE_X32BIT:
0279 *dso = __machine__findnew_compat(machine, &vdso_info->vdsox32);
0280 return 1;
0281 case DSO__TYPE_UNKNOWN:
0282 case DSO__TYPE_64BIT:
0283 default:
0284 return 0;
0285 }
0286 }
0287
0288 #endif
0289
0290 static struct dso *machine__find_vdso(struct machine *machine,
0291 struct thread *thread)
0292 {
0293 struct dso *dso = NULL;
0294 enum dso_type dso_type;
0295
0296 dso_type = machine__thread_dso_type(machine, thread);
0297 switch (dso_type) {
0298 case DSO__TYPE_32BIT:
0299 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO32, true);
0300 if (!dso) {
0301 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO,
0302 true);
0303 if (dso && dso_type != dso__type(dso, machine))
0304 dso = NULL;
0305 }
0306 break;
0307 case DSO__TYPE_X32BIT:
0308 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSOX32, true);
0309 break;
0310 case DSO__TYPE_64BIT:
0311 case DSO__TYPE_UNKNOWN:
0312 default:
0313 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, true);
0314 break;
0315 }
0316
0317 return dso;
0318 }
0319
0320 struct dso *machine__findnew_vdso(struct machine *machine,
0321 struct thread *thread)
0322 {
0323 struct vdso_info *vdso_info;
0324 struct dso *dso = NULL;
0325
0326 down_write(&machine->dsos.lock);
0327 if (!machine->vdso_info)
0328 machine->vdso_info = vdso_info__new();
0329
0330 vdso_info = machine->vdso_info;
0331 if (!vdso_info)
0332 goto out_unlock;
0333
0334 dso = machine__find_vdso(machine, thread);
0335 if (dso)
0336 goto out_unlock;
0337
0338 #if BITS_PER_LONG == 64
0339 if (__machine__findnew_vdso_compat(machine, thread, vdso_info, &dso))
0340 goto out_unlock;
0341 #endif
0342
0343 dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, true);
0344 if (!dso) {
0345 char *file;
0346
0347 file = get_file(&vdso_info->vdso);
0348 if (file)
0349 dso = __machine__addnew_vdso(machine, DSO__NAME_VDSO, file);
0350 }
0351
0352 out_unlock:
0353 dso__get(dso);
0354 up_write(&machine->dsos.lock);
0355 return dso;
0356 }
0357
0358 bool dso__is_vdso(struct dso *dso)
0359 {
0360 return !strcmp(dso->short_name, DSO__NAME_VDSO) ||
0361 !strcmp(dso->short_name, DSO__NAME_VDSO32) ||
0362 !strcmp(dso->short_name, DSO__NAME_VDSOX32);
0363 }