blob: dffc8dce604676ec3146c75b427e3676f5db14b0 [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"
Jiri Olsaacebd402014-07-14 23:46:47 +020015#include "debug.h"
Adrian Hunter2a030682014-07-22 16:17:53 +030016#include "machine.h"
Arnaldo Carvalho de Melo8e16017d2013-01-24 22:16:43 -030017#include <linux/string.h>
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020018
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -030019const char *map_type__name[MAP__NR_TYPES] = {
20 [MAP__FUNCTION] = "Functions",
21 [MAP__VARIABLE] = "Variables",
22};
23
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020024static inline int is_anon_memory(const char *filename)
25{
Joshua Zhud0528b52013-01-05 13:29:57 +080026 return !strcmp(filename, "//anon") ||
Andi Kleen89365e62013-04-24 17:03:02 -070027 !strcmp(filename, "/dev/zero (deleted)") ||
Joshua Zhud0528b52013-01-05 13:29:57 +080028 !strcmp(filename, "/anon_hugepage (deleted)");
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020029}
30
Jiri Olsa87ffef72011-08-24 15:18:34 +020031static inline int is_no_dso_memory(const char *filename)
32{
Namhyung Kim1e825742012-11-07 16:27:11 +090033 return !strncmp(filename, "[stack", 6) ||
Jiri Olsa87ffef72011-08-24 15:18:34 +020034 !strcmp(filename, "[heap]");
35}
36
Michael Lentineeca81832014-05-20 11:48:50 +020037static inline int is_android_lib(const char *filename)
38{
39 return !strncmp(filename, "/data/app-lib", 13) ||
40 !strncmp(filename, "/system/lib", 11);
41}
42
43static inline bool replace_android_lib(const char *filename, char *newfilename)
44{
45 const char *libname;
46 char *app_abi;
47 size_t app_abi_length, new_length;
48 size_t lib_length = 0;
49
50 libname = strrchr(filename, '/');
51 if (libname)
52 lib_length = strlen(libname);
53
54 app_abi = getenv("APP_ABI");
55 if (!app_abi)
56 return false;
57
58 app_abi_length = strlen(app_abi);
59
60 if (!strncmp(filename, "/data/app-lib", 13)) {
61 char *apk_path;
62
63 if (!app_abi_length)
64 return false;
65
66 new_length = 7 + app_abi_length + lib_length;
67
68 apk_path = getenv("APK_PATH");
69 if (apk_path) {
70 new_length += strlen(apk_path) + 1;
71 if (new_length > PATH_MAX)
72 return false;
73 snprintf(newfilename, new_length,
74 "%s/libs/%s/%s", apk_path, app_abi, libname);
75 } else {
76 if (new_length > PATH_MAX)
77 return false;
78 snprintf(newfilename, new_length,
79 "libs/%s/%s", app_abi, libname);
80 }
81 return true;
82 }
83
84 if (!strncmp(filename, "/system/lib/", 11)) {
85 char *ndk, *app;
86 const char *arch;
87 size_t ndk_length;
88 size_t app_length;
89
90 ndk = getenv("NDK_ROOT");
91 app = getenv("APP_PLATFORM");
92
93 if (!(ndk && app))
94 return false;
95
96 ndk_length = strlen(ndk);
97 app_length = strlen(app);
98
99 if (!(ndk_length && app_length && app_abi_length))
100 return false;
101
102 arch = !strncmp(app_abi, "arm", 3) ? "arm" :
103 !strncmp(app_abi, "mips", 4) ? "mips" :
104 !strncmp(app_abi, "x86", 3) ? "x86" : NULL;
105
106 if (!arch)
107 return false;
108
109 new_length = 27 + ndk_length +
110 app_length + lib_length
111 + strlen(arch);
112
113 if (new_length > PATH_MAX)
114 return false;
115 snprintf(newfilename, new_length,
116 "%s/platforms/%s/arch-%s/usr/lib/%s",
117 ndk, app, arch, libname);
118
119 return true;
120 }
121 return false;
122}
123
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300124void map__init(struct map *map, enum map_type type,
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -0200125 u64 start, u64 end, u64 pgoff, struct dso *dso)
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200126{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300127 map->type = type;
128 map->start = start;
129 map->end = end;
130 map->pgoff = pgoff;
Adrian Hunter91767532014-01-29 16:14:36 +0200131 map->reloc = 0;
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300132 map->dso = dso;
133 map->map_ip = map__map_ip;
134 map->unmap_ip = map__unmap_ip;
135 RB_CLEAR_NODE(&map->rb_node);
136 map->groups = NULL;
137 map->referenced = false;
138 map->erange_warned = false;
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200139}
140
Adrian Hunter2a030682014-07-22 16:17:53 +0300141struct map *map__new(struct machine *machine, u64 start, u64 len,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200142 u64 pgoff, u32 pid, u32 d_maj, u32 d_min, u64 ino,
Don Zickus7ef80702014-05-19 15:13:49 -0400143 u64 ino_gen, u32 prot, u32 flags, char *filename,
Dave Martin361d1342010-07-27 16:40:02 +0100144 enum map_type type)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200145{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300146 struct map *map = malloc(sizeof(*map));
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200147
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300148 if (map != NULL) {
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200149 char newfilename[PATH_MAX];
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200150 struct dso *dso;
Michael Lentineeca81832014-05-20 11:48:50 +0200151 int anon, no_dso, vdso, android;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200152
Michael Lentineeca81832014-05-20 11:48:50 +0200153 android = is_android_lib(filename);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200154 anon = is_anon_memory(filename);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200155 vdso = is_vdso_map(filename);
Jiri Olsa87ffef72011-08-24 15:18:34 +0200156 no_dso = is_no_dso_memory(filename);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200157
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200158 map->maj = d_maj;
159 map->min = d_min;
160 map->ino = ino;
161 map->ino_generation = ino_gen;
Don Zickus7ef80702014-05-19 15:13:49 -0400162 map->prot = prot;
163 map->flags = flags;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200164
Namhyung Kim578c03c2014-01-16 10:49:31 +0900165 if ((anon || no_dso) && type == MAP__FUNCTION) {
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -0300166 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200167 filename = newfilename;
168 }
169
Michael Lentineeca81832014-05-20 11:48:50 +0200170 if (android) {
171 if (replace_android_lib(filename, newfilename))
172 filename = newfilename;
173 }
174
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200175 if (vdso) {
176 pgoff = 0;
Adrian Hunter2a030682014-07-22 16:17:53 +0300177 dso = vdso__dso_findnew(machine);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200178 } else
Adrian Hunter2a030682014-07-22 16:17:53 +0300179 dso = __dsos__findnew(&machine->user_dsos, filename);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200180
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200181 if (dso == NULL)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200182 goto out_delete;
183
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300184 map__init(map, type, start, start + len, pgoff, dso);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200185
Jiri Olsa87ffef72011-08-24 15:18:34 +0200186 if (anon || no_dso) {
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300187 map->map_ip = map->unmap_ip = identity__map_ip;
Jiri Olsa87ffef72011-08-24 15:18:34 +0200188
189 /*
190 * Set memory without DSO as loaded. All map__find_*
191 * functions still return NULL, and we avoid the
192 * unnecessary map__load warning.
193 */
Namhyung Kim578c03c2014-01-16 10:49:31 +0900194 if (type != MAP__FUNCTION)
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300195 dso__set_loaded(dso, map->type);
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -0200196 }
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200197 }
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300198 return map;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200199out_delete:
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300200 free(map);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200201 return NULL;
202}
203
Namhyung Kime5a18452012-08-06 13:41:20 +0900204/*
205 * Constructor variant for modules (where we know from /proc/modules where
206 * they are loaded) and for vmlinux, where only after we load all the
207 * symbols we'll know where it starts and ends.
208 */
209struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
210{
211 struct map *map = calloc(1, (sizeof(*map) +
212 (dso->kernel ? sizeof(struct kmap) : 0)));
213 if (map != NULL) {
214 /*
215 * ->end will be filled after we load all the symbols
216 */
217 map__init(map, type, start, 0, 0, dso);
218 }
219
220 return map;
221}
222
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300223void map__delete(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200224{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300225 free(map);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200226}
227
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300228void map__fixup_start(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200229{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300230 struct rb_root *symbols = &map->dso->symbols[map->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200231 struct rb_node *nd = rb_first(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200232 if (nd != NULL) {
233 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300234 map->start = sym->start;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200235 }
236}
237
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300238void map__fixup_end(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200239{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300240 struct rb_root *symbols = &map->dso->symbols[map->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200241 struct rb_node *nd = rb_last(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200242 if (nd != NULL) {
243 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300244 map->end = sym->end;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200245 }
246}
247
Arnaldo Carvalho de Melod70a5402009-10-30 16:28:25 -0200248#define DSO__DELETED "(deleted)"
249
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300250int map__load(struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200251{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300252 const char *name = map->dso->long_name;
Masami Hiramatsua1281682009-12-15 10:32:33 -0500253 int nr;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200254
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300255 if (dso__loaded(map->dso, map->type))
Masami Hiramatsua1281682009-12-15 10:32:33 -0500256 return 0;
257
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300258 nr = dso__load(map->dso, map, filter);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200259 if (nr < 0) {
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300260 if (map->dso->has_build_id) {
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200261 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
262
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300263 build_id__sprintf(map->dso->build_id,
264 sizeof(map->dso->build_id),
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200265 sbuild_id);
266 pr_warning("%s with build id %s not found",
267 name, sbuild_id);
268 } else
269 pr_warning("Failed to open %s", name);
270
271 pr_warning(", continuing without symbols\n");
272 return -1;
273 } else if (nr == 0) {
Ingo Molnar89fe8082013-09-30 12:07:11 +0200274#ifdef HAVE_LIBELF_SUPPORT
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200275 const size_t len = strlen(name);
276 const size_t real_len = len - sizeof(DSO__DELETED);
277
278 if (len > sizeof(DSO__DELETED) &&
279 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
David Aherne77b15b2011-10-18 18:44:45 -0600280 pr_warning("%.*s was updated (is prelink enabled?). "
281 "Restart the long running apps that use it!\n",
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200282 (int)real_len, name);
283 } else {
284 pr_warning("no symbols found in %s, maybe install "
285 "a debug package?\n", name);
286 }
Namhyung Kim393be2e2012-08-06 13:41:21 +0900287#endif
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200288 return -1;
289 }
290
291 return 0;
292}
293
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300294struct symbol *map__find_symbol(struct map *map, u64 addr,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200295 symbol_filter_t filter)
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200296{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300297 if (map__load(map, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200298 return NULL;
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200299
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300300 return dso__find_symbol(map->dso, map->type, addr);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200301}
302
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300303struct symbol *map__find_symbol_by_name(struct map *map, const char *name,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200304 symbol_filter_t filter)
305{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300306 if (map__load(map, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200307 return NULL;
308
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300309 if (!dso__sorted_by_name(map->dso, map->type))
310 dso__sort_by_name(map->dso, map->type);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200311
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300312 return dso__find_symbol_by_name(map->dso, map->type, name);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200313}
314
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300315struct map *map__clone(struct map *map)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200316{
Arnaldo Carvalho de Melo8e16017d2013-01-24 22:16:43 -0300317 return memdup(map, sizeof(*map));
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200318}
319
320int map__overlap(struct map *l, struct map *r)
321{
322 if (l->start > r->start) {
323 struct map *t = l;
324 l = r;
325 r = t;
326 }
327
328 if (l->end > r->start)
329 return 1;
330
331 return 0;
332}
333
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300334size_t map__fprintf(struct map *map, FILE *fp)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200335{
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200336 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300337 map->start, map->end, map->pgoff, map->dso->name);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200338}
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200339
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900340size_t map__fprintf_dsoname(struct map *map, FILE *fp)
341{
Feng Tang8f28f192012-08-27 15:38:26 +0800342 const char *dsoname = "[unknown]";
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900343
Akihiro Nagai0bc8d202012-01-30 13:43:20 +0900344 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
345 if (symbol_conf.show_kernel_path && map->dso->long_name)
346 dsoname = map->dso->long_name;
347 else if (map->dso->name)
348 dsoname = map->dso->name;
Feng Tang8f28f192012-08-27 15:38:26 +0800349 }
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900350
351 return fprintf(fp, "%s", dsoname);
352}
353
Adrian Huntercc8fae12013-12-06 09:42:57 +0200354int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
355 FILE *fp)
356{
357 char *srcline;
358 int ret = 0;
359
360 if (map && map->dso) {
361 srcline = get_srcline(map->dso,
362 map__rip_2objdump(map, addr));
363 if (srcline != SRCLINE_UNKNOWN)
364 ret = fprintf(fp, "%s%s", prefix, srcline);
365 free_srcline(srcline);
366 }
367 return ret;
368}
369
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300370/**
371 * map__rip_2objdump - convert symbol start address to objdump address.
372 * @map: memory map
373 * @rip: symbol start address
374 *
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200375 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300376 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
377 * relative to section start.
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300378 *
379 * Return: Address suitable for passing to "objdump --start-address="
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200380 */
381u64 map__rip_2objdump(struct map *map, u64 rip)
382{
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300383 if (!map->dso->adjust_symbols)
384 return rip;
385
386 if (map->dso->rel)
387 return rip - map->pgoff;
388
Adrian Hunter91767532014-01-29 16:14:36 +0200389 return map->unmap_ip(map, rip) - map->reloc;
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200390}
Kirill Smelkovee11b902010-02-07 11:46:15 -0200391
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300392/**
393 * map__objdump_2mem - convert objdump address to a memory address.
394 * @map: memory map
395 * @ip: objdump address
396 *
397 * Closely related to map__rip_2objdump(), this function takes an address from
398 * objdump and converts it to a memory address. Note this assumes that @map
399 * contains the address. To be sure the result is valid, check it forwards
400 * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
401 *
402 * Return: Memory address.
403 */
404u64 map__objdump_2mem(struct map *map, u64 ip)
405{
406 if (!map->dso->adjust_symbols)
407 return map->unmap_ip(map, ip);
408
409 if (map->dso->rel)
410 return map->unmap_ip(map, ip + map->pgoff);
411
Adrian Hunter91767532014-01-29 16:14:36 +0200412 return ip + map->reloc;
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300413}
414
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300415void map_groups__init(struct map_groups *mg)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300416{
417 int i;
418 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300419 mg->maps[i] = RB_ROOT;
420 INIT_LIST_HEAD(&mg->removed_maps[i]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300421 }
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300422 mg->machine = NULL;
Arnaldo Carvalho de Meloa26ca672014-03-25 15:26:44 -0300423 mg->refcnt = 1;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300424}
425
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300426static void maps__delete(struct rb_root *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300427{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300428 struct rb_node *next = rb_first(maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300429
430 while (next) {
431 struct map *pos = rb_entry(next, struct map, rb_node);
432
433 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300434 rb_erase(&pos->rb_node, maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300435 map__delete(pos);
436 }
437}
438
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300439static void maps__delete_removed(struct list_head *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300440{
441 struct map *pos, *n;
442
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300443 list_for_each_entry_safe(pos, n, maps, node) {
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300444 list_del(&pos->node);
445 map__delete(pos);
446 }
447}
448
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300449void map_groups__exit(struct map_groups *mg)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300450{
451 int i;
452
453 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300454 maps__delete(&mg->maps[i]);
455 maps__delete_removed(&mg->removed_maps[i]);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300456 }
457}
458
Adrian Hunter29ce3612014-07-16 11:07:13 +0300459bool map_groups__empty(struct map_groups *mg)
460{
461 int i;
462
463 for (i = 0; i < MAP__NR_TYPES; ++i) {
464 if (maps__first(&mg->maps[i]))
465 return false;
466 if (!list_empty(&mg->removed_maps[i]))
467 return false;
468 }
469
470 return true;
471}
472
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300473struct map_groups *map_groups__new(void)
474{
475 struct map_groups *mg = malloc(sizeof(*mg));
476
477 if (mg != NULL)
478 map_groups__init(mg);
479
480 return mg;
481}
482
483void map_groups__delete(struct map_groups *mg)
484{
485 map_groups__exit(mg);
486 free(mg);
487}
488
Arnaldo Carvalho de Meloa26ca672014-03-25 15:26:44 -0300489void map_groups__put(struct map_groups *mg)
490{
491 if (--mg->refcnt == 0)
492 map_groups__delete(mg);
493}
494
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300495void map_groups__flush(struct map_groups *mg)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300496{
497 int type;
498
499 for (type = 0; type < MAP__NR_TYPES; type++) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300500 struct rb_root *root = &mg->maps[type];
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300501 struct rb_node *next = rb_first(root);
502
503 while (next) {
504 struct map *pos = rb_entry(next, struct map, rb_node);
505 next = rb_next(&pos->rb_node);
506 rb_erase(&pos->rb_node, root);
507 /*
508 * We may have references to this map, for
509 * instance in some hist_entry instances, so
510 * just move them to a separate list.
511 */
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300512 list_add_tail(&pos->node, &mg->removed_maps[pos->type]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300513 }
514 }
515}
516
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300517struct symbol *map_groups__find_symbol(struct map_groups *mg,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300518 enum map_type type, u64 addr,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300519 struct map **mapp,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300520 symbol_filter_t filter)
521{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300522 struct map *map = map_groups__find(mg, type, addr);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300523
Masami Hiramatsu4afc81c2014-01-23 02:29:50 +0000524 /* Ensure map is loaded before using map->map_ip */
525 if (map != NULL && map__load(map, filter) >= 0) {
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300526 if (mapp != NULL)
527 *mapp = map;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300528 return map__find_symbol(map, map->map_ip(map, addr), filter);
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300529 }
530
531 return NULL;
532}
533
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300534struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300535 enum map_type type,
536 const char *name,
537 struct map **mapp,
538 symbol_filter_t filter)
539{
540 struct rb_node *nd;
541
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300542 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300543 struct map *pos = rb_entry(nd, struct map, rb_node);
544 struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
545
546 if (sym == NULL)
547 continue;
548 if (mapp != NULL)
549 *mapp = pos;
550 return sym;
551 }
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300552
553 return NULL;
554}
555
Arnaldo Carvalho de Melo4e987712013-10-14 13:43:38 +0300556int map_groups__find_ams(struct addr_map_symbol *ams, symbol_filter_t filter)
557{
558 if (ams->addr < ams->map->start || ams->addr > ams->map->end) {
559 if (ams->map->groups == NULL)
560 return -1;
561 ams->map = map_groups__find(ams->map->groups, ams->map->type,
562 ams->addr);
563 if (ams->map == NULL)
564 return -1;
565 }
566
567 ams->al_addr = ams->map->map_ip(ams->map, ams->addr);
568 ams->sym = map__find_symbol(ams->map, ams->al_addr, filter);
569
570 return ams->sym ? 0 : -1;
571}
572
Jiri Olsaacebd402014-07-14 23:46:47 +0200573size_t __map_groups__fprintf_maps(struct map_groups *mg, enum map_type type,
574 FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300575{
576 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
577 struct rb_node *nd;
578
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300579 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300580 struct map *pos = rb_entry(nd, struct map, rb_node);
581 printed += fprintf(fp, "Map:");
582 printed += map__fprintf(pos, fp);
583 if (verbose > 2) {
584 printed += dso__fprintf(pos->dso, type, fp);
585 printed += fprintf(fp, "--\n");
586 }
587 }
588
589 return printed;
590}
591
Jiri Olsaacebd402014-07-14 23:46:47 +0200592static size_t map_groups__fprintf_maps(struct map_groups *mg, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300593{
594 size_t printed = 0, i;
595 for (i = 0; i < MAP__NR_TYPES; ++i)
Jiri Olsaacebd402014-07-14 23:46:47 +0200596 printed += __map_groups__fprintf_maps(mg, i, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300597 return printed;
598}
599
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300600static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg,
Jiri Olsaacebd402014-07-14 23:46:47 +0200601 enum map_type type, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300602{
603 struct map *pos;
604 size_t printed = 0;
605
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300606 list_for_each_entry(pos, &mg->removed_maps[type], node) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300607 printed += fprintf(fp, "Map:");
608 printed += map__fprintf(pos, fp);
609 if (verbose > 1) {
610 printed += dso__fprintf(pos->dso, type, fp);
611 printed += fprintf(fp, "--\n");
612 }
613 }
614 return printed;
615}
616
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300617static size_t map_groups__fprintf_removed_maps(struct map_groups *mg,
Jiri Olsaacebd402014-07-14 23:46:47 +0200618 FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300619{
620 size_t printed = 0, i;
621 for (i = 0; i < MAP__NR_TYPES; ++i)
Jiri Olsaacebd402014-07-14 23:46:47 +0200622 printed += __map_groups__fprintf_removed_maps(mg, i, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300623 return printed;
624}
625
Jiri Olsaacebd402014-07-14 23:46:47 +0200626size_t map_groups__fprintf(struct map_groups *mg, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300627{
Jiri Olsaacebd402014-07-14 23:46:47 +0200628 size_t printed = map_groups__fprintf_maps(mg, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300629 printed += fprintf(fp, "Removed maps:\n");
Jiri Olsaacebd402014-07-14 23:46:47 +0200630 return printed + map_groups__fprintf_removed_maps(mg, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300631}
632
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300633int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
Jiri Olsaacebd402014-07-14 23:46:47 +0200634 FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300635{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300636 struct rb_root *root = &mg->maps[map->type];
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300637 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300638 int err = 0;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300639
640 while (next) {
641 struct map *pos = rb_entry(next, struct map, rb_node);
642 next = rb_next(&pos->rb_node);
643
644 if (!map__overlap(pos, map))
645 continue;
646
647 if (verbose >= 2) {
648 fputs("overlapping maps:\n", fp);
649 map__fprintf(map, fp);
650 map__fprintf(pos, fp);
651 }
652
653 rb_erase(&pos->rb_node, root);
654 /*
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300655 * Now check if we need to create new maps for areas not
656 * overlapped by the new map:
657 */
658 if (map->start > pos->start) {
659 struct map *before = map__clone(pos);
660
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300661 if (before == NULL) {
662 err = -ENOMEM;
663 goto move_map;
664 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300665
666 before->end = map->start - 1;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300667 map_groups__insert(mg, before);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300668 if (verbose >= 2)
669 map__fprintf(before, fp);
670 }
671
672 if (map->end < pos->end) {
673 struct map *after = map__clone(pos);
674
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300675 if (after == NULL) {
676 err = -ENOMEM;
677 goto move_map;
678 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300679
680 after->start = map->end + 1;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300681 map_groups__insert(mg, after);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300682 if (verbose >= 2)
683 map__fprintf(after, fp);
684 }
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300685move_map:
686 /*
687 * If we have references, just move them to a separate list.
688 */
689 if (pos->referenced)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300690 list_add_tail(&pos->node, &mg->removed_maps[map->type]);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300691 else
692 map__delete(pos);
693
694 if (err)
695 return err;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300696 }
697
698 return 0;
699}
700
701/*
702 * XXX This should not really _copy_ te maps, but refcount them.
703 */
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300704int map_groups__clone(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300705 struct map_groups *parent, enum map_type type)
706{
707 struct rb_node *nd;
708 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
709 struct map *map = rb_entry(nd, struct map, rb_node);
710 struct map *new = map__clone(map);
711 if (new == NULL)
712 return -ENOMEM;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300713 map_groups__insert(mg, new);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300714 }
715 return 0;
716}
717
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300718void maps__insert(struct rb_root *maps, struct map *map)
719{
720 struct rb_node **p = &maps->rb_node;
721 struct rb_node *parent = NULL;
722 const u64 ip = map->start;
723 struct map *m;
724
725 while (*p != NULL) {
726 parent = *p;
727 m = rb_entry(parent, struct map, rb_node);
728 if (ip < m->start)
729 p = &(*p)->rb_left;
730 else
731 p = &(*p)->rb_right;
732 }
733
734 rb_link_node(&map->rb_node, parent, p);
735 rb_insert_color(&map->rb_node, maps);
736}
737
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300738void maps__remove(struct rb_root *maps, struct map *map)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300739{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300740 rb_erase(&map->rb_node, maps);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300741}
742
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300743struct map *maps__find(struct rb_root *maps, u64 ip)
744{
745 struct rb_node **p = &maps->rb_node;
746 struct rb_node *parent = NULL;
747 struct map *m;
748
749 while (*p != NULL) {
750 parent = *p;
751 m = rb_entry(parent, struct map, rb_node);
752 if (ip < m->start)
753 p = &(*p)->rb_left;
754 else if (ip > m->end)
755 p = &(*p)->rb_right;
756 else
757 return m;
758 }
759
760 return NULL;
761}
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300762
763struct map *maps__first(struct rb_root *maps)
764{
765 struct rb_node *first = rb_first(maps);
766
767 if (first)
768 return rb_entry(first, struct map, rb_node);
769 return NULL;
770}
771
772struct map *maps__next(struct map *map)
773{
774 struct rb_node *next = rb_next(&map->rb_node);
775
776 if (next)
777 return rb_entry(next, struct map, rb_node);
778 return NULL;
779}