49e353eaa3378d023b0d88bab0323debd478c20b
[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->maj = d_maj;
166                 map->min = d_min;
167                 map->ino = ino;
168                 map->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         refcount_set(&mg->refcnt, 1);
576 }
577
578 void map_groups__insert(struct map_groups *mg, struct map *map)
579 {
580         maps__insert(&mg->maps, map);
581 }
582
583 static void __maps__purge(struct maps *maps)
584 {
585         struct map *pos, *next;
586
587         maps__for_each_entry_safe(maps, pos, next) {
588                 rb_erase_init(&pos->rb_node,  &maps->entries);
589                 map__put(pos);
590         }
591 }
592
593 static void maps__exit(struct maps *maps)
594 {
595         down_write(&maps->lock);
596         __maps__purge(maps);
597         up_write(&maps->lock);
598 }
599
600 void map_groups__exit(struct map_groups *mg)
601 {
602         maps__exit(&mg->maps);
603 }
604
605 bool map_groups__empty(struct map_groups *mg)
606 {
607         return !maps__first(&mg->maps);
608 }
609
610 struct map_groups *map_groups__new(struct machine *machine)
611 {
612         struct map_groups *mg = zalloc(sizeof(*mg));
613
614         if (mg != NULL)
615                 map_groups__init(mg, machine);
616
617         return mg;
618 }
619
620 void map_groups__delete(struct map_groups *mg)
621 {
622         map_groups__exit(mg);
623         unwind__finish_access(mg);
624         free(mg);
625 }
626
627 void map_groups__put(struct map_groups *mg)
628 {
629         if (mg && refcount_dec_and_test(&mg->refcnt))
630                 map_groups__delete(mg);
631 }
632
633 struct symbol *map_groups__find_symbol(struct map_groups *mg,
634                                        u64 addr, struct map **mapp)
635 {
636         struct map *map = map_groups__find(mg, addr);
637
638         /* Ensure map is loaded before using map->map_ip */
639         if (map != NULL && map__load(map) >= 0) {
640                 if (mapp != NULL)
641                         *mapp = map;
642                 return map__find_symbol(map, map->map_ip(map, addr));
643         }
644
645         return NULL;
646 }
647
648 static bool map__contains_symbol(struct map *map, struct symbol *sym)
649 {
650         u64 ip = map->unmap_ip(map, sym->start);
651
652         return ip >= map->start && ip < map->end;
653 }
654
655 struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name,
656                                          struct map **mapp)
657 {
658         struct symbol *sym;
659         struct map *pos;
660
661         down_read(&maps->lock);
662
663         maps__for_each_entry(maps, pos) {
664                 sym = map__find_symbol_by_name(pos, name);
665
666                 if (sym == NULL)
667                         continue;
668                 if (!map__contains_symbol(pos, sym)) {
669                         sym = NULL;
670                         continue;
671                 }
672                 if (mapp != NULL)
673                         *mapp = pos;
674                 goto out;
675         }
676
677         sym = NULL;
678 out:
679         up_read(&maps->lock);
680         return sym;
681 }
682
683 struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
684                                                const char *name,
685                                                struct map **mapp)
686 {
687         return maps__find_symbol_by_name(&mg->maps, name, mapp);
688 }
689
690 int map_groups__find_ams(struct map_groups *mg, struct addr_map_symbol *ams)
691 {
692         if (ams->addr < ams->ms.map->start || ams->addr >= ams->ms.map->end) {
693                 if (mg == NULL)
694                         return -1;
695                 ams->ms.map = map_groups__find(mg, ams->addr);
696                 if (ams->ms.map == NULL)
697                         return -1;
698         }
699
700         ams->al_addr = ams->ms.map->map_ip(ams->ms.map, ams->addr);
701         ams->ms.sym = map__find_symbol(ams->ms.map, ams->al_addr);
702
703         return ams->ms.sym ? 0 : -1;
704 }
705
706 static size_t maps__fprintf(struct maps *maps, FILE *fp)
707 {
708         size_t printed = 0;
709         struct map *pos;
710
711         down_read(&maps->lock);
712
713         maps__for_each_entry(maps, pos) {
714                 printed += fprintf(fp, "Map:");
715                 printed += map__fprintf(pos, fp);
716                 if (verbose > 2) {
717                         printed += dso__fprintf(pos->dso, fp);
718                         printed += fprintf(fp, "--\n");
719                 }
720         }
721
722         up_read(&maps->lock);
723
724         return printed;
725 }
726
727 size_t map_groups__fprintf(struct map_groups *mg, FILE *fp)
728 {
729         return maps__fprintf(&mg->maps, fp);
730 }
731
732 static void __map_groups__insert(struct map_groups *mg, struct map *map)
733 {
734         __maps__insert(&mg->maps, map);
735 }
736
737 int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map, FILE *fp)
738 {
739         struct maps *maps = &mg->maps;
740         struct rb_root *root;
741         struct rb_node *next, *first;
742         int err = 0;
743
744         down_write(&maps->lock);
745
746         root = &maps->entries;
747
748         /*
749          * Find first map where end > map->start.
750          * Same as find_vma() in kernel.
751          */
752         next = root->rb_node;
753         first = NULL;
754         while (next) {
755                 struct map *pos = rb_entry(next, struct map, rb_node);
756
757                 if (pos->end > map->start) {
758                         first = next;
759                         if (pos->start <= map->start)
760                                 break;
761                         next = next->rb_left;
762                 } else
763                         next = next->rb_right;
764         }
765
766         next = first;
767         while (next) {
768                 struct map *pos = rb_entry(next, struct map, rb_node);
769                 next = rb_next(&pos->rb_node);
770
771                 /*
772                  * Stop if current map starts after map->end.
773                  * Maps are ordered by start: next will not overlap for sure.
774                  */
775                 if (pos->start >= map->end)
776                         break;
777
778                 if (verbose >= 2) {
779
780                         if (use_browser) {
781                                 pr_debug("overlapping maps in %s (disable tui for more info)\n",
782                                            map->dso->name);
783                         } else {
784                                 fputs("overlapping maps:\n", fp);
785                                 map__fprintf(map, fp);
786                                 map__fprintf(pos, fp);
787                         }
788                 }
789
790                 rb_erase_init(&pos->rb_node, root);
791                 /*
792                  * Now check if we need to create new maps for areas not
793                  * overlapped by the new map:
794                  */
795                 if (map->start > pos->start) {
796                         struct map *before = map__clone(pos);
797
798                         if (before == NULL) {
799                                 err = -ENOMEM;
800                                 goto put_map;
801                         }
802
803                         before->end = map->start;
804                         __map_groups__insert(mg, before);
805                         if (verbose >= 2 && !use_browser)
806                                 map__fprintf(before, fp);
807                         map__put(before);
808                 }
809
810                 if (map->end < pos->end) {
811                         struct map *after = map__clone(pos);
812
813                         if (after == NULL) {
814                                 err = -ENOMEM;
815                                 goto put_map;
816                         }
817
818                         after->start = map->end;
819                         after->pgoff += map->end - pos->start;
820                         assert(pos->map_ip(pos, map->end) == after->map_ip(after, map->end));
821                         __map_groups__insert(mg, after);
822                         if (verbose >= 2 && !use_browser)
823                                 map__fprintf(after, fp);
824                         map__put(after);
825                 }
826 put_map:
827                 map__put(pos);
828
829                 if (err)
830                         goto out;
831         }
832
833         err = 0;
834 out:
835         up_write(&maps->lock);
836         return err;
837 }
838
839 /*
840  * XXX This should not really _copy_ te maps, but refcount them.
841  */
842 int map_groups__clone(struct thread *thread, struct map_groups *parent)
843 {
844         struct map_groups *mg = thread->mg;
845         int err = -ENOMEM;
846         struct map *map;
847         struct maps *maps = &parent->maps;
848
849         down_read(&maps->lock);
850
851         maps__for_each_entry(maps, map) {
852                 struct map *new = map__clone(map);
853                 if (new == NULL)
854                         goto out_unlock;
855
856                 err = unwind__prepare_access(mg, new, NULL);
857                 if (err)
858                         goto out_unlock;
859
860                 map_groups__insert(mg, new);
861                 map__put(new);
862         }
863
864         err = 0;
865 out_unlock:
866         up_read(&maps->lock);
867         return err;
868 }
869
870 static void __maps__insert(struct maps *maps, struct map *map)
871 {
872         struct rb_node **p = &maps->entries.rb_node;
873         struct rb_node *parent = NULL;
874         const u64 ip = map->start;
875         struct map *m;
876
877         while (*p != NULL) {
878                 parent = *p;
879                 m = rb_entry(parent, struct map, rb_node);
880                 if (ip < m->start)
881                         p = &(*p)->rb_left;
882                 else
883                         p = &(*p)->rb_right;
884         }
885
886         rb_link_node(&map->rb_node, parent, p);
887         rb_insert_color(&map->rb_node, &maps->entries);
888         map__get(map);
889 }
890
891 void maps__insert(struct maps *maps, struct map *map)
892 {
893         down_write(&maps->lock);
894         __maps__insert(maps, map);
895         up_write(&maps->lock);
896 }
897
898 static void __maps__remove(struct maps *maps, struct map *map)
899 {
900         rb_erase_init(&map->rb_node, &maps->entries);
901         map__put(map);
902 }
903
904 void maps__remove(struct maps *maps, struct map *map)
905 {
906         down_write(&maps->lock);
907         __maps__remove(maps, map);
908         up_write(&maps->lock);
909 }
910
911 struct map *maps__find(struct maps *maps, u64 ip)
912 {
913         struct rb_node *p;
914         struct map *m;
915
916         down_read(&maps->lock);
917
918         p = maps->entries.rb_node;
919         while (p != NULL) {
920                 m = rb_entry(p, struct map, rb_node);
921                 if (ip < m->start)
922                         p = p->rb_left;
923                 else if (ip >= m->end)
924                         p = p->rb_right;
925                 else
926                         goto out;
927         }
928
929         m = NULL;
930 out:
931         up_read(&maps->lock);
932         return m;
933 }
934
935 struct map *maps__first(struct maps *maps)
936 {
937         struct rb_node *first = rb_first(&maps->entries);
938
939         if (first)
940                 return rb_entry(first, struct map, rb_node);
941         return NULL;
942 }
943
944 static struct map *__map__next(struct map *map)
945 {
946         struct rb_node *next = rb_next(&map->rb_node);
947
948         if (next)
949                 return rb_entry(next, struct map, rb_node);
950         return NULL;
951 }
952
953 struct map *map__next(struct map *map)
954 {
955         return map ? __map__next(map) : NULL;
956 }
957
958 struct kmap *__map__kmap(struct map *map)
959 {
960         if (!map->dso || !map->dso->kernel)
961                 return NULL;
962         return (struct kmap *)(map + 1);
963 }
964
965 struct kmap *map__kmap(struct map *map)
966 {
967         struct kmap *kmap = __map__kmap(map);
968
969         if (!kmap)
970                 pr_err("Internal error: map__kmap with a non-kernel map\n");
971         return kmap;
972 }
973
974 struct map_groups *map__kmaps(struct map *map)
975 {
976         struct kmap *kmap = map__kmap(map);
977
978         if (!kmap || !kmap->kmaps) {
979                 pr_err("Internal error: map__kmaps with a non-kernel map\n");
980                 return NULL;
981         }
982         return kmap->kmaps;
983 }