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