xref: /openbmc/linux/tools/perf/util/map.c (revision e6a9efce)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <inttypes.h>
3 #include <limits.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <linux/string.h>
8 #include <linux/zalloc.h>
9 #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
10 #include "debug.h"
11 #include "dso.h"
12 #include "map.h"
13 #include "namespaces.h"
14 #include "srcline.h"
15 #include "symbol.h"
16 #include "thread.h"
17 #include "vdso.h"
18 
19 static inline int is_android_lib(const char *filename)
20 {
21 	return strstarts(filename, "/data/app-lib/") ||
22 	       strstarts(filename, "/system/lib/");
23 }
24 
25 static inline bool replace_android_lib(const char *filename, char *newfilename)
26 {
27 	const char *libname;
28 	char *app_abi;
29 	size_t app_abi_length, new_length;
30 	size_t lib_length = 0;
31 
32 	libname  = strrchr(filename, '/');
33 	if (libname)
34 		lib_length = strlen(libname);
35 
36 	app_abi = getenv("APP_ABI");
37 	if (!app_abi)
38 		return false;
39 
40 	app_abi_length = strlen(app_abi);
41 
42 	if (strstarts(filename, "/data/app-lib/")) {
43 		char *apk_path;
44 
45 		if (!app_abi_length)
46 			return false;
47 
48 		new_length = 7 + app_abi_length + lib_length;
49 
50 		apk_path = getenv("APK_PATH");
51 		if (apk_path) {
52 			new_length += strlen(apk_path) + 1;
53 			if (new_length > PATH_MAX)
54 				return false;
55 			snprintf(newfilename, new_length,
56 				 "%s/libs/%s/%s", apk_path, app_abi, libname);
57 		} else {
58 			if (new_length > PATH_MAX)
59 				return false;
60 			snprintf(newfilename, new_length,
61 				 "libs/%s/%s", app_abi, libname);
62 		}
63 		return true;
64 	}
65 
66 	if (strstarts(filename, "/system/lib/")) {
67 		char *ndk, *app;
68 		const char *arch;
69 		int ndk_length, app_length;
70 
71 		ndk = getenv("NDK_ROOT");
72 		app = getenv("APP_PLATFORM");
73 
74 		if (!(ndk && app))
75 			return false;
76 
77 		ndk_length = strlen(ndk);
78 		app_length = strlen(app);
79 
80 		if (!(ndk_length && app_length && app_abi_length))
81 			return false;
82 
83 		arch = !strncmp(app_abi, "arm", 3) ? "arm" :
84 		       !strncmp(app_abi, "mips", 4) ? "mips" :
85 		       !strncmp(app_abi, "x86", 3) ? "x86" : NULL;
86 
87 		if (!arch)
88 			return false;
89 
90 		new_length = 27 + ndk_length +
91 			     app_length + lib_length
92 			   + strlen(arch);
93 
94 		if (new_length > PATH_MAX)
95 			return false;
96 		snprintf(newfilename, new_length,
97 			"%.*s/platforms/%.*s/arch-%s/usr/lib/%s",
98 			ndk_length, ndk, app_length, app, arch, libname);
99 
100 		return true;
101 	}
102 	return false;
103 }
104 
105 void map__init(struct map *map, u64 start, u64 end, u64 pgoff, struct dso *dso)
106 {
107 	map__set_start(map, start);
108 	map__set_end(map, end);
109 	map__set_pgoff(map, pgoff);
110 	map__set_reloc(map, 0);
111 	map__set_dso(map, dso__get(dso));
112 	map__set_map_ip(map, map__dso_map_ip);
113 	map__set_unmap_ip(map, map__dso_unmap_ip);
114 	map__set_erange_warned(map, false);
115 	refcount_set(map__refcnt(map), 1);
116 }
117 
118 struct map *map__new(struct machine *machine, u64 start, u64 len,
119 		     u64 pgoff, struct dso_id *id,
120 		     u32 prot, u32 flags, struct build_id *bid,
121 		     char *filename, struct thread *thread)
122 {
123 	struct map *map = malloc(sizeof(*map));
124 	struct nsinfo *nsi = NULL;
125 	struct nsinfo *nnsi;
126 
127 	if (map != NULL) {
128 		char newfilename[PATH_MAX];
129 		struct dso *dso, *header_bid_dso;
130 		int anon, no_dso, vdso, android;
131 
132 		android = is_android_lib(filename);
133 		anon = is_anon_memory(filename) || flags & MAP_HUGETLB;
134 		vdso = is_vdso_map(filename);
135 		no_dso = is_no_dso_memory(filename);
136 		map->prot = prot;
137 		map->flags = flags;
138 		nsi = nsinfo__get(thread->nsinfo);
139 
140 		if ((anon || no_dso) && nsi && (prot & PROT_EXEC)) {
141 			snprintf(newfilename, sizeof(newfilename),
142 				 "/tmp/perf-%d.map", nsinfo__pid(nsi));
143 			filename = newfilename;
144 		}
145 
146 		if (android) {
147 			if (replace_android_lib(filename, newfilename))
148 				filename = newfilename;
149 		}
150 
151 		if (vdso) {
152 			/* The vdso maps are always on the host and not the
153 			 * container.  Ensure that we don't use setns to look
154 			 * them up.
155 			 */
156 			nnsi = nsinfo__copy(nsi);
157 			if (nnsi) {
158 				nsinfo__put(nsi);
159 				nsinfo__clear_need_setns(nnsi);
160 				nsi = nnsi;
161 			}
162 			pgoff = 0;
163 			dso = machine__findnew_vdso(machine, thread);
164 		} else
165 			dso = machine__findnew_dso_id(machine, filename, id);
166 
167 		if (dso == NULL)
168 			goto out_delete;
169 
170 		map__init(map, start, start + len, pgoff, dso);
171 
172 		if (anon || no_dso) {
173 			map->map_ip = map->unmap_ip = identity__map_ip;
174 
175 			/*
176 			 * Set memory without DSO as loaded. All map__find_*
177 			 * functions still return NULL, and we avoid the
178 			 * unnecessary map__load warning.
179 			 */
180 			if (!(prot & PROT_EXEC))
181 				dso__set_loaded(dso);
182 		}
183 		mutex_lock(&dso->lock);
184 		nsinfo__put(dso->nsinfo);
185 		dso->nsinfo = nsi;
186 		mutex_unlock(&dso->lock);
187 
188 		if (build_id__is_defined(bid)) {
189 			dso__set_build_id(dso, bid);
190 		} else {
191 			/*
192 			 * If the mmap event had no build ID, search for an existing dso from the
193 			 * build ID header by name. Otherwise only the dso loaded at the time of
194 			 * reading the header will have the build ID set and all future mmaps will
195 			 * have it missing.
196 			 */
197 			down_read(&machine->dsos.lock);
198 			header_bid_dso = __dsos__find(&machine->dsos, filename, false);
199 			up_read(&machine->dsos.lock);
200 			if (header_bid_dso && header_bid_dso->header_build_id) {
201 				dso__set_build_id(dso, &header_bid_dso->bid);
202 				dso->header_build_id = 1;
203 			}
204 		}
205 		dso__put(dso);
206 	}
207 	return map;
208 out_delete:
209 	nsinfo__put(nsi);
210 	free(map);
211 	return NULL;
212 }
213 
214 /*
215  * Constructor variant for modules (where we know from /proc/modules where
216  * they are loaded) and for vmlinux, where only after we load all the
217  * symbols we'll know where it starts and ends.
218  */
219 struct map *map__new2(u64 start, struct dso *dso)
220 {
221 	struct map *map = calloc(1, (sizeof(*map) +
222 				     (dso->kernel ? sizeof(struct kmap) : 0)));
223 	if (map != NULL) {
224 		/*
225 		 * ->end will be filled after we load all the symbols
226 		 */
227 		map__init(map, start, 0, 0, dso);
228 	}
229 
230 	return map;
231 }
232 
233 bool __map__is_kernel(const struct map *map)
234 {
235 	if (!map__dso(map)->kernel)
236 		return false;
237 	return machine__kernel_map(maps__machine(map__kmaps((struct map *)map))) == map;
238 }
239 
240 bool __map__is_extra_kernel_map(const struct map *map)
241 {
242 	struct kmap *kmap = __map__kmap((struct map *)map);
243 
244 	return kmap && kmap->name[0];
245 }
246 
247 bool __map__is_bpf_prog(const struct map *map)
248 {
249 	const char *name;
250 	struct dso *dso = map__dso(map);
251 
252 	if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
253 		return true;
254 
255 	/*
256 	 * If PERF_RECORD_BPF_EVENT is not included, the dso will not have
257 	 * type of DSO_BINARY_TYPE__BPF_PROG_INFO. In such cases, we can
258 	 * guess the type based on name.
259 	 */
260 	name = dso->short_name;
261 	return name && (strstr(name, "bpf_prog_") == name);
262 }
263 
264 bool __map__is_bpf_image(const struct map *map)
265 {
266 	const char *name;
267 	struct dso *dso = map__dso(map);
268 
269 	if (dso->binary_type == DSO_BINARY_TYPE__BPF_IMAGE)
270 		return true;
271 
272 	/*
273 	 * If PERF_RECORD_KSYMBOL is not included, the dso will not have
274 	 * type of DSO_BINARY_TYPE__BPF_IMAGE. In such cases, we can
275 	 * guess the type based on name.
276 	 */
277 	name = dso->short_name;
278 	return name && is_bpf_image(name);
279 }
280 
281 bool __map__is_ool(const struct map *map)
282 {
283 	const struct dso *dso = map__dso(map);
284 
285 	return dso && dso->binary_type == DSO_BINARY_TYPE__OOL;
286 }
287 
288 bool map__has_symbols(const struct map *map)
289 {
290 	return dso__has_symbols(map__dso(map));
291 }
292 
293 static void map__exit(struct map *map)
294 {
295 	BUG_ON(refcount_read(map__refcnt(map)) != 0);
296 	dso__zput(map->dso);
297 }
298 
299 void map__delete(struct map *map)
300 {
301 	map__exit(map);
302 	free(map);
303 }
304 
305 void map__put(struct map *map)
306 {
307 	if (map && refcount_dec_and_test(map__refcnt(map)))
308 		map__delete(map);
309 }
310 
311 void map__fixup_start(struct map *map)
312 {
313 	struct dso *dso = map__dso(map);
314 	struct rb_root_cached *symbols = &dso->symbols;
315 	struct rb_node *nd = rb_first_cached(symbols);
316 
317 	if (nd != NULL) {
318 		struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
319 
320 		map__set_start(map, sym->start);
321 	}
322 }
323 
324 void map__fixup_end(struct map *map)
325 {
326 	struct dso *dso = map__dso(map);
327 	struct rb_root_cached *symbols = &dso->symbols;
328 	struct rb_node *nd = rb_last(&symbols->rb_root);
329 
330 	if (nd != NULL) {
331 		struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
332 		map__set_end(map, sym->end);
333 	}
334 }
335 
336 #define DSO__DELETED "(deleted)"
337 
338 int map__load(struct map *map)
339 {
340 	struct dso *dso = map__dso(map);
341 	const char *name = dso->long_name;
342 	int nr;
343 
344 	if (dso__loaded(dso))
345 		return 0;
346 
347 	nr = dso__load(dso, map);
348 	if (nr < 0) {
349 		if (dso->has_build_id) {
350 			char sbuild_id[SBUILD_ID_SIZE];
351 
352 			build_id__sprintf(&dso->bid, sbuild_id);
353 			pr_debug("%s with build id %s not found", name, sbuild_id);
354 		} else
355 			pr_debug("Failed to open %s", name);
356 
357 		pr_debug(", continuing without symbols\n");
358 		return -1;
359 	} else if (nr == 0) {
360 #ifdef HAVE_LIBELF_SUPPORT
361 		const size_t len = strlen(name);
362 		const size_t real_len = len - sizeof(DSO__DELETED);
363 
364 		if (len > sizeof(DSO__DELETED) &&
365 		    strcmp(name + real_len + 1, DSO__DELETED) == 0) {
366 			pr_debug("%.*s was updated (is prelink enabled?). "
367 				"Restart the long running apps that use it!\n",
368 				   (int)real_len, name);
369 		} else {
370 			pr_debug("no symbols found in %s, maybe install a debug package?\n", name);
371 		}
372 #endif
373 		return -1;
374 	}
375 
376 	return 0;
377 }
378 
379 struct symbol *map__find_symbol(struct map *map, u64 addr)
380 {
381 	if (map__load(map) < 0)
382 		return NULL;
383 
384 	return dso__find_symbol(map__dso(map), addr);
385 }
386 
387 struct symbol *map__find_symbol_by_name(struct map *map, const char *name)
388 {
389 	struct dso *dso;
390 
391 	if (map__load(map) < 0)
392 		return NULL;
393 
394 	dso = map__dso(map);
395 	if (!dso__sorted_by_name(dso))
396 		dso__sort_by_name(dso);
397 
398 	return dso__find_symbol_by_name(dso, name);
399 }
400 
401 struct map *map__clone(struct map *from)
402 {
403 	size_t size = sizeof(struct map);
404 	struct map *map;
405 	struct dso *dso = map__dso(from);
406 
407 	if (dso && dso->kernel)
408 		size += sizeof(struct kmap);
409 
410 	map = memdup(from, size);
411 	if (map != NULL) {
412 		refcount_set(&map->refcnt, 1);
413 		map->dso = dso__get(dso);
414 	}
415 
416 	return map;
417 }
418 
419 size_t map__fprintf(struct map *map, FILE *fp)
420 {
421 	const struct dso *dso = map__dso(map);
422 
423 	return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
424 		       map__start(map), map__end(map), map__pgoff(map), dso->name);
425 }
426 
427 size_t map__fprintf_dsoname(struct map *map, FILE *fp)
428 {
429 	char buf[symbol_conf.pad_output_len_dso + 1];
430 	const char *dsoname = "[unknown]";
431 	const struct dso *dso = map ? map__dso(map) : NULL;
432 
433 	if (dso) {
434 		if (symbol_conf.show_kernel_path && dso->long_name)
435 			dsoname = dso->long_name;
436 		else
437 			dsoname = dso->name;
438 	}
439 
440 	if (symbol_conf.pad_output_len_dso) {
441 		scnprintf_pad(buf, symbol_conf.pad_output_len_dso, "%s", dsoname);
442 		dsoname = buf;
443 	}
444 
445 	return fprintf(fp, "%s", dsoname);
446 }
447 
448 char *map__srcline(struct map *map, u64 addr, struct symbol *sym)
449 {
450 	if (map == NULL)
451 		return SRCLINE_UNKNOWN;
452 
453 	return get_srcline(map__dso(map), map__rip_2objdump(map, addr), sym, true, true, addr);
454 }
455 
456 int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
457 			 FILE *fp)
458 {
459 	const struct dso *dso = map ? map__dso(map) : NULL;
460 	int ret = 0;
461 
462 	if (dso) {
463 		char *srcline = map__srcline(map, addr, NULL);
464 		if (strncmp(srcline, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0)
465 			ret = fprintf(fp, "%s%s", prefix, srcline);
466 		free_srcline(srcline);
467 	}
468 	return ret;
469 }
470 
471 void srccode_state_free(struct srccode_state *state)
472 {
473 	zfree(&state->srcfile);
474 	state->line = 0;
475 }
476 
477 /**
478  * map__rip_2objdump - convert symbol start address to objdump address.
479  * @map: memory map
480  * @rip: symbol start address
481  *
482  * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
483  * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
484  * relative to section start.
485  *
486  * Return: Address suitable for passing to "objdump --start-address="
487  */
488 u64 map__rip_2objdump(struct map *map, u64 rip)
489 {
490 	struct kmap *kmap = __map__kmap(map);
491 	const struct dso *dso = map__dso(map);
492 
493 	/*
494 	 * vmlinux does not have program headers for PTI entry trampolines and
495 	 * kcore may not either. However the trampoline object code is on the
496 	 * main kernel map, so just use that instead.
497 	 */
498 	if (kmap && is_entry_trampoline(kmap->name) && kmap->kmaps) {
499 		struct machine *machine = maps__machine(kmap->kmaps);
500 
501 		if (machine) {
502 			struct map *kernel_map = machine__kernel_map(machine);
503 
504 			if (kernel_map)
505 				map = kernel_map;
506 		}
507 	}
508 
509 	if (!dso->adjust_symbols)
510 		return rip;
511 
512 	if (dso->rel)
513 		return rip - map__pgoff(map);
514 
515 	/*
516 	 * kernel modules also have DSO_TYPE_USER in dso->kernel,
517 	 * but all kernel modules are ET_REL, so won't get here.
518 	 */
519 	if (dso->kernel == DSO_SPACE__USER)
520 		return rip + dso->text_offset;
521 
522 	return map__unmap_ip(map, rip) - map__reloc(map);
523 }
524 
525 /**
526  * map__objdump_2mem - convert objdump address to a memory address.
527  * @map: memory map
528  * @ip: objdump address
529  *
530  * Closely related to map__rip_2objdump(), this function takes an address from
531  * objdump and converts it to a memory address.  Note this assumes that @map
532  * contains the address.  To be sure the result is valid, check it forwards
533  * e.g. map__rip_2objdump(map__map_ip(map, map__objdump_2mem(map, ip))) == ip
534  *
535  * Return: Memory address.
536  */
537 u64 map__objdump_2mem(struct map *map, u64 ip)
538 {
539 	const struct dso *dso = map__dso(map);
540 
541 	if (!dso->adjust_symbols)
542 		return map__unmap_ip(map, ip);
543 
544 	if (dso->rel)
545 		return map__unmap_ip(map, ip + map__pgoff(map));
546 
547 	/*
548 	 * kernel modules also have DSO_TYPE_USER in dso->kernel,
549 	 * but all kernel modules are ET_REL, so won't get here.
550 	 */
551 	if (dso->kernel == DSO_SPACE__USER)
552 		return map__unmap_ip(map, ip - dso->text_offset);
553 
554 	return ip + map__reloc(map);
555 }
556 
557 bool map__contains_symbol(const struct map *map, const struct symbol *sym)
558 {
559 	u64 ip = map__unmap_ip(map, sym->start);
560 
561 	return ip >= map__start(map) && ip < map__end(map);
562 }
563 
564 struct kmap *__map__kmap(struct map *map)
565 {
566 	const struct dso *dso = map__dso(map);
567 
568 	if (!dso || !dso->kernel)
569 		return NULL;
570 	return (struct kmap *)(map + 1);
571 }
572 
573 struct kmap *map__kmap(struct map *map)
574 {
575 	struct kmap *kmap = __map__kmap(map);
576 
577 	if (!kmap)
578 		pr_err("Internal error: map__kmap with a non-kernel map\n");
579 	return kmap;
580 }
581 
582 struct maps *map__kmaps(struct map *map)
583 {
584 	struct kmap *kmap = map__kmap(map);
585 
586 	if (!kmap || !kmap->kmaps) {
587 		pr_err("Internal error: map__kmaps with a non-kernel map\n");
588 		return NULL;
589 	}
590 	return kmap->kmaps;
591 }
592 
593 u64 map__dso_map_ip(const struct map *map, u64 ip)
594 {
595 	return ip - map__start(map) + map__pgoff(map);
596 }
597 
598 u64 map__dso_unmap_ip(const struct map *map, u64 ip)
599 {
600 	return ip + map__start(map) - map__pgoff(map);
601 }
602 
603 u64 identity__map_ip(const struct map *map __maybe_unused, u64 ip)
604 {
605 	return ip;
606 }
607