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