Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 1 | #include "event.h" |
| 2 | #include "symbol.h" |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <stdio.h> |
Arnaldo Carvalho de Melo | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 6 | #include "debug.h" |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 7 | |
| 8 | static inline int is_anon_memory(const char *filename) |
| 9 | { |
| 10 | return strcmp(filename, "//anon") == 0; |
| 11 | } |
| 12 | |
| 13 | static int strcommon(const char *pathname, char *cwd, int cwdlen) |
| 14 | { |
| 15 | int n = 0; |
| 16 | |
| 17 | while (n < cwdlen && pathname[n] == cwd[n]) |
| 18 | ++n; |
| 19 | |
| 20 | return n; |
| 21 | } |
| 22 | |
Arnaldo Carvalho de Melo | 3610583 | 2009-11-27 16:29:16 -0200 | [diff] [blame] | 23 | void map__init(struct map *self, enum map_type type, |
| 24 | u64 start, u64 end, u64 pgoff, struct dso *dso) |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 25 | { |
Arnaldo Carvalho de Melo | 3610583 | 2009-11-27 16:29:16 -0200 | [diff] [blame] | 26 | self->type = type; |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 27 | self->start = start; |
| 28 | self->end = end; |
| 29 | self->pgoff = pgoff; |
| 30 | self->dso = dso; |
| 31 | self->map_ip = map__map_ip; |
| 32 | self->unmap_ip = map__unmap_ip; |
| 33 | RB_CLEAR_NODE(&self->rb_node); |
| 34 | } |
| 35 | |
Arnaldo Carvalho de Melo | 3610583 | 2009-11-27 16:29:16 -0200 | [diff] [blame] | 36 | struct map *map__new(struct mmap_event *event, enum map_type type, |
| 37 | char *cwd, int cwdlen) |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 38 | { |
| 39 | struct map *self = malloc(sizeof(*self)); |
| 40 | |
| 41 | if (self != NULL) { |
| 42 | const char *filename = event->filename; |
| 43 | char newfilename[PATH_MAX]; |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 44 | struct dso *dso; |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 45 | int anon; |
| 46 | |
| 47 | if (cwd) { |
| 48 | int n = strcommon(filename, cwd, cwdlen); |
| 49 | |
| 50 | if (n == cwdlen) { |
| 51 | snprintf(newfilename, sizeof(newfilename), |
| 52 | ".%s", filename + n); |
| 53 | filename = newfilename; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | anon = is_anon_memory(filename); |
| 58 | |
| 59 | if (anon) { |
| 60 | snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid); |
| 61 | filename = newfilename; |
| 62 | } |
| 63 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 64 | dso = dsos__findnew(filename); |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 65 | if (dso == NULL) |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 66 | goto out_delete; |
| 67 | |
Arnaldo Carvalho de Melo | 3610583 | 2009-11-27 16:29:16 -0200 | [diff] [blame] | 68 | map__init(self, type, event->start, event->start + event->len, |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 69 | event->pgoff, dso); |
| 70 | |
Arnaldo Carvalho de Melo | 8d92c02 | 2010-02-03 16:52:02 -0200 | [diff] [blame^] | 71 | if (anon) { |
| 72 | set_identity: |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 73 | self->map_ip = self->unmap_ip = identity__map_ip; |
Arnaldo Carvalho de Melo | 8d92c02 | 2010-02-03 16:52:02 -0200 | [diff] [blame^] | 74 | } else if (strcmp(filename, "[vdso]") == 0) { |
| 75 | dso__set_loaded(dso, self->type); |
| 76 | goto set_identity; |
| 77 | } |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 78 | } |
| 79 | return self; |
| 80 | out_delete: |
| 81 | free(self); |
| 82 | return NULL; |
| 83 | } |
| 84 | |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 85 | void map__delete(struct map *self) |
| 86 | { |
| 87 | free(self); |
| 88 | } |
| 89 | |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 90 | void map__fixup_start(struct map *self) |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 91 | { |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 92 | struct rb_root *symbols = &self->dso->symbols[self->type]; |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 93 | struct rb_node *nd = rb_first(symbols); |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 94 | if (nd != NULL) { |
| 95 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); |
| 96 | self->start = sym->start; |
| 97 | } |
| 98 | } |
| 99 | |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 100 | void map__fixup_end(struct map *self) |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 101 | { |
Arnaldo Carvalho de Melo | 6a4694a | 2009-11-27 16:29:17 -0200 | [diff] [blame] | 102 | struct rb_root *symbols = &self->dso->symbols[self->type]; |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame] | 103 | struct rb_node *nd = rb_last(symbols); |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 104 | if (nd != NULL) { |
| 105 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); |
| 106 | self->end = sym->end; |
| 107 | } |
| 108 | } |
| 109 | |
Arnaldo Carvalho de Melo | d70a540 | 2009-10-30 16:28:25 -0200 | [diff] [blame] | 110 | #define DSO__DELETED "(deleted)" |
| 111 | |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 112 | int map__load(struct map *self, symbol_filter_t filter) |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 113 | { |
| 114 | const char *name = self->dso->long_name; |
Masami Hiramatsu | a128168 | 2009-12-15 10:32:33 -0500 | [diff] [blame] | 115 | int nr; |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 116 | |
Masami Hiramatsu | a128168 | 2009-12-15 10:32:33 -0500 | [diff] [blame] | 117 | if (dso__loaded(self->dso, self->type)) |
| 118 | return 0; |
| 119 | |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 120 | nr = dso__load(self->dso, self, filter); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 121 | if (nr < 0) { |
| 122 | if (self->dso->has_build_id) { |
| 123 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
| 124 | |
| 125 | build_id__sprintf(self->dso->build_id, |
| 126 | sizeof(self->dso->build_id), |
| 127 | sbuild_id); |
| 128 | pr_warning("%s with build id %s not found", |
| 129 | name, sbuild_id); |
| 130 | } else |
| 131 | pr_warning("Failed to open %s", name); |
| 132 | |
| 133 | pr_warning(", continuing without symbols\n"); |
| 134 | return -1; |
| 135 | } else if (nr == 0) { |
| 136 | const size_t len = strlen(name); |
| 137 | const size_t real_len = len - sizeof(DSO__DELETED); |
| 138 | |
| 139 | if (len > sizeof(DSO__DELETED) && |
| 140 | strcmp(name + real_len + 1, DSO__DELETED) == 0) { |
| 141 | pr_warning("%.*s was updated, restart the long " |
| 142 | "running apps that use it!\n", |
| 143 | (int)real_len, name); |
| 144 | } else { |
| 145 | pr_warning("no symbols found in %s, maybe install " |
| 146 | "a debug package?\n", name); |
| 147 | } |
| 148 | |
| 149 | return -1; |
| 150 | } |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 151 | /* |
| 152 | * Only applies to the kernel, as its symtabs aren't relative like the |
| 153 | * module ones. |
| 154 | */ |
| 155 | if (self->dso->kernel) |
| 156 | map__reloc_vmlinux(self); |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 161 | struct symbol *map__find_symbol(struct map *self, u64 addr, |
| 162 | symbol_filter_t filter) |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 163 | { |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 164 | if (map__load(self, filter) < 0) |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 165 | return NULL; |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 166 | |
Arnaldo Carvalho de Melo | ea08d8c | 2009-12-11 18:56:39 -0200 | [diff] [blame] | 167 | return dso__find_symbol(self->dso, self->type, addr); |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 168 | } |
| 169 | |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 170 | struct symbol *map__find_symbol_by_name(struct map *self, const char *name, |
| 171 | symbol_filter_t filter) |
| 172 | { |
Arnaldo Carvalho de Melo | 9de89fe | 2010-02-03 16:52:00 -0200 | [diff] [blame] | 173 | if (map__load(self, filter) < 0) |
Arnaldo Carvalho de Melo | 79406cd | 2009-12-11 18:50:22 -0200 | [diff] [blame] | 174 | return NULL; |
| 175 | |
| 176 | if (!dso__sorted_by_name(self->dso, self->type)) |
| 177 | dso__sort_by_name(self->dso, self->type); |
| 178 | |
| 179 | return dso__find_symbol_by_name(self->dso, self->type, name); |
| 180 | } |
| 181 | |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 182 | struct map *map__clone(struct map *self) |
| 183 | { |
| 184 | struct map *map = malloc(sizeof(*self)); |
| 185 | |
| 186 | if (!map) |
| 187 | return NULL; |
| 188 | |
| 189 | memcpy(map, self, sizeof(*self)); |
| 190 | |
| 191 | return map; |
| 192 | } |
| 193 | |
| 194 | int map__overlap(struct map *l, struct map *r) |
| 195 | { |
| 196 | if (l->start > r->start) { |
| 197 | struct map *t = l; |
| 198 | l = r; |
| 199 | r = t; |
| 200 | } |
| 201 | |
| 202 | if (l->end > r->start) |
| 203 | return 1; |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | size_t map__fprintf(struct map *self, FILE *fp) |
| 209 | { |
| 210 | return fprintf(fp, " %Lx-%Lx %Lx %s\n", |
| 211 | self->start, self->end, self->pgoff, self->dso->name); |
| 212 | } |