Commit | Line | Data |
---|---|---|
66e274f3 FW |
1 | #include "event.h" |
2 | #include "symbol.h" | |
3 | #include <stdlib.h> | |
4 | #include <string.h> | |
5 | #include <stdio.h> | |
e4204992 | 6 | #include "debug.h" |
66e274f3 FW |
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 | ||
e4204992 | 23 | struct map *map__new(struct mmap_event *event, char *cwd, int cwdlen, |
66bd8424 | 24 | unsigned int sym_priv_size) |
66e274f3 FW |
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 | ||
66bd8424 | 54 | self->dso = dsos__findnew(filename, sym_priv_size); |
66e274f3 FW |
55 | if (self->dso == NULL) |
56 | goto out_delete; | |
57 | ||
58 | if (self->dso == vdso || anon) | |
ed52ce2e ACM |
59 | self->map_ip = self->unmap_ip = identity__map_ip; |
60 | else { | |
66e274f3 | 61 | self->map_ip = map__map_ip; |
ed52ce2e ACM |
62 | self->unmap_ip = map__unmap_ip; |
63 | } | |
66e274f3 FW |
64 | } |
65 | return self; | |
66 | out_delete: | |
67 | free(self); | |
68 | return NULL; | |
69 | } | |
70 | ||
66bd8424 ACM |
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 | ||
66e274f3 FW |
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 | } |