]>
Commit | Line | Data |
---|---|---|
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" |
8e16017d | 15 | #include <linux/string.h> |
66e274f3 | 16 | |
3846df2e ACM |
17 | const char *map_type__name[MAP__NR_TYPES] = { |
18 | [MAP__FUNCTION] = "Functions", | |
19 | [MAP__VARIABLE] = "Variables", | |
20 | }; | |
21 | ||
66e274f3 FW |
22 | static inline int is_anon_memory(const char *filename) |
23 | { | |
d0528b5d | 24 | return !strcmp(filename, "//anon") || |
89365e6c | 25 | !strcmp(filename, "/dev/zero (deleted)") || |
d0528b5d | 26 | !strcmp(filename, "/anon_hugepage (deleted)"); |
66e274f3 FW |
27 | } |
28 | ||
87ffef79 JO |
29 | static inline int is_no_dso_memory(const char *filename) |
30 | { | |
1e82574d | 31 | return !strncmp(filename, "[stack", 6) || |
87ffef79 JO |
32 | !strcmp(filename, "[heap]"); |
33 | } | |
34 | ||
237a7e04 | 35 | void map__init(struct map *map, enum map_type type, |
3610583c | 36 | u64 start, u64 end, u64 pgoff, struct dso *dso) |
afb7b4f0 | 37 | { |
237a7e04 ACM |
38 | map->type = type; |
39 | map->start = start; | |
40 | map->end = end; | |
41 | map->pgoff = pgoff; | |
9176753d | 42 | map->reloc = 0; |
237a7e04 ACM |
43 | map->dso = dso; |
44 | map->map_ip = map__map_ip; | |
45 | map->unmap_ip = map__unmap_ip; | |
46 | RB_CLEAR_NODE(&map->rb_node); | |
47 | map->groups = NULL; | |
48 | map->referenced = false; | |
49 | map->erange_warned = false; | |
afb7b4f0 ACM |
50 | } |
51 | ||
a1645ce1 | 52 | struct map *map__new(struct list_head *dsos__list, u64 start, u64 len, |
5c5e854b SE |
53 | u64 pgoff, u32 pid, u32 d_maj, u32 d_min, u64 ino, |
54 | u64 ino_gen, char *filename, | |
361d1346 | 55 | enum map_type type) |
66e274f3 | 56 | { |
237a7e04 | 57 | struct map *map = malloc(sizeof(*map)); |
66e274f3 | 58 | |
237a7e04 | 59 | if (map != NULL) { |
66e274f3 | 60 | char newfilename[PATH_MAX]; |
afb7b4f0 | 61 | struct dso *dso; |
7dbf4dcf | 62 | int anon, no_dso, vdso; |
66e274f3 | 63 | |
66e274f3 | 64 | anon = is_anon_memory(filename); |
7dbf4dcf | 65 | vdso = is_vdso_map(filename); |
87ffef79 | 66 | no_dso = is_no_dso_memory(filename); |
66e274f3 | 67 | |
5c5e854b SE |
68 | map->maj = d_maj; |
69 | map->min = d_min; | |
70 | map->ino = ino; | |
71 | map->ino_generation = ino_gen; | |
72 | ||
578c03c8 | 73 | if ((anon || no_dso) && type == MAP__FUNCTION) { |
b177f63f | 74 | snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid); |
66e274f3 FW |
75 | filename = newfilename; |
76 | } | |
77 | ||
7dbf4dcf JO |
78 | if (vdso) { |
79 | pgoff = 0; | |
80 | dso = vdso__dso_findnew(dsos__list); | |
81 | } else | |
82 | dso = __dsos__findnew(dsos__list, filename); | |
83 | ||
afb7b4f0 | 84 | if (dso == NULL) |
66e274f3 FW |
85 | goto out_delete; |
86 | ||
237a7e04 | 87 | map__init(map, type, start, start + len, pgoff, dso); |
afb7b4f0 | 88 | |
87ffef79 | 89 | if (anon || no_dso) { |
237a7e04 | 90 | map->map_ip = map->unmap_ip = identity__map_ip; |
87ffef79 JO |
91 | |
92 | /* | |
93 | * Set memory without DSO as loaded. All map__find_* | |
94 | * functions still return NULL, and we avoid the | |
95 | * unnecessary map__load warning. | |
96 | */ | |
578c03c8 | 97 | if (type != MAP__FUNCTION) |
237a7e04 | 98 | dso__set_loaded(dso, map->type); |
8d92c02a | 99 | } |
66e274f3 | 100 | } |
237a7e04 | 101 | return map; |
66e274f3 | 102 | out_delete: |
237a7e04 | 103 | free(map); |
66e274f3 FW |
104 | return NULL; |
105 | } | |
106 | ||
e5a1845f NK |
107 | /* |
108 | * Constructor variant for modules (where we know from /proc/modules where | |
109 | * they are loaded) and for vmlinux, where only after we load all the | |
110 | * symbols we'll know where it starts and ends. | |
111 | */ | |
112 | struct map *map__new2(u64 start, struct dso *dso, enum map_type type) | |
113 | { | |
114 | struct map *map = calloc(1, (sizeof(*map) + | |
115 | (dso->kernel ? sizeof(struct kmap) : 0))); | |
116 | if (map != NULL) { | |
117 | /* | |
118 | * ->end will be filled after we load all the symbols | |
119 | */ | |
120 | map__init(map, type, start, 0, 0, dso); | |
121 | } | |
122 | ||
123 | return map; | |
124 | } | |
125 | ||
237a7e04 | 126 | void map__delete(struct map *map) |
c338aee8 | 127 | { |
237a7e04 | 128 | free(map); |
c338aee8 ACM |
129 | } |
130 | ||
237a7e04 | 131 | void map__fixup_start(struct map *map) |
c338aee8 | 132 | { |
237a7e04 | 133 | struct rb_root *symbols = &map->dso->symbols[map->type]; |
fcf1203a | 134 | struct rb_node *nd = rb_first(symbols); |
c338aee8 ACM |
135 | if (nd != NULL) { |
136 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); | |
237a7e04 | 137 | map->start = sym->start; |
c338aee8 ACM |
138 | } |
139 | } | |
140 | ||
237a7e04 | 141 | void map__fixup_end(struct map *map) |
c338aee8 | 142 | { |
237a7e04 | 143 | struct rb_root *symbols = &map->dso->symbols[map->type]; |
fcf1203a | 144 | struct rb_node *nd = rb_last(symbols); |
c338aee8 ACM |
145 | if (nd != NULL) { |
146 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); | |
237a7e04 | 147 | map->end = sym->end; |
c338aee8 ACM |
148 | } |
149 | } | |
150 | ||
d70a5402 ACM |
151 | #define DSO__DELETED "(deleted)" |
152 | ||
237a7e04 | 153 | int map__load(struct map *map, symbol_filter_t filter) |
66bd8424 | 154 | { |
237a7e04 | 155 | const char *name = map->dso->long_name; |
a128168d | 156 | int nr; |
79406cd7 | 157 | |
237a7e04 | 158 | if (dso__loaded(map->dso, map->type)) |
a128168d MH |
159 | return 0; |
160 | ||
237a7e04 | 161 | nr = dso__load(map->dso, map, filter); |
79406cd7 | 162 | if (nr < 0) { |
237a7e04 | 163 | if (map->dso->has_build_id) { |
79406cd7 ACM |
164 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
165 | ||
237a7e04 ACM |
166 | build_id__sprintf(map->dso->build_id, |
167 | sizeof(map->dso->build_id), | |
79406cd7 ACM |
168 | sbuild_id); |
169 | pr_warning("%s with build id %s not found", | |
170 | name, sbuild_id); | |
171 | } else | |
172 | pr_warning("Failed to open %s", name); | |
173 | ||
174 | pr_warning(", continuing without symbols\n"); | |
175 | return -1; | |
176 | } else if (nr == 0) { | |
89fe808a | 177 | #ifdef HAVE_LIBELF_SUPPORT |
79406cd7 ACM |
178 | const size_t len = strlen(name); |
179 | const size_t real_len = len - sizeof(DSO__DELETED); | |
180 | ||
181 | if (len > sizeof(DSO__DELETED) && | |
182 | strcmp(name + real_len + 1, DSO__DELETED) == 0) { | |
e77b15bd DA |
183 | pr_warning("%.*s was updated (is prelink enabled?). " |
184 | "Restart the long running apps that use it!\n", | |
79406cd7 ACM |
185 | (int)real_len, name); |
186 | } else { | |
187 | pr_warning("no symbols found in %s, maybe install " | |
188 | "a debug package?\n", name); | |
66bd8424 | 189 | } |
393be2e3 | 190 | #endif |
79406cd7 | 191 | return -1; |
66bd8424 ACM |
192 | } |
193 | ||
79406cd7 ACM |
194 | return 0; |
195 | } | |
196 | ||
237a7e04 | 197 | struct symbol *map__find_symbol(struct map *map, u64 addr, |
9de89fe7 | 198 | symbol_filter_t filter) |
79406cd7 | 199 | { |
237a7e04 | 200 | if (map__load(map, filter) < 0) |
79406cd7 ACM |
201 | return NULL; |
202 | ||
237a7e04 | 203 | return dso__find_symbol(map->dso, map->type, addr); |
66bd8424 ACM |
204 | } |
205 | ||
237a7e04 | 206 | struct symbol *map__find_symbol_by_name(struct map *map, const char *name, |
79406cd7 ACM |
207 | symbol_filter_t filter) |
208 | { | |
237a7e04 | 209 | if (map__load(map, filter) < 0) |
79406cd7 ACM |
210 | return NULL; |
211 | ||
237a7e04 ACM |
212 | if (!dso__sorted_by_name(map->dso, map->type)) |
213 | dso__sort_by_name(map->dso, map->type); | |
79406cd7 | 214 | |
237a7e04 | 215 | return dso__find_symbol_by_name(map->dso, map->type, name); |
79406cd7 ACM |
216 | } |
217 | ||
237a7e04 | 218 | struct map *map__clone(struct map *map) |
66e274f3 | 219 | { |
8e16017d | 220 | return memdup(map, sizeof(*map)); |
66e274f3 FW |
221 | } |
222 | ||
223 | int map__overlap(struct map *l, struct map *r) | |
224 | { | |
225 | if (l->start > r->start) { | |
226 | struct map *t = l; | |
227 | l = r; | |
228 | r = t; | |
229 | } | |
230 | ||
231 | if (l->end > r->start) | |
232 | return 1; | |
233 | ||
234 | return 0; | |
235 | } | |
236 | ||
237a7e04 | 237 | size_t map__fprintf(struct map *map, FILE *fp) |
66e274f3 | 238 | { |
9486aa38 | 239 | return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n", |
237a7e04 | 240 | map->start, map->end, map->pgoff, map->dso->name); |
66e274f3 | 241 | } |
7a2b6209 | 242 | |
547a92e0 AN |
243 | size_t map__fprintf_dsoname(struct map *map, FILE *fp) |
244 | { | |
8f28f19a | 245 | const char *dsoname = "[unknown]"; |
547a92e0 | 246 | |
0bc8d205 AN |
247 | if (map && map->dso && (map->dso->name || map->dso->long_name)) { |
248 | if (symbol_conf.show_kernel_path && map->dso->long_name) | |
249 | dsoname = map->dso->long_name; | |
250 | else if (map->dso->name) | |
251 | dsoname = map->dso->name; | |
8f28f19a | 252 | } |
547a92e0 AN |
253 | |
254 | return fprintf(fp, "%s", dsoname); | |
255 | } | |
256 | ||
cc8fae1d AH |
257 | int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix, |
258 | FILE *fp) | |
259 | { | |
260 | char *srcline; | |
261 | int ret = 0; | |
262 | ||
263 | if (map && map->dso) { | |
264 | srcline = get_srcline(map->dso, | |
265 | map__rip_2objdump(map, addr)); | |
266 | if (srcline != SRCLINE_UNKNOWN) | |
267 | ret = fprintf(fp, "%s%s", prefix, srcline); | |
268 | free_srcline(srcline); | |
269 | } | |
270 | return ret; | |
271 | } | |
272 | ||
1d5077bd AH |
273 | /** |
274 | * map__rip_2objdump - convert symbol start address to objdump address. | |
275 | * @map: memory map | |
276 | * @rip: symbol start address | |
277 | * | |
7a2b6209 | 278 | * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN. |
0131c4ec AH |
279 | * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is |
280 | * relative to section start. | |
1d5077bd AH |
281 | * |
282 | * Return: Address suitable for passing to "objdump --start-address=" | |
7a2b6209 KS |
283 | */ |
284 | u64 map__rip_2objdump(struct map *map, u64 rip) | |
285 | { | |
0131c4ec AH |
286 | if (!map->dso->adjust_symbols) |
287 | return rip; | |
288 | ||
289 | if (map->dso->rel) | |
290 | return rip - map->pgoff; | |
291 | ||
9176753d | 292 | return map->unmap_ip(map, rip) - map->reloc; |
7a2b6209 | 293 | } |
ee11b90b | 294 | |
1d5077bd AH |
295 | /** |
296 | * map__objdump_2mem - convert objdump address to a memory address. | |
297 | * @map: memory map | |
298 | * @ip: objdump address | |
299 | * | |
300 | * Closely related to map__rip_2objdump(), this function takes an address from | |
301 | * objdump and converts it to a memory address. Note this assumes that @map | |
302 | * contains the address. To be sure the result is valid, check it forwards | |
303 | * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip | |
304 | * | |
305 | * Return: Memory address. | |
306 | */ | |
307 | u64 map__objdump_2mem(struct map *map, u64 ip) | |
308 | { | |
309 | if (!map->dso->adjust_symbols) | |
310 | return map->unmap_ip(map, ip); | |
311 | ||
312 | if (map->dso->rel) | |
313 | return map->unmap_ip(map, ip + map->pgoff); | |
314 | ||
9176753d | 315 | return ip + map->reloc; |
1d5077bd AH |
316 | } |
317 | ||
98dfd55d | 318 | void map_groups__init(struct map_groups *mg) |
c6e718ff ACM |
319 | { |
320 | int i; | |
321 | for (i = 0; i < MAP__NR_TYPES; ++i) { | |
98dfd55d ACM |
322 | mg->maps[i] = RB_ROOT; |
323 | INIT_LIST_HEAD(&mg->removed_maps[i]); | |
c6e718ff | 324 | } |
98dfd55d | 325 | mg->machine = NULL; |
c6e718ff ACM |
326 | } |
327 | ||
98dfd55d | 328 | static void maps__delete(struct rb_root *maps) |
591765fd | 329 | { |
98dfd55d | 330 | struct rb_node *next = rb_first(maps); |
591765fd ACM |
331 | |
332 | while (next) { | |
333 | struct map *pos = rb_entry(next, struct map, rb_node); | |
334 | ||
335 | next = rb_next(&pos->rb_node); | |
98dfd55d | 336 | rb_erase(&pos->rb_node, maps); |
591765fd ACM |
337 | map__delete(pos); |
338 | } | |
339 | } | |
340 | ||
98dfd55d | 341 | static void maps__delete_removed(struct list_head *maps) |
591765fd ACM |
342 | { |
343 | struct map *pos, *n; | |
344 | ||
98dfd55d | 345 | list_for_each_entry_safe(pos, n, maps, node) { |
591765fd ACM |
346 | list_del(&pos->node); |
347 | map__delete(pos); | |
348 | } | |
349 | } | |
350 | ||
98dfd55d | 351 | void map_groups__exit(struct map_groups *mg) |
591765fd ACM |
352 | { |
353 | int i; | |
354 | ||
355 | for (i = 0; i < MAP__NR_TYPES; ++i) { | |
98dfd55d ACM |
356 | maps__delete(&mg->maps[i]); |
357 | maps__delete_removed(&mg->removed_maps[i]); | |
591765fd ACM |
358 | } |
359 | } | |
360 | ||
98dfd55d | 361 | void map_groups__flush(struct map_groups *mg) |
c6e718ff ACM |
362 | { |
363 | int type; | |
364 | ||
365 | for (type = 0; type < MAP__NR_TYPES; type++) { | |
98dfd55d | 366 | struct rb_root *root = &mg->maps[type]; |
c6e718ff ACM |
367 | struct rb_node *next = rb_first(root); |
368 | ||
369 | while (next) { | |
370 | struct map *pos = rb_entry(next, struct map, rb_node); | |
371 | next = rb_next(&pos->rb_node); | |
372 | rb_erase(&pos->rb_node, root); | |
373 | /* | |
374 | * We may have references to this map, for | |
375 | * instance in some hist_entry instances, so | |
376 | * just move them to a separate list. | |
377 | */ | |
98dfd55d | 378 | list_add_tail(&pos->node, &mg->removed_maps[pos->type]); |
c6e718ff ACM |
379 | } |
380 | } | |
381 | } | |
382 | ||
98dfd55d | 383 | struct symbol *map_groups__find_symbol(struct map_groups *mg, |
4b8cf846 | 384 | enum map_type type, u64 addr, |
7e5e1b14 | 385 | struct map **mapp, |
4b8cf846 ACM |
386 | symbol_filter_t filter) |
387 | { | |
98dfd55d | 388 | struct map *map = map_groups__find(mg, type, addr); |
4b8cf846 | 389 | |
4afc81cd MH |
390 | /* Ensure map is loaded before using map->map_ip */ |
391 | if (map != NULL && map__load(map, filter) >= 0) { | |
7e5e1b14 ACM |
392 | if (mapp != NULL) |
393 | *mapp = map; | |
4b8cf846 | 394 | return map__find_symbol(map, map->map_ip(map, addr), filter); |
7e5e1b14 ACM |
395 | } |
396 | ||
397 | return NULL; | |
398 | } | |
399 | ||
98dfd55d | 400 | struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg, |
7e5e1b14 ACM |
401 | enum map_type type, |
402 | const char *name, | |
403 | struct map **mapp, | |
404 | symbol_filter_t filter) | |
405 | { | |
406 | struct rb_node *nd; | |
407 | ||
98dfd55d | 408 | for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) { |
7e5e1b14 ACM |
409 | struct map *pos = rb_entry(nd, struct map, rb_node); |
410 | struct symbol *sym = map__find_symbol_by_name(pos, name, filter); | |
411 | ||
412 | if (sym == NULL) | |
413 | continue; | |
414 | if (mapp != NULL) | |
415 | *mapp = pos; | |
416 | return sym; | |
417 | } | |
4b8cf846 ACM |
418 | |
419 | return NULL; | |
420 | } | |
421 | ||
4e987712 ACM |
422 | int map_groups__find_ams(struct addr_map_symbol *ams, symbol_filter_t filter) |
423 | { | |
424 | if (ams->addr < ams->map->start || ams->addr > ams->map->end) { | |
425 | if (ams->map->groups == NULL) | |
426 | return -1; | |
427 | ams->map = map_groups__find(ams->map->groups, ams->map->type, | |
428 | ams->addr); | |
429 | if (ams->map == NULL) | |
430 | return -1; | |
431 | } | |
432 | ||
433 | ams->al_addr = ams->map->map_ip(ams->map, ams->addr); | |
434 | ams->sym = map__find_symbol(ams->map, ams->al_addr, filter); | |
435 | ||
436 | return ams->sym ? 0 : -1; | |
437 | } | |
438 | ||
98dfd55d | 439 | size_t __map_groups__fprintf_maps(struct map_groups *mg, |
c6e718ff ACM |
440 | enum map_type type, int verbose, FILE *fp) |
441 | { | |
442 | size_t printed = fprintf(fp, "%s:\n", map_type__name[type]); | |
443 | struct rb_node *nd; | |
444 | ||
98dfd55d | 445 | for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) { |
c6e718ff ACM |
446 | struct map *pos = rb_entry(nd, struct map, rb_node); |
447 | printed += fprintf(fp, "Map:"); | |
448 | printed += map__fprintf(pos, fp); | |
449 | if (verbose > 2) { | |
450 | printed += dso__fprintf(pos->dso, type, fp); | |
451 | printed += fprintf(fp, "--\n"); | |
452 | } | |
453 | } | |
454 | ||
455 | return printed; | |
456 | } | |
457 | ||
98dfd55d | 458 | size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp) |
c6e718ff ACM |
459 | { |
460 | size_t printed = 0, i; | |
461 | for (i = 0; i < MAP__NR_TYPES; ++i) | |
98dfd55d | 462 | printed += __map_groups__fprintf_maps(mg, i, verbose, fp); |
c6e718ff ACM |
463 | return printed; |
464 | } | |
465 | ||
98dfd55d | 466 | static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg, |
c6e718ff ACM |
467 | enum map_type type, |
468 | int verbose, FILE *fp) | |
469 | { | |
470 | struct map *pos; | |
471 | size_t printed = 0; | |
472 | ||
98dfd55d | 473 | list_for_each_entry(pos, &mg->removed_maps[type], node) { |
c6e718ff ACM |
474 | printed += fprintf(fp, "Map:"); |
475 | printed += map__fprintf(pos, fp); | |
476 | if (verbose > 1) { | |
477 | printed += dso__fprintf(pos->dso, type, fp); | |
478 | printed += fprintf(fp, "--\n"); | |
479 | } | |
480 | } | |
481 | return printed; | |
482 | } | |
483 | ||
98dfd55d | 484 | static size_t map_groups__fprintf_removed_maps(struct map_groups *mg, |
c6e718ff ACM |
485 | int verbose, FILE *fp) |
486 | { | |
487 | size_t printed = 0, i; | |
488 | for (i = 0; i < MAP__NR_TYPES; ++i) | |
98dfd55d | 489 | printed += __map_groups__fprintf_removed_maps(mg, i, verbose, fp); |
c6e718ff ACM |
490 | return printed; |
491 | } | |
492 | ||
98dfd55d | 493 | size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp) |
c6e718ff | 494 | { |
98dfd55d | 495 | size_t printed = map_groups__fprintf_maps(mg, verbose, fp); |
c6e718ff | 496 | printed += fprintf(fp, "Removed maps:\n"); |
98dfd55d | 497 | return printed + map_groups__fprintf_removed_maps(mg, verbose, fp); |
c6e718ff ACM |
498 | } |
499 | ||
98dfd55d | 500 | int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map, |
c6e718ff ACM |
501 | int verbose, FILE *fp) |
502 | { | |
98dfd55d | 503 | struct rb_root *root = &mg->maps[map->type]; |
c6e718ff | 504 | struct rb_node *next = rb_first(root); |
0a1eae39 | 505 | int err = 0; |
c6e718ff ACM |
506 | |
507 | while (next) { | |
508 | struct map *pos = rb_entry(next, struct map, rb_node); | |
509 | next = rb_next(&pos->rb_node); | |
510 | ||
511 | if (!map__overlap(pos, map)) | |
512 | continue; | |
513 | ||
514 | if (verbose >= 2) { | |
515 | fputs("overlapping maps:\n", fp); | |
516 | map__fprintf(map, fp); | |
517 | map__fprintf(pos, fp); | |
518 | } | |
519 | ||
520 | rb_erase(&pos->rb_node, root); | |
c6e718ff ACM |
521 | /* |
522 | * Now check if we need to create new maps for areas not | |
523 | * overlapped by the new map: | |
524 | */ | |
525 | if (map->start > pos->start) { | |
526 | struct map *before = map__clone(pos); | |
527 | ||
0a1eae39 ACM |
528 | if (before == NULL) { |
529 | err = -ENOMEM; | |
530 | goto move_map; | |
531 | } | |
c6e718ff ACM |
532 | |
533 | before->end = map->start - 1; | |
98dfd55d | 534 | map_groups__insert(mg, before); |
c6e718ff ACM |
535 | if (verbose >= 2) |
536 | map__fprintf(before, fp); | |
537 | } | |
538 | ||
539 | if (map->end < pos->end) { | |
540 | struct map *after = map__clone(pos); | |
541 | ||
0a1eae39 ACM |
542 | if (after == NULL) { |
543 | err = -ENOMEM; | |
544 | goto move_map; | |
545 | } | |
c6e718ff ACM |
546 | |
547 | after->start = map->end + 1; | |
98dfd55d | 548 | map_groups__insert(mg, after); |
c6e718ff ACM |
549 | if (verbose >= 2) |
550 | map__fprintf(after, fp); | |
551 | } | |
0a1eae39 ACM |
552 | move_map: |
553 | /* | |
554 | * If we have references, just move them to a separate list. | |
555 | */ | |
556 | if (pos->referenced) | |
98dfd55d | 557 | list_add_tail(&pos->node, &mg->removed_maps[map->type]); |
0a1eae39 ACM |
558 | else |
559 | map__delete(pos); | |
560 | ||
561 | if (err) | |
562 | return err; | |
c6e718ff ACM |
563 | } |
564 | ||
565 | return 0; | |
566 | } | |
567 | ||
568 | /* | |
569 | * XXX This should not really _copy_ te maps, but refcount them. | |
570 | */ | |
98dfd55d | 571 | int map_groups__clone(struct map_groups *mg, |
c6e718ff ACM |
572 | struct map_groups *parent, enum map_type type) |
573 | { | |
574 | struct rb_node *nd; | |
575 | for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) { | |
576 | struct map *map = rb_entry(nd, struct map, rb_node); | |
577 | struct map *new = map__clone(map); | |
578 | if (new == NULL) | |
579 | return -ENOMEM; | |
98dfd55d | 580 | map_groups__insert(mg, new); |
c6e718ff ACM |
581 | } |
582 | return 0; | |
583 | } | |
584 | ||
4b8cf846 ACM |
585 | void maps__insert(struct rb_root *maps, struct map *map) |
586 | { | |
587 | struct rb_node **p = &maps->rb_node; | |
588 | struct rb_node *parent = NULL; | |
589 | const u64 ip = map->start; | |
590 | struct map *m; | |
591 | ||
592 | while (*p != NULL) { | |
593 | parent = *p; | |
594 | m = rb_entry(parent, struct map, rb_node); | |
595 | if (ip < m->start) | |
596 | p = &(*p)->rb_left; | |
597 | else | |
598 | p = &(*p)->rb_right; | |
599 | } | |
600 | ||
601 | rb_link_node(&map->rb_node, parent, p); | |
602 | rb_insert_color(&map->rb_node, maps); | |
603 | } | |
604 | ||
237a7e04 | 605 | void maps__remove(struct rb_root *maps, struct map *map) |
076c6e45 | 606 | { |
237a7e04 | 607 | rb_erase(&map->rb_node, maps); |
076c6e45 ACM |
608 | } |
609 | ||
4b8cf846 ACM |
610 | struct map *maps__find(struct rb_root *maps, u64 ip) |
611 | { | |
612 | struct rb_node **p = &maps->rb_node; | |
613 | struct rb_node *parent = NULL; | |
614 | struct map *m; | |
615 | ||
616 | while (*p != NULL) { | |
617 | parent = *p; | |
618 | m = rb_entry(parent, struct map, rb_node); | |
619 | if (ip < m->start) | |
620 | p = &(*p)->rb_left; | |
621 | else if (ip > m->end) | |
622 | p = &(*p)->rb_right; | |
623 | else | |
624 | return m; | |
625 | } | |
626 | ||
627 | return NULL; | |
628 | } | |
8e0cf965 AH |
629 | |
630 | struct map *maps__first(struct rb_root *maps) | |
631 | { | |
632 | struct rb_node *first = rb_first(maps); | |
633 | ||
634 | if (first) | |
635 | return rb_entry(first, struct map, rb_node); | |
636 | return NULL; | |
637 | } | |
638 | ||
639 | struct map *maps__next(struct map *map) | |
640 | { | |
641 | struct rb_node *next = rb_next(&map->rb_node); | |
642 | ||
643 | if (next) | |
644 | return rb_entry(next, struct map, rb_node); | |
645 | return NULL; | |
646 | } |