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" |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 22 | |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 23 | static void __maps__insert(struct maps *maps, struct map *map); |
| 24 | |
Wang Nan | 0ac3348 | 2016-09-06 04:58:27 +0000 | [diff] [blame] | 25 | static inline int is_anon_memory(const char *filename, u32 flags) |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 26 | { |
Arnaldo Carvalho de Melo | fbef103 | 2016-09-12 16:47:57 -0300 | [diff] [blame] | 27 | return flags & MAP_HUGETLB || |
Wang Nan | 0ac3348 | 2016-09-06 04:58:27 +0000 | [diff] [blame] | 28 | !strcmp(filename, "//anon") || |
Yannick Brosseau | b2be545 | 2015-11-26 03:42:32 -0800 | [diff] [blame] | 29 | !strncmp(filename, "/dev/zero", sizeof("/dev/zero") - 1) || |
| 30 | !strncmp(filename, "/anon_hugepage", sizeof("/anon_hugepage") - 1); |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 31 | } |
| 32 | |
Jiri Olsa | 87ffef7 | 2011-08-24 15:18:34 +0200 | [diff] [blame] | 33 | static inline int is_no_dso_memory(const char *filename) |
| 34 | { |
Namhyung Kim | 1e82574 | 2012-11-07 16:27:11 +0900 | [diff] [blame] | 35 | return !strncmp(filename, "[stack", 6) || |
Don Zickus | 700be56 | 2014-08-19 22:31:14 -0400 | [diff] [blame] | 36 | !strncmp(filename, "/SYSV",5) || |
Jiri Olsa | 87ffef7 | 2011-08-24 15:18:34 +0200 | [diff] [blame] | 37 | !strcmp(filename, "[heap]"); |
| 38 | } |
| 39 | |
Michael Lentine | eca8183 | 2014-05-20 11:48:50 +0200 | [diff] [blame] | 40 | static inline int is_android_lib(const char *filename) |
| 41 | { |
| 42 | return !strncmp(filename, "/data/app-lib", 13) || |
| 43 | !strncmp(filename, "/system/lib", 11); |
| 44 | } |
| 45 | |
| 46 | static inline bool replace_android_lib(const char *filename, char *newfilename) |
| 47 | { |
| 48 | const char *libname; |
| 49 | char *app_abi; |
| 50 | size_t app_abi_length, new_length; |
| 51 | size_t lib_length = 0; |
| 52 | |
| 53 | libname = strrchr(filename, '/'); |
| 54 | if (libname) |
| 55 | lib_length = strlen(libname); |
| 56 | |
| 57 | app_abi = getenv("APP_ABI"); |
| 58 | if (!app_abi) |
| 59 | return false; |
| 60 | |
| 61 | app_abi_length = strlen(app_abi); |
| 62 | |
| 63 | if (!strncmp(filename, "/data/app-lib", 13)) { |
| 64 | char *apk_path; |
| 65 | |
| 66 | if (!app_abi_length) |
| 67 | return false; |
| 68 | |
| 69 | new_length = 7 + app_abi_length + lib_length; |
| 70 | |
| 71 | apk_path = getenv("APK_PATH"); |
| 72 | if (apk_path) { |
| 73 | new_length += strlen(apk_path) + 1; |
| 74 | if (new_length > PATH_MAX) |
| 75 | return false; |
| 76 | snprintf(newfilename, new_length, |
| 77 | "%s/libs/%s/%s", apk_path, app_abi, libname); |
| 78 | } else { |
| 79 | if (new_length > PATH_MAX) |
| 80 | return false; |
| 81 | snprintf(newfilename, new_length, |
| 82 | "libs/%s/%s", app_abi, libname); |
| 83 | } |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | if (!strncmp(filename, "/system/lib/", 11)) { |
| 88 | char *ndk, *app; |
| 89 | const char *arch; |
| 90 | size_t ndk_length; |
| 91 | size_t app_length; |
| 92 | |
| 93 | ndk = getenv("NDK_ROOT"); |
| 94 | app = getenv("APP_PLATFORM"); |
| 95 | |
| 96 | if (!(ndk && app)) |
| 97 | return false; |
| 98 | |
| 99 | ndk_length = strlen(ndk); |
| 100 | app_length = strlen(app); |
| 101 | |
| 102 | if (!(ndk_length && app_length && app_abi_length)) |
| 103 | return false; |
| 104 | |
| 105 | arch = !strncmp(app_abi, "arm", 3) ? "arm" : |
| 106 | !strncmp(app_abi, "mips", 4) ? "mips" : |
| 107 | !strncmp(app_abi, "x86", 3) ? "x86" : NULL; |
| 108 | |
| 109 | if (!arch) |
| 110 | return false; |
| 111 | |
| 112 | new_length = 27 + ndk_length + |
| 113 | app_length + lib_length |
| 114 | + strlen(arch); |
| 115 | |
| 116 | if (new_length > PATH_MAX) |
| 117 | return false; |
| 118 | snprintf(newfilename, new_length, |
| 119 | "%s/platforms/%s/arch-%s/usr/lib/%s", |
| 120 | ndk, app, arch, libname); |
| 121 | |
| 122 | return true; |
| 123 | } |
| 124 | return false; |
| 125 | } |
| 126 | |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 127 | 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] | 128 | { |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 129 | map->start = start; |
| 130 | map->end = end; |
| 131 | map->pgoff = pgoff; |
Adrian Hunter | 9176753 | 2014-01-29 16:14:36 +0200 | [diff] [blame] | 132 | map->reloc = 0; |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 133 | map->dso = dso__get(dso); |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 134 | map->map_ip = map__map_ip; |
| 135 | map->unmap_ip = map__unmap_ip; |
| 136 | RB_CLEAR_NODE(&map->rb_node); |
| 137 | map->groups = NULL; |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 138 | map->erange_warned = false; |
Elena Reshetova | e3a42cd | 2017-02-21 17:34:59 +0200 | [diff] [blame] | 139 | refcount_set(&map->refcnt, 1); |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 140 | } |
| 141 | |
Adrian Hunter | 2a03068 | 2014-07-22 16:17:53 +0300 | [diff] [blame] | 142 | struct map *map__new(struct machine *machine, u64 start, u64 len, |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 143 | u64 pgoff, u32 d_maj, u32 d_min, u64 ino, |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 144 | u64 ino_gen, u32 prot, u32 flags, char *filename, |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 145 | struct thread *thread) |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 146 | { |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 147 | struct map *map = malloc(sizeof(*map)); |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 148 | struct nsinfo *nsi = NULL; |
| 149 | struct nsinfo *nnsi; |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 150 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 151 | if (map != NULL) { |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 152 | char newfilename[PATH_MAX]; |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 153 | struct dso *dso; |
Michael Lentine | eca8183 | 2014-05-20 11:48:50 +0200 | [diff] [blame] | 154 | int anon, no_dso, vdso, android; |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 155 | |
Michael Lentine | eca8183 | 2014-05-20 11:48:50 +0200 | [diff] [blame] | 156 | android = is_android_lib(filename); |
Wang Nan | 0ac3348 | 2016-09-06 04:58:27 +0000 | [diff] [blame] | 157 | anon = is_anon_memory(filename, flags); |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 158 | vdso = is_vdso_map(filename); |
Jiri Olsa | 87ffef7 | 2011-08-24 15:18:34 +0200 | [diff] [blame] | 159 | no_dso = is_no_dso_memory(filename); |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 160 | |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 161 | map->maj = d_maj; |
| 162 | map->min = d_min; |
| 163 | map->ino = ino; |
| 164 | map->ino_generation = ino_gen; |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 165 | map->prot = prot; |
| 166 | map->flags = flags; |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 167 | nsi = nsinfo__get(thread->nsinfo); |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 168 | |
Arnaldo Carvalho de Melo | d183b26 | 2018-04-26 16:08:38 -0300 | [diff] [blame] | 169 | if ((anon || no_dso) && nsi && (prot & PROT_EXEC)) { |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 170 | snprintf(newfilename, sizeof(newfilename), |
| 171 | "/tmp/perf-%d.map", nsi->pid); |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 172 | filename = newfilename; |
| 173 | } |
| 174 | |
Michael Lentine | eca8183 | 2014-05-20 11:48:50 +0200 | [diff] [blame] | 175 | if (android) { |
| 176 | if (replace_android_lib(filename, newfilename)) |
| 177 | filename = newfilename; |
| 178 | } |
| 179 | |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 180 | if (vdso) { |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 181 | /* The vdso maps are always on the host and not the |
| 182 | * container. Ensure that we don't use setns to look |
| 183 | * them up. |
| 184 | */ |
| 185 | nnsi = nsinfo__copy(nsi); |
| 186 | if (nnsi) { |
| 187 | nsinfo__put(nsi); |
| 188 | nnsi->need_setns = false; |
| 189 | nsi = nnsi; |
| 190 | } |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 191 | pgoff = 0; |
Arnaldo Carvalho de Melo | 9a4388c | 2015-05-29 11:54:08 -0300 | [diff] [blame] | 192 | dso = machine__findnew_vdso(machine, thread); |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 193 | } else |
Arnaldo Carvalho de Melo | aa7cc2a | 2015-05-29 11:31:12 -0300 | [diff] [blame] | 194 | dso = machine__findnew_dso(machine, filename); |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 195 | |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 196 | if (dso == NULL) |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 197 | goto out_delete; |
| 198 | |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 199 | map__init(map, start, start + len, pgoff, dso); |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 200 | |
Jiri Olsa | 87ffef7 | 2011-08-24 15:18:34 +0200 | [diff] [blame] | 201 | if (anon || no_dso) { |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 202 | map->map_ip = map->unmap_ip = identity__map_ip; |
Jiri Olsa | 87ffef7 | 2011-08-24 15:18:34 +0200 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | * Set memory without DSO as loaded. All map__find_* |
| 206 | * functions still return NULL, and we avoid the |
| 207 | * unnecessary map__load warning. |
| 208 | */ |
Arnaldo Carvalho de Melo | d183b26 | 2018-04-26 16:08:38 -0300 | [diff] [blame] | 209 | if (!(prot & PROT_EXEC)) |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 210 | dso__set_loaded(dso); |
Arnaldo Carvalho de Melo | 8d92c02 | 2010-02-03 16:52:02 -0200 | [diff] [blame] | 211 | } |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 212 | dso->nsinfo = nsi; |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 213 | dso__put(dso); |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 214 | } |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 215 | return map; |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 216 | out_delete: |
Krister Johansen | bf2e710 | 2017-07-05 18:48:09 -0700 | [diff] [blame] | 217 | nsinfo__put(nsi); |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 218 | free(map); |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 219 | return NULL; |
| 220 | } |
| 221 | |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 222 | /* |
| 223 | * Constructor variant for modules (where we know from /proc/modules where |
| 224 | * they are loaded) and for vmlinux, where only after we load all the |
| 225 | * symbols we'll know where it starts and ends. |
| 226 | */ |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 227 | struct map *map__new2(u64 start, struct dso *dso) |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 228 | { |
| 229 | struct map *map = calloc(1, (sizeof(*map) + |
| 230 | (dso->kernel ? sizeof(struct kmap) : 0))); |
| 231 | if (map != NULL) { |
| 232 | /* |
| 233 | * ->end will be filled after we load all the symbols |
| 234 | */ |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 235 | map__init(map, start, 0, 0, dso); |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | return map; |
| 239 | } |
| 240 | |
Arnaldo Carvalho de Melo | e6ce7126 | 2015-07-23 11:06:16 -0300 | [diff] [blame] | 241 | /* |
| 242 | * Use this and __map__is_kmodule() for map instances that are in |
| 243 | * machine->kmaps, and thus have map->groups->machine all properly set, to |
| 244 | * disambiguate between the kernel and modules. |
| 245 | * |
| 246 | * When the need arises, introduce map__is_{kernel,kmodule)() that |
| 247 | * checks (map->groups != NULL && map->groups->machine != NULL && |
| 248 | * map->dso->kernel) before calling __map__is_{kernel,kmodule}()) |
| 249 | */ |
| 250 | bool __map__is_kernel(const struct map *map) |
| 251 | { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 252 | return machine__kernel_map(map->groups->machine) == map; |
Arnaldo Carvalho de Melo | e6ce7126 | 2015-07-23 11:06:16 -0300 | [diff] [blame] | 253 | } |
| 254 | |
Adrian Hunter | 5759a68 | 2018-05-22 13:54:35 +0300 | [diff] [blame] | 255 | bool __map__is_extra_kernel_map(const struct map *map) |
| 256 | { |
| 257 | struct kmap *kmap = __map__kmap((struct map *)map); |
| 258 | |
| 259 | return kmap && kmap->name[0]; |
| 260 | } |
| 261 | |
Arnaldo Carvalho de Melo | e94b861 | 2018-04-23 17:13:49 -0300 | [diff] [blame] | 262 | bool map__has_symbols(const struct map *map) |
| 263 | { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 264 | return dso__has_symbols(map->dso); |
Arnaldo Carvalho de Melo | e94b861 | 2018-04-23 17:13:49 -0300 | [diff] [blame] | 265 | } |
| 266 | |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 267 | static void map__exit(struct map *map) |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 268 | { |
Arnaldo Carvalho de Melo | facf3f0 | 2015-05-25 15:30:09 -0300 | [diff] [blame] | 269 | BUG_ON(!RB_EMPTY_NODE(&map->rb_node)); |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 270 | dso__zput(map->dso); |
| 271 | } |
| 272 | |
| 273 | void map__delete(struct map *map) |
| 274 | { |
| 275 | map__exit(map); |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 276 | free(map); |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 277 | } |
| 278 | |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 279 | void map__put(struct map *map) |
| 280 | { |
Elena Reshetova | e3a42cd | 2017-02-21 17:34:59 +0200 | [diff] [blame] | 281 | if (map && refcount_dec_and_test(&map->refcnt)) |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 282 | map__delete(map); |
| 283 | } |
| 284 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 285 | void map__fixup_start(struct map *map) |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 286 | { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 287 | struct rb_root *symbols = &map->dso->symbols; |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 288 | struct rb_node *nd = rb_first(symbols); |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 289 | if (nd != NULL) { |
| 290 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 291 | map->start = sym->start; |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 295 | void map__fixup_end(struct map *map) |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 296 | { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 297 | struct rb_root *symbols = &map->dso->symbols; |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 298 | struct rb_node *nd = rb_last(symbols); |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 299 | if (nd != NULL) { |
| 300 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 301 | map->end = sym->end; |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
Arnaldo Carvalho de Melo | d70a540 | 2009-10-30 16:28:25 -0200 | [diff] [blame] | 305 | #define DSO__DELETED "(deleted)" |
| 306 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 307 | int map__load(struct map *map) |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 308 | { |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 309 | const char *name = map->dso->long_name; |
Masami Hiramatsu | a128168 | 2009-12-15 10:32:33 -0500 | [diff] [blame] | 310 | int nr; |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 311 | |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 312 | if (dso__loaded(map->dso)) |
Masami Hiramatsu | a128168 | 2009-12-15 10:32:33 -0500 | [diff] [blame] | 313 | return 0; |
| 314 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 315 | nr = dso__load(map->dso, map); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 316 | if (nr < 0) { |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 317 | if (map->dso->has_build_id) { |
Masami Hiramatsu | b5d8bbe | 2016-05-11 22:51:59 +0900 | [diff] [blame] | 318 | char sbuild_id[SBUILD_ID_SIZE]; |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 319 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 320 | build_id__sprintf(map->dso->build_id, |
| 321 | sizeof(map->dso->build_id), |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 322 | sbuild_id); |
| 323 | pr_warning("%s with build id %s not found", |
| 324 | name, sbuild_id); |
| 325 | } else |
| 326 | pr_warning("Failed to open %s", name); |
| 327 | |
| 328 | pr_warning(", continuing without symbols\n"); |
| 329 | return -1; |
| 330 | } else if (nr == 0) { |
Ingo Molnar | 89fe808 | 2013-09-30 12:07:11 +0200 | [diff] [blame] | 331 | #ifdef HAVE_LIBELF_SUPPORT |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 332 | const size_t len = strlen(name); |
| 333 | const size_t real_len = len - sizeof(DSO__DELETED); |
| 334 | |
| 335 | if (len > sizeof(DSO__DELETED) && |
| 336 | strcmp(name + real_len + 1, DSO__DELETED) == 0) { |
David Ahern | e77b15b | 2011-10-18 18:44:45 -0600 | [diff] [blame] | 337 | pr_warning("%.*s was updated (is prelink enabled?). " |
| 338 | "Restart the long running apps that use it!\n", |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 339 | (int)real_len, name); |
| 340 | } else { |
| 341 | pr_warning("no symbols found in %s, maybe install " |
| 342 | "a debug package?\n", name); |
| 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 | |
| 384 | int map__overlap(struct map *l, struct map *r) |
| 385 | { |
| 386 | if (l->start > r->start) { |
| 387 | struct map *t = l; |
| 388 | l = r; |
| 389 | r = t; |
| 390 | } |
| 391 | |
| 392 | if (l->end > r->start) |
| 393 | return 1; |
| 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 398 | size_t map__fprintf(struct map *map, FILE *fp) |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 399 | { |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 400 | return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n", |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 401 | map->start, map->end, map->pgoff, map->dso->name); |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 402 | } |
Kirill Smelkov | 7a2b620 | 2010-02-03 16:52:07 -0200 | [diff] [blame] | 403 | |
Akihiro Nagai | 547a92e | 2012-01-30 13:42:57 +0900 | [diff] [blame] | 404 | size_t map__fprintf_dsoname(struct map *map, FILE *fp) |
| 405 | { |
Feng Tang | 8f28f19 | 2012-08-27 15:38:26 +0800 | [diff] [blame] | 406 | const char *dsoname = "[unknown]"; |
Akihiro Nagai | 547a92e | 2012-01-30 13:42:57 +0900 | [diff] [blame] | 407 | |
Arnaldo Carvalho de Melo | 5eae7d8 | 2017-02-13 17:11:03 -0300 | [diff] [blame] | 408 | if (map && map->dso) { |
Akihiro Nagai | 0bc8d20 | 2012-01-30 13:43:20 +0900 | [diff] [blame] | 409 | if (symbol_conf.show_kernel_path && map->dso->long_name) |
| 410 | dsoname = map->dso->long_name; |
Arnaldo Carvalho de Melo | 5eae7d8 | 2017-02-13 17:11:03 -0300 | [diff] [blame] | 411 | else |
Akihiro Nagai | 0bc8d20 | 2012-01-30 13:43:20 +0900 | [diff] [blame] | 412 | dsoname = map->dso->name; |
Feng Tang | 8f28f19 | 2012-08-27 15:38:26 +0800 | [diff] [blame] | 413 | } |
Akihiro Nagai | 547a92e | 2012-01-30 13:42:57 +0900 | [diff] [blame] | 414 | |
| 415 | return fprintf(fp, "%s", dsoname); |
| 416 | } |
| 417 | |
Arnaldo Carvalho de Melo | e2d88aa | 2018-05-28 11:05:20 -0300 | [diff] [blame] | 418 | char *map__srcline(struct map *map, u64 addr, struct symbol *sym) |
| 419 | { |
| 420 | if (map == NULL) |
| 421 | return SRCLINE_UNKNOWN; |
| 422 | return get_srcline(map->dso, map__rip_2objdump(map, addr), sym, true, true, addr); |
| 423 | } |
| 424 | |
Adrian Hunter | cc8fae1 | 2013-12-06 09:42:57 +0200 | [diff] [blame] | 425 | int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix, |
| 426 | FILE *fp) |
| 427 | { |
Adrian Hunter | cc8fae1 | 2013-12-06 09:42:57 +0200 | [diff] [blame] | 428 | int ret = 0; |
| 429 | |
| 430 | if (map && map->dso) { |
Arnaldo Carvalho de Melo | e2d88aa | 2018-05-28 11:05:20 -0300 | [diff] [blame] | 431 | char *srcline = map__srcline(map, addr, NULL); |
Adrian Hunter | cc8fae1 | 2013-12-06 09:42:57 +0200 | [diff] [blame] | 432 | if (srcline != SRCLINE_UNKNOWN) |
| 433 | ret = fprintf(fp, "%s%s", prefix, srcline); |
| 434 | free_srcline(srcline); |
| 435 | } |
| 436 | return ret; |
| 437 | } |
| 438 | |
Adrian Hunter | 1d5077b | 2013-10-14 13:43:44 +0300 | [diff] [blame] | 439 | /** |
| 440 | * map__rip_2objdump - convert symbol start address to objdump address. |
| 441 | * @map: memory map |
| 442 | * @rip: symbol start address |
| 443 | * |
Kirill Smelkov | 7a2b620 | 2010-02-03 16:52:07 -0200 | [diff] [blame] | 444 | * 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] | 445 | * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is |
| 446 | * relative to section start. |
Adrian Hunter | 1d5077b | 2013-10-14 13:43:44 +0300 | [diff] [blame] | 447 | * |
| 448 | * Return: Address suitable for passing to "objdump --start-address=" |
Kirill Smelkov | 7a2b620 | 2010-02-03 16:52:07 -0200 | [diff] [blame] | 449 | */ |
| 450 | u64 map__rip_2objdump(struct map *map, u64 rip) |
| 451 | { |
Adrian Hunter | 97802f3 | 2018-06-05 10:30:00 +0300 | [diff] [blame^] | 452 | struct kmap *kmap = __map__kmap(map); |
| 453 | |
| 454 | /* |
| 455 | * vmlinux does not have program headers for PTI entry trampolines and |
| 456 | * kcore may not either. However the trampoline object code is on the |
| 457 | * main kernel map, so just use that instead. |
| 458 | */ |
| 459 | if (kmap && is_entry_trampoline(kmap->name) && kmap->kmaps && kmap->kmaps->machine) { |
| 460 | struct map *kernel_map = machine__kernel_map(kmap->kmaps->machine); |
| 461 | |
| 462 | if (kernel_map) |
| 463 | map = kernel_map; |
| 464 | } |
| 465 | |
Adrian Hunter | 0131c4e | 2013-08-07 14:38:50 +0300 | [diff] [blame] | 466 | if (!map->dso->adjust_symbols) |
| 467 | return rip; |
| 468 | |
| 469 | if (map->dso->rel) |
| 470 | return rip - map->pgoff; |
| 471 | |
Wang Nan | a58f703 | 2016-04-07 10:24:30 +0000 | [diff] [blame] | 472 | /* |
| 473 | * kernel modules also have DSO_TYPE_USER in dso->kernel, |
| 474 | * but all kernel modules are ET_REL, so won't get here. |
| 475 | */ |
| 476 | if (map->dso->kernel == DSO_TYPE_USER) |
| 477 | return rip + map->dso->text_offset; |
| 478 | |
Adrian Hunter | 9176753 | 2014-01-29 16:14:36 +0200 | [diff] [blame] | 479 | return map->unmap_ip(map, rip) - map->reloc; |
Kirill Smelkov | 7a2b620 | 2010-02-03 16:52:07 -0200 | [diff] [blame] | 480 | } |
Kirill Smelkov | ee11b90 | 2010-02-07 11:46:15 -0200 | [diff] [blame] | 481 | |
Adrian Hunter | 1d5077b | 2013-10-14 13:43:44 +0300 | [diff] [blame] | 482 | /** |
| 483 | * map__objdump_2mem - convert objdump address to a memory address. |
| 484 | * @map: memory map |
| 485 | * @ip: objdump address |
| 486 | * |
| 487 | * Closely related to map__rip_2objdump(), this function takes an address from |
| 488 | * objdump and converts it to a memory address. Note this assumes that @map |
| 489 | * contains the address. To be sure the result is valid, check it forwards |
| 490 | * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip |
| 491 | * |
| 492 | * Return: Memory address. |
| 493 | */ |
| 494 | u64 map__objdump_2mem(struct map *map, u64 ip) |
| 495 | { |
| 496 | if (!map->dso->adjust_symbols) |
| 497 | return map->unmap_ip(map, ip); |
| 498 | |
| 499 | if (map->dso->rel) |
| 500 | return map->unmap_ip(map, ip + map->pgoff); |
| 501 | |
Wang Nan | a58f703 | 2016-04-07 10:24:30 +0000 | [diff] [blame] | 502 | /* |
| 503 | * kernel modules also have DSO_TYPE_USER in dso->kernel, |
| 504 | * but all kernel modules are ET_REL, so won't get here. |
| 505 | */ |
| 506 | if (map->dso->kernel == DSO_TYPE_USER) |
| 507 | return map->unmap_ip(map, ip - map->dso->text_offset); |
| 508 | |
Adrian Hunter | 9176753 | 2014-01-29 16:14:36 +0200 | [diff] [blame] | 509 | return ip + map->reloc; |
Adrian Hunter | 1d5077b | 2013-10-14 13:43:44 +0300 | [diff] [blame] | 510 | } |
| 511 | |
Arnaldo Carvalho de Melo | 1eee78a | 2015-05-22 12:58:53 -0300 | [diff] [blame] | 512 | static void maps__init(struct maps *maps) |
| 513 | { |
| 514 | maps->entries = RB_ROOT; |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 515 | init_rwsem(&maps->lock); |
Arnaldo Carvalho de Melo | 1eee78a | 2015-05-22 12:58:53 -0300 | [diff] [blame] | 516 | } |
| 517 | |
Arnaldo Carvalho de Melo | 11246c7 | 2014-10-21 17:29:02 -0300 | [diff] [blame] | 518 | 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] | 519 | { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 520 | maps__init(&mg->maps); |
Arnaldo Carvalho de Melo | 11246c7 | 2014-10-21 17:29:02 -0300 | [diff] [blame] | 521 | mg->machine = machine; |
Elena Reshetova | ead05e8 | 2017-02-21 17:35:00 +0200 | [diff] [blame] | 522 | refcount_set(&mg->refcnt, 1); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 523 | } |
| 524 | |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 525 | static void __maps__purge(struct maps *maps) |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 526 | { |
Arnaldo Carvalho de Melo | 1eee78a | 2015-05-22 12:58:53 -0300 | [diff] [blame] | 527 | struct rb_root *root = &maps->entries; |
| 528 | struct rb_node *next = rb_first(root); |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 529 | |
| 530 | while (next) { |
| 531 | struct map *pos = rb_entry(next, struct map, rb_node); |
| 532 | |
| 533 | next = rb_next(&pos->rb_node); |
Arnaldo Carvalho de Melo | facf3f0 | 2015-05-25 15:30:09 -0300 | [diff] [blame] | 534 | rb_erase_init(&pos->rb_node, root); |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 535 | map__put(pos); |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
Arnaldo Carvalho de Melo | 1eee78a | 2015-05-22 12:58:53 -0300 | [diff] [blame] | 539 | static void maps__exit(struct maps *maps) |
| 540 | { |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 541 | down_write(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 542 | __maps__purge(maps); |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 543 | up_write(&maps->lock); |
Arnaldo Carvalho de Melo | 1eee78a | 2015-05-22 12:58:53 -0300 | [diff] [blame] | 544 | } |
| 545 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 546 | void map_groups__exit(struct map_groups *mg) |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 547 | { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 548 | maps__exit(&mg->maps); |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 549 | } |
| 550 | |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 551 | bool map_groups__empty(struct map_groups *mg) |
| 552 | { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 553 | return !maps__first(&mg->maps); |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 554 | } |
| 555 | |
Arnaldo Carvalho de Melo | 11246c7 | 2014-10-21 17:29:02 -0300 | [diff] [blame] | 556 | struct map_groups *map_groups__new(struct machine *machine) |
Arnaldo Carvalho de Melo | 93d5731 | 2014-03-21 17:57:01 -0300 | [diff] [blame] | 557 | { |
| 558 | struct map_groups *mg = malloc(sizeof(*mg)); |
| 559 | |
| 560 | if (mg != NULL) |
Arnaldo Carvalho de Melo | 11246c7 | 2014-10-21 17:29:02 -0300 | [diff] [blame] | 561 | map_groups__init(mg, machine); |
Arnaldo Carvalho de Melo | 93d5731 | 2014-03-21 17:57:01 -0300 | [diff] [blame] | 562 | |
| 563 | return mg; |
| 564 | } |
| 565 | |
| 566 | void map_groups__delete(struct map_groups *mg) |
| 567 | { |
| 568 | map_groups__exit(mg); |
| 569 | free(mg); |
| 570 | } |
| 571 | |
Arnaldo Carvalho de Melo | a26ca67 | 2014-03-25 15:26:44 -0300 | [diff] [blame] | 572 | void map_groups__put(struct map_groups *mg) |
| 573 | { |
Elena Reshetova | ead05e8 | 2017-02-21 17:35:00 +0200 | [diff] [blame] | 574 | if (mg && refcount_dec_and_test(&mg->refcnt)) |
Arnaldo Carvalho de Melo | a26ca67 | 2014-03-25 15:26:44 -0300 | [diff] [blame] | 575 | map_groups__delete(mg); |
| 576 | } |
| 577 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 578 | struct symbol *map_groups__find_symbol(struct map_groups *mg, |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 579 | u64 addr, struct map **mapp) |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 580 | { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 581 | struct map *map = map_groups__find(mg, addr); |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 582 | |
Masami Hiramatsu | 4afc81c | 2014-01-23 02:29:50 +0000 | [diff] [blame] | 583 | /* Ensure map is loaded before using map->map_ip */ |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 584 | if (map != NULL && map__load(map) >= 0) { |
Arnaldo Carvalho de Melo | 7e5e1b14 | 2010-03-26 12:30:40 -0300 | [diff] [blame] | 585 | if (mapp != NULL) |
| 586 | *mapp = map; |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 587 | return map__find_symbol(map, map->map_ip(map, addr)); |
Arnaldo Carvalho de Melo | 7e5e1b14 | 2010-03-26 12:30:40 -0300 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | return NULL; |
| 591 | } |
| 592 | |
Arnaldo Carvalho de Melo | b7f9ff5 | 2015-09-29 17:34:46 -0300 | [diff] [blame] | 593 | 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] | 594 | struct map **mapp) |
Arnaldo Carvalho de Melo | 7e5e1b14 | 2010-03-26 12:30:40 -0300 | [diff] [blame] | 595 | { |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 596 | struct symbol *sym; |
Arnaldo Carvalho de Melo | 7e5e1b14 | 2010-03-26 12:30:40 -0300 | [diff] [blame] | 597 | struct rb_node *nd; |
| 598 | |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 599 | down_read(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 600 | |
| 601 | 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] | 602 | struct map *pos = rb_entry(nd, struct map, rb_node); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 603 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 604 | sym = map__find_symbol_by_name(pos, name); |
Arnaldo Carvalho de Melo | 7e5e1b14 | 2010-03-26 12:30:40 -0300 | [diff] [blame] | 605 | |
| 606 | if (sym == NULL) |
| 607 | continue; |
| 608 | if (mapp != NULL) |
| 609 | *mapp = pos; |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 610 | goto out; |
Arnaldo Carvalho de Melo | 7e5e1b14 | 2010-03-26 12:30:40 -0300 | [diff] [blame] | 611 | } |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 612 | |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 613 | sym = NULL; |
| 614 | out: |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 615 | up_read(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 616 | return sym; |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 617 | } |
| 618 | |
Arnaldo Carvalho de Melo | b7f9ff5 | 2015-09-29 17:34:46 -0300 | [diff] [blame] | 619 | 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] | 620 | const char *name, |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 621 | struct map **mapp) |
Arnaldo Carvalho de Melo | b7f9ff5 | 2015-09-29 17:34:46 -0300 | [diff] [blame] | 622 | { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 623 | return maps__find_symbol_by_name(&mg->maps, name, mapp); |
Arnaldo Carvalho de Melo | b7f9ff5 | 2015-09-29 17:34:46 -0300 | [diff] [blame] | 624 | } |
| 625 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 626 | int map_groups__find_ams(struct addr_map_symbol *ams) |
Arnaldo Carvalho de Melo | 4e98771 | 2013-10-14 13:43:38 +0300 | [diff] [blame] | 627 | { |
Stephane Eranian | 77faf4d | 2014-10-06 10:35:32 +0200 | [diff] [blame] | 628 | 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] | 629 | if (ams->map->groups == NULL) |
| 630 | return -1; |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 631 | ams->map = map_groups__find(ams->map->groups, ams->addr); |
Arnaldo Carvalho de Melo | 4e98771 | 2013-10-14 13:43:38 +0300 | [diff] [blame] | 632 | if (ams->map == NULL) |
| 633 | return -1; |
| 634 | } |
| 635 | |
| 636 | 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] | 637 | ams->sym = map__find_symbol(ams->map, ams->al_addr); |
Arnaldo Carvalho de Melo | 4e98771 | 2013-10-14 13:43:38 +0300 | [diff] [blame] | 638 | |
| 639 | return ams->sym ? 0 : -1; |
| 640 | } |
| 641 | |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 642 | static size_t maps__fprintf(struct maps *maps, FILE *fp) |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 643 | { |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 644 | size_t printed = 0; |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 645 | struct rb_node *nd; |
| 646 | |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 647 | down_read(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 648 | |
| 649 | 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] | 650 | struct map *pos = rb_entry(nd, struct map, rb_node); |
| 651 | printed += fprintf(fp, "Map:"); |
| 652 | printed += map__fprintf(pos, fp); |
| 653 | if (verbose > 2) { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 654 | printed += dso__fprintf(pos->dso, fp); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 655 | printed += fprintf(fp, "--\n"); |
| 656 | } |
| 657 | } |
| 658 | |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 659 | up_read(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 660 | |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 661 | return printed; |
| 662 | } |
| 663 | |
Arnaldo Carvalho de Melo | 5c24b67 | 2015-06-15 23:29:51 -0300 | [diff] [blame] | 664 | 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] | 665 | { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 666 | return maps__fprintf(&mg->maps, fp); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 667 | } |
| 668 | |
Jiri Olsa | cb8382e | 2015-11-04 15:08:11 +0100 | [diff] [blame] | 669 | static void __map_groups__insert(struct map_groups *mg, struct map *map) |
| 670 | { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 671 | __maps__insert(&mg->maps, map); |
Jiri Olsa | cb8382e | 2015-11-04 15:08:11 +0100 | [diff] [blame] | 672 | map->groups = mg; |
| 673 | } |
| 674 | |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 675 | 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] | 676 | { |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 677 | struct rb_root *root; |
| 678 | struct rb_node *next; |
Arnaldo Carvalho de Melo | 0a1eae3 | 2010-08-02 19:45:23 -0300 | [diff] [blame] | 679 | int err = 0; |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 680 | |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 681 | down_write(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 682 | |
| 683 | root = &maps->entries; |
| 684 | next = rb_first(root); |
| 685 | |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 686 | while (next) { |
| 687 | struct map *pos = rb_entry(next, struct map, rb_node); |
| 688 | next = rb_next(&pos->rb_node); |
| 689 | |
| 690 | if (!map__overlap(pos, map)) |
| 691 | continue; |
| 692 | |
| 693 | if (verbose >= 2) { |
Alexis Berlemont | 21e8c81 | 2016-10-12 23:48:23 +0200 | [diff] [blame] | 694 | |
| 695 | if (use_browser) { |
| 696 | pr_warning("overlapping maps in %s " |
| 697 | "(disable tui for more info)\n", |
| 698 | map->dso->name); |
| 699 | } else { |
| 700 | fputs("overlapping maps:\n", fp); |
| 701 | map__fprintf(map, fp); |
| 702 | map__fprintf(pos, fp); |
| 703 | } |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 704 | } |
| 705 | |
Arnaldo Carvalho de Melo | facf3f0 | 2015-05-25 15:30:09 -0300 | [diff] [blame] | 706 | rb_erase_init(&pos->rb_node, root); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 707 | /* |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 708 | * Now check if we need to create new maps for areas not |
| 709 | * overlapped by the new map: |
| 710 | */ |
| 711 | if (map->start > pos->start) { |
| 712 | struct map *before = map__clone(pos); |
| 713 | |
Arnaldo Carvalho de Melo | 0a1eae3 | 2010-08-02 19:45:23 -0300 | [diff] [blame] | 714 | if (before == NULL) { |
| 715 | err = -ENOMEM; |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 716 | goto put_map; |
Arnaldo Carvalho de Melo | 0a1eae3 | 2010-08-02 19:45:23 -0300 | [diff] [blame] | 717 | } |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 718 | |
Stephane Eranian | 77faf4d | 2014-10-06 10:35:32 +0200 | [diff] [blame] | 719 | before->end = map->start; |
Jiri Olsa | cb8382e | 2015-11-04 15:08:11 +0100 | [diff] [blame] | 720 | __map_groups__insert(pos->groups, before); |
Alexis Berlemont | 21e8c81 | 2016-10-12 23:48:23 +0200 | [diff] [blame] | 721 | if (verbose >= 2 && !use_browser) |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 722 | map__fprintf(before, fp); |
Masami Hiramatsu | d91130e | 2015-12-09 11:11:31 +0900 | [diff] [blame] | 723 | map__put(before); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | if (map->end < pos->end) { |
| 727 | struct map *after = map__clone(pos); |
| 728 | |
Arnaldo Carvalho de Melo | 0a1eae3 | 2010-08-02 19:45:23 -0300 | [diff] [blame] | 729 | if (after == NULL) { |
| 730 | err = -ENOMEM; |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 731 | goto put_map; |
Arnaldo Carvalho de Melo | 0a1eae3 | 2010-08-02 19:45:23 -0300 | [diff] [blame] | 732 | } |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 733 | |
Stephane Eranian | 77faf4d | 2014-10-06 10:35:32 +0200 | [diff] [blame] | 734 | after->start = map->end; |
Jiri Olsa | cb8382e | 2015-11-04 15:08:11 +0100 | [diff] [blame] | 735 | __map_groups__insert(pos->groups, after); |
Alexis Berlemont | 21e8c81 | 2016-10-12 23:48:23 +0200 | [diff] [blame] | 736 | if (verbose >= 2 && !use_browser) |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 737 | map__fprintf(after, fp); |
Masami Hiramatsu | d91130e | 2015-12-09 11:11:31 +0900 | [diff] [blame] | 738 | map__put(after); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 739 | } |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 740 | put_map: |
Arnaldo Carvalho de Melo | 5c24b67 | 2015-06-15 23:29:51 -0300 | [diff] [blame] | 741 | map__put(pos); |
Arnaldo Carvalho de Melo | 0a1eae3 | 2010-08-02 19:45:23 -0300 | [diff] [blame] | 742 | |
| 743 | if (err) |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 744 | goto out; |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 745 | } |
| 746 | |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 747 | err = 0; |
| 748 | out: |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 749 | up_write(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 750 | return err; |
| 751 | } |
| 752 | |
| 753 | int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map, |
| 754 | FILE *fp) |
| 755 | { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 756 | return maps__fixup_overlappings(&mg->maps, map, fp); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | /* |
| 760 | * XXX This should not really _copy_ te maps, but refcount them. |
| 761 | */ |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 762 | 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] | 763 | { |
Jiri Olsa | 6c50258 | 2016-07-04 14:16:23 +0200 | [diff] [blame] | 764 | struct map_groups *mg = thread->mg; |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 765 | int err = -ENOMEM; |
Arnaldo Carvalho de Melo | 4bb7123d | 2015-05-22 11:52:22 -0300 | [diff] [blame] | 766 | struct map *map; |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 767 | struct maps *maps = &parent->maps; |
Arnaldo Carvalho de Melo | 4bb7123d | 2015-05-22 11:52:22 -0300 | [diff] [blame] | 768 | |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 769 | down_read(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 770 | |
Arnaldo Carvalho de Melo | 4bb7123d | 2015-05-22 11:52:22 -0300 | [diff] [blame] | 771 | for (map = maps__first(maps); map; map = map__next(map)) { |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 772 | struct map *new = map__clone(map); |
| 773 | if (new == NULL) |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 774 | goto out_unlock; |
Jiri Olsa | 6c50258 | 2016-07-04 14:16:23 +0200 | [diff] [blame] | 775 | |
| 776 | err = unwind__prepare_access(thread, new, NULL); |
| 777 | if (err) |
| 778 | goto out_unlock; |
| 779 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 780 | map_groups__insert(mg, new); |
Masami Hiramatsu | bae32b5 | 2015-12-09 11:11:20 +0900 | [diff] [blame] | 781 | map__put(new); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 782 | } |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 783 | |
| 784 | err = 0; |
| 785 | out_unlock: |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 786 | up_read(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 787 | return err; |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 788 | } |
| 789 | |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 790 | static void __maps__insert(struct maps *maps, struct map *map) |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 791 | { |
Arnaldo Carvalho de Melo | 1eee78a | 2015-05-22 12:58:53 -0300 | [diff] [blame] | 792 | struct rb_node **p = &maps->entries.rb_node; |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 793 | struct rb_node *parent = NULL; |
| 794 | const u64 ip = map->start; |
| 795 | struct map *m; |
| 796 | |
| 797 | while (*p != NULL) { |
| 798 | parent = *p; |
| 799 | m = rb_entry(parent, struct map, rb_node); |
| 800 | if (ip < m->start) |
| 801 | p = &(*p)->rb_left; |
| 802 | else |
| 803 | p = &(*p)->rb_right; |
| 804 | } |
| 805 | |
| 806 | rb_link_node(&map->rb_node, parent, p); |
Arnaldo Carvalho de Melo | 1eee78a | 2015-05-22 12:58:53 -0300 | [diff] [blame] | 807 | rb_insert_color(&map->rb_node, &maps->entries); |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 808 | map__get(map); |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 809 | } |
| 810 | |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 811 | void maps__insert(struct maps *maps, struct map *map) |
| 812 | { |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 813 | down_write(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 814 | __maps__insert(maps, map); |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 815 | up_write(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | static void __maps__remove(struct maps *maps, struct map *map) |
Arnaldo Carvalho de Melo | 076c6e45 | 2010-08-02 18:18:28 -0300 | [diff] [blame] | 819 | { |
Arnaldo Carvalho de Melo | facf3f0 | 2015-05-25 15:30:09 -0300 | [diff] [blame] | 820 | rb_erase_init(&map->rb_node, &maps->entries); |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 821 | map__put(map); |
Arnaldo Carvalho de Melo | 076c6e45 | 2010-08-02 18:18:28 -0300 | [diff] [blame] | 822 | } |
| 823 | |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 824 | void maps__remove(struct maps *maps, struct map *map) |
| 825 | { |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 826 | down_write(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 827 | __maps__remove(maps, map); |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 828 | up_write(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 829 | } |
| 830 | |
Arnaldo Carvalho de Melo | 1eee78a | 2015-05-22 12:58:53 -0300 | [diff] [blame] | 831 | struct map *maps__find(struct maps *maps, u64 ip) |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 832 | { |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 833 | struct rb_node **p, *parent = NULL; |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 834 | struct map *m; |
| 835 | |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 836 | down_read(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 837 | |
| 838 | p = &maps->entries.rb_node; |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 839 | while (*p != NULL) { |
| 840 | parent = *p; |
| 841 | m = rb_entry(parent, struct map, rb_node); |
| 842 | if (ip < m->start) |
| 843 | p = &(*p)->rb_left; |
Namhyung Kim | 4955ea2 | 2014-10-14 16:05:38 -0300 | [diff] [blame] | 844 | else if (ip >= m->end) |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 845 | p = &(*p)->rb_right; |
| 846 | else |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 847 | goto out; |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 848 | } |
| 849 | |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 850 | m = NULL; |
| 851 | out: |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 852 | up_read(&maps->lock); |
Arnaldo Carvalho de Melo | 6a2ffcd | 2015-05-22 13:45:24 -0300 | [diff] [blame] | 853 | return m; |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 854 | } |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 855 | |
Arnaldo Carvalho de Melo | 1eee78a | 2015-05-22 12:58:53 -0300 | [diff] [blame] | 856 | struct map *maps__first(struct maps *maps) |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 857 | { |
Arnaldo Carvalho de Melo | 1eee78a | 2015-05-22 12:58:53 -0300 | [diff] [blame] | 858 | struct rb_node *first = rb_first(&maps->entries); |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 859 | |
| 860 | if (first) |
| 861 | return rb_entry(first, struct map, rb_node); |
| 862 | return NULL; |
| 863 | } |
| 864 | |
Arnaldo Carvalho de Melo | 4d4dee9 | 2015-05-21 17:48:33 -0300 | [diff] [blame] | 865 | struct map *map__next(struct map *map) |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 866 | { |
| 867 | struct rb_node *next = rb_next(&map->rb_node); |
| 868 | |
| 869 | if (next) |
| 870 | return rb_entry(next, struct map, rb_node); |
| 871 | return NULL; |
| 872 | } |
Wang Nan | ba92732 | 2015-04-07 08:22:45 +0000 | [diff] [blame] | 873 | |
Adrian Hunter | 5759a68 | 2018-05-22 13:54:35 +0300 | [diff] [blame] | 874 | struct kmap *__map__kmap(struct map *map) |
| 875 | { |
| 876 | if (!map->dso || !map->dso->kernel) |
| 877 | return NULL; |
| 878 | return (struct kmap *)(map + 1); |
| 879 | } |
| 880 | |
Wang Nan | ba92732 | 2015-04-07 08:22:45 +0000 | [diff] [blame] | 881 | struct kmap *map__kmap(struct map *map) |
| 882 | { |
Adrian Hunter | 5759a68 | 2018-05-22 13:54:35 +0300 | [diff] [blame] | 883 | struct kmap *kmap = __map__kmap(map); |
| 884 | |
| 885 | if (!kmap) |
Wang Nan | ba92732 | 2015-04-07 08:22:45 +0000 | [diff] [blame] | 886 | pr_err("Internal error: map__kmap with a non-kernel map\n"); |
Adrian Hunter | 5759a68 | 2018-05-22 13:54:35 +0300 | [diff] [blame] | 887 | return kmap; |
Wang Nan | ba92732 | 2015-04-07 08:22:45 +0000 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | struct map_groups *map__kmaps(struct map *map) |
| 891 | { |
| 892 | struct kmap *kmap = map__kmap(map); |
| 893 | |
| 894 | if (!kmap || !kmap->kmaps) { |
| 895 | pr_err("Internal error: map__kmaps with a non-kernel map\n"); |
| 896 | return NULL; |
| 897 | } |
| 898 | return kmap->kmaps; |
| 899 | } |