blob: cf407cc9d91522e0ae63ad60720f8f3ebc641286 [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"
Andi Kleendd2e18e2018-12-03 16:18:48 -080022#include "srccode.h"
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020023
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -030024static void __maps__insert(struct maps *maps, struct map *map);
Eric Saint-Etienne1e628562018-11-21 09:51:19 -080025static void __maps__insert_name(struct maps *maps, struct map *map);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -030026
Wang Nan0ac33482016-09-06 04:58:27 +000027static inline int is_anon_memory(const char *filename, u32 flags)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020028{
Arnaldo Carvalho de Melofbef1032016-09-12 16:47:57 -030029 return flags & MAP_HUGETLB ||
Wang Nan0ac33482016-09-06 04:58:27 +000030 !strcmp(filename, "//anon") ||
Yannick Brosseaub2be5452015-11-26 03:42:32 -080031 !strncmp(filename, "/dev/zero", sizeof("/dev/zero") - 1) ||
32 !strncmp(filename, "/anon_hugepage", sizeof("/anon_hugepage") - 1);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020033}
34
Jiri Olsa87ffef72011-08-24 15:18:34 +020035static inline int is_no_dso_memory(const char *filename)
36{
Namhyung Kim1e825742012-11-07 16:27:11 +090037 return !strncmp(filename, "[stack", 6) ||
Don Zickus700be562014-08-19 22:31:14 -040038 !strncmp(filename, "/SYSV",5) ||
Jiri Olsa87ffef72011-08-24 15:18:34 +020039 !strcmp(filename, "[heap]");
40}
41
Michael Lentineeca81832014-05-20 11:48:50 +020042static inline int is_android_lib(const char *filename)
43{
44 return !strncmp(filename, "/data/app-lib", 13) ||
45 !strncmp(filename, "/system/lib", 11);
46}
47
48static inline bool replace_android_lib(const char *filename, char *newfilename)
49{
50 const char *libname;
51 char *app_abi;
52 size_t app_abi_length, new_length;
53 size_t lib_length = 0;
54
55 libname = strrchr(filename, '/');
56 if (libname)
57 lib_length = strlen(libname);
58
59 app_abi = getenv("APP_ABI");
60 if (!app_abi)
61 return false;
62
63 app_abi_length = strlen(app_abi);
64
65 if (!strncmp(filename, "/data/app-lib", 13)) {
66 char *apk_path;
67
68 if (!app_abi_length)
69 return false;
70
71 new_length = 7 + app_abi_length + lib_length;
72
73 apk_path = getenv("APK_PATH");
74 if (apk_path) {
75 new_length += strlen(apk_path) + 1;
76 if (new_length > PATH_MAX)
77 return false;
78 snprintf(newfilename, new_length,
79 "%s/libs/%s/%s", apk_path, app_abi, libname);
80 } else {
81 if (new_length > PATH_MAX)
82 return false;
83 snprintf(newfilename, new_length,
84 "libs/%s/%s", app_abi, libname);
85 }
86 return true;
87 }
88
89 if (!strncmp(filename, "/system/lib/", 11)) {
90 char *ndk, *app;
91 const char *arch;
92 size_t ndk_length;
93 size_t app_length;
94
95 ndk = getenv("NDK_ROOT");
96 app = getenv("APP_PLATFORM");
97
98 if (!(ndk && app))
99 return false;
100
101 ndk_length = strlen(ndk);
102 app_length = strlen(app);
103
104 if (!(ndk_length && app_length && app_abi_length))
105 return false;
106
107 arch = !strncmp(app_abi, "arm", 3) ? "arm" :
108 !strncmp(app_abi, "mips", 4) ? "mips" :
109 !strncmp(app_abi, "x86", 3) ? "x86" : NULL;
110
111 if (!arch)
112 return false;
113
114 new_length = 27 + ndk_length +
115 app_length + lib_length
116 + strlen(arch);
117
118 if (new_length > PATH_MAX)
119 return false;
120 snprintf(newfilename, new_length,
121 "%s/platforms/%s/arch-%s/usr/lib/%s",
122 ndk, app, arch, libname);
123
124 return true;
125 }
126 return false;
127}
128
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300129void map__init(struct map *map, u64 start, u64 end, u64 pgoff, struct dso *dso)
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200130{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300131 map->start = start;
132 map->end = end;
133 map->pgoff = pgoff;
Adrian Hunter91767532014-01-29 16:14:36 +0200134 map->reloc = 0;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300135 map->dso = dso__get(dso);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300136 map->map_ip = map__map_ip;
137 map->unmap_ip = map__unmap_ip;
138 RB_CLEAR_NODE(&map->rb_node);
139 map->groups = NULL;
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300140 map->erange_warned = false;
Elena Reshetovae3a42cd2017-02-21 17:34:59 +0200141 refcount_set(&map->refcnt, 1);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200142}
143
Adrian Hunter2a030682014-07-22 16:17:53 +0300144struct map *map__new(struct machine *machine, u64 start, u64 len,
Krister Johansenbf2e7102017-07-05 18:48:09 -0700145 u64 pgoff, u32 d_maj, u32 d_min, u64 ino,
Don Zickus7ef80702014-05-19 15:13:49 -0400146 u64 ino_gen, u32 prot, u32 flags, char *filename,
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300147 struct thread *thread)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200148{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300149 struct map *map = malloc(sizeof(*map));
Krister Johansenbf2e7102017-07-05 18:48:09 -0700150 struct nsinfo *nsi = NULL;
151 struct nsinfo *nnsi;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200152
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300153 if (map != NULL) {
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200154 char newfilename[PATH_MAX];
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200155 struct dso *dso;
Michael Lentineeca81832014-05-20 11:48:50 +0200156 int anon, no_dso, vdso, android;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200157
Michael Lentineeca81832014-05-20 11:48:50 +0200158 android = is_android_lib(filename);
Wang Nan0ac33482016-09-06 04:58:27 +0000159 anon = is_anon_memory(filename, flags);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200160 vdso = is_vdso_map(filename);
Jiri Olsa87ffef72011-08-24 15:18:34 +0200161 no_dso = is_no_dso_memory(filename);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200162
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200163 map->maj = d_maj;
164 map->min = d_min;
165 map->ino = ino;
166 map->ino_generation = ino_gen;
Don Zickus7ef80702014-05-19 15:13:49 -0400167 map->prot = prot;
168 map->flags = flags;
Krister Johansenbf2e7102017-07-05 18:48:09 -0700169 nsi = nsinfo__get(thread->nsinfo);
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200170
Arnaldo Carvalho de Melod183b262018-04-26 16:08:38 -0300171 if ((anon || no_dso) && nsi && (prot & PROT_EXEC)) {
Krister Johansenbf2e7102017-07-05 18:48:09 -0700172 snprintf(newfilename, sizeof(newfilename),
173 "/tmp/perf-%d.map", nsi->pid);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200174 filename = newfilename;
175 }
176
Michael Lentineeca81832014-05-20 11:48:50 +0200177 if (android) {
178 if (replace_android_lib(filename, newfilename))
179 filename = newfilename;
180 }
181
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200182 if (vdso) {
Krister Johansenbf2e7102017-07-05 18:48:09 -0700183 /* The vdso maps are always on the host and not the
184 * container. Ensure that we don't use setns to look
185 * them up.
186 */
187 nnsi = nsinfo__copy(nsi);
188 if (nnsi) {
189 nsinfo__put(nsi);
190 nnsi->need_setns = false;
191 nsi = nnsi;
192 }
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200193 pgoff = 0;
Arnaldo Carvalho de Melo9a4388c2015-05-29 11:54:08 -0300194 dso = machine__findnew_vdso(machine, thread);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200195 } else
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -0300196 dso = machine__findnew_dso(machine, filename);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200197
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200198 if (dso == NULL)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200199 goto out_delete;
200
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300201 map__init(map, start, start + len, pgoff, dso);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -0200202
Jiri Olsa87ffef72011-08-24 15:18:34 +0200203 if (anon || no_dso) {
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300204 map->map_ip = map->unmap_ip = identity__map_ip;
Jiri Olsa87ffef72011-08-24 15:18:34 +0200205
206 /*
207 * Set memory without DSO as loaded. All map__find_*
208 * functions still return NULL, and we avoid the
209 * unnecessary map__load warning.
210 */
Arnaldo Carvalho de Melod183b262018-04-26 16:08:38 -0300211 if (!(prot & PROT_EXEC))
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300212 dso__set_loaded(dso);
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -0200213 }
Krister Johansenbf2e7102017-07-05 18:48:09 -0700214 dso->nsinfo = nsi;
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300215 dso__put(dso);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200216 }
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300217 return map;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200218out_delete:
Krister Johansenbf2e7102017-07-05 18:48:09 -0700219 nsinfo__put(nsi);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300220 free(map);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200221 return NULL;
222}
223
Namhyung Kime5a18452012-08-06 13:41:20 +0900224/*
225 * Constructor variant for modules (where we know from /proc/modules where
226 * they are loaded) and for vmlinux, where only after we load all the
227 * symbols we'll know where it starts and ends.
228 */
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300229struct map *map__new2(u64 start, struct dso *dso)
Namhyung Kime5a18452012-08-06 13:41:20 +0900230{
231 struct map *map = calloc(1, (sizeof(*map) +
232 (dso->kernel ? sizeof(struct kmap) : 0)));
233 if (map != NULL) {
234 /*
235 * ->end will be filled after we load all the symbols
236 */
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300237 map__init(map, start, 0, 0, dso);
Namhyung Kime5a18452012-08-06 13:41:20 +0900238 }
239
240 return map;
241}
242
Arnaldo Carvalho de Meloe6ce71262015-07-23 11:06:16 -0300243/*
244 * Use this and __map__is_kmodule() for map instances that are in
245 * machine->kmaps, and thus have map->groups->machine all properly set, to
246 * disambiguate between the kernel and modules.
247 *
248 * When the need arises, introduce map__is_{kernel,kmodule)() that
249 * checks (map->groups != NULL && map->groups->machine != NULL &&
250 * map->dso->kernel) before calling __map__is_{kernel,kmodule}())
251 */
252bool __map__is_kernel(const struct map *map)
253{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300254 return machine__kernel_map(map->groups->machine) == map;
Arnaldo Carvalho de Meloe6ce71262015-07-23 11:06:16 -0300255}
256
Adrian Hunter5759a682018-05-22 13:54:35 +0300257bool __map__is_extra_kernel_map(const struct map *map)
258{
259 struct kmap *kmap = __map__kmap((struct map *)map);
260
261 return kmap && kmap->name[0];
262}
263
Arnaldo Carvalho de Meloe94b8612018-04-23 17:13:49 -0300264bool map__has_symbols(const struct map *map)
265{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300266 return dso__has_symbols(map->dso);
Arnaldo Carvalho de Meloe94b8612018-04-23 17:13:49 -0300267}
268
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300269static void map__exit(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200270{
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -0300271 BUG_ON(!RB_EMPTY_NODE(&map->rb_node));
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300272 dso__zput(map->dso);
273}
274
275void map__delete(struct map *map)
276{
277 map__exit(map);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300278 free(map);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200279}
280
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300281void map__put(struct map *map)
282{
Elena Reshetovae3a42cd2017-02-21 17:34:59 +0200283 if (map && refcount_dec_and_test(&map->refcnt))
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300284 map__delete(map);
285}
286
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300287void map__fixup_start(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200288{
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800289 struct rb_root_cached *symbols = &map->dso->symbols;
290 struct rb_node *nd = rb_first_cached(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200291 if (nd != NULL) {
292 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300293 map->start = sym->start;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200294 }
295}
296
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300297void map__fixup_end(struct map *map)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200298{
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800299 struct rb_root_cached *symbols = &map->dso->symbols;
300 struct rb_node *nd = rb_last(&symbols->rb_root);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200301 if (nd != NULL) {
302 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300303 map->end = sym->end;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -0200304 }
305}
306
Arnaldo Carvalho de Melod70a5402009-10-30 16:28:25 -0200307#define DSO__DELETED "(deleted)"
308
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300309int map__load(struct map *map)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200310{
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300311 const char *name = map->dso->long_name;
Masami Hiramatsua1281682009-12-15 10:32:33 -0500312 int nr;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200313
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300314 if (dso__loaded(map->dso))
Masami Hiramatsua1281682009-12-15 10:32:33 -0500315 return 0;
316
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300317 nr = dso__load(map->dso, map);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200318 if (nr < 0) {
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300319 if (map->dso->has_build_id) {
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +0900320 char sbuild_id[SBUILD_ID_SIZE];
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200321
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300322 build_id__sprintf(map->dso->build_id,
323 sizeof(map->dso->build_id),
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200324 sbuild_id);
Arnaldo Carvalho de Melod8e75a12018-09-04 10:43:07 -0300325 pr_debug("%s with build id %s not found", name, sbuild_id);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200326 } else
Arnaldo Carvalho de Melod8e75a12018-09-04 10:43:07 -0300327 pr_debug("Failed to open %s", name);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200328
Arnaldo Carvalho de Melod8e75a12018-09-04 10:43:07 -0300329 pr_debug(", continuing without symbols\n");
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200330 return -1;
331 } else if (nr == 0) {
Ingo Molnar89fe8082013-09-30 12:07:11 +0200332#ifdef HAVE_LIBELF_SUPPORT
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200333 const size_t len = strlen(name);
334 const size_t real_len = len - sizeof(DSO__DELETED);
335
336 if (len > sizeof(DSO__DELETED) &&
337 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
Arnaldo Carvalho de Melod8e75a12018-09-04 10:43:07 -0300338 pr_debug("%.*s was updated (is prelink enabled?). "
David Aherne77b15b2011-10-18 18:44:45 -0600339 "Restart the long running apps that use it!\n",
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200340 (int)real_len, name);
341 } else {
Arnaldo Carvalho de Melod8e75a12018-09-04 10:43:07 -0300342 pr_debug("no symbols found in %s, maybe install a debug package?\n", name);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200343 }
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
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300384size_t map__fprintf(struct map *map, FILE *fp)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200385{
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200386 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
Arnaldo Carvalho de Melo237a7e02013-01-24 21:59:59 -0300387 map->start, map->end, map->pgoff, map->dso->name);
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200388}
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200389
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900390size_t map__fprintf_dsoname(struct map *map, FILE *fp)
391{
Feng Tang8f28f192012-08-27 15:38:26 +0800392 const char *dsoname = "[unknown]";
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900393
Arnaldo Carvalho de Melo5eae7d82017-02-13 17:11:03 -0300394 if (map && map->dso) {
Akihiro Nagai0bc8d202012-01-30 13:43:20 +0900395 if (symbol_conf.show_kernel_path && map->dso->long_name)
396 dsoname = map->dso->long_name;
Arnaldo Carvalho de Melo5eae7d82017-02-13 17:11:03 -0300397 else
Akihiro Nagai0bc8d202012-01-30 13:43:20 +0900398 dsoname = map->dso->name;
Feng Tang8f28f192012-08-27 15:38:26 +0800399 }
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900400
401 return fprintf(fp, "%s", dsoname);
402}
403
Arnaldo Carvalho de Meloe2d88aa2018-05-28 11:05:20 -0300404char *map__srcline(struct map *map, u64 addr, struct symbol *sym)
405{
406 if (map == NULL)
407 return SRCLINE_UNKNOWN;
408 return get_srcline(map->dso, map__rip_2objdump(map, addr), sym, true, true, addr);
409}
410
Adrian Huntercc8fae12013-12-06 09:42:57 +0200411int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
412 FILE *fp)
413{
Adrian Huntercc8fae12013-12-06 09:42:57 +0200414 int ret = 0;
415
416 if (map && map->dso) {
Arnaldo Carvalho de Meloe2d88aa2018-05-28 11:05:20 -0300417 char *srcline = map__srcline(map, addr, NULL);
Adrian Huntercc8fae12013-12-06 09:42:57 +0200418 if (srcline != SRCLINE_UNKNOWN)
419 ret = fprintf(fp, "%s%s", prefix, srcline);
420 free_srcline(srcline);
421 }
422 return ret;
423}
424
Andi Kleendd2e18e2018-12-03 16:18:48 -0800425int map__fprintf_srccode(struct map *map, u64 addr,
426 FILE *fp,
427 struct srccode_state *state)
428{
429 char *srcfile;
430 int ret = 0;
431 unsigned line;
432 int len;
433 char *srccode;
434
435 if (!map || !map->dso)
436 return 0;
437 srcfile = get_srcline_split(map->dso,
438 map__rip_2objdump(map, addr),
439 &line);
440 if (!srcfile)
441 return 0;
442
443 /* Avoid redundant printing */
444 if (state &&
445 state->srcfile &&
446 !strcmp(state->srcfile, srcfile) &&
447 state->line == line) {
448 free(srcfile);
449 return 0;
450 }
451
452 srccode = find_sourceline(srcfile, line, &len);
453 if (!srccode)
454 goto out_free_line;
455
456 ret = fprintf(fp, "|%-8d %.*s", line, len, srccode);
457 state->srcfile = srcfile;
458 state->line = line;
459 return ret;
460
461out_free_line:
462 free(srcfile);
463 return ret;
464}
465
466
467void srccode_state_free(struct srccode_state *state)
468{
469 zfree(&state->srcfile);
470 state->line = 0;
471}
472
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300473/**
474 * map__rip_2objdump - convert symbol start address to objdump address.
475 * @map: memory map
476 * @rip: symbol start address
477 *
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200478 * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300479 * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
480 * relative to section start.
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300481 *
482 * Return: Address suitable for passing to "objdump --start-address="
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200483 */
484u64 map__rip_2objdump(struct map *map, u64 rip)
485{
Adrian Hunter97802f32018-06-05 10:30:00 +0300486 struct kmap *kmap = __map__kmap(map);
487
488 /*
489 * vmlinux does not have program headers for PTI entry trampolines and
490 * kcore may not either. However the trampoline object code is on the
491 * main kernel map, so just use that instead.
492 */
493 if (kmap && is_entry_trampoline(kmap->name) && kmap->kmaps && kmap->kmaps->machine) {
494 struct map *kernel_map = machine__kernel_map(kmap->kmaps->machine);
495
496 if (kernel_map)
497 map = kernel_map;
498 }
499
Adrian Hunter0131c4e2013-08-07 14:38:50 +0300500 if (!map->dso->adjust_symbols)
501 return rip;
502
503 if (map->dso->rel)
504 return rip - map->pgoff;
505
Wang Nana58f7032016-04-07 10:24:30 +0000506 /*
507 * kernel modules also have DSO_TYPE_USER in dso->kernel,
508 * but all kernel modules are ET_REL, so won't get here.
509 */
510 if (map->dso->kernel == DSO_TYPE_USER)
511 return rip + map->dso->text_offset;
512
Adrian Hunter91767532014-01-29 16:14:36 +0200513 return map->unmap_ip(map, rip) - map->reloc;
Kirill Smelkov7a2b6202010-02-03 16:52:07 -0200514}
Kirill Smelkovee11b902010-02-07 11:46:15 -0200515
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300516/**
517 * map__objdump_2mem - convert objdump address to a memory address.
518 * @map: memory map
519 * @ip: objdump address
520 *
521 * Closely related to map__rip_2objdump(), this function takes an address from
522 * objdump and converts it to a memory address. Note this assumes that @map
523 * contains the address. To be sure the result is valid, check it forwards
524 * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
525 *
526 * Return: Memory address.
527 */
528u64 map__objdump_2mem(struct map *map, u64 ip)
529{
530 if (!map->dso->adjust_symbols)
531 return map->unmap_ip(map, ip);
532
533 if (map->dso->rel)
534 return map->unmap_ip(map, ip + map->pgoff);
535
Wang Nana58f7032016-04-07 10:24:30 +0000536 /*
537 * kernel modules also have DSO_TYPE_USER in dso->kernel,
538 * but all kernel modules are ET_REL, so won't get here.
539 */
540 if (map->dso->kernel == DSO_TYPE_USER)
541 return map->unmap_ip(map, ip - map->dso->text_offset);
542
Adrian Hunter91767532014-01-29 16:14:36 +0200543 return ip + map->reloc;
Adrian Hunter1d5077b2013-10-14 13:43:44 +0300544}
545
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300546static void maps__init(struct maps *maps)
547{
548 maps->entries = RB_ROOT;
Eric Saint-Etienne1e628562018-11-21 09:51:19 -0800549 maps->names = RB_ROOT;
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300550 init_rwsem(&maps->lock);
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300551}
552
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300553void map_groups__init(struct map_groups *mg, struct machine *machine)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300554{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300555 maps__init(&mg->maps);
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300556 mg->machine = machine;
Elena Reshetovaead05e82017-02-21 17:35:00 +0200557 refcount_set(&mg->refcnt, 1);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300558}
559
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300560static void __maps__purge(struct maps *maps)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300561{
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300562 struct rb_root *root = &maps->entries;
563 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300564
565 while (next) {
566 struct map *pos = rb_entry(next, struct map, rb_node);
567
568 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -0300569 rb_erase_init(&pos->rb_node, root);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300570 map__put(pos);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300571 }
572}
573
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300574static void maps__exit(struct maps *maps)
575{
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300576 down_write(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300577 __maps__purge(maps);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300578 up_write(&maps->lock);
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300579}
580
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300581void map_groups__exit(struct map_groups *mg)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300582{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300583 maps__exit(&mg->maps);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -0300584}
585
Adrian Hunter29ce3612014-07-16 11:07:13 +0300586bool map_groups__empty(struct map_groups *mg)
587{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300588 return !maps__first(&mg->maps);
Adrian Hunter29ce3612014-07-16 11:07:13 +0300589}
590
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300591struct map_groups *map_groups__new(struct machine *machine)
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300592{
593 struct map_groups *mg = malloc(sizeof(*mg));
594
595 if (mg != NULL)
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -0300596 map_groups__init(mg, machine);
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300597
598 return mg;
599}
600
601void map_groups__delete(struct map_groups *mg)
602{
603 map_groups__exit(mg);
604 free(mg);
605}
606
Arnaldo Carvalho de Meloa26ca672014-03-25 15:26:44 -0300607void map_groups__put(struct map_groups *mg)
608{
Elena Reshetovaead05e82017-02-21 17:35:00 +0200609 if (mg && refcount_dec_and_test(&mg->refcnt))
Arnaldo Carvalho de Meloa26ca672014-03-25 15:26:44 -0300610 map_groups__delete(mg);
611}
612
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300613struct symbol *map_groups__find_symbol(struct map_groups *mg,
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300614 u64 addr, struct map **mapp)
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300615{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300616 struct map *map = map_groups__find(mg, addr);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300617
Masami Hiramatsu4afc81c2014-01-23 02:29:50 +0000618 /* Ensure map is loaded before using map->map_ip */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300619 if (map != NULL && map__load(map) >= 0) {
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300620 if (mapp != NULL)
621 *mapp = map;
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300622 return map__find_symbol(map, map->map_ip(map, addr));
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300623 }
624
625 return NULL;
626}
627
Adrian Hunter03db8b52018-09-07 11:51:16 +0300628static bool map__contains_symbol(struct map *map, struct symbol *sym)
629{
630 u64 ip = map->unmap_ip(map, sym->start);
631
632 return ip >= map->start && ip < map->end;
633}
634
Arnaldo Carvalho de Melob7f9ff52015-09-29 17:34:46 -0300635struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300636 struct map **mapp)
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300637{
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300638 struct symbol *sym;
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300639 struct rb_node *nd;
640
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300641 down_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300642
643 for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300644 struct map *pos = rb_entry(nd, struct map, rb_node);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300645
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300646 sym = map__find_symbol_by_name(pos, name);
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300647
648 if (sym == NULL)
649 continue;
Adrian Hunter03db8b52018-09-07 11:51:16 +0300650 if (!map__contains_symbol(pos, sym)) {
651 sym = NULL;
652 continue;
653 }
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300654 if (mapp != NULL)
655 *mapp = pos;
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300656 goto out;
Arnaldo Carvalho de Melo7e5e1b142010-03-26 12:30:40 -0300657 }
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300658
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300659 sym = NULL;
660out:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300661 up_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300662 return sym;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300663}
664
Arnaldo Carvalho de Melob7f9ff52015-09-29 17:34:46 -0300665struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melob7f9ff52015-09-29 17:34:46 -0300666 const char *name,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300667 struct map **mapp)
Arnaldo Carvalho de Melob7f9ff52015-09-29 17:34:46 -0300668{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300669 return maps__find_symbol_by_name(&mg->maps, name, mapp);
Arnaldo Carvalho de Melob7f9ff52015-09-29 17:34:46 -0300670}
671
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300672int map_groups__find_ams(struct addr_map_symbol *ams)
Arnaldo Carvalho de Melo4e987712013-10-14 13:43:38 +0300673{
Stephane Eranian77faf4d2014-10-06 10:35:32 +0200674 if (ams->addr < ams->map->start || ams->addr >= ams->map->end) {
Arnaldo Carvalho de Melo4e987712013-10-14 13:43:38 +0300675 if (ams->map->groups == NULL)
676 return -1;
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300677 ams->map = map_groups__find(ams->map->groups, ams->addr);
Arnaldo Carvalho de Melo4e987712013-10-14 13:43:38 +0300678 if (ams->map == NULL)
679 return -1;
680 }
681
682 ams->al_addr = ams->map->map_ip(ams->map, ams->addr);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300683 ams->sym = map__find_symbol(ams->map, ams->al_addr);
Arnaldo Carvalho de Melo4e987712013-10-14 13:43:38 +0300684
685 return ams->sym ? 0 : -1;
686}
687
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300688static size_t maps__fprintf(struct maps *maps, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300689{
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300690 size_t printed = 0;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300691 struct rb_node *nd;
692
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300693 down_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300694
695 for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300696 struct map *pos = rb_entry(nd, struct map, rb_node);
697 printed += fprintf(fp, "Map:");
698 printed += map__fprintf(pos, fp);
699 if (verbose > 2) {
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300700 printed += dso__fprintf(pos->dso, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300701 printed += fprintf(fp, "--\n");
702 }
703 }
704
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300705 up_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300706
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300707 return printed;
708}
709
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300710size_t map_groups__fprintf(struct map_groups *mg, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300711{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300712 return maps__fprintf(&mg->maps, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300713}
714
Jiri Olsacb8382e2015-11-04 15:08:11 +0100715static void __map_groups__insert(struct map_groups *mg, struct map *map)
716{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300717 __maps__insert(&mg->maps, map);
Eric Saint-Etienne1e628562018-11-21 09:51:19 -0800718 __maps__insert_name(&mg->maps, map);
Jiri Olsacb8382e2015-11-04 15:08:11 +0100719 map->groups = mg;
720}
721
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300722static int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300723{
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300724 struct rb_root *root;
Konstantin Khlebnikov6a9405b2018-08-07 17:24:54 +0300725 struct rb_node *next, *first;
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300726 int err = 0;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300727
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300728 down_write(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300729
730 root = &maps->entries;
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300731
Konstantin Khlebnikov6a9405b2018-08-07 17:24:54 +0300732 /*
733 * Find first map where end > map->start.
734 * Same as find_vma() in kernel.
735 */
736 next = root->rb_node;
737 first = NULL;
738 while (next) {
739 struct map *pos = rb_entry(next, struct map, rb_node);
740
741 if (pos->end > map->start) {
742 first = next;
743 if (pos->start <= map->start)
744 break;
745 next = next->rb_left;
746 } else
747 next = next->rb_right;
748 }
749
750 next = first;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300751 while (next) {
752 struct map *pos = rb_entry(next, struct map, rb_node);
753 next = rb_next(&pos->rb_node);
754
Konstantin Khlebnikov6a9405b2018-08-07 17:24:54 +0300755 /*
756 * Stop if current map starts after map->end.
757 * Maps are ordered by start: next will not overlap for sure.
758 */
759 if (pos->start >= map->end)
760 break;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300761
762 if (verbose >= 2) {
Alexis Berlemont21e8c812016-10-12 23:48:23 +0200763
764 if (use_browser) {
Arnaldo Carvalho de Melod8e75a12018-09-04 10:43:07 -0300765 pr_debug("overlapping maps in %s (disable tui for more info)\n",
Alexis Berlemont21e8c812016-10-12 23:48:23 +0200766 map->dso->name);
767 } else {
768 fputs("overlapping maps:\n", fp);
769 map__fprintf(map, fp);
770 map__fprintf(pos, fp);
771 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300772 }
773
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -0300774 rb_erase_init(&pos->rb_node, root);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300775 /*
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300776 * Now check if we need to create new maps for areas not
777 * overlapped by the new map:
778 */
779 if (map->start > pos->start) {
780 struct map *before = map__clone(pos);
781
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300782 if (before == NULL) {
783 err = -ENOMEM;
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300784 goto put_map;
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300785 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300786
Stephane Eranian77faf4d2014-10-06 10:35:32 +0200787 before->end = map->start;
Jiri Olsacb8382e2015-11-04 15:08:11 +0100788 __map_groups__insert(pos->groups, before);
Alexis Berlemont21e8c812016-10-12 23:48:23 +0200789 if (verbose >= 2 && !use_browser)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300790 map__fprintf(before, fp);
Masami Hiramatsud91130e2015-12-09 11:11:31 +0900791 map__put(before);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300792 }
793
794 if (map->end < pos->end) {
795 struct map *after = map__clone(pos);
796
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300797 if (after == NULL) {
798 err = -ENOMEM;
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300799 goto put_map;
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300800 }
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300801
Stephane Eranian77faf4d2014-10-06 10:35:32 +0200802 after->start = map->end;
Jiri Olsacb8382e2015-11-04 15:08:11 +0100803 __map_groups__insert(pos->groups, after);
Alexis Berlemont21e8c812016-10-12 23:48:23 +0200804 if (verbose >= 2 && !use_browser)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300805 map__fprintf(after, fp);
Masami Hiramatsud91130e2015-12-09 11:11:31 +0900806 map__put(after);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300807 }
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300808put_map:
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300809 map__put(pos);
Arnaldo Carvalho de Melo0a1eae32010-08-02 19:45:23 -0300810
811 if (err)
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300812 goto out;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300813 }
814
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300815 err = 0;
816out:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300817 up_write(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300818 return err;
819}
820
821int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
822 FILE *fp)
823{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300824 return maps__fixup_overlappings(&mg->maps, map, fp);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300825}
826
827/*
828 * XXX This should not really _copy_ te maps, but refcount them.
829 */
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300830int map_groups__clone(struct thread *thread, struct map_groups *parent)
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300831{
Jiri Olsa6c502582016-07-04 14:16:23 +0200832 struct map_groups *mg = thread->mg;
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300833 int err = -ENOMEM;
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300834 struct map *map;
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300835 struct maps *maps = &parent->maps;
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300836
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300837 down_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300838
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300839 for (map = maps__first(maps); map; map = map__next(map)) {
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300840 struct map *new = map__clone(map);
841 if (new == NULL)
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300842 goto out_unlock;
Jiri Olsa6c502582016-07-04 14:16:23 +0200843
844 err = unwind__prepare_access(thread, new, NULL);
845 if (err)
846 goto out_unlock;
847
Arnaldo Carvalho de Melo98dfd552011-08-23 14:31:30 -0300848 map_groups__insert(mg, new);
Masami Hiramatsubae32b52015-12-09 11:11:20 +0900849 map__put(new);
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300850 }
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300851
852 err = 0;
853out_unlock:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300854 up_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300855 return err;
Arnaldo Carvalho de Meloc6e718f2010-03-26 12:11:06 -0300856}
857
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300858static void __maps__insert(struct maps *maps, struct map *map)
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300859{
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300860 struct rb_node **p = &maps->entries.rb_node;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300861 struct rb_node *parent = NULL;
862 const u64 ip = map->start;
863 struct map *m;
864
865 while (*p != NULL) {
866 parent = *p;
867 m = rb_entry(parent, struct map, rb_node);
868 if (ip < m->start)
869 p = &(*p)->rb_left;
870 else
871 p = &(*p)->rb_right;
872 }
873
874 rb_link_node(&map->rb_node, parent, p);
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300875 rb_insert_color(&map->rb_node, &maps->entries);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300876 map__get(map);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300877}
878
Eric Saint-Etienne1e628562018-11-21 09:51:19 -0800879static void __maps__insert_name(struct maps *maps, struct map *map)
880{
881 struct rb_node **p = &maps->names.rb_node;
882 struct rb_node *parent = NULL;
883 struct map *m;
884 int rc;
885
886 while (*p != NULL) {
887 parent = *p;
888 m = rb_entry(parent, struct map, rb_node_name);
889 rc = strcmp(m->dso->short_name, map->dso->short_name);
890 if (rc < 0)
891 p = &(*p)->rb_left;
892 else if (rc > 0)
893 p = &(*p)->rb_right;
894 else
895 return;
896 }
897 rb_link_node(&map->rb_node_name, parent, p);
898 rb_insert_color(&map->rb_node_name, &maps->names);
899 map__get(map);
900}
901
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300902void maps__insert(struct maps *maps, struct map *map)
903{
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300904 down_write(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300905 __maps__insert(maps, map);
Eric Saint-Etienne1e628562018-11-21 09:51:19 -0800906 __maps__insert_name(maps, map);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300907 up_write(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300908}
909
910static void __maps__remove(struct maps *maps, struct map *map)
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300911{
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -0300912 rb_erase_init(&map->rb_node, &maps->entries);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -0300913 map__put(map);
Arnaldo Carvalho de Melo076c6e452010-08-02 18:18:28 -0300914}
915
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300916void maps__remove(struct maps *maps, struct map *map)
917{
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300918 down_write(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300919 __maps__remove(maps, map);
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300920 up_write(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300921}
922
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300923struct map *maps__find(struct maps *maps, u64 ip)
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300924{
Eric Saint-Etienneb18e0882018-11-23 02:42:39 -0800925 struct rb_node *p;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300926 struct map *m;
927
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300928 down_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300929
Eric Saint-Etienneb18e0882018-11-23 02:42:39 -0800930 p = maps->entries.rb_node;
931 while (p != NULL) {
932 m = rb_entry(p, struct map, rb_node);
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300933 if (ip < m->start)
Eric Saint-Etienneb18e0882018-11-23 02:42:39 -0800934 p = p->rb_left;
Namhyung Kim4955ea22014-10-14 16:05:38 -0300935 else if (ip >= m->end)
Eric Saint-Etienneb18e0882018-11-23 02:42:39 -0800936 p = p->rb_right;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300937 else
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300938 goto out;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300939 }
940
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300941 m = NULL;
942out:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300943 up_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300944 return m;
Arnaldo Carvalho de Melo4b8cf842010-03-25 19:58:58 -0300945}
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300946
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300947struct map *maps__first(struct maps *maps)
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300948{
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300949 struct rb_node *first = rb_first(&maps->entries);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300950
951 if (first)
952 return rb_entry(first, struct map, rb_node);
953 return NULL;
954}
955
Arnaldo Carvalho de Melo4d4dee92015-05-21 17:48:33 -0300956struct map *map__next(struct map *map)
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300957{
958 struct rb_node *next = rb_next(&map->rb_node);
959
960 if (next)
961 return rb_entry(next, struct map, rb_node);
962 return NULL;
963}
Wang Nanba927322015-04-07 08:22:45 +0000964
Adrian Hunter5759a682018-05-22 13:54:35 +0300965struct kmap *__map__kmap(struct map *map)
966{
967 if (!map->dso || !map->dso->kernel)
968 return NULL;
969 return (struct kmap *)(map + 1);
970}
971
Wang Nanba927322015-04-07 08:22:45 +0000972struct kmap *map__kmap(struct map *map)
973{
Adrian Hunter5759a682018-05-22 13:54:35 +0300974 struct kmap *kmap = __map__kmap(map);
975
976 if (!kmap)
Wang Nanba927322015-04-07 08:22:45 +0000977 pr_err("Internal error: map__kmap with a non-kernel map\n");
Adrian Hunter5759a682018-05-22 13:54:35 +0300978 return kmap;
Wang Nanba927322015-04-07 08:22:45 +0000979}
980
981struct map_groups *map__kmaps(struct map *map)
982{
983 struct kmap *kmap = map__kmap(map);
984
985 if (!kmap || !kmap->kmaps) {
986 pr_err("Internal error: map__kmaps with a non-kernel map\n");
987 return NULL;
988 }
989 return kmap->kmaps;
990}