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