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