| 
									
										
										
										
											2012-09-10 18:50:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <sys/types.h>
 | 
					
						
							|  |  |  | #include <sys/stat.h>
 | 
					
						
							|  |  |  | #include <fcntl.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <linux/kernel.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "vdso.h"
 | 
					
						
							|  |  |  | #include "util.h"
 | 
					
						
							|  |  |  | #include "symbol.h"
 | 
					
						
							| 
									
										
										
										
											2014-07-22 16:17:53 +03:00
										 |  |  | #include "machine.h"
 | 
					
						
							| 
									
										
										
										
											2012-09-10 18:50:19 +02:00
										 |  |  | #include "linux/string.h"
 | 
					
						
							| 
									
										
										
										
											2014-07-14 23:46:48 +02:00
										 |  |  | #include "debug.h"
 | 
					
						
							| 
									
										
										
										
											2012-09-10 18:50:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-22 16:17:54 +03:00
										 |  |  | #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; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-23 14:23:00 +03:00
										 |  |  | static struct vdso_info *vdso_info__new(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	static const struct vdso_info vdso_info_init = { | 
					
						
							|  |  |  | 		.vdso    = { | 
					
						
							|  |  |  | 			.temp_file_name = VDSO__TEMP_FILE_NAME, | 
					
						
							| 
									
										
										
										
											2014-07-22 16:17:57 +03:00
										 |  |  | 			.dso_name = DSO__NAME_VDSO, | 
					
						
							| 
									
										
										
										
											2014-07-23 14:23:00 +03:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return memdup(&vdso_info_init, sizeof(vdso_info_init)); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2012-09-10 18:50:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | static int find_vdso_map(void **start, void **end) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	FILE *maps; | 
					
						
							|  |  |  | 	char line[128]; | 
					
						
							|  |  |  | 	int found = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	maps = fopen("/proc/self/maps", "r"); | 
					
						
							|  |  |  | 	if (!maps) { | 
					
						
							|  |  |  | 		pr_err("vdso: cannot open maps\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while (!found && fgets(line, sizeof(line), maps)) { | 
					
						
							|  |  |  | 		int m = -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* We care only about private r-x mappings. */ | 
					
						
							|  |  |  | 		if (2 != sscanf(line, "%p-%p r-xp %*x %*x:%*x %*u %n", | 
					
						
							|  |  |  | 				start, end, &m)) | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		if (m < 0) | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!strncmp(&line[m], VDSO__MAP_NAME, | 
					
						
							|  |  |  | 			     sizeof(VDSO__MAP_NAME) - 1)) | 
					
						
							|  |  |  | 			found = 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fclose(maps); | 
					
						
							|  |  |  | 	return !found; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-22 16:17:54 +03:00
										 |  |  | static char *get_file(struct vdso_file *vdso_file) | 
					
						
							| 
									
										
										
										
											2012-09-10 18:50:19 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	char *vdso = NULL; | 
					
						
							|  |  |  | 	char *buf = NULL; | 
					
						
							|  |  |  | 	void *start, *end; | 
					
						
							|  |  |  | 	size_t size; | 
					
						
							|  |  |  | 	int fd; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-22 16:17:54 +03:00
										 |  |  | 	if (vdso_file->found) | 
					
						
							|  |  |  | 		return vdso_file->temp_file_name; | 
					
						
							| 
									
										
										
										
											2012-09-10 18:50:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-22 16:17:54 +03:00
										 |  |  | 	if (vdso_file->error || find_vdso_map(&start, &end)) | 
					
						
							| 
									
										
										
										
											2012-09-10 18:50:19 +02:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	size = end - start; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	buf = memdup(start, size); | 
					
						
							|  |  |  | 	if (!buf) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-22 16:17:54 +03:00
										 |  |  | 	fd = mkstemp(vdso_file->temp_file_name); | 
					
						
							| 
									
										
										
										
											2012-09-10 18:50:19 +02:00
										 |  |  | 	if (fd < 0) | 
					
						
							|  |  |  | 		goto out; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (size == (size_t) write(fd, buf, size)) | 
					
						
							| 
									
										
										
										
											2014-07-22 16:17:54 +03:00
										 |  |  | 		vdso = vdso_file->temp_file_name; | 
					
						
							| 
									
										
										
										
											2012-09-10 18:50:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	close(fd); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  out: | 
					
						
							|  |  |  | 	free(buf); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-22 16:17:54 +03:00
										 |  |  | 	vdso_file->found = (vdso != NULL); | 
					
						
							|  |  |  | 	vdso_file->error = !vdso_file->found; | 
					
						
							| 
									
										
										
										
											2012-09-10 18:50:19 +02:00
										 |  |  | 	return vdso; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-23 14:23:00 +03:00
										 |  |  | void vdso__exit(struct machine *machine) | 
					
						
							| 
									
										
										
										
											2012-09-10 18:50:19 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-07-23 14:23:00 +03:00
										 |  |  | 	struct vdso_info *vdso_info = machine->vdso_info; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!vdso_info) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-22 16:17:54 +03:00
										 |  |  | 	if (vdso_info->vdso.found) | 
					
						
							|  |  |  | 		unlink(vdso_info->vdso.temp_file_name); | 
					
						
							| 
									
										
										
										
											2014-07-23 14:23:00 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	zfree(&machine->vdso_info); | 
					
						
							| 
									
										
										
										
											2012-09-10 18:50:19 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-22 16:17:56 +03:00
										 |  |  | 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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-22 16:18:00 +03:00
										 |  |  | struct dso *vdso__dso_findnew(struct machine *machine, | 
					
						
							|  |  |  | 			      struct thread *thread __maybe_unused) | 
					
						
							| 
									
										
										
										
											2012-09-10 18:50:19 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-07-23 14:23:00 +03:00
										 |  |  | 	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; | 
					
						
							| 
									
										
										
										
											2012-09-10 18:50:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-22 16:17:57 +03:00
										 |  |  | 	dso = dsos__find(&machine->user_dsos, DSO__NAME_VDSO, true); | 
					
						
							| 
									
										
										
										
											2012-09-10 18:50:19 +02:00
										 |  |  | 	if (!dso) { | 
					
						
							|  |  |  | 		char *file; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-22 16:17:54 +03:00
										 |  |  | 		file = get_file(&vdso_info->vdso); | 
					
						
							| 
									
										
										
										
											2012-09-10 18:50:19 +02:00
										 |  |  | 		if (!file) | 
					
						
							|  |  |  | 			return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-22 16:17:57 +03:00
										 |  |  | 		dso = vdso__new(machine, DSO__NAME_VDSO, file); | 
					
						
							| 
									
										
										
										
											2012-09-10 18:50:19 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return dso; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-07-22 16:17:57 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | bool dso__is_vdso(struct dso *dso) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return !strcmp(dso->short_name, DSO__NAME_VDSO); | 
					
						
							|  |  |  | } |