blob: 8fe57031e1a85ba3816b322e30526bcf9f237d14 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Frederic Weisbecker66e274f2009-08-12 11:07:25 +02002#include "symbol.h"
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -03003#include <errno.h>
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02004#include <inttypes.h>
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -03005#include <limits.h>
Frederic Weisbecker66e274f2009-08-12 11:07:25 +02006#include <stdlib.h>
7#include <string.h>
8#include <stdio.h>
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08009#include <unistd.h>
Arnaldo Carvalho de Melofbef1032016-09-12 16:47:57 -030010#include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -030011#include "map.h"
David Ahern5cd95c22012-07-20 17:25:47 -060012#include "thread.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020013#include "vdso.h"
Jiri Olsaebb296c2012-10-27 23:18:28 +020014#include "build-id.h"
Adrian Huntercc8fae12013-12-06 09:42:57 +020015#include "util.h"
Jiri Olsaacebd402014-07-14 23:46:47 +020016#include "debug.h"
Adrian Hunter2a030682014-07-22 16:17:53 +030017#include "machine.h"
Arnaldo Carvalho de Melo8e160172013-01-24 22:16:43 -030018#include <linux/string.h>
Arnaldo Carvalho de Melo632a5ca2017-04-17 16:30:49 -030019#include "srcline.h"
Krister Johansen843ff372017-07-05 18:48:08 -070020#include "namespaces.h"
Jiri Olsa6c502582016-07-04 14:16:23 +020021#include "unwind.h"
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020022
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -030023static void __maps__insert(struct maps *maps, struct map *map);
24
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -030025const char *map_type__name[MAP__NR_TYPES] = {
26 [MAP__FUNCTION] = "Functions",
27 [MAP__VARIABLE] = "Variables",
28};
29
Wang Nan0ac33482016-09-06 04:58:27 +000030static inline int is_anon_memory(const char *filename, u32 flags)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020031{
Arnaldo Carvalho de Melofbef1032016-09-12 16:47:57 -030032 return flags & MAP_HUGETLB ||
Wang Nan0ac33482016-09-06 04:58:27 +000033 !strcmp(filename, "//anon") ||
Yannick Brosseaub2be5452015-11-26 03:42:32 -080034 !strncmp(filename, "/dev/zero", sizeof("/dev/zero") - 1) ||
35 !strncmp(filename, "/anon_hugepage", sizeof("/anon_hugepage") - 1);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020036}
37
Jiri Olsa87ffef72011-08-24 15:18:34 +020038static inline int is_no_dso_memory(const char *filename)
39{
Namhyung Kim1e825742012-11-07 16:27:11 +090040 return !strncmp(filename, "[stack", 6) ||
Don Zickus700be562014-08-19 22:31:14 -040041 !strncmp(filename, "/SYSV",5) ||
Jiri Olsa87ffef72011-08-24 15:18:34 +020042 !strcmp(filename, "[heap]");
43}
44
Michael Lentineeca81832014-05-20 11:48:50 +020045static inline int is_android_lib(const char *filename)
46{
47 return !strncmp(filename, "/data/app-lib", 13) ||
48 !strncmp(filename, "/system/lib", 11);
49}
50
51static inline bool replace_android_lib(const char *filename, char *newfilename)
52{
53 const char *libname;
54 char *app_abi;
55 size_t app_abi_length, new_length;
56 size_t lib_length = 0;
57
58 libname = strrchr(filename, '/');
59 if (libname)
60 lib_length = strlen(libname);
61
62 app_abi = getenv("APP_ABI");
63 if (!app_abi)
64 return false;
65
66 app_abi_length = strlen(app_abi);
67
68 if (!strncmp(filename, "/data/app-lib", 13)) {
69 char *apk_path;
70
71 if (!app_abi_length)
72 return false;
73
74 new_length = 7 + app_abi_length + lib_length;
75
76 apk_path = getenv("APK_PATH");
77 if (apk_path) {
78 new_length += strlen(apk_path) + 1;
79 if (new_length > PATH_MAX)
80 return false;
81 snprintf(newfilename, new_length,
82 "%s/libs/%s/%s", apk_path, app_abi, libname);
83 } else {
84 if (new_length > PATH_MAX)
85 return false;
86 snprintf(newfilename, new_length,
87 "libs/%s/%s", app_abi, libname);
88 }
89 return true;
90 }
91
92 if (!strncmp(filename, "/system/lib/", 11)) {
93 char *ndk, *app;
94 const char *arch;
95 size_t ndk_length;
96 size_t app_length;
97
98 ndk = getenv("NDK_ROOT");
99 app = getenv("APP_PLATFORM");
100
101 if (!(ndk && app))
102 return false;
103
104 ndk_length = strlen(ndk);
105 app_length = strlen(app);
106
107 if (!(ndk_length && app_length && app_abi_length))
108 return false;
109
110 arch = !strncmp(app_abi, "arm", 3) ? "arm" :
111 !strncmp(app_abi, "mips", 4) ? "mips" :
112 !strncmp(app_abi, "x86", 3) ? "x86" : NULL;
113
114 if (!arch)
115 return false;
116
117 new_length = 27 + ndk_length +
118 app_length + lib_length
119 + strlen(arch);
120
121 if (new_length > PATH_MAX)
122 return false;
123 snprintf(newfilename, new_length,
124 "%s/platforms/%s/arch-%s/usr/lib/%s",
125 ndk, app, arch, libname);
126
127 return true;
128 }
129 return false;
130}
131
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300132void map__init(struct map *map, enum map_type type,
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -0200133 u64 start, u64 end, u64 pgoff, struct dso *dso)
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200134{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300135 map->type = type;
136 map->start = start;
137 map->end = end;
138 map->pgoff = pgoff;
Adrian Hunter91767532014-01-29 16:14:36 +0200139 map->reloc = 0;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300140 map->dso = dso__get(dso);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300141 map->map_ip = map__map_ip;
142 map->unmap_ip = map__unmap_ip;
143 RB_CLEAR_NODE(&map->rb_node);
144 map->groups = NULL;
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300145 map->erange_warned = false;
Elena Reshetovae3a42cd2017-02-21 17:34:59 +0200146 refcount_set(&map->refcnt, 1);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200147}
148
Adrian Hunter2a030682014-07-22 16:17:53 +0300149struct map *map__new(struct machine *machine, u64 start, u64 len,
Krister Johansenbf2e7102017-07-05 18:48:09 -0700150 u64 pgoff, u32 d_maj, u32 d_min, u64 ino,
Don Zickus7ef80702014-05-19 15:13:49 -0400151 u64 ino_gen, u32 prot, u32 flags, char *filename,
Adrian Hunter5835edd2014-07-22 16:18:00 +0300152 enum map_type type, struct thread *thread)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200153{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300154 struct map *map = malloc(sizeof(*map));
Krister Johansenbf2e7102017-07-05 18:48:09 -0700155 struct nsinfo *nsi = NULL;
156 struct nsinfo *nnsi;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200157
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300158 if (map != NULL) {
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200159 char newfilename[PATH_MAX];
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200160 struct dso *dso;
Michael Lentineeca81832014-05-20 11:48:50 +0200161 int anon, no_dso, vdso, android;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200162
Michael Lentineeca81832014-05-20 11:48:50 +0200163 android = is_android_lib(filename);
Wang Nan0ac33482016-09-06 04:58:27 +0000164 anon = is_anon_memory(filename, flags);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200165 vdso = is_vdso_map(filename);
Jiri Olsa87ffef72011-08-24 15:18:34 +0200166 no_dso = is_no_dso_memory(filename);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200167
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200168 map->maj = d_maj;
169 map->min = d_min;
170 map->ino = ino;
171 map->ino_generation = ino_gen;
Don Zickus7ef80702014-05-19 15:13:49 -0400172 map->prot = prot;
173 map->flags = flags;
Krister Johansenbf2e7102017-07-05 18:48:09 -0700174 nsi = nsinfo__get(thread->nsinfo);
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200175
Krister Johansenbf2e7102017-07-05 18:48:09 -0700176 if ((anon || no_dso) && nsi && type == MAP__FUNCTION) {
177 snprintf(newfilename, sizeof(newfilename),
178 "/tmp/perf-%d.map", nsi->pid);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200179 filename = newfilename;
180 }
181
Michael Lentineeca81832014-05-20 11:48:50 +0200182 if (android) {
183 if (replace_android_lib(filename, newfilename))
184 filename = newfilename;
185 }
186
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200187 if (vdso) {
Krister Johansenbf2e7102017-07-05 18:48:09 -0700188 /* The vdso maps are always on the host and not the
189 * container. Ensure that we don't use setns to look
190 * them up.
191 */
192 nnsi = nsinfo__copy(nsi);
193 if (nnsi) {
194 nsinfo__put(nsi);
195 nnsi->need_setns = false;
196 nsi = nnsi;
197 }
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200198 pgoff = 0;
Arnaldo Carvalho de Melo9a4388c2015-05-29 11:54:08 -0300199 dso = machine__findnew_vdso(machine, thread);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200200 } else
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -0300201 dso = machine__findnew_dso(machine, filename);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200202
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200203 if (dso == NULL)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200204 goto out_delete;
205
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300206 map__init(map, type, start, start + len, pgoff, dso);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200207
Jiri Olsa87ffef72011-08-24 15:18:34 +0200208 if (anon || no_dso) {
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300209 map->map_ip = map->unmap_ip = identity__map_ip;
Jiri Olsa87ffef72011-08-24 15:18:34 +0200210
211 /*
212 * Set memory without DSO as loaded. All map__find_*
213 * functions still return NULL, and we avoid the
214 * unnecessary map__load warning.
215 */
Namhyung Kim578c03c2014-01-16 10:49:31 +0900216 if (type != MAP__FUNCTION)
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300217 dso__set_loaded(dso, map->type);
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -0200218 }
Krister Johansenbf2e7102017-07-05 18:48:09 -0700219 dso->nsinfo = nsi;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300220 dso__put(dso);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200221 }
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300222 return map;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200223out_delete:
Krister Johansenbf2e7102017-07-05 18:48:09 -0700224 nsinfo__put(nsi);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300225 free(map);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200226 return NULL;
227}
228
Namhyung Kime5a18452012-08-06 13:41:20 +0900229/*
230 * Constructor variant for modules (where we know from /proc/modules where
231 * they are loaded) and for vmlinux, where only after we load all the
232 * symbols we'll know where it starts and ends.
233 */
234struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
235{
236 struct map *map = calloc(1, (sizeof(*map) +
237 (dso->kernel ? sizeof(struct kmap) : 0)));
238 if (map != NULL) {
239 /*
240 * ->end will be filled after we load all the symbols
241 */
242 map__init(map, type, start, 0, 0, dso);
243 }
244
245 return map;
246}
247
Arnaldo Carvalho de Meloe6ce71262015-07-23 11:06:16 -0300248/*
249 * Use this and __map__is_kmodule() for map instances that are in
250 * machine->kmaps, and thus have map->groups->machine all properly set, to
251 * disambiguate between the kernel and modules.
252 *
253 * When the need arises, introduce map__is_{kernel,kmodule)() that
254 * checks (map->groups != NULL && map->groups->machine != NULL &&
255 * map->dso->kernel) before calling __map__is_{kernel,kmodule}())
256 */
257bool __map__is_kernel(const struct map *map)
258{
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -0300259 return __machine__kernel_map(map->groups->machine, map->type) == map;
Arnaldo Carvalho de Meloe6ce71262015-07-23 11:06:16 -0300260}
261
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300262static void map__exit(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200263{
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -0300264 BUG_ON(!RB_EMPTY_NODE(&map->rb_node));
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300265 dso__zput(map->dso);
266}
267
268void map__delete(struct map *map)
269{
270 map__exit(map);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300271 free(map);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200272}
273
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300274void map__put(struct map *map)
275{
Elena Reshetovae3a42cd2017-02-21 17:34:59 +0200276 if (map && refcount_dec_and_test(&map->refcnt))
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300277 map__delete(map);
278}
279
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300280void map__fixup_start(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200281{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300282 struct rb_root *symbols = &map->dso->symbols[map->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200283 struct rb_node *nd = rb_first(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200284 if (nd != NULL) {
285 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300286 map->start = sym->start;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200287 }
288}
289
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300290void map__fixup_end(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200291{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300292 struct rb_root *symbols = &map->dso->symbols[map->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200293 struct rb_node *nd = rb_last(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200294 if (nd != NULL) {
295 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300296 map->end = sym->end;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200297 }
298}
299
Arnaldo Carvalho de Melod70a5402009-10-30 16:28:25 -0200300#define DSO__DELETED "(deleted)"
301
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300302int map__load(struct map *map)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200303{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300304 const char *name = map->dso->long_name;
Masami Hiramatsua1281682009-12-15 10:32:33 -0500305 int nr;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200306
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300307 if (dso__loaded(map->dso, map->type))
Masami Hiramatsua1281682009-12-15 10:32:33 -0500308 return 0;
309
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300310 nr = dso__load(map->dso, map);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200311 if (nr < 0) {
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300312 if (map->dso->has_build_id) {
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +0900313 char sbuild_id[SBUILD_ID_SIZE];
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200314
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300315 build_id__sprintf(map->dso->build_id,
316 sizeof(map->dso->build_id),
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200317 sbuild_id);
318 pr_warning("%s with build id %s not found",
319 name, sbuild_id);
320 } else
321 pr_warning("Failed to open %s", name);
322
323 pr_warning(", continuing without symbols\n");
324 return -1;
325 } else if (nr == 0) {
Ingo Molnar89fe8082013-09-30 12:07:11 +0200326#ifdef HAVE_LIBELF_SUPPORT
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200327 const size_t len = strlen(name);
328 const size_t real_len = len - sizeof(DSO__DELETED);
329
330 if (len > sizeof(DSO__DELETED) &&
331 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
David Aherne77b15b2011-10-18 18:44:45 -0600332 pr_warning("%.*s was updated (is prelink enabled?). "
333 "Restart the long running apps that use it!\n",
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200334 (int)real_len, name);
335 } else {
336 pr_warning("no symbols found in %s, maybe install "
337 "a debug package?\n", name);
338 }
Namhyung Kim393be2e2012-08-06 13:41:21 +0900339#endif
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200340 return -1;
341 }
342
343 return 0;
344}
345
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300346struct symbol *map__find_symbol(struct map *map, u64 addr)
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200347{
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300348 if (map__load(map) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200349 return NULL;
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200350
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300351 return dso__find_symbol(map->dso, map->type, addr);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200352}
353
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300354struct symbol *map__find_symbol_by_name(struct map *map, const char *name)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200355{
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300356 if (map__load(map) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200357 return NULL;
358
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300359 if (!dso__sorted_by_name(map->dso, map->type))
360 dso__sort_by_name(map->dso, map->type);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200361
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300362 return dso__find_symbol_by_name(map->dso, map->type, name);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200363}
364
Arnaldo Carvalho de Melo66671d02015-08-18 15:19:50 -0300365struct map *map__clone(struct map *from)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200366{
Arnaldo Carvalho de Melo66671d02015-08-18 15:19:50 -0300367 struct map *map = memdup(from, sizeof(*map));
368
369 if (map != NULL) {
Elena Reshetovae3a42cd2017-02-21 17:34:59 +0200370 refcount_set(&map->refcnt, 1);
Arnaldo Carvalho de Melo66671d02015-08-18 15:19:50 -0300371 RB_CLEAR_NODE(&map->rb_node);
372 dso__get(map->dso);
373 map->groups = NULL;
374 }
375
376 return map;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200377}
378
379int map__overlap(struct map *l, struct map *r)
380{
381 if (l->start > r->start) {
382 struct map *t = l;
383 l = r;
384 r = t;
385 }
386
387 if (l->end > r->start)
388 return 1;
389
390 return 0;
391}
392
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300393size_t map__fprintf(struct map *map, FILE *fp)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200394{
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200395 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300396 map->start, map->end, map->pgoff, map->dso->name);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200397}
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200398
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900399size_t map__fprintf_dsoname(struct map *map, FILE *fp)
400{
Feng Tang8f28f192012-08-27 15:38:26 +0800401 const char *dsoname = "[unknown]";
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900402
Arnaldo Carvalho de Melo5eae7d82017-02-13 17:11:03 -0300403 if (map && map->dso) {
Akihiro Nagai0bc8d202012-01-30 13:43:20 +0900404 if (symbol_conf.show_kernel_path && map->dso->long_name)
405 dsoname = map->dso->long_name;
Arnaldo Carvalho de Melo5eae7d82017-02-13 17:11:03 -0300406 else
Akihiro Nagai0bc8d202012-01-30 13:43:20 +0900407 dsoname = map->dso->name;
Feng Tang8f28f192012-08-27 15:38:26 +0800408 }
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900409
410 return fprintf(fp, "%s", dsoname);
411}
412
Adrian Huntercc8fae12013-12-06 09:42:57 +0200413int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
414 FILE *fp)
415{
416 char *srcline;
417 int ret = 0;
418
419 if (map && map->dso) {
420 srcline = get_srcline(map->dso,
Milian Wolff5dfa2102017-03-18 22:49:28 +0100421 map__rip_2objdump(map, addr), NULL,
Jin Yao935f5a92017-12-30 00:26:52 +0800422 true, true, addr);
Adrian Huntercc8fae12013-12-06 09:42:57 +0200423 if (srcline != SRCLINE_UNKNOWN)
424 ret = fprintf(fp, "%s%s", prefix, srcline);
425 free_srcline(srcline);
426 }
427 return ret;
428}
429
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300430/**
431 * map__rip_2objdump - convert symbol start address to objdump address.
432 * @map: memory map
433 * @rip: symbol start address
434 *
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200435 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300436 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
437 * relative to section start.
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300438 *
439 * Return: Address suitable for passing to "objdump --start-address="
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200440 */
441u64 map__rip_2objdump(struct map *map, u64 rip)
442{
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300443 if (!map->dso->adjust_symbols)
444 return rip;
445
446 if (map->dso->rel)
447 return rip - map->pgoff;
448
Wang Nana58f7032016-04-07 10:24:30 +0000449 /*
450 * kernel modules also have DSO_TYPE_USER in dso->kernel,
451 * but all kernel modules are ET_REL, so won't get here.
452 */
453 if (map->dso->kernel == DSO_TYPE_USER)
454 return rip + map->dso->text_offset;
455
Adrian Hunter91767532014-01-29 16:14:36 +0200456 return map->unmap_ip(map, rip) - map->reloc;
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200457}
Kirill Smelkovee11b902010-02-07 11:46:15 -0200458
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300459/**
460 * map__objdump_2mem - convert objdump address to a memory address.
461 * @map: memory map
462 * @ip: objdump address
463 *
464 * Closely related to map__rip_2objdump(), this function takes an address from
465 * objdump and converts it to a memory address. Note this assumes that @map
466 * contains the address. To be sure the result is valid, check it forwards
467 * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
468 *
469 * Return: Memory address.
470 */
471u64 map__objdump_2mem(struct map *map, u64 ip)
472{
473 if (!map->dso->adjust_symbols)
474 return map->unmap_ip(map, ip);
475
476 if (map->dso->rel)
477 return map->unmap_ip(map, ip + map->pgoff);
478
Wang Nana58f7032016-04-07 10:24:30 +0000479 /*
480 * kernel modules also have DSO_TYPE_USER in dso->kernel,
481 * but all kernel modules are ET_REL, so won't get here.
482 */
483 if (map->dso->kernel == DSO_TYPE_USER)
484 return map->unmap_ip(map, ip - map->dso->text_offset);
485
Adrian Hunter91767532014-01-29 16:14:36 +0200486 return ip + map->reloc;
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300487}
488
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300489static void maps__init(struct maps *maps)
490{
491 maps->entries = RB_ROOT;
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300492 init_rwsem(&maps->lock);
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300493}
494
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300495void map_groups__init(struct map_groups *mg, struct machine *machine)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300496{
497 int i;
498 for (i = 0; i < MAP__NR_TYPES; ++i) {
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300499 maps__init(&mg->maps[i]);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300500 }
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300501 mg->machine = machine;
Elena Reshetovaead05e82017-02-21 17:35:00 +0200502 refcount_set(&mg->refcnt, 1);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300503}
504
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300505static void __maps__purge(struct maps *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300506{
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300507 struct rb_root *root = &maps->entries;
508 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300509
510 while (next) {
511 struct map *pos = rb_entry(next, struct map, rb_node);
512
513 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -0300514 rb_erase_init(&pos->rb_node, root);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300515 map__put(pos);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300516 }
517}
518
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300519static void maps__exit(struct maps *maps)
520{
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300521 down_write(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300522 __maps__purge(maps);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300523 up_write(&maps->lock);
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300524}
525
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300526void map_groups__exit(struct map_groups *mg)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300527{
528 int i;
529
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300530 for (i = 0; i < MAP__NR_TYPES; ++i)
531 maps__exit(&mg->maps[i]);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300532}
533
Adrian Hunter29ce3612014-07-16 11:07:13 +0300534bool map_groups__empty(struct map_groups *mg)
535{
536 int i;
537
538 for (i = 0; i < MAP__NR_TYPES; ++i) {
539 if (maps__first(&mg->maps[i]))
540 return false;
Adrian Hunter29ce3612014-07-16 11:07:13 +0300541 }
542
543 return true;
544}
545
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300546struct map_groups *map_groups__new(struct machine *machine)
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300547{
548 struct map_groups *mg = malloc(sizeof(*mg));
549
550 if (mg != NULL)
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300551 map_groups__init(mg, machine);
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300552
553 return mg;
554}
555
556void map_groups__delete(struct map_groups *mg)
557{
558 map_groups__exit(mg);
559 free(mg);
560}
561
Arnaldo Carvalho de Meloa26ca672014-03-25 15:26:44 -0300562void map_groups__put(struct map_groups *mg)
563{
Elena Reshetovaead05e82017-02-21 17:35:00 +0200564 if (mg && refcount_dec_and_test(&mg->refcnt))
Arnaldo Carvalho de Meloa26ca672014-03-25 15:26:44 -0300565 map_groups__delete(mg);
566}
567
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300568struct symbol *map_groups__find_symbol(struct map_groups *mg,
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300569 enum map_type type, u64 addr,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300570 struct map **mapp)
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300571{
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300572 struct map *map = map_groups__find(mg, type, addr);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300573
Masami Hiramatsu4afc81c2014-01-23 02:29:50 +0000574 /* Ensure map is loaded before using map->map_ip */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300575 if (map != NULL && map__load(map) >= 0) {
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300576 if (mapp != NULL)
577 *mapp = map;
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300578 return map__find_symbol(map, map->map_ip(map, addr));
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300579 }
580
581 return NULL;
582}
583
Arnaldo Carvalho de Melob7f9ff52015-09-29 17:34:46 -0300584struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300585 struct map **mapp)
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300586{
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300587 struct symbol *sym;
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300588 struct rb_node *nd;
589
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300590 down_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300591
592 for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300593 struct map *pos = rb_entry(nd, struct map, rb_node);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300594
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300595 sym = map__find_symbol_by_name(pos, name);
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300596
597 if (sym == NULL)
598 continue;
599 if (mapp != NULL)
600 *mapp = pos;
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300601 goto out;
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300602 }
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300603
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300604 sym = NULL;
605out:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300606 up_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300607 return sym;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300608}
609
Arnaldo Carvalho de Melob7f9ff52015-09-29 17:34:46 -0300610struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
611 enum map_type type,
612 const char *name,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300613 struct map **mapp)
Arnaldo Carvalho de Melob7f9ff52015-09-29 17:34:46 -0300614{
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300615 struct symbol *sym = maps__find_symbol_by_name(&mg->maps[type], name, mapp);
Arnaldo Carvalho de Melob7f9ff52015-09-29 17:34:46 -0300616
617 return sym;
618}
619
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300620int map_groups__find_ams(struct addr_map_symbol *ams)
Arnaldo Carvalho de Melo4e987712013-10-14 13:43:38 +0300621{
Stephane Eranian77faf4d2014-10-06 10:35:32 +0200622 if (ams->addr < ams->map->start || ams->addr >= ams->map->end) {
Arnaldo Carvalho de Melo4e987712013-10-14 13:43:38 +0300623 if (ams->map->groups == NULL)
624 return -1;
625 ams->map = map_groups__find(ams->map->groups, ams->map->type,
626 ams->addr);
627 if (ams->map == NULL)
628 return -1;
629 }
630
631 ams->al_addr = ams->map->map_ip(ams->map, ams->addr);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300632 ams->sym = map__find_symbol(ams->map, ams->al_addr);
Arnaldo Carvalho de Melo4e987712013-10-14 13:43:38 +0300633
634 return ams->sym ? 0 : -1;
635}
636
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300637static size_t maps__fprintf(struct maps *maps, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300638{
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300639 size_t printed = 0;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300640 struct rb_node *nd;
641
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300642 down_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300643
644 for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300645 struct map *pos = rb_entry(nd, struct map, rb_node);
646 printed += fprintf(fp, "Map:");
647 printed += map__fprintf(pos, fp);
648 if (verbose > 2) {
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300649 printed += dso__fprintf(pos->dso, pos->type, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300650 printed += fprintf(fp, "--\n");
651 }
652 }
653
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300654 up_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300655
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300656 return printed;
657}
658
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300659size_t __map_groups__fprintf_maps(struct map_groups *mg, enum map_type type,
660 FILE *fp)
661{
662 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
663 return printed += maps__fprintf(&mg->maps[type], fp);
664}
665
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300666size_t map_groups__fprintf(struct map_groups *mg, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300667{
668 size_t printed = 0, i;
669 for (i = 0; i < MAP__NR_TYPES; ++i)
Jiri Olsaacebd402014-07-14 23:46:47 +0200670 printed += __map_groups__fprintf_maps(mg, i, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300671 return printed;
672}
673
Jiri Olsacb8382e2015-11-04 15:08:11 +0100674static void __map_groups__insert(struct map_groups *mg, struct map *map)
675{
676 __maps__insert(&mg->maps[map->type], map);
677 map->groups = mg;
678}
679
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300680static int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300681{
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300682 struct rb_root *root;
683 struct rb_node *next;
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300684 int err = 0;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300685
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300686 down_write(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300687
688 root = &maps->entries;
689 next = rb_first(root);
690
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300691 while (next) {
692 struct map *pos = rb_entry(next, struct map, rb_node);
693 next = rb_next(&pos->rb_node);
694
695 if (!map__overlap(pos, map))
696 continue;
697
698 if (verbose >= 2) {
Alexis Berlemont21e8c812016-10-12 23:48:23 +0200699
700 if (use_browser) {
701 pr_warning("overlapping maps in %s "
702 "(disable tui for more info)\n",
703 map->dso->name);
704 } else {
705 fputs("overlapping maps:\n", fp);
706 map__fprintf(map, fp);
707 map__fprintf(pos, fp);
708 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300709 }
710
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -0300711 rb_erase_init(&pos->rb_node, root);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300712 /*
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300713 * Now check if we need to create new maps for areas not
714 * overlapped by the new map:
715 */
716 if (map->start > pos->start) {
717 struct map *before = map__clone(pos);
718
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300719 if (before == NULL) {
720 err = -ENOMEM;
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300721 goto put_map;
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300722 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300723
Stephane Eranian77faf4d2014-10-06 10:35:32 +0200724 before->end = map->start;
Jiri Olsacb8382e2015-11-04 15:08:11 +0100725 __map_groups__insert(pos->groups, before);
Alexis Berlemont21e8c812016-10-12 23:48:23 +0200726 if (verbose >= 2 && !use_browser)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300727 map__fprintf(before, fp);
Masami Hiramatsud91130e2015-12-09 11:11:31 +0900728 map__put(before);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300729 }
730
731 if (map->end < pos->end) {
732 struct map *after = map__clone(pos);
733
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300734 if (after == NULL) {
735 err = -ENOMEM;
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300736 goto put_map;
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300737 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300738
Stephane Eranian77faf4d2014-10-06 10:35:32 +0200739 after->start = map->end;
Jiri Olsacb8382e2015-11-04 15:08:11 +0100740 __map_groups__insert(pos->groups, after);
Alexis Berlemont21e8c812016-10-12 23:48:23 +0200741 if (verbose >= 2 && !use_browser)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300742 map__fprintf(after, fp);
Masami Hiramatsud91130e2015-12-09 11:11:31 +0900743 map__put(after);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300744 }
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300745put_map:
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300746 map__put(pos);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300747
748 if (err)
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300749 goto out;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300750 }
751
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300752 err = 0;
753out:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300754 up_write(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300755 return err;
756}
757
758int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
759 FILE *fp)
760{
761 return maps__fixup_overlappings(&mg->maps[map->type], map, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300762}
763
764/*
765 * XXX This should not really _copy_ te maps, but refcount them.
766 */
Jiri Olsa6c502582016-07-04 14:16:23 +0200767int map_groups__clone(struct thread *thread,
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300768 struct map_groups *parent, enum map_type type)
769{
Jiri Olsa6c502582016-07-04 14:16:23 +0200770 struct map_groups *mg = thread->mg;
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300771 int err = -ENOMEM;
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300772 struct map *map;
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300773 struct maps *maps = &parent->maps[type];
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300774
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300775 down_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300776
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300777 for (map = maps__first(maps); map; map = map__next(map)) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300778 struct map *new = map__clone(map);
779 if (new == NULL)
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300780 goto out_unlock;
Jiri Olsa6c502582016-07-04 14:16:23 +0200781
782 err = unwind__prepare_access(thread, new, NULL);
783 if (err)
784 goto out_unlock;
785
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300786 map_groups__insert(mg, new);
Masami Hiramatsubae32b52015-12-09 11:11:20 +0900787 map__put(new);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300788 }
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300789
790 err = 0;
791out_unlock:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300792 up_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300793 return err;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300794}
795
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300796static void __maps__insert(struct maps *maps, struct map *map)
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300797{
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300798 struct rb_node **p = &maps->entries.rb_node;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300799 struct rb_node *parent = NULL;
800 const u64 ip = map->start;
801 struct map *m;
802
803 while (*p != NULL) {
804 parent = *p;
805 m = rb_entry(parent, struct map, rb_node);
806 if (ip < m->start)
807 p = &(*p)->rb_left;
808 else
809 p = &(*p)->rb_right;
810 }
811
812 rb_link_node(&map->rb_node, parent, p);
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300813 rb_insert_color(&map->rb_node, &maps->entries);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300814 map__get(map);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300815}
816
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300817void maps__insert(struct maps *maps, struct map *map)
818{
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300819 down_write(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300820 __maps__insert(maps, map);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300821 up_write(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300822}
823
824static void __maps__remove(struct maps *maps, struct map *map)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300825{
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -0300826 rb_erase_init(&map->rb_node, &maps->entries);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300827 map__put(map);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300828}
829
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300830void maps__remove(struct maps *maps, struct map *map)
831{
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300832 down_write(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300833 __maps__remove(maps, map);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300834 up_write(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300835}
836
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300837struct map *maps__find(struct maps *maps, u64 ip)
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300838{
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300839 struct rb_node **p, *parent = NULL;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300840 struct map *m;
841
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300842 down_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300843
844 p = &maps->entries.rb_node;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300845 while (*p != NULL) {
846 parent = *p;
847 m = rb_entry(parent, struct map, rb_node);
848 if (ip < m->start)
849 p = &(*p)->rb_left;
Namhyung Kim4955ea22014-10-14 16:05:38 -0300850 else if (ip >= m->end)
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300851 p = &(*p)->rb_right;
852 else
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300853 goto out;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300854 }
855
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300856 m = NULL;
857out:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300858 up_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300859 return m;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300860}
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300861
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300862struct map *maps__first(struct maps *maps)
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300863{
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300864 struct rb_node *first = rb_first(&maps->entries);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300865
866 if (first)
867 return rb_entry(first, struct map, rb_node);
868 return NULL;
869}
870
Arnaldo Carvalho de Melo4d4dee92015-05-21 17:48:33 -0300871struct map *map__next(struct map *map)
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300872{
873 struct rb_node *next = rb_next(&map->rb_node);
874
875 if (next)
876 return rb_entry(next, struct map, rb_node);
877 return NULL;
878}
Wang Nanba927322015-04-07 08:22:45 +0000879
880struct kmap *map__kmap(struct map *map)
881{
882 if (!map->dso || !map->dso->kernel) {
883 pr_err("Internal error: map__kmap with a non-kernel map\n");
884 return NULL;
885 }
886 return (struct kmap *)(map + 1);
887}
888
889struct map_groups *map__kmaps(struct map *map)
890{
891 struct kmap *kmap = map__kmap(map);
892
893 if (!kmap || !kmap->kmaps) {
894 pr_err("Internal error: map__kmaps with a non-kernel map\n");
895 return NULL;
896 }
897 return kmap->kmaps;
898}