Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 1 | #include "symbol.h" |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 2 | #include <errno.h> |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 3 | #include <inttypes.h> |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 4 | #include <limits.h> |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | #include <stdio.h> |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 8 | #include <unistd.h> |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 9 | #include "map.h" |
David Ahern | 5cd95c2 | 2012-07-20 17:25:47 -0600 | [diff] [blame] | 10 | #include "thread.h" |
David Ahern | c80c3c2 | 2012-07-20 17:25:51 -0600 | [diff] [blame] | 11 | #include "strlist.h" |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 12 | #include "vdso.h" |
Jiri Olsa | ebb296c | 2012-10-27 23:18:28 +0200 | [diff] [blame] | 13 | #include "build-id.h" |
Adrian Hunter | cc8fae1 | 2013-12-06 09:42:57 +0200 | [diff] [blame] | 14 | #include "util.h" |
Arnaldo Carvalho de Melo | 8e16017d | 2013-01-24 22:16:43 -0300 | [diff] [blame] | 15 | #include <linux/string.h> |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 16 | |
Arnaldo Carvalho de Melo | 3846df2 | 2010-02-22 16:15:39 -0300 | [diff] [blame] | 17 | const char *map_type__name[MAP__NR_TYPES] = { |
| 18 | [MAP__FUNCTION] = "Functions", |
| 19 | [MAP__VARIABLE] = "Variables", |
| 20 | }; |
| 21 | |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 22 | static inline int is_anon_memory(const char *filename) |
| 23 | { |
Joshua Zhu | d0528b5 | 2013-01-05 13:29:57 +0800 | [diff] [blame] | 24 | return !strcmp(filename, "//anon") || |
Andi Kleen | 89365e6 | 2013-04-24 17:03:02 -0700 | [diff] [blame] | 25 | !strcmp(filename, "/dev/zero (deleted)") || |
Joshua Zhu | d0528b5 | 2013-01-05 13:29:57 +0800 | [diff] [blame] | 26 | !strcmp(filename, "/anon_hugepage (deleted)"); |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 27 | } |
| 28 | |
Jiri Olsa | 87ffef7 | 2011-08-24 15:18:34 +0200 | [diff] [blame] | 29 | static inline int is_no_dso_memory(const char *filename) |
| 30 | { |
Namhyung Kim | 1e82574 | 2012-11-07 16:27:11 +0900 | [diff] [blame] | 31 | return !strncmp(filename, "[stack", 6) || |
Jiri Olsa | 87ffef7 | 2011-08-24 15:18:34 +0200 | [diff] [blame] | 32 | !strcmp(filename, "[heap]"); |
| 33 | } |
| 34 | |
Michael Lentine | eca8183 | 2014-05-20 11:48:50 +0200 | [diff] [blame] | 35 | static inline int is_android_lib(const char *filename) |
| 36 | { |
| 37 | return !strncmp(filename, "/data/app-lib", 13) || |
| 38 | !strncmp(filename, "/system/lib", 11); |
| 39 | } |
| 40 | |
| 41 | static inline bool replace_android_lib(const char *filename, char *newfilename) |
| 42 | { |
| 43 | const char *libname; |
| 44 | char *app_abi; |
| 45 | size_t app_abi_length, new_length; |
| 46 | size_t lib_length = 0; |
| 47 | |
| 48 | libname = strrchr(filename, '/'); |
| 49 | if (libname) |
| 50 | lib_length = strlen(libname); |
| 51 | |
| 52 | app_abi = getenv("APP_ABI"); |
| 53 | if (!app_abi) |
| 54 | return false; |
| 55 | |
| 56 | app_abi_length = strlen(app_abi); |
| 57 | |
| 58 | if (!strncmp(filename, "/data/app-lib", 13)) { |
| 59 | char *apk_path; |
| 60 | |
| 61 | if (!app_abi_length) |
| 62 | return false; |
| 63 | |
| 64 | new_length = 7 + app_abi_length + lib_length; |
| 65 | |
| 66 | apk_path = getenv("APK_PATH"); |
| 67 | if (apk_path) { |
| 68 | new_length += strlen(apk_path) + 1; |
| 69 | if (new_length > PATH_MAX) |
| 70 | return false; |
| 71 | snprintf(newfilename, new_length, |
| 72 | "%s/libs/%s/%s", apk_path, app_abi, libname); |
| 73 | } else { |
| 74 | if (new_length > PATH_MAX) |
| 75 | return false; |
| 76 | snprintf(newfilename, new_length, |
| 77 | "libs/%s/%s", app_abi, libname); |
| 78 | } |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | if (!strncmp(filename, "/system/lib/", 11)) { |
| 83 | char *ndk, *app; |
| 84 | const char *arch; |
| 85 | size_t ndk_length; |
| 86 | size_t app_length; |
| 87 | |
| 88 | ndk = getenv("NDK_ROOT"); |
| 89 | app = getenv("APP_PLATFORM"); |
| 90 | |
| 91 | if (!(ndk && app)) |
| 92 | return false; |
| 93 | |
| 94 | ndk_length = strlen(ndk); |
| 95 | app_length = strlen(app); |
| 96 | |
| 97 | if (!(ndk_length && app_length && app_abi_length)) |
| 98 | return false; |
| 99 | |
| 100 | arch = !strncmp(app_abi, "arm", 3) ? "arm" : |
| 101 | !strncmp(app_abi, "mips", 4) ? "mips" : |
| 102 | !strncmp(app_abi, "x86", 3) ? "x86" : NULL; |
| 103 | |
| 104 | if (!arch) |
| 105 | return false; |
| 106 | |
| 107 | new_length = 27 + ndk_length + |
| 108 | app_length + lib_length |
| 109 | + strlen(arch); |
| 110 | |
| 111 | if (new_length > PATH_MAX) |
| 112 | return false; |
| 113 | snprintf(newfilename, new_length, |
| 114 | "%s/platforms/%s/arch-%s/usr/lib/%s", |
| 115 | ndk, app, arch, libname); |
| 116 | |
| 117 | return true; |
| 118 | } |
| 119 | return false; |
| 120 | } |
| 121 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 122 | void map__init(struct map *map, enum map_type type, |
Arnaldo Carvalho de Melo | 3610583 | 2009-11-27 16:29:16 -0200 | [diff] [blame] | 123 | u64 start, u64 end, u64 pgoff, struct dso *dso) |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 124 | { |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 125 | map->type = type; |
| 126 | map->start = start; |
| 127 | map->end = end; |
| 128 | map->pgoff = pgoff; |
Adrian Hunter | 9176753 | 2014-01-29 16:14:36 +0200 | [diff] [blame] | 129 | map->reloc = 0; |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 130 | map->dso = dso; |
| 131 | map->map_ip = map__map_ip; |
| 132 | map->unmap_ip = map__unmap_ip; |
| 133 | RB_CLEAR_NODE(&map->rb_node); |
| 134 | map->groups = NULL; |
| 135 | map->referenced = false; |
| 136 | map->erange_warned = false; |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 137 | } |
| 138 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 139 | struct map *map__new(struct list_head *dsos__list, u64 start, u64 len, |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 140 | u64 pgoff, u32 pid, u32 d_maj, u32 d_min, u64 ino, |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 141 | u64 ino_gen, u32 prot, u32 flags, char *filename, |
Dave Martin | 361d134 | 2010-07-27 16:40:02 +0100 | [diff] [blame] | 142 | enum map_type type) |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 143 | { |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 144 | struct map *map = malloc(sizeof(*map)); |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 145 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 146 | if (map != NULL) { |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 147 | char newfilename[PATH_MAX]; |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 148 | struct dso *dso; |
Michael Lentine | eca8183 | 2014-05-20 11:48:50 +0200 | [diff] [blame] | 149 | int anon, no_dso, vdso, android; |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 150 | |
Michael Lentine | eca8183 | 2014-05-20 11:48:50 +0200 | [diff] [blame] | 151 | android = is_android_lib(filename); |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 152 | anon = is_anon_memory(filename); |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 153 | vdso = is_vdso_map(filename); |
Jiri Olsa | 87ffef7 | 2011-08-24 15:18:34 +0200 | [diff] [blame] | 154 | no_dso = is_no_dso_memory(filename); |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 155 | |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 156 | map->maj = d_maj; |
| 157 | map->min = d_min; |
| 158 | map->ino = ino; |
| 159 | map->ino_generation = ino_gen; |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 160 | map->prot = prot; |
| 161 | map->flags = flags; |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 162 | |
Namhyung Kim | 578c03c | 2014-01-16 10:49:31 +0900 | [diff] [blame] | 163 | if ((anon || no_dso) && type == MAP__FUNCTION) { |
Arnaldo Carvalho de Melo | b177f63 | 2010-03-25 19:58:57 -0300 | [diff] [blame] | 164 | snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid); |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 165 | filename = newfilename; |
| 166 | } |
| 167 | |
Michael Lentine | eca8183 | 2014-05-20 11:48:50 +0200 | [diff] [blame] | 168 | if (android) { |
| 169 | if (replace_android_lib(filename, newfilename)) |
| 170 | filename = newfilename; |
| 171 | } |
| 172 | |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 173 | if (vdso) { |
| 174 | pgoff = 0; |
| 175 | dso = vdso__dso_findnew(dsos__list); |
| 176 | } else |
| 177 | dso = __dsos__findnew(dsos__list, filename); |
| 178 | |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 179 | if (dso == NULL) |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 180 | goto out_delete; |
| 181 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 182 | map__init(map, type, start, start + len, pgoff, dso); |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 183 | |
Jiri Olsa | 87ffef7 | 2011-08-24 15:18:34 +0200 | [diff] [blame] | 184 | if (anon || no_dso) { |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 185 | map->map_ip = map->unmap_ip = identity__map_ip; |
Jiri Olsa | 87ffef7 | 2011-08-24 15:18:34 +0200 | [diff] [blame] | 186 | |
| 187 | /* |
| 188 | * Set memory without DSO as loaded. All map__find_* |
| 189 | * functions still return NULL, and we avoid the |
| 190 | * unnecessary map__load warning. |
| 191 | */ |
Namhyung Kim | 578c03c | 2014-01-16 10:49:31 +0900 | [diff] [blame] | 192 | if (type != MAP__FUNCTION) |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 193 | dso__set_loaded(dso, map->type); |
Arnaldo Carvalho de Melo | 8d92c02 | 2010-02-03 16:52:02 -0200 | [diff] [blame] | 194 | } |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 195 | } |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 196 | return map; |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 197 | out_delete: |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 198 | free(map); |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 199 | return NULL; |
| 200 | } |
| 201 | |
Namhyung Kim | e5a1845 | 2012-08-06 13:41:20 +0900 | [diff] [blame] | 202 | /* |
| 203 | * Constructor variant for modules (where we know from /proc/modules where |
| 204 | * they are loaded) and for vmlinux, where only after we load all the |
| 205 | * symbols we'll know where it starts and ends. |
| 206 | */ |
| 207 | struct map *map__new2(u64 start, struct dso *dso, enum map_type type) |
| 208 | { |
| 209 | struct map *map = calloc(1, (sizeof(*map) + |
| 210 | (dso->kernel ? sizeof(struct kmap) : 0))); |
| 211 | if (map != NULL) { |
| 212 | /* |
| 213 | * ->end will be filled after we load all the symbols |
| 214 | */ |
| 215 | map__init(map, type, start, 0, 0, dso); |
| 216 | } |
| 217 | |
| 218 | return map; |
| 219 | } |
| 220 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 221 | void map__delete(struct map *map) |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 222 | { |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 223 | free(map); |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 224 | } |
| 225 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 226 | void map__fixup_start(struct map *map) |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 227 | { |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 228 | struct rb_root *symbols = &map->dso->symbols[map->type]; |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 229 | struct rb_node *nd = rb_first(symbols); |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 230 | if (nd != NULL) { |
| 231 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 232 | map->start = sym->start; |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 236 | void map__fixup_end(struct map *map) |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 237 | { |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 238 | struct rb_root *symbols = &map->dso->symbols[map->type]; |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 239 | struct rb_node *nd = rb_last(symbols); |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 240 | if (nd != NULL) { |
| 241 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 242 | map->end = sym->end; |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Arnaldo Carvalho de Melo | d70a540 | 2009-10-30 16:28:25 -0200 | [diff] [blame] | 246 | #define DSO__DELETED "(deleted)" |
| 247 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 248 | int map__load(struct map *map, symbol_filter_t filter) |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 249 | { |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 250 | const char *name = map->dso->long_name; |
Masami Hiramatsu | a128168 | 2009-12-15 10:32:33 -0500 | [diff] [blame] | 251 | int nr; |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 252 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 253 | if (dso__loaded(map->dso, map->type)) |
Masami Hiramatsu | a128168 | 2009-12-15 10:32:33 -0500 | [diff] [blame] | 254 | return 0; |
| 255 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 256 | nr = dso__load(map->dso, map, filter); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 257 | if (nr < 0) { |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 258 | if (map->dso->has_build_id) { |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 259 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
| 260 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 261 | build_id__sprintf(map->dso->build_id, |
| 262 | sizeof(map->dso->build_id), |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 263 | sbuild_id); |
| 264 | pr_warning("%s with build id %s not found", |
| 265 | name, sbuild_id); |
| 266 | } else |
| 267 | pr_warning("Failed to open %s", name); |
| 268 | |
| 269 | pr_warning(", continuing without symbols\n"); |
| 270 | return -1; |
| 271 | } else if (nr == 0) { |
Ingo Molnar | 89fe808 | 2013-09-30 12:07:11 +0200 | [diff] [blame] | 272 | #ifdef HAVE_LIBELF_SUPPORT |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 273 | const size_t len = strlen(name); |
| 274 | const size_t real_len = len - sizeof(DSO__DELETED); |
| 275 | |
| 276 | if (len > sizeof(DSO__DELETED) && |
| 277 | strcmp(name + real_len + 1, DSO__DELETED) == 0) { |
David Ahern | e77b15b | 2011-10-18 18:44:45 -0600 | [diff] [blame] | 278 | pr_warning("%.*s was updated (is prelink enabled?). " |
| 279 | "Restart the long running apps that use it!\n", |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 280 | (int)real_len, name); |
| 281 | } else { |
| 282 | pr_warning("no symbols found in %s, maybe install " |
| 283 | "a debug package?\n", name); |
| 284 | } |
Namhyung Kim | 393be2e | 2012-08-06 13:41:21 +0900 | [diff] [blame] | 285 | #endif |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 286 | return -1; |
| 287 | } |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 292 | struct symbol *map__find_symbol(struct map *map, u64 addr, |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 293 | symbol_filter_t filter) |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 294 | { |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 295 | if (map__load(map, filter) < 0) |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 296 | return NULL; |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 297 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 298 | return dso__find_symbol(map->dso, map->type, addr); |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 299 | } |
| 300 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 301 | 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] | 302 | symbol_filter_t filter) |
| 303 | { |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 304 | if (map__load(map, filter) < 0) |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 305 | return NULL; |
| 306 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 307 | if (!dso__sorted_by_name(map->dso, map->type)) |
| 308 | dso__sort_by_name(map->dso, map->type); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 309 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 310 | 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] | 311 | } |
| 312 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 313 | struct map *map__clone(struct map *map) |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 314 | { |
Arnaldo Carvalho de Melo | 8e16017d | 2013-01-24 22:16:43 -0300 | [diff] [blame] | 315 | return memdup(map, sizeof(*map)); |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | int map__overlap(struct map *l, struct map *r) |
| 319 | { |
| 320 | if (l->start > r->start) { |
| 321 | struct map *t = l; |
| 322 | l = r; |
| 323 | r = t; |
| 324 | } |
| 325 | |
| 326 | if (l->end > r->start) |
| 327 | return 1; |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 332 | size_t map__fprintf(struct map *map, FILE *fp) |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 333 | { |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 334 | return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n", |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 335 | map->start, map->end, map->pgoff, map->dso->name); |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 336 | } |
Kirill Smelkov | 7a2b620 | 2010-02-03 16:52:07 -0200 | [diff] [blame] | 337 | |
Akihiro Nagai | 547a92e | 2012-01-30 13:42:57 +0900 | [diff] [blame] | 338 | size_t map__fprintf_dsoname(struct map *map, FILE *fp) |
| 339 | { |
Feng Tang | 8f28f19 | 2012-08-27 15:38:26 +0800 | [diff] [blame] | 340 | const char *dsoname = "[unknown]"; |
Akihiro Nagai | 547a92e | 2012-01-30 13:42:57 +0900 | [diff] [blame] | 341 | |
Akihiro Nagai | 0bc8d20 | 2012-01-30 13:43:20 +0900 | [diff] [blame] | 342 | if (map && map->dso && (map->dso->name || map->dso->long_name)) { |
| 343 | if (symbol_conf.show_kernel_path && map->dso->long_name) |
| 344 | dsoname = map->dso->long_name; |
| 345 | else if (map->dso->name) |
| 346 | dsoname = map->dso->name; |
Feng Tang | 8f28f19 | 2012-08-27 15:38:26 +0800 | [diff] [blame] | 347 | } |
Akihiro Nagai | 547a92e | 2012-01-30 13:42:57 +0900 | [diff] [blame] | 348 | |
| 349 | return fprintf(fp, "%s", dsoname); |
| 350 | } |
| 351 | |
Adrian Hunter | cc8fae1 | 2013-12-06 09:42:57 +0200 | [diff] [blame] | 352 | int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix, |
| 353 | FILE *fp) |
| 354 | { |
| 355 | char *srcline; |
| 356 | int ret = 0; |
| 357 | |
| 358 | if (map && map->dso) { |
| 359 | srcline = get_srcline(map->dso, |
| 360 | map__rip_2objdump(map, addr)); |
| 361 | if (srcline != SRCLINE_UNKNOWN) |
| 362 | ret = fprintf(fp, "%s%s", prefix, srcline); |
| 363 | free_srcline(srcline); |
| 364 | } |
| 365 | return ret; |
| 366 | } |
| 367 | |
Adrian Hunter | 1d5077b | 2013-10-14 13:43:44 +0300 | [diff] [blame] | 368 | /** |
| 369 | * map__rip_2objdump - convert symbol start address to objdump address. |
| 370 | * @map: memory map |
| 371 | * @rip: symbol start address |
| 372 | * |
Kirill Smelkov | 7a2b620 | 2010-02-03 16:52:07 -0200 | [diff] [blame] | 373 | * 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] | 374 | * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is |
| 375 | * relative to section start. |
Adrian Hunter | 1d5077b | 2013-10-14 13:43:44 +0300 | [diff] [blame] | 376 | * |
| 377 | * Return: Address suitable for passing to "objdump --start-address=" |
Kirill Smelkov | 7a2b620 | 2010-02-03 16:52:07 -0200 | [diff] [blame] | 378 | */ |
| 379 | u64 map__rip_2objdump(struct map *map, u64 rip) |
| 380 | { |
Adrian Hunter | 0131c4e | 2013-08-07 14:38:50 +0300 | [diff] [blame] | 381 | if (!map->dso->adjust_symbols) |
| 382 | return rip; |
| 383 | |
| 384 | if (map->dso->rel) |
| 385 | return rip - map->pgoff; |
| 386 | |
Adrian Hunter | 9176753 | 2014-01-29 16:14:36 +0200 | [diff] [blame] | 387 | return map->unmap_ip(map, rip) - map->reloc; |
Kirill Smelkov | 7a2b620 | 2010-02-03 16:52:07 -0200 | [diff] [blame] | 388 | } |
Kirill Smelkov | ee11b90 | 2010-02-07 11:46:15 -0200 | [diff] [blame] | 389 | |
Adrian Hunter | 1d5077b | 2013-10-14 13:43:44 +0300 | [diff] [blame] | 390 | /** |
| 391 | * map__objdump_2mem - convert objdump address to a memory address. |
| 392 | * @map: memory map |
| 393 | * @ip: objdump address |
| 394 | * |
| 395 | * Closely related to map__rip_2objdump(), this function takes an address from |
| 396 | * objdump and converts it to a memory address. Note this assumes that @map |
| 397 | * contains the address. To be sure the result is valid, check it forwards |
| 398 | * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip |
| 399 | * |
| 400 | * Return: Memory address. |
| 401 | */ |
| 402 | u64 map__objdump_2mem(struct map *map, u64 ip) |
| 403 | { |
| 404 | if (!map->dso->adjust_symbols) |
| 405 | return map->unmap_ip(map, ip); |
| 406 | |
| 407 | if (map->dso->rel) |
| 408 | return map->unmap_ip(map, ip + map->pgoff); |
| 409 | |
Adrian Hunter | 9176753 | 2014-01-29 16:14:36 +0200 | [diff] [blame] | 410 | return ip + map->reloc; |
Adrian Hunter | 1d5077b | 2013-10-14 13:43:44 +0300 | [diff] [blame] | 411 | } |
| 412 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 413 | void map_groups__init(struct map_groups *mg) |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 414 | { |
| 415 | int i; |
| 416 | for (i = 0; i < MAP__NR_TYPES; ++i) { |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 417 | mg->maps[i] = RB_ROOT; |
| 418 | INIT_LIST_HEAD(&mg->removed_maps[i]); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 419 | } |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 420 | mg->machine = NULL; |
Arnaldo Carvalho de Melo | a26ca67 | 2014-03-25 15:26:44 -0300 | [diff] [blame] | 421 | mg->refcnt = 1; |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 422 | } |
| 423 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 424 | static void maps__delete(struct rb_root *maps) |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 425 | { |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 426 | struct rb_node *next = rb_first(maps); |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 427 | |
| 428 | while (next) { |
| 429 | struct map *pos = rb_entry(next, struct map, rb_node); |
| 430 | |
| 431 | next = rb_next(&pos->rb_node); |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 432 | rb_erase(&pos->rb_node, maps); |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 433 | map__delete(pos); |
| 434 | } |
| 435 | } |
| 436 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 437 | static void maps__delete_removed(struct list_head *maps) |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 438 | { |
| 439 | struct map *pos, *n; |
| 440 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 441 | list_for_each_entry_safe(pos, n, maps, node) { |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 442 | list_del(&pos->node); |
| 443 | map__delete(pos); |
| 444 | } |
| 445 | } |
| 446 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 447 | void map_groups__exit(struct map_groups *mg) |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 448 | { |
| 449 | int i; |
| 450 | |
| 451 | for (i = 0; i < MAP__NR_TYPES; ++i) { |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 452 | maps__delete(&mg->maps[i]); |
| 453 | maps__delete_removed(&mg->removed_maps[i]); |
Arnaldo Carvalho de Melo | 591765f | 2010-07-30 18:28:42 -0300 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame^] | 457 | bool map_groups__empty(struct map_groups *mg) |
| 458 | { |
| 459 | int i; |
| 460 | |
| 461 | for (i = 0; i < MAP__NR_TYPES; ++i) { |
| 462 | if (maps__first(&mg->maps[i])) |
| 463 | return false; |
| 464 | if (!list_empty(&mg->removed_maps[i])) |
| 465 | return false; |
| 466 | } |
| 467 | |
| 468 | return true; |
| 469 | } |
| 470 | |
Arnaldo Carvalho de Melo | 93d5731 | 2014-03-21 17:57:01 -0300 | [diff] [blame] | 471 | struct map_groups *map_groups__new(void) |
| 472 | { |
| 473 | struct map_groups *mg = malloc(sizeof(*mg)); |
| 474 | |
| 475 | if (mg != NULL) |
| 476 | map_groups__init(mg); |
| 477 | |
| 478 | return mg; |
| 479 | } |
| 480 | |
| 481 | void map_groups__delete(struct map_groups *mg) |
| 482 | { |
| 483 | map_groups__exit(mg); |
| 484 | free(mg); |
| 485 | } |
| 486 | |
Arnaldo Carvalho de Melo | a26ca67 | 2014-03-25 15:26:44 -0300 | [diff] [blame] | 487 | void map_groups__put(struct map_groups *mg) |
| 488 | { |
| 489 | if (--mg->refcnt == 0) |
| 490 | map_groups__delete(mg); |
| 491 | } |
| 492 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 493 | void map_groups__flush(struct map_groups *mg) |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 494 | { |
| 495 | int type; |
| 496 | |
| 497 | for (type = 0; type < MAP__NR_TYPES; type++) { |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 498 | struct rb_root *root = &mg->maps[type]; |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 499 | struct rb_node *next = rb_first(root); |
| 500 | |
| 501 | while (next) { |
| 502 | struct map *pos = rb_entry(next, struct map, rb_node); |
| 503 | next = rb_next(&pos->rb_node); |
| 504 | rb_erase(&pos->rb_node, root); |
| 505 | /* |
| 506 | * We may have references to this map, for |
| 507 | * instance in some hist_entry instances, so |
| 508 | * just move them to a separate list. |
| 509 | */ |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 510 | list_add_tail(&pos->node, &mg->removed_maps[pos->type]); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 515 | struct symbol *map_groups__find_symbol(struct map_groups *mg, |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 516 | enum map_type type, u64 addr, |
Arnaldo Carvalho de Melo | 7e5e1b14 | 2010-03-26 12:30:40 -0300 | [diff] [blame] | 517 | struct map **mapp, |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 518 | symbol_filter_t filter) |
| 519 | { |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 520 | struct map *map = map_groups__find(mg, type, addr); |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 521 | |
Masami Hiramatsu | 4afc81c | 2014-01-23 02:29:50 +0000 | [diff] [blame] | 522 | /* Ensure map is loaded before using map->map_ip */ |
| 523 | if (map != NULL && map__load(map, filter) >= 0) { |
Arnaldo Carvalho de Melo | 7e5e1b14 | 2010-03-26 12:30:40 -0300 | [diff] [blame] | 524 | if (mapp != NULL) |
| 525 | *mapp = map; |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 526 | return map__find_symbol(map, map->map_ip(map, addr), filter); |
Arnaldo Carvalho de Melo | 7e5e1b14 | 2010-03-26 12:30:40 -0300 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | return NULL; |
| 530 | } |
| 531 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 532 | struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg, |
Arnaldo Carvalho de Melo | 7e5e1b14 | 2010-03-26 12:30:40 -0300 | [diff] [blame] | 533 | enum map_type type, |
| 534 | const char *name, |
| 535 | struct map **mapp, |
| 536 | symbol_filter_t filter) |
| 537 | { |
| 538 | struct rb_node *nd; |
| 539 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 540 | for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | 7e5e1b14 | 2010-03-26 12:30:40 -0300 | [diff] [blame] | 541 | struct map *pos = rb_entry(nd, struct map, rb_node); |
| 542 | struct symbol *sym = map__find_symbol_by_name(pos, name, filter); |
| 543 | |
| 544 | if (sym == NULL) |
| 545 | continue; |
| 546 | if (mapp != NULL) |
| 547 | *mapp = pos; |
| 548 | return sym; |
| 549 | } |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 550 | |
| 551 | return NULL; |
| 552 | } |
| 553 | |
Arnaldo Carvalho de Melo | 4e98771 | 2013-10-14 13:43:38 +0300 | [diff] [blame] | 554 | int map_groups__find_ams(struct addr_map_symbol *ams, symbol_filter_t filter) |
| 555 | { |
| 556 | if (ams->addr < ams->map->start || ams->addr > ams->map->end) { |
| 557 | if (ams->map->groups == NULL) |
| 558 | return -1; |
| 559 | ams->map = map_groups__find(ams->map->groups, ams->map->type, |
| 560 | ams->addr); |
| 561 | if (ams->map == NULL) |
| 562 | return -1; |
| 563 | } |
| 564 | |
| 565 | ams->al_addr = ams->map->map_ip(ams->map, ams->addr); |
| 566 | ams->sym = map__find_symbol(ams->map, ams->al_addr, filter); |
| 567 | |
| 568 | return ams->sym ? 0 : -1; |
| 569 | } |
| 570 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 571 | size_t __map_groups__fprintf_maps(struct map_groups *mg, |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 572 | enum map_type type, int verbose, FILE *fp) |
| 573 | { |
| 574 | size_t printed = fprintf(fp, "%s:\n", map_type__name[type]); |
| 575 | struct rb_node *nd; |
| 576 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 577 | for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 578 | struct map *pos = rb_entry(nd, struct map, rb_node); |
| 579 | printed += fprintf(fp, "Map:"); |
| 580 | printed += map__fprintf(pos, fp); |
| 581 | if (verbose > 2) { |
| 582 | printed += dso__fprintf(pos->dso, type, fp); |
| 583 | printed += fprintf(fp, "--\n"); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | return printed; |
| 588 | } |
| 589 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 590 | size_t map_groups__fprintf_maps(struct map_groups *mg, int verbose, FILE *fp) |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 591 | { |
| 592 | size_t printed = 0, i; |
| 593 | for (i = 0; i < MAP__NR_TYPES; ++i) |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 594 | printed += __map_groups__fprintf_maps(mg, i, verbose, fp); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 595 | return printed; |
| 596 | } |
| 597 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 598 | static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg, |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 599 | enum map_type type, |
| 600 | int verbose, FILE *fp) |
| 601 | { |
| 602 | struct map *pos; |
| 603 | size_t printed = 0; |
| 604 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 605 | list_for_each_entry(pos, &mg->removed_maps[type], node) { |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 606 | printed += fprintf(fp, "Map:"); |
| 607 | printed += map__fprintf(pos, fp); |
| 608 | if (verbose > 1) { |
| 609 | printed += dso__fprintf(pos->dso, type, fp); |
| 610 | printed += fprintf(fp, "--\n"); |
| 611 | } |
| 612 | } |
| 613 | return printed; |
| 614 | } |
| 615 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 616 | static size_t map_groups__fprintf_removed_maps(struct map_groups *mg, |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 617 | int verbose, FILE *fp) |
| 618 | { |
| 619 | size_t printed = 0, i; |
| 620 | for (i = 0; i < MAP__NR_TYPES; ++i) |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 621 | printed += __map_groups__fprintf_removed_maps(mg, i, verbose, fp); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 622 | return printed; |
| 623 | } |
| 624 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 625 | size_t map_groups__fprintf(struct map_groups *mg, int verbose, FILE *fp) |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 626 | { |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 627 | size_t printed = map_groups__fprintf_maps(mg, verbose, fp); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 628 | printed += fprintf(fp, "Removed maps:\n"); |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 629 | return printed + map_groups__fprintf_removed_maps(mg, verbose, fp); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 630 | } |
| 631 | |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 632 | int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map, |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 633 | int verbose, FILE *fp) |
| 634 | { |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 635 | struct rb_root *root = &mg->maps[map->type]; |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 636 | struct rb_node *next = rb_first(root); |
Arnaldo Carvalho de Melo | 0a1eae3 | 2010-08-02 19:45:23 -0300 | [diff] [blame] | 637 | int err = 0; |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 638 | |
| 639 | while (next) { |
| 640 | struct map *pos = rb_entry(next, struct map, rb_node); |
| 641 | next = rb_next(&pos->rb_node); |
| 642 | |
| 643 | if (!map__overlap(pos, map)) |
| 644 | continue; |
| 645 | |
| 646 | if (verbose >= 2) { |
| 647 | fputs("overlapping maps:\n", fp); |
| 648 | map__fprintf(map, fp); |
| 649 | map__fprintf(pos, fp); |
| 650 | } |
| 651 | |
| 652 | rb_erase(&pos->rb_node, root); |
| 653 | /* |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 654 | * Now check if we need to create new maps for areas not |
| 655 | * overlapped by the new map: |
| 656 | */ |
| 657 | if (map->start > pos->start) { |
| 658 | struct map *before = map__clone(pos); |
| 659 | |
Arnaldo Carvalho de Melo | 0a1eae3 | 2010-08-02 19:45:23 -0300 | [diff] [blame] | 660 | if (before == NULL) { |
| 661 | err = -ENOMEM; |
| 662 | goto move_map; |
| 663 | } |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 664 | |
| 665 | before->end = map->start - 1; |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 666 | map_groups__insert(mg, before); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 667 | if (verbose >= 2) |
| 668 | map__fprintf(before, fp); |
| 669 | } |
| 670 | |
| 671 | if (map->end < pos->end) { |
| 672 | struct map *after = map__clone(pos); |
| 673 | |
Arnaldo Carvalho de Melo | 0a1eae3 | 2010-08-02 19:45:23 -0300 | [diff] [blame] | 674 | if (after == NULL) { |
| 675 | err = -ENOMEM; |
| 676 | goto move_map; |
| 677 | } |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 678 | |
| 679 | after->start = map->end + 1; |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 680 | map_groups__insert(mg, after); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 681 | if (verbose >= 2) |
| 682 | map__fprintf(after, fp); |
| 683 | } |
Arnaldo Carvalho de Melo | 0a1eae3 | 2010-08-02 19:45:23 -0300 | [diff] [blame] | 684 | move_map: |
| 685 | /* |
| 686 | * If we have references, just move them to a separate list. |
| 687 | */ |
| 688 | if (pos->referenced) |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 689 | list_add_tail(&pos->node, &mg->removed_maps[map->type]); |
Arnaldo Carvalho de Melo | 0a1eae3 | 2010-08-02 19:45:23 -0300 | [diff] [blame] | 690 | else |
| 691 | map__delete(pos); |
| 692 | |
| 693 | if (err) |
| 694 | return err; |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | /* |
| 701 | * XXX This should not really _copy_ te maps, but refcount them. |
| 702 | */ |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 703 | int map_groups__clone(struct map_groups *mg, |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 704 | struct map_groups *parent, enum map_type type) |
| 705 | { |
| 706 | struct rb_node *nd; |
| 707 | for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) { |
| 708 | struct map *map = rb_entry(nd, struct map, rb_node); |
| 709 | struct map *new = map__clone(map); |
| 710 | if (new == NULL) |
| 711 | return -ENOMEM; |
Arnaldo Carvalho de Melo | 98dfd55 | 2011-08-23 14:31:30 -0300 | [diff] [blame] | 712 | map_groups__insert(mg, new); |
Arnaldo Carvalho de Melo | c6e718f | 2010-03-26 12:11:06 -0300 | [diff] [blame] | 713 | } |
| 714 | return 0; |
| 715 | } |
| 716 | |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 717 | void maps__insert(struct rb_root *maps, struct map *map) |
| 718 | { |
| 719 | struct rb_node **p = &maps->rb_node; |
| 720 | struct rb_node *parent = NULL; |
| 721 | const u64 ip = map->start; |
| 722 | struct map *m; |
| 723 | |
| 724 | while (*p != NULL) { |
| 725 | parent = *p; |
| 726 | m = rb_entry(parent, struct map, rb_node); |
| 727 | if (ip < m->start) |
| 728 | p = &(*p)->rb_left; |
| 729 | else |
| 730 | p = &(*p)->rb_right; |
| 731 | } |
| 732 | |
| 733 | rb_link_node(&map->rb_node, parent, p); |
| 734 | rb_insert_color(&map->rb_node, maps); |
| 735 | } |
| 736 | |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 737 | void maps__remove(struct rb_root *maps, struct map *map) |
Arnaldo Carvalho de Melo | 076c6e45 | 2010-08-02 18:18:28 -0300 | [diff] [blame] | 738 | { |
Arnaldo Carvalho de Melo | 237a7e0 | 2013-01-24 21:59:59 -0300 | [diff] [blame] | 739 | rb_erase(&map->rb_node, maps); |
Arnaldo Carvalho de Melo | 076c6e45 | 2010-08-02 18:18:28 -0300 | [diff] [blame] | 740 | } |
| 741 | |
Arnaldo Carvalho de Melo | 4b8cf84 | 2010-03-25 19:58:58 -0300 | [diff] [blame] | 742 | struct map *maps__find(struct rb_root *maps, u64 ip) |
| 743 | { |
| 744 | struct rb_node **p = &maps->rb_node; |
| 745 | struct rb_node *parent = NULL; |
| 746 | struct map *m; |
| 747 | |
| 748 | while (*p != NULL) { |
| 749 | parent = *p; |
| 750 | m = rb_entry(parent, struct map, rb_node); |
| 751 | if (ip < m->start) |
| 752 | p = &(*p)->rb_left; |
| 753 | else if (ip > m->end) |
| 754 | p = &(*p)->rb_right; |
| 755 | else |
| 756 | return m; |
| 757 | } |
| 758 | |
| 759 | return NULL; |
| 760 | } |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 761 | |
| 762 | struct map *maps__first(struct rb_root *maps) |
| 763 | { |
| 764 | struct rb_node *first = rb_first(maps); |
| 765 | |
| 766 | if (first) |
| 767 | return rb_entry(first, struct map, rb_node); |
| 768 | return NULL; |
| 769 | } |
| 770 | |
| 771 | struct map *maps__next(struct map *map) |
| 772 | { |
| 773 | struct rb_node *next = rb_next(&map->rb_node); |
| 774 | |
| 775 | if (next) |
| 776 | return rb_entry(next, struct map, rb_node); |
| 777 | return NULL; |
| 778 | } |