#include #include #include #include #include #include #include #include #include "vdso.h" #include "util.h" #include "symbol.h" #include "machine.h" #include "linux/string.h" #include "debug.h" /* * Include definition of find_vdso_map() also used in perf-read-vdso.c for * building perf-read-vdso32 and perf-read-vdsox32. */ #include "find-vdso-map.c" #define VDSO__TEMP_FILE_NAME "/tmp/perf-vdso.so-XXXXXX" struct vdso_file { bool found; bool error; char temp_file_name[sizeof(VDSO__TEMP_FILE_NAME)]; const char *dso_name; }; struct vdso_info { struct vdso_file vdso; }; static struct vdso_info *vdso_info__new(void) { static const struct vdso_info vdso_info_init = { .vdso = { .temp_file_name = VDSO__TEMP_FILE_NAME, .dso_name = DSO__NAME_VDSO, }, }; return memdup(&vdso_info_init, sizeof(vdso_info_init)); } static char *get_file(struct vdso_file *vdso_file) { char *vdso = NULL; char *buf = NULL; void *start, *end; size_t size; int fd; if (vdso_file->found) return vdso_file->temp_file_name; if (vdso_file->error || find_vdso_map(&start, &end)) return NULL; size = end - start; buf = memdup(start, size); if (!buf) return NULL; fd = mkstemp(vdso_file->temp_file_name); if (fd < 0) goto out; if (size == (size_t) write(fd, buf, size)) vdso = vdso_file->temp_file_name; close(fd); out: free(buf); vdso_file->found = (vdso != NULL); vdso_file->error = !vdso_file->found; return vdso; } void vdso__exit(struct machine *machine) { struct vdso_info *vdso_info = machine->vdso_info; if (!vdso_info) return; if (vdso_info->vdso.found) unlink(vdso_info->vdso.temp_file_name); zfree(&machine->vdso_info); } static struct dso *vdso__new(struct machine *machine, const char *short_name, const char *long_name) { struct dso *dso; dso = dso__new(short_name); if (dso != NULL) { dsos__add(&machine->user_dsos, dso); dso__set_long_name(dso, long_name, false); } return dso; } struct dso *vdso__dso_findnew(struct machine *machine, struct thread *thread __maybe_unused) { struct vdso_info *vdso_info; struct dso *dso; if (!machine->vdso_info) machine->vdso_info = vdso_info__new(); vdso_info = machine->vdso_info; if (!vdso_info) return NULL; dso = dsos__find(&machine->user_dsos, DSO__NAME_VDSO, true); if (!dso) { char *file; file = get_file(&vdso_info->vdso); if (!file) return NULL; dso = vdso__new(machine, DSO__NAME_VDSO, file); } return dso; } bool dso__is_vdso(struct dso *dso) { return !strcmp(dso->short_name, DSO__NAME_VDSO); }