blob: b6b163642c7d1188c152d182fdcdbf1247674be6 [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"
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020014
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -030015const char *map_type__name[MAP__NR_TYPES] = {
16 [MAP__FUNCTION] = "Functions",
17 [MAP__VARIABLE] = "Variables",
18};
19
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020020static inline int is_anon_memory(const char *filename)
21{
Joshua Zhud0528b52013-01-05 13:29:57 +080022 return !strcmp(filename, "//anon") ||
23 !strcmp(filename, "/anon_hugepage (deleted)");
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020024}
25
Jiri Olsa87ffef72011-08-24 15:18:34 +020026static inline int is_no_dso_memory(const char *filename)
27{
Namhyung Kim1e825742012-11-07 16:27:11 +090028 return !strncmp(filename, "[stack", 6) ||
Jiri Olsa87ffef72011-08-24 15:18:34 +020029 !strcmp(filename, "[heap]");
30}
31
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030032void map__init(struct map *map, enum map_type type,
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020033 u64 start, u64 end, u64 pgoff, struct dso *dso)
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020034{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030035 map->type = type;
36 map->start = start;
37 map->end = end;
38 map->pgoff = pgoff;
39 map->dso = dso;
40 map->map_ip = map__map_ip;
41 map->unmap_ip = map__unmap_ip;
42 RB_CLEAR_NODE(&map->rb_node);
43 map->groups = NULL;
44 map->referenced = false;
45 map->erange_warned = false;
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020046}
47
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080048struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
49 u64 pgoff, u32 pid, char *filename,
Dave Martin361d1342010-07-27 16:40:02 +010050 enum map_type type)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020051{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030052 struct map *map = malloc(sizeof(*map));
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020053
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030054 if (map != NULL) {
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020055 char newfilename[PATH_MAX];
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020056 struct dso *dso;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020057 int anon, no_dso, vdso;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020058
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020059 anon = is_anon_memory(filename);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020060 vdso = is_vdso_map(filename);
Jiri Olsa87ffef72011-08-24 15:18:34 +020061 no_dso = is_no_dso_memory(filename);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020062
63 if (anon) {
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -030064 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020065 filename = newfilename;
66 }
67
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020068 if (vdso) {
69 pgoff = 0;
70 dso = vdso__dso_findnew(dsos__list);
71 } else
72 dso = __dsos__findnew(dsos__list, filename);
73
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020074 if (dso == NULL)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020075 goto out_delete;
76
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030077 map__init(map, type, start, start + len, pgoff, dso);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020078
Jiri Olsa87ffef72011-08-24 15:18:34 +020079 if (anon || no_dso) {
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030080 map->map_ip = map->unmap_ip = identity__map_ip;
Jiri Olsa87ffef72011-08-24 15:18:34 +020081
82 /*
83 * Set memory without DSO as loaded. All map__find_*
84 * functions still return NULL, and we avoid the
85 * unnecessary map__load warning.
86 */
87 if (no_dso)
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030088 dso__set_loaded(dso, map->type);
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -020089 }
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020090 }
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030091 return map;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020092out_delete:
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030093 free(map);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020094 return NULL;
95}
96
Namhyung Kime5a18452012-08-06 13:41:20 +090097/*
98 * Constructor variant for modules (where we know from /proc/modules where
99 * they are loaded) and for vmlinux, where only after we load all the
100 * symbols we'll know where it starts and ends.
101 */
102struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
103{
104 struct map *map = calloc(1, (sizeof(*map) +
105 (dso->kernel ? sizeof(struct kmap) : 0)));
106 if (map != NULL) {
107 /*
108 * ->end will be filled after we load all the symbols
109 */
110 map__init(map, type, start, 0, 0, dso);
111 }
112
113 return map;
114}
115
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300116void map__delete(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200117{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300118 free(map);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200119}
120
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300121void map__fixup_start(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200122{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300123 struct rb_root *symbols = &map->dso->symbols[map->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200124 struct rb_node *nd = rb_first(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200125 if (nd != NULL) {
126 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300127 map->start = sym->start;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200128 }
129}
130
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300131void map__fixup_end(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200132{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300133 struct rb_root *symbols = &map->dso->symbols[map->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200134 struct rb_node *nd = rb_last(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200135 if (nd != NULL) {
136 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300137 map->end = sym->end;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200138 }
139}
140
Arnaldo Carvalho de Melod70a5402009-10-30 16:28:25 -0200141#define DSO__DELETED "(deleted)"
142
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300143int map__load(struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200144{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300145 const char *name = map->dso->long_name;
Masami Hiramatsua1281682009-12-15 10:32:33 -0500146 int nr;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200147
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300148 if (dso__loaded(map->dso, map->type))
Masami Hiramatsua1281682009-12-15 10:32:33 -0500149 return 0;
150
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300151 nr = dso__load(map->dso, map, filter);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200152 if (nr < 0) {
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300153 if (map->dso->has_build_id) {
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200154 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
155
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300156 build_id__sprintf(map->dso->build_id,
157 sizeof(map->dso->build_id),
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200158 sbuild_id);
159 pr_warning("%s with build id %s not found",
160 name, sbuild_id);
161 } else
162 pr_warning("Failed to open %s", name);
163
164 pr_warning(", continuing without symbols\n");
165 return -1;
166 } else if (nr == 0) {
Namhyung Kim29a0fc92012-09-28 18:31:59 +0900167#ifdef LIBELF_SUPPORT
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200168 const size_t len = strlen(name);
169 const size_t real_len = len - sizeof(DSO__DELETED);
170
171 if (len > sizeof(DSO__DELETED) &&
172 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
David Aherne77b15b2011-10-18 18:44:45 -0600173 pr_warning("%.*s was updated (is prelink enabled?). "
174 "Restart the long running apps that use it!\n",
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200175 (int)real_len, name);
176 } else {
177 pr_warning("no symbols found in %s, maybe install "
178 "a debug package?\n", name);
179 }
Namhyung Kim393be2e2012-08-06 13:41:21 +0900180#endif
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200181 return -1;
182 }
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200183 /*
184 * Only applies to the kernel, as its symtabs aren't relative like the
185 * module ones.
186 */
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300187 if (map->dso->kernel)
188 map__reloc_vmlinux(map);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200189
190 return 0;
191}
192
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300193struct symbol *map__find_symbol(struct map *map, u64 addr,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200194 symbol_filter_t filter)
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200195{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300196 if (map__load(map, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200197 return NULL;
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200198
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300199 return dso__find_symbol(map->dso, map->type, addr);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200200}
201
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300202struct symbol *map__find_symbol_by_name(struct map *map, const char *name,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200203 symbol_filter_t filter)
204{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300205 if (map__load(map, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200206 return NULL;
207
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300208 if (!dso__sorted_by_name(map->dso, map->type))
209 dso__sort_by_name(map->dso, map->type);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200210
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300211 return dso__find_symbol_by_name(map->dso, map->type, name);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200212}
213
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300214struct map *map__clone(struct map *map)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200215{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300216 struct map *clone = malloc(sizeof(*clone));
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200217
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300218 if (clone != NULL)
219 memcpy(clone, map, sizeof(*clone));
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200220
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300221 return clone;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200222}
223
224int map__overlap(struct map *l, struct map *r)
225{
226 if (l->start > r->start) {
227 struct map *t = l;
228 l = r;
229 r = t;
230 }
231
232 if (l->end > r->start)
233 return 1;
234
235 return 0;
236}
237
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300238size_t map__fprintf(struct map *map, FILE *fp)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200239{
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200240 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300241 map->start, map->end, map->pgoff, map->dso->name);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200242}
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200243
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900244size_t map__fprintf_dsoname(struct map *map, FILE *fp)
245{
Feng Tang8f28f192012-08-27 15:38:26 +0800246 const char *dsoname = "[unknown]";
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900247
Akihiro Nagai0bc8d202012-01-30 13:43:20 +0900248 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
249 if (symbol_conf.show_kernel_path && map->dso->long_name)
250 dsoname = map->dso->long_name;
251 else if (map->dso->name)
252 dsoname = map->dso->name;
Feng Tang8f28f192012-08-27 15:38:26 +0800253 }
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900254
255 return fprintf(fp, "%s", dsoname);
256}
257
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200258/*
259 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
260 * map->dso->adjust_symbols==1 for ET_EXEC-like cases.
261 */
262u64 map__rip_2objdump(struct map *map, u64 rip)
263{
264 u64 addr = map->dso->adjust_symbols ?
265 map->unmap_ip(map, rip) : /* RIP -> IP */
266 rip;
267 return addr;
268}
Kirill Smelkovee11b902010-02-07 11:46:15 -0200269
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300270void map_groups__init(struct map_groups *mg)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300271{
272 int i;
273 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300274 mg->maps[i] = RB_ROOT;
275 INIT_LIST_HEAD(&mg->removed_maps[i]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300276 }
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300277 mg->machine = NULL;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300278}
279
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300280static void maps__delete(struct rb_root *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300281{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300282 struct rb_node *next = rb_first(maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300283
284 while (next) {
285 struct map *pos = rb_entry(next, struct map, rb_node);
286
287 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300288 rb_erase(&pos->rb_node, maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300289 map__delete(pos);
290 }
291}
292
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300293static void maps__delete_removed(struct list_head *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300294{
295 struct map *pos, *n;
296
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300297 list_for_each_entry_safe(pos, n, maps, node) {
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300298 list_del(&pos->node);
299 map__delete(pos);
300 }
301}
302
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300303void map_groups__exit(struct map_groups *mg)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300304{
305 int i;
306
307 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300308 maps__delete(&mg->maps[i]);
309 maps__delete_removed(&mg->removed_maps[i]);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300310 }
311}
312
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300313void map_groups__flush(struct map_groups *mg)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300314{
315 int type;
316
317 for (type = 0; type < MAP__NR_TYPES; type++) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300318 struct rb_root *root = &mg->maps[type];
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300319 struct rb_node *next = rb_first(root);
320
321 while (next) {
322 struct map *pos = rb_entry(next, struct map, rb_node);
323 next = rb_next(&pos->rb_node);
324 rb_erase(&pos->rb_node, root);
325 /*
326 * We may have references to this map, for
327 * instance in some hist_entry instances, so
328 * just move them to a separate list.
329 */
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300330 list_add_tail(&pos->node, &mg->removed_maps[pos->type]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300331 }
332 }
333}
334
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300335struct symbol *map_groups__find_symbol(struct map_groups *mg,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300336 enum map_type type, u64 addr,
Arnaldo Carvalho de Melo7e5e1b12010-03-26 12:30:40 -0300337 struct map **mapp,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300338 symbol_filter_t filter)
339{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300340 struct map *map = map_groups__find(mg, type, addr);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300341
Arnaldo Carvalho de Melo7e5e1b12010-03-26 12:30:40 -0300342 if (map != NULL) {
343 if (mapp != NULL)
344 *mapp = map;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300345 return map__find_symbol(map, map->map_ip(map, addr), filter);
Arnaldo Carvalho de Melo7e5e1b12010-03-26 12:30:40 -0300346 }
347
348 return NULL;
349}
350
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300351struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo7e5e1b12010-03-26 12:30:40 -0300352 enum map_type type,
353 const char *name,
354 struct map **mapp,
355 symbol_filter_t filter)
356{
357 struct rb_node *nd;
358
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300359 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo7e5e1b12010-03-26 12:30:40 -0300360 struct map *pos = rb_entry(nd, struct map, rb_node);
361 struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
362
363 if (sym == NULL)
364 continue;
365 if (mapp != NULL)
366 *mapp = pos;
367 return sym;
368 }
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300369
370 return NULL;
371}
372
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300373size_t __map_groups__fprintf_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300374 enum map_type type, int verbose, FILE *fp)
375{
376 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
377 struct rb_node *nd;
378
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300379 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300380 struct map *pos = rb_entry(nd, struct map, rb_node);
381 printed += fprintf(fp, "Map:");
382 printed += map__fprintf(pos, fp);
383 if (verbose > 2) {
384 printed += dso__fprintf(pos->dso, type, fp);
385 printed += fprintf(fp, "--\n");
386 }
387 }
388
389 return printed;
390}
391
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300392size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300393{
394 size_t printed = 0, i;
395 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300396 printed += __map_groups__fprintf_maps(mg, i, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300397 return printed;
398}
399
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300400static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300401 enum map_type type,
402 int verbose, FILE *fp)
403{
404 struct map *pos;
405 size_t printed = 0;
406
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300407 list_for_each_entry(pos, &mg->removed_maps[type], node) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300408 printed += fprintf(fp, "Map:");
409 printed += map__fprintf(pos, fp);
410 if (verbose > 1) {
411 printed += dso__fprintf(pos->dso, type, fp);
412 printed += fprintf(fp, "--\n");
413 }
414 }
415 return printed;
416}
417
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300418static size_t map_groups__fprintf_removed_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300419 int verbose, FILE *fp)
420{
421 size_t printed = 0, i;
422 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300423 printed += __map_groups__fprintf_removed_maps(mg, i, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300424 return printed;
425}
426
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300427size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300428{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300429 size_t printed = map_groups__fprintf_maps(mg, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300430 printed += fprintf(fp, "Removed maps:\n");
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300431 return printed + map_groups__fprintf_removed_maps(mg, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300432}
433
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300434int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300435 int verbose, FILE *fp)
436{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300437 struct rb_root *root = &mg->maps[map->type];
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300438 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300439 int err = 0;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300440
441 while (next) {
442 struct map *pos = rb_entry(next, struct map, rb_node);
443 next = rb_next(&pos->rb_node);
444
445 if (!map__overlap(pos, map))
446 continue;
447
448 if (verbose >= 2) {
449 fputs("overlapping maps:\n", fp);
450 map__fprintf(map, fp);
451 map__fprintf(pos, fp);
452 }
453
454 rb_erase(&pos->rb_node, root);
455 /*
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300456 * Now check if we need to create new maps for areas not
457 * overlapped by the new map:
458 */
459 if (map->start > pos->start) {
460 struct map *before = map__clone(pos);
461
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300462 if (before == NULL) {
463 err = -ENOMEM;
464 goto move_map;
465 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300466
467 before->end = map->start - 1;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300468 map_groups__insert(mg, before);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300469 if (verbose >= 2)
470 map__fprintf(before, fp);
471 }
472
473 if (map->end < pos->end) {
474 struct map *after = map__clone(pos);
475
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300476 if (after == NULL) {
477 err = -ENOMEM;
478 goto move_map;
479 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300480
481 after->start = map->end + 1;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300482 map_groups__insert(mg, after);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300483 if (verbose >= 2)
484 map__fprintf(after, fp);
485 }
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300486move_map:
487 /*
488 * If we have references, just move them to a separate list.
489 */
490 if (pos->referenced)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300491 list_add_tail(&pos->node, &mg->removed_maps[map->type]);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300492 else
493 map__delete(pos);
494
495 if (err)
496 return err;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300497 }
498
499 return 0;
500}
501
502/*
503 * XXX This should not really _copy_ te maps, but refcount them.
504 */
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300505int map_groups__clone(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300506 struct map_groups *parent, enum map_type type)
507{
508 struct rb_node *nd;
509 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
510 struct map *map = rb_entry(nd, struct map, rb_node);
511 struct map *new = map__clone(map);
512 if (new == NULL)
513 return -ENOMEM;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300514 map_groups__insert(mg, new);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300515 }
516 return 0;
517}
518
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300519static u64 map__reloc_map_ip(struct map *map, u64 ip)
520{
521 return ip + (s64)map->pgoff;
522}
523
524static u64 map__reloc_unmap_ip(struct map *map, u64 ip)
525{
526 return ip - (s64)map->pgoff;
527}
528
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300529void map__reloc_vmlinux(struct map *map)
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300530{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300531 struct kmap *kmap = map__kmap(map);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300532 s64 reloc;
533
534 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->unrelocated_addr)
535 return;
536
537 reloc = (kmap->ref_reloc_sym->unrelocated_addr -
538 kmap->ref_reloc_sym->addr);
539
540 if (!reloc)
541 return;
542
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300543 map->map_ip = map__reloc_map_ip;
544 map->unmap_ip = map__reloc_unmap_ip;
545 map->pgoff = reloc;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300546}
547
548void maps__insert(struct rb_root *maps, struct map *map)
549{
550 struct rb_node **p = &maps->rb_node;
551 struct rb_node *parent = NULL;
552 const u64 ip = map->start;
553 struct map *m;
554
555 while (*p != NULL) {
556 parent = *p;
557 m = rb_entry(parent, struct map, rb_node);
558 if (ip < m->start)
559 p = &(*p)->rb_left;
560 else
561 p = &(*p)->rb_right;
562 }
563
564 rb_link_node(&map->rb_node, parent, p);
565 rb_insert_color(&map->rb_node, maps);
566}
567
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300568void maps__remove(struct rb_root *maps, struct map *map)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300569{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300570 rb_erase(&map->rb_node, maps);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300571}
572
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300573struct map *maps__find(struct rb_root *maps, u64 ip)
574{
575 struct rb_node **p = &maps->rb_node;
576 struct rb_node *parent = NULL;
577 struct map *m;
578
579 while (*p != NULL) {
580 parent = *p;
581 m = rb_entry(parent, struct map, rb_node);
582 if (ip < m->start)
583 p = &(*p)->rb_left;
584 else if (ip > m->end)
585 p = &(*p)->rb_right;
586 else
587 return m;
588 }
589
590 return NULL;
591}