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