blob: ef5bc913ca7a9fdda7946401e55f9b0295dddfb6 [file] [log] [blame]
Frederic Weisbecker66e274f2009-08-12 11:07:25 +02001#include "symbol.h"
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -03002#include <errno.h>
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02003#include <inttypes.h>
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -03004#include <limits.h>
Frederic Weisbecker66e274f2009-08-12 11:07:25 +02005#include <stdlib.h>
6#include <string.h>
7#include <stdio.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08008#include <unistd.h>
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -03009#include "map.h"
David Ahern5cd95c22012-07-20 17:25:47 -060010#include "thread.h"
David Ahernc80c3c22012-07-20 17:25:51 -060011#include "strlist.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020012#include "vdso.h"
Jiri Olsaebb296c2012-10-27 23:18:28 +020013#include "build-id.h"
Arnaldo Carvalho de Melo8e16017d2013-01-24 22:16:43 -030014#include <linux/string.h>
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020015
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -030016const char *map_type__name[MAP__NR_TYPES] = {
17 [MAP__FUNCTION] = "Functions",
18 [MAP__VARIABLE] = "Variables",
19};
20
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020021static inline int is_anon_memory(const char *filename)
22{
Joshua Zhud0528b52013-01-05 13:29:57 +080023 return !strcmp(filename, "//anon") ||
Andi Kleen89365e62013-04-24 17:03:02 -070024 !strcmp(filename, "/dev/zero (deleted)") ||
Joshua Zhud0528b52013-01-05 13:29:57 +080025 !strcmp(filename, "/anon_hugepage (deleted)");
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020026}
27
Jiri Olsa87ffef72011-08-24 15:18:34 +020028static inline int is_no_dso_memory(const char *filename)
29{
Namhyung Kim1e825742012-11-07 16:27:11 +090030 return !strncmp(filename, "[stack", 6) ||
Jiri Olsa87ffef72011-08-24 15:18:34 +020031 !strcmp(filename, "[heap]");
32}
33
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030034void map__init(struct map *map, enum map_type type,
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020035 u64 start, u64 end, u64 pgoff, struct dso *dso)
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020036{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030037 map->type = type;
38 map->start = start;
39 map->end = end;
40 map->pgoff = pgoff;
41 map->dso = dso;
42 map->map_ip = map__map_ip;
43 map->unmap_ip = map__unmap_ip;
44 RB_CLEAR_NODE(&map->rb_node);
45 map->groups = NULL;
46 map->referenced = false;
47 map->erange_warned = false;
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020048}
49
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080050struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
Stephane Eranian5c5e8542013-08-21 12:10:25 +020051 u64 pgoff, u32 pid, u32 d_maj, u32 d_min, u64 ino,
52 u64 ino_gen, char *filename,
Dave Martin361d1342010-07-27 16:40:02 +010053 enum map_type type)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020054{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030055 struct map *map = malloc(sizeof(*map));
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020056
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030057 if (map != NULL) {
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020058 char newfilename[PATH_MAX];
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020059 struct dso *dso;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020060 int anon, no_dso, vdso;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020061
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020062 anon = is_anon_memory(filename);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020063 vdso = is_vdso_map(filename);
Jiri Olsa87ffef72011-08-24 15:18:34 +020064 no_dso = is_no_dso_memory(filename);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020065
Stephane Eranian5c5e8542013-08-21 12:10:25 +020066 map->maj = d_maj;
67 map->min = d_min;
68 map->ino = ino;
69 map->ino_generation = ino_gen;
70
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020071 if (anon) {
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -030072 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020073 filename = newfilename;
74 }
75
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020076 if (vdso) {
77 pgoff = 0;
78 dso = vdso__dso_findnew(dsos__list);
79 } else
80 dso = __dsos__findnew(dsos__list, filename);
81
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020082 if (dso == NULL)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020083 goto out_delete;
84
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030085 map__init(map, type, start, start + len, pgoff, dso);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020086
Jiri Olsa87ffef72011-08-24 15:18:34 +020087 if (anon || no_dso) {
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030088 map->map_ip = map->unmap_ip = identity__map_ip;
Jiri Olsa87ffef72011-08-24 15:18:34 +020089
90 /*
91 * Set memory without DSO as loaded. All map__find_*
92 * functions still return NULL, and we avoid the
93 * unnecessary map__load warning.
94 */
95 if (no_dso)
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030096 dso__set_loaded(dso, map->type);
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -020097 }
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020098 }
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030099 return map;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200100out_delete:
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300101 free(map);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200102 return NULL;
103}
104
Namhyung Kime5a18452012-08-06 13:41:20 +0900105/*
106 * Constructor variant for modules (where we know from /proc/modules where
107 * they are loaded) and for vmlinux, where only after we load all the
108 * symbols we'll know where it starts and ends.
109 */
110struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
111{
112 struct map *map = calloc(1, (sizeof(*map) +
113 (dso->kernel ? sizeof(struct kmap) : 0)));
114 if (map != NULL) {
115 /*
116 * ->end will be filled after we load all the symbols
117 */
118 map__init(map, type, start, 0, 0, dso);
119 }
120
121 return map;
122}
123
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300124void map__delete(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200125{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300126 free(map);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200127}
128
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300129void map__fixup_start(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200130{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300131 struct rb_root *symbols = &map->dso->symbols[map->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200132 struct rb_node *nd = rb_first(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200133 if (nd != NULL) {
134 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300135 map->start = sym->start;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200136 }
137}
138
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300139void map__fixup_end(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200140{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300141 struct rb_root *symbols = &map->dso->symbols[map->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200142 struct rb_node *nd = rb_last(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200143 if (nd != NULL) {
144 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300145 map->end = sym->end;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200146 }
147}
148
Arnaldo Carvalho de Melod70a5402009-10-30 16:28:25 -0200149#define DSO__DELETED "(deleted)"
150
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300151int map__load(struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200152{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300153 const char *name = map->dso->long_name;
Masami Hiramatsua1281682009-12-15 10:32:33 -0500154 int nr;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200155
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300156 if (dso__loaded(map->dso, map->type))
Masami Hiramatsua1281682009-12-15 10:32:33 -0500157 return 0;
158
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300159 nr = dso__load(map->dso, map, filter);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200160 if (nr < 0) {
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300161 if (map->dso->has_build_id) {
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200162 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
163
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300164 build_id__sprintf(map->dso->build_id,
165 sizeof(map->dso->build_id),
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200166 sbuild_id);
167 pr_warning("%s with build id %s not found",
168 name, sbuild_id);
169 } else
170 pr_warning("Failed to open %s", name);
171
172 pr_warning(", continuing without symbols\n");
173 return -1;
174 } else if (nr == 0) {
Ingo Molnar89fe8082013-09-30 12:07:11 +0200175#ifdef HAVE_LIBELF_SUPPORT
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200176 const size_t len = strlen(name);
177 const size_t real_len = len - sizeof(DSO__DELETED);
178
179 if (len > sizeof(DSO__DELETED) &&
180 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
David Aherne77b15b2011-10-18 18:44:45 -0600181 pr_warning("%.*s was updated (is prelink enabled?). "
182 "Restart the long running apps that use it!\n",
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200183 (int)real_len, name);
184 } else {
185 pr_warning("no symbols found in %s, maybe install "
186 "a debug package?\n", name);
187 }
Namhyung Kim393be2e2012-08-06 13:41:21 +0900188#endif
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200189 return -1;
190 }
191
192 return 0;
193}
194
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300195struct symbol *map__find_symbol(struct map *map, u64 addr,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200196 symbol_filter_t filter)
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200197{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300198 if (map__load(map, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200199 return NULL;
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200200
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300201 return dso__find_symbol(map->dso, map->type, addr);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200202}
203
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300204struct symbol *map__find_symbol_by_name(struct map *map, const char *name,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200205 symbol_filter_t filter)
206{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300207 if (map__load(map, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200208 return NULL;
209
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300210 if (!dso__sorted_by_name(map->dso, map->type))
211 dso__sort_by_name(map->dso, map->type);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200212
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300213 return dso__find_symbol_by_name(map->dso, map->type, name);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200214}
215
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300216struct map *map__clone(struct map *map)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200217{
Arnaldo Carvalho de Melo8e16017d2013-01-24 22:16:43 -0300218 return memdup(map, sizeof(*map));
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200219}
220
221int map__overlap(struct map *l, struct map *r)
222{
223 if (l->start > r->start) {
224 struct map *t = l;
225 l = r;
226 r = t;
227 }
228
229 if (l->end > r->start)
230 return 1;
231
232 return 0;
233}
234
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300235size_t map__fprintf(struct map *map, FILE *fp)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200236{
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200237 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300238 map->start, map->end, map->pgoff, map->dso->name);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200239}
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200240
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900241size_t map__fprintf_dsoname(struct map *map, FILE *fp)
242{
Feng Tang8f28f192012-08-27 15:38:26 +0800243 const char *dsoname = "[unknown]";
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900244
Akihiro Nagai0bc8d202012-01-30 13:43:20 +0900245 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
246 if (symbol_conf.show_kernel_path && map->dso->long_name)
247 dsoname = map->dso->long_name;
248 else if (map->dso->name)
249 dsoname = map->dso->name;
Feng Tang8f28f192012-08-27 15:38:26 +0800250 }
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900251
252 return fprintf(fp, "%s", dsoname);
253}
254
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300255/**
256 * map__rip_2objdump - convert symbol start address to objdump address.
257 * @map: memory map
258 * @rip: symbol start address
259 *
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200260 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300261 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
262 * relative to section start.
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300263 *
264 * Return: Address suitable for passing to "objdump --start-address="
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200265 */
266u64 map__rip_2objdump(struct map *map, u64 rip)
267{
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300268 if (!map->dso->adjust_symbols)
269 return rip;
270
271 if (map->dso->rel)
272 return rip - map->pgoff;
273
274 return map->unmap_ip(map, rip);
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200275}
Kirill Smelkovee11b902010-02-07 11:46:15 -0200276
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300277/**
278 * map__objdump_2mem - convert objdump address to a memory address.
279 * @map: memory map
280 * @ip: objdump address
281 *
282 * Closely related to map__rip_2objdump(), this function takes an address from
283 * objdump and converts it to a memory address. Note this assumes that @map
284 * contains the address. To be sure the result is valid, check it forwards
285 * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
286 *
287 * Return: Memory address.
288 */
289u64 map__objdump_2mem(struct map *map, u64 ip)
290{
291 if (!map->dso->adjust_symbols)
292 return map->unmap_ip(map, ip);
293
294 if (map->dso->rel)
295 return map->unmap_ip(map, ip + map->pgoff);
296
297 return ip;
298}
299
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300300void map_groups__init(struct map_groups *mg)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300301{
302 int i;
303 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300304 mg->maps[i] = RB_ROOT;
305 INIT_LIST_HEAD(&mg->removed_maps[i]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300306 }
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300307 mg->machine = NULL;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300308}
309
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300310static void maps__delete(struct rb_root *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300311{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300312 struct rb_node *next = rb_first(maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300313
314 while (next) {
315 struct map *pos = rb_entry(next, struct map, rb_node);
316
317 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300318 rb_erase(&pos->rb_node, maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300319 map__delete(pos);
320 }
321}
322
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300323static void maps__delete_removed(struct list_head *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300324{
325 struct map *pos, *n;
326
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300327 list_for_each_entry_safe(pos, n, maps, node) {
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300328 list_del(&pos->node);
329 map__delete(pos);
330 }
331}
332
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300333void map_groups__exit(struct map_groups *mg)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300334{
335 int i;
336
337 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300338 maps__delete(&mg->maps[i]);
339 maps__delete_removed(&mg->removed_maps[i]);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300340 }
341}
342
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300343void map_groups__flush(struct map_groups *mg)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300344{
345 int type;
346
347 for (type = 0; type < MAP__NR_TYPES; type++) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300348 struct rb_root *root = &mg->maps[type];
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300349 struct rb_node *next = rb_first(root);
350
351 while (next) {
352 struct map *pos = rb_entry(next, struct map, rb_node);
353 next = rb_next(&pos->rb_node);
354 rb_erase(&pos->rb_node, root);
355 /*
356 * We may have references to this map, for
357 * instance in some hist_entry instances, so
358 * just move them to a separate list.
359 */
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300360 list_add_tail(&pos->node, &mg->removed_maps[pos->type]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300361 }
362 }
363}
364
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300365struct symbol *map_groups__find_symbol(struct map_groups *mg,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300366 enum map_type type, u64 addr,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300367 struct map **mapp,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300368 symbol_filter_t filter)
369{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300370 struct map *map = map_groups__find(mg, type, addr);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300371
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300372 if (map != NULL) {
373 if (mapp != NULL)
374 *mapp = map;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300375 return map__find_symbol(map, map->map_ip(map, addr), filter);
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300376 }
377
378 return NULL;
379}
380
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300381struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300382 enum map_type type,
383 const char *name,
384 struct map **mapp,
385 symbol_filter_t filter)
386{
387 struct rb_node *nd;
388
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300389 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300390 struct map *pos = rb_entry(nd, struct map, rb_node);
391 struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
392
393 if (sym == NULL)
394 continue;
395 if (mapp != NULL)
396 *mapp = pos;
397 return sym;
398 }
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300399
400 return NULL;
401}
402
Arnaldo Carvalho de Melo4e987712013-10-14 13:43:38 +0300403int map_groups__find_ams(struct addr_map_symbol *ams, symbol_filter_t filter)
404{
405 if (ams->addr < ams->map->start || ams->addr > ams->map->end) {
406 if (ams->map->groups == NULL)
407 return -1;
408 ams->map = map_groups__find(ams->map->groups, ams->map->type,
409 ams->addr);
410 if (ams->map == NULL)
411 return -1;
412 }
413
414 ams->al_addr = ams->map->map_ip(ams->map, ams->addr);
415 ams->sym = map__find_symbol(ams->map, ams->al_addr, filter);
416
417 return ams->sym ? 0 : -1;
418}
419
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300420size_t __map_groups__fprintf_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300421 enum map_type type, int verbose, FILE *fp)
422{
423 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
424 struct rb_node *nd;
425
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300426 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300427 struct map *pos = rb_entry(nd, struct map, rb_node);
428 printed += fprintf(fp, "Map:");
429 printed += map__fprintf(pos, fp);
430 if (verbose > 2) {
431 printed += dso__fprintf(pos->dso, type, fp);
432 printed += fprintf(fp, "--\n");
433 }
434 }
435
436 return printed;
437}
438
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300439size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300440{
441 size_t printed = 0, i;
442 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300443 printed += __map_groups__fprintf_maps(mg, i, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300444 return printed;
445}
446
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300447static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300448 enum map_type type,
449 int verbose, FILE *fp)
450{
451 struct map *pos;
452 size_t printed = 0;
453
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300454 list_for_each_entry(pos, &mg->removed_maps[type], node) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300455 printed += fprintf(fp, "Map:");
456 printed += map__fprintf(pos, fp);
457 if (verbose > 1) {
458 printed += dso__fprintf(pos->dso, type, fp);
459 printed += fprintf(fp, "--\n");
460 }
461 }
462 return printed;
463}
464
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300465static size_t map_groups__fprintf_removed_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300466 int verbose, FILE *fp)
467{
468 size_t printed = 0, i;
469 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300470 printed += __map_groups__fprintf_removed_maps(mg, i, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300471 return printed;
472}
473
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300474size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300475{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300476 size_t printed = map_groups__fprintf_maps(mg, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300477 printed += fprintf(fp, "Removed maps:\n");
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300478 return printed + map_groups__fprintf_removed_maps(mg, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300479}
480
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300481int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300482 int verbose, FILE *fp)
483{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300484 struct rb_root *root = &mg->maps[map->type];
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300485 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300486 int err = 0;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300487
488 while (next) {
489 struct map *pos = rb_entry(next, struct map, rb_node);
490 next = rb_next(&pos->rb_node);
491
492 if (!map__overlap(pos, map))
493 continue;
494
495 if (verbose >= 2) {
496 fputs("overlapping maps:\n", fp);
497 map__fprintf(map, fp);
498 map__fprintf(pos, fp);
499 }
500
501 rb_erase(&pos->rb_node, root);
502 /*
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300503 * Now check if we need to create new maps for areas not
504 * overlapped by the new map:
505 */
506 if (map->start > pos->start) {
507 struct map *before = map__clone(pos);
508
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300509 if (before == NULL) {
510 err = -ENOMEM;
511 goto move_map;
512 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300513
514 before->end = map->start - 1;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300515 map_groups__insert(mg, before);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300516 if (verbose >= 2)
517 map__fprintf(before, fp);
518 }
519
520 if (map->end < pos->end) {
521 struct map *after = map__clone(pos);
522
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300523 if (after == NULL) {
524 err = -ENOMEM;
525 goto move_map;
526 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300527
528 after->start = map->end + 1;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300529 map_groups__insert(mg, after);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300530 if (verbose >= 2)
531 map__fprintf(after, fp);
532 }
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300533move_map:
534 /*
535 * If we have references, just move them to a separate list.
536 */
537 if (pos->referenced)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300538 list_add_tail(&pos->node, &mg->removed_maps[map->type]);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300539 else
540 map__delete(pos);
541
542 if (err)
543 return err;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300544 }
545
546 return 0;
547}
548
549/*
550 * XXX This should not really _copy_ te maps, but refcount them.
551 */
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300552int map_groups__clone(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300553 struct map_groups *parent, enum map_type type)
554{
555 struct rb_node *nd;
556 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
557 struct map *map = rb_entry(nd, struct map, rb_node);
558 struct map *new = map__clone(map);
559 if (new == NULL)
560 return -ENOMEM;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300561 map_groups__insert(mg, new);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300562 }
563 return 0;
564}
565
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300566void maps__insert(struct rb_root *maps, struct map *map)
567{
568 struct rb_node **p = &maps->rb_node;
569 struct rb_node *parent = NULL;
570 const u64 ip = map->start;
571 struct map *m;
572
573 while (*p != NULL) {
574 parent = *p;
575 m = rb_entry(parent, struct map, rb_node);
576 if (ip < m->start)
577 p = &(*p)->rb_left;
578 else
579 p = &(*p)->rb_right;
580 }
581
582 rb_link_node(&map->rb_node, parent, p);
583 rb_insert_color(&map->rb_node, maps);
584}
585
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300586void maps__remove(struct rb_root *maps, struct map *map)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300587{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300588 rb_erase(&map->rb_node, maps);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300589}
590
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300591struct map *maps__find(struct rb_root *maps, u64 ip)
592{
593 struct rb_node **p = &maps->rb_node;
594 struct rb_node *parent = NULL;
595 struct map *m;
596
597 while (*p != NULL) {
598 parent = *p;
599 m = rb_entry(parent, struct map, rb_node);
600 if (ip < m->start)
601 p = &(*p)->rb_left;
602 else if (ip > m->end)
603 p = &(*p)->rb_right;
604 else
605 return m;
606 }
607
608 return NULL;
609}
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300610
611struct map *maps__first(struct rb_root *maps)
612{
613 struct rb_node *first = rb_first(maps);
614
615 if (first)
616 return rb_entry(first, struct map, rb_node);
617 return NULL;
618}
619
620struct map *maps__next(struct map *map)
621{
622 struct rb_node *next = rb_next(&map->rb_node);
623
624 if (next)
625 return rb_entry(next, struct map, rb_node);
626 return NULL;
627}