blob: 381999dd5c1f8890c473faa624db3c50e5c03c1c [file] [log] [blame]
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001#include "util.h"
2#include "../perf.h"
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02003#include "session.h"
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02004#include "sort.h"
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -03005#include "string.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03006#include "symbol.h"
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03007#include "thread.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03008
Frederic Weisbecker8f288272009-08-16 22:05:48 +02009#include "debug.h"
10
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020011#include <asm/bug.h>
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030012#include <libelf.h>
13#include <gelf.h>
14#include <elf.h>
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -020015#include <limits.h>
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030016#include <sys/utsname.h>
Peter Zijlstra2cdbc462009-08-05 14:05:16 +020017
Arnaldo Carvalho de Meloc12e15e2009-11-21 14:31:25 -020018#ifndef NT_GNU_BUILD_ID
19#define NT_GNU_BUILD_ID 3
20#endif
21
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -030022enum dso_origin {
23 DSO__ORIG_KERNEL = 0,
24 DSO__ORIG_JAVA_JIT,
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -020025 DSO__ORIG_BUILD_ID_CACHE,
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -030026 DSO__ORIG_FEDORA,
27 DSO__ORIG_UBUNTU,
28 DSO__ORIG_BUILDID,
29 DSO__ORIG_DSO,
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030030 DSO__ORIG_KMODULE,
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -030031 DSO__ORIG_NOT_FOUND,
32};
33
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -020034static void dsos__add(struct list_head *head, struct dso *dso);
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020035static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020036static int dso__load_kernel_sym(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -020037 struct perf_session *session, symbol_filter_t filter);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -020038static int vmlinux_path__nr_entries;
39static char **vmlinux_path;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030040
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -020041struct symbol_conf symbol_conf = {
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -020042 .exclude_other = true,
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020043 .use_modules = true,
44 .try_vmlinux_path = true,
45};
46
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020047bool dso__loaded(const struct dso *self, enum map_type type)
48{
49 return self->loaded & (1 << type);
50}
51
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -020052bool dso__sorted_by_name(const struct dso *self, enum map_type type)
53{
54 return self->sorted_by_name & (1 << type);
55}
56
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020057static void dso__set_loaded(struct dso *self, enum map_type type)
58{
59 self->loaded |= (1 << type);
60}
61
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -020062static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
63{
64 self->sorted_by_name |= (1 << type);
65}
66
Arnaldo Carvalho de Melo36a3e642010-01-04 16:19:27 -020067bool symbol_type__is_a(char symbol_type, enum map_type map_type)
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020068{
69 switch (map_type) {
70 case MAP__FUNCTION:
71 return symbol_type == 'T' || symbol_type == 'W';
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -020072 case MAP__VARIABLE:
73 return symbol_type == 'D' || symbol_type == 'd';
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020074 default:
75 return false;
76 }
77}
78
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -020079static void symbols__fixup_end(struct rb_root *self)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030080{
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -020081 struct rb_node *nd, *prevnd = rb_first(self);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030082 struct symbol *curr, *prev;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030083
84 if (prevnd == NULL)
85 return;
86
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030087 curr = rb_entry(prevnd, struct symbol, rb_node);
88
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030089 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030090 prev = curr;
91 curr = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030092
93 if (prev->end == prev->start)
94 prev->end = curr->start - 1;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030095 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030096
97 /* Last entry */
98 if (curr->end == curr->start)
99 curr->end = roundup(curr->start, 4096);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300100}
101
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200102static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300103{
104 struct map *prev, *curr;
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200105 struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300106
107 if (prevnd == NULL)
108 return;
109
110 curr = rb_entry(prevnd, struct map, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300111
112 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
113 prev = curr;
114 curr = rb_entry(nd, struct map, rb_node);
115 prev->end = curr->start - 1;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300116 }
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -0200117
118 /*
119 * We still haven't the actual symbols, so guess the
120 * last map final address.
121 */
122 curr->end = ~0UL;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300123}
124
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200125static void map_groups__fixup_end(struct map_groups *self)
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -0200126{
127 int i;
128 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200129 __map_groups__fixup_end(self, i);
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -0200130}
131
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200132static struct symbol *symbol__new(u64 start, u64 len, const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300133{
Arnaldo Carvalho de Melo0085c952009-05-28 14:55:13 -0300134 size_t namelen = strlen(name) + 1;
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200135 struct symbol *self = zalloc(symbol_conf.priv_size +
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200136 sizeof(*self) + namelen);
137 if (self == NULL)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200138 return NULL;
139
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200140 if (symbol_conf.priv_size)
141 self = ((void *)self) + symbol_conf.priv_size;
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200142
Ingo Molnar0b73da32009-06-06 15:48:52 +0200143 self->start = start;
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200144 self->end = len ? start + len - 1 : start;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200145
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200146 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200147
Ingo Molnar0b73da32009-06-06 15:48:52 +0200148 memcpy(self->name, name, namelen);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300149
150 return self;
151}
152
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200153static void symbol__delete(struct symbol *self)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300154{
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200155 free(((void *)self) - symbol_conf.priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300156}
157
158static size_t symbol__fprintf(struct symbol *self, FILE *fp)
159{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300160 return fprintf(fp, " %llx-%llx %s\n",
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300161 self->start, self->end, self->name);
162}
163
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200164void dso__set_long_name(struct dso *self, char *name)
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200165{
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -0200166 if (name == NULL)
167 return;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200168 self->long_name = name;
169 self->long_name_len = strlen(name);
170}
171
172static void dso__set_basename(struct dso *self)
173{
174 self->short_name = basename(self->long_name);
175}
176
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200177struct dso *dso__new(const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300178{
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200179 struct dso *self = zalloc(sizeof(*self) + strlen(name) + 1);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300180
181 if (self != NULL) {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200182 int i;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300183 strcpy(self->name, name);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200184 dso__set_long_name(self, self->name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300185 self->short_name = self->name;
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200186 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200187 self->symbols[i] = self->symbol_names[i] = RB_ROOT;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300188 self->slen_calculated = 0;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300189 self->origin = DSO__ORIG_NOT_FOUND;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200190 self->loaded = 0;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200191 self->sorted_by_name = 0;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200192 self->has_build_id = 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300193 }
194
195 return self;
196}
197
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200198static void symbols__delete(struct rb_root *self)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300199{
200 struct symbol *pos;
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200201 struct rb_node *next = rb_first(self);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300202
203 while (next) {
204 pos = rb_entry(next, struct symbol, rb_node);
205 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200206 rb_erase(&pos->rb_node, self);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200207 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300208 }
209}
210
211void dso__delete(struct dso *self)
212{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200213 int i;
214 for (i = 0; i < MAP__NR_TYPES; ++i)
215 symbols__delete(&self->symbols[i]);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300216 if (self->long_name != self->name)
217 free(self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300218 free(self);
219}
220
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200221void dso__set_build_id(struct dso *self, void *build_id)
222{
223 memcpy(self->build_id, build_id, sizeof(self->build_id));
224 self->has_build_id = 1;
225}
226
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200227static void symbols__insert(struct rb_root *self, struct symbol *sym)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300228{
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200229 struct rb_node **p = &self->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300230 struct rb_node *parent = NULL;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000231 const u64 ip = sym->start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300232 struct symbol *s;
233
234 while (*p != NULL) {
235 parent = *p;
236 s = rb_entry(parent, struct symbol, rb_node);
237 if (ip < s->start)
238 p = &(*p)->rb_left;
239 else
240 p = &(*p)->rb_right;
241 }
242 rb_link_node(&sym->rb_node, parent, p);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200243 rb_insert_color(&sym->rb_node, self);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300244}
245
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200246static struct symbol *symbols__find(struct rb_root *self, u64 ip)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300247{
248 struct rb_node *n;
249
250 if (self == NULL)
251 return NULL;
252
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200253 n = self->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300254
255 while (n) {
256 struct symbol *s = rb_entry(n, struct symbol, rb_node);
257
258 if (ip < s->start)
259 n = n->rb_left;
260 else if (ip > s->end)
261 n = n->rb_right;
262 else
263 return s;
264 }
265
266 return NULL;
267}
268
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200269struct symbol_name_rb_node {
270 struct rb_node rb_node;
271 struct symbol sym;
272};
273
274static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
275{
276 struct rb_node **p = &self->rb_node;
277 struct rb_node *parent = NULL;
278 struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
279
280 while (*p != NULL) {
281 parent = *p;
282 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
283 if (strcmp(sym->name, s->sym.name) < 0)
284 p = &(*p)->rb_left;
285 else
286 p = &(*p)->rb_right;
287 }
288 rb_link_node(&symn->rb_node, parent, p);
289 rb_insert_color(&symn->rb_node, self);
290}
291
292static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
293{
294 struct rb_node *nd;
295
296 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
297 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
298 symbols__insert_by_name(self, pos);
299 }
300}
301
302static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
303{
304 struct rb_node *n;
305
306 if (self == NULL)
307 return NULL;
308
309 n = self->rb_node;
310
311 while (n) {
312 struct symbol_name_rb_node *s;
313 int cmp;
314
315 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
316 cmp = strcmp(name, s->sym.name);
317
318 if (cmp < 0)
319 n = n->rb_left;
320 else if (cmp > 0)
321 n = n->rb_right;
322 else
323 return &s->sym;
324 }
325
326 return NULL;
327}
328
329struct symbol *dso__find_symbol(struct dso *self,
330 enum map_type type, u64 addr)
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200331{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200332 return symbols__find(&self->symbols[type], addr);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200333}
334
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200335struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
336 const char *name)
337{
338 return symbols__find_by_name(&self->symbol_names[type], name);
339}
340
341void dso__sort_by_name(struct dso *self, enum map_type type)
342{
343 dso__set_sorted_by_name(self, type);
344 return symbols__sort_by_name(&self->symbol_names[type],
345 &self->symbols[type]);
346}
347
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200348int build_id__sprintf(u8 *self, int len, char *bf)
349{
350 char *bid = bf;
351 u8 *raw = self;
352 int i;
353
354 for (i = 0; i < len; ++i) {
355 sprintf(bid, "%02x", *raw);
356 ++raw;
357 bid += 2;
358 }
359
360 return raw - self;
361}
362
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200363size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300364{
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200365 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200366
367 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200368 return fprintf(fp, "%s", sbuild_id);
369}
370
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200371size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200372{
373 struct rb_node *nd;
374 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
375
376 ret += dso__fprintf_buildid(self, fp);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200377 ret += fprintf(fp, ")\n");
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200378 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
379 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
380 ret += symbol__fprintf(pos, fp);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300381 }
382
383 return ret;
384}
385
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200386int kallsyms__parse(void *arg, int (*process_symbol)(void *arg, const char *name,
387 char type, u64 start))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300388{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300389 char *line = NULL;
390 size_t n;
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200391 int err = 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300392 FILE *file = fopen("/proc/kallsyms", "r");
393
394 if (file == NULL)
395 goto out_failure;
396
397 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000398 u64 start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300399 int line_len, len;
400 char symbol_type;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300401 char *symbol_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300402
403 line_len = getline(&line, &n, file);
404 if (line_len < 0)
405 break;
406
407 if (!line)
408 goto out_failure;
409
410 line[--line_len] = '\0'; /* \n */
411
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300412 len = hex2u64(line, &start);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300413
414 len++;
415 if (len + 2 >= line_len)
416 continue;
417
418 symbol_type = toupper(line[len]);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300419 symbol_name = line + len + 2;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300420
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200421 err = process_symbol(arg, symbol_name, symbol_type, start);
422 if (err)
423 break;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300424 }
425
426 free(line);
427 fclose(file);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200428 return err;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300429
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300430out_failure:
431 return -1;
432}
433
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200434struct process_kallsyms_args {
435 struct map *map;
436 struct dso *dso;
437};
438
439static int map__process_kallsym_symbol(void *arg, const char *name,
440 char type, u64 start)
441{
442 struct symbol *sym;
443 struct process_kallsyms_args *a = arg;
444 struct rb_root *root = &a->dso->symbols[a->map->type];
445
446 if (!symbol_type__is_a(type, a->map->type))
447 return 0;
448
449 /*
450 * Will fix up the end later, when we have all symbols sorted.
451 */
452 sym = symbol__new(start, 0, name);
453
454 if (sym == NULL)
455 return -ENOMEM;
456 /*
457 * We will pass the symbols to the filter later, in
458 * map__split_kallsyms, when we have split the maps per module
459 */
460 symbols__insert(root, sym);
461 return 0;
462}
463
464/*
465 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
466 * so that we can in the next step set the symbol ->end address and then
467 * call kernel_maps__split_kallsyms.
468 */
469static int dso__load_all_kallsyms(struct dso *self, struct map *map)
470{
471 struct process_kallsyms_args args = { .map = map, .dso = self, };
472 return kallsyms__parse(&args, map__process_kallsym_symbol);
473}
474
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300475/*
476 * Split the symbols into maps, making sure there are no overlaps, i.e. the
477 * kernel range is broken in several maps, named [kernel].N, as we don't have
478 * the original ELF section names vmlinux have.
479 */
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200480static int dso__split_kallsyms(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200481 struct perf_session *session, symbol_filter_t filter)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300482{
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200483 struct map *curr_map = map;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300484 struct symbol *pos;
485 int count = 0;
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200486 struct rb_root *root = &self->symbols[map->type];
487 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300488 int kernel_range = 0;
489
490 while (next) {
491 char *module;
492
493 pos = rb_entry(next, struct symbol, rb_node);
494 next = rb_next(&pos->rb_node);
495
496 module = strchr(pos->name, '\t');
497 if (module) {
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200498 if (!symbol_conf.use_modules)
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200499 goto discard_symbol;
500
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300501 *module++ = '\0';
502
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200503 if (strcmp(curr_map->dso->short_name, module)) {
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200504 curr_map = map_groups__find_by_name(&session->kmaps, map->type, module);
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200505 if (curr_map == NULL) {
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200506 pr_debug("/proc/{kallsyms,modules} "
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200507 "inconsistency while looking "
508 "for \"%s\" module!\n", module);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300509 return -1;
510 }
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200511
512 if (curr_map->dso->loaded)
513 goto discard_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300514 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300515 /*
516 * So that we look just like we get from .ko files,
517 * i.e. not prelinked, relative to map->start.
518 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200519 pos->start = curr_map->map_ip(curr_map, pos->start);
520 pos->end = curr_map->map_ip(curr_map, pos->end);
521 } else if (curr_map != map) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300522 char dso_name[PATH_MAX];
523 struct dso *dso;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300524
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300525 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
526 kernel_range++);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300527
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200528 dso = dso__new(dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300529 if (dso == NULL)
530 return -1;
531
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200532 curr_map = map__new2(pos->start, dso, map->type);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300533 if (map == NULL) {
534 dso__delete(dso);
535 return -1;
536 }
537
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200538 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200539 map_groups__insert(&session->kmaps, curr_map);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300540 ++kernel_range;
541 }
542
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200543 if (filter && filter(curr_map, pos)) {
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200544discard_symbol: rb_erase(&pos->rb_node, root);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200545 symbol__delete(pos);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300546 } else {
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200547 if (curr_map != map) {
548 rb_erase(&pos->rb_node, root);
549 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300550 }
Mike Galbraith9974f492009-07-02 08:05:58 +0200551 count++;
552 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300553 }
554
Mike Galbraith9974f492009-07-02 08:05:58 +0200555 return count;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300556}
557
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300558
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200559static int dso__load_kallsyms(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200560 struct perf_session *session, symbol_filter_t filter)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300561{
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200562 if (dso__load_all_kallsyms(self, map) < 0)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300563 return -1;
564
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200565 symbols__fixup_end(&self->symbols[map->type]);
566 self->origin = DSO__ORIG_KERNEL;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300567
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200568 return dso__split_kallsyms(self, map, session, filter);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300569}
570
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300571static int dso__load_perf_map(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200572 symbol_filter_t filter)
Pekka Enberg80d496b2009-06-08 21:12:48 +0300573{
574 char *line = NULL;
575 size_t n;
576 FILE *file;
577 int nr_syms = 0;
578
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300579 file = fopen(self->long_name, "r");
Pekka Enberg80d496b2009-06-08 21:12:48 +0300580 if (file == NULL)
581 goto out_failure;
582
583 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000584 u64 start, size;
Pekka Enberg80d496b2009-06-08 21:12:48 +0300585 struct symbol *sym;
586 int line_len, len;
587
588 line_len = getline(&line, &n, file);
589 if (line_len < 0)
590 break;
591
592 if (!line)
593 goto out_failure;
594
595 line[--line_len] = '\0'; /* \n */
596
597 len = hex2u64(line, &start);
598
599 len++;
600 if (len + 2 >= line_len)
601 continue;
602
603 len += hex2u64(line + len, &size);
604
605 len++;
606 if (len + 2 >= line_len)
607 continue;
608
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200609 sym = symbol__new(start, size, line + len);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300610
611 if (sym == NULL)
612 goto out_delete_line;
613
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300614 if (filter && filter(map, sym))
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200615 symbol__delete(sym);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300616 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200617 symbols__insert(&self->symbols[map->type], sym);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300618 nr_syms++;
619 }
620 }
621
622 free(line);
623 fclose(file);
624
625 return nr_syms;
626
627out_delete_line:
628 free(line);
629out_failure:
630 return -1;
631}
632
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300633/**
634 * elf_symtab__for_each_symbol - iterate thru all the symbols
635 *
636 * @self: struct elf_symtab instance to iterate
Ingo Molnar83a09442009-08-15 12:26:57 +0200637 * @idx: uint32_t idx
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300638 * @sym: GElf_Sym iterator
639 */
Ingo Molnar83a09442009-08-15 12:26:57 +0200640#define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
641 for (idx = 0, gelf_getsym(syms, idx, &sym);\
642 idx < nr_syms; \
643 idx++, gelf_getsym(syms, idx, &sym))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300644
645static inline uint8_t elf_sym__type(const GElf_Sym *sym)
646{
647 return GELF_ST_TYPE(sym->st_info);
648}
649
650static inline int elf_sym__is_function(const GElf_Sym *sym)
651{
652 return elf_sym__type(sym) == STT_FUNC &&
653 sym->st_name != 0 &&
Arnaldo Carvalho de Melo81833132009-10-05 23:35:03 -0300654 sym->st_shndx != SHN_UNDEF;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300655}
656
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200657static inline bool elf_sym__is_object(const GElf_Sym *sym)
658{
659 return elf_sym__type(sym) == STT_OBJECT &&
660 sym->st_name != 0 &&
661 sym->st_shndx != SHN_UNDEF;
662}
663
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200664static inline int elf_sym__is_label(const GElf_Sym *sym)
665{
666 return elf_sym__type(sym) == STT_NOTYPE &&
667 sym->st_name != 0 &&
668 sym->st_shndx != SHN_UNDEF &&
669 sym->st_shndx != SHN_ABS;
670}
671
672static inline const char *elf_sec__name(const GElf_Shdr *shdr,
673 const Elf_Data *secstrs)
674{
675 return secstrs->d_buf + shdr->sh_name;
676}
677
678static inline int elf_sec__is_text(const GElf_Shdr *shdr,
679 const Elf_Data *secstrs)
680{
681 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
682}
683
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200684static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
685 const Elf_Data *secstrs)
686{
687 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
688}
689
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300690static inline const char *elf_sym__name(const GElf_Sym *sym,
691 const Elf_Data *symstrs)
692{
693 return symstrs->d_buf + sym->st_name;
694}
695
696static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
697 GElf_Shdr *shp, const char *name,
Ingo Molnar83a09442009-08-15 12:26:57 +0200698 size_t *idx)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300699{
700 Elf_Scn *sec = NULL;
701 size_t cnt = 1;
702
703 while ((sec = elf_nextscn(elf, sec)) != NULL) {
704 char *str;
705
706 gelf_getshdr(sec, shp);
707 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
708 if (!strcmp(name, str)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200709 if (idx)
710 *idx = cnt;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300711 break;
712 }
713 ++cnt;
714 }
715
716 return sec;
717}
718
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300719#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
720 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
721 idx < nr_entries; \
722 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
723
724#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
725 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
726 idx < nr_entries; \
727 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
728
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300729/*
730 * We need to check if we have a .dynsym, so that we can handle the
731 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
732 * .dynsym or .symtab).
733 * And always look at the original dso, not at debuginfo packages, that
734 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
735 */
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200736static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
737 symbol_filter_t filter)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300738{
739 uint32_t nr_rel_entries, idx;
740 GElf_Sym sym;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000741 u64 plt_offset;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300742 GElf_Shdr shdr_plt;
743 struct symbol *f;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300744 GElf_Shdr shdr_rel_plt, shdr_dynsym;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300745 Elf_Data *reldata, *syms, *symstrs;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300746 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
747 size_t dynsym_idx;
748 GElf_Ehdr ehdr;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300749 char sympltname[1024];
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300750 Elf *elf;
751 int nr = 0, symidx, fd, err = 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300752
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300753 fd = open(self->long_name, O_RDONLY);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300754 if (fd < 0)
755 goto out;
756
Marti Raudsepp84087122009-10-24 19:10:36 +0300757 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300758 if (elf == NULL)
759 goto out_close;
760
761 if (gelf_getehdr(elf, &ehdr) == NULL)
762 goto out_elf_end;
763
764 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
765 ".dynsym", &dynsym_idx);
766 if (scn_dynsym == NULL)
767 goto out_elf_end;
768
769 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300770 ".rela.plt", NULL);
771 if (scn_plt_rel == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300772 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300773 ".rel.plt", NULL);
774 if (scn_plt_rel == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300775 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300776 }
777
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300778 err = -1;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300779
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300780 if (shdr_rel_plt.sh_link != dynsym_idx)
781 goto out_elf_end;
782
783 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
784 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300785
786 /*
Ingo Molnar83a09442009-08-15 12:26:57 +0200787 * Fetch the relocation section to find the idxes to the GOT
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300788 * and the symbols in the .dynsym they refer to.
789 */
790 reldata = elf_getdata(scn_plt_rel, NULL);
791 if (reldata == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300792 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300793
794 syms = elf_getdata(scn_dynsym, NULL);
795 if (syms == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300796 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300797
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300798 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300799 if (scn_symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300800 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300801
802 symstrs = elf_getdata(scn_symstrs, NULL);
803 if (symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300804 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300805
806 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
807 plt_offset = shdr_plt.sh_offset;
808
809 if (shdr_rel_plt.sh_type == SHT_RELA) {
810 GElf_Rela pos_mem, *pos;
811
812 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
813 nr_rel_entries) {
814 symidx = GELF_R_SYM(pos->r_info);
815 plt_offset += shdr_plt.sh_entsize;
816 gelf_getsym(syms, symidx, &sym);
817 snprintf(sympltname, sizeof(sympltname),
818 "%s@plt", elf_sym__name(&sym, symstrs));
819
820 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200821 sympltname);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300822 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300823 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300824
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200825 if (filter && filter(map, f))
826 symbol__delete(f);
827 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200828 symbols__insert(&self->symbols[map->type], f);
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200829 ++nr;
830 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300831 }
832 } else if (shdr_rel_plt.sh_type == SHT_REL) {
833 GElf_Rel pos_mem, *pos;
834 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
835 nr_rel_entries) {
836 symidx = GELF_R_SYM(pos->r_info);
837 plt_offset += shdr_plt.sh_entsize;
838 gelf_getsym(syms, symidx, &sym);
839 snprintf(sympltname, sizeof(sympltname),
840 "%s@plt", elf_sym__name(&sym, symstrs));
841
842 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200843 sympltname);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300844 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300845 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300846
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200847 if (filter && filter(map, f))
848 symbol__delete(f);
849 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200850 symbols__insert(&self->symbols[map->type], f);
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200851 ++nr;
852 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300853 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300854 }
855
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300856 err = 0;
857out_elf_end:
858 elf_end(elf);
859out_close:
860 close(fd);
861
862 if (err == 0)
863 return nr;
864out:
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200865 pr_warning("%s: problems reading %s PLT info.\n",
866 __func__, self->long_name);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300867 return 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300868}
869
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -0200870static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
871{
872 switch (type) {
873 case MAP__FUNCTION:
874 return elf_sym__is_function(self);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200875 case MAP__VARIABLE:
876 return elf_sym__is_object(self);
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -0200877 default:
878 return false;
879 }
880}
881
882static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
883{
884 switch (type) {
885 case MAP__FUNCTION:
886 return elf_sec__is_text(self, secstrs);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200887 case MAP__VARIABLE:
888 return elf_sec__is_data(self, secstrs);
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -0200889 default:
890 return false;
891 }
892}
893
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200894static int dso__load_sym(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200895 struct perf_session *session, const char *name, int fd,
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200896 symbol_filter_t filter, int kernel, int kmodule)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300897{
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300898 struct map *curr_map = map;
899 struct dso *curr_dso = self;
900 size_t dso_name_len = strlen(self->short_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200901 Elf_Data *symstrs, *secstrs;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300902 uint32_t nr_syms;
903 int err = -1;
Ingo Molnar83a09442009-08-15 12:26:57 +0200904 uint32_t idx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300905 GElf_Ehdr ehdr;
906 GElf_Shdr shdr;
907 Elf_Data *syms;
908 GElf_Sym sym;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300909 Elf_Scn *sec, *sec_strndx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300910 Elf *elf;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300911 int nr = 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300912
Marti Raudsepp84087122009-10-24 19:10:36 +0300913 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300914 if (elf == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200915 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300916 goto out_close;
917 }
918
919 if (gelf_getehdr(elf, &ehdr) == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200920 pr_err("%s: cannot get elf header.\n", __func__);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300921 goto out_elf_end;
922 }
923
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300924 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
925 if (sec == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300926 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
927 if (sec == NULL)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300928 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300929 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300930
931 syms = elf_getdata(sec, NULL);
932 if (syms == NULL)
933 goto out_elf_end;
934
935 sec = elf_getscn(elf, shdr.sh_link);
936 if (sec == NULL)
937 goto out_elf_end;
938
939 symstrs = elf_getdata(sec, NULL);
940 if (symstrs == NULL)
941 goto out_elf_end;
942
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200943 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
944 if (sec_strndx == NULL)
945 goto out_elf_end;
946
947 secstrs = elf_getdata(sec_strndx, NULL);
Stoyan Gaydarov9b30a262009-07-30 05:25:29 -0500948 if (secstrs == NULL)
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200949 goto out_elf_end;
950
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300951 nr_syms = shdr.sh_size / shdr.sh_entsize;
952
Arjan van de Vene9fbc9d2009-06-06 21:22:33 +0200953 memset(&sym, 0, sizeof(sym));
Mike Galbraithd20ff6b2009-07-20 14:01:38 +0200954 if (!kernel) {
955 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -0300956 elf_section_by_name(elf, &ehdr, &shdr,
957 ".gnu.prelink_undo",
958 NULL) != NULL);
Mike Galbraithd20ff6b2009-07-20 14:01:38 +0200959 } else self->adjust_symbols = 0;
960
Ingo Molnar83a09442009-08-15 12:26:57 +0200961 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300962 struct symbol *f;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200963 const char *elf_name = elf_sym__name(&sym, symstrs);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300964 char *demangled = NULL;
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200965 int is_label = elf_sym__is_label(&sym);
966 const char *section_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300967
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -0200968 if (kernel && session->ref_reloc_sym.name != NULL &&
969 strcmp(elf_name, session->ref_reloc_sym.name) == 0)
970 perf_session__reloc_vmlinux_maps(session, sym.st_value);
971
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -0200972 if (!is_label && !elf_sym__is_a(&sym, map->type))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300973 continue;
974
975 sec = elf_getscn(elf, sym.st_shndx);
976 if (!sec)
977 goto out_elf_end;
978
979 gelf_getshdr(sec, &shdr);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200980
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -0200981 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200982 continue;
983
984 section_name = elf_sec__name(&shdr, secstrs);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200985
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300986 if (kernel || kmodule) {
987 char dso_name[PATH_MAX];
988
989 if (strcmp(section_name,
990 curr_dso->short_name + dso_name_len) == 0)
991 goto new_symbol;
992
993 if (strcmp(section_name, ".text") == 0) {
994 curr_map = map;
995 curr_dso = self;
996 goto new_symbol;
997 }
998
999 snprintf(dso_name, sizeof(dso_name),
1000 "%s%s", self->short_name, section_name);
1001
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001002 curr_map = map_groups__find_by_name(&session->kmaps, map->type, dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001003 if (curr_map == NULL) {
1004 u64 start = sym.st_value;
1005
1006 if (kmodule)
1007 start += map->start + shdr.sh_offset;
1008
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001009 curr_dso = dso__new(dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001010 if (curr_dso == NULL)
1011 goto out_elf_end;
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001012 curr_map = map__new2(start, curr_dso,
1013 MAP__FUNCTION);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001014 if (curr_map == NULL) {
1015 dso__delete(curr_dso);
1016 goto out_elf_end;
1017 }
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -02001018 curr_map->map_ip = identity__map_ip;
1019 curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001020 curr_dso->origin = DSO__ORIG_KERNEL;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001021 map_groups__insert(&session->kmaps, curr_map);
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001022 dsos__add(&dsos__kernel, curr_dso);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001023 } else
1024 curr_dso = curr_map->dso;
1025
1026 goto new_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001027 }
1028
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001029 if (curr_dso->adjust_symbols) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001030 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
1031 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
1032 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001033 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001034 }
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -03001035 /*
1036 * We need to figure out if the object was created from C++ sources
1037 * DWARF DW_compile_unit has this, but we don't always have access
1038 * to it...
1039 */
Ingo Molnar83a09442009-08-15 12:26:57 +02001040 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -03001041 if (demangled != NULL)
Ingo Molnar83a09442009-08-15 12:26:57 +02001042 elf_name = demangled;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001043new_symbol:
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001044 f = symbol__new(sym.st_value, sym.st_size, elf_name);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -03001045 free(demangled);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001046 if (!f)
1047 goto out_elf_end;
1048
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001049 if (filter && filter(curr_map, f))
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001050 symbol__delete(f);
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -03001051 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001052 symbols__insert(&curr_dso->symbols[curr_map->type], f);
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -03001053 nr++;
1054 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001055 }
1056
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001057 /*
1058 * For misannotated, zeroed, ASM function sizes.
1059 */
1060 if (nr > 0)
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001061 symbols__fixup_end(&self->symbols[map->type]);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001062 err = nr;
1063out_elf_end:
1064 elf_end(elf);
1065out_close:
1066 return err;
1067}
1068
Arnaldo Carvalho de Melo78075ca2009-11-20 20:51:26 -02001069static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1070{
1071 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1072}
1073
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001074static bool __dsos__read_build_ids(struct list_head *head)
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001075{
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -02001076 bool have_build_id = false;
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001077 struct dso *pos;
1078
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001079 list_for_each_entry(pos, head, node)
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -02001080 if (filename__read_build_id(pos->long_name, pos->build_id,
1081 sizeof(pos->build_id)) > 0) {
1082 have_build_id = true;
1083 pos->has_build_id = true;
1084 }
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001085
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -02001086 return have_build_id;
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001087}
1088
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001089bool dsos__read_build_ids(void)
1090{
Arnaldo Carvalho de Melo8b4825b2009-12-09 20:09:37 -02001091 bool kbuildids = __dsos__read_build_ids(&dsos__kernel),
1092 ubuildids = __dsos__read_build_ids(&dsos__user);
1093 return kbuildids || ubuildids;
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001094}
1095
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001096/*
1097 * Align offset to 4 bytes as needed for note name and descriptor data.
1098 */
1099#define NOTE_ALIGN(n) (((n) + 3) & -4U)
1100
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -02001101int filename__read_build_id(const char *filename, void *bf, size_t size)
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001102{
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -02001103 int fd, err = -1;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001104 GElf_Ehdr ehdr;
1105 GElf_Shdr shdr;
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001106 Elf_Data *data;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001107 Elf_Scn *sec;
Pekka Enberge57cfcd2009-11-22 12:29:44 +02001108 Elf_Kind ek;
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001109 void *ptr;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001110 Elf *elf;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001111
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -02001112 if (size < BUILD_ID_SIZE)
1113 goto out;
1114
1115 fd = open(filename, O_RDONLY);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001116 if (fd < 0)
1117 goto out;
1118
Marti Raudsepp84087122009-10-24 19:10:36 +03001119 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001120 if (elf == NULL) {
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001121 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001122 goto out_close;
1123 }
1124
Pekka Enberge57cfcd2009-11-22 12:29:44 +02001125 ek = elf_kind(elf);
1126 if (ek != ELF_K_ELF)
1127 goto out_elf_end;
1128
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001129 if (gelf_getehdr(elf, &ehdr) == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001130 pr_err("%s: cannot get elf header.\n", __func__);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001131 goto out_elf_end;
1132 }
1133
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -02001134 sec = elf_section_by_name(elf, &ehdr, &shdr,
1135 ".note.gnu.build-id", NULL);
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001136 if (sec == NULL) {
1137 sec = elf_section_by_name(elf, &ehdr, &shdr,
1138 ".notes", NULL);
1139 if (sec == NULL)
1140 goto out_elf_end;
1141 }
1142
1143 data = elf_getdata(sec, NULL);
1144 if (data == NULL)
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001145 goto out_elf_end;
1146
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001147 ptr = data->d_buf;
1148 while (ptr < (data->d_buf + data->d_size)) {
1149 GElf_Nhdr *nhdr = ptr;
1150 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1151 descsz = NOTE_ALIGN(nhdr->n_descsz);
1152 const char *name;
1153
1154 ptr += sizeof(*nhdr);
1155 name = ptr;
1156 ptr += namesz;
1157 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1158 nhdr->n_namesz == sizeof("GNU")) {
1159 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1160 memcpy(bf, ptr, BUILD_ID_SIZE);
1161 err = BUILD_ID_SIZE;
1162 break;
1163 }
1164 }
1165 ptr += descsz;
1166 }
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -02001167out_elf_end:
1168 elf_end(elf);
1169out_close:
1170 close(fd);
1171out:
1172 return err;
1173}
1174
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001175int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1176{
1177 int fd, err = -1;
1178
1179 if (size < BUILD_ID_SIZE)
1180 goto out;
1181
1182 fd = open(filename, O_RDONLY);
1183 if (fd < 0)
1184 goto out;
1185
1186 while (1) {
1187 char bf[BUFSIZ];
1188 GElf_Nhdr nhdr;
1189 int namesz, descsz;
1190
1191 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1192 break;
1193
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001194 namesz = NOTE_ALIGN(nhdr.n_namesz);
1195 descsz = NOTE_ALIGN(nhdr.n_descsz);
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001196 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1197 nhdr.n_namesz == sizeof("GNU")) {
1198 if (read(fd, bf, namesz) != namesz)
1199 break;
1200 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1201 if (read(fd, build_id,
1202 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1203 err = 0;
1204 break;
1205 }
1206 } else if (read(fd, bf, descsz) != descsz)
1207 break;
1208 } else {
1209 int n = namesz + descsz;
1210 if (read(fd, bf, n) != n)
1211 break;
1212 }
1213 }
1214 close(fd);
1215out:
1216 return err;
1217}
1218
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001219char dso__symtab_origin(const struct dso *self)
1220{
1221 static const char origin[] = {
1222 [DSO__ORIG_KERNEL] = 'k',
1223 [DSO__ORIG_JAVA_JIT] = 'j',
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02001224 [DSO__ORIG_BUILD_ID_CACHE] = 'B',
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001225 [DSO__ORIG_FEDORA] = 'f',
1226 [DSO__ORIG_UBUNTU] = 'u',
1227 [DSO__ORIG_BUILDID] = 'b',
1228 [DSO__ORIG_DSO] = 'd',
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001229 [DSO__ORIG_KMODULE] = 'K',
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001230 };
1231
1232 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1233 return '!';
1234 return origin[self->origin];
1235}
1236
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001237int dso__load(struct dso *self, struct map *map, struct perf_session *session,
1238 symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001239{
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001240 int size = PATH_MAX;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001241 char *name;
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001242 u8 build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02001243 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001244 int ret = -1;
1245 int fd;
1246
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001247 dso__set_loaded(self, map->type);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001248
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001249 if (self->kernel)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001250 return dso__load_kernel_sym(self, map, session, filter);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001251
1252 name = malloc(size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001253 if (!name)
1254 return -1;
1255
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -03001256 self->adjust_symbols = 0;
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001257
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001258 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001259 ret = dso__load_perf_map(self, map, filter);
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001260 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1261 DSO__ORIG_NOT_FOUND;
1262 return ret;
1263 }
1264
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02001265 self->origin = DSO__ORIG_BUILD_ID_CACHE;
Pekka Enberg80d496b2009-06-08 21:12:48 +03001266
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02001267 if (self->has_build_id) {
1268 build_id__sprintf(self->build_id, sizeof(self->build_id),
1269 build_id_hex);
1270 snprintf(name, size, "%s/%s/.build-id/%.2s/%s",
1271 getenv("HOME"), DEBUG_CACHE_DIR,
1272 build_id_hex, build_id_hex + 2);
1273 goto open_file;
1274 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001275more:
1276 do {
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001277 self->origin++;
1278 switch (self->origin) {
1279 case DSO__ORIG_FEDORA:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001280 snprintf(name, size, "/usr/lib/debug%s.debug",
1281 self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001282 break;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001283 case DSO__ORIG_UBUNTU:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001284 snprintf(name, size, "/usr/lib/debug%s",
1285 self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001286 break;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001287 case DSO__ORIG_BUILDID:
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001288 if (filename__read_build_id(self->long_name, build_id,
1289 sizeof(build_id))) {
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001290 build_id__sprintf(build_id, sizeof(build_id),
1291 build_id_hex);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001292 snprintf(name, size,
1293 "/usr/lib/debug/.build-id/%.2s/%s.debug",
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001294 build_id_hex, build_id_hex + 2);
1295 if (self->has_build_id)
1296 goto compare_build_id;
1297 break;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001298 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001299 self->origin++;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001300 /* Fall thru */
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001301 case DSO__ORIG_DSO:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001302 snprintf(name, size, "%s", self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001303 break;
1304
1305 default:
1306 goto out;
1307 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001308
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001309 if (self->has_build_id) {
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001310 if (filename__read_build_id(name, build_id,
1311 sizeof(build_id)) < 0)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001312 goto more;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001313compare_build_id:
Arnaldo Carvalho de Melo78075ca2009-11-20 20:51:26 -02001314 if (!dso__build_id_equal(self, build_id))
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001315 goto more;
1316 }
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02001317open_file:
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001318 fd = open(name, O_RDONLY);
1319 } while (fd < 0);
1320
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001321 ret = dso__load_sym(self, map, NULL, name, fd, filter, 0, 0);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001322 close(fd);
1323
1324 /*
1325 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1326 */
1327 if (!ret)
1328 goto more;
1329
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001330 if (ret > 0) {
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -02001331 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001332 if (nr_plt > 0)
1333 ret += nr_plt;
1334 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001335out:
1336 free(name);
Arnaldo Carvalho de Melo1340e6b2009-08-11 17:04:36 -03001337 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1338 return 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001339 return ret;
1340}
1341
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -02001342struct map *map_groups__find_by_name(struct map_groups *self,
1343 enum map_type type, const char *name)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001344{
1345 struct rb_node *nd;
1346
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -02001347 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001348 struct map *map = rb_entry(nd, struct map, rb_node);
1349
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001350 if (map->dso && strcmp(map->dso->short_name, name) == 0)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001351 return map;
1352 }
1353
1354 return NULL;
1355}
1356
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001357static int dso__kernel_module_get_build_id(struct dso *self)
1358{
1359 char filename[PATH_MAX];
1360 /*
1361 * kernel module short names are of the form "[module]" and
1362 * we need just "module" here.
1363 */
1364 const char *name = self->short_name + 1;
1365
1366 snprintf(filename, sizeof(filename),
1367 "/sys/module/%.*s/notes/.note.gnu.build-id",
1368 (int)strlen(name - 1), name);
1369
1370 if (sysfs__read_build_id(filename, self->build_id,
1371 sizeof(self->build_id)) == 0)
1372 self->has_build_id = true;
1373
1374 return 0;
1375}
1376
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001377static int perf_session__set_modules_path_dir(struct perf_session *self, char *dirname)
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001378{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001379 struct dirent *dent;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001380 DIR *dir = opendir(dirname);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001381
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001382 if (!dir) {
Arnaldo Carvalho de Melo87f8ea42009-11-22 13:21:41 -02001383 pr_debug("%s: cannot open %s dir\n", __func__, dirname);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001384 return -1;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001385 }
1386
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001387 while ((dent = readdir(dir)) != NULL) {
1388 char path[PATH_MAX];
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001389
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001390 if (dent->d_type == DT_DIR) {
1391 if (!strcmp(dent->d_name, ".") ||
1392 !strcmp(dent->d_name, ".."))
1393 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001394
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001395 snprintf(path, sizeof(path), "%s/%s",
1396 dirname, dent->d_name);
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001397 if (perf_session__set_modules_path_dir(self, path) < 0)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001398 goto failure;
1399 } else {
1400 char *dot = strrchr(dent->d_name, '.'),
1401 dso_name[PATH_MAX];
1402 struct map *map;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001403 char *long_name;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001404
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001405 if (dot == NULL || strcmp(dot, ".ko"))
1406 continue;
1407 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1408 (int)(dot - dent->d_name), dent->d_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001409
Arnaldo Carvalho de Meloa2a99e82009-10-05 14:26:18 -03001410 strxfrchar(dso_name, '-', '_');
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001411 map = map_groups__find_by_name(&self->kmaps, MAP__FUNCTION, dso_name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001412 if (map == NULL)
1413 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001414
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001415 snprintf(path, sizeof(path), "%s/%s",
1416 dirname, dent->d_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001417
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001418 long_name = strdup(path);
1419 if (long_name == NULL)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001420 goto failure;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001421 dso__set_long_name(map->dso, long_name);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001422 dso__kernel_module_get_build_id(map->dso);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001423 }
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001424 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001425
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001426 return 0;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001427failure:
1428 closedir(dir);
1429 return -1;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001430}
1431
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001432static int perf_session__set_modules_path(struct perf_session *self)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001433{
1434 struct utsname uts;
1435 char modules_path[PATH_MAX];
1436
1437 if (uname(&uts) < 0)
1438 return -1;
1439
1440 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1441 uts.release);
1442
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001443 return perf_session__set_modules_path_dir(self, modules_path);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001444}
1445
1446/*
1447 * Constructor variant for modules (where we know from /proc/modules where
1448 * they are loaded) and for vmlinux, where only after we load all the
1449 * symbols we'll know where it starts and ends.
1450 */
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001451static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001452{
1453 struct map *self = malloc(sizeof(*self));
1454
1455 if (self != NULL) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001456 /*
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -02001457 * ->end will be filled after we load all the symbols
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001458 */
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001459 map__init(self, type, start, 0, 0, dso);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001460 }
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -02001461
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001462 return self;
1463}
1464
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001465struct map *perf_session__new_module_map(struct perf_session *self, u64 start,
1466 const char *filename)
1467{
1468 struct map *map;
1469 struct dso *dso = __dsos__findnew(&dsos__kernel, filename);
1470
1471 if (dso == NULL)
1472 return NULL;
1473
1474 map = map__new2(start, dso, MAP__FUNCTION);
1475 if (map == NULL)
1476 return NULL;
1477
1478 dso->origin = DSO__ORIG_KMODULE;
1479 map_groups__insert(&self->kmaps, map);
1480 return map;
1481}
1482
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001483static int perf_session__create_module_maps(struct perf_session *self)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001484{
1485 char *line = NULL;
1486 size_t n;
1487 FILE *file = fopen("/proc/modules", "r");
1488 struct map *map;
1489
1490 if (file == NULL)
1491 return -1;
1492
1493 while (!feof(file)) {
1494 char name[PATH_MAX];
1495 u64 start;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001496 char *sep;
1497 int line_len;
1498
1499 line_len = getline(&line, &n, file);
1500 if (line_len < 0)
1501 break;
1502
1503 if (!line)
1504 goto out_failure;
1505
1506 line[--line_len] = '\0'; /* \n */
1507
1508 sep = strrchr(line, 'x');
1509 if (sep == NULL)
1510 continue;
1511
1512 hex2u64(sep + 1, &start);
1513
1514 sep = strchr(line, ' ');
1515 if (sep == NULL)
1516 continue;
1517
1518 *sep = '\0';
1519
1520 snprintf(name, sizeof(name), "[%s]", line);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001521 map = perf_session__new_module_map(self, start, name);
1522 if (map == NULL)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001523 goto out_delete_line;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001524 dso__kernel_module_get_build_id(map->dso);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001525 }
1526
1527 free(line);
1528 fclose(file);
1529
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001530 return perf_session__set_modules_path(self);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001531
1532out_delete_line:
1533 free(line);
1534out_failure:
1535 return -1;
1536}
1537
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -02001538static int dso__load_vmlinux(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001539 struct perf_session *session,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001540 const char *vmlinux, symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001541{
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001542 int err = -1, fd;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001543
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001544 if (self->has_build_id) {
1545 u8 build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001546
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001547 if (filename__read_build_id(vmlinux, build_id,
1548 sizeof(build_id)) < 0) {
1549 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1550 return -1;
1551 }
1552 if (!dso__build_id_equal(self, build_id)) {
1553 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1554 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1555
1556 build_id__sprintf(self->build_id,
1557 sizeof(self->build_id),
1558 expected_build_id);
1559 build_id__sprintf(build_id, sizeof(build_id),
1560 vmlinux_build_id);
1561 pr_debug("build_id in %s is %s while expected is %s, "
1562 "ignoring it\n", vmlinux, vmlinux_build_id,
1563 expected_build_id);
1564 return -1;
1565 }
1566 }
1567
1568 fd = open(vmlinux, O_RDONLY);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001569 if (fd < 0)
1570 return -1;
1571
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001572 dso__set_loaded(self, map->type);
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001573 err = dso__load_sym(self, map, session, self->long_name, fd, filter, 1, 0);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001574 close(fd);
1575
1576 return err;
1577}
1578
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001579static int dso__load_kernel_sym(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001580 struct perf_session *session, symbol_filter_t filter)
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001581{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001582 int err;
1583 bool is_kallsyms;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001584
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001585 if (vmlinux_path != NULL) {
1586 int i;
1587 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1588 vmlinux_path__nr_entries);
1589 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001590 err = dso__load_vmlinux(self, map, session,
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001591 vmlinux_path[i], filter);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001592 if (err > 0) {
1593 pr_debug("Using %s for symbols\n",
1594 vmlinux_path[i]);
1595 dso__set_long_name(self,
1596 strdup(vmlinux_path[i]));
1597 goto out_fixup;
1598 }
1599 }
1600 }
1601
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001602 /*
1603 * Say the kernel DSO was created when processing the build-id header table,
1604 * we have a build-id, so check if it is the same as the running kernel,
1605 * using it if it is.
1606 */
1607 if (self->has_build_id) {
1608 u8 kallsyms_build_id[BUILD_ID_SIZE];
1609
1610 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
1611 sizeof(kallsyms_build_id)) == 0)
1612
1613 is_kallsyms = dso__build_id_equal(self, kallsyms_build_id);
1614 if (is_kallsyms)
1615 goto do_kallsyms;
1616 goto do_vmlinux;
1617 }
1618
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001619 is_kallsyms = self->long_name[0] == '[';
1620 if (is_kallsyms)
1621 goto do_kallsyms;
1622
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001623do_vmlinux:
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001624 err = dso__load_vmlinux(self, map, session, self->long_name, filter);
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -02001625 if (err <= 0) {
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001626 pr_info("The file %s cannot be used, "
1627 "trying to use /proc/kallsyms...", self->long_name);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001628do_kallsyms:
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001629 err = dso__load_kallsyms(self, map, session, filter);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001630 if (err > 0 && !is_kallsyms)
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -02001631 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1632 }
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001633
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001634 if (err > 0) {
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001635out_fixup:
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001636 map__fixup_start(map);
1637 map__fixup_end(map);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001638 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001639
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001640 return err;
1641}
1642
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001643LIST_HEAD(dsos__user);
1644LIST_HEAD(dsos__kernel);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001645struct dso *vdso;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001646
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001647static void dsos__add(struct list_head *head, struct dso *dso)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001648{
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001649 list_add_tail(&dso->node, head);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001650}
1651
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001652static struct dso *dsos__find(struct list_head *head, const char *name)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001653{
1654 struct dso *pos;
1655
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001656 list_for_each_entry(pos, head, node)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001657 if (strcmp(pos->name, name) == 0)
1658 return pos;
1659 return NULL;
1660}
1661
Arnaldo Carvalho de Meloa89e5ab2010-01-07 19:59:39 -02001662struct dso *__dsos__findnew(struct list_head *head, const char *name)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001663{
Arnaldo Carvalho de Meloa89e5ab2010-01-07 19:59:39 -02001664 struct dso *dso = dsos__find(head, name);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001665
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -02001666 if (!dso) {
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001667 dso = dso__new(name);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001668 if (dso != NULL) {
Arnaldo Carvalho de Meloa89e5ab2010-01-07 19:59:39 -02001669 dsos__add(head, dso);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001670 dso__set_basename(dso);
1671 }
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001672 }
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001673
1674 return dso;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001675}
1676
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001677static void __dsos__fprintf(struct list_head *head, FILE *fp)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001678{
1679 struct dso *pos;
1680
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001681 list_for_each_entry(pos, head, node) {
1682 int i;
1683 for (i = 0; i < MAP__NR_TYPES; ++i)
1684 dso__fprintf(pos, i, fp);
1685 }
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001686}
1687
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001688void dsos__fprintf(FILE *fp)
1689{
1690 __dsos__fprintf(&dsos__kernel, fp);
1691 __dsos__fprintf(&dsos__user, fp);
1692}
1693
1694static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp)
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001695{
1696 struct dso *pos;
1697 size_t ret = 0;
1698
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001699 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001700 ret += dso__fprintf_buildid(pos, fp);
Arnaldo Carvalho de Melo1124ba72009-11-16 21:45:25 -02001701 ret += fprintf(fp, " %s\n", pos->long_name);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001702 }
1703 return ret;
1704}
1705
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001706size_t dsos__fprintf_buildid(FILE *fp)
1707{
1708 return (__dsos__fprintf_buildid(&dsos__kernel, fp) +
1709 __dsos__fprintf_buildid(&dsos__user, fp));
1710}
1711
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02001712static struct dso *dsos__create_kernel(const char *vmlinux)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001713{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001714 struct dso *kernel = dso__new(vmlinux ?: "[kernel.kallsyms]");
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001715
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001716 if (kernel == NULL)
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001717 return NULL;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001718
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -02001719 kernel->short_name = "[kernel]";
1720 kernel->kernel = 1;
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001721
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001722 vdso = dso__new("[vdso]");
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001723 if (vdso == NULL)
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001724 goto out_delete_kernel_dso;
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001725 dso__set_loaded(vdso, MAP__FUNCTION);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001726
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001727 if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1728 sizeof(kernel->build_id)) == 0)
1729 kernel->has_build_id = true;
1730
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001731 dsos__add(&dsos__kernel, kernel);
1732 dsos__add(&dsos__user, vdso);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001733
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001734 return kernel;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001735
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001736out_delete_kernel_dso:
1737 dso__delete(kernel);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001738 return NULL;
1739}
1740
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001741int __map_groups__create_kernel_maps(struct map_groups *self,
1742 struct map *vmlinux_maps[MAP__NR_TYPES],
1743 struct dso *kernel)
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001744{
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02001745 enum map_type type;
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001746
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02001747 for (type = 0; type < MAP__NR_TYPES; ++type) {
1748 vmlinux_maps[type] = map__new2(0, kernel, type);
1749 if (vmlinux_maps[type] == NULL)
1750 return -1;
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001751
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02001752 vmlinux_maps[type]->map_ip =
1753 vmlinux_maps[type]->unmap_ip = identity__map_ip;
1754 map_groups__insert(self, vmlinux_maps[type]);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001755 }
1756
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001757 return 0;
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001758}
1759
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001760static int map_groups__create_kernel_maps(struct map_groups *self,
1761 struct map *vmlinux_maps[MAP__NR_TYPES],
1762 const char *vmlinux)
1763{
1764 struct dso *kernel = dsos__create_kernel(vmlinux);
1765
1766 if (kernel == NULL)
1767 return -1;
1768
1769 return __map_groups__create_kernel_maps(self, vmlinux_maps, kernel);
1770}
1771
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001772static void vmlinux_path__exit(void)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001773{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001774 while (--vmlinux_path__nr_entries >= 0) {
1775 free(vmlinux_path[vmlinux_path__nr_entries]);
1776 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1777 }
1778
1779 free(vmlinux_path);
1780 vmlinux_path = NULL;
1781}
1782
1783static int vmlinux_path__init(void)
1784{
1785 struct utsname uts;
1786 char bf[PATH_MAX];
1787
1788 if (uname(&uts) < 0)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001789 return -1;
1790
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001791 vmlinux_path = malloc(sizeof(char *) * 5);
1792 if (vmlinux_path == NULL)
1793 return -1;
1794
1795 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1796 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1797 goto out_fail;
1798 ++vmlinux_path__nr_entries;
1799 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1800 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1801 goto out_fail;
1802 ++vmlinux_path__nr_entries;
1803 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1804 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1805 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1806 goto out_fail;
1807 ++vmlinux_path__nr_entries;
1808 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1809 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1810 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1811 goto out_fail;
1812 ++vmlinux_path__nr_entries;
1813 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1814 uts.release);
1815 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1816 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1817 goto out_fail;
1818 ++vmlinux_path__nr_entries;
1819
1820 return 0;
1821
1822out_fail:
1823 vmlinux_path__exit();
1824 return -1;
1825}
1826
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001827static int setup_list(struct strlist **list, const char *list_str,
1828 const char *list_name)
1829{
1830 if (list_str == NULL)
1831 return 0;
1832
1833 *list = strlist__new(true, list_str);
1834 if (!*list) {
1835 pr_err("problems parsing %s list\n", list_name);
1836 return -1;
1837 }
1838 return 0;
1839}
1840
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001841int symbol__init(void)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001842{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001843 elf_version(EV_CURRENT);
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001844 if (symbol_conf.sort_by_name)
1845 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1846 sizeof(struct symbol));
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001847
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001848 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001849 return -1;
1850
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02001851 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1852 pr_err("'.' is the only non valid --field-separator argument\n");
1853 return -1;
1854 }
1855
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001856 if (setup_list(&symbol_conf.dso_list,
1857 symbol_conf.dso_list_str, "dso") < 0)
1858 return -1;
1859
1860 if (setup_list(&symbol_conf.comm_list,
1861 symbol_conf.comm_list_str, "comm") < 0)
1862 goto out_free_dso_list;
1863
1864 if (setup_list(&symbol_conf.sym_list,
1865 symbol_conf.sym_list_str, "symbol") < 0)
1866 goto out_free_comm_list;
1867
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001868 return 0;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001869
1870out_free_dso_list:
1871 strlist__delete(symbol_conf.dso_list);
1872out_free_comm_list:
1873 strlist__delete(symbol_conf.comm_list);
1874 return -1;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001875}
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001876
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001877int perf_session__create_kernel_maps(struct perf_session *self)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001878{
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02001879 if (map_groups__create_kernel_maps(&self->kmaps, self->vmlinux_maps,
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001880 symbol_conf.vmlinux_name) < 0)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001881 return -1;
1882
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001883 if (symbol_conf.use_modules &&
1884 perf_session__create_module_maps(self) < 0)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001885 pr_debug("Failed to load list of modules for session %s, "
1886 "continuing...\n", self->filename);
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -02001887 /*
1888 * Now that we have all the maps created, just set the ->end of them:
1889 */
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001890 map_groups__fixup_end(&self->kmaps);
Arnaldo Carvalho de Melo6671cb12009-11-20 20:51:24 -02001891 return 0;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001892}