xref: /linux/tools/perf/util/find-map.c (revision 473f6c8f437b049f8ec015d57cd59bb983b1d85c)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 
6 static int find_map(void **start, void **end, const char *name)
7 {
8 	FILE *maps;
9 	char *line = NULL;
10 	size_t len = 0;
11 	int found = 0;
12 
13 	maps = fopen("/proc/self/maps", "r");
14 	if (!maps) {
15 		fprintf(stderr, "cannot open maps\n");
16 		return -1;
17 	}
18 
19 	while (!found && getline(&line, &len, maps) != -1) {
20 		int m = -1;
21 
22 		/* We care only about private r-x mappings. */
23 		if (2 != sscanf(line, "%p-%p r-xp %*x %*x:%*x %*u %n",
24 				start, end, &m))
25 			continue;
26 		if (m < 0)
27 			continue;
28 
29 		if (!strncmp(&line[m], name, strlen(name)))
30 			found = 1;
31 	}
32 
33 	free(line);
34 	fclose(maps);
35 	return !found;
36 }
37