blob: 36ff0bf0315dd8632f081c3d09a40c1ff14e15e4 [file] [log] [blame]
Frederic Weisbecker66e274f2009-08-12 11:07:25 +02001#include "event.h"
2#include "symbol.h"
3#include <stdlib.h>
4#include <string.h>
5#include <stdio.h>
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -02006#include "debug.h"
Frederic Weisbecker66e274f2009-08-12 11:07:25 +02007
8static inline int is_anon_memory(const char *filename)
9{
10 return strcmp(filename, "//anon") == 0;
11}
12
13static 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 Melo36105832009-11-27 16:29:16 -020023void map__init(struct map *self, enum map_type type,
24 u64 start, u64 end, u64 pgoff, struct dso *dso)
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020025{
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020026 self->type = type;
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020027 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 Melo36105832009-11-27 16:29:16 -020036struct map *map__new(struct mmap_event *event, enum map_type type,
37 char *cwd, int cwdlen)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020038{
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 Meloafb7b4f2009-10-30 16:28:23 -020044 struct dso *dso;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020045 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 Melo00a192b2009-10-30 16:28:24 -020064 dso = dsos__findnew(filename);
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020065 if (dso == NULL)
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020066 goto out_delete;
67
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020068 map__init(self, type, event->start, event->start + event->len,
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -020069 event->pgoff, dso);
70
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020071 if (self->dso == vdso || anon)
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -020072 self->map_ip = self->unmap_ip = identity__map_ip;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +020073 }
74 return self;
75out_delete:
76 free(self);
77 return NULL;
78}
79
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020080void map__delete(struct map *self)
81{
82 free(self);
83}
84
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -020085void map__fixup_start(struct map *self)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020086{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -020087 struct rb_root *symbols = &self->dso->symbols[self->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -020088 struct rb_node *nd = rb_first(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020089 if (nd != NULL) {
90 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
91 self->start = sym->start;
92 }
93}
94
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -020095void map__fixup_end(struct map *self)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020096{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -020097 struct rb_root *symbols = &self->dso->symbols[self->type];
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -020098 struct rb_node *nd = rb_last(symbols);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020099 if (nd != NULL) {
100 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
101 self->end = sym->end;
102 }
103}
104
Arnaldo Carvalho de Melod70a5402009-10-30 16:28:25 -0200105#define DSO__DELETED "(deleted)"
106
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200107int map__load(struct map *self, symbol_filter_t filter)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200108{
109 const char *name = self->dso->long_name;
Masami Hiramatsua1281682009-12-15 10:32:33 -0500110 int nr;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200111
Masami Hiramatsua1281682009-12-15 10:32:33 -0500112 if (dso__loaded(self->dso, self->type))
113 return 0;
114
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200115 nr = dso__load(self->dso, self, filter);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200116 if (nr < 0) {
117 if (self->dso->has_build_id) {
118 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
119
120 build_id__sprintf(self->dso->build_id,
121 sizeof(self->dso->build_id),
122 sbuild_id);
123 pr_warning("%s with build id %s not found",
124 name, sbuild_id);
125 } else
126 pr_warning("Failed to open %s", name);
127
128 pr_warning(", continuing without symbols\n");
129 return -1;
130 } else if (nr == 0) {
131 const size_t len = strlen(name);
132 const size_t real_len = len - sizeof(DSO__DELETED);
133
134 if (len > sizeof(DSO__DELETED) &&
135 strcmp(name + real_len + 1, DSO__DELETED) == 0) {
136 pr_warning("%.*s was updated, restart the long "
137 "running apps that use it!\n",
138 (int)real_len, name);
139 } else {
140 pr_warning("no symbols found in %s, maybe install "
141 "a debug package?\n", name);
142 }
143
144 return -1;
145 }
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200146 /*
147 * Only applies to the kernel, as its symtabs aren't relative like the
148 * module ones.
149 */
150 if (self->dso->kernel)
151 map__reloc_vmlinux(self);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200152
153 return 0;
154}
155
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200156struct symbol *map__find_symbol(struct map *self, u64 addr,
157 symbol_filter_t filter)
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200158{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200159 if (map__load(self, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200160 return NULL;
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200161
Arnaldo Carvalho de Meloea08d8c2009-12-11 18:56:39 -0200162 return dso__find_symbol(self->dso, self->type, addr);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200163}
164
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200165struct symbol *map__find_symbol_by_name(struct map *self, const char *name,
166 symbol_filter_t filter)
167{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200168 if (map__load(self, filter) < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200169 return NULL;
170
171 if (!dso__sorted_by_name(self->dso, self->type))
172 dso__sort_by_name(self->dso, self->type);
173
174 return dso__find_symbol_by_name(self->dso, self->type, name);
175}
176
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200177struct map *map__clone(struct map *self)
178{
179 struct map *map = malloc(sizeof(*self));
180
181 if (!map)
182 return NULL;
183
184 memcpy(map, self, sizeof(*self));
185
186 return map;
187}
188
189int map__overlap(struct map *l, struct map *r)
190{
191 if (l->start > r->start) {
192 struct map *t = l;
193 l = r;
194 r = t;
195 }
196
197 if (l->end > r->start)
198 return 1;
199
200 return 0;
201}
202
203size_t map__fprintf(struct map *self, FILE *fp)
204{
205 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
206 self->start, self->end, self->pgoff, self->dso->name);
207}