blob: 9b9bd719aa197d0a240809834109581a57d810b2 [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"
Adrian Huntercc8fae12013-12-06 09:42:57 +020014#include "util.h"
Arnaldo Carvalho de Melo8e160172013-01-24 22:16:43 -030015#include <linux/string.h>
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020016
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -030017const char *map_type__name[MAP__NR_TYPES] = {
18 [MAP__FUNCTION] = "Functions",
19 [MAP__VARIABLE] = "Variables",
20};
21
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020022static inline int is_anon_memory(const char *filename)
23{
Joshua Zhud0528b52013-01-05 13:29:57 +080024 return !strcmp(filename, "//anon") ||
Andi Kleen89365e62013-04-24 17:03:02 -070025 !strcmp(filename, "/dev/zero (deleted)") ||
Joshua Zhud0528b52013-01-05 13:29:57 +080026 !strcmp(filename, "/anon_hugepage (deleted)");
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020027}
28
Jiri Olsa87ffef72011-08-24 15:18:34 +020029static inline int is_no_dso_memory(const char *filename)
30{
Namhyung Kim1e825742012-11-07 16:27:11 +090031 return !strncmp(filename, "[stack", 6) ||
Jiri Olsa87ffef72011-08-24 15:18:34 +020032 !strcmp(filename, "[heap]");
33}
34
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030035void map__init(struct map *map, enum map_type type,
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020036 u64 start, u64 end, u64 pgoff, struct dso *dso)
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020037{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030038 map->type = type;
39 map->start = start;
40 map->end = end;
41 map->pgoff = pgoff;
42 map->dso = dso;
43 map->map_ip = map__map_ip;
44 map->unmap_ip = map__unmap_ip;
45 RB_CLEAR_NODE(&map->rb_node);
46 map->groups = NULL;
47 map->referenced = false;
48 map->erange_warned = false;
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020049}
50
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080051struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
Stephane Eranian5c5e8542013-08-21 12:10:25 +020052 u64 pgoff, u32 pid, u32 d_maj, u32 d_min, u64 ino,
53 u64 ino_gen, char *filename,
Dave Martin361d1342010-07-27 16:40:02 +010054 enum map_type type)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020055{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030056 struct map *map = malloc(sizeof(*map));
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020057
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030058 if (map != NULL) {
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020059 char newfilename[PATH_MAX];
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020060 struct dso *dso;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020061 int anon, no_dso, vdso;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020062
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020063 anon = is_anon_memory(filename);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020064 vdso = is_vdso_map(filename);
Jiri Olsa87ffef72011-08-24 15:18:34 +020065 no_dso = is_no_dso_memory(filename);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020066
Stephane Eranian5c5e8542013-08-21 12:10:25 +020067 map->maj = d_maj;
68 map->min = d_min;
69 map->ino = ino;
70 map->ino_generation = ino_gen;
71
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020072 if (anon) {
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -030073 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020074 filename = newfilename;
75 }
76
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020077 if (vdso) {
78 pgoff = 0;
79 dso = vdso__dso_findnew(dsos__list);
80 } else
81 dso = __dsos__findnew(dsos__list, filename);
82
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020083 if (dso == NULL)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020084 goto out_delete;
85
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030086 map__init(map, type, start, start + len, pgoff, dso);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020087
Jiri Olsa87ffef72011-08-24 15:18:34 +020088 if (anon || no_dso) {
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030089 map->map_ip = map->unmap_ip = identity__map_ip;
Jiri Olsa87ffef72011-08-24 15:18:34 +020090
91 /*
92 * Set memory without DSO as loaded. All map__find_*
93 * functions still return NULL, and we avoid the
94 * unnecessary map__load warning.
95 */
96 if (no_dso)
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030097 dso__set_loaded(dso, map->type);
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -020098 }
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020099 }
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300100 return map;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200101out_delete:
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300102 free(map);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200103 return NULL;
104}
105
Namhyung Kime5a18452012-08-06 13:41:20 +0900106/*
107 * Constructor variant for modules (where we know from /proc/modules where
108 * they are loaded) and for vmlinux, where only after we load all the
109 * symbols we'll know where it starts and ends.
110 */
111struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
112{
113 struct map *map = calloc(1, (sizeof(*map) +
114 (dso->kernel ? sizeof(struct kmap) : 0)));
115 if (map != NULL) {
116 /*
117 * ->end will be filled after we load all the symbols
118 */
119 map__init(map, type, start, 0, 0, dso);
120 }
121
122 return map;
123}
124
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300125void map__delete(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200126{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300127 free(map);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200128}
129
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300130void map__fixup_start(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200131{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300132 struct rb_root *symbols = &map->dso->symbols[map->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200133 struct rb_node *nd = rb_first(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200134 if (nd != NULL) {
135 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300136 map->start = sym->start;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200137 }
138}
139
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300140void map__fixup_end(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200141{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300142 struct rb_root *symbols = &map->dso->symbols[map->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200143 struct rb_node *nd = rb_last(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200144 if (nd != NULL) {
145 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300146 map->end = sym->end;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200147 }
148}
149
Arnaldo Carvalho de Melod70a5402009-10-30 16:28:25 -0200150#define DSO__DELETED "(deleted)"
151
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300152int map__load(struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200153{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300154 const char *name = map->dso->long_name;
Masami Hiramatsua1281682009-12-15 10:32:33 -0500155 int nr;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200156
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300157 if (dso__loaded(map->dso, map->type))
Masami Hiramatsua1281682009-12-15 10:32:33 -0500158 return 0;
159
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300160 nr = dso__load(map->dso, map, filter);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200161 if (nr < 0) {
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300162 if (map->dso->has_build_id) {
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200163 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
164
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300165 build_id__sprintf(map->dso->build_id,
166 sizeof(map->dso->build_id),
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200167 sbuild_id);
168 pr_warning("%s with build id %s not found",
169 name, sbuild_id);
170 } else
171 pr_warning("Failed to open %s", name);
172
173 pr_warning(", continuing without symbols\n");
174 return -1;
175 } else if (nr == 0) {
Ingo Molnar89fe8082013-09-30 12:07:11 +0200176#ifdef HAVE_LIBELF_SUPPORT
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200177 const size_t len = strlen(name);
178 const size_t real_len = len - sizeof(DSO__DELETED);
179
180 if (len > sizeof(DSO__DELETED) &&
181 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
David Aherne77b15b2011-10-18 18:44:45 -0600182 pr_warning("%.*s was updated (is prelink enabled?). "
183 "Restart the long running apps that use it!\n",
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200184 (int)real_len, name);
185 } else {
186 pr_warning("no symbols found in %s, maybe install "
187 "a debug package?\n", name);
188 }
Namhyung Kim393be2e2012-08-06 13:41:21 +0900189#endif
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200190 return -1;
191 }
192
193 return 0;
194}
195
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300196struct symbol *map__find_symbol(struct map *map, u64 addr,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200197 symbol_filter_t filter)
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200198{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300199 if (map__load(map, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200200 return NULL;
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200201
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300202 return dso__find_symbol(map->dso, map->type, addr);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200203}
204
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300205struct symbol *map__find_symbol_by_name(struct map *map, const char *name,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200206 symbol_filter_t filter)
207{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300208 if (map__load(map, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200209 return NULL;
210
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300211 if (!dso__sorted_by_name(map->dso, map->type))
212 dso__sort_by_name(map->dso, map->type);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200213
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300214 return dso__find_symbol_by_name(map->dso, map->type, name);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200215}
216
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300217struct map *map__clone(struct map *map)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200218{
Arnaldo Carvalho de Melo8e160172013-01-24 22:16:43 -0300219 return memdup(map, sizeof(*map));
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200220}
221
222int map__overlap(struct map *l, struct map *r)
223{
224 if (l->start > r->start) {
225 struct map *t = l;
226 l = r;
227 r = t;
228 }
229
230 if (l->end > r->start)
231 return 1;
232
233 return 0;
234}
235
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300236size_t map__fprintf(struct map *map, FILE *fp)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200237{
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200238 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300239 map->start, map->end, map->pgoff, map->dso->name);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200240}
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200241
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900242size_t map__fprintf_dsoname(struct map *map, FILE *fp)
243{
Feng Tang8f28f192012-08-27 15:38:26 +0800244 const char *dsoname = "[unknown]";
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900245
Akihiro Nagai0bc8d202012-01-30 13:43:20 +0900246 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
247 if (symbol_conf.show_kernel_path && map->dso->long_name)
248 dsoname = map->dso->long_name;
249 else if (map->dso->name)
250 dsoname = map->dso->name;
Feng Tang8f28f192012-08-27 15:38:26 +0800251 }
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900252
253 return fprintf(fp, "%s", dsoname);
254}
255
Adrian Huntercc8fae12013-12-06 09:42:57 +0200256int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
257 FILE *fp)
258{
259 char *srcline;
260 int ret = 0;
261
262 if (map && map->dso) {
263 srcline = get_srcline(map->dso,
264 map__rip_2objdump(map, addr));
265 if (srcline != SRCLINE_UNKNOWN)
266 ret = fprintf(fp, "%s%s", prefix, srcline);
267 free_srcline(srcline);
268 }
269 return ret;
270}
271
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300272/**
273 * map__rip_2objdump - convert symbol start address to objdump address.
274 * @map: memory map
275 * @rip: symbol start address
276 *
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200277 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300278 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
279 * relative to section start.
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300280 *
281 * Return: Address suitable for passing to "objdump --start-address="
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200282 */
283u64 map__rip_2objdump(struct map *map, u64 rip)
284{
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300285 if (!map->dso->adjust_symbols)
286 return rip;
287
288 if (map->dso->rel)
289 return rip - map->pgoff;
290
291 return map->unmap_ip(map, rip);
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200292}
Kirill Smelkovee11b902010-02-07 11:46:15 -0200293
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300294/**
295 * map__objdump_2mem - convert objdump address to a memory address.
296 * @map: memory map
297 * @ip: objdump address
298 *
299 * Closely related to map__rip_2objdump(), this function takes an address from
300 * objdump and converts it to a memory address. Note this assumes that @map
301 * contains the address. To be sure the result is valid, check it forwards
302 * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
303 *
304 * Return: Memory address.
305 */
306u64 map__objdump_2mem(struct map *map, u64 ip)
307{
308 if (!map->dso->adjust_symbols)
309 return map->unmap_ip(map, ip);
310
311 if (map->dso->rel)
312 return map->unmap_ip(map, ip + map->pgoff);
313
314 return ip;
315}
316
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300317void map_groups__init(struct map_groups *mg)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300318{
319 int i;
320 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300321 mg->maps[i] = RB_ROOT;
322 INIT_LIST_HEAD(&mg->removed_maps[i]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300323 }
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300324 mg->machine = NULL;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300325}
326
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300327static void maps__delete(struct rb_root *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300328{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300329 struct rb_node *next = rb_first(maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300330
331 while (next) {
332 struct map *pos = rb_entry(next, struct map, rb_node);
333
334 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300335 rb_erase(&pos->rb_node, maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300336 map__delete(pos);
337 }
338}
339
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300340static void maps__delete_removed(struct list_head *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300341{
342 struct map *pos, *n;
343
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300344 list_for_each_entry_safe(pos, n, maps, node) {
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300345 list_del(&pos->node);
346 map__delete(pos);
347 }
348}
349
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300350void map_groups__exit(struct map_groups *mg)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300351{
352 int i;
353
354 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300355 maps__delete(&mg->maps[i]);
356 maps__delete_removed(&mg->removed_maps[i]);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300357 }
358}
359
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300360void map_groups__flush(struct map_groups *mg)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300361{
362 int type;
363
364 for (type = 0; type < MAP__NR_TYPES; type++) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300365 struct rb_root *root = &mg->maps[type];
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300366 struct rb_node *next = rb_first(root);
367
368 while (next) {
369 struct map *pos = rb_entry(next, struct map, rb_node);
370 next = rb_next(&pos->rb_node);
371 rb_erase(&pos->rb_node, root);
372 /*
373 * We may have references to this map, for
374 * instance in some hist_entry instances, so
375 * just move them to a separate list.
376 */
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300377 list_add_tail(&pos->node, &mg->removed_maps[pos->type]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300378 }
379 }
380}
381
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300382struct symbol *map_groups__find_symbol(struct map_groups *mg,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300383 enum map_type type, u64 addr,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300384 struct map **mapp,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300385 symbol_filter_t filter)
386{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300387 struct map *map = map_groups__find(mg, type, addr);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300388
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300389 if (map != NULL) {
390 if (mapp != NULL)
391 *mapp = map;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300392 return map__find_symbol(map, map->map_ip(map, addr), filter);
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300393 }
394
395 return NULL;
396}
397
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300398struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300399 enum map_type type,
400 const char *name,
401 struct map **mapp,
402 symbol_filter_t filter)
403{
404 struct rb_node *nd;
405
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300406 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300407 struct map *pos = rb_entry(nd, struct map, rb_node);
408 struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
409
410 if (sym == NULL)
411 continue;
412 if (mapp != NULL)
413 *mapp = pos;
414 return sym;
415 }
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300416
417 return NULL;
418}
419
Arnaldo Carvalho de Melo4e987712013-10-14 13:43:38 +0300420int map_groups__find_ams(struct addr_map_symbol *ams, symbol_filter_t filter)
421{
422 if (ams->addr < ams->map->start || ams->addr > ams->map->end) {
423 if (ams->map->groups == NULL)
424 return -1;
425 ams->map = map_groups__find(ams->map->groups, ams->map->type,
426 ams->addr);
427 if (ams->map == NULL)
428 return -1;
429 }
430
431 ams->al_addr = ams->map->map_ip(ams->map, ams->addr);
432 ams->sym = map__find_symbol(ams->map, ams->al_addr, filter);
433
434 return ams->sym ? 0 : -1;
435}
436
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300437size_t __map_groups__fprintf_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300438 enum map_type type, int verbose, FILE *fp)
439{
440 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
441 struct rb_node *nd;
442
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300443 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300444 struct map *pos = rb_entry(nd, struct map, rb_node);
445 printed += fprintf(fp, "Map:");
446 printed += map__fprintf(pos, fp);
447 if (verbose > 2) {
448 printed += dso__fprintf(pos->dso, type, fp);
449 printed += fprintf(fp, "--\n");
450 }
451 }
452
453 return printed;
454}
455
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300456size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300457{
458 size_t printed = 0, i;
459 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300460 printed += __map_groups__fprintf_maps(mg, i, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300461 return printed;
462}
463
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300464static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300465 enum map_type type,
466 int verbose, FILE *fp)
467{
468 struct map *pos;
469 size_t printed = 0;
470
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300471 list_for_each_entry(pos, &mg->removed_maps[type], node) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300472 printed += fprintf(fp, "Map:");
473 printed += map__fprintf(pos, fp);
474 if (verbose > 1) {
475 printed += dso__fprintf(pos->dso, type, fp);
476 printed += fprintf(fp, "--\n");
477 }
478 }
479 return printed;
480}
481
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300482static size_t map_groups__fprintf_removed_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300483 int verbose, FILE *fp)
484{
485 size_t printed = 0, i;
486 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300487 printed += __map_groups__fprintf_removed_maps(mg, i, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300488 return printed;
489}
490
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300491size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300492{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300493 size_t printed = map_groups__fprintf_maps(mg, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300494 printed += fprintf(fp, "Removed maps:\n");
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300495 return printed + map_groups__fprintf_removed_maps(mg, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300496}
497
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300498int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300499 int verbose, FILE *fp)
500{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300501 struct rb_root *root = &mg->maps[map->type];
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300502 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300503 int err = 0;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300504
505 while (next) {
506 struct map *pos = rb_entry(next, struct map, rb_node);
507 next = rb_next(&pos->rb_node);
508
509 if (!map__overlap(pos, map))
510 continue;
511
512 if (verbose >= 2) {
513 fputs("overlapping maps:\n", fp);
514 map__fprintf(map, fp);
515 map__fprintf(pos, fp);
516 }
517
518 rb_erase(&pos->rb_node, root);
519 /*
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300520 * Now check if we need to create new maps for areas not
521 * overlapped by the new map:
522 */
523 if (map->start > pos->start) {
524 struct map *before = map__clone(pos);
525
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300526 if (before == NULL) {
527 err = -ENOMEM;
528 goto move_map;
529 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300530
531 before->end = map->start - 1;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300532 map_groups__insert(mg, before);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300533 if (verbose >= 2)
534 map__fprintf(before, fp);
535 }
536
537 if (map->end < pos->end) {
538 struct map *after = map__clone(pos);
539
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300540 if (after == NULL) {
541 err = -ENOMEM;
542 goto move_map;
543 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300544
545 after->start = map->end + 1;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300546 map_groups__insert(mg, after);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300547 if (verbose >= 2)
548 map__fprintf(after, fp);
549 }
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300550move_map:
551 /*
552 * If we have references, just move them to a separate list.
553 */
554 if (pos->referenced)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300555 list_add_tail(&pos->node, &mg->removed_maps[map->type]);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300556 else
557 map__delete(pos);
558
559 if (err)
560 return err;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300561 }
562
563 return 0;
564}
565
566/*
567 * XXX This should not really _copy_ te maps, but refcount them.
568 */
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300569int map_groups__clone(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300570 struct map_groups *parent, enum map_type type)
571{
572 struct rb_node *nd;
573 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
574 struct map *map = rb_entry(nd, struct map, rb_node);
575 struct map *new = map__clone(map);
576 if (new == NULL)
577 return -ENOMEM;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300578 map_groups__insert(mg, new);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300579 }
580 return 0;
581}
582
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300583void maps__insert(struct rb_root *maps, struct map *map)
584{
585 struct rb_node **p = &maps->rb_node;
586 struct rb_node *parent = NULL;
587 const u64 ip = map->start;
588 struct map *m;
589
590 while (*p != NULL) {
591 parent = *p;
592 m = rb_entry(parent, struct map, rb_node);
593 if (ip < m->start)
594 p = &(*p)->rb_left;
595 else
596 p = &(*p)->rb_right;
597 }
598
599 rb_link_node(&map->rb_node, parent, p);
600 rb_insert_color(&map->rb_node, maps);
601}
602
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300603void maps__remove(struct rb_root *maps, struct map *map)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300604{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300605 rb_erase(&map->rb_node, maps);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300606}
607
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300608struct map *maps__find(struct rb_root *maps, u64 ip)
609{
610 struct rb_node **p = &maps->rb_node;
611 struct rb_node *parent = NULL;
612 struct map *m;
613
614 while (*p != NULL) {
615 parent = *p;
616 m = rb_entry(parent, struct map, rb_node);
617 if (ip < m->start)
618 p = &(*p)->rb_left;
619 else if (ip > m->end)
620 p = &(*p)->rb_right;
621 else
622 return m;
623 }
624
625 return NULL;
626}
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300627
628struct map *maps__first(struct rb_root *maps)
629{
630 struct rb_node *first = rb_first(maps);
631
632 if (first)
633 return rb_entry(first, struct map, rb_node);
634 return NULL;
635}
636
637struct map *maps__next(struct map *map)
638{
639 struct rb_node *next = rb_next(&map->rb_node);
640
641 if (next)
642 return rb_entry(next, struct map, rb_node);
643 return NULL;
644}