blob: 8bcdf9e54089acaf4963eb8288305bcba4d047d6 [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,
51 u64 pgoff, u32 pid, char *filename,
Dave Martin361d1342010-07-27 16:40:02 +010052 enum map_type type)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020053{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030054 struct map *map = malloc(sizeof(*map));
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020055
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030056 if (map != NULL) {
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020057 char newfilename[PATH_MAX];
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020058 struct dso *dso;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020059 int anon, no_dso, vdso;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020060
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020061 anon = is_anon_memory(filename);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020062 vdso = is_vdso_map(filename);
Jiri Olsa87ffef72011-08-24 15:18:34 +020063 no_dso = is_no_dso_memory(filename);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020064
65 if (anon) {
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -030066 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020067 filename = newfilename;
68 }
69
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020070 if (vdso) {
71 pgoff = 0;
72 dso = vdso__dso_findnew(dsos__list);
73 } else
74 dso = __dsos__findnew(dsos__list, filename);
75
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020076 if (dso == NULL)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020077 goto out_delete;
78
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030079 map__init(map, type, start, start + len, pgoff, dso);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020080
Jiri Olsa87ffef72011-08-24 15:18:34 +020081 if (anon || no_dso) {
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030082 map->map_ip = map->unmap_ip = identity__map_ip;
Jiri Olsa87ffef72011-08-24 15:18:34 +020083
84 /*
85 * Set memory without DSO as loaded. All map__find_*
86 * functions still return NULL, and we avoid the
87 * unnecessary map__load warning.
88 */
89 if (no_dso)
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030090 dso__set_loaded(dso, map->type);
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -020091 }
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020092 }
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030093 return map;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020094out_delete:
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030095 free(map);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020096 return NULL;
97}
98
Namhyung Kime5a18452012-08-06 13:41:20 +090099/*
100 * Constructor variant for modules (where we know from /proc/modules where
101 * they are loaded) and for vmlinux, where only after we load all the
102 * symbols we'll know where it starts and ends.
103 */
104struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
105{
106 struct map *map = calloc(1, (sizeof(*map) +
107 (dso->kernel ? sizeof(struct kmap) : 0)));
108 if (map != NULL) {
109 /*
110 * ->end will be filled after we load all the symbols
111 */
112 map__init(map, type, start, 0, 0, dso);
113 }
114
115 return map;
116}
117
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300118void map__delete(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200119{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300120 free(map);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200121}
122
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300123void map__fixup_start(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200124{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300125 struct rb_root *symbols = &map->dso->symbols[map->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200126 struct rb_node *nd = rb_first(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200127 if (nd != NULL) {
128 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300129 map->start = sym->start;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200130 }
131}
132
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300133void map__fixup_end(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200134{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300135 struct rb_root *symbols = &map->dso->symbols[map->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200136 struct rb_node *nd = rb_last(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200137 if (nd != NULL) {
138 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300139 map->end = sym->end;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200140 }
141}
142
Arnaldo Carvalho de Melod70a5402009-10-30 16:28:25 -0200143#define DSO__DELETED "(deleted)"
144
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300145int map__load(struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200146{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300147 const char *name = map->dso->long_name;
Masami Hiramatsua1281682009-12-15 10:32:33 -0500148 int nr;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200149
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300150 if (dso__loaded(map->dso, map->type))
Masami Hiramatsua1281682009-12-15 10:32:33 -0500151 return 0;
152
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300153 nr = dso__load(map->dso, map, filter);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200154 if (nr < 0) {
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300155 if (map->dso->has_build_id) {
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200156 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
157
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300158 build_id__sprintf(map->dso->build_id,
159 sizeof(map->dso->build_id),
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200160 sbuild_id);
161 pr_warning("%s with build id %s not found",
162 name, sbuild_id);
163 } else
164 pr_warning("Failed to open %s", name);
165
166 pr_warning(", continuing without symbols\n");
167 return -1;
168 } else if (nr == 0) {
Namhyung Kim29a0fc92012-09-28 18:31:59 +0900169#ifdef LIBELF_SUPPORT
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200170 const size_t len = strlen(name);
171 const size_t real_len = len - sizeof(DSO__DELETED);
172
173 if (len > sizeof(DSO__DELETED) &&
174 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
David Aherne77b15b2011-10-18 18:44:45 -0600175 pr_warning("%.*s was updated (is prelink enabled?). "
176 "Restart the long running apps that use it!\n",
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200177 (int)real_len, name);
178 } else {
179 pr_warning("no symbols found in %s, maybe install "
180 "a debug package?\n", name);
181 }
Namhyung Kim393be2e2012-08-06 13:41:21 +0900182#endif
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200183 return -1;
184 }
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200185 /*
186 * Only applies to the kernel, as its symtabs aren't relative like the
187 * module ones.
188 */
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300189 if (map->dso->kernel)
190 map__reloc_vmlinux(map);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200191
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
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200255/*
256 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
257 * map->dso->adjust_symbols==1 for ET_EXEC-like cases.
258 */
259u64 map__rip_2objdump(struct map *map, u64 rip)
260{
261 u64 addr = map->dso->adjust_symbols ?
262 map->unmap_ip(map, rip) : /* RIP -> IP */
263 rip;
264 return addr;
265}
Kirill Smelkovee11b902010-02-07 11:46:15 -0200266
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300267void map_groups__init(struct map_groups *mg)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300268{
269 int i;
270 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300271 mg->maps[i] = RB_ROOT;
272 INIT_LIST_HEAD(&mg->removed_maps[i]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300273 }
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300274 mg->machine = NULL;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300275}
276
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300277static void maps__delete(struct rb_root *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300278{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300279 struct rb_node *next = rb_first(maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300280
281 while (next) {
282 struct map *pos = rb_entry(next, struct map, rb_node);
283
284 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300285 rb_erase(&pos->rb_node, maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300286 map__delete(pos);
287 }
288}
289
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300290static void maps__delete_removed(struct list_head *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300291{
292 struct map *pos, *n;
293
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300294 list_for_each_entry_safe(pos, n, maps, node) {
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300295 list_del(&pos->node);
296 map__delete(pos);
297 }
298}
299
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300300void map_groups__exit(struct map_groups *mg)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300301{
302 int i;
303
304 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300305 maps__delete(&mg->maps[i]);
306 maps__delete_removed(&mg->removed_maps[i]);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300307 }
308}
309
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300310void map_groups__flush(struct map_groups *mg)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300311{
312 int type;
313
314 for (type = 0; type < MAP__NR_TYPES; type++) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300315 struct rb_root *root = &mg->maps[type];
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300316 struct rb_node *next = rb_first(root);
317
318 while (next) {
319 struct map *pos = rb_entry(next, struct map, rb_node);
320 next = rb_next(&pos->rb_node);
321 rb_erase(&pos->rb_node, root);
322 /*
323 * We may have references to this map, for
324 * instance in some hist_entry instances, so
325 * just move them to a separate list.
326 */
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300327 list_add_tail(&pos->node, &mg->removed_maps[pos->type]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300328 }
329 }
330}
331
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300332struct symbol *map_groups__find_symbol(struct map_groups *mg,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300333 enum map_type type, u64 addr,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300334 struct map **mapp,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300335 symbol_filter_t filter)
336{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300337 struct map *map = map_groups__find(mg, type, addr);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300338
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300339 if (map != NULL) {
340 if (mapp != NULL)
341 *mapp = map;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300342 return map__find_symbol(map, map->map_ip(map, addr), filter);
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300343 }
344
345 return NULL;
346}
347
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300348struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300349 enum map_type type,
350 const char *name,
351 struct map **mapp,
352 symbol_filter_t filter)
353{
354 struct rb_node *nd;
355
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300356 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300357 struct map *pos = rb_entry(nd, struct map, rb_node);
358 struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
359
360 if (sym == NULL)
361 continue;
362 if (mapp != NULL)
363 *mapp = pos;
364 return sym;
365 }
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300366
367 return NULL;
368}
369
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300370size_t __map_groups__fprintf_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300371 enum map_type type, int verbose, FILE *fp)
372{
373 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
374 struct rb_node *nd;
375
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300376 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300377 struct map *pos = rb_entry(nd, struct map, rb_node);
378 printed += fprintf(fp, "Map:");
379 printed += map__fprintf(pos, fp);
380 if (verbose > 2) {
381 printed += dso__fprintf(pos->dso, type, fp);
382 printed += fprintf(fp, "--\n");
383 }
384 }
385
386 return printed;
387}
388
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300389size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300390{
391 size_t printed = 0, i;
392 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300393 printed += __map_groups__fprintf_maps(mg, i, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300394 return printed;
395}
396
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300397static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300398 enum map_type type,
399 int verbose, FILE *fp)
400{
401 struct map *pos;
402 size_t printed = 0;
403
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300404 list_for_each_entry(pos, &mg->removed_maps[type], node) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300405 printed += fprintf(fp, "Map:");
406 printed += map__fprintf(pos, fp);
407 if (verbose > 1) {
408 printed += dso__fprintf(pos->dso, type, fp);
409 printed += fprintf(fp, "--\n");
410 }
411 }
412 return printed;
413}
414
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300415static size_t map_groups__fprintf_removed_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300416 int verbose, FILE *fp)
417{
418 size_t printed = 0, i;
419 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300420 printed += __map_groups__fprintf_removed_maps(mg, i, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300421 return printed;
422}
423
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300424size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300425{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300426 size_t printed = map_groups__fprintf_maps(mg, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300427 printed += fprintf(fp, "Removed maps:\n");
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300428 return printed + map_groups__fprintf_removed_maps(mg, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300429}
430
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300431int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300432 int verbose, FILE *fp)
433{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300434 struct rb_root *root = &mg->maps[map->type];
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300435 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300436 int err = 0;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300437
438 while (next) {
439 struct map *pos = rb_entry(next, struct map, rb_node);
440 next = rb_next(&pos->rb_node);
441
442 if (!map__overlap(pos, map))
443 continue;
444
445 if (verbose >= 2) {
446 fputs("overlapping maps:\n", fp);
447 map__fprintf(map, fp);
448 map__fprintf(pos, fp);
449 }
450
451 rb_erase(&pos->rb_node, root);
452 /*
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300453 * Now check if we need to create new maps for areas not
454 * overlapped by the new map:
455 */
456 if (map->start > pos->start) {
457 struct map *before = map__clone(pos);
458
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300459 if (before == NULL) {
460 err = -ENOMEM;
461 goto move_map;
462 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300463
464 before->end = map->start - 1;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300465 map_groups__insert(mg, before);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300466 if (verbose >= 2)
467 map__fprintf(before, fp);
468 }
469
470 if (map->end < pos->end) {
471 struct map *after = map__clone(pos);
472
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300473 if (after == NULL) {
474 err = -ENOMEM;
475 goto move_map;
476 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300477
478 after->start = map->end + 1;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300479 map_groups__insert(mg, after);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300480 if (verbose >= 2)
481 map__fprintf(after, fp);
482 }
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300483move_map:
484 /*
485 * If we have references, just move them to a separate list.
486 */
487 if (pos->referenced)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300488 list_add_tail(&pos->node, &mg->removed_maps[map->type]);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300489 else
490 map__delete(pos);
491
492 if (err)
493 return err;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300494 }
495
496 return 0;
497}
498
499/*
500 * XXX This should not really _copy_ te maps, but refcount them.
501 */
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300502int map_groups__clone(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300503 struct map_groups *parent, enum map_type type)
504{
505 struct rb_node *nd;
506 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
507 struct map *map = rb_entry(nd, struct map, rb_node);
508 struct map *new = map__clone(map);
509 if (new == NULL)
510 return -ENOMEM;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300511 map_groups__insert(mg, new);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300512 }
513 return 0;
514}
515
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300516static u64 map__reloc_map_ip(struct map *map, u64 ip)
517{
518 return ip + (s64)map->pgoff;
519}
520
521static u64 map__reloc_unmap_ip(struct map *map, u64 ip)
522{
523 return ip - (s64)map->pgoff;
524}
525
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300526void map__reloc_vmlinux(struct map *map)
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300527{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300528 struct kmap *kmap = map__kmap(map);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300529 s64 reloc;
530
531 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->unrelocated_addr)
532 return;
533
534 reloc = (kmap->ref_reloc_sym->unrelocated_addr -
535 kmap->ref_reloc_sym->addr);
536
537 if (!reloc)
538 return;
539
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300540 map->map_ip = map__reloc_map_ip;
541 map->unmap_ip = map__reloc_unmap_ip;
542 map->pgoff = reloc;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300543}
544
545void maps__insert(struct rb_root *maps, struct map *map)
546{
547 struct rb_node **p = &maps->rb_node;
548 struct rb_node *parent = NULL;
549 const u64 ip = map->start;
550 struct map *m;
551
552 while (*p != NULL) {
553 parent = *p;
554 m = rb_entry(parent, struct map, rb_node);
555 if (ip < m->start)
556 p = &(*p)->rb_left;
557 else
558 p = &(*p)->rb_right;
559 }
560
561 rb_link_node(&map->rb_node, parent, p);
562 rb_insert_color(&map->rb_node, maps);
563}
564
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300565void maps__remove(struct rb_root *maps, struct map *map)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300566{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300567 rb_erase(&map->rb_node, maps);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300568}
569
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300570struct map *maps__find(struct rb_root *maps, u64 ip)
571{
572 struct rb_node **p = &maps->rb_node;
573 struct rb_node *parent = NULL;
574 struct map *m;
575
576 while (*p != NULL) {
577 parent = *p;
578 m = rb_entry(parent, struct map, rb_node);
579 if (ip < m->start)
580 p = &(*p)->rb_left;
581 else if (ip > m->end)
582 p = &(*p)->rb_right;
583 else
584 return m;
585 }
586
587 return NULL;
588}