blob: ae4c5e12debdc76bcf1345e90faf9d2616e46a31 [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;
Adrian Hunter91767532014-01-29 16:14:36 +020042 map->reloc = 0;
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030043 map->dso = dso;
44 map->map_ip = map__map_ip;
45 map->unmap_ip = map__unmap_ip;
46 RB_CLEAR_NODE(&map->rb_node);
47 map->groups = NULL;
48 map->referenced = false;
49 map->erange_warned = false;
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020050}
51
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080052struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
Stephane Eranian5c5e8542013-08-21 12:10:25 +020053 u64 pgoff, u32 pid, u32 d_maj, u32 d_min, u64 ino,
54 u64 ino_gen, char *filename,
Dave Martin361d1342010-07-27 16:40:02 +010055 enum map_type type)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020056{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030057 struct map *map = malloc(sizeof(*map));
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020058
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030059 if (map != NULL) {
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020060 char newfilename[PATH_MAX];
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020061 struct dso *dso;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020062 int anon, no_dso, vdso;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020063
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020064 anon = is_anon_memory(filename);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020065 vdso = is_vdso_map(filename);
Jiri Olsa87ffef72011-08-24 15:18:34 +020066 no_dso = is_no_dso_memory(filename);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020067
Stephane Eranian5c5e8542013-08-21 12:10:25 +020068 map->maj = d_maj;
69 map->min = d_min;
70 map->ino = ino;
71 map->ino_generation = ino_gen;
72
Namhyung Kim578c03c2014-01-16 10:49:31 +090073 if ((anon || no_dso) && type == MAP__FUNCTION) {
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -030074 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020075 filename = newfilename;
76 }
77
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020078 if (vdso) {
79 pgoff = 0;
80 dso = vdso__dso_findnew(dsos__list);
81 } else
82 dso = __dsos__findnew(dsos__list, filename);
83
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020084 if (dso == NULL)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020085 goto out_delete;
86
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030087 map__init(map, type, start, start + len, pgoff, dso);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020088
Jiri Olsa87ffef72011-08-24 15:18:34 +020089 if (anon || no_dso) {
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030090 map->map_ip = map->unmap_ip = identity__map_ip;
Jiri Olsa87ffef72011-08-24 15:18:34 +020091
92 /*
93 * Set memory without DSO as loaded. All map__find_*
94 * functions still return NULL, and we avoid the
95 * unnecessary map__load warning.
96 */
Namhyung Kim578c03c2014-01-16 10:49:31 +090097 if (type != MAP__FUNCTION)
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -030098 dso__set_loaded(dso, map->type);
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -020099 }
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200100 }
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300101 return map;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200102out_delete:
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300103 free(map);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200104 return NULL;
105}
106
Namhyung Kime5a18452012-08-06 13:41:20 +0900107/*
108 * Constructor variant for modules (where we know from /proc/modules where
109 * they are loaded) and for vmlinux, where only after we load all the
110 * symbols we'll know where it starts and ends.
111 */
112struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
113{
114 struct map *map = calloc(1, (sizeof(*map) +
115 (dso->kernel ? sizeof(struct kmap) : 0)));
116 if (map != NULL) {
117 /*
118 * ->end will be filled after we load all the symbols
119 */
120 map__init(map, type, start, 0, 0, dso);
121 }
122
123 return map;
124}
125
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300126void map__delete(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200127{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300128 free(map);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200129}
130
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300131void map__fixup_start(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_first(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->start = sym->start;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200138 }
139}
140
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300141void map__fixup_end(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200142{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300143 struct rb_root *symbols = &map->dso->symbols[map->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200144 struct rb_node *nd = rb_last(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200145 if (nd != NULL) {
146 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300147 map->end = sym->end;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200148 }
149}
150
Arnaldo Carvalho de Melod70a5402009-10-30 16:28:25 -0200151#define DSO__DELETED "(deleted)"
152
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300153int map__load(struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200154{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300155 const char *name = map->dso->long_name;
Masami Hiramatsua1281682009-12-15 10:32:33 -0500156 int nr;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200157
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300158 if (dso__loaded(map->dso, map->type))
Masami Hiramatsua1281682009-12-15 10:32:33 -0500159 return 0;
160
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300161 nr = dso__load(map->dso, map, filter);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200162 if (nr < 0) {
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300163 if (map->dso->has_build_id) {
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200164 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
165
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300166 build_id__sprintf(map->dso->build_id,
167 sizeof(map->dso->build_id),
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200168 sbuild_id);
169 pr_warning("%s with build id %s not found",
170 name, sbuild_id);
171 } else
172 pr_warning("Failed to open %s", name);
173
174 pr_warning(", continuing without symbols\n");
175 return -1;
176 } else if (nr == 0) {
Ingo Molnar89fe8082013-09-30 12:07:11 +0200177#ifdef HAVE_LIBELF_SUPPORT
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200178 const size_t len = strlen(name);
179 const size_t real_len = len - sizeof(DSO__DELETED);
180
181 if (len > sizeof(DSO__DELETED) &&
182 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
David Aherne77b15b2011-10-18 18:44:45 -0600183 pr_warning("%.*s was updated (is prelink enabled?). "
184 "Restart the long running apps that use it!\n",
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200185 (int)real_len, name);
186 } else {
187 pr_warning("no symbols found in %s, maybe install "
188 "a debug package?\n", name);
189 }
Namhyung Kim393be2e2012-08-06 13:41:21 +0900190#endif
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200191 return -1;
192 }
193
194 return 0;
195}
196
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300197struct symbol *map__find_symbol(struct map *map, u64 addr,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200198 symbol_filter_t filter)
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200199{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300200 if (map__load(map, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200201 return NULL;
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200202
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300203 return dso__find_symbol(map->dso, map->type, addr);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200204}
205
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300206struct symbol *map__find_symbol_by_name(struct map *map, const char *name,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200207 symbol_filter_t filter)
208{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300209 if (map__load(map, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200210 return NULL;
211
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300212 if (!dso__sorted_by_name(map->dso, map->type))
213 dso__sort_by_name(map->dso, map->type);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200214
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300215 return dso__find_symbol_by_name(map->dso, map->type, name);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200216}
217
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300218struct map *map__clone(struct map *map)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200219{
Arnaldo Carvalho de Melo8e160172013-01-24 22:16:43 -0300220 return memdup(map, sizeof(*map));
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200221}
222
223int map__overlap(struct map *l, struct map *r)
224{
225 if (l->start > r->start) {
226 struct map *t = l;
227 l = r;
228 r = t;
229 }
230
231 if (l->end > r->start)
232 return 1;
233
234 return 0;
235}
236
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300237size_t map__fprintf(struct map *map, FILE *fp)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200238{
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200239 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300240 map->start, map->end, map->pgoff, map->dso->name);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200241}
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200242
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900243size_t map__fprintf_dsoname(struct map *map, FILE *fp)
244{
Feng Tang8f28f192012-08-27 15:38:26 +0800245 const char *dsoname = "[unknown]";
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900246
Akihiro Nagai0bc8d202012-01-30 13:43:20 +0900247 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
248 if (symbol_conf.show_kernel_path && map->dso->long_name)
249 dsoname = map->dso->long_name;
250 else if (map->dso->name)
251 dsoname = map->dso->name;
Feng Tang8f28f192012-08-27 15:38:26 +0800252 }
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900253
254 return fprintf(fp, "%s", dsoname);
255}
256
Adrian Huntercc8fae12013-12-06 09:42:57 +0200257int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
258 FILE *fp)
259{
260 char *srcline;
261 int ret = 0;
262
263 if (map && map->dso) {
264 srcline = get_srcline(map->dso,
265 map__rip_2objdump(map, addr));
266 if (srcline != SRCLINE_UNKNOWN)
267 ret = fprintf(fp, "%s%s", prefix, srcline);
268 free_srcline(srcline);
269 }
270 return ret;
271}
272
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300273/**
274 * map__rip_2objdump - convert symbol start address to objdump address.
275 * @map: memory map
276 * @rip: symbol start address
277 *
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200278 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300279 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
280 * relative to section start.
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300281 *
282 * Return: Address suitable for passing to "objdump --start-address="
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200283 */
284u64 map__rip_2objdump(struct map *map, u64 rip)
285{
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300286 if (!map->dso->adjust_symbols)
287 return rip;
288
289 if (map->dso->rel)
290 return rip - map->pgoff;
291
Adrian Hunter91767532014-01-29 16:14:36 +0200292 return map->unmap_ip(map, rip) - map->reloc;
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200293}
Kirill Smelkovee11b902010-02-07 11:46:15 -0200294
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300295/**
296 * map__objdump_2mem - convert objdump address to a memory address.
297 * @map: memory map
298 * @ip: objdump address
299 *
300 * Closely related to map__rip_2objdump(), this function takes an address from
301 * objdump and converts it to a memory address. Note this assumes that @map
302 * contains the address. To be sure the result is valid, check it forwards
303 * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
304 *
305 * Return: Memory address.
306 */
307u64 map__objdump_2mem(struct map *map, u64 ip)
308{
309 if (!map->dso->adjust_symbols)
310 return map->unmap_ip(map, ip);
311
312 if (map->dso->rel)
313 return map->unmap_ip(map, ip + map->pgoff);
314
Adrian Hunter91767532014-01-29 16:14:36 +0200315 return ip + map->reloc;
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300316}
317
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300318void map_groups__init(struct map_groups *mg)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300319{
320 int i;
321 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300322 mg->maps[i] = RB_ROOT;
323 INIT_LIST_HEAD(&mg->removed_maps[i]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300324 }
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300325 mg->machine = NULL;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300326}
327
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300328static void maps__delete(struct rb_root *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300329{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300330 struct rb_node *next = rb_first(maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300331
332 while (next) {
333 struct map *pos = rb_entry(next, struct map, rb_node);
334
335 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300336 rb_erase(&pos->rb_node, maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300337 map__delete(pos);
338 }
339}
340
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300341static void maps__delete_removed(struct list_head *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300342{
343 struct map *pos, *n;
344
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300345 list_for_each_entry_safe(pos, n, maps, node) {
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300346 list_del(&pos->node);
347 map__delete(pos);
348 }
349}
350
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300351void map_groups__exit(struct map_groups *mg)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300352{
353 int i;
354
355 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300356 maps__delete(&mg->maps[i]);
357 maps__delete_removed(&mg->removed_maps[i]);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300358 }
359}
360
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300361struct map_groups *map_groups__new(void)
362{
363 struct map_groups *mg = malloc(sizeof(*mg));
364
365 if (mg != NULL)
366 map_groups__init(mg);
367
368 return mg;
369}
370
371void map_groups__delete(struct map_groups *mg)
372{
373 map_groups__exit(mg);
374 free(mg);
375}
376
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300377void map_groups__flush(struct map_groups *mg)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300378{
379 int type;
380
381 for (type = 0; type < MAP__NR_TYPES; type++) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300382 struct rb_root *root = &mg->maps[type];
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300383 struct rb_node *next = rb_first(root);
384
385 while (next) {
386 struct map *pos = rb_entry(next, struct map, rb_node);
387 next = rb_next(&pos->rb_node);
388 rb_erase(&pos->rb_node, root);
389 /*
390 * We may have references to this map, for
391 * instance in some hist_entry instances, so
392 * just move them to a separate list.
393 */
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300394 list_add_tail(&pos->node, &mg->removed_maps[pos->type]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300395 }
396 }
397}
398
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300399struct symbol *map_groups__find_symbol(struct map_groups *mg,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300400 enum map_type type, u64 addr,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300401 struct map **mapp,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300402 symbol_filter_t filter)
403{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300404 struct map *map = map_groups__find(mg, type, addr);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300405
Masami Hiramatsu4afc81c2014-01-23 02:29:50 +0000406 /* Ensure map is loaded before using map->map_ip */
407 if (map != NULL && map__load(map, filter) >= 0) {
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300408 if (mapp != NULL)
409 *mapp = map;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300410 return map__find_symbol(map, map->map_ip(map, addr), filter);
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300411 }
412
413 return NULL;
414}
415
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300416struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300417 enum map_type type,
418 const char *name,
419 struct map **mapp,
420 symbol_filter_t filter)
421{
422 struct rb_node *nd;
423
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300424 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300425 struct map *pos = rb_entry(nd, struct map, rb_node);
426 struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
427
428 if (sym == NULL)
429 continue;
430 if (mapp != NULL)
431 *mapp = pos;
432 return sym;
433 }
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300434
435 return NULL;
436}
437
Arnaldo Carvalho de Melo4e987712013-10-14 13:43:38 +0300438int map_groups__find_ams(struct addr_map_symbol *ams, symbol_filter_t filter)
439{
440 if (ams->addr < ams->map->start || ams->addr > ams->map->end) {
441 if (ams->map->groups == NULL)
442 return -1;
443 ams->map = map_groups__find(ams->map->groups, ams->map->type,
444 ams->addr);
445 if (ams->map == NULL)
446 return -1;
447 }
448
449 ams->al_addr = ams->map->map_ip(ams->map, ams->addr);
450 ams->sym = map__find_symbol(ams->map, ams->al_addr, filter);
451
452 return ams->sym ? 0 : -1;
453}
454
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300455size_t __map_groups__fprintf_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300456 enum map_type type, int verbose, FILE *fp)
457{
458 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
459 struct rb_node *nd;
460
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300461 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300462 struct map *pos = rb_entry(nd, struct map, rb_node);
463 printed += fprintf(fp, "Map:");
464 printed += map__fprintf(pos, fp);
465 if (verbose > 2) {
466 printed += dso__fprintf(pos->dso, type, fp);
467 printed += fprintf(fp, "--\n");
468 }
469 }
470
471 return printed;
472}
473
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300474size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300475{
476 size_t printed = 0, i;
477 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300478 printed += __map_groups__fprintf_maps(mg, i, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300479 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 enum map_type type,
484 int verbose, FILE *fp)
485{
486 struct map *pos;
487 size_t printed = 0;
488
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300489 list_for_each_entry(pos, &mg->removed_maps[type], node) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300490 printed += fprintf(fp, "Map:");
491 printed += map__fprintf(pos, fp);
492 if (verbose > 1) {
493 printed += dso__fprintf(pos->dso, type, fp);
494 printed += fprintf(fp, "--\n");
495 }
496 }
497 return printed;
498}
499
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300500static size_t map_groups__fprintf_removed_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300501 int verbose, FILE *fp)
502{
503 size_t printed = 0, i;
504 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300505 printed += __map_groups__fprintf_removed_maps(mg, i, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300506 return printed;
507}
508
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300509size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300510{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300511 size_t printed = map_groups__fprintf_maps(mg, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300512 printed += fprintf(fp, "Removed maps:\n");
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300513 return printed + map_groups__fprintf_removed_maps(mg, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300514}
515
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300516int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300517 int verbose, FILE *fp)
518{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300519 struct rb_root *root = &mg->maps[map->type];
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300520 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300521 int err = 0;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300522
523 while (next) {
524 struct map *pos = rb_entry(next, struct map, rb_node);
525 next = rb_next(&pos->rb_node);
526
527 if (!map__overlap(pos, map))
528 continue;
529
530 if (verbose >= 2) {
531 fputs("overlapping maps:\n", fp);
532 map__fprintf(map, fp);
533 map__fprintf(pos, fp);
534 }
535
536 rb_erase(&pos->rb_node, root);
537 /*
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300538 * Now check if we need to create new maps for areas not
539 * overlapped by the new map:
540 */
541 if (map->start > pos->start) {
542 struct map *before = map__clone(pos);
543
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300544 if (before == NULL) {
545 err = -ENOMEM;
546 goto move_map;
547 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300548
549 before->end = map->start - 1;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300550 map_groups__insert(mg, before);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300551 if (verbose >= 2)
552 map__fprintf(before, fp);
553 }
554
555 if (map->end < pos->end) {
556 struct map *after = map__clone(pos);
557
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300558 if (after == NULL) {
559 err = -ENOMEM;
560 goto move_map;
561 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300562
563 after->start = map->end + 1;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300564 map_groups__insert(mg, after);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300565 if (verbose >= 2)
566 map__fprintf(after, fp);
567 }
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300568move_map:
569 /*
570 * If we have references, just move them to a separate list.
571 */
572 if (pos->referenced)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300573 list_add_tail(&pos->node, &mg->removed_maps[map->type]);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300574 else
575 map__delete(pos);
576
577 if (err)
578 return err;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300579 }
580
581 return 0;
582}
583
584/*
585 * XXX This should not really _copy_ te maps, but refcount them.
586 */
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300587int map_groups__clone(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300588 struct map_groups *parent, enum map_type type)
589{
590 struct rb_node *nd;
591 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
592 struct map *map = rb_entry(nd, struct map, rb_node);
593 struct map *new = map__clone(map);
594 if (new == NULL)
595 return -ENOMEM;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300596 map_groups__insert(mg, new);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300597 }
598 return 0;
599}
600
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300601void maps__insert(struct rb_root *maps, struct map *map)
602{
603 struct rb_node **p = &maps->rb_node;
604 struct rb_node *parent = NULL;
605 const u64 ip = map->start;
606 struct map *m;
607
608 while (*p != NULL) {
609 parent = *p;
610 m = rb_entry(parent, struct map, rb_node);
611 if (ip < m->start)
612 p = &(*p)->rb_left;
613 else
614 p = &(*p)->rb_right;
615 }
616
617 rb_link_node(&map->rb_node, parent, p);
618 rb_insert_color(&map->rb_node, maps);
619}
620
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300621void maps__remove(struct rb_root *maps, struct map *map)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300622{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300623 rb_erase(&map->rb_node, maps);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300624}
625
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300626struct map *maps__find(struct rb_root *maps, u64 ip)
627{
628 struct rb_node **p = &maps->rb_node;
629 struct rb_node *parent = NULL;
630 struct map *m;
631
632 while (*p != NULL) {
633 parent = *p;
634 m = rb_entry(parent, struct map, rb_node);
635 if (ip < m->start)
636 p = &(*p)->rb_left;
637 else if (ip > m->end)
638 p = &(*p)->rb_right;
639 else
640 return m;
641 }
642
643 return NULL;
644}
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300645
646struct map *maps__first(struct rb_root *maps)
647{
648 struct rb_node *first = rb_first(maps);
649
650 if (first)
651 return rb_entry(first, struct map, rb_node);
652 return NULL;
653}
654
655struct map *maps__next(struct map *map)
656{
657 struct rb_node *next = rb_next(&map->rb_node);
658
659 if (next)
660 return rb_entry(next, struct map, rb_node);
661 return NULL;
662}