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