]> git.ipfire.org Git - thirdparty/linux.git/blame - tools/perf/util/map.c
perf tools: Check maps for bpf programs
[thirdparty/linux.git] / tools / perf / util / map.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
66e274f3 2#include "symbol.h"
c6e718ff 3#include <errno.h>
9486aa38 4#include <inttypes.h>
4b8cf846 5#include <limits.h>
66e274f3
FW
6#include <stdlib.h>
7#include <string.h>
8#include <stdio.h>
a1645ce1 9#include <unistd.h>
fbef103f 10#include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
4b8cf846 11#include "map.h"
5cd95c2d 12#include "thread.h"
7dbf4dcf 13#include "vdso.h"
ebb296c2 14#include "build-id.h"
cc8fae1d 15#include "util.h"
acebd408 16#include "debug.h"
2a03068c 17#include "machine.h"
8e16017d 18#include <linux/string.h>
632a5cab 19#include "srcline.h"
843ff37b 20#include "namespaces.h"
6c502584 21#include "unwind.h"
dd2e18e9 22#include "srccode.h"
66e274f3 23
6a2ffcdd 24static void __maps__insert(struct maps *maps, struct map *map);
1e628569 25static void __maps__insert_name(struct maps *maps, struct map *map);
6a2ffcdd 26
0ac3348e 27static inline int is_anon_memory(const char *filename, u32 flags)
66e274f3 28{
fbef103f 29 return flags & MAP_HUGETLB ||
0ac3348e 30 !strcmp(filename, "//anon") ||
b2be5451
YB
31 !strncmp(filename, "/dev/zero", sizeof("/dev/zero") - 1) ||
32 !strncmp(filename, "/anon_hugepage", sizeof("/anon_hugepage") - 1);
66e274f3
FW
33}
34
87ffef79
JO
35static inline int is_no_dso_memory(const char *filename)
36{
1e82574d 37 return !strncmp(filename, "[stack", 6) ||
700be564 38 !strncmp(filename, "/SYSV",5) ||
87ffef79
JO
39 !strcmp(filename, "[heap]");
40}
41
eca81836
ML
42static inline int is_android_lib(const char *filename)
43{
44 return !strncmp(filename, "/data/app-lib", 13) ||
45 !strncmp(filename, "/system/lib", 11);
46}
47
48static inline bool replace_android_lib(const char *filename, char *newfilename)
49{
50 const char *libname;
51 char *app_abi;
52 size_t app_abi_length, new_length;
53 size_t lib_length = 0;
54
55 libname = strrchr(filename, '/');
56 if (libname)
57 lib_length = strlen(libname);
58
59 app_abi = getenv("APP_ABI");
60 if (!app_abi)
61 return false;
62
63 app_abi_length = strlen(app_abi);
64
65 if (!strncmp(filename, "/data/app-lib", 13)) {
66 char *apk_path;
67
68 if (!app_abi_length)
69 return false;
70
71 new_length = 7 + app_abi_length + lib_length;
72
73 apk_path = getenv("APK_PATH");
74 if (apk_path) {
75 new_length += strlen(apk_path) + 1;
76 if (new_length > PATH_MAX)
77 return false;
78 snprintf(newfilename, new_length,
79 "%s/libs/%s/%s", apk_path, app_abi, libname);
80 } else {
81 if (new_length > PATH_MAX)
82 return false;
83 snprintf(newfilename, new_length,
84 "libs/%s/%s", app_abi, libname);
85 }
86 return true;
87 }
88
89 if (!strncmp(filename, "/system/lib/", 11)) {
90 char *ndk, *app;
91 const char *arch;
92 size_t ndk_length;
93 size_t app_length;
94
95 ndk = getenv("NDK_ROOT");
96 app = getenv("APP_PLATFORM");
97
98 if (!(ndk && app))
99 return false;
100
101 ndk_length = strlen(ndk);
102 app_length = strlen(app);
103
104 if (!(ndk_length && app_length && app_abi_length))
105 return false;
106
107 arch = !strncmp(app_abi, "arm", 3) ? "arm" :
108 !strncmp(app_abi, "mips", 4) ? "mips" :
109 !strncmp(app_abi, "x86", 3) ? "x86" : NULL;
110
111 if (!arch)
112 return false;
113
114 new_length = 27 + ndk_length +
115 app_length + lib_length
116 + strlen(arch);
117
118 if (new_length > PATH_MAX)
119 return false;
120 snprintf(newfilename, new_length,
121 "%s/platforms/%s/arch-%s/usr/lib/%s",
122 ndk, app, arch, libname);
123
124 return true;
125 }
126 return false;
127}
128
3183f8ca 129void map__init(struct map *map, u64 start, u64 end, u64 pgoff, struct dso *dso)
afb7b4f0 130{
237a7e04
ACM
131 map->start = start;
132 map->end = end;
133 map->pgoff = pgoff;
9176753d 134 map->reloc = 0;
d3a7c489 135 map->dso = dso__get(dso);
237a7e04
ACM
136 map->map_ip = map__map_ip;
137 map->unmap_ip = map__unmap_ip;
138 RB_CLEAR_NODE(&map->rb_node);
139 map->groups = NULL;
237a7e04 140 map->erange_warned = false;
e3a42cdd 141 refcount_set(&map->refcnt, 1);
afb7b4f0
ACM
142}
143
2a03068c 144struct map *map__new(struct machine *machine, u64 start, u64 len,
bf2e710b 145 u64 pgoff, u32 d_maj, u32 d_min, u64 ino,
7ef80703 146 u64 ino_gen, u32 prot, u32 flags, char *filename,
3183f8ca 147 struct thread *thread)
66e274f3 148{
237a7e04 149 struct map *map = malloc(sizeof(*map));
bf2e710b
KJ
150 struct nsinfo *nsi = NULL;
151 struct nsinfo *nnsi;
66e274f3 152
237a7e04 153 if (map != NULL) {
66e274f3 154 char newfilename[PATH_MAX];
afb7b4f0 155 struct dso *dso;
eca81836 156 int anon, no_dso, vdso, android;
66e274f3 157
eca81836 158 android = is_android_lib(filename);
0ac3348e 159 anon = is_anon_memory(filename, flags);
7dbf4dcf 160 vdso = is_vdso_map(filename);
87ffef79 161 no_dso = is_no_dso_memory(filename);
66e274f3 162
5c5e854b
SE
163 map->maj = d_maj;
164 map->min = d_min;
165 map->ino = ino;
166 map->ino_generation = ino_gen;
7ef80703
DZ
167 map->prot = prot;
168 map->flags = flags;
bf2e710b 169 nsi = nsinfo__get(thread->nsinfo);
5c5e854b 170
d183b261 171 if ((anon || no_dso) && nsi && (prot & PROT_EXEC)) {
bf2e710b
KJ
172 snprintf(newfilename, sizeof(newfilename),
173 "/tmp/perf-%d.map", nsi->pid);
66e274f3
FW
174 filename = newfilename;
175 }
176
eca81836
ML
177 if (android) {
178 if (replace_android_lib(filename, newfilename))
179 filename = newfilename;
180 }
181
7dbf4dcf 182 if (vdso) {
bf2e710b
KJ
183 /* The vdso maps are always on the host and not the
184 * container. Ensure that we don't use setns to look
185 * them up.
186 */
187 nnsi = nsinfo__copy(nsi);
188 if (nnsi) {
189 nsinfo__put(nsi);
190 nnsi->need_setns = false;
191 nsi = nnsi;
192 }
7dbf4dcf 193 pgoff = 0;
9a4388c7 194 dso = machine__findnew_vdso(machine, thread);
7dbf4dcf 195 } else
aa7cc2ae 196 dso = machine__findnew_dso(machine, filename);
7dbf4dcf 197
afb7b4f0 198 if (dso == NULL)
66e274f3
FW
199 goto out_delete;
200
3183f8ca 201 map__init(map, start, start + len, pgoff, dso);
afb7b4f0 202
87ffef79 203 if (anon || no_dso) {
237a7e04 204 map->map_ip = map->unmap_ip = identity__map_ip;
87ffef79
JO
205
206 /*
207 * Set memory without DSO as loaded. All map__find_*
208 * functions still return NULL, and we avoid the
209 * unnecessary map__load warning.
210 */
d183b261 211 if (!(prot & PROT_EXEC))
3183f8ca 212 dso__set_loaded(dso);
8d92c02a 213 }
bf2e710b 214 dso->nsinfo = nsi;
d3a7c489 215 dso__put(dso);
66e274f3 216 }
237a7e04 217 return map;
66e274f3 218out_delete:
bf2e710b 219 nsinfo__put(nsi);
237a7e04 220 free(map);
66e274f3
FW
221 return NULL;
222}
223
e5a1845f
NK
224/*
225 * Constructor variant for modules (where we know from /proc/modules where
226 * they are loaded) and for vmlinux, where only after we load all the
227 * symbols we'll know where it starts and ends.
228 */
3183f8ca 229struct map *map__new2(u64 start, struct dso *dso)
e5a1845f
NK
230{
231 struct map *map = calloc(1, (sizeof(*map) +
232 (dso->kernel ? sizeof(struct kmap) : 0)));
233 if (map != NULL) {
234 /*
235 * ->end will be filled after we load all the symbols
236 */
3183f8ca 237 map__init(map, start, 0, 0, dso);
e5a1845f
NK
238 }
239
240 return map;
241}
242
e6ce7126
ACM
243/*
244 * Use this and __map__is_kmodule() for map instances that are in
245 * machine->kmaps, and thus have map->groups->machine all properly set, to
246 * disambiguate between the kernel and modules.
247 *
248 * When the need arises, introduce map__is_{kernel,kmodule)() that
249 * checks (map->groups != NULL && map->groups->machine != NULL &&
250 * map->dso->kernel) before calling __map__is_{kernel,kmodule}())
251 */
252bool __map__is_kernel(const struct map *map)
253{
3183f8ca 254 return machine__kernel_map(map->groups->machine) == map;
e6ce7126
ACM
255}
256
5759a682
AH
257bool __map__is_extra_kernel_map(const struct map *map)
258{
259 struct kmap *kmap = __map__kmap((struct map *)map);
260
261 return kmap && kmap->name[0];
262}
263
a93e0b23
SL
264bool __map__is_bpf_prog(const struct map *map)
265{
266 const char *name;
267
268 if (map->dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
269 return true;
270
271 /*
272 * If PERF_RECORD_BPF_EVENT is not included, the dso will not have
273 * type of DSO_BINARY_TYPE__BPF_PROG_INFO. In such cases, we can
274 * guess the type based on name.
275 */
276 name = map->dso->short_name;
277 return name && (strstr(name, "bpf_prog_") == name);
278}
279
e94b861a
ACM
280bool map__has_symbols(const struct map *map)
281{
3183f8ca 282 return dso__has_symbols(map->dso);
e94b861a
ACM
283}
284
d3a7c489 285static void map__exit(struct map *map)
c338aee8 286{
facf3f06 287 BUG_ON(!RB_EMPTY_NODE(&map->rb_node));
d3a7c489
ACM
288 dso__zput(map->dso);
289}
290
291void map__delete(struct map *map)
292{
293 map__exit(map);
237a7e04 294 free(map);
c338aee8
ACM
295}
296
84c2cafa
ACM
297void map__put(struct map *map)
298{
e3a42cdd 299 if (map && refcount_dec_and_test(&map->refcnt))
84c2cafa
ACM
300 map__delete(map);
301}
302
237a7e04 303void map__fixup_start(struct map *map)
c338aee8 304{
7137ff50
DB
305 struct rb_root_cached *symbols = &map->dso->symbols;
306 struct rb_node *nd = rb_first_cached(symbols);
c338aee8
ACM
307 if (nd != NULL) {
308 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
237a7e04 309 map->start = sym->start;
c338aee8
ACM
310 }
311}
312
237a7e04 313void map__fixup_end(struct map *map)
c338aee8 314{
7137ff50
DB
315 struct rb_root_cached *symbols = &map->dso->symbols;
316 struct rb_node *nd = rb_last(&symbols->rb_root);
c338aee8
ACM
317 if (nd != NULL) {
318 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
237a7e04 319 map->end = sym->end;
c338aee8
ACM
320 }
321}
322
d70a5402
ACM
323#define DSO__DELETED "(deleted)"
324
be39db9f 325int map__load(struct map *map)
66bd8424 326{
237a7e04 327 const char *name = map->dso->long_name;
a128168d 328 int nr;
79406cd7 329
3183f8ca 330 if (dso__loaded(map->dso))
a128168d
MH
331 return 0;
332
be39db9f 333 nr = dso__load(map->dso, map);
79406cd7 334 if (nr < 0) {
237a7e04 335 if (map->dso->has_build_id) {
b5d8bbe8 336 char sbuild_id[SBUILD_ID_SIZE];
79406cd7 337
237a7e04
ACM
338 build_id__sprintf(map->dso->build_id,
339 sizeof(map->dso->build_id),
79406cd7 340 sbuild_id);
d8e75a11 341 pr_debug("%s with build id %s not found", name, sbuild_id);
79406cd7 342 } else
d8e75a11 343 pr_debug("Failed to open %s", name);
79406cd7 344
d8e75a11 345 pr_debug(", continuing without symbols\n");
79406cd7
ACM
346 return -1;
347 } else if (nr == 0) {
89fe808a 348#ifdef HAVE_LIBELF_SUPPORT
79406cd7
ACM
349 const size_t len = strlen(name);
350 const size_t real_len = len - sizeof(DSO__DELETED);
351
352 if (len > sizeof(DSO__DELETED) &&
353 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
d8e75a11 354 pr_debug("%.*s was updated (is prelink enabled?). "
e77b15bd 355 "Restart the long running apps that use it!\n",
79406cd7
ACM
356 (int)real_len, name);
357 } else {
d8e75a11 358 pr_debug("no symbols found in %s, maybe install a debug package?\n", name);
66bd8424 359 }
393be2e3 360#endif
79406cd7 361 return -1;
66bd8424
ACM
362 }
363
79406cd7
ACM
364 return 0;
365}
366
be39db9f 367struct symbol *map__find_symbol(struct map *map, u64 addr)
79406cd7 368{
be39db9f 369 if (map__load(map) < 0)
79406cd7
ACM
370 return NULL;
371
3183f8ca 372 return dso__find_symbol(map->dso, addr);
66bd8424
ACM
373}
374
be39db9f 375struct symbol *map__find_symbol_by_name(struct map *map, const char *name)
79406cd7 376{
be39db9f 377 if (map__load(map) < 0)
79406cd7
ACM
378 return NULL;
379
3183f8ca
ACM
380 if (!dso__sorted_by_name(map->dso))
381 dso__sort_by_name(map->dso);
79406cd7 382
3183f8ca 383 return dso__find_symbol_by_name(map->dso, name);
79406cd7
ACM
384}
385
66671d00 386struct map *map__clone(struct map *from)
66e274f3 387{
66671d00
ACM
388 struct map *map = memdup(from, sizeof(*map));
389
390 if (map != NULL) {
e3a42cdd 391 refcount_set(&map->refcnt, 1);
66671d00
ACM
392 RB_CLEAR_NODE(&map->rb_node);
393 dso__get(map->dso);
394 map->groups = NULL;
395 }
396
397 return map;
66e274f3
FW
398}
399
237a7e04 400size_t map__fprintf(struct map *map, FILE *fp)
66e274f3 401{
9486aa38 402 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
237a7e04 403 map->start, map->end, map->pgoff, map->dso->name);
66e274f3 404}
7a2b6209 405
547a92e0
AN
406size_t map__fprintf_dsoname(struct map *map, FILE *fp)
407{
8f28f19a 408 const char *dsoname = "[unknown]";
547a92e0 409
5eae7d84 410 if (map && map->dso) {
0bc8d205
AN
411 if (symbol_conf.show_kernel_path && map->dso->long_name)
412 dsoname = map->dso->long_name;
5eae7d84 413 else
0bc8d205 414 dsoname = map->dso->name;
8f28f19a 415 }
547a92e0
AN
416
417 return fprintf(fp, "%s", dsoname);
418}
419
e2d88aaa
ACM
420char *map__srcline(struct map *map, u64 addr, struct symbol *sym)
421{
422 if (map == NULL)
423 return SRCLINE_UNKNOWN;
424 return get_srcline(map->dso, map__rip_2objdump(map, addr), sym, true, true, addr);
425}
426
cc8fae1d
AH
427int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
428 FILE *fp)
429{
cc8fae1d
AH
430 int ret = 0;
431
432 if (map && map->dso) {
e2d88aaa 433 char *srcline = map__srcline(map, addr, NULL);
cc8fae1d
AH
434 if (srcline != SRCLINE_UNKNOWN)
435 ret = fprintf(fp, "%s%s", prefix, srcline);
436 free_srcline(srcline);
437 }
438 return ret;
439}
440
dd2e18e9
AK
441int map__fprintf_srccode(struct map *map, u64 addr,
442 FILE *fp,
443 struct srccode_state *state)
444{
445 char *srcfile;
446 int ret = 0;
447 unsigned line;
448 int len;
449 char *srccode;
450
451 if (!map || !map->dso)
452 return 0;
453 srcfile = get_srcline_split(map->dso,
454 map__rip_2objdump(map, addr),
455 &line);
456 if (!srcfile)
457 return 0;
458
459 /* Avoid redundant printing */
460 if (state &&
461 state->srcfile &&
462 !strcmp(state->srcfile, srcfile) &&
463 state->line == line) {
464 free(srcfile);
465 return 0;
466 }
467
468 srccode = find_sourceline(srcfile, line, &len);
469 if (!srccode)
470 goto out_free_line;
471
472 ret = fprintf(fp, "|%-8d %.*s", line, len, srccode);
473 state->srcfile = srcfile;
474 state->line = line;
475 return ret;
476
477out_free_line:
478 free(srcfile);
479 return ret;
480}
481
482
483void srccode_state_free(struct srccode_state *state)
484{
485 zfree(&state->srcfile);
486 state->line = 0;
487}
488
1d5077bd
AH
489/**
490 * map__rip_2objdump - convert symbol start address to objdump address.
491 * @map: memory map
492 * @rip: symbol start address
493 *
7a2b6209 494 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
0131c4ec
AH
495 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
496 * relative to section start.
1d5077bd
AH
497 *
498 * Return: Address suitable for passing to "objdump --start-address="
7a2b6209
KS
499 */
500u64 map__rip_2objdump(struct map *map, u64 rip)
501{
97802f3b
AH
502 struct kmap *kmap = __map__kmap(map);
503
504 /*
505 * vmlinux does not have program headers for PTI entry trampolines and
506 * kcore may not either. However the trampoline object code is on the
507 * main kernel map, so just use that instead.
508 */
509 if (kmap && is_entry_trampoline(kmap->name) && kmap->kmaps && kmap->kmaps->machine) {
510 struct map *kernel_map = machine__kernel_map(kmap->kmaps->machine);
511
512 if (kernel_map)
513 map = kernel_map;
514 }
515
0131c4ec
AH
516 if (!map->dso->adjust_symbols)
517 return rip;
518
519 if (map->dso->rel)
520 return rip - map->pgoff;
521
a58f7033
WN
522 /*
523 * kernel modules also have DSO_TYPE_USER in dso->kernel,
524 * but all kernel modules are ET_REL, so won't get here.
525 */
526 if (map->dso->kernel == DSO_TYPE_USER)
527 return rip + map->dso->text_offset;
528
9176753d 529 return map->unmap_ip(map, rip) - map->reloc;
7a2b6209 530}
ee11b90b 531
1d5077bd
AH
532/**
533 * map__objdump_2mem - convert objdump address to a memory address.
534 * @map: memory map
535 * @ip: objdump address
536 *
537 * Closely related to map__rip_2objdump(), this function takes an address from
538 * objdump and converts it to a memory address. Note this assumes that @map
539 * contains the address. To be sure the result is valid, check it forwards
540 * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
541 *
542 * Return: Memory address.
543 */
544u64 map__objdump_2mem(struct map *map, u64 ip)
545{
546 if (!map->dso->adjust_symbols)
547 return map->unmap_ip(map, ip);
548
549 if (map->dso->rel)
550 return map->unmap_ip(map, ip + map->pgoff);
551
a58f7033
WN
552 /*
553 * kernel modules also have DSO_TYPE_USER in dso->kernel,
554 * but all kernel modules are ET_REL, so won't get here.
555 */
556 if (map->dso->kernel == DSO_TYPE_USER)
557 return map->unmap_ip(map, ip - map->dso->text_offset);
558
9176753d 559 return ip + map->reloc;
1d5077bd
AH
560}
561
1eee78ae
ACM
562static void maps__init(struct maps *maps)
563{
564 maps->entries = RB_ROOT;
1e628569 565 maps->names = RB_ROOT;
0a7c74ea 566 init_rwsem(&maps->lock);
1eee78ae
ACM
567}
568
11246c70 569void map_groups__init(struct map_groups *mg, struct machine *machine)
c6e718ff 570{
3183f8ca 571 maps__init(&mg->maps);
11246c70 572 mg->machine = machine;
ead05e8f 573 refcount_set(&mg->refcnt, 1);
c6e718ff
ACM
574}
575
41f30914
ACM
576void map_groups__insert(struct map_groups *mg, struct map *map)
577{
578 maps__insert(&mg->maps, map);
579 map->groups = mg;
580}
581
6a2ffcdd 582static void __maps__purge(struct maps *maps)
591765fd 583{
1eee78ae
ACM
584 struct rb_root *root = &maps->entries;
585 struct rb_node *next = rb_first(root);
591765fd
ACM
586
587 while (next) {
588 struct map *pos = rb_entry(next, struct map, rb_node);
589
590 next = rb_next(&pos->rb_node);
facf3f06 591 rb_erase_init(&pos->rb_node, root);
84c2cafa 592 map__put(pos);
591765fd
ACM
593 }
594}
595
da3a53a7
CD
596static void __maps__purge_names(struct maps *maps)
597{
598 struct rb_root *root = &maps->names;
599 struct rb_node *next = rb_first(root);
600
601 while (next) {
602 struct map *pos = rb_entry(next, struct map, rb_node_name);
603
604 next = rb_next(&pos->rb_node_name);
605 rb_erase_init(&pos->rb_node_name, root);
606 map__put(pos);
607 }
608}
609
1eee78ae
ACM
610static void maps__exit(struct maps *maps)
611{
0a7c74ea 612 down_write(&maps->lock);
6a2ffcdd 613 __maps__purge(maps);
da3a53a7 614 __maps__purge_names(maps);
0a7c74ea 615 up_write(&maps->lock);
1eee78ae
ACM
616}
617
98dfd55d 618void map_groups__exit(struct map_groups *mg)
591765fd 619{
3183f8ca 620 maps__exit(&mg->maps);
591765fd
ACM
621}
622
29ce3612
AH
623bool map_groups__empty(struct map_groups *mg)
624{
3183f8ca 625 return !maps__first(&mg->maps);
29ce3612
AH
626}
627
11246c70 628struct map_groups *map_groups__new(struct machine *machine)
93d5731d
ACM
629{
630 struct map_groups *mg = malloc(sizeof(*mg));
631
632 if (mg != NULL)
11246c70 633 map_groups__init(mg, machine);
93d5731d
ACM
634
635 return mg;
636}
637
638void map_groups__delete(struct map_groups *mg)
639{
640 map_groups__exit(mg);
641 free(mg);
642}
643
a26ca671
ACM
644void map_groups__put(struct map_groups *mg)
645{
ead05e8f 646 if (mg && refcount_dec_and_test(&mg->refcnt))
a26ca671
ACM
647 map_groups__delete(mg);
648}
649
98dfd55d 650struct symbol *map_groups__find_symbol(struct map_groups *mg,
3183f8ca 651 u64 addr, struct map **mapp)
4b8cf846 652{
3183f8ca 653 struct map *map = map_groups__find(mg, addr);
4b8cf846 654
4afc81cd 655 /* Ensure map is loaded before using map->map_ip */
be39db9f 656 if (map != NULL && map__load(map) >= 0) {
7e5e1b14
ACM
657 if (mapp != NULL)
658 *mapp = map;
be39db9f 659 return map__find_symbol(map, map->map_ip(map, addr));
7e5e1b14
ACM
660 }
661
662 return NULL;
663}
664
03db8b58
AH
665static bool map__contains_symbol(struct map *map, struct symbol *sym)
666{
667 u64 ip = map->unmap_ip(map, sym->start);
668
669 return ip >= map->start && ip < map->end;
670}
671
b7f9ff56 672struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name,
be39db9f 673 struct map **mapp)
7e5e1b14 674{
6a2ffcdd 675 struct symbol *sym;
7e5e1b14
ACM
676 struct rb_node *nd;
677
0a7c74ea 678 down_read(&maps->lock);
6a2ffcdd
ACM
679
680 for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
7e5e1b14 681 struct map *pos = rb_entry(nd, struct map, rb_node);
6a2ffcdd 682
be39db9f 683 sym = map__find_symbol_by_name(pos, name);
7e5e1b14
ACM
684
685 if (sym == NULL)
686 continue;
03db8b58
AH
687 if (!map__contains_symbol(pos, sym)) {
688 sym = NULL;
689 continue;
690 }
7e5e1b14
ACM
691 if (mapp != NULL)
692 *mapp = pos;
6a2ffcdd 693 goto out;
7e5e1b14 694 }
4b8cf846 695
6a2ffcdd
ACM
696 sym = NULL;
697out:
0a7c74ea 698 up_read(&maps->lock);
6a2ffcdd 699 return sym;
4b8cf846
ACM
700}
701
b7f9ff56 702struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
b7f9ff56 703 const char *name,
be39db9f 704 struct map **mapp)
b7f9ff56 705{
3183f8ca 706 return maps__find_symbol_by_name(&mg->maps, name, mapp);
b7f9ff56
ACM
707}
708
be39db9f 709int map_groups__find_ams(struct addr_map_symbol *ams)
4e987712 710{
77faf4d0 711 if (ams->addr < ams->map->start || ams->addr >= ams->map->end) {
4e987712
ACM
712 if (ams->map->groups == NULL)
713 return -1;
3183f8ca 714 ams->map = map_groups__find(ams->map->groups, ams->addr);
4e987712
ACM
715 if (ams->map == NULL)
716 return -1;
717 }
718
719 ams->al_addr = ams->map->map_ip(ams->map, ams->addr);
be39db9f 720 ams->sym = map__find_symbol(ams->map, ams->al_addr);
4e987712
ACM
721
722 return ams->sym ? 0 : -1;
723}
724
6a2ffcdd 725static size_t maps__fprintf(struct maps *maps, FILE *fp)
c6e718ff 726{
6a2ffcdd 727 size_t printed = 0;
c6e718ff
ACM
728 struct rb_node *nd;
729
0a7c74ea 730 down_read(&maps->lock);
6a2ffcdd
ACM
731
732 for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
c6e718ff
ACM
733 struct map *pos = rb_entry(nd, struct map, rb_node);
734 printed += fprintf(fp, "Map:");
735 printed += map__fprintf(pos, fp);
736 if (verbose > 2) {
3183f8ca 737 printed += dso__fprintf(pos->dso, fp);
c6e718ff
ACM
738 printed += fprintf(fp, "--\n");
739 }
740 }
741
0a7c74ea 742 up_read(&maps->lock);
6a2ffcdd 743
c6e718ff
ACM
744 return printed;
745}
746
5c24b67a 747size_t map_groups__fprintf(struct map_groups *mg, FILE *fp)
c6e718ff 748{
3183f8ca 749 return maps__fprintf(&mg->maps, fp);
c6e718ff
ACM
750}
751
cb8382e0
JO
752static void __map_groups__insert(struct map_groups *mg, struct map *map)
753{
3183f8ca 754 __maps__insert(&mg->maps, map);
1e628569 755 __maps__insert_name(&mg->maps, map);
cb8382e0
JO
756 map->groups = mg;
757}
758
6a2ffcdd 759static int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp)
c6e718ff 760{
6a2ffcdd 761 struct rb_root *root;
6a9405b5 762 struct rb_node *next, *first;
0a1eae39 763 int err = 0;
c6e718ff 764
0a7c74ea 765 down_write(&maps->lock);
6a2ffcdd
ACM
766
767 root = &maps->entries;
6a2ffcdd 768
6a9405b5
KK
769 /*
770 * Find first map where end > map->start.
771 * Same as find_vma() in kernel.
772 */
773 next = root->rb_node;
774 first = NULL;
775 while (next) {
776 struct map *pos = rb_entry(next, struct map, rb_node);
777
778 if (pos->end > map->start) {
779 first = next;
780 if (pos->start <= map->start)
781 break;
782 next = next->rb_left;
783 } else
784 next = next->rb_right;
785 }
786
787 next = first;
c6e718ff
ACM
788 while (next) {
789 struct map *pos = rb_entry(next, struct map, rb_node);
790 next = rb_next(&pos->rb_node);
791
6a9405b5
KK
792 /*
793 * Stop if current map starts after map->end.
794 * Maps are ordered by start: next will not overlap for sure.
795 */
796 if (pos->start >= map->end)
797 break;
c6e718ff
ACM
798
799 if (verbose >= 2) {
21e8c810
AB
800
801 if (use_browser) {
d8e75a11 802 pr_debug("overlapping maps in %s (disable tui for more info)\n",
21e8c810
AB
803 map->dso->name);
804 } else {
805 fputs("overlapping maps:\n", fp);
806 map__fprintf(map, fp);
807 map__fprintf(pos, fp);
808 }
c6e718ff
ACM
809 }
810
facf3f06 811 rb_erase_init(&pos->rb_node, root);
c6e718ff
ACM
812 /*
813 * Now check if we need to create new maps for areas not
814 * overlapped by the new map:
815 */
816 if (map->start > pos->start) {
817 struct map *before = map__clone(pos);
818
0a1eae39
ACM
819 if (before == NULL) {
820 err = -ENOMEM;
84c2cafa 821 goto put_map;
0a1eae39 822 }
c6e718ff 823
77faf4d0 824 before->end = map->start;
cb8382e0 825 __map_groups__insert(pos->groups, before);
21e8c810 826 if (verbose >= 2 && !use_browser)
c6e718ff 827 map__fprintf(before, fp);
d91130e9 828 map__put(before);
c6e718ff
ACM
829 }
830
831 if (map->end < pos->end) {
832 struct map *after = map__clone(pos);
833
0a1eae39
ACM
834 if (after == NULL) {
835 err = -ENOMEM;
84c2cafa 836 goto put_map;
0a1eae39 837 }
c6e718ff 838
77faf4d0 839 after->start = map->end;
cb8382e0 840 __map_groups__insert(pos->groups, after);
21e8c810 841 if (verbose >= 2 && !use_browser)
c6e718ff 842 map__fprintf(after, fp);
d91130e9 843 map__put(after);
c6e718ff 844 }
84c2cafa 845put_map:
5c24b67a 846 map__put(pos);
0a1eae39
ACM
847
848 if (err)
6a2ffcdd 849 goto out;
c6e718ff
ACM
850 }
851
6a2ffcdd
ACM
852 err = 0;
853out:
0a7c74ea 854 up_write(&maps->lock);
6a2ffcdd
ACM
855 return err;
856}
857
858int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
859 FILE *fp)
860{
3183f8ca 861 return maps__fixup_overlappings(&mg->maps, map, fp);
c6e718ff
ACM
862}
863
864/*
865 * XXX This should not really _copy_ te maps, but refcount them.
866 */
3183f8ca 867int map_groups__clone(struct thread *thread, struct map_groups *parent)
c6e718ff 868{
6c502584 869 struct map_groups *mg = thread->mg;
6a2ffcdd 870 int err = -ENOMEM;
4bb7123d 871 struct map *map;
3183f8ca 872 struct maps *maps = &parent->maps;
4bb7123d 873
0a7c74ea 874 down_read(&maps->lock);
6a2ffcdd 875
4bb7123d 876 for (map = maps__first(maps); map; map = map__next(map)) {
c6e718ff
ACM
877 struct map *new = map__clone(map);
878 if (new == NULL)
6a2ffcdd 879 goto out_unlock;
6c502584
JO
880
881 err = unwind__prepare_access(thread, new, NULL);
882 if (err)
883 goto out_unlock;
884
98dfd55d 885 map_groups__insert(mg, new);
bae32b50 886 map__put(new);
c6e718ff 887 }
6a2ffcdd
ACM
888
889 err = 0;
890out_unlock:
0a7c74ea 891 up_read(&maps->lock);
6a2ffcdd 892 return err;
c6e718ff
ACM
893}
894
6a2ffcdd 895static void __maps__insert(struct maps *maps, struct map *map)
4b8cf846 896{
1eee78ae 897 struct rb_node **p = &maps->entries.rb_node;
4b8cf846
ACM
898 struct rb_node *parent = NULL;
899 const u64 ip = map->start;
900 struct map *m;
901
902 while (*p != NULL) {
903 parent = *p;
904 m = rb_entry(parent, struct map, rb_node);
905 if (ip < m->start)
906 p = &(*p)->rb_left;
907 else
908 p = &(*p)->rb_right;
909 }
910
911 rb_link_node(&map->rb_node, parent, p);
1eee78ae 912 rb_insert_color(&map->rb_node, &maps->entries);
84c2cafa 913 map__get(map);
4b8cf846
ACM
914}
915
1e628569
ESE
916static void __maps__insert_name(struct maps *maps, struct map *map)
917{
918 struct rb_node **p = &maps->names.rb_node;
919 struct rb_node *parent = NULL;
920 struct map *m;
921 int rc;
922
923 while (*p != NULL) {
924 parent = *p;
925 m = rb_entry(parent, struct map, rb_node_name);
926 rc = strcmp(m->dso->short_name, map->dso->short_name);
927 if (rc < 0)
928 p = &(*p)->rb_left;
929 else if (rc > 0)
930 p = &(*p)->rb_right;
931 else
932 return;
933 }
934 rb_link_node(&map->rb_node_name, parent, p);
935 rb_insert_color(&map->rb_node_name, &maps->names);
936 map__get(map);
937}
938
6a2ffcdd
ACM
939void maps__insert(struct maps *maps, struct map *map)
940{
0a7c74ea 941 down_write(&maps->lock);
6a2ffcdd 942 __maps__insert(maps, map);
1e628569 943 __maps__insert_name(maps, map);
0a7c74ea 944 up_write(&maps->lock);
6a2ffcdd
ACM
945}
946
947static void __maps__remove(struct maps *maps, struct map *map)
076c6e45 948{
facf3f06 949 rb_erase_init(&map->rb_node, &maps->entries);
84c2cafa 950 map__put(map);
b49265e0
CD
951
952 rb_erase_init(&map->rb_node_name, &maps->names);
953 map__put(map);
076c6e45
ACM
954}
955
6a2ffcdd
ACM
956void maps__remove(struct maps *maps, struct map *map)
957{
0a7c74ea 958 down_write(&maps->lock);
6a2ffcdd 959 __maps__remove(maps, map);
0a7c74ea 960 up_write(&maps->lock);
6a2ffcdd
ACM
961}
962
1eee78ae 963struct map *maps__find(struct maps *maps, u64 ip)
4b8cf846 964{
b18e0888 965 struct rb_node *p;
4b8cf846
ACM
966 struct map *m;
967
0a7c74ea 968 down_read(&maps->lock);
6a2ffcdd 969
b18e0888
ESE
970 p = maps->entries.rb_node;
971 while (p != NULL) {
972 m = rb_entry(p, struct map, rb_node);
4b8cf846 973 if (ip < m->start)
b18e0888 974 p = p->rb_left;
4955ea22 975 else if (ip >= m->end)
b18e0888 976 p = p->rb_right;
4b8cf846 977 else
6a2ffcdd 978 goto out;
4b8cf846
ACM
979 }
980
6a2ffcdd
ACM
981 m = NULL;
982out:
0a7c74ea 983 up_read(&maps->lock);
6a2ffcdd 984 return m;
4b8cf846 985}
8e0cf965 986
1eee78ae 987struct map *maps__first(struct maps *maps)
8e0cf965 988{
1eee78ae 989 struct rb_node *first = rb_first(&maps->entries);
8e0cf965
AH
990
991 if (first)
992 return rb_entry(first, struct map, rb_node);
993 return NULL;
994}
995
4d4dee9a 996struct map *map__next(struct map *map)
8e0cf965
AH
997{
998 struct rb_node *next = rb_next(&map->rb_node);
999
1000 if (next)
1001 return rb_entry(next, struct map, rb_node);
1002 return NULL;
1003}
ba92732e 1004
5759a682 1005struct kmap *__map__kmap(struct map *map)
ba92732e 1006{
5759a682 1007 if (!map->dso || !map->dso->kernel)
ba92732e 1008 return NULL;
ba92732e
WN
1009 return (struct kmap *)(map + 1);
1010}
1011
5759a682
AH
1012struct kmap *map__kmap(struct map *map)
1013{
1014 struct kmap *kmap = __map__kmap(map);
1015
1016 if (!kmap)
1017 pr_err("Internal error: map__kmap with a non-kernel map\n");
1018 return kmap;
1019}
1020
ba92732e
WN
1021struct map_groups *map__kmaps(struct map *map)
1022{
1023 struct kmap *kmap = map__kmap(map);
1024
1025 if (!kmap || !kmap->kmaps) {
1026 pr_err("Internal error: map__kmaps with a non-kernel map\n");
1027 return NULL;
1028 }
1029 return kmap->kmaps;
1030}