Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 2 | #include "symbol.h" |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 3 | #include <errno.h> |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 4 | #include <inttypes.h> |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 5 | #include <limits.h> |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | #include <stdio.h> |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 9 | #include <unistd.h> |
Arnaldo Carvalho de Melo | fbef103 | 2016-09-12 16:47:57 -0300 | [diff] [blame] | 10 | #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */ |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 11 | #include "map.h" |
David Ahern | 5cd95c2 | 2012-07-20 17:25:47 -0600 | [diff] [blame] | 12 | #include "thread.h" |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 13 | #include "vdso.h" |
Jiri Olsa | ebb296c | 2012-10-27 23:18:28 +0200 | [diff] [blame] | 14 | #include "build-id.h" |
Adrian Hunter | cc8fae1 | 2013-12-06 09:42:57 +0200 | [diff] [blame] | 15 | #include "util.h" |
Jiri Olsa | acebd40 | 2014-07-14 23:46:47 +0200 | [diff] [blame] | 16 | #include "debug.h" |
Adrian Hunter | 2a03068 | 2014-07-22 16:17:53 +0300 | [diff] [blame] | 17 | #include "machine.h" |
Arnaldo Carvalho de Melo | 8e16017 | 2013-01-24 22:16:43 -0300 | [diff] [blame] | 18 | #include <linux/string.h> |
Arnaldo Carvalho de Melo | 632a5ca | 2017-04-17 16:30:49 -0300 | [diff] [blame] | 19 | #include "srcline.h" |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 20 | #include "namespaces.h" |
Jiri Olsa | 6c50258 | 2016-07-04 14:16:23 +0200 | [diff] [blame] | 21 | #include "unwind.h" |
Andi Kleen | dd2e18e | 2018-12-03 16:18:48 -0800 | [diff] [blame] | 22 | #include "srccode.h" |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 23 | |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 24 | static void __maps__insert(struct maps *maps, struct map *map); |
Eric Saint-Etienne | 1e62856 | 2018-11-21 09:51:19 -0800 | [diff] [blame] | 25 | static void __maps__insert_name(struct maps *maps, struct map *map); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 26 | |
Wang Nan | 0ac3348 | 2016-09-06 04:58:27 +0000 | [diff] [blame] | 27 | static inline int is_anon_memory(const char *filename, u32 flags) |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 28 | { |
Arnaldo Carvalho de Melo | fbef103 | 2016-09-12 16:47:57 -0300 | [diff] [blame] | 29 | return flags & MAP_HUGETLB || |
Wang Nan | 0ac3348 | 2016-09-06 04:58:27 +0000 | [diff] [blame] | 30 | !strcmp(filename, "//anon") || |
Yannick Brosseau | b2be545 | 2015-11-26 03:42:32 -0800 | [diff] [blame] | 31 | !strncmp(filename, "/dev/zero", sizeof("/dev/zero") - 1) || |
| 32 | !strncmp(filename, "/anon_hugepage", sizeof("/anon_hugepage") - 1); |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 33 | } |
| 34 | |
Jiri Olsa | 87ffef7 | 2011-08-24 15:18:34 +0200 | [diff] [blame] | 35 | static inline int is_no_dso_memory(const char *filename) |
| 36 | { |
Namhyung Kim | 1e82574 | 2012-11-07 16:27:11 +0900 | [diff] [blame] | 37 | return !strncmp(filename, "[stack", 6) || |
Don Zickus | 700be56 | 2014-08-19 22:31:14 -0400 | [diff] [blame] | 38 | !strncmp(filename, "/SYSV",5) || |
Jiri Olsa | 87ffef7 | 2011-08-24 15:18:34 +0200 | [diff] [blame] | 39 | !strcmp(filename, "[heap]"); |
| 40 | } |
| 41 | |
Michael Lentine | eca8183 | 2014-05-20 11:48:50 +0200 | [diff] [blame] | 42 | static 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 | |
| 48 | static 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 Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 129 | void map__init(struct map *map, u64 start, u64 end, u64 pgoff, struct dso *dso) |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 130 | { |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 131 | map->start = start; |
| 132 | map->end = end; |
| 133 | map->pgoff = pgoff; |
Adrian Hunter | 9176753 | 2014-01-29 16:14:36 +0200 | [diff] [blame] | 134 | map->reloc = 0; |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 135 | map->dso = dso__get(dso); |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 136 | 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 Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 140 | map->erange_warned = false; |
Elena Reshetova | e3a42cd | 2017-02-21 17:34:59 +0200 | [diff] [blame] | 141 | refcount_set(&map->refcnt, 1); |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 142 | } |
| 143 | |
Adrian Hunter | 2a03068 | 2014-07-22 16:17:53 +0300 | [diff] [blame] | 144 | struct map *map__new(struct machine *machine, u64 start, u64 len, |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 145 | u64 pgoff, u32 d_maj, u32 d_min, u64 ino, |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 146 | u64 ino_gen, u32 prot, u32 flags, char *filename, |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 147 | struct thread *thread) |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 148 | { |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 149 | struct map *map = malloc(sizeof(*map)); |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 150 | struct nsinfo *nsi = NULL; |
| 151 | struct nsinfo *nnsi; |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 152 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 153 | if (map != NULL) { |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 154 | char newfilename[PATH_MAX]; |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 155 | struct dso *dso; |
Michael Lentine | eca8183 | 2014-05-20 11:48:50 +0200 | [diff] [blame] | 156 | int anon, no_dso, vdso, android; |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 157 | |
Michael Lentine | eca8183 | 2014-05-20 11:48:50 +0200 | [diff] [blame] | 158 | android = is_android_lib(filename); |
Wang Nan | 0ac3348 | 2016-09-06 04:58:27 +0000 | [diff] [blame] | 159 | anon = is_anon_memory(filename, flags); |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 160 | vdso = is_vdso_map(filename); |
Jiri Olsa | 87ffef7 | 2011-08-24 15:18:34 +0200 | [diff] [blame] | 161 | no_dso = is_no_dso_memory(filename); |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 162 | |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 163 | map->maj = d_maj; |
| 164 | map->min = d_min; |
| 165 | map->ino = ino; |
| 166 | map->ino_generation = ino_gen; |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 167 | map->prot = prot; |
| 168 | map->flags = flags; |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 169 | nsi = nsinfo__get(thread->nsinfo); |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 170 | |
Arnaldo Carvalho de Melo | d183b26 | 2018-04-26 16:08:38 -0300 | [diff] [blame] | 171 | if ((anon || no_dso) && nsi && (prot & PROT_EXEC)) { |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 172 | snprintf(newfilename, sizeof(newfilename), |
| 173 | "/tmp/perf-%d.map", nsi->pid); |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 174 | filename = newfilename; |
| 175 | } |
| 176 | |
Michael Lentine | eca8183 | 2014-05-20 11:48:50 +0200 | [diff] [blame] | 177 | if (android) { |
| 178 | if (replace_android_lib(filename, newfilename)) |
| 179 | filename = newfilename; |
| 180 | } |
| 181 | |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 182 | if (vdso) { |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 183 | /* 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 Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 193 | pgoff = 0; |
Arnaldo Carvalho de Melo | 9a4388c | 2015-05-29 11:54:08 -0300 | [diff] [blame] | 194 | dso = machine__findnew_vdso(machine, thread); |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 195 | } else |
Arnaldo Carvalho de Melo | aa7cc2a | 2015-05-29 11:31:12 -0300 | [diff] [blame] | 196 | dso = machine__findnew_dso(machine, filename); |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 197 | |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 198 | if (dso == NULL) |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 199 | goto out_delete; |
| 200 | |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 201 | map__init(map, start, start + len, pgoff, dso); |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 202 | |
Jiri Olsa | 87ffef7 | 2011-08-24 15:18:34 +0200 | [diff] [blame] | 203 | if (anon || no_dso) { |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 204 | map->map_ip = map->unmap_ip = identity__map_ip; |
Jiri Olsa | 87ffef7 | 2011-08-24 15:18:34 +0200 | [diff] [blame] | 205 | |
| 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 Melo | d183b26 | 2018-04-26 16:08:38 -0300 | [diff] [blame] | 211 | if (!(prot & PROT_EXEC)) |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 212 | dso__set_loaded(dso); |
Arnaldo Carvalho de Melo | 8d92c02 | 2010-02-03 16:52:02 -0200 | [diff] [blame] | 213 | } |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 214 | dso->nsinfo = nsi; |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 215 | dso__put(dso); |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 216 | } |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 217 | return map; |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 218 | out_delete: |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 219 | nsinfo__put(nsi); |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 220 | free(map); |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 221 | return NULL; |
| 222 | } |
| 223 | |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 224 | /* |
| 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 Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 229 | struct map *map__new2(u64 start, struct dso *dso) |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 230 | { |
| 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 Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 237 | map__init(map, start, 0, 0, dso); |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | return map; |
| 241 | } |
| 242 | |
Arnaldo Carvalho de Melo | e6ce7126 | 2015-07-23 11:06:16 -0300 | [diff] [blame] | 243 | /* |
| 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 | */ |
| 252 | bool __map__is_kernel(const struct map *map) |
| 253 | { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 254 | return machine__kernel_map(map->groups->machine) == map; |
Arnaldo Carvalho de Melo | e6ce7126 | 2015-07-23 11:06:16 -0300 | [diff] [blame] | 255 | } |
| 256 | |
Adrian Hunter | 5759a68 | 2018-05-22 13:54:35 +0300 | [diff] [blame] | 257 | bool __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 Melo | e94b861 | 2018-04-23 17:13:49 -0300 | [diff] [blame] | 264 | bool map__has_symbols(const struct map *map) |
| 265 | { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 266 | return dso__has_symbols(map->dso); |
Arnaldo Carvalho de Melo | e94b861 | 2018-04-23 17:13:49 -0300 | [diff] [blame] | 267 | } |
| 268 | |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 269 | static void map__exit(struct map *map) |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 270 | { |
Arnaldo Carvalho de Melo | facf3f0 | 2015-05-25 15:30:09 -0300 | [diff] [blame] | 271 | BUG_ON(!RB_EMPTY_NODE(&map->rb_node)); |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 272 | dso__zput(map->dso); |
| 273 | } |
| 274 | |
| 275 | void map__delete(struct map *map) |
| 276 | { |
| 277 | map__exit(map); |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 278 | free(map); |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 279 | } |
| 280 | |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 281 | void map__put(struct map *map) |
| 282 | { |
Elena Reshetova | e3a42cd | 2017-02-21 17:34:59 +0200 | [diff] [blame] | 283 | if (map && refcount_dec_and_test(&map->refcnt)) |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 284 | map__delete(map); |
| 285 | } |
| 286 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 287 | void map__fixup_start(struct map *map) |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 288 | { |
Davidlohr Bueso | 7137ff5 | 2018-12-06 11:18:17 -0800 | [diff] [blame] | 289 | struct rb_root_cached *symbols = &map->dso->symbols; |
| 290 | struct rb_node *nd = rb_first_cached(symbols); |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 291 | if (nd != NULL) { |
| 292 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 293 | map->start = sym->start; |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 297 | void map__fixup_end(struct map *map) |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 298 | { |
Davidlohr Bueso | 7137ff5 | 2018-12-06 11:18:17 -0800 | [diff] [blame] | 299 | struct rb_root_cached *symbols = &map->dso->symbols; |
| 300 | struct rb_node *nd = rb_last(&symbols->rb_root); |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 301 | if (nd != NULL) { |
| 302 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 303 | map->end = sym->end; |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
Arnaldo Carvalho de Melo | d70a540 | 2009-10-30 16:28:25 -0200 | [diff] [blame] | 307 | #define DSO__DELETED "(deleted)" |
| 308 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 309 | int map__load(struct map *map) |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 310 | { |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 311 | const char *name = map->dso->long_name; |
Masami Hiramatsu | a128168 | 2009-12-15 10:32:33 -0500 | [diff] [blame] | 312 | int nr; |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 313 | |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 314 | if (dso__loaded(map->dso)) |
Masami Hiramatsu | a128168 | 2009-12-15 10:32:33 -0500 | [diff] [blame] | 315 | return 0; |
| 316 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 317 | nr = dso__load(map->dso, map); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 318 | if (nr < 0) { |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 319 | if (map->dso->has_build_id) { |
Masami Hiramatsu | b5d8bbe | 2016-05-11 22:51:59 +0900 | [diff] [blame] | 320 | char sbuild_id[SBUILD_ID_SIZE]; |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 321 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 322 | build_id__sprintf(map->dso->build_id, |
| 323 | sizeof(map->dso->build_id), |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 324 | sbuild_id); |
Arnaldo Carvalho de Melo | d8e75a1 | 2018-09-04 10:43:07 -0300 | [diff] [blame] | 325 | pr_debug("%s with build id %s not found", name, sbuild_id); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 326 | } else |
Arnaldo Carvalho de Melo | d8e75a1 | 2018-09-04 10:43:07 -0300 | [diff] [blame] | 327 | pr_debug("Failed to open %s", name); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 328 | |
Arnaldo Carvalho de Melo | d8e75a1 | 2018-09-04 10:43:07 -0300 | [diff] [blame] | 329 | pr_debug(", continuing without symbols\n"); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 330 | return -1; |
| 331 | } else if (nr == 0) { |
Ingo Molnar | 89fe808 | 2013-09-30 12:07:11 +0200 | [diff] [blame] | 332 | #ifdef HAVE_LIBELF_SUPPORT |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 333 | 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 Melo | d8e75a1 | 2018-09-04 10:43:07 -0300 | [diff] [blame] | 338 | pr_debug("%.*s was updated (is prelink enabled?). " |
David Ahern | e77b15b | 2011-10-18 18:44:45 -0600 | [diff] [blame] | 339 | "Restart the long running apps that use it!\n", |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 340 | (int)real_len, name); |
| 341 | } else { |
Arnaldo Carvalho de Melo | d8e75a1 | 2018-09-04 10:43:07 -0300 | [diff] [blame] | 342 | pr_debug("no symbols found in %s, maybe install a debug package?\n", name); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 343 | } |
Namhyung Kim | 393be2e | 2012-08-06 13:41:21 +0900 | [diff] [blame] | 344 | #endif |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 345 | return -1; |
| 346 | } |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 351 | struct symbol *map__find_symbol(struct map *map, u64 addr) |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 352 | { |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 353 | if (map__load(map) < 0) |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 354 | return NULL; |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 355 | |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 356 | return dso__find_symbol(map->dso, addr); |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 357 | } |
| 358 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 359 | struct symbol *map__find_symbol_by_name(struct map *map, const char *name) |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 360 | { |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 361 | if (map__load(map) < 0) |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 362 | return NULL; |
| 363 | |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 364 | if (!dso__sorted_by_name(map->dso)) |
| 365 | dso__sort_by_name(map->dso); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 366 | |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 367 | return dso__find_symbol_by_name(map->dso, name); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 368 | } |
| 369 | |
Arnaldo Carvalho de Melo | 66671d0 | 2015-08-18 15:19:50 -0300 | [diff] [blame] | 370 | struct map *map__clone(struct map *from) |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 371 | { |
Arnaldo Carvalho de Melo | 66671d0 | 2015-08-18 15:19:50 -0300 | [diff] [blame] | 372 | struct map *map = memdup(from, sizeof(*map)); |
| 373 | |
| 374 | if (map != NULL) { |
Elena Reshetova | e3a42cd | 2017-02-21 17:34:59 +0200 | [diff] [blame] | 375 | refcount_set(&map->refcnt, 1); |
Arnaldo Carvalho de Melo | 66671d0 | 2015-08-18 15:19:50 -0300 | [diff] [blame] | 376 | RB_CLEAR_NODE(&map->rb_node); |
| 377 | dso__get(map->dso); |
| 378 | map->groups = NULL; |
| 379 | } |
| 380 | |
| 381 | return map; |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 382 | } |
| 383 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 384 | size_t map__fprintf(struct map *map, FILE *fp) |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 385 | { |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 386 | return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n", |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 387 | map->start, map->end, map->pgoff, map->dso->name); |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 388 | } |
Kirill Smelkov | 7a2b620 | 2010-02-03 16:52:07 -0200 | [diff] [blame] | 389 | |
Akihiro Nagai | 547a92e | 2012-01-30 13:42:57 +0900 | [diff] [blame] | 390 | size_t map__fprintf_dsoname(struct map *map, FILE *fp) |
| 391 | { |
Feng Tang | 8f28f19 | 2012-08-27 15:38:26 +0800 | [diff] [blame] | 392 | const char *dsoname = "[unknown]"; |
Akihiro Nagai | 547a92e | 2012-01-30 13:42:57 +0900 | [diff] [blame] | 393 | |
Arnaldo Carvalho de Melo | 5eae7d8 | 2017-02-13 17:11:03 -0300 | [diff] [blame] | 394 | if (map && map->dso) { |
Akihiro Nagai | 0bc8d20 | 2012-01-30 13:43:20 +0900 | [diff] [blame] | 395 | if (symbol_conf.show_kernel_path && map->dso->long_name) |
| 396 | dsoname = map->dso->long_name; |
Arnaldo Carvalho de Melo | 5eae7d8 | 2017-02-13 17:11:03 -0300 | [diff] [blame] | 397 | else |
Akihiro Nagai | 0bc8d20 | 2012-01-30 13:43:20 +0900 | [diff] [blame] | 398 | dsoname = map->dso->name; |
Feng Tang | 8f28f19 | 2012-08-27 15:38:26 +0800 | [diff] [blame] | 399 | } |
Akihiro Nagai | 547a92e | 2012-01-30 13:42:57 +0900 | [diff] [blame] | 400 | |
| 401 | return fprintf(fp, "%s", dsoname); |
| 402 | } |
| 403 | |
Arnaldo Carvalho de Melo | e2d88aa | 2018-05-28 11:05:20 -0300 | [diff] [blame] | 404 | char *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 Hunter | cc8fae1 | 2013-12-06 09:42:57 +0200 | [diff] [blame] | 411 | int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix, |
| 412 | FILE *fp) |
| 413 | { |
Adrian Hunter | cc8fae1 | 2013-12-06 09:42:57 +0200 | [diff] [blame] | 414 | int ret = 0; |
| 415 | |
| 416 | if (map && map->dso) { |
Arnaldo Carvalho de Melo | e2d88aa | 2018-05-28 11:05:20 -0300 | [diff] [blame] | 417 | char *srcline = map__srcline(map, addr, NULL); |
Adrian Hunter | cc8fae1 | 2013-12-06 09:42:57 +0200 | [diff] [blame] | 418 | if (srcline != SRCLINE_UNKNOWN) |
| 419 | ret = fprintf(fp, "%s%s", prefix, srcline); |
| 420 | free_srcline(srcline); |
| 421 | } |
| 422 | return ret; |
| 423 | } |
| 424 | |
Andi Kleen | dd2e18e | 2018-12-03 16:18:48 -0800 | [diff] [blame] | 425 | int 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 | |
| 461 | out_free_line: |
| 462 | free(srcfile); |
| 463 | return ret; |
| 464 | } |
| 465 | |
| 466 | |
| 467 | void srccode_state_free(struct srccode_state *state) |
| 468 | { |
| 469 | zfree(&state->srcfile); |
| 470 | state->line = 0; |
| 471 | } |
| 472 | |
Adrian Hunter | 1d5077b | 2013-10-14 13:43:44 +0300 | [diff] [blame] | 473 | /** |
| 474 | * map__rip_2objdump - convert symbol start address to objdump address. |
| 475 | * @map: memory map |
| 476 | * @rip: symbol start address |
| 477 | * |
Kirill Smelkov | 7a2b620 | 2010-02-03 16:52:07 -0200 | [diff] [blame] | 478 | * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN. |
Adrian Hunter | 0131c4e | 2013-08-07 14:38:50 +0300 | [diff] [blame] | 479 | * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is |
| 480 | * relative to section start. |
Adrian Hunter | 1d5077b | 2013-10-14 13:43:44 +0300 | [diff] [blame] | 481 | * |
| 482 | * Return: Address suitable for passing to "objdump --start-address=" |
Kirill Smelkov | 7a2b620 | 2010-02-03 16:52:07 -0200 | [diff] [blame] | 483 | */ |
| 484 | u64 map__rip_2objdump(struct map *map, u64 rip) |
| 485 | { |
Adrian Hunter | 97802f3 | 2018-06-05 10:30:00 +0300 | [diff] [blame] | 486 | 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 Hunter | 0131c4e | 2013-08-07 14:38:50 +0300 | [diff] [blame] | 500 | if (!map->dso->adjust_symbols) |
| 501 | return rip; |
| 502 | |
| 503 | if (map->dso->rel) |
| 504 | return rip - map->pgoff; |
| 505 | |
Wang Nan | a58f703 | 2016-04-07 10:24:30 +0000 | [diff] [blame] | 506 | /* |
| 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 Hunter | 9176753 | 2014-01-29 16:14:36 +0200 | [diff] [blame] | 513 | return map->unmap_ip(map, rip) - map->reloc; |
Kirill Smelkov | 7a2b620 | 2010-02-03 16:52:07 -0200 | [diff] [blame] | 514 | } |
Kirill Smelkov | ee11b90 | 2010-02-07 11:46:15 -0200 | [diff] [blame] | 515 | |
Adrian Hunter | 1d5077b | 2013-10-14 13:43:44 +0300 | [diff] [blame] | 516 | /** |
| 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 | */ |
| 528 | u64 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 Nan | a58f703 | 2016-04-07 10:24:30 +0000 | [diff] [blame] | 536 | /* |
| 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 Hunter | 9176753 | 2014-01-29 16:14:36 +0200 | [diff] [blame] | 543 | return ip + map->reloc; |
Adrian Hunter | 1d5077b | 2013-10-14 13:43:44 +0300 | [diff] [blame] | 544 | } |
| 545 | |
Arnaldo Carvalho de Melo | 1eee78a | 2015-05-22 12:58:53 -0300 | [diff] [blame] | 546 | static void maps__init(struct maps *maps) |
| 547 | { |
| 548 | maps->entries = RB_ROOT; |
Eric Saint-Etienne | 1e62856 | 2018-11-21 09:51:19 -0800 | [diff] [blame] | 549 | maps->names = RB_ROOT; |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 550 | init_rwsem(&maps->lock); |
Arnaldo Carvalho de Melo | 1eee78a | 2015-05-22 12:58:53 -0300 | [diff] [blame] | 551 | } |
| 552 | |
Arnaldo Carvalho de Melo | 11246c7 | 2014-10-21 17:29:02 -0300 | [diff] [blame] | 553 | void map_groups__init(struct map_groups *mg, struct machine *machine) |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 554 | { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 555 | maps__init(&mg->maps); |
Arnaldo Carvalho de Melo | 11246c7 | 2014-10-21 17:29:02 -0300 | [diff] [blame] | 556 | mg->machine = machine; |
Elena Reshetova | ead05e8 | 2017-02-21 17:35:00 +0200 | [diff] [blame] | 557 | refcount_set(&mg->refcnt, 1); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 558 | } |
| 559 | |
Arnaldo Carvalho de Melo | 41f3091 | 2019-01-27 13:44:29 +0100 | [diff] [blame] | 560 | void map_groups__insert(struct map_groups *mg, struct map *map) |
| 561 | { |
| 562 | maps__insert(&mg->maps, map); |
| 563 | map->groups = mg; |
| 564 | } |
| 565 | |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 566 | static void __maps__purge(struct maps *maps) |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 567 | { |
Arnaldo Carvalho de Melo | 1eee78a | 2015-05-22 12:58:53 -0300 | [diff] [blame] | 568 | struct rb_root *root = &maps->entries; |
| 569 | struct rb_node *next = rb_first(root); |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 570 | |
| 571 | while (next) { |
| 572 | struct map *pos = rb_entry(next, struct map, rb_node); |
| 573 | |
| 574 | next = rb_next(&pos->rb_node); |
Arnaldo Carvalho de Melo | facf3f0 | 2015-05-25 15:30:09 -0300 | [diff] [blame] | 575 | rb_erase_init(&pos->rb_node, root); |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 576 | map__put(pos); |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
Changbin Du | da3a53a7 | 2019-03-16 16:05:51 +0800 | [diff] [blame] | 580 | static void __maps__purge_names(struct maps *maps) |
| 581 | { |
| 582 | struct rb_root *root = &maps->names; |
| 583 | struct rb_node *next = rb_first(root); |
| 584 | |
| 585 | while (next) { |
| 586 | struct map *pos = rb_entry(next, struct map, rb_node_name); |
| 587 | |
| 588 | next = rb_next(&pos->rb_node_name); |
| 589 | rb_erase_init(&pos->rb_node_name, root); |
| 590 | map__put(pos); |
| 591 | } |
| 592 | } |
| 593 | |
Arnaldo Carvalho de Melo | 1eee78a | 2015-05-22 12:58:53 -0300 | [diff] [blame] | 594 | static void maps__exit(struct maps *maps) |
| 595 | { |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 596 | down_write(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 597 | __maps__purge(maps); |
Changbin Du | da3a53a7 | 2019-03-16 16:05:51 +0800 | [diff] [blame] | 598 | __maps__purge_names(maps); |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 599 | up_write(&maps->lock); |
Arnaldo Carvalho de Melo | 1eee78a | 2015-05-22 12:58:53 -0300 | [diff] [blame] | 600 | } |
| 601 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 602 | void map_groups__exit(struct map_groups *mg) |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 603 | { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 604 | maps__exit(&mg->maps); |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 605 | } |
| 606 | |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 607 | bool map_groups__empty(struct map_groups *mg) |
| 608 | { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 609 | return !maps__first(&mg->maps); |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 610 | } |
| 611 | |
Arnaldo Carvalho de Melo | 11246c7 | 2014-10-21 17:29:02 -0300 | [diff] [blame] | 612 | struct map_groups *map_groups__new(struct machine *machine) |
Arnaldo Carvalho de Melo | 93d5731 | 2014-03-21 17:57:01 -0300 | [diff] [blame] | 613 | { |
| 614 | struct map_groups *mg = malloc(sizeof(*mg)); |
| 615 | |
| 616 | if (mg != NULL) |
Arnaldo Carvalho de Melo | 11246c7 | 2014-10-21 17:29:02 -0300 | [diff] [blame] | 617 | map_groups__init(mg, machine); |
Arnaldo Carvalho de Melo | 93d5731 | 2014-03-21 17:57:01 -0300 | [diff] [blame] | 618 | |
| 619 | return mg; |
| 620 | } |
| 621 | |
| 622 | void map_groups__delete(struct map_groups *mg) |
| 623 | { |
| 624 | map_groups__exit(mg); |
| 625 | free(mg); |
| 626 | } |
| 627 | |
Arnaldo Carvalho de Melo | a26ca67 | 2014-03-25 15:26:44 -0300 | [diff] [blame] | 628 | void map_groups__put(struct map_groups *mg) |
| 629 | { |
Elena Reshetova | ead05e8 | 2017-02-21 17:35:00 +0200 | [diff] [blame] | 630 | if (mg && refcount_dec_and_test(&mg->refcnt)) |
Arnaldo Carvalho de Melo | a26ca67 | 2014-03-25 15:26:44 -0300 | [diff] [blame] | 631 | map_groups__delete(mg); |
| 632 | } |
| 633 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 634 | struct symbol *map_groups__find_symbol(struct map_groups *mg, |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 635 | u64 addr, struct map **mapp) |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 636 | { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 637 | struct map *map = map_groups__find(mg, addr); |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 638 | |
Masami Hiramatsu | 4afc81c | 2014-01-23 02:29:50 +0000 | [diff] [blame] | 639 | /* Ensure map is loaded before using map->map_ip */ |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 640 | if (map != NULL && map__load(map) >= 0) { |
Arnaldo Carvalho de Melo | 7e5e1b14 | 2010-03-26 12:30:40 -0300 | [diff] [blame] | 641 | if (mapp != NULL) |
| 642 | *mapp = map; |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 643 | return map__find_symbol(map, map->map_ip(map, addr)); |
Arnaldo Carvalho de Melo | 7e5e1b14 | 2010-03-26 12:30:40 -0300 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | return NULL; |
| 647 | } |
| 648 | |
Adrian Hunter | 03db8b5 | 2018-09-07 11:51:16 +0300 | [diff] [blame] | 649 | static bool map__contains_symbol(struct map *map, struct symbol *sym) |
| 650 | { |
| 651 | u64 ip = map->unmap_ip(map, sym->start); |
| 652 | |
| 653 | return ip >= map->start && ip < map->end; |
| 654 | } |
| 655 | |
Arnaldo Carvalho de Melo | b7f9ff5 | 2015-09-29 17:34:46 -0300 | [diff] [blame] | 656 | struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name, |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 657 | struct map **mapp) |
Arnaldo Carvalho de Melo | 7e5e1b14 | 2010-03-26 12:30:40 -0300 | [diff] [blame] | 658 | { |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 659 | struct symbol *sym; |
Arnaldo Carvalho de Melo | 7e5e1b14 | 2010-03-26 12:30:40 -0300 | [diff] [blame] | 660 | struct rb_node *nd; |
| 661 | |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 662 | down_read(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 663 | |
| 664 | for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | 7e5e1b14 | 2010-03-26 12:30:40 -0300 | [diff] [blame] | 665 | struct map *pos = rb_entry(nd, struct map, rb_node); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 666 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 667 | sym = map__find_symbol_by_name(pos, name); |
Arnaldo Carvalho de Melo | 7e5e1b14 | 2010-03-26 12:30:40 -0300 | [diff] [blame] | 668 | |
| 669 | if (sym == NULL) |
| 670 | continue; |
Adrian Hunter | 03db8b5 | 2018-09-07 11:51:16 +0300 | [diff] [blame] | 671 | if (!map__contains_symbol(pos, sym)) { |
| 672 | sym = NULL; |
| 673 | continue; |
| 674 | } |
Arnaldo Carvalho de Melo | 7e5e1b14 | 2010-03-26 12:30:40 -0300 | [diff] [blame] | 675 | if (mapp != NULL) |
| 676 | *mapp = pos; |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 677 | goto out; |
Arnaldo Carvalho de Melo | 7e5e1b14 | 2010-03-26 12:30:40 -0300 | [diff] [blame] | 678 | } |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 679 | |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 680 | sym = NULL; |
| 681 | out: |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 682 | up_read(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 683 | return sym; |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 684 | } |
| 685 | |
Arnaldo Carvalho de Melo | b7f9ff5 | 2015-09-29 17:34:46 -0300 | [diff] [blame] | 686 | struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg, |
Arnaldo Carvalho de Melo | b7f9ff5 | 2015-09-29 17:34:46 -0300 | [diff] [blame] | 687 | const char *name, |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 688 | struct map **mapp) |
Arnaldo Carvalho de Melo | b7f9ff5 | 2015-09-29 17:34:46 -0300 | [diff] [blame] | 689 | { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 690 | return maps__find_symbol_by_name(&mg->maps, name, mapp); |
Arnaldo Carvalho de Melo | b7f9ff5 | 2015-09-29 17:34:46 -0300 | [diff] [blame] | 691 | } |
| 692 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 693 | int map_groups__find_ams(struct addr_map_symbol *ams) |
Arnaldo Carvalho de Melo | 4e98771 | 2013-10-14 13:43:38 +0300 | [diff] [blame] | 694 | { |
Stephane Eranian | 77faf4d | 2014-10-06 10:35:32 +0200 | [diff] [blame] | 695 | if (ams->addr < ams->map->start || ams->addr >= ams->map->end) { |
Arnaldo Carvalho de Melo | 4e98771 | 2013-10-14 13:43:38 +0300 | [diff] [blame] | 696 | if (ams->map->groups == NULL) |
| 697 | return -1; |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 698 | ams->map = map_groups__find(ams->map->groups, ams->addr); |
Arnaldo Carvalho de Melo | 4e98771 | 2013-10-14 13:43:38 +0300 | [diff] [blame] | 699 | if (ams->map == NULL) |
| 700 | return -1; |
| 701 | } |
| 702 | |
| 703 | ams->al_addr = ams->map->map_ip(ams->map, ams->addr); |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 704 | ams->sym = map__find_symbol(ams->map, ams->al_addr); |
Arnaldo Carvalho de Melo | 4e98771 | 2013-10-14 13:43:38 +0300 | [diff] [blame] | 705 | |
| 706 | return ams->sym ? 0 : -1; |
| 707 | } |
| 708 | |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 709 | static size_t maps__fprintf(struct maps *maps, FILE *fp) |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 710 | { |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 711 | size_t printed = 0; |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 712 | struct rb_node *nd; |
| 713 | |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 714 | down_read(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 715 | |
| 716 | for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 717 | struct map *pos = rb_entry(nd, struct map, rb_node); |
| 718 | printed += fprintf(fp, "Map:"); |
| 719 | printed += map__fprintf(pos, fp); |
| 720 | if (verbose > 2) { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 721 | printed += dso__fprintf(pos->dso, fp); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 722 | printed += fprintf(fp, "--\n"); |
| 723 | } |
| 724 | } |
| 725 | |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 726 | up_read(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 727 | |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 728 | return printed; |
| 729 | } |
| 730 | |
Arnaldo Carvalho de Melo | 5c24b67 | 2015-06-15 23:29:51 -0300 | [diff] [blame] | 731 | size_t map_groups__fprintf(struct map_groups *mg, FILE *fp) |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 732 | { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 733 | return maps__fprintf(&mg->maps, fp); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 734 | } |
| 735 | |
Jiri Olsa | cb8382e | 2015-11-04 15:08:11 +0100 | [diff] [blame] | 736 | static void __map_groups__insert(struct map_groups *mg, struct map *map) |
| 737 | { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 738 | __maps__insert(&mg->maps, map); |
Eric Saint-Etienne | 1e62856 | 2018-11-21 09:51:19 -0800 | [diff] [blame] | 739 | __maps__insert_name(&mg->maps, map); |
Jiri Olsa | cb8382e | 2015-11-04 15:08:11 +0100 | [diff] [blame] | 740 | map->groups = mg; |
| 741 | } |
| 742 | |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 743 | static int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp) |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 744 | { |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 745 | struct rb_root *root; |
Konstantin Khlebnikov | 6a9405b | 2018-08-07 17:24:54 +0300 | [diff] [blame] | 746 | struct rb_node *next, *first; |
Arnaldo Carvalho de Melo | 0a1eae3 | 2010-08-02 19:45:23 -0300 | [diff] [blame] | 747 | int err = 0; |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 748 | |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 749 | down_write(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 750 | |
| 751 | root = &maps->entries; |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 752 | |
Konstantin Khlebnikov | 6a9405b | 2018-08-07 17:24:54 +0300 | [diff] [blame] | 753 | /* |
| 754 | * Find first map where end > map->start. |
| 755 | * Same as find_vma() in kernel. |
| 756 | */ |
| 757 | next = root->rb_node; |
| 758 | first = NULL; |
| 759 | while (next) { |
| 760 | struct map *pos = rb_entry(next, struct map, rb_node); |
| 761 | |
| 762 | if (pos->end > map->start) { |
| 763 | first = next; |
| 764 | if (pos->start <= map->start) |
| 765 | break; |
| 766 | next = next->rb_left; |
| 767 | } else |
| 768 | next = next->rb_right; |
| 769 | } |
| 770 | |
| 771 | next = first; |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 772 | while (next) { |
| 773 | struct map *pos = rb_entry(next, struct map, rb_node); |
| 774 | next = rb_next(&pos->rb_node); |
| 775 | |
Konstantin Khlebnikov | 6a9405b | 2018-08-07 17:24:54 +0300 | [diff] [blame] | 776 | /* |
| 777 | * Stop if current map starts after map->end. |
| 778 | * Maps are ordered by start: next will not overlap for sure. |
| 779 | */ |
| 780 | if (pos->start >= map->end) |
| 781 | break; |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 782 | |
| 783 | if (verbose >= 2) { |
Alexis Berlemont | 21e8c81 | 2016-10-12 23:48:23 +0200 | [diff] [blame] | 784 | |
| 785 | if (use_browser) { |
Arnaldo Carvalho de Melo | d8e75a1 | 2018-09-04 10:43:07 -0300 | [diff] [blame] | 786 | pr_debug("overlapping maps in %s (disable tui for more info)\n", |
Alexis Berlemont | 21e8c81 | 2016-10-12 23:48:23 +0200 | [diff] [blame] | 787 | map->dso->name); |
| 788 | } else { |
| 789 | fputs("overlapping maps:\n", fp); |
| 790 | map__fprintf(map, fp); |
| 791 | map__fprintf(pos, fp); |
| 792 | } |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 793 | } |
| 794 | |
Arnaldo Carvalho de Melo | facf3f0 | 2015-05-25 15:30:09 -0300 | [diff] [blame] | 795 | rb_erase_init(&pos->rb_node, root); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 796 | /* |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 797 | * Now check if we need to create new maps for areas not |
| 798 | * overlapped by the new map: |
| 799 | */ |
| 800 | if (map->start > pos->start) { |
| 801 | struct map *before = map__clone(pos); |
| 802 | |
Arnaldo Carvalho de Melo | 0a1eae3 | 2010-08-02 19:45:23 -0300 | [diff] [blame] | 803 | if (before == NULL) { |
| 804 | err = -ENOMEM; |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 805 | goto put_map; |
Arnaldo Carvalho de Melo | 0a1eae3 | 2010-08-02 19:45:23 -0300 | [diff] [blame] | 806 | } |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 807 | |
Stephane Eranian | 77faf4d | 2014-10-06 10:35:32 +0200 | [diff] [blame] | 808 | before->end = map->start; |
Jiri Olsa | cb8382e | 2015-11-04 15:08:11 +0100 | [diff] [blame] | 809 | __map_groups__insert(pos->groups, before); |
Alexis Berlemont | 21e8c81 | 2016-10-12 23:48:23 +0200 | [diff] [blame] | 810 | if (verbose >= 2 && !use_browser) |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 811 | map__fprintf(before, fp); |
Masami Hiramatsu | d91130e | 2015-12-09 11:11:31 +0900 | [diff] [blame] | 812 | map__put(before); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | if (map->end < pos->end) { |
| 816 | struct map *after = map__clone(pos); |
| 817 | |
Arnaldo Carvalho de Melo | 0a1eae3 | 2010-08-02 19:45:23 -0300 | [diff] [blame] | 818 | if (after == NULL) { |
| 819 | err = -ENOMEM; |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 820 | goto put_map; |
Arnaldo Carvalho de Melo | 0a1eae3 | 2010-08-02 19:45:23 -0300 | [diff] [blame] | 821 | } |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 822 | |
Stephane Eranian | 77faf4d | 2014-10-06 10:35:32 +0200 | [diff] [blame] | 823 | after->start = map->end; |
Jiri Olsa | cb8382e | 2015-11-04 15:08:11 +0100 | [diff] [blame] | 824 | __map_groups__insert(pos->groups, after); |
Alexis Berlemont | 21e8c81 | 2016-10-12 23:48:23 +0200 | [diff] [blame] | 825 | if (verbose >= 2 && !use_browser) |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 826 | map__fprintf(after, fp); |
Masami Hiramatsu | d91130e | 2015-12-09 11:11:31 +0900 | [diff] [blame] | 827 | map__put(after); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 828 | } |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 829 | put_map: |
Arnaldo Carvalho de Melo | 5c24b67 | 2015-06-15 23:29:51 -0300 | [diff] [blame] | 830 | map__put(pos); |
Arnaldo Carvalho de Melo | 0a1eae3 | 2010-08-02 19:45:23 -0300 | [diff] [blame] | 831 | |
| 832 | if (err) |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 833 | goto out; |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 834 | } |
| 835 | |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 836 | err = 0; |
| 837 | out: |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 838 | up_write(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 839 | return err; |
| 840 | } |
| 841 | |
| 842 | int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map, |
| 843 | FILE *fp) |
| 844 | { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 845 | return maps__fixup_overlappings(&mg->maps, map, fp); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | /* |
| 849 | * XXX This should not really _copy_ te maps, but refcount them. |
| 850 | */ |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 851 | int map_groups__clone(struct thread *thread, struct map_groups *parent) |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 852 | { |
Jiri Olsa | 6c50258 | 2016-07-04 14:16:23 +0200 | [diff] [blame] | 853 | struct map_groups *mg = thread->mg; |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 854 | int err = -ENOMEM; |
Arnaldo Carvalho de Melo | 4bb7123d | 2015-05-22 11:52:22 -0300 | [diff] [blame] | 855 | struct map *map; |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 856 | struct maps *maps = &parent->maps; |
Arnaldo Carvalho de Melo | 4bb7123d | 2015-05-22 11:52:22 -0300 | [diff] [blame] | 857 | |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 858 | down_read(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 859 | |
Arnaldo Carvalho de Melo | 4bb7123d | 2015-05-22 11:52:22 -0300 | [diff] [blame] | 860 | for (map = maps__first(maps); map; map = map__next(map)) { |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 861 | struct map *new = map__clone(map); |
| 862 | if (new == NULL) |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 863 | goto out_unlock; |
Jiri Olsa | 6c50258 | 2016-07-04 14:16:23 +0200 | [diff] [blame] | 864 | |
| 865 | err = unwind__prepare_access(thread, new, NULL); |
| 866 | if (err) |
| 867 | goto out_unlock; |
| 868 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 869 | map_groups__insert(mg, new); |
Masami Hiramatsu | bae32b5 | 2015-12-09 11:11:20 +0900 | [diff] [blame] | 870 | map__put(new); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 871 | } |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 872 | |
| 873 | err = 0; |
| 874 | out_unlock: |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 875 | up_read(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 876 | return err; |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 877 | } |
| 878 | |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 879 | static void __maps__insert(struct maps *maps, struct map *map) |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 880 | { |
Arnaldo Carvalho de Melo | 1eee78a | 2015-05-22 12:58:53 -0300 | [diff] [blame] | 881 | struct rb_node **p = &maps->entries.rb_node; |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 882 | struct rb_node *parent = NULL; |
| 883 | const u64 ip = map->start; |
| 884 | struct map *m; |
| 885 | |
| 886 | while (*p != NULL) { |
| 887 | parent = *p; |
| 888 | m = rb_entry(parent, struct map, rb_node); |
| 889 | if (ip < m->start) |
| 890 | p = &(*p)->rb_left; |
| 891 | else |
| 892 | p = &(*p)->rb_right; |
| 893 | } |
| 894 | |
| 895 | rb_link_node(&map->rb_node, parent, p); |
Arnaldo Carvalho de Melo | 1eee78a | 2015-05-22 12:58:53 -0300 | [diff] [blame] | 896 | rb_insert_color(&map->rb_node, &maps->entries); |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 897 | map__get(map); |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 898 | } |
| 899 | |
Eric Saint-Etienne | 1e62856 | 2018-11-21 09:51:19 -0800 | [diff] [blame] | 900 | static void __maps__insert_name(struct maps *maps, struct map *map) |
| 901 | { |
| 902 | struct rb_node **p = &maps->names.rb_node; |
| 903 | struct rb_node *parent = NULL; |
| 904 | struct map *m; |
| 905 | int rc; |
| 906 | |
| 907 | while (*p != NULL) { |
| 908 | parent = *p; |
| 909 | m = rb_entry(parent, struct map, rb_node_name); |
| 910 | rc = strcmp(m->dso->short_name, map->dso->short_name); |
| 911 | if (rc < 0) |
| 912 | p = &(*p)->rb_left; |
| 913 | else if (rc > 0) |
| 914 | p = &(*p)->rb_right; |
| 915 | else |
| 916 | return; |
| 917 | } |
| 918 | rb_link_node(&map->rb_node_name, parent, p); |
| 919 | rb_insert_color(&map->rb_node_name, &maps->names); |
| 920 | map__get(map); |
| 921 | } |
| 922 | |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 923 | void maps__insert(struct maps *maps, struct map *map) |
| 924 | { |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 925 | down_write(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 926 | __maps__insert(maps, map); |
Eric Saint-Etienne | 1e62856 | 2018-11-21 09:51:19 -0800 | [diff] [blame] | 927 | __maps__insert_name(maps, map); |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 928 | up_write(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | static void __maps__remove(struct maps *maps, struct map *map) |
Arnaldo Carvalho de Melo | 076c6e45 | 2010-08-02 18:18:28 -0300 | [diff] [blame] | 932 | { |
Arnaldo Carvalho de Melo | facf3f0 | 2015-05-25 15:30:09 -0300 | [diff] [blame] | 933 | rb_erase_init(&map->rb_node, &maps->entries); |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 934 | map__put(map); |
Changbin Du | b49265e | 2019-03-16 16:05:50 +0800 | [diff] [blame] | 935 | |
| 936 | rb_erase_init(&map->rb_node_name, &maps->names); |
| 937 | map__put(map); |
Arnaldo Carvalho de Melo | 076c6e45 | 2010-08-02 18:18:28 -0300 | [diff] [blame] | 938 | } |
| 939 | |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 940 | void maps__remove(struct maps *maps, struct map *map) |
| 941 | { |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 942 | down_write(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 943 | __maps__remove(maps, map); |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 944 | up_write(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 945 | } |
| 946 | |
Arnaldo Carvalho de Melo | 1eee78a | 2015-05-22 12:58:53 -0300 | [diff] [blame] | 947 | struct map *maps__find(struct maps *maps, u64 ip) |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 948 | { |
Eric Saint-Etienne | b18e088 | 2018-11-23 02:42:39 -0800 | [diff] [blame] | 949 | struct rb_node *p; |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 950 | struct map *m; |
| 951 | |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 952 | down_read(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 953 | |
Eric Saint-Etienne | b18e088 | 2018-11-23 02:42:39 -0800 | [diff] [blame] | 954 | p = maps->entries.rb_node; |
| 955 | while (p != NULL) { |
| 956 | m = rb_entry(p, struct map, rb_node); |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 957 | if (ip < m->start) |
Eric Saint-Etienne | b18e088 | 2018-11-23 02:42:39 -0800 | [diff] [blame] | 958 | p = p->rb_left; |
Namhyung Kim | 4955ea2 | 2014-10-14 16:05:38 -0300 | [diff] [blame] | 959 | else if (ip >= m->end) |
Eric Saint-Etienne | b18e088 | 2018-11-23 02:42:39 -0800 | [diff] [blame] | 960 | p = p->rb_right; |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 961 | else |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 962 | goto out; |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 963 | } |
| 964 | |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 965 | m = NULL; |
| 966 | out: |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 967 | up_read(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 968 | return m; |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 969 | } |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 970 | |
Arnaldo Carvalho de Melo | 1eee78a | 2015-05-22 12:58:53 -0300 | [diff] [blame] | 971 | struct map *maps__first(struct maps *maps) |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 972 | { |
Arnaldo Carvalho de Melo | 1eee78a | 2015-05-22 12:58:53 -0300 | [diff] [blame] | 973 | struct rb_node *first = rb_first(&maps->entries); |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 974 | |
| 975 | if (first) |
| 976 | return rb_entry(first, struct map, rb_node); |
| 977 | return NULL; |
| 978 | } |
| 979 | |
Arnaldo Carvalho de Melo | 4d4dee9 | 2015-05-21 17:48:33 -0300 | [diff] [blame] | 980 | struct map *map__next(struct map *map) |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 981 | { |
| 982 | struct rb_node *next = rb_next(&map->rb_node); |
| 983 | |
| 984 | if (next) |
| 985 | return rb_entry(next, struct map, rb_node); |
| 986 | return NULL; |
| 987 | } |
Wang Nan | ba92732 | 2015-04-07 08:22:45 +0000 | [diff] [blame] | 988 | |
Adrian Hunter | 5759a68 | 2018-05-22 13:54:35 +0300 | [diff] [blame] | 989 | struct kmap *__map__kmap(struct map *map) |
| 990 | { |
| 991 | if (!map->dso || !map->dso->kernel) |
| 992 | return NULL; |
| 993 | return (struct kmap *)(map + 1); |
| 994 | } |
| 995 | |
Wang Nan | ba92732 | 2015-04-07 08:22:45 +0000 | [diff] [blame] | 996 | struct kmap *map__kmap(struct map *map) |
| 997 | { |
Adrian Hunter | 5759a68 | 2018-05-22 13:54:35 +0300 | [diff] [blame] | 998 | struct kmap *kmap = __map__kmap(map); |
| 999 | |
| 1000 | if (!kmap) |
Wang Nan | ba92732 | 2015-04-07 08:22:45 +0000 | [diff] [blame] | 1001 | pr_err("Internal error: map__kmap with a non-kernel map\n"); |
Adrian Hunter | 5759a68 | 2018-05-22 13:54:35 +0300 | [diff] [blame] | 1002 | return kmap; |
Wang Nan | ba92732 | 2015-04-07 08:22:45 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | struct map_groups *map__kmaps(struct map *map) |
| 1006 | { |
| 1007 | struct kmap *kmap = map__kmap(map); |
| 1008 | |
| 1009 | if (!kmap || !kmap->kmaps) { |
| 1010 | pr_err("Internal error: map__kmaps with a non-kernel map\n"); |
| 1011 | return NULL; |
| 1012 | } |
| 1013 | return kmap->kmaps; |
| 1014 | } |