blob: ab92763edb03f757e2941f95040133f96f84da02 [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,
25 DSO__ORIG_FEDORA,
26 DSO__ORIG_UBUNTU,
27 DSO__ORIG_BUILDID,
28 DSO__ORIG_DSO,
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030029 DSO__ORIG_KMODULE,
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -030030 DSO__ORIG_NOT_FOUND,
31};
32
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -020033static void dsos__add(struct list_head *head, struct dso *dso);
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020034static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020035static int dso__load_kernel_sym(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -020036 struct perf_session *session, symbol_filter_t filter);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -020037static int vmlinux_path__nr_entries;
38static char **vmlinux_path;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030039
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -020040struct symbol_conf symbol_conf = {
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -020041 .exclude_other = true,
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020042 .use_modules = true,
43 .try_vmlinux_path = true,
44};
45
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020046bool dso__loaded(const struct dso *self, enum map_type type)
47{
48 return self->loaded & (1 << type);
49}
50
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -020051bool dso__sorted_by_name(const struct dso *self, enum map_type type)
52{
53 return self->sorted_by_name & (1 << type);
54}
55
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020056static void dso__set_loaded(struct dso *self, enum map_type type)
57{
58 self->loaded |= (1 << type);
59}
60
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -020061static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
62{
63 self->sorted_by_name |= (1 << type);
64}
65
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020066static bool symbol_type__is_a(char symbol_type, enum map_type map_type)
67{
68 switch (map_type) {
69 case MAP__FUNCTION:
70 return symbol_type == 'T' || symbol_type == 'W';
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -020071 case MAP__VARIABLE:
72 return symbol_type == 'D' || symbol_type == 'd';
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020073 default:
74 return false;
75 }
76}
77
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -020078static void symbols__fixup_end(struct rb_root *self)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030079{
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -020080 struct rb_node *nd, *prevnd = rb_first(self);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030081 struct symbol *curr, *prev;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030082
83 if (prevnd == NULL)
84 return;
85
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030086 curr = rb_entry(prevnd, struct symbol, rb_node);
87
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030088 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030089 prev = curr;
90 curr = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030091
92 if (prev->end == prev->start)
93 prev->end = curr->start - 1;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030094 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030095
96 /* Last entry */
97 if (curr->end == curr->start)
98 curr->end = roundup(curr->start, 4096);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030099}
100
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200101static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300102{
103 struct map *prev, *curr;
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200104 struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300105
106 if (prevnd == NULL)
107 return;
108
109 curr = rb_entry(prevnd, struct map, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300110
111 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
112 prev = curr;
113 curr = rb_entry(nd, struct map, rb_node);
114 prev->end = curr->start - 1;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300115 }
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -0200116
117 /*
118 * We still haven't the actual symbols, so guess the
119 * last map final address.
120 */
121 curr->end = ~0UL;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300122}
123
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200124static void map_groups__fixup_end(struct map_groups *self)
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -0200125{
126 int i;
127 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200128 __map_groups__fixup_end(self, i);
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -0200129}
130
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200131static struct symbol *symbol__new(u64 start, u64 len, const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300132{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300133 size_t namelen = strlen(name) + 1;
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200134 struct symbol *self = zalloc(symbol_conf.priv_size +
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200135 sizeof(*self) + namelen);
136 if (self == NULL)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200137 return NULL;
138
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200139 if (symbol_conf.priv_size)
140 self = ((void *)self) + symbol_conf.priv_size;
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200141
Ingo Molnar0b73da32009-06-06 15:48:52 +0200142 self->start = start;
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200143 self->end = len ? start + len - 1 : start;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200144
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200145 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200146
Ingo Molnar0b73da32009-06-06 15:48:52 +0200147 memcpy(self->name, name, namelen);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300148
149 return self;
150}
151
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200152static void symbol__delete(struct symbol *self)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300153{
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200154 free(((void *)self) - symbol_conf.priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300155}
156
157static size_t symbol__fprintf(struct symbol *self, FILE *fp)
158{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300159 return fprintf(fp, " %llx-%llx %s\n",
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300160 self->start, self->end, self->name);
161}
162
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200163static void dso__set_long_name(struct dso *self, char *name)
164{
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -0200165 if (name == NULL)
166 return;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200167 self->long_name = name;
168 self->long_name_len = strlen(name);
169}
170
171static void dso__set_basename(struct dso *self)
172{
173 self->short_name = basename(self->long_name);
174}
175
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200176struct dso *dso__new(const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300177{
178 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
179
180 if (self != NULL) {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200181 int i;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300182 strcpy(self->name, name);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200183 dso__set_long_name(self, self->name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300184 self->short_name = self->name;
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200185 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200186 self->symbols[i] = self->symbol_names[i] = RB_ROOT;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300187 self->slen_calculated = 0;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300188 self->origin = DSO__ORIG_NOT_FOUND;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200189 self->loaded = 0;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200190 self->sorted_by_name = 0;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200191 self->has_build_id = 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300192 }
193
194 return self;
195}
196
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200197static void symbols__delete(struct rb_root *self)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300198{
199 struct symbol *pos;
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200200 struct rb_node *next = rb_first(self);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300201
202 while (next) {
203 pos = rb_entry(next, struct symbol, rb_node);
204 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200205 rb_erase(&pos->rb_node, self);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200206 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300207 }
208}
209
210void dso__delete(struct dso *self)
211{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200212 int i;
213 for (i = 0; i < MAP__NR_TYPES; ++i)
214 symbols__delete(&self->symbols[i]);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300215 if (self->long_name != self->name)
216 free(self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300217 free(self);
218}
219
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200220void dso__set_build_id(struct dso *self, void *build_id)
221{
222 memcpy(self->build_id, build_id, sizeof(self->build_id));
223 self->has_build_id = 1;
224}
225
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200226static void symbols__insert(struct rb_root *self, struct symbol *sym)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300227{
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200228 struct rb_node **p = &self->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300229 struct rb_node *parent = NULL;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000230 const u64 ip = sym->start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300231 struct symbol *s;
232
233 while (*p != NULL) {
234 parent = *p;
235 s = rb_entry(parent, struct symbol, rb_node);
236 if (ip < s->start)
237 p = &(*p)->rb_left;
238 else
239 p = &(*p)->rb_right;
240 }
241 rb_link_node(&sym->rb_node, parent, p);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200242 rb_insert_color(&sym->rb_node, self);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300243}
244
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200245static struct symbol *symbols__find(struct rb_root *self, u64 ip)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300246{
247 struct rb_node *n;
248
249 if (self == NULL)
250 return NULL;
251
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200252 n = self->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300253
254 while (n) {
255 struct symbol *s = rb_entry(n, struct symbol, rb_node);
256
257 if (ip < s->start)
258 n = n->rb_left;
259 else if (ip > s->end)
260 n = n->rb_right;
261 else
262 return s;
263 }
264
265 return NULL;
266}
267
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200268struct symbol_name_rb_node {
269 struct rb_node rb_node;
270 struct symbol sym;
271};
272
273static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
274{
275 struct rb_node **p = &self->rb_node;
276 struct rb_node *parent = NULL;
277 struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
278
279 while (*p != NULL) {
280 parent = *p;
281 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
282 if (strcmp(sym->name, s->sym.name) < 0)
283 p = &(*p)->rb_left;
284 else
285 p = &(*p)->rb_right;
286 }
287 rb_link_node(&symn->rb_node, parent, p);
288 rb_insert_color(&symn->rb_node, self);
289}
290
291static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
292{
293 struct rb_node *nd;
294
295 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
296 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
297 symbols__insert_by_name(self, pos);
298 }
299}
300
301static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
302{
303 struct rb_node *n;
304
305 if (self == NULL)
306 return NULL;
307
308 n = self->rb_node;
309
310 while (n) {
311 struct symbol_name_rb_node *s;
312 int cmp;
313
314 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
315 cmp = strcmp(name, s->sym.name);
316
317 if (cmp < 0)
318 n = n->rb_left;
319 else if (cmp > 0)
320 n = n->rb_right;
321 else
322 return &s->sym;
323 }
324
325 return NULL;
326}
327
328struct symbol *dso__find_symbol(struct dso *self,
329 enum map_type type, u64 addr)
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200330{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200331 return symbols__find(&self->symbols[type], addr);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200332}
333
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200334struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
335 const char *name)
336{
337 return symbols__find_by_name(&self->symbol_names[type], name);
338}
339
340void dso__sort_by_name(struct dso *self, enum map_type type)
341{
342 dso__set_sorted_by_name(self, type);
343 return symbols__sort_by_name(&self->symbol_names[type],
344 &self->symbols[type]);
345}
346
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200347int build_id__sprintf(u8 *self, int len, char *bf)
348{
349 char *bid = bf;
350 u8 *raw = self;
351 int i;
352
353 for (i = 0; i < len; ++i) {
354 sprintf(bid, "%02x", *raw);
355 ++raw;
356 bid += 2;
357 }
358
359 return raw - self;
360}
361
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200362size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300363{
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200364 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200365
366 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200367 return fprintf(fp, "%s", sbuild_id);
368}
369
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200370size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200371{
372 struct rb_node *nd;
373 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
374
375 ret += dso__fprintf_buildid(self, fp);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200376 ret += fprintf(fp, ")\n");
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200377 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
378 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
379 ret += symbol__fprintf(pos, fp);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300380 }
381
382 return ret;
383}
384
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300385/*
386 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
387 * so that we can in the next step set the symbol ->end address and then
388 * call kernel_maps__split_kallsyms.
389 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200390static int dso__load_all_kallsyms(struct dso *self, struct map *map)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300391{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300392 char *line = NULL;
393 size_t n;
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200394 struct rb_root *root = &self->symbols[map->type];
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300395 FILE *file = fopen("/proc/kallsyms", "r");
396
397 if (file == NULL)
398 goto out_failure;
399
400 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000401 u64 start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300402 struct symbol *sym;
403 int line_len, len;
404 char symbol_type;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300405 char *symbol_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300406
407 line_len = getline(&line, &n, file);
408 if (line_len < 0)
409 break;
410
411 if (!line)
412 goto out_failure;
413
414 line[--line_len] = '\0'; /* \n */
415
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300416 len = hex2u64(line, &start);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300417
418 len++;
419 if (len + 2 >= line_len)
420 continue;
421
422 symbol_type = toupper(line[len]);
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -0200423 if (!symbol_type__is_a(symbol_type, map->type))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300424 continue;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300425
426 symbol_name = line + len + 2;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300427 /*
428 * Will fix up the end later, when we have all symbols sorted.
429 */
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200430 sym = symbol__new(start, 0, symbol_name);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300431
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300432 if (sym == NULL)
433 goto out_delete_line;
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200434 /*
435 * We will pass the symbols to the filter later, in
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200436 * map__split_kallsyms, when we have split the maps per module
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200437 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200438 symbols__insert(root, sym);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300439 }
440
441 free(line);
442 fclose(file);
443
444 return 0;
445
446out_delete_line:
447 free(line);
448out_failure:
449 return -1;
450}
451
452/*
453 * Split the symbols into maps, making sure there are no overlaps, i.e. the
454 * kernel range is broken in several maps, named [kernel].N, as we don't have
455 * the original ELF section names vmlinux have.
456 */
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200457static int dso__split_kallsyms(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200458 struct perf_session *session, symbol_filter_t filter)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300459{
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200460 struct map *curr_map = map;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300461 struct symbol *pos;
462 int count = 0;
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200463 struct rb_root *root = &self->symbols[map->type];
464 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300465 int kernel_range = 0;
466
467 while (next) {
468 char *module;
469
470 pos = rb_entry(next, struct symbol, rb_node);
471 next = rb_next(&pos->rb_node);
472
473 module = strchr(pos->name, '\t');
474 if (module) {
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200475 if (!symbol_conf.use_modules)
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200476 goto discard_symbol;
477
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300478 *module++ = '\0';
479
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200480 if (strcmp(self->name, module)) {
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200481 curr_map = map_groups__find_by_name(&session->kmaps, map->type, module);
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200482 if (curr_map == NULL) {
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200483 pr_debug("/proc/{kallsyms,modules} "
484 "inconsistency!\n");
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300485 return -1;
486 }
487 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300488 /*
489 * So that we look just like we get from .ko files,
490 * i.e. not prelinked, relative to map->start.
491 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200492 pos->start = curr_map->map_ip(curr_map, pos->start);
493 pos->end = curr_map->map_ip(curr_map, pos->end);
494 } else if (curr_map != map) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300495 char dso_name[PATH_MAX];
496 struct dso *dso;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300497
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300498 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
499 kernel_range++);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300500
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200501 dso = dso__new(dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300502 if (dso == NULL)
503 return -1;
504
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200505 curr_map = map__new2(pos->start, dso, map->type);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300506 if (map == NULL) {
507 dso__delete(dso);
508 return -1;
509 }
510
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200511 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200512 map_groups__insert(&session->kmaps, curr_map);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300513 ++kernel_range;
514 }
515
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200516 if (filter && filter(curr_map, pos)) {
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200517discard_symbol: rb_erase(&pos->rb_node, root);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200518 symbol__delete(pos);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300519 } else {
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200520 if (curr_map != map) {
521 rb_erase(&pos->rb_node, root);
522 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300523 }
Mike Galbraith9974f492009-07-02 08:05:58 +0200524 count++;
525 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300526 }
527
Mike Galbraith9974f492009-07-02 08:05:58 +0200528 return count;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300529}
530
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300531
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200532static int dso__load_kallsyms(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200533 struct perf_session *session, symbol_filter_t filter)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300534{
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200535 if (dso__load_all_kallsyms(self, map) < 0)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300536 return -1;
537
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200538 symbols__fixup_end(&self->symbols[map->type]);
539 self->origin = DSO__ORIG_KERNEL;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300540
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200541 return dso__split_kallsyms(self, map, session, filter);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300542}
543
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300544static int dso__load_perf_map(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200545 symbol_filter_t filter)
Pekka Enberg80d496b2009-06-08 21:12:48 +0300546{
547 char *line = NULL;
548 size_t n;
549 FILE *file;
550 int nr_syms = 0;
551
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300552 file = fopen(self->long_name, "r");
Pekka Enberg80d496b2009-06-08 21:12:48 +0300553 if (file == NULL)
554 goto out_failure;
555
556 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000557 u64 start, size;
Pekka Enberg80d496b2009-06-08 21:12:48 +0300558 struct symbol *sym;
559 int line_len, len;
560
561 line_len = getline(&line, &n, file);
562 if (line_len < 0)
563 break;
564
565 if (!line)
566 goto out_failure;
567
568 line[--line_len] = '\0'; /* \n */
569
570 len = hex2u64(line, &start);
571
572 len++;
573 if (len + 2 >= line_len)
574 continue;
575
576 len += hex2u64(line + len, &size);
577
578 len++;
579 if (len + 2 >= line_len)
580 continue;
581
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200582 sym = symbol__new(start, size, line + len);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300583
584 if (sym == NULL)
585 goto out_delete_line;
586
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300587 if (filter && filter(map, sym))
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200588 symbol__delete(sym);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300589 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200590 symbols__insert(&self->symbols[map->type], sym);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300591 nr_syms++;
592 }
593 }
594
595 free(line);
596 fclose(file);
597
598 return nr_syms;
599
600out_delete_line:
601 free(line);
602out_failure:
603 return -1;
604}
605
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300606/**
607 * elf_symtab__for_each_symbol - iterate thru all the symbols
608 *
609 * @self: struct elf_symtab instance to iterate
Ingo Molnar83a09442009-08-15 12:26:57 +0200610 * @idx: uint32_t idx
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300611 * @sym: GElf_Sym iterator
612 */
Ingo Molnar83a09442009-08-15 12:26:57 +0200613#define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
614 for (idx = 0, gelf_getsym(syms, idx, &sym);\
615 idx < nr_syms; \
616 idx++, gelf_getsym(syms, idx, &sym))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300617
618static inline uint8_t elf_sym__type(const GElf_Sym *sym)
619{
620 return GELF_ST_TYPE(sym->st_info);
621}
622
623static inline int elf_sym__is_function(const GElf_Sym *sym)
624{
625 return elf_sym__type(sym) == STT_FUNC &&
626 sym->st_name != 0 &&
Arnaldo Carvalho de Melo81833132009-10-05 23:35:03 -0300627 sym->st_shndx != SHN_UNDEF;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300628}
629
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200630static inline bool elf_sym__is_object(const GElf_Sym *sym)
631{
632 return elf_sym__type(sym) == STT_OBJECT &&
633 sym->st_name != 0 &&
634 sym->st_shndx != SHN_UNDEF;
635}
636
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200637static inline int elf_sym__is_label(const GElf_Sym *sym)
638{
639 return elf_sym__type(sym) == STT_NOTYPE &&
640 sym->st_name != 0 &&
641 sym->st_shndx != SHN_UNDEF &&
642 sym->st_shndx != SHN_ABS;
643}
644
645static inline const char *elf_sec__name(const GElf_Shdr *shdr,
646 const Elf_Data *secstrs)
647{
648 return secstrs->d_buf + shdr->sh_name;
649}
650
651static inline int elf_sec__is_text(const GElf_Shdr *shdr,
652 const Elf_Data *secstrs)
653{
654 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
655}
656
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200657static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
658 const Elf_Data *secstrs)
659{
660 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
661}
662
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300663static inline const char *elf_sym__name(const GElf_Sym *sym,
664 const Elf_Data *symstrs)
665{
666 return symstrs->d_buf + sym->st_name;
667}
668
669static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
670 GElf_Shdr *shp, const char *name,
Ingo Molnar83a09442009-08-15 12:26:57 +0200671 size_t *idx)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300672{
673 Elf_Scn *sec = NULL;
674 size_t cnt = 1;
675
676 while ((sec = elf_nextscn(elf, sec)) != NULL) {
677 char *str;
678
679 gelf_getshdr(sec, shp);
680 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
681 if (!strcmp(name, str)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200682 if (idx)
683 *idx = cnt;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300684 break;
685 }
686 ++cnt;
687 }
688
689 return sec;
690}
691
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300692#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
693 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
694 idx < nr_entries; \
695 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
696
697#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
698 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
699 idx < nr_entries; \
700 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
701
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300702/*
703 * We need to check if we have a .dynsym, so that we can handle the
704 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
705 * .dynsym or .symtab).
706 * And always look at the original dso, not at debuginfo packages, that
707 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
708 */
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200709static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
710 symbol_filter_t filter)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300711{
712 uint32_t nr_rel_entries, idx;
713 GElf_Sym sym;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000714 u64 plt_offset;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300715 GElf_Shdr shdr_plt;
716 struct symbol *f;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300717 GElf_Shdr shdr_rel_plt, shdr_dynsym;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300718 Elf_Data *reldata, *syms, *symstrs;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300719 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
720 size_t dynsym_idx;
721 GElf_Ehdr ehdr;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300722 char sympltname[1024];
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300723 Elf *elf;
724 int nr = 0, symidx, fd, err = 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300725
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300726 fd = open(self->long_name, O_RDONLY);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300727 if (fd < 0)
728 goto out;
729
Marti Raudsepp84087122009-10-24 19:10:36 +0300730 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300731 if (elf == NULL)
732 goto out_close;
733
734 if (gelf_getehdr(elf, &ehdr) == NULL)
735 goto out_elf_end;
736
737 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
738 ".dynsym", &dynsym_idx);
739 if (scn_dynsym == NULL)
740 goto out_elf_end;
741
742 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300743 ".rela.plt", NULL);
744 if (scn_plt_rel == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300745 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300746 ".rel.plt", NULL);
747 if (scn_plt_rel == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300748 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300749 }
750
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300751 err = -1;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300752
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300753 if (shdr_rel_plt.sh_link != dynsym_idx)
754 goto out_elf_end;
755
756 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
757 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300758
759 /*
Ingo Molnar83a09442009-08-15 12:26:57 +0200760 * Fetch the relocation section to find the idxes to the GOT
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300761 * and the symbols in the .dynsym they refer to.
762 */
763 reldata = elf_getdata(scn_plt_rel, NULL);
764 if (reldata == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300765 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300766
767 syms = elf_getdata(scn_dynsym, NULL);
768 if (syms == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300769 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300770
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300771 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300772 if (scn_symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300773 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300774
775 symstrs = elf_getdata(scn_symstrs, NULL);
776 if (symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300777 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300778
779 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
780 plt_offset = shdr_plt.sh_offset;
781
782 if (shdr_rel_plt.sh_type == SHT_RELA) {
783 GElf_Rela pos_mem, *pos;
784
785 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
786 nr_rel_entries) {
787 symidx = GELF_R_SYM(pos->r_info);
788 plt_offset += shdr_plt.sh_entsize;
789 gelf_getsym(syms, symidx, &sym);
790 snprintf(sympltname, sizeof(sympltname),
791 "%s@plt", elf_sym__name(&sym, symstrs));
792
793 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200794 sympltname);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300795 if (!f)
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 Melo82164162009-11-16 13:48:11 -0200798 if (filter && filter(map, f))
799 symbol__delete(f);
800 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200801 symbols__insert(&self->symbols[map->type], f);
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200802 ++nr;
803 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300804 }
805 } else if (shdr_rel_plt.sh_type == SHT_REL) {
806 GElf_Rel pos_mem, *pos;
807 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
808 nr_rel_entries) {
809 symidx = GELF_R_SYM(pos->r_info);
810 plt_offset += shdr_plt.sh_entsize;
811 gelf_getsym(syms, symidx, &sym);
812 snprintf(sympltname, sizeof(sympltname),
813 "%s@plt", elf_sym__name(&sym, symstrs));
814
815 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200816 sympltname);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300817 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300818 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300819
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200820 if (filter && filter(map, f))
821 symbol__delete(f);
822 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200823 symbols__insert(&self->symbols[map->type], f);
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200824 ++nr;
825 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300826 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300827 }
828
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300829 err = 0;
830out_elf_end:
831 elf_end(elf);
832out_close:
833 close(fd);
834
835 if (err == 0)
836 return nr;
837out:
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200838 pr_warning("%s: problems reading %s PLT info.\n",
839 __func__, self->long_name);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300840 return 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300841}
842
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -0200843static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
844{
845 switch (type) {
846 case MAP__FUNCTION:
847 return elf_sym__is_function(self);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200848 case MAP__VARIABLE:
849 return elf_sym__is_object(self);
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -0200850 default:
851 return false;
852 }
853}
854
855static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
856{
857 switch (type) {
858 case MAP__FUNCTION:
859 return elf_sec__is_text(self, secstrs);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200860 case MAP__VARIABLE:
861 return elf_sec__is_data(self, secstrs);
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -0200862 default:
863 return false;
864 }
865}
866
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200867static int dso__load_sym(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200868 struct perf_session *session, const char *name, int fd,
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200869 symbol_filter_t filter, int kernel, int kmodule)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300870{
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300871 struct map *curr_map = map;
872 struct dso *curr_dso = self;
873 size_t dso_name_len = strlen(self->short_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200874 Elf_Data *symstrs, *secstrs;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300875 uint32_t nr_syms;
876 int err = -1;
Ingo Molnar83a09442009-08-15 12:26:57 +0200877 uint32_t idx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300878 GElf_Ehdr ehdr;
879 GElf_Shdr shdr;
880 Elf_Data *syms;
881 GElf_Sym sym;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300882 Elf_Scn *sec, *sec_strndx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300883 Elf *elf;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300884 int nr = 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300885
Marti Raudsepp84087122009-10-24 19:10:36 +0300886 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300887 if (elf == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200888 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300889 goto out_close;
890 }
891
892 if (gelf_getehdr(elf, &ehdr) == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200893 pr_err("%s: cannot get elf header.\n", __func__);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300894 goto out_elf_end;
895 }
896
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300897 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
898 if (sec == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300899 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
900 if (sec == NULL)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300901 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300902 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300903
904 syms = elf_getdata(sec, NULL);
905 if (syms == NULL)
906 goto out_elf_end;
907
908 sec = elf_getscn(elf, shdr.sh_link);
909 if (sec == NULL)
910 goto out_elf_end;
911
912 symstrs = elf_getdata(sec, NULL);
913 if (symstrs == NULL)
914 goto out_elf_end;
915
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200916 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
917 if (sec_strndx == NULL)
918 goto out_elf_end;
919
920 secstrs = elf_getdata(sec_strndx, NULL);
Stoyan Gaydarov9b30a262009-07-30 05:25:29 -0500921 if (secstrs == NULL)
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200922 goto out_elf_end;
923
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300924 nr_syms = shdr.sh_size / shdr.sh_entsize;
925
Arjan van de Vene9fbc9d2009-06-06 21:22:33 +0200926 memset(&sym, 0, sizeof(sym));
Mike Galbraithd20ff6b2009-07-20 14:01:38 +0200927 if (!kernel) {
928 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -0300929 elf_section_by_name(elf, &ehdr, &shdr,
930 ".gnu.prelink_undo",
931 NULL) != NULL);
Mike Galbraithd20ff6b2009-07-20 14:01:38 +0200932 } else self->adjust_symbols = 0;
933
Ingo Molnar83a09442009-08-15 12:26:57 +0200934 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300935 struct symbol *f;
Ingo Molnar83a09442009-08-15 12:26:57 +0200936 const char *elf_name;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300937 char *demangled = NULL;
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200938 int is_label = elf_sym__is_label(&sym);
939 const char *section_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300940
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -0200941 if (!is_label && !elf_sym__is_a(&sym, map->type))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300942 continue;
943
944 sec = elf_getscn(elf, sym.st_shndx);
945 if (!sec)
946 goto out_elf_end;
947
948 gelf_getshdr(sec, &shdr);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200949
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -0200950 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200951 continue;
952
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300953 elf_name = elf_sym__name(&sym, symstrs);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200954 section_name = elf_sec__name(&shdr, secstrs);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200955
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300956 if (kernel || kmodule) {
957 char dso_name[PATH_MAX];
958
959 if (strcmp(section_name,
960 curr_dso->short_name + dso_name_len) == 0)
961 goto new_symbol;
962
963 if (strcmp(section_name, ".text") == 0) {
964 curr_map = map;
965 curr_dso = self;
966 goto new_symbol;
967 }
968
969 snprintf(dso_name, sizeof(dso_name),
970 "%s%s", self->short_name, section_name);
971
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200972 curr_map = map_groups__find_by_name(&session->kmaps, map->type, dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300973 if (curr_map == NULL) {
974 u64 start = sym.st_value;
975
976 if (kmodule)
977 start += map->start + shdr.sh_offset;
978
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200979 curr_dso = dso__new(dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300980 if (curr_dso == NULL)
981 goto out_elf_end;
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -0200982 curr_map = map__new2(start, curr_dso,
983 MAP__FUNCTION);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300984 if (curr_map == NULL) {
985 dso__delete(curr_dso);
986 goto out_elf_end;
987 }
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200988 curr_map->map_ip = identity__map_ip;
989 curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300990 curr_dso->origin = DSO__ORIG_KERNEL;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -0200991 map_groups__insert(&session->kmaps, curr_map);
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200992 dsos__add(&dsos__kernel, curr_dso);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300993 } else
994 curr_dso = curr_map->dso;
995
996 goto new_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300997 }
998
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300999 if (curr_dso->adjust_symbols) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001000 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
1001 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
1002 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001003 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001004 }
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -03001005 /*
1006 * We need to figure out if the object was created from C++ sources
1007 * DWARF DW_compile_unit has this, but we don't always have access
1008 * to it...
1009 */
Ingo Molnar83a09442009-08-15 12:26:57 +02001010 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -03001011 if (demangled != NULL)
Ingo Molnar83a09442009-08-15 12:26:57 +02001012 elf_name = demangled;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001013new_symbol:
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001014 f = symbol__new(sym.st_value, sym.st_size, elf_name);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -03001015 free(demangled);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001016 if (!f)
1017 goto out_elf_end;
1018
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001019 if (filter && filter(curr_map, f))
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001020 symbol__delete(f);
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -03001021 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001022 symbols__insert(&curr_dso->symbols[curr_map->type], f);
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -03001023 nr++;
1024 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001025 }
1026
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001027 /*
1028 * For misannotated, zeroed, ASM function sizes.
1029 */
1030 if (nr > 0)
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001031 symbols__fixup_end(&self->symbols[map->type]);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001032 err = nr;
1033out_elf_end:
1034 elf_end(elf);
1035out_close:
1036 return err;
1037}
1038
Arnaldo Carvalho de Melo78075ca2009-11-20 20:51:26 -02001039static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1040{
1041 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1042}
1043
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001044static bool __dsos__read_build_ids(struct list_head *head)
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001045{
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -02001046 bool have_build_id = false;
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001047 struct dso *pos;
1048
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001049 list_for_each_entry(pos, head, node)
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -02001050 if (filename__read_build_id(pos->long_name, pos->build_id,
1051 sizeof(pos->build_id)) > 0) {
1052 have_build_id = true;
1053 pos->has_build_id = true;
1054 }
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001055
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -02001056 return have_build_id;
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001057}
1058
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001059bool dsos__read_build_ids(void)
1060{
Arnaldo Carvalho de Melo8b4825b2009-12-09 20:09:37 -02001061 bool kbuildids = __dsos__read_build_ids(&dsos__kernel),
1062 ubuildids = __dsos__read_build_ids(&dsos__user);
1063 return kbuildids || ubuildids;
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001064}
1065
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001066/*
1067 * Align offset to 4 bytes as needed for note name and descriptor data.
1068 */
1069#define NOTE_ALIGN(n) (((n) + 3) & -4U)
1070
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -02001071int filename__read_build_id(const char *filename, void *bf, size_t size)
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001072{
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -02001073 int fd, err = -1;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001074 GElf_Ehdr ehdr;
1075 GElf_Shdr shdr;
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001076 Elf_Data *data;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001077 Elf_Scn *sec;
Pekka Enberge57cfcd2009-11-22 12:29:44 +02001078 Elf_Kind ek;
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001079 void *ptr;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001080 Elf *elf;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001081
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -02001082 if (size < BUILD_ID_SIZE)
1083 goto out;
1084
1085 fd = open(filename, O_RDONLY);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001086 if (fd < 0)
1087 goto out;
1088
Marti Raudsepp84087122009-10-24 19:10:36 +03001089 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001090 if (elf == NULL) {
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001091 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001092 goto out_close;
1093 }
1094
Pekka Enberge57cfcd2009-11-22 12:29:44 +02001095 ek = elf_kind(elf);
1096 if (ek != ELF_K_ELF)
1097 goto out_elf_end;
1098
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001099 if (gelf_getehdr(elf, &ehdr) == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001100 pr_err("%s: cannot get elf header.\n", __func__);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001101 goto out_elf_end;
1102 }
1103
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -02001104 sec = elf_section_by_name(elf, &ehdr, &shdr,
1105 ".note.gnu.build-id", NULL);
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001106 if (sec == NULL) {
1107 sec = elf_section_by_name(elf, &ehdr, &shdr,
1108 ".notes", NULL);
1109 if (sec == NULL)
1110 goto out_elf_end;
1111 }
1112
1113 data = elf_getdata(sec, NULL);
1114 if (data == NULL)
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001115 goto out_elf_end;
1116
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001117 ptr = data->d_buf;
1118 while (ptr < (data->d_buf + data->d_size)) {
1119 GElf_Nhdr *nhdr = ptr;
1120 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1121 descsz = NOTE_ALIGN(nhdr->n_descsz);
1122 const char *name;
1123
1124 ptr += sizeof(*nhdr);
1125 name = ptr;
1126 ptr += namesz;
1127 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1128 nhdr->n_namesz == sizeof("GNU")) {
1129 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1130 memcpy(bf, ptr, BUILD_ID_SIZE);
1131 err = BUILD_ID_SIZE;
1132 break;
1133 }
1134 }
1135 ptr += descsz;
1136 }
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -02001137out_elf_end:
1138 elf_end(elf);
1139out_close:
1140 close(fd);
1141out:
1142 return err;
1143}
1144
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001145int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1146{
1147 int fd, err = -1;
1148
1149 if (size < BUILD_ID_SIZE)
1150 goto out;
1151
1152 fd = open(filename, O_RDONLY);
1153 if (fd < 0)
1154 goto out;
1155
1156 while (1) {
1157 char bf[BUFSIZ];
1158 GElf_Nhdr nhdr;
1159 int namesz, descsz;
1160
1161 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1162 break;
1163
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001164 namesz = NOTE_ALIGN(nhdr.n_namesz);
1165 descsz = NOTE_ALIGN(nhdr.n_descsz);
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001166 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1167 nhdr.n_namesz == sizeof("GNU")) {
1168 if (read(fd, bf, namesz) != namesz)
1169 break;
1170 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1171 if (read(fd, build_id,
1172 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1173 err = 0;
1174 break;
1175 }
1176 } else if (read(fd, bf, descsz) != descsz)
1177 break;
1178 } else {
1179 int n = namesz + descsz;
1180 if (read(fd, bf, n) != n)
1181 break;
1182 }
1183 }
1184 close(fd);
1185out:
1186 return err;
1187}
1188
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001189char dso__symtab_origin(const struct dso *self)
1190{
1191 static const char origin[] = {
1192 [DSO__ORIG_KERNEL] = 'k',
1193 [DSO__ORIG_JAVA_JIT] = 'j',
1194 [DSO__ORIG_FEDORA] = 'f',
1195 [DSO__ORIG_UBUNTU] = 'u',
1196 [DSO__ORIG_BUILDID] = 'b',
1197 [DSO__ORIG_DSO] = 'd',
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001198 [DSO__ORIG_KMODULE] = 'K',
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001199 };
1200
1201 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1202 return '!';
1203 return origin[self->origin];
1204}
1205
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001206int dso__load(struct dso *self, struct map *map, struct perf_session *session,
1207 symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001208{
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001209 int size = PATH_MAX;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001210 char *name;
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001211 u8 build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001212 int ret = -1;
1213 int fd;
1214
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001215 dso__set_loaded(self, map->type);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001216
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001217 if (self->kernel)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001218 return dso__load_kernel_sym(self, map, session, filter);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001219
1220 name = malloc(size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001221 if (!name)
1222 return -1;
1223
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -03001224 self->adjust_symbols = 0;
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001225
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001226 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001227 ret = dso__load_perf_map(self, map, filter);
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001228 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1229 DSO__ORIG_NOT_FOUND;
1230 return ret;
1231 }
1232
1233 self->origin = DSO__ORIG_FEDORA - 1;
Pekka Enberg80d496b2009-06-08 21:12:48 +03001234
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001235more:
1236 do {
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001237 self->origin++;
1238 switch (self->origin) {
1239 case DSO__ORIG_FEDORA:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001240 snprintf(name, size, "/usr/lib/debug%s.debug",
1241 self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001242 break;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001243 case DSO__ORIG_UBUNTU:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001244 snprintf(name, size, "/usr/lib/debug%s",
1245 self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001246 break;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001247 case DSO__ORIG_BUILDID:
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001248 if (filename__read_build_id(self->long_name, build_id,
1249 sizeof(build_id))) {
1250 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1251
1252 build_id__sprintf(build_id, sizeof(build_id),
1253 build_id_hex);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001254 snprintf(name, size,
1255 "/usr/lib/debug/.build-id/%.2s/%s.debug",
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001256 build_id_hex, build_id_hex + 2);
1257 if (self->has_build_id)
1258 goto compare_build_id;
1259 break;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001260 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001261 self->origin++;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001262 /* Fall thru */
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001263 case DSO__ORIG_DSO:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001264 snprintf(name, size, "%s", self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001265 break;
1266
1267 default:
1268 goto out;
1269 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001270
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001271 if (self->has_build_id) {
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001272 if (filename__read_build_id(name, build_id,
1273 sizeof(build_id)) < 0)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001274 goto more;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001275compare_build_id:
Arnaldo Carvalho de Melo78075ca2009-11-20 20:51:26 -02001276 if (!dso__build_id_equal(self, build_id))
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001277 goto more;
1278 }
1279
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001280 fd = open(name, O_RDONLY);
1281 } while (fd < 0);
1282
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001283 ret = dso__load_sym(self, map, NULL, name, fd, filter, 0, 0);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001284 close(fd);
1285
1286 /*
1287 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1288 */
1289 if (!ret)
1290 goto more;
1291
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001292 if (ret > 0) {
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -02001293 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001294 if (nr_plt > 0)
1295 ret += nr_plt;
1296 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001297out:
1298 free(name);
Arnaldo Carvalho de Melo1340e6b2009-08-11 17:04:36 -03001299 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1300 return 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001301 return ret;
1302}
1303
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -02001304struct map *map_groups__find_by_name(struct map_groups *self,
1305 enum map_type type, const char *name)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001306{
1307 struct rb_node *nd;
1308
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -02001309 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001310 struct map *map = rb_entry(nd, struct map, rb_node);
1311
1312 if (map->dso && strcmp(map->dso->name, name) == 0)
1313 return map;
1314 }
1315
1316 return NULL;
1317}
1318
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001319static int perf_session__set_modules_path_dir(struct perf_session *self, char *dirname)
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001320{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001321 struct dirent *dent;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001322 DIR *dir = opendir(dirname);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001323
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001324 if (!dir) {
Arnaldo Carvalho de Melo87f8ea42009-11-22 13:21:41 -02001325 pr_debug("%s: cannot open %s dir\n", __func__, dirname);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001326 return -1;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001327 }
1328
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001329 while ((dent = readdir(dir)) != NULL) {
1330 char path[PATH_MAX];
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001331
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001332 if (dent->d_type == DT_DIR) {
1333 if (!strcmp(dent->d_name, ".") ||
1334 !strcmp(dent->d_name, ".."))
1335 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001336
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001337 snprintf(path, sizeof(path), "%s/%s",
1338 dirname, dent->d_name);
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001339 if (perf_session__set_modules_path_dir(self, path) < 0)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001340 goto failure;
1341 } else {
1342 char *dot = strrchr(dent->d_name, '.'),
1343 dso_name[PATH_MAX];
1344 struct map *map;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001345 char *long_name;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001346
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001347 if (dot == NULL || strcmp(dot, ".ko"))
1348 continue;
1349 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1350 (int)(dot - dent->d_name), dent->d_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001351
Arnaldo Carvalho de Meloa2a99e82009-10-05 14:26:18 -03001352 strxfrchar(dso_name, '-', '_');
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001353 map = map_groups__find_by_name(&self->kmaps, MAP__FUNCTION, dso_name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001354 if (map == NULL)
1355 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001356
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001357 snprintf(path, sizeof(path), "%s/%s",
1358 dirname, dent->d_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001359
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001360 long_name = strdup(path);
1361 if (long_name == NULL)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001362 goto failure;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001363 dso__set_long_name(map->dso, long_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001364 }
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001365 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001366
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001367 return 0;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001368failure:
1369 closedir(dir);
1370 return -1;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001371}
1372
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001373static int perf_session__set_modules_path(struct perf_session *self)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001374{
1375 struct utsname uts;
1376 char modules_path[PATH_MAX];
1377
1378 if (uname(&uts) < 0)
1379 return -1;
1380
1381 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1382 uts.release);
1383
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001384 return perf_session__set_modules_path_dir(self, modules_path);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001385}
1386
1387/*
1388 * Constructor variant for modules (where we know from /proc/modules where
1389 * they are loaded) and for vmlinux, where only after we load all the
1390 * symbols we'll know where it starts and ends.
1391 */
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001392static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001393{
1394 struct map *self = malloc(sizeof(*self));
1395
1396 if (self != NULL) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001397 /*
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -02001398 * ->end will be filled after we load all the symbols
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001399 */
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001400 map__init(self, type, start, 0, 0, dso);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001401 }
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -02001402
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001403 return self;
1404}
1405
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001406static int perf_session__create_module_maps(struct perf_session *self)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001407{
1408 char *line = NULL;
1409 size_t n;
1410 FILE *file = fopen("/proc/modules", "r");
1411 struct map *map;
1412
1413 if (file == NULL)
1414 return -1;
1415
1416 while (!feof(file)) {
1417 char name[PATH_MAX];
1418 u64 start;
1419 struct dso *dso;
1420 char *sep;
1421 int line_len;
1422
1423 line_len = getline(&line, &n, file);
1424 if (line_len < 0)
1425 break;
1426
1427 if (!line)
1428 goto out_failure;
1429
1430 line[--line_len] = '\0'; /* \n */
1431
1432 sep = strrchr(line, 'x');
1433 if (sep == NULL)
1434 continue;
1435
1436 hex2u64(sep + 1, &start);
1437
1438 sep = strchr(line, ' ');
1439 if (sep == NULL)
1440 continue;
1441
1442 *sep = '\0';
1443
1444 snprintf(name, sizeof(name), "[%s]", line);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001445 dso = dso__new(name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001446
1447 if (dso == NULL)
1448 goto out_delete_line;
1449
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001450 map = map__new2(start, dso, MAP__FUNCTION);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001451 if (map == NULL) {
1452 dso__delete(dso);
1453 goto out_delete_line;
1454 }
1455
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001456 snprintf(name, sizeof(name),
1457 "/sys/module/%s/notes/.note.gnu.build-id", line);
1458 if (sysfs__read_build_id(name, dso->build_id,
1459 sizeof(dso->build_id)) == 0)
1460 dso->has_build_id = true;
1461
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001462 dso->origin = DSO__ORIG_KMODULE;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001463 map_groups__insert(&self->kmaps, map);
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001464 dsos__add(&dsos__kernel, dso);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001465 }
1466
1467 free(line);
1468 fclose(file);
1469
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001470 return perf_session__set_modules_path(self);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001471
1472out_delete_line:
1473 free(line);
1474out_failure:
1475 return -1;
1476}
1477
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -02001478static int dso__load_vmlinux(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001479 struct perf_session *session,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001480 const char *vmlinux, symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001481{
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001482 int err = -1, fd;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001483
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001484 if (self->has_build_id) {
1485 u8 build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001486
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001487 if (filename__read_build_id(vmlinux, build_id,
1488 sizeof(build_id)) < 0) {
1489 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1490 return -1;
1491 }
1492 if (!dso__build_id_equal(self, build_id)) {
1493 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1494 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1495
1496 build_id__sprintf(self->build_id,
1497 sizeof(self->build_id),
1498 expected_build_id);
1499 build_id__sprintf(build_id, sizeof(build_id),
1500 vmlinux_build_id);
1501 pr_debug("build_id in %s is %s while expected is %s, "
1502 "ignoring it\n", vmlinux, vmlinux_build_id,
1503 expected_build_id);
1504 return -1;
1505 }
1506 }
1507
1508 fd = open(vmlinux, O_RDONLY);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001509 if (fd < 0)
1510 return -1;
1511
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001512 dso__set_loaded(self, map->type);
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001513 err = dso__load_sym(self, map, session, self->long_name, fd, filter, 1, 0);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001514 close(fd);
1515
1516 return err;
1517}
1518
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001519static int dso__load_kernel_sym(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001520 struct perf_session *session, symbol_filter_t filter)
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001521{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001522 int err;
1523 bool is_kallsyms;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001524
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001525 if (vmlinux_path != NULL) {
1526 int i;
1527 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1528 vmlinux_path__nr_entries);
1529 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001530 err = dso__load_vmlinux(self, map, session,
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001531 vmlinux_path[i], filter);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001532 if (err > 0) {
1533 pr_debug("Using %s for symbols\n",
1534 vmlinux_path[i]);
1535 dso__set_long_name(self,
1536 strdup(vmlinux_path[i]));
1537 goto out_fixup;
1538 }
1539 }
1540 }
1541
1542 is_kallsyms = self->long_name[0] == '[';
1543 if (is_kallsyms)
1544 goto do_kallsyms;
1545
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001546 err = dso__load_vmlinux(self, map, session, self->long_name, filter);
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -02001547 if (err <= 0) {
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001548 pr_info("The file %s cannot be used, "
1549 "trying to use /proc/kallsyms...", self->long_name);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001550do_kallsyms:
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001551 err = dso__load_kallsyms(self, map, session, filter);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001552 if (err > 0 && !is_kallsyms)
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -02001553 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1554 }
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001555
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001556 if (err > 0) {
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001557out_fixup:
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001558 map__fixup_start(map);
1559 map__fixup_end(map);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001560 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001561
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001562 return err;
1563}
1564
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001565LIST_HEAD(dsos__user);
1566LIST_HEAD(dsos__kernel);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001567struct dso *vdso;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001568
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001569static void dsos__add(struct list_head *head, struct dso *dso)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001570{
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001571 list_add_tail(&dso->node, head);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001572}
1573
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001574static struct dso *dsos__find(struct list_head *head, const char *name)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001575{
1576 struct dso *pos;
1577
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001578 list_for_each_entry(pos, head, node)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001579 if (strcmp(pos->name, name) == 0)
1580 return pos;
1581 return NULL;
1582}
1583
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001584struct dso *dsos__findnew(const char *name)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001585{
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001586 struct dso *dso = dsos__find(&dsos__user, name);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001587
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -02001588 if (!dso) {
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001589 dso = dso__new(name);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001590 if (dso != NULL) {
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001591 dsos__add(&dsos__user, dso);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001592 dso__set_basename(dso);
1593 }
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001594 }
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001595
1596 return dso;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001597}
1598
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001599static void __dsos__fprintf(struct list_head *head, FILE *fp)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001600{
1601 struct dso *pos;
1602
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001603 list_for_each_entry(pos, head, node) {
1604 int i;
1605 for (i = 0; i < MAP__NR_TYPES; ++i)
1606 dso__fprintf(pos, i, fp);
1607 }
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001608}
1609
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001610void dsos__fprintf(FILE *fp)
1611{
1612 __dsos__fprintf(&dsos__kernel, fp);
1613 __dsos__fprintf(&dsos__user, fp);
1614}
1615
1616static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp)
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001617{
1618 struct dso *pos;
1619 size_t ret = 0;
1620
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001621 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001622 ret += dso__fprintf_buildid(pos, fp);
Arnaldo Carvalho de Melo1124ba72009-11-16 21:45:25 -02001623 ret += fprintf(fp, " %s\n", pos->long_name);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001624 }
1625 return ret;
1626}
1627
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001628size_t dsos__fprintf_buildid(FILE *fp)
1629{
1630 return (__dsos__fprintf_buildid(&dsos__kernel, fp) +
1631 __dsos__fprintf_buildid(&dsos__user, fp));
1632}
1633
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001634static struct dso *dsos__create_kernel( const char *vmlinux)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001635{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001636 struct dso *kernel = dso__new(vmlinux ?: "[kernel.kallsyms]");
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001637
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001638 if (kernel == NULL)
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001639 return NULL;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001640
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -02001641 kernel->short_name = "[kernel]";
1642 kernel->kernel = 1;
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001643
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001644 vdso = dso__new("[vdso]");
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001645 if (vdso == NULL)
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001646 goto out_delete_kernel_dso;
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001647 dso__set_loaded(vdso, MAP__FUNCTION);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001648
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001649 if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1650 sizeof(kernel->build_id)) == 0)
1651 kernel->has_build_id = true;
1652
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001653 dsos__add(&dsos__kernel, kernel);
1654 dsos__add(&dsos__user, vdso);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001655
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001656 return kernel;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001657
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001658out_delete_kernel_dso:
1659 dso__delete(kernel);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02001660 return NULL;
1661}
1662
1663static int map_groups__create_kernel_maps(struct map_groups *self, const char *vmlinux)
1664{
1665 struct map *functions, *variables;
1666 struct dso *kernel = dsos__create_kernel(vmlinux);
1667
1668 if (kernel == NULL)
1669 return -1;
1670
1671 functions = map__new2(0, kernel, MAP__FUNCTION);
1672 if (functions == NULL)
1673 return -1;
1674
1675 variables = map__new2(0, kernel, MAP__VARIABLE);
1676 if (variables == NULL) {
1677 map__delete(functions);
1678 return -1;
1679 }
1680
1681 functions->map_ip = functions->unmap_ip =
1682 variables->map_ip = variables->unmap_ip = identity__map_ip;
1683 map_groups__insert(self, functions);
1684 map_groups__insert(self, variables);
1685
1686 return 0;
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001687}
1688
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001689static void vmlinux_path__exit(void)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001690{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001691 while (--vmlinux_path__nr_entries >= 0) {
1692 free(vmlinux_path[vmlinux_path__nr_entries]);
1693 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1694 }
1695
1696 free(vmlinux_path);
1697 vmlinux_path = NULL;
1698}
1699
1700static int vmlinux_path__init(void)
1701{
1702 struct utsname uts;
1703 char bf[PATH_MAX];
1704
1705 if (uname(&uts) < 0)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001706 return -1;
1707
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001708 vmlinux_path = malloc(sizeof(char *) * 5);
1709 if (vmlinux_path == NULL)
1710 return -1;
1711
1712 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1713 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1714 goto out_fail;
1715 ++vmlinux_path__nr_entries;
1716 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1717 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1718 goto out_fail;
1719 ++vmlinux_path__nr_entries;
1720 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1721 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1722 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1723 goto out_fail;
1724 ++vmlinux_path__nr_entries;
1725 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1726 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1727 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1728 goto out_fail;
1729 ++vmlinux_path__nr_entries;
1730 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1731 uts.release);
1732 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1733 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1734 goto out_fail;
1735 ++vmlinux_path__nr_entries;
1736
1737 return 0;
1738
1739out_fail:
1740 vmlinux_path__exit();
1741 return -1;
1742}
1743
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001744static int setup_list(struct strlist **list, const char *list_str,
1745 const char *list_name)
1746{
1747 if (list_str == NULL)
1748 return 0;
1749
1750 *list = strlist__new(true, list_str);
1751 if (!*list) {
1752 pr_err("problems parsing %s list\n", list_name);
1753 return -1;
1754 }
1755 return 0;
1756}
1757
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001758int symbol__init(void)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001759{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001760 elf_version(EV_CURRENT);
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001761 if (symbol_conf.sort_by_name)
1762 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1763 sizeof(struct symbol));
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001764
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001765 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001766 return -1;
1767
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02001768 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1769 pr_err("'.' is the only non valid --field-separator argument\n");
1770 return -1;
1771 }
1772
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001773 if (setup_list(&symbol_conf.dso_list,
1774 symbol_conf.dso_list_str, "dso") < 0)
1775 return -1;
1776
1777 if (setup_list(&symbol_conf.comm_list,
1778 symbol_conf.comm_list_str, "comm") < 0)
1779 goto out_free_dso_list;
1780
1781 if (setup_list(&symbol_conf.sym_list,
1782 symbol_conf.sym_list_str, "symbol") < 0)
1783 goto out_free_comm_list;
1784
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001785 return 0;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001786
1787out_free_dso_list:
1788 strlist__delete(symbol_conf.dso_list);
1789out_free_comm_list:
1790 strlist__delete(symbol_conf.comm_list);
1791 return -1;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001792}
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001793
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001794int perf_session__create_kernel_maps(struct perf_session *self)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001795{
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001796 if (map_groups__create_kernel_maps(&self->kmaps,
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001797 symbol_conf.vmlinux_name) < 0)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001798 return -1;
1799
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001800 if (symbol_conf.use_modules &&
1801 perf_session__create_module_maps(self) < 0)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001802 pr_debug("Failed to load list of modules for session %s, "
1803 "continuing...\n", self->filename);
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -02001804 /*
1805 * Now that we have all the maps created, just set the ->end of them:
1806 */
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001807 map_groups__fixup_end(&self->kmaps);
Arnaldo Carvalho de Melo6671cb12009-11-20 20:51:24 -02001808 return 0;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001809}