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