blob: 7af14807ee903a1d60968e00d7c18752b386ebf5 [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 Melo8e16017d2013-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,
Don Zickus7ef80702014-05-19 15:13:49 -0400141 u64 ino_gen, u32 prot, u32 flags, 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;
Don Zickus7ef80702014-05-19 15:13:49 -0400160 map->prot = prot;
161 map->flags = flags;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200162
Namhyung Kim578c03c2014-01-16 10:49:31 +0900163 if ((anon || no_dso) && type == MAP__FUNCTION) {
Arnaldo Carvalho de Melob177f632010-03-25 19:58:57 -0300164 snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200165 filename = newfilename;
166 }
167
Michael Lentineeca81832014-05-20 11:48:50 +0200168 if (android) {
169 if (replace_android_lib(filename, newfilename))
170 filename = newfilename;
171 }
172
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200173 if (vdso) {
174 pgoff = 0;
175 dso = vdso__dso_findnew(dsos__list);
176 } else
177 dso = __dsos__findnew(dsos__list, filename);
178
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200179 if (dso == NULL)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200180 goto out_delete;
181
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300182 map__init(map, type, start, start + len, pgoff, dso);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200183
Jiri Olsa87ffef72011-08-24 15:18:34 +0200184 if (anon || no_dso) {
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300185 map->map_ip = map->unmap_ip = identity__map_ip;
Jiri Olsa87ffef72011-08-24 15:18:34 +0200186
187 /*
188 * Set memory without DSO as loaded. All map__find_*
189 * functions still return NULL, and we avoid the
190 * unnecessary map__load warning.
191 */
Namhyung Kim578c03c2014-01-16 10:49:31 +0900192 if (type != MAP__FUNCTION)
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300193 dso__set_loaded(dso, map->type);
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -0200194 }
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200195 }
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300196 return map;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200197out_delete:
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300198 free(map);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200199 return NULL;
200}
201
Namhyung Kime5a18452012-08-06 13:41:20 +0900202/*
203 * Constructor variant for modules (where we know from /proc/modules where
204 * they are loaded) and for vmlinux, where only after we load all the
205 * symbols we'll know where it starts and ends.
206 */
207struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
208{
209 struct map *map = calloc(1, (sizeof(*map) +
210 (dso->kernel ? sizeof(struct kmap) : 0)));
211 if (map != NULL) {
212 /*
213 * ->end will be filled after we load all the symbols
214 */
215 map__init(map, type, start, 0, 0, dso);
216 }
217
218 return map;
219}
220
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300221void map__delete(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200222{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300223 free(map);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200224}
225
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300226void map__fixup_start(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200227{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300228 struct rb_root *symbols = &map->dso->symbols[map->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200229 struct rb_node *nd = rb_first(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200230 if (nd != NULL) {
231 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300232 map->start = sym->start;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200233 }
234}
235
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300236void map__fixup_end(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200237{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300238 struct rb_root *symbols = &map->dso->symbols[map->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200239 struct rb_node *nd = rb_last(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200240 if (nd != NULL) {
241 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300242 map->end = sym->end;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200243 }
244}
245
Arnaldo Carvalho de Melod70a5402009-10-30 16:28:25 -0200246#define DSO__DELETED "(deleted)"
247
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300248int map__load(struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200249{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300250 const char *name = map->dso->long_name;
Masami Hiramatsua1281682009-12-15 10:32:33 -0500251 int nr;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200252
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300253 if (dso__loaded(map->dso, map->type))
Masami Hiramatsua1281682009-12-15 10:32:33 -0500254 return 0;
255
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300256 nr = dso__load(map->dso, map, filter);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200257 if (nr < 0) {
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300258 if (map->dso->has_build_id) {
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200259 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
260
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300261 build_id__sprintf(map->dso->build_id,
262 sizeof(map->dso->build_id),
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200263 sbuild_id);
264 pr_warning("%s with build id %s not found",
265 name, sbuild_id);
266 } else
267 pr_warning("Failed to open %s", name);
268
269 pr_warning(", continuing without symbols\n");
270 return -1;
271 } else if (nr == 0) {
Ingo Molnar89fe8082013-09-30 12:07:11 +0200272#ifdef HAVE_LIBELF_SUPPORT
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200273 const size_t len = strlen(name);
274 const size_t real_len = len - sizeof(DSO__DELETED);
275
276 if (len > sizeof(DSO__DELETED) &&
277 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
David Aherne77b15b2011-10-18 18:44:45 -0600278 pr_warning("%.*s was updated (is prelink enabled?). "
279 "Restart the long running apps that use it!\n",
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200280 (int)real_len, name);
281 } else {
282 pr_warning("no symbols found in %s, maybe install "
283 "a debug package?\n", name);
284 }
Namhyung Kim393be2e2012-08-06 13:41:21 +0900285#endif
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200286 return -1;
287 }
288
289 return 0;
290}
291
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300292struct symbol *map__find_symbol(struct map *map, u64 addr,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200293 symbol_filter_t filter)
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200294{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300295 if (map__load(map, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200296 return NULL;
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200297
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300298 return dso__find_symbol(map->dso, map->type, addr);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200299}
300
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300301struct symbol *map__find_symbol_by_name(struct map *map, const char *name,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200302 symbol_filter_t filter)
303{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300304 if (map__load(map, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200305 return NULL;
306
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300307 if (!dso__sorted_by_name(map->dso, map->type))
308 dso__sort_by_name(map->dso, map->type);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200309
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300310 return dso__find_symbol_by_name(map->dso, map->type, name);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200311}
312
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300313struct map *map__clone(struct map *map)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200314{
Arnaldo Carvalho de Melo8e16017d2013-01-24 22:16:43 -0300315 return memdup(map, sizeof(*map));
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200316}
317
318int map__overlap(struct map *l, struct map *r)
319{
320 if (l->start > r->start) {
321 struct map *t = l;
322 l = r;
323 r = t;
324 }
325
326 if (l->end > r->start)
327 return 1;
328
329 return 0;
330}
331
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300332size_t map__fprintf(struct map *map, FILE *fp)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200333{
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200334 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300335 map->start, map->end, map->pgoff, map->dso->name);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200336}
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200337
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900338size_t map__fprintf_dsoname(struct map *map, FILE *fp)
339{
Feng Tang8f28f192012-08-27 15:38:26 +0800340 const char *dsoname = "[unknown]";
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900341
Akihiro Nagai0bc8d202012-01-30 13:43:20 +0900342 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
343 if (symbol_conf.show_kernel_path && map->dso->long_name)
344 dsoname = map->dso->long_name;
345 else if (map->dso->name)
346 dsoname = map->dso->name;
Feng Tang8f28f192012-08-27 15:38:26 +0800347 }
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900348
349 return fprintf(fp, "%s", dsoname);
350}
351
Adrian Huntercc8fae12013-12-06 09:42:57 +0200352int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
353 FILE *fp)
354{
355 char *srcline;
356 int ret = 0;
357
358 if (map && map->dso) {
359 srcline = get_srcline(map->dso,
360 map__rip_2objdump(map, addr));
361 if (srcline != SRCLINE_UNKNOWN)
362 ret = fprintf(fp, "%s%s", prefix, srcline);
363 free_srcline(srcline);
364 }
365 return ret;
366}
367
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300368/**
369 * map__rip_2objdump - convert symbol start address to objdump address.
370 * @map: memory map
371 * @rip: symbol start address
372 *
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200373 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300374 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
375 * relative to section start.
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300376 *
377 * Return: Address suitable for passing to "objdump --start-address="
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200378 */
379u64 map__rip_2objdump(struct map *map, u64 rip)
380{
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300381 if (!map->dso->adjust_symbols)
382 return rip;
383
384 if (map->dso->rel)
385 return rip - map->pgoff;
386
Adrian Hunter91767532014-01-29 16:14:36 +0200387 return map->unmap_ip(map, rip) - map->reloc;
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200388}
Kirill Smelkovee11b902010-02-07 11:46:15 -0200389
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300390/**
391 * map__objdump_2mem - convert objdump address to a memory address.
392 * @map: memory map
393 * @ip: objdump address
394 *
395 * Closely related to map__rip_2objdump(), this function takes an address from
396 * objdump and converts it to a memory address. Note this assumes that @map
397 * contains the address. To be sure the result is valid, check it forwards
398 * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
399 *
400 * Return: Memory address.
401 */
402u64 map__objdump_2mem(struct map *map, u64 ip)
403{
404 if (!map->dso->adjust_symbols)
405 return map->unmap_ip(map, ip);
406
407 if (map->dso->rel)
408 return map->unmap_ip(map, ip + map->pgoff);
409
Adrian Hunter91767532014-01-29 16:14:36 +0200410 return ip + map->reloc;
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300411}
412
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300413void map_groups__init(struct map_groups *mg)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300414{
415 int i;
416 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300417 mg->maps[i] = RB_ROOT;
418 INIT_LIST_HEAD(&mg->removed_maps[i]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300419 }
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300420 mg->machine = NULL;
Arnaldo Carvalho de Meloa26ca672014-03-25 15:26:44 -0300421 mg->refcnt = 1;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300422}
423
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300424static void maps__delete(struct rb_root *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300425{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300426 struct rb_node *next = rb_first(maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300427
428 while (next) {
429 struct map *pos = rb_entry(next, struct map, rb_node);
430
431 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300432 rb_erase(&pos->rb_node, maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300433 map__delete(pos);
434 }
435}
436
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300437static void maps__delete_removed(struct list_head *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300438{
439 struct map *pos, *n;
440
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300441 list_for_each_entry_safe(pos, n, maps, node) {
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300442 list_del(&pos->node);
443 map__delete(pos);
444 }
445}
446
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300447void map_groups__exit(struct map_groups *mg)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300448{
449 int i;
450
451 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300452 maps__delete(&mg->maps[i]);
453 maps__delete_removed(&mg->removed_maps[i]);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300454 }
455}
456
Adrian Hunter29ce3612014-07-16 11:07:13 +0300457bool map_groups__empty(struct map_groups *mg)
458{
459 int i;
460
461 for (i = 0; i < MAP__NR_TYPES; ++i) {
462 if (maps__first(&mg->maps[i]))
463 return false;
464 if (!list_empty(&mg->removed_maps[i]))
465 return false;
466 }
467
468 return true;
469}
470
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300471struct map_groups *map_groups__new(void)
472{
473 struct map_groups *mg = malloc(sizeof(*mg));
474
475 if (mg != NULL)
476 map_groups__init(mg);
477
478 return mg;
479}
480
481void map_groups__delete(struct map_groups *mg)
482{
483 map_groups__exit(mg);
484 free(mg);
485}
486
Arnaldo Carvalho de Meloa26ca672014-03-25 15:26:44 -0300487void map_groups__put(struct map_groups *mg)
488{
489 if (--mg->refcnt == 0)
490 map_groups__delete(mg);
491}
492
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300493void map_groups__flush(struct map_groups *mg)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300494{
495 int type;
496
497 for (type = 0; type < MAP__NR_TYPES; type++) {
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300498 struct rb_root *root = &mg->maps[type];
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300499 struct rb_node *next = rb_first(root);
500
501 while (next) {
502 struct map *pos = rb_entry(next, struct map, rb_node);
503 next = rb_next(&pos->rb_node);
504 rb_erase(&pos->rb_node, root);
505 /*
506 * We may have references to this map, for
507 * instance in some hist_entry instances, so
508 * just move them to a separate list.
509 */
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300510 list_add_tail(&pos->node, &mg->removed_maps[pos->type]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300511 }
512 }
513}
514
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300515struct symbol *map_groups__find_symbol(struct map_groups *mg,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300516 enum map_type type, u64 addr,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300517 struct map **mapp,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300518 symbol_filter_t filter)
519{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300520 struct map *map = map_groups__find(mg, type, addr);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300521
Masami Hiramatsu4afc81c2014-01-23 02:29:50 +0000522 /* Ensure map is loaded before using map->map_ip */
523 if (map != NULL && map__load(map, filter) >= 0) {
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300524 if (mapp != NULL)
525 *mapp = map;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300526 return map__find_symbol(map, map->map_ip(map, addr), filter);
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300527 }
528
529 return NULL;
530}
531
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300532struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300533 enum map_type type,
534 const char *name,
535 struct map **mapp,
536 symbol_filter_t filter)
537{
538 struct rb_node *nd;
539
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300540 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300541 struct map *pos = rb_entry(nd, struct map, rb_node);
542 struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
543
544 if (sym == NULL)
545 continue;
546 if (mapp != NULL)
547 *mapp = pos;
548 return sym;
549 }
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300550
551 return NULL;
552}
553
Arnaldo Carvalho de Melo4e987712013-10-14 13:43:38 +0300554int map_groups__find_ams(struct addr_map_symbol *ams, symbol_filter_t filter)
555{
556 if (ams->addr < ams->map->start || ams->addr > ams->map->end) {
557 if (ams->map->groups == NULL)
558 return -1;
559 ams->map = map_groups__find(ams->map->groups, ams->map->type,
560 ams->addr);
561 if (ams->map == NULL)
562 return -1;
563 }
564
565 ams->al_addr = ams->map->map_ip(ams->map, ams->addr);
566 ams->sym = map__find_symbol(ams->map, ams->al_addr, filter);
567
568 return ams->sym ? 0 : -1;
569}
570
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300571size_t __map_groups__fprintf_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300572 enum map_type type, int verbose, FILE *fp)
573{
574 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
575 struct rb_node *nd;
576
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300577 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300578 struct map *pos = rb_entry(nd, struct map, rb_node);
579 printed += fprintf(fp, "Map:");
580 printed += map__fprintf(pos, fp);
581 if (verbose > 2) {
582 printed += dso__fprintf(pos->dso, type, fp);
583 printed += fprintf(fp, "--\n");
584 }
585 }
586
587 return printed;
588}
589
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300590size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300591{
592 size_t printed = 0, i;
593 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300594 printed += __map_groups__fprintf_maps(mg, i, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300595 return printed;
596}
597
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300598static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300599 enum map_type type,
600 int verbose, FILE *fp)
601{
602 struct map *pos;
603 size_t printed = 0;
604
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300605 list_for_each_entry(pos, &mg->removed_maps[type], node) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300606 printed += fprintf(fp, "Map:");
607 printed += map__fprintf(pos, fp);
608 if (verbose > 1) {
609 printed += dso__fprintf(pos->dso, type, fp);
610 printed += fprintf(fp, "--\n");
611 }
612 }
613 return printed;
614}
615
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300616static size_t map_groups__fprintf_removed_maps(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300617 int verbose, FILE *fp)
618{
619 size_t printed = 0, i;
620 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300621 printed += __map_groups__fprintf_removed_maps(mg, i, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300622 return printed;
623}
624
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300625size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300626{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300627 size_t printed = map_groups__fprintf_maps(mg, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300628 printed += fprintf(fp, "Removed maps:\n");
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300629 return printed + map_groups__fprintf_removed_maps(mg, verbose, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300630}
631
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300632int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300633 int verbose, FILE *fp)
634{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300635 struct rb_root *root = &mg->maps[map->type];
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300636 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300637 int err = 0;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300638
639 while (next) {
640 struct map *pos = rb_entry(next, struct map, rb_node);
641 next = rb_next(&pos->rb_node);
642
643 if (!map__overlap(pos, map))
644 continue;
645
646 if (verbose >= 2) {
647 fputs("overlapping maps:\n", fp);
648 map__fprintf(map, fp);
649 map__fprintf(pos, fp);
650 }
651
652 rb_erase(&pos->rb_node, root);
653 /*
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300654 * Now check if we need to create new maps for areas not
655 * overlapped by the new map:
656 */
657 if (map->start > pos->start) {
658 struct map *before = map__clone(pos);
659
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300660 if (before == NULL) {
661 err = -ENOMEM;
662 goto move_map;
663 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300664
665 before->end = map->start - 1;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300666 map_groups__insert(mg, before);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300667 if (verbose >= 2)
668 map__fprintf(before, fp);
669 }
670
671 if (map->end < pos->end) {
672 struct map *after = map__clone(pos);
673
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300674 if (after == NULL) {
675 err = -ENOMEM;
676 goto move_map;
677 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300678
679 after->start = map->end + 1;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300680 map_groups__insert(mg, after);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300681 if (verbose >= 2)
682 map__fprintf(after, fp);
683 }
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300684move_map:
685 /*
686 * If we have references, just move them to a separate list.
687 */
688 if (pos->referenced)
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300689 list_add_tail(&pos->node, &mg->removed_maps[map->type]);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300690 else
691 map__delete(pos);
692
693 if (err)
694 return err;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300695 }
696
697 return 0;
698}
699
700/*
701 * XXX This should not really _copy_ te maps, but refcount them.
702 */
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300703int map_groups__clone(struct map_groups *mg,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300704 struct map_groups *parent, enum map_type type)
705{
706 struct rb_node *nd;
707 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
708 struct map *map = rb_entry(nd, struct map, rb_node);
709 struct map *new = map__clone(map);
710 if (new == NULL)
711 return -ENOMEM;
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300712 map_groups__insert(mg, new);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300713 }
714 return 0;
715}
716
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300717void maps__insert(struct rb_root *maps, struct map *map)
718{
719 struct rb_node **p = &maps->rb_node;
720 struct rb_node *parent = NULL;
721 const u64 ip = map->start;
722 struct map *m;
723
724 while (*p != NULL) {
725 parent = *p;
726 m = rb_entry(parent, struct map, rb_node);
727 if (ip < m->start)
728 p = &(*p)->rb_left;
729 else
730 p = &(*p)->rb_right;
731 }
732
733 rb_link_node(&map->rb_node, parent, p);
734 rb_insert_color(&map->rb_node, maps);
735}
736
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300737void maps__remove(struct rb_root *maps, struct map *map)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300738{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300739 rb_erase(&map->rb_node, maps);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300740}
741
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300742struct map *maps__find(struct rb_root *maps, u64 ip)
743{
744 struct rb_node **p = &maps->rb_node;
745 struct rb_node *parent = NULL;
746 struct map *m;
747
748 while (*p != NULL) {
749 parent = *p;
750 m = rb_entry(parent, struct map, rb_node);
751 if (ip < m->start)
752 p = &(*p)->rb_left;
753 else if (ip > m->end)
754 p = &(*p)->rb_right;
755 else
756 return m;
757 }
758
759 return NULL;
760}
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300761
762struct map *maps__first(struct rb_root *maps)
763{
764 struct rb_node *first = rb_first(maps);
765
766 if (first)
767 return rb_entry(first, struct map, rb_node);
768 return NULL;
769}
770
771struct map *maps__next(struct map *map)
772{
773 struct rb_node *next = rb_next(&map->rb_node);
774
775 if (next)
776 return rb_entry(next, struct map, rb_node);
777 return NULL;
778}