blob: 89ac5b5dc218e874024f41cc2d730cd40152f9f6 [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
Wang Nan0ac33482016-09-06 04:58:27 +000025static inline int is_anon_memory(const char *filename, u32 flags)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020026{
Arnaldo Carvalho de Melofbef1032016-09-12 16:47:57 -030027 return flags & MAP_HUGETLB ||
Wang Nan0ac33482016-09-06 04:58:27 +000028 !strcmp(filename, "//anon") ||
Yannick Brosseaub2be5452015-11-26 03:42:32 -080029 !strncmp(filename, "/dev/zero", sizeof("/dev/zero") - 1) ||
30 !strncmp(filename, "/anon_hugepage", sizeof("/anon_hugepage") - 1);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020031}
32
Jiri Olsa87ffef72011-08-24 15:18:34 +020033static inline int is_no_dso_memory(const char *filename)
34{
Namhyung Kim1e825742012-11-07 16:27:11 +090035 return !strncmp(filename, "[stack", 6) ||
Don Zickus700be562014-08-19 22:31:14 -040036 !strncmp(filename, "/SYSV",5) ||
Jiri Olsa87ffef72011-08-24 15:18:34 +020037 !strcmp(filename, "[heap]");
38}
39
Michael Lentineeca81832014-05-20 11:48:50 +020040static inline int is_android_lib(const char *filename)
41{
42 return !strncmp(filename, "/data/app-lib", 13) ||
43 !strncmp(filename, "/system/lib", 11);
44}
45
46static inline bool replace_android_lib(const char *filename, char *newfilename)
47{
48 const char *libname;
49 char *app_abi;
50 size_t app_abi_length, new_length;
51 size_t lib_length = 0;
52
53 libname = strrchr(filename, '/');
54 if (libname)
55 lib_length = strlen(libname);
56
57 app_abi = getenv("APP_ABI");
58 if (!app_abi)
59 return false;
60
61 app_abi_length = strlen(app_abi);
62
63 if (!strncmp(filename, "/data/app-lib", 13)) {
64 char *apk_path;
65
66 if (!app_abi_length)
67 return false;
68
69 new_length = 7 + app_abi_length + lib_length;
70
71 apk_path = getenv("APK_PATH");
72 if (apk_path) {
73 new_length += strlen(apk_path) + 1;
74 if (new_length > PATH_MAX)
75 return false;
76 snprintf(newfilename, new_length,
77 "%s/libs/%s/%s", apk_path, app_abi, libname);
78 } else {
79 if (new_length > PATH_MAX)
80 return false;
81 snprintf(newfilename, new_length,
82 "libs/%s/%s", app_abi, libname);
83 }
84 return true;
85 }
86
87 if (!strncmp(filename, "/system/lib/", 11)) {
88 char *ndk, *app;
89 const char *arch;
90 size_t ndk_length;
91 size_t app_length;
92
93 ndk = getenv("NDK_ROOT");
94 app = getenv("APP_PLATFORM");
95
96 if (!(ndk && app))
97 return false;
98
99 ndk_length = strlen(ndk);
100 app_length = strlen(app);
101
102 if (!(ndk_length && app_length && app_abi_length))
103 return false;
104
105 arch = !strncmp(app_abi, "arm", 3) ? "arm" :
106 !strncmp(app_abi, "mips", 4) ? "mips" :
107 !strncmp(app_abi, "x86", 3) ? "x86" : NULL;
108
109 if (!arch)
110 return false;
111
112 new_length = 27 + ndk_length +
113 app_length + lib_length
114 + strlen(arch);
115
116 if (new_length > PATH_MAX)
117 return false;
118 snprintf(newfilename, new_length,
119 "%s/platforms/%s/arch-%s/usr/lib/%s",
120 ndk, app, arch, libname);
121
122 return true;
123 }
124 return false;
125}
126
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300127void map__init(struct map *map, u64 start, u64 end, u64 pgoff, struct dso *dso)
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200128{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300129 map->start = start;
130 map->end = end;
131 map->pgoff = pgoff;
Adrian Hunter91767532014-01-29 16:14:36 +0200132 map->reloc = 0;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300133 map->dso = dso__get(dso);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300134 map->map_ip = map__map_ip;
135 map->unmap_ip = map__unmap_ip;
136 RB_CLEAR_NODE(&map->rb_node);
137 map->groups = NULL;
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300138 map->erange_warned = false;
Elena Reshetovae3a42cd2017-02-21 17:34:59 +0200139 refcount_set(&map->refcnt, 1);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200140}
141
Adrian Hunter2a030682014-07-22 16:17:53 +0300142struct map *map__new(struct machine *machine, u64 start, u64 len,
Krister Johansenbf2e7102017-07-05 18:48:09 -0700143 u64 pgoff, u32 d_maj, u32 d_min, u64 ino,
Don Zickus7ef80702014-05-19 15:13:49 -0400144 u64 ino_gen, u32 prot, u32 flags, char *filename,
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300145 struct thread *thread)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200146{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300147 struct map *map = malloc(sizeof(*map));
Krister Johansenbf2e7102017-07-05 18:48:09 -0700148 struct nsinfo *nsi = NULL;
149 struct nsinfo *nnsi;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200150
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300151 if (map != NULL) {
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200152 char newfilename[PATH_MAX];
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200153 struct dso *dso;
Michael Lentineeca81832014-05-20 11:48:50 +0200154 int anon, no_dso, vdso, android;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200155
Michael Lentineeca81832014-05-20 11:48:50 +0200156 android = is_android_lib(filename);
Wang Nan0ac33482016-09-06 04:58:27 +0000157 anon = is_anon_memory(filename, flags);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200158 vdso = is_vdso_map(filename);
Jiri Olsa87ffef72011-08-24 15:18:34 +0200159 no_dso = is_no_dso_memory(filename);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200160
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200161 map->maj = d_maj;
162 map->min = d_min;
163 map->ino = ino;
164 map->ino_generation = ino_gen;
Don Zickus7ef80702014-05-19 15:13:49 -0400165 map->prot = prot;
166 map->flags = flags;
Krister Johansenbf2e7102017-07-05 18:48:09 -0700167 nsi = nsinfo__get(thread->nsinfo);
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200168
Arnaldo Carvalho de Melod183b262018-04-26 16:08:38 -0300169 if ((anon || no_dso) && nsi && (prot & PROT_EXEC)) {
Krister Johansenbf2e7102017-07-05 18:48:09 -0700170 snprintf(newfilename, sizeof(newfilename),
171 "/tmp/perf-%d.map", nsi->pid);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200172 filename = newfilename;
173 }
174
Michael Lentineeca81832014-05-20 11:48:50 +0200175 if (android) {
176 if (replace_android_lib(filename, newfilename))
177 filename = newfilename;
178 }
179
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200180 if (vdso) {
Krister Johansenbf2e7102017-07-05 18:48:09 -0700181 /* The vdso maps are always on the host and not the
182 * container. Ensure that we don't use setns to look
183 * them up.
184 */
185 nnsi = nsinfo__copy(nsi);
186 if (nnsi) {
187 nsinfo__put(nsi);
188 nnsi->need_setns = false;
189 nsi = nnsi;
190 }
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200191 pgoff = 0;
Arnaldo Carvalho de Melo9a4388c2015-05-29 11:54:08 -0300192 dso = machine__findnew_vdso(machine, thread);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200193 } else
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -0300194 dso = machine__findnew_dso(machine, filename);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200195
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200196 if (dso == NULL)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200197 goto out_delete;
198
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300199 map__init(map, start, start + len, pgoff, dso);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200200
Jiri Olsa87ffef72011-08-24 15:18:34 +0200201 if (anon || no_dso) {
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300202 map->map_ip = map->unmap_ip = identity__map_ip;
Jiri Olsa87ffef72011-08-24 15:18:34 +0200203
204 /*
205 * Set memory without DSO as loaded. All map__find_*
206 * functions still return NULL, and we avoid the
207 * unnecessary map__load warning.
208 */
Arnaldo Carvalho de Melod183b262018-04-26 16:08:38 -0300209 if (!(prot & PROT_EXEC))
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300210 dso__set_loaded(dso);
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -0200211 }
Krister Johansenbf2e7102017-07-05 18:48:09 -0700212 dso->nsinfo = nsi;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300213 dso__put(dso);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200214 }
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300215 return map;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200216out_delete:
Krister Johansenbf2e7102017-07-05 18:48:09 -0700217 nsinfo__put(nsi);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300218 free(map);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200219 return NULL;
220}
221
Namhyung Kime5a18452012-08-06 13:41:20 +0900222/*
223 * Constructor variant for modules (where we know from /proc/modules where
224 * they are loaded) and for vmlinux, where only after we load all the
225 * symbols we'll know where it starts and ends.
226 */
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300227struct map *map__new2(u64 start, struct dso *dso)
Namhyung Kime5a18452012-08-06 13:41:20 +0900228{
229 struct map *map = calloc(1, (sizeof(*map) +
230 (dso->kernel ? sizeof(struct kmap) : 0)));
231 if (map != NULL) {
232 /*
233 * ->end will be filled after we load all the symbols
234 */
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300235 map__init(map, start, 0, 0, dso);
Namhyung Kime5a18452012-08-06 13:41:20 +0900236 }
237
238 return map;
239}
240
Arnaldo Carvalho de Meloe6ce71262015-07-23 11:06:16 -0300241/*
242 * Use this and __map__is_kmodule() for map instances that are in
243 * machine->kmaps, and thus have map->groups->machine all properly set, to
244 * disambiguate between the kernel and modules.
245 *
246 * When the need arises, introduce map__is_{kernel,kmodule)() that
247 * checks (map->groups != NULL && map->groups->machine != NULL &&
248 * map->dso->kernel) before calling __map__is_{kernel,kmodule}())
249 */
250bool __map__is_kernel(const struct map *map)
251{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300252 return machine__kernel_map(map->groups->machine) == map;
Arnaldo Carvalho de Meloe6ce71262015-07-23 11:06:16 -0300253}
254
Adrian Hunter5759a682018-05-22 13:54:35 +0300255bool __map__is_extra_kernel_map(const struct map *map)
256{
257 struct kmap *kmap = __map__kmap((struct map *)map);
258
259 return kmap && kmap->name[0];
260}
261
Arnaldo Carvalho de Meloe94b8612018-04-23 17:13:49 -0300262bool map__has_symbols(const struct map *map)
263{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300264 return dso__has_symbols(map->dso);
Arnaldo Carvalho de Meloe94b8612018-04-23 17:13:49 -0300265}
266
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300267static void map__exit(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200268{
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -0300269 BUG_ON(!RB_EMPTY_NODE(&map->rb_node));
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300270 dso__zput(map->dso);
271}
272
273void map__delete(struct map *map)
274{
275 map__exit(map);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300276 free(map);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200277}
278
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300279void map__put(struct map *map)
280{
Elena Reshetovae3a42cd2017-02-21 17:34:59 +0200281 if (map && refcount_dec_and_test(&map->refcnt))
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300282 map__delete(map);
283}
284
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300285void map__fixup_start(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200286{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300287 struct rb_root *symbols = &map->dso->symbols;
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200288 struct rb_node *nd = rb_first(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200289 if (nd != NULL) {
290 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300291 map->start = sym->start;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200292 }
293}
294
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300295void map__fixup_end(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200296{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300297 struct rb_root *symbols = &map->dso->symbols;
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200298 struct rb_node *nd = rb_last(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200299 if (nd != NULL) {
300 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300301 map->end = sym->end;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200302 }
303}
304
Arnaldo Carvalho de Melod70a5402009-10-30 16:28:25 -0200305#define DSO__DELETED "(deleted)"
306
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300307int map__load(struct map *map)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200308{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300309 const char *name = map->dso->long_name;
Masami Hiramatsua1281682009-12-15 10:32:33 -0500310 int nr;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200311
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300312 if (dso__loaded(map->dso))
Masami Hiramatsua1281682009-12-15 10:32:33 -0500313 return 0;
314
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300315 nr = dso__load(map->dso, map);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200316 if (nr < 0) {
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300317 if (map->dso->has_build_id) {
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +0900318 char sbuild_id[SBUILD_ID_SIZE];
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200319
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300320 build_id__sprintf(map->dso->build_id,
321 sizeof(map->dso->build_id),
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200322 sbuild_id);
323 pr_warning("%s with build id %s not found",
324 name, sbuild_id);
325 } else
326 pr_warning("Failed to open %s", name);
327
328 pr_warning(", continuing without symbols\n");
329 return -1;
330 } else if (nr == 0) {
Ingo Molnar89fe8082013-09-30 12:07:11 +0200331#ifdef HAVE_LIBELF_SUPPORT
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200332 const size_t len = strlen(name);
333 const size_t real_len = len - sizeof(DSO__DELETED);
334
335 if (len > sizeof(DSO__DELETED) &&
336 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
David Aherne77b15b2011-10-18 18:44:45 -0600337 pr_warning("%.*s was updated (is prelink enabled?). "
338 "Restart the long running apps that use it!\n",
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200339 (int)real_len, name);
340 } else {
341 pr_warning("no symbols found in %s, maybe install "
342 "a debug package?\n", name);
343 }
Namhyung Kim393be2e2012-08-06 13:41:21 +0900344#endif
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200345 return -1;
346 }
347
348 return 0;
349}
350
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300351struct symbol *map__find_symbol(struct map *map, u64 addr)
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200352{
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300353 if (map__load(map) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200354 return NULL;
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200355
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300356 return dso__find_symbol(map->dso, addr);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200357}
358
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300359struct symbol *map__find_symbol_by_name(struct map *map, const char *name)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200360{
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300361 if (map__load(map) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200362 return NULL;
363
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300364 if (!dso__sorted_by_name(map->dso))
365 dso__sort_by_name(map->dso);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200366
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300367 return dso__find_symbol_by_name(map->dso, name);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200368}
369
Arnaldo Carvalho de Melo66671d02015-08-18 15:19:50 -0300370struct map *map__clone(struct map *from)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200371{
Arnaldo Carvalho de Melo66671d02015-08-18 15:19:50 -0300372 struct map *map = memdup(from, sizeof(*map));
373
374 if (map != NULL) {
Elena Reshetovae3a42cd2017-02-21 17:34:59 +0200375 refcount_set(&map->refcnt, 1);
Arnaldo Carvalho de Melo66671d02015-08-18 15:19:50 -0300376 RB_CLEAR_NODE(&map->rb_node);
377 dso__get(map->dso);
378 map->groups = NULL;
379 }
380
381 return map;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200382}
383
384int map__overlap(struct map *l, struct map *r)
385{
386 if (l->start > r->start) {
387 struct map *t = l;
388 l = r;
389 r = t;
390 }
391
392 if (l->end > r->start)
393 return 1;
394
395 return 0;
396}
397
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300398size_t map__fprintf(struct map *map, FILE *fp)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200399{
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200400 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300401 map->start, map->end, map->pgoff, map->dso->name);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200402}
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200403
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900404size_t map__fprintf_dsoname(struct map *map, FILE *fp)
405{
Feng Tang8f28f192012-08-27 15:38:26 +0800406 const char *dsoname = "[unknown]";
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900407
Arnaldo Carvalho de Melo5eae7d82017-02-13 17:11:03 -0300408 if (map && map->dso) {
Akihiro Nagai0bc8d202012-01-30 13:43:20 +0900409 if (symbol_conf.show_kernel_path && map->dso->long_name)
410 dsoname = map->dso->long_name;
Arnaldo Carvalho de Melo5eae7d82017-02-13 17:11:03 -0300411 else
Akihiro Nagai0bc8d202012-01-30 13:43:20 +0900412 dsoname = map->dso->name;
Feng Tang8f28f192012-08-27 15:38:26 +0800413 }
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900414
415 return fprintf(fp, "%s", dsoname);
416}
417
Arnaldo Carvalho de Meloe2d88aa2018-05-28 11:05:20 -0300418char *map__srcline(struct map *map, u64 addr, struct symbol *sym)
419{
420 if (map == NULL)
421 return SRCLINE_UNKNOWN;
422 return get_srcline(map->dso, map__rip_2objdump(map, addr), sym, true, true, addr);
423}
424
Adrian Huntercc8fae12013-12-06 09:42:57 +0200425int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
426 FILE *fp)
427{
Adrian Huntercc8fae12013-12-06 09:42:57 +0200428 int ret = 0;
429
430 if (map && map->dso) {
Arnaldo Carvalho de Meloe2d88aa2018-05-28 11:05:20 -0300431 char *srcline = map__srcline(map, addr, NULL);
Adrian Huntercc8fae12013-12-06 09:42:57 +0200432 if (srcline != SRCLINE_UNKNOWN)
433 ret = fprintf(fp, "%s%s", prefix, srcline);
434 free_srcline(srcline);
435 }
436 return ret;
437}
438
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300439/**
440 * map__rip_2objdump - convert symbol start address to objdump address.
441 * @map: memory map
442 * @rip: symbol start address
443 *
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200444 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300445 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
446 * relative to section start.
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300447 *
448 * Return: Address suitable for passing to "objdump --start-address="
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200449 */
450u64 map__rip_2objdump(struct map *map, u64 rip)
451{
Adrian Hunter97802f32018-06-05 10:30:00 +0300452 struct kmap *kmap = __map__kmap(map);
453
454 /*
455 * vmlinux does not have program headers for PTI entry trampolines and
456 * kcore may not either. However the trampoline object code is on the
457 * main kernel map, so just use that instead.
458 */
459 if (kmap && is_entry_trampoline(kmap->name) && kmap->kmaps && kmap->kmaps->machine) {
460 struct map *kernel_map = machine__kernel_map(kmap->kmaps->machine);
461
462 if (kernel_map)
463 map = kernel_map;
464 }
465
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300466 if (!map->dso->adjust_symbols)
467 return rip;
468
469 if (map->dso->rel)
470 return rip - map->pgoff;
471
Wang Nana58f7032016-04-07 10:24:30 +0000472 /*
473 * kernel modules also have DSO_TYPE_USER in dso->kernel,
474 * but all kernel modules are ET_REL, so won't get here.
475 */
476 if (map->dso->kernel == DSO_TYPE_USER)
477 return rip + map->dso->text_offset;
478
Adrian Hunter91767532014-01-29 16:14:36 +0200479 return map->unmap_ip(map, rip) - map->reloc;
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200480}
Kirill Smelkovee11b902010-02-07 11:46:15 -0200481
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300482/**
483 * map__objdump_2mem - convert objdump address to a memory address.
484 * @map: memory map
485 * @ip: objdump address
486 *
487 * Closely related to map__rip_2objdump(), this function takes an address from
488 * objdump and converts it to a memory address. Note this assumes that @map
489 * contains the address. To be sure the result is valid, check it forwards
490 * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
491 *
492 * Return: Memory address.
493 */
494u64 map__objdump_2mem(struct map *map, u64 ip)
495{
496 if (!map->dso->adjust_symbols)
497 return map->unmap_ip(map, ip);
498
499 if (map->dso->rel)
500 return map->unmap_ip(map, ip + map->pgoff);
501
Wang Nana58f7032016-04-07 10:24:30 +0000502 /*
503 * kernel modules also have DSO_TYPE_USER in dso->kernel,
504 * but all kernel modules are ET_REL, so won't get here.
505 */
506 if (map->dso->kernel == DSO_TYPE_USER)
507 return map->unmap_ip(map, ip - map->dso->text_offset);
508
Adrian Hunter91767532014-01-29 16:14:36 +0200509 return ip + map->reloc;
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300510}
511
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300512static void maps__init(struct maps *maps)
513{
514 maps->entries = RB_ROOT;
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300515 init_rwsem(&maps->lock);
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300516}
517
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300518void map_groups__init(struct map_groups *mg, struct machine *machine)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300519{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300520 maps__init(&mg->maps);
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300521 mg->machine = machine;
Elena Reshetovaead05e82017-02-21 17:35:00 +0200522 refcount_set(&mg->refcnt, 1);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300523}
524
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300525static void __maps__purge(struct maps *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300526{
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300527 struct rb_root *root = &maps->entries;
528 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300529
530 while (next) {
531 struct map *pos = rb_entry(next, struct map, rb_node);
532
533 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -0300534 rb_erase_init(&pos->rb_node, root);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300535 map__put(pos);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300536 }
537}
538
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300539static void maps__exit(struct maps *maps)
540{
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300541 down_write(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300542 __maps__purge(maps);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300543 up_write(&maps->lock);
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300544}
545
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300546void map_groups__exit(struct map_groups *mg)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300547{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300548 maps__exit(&mg->maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300549}
550
Adrian Hunter29ce3612014-07-16 11:07:13 +0300551bool map_groups__empty(struct map_groups *mg)
552{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300553 return !maps__first(&mg->maps);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300554}
555
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300556struct map_groups *map_groups__new(struct machine *machine)
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300557{
558 struct map_groups *mg = malloc(sizeof(*mg));
559
560 if (mg != NULL)
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300561 map_groups__init(mg, machine);
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300562
563 return mg;
564}
565
566void map_groups__delete(struct map_groups *mg)
567{
568 map_groups__exit(mg);
569 free(mg);
570}
571
Arnaldo Carvalho de Meloa26ca672014-03-25 15:26:44 -0300572void map_groups__put(struct map_groups *mg)
573{
Elena Reshetovaead05e82017-02-21 17:35:00 +0200574 if (mg && refcount_dec_and_test(&mg->refcnt))
Arnaldo Carvalho de Meloa26ca672014-03-25 15:26:44 -0300575 map_groups__delete(mg);
576}
577
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300578struct symbol *map_groups__find_symbol(struct map_groups *mg,
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300579 u64 addr, struct map **mapp)
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300580{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300581 struct map *map = map_groups__find(mg, addr);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300582
Masami Hiramatsu4afc81c2014-01-23 02:29:50 +0000583 /* Ensure map is loaded before using map->map_ip */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300584 if (map != NULL && map__load(map) >= 0) {
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300585 if (mapp != NULL)
586 *mapp = map;
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300587 return map__find_symbol(map, map->map_ip(map, addr));
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300588 }
589
590 return NULL;
591}
592
Arnaldo Carvalho de Melob7f9ff52015-09-29 17:34:46 -0300593struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300594 struct map **mapp)
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300595{
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300596 struct symbol *sym;
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300597 struct rb_node *nd;
598
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300599 down_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300600
601 for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300602 struct map *pos = rb_entry(nd, struct map, rb_node);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300603
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300604 sym = map__find_symbol_by_name(pos, name);
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300605
606 if (sym == NULL)
607 continue;
608 if (mapp != NULL)
609 *mapp = pos;
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300610 goto out;
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300611 }
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300612
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300613 sym = NULL;
614out:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300615 up_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300616 return sym;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300617}
618
Arnaldo Carvalho de Melob7f9ff52015-09-29 17:34:46 -0300619struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melob7f9ff52015-09-29 17:34:46 -0300620 const char *name,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300621 struct map **mapp)
Arnaldo Carvalho de Melob7f9ff52015-09-29 17:34:46 -0300622{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300623 return maps__find_symbol_by_name(&mg->maps, name, mapp);
Arnaldo Carvalho de Melob7f9ff52015-09-29 17:34:46 -0300624}
625
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300626int map_groups__find_ams(struct addr_map_symbol *ams)
Arnaldo Carvalho de Melo4e987712013-10-14 13:43:38 +0300627{
Stephane Eranian77faf4d2014-10-06 10:35:32 +0200628 if (ams->addr < ams->map->start || ams->addr >= ams->map->end) {
Arnaldo Carvalho de Melo4e987712013-10-14 13:43:38 +0300629 if (ams->map->groups == NULL)
630 return -1;
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300631 ams->map = map_groups__find(ams->map->groups, ams->addr);
Arnaldo Carvalho de Melo4e987712013-10-14 13:43:38 +0300632 if (ams->map == NULL)
633 return -1;
634 }
635
636 ams->al_addr = ams->map->map_ip(ams->map, ams->addr);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300637 ams->sym = map__find_symbol(ams->map, ams->al_addr);
Arnaldo Carvalho de Melo4e987712013-10-14 13:43:38 +0300638
639 return ams->sym ? 0 : -1;
640}
641
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300642static size_t maps__fprintf(struct maps *maps, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300643{
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300644 size_t printed = 0;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300645 struct rb_node *nd;
646
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300647 down_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300648
649 for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300650 struct map *pos = rb_entry(nd, struct map, rb_node);
651 printed += fprintf(fp, "Map:");
652 printed += map__fprintf(pos, fp);
653 if (verbose > 2) {
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300654 printed += dso__fprintf(pos->dso, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300655 printed += fprintf(fp, "--\n");
656 }
657 }
658
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300659 up_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300660
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300661 return printed;
662}
663
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300664size_t map_groups__fprintf(struct map_groups *mg, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300665{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300666 return maps__fprintf(&mg->maps, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300667}
668
Jiri Olsacb8382e2015-11-04 15:08:11 +0100669static void __map_groups__insert(struct map_groups *mg, struct map *map)
670{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300671 __maps__insert(&mg->maps, map);
Jiri Olsacb8382e2015-11-04 15:08:11 +0100672 map->groups = mg;
673}
674
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300675static int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300676{
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300677 struct rb_root *root;
678 struct rb_node *next;
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300679 int err = 0;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300680
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300681 down_write(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300682
683 root = &maps->entries;
684 next = rb_first(root);
685
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300686 while (next) {
687 struct map *pos = rb_entry(next, struct map, rb_node);
688 next = rb_next(&pos->rb_node);
689
690 if (!map__overlap(pos, map))
691 continue;
692
693 if (verbose >= 2) {
Alexis Berlemont21e8c812016-10-12 23:48:23 +0200694
695 if (use_browser) {
696 pr_warning("overlapping maps in %s "
697 "(disable tui for more info)\n",
698 map->dso->name);
699 } else {
700 fputs("overlapping maps:\n", fp);
701 map__fprintf(map, fp);
702 map__fprintf(pos, fp);
703 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300704 }
705
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -0300706 rb_erase_init(&pos->rb_node, root);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300707 /*
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300708 * Now check if we need to create new maps for areas not
709 * overlapped by the new map:
710 */
711 if (map->start > pos->start) {
712 struct map *before = map__clone(pos);
713
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300714 if (before == NULL) {
715 err = -ENOMEM;
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300716 goto put_map;
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300717 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300718
Stephane Eranian77faf4d2014-10-06 10:35:32 +0200719 before->end = map->start;
Jiri Olsacb8382e2015-11-04 15:08:11 +0100720 __map_groups__insert(pos->groups, before);
Alexis Berlemont21e8c812016-10-12 23:48:23 +0200721 if (verbose >= 2 && !use_browser)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300722 map__fprintf(before, fp);
Masami Hiramatsud91130e2015-12-09 11:11:31 +0900723 map__put(before);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300724 }
725
726 if (map->end < pos->end) {
727 struct map *after = map__clone(pos);
728
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300729 if (after == NULL) {
730 err = -ENOMEM;
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300731 goto put_map;
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300732 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300733
Stephane Eranian77faf4d2014-10-06 10:35:32 +0200734 after->start = map->end;
Jiri Olsacb8382e2015-11-04 15:08:11 +0100735 __map_groups__insert(pos->groups, after);
Alexis Berlemont21e8c812016-10-12 23:48:23 +0200736 if (verbose >= 2 && !use_browser)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300737 map__fprintf(after, fp);
Masami Hiramatsud91130e2015-12-09 11:11:31 +0900738 map__put(after);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300739 }
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300740put_map:
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300741 map__put(pos);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300742
743 if (err)
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300744 goto out;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300745 }
746
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300747 err = 0;
748out:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300749 up_write(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300750 return err;
751}
752
753int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
754 FILE *fp)
755{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300756 return maps__fixup_overlappings(&mg->maps, map, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300757}
758
759/*
760 * XXX This should not really _copy_ te maps, but refcount them.
761 */
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300762int map_groups__clone(struct thread *thread, struct map_groups *parent)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300763{
Jiri Olsa6c502582016-07-04 14:16:23 +0200764 struct map_groups *mg = thread->mg;
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300765 int err = -ENOMEM;
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300766 struct map *map;
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300767 struct maps *maps = &parent->maps;
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300768
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300769 down_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300770
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300771 for (map = maps__first(maps); map; map = map__next(map)) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300772 struct map *new = map__clone(map);
773 if (new == NULL)
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300774 goto out_unlock;
Jiri Olsa6c502582016-07-04 14:16:23 +0200775
776 err = unwind__prepare_access(thread, new, NULL);
777 if (err)
778 goto out_unlock;
779
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300780 map_groups__insert(mg, new);
Masami Hiramatsubae32b52015-12-09 11:11:20 +0900781 map__put(new);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300782 }
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300783
784 err = 0;
785out_unlock:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300786 up_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300787 return err;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300788}
789
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300790static void __maps__insert(struct maps *maps, struct map *map)
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300791{
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300792 struct rb_node **p = &maps->entries.rb_node;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300793 struct rb_node *parent = NULL;
794 const u64 ip = map->start;
795 struct map *m;
796
797 while (*p != NULL) {
798 parent = *p;
799 m = rb_entry(parent, struct map, rb_node);
800 if (ip < m->start)
801 p = &(*p)->rb_left;
802 else
803 p = &(*p)->rb_right;
804 }
805
806 rb_link_node(&map->rb_node, parent, p);
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300807 rb_insert_color(&map->rb_node, &maps->entries);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300808 map__get(map);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300809}
810
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300811void maps__insert(struct maps *maps, struct map *map)
812{
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300813 down_write(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300814 __maps__insert(maps, map);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300815 up_write(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300816}
817
818static void __maps__remove(struct maps *maps, struct map *map)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300819{
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -0300820 rb_erase_init(&map->rb_node, &maps->entries);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300821 map__put(map);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300822}
823
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300824void maps__remove(struct maps *maps, struct map *map)
825{
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300826 down_write(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300827 __maps__remove(maps, map);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300828 up_write(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300829}
830
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300831struct map *maps__find(struct maps *maps, u64 ip)
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300832{
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300833 struct rb_node **p, *parent = NULL;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300834 struct map *m;
835
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300836 down_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300837
838 p = &maps->entries.rb_node;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300839 while (*p != NULL) {
840 parent = *p;
841 m = rb_entry(parent, struct map, rb_node);
842 if (ip < m->start)
843 p = &(*p)->rb_left;
Namhyung Kim4955ea22014-10-14 16:05:38 -0300844 else if (ip >= m->end)
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300845 p = &(*p)->rb_right;
846 else
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300847 goto out;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300848 }
849
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300850 m = NULL;
851out:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300852 up_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300853 return m;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300854}
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300855
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300856struct map *maps__first(struct maps *maps)
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300857{
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300858 struct rb_node *first = rb_first(&maps->entries);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300859
860 if (first)
861 return rb_entry(first, struct map, rb_node);
862 return NULL;
863}
864
Arnaldo Carvalho de Melo4d4dee92015-05-21 17:48:33 -0300865struct map *map__next(struct map *map)
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300866{
867 struct rb_node *next = rb_next(&map->rb_node);
868
869 if (next)
870 return rb_entry(next, struct map, rb_node);
871 return NULL;
872}
Wang Nanba927322015-04-07 08:22:45 +0000873
Adrian Hunter5759a682018-05-22 13:54:35 +0300874struct kmap *__map__kmap(struct map *map)
875{
876 if (!map->dso || !map->dso->kernel)
877 return NULL;
878 return (struct kmap *)(map + 1);
879}
880
Wang Nanba927322015-04-07 08:22:45 +0000881struct kmap *map__kmap(struct map *map)
882{
Adrian Hunter5759a682018-05-22 13:54:35 +0300883 struct kmap *kmap = __map__kmap(map);
884
885 if (!kmap)
Wang Nanba927322015-04-07 08:22:45 +0000886 pr_err("Internal error: map__kmap with a non-kernel map\n");
Adrian Hunter5759a682018-05-22 13:54:35 +0300887 return kmap;
Wang Nanba927322015-04-07 08:22:45 +0000888}
889
890struct map_groups *map__kmaps(struct map *map)
891{
892 struct kmap *kmap = map__kmap(map);
893
894 if (!kmap || !kmap->kmaps) {
895 pr_err("Internal error: map__kmaps with a non-kernel map\n");
896 return NULL;
897 }
898 return kmap->kmaps;
899}