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 | e420499 | 2009-10-20 14:25:40 -0200 | [diff] [blame] | 23 | struct map *map__new(struct mmap_event *event, char *cwd, int cwdlen, |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 24 | unsigned int sym_priv_size) |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 25 | { |
| 26 | struct map *self = malloc(sizeof(*self)); |
| 27 | |
| 28 | if (self != NULL) { |
| 29 | const char *filename = event->filename; |
| 30 | char newfilename[PATH_MAX]; |
| 31 | int anon; |
| 32 | |
| 33 | if (cwd) { |
| 34 | int n = strcommon(filename, cwd, cwdlen); |
| 35 | |
| 36 | if (n == cwdlen) { |
| 37 | snprintf(newfilename, sizeof(newfilename), |
| 38 | ".%s", filename + n); |
| 39 | filename = newfilename; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | anon = is_anon_memory(filename); |
| 44 | |
| 45 | if (anon) { |
| 46 | snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid); |
| 47 | filename = newfilename; |
| 48 | } |
| 49 | |
| 50 | self->start = event->start; |
| 51 | self->end = event->start + event->len; |
| 52 | self->pgoff = event->pgoff; |
| 53 | |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 54 | self->dso = dsos__findnew(filename, sym_priv_size); |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 55 | if (self->dso == NULL) |
| 56 | goto out_delete; |
| 57 | |
| 58 | if (self->dso == vdso || anon) |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 59 | self->map_ip = self->unmap_ip = identity__map_ip; |
| 60 | else { |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 61 | self->map_ip = map__map_ip; |
Arnaldo Carvalho de Melo | ed52ce2 | 2009-10-19 17:17:57 -0200 | [diff] [blame] | 62 | self->unmap_ip = map__unmap_ip; |
| 63 | } |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 64 | } |
| 65 | return self; |
| 66 | out_delete: |
| 67 | free(self); |
| 68 | return NULL; |
| 69 | } |
| 70 | |
Arnaldo Carvalho de Melo | 66bd842 | 2009-10-28 21:51:21 -0200 | [diff] [blame] | 71 | struct symbol * |
| 72 | map__find_symbol(struct map *self, u64 ip, symbol_filter_t filter) |
| 73 | { |
| 74 | if (!self->dso->loaded) { |
| 75 | int nr = dso__load(self->dso, self, filter); |
| 76 | |
| 77 | if (nr < 0) { |
| 78 | pr_warning("Failed to open %s, continuing without symbols\n", |
| 79 | self->dso->long_name); |
| 80 | return NULL; |
| 81 | } else if (nr == 0) { |
| 82 | pr_warning("No symbols found in %s, maybe install a debug package?\n", |
| 83 | self->dso->long_name); |
| 84 | return NULL; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return self->dso->find_symbol(self->dso, ip); |
| 89 | } |
| 90 | |
Frederic Weisbecker | 66e274f | 2009-08-12 11:07:25 +0200 | [diff] [blame] | 91 | struct map *map__clone(struct map *self) |
| 92 | { |
| 93 | struct map *map = malloc(sizeof(*self)); |
| 94 | |
| 95 | if (!map) |
| 96 | return NULL; |
| 97 | |
| 98 | memcpy(map, self, sizeof(*self)); |
| 99 | |
| 100 | return map; |
| 101 | } |
| 102 | |
| 103 | int map__overlap(struct map *l, struct map *r) |
| 104 | { |
| 105 | if (l->start > r->start) { |
| 106 | struct map *t = l; |
| 107 | l = r; |
| 108 | r = t; |
| 109 | } |
| 110 | |
| 111 | if (l->end > r->start) |
| 112 | return 1; |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | size_t map__fprintf(struct map *self, FILE *fp) |
| 118 | { |
| 119 | return fprintf(fp, " %Lx-%Lx %Lx %s\n", |
| 120 | self->start, self->end, self->pgoff, self->dso->name); |
| 121 | } |