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