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 | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 23 | void map__init(struct map *self, u64 start, u64 end, u64 pgoff, |
| 24 | struct dso *dso) |
| 25 | { |
| 26 | self->start = start; |
| 27 | self->end = end; |
| 28 | self->pgoff = pgoff; |
| 29 | self->dso = dso; |
| 30 | self->map_ip = map__map_ip; |
| 31 | self->unmap_ip = map__unmap_ip; |
| 32 | RB_CLEAR_NODE(&self->rb_node); |
| 33 | } |
| 34 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 35 | struct map *map__new(struct mmap_event *event, char *cwd, int cwdlen) |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 36 | { |
| 37 | struct map *self = malloc(sizeof(*self)); |
| 38 | |
| 39 | if (self != NULL) { |
| 40 | const char *filename = event->filename; |
| 41 | char newfilename[PATH_MAX]; |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 42 | struct dso *dso; |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 43 | int anon; |
| 44 | |
| 45 | if (cwd) { |
| 46 | int n = strcommon(filename, cwd, cwdlen); |
| 47 | |
| 48 | if (n == cwdlen) { |
| 49 | snprintf(newfilename, sizeof(newfilename), |
| 50 | ".%s", filename + n); |
| 51 | filename = newfilename; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | anon = is_anon_memory(filename); |
| 56 | |
| 57 | if (anon) { |
| 58 | snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid); |
| 59 | filename = newfilename; |
| 60 | } |
| 61 | |
Arnaldo Carvalho de Melo | 00a192b | 2009-10-30 16:28:24 -0200 | [diff] [blame] | 62 | dso = dsos__findnew(filename); |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 63 | if (dso == NULL) |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 64 | goto out_delete; |
| 65 | |
Arnaldo Carvalho de Melo | afb7b4f | 2009-10-30 16:28:23 -0200 | [diff] [blame] | 66 | map__init(self, event->start, event->start + event->len, |
| 67 | event->pgoff, dso); |
| 68 | |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 69 | if (self->dso == vdso || anon) |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 70 | self->map_ip = self->unmap_ip = identity__map_ip; |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 71 | } |
| 72 | return self; |
| 73 | out_delete: |
| 74 | free(self); |
| 75 | return NULL; |
| 76 | } |
| 77 | |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 78 | void map__delete(struct map *self) |
| 79 | { |
| 80 | free(self); |
| 81 | } |
| 82 | |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame^] | 83 | void map__fixup_start(struct map *self, struct rb_root *symbols) |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 84 | { |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame^] | 85 | struct rb_node *nd = rb_first(symbols); |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 86 | if (nd != NULL) { |
| 87 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); |
| 88 | self->start = sym->start; |
| 89 | } |
| 90 | } |
| 91 | |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame^] | 92 | void map__fixup_end(struct map *self, struct rb_root *symbols) |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 93 | { |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame^] | 94 | struct rb_node *nd = rb_last(symbols); |
Arnaldo Carvalho de Melo | c338aee | 2009-11-20 20:51:27 -0200 | [diff] [blame] | 95 | if (nd != NULL) { |
| 96 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); |
| 97 | self->end = sym->end; |
| 98 | } |
| 99 | } |
| 100 | |
Arnaldo Carvalho de Melo | d70a540 | 2009-10-30 16:28:25 -0200 | [diff] [blame] | 101 | #define DSO__DELETED "(deleted)" |
| 102 | |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame^] | 103 | struct symbol *map__find_function(struct map *self, u64 ip, |
| 104 | symbol_filter_t filter) |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 105 | { |
| 106 | if (!self->dso->loaded) { |
| 107 | int nr = dso__load(self->dso, self, filter); |
| 108 | |
| 109 | if (nr < 0) { |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 110 | if (self->dso->has_build_id) { |
| 111 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
| 112 | |
| 113 | build_id__sprintf(self->dso->build_id, |
| 114 | sizeof(self->dso->build_id), |
| 115 | sbuild_id); |
| 116 | pr_warning("%s with build id %s not found", |
| 117 | self->dso->long_name, sbuild_id); |
| 118 | } else |
| 119 | pr_warning("Failed to open %s", |
| 120 | self->dso->long_name); |
| 121 | pr_warning(", continuing without symbols\n"); |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 122 | return NULL; |
| 123 | } else if (nr == 0) { |
Arnaldo Carvalho de Melo | d70a540 | 2009-10-30 16:28:25 -0200 | [diff] [blame] | 124 | const char *name = self->dso->long_name; |
| 125 | const size_t len = strlen(name); |
| 126 | const size_t real_len = len - sizeof(DSO__DELETED); |
| 127 | |
| 128 | if (len > sizeof(DSO__DELETED) && |
Ingo Molnar | 900b20d | 2009-11-02 19:25:25 +0100 | [diff] [blame] | 129 | strcmp(name + real_len + 1, DSO__DELETED) == 0) { |
| 130 | pr_warning("%.*s was updated, restart the long running apps that use it!\n", |
| 131 | (int)real_len, name); |
| 132 | } else { |
| 133 | pr_warning("no symbols found in %s, maybe install a debug package?\n", name); |
| 134 | } |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 135 | return NULL; |
| 136 | } |
| 137 | } |
| 138 | |
Arnaldo Carvalho de Melo | fcf1203 | 2009-11-24 13:01:52 -0200 | [diff] [blame^] | 139 | return self->dso->find_function(self->dso, ip); |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 140 | } |
| 141 | |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 142 | struct map *map__clone(struct map *self) |
| 143 | { |
| 144 | struct map *map = malloc(sizeof(*self)); |
| 145 | |
| 146 | if (!map) |
| 147 | return NULL; |
| 148 | |
| 149 | memcpy(map, self, sizeof(*self)); |
| 150 | |
| 151 | return map; |
| 152 | } |
| 153 | |
| 154 | int map__overlap(struct map *l, struct map *r) |
| 155 | { |
| 156 | if (l->start > r->start) { |
| 157 | struct map *t = l; |
| 158 | l = r; |
| 159 | r = t; |
| 160 | } |
| 161 | |
| 162 | if (l->end > r->start) |
| 163 | return 1; |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | size_t map__fprintf(struct map *self, FILE *fp) |
| 169 | { |
| 170 | return fprintf(fp, " %Lx-%Lx %Lx %s\n", |
| 171 | self->start, self->end, self->pgoff, self->dso->name); |
| 172 | } |