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