blob: 8ccbb32eda25341968a32e62c980c7bc6ebc5e09 [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
Michael Lentineeca81832014-05-20 11:48:50 +020035static inline int is_android_lib(const char *filename)
36{
37 return !strncmp(filename, "/data/app-lib", 13) ||
38 !strncmp(filename, "/system/lib", 11);
39}
40
41static inline bool replace_android_lib(const char *filename, char *newfilename)
42{
43 const char *libname;
44 char *app_abi;
45 size_t app_abi_length, new_length;
46 size_t lib_length = 0;
47
48 libname = strrchr(filename, '/');
49 if (libname)
50 lib_length = strlen(libname);
51
52 app_abi = getenv("APP_ABI");
53 if (!app_abi)
54 return false;
55
56 app_abi_length = strlen(app_abi);
57
58 if (!strncmp(filename, "/data/app-lib", 13)) {
59 char *apk_path;
60
61 if (!app_abi_length)
62 return false;
63
64 new_length = 7 + app_abi_length + lib_length;
65
66 apk_path = getenv("APK_PATH");
67 if (apk_path) {
68 new_length += strlen(apk_path) + 1;
69 if (new_length > PATH_MAX)
70 return false;
71 snprintf(newfilename, new_length,
72 "%s/libs/%s/%s", apk_path, app_abi, libname);
73 } else {
74 if (new_length > PATH_MAX)
75 return false;
76 snprintf(newfilename, new_length,
77 "libs/%s/%s", app_abi, libname);
78 }
79 return true;
80 }
81
82 if (!strncmp(filename, "/system/lib/", 11)) {
83 char *ndk, *app;
84 const char *arch;
85 size_t ndk_length;
86 size_t app_length;
87
88 ndk = getenv("NDK_ROOT");
89 app = getenv("APP_PLATFORM");
90
91 if (!(ndk && app))
92 return false;
93
94 ndk_length = strlen(ndk);
95 app_length = strlen(app);
96
97 if (!(ndk_length && app_length && app_abi_length))
98 return false;
99
100 arch = !strncmp(app_abi, "arm", 3) ? "arm" :
101 !strncmp(app_abi, "mips", 4) ? "mips" :
102 !strncmp(app_abi, "x86", 3) ? "x86" : NULL;
103
104 if (!arch)
105 return false;
106
107 new_length = 27 + ndk_length +
108 app_length + lib_length
109 + strlen(arch);
110
111 if (new_length > PATH_MAX)
112 return false;
113 snprintf(newfilename, new_length,
114 "%s/platforms/%s/arch-%s/usr/lib/%s",
115 ndk, app, arch, libname);
116
117 return true;
118 }
119 return false;
120}
121
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300122void map__init(struct map *map, enum map_type type,
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -0200123 u64 start, u64 end, u64 pgoff, struct dso *dso)
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200124{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300125 map->type = type;
126 map->start = start;
127 map->end = end;
128 map->pgoff = pgoff;
Adrian Hunter91767532014-01-29 16:14:36 +0200129 map->reloc = 0;
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300130 map->dso = dso;
131 map->map_ip = map__map_ip;
132 map->unmap_ip = map__unmap_ip;
133 RB_CLEAR_NODE(&map->rb_node);
134 map->groups = NULL;
135 map->referenced = false;
136 map->erange_warned = false;
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200137}
138
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800139struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200140 u64 pgoff, u32 pid, u32 d_maj, u32 d_min, u64 ino,
141 u64 ino_gen, char *filename,
Dave Martin361d1342010-07-27 16:40:02 +0100142 enum map_type type)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200143{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300144 struct map *map = malloc(sizeof(*map));
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200145
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300146 if (map != NULL) {
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200147 char newfilename[PATH_MAX];
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200148 struct dso *dso;
Michael Lentineeca81832014-05-20 11:48:50 +0200149 int anon, no_dso, vdso, android;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200150
Michael Lentineeca81832014-05-20 11:48:50 +0200151 android = is_android_lib(filename);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200152 anon = is_anon_memory(filename);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200153 vdso = is_vdso_map(filename);
Jiri Olsa87ffef72011-08-24 15:18:34 +0200154 no_dso = is_no_dso_memory(filename);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200155
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200156 map->maj = d_maj;
157 map->min = d_min;
158 map->ino = ino;
159 map->ino_generation = ino_gen;
160
Namhyung Kim578c03c2014-01-16 10:49:31 +0900161 if ((anon || no_dso) && type == MAP__FUNCTION) {
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -0300162 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200163 filename = newfilename;
164 }
165
Michael Lentineeca81832014-05-20 11:48:50 +0200166 if (android) {
167 if (replace_android_lib(filename, newfilename))
168 filename = newfilename;
169 }
170
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200171 if (vdso) {
172 pgoff = 0;
173 dso = vdso__dso_findnew(dsos__list);
174 } else
175 dso = __dsos__findnew(dsos__list, filename);
176
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200177 if (dso == NULL)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200178 goto out_delete;
179
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300180 map__init(map, type, start, start + len, pgoff, dso);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200181
Jiri Olsa87ffef72011-08-24 15:18:34 +0200182 if (anon || no_dso) {
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300183 map->map_ip = map->unmap_ip = identity__map_ip;
Jiri Olsa87ffef72011-08-24 15:18:34 +0200184
185 /*
186 * Set memory without DSO as loaded. All map__find_*
187 * functions still return NULL, and we avoid the
188 * unnecessary map__load warning.
189 */
Namhyung Kim578c03c2014-01-16 10:49:31 +0900190 if (type != MAP__FUNCTION)
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300191 dso__set_loaded(dso, map->type);
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -0200192 }
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200193 }
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300194 return map;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200195out_delete:
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300196 free(map);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200197 return NULL;
198}
199
Namhyung Kime5a18452012-08-06 13:41:20 +0900200/*
201 * Constructor variant for modules (where we know from /proc/modules where
202 * they are loaded) and for vmlinux, where only after we load all the
203 * symbols we'll know where it starts and ends.
204 */
205struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
206{
207 struct map *map = calloc(1, (sizeof(*map) +
208 (dso->kernel ? sizeof(struct kmap) : 0)));
209 if (map != NULL) {
210 /*
211 * ->end will be filled after we load all the symbols
212 */
213 map__init(map, type, start, 0, 0, dso);
214 }
215
216 return map;
217}
218
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300219void map__delete(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200220{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300221 free(map);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200222}
223
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300224void map__fixup_start(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200225{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300226 struct rb_root *symbols = &map->dso->symbols[map->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200227 struct rb_node *nd = rb_first(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200228 if (nd != NULL) {
229 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300230 map->start = sym->start;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200231 }
232}
233
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300234void map__fixup_end(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200235{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300236 struct rb_root *symbols = &map->dso->symbols[map->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200237 struct rb_node *nd = rb_last(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200238 if (nd != NULL) {
239 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300240 map->end = sym->end;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200241 }
242}
243
Arnaldo Carvalho de Melod70a5402009-10-30 16:28:25 -0200244#define DSO__DELETED "(deleted)"
245
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300246int map__load(struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200247{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300248 const char *name = map->dso->long_name;
Masami Hiramatsua1281682009-12-15 10:32:33 -0500249 int nr;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200250
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300251 if (dso__loaded(map->dso, map->type))
Masami Hiramatsua1281682009-12-15 10:32:33 -0500252 return 0;
253
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300254 nr = dso__load(map->dso, map, filter);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200255 if (nr < 0) {
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300256 if (map->dso->has_build_id) {
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200257 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
258
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300259 build_id__sprintf(map->dso->build_id,
260 sizeof(map->dso->build_id),
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200261 sbuild_id);
262 pr_warning("%s with build id %s not found",
263 name, sbuild_id);
264 } else
265 pr_warning("Failed to open %s", name);
266
267 pr_warning(", continuing without symbols\n");
268 return -1;
269 } else if (nr == 0) {
Ingo Molnar89fe8082013-09-30 12:07:11 +0200270#ifdef HAVE_LIBELF_SUPPORT
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200271 const size_t len = strlen(name);
272 const size_t real_len = len - sizeof(DSO__DELETED);
273
274 if (len > sizeof(DSO__DELETED) &&
275 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
David Aherne77b15b2011-10-18 18:44:45 -0600276 pr_warning("%.*s was updated (is prelink enabled?). "
277 "Restart the long running apps that use it!\n",
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200278 (int)real_len, name);
279 } else {
280 pr_warning("no symbols found in %s, maybe install "
281 "a debug package?\n", name);
282 }
Namhyung Kim393be2e2012-08-06 13:41:21 +0900283#endif
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200284 return -1;
285 }
286
287 return 0;
288}
289
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300290struct symbol *map__find_symbol(struct map *map, u64 addr,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200291 symbol_filter_t filter)
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200292{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300293 if (map__load(map, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200294 return NULL;
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200295
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300296 return dso__find_symbol(map->dso, map->type, addr);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200297}
298
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300299struct symbol *map__find_symbol_by_name(struct map *map, const char *name,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200300 symbol_filter_t filter)
301{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300302 if (map__load(map, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200303 return NULL;
304
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300305 if (!dso__sorted_by_name(map->dso, map->type))
306 dso__sort_by_name(map->dso, map->type);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200307
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300308 return dso__find_symbol_by_name(map->dso, map->type, name);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200309}
310
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300311struct map *map__clone(struct map *map)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200312{
Arnaldo Carvalho de Melo8e160172013-01-24 22:16:43 -0300313 return memdup(map, sizeof(*map));
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200314}
315
316int map__overlap(struct map *l, struct map *r)
317{
318 if (l->start > r->start) {
319 struct map *t = l;
320 l = r;
321 r = t;
322 }
323
324 if (l->end > r->start)
325 return 1;
326
327 return 0;
328}
329
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300330size_t map__fprintf(struct map *map, FILE *fp)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200331{
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200332 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300333 map->start, map->end, map->pgoff, map->dso->name);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200334}
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200335
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900336size_t map__fprintf_dsoname(struct map *map, FILE *fp)
337{
Feng Tang8f28f192012-08-27 15:38:26 +0800338 const char *dsoname = "[unknown]";
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900339
Akihiro Nagai0bc8d202012-01-30 13:43:20 +0900340 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
341 if (symbol_conf.show_kernel_path && map->dso->long_name)
342 dsoname = map->dso->long_name;
343 else if (map->dso->name)
344 dsoname = map->dso->name;
Feng Tang8f28f192012-08-27 15:38:26 +0800345 }
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900346
347 return fprintf(fp, "%s", dsoname);
348}
349
Adrian Huntercc8fae12013-12-06 09:42:57 +0200350int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
351 FILE *fp)
352{
353 char *srcline;
354 int ret = 0;
355
356 if (map && map->dso) {
357 srcline = get_srcline(map->dso,
358 map__rip_2objdump(map, addr));
359 if (srcline != SRCLINE_UNKNOWN)
360 ret = fprintf(fp, "%s%s", prefix, srcline);
361 free_srcline(srcline);
362 }
363 return ret;
364}
365
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300366/**
367 * map__rip_2objdump - convert symbol start address to objdump address.
368 * @map: memory map
369 * @rip: symbol start address
370 *
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200371 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300372 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
373 * relative to section start.
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300374 *
375 * Return: Address suitable for passing to "objdump --start-address="
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200376 */
377u64 map__rip_2objdump(struct map *map, u64 rip)
378{
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300379 if (!map->dso->adjust_symbols)
380 return rip;
381
382 if (map->dso->rel)
383 return rip - map->pgoff;
384
Adrian Hunter91767532014-01-29 16:14:36 +0200385 return map->unmap_ip(map, rip) - map->reloc;
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200386}
Kirill Smelkovee11b902010-02-07 11:46:15 -0200387
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300388/**
389 * map__objdump_2mem - convert objdump address to a memory address.
390 * @map: memory map
391 * @ip: objdump address
392 *
393 * Closely related to map__rip_2objdump(), this function takes an address from
394 * objdump and converts it to a memory address. Note this assumes that @map
395 * contains the address. To be sure the result is valid, check it forwards
396 * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
397 *
398 * Return: Memory address.
399 */
400u64 map__objdump_2mem(struct map *map, u64 ip)
401{
402 if (!map->dso->adjust_symbols)
403 return map->unmap_ip(map, ip);
404
405 if (map->dso->rel)
406 return map->unmap_ip(map, ip + map->pgoff);
407
Adrian Hunter91767532014-01-29 16:14:36 +0200408 return ip + map->reloc;
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300409}
410
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300411void map_groups__init(struct map_groups *mg)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300412{
413 int i;
414 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300415 mg->maps[i] = RB_ROOT;
416 INIT_LIST_HEAD(&mg->removed_maps[i]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300417 }
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300418 mg->machine = NULL;
Arnaldo Carvalho de Meloa26ca672014-03-25 15:26:44 -0300419 mg->refcnt = 1;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300420}
421
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300422static void maps__delete(struct rb_root *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300423{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300424 struct rb_node *next = rb_first(maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300425
426 while (next) {
427 struct map *pos = rb_entry(next, struct map, rb_node);
428
429 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300430 rb_erase(&pos->rb_node, maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300431 map__delete(pos);
432 }
433}
434
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300435static void maps__delete_removed(struct list_head *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300436{
437 struct map *pos, *n;
438
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300439 list_for_each_entry_safe(pos, n, maps, node) {
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300440 list_del(&pos->node);
441 map__delete(pos);
442 }
443}
444
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300445void map_groups__exit(struct map_groups *mg)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300446{
447 int i;
448
449 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300450 maps__delete(&mg->maps[i]);
451 maps__delete_removed(&mg->removed_maps[i]);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300452 }
453}
454
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300455struct map_groups *map_groups__new(void)
456{
457 struct map_groups *mg = malloc(sizeof(*mg));
458
459 if (mg != NULL)
460 map_groups__init(mg);
461
462 return mg;
463}
464
465void map_groups__delete(struct map_groups *mg)
466{
467 map_groups__exit(mg);
468 free(mg);
469}
470
Arnaldo Carvalho de Meloa26ca672014-03-25 15:26:44 -0300471void map_groups__put(struct map_groups *mg)
472{
473 if (--mg->refcnt == 0)
474 map_groups__delete(mg);
475}
476
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300477void map_groups__flush(struct map_groups *mg)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300478{
479 int type;
480
481 for (type = 0; type < MAP__NR_TYPES; type++) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300482 struct rb_root *root = &mg->maps[type];
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300483 struct rb_node *next = rb_first(root);
484
485 while (next) {
486 struct map *pos = rb_entry(next, struct map, rb_node);
487 next = rb_next(&pos->rb_node);
488 rb_erase(&pos->rb_node, root);
489 /*
490 * We may have references to this map, for
491 * instance in some hist_entry instances, so
492 * just move them to a separate list.
493 */
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300494 list_add_tail(&pos->node, &mg->removed_maps[pos->type]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300495 }
496 }
497}
498
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300499struct symbol *map_groups__find_symbol(struct map_groups *mg,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300500 enum map_type type, u64 addr,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300501 struct map **mapp,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300502 symbol_filter_t filter)
503{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300504 struct map *map = map_groups__find(mg, type, addr);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300505
Masami Hiramatsu4afc81c2014-01-23 02:29:50 +0000506 /* Ensure map is loaded before using map->map_ip */
507 if (map != NULL && map__load(map, filter) >= 0) {
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300508 if (mapp != NULL)
509 *mapp = map;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300510 return map__find_symbol(map, map->map_ip(map, addr), filter);
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300511 }
512
513 return NULL;
514}
515
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300516struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300517 enum map_type type,
518 const char *name,
519 struct map **mapp,
520 symbol_filter_t filter)
521{
522 struct rb_node *nd;
523
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300524 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300525 struct map *pos = rb_entry(nd, struct map, rb_node);
526 struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
527
528 if (sym == NULL)
529 continue;
530 if (mapp != NULL)
531 *mapp = pos;
532 return sym;
533 }
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300534
535 return NULL;
536}
537
Arnaldo Carvalho de Melo4e987712013-10-14 13:43:38 +0300538int map_groups__find_ams(struct addr_map_symbol *ams, symbol_filter_t filter)
539{
540 if (ams->addr < ams->map->start || ams->addr > ams->map->end) {
541 if (ams->map->groups == NULL)
542 return -1;
543 ams->map = map_groups__find(ams->map->groups, ams->map->type,
544 ams->addr);
545 if (ams->map == NULL)
546 return -1;
547 }
548
549 ams->al_addr = ams->map->map_ip(ams->map, ams->addr);
550 ams->sym = map__find_symbol(ams->map, ams->al_addr, filter);
551
552 return ams->sym ? 0 : -1;
553}
554
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300555size_t __map_groups__fprintf_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300556 enum map_type type, int verbose, FILE *fp)
557{
558 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
559 struct rb_node *nd;
560
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300561 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300562 struct map *pos = rb_entry(nd, struct map, rb_node);
563 printed += fprintf(fp, "Map:");
564 printed += map__fprintf(pos, fp);
565 if (verbose > 2) {
566 printed += dso__fprintf(pos->dso, type, fp);
567 printed += fprintf(fp, "--\n");
568 }
569 }
570
571 return printed;
572}
573
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300574size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300575{
576 size_t printed = 0, i;
577 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300578 printed += __map_groups__fprintf_maps(mg, i, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300579 return printed;
580}
581
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300582static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300583 enum map_type type,
584 int verbose, FILE *fp)
585{
586 struct map *pos;
587 size_t printed = 0;
588
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300589 list_for_each_entry(pos, &mg->removed_maps[type], node) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300590 printed += fprintf(fp, "Map:");
591 printed += map__fprintf(pos, fp);
592 if (verbose > 1) {
593 printed += dso__fprintf(pos->dso, type, fp);
594 printed += fprintf(fp, "--\n");
595 }
596 }
597 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,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300601 int verbose, FILE *fp)
602{
603 size_t printed = 0, i;
604 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300605 printed += __map_groups__fprintf_removed_maps(mg, i, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300606 return printed;
607}
608
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300609size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300610{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300611 size_t printed = map_groups__fprintf_maps(mg, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300612 printed += fprintf(fp, "Removed maps:\n");
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300613 return printed + map_groups__fprintf_removed_maps(mg, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300614}
615
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300616int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300617 int verbose, FILE *fp)
618{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300619 struct rb_root *root = &mg->maps[map->type];
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300620 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300621 int err = 0;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300622
623 while (next) {
624 struct map *pos = rb_entry(next, struct map, rb_node);
625 next = rb_next(&pos->rb_node);
626
627 if (!map__overlap(pos, map))
628 continue;
629
630 if (verbose >= 2) {
631 fputs("overlapping maps:\n", fp);
632 map__fprintf(map, fp);
633 map__fprintf(pos, fp);
634 }
635
636 rb_erase(&pos->rb_node, root);
637 /*
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300638 * Now check if we need to create new maps for areas not
639 * overlapped by the new map:
640 */
641 if (map->start > pos->start) {
642 struct map *before = map__clone(pos);
643
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300644 if (before == NULL) {
645 err = -ENOMEM;
646 goto move_map;
647 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300648
649 before->end = map->start - 1;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300650 map_groups__insert(mg, before);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300651 if (verbose >= 2)
652 map__fprintf(before, fp);
653 }
654
655 if (map->end < pos->end) {
656 struct map *after = map__clone(pos);
657
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300658 if (after == NULL) {
659 err = -ENOMEM;
660 goto move_map;
661 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300662
663 after->start = map->end + 1;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300664 map_groups__insert(mg, after);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300665 if (verbose >= 2)
666 map__fprintf(after, fp);
667 }
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300668move_map:
669 /*
670 * If we have references, just move them to a separate list.
671 */
672 if (pos->referenced)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300673 list_add_tail(&pos->node, &mg->removed_maps[map->type]);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300674 else
675 map__delete(pos);
676
677 if (err)
678 return err;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300679 }
680
681 return 0;
682}
683
684/*
685 * XXX This should not really _copy_ te maps, but refcount them.
686 */
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300687int map_groups__clone(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300688 struct map_groups *parent, enum map_type type)
689{
690 struct rb_node *nd;
691 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
692 struct map *map = rb_entry(nd, struct map, rb_node);
693 struct map *new = map__clone(map);
694 if (new == NULL)
695 return -ENOMEM;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300696 map_groups__insert(mg, new);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300697 }
698 return 0;
699}
700
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300701void maps__insert(struct rb_root *maps, struct map *map)
702{
703 struct rb_node **p = &maps->rb_node;
704 struct rb_node *parent = NULL;
705 const u64 ip = map->start;
706 struct map *m;
707
708 while (*p != NULL) {
709 parent = *p;
710 m = rb_entry(parent, struct map, rb_node);
711 if (ip < m->start)
712 p = &(*p)->rb_left;
713 else
714 p = &(*p)->rb_right;
715 }
716
717 rb_link_node(&map->rb_node, parent, p);
718 rb_insert_color(&map->rb_node, maps);
719}
720
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300721void maps__remove(struct rb_root *maps, struct map *map)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300722{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300723 rb_erase(&map->rb_node, maps);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300724}
725
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300726struct map *maps__find(struct rb_root *maps, u64 ip)
727{
728 struct rb_node **p = &maps->rb_node;
729 struct rb_node *parent = NULL;
730 struct map *m;
731
732 while (*p != NULL) {
733 parent = *p;
734 m = rb_entry(parent, struct map, rb_node);
735 if (ip < m->start)
736 p = &(*p)->rb_left;
737 else if (ip > m->end)
738 p = &(*p)->rb_right;
739 else
740 return m;
741 }
742
743 return NULL;
744}
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300745
746struct map *maps__first(struct rb_root *maps)
747{
748 struct rb_node *first = rb_first(maps);
749
750 if (first)
751 return rb_entry(first, struct map, rb_node);
752 return NULL;
753}
754
755struct map *maps__next(struct map *map)
756{
757 struct rb_node *next = rb_next(&map->rb_node);
758
759 if (next)
760 return rb_entry(next, struct map, rb_node);
761 return NULL;
762}