blob: e0d9480dc371d6a7964134b4ca90ed10ef62e287 [file] [log] [blame]
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03001#define _GNU_SOURCE
2#include <ctype.h>
3#include <dirent.h>
4#include <errno.h>
5#include <libgen.h>
6#include <stdlib.h>
7#include <stdio.h>
8#include <string.h>
9#include <sys/types.h>
10#include <sys/stat.h>
11#include <sys/param.h>
12#include <fcntl.h>
13#include <unistd.h>
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -030014#include "build-id.h"
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030015#include "debug.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030016#include "symbol.h"
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -030017#include "strlist.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030018
19#include <libelf.h>
20#include <gelf.h>
21#include <elf.h>
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -020022#include <limits.h>
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030023#include <sys/utsname.h>
Peter Zijlstra2cdbc462009-08-05 14:05:16 +020024
Arnaldo Carvalho de Meloc12e15e2009-11-21 14:31:25 -020025#ifndef NT_GNU_BUILD_ID
26#define NT_GNU_BUILD_ID 3
27#endif
28
Dave Martin21916c32010-07-30 09:08:08 -030029static bool dso__build_id_equal(const struct dso *self, u8 *build_id);
30static int elf_read_build_id(Elf *elf, void *bf, size_t size);
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -020031static void dsos__add(struct list_head *head, struct dso *dso);
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020032static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020033static int dso__load_kernel_sym(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -020034 symbol_filter_t filter);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080035static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
36 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 Melo8a6c5b22010-07-20 14:42:52 -030046int dso__name_len(const struct dso *self)
47{
48 if (verbose)
49 return self->long_name_len;
50
51 return self->short_name_len;
52}
53
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020054bool dso__loaded(const struct dso *self, enum map_type type)
55{
56 return self->loaded & (1 << type);
57}
58
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -020059bool dso__sorted_by_name(const struct dso *self, enum map_type type)
60{
61 return self->sorted_by_name & (1 << type);
62}
63
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -020064static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
65{
66 self->sorted_by_name |= (1 << type);
67}
68
Arnaldo Carvalho de Melo36a3e642010-01-04 16:19:27 -020069bool symbol_type__is_a(char symbol_type, enum map_type map_type)
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020070{
71 switch (map_type) {
72 case MAP__FUNCTION:
73 return symbol_type == 'T' || symbol_type == 'W';
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -020074 case MAP__VARIABLE:
75 return symbol_type == 'D' || symbol_type == 'd';
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020076 default:
77 return false;
78 }
79}
80
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -020081static void symbols__fixup_end(struct rb_root *self)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030082{
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -020083 struct rb_node *nd, *prevnd = rb_first(self);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030084 struct symbol *curr, *prev;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030085
86 if (prevnd == NULL)
87 return;
88
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030089 curr = rb_entry(prevnd, struct symbol, rb_node);
90
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030091 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030092 prev = curr;
93 curr = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030094
95 if (prev->end == prev->start)
96 prev->end = curr->start - 1;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030097 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030098
99 /* Last entry */
100 if (curr->end == curr->start)
101 curr->end = roundup(curr->start, 4096);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300102}
103
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200104static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300105{
106 struct map *prev, *curr;
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200107 struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300108
109 if (prevnd == NULL)
110 return;
111
112 curr = rb_entry(prevnd, struct map, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300113
114 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
115 prev = curr;
116 curr = rb_entry(nd, struct map, rb_node);
117 prev->end = curr->start - 1;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300118 }
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -0200119
120 /*
121 * We still haven't the actual symbols, so guess the
122 * last map final address.
123 */
124 curr->end = ~0UL;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300125}
126
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200127static void map_groups__fixup_end(struct map_groups *self)
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -0200128{
129 int i;
130 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200131 __map_groups__fixup_end(self, i);
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -0200132}
133
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200134static struct symbol *symbol__new(u64 start, u64 len, const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300135{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300136 size_t namelen = strlen(name) + 1;
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -0300137 struct symbol *self = calloc(1, (symbol_conf.priv_size +
138 sizeof(*self) + namelen));
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200139 if (self == NULL)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200140 return NULL;
141
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200142 if (symbol_conf.priv_size)
143 self = ((void *)self) + symbol_conf.priv_size;
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200144
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300145 self->start = start;
146 self->end = len ? start + len - 1 : start;
147 self->namelen = namelen - 1;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200148
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -0200149 pr_debug4("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200150
Ingo Molnar0b73da32009-06-06 15:48:52 +0200151 memcpy(self->name, name, namelen);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300152
153 return self;
154}
155
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -0300156void symbol__delete(struct symbol *self)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300157{
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200158 free(((void *)self) - symbol_conf.priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300159}
160
161static size_t symbol__fprintf(struct symbol *self, FILE *fp)
162{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300163 return fprintf(fp, " %llx-%llx %s\n",
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300164 self->start, self->end, self->name);
165}
166
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200167void dso__set_long_name(struct dso *self, char *name)
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200168{
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -0200169 if (name == NULL)
170 return;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200171 self->long_name = name;
172 self->long_name_len = strlen(name);
173}
174
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -0300175static void dso__set_short_name(struct dso *self, const char *name)
176{
177 if (name == NULL)
178 return;
179 self->short_name = name;
180 self->short_name_len = strlen(name);
181}
182
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200183static void dso__set_basename(struct dso *self)
184{
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -0300185 dso__set_short_name(self, basename(self->long_name));
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200186}
187
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200188struct dso *dso__new(const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300189{
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -0300190 struct dso *self = calloc(1, sizeof(*self) + strlen(name) + 1);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300191
192 if (self != NULL) {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200193 int i;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300194 strcpy(self->name, name);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200195 dso__set_long_name(self, self->name);
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -0300196 dso__set_short_name(self, self->name);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200197 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200198 self->symbols[i] = self->symbol_names[i] = RB_ROOT;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300199 self->slen_calculated = 0;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300200 self->origin = DSO__ORIG_NOT_FOUND;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200201 self->loaded = 0;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200202 self->sorted_by_name = 0;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200203 self->has_build_id = 0;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800204 self->kernel = DSO_TYPE_USER;
Masami Hiramatsu0ab061c2010-04-21 15:56:16 -0400205 INIT_LIST_HEAD(&self->node);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300206 }
207
208 return self;
209}
210
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200211static void symbols__delete(struct rb_root *self)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300212{
213 struct symbol *pos;
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200214 struct rb_node *next = rb_first(self);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300215
216 while (next) {
217 pos = rb_entry(next, struct symbol, rb_node);
218 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200219 rb_erase(&pos->rb_node, self);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200220 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300221 }
222}
223
224void dso__delete(struct dso *self)
225{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200226 int i;
227 for (i = 0; i < MAP__NR_TYPES; ++i)
228 symbols__delete(&self->symbols[i]);
Arnaldo Carvalho de Melo6e406252010-07-29 15:11:30 -0300229 if (self->sname_alloc)
230 free((char *)self->short_name);
231 if (self->lname_alloc)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300232 free(self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300233 free(self);
234}
235
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200236void dso__set_build_id(struct dso *self, void *build_id)
237{
238 memcpy(self->build_id, build_id, sizeof(self->build_id));
239 self->has_build_id = 1;
240}
241
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200242static void symbols__insert(struct rb_root *self, struct symbol *sym)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300243{
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200244 struct rb_node **p = &self->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300245 struct rb_node *parent = NULL;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000246 const u64 ip = sym->start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300247 struct symbol *s;
248
249 while (*p != NULL) {
250 parent = *p;
251 s = rb_entry(parent, struct symbol, rb_node);
252 if (ip < s->start)
253 p = &(*p)->rb_left;
254 else
255 p = &(*p)->rb_right;
256 }
257 rb_link_node(&sym->rb_node, parent, p);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200258 rb_insert_color(&sym->rb_node, self);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300259}
260
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200261static struct symbol *symbols__find(struct rb_root *self, u64 ip)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300262{
263 struct rb_node *n;
264
265 if (self == NULL)
266 return NULL;
267
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200268 n = self->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300269
270 while (n) {
271 struct symbol *s = rb_entry(n, struct symbol, rb_node);
272
273 if (ip < s->start)
274 n = n->rb_left;
275 else if (ip > s->end)
276 n = n->rb_right;
277 else
278 return s;
279 }
280
281 return NULL;
282}
283
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200284struct symbol_name_rb_node {
285 struct rb_node rb_node;
286 struct symbol sym;
287};
288
289static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
290{
291 struct rb_node **p = &self->rb_node;
292 struct rb_node *parent = NULL;
293 struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
294
295 while (*p != NULL) {
296 parent = *p;
297 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
298 if (strcmp(sym->name, s->sym.name) < 0)
299 p = &(*p)->rb_left;
300 else
301 p = &(*p)->rb_right;
302 }
303 rb_link_node(&symn->rb_node, parent, p);
304 rb_insert_color(&symn->rb_node, self);
305}
306
307static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
308{
309 struct rb_node *nd;
310
311 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
312 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
313 symbols__insert_by_name(self, pos);
314 }
315}
316
317static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
318{
319 struct rb_node *n;
320
321 if (self == NULL)
322 return NULL;
323
324 n = self->rb_node;
325
326 while (n) {
327 struct symbol_name_rb_node *s;
328 int cmp;
329
330 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
331 cmp = strcmp(name, s->sym.name);
332
333 if (cmp < 0)
334 n = n->rb_left;
335 else if (cmp > 0)
336 n = n->rb_right;
337 else
338 return &s->sym;
339 }
340
341 return NULL;
342}
343
344struct symbol *dso__find_symbol(struct dso *self,
345 enum map_type type, u64 addr)
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200346{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200347 return symbols__find(&self->symbols[type], addr);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200348}
349
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200350struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
351 const char *name)
352{
353 return symbols__find_by_name(&self->symbol_names[type], name);
354}
355
356void dso__sort_by_name(struct dso *self, enum map_type type)
357{
358 dso__set_sorted_by_name(self, type);
359 return symbols__sort_by_name(&self->symbol_names[type],
360 &self->symbols[type]);
361}
362
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200363int build_id__sprintf(const u8 *self, int len, char *bf)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200364{
365 char *bid = bf;
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200366 const u8 *raw = self;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200367 int i;
368
369 for (i = 0; i < len; ++i) {
370 sprintf(bid, "%02x", *raw);
371 ++raw;
372 bid += 2;
373 }
374
375 return raw - self;
376}
377
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200378size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300379{
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200380 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200381
382 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200383 return fprintf(fp, "%s", sbuild_id);
384}
385
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200386size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200387{
388 struct rb_node *nd;
389 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
390
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -0300391 if (self->short_name != self->long_name)
392 ret += fprintf(fp, "%s, ", self->long_name);
393 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
394 self->loaded ? "" : "NOT ");
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200395 ret += dso__fprintf_buildid(self, fp);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200396 ret += fprintf(fp, ")\n");
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200397 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
398 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
399 ret += symbol__fprintf(pos, fp);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300400 }
401
402 return ret;
403}
404
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200405int kallsyms__parse(const char *filename, void *arg,
406 int (*process_symbol)(void *arg, const char *name,
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200407 char type, u64 start))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300408{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300409 char *line = NULL;
410 size_t n;
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200411 int err = 0;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200412 FILE *file = fopen(filename, "r");
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300413
414 if (file == NULL)
415 goto out_failure;
416
417 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000418 u64 start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300419 int line_len, len;
420 char symbol_type;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300421 char *symbol_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300422
423 line_len = getline(&line, &n, file);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800424 if (line_len < 0 || !line)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300425 break;
426
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300427 line[--line_len] = '\0'; /* \n */
428
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300429 len = hex2u64(line, &start);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300430
431 len++;
432 if (len + 2 >= line_len)
433 continue;
434
435 symbol_type = toupper(line[len]);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300436 symbol_name = line + len + 2;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300437
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200438 err = process_symbol(arg, symbol_name, symbol_type, start);
439 if (err)
440 break;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300441 }
442
443 free(line);
444 fclose(file);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200445 return err;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300446
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300447out_failure:
448 return -1;
449}
450
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200451struct process_kallsyms_args {
452 struct map *map;
453 struct dso *dso;
454};
455
456static int map__process_kallsym_symbol(void *arg, const char *name,
457 char type, u64 start)
458{
459 struct symbol *sym;
460 struct process_kallsyms_args *a = arg;
461 struct rb_root *root = &a->dso->symbols[a->map->type];
462
463 if (!symbol_type__is_a(type, a->map->type))
464 return 0;
465
466 /*
467 * Will fix up the end later, when we have all symbols sorted.
468 */
469 sym = symbol__new(start, 0, name);
470
471 if (sym == NULL)
472 return -ENOMEM;
473 /*
474 * We will pass the symbols to the filter later, in
475 * map__split_kallsyms, when we have split the maps per module
476 */
477 symbols__insert(root, sym);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800478
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200479 return 0;
480}
481
482/*
483 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
484 * so that we can in the next step set the symbol ->end address and then
485 * call kernel_maps__split_kallsyms.
486 */
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200487static int dso__load_all_kallsyms(struct dso *self, const char *filename,
488 struct map *map)
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200489{
490 struct process_kallsyms_args args = { .map = map, .dso = self, };
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200491 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200492}
493
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300494/*
495 * Split the symbols into maps, making sure there are no overlaps, i.e. the
496 * kernel range is broken in several maps, named [kernel].N, as we don't have
497 * the original ELF section names vmlinux have.
498 */
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200499static int dso__split_kallsyms(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200500 symbol_filter_t filter)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300501{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200502 struct map_groups *kmaps = map__kmap(map)->kmaps;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300503 struct machine *machine = kmaps->machine;
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200504 struct map *curr_map = map;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300505 struct symbol *pos;
506 int count = 0;
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200507 struct rb_root *root = &self->symbols[map->type];
508 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300509 int kernel_range = 0;
510
511 while (next) {
512 char *module;
513
514 pos = rb_entry(next, struct symbol, rb_node);
515 next = rb_next(&pos->rb_node);
516
517 module = strchr(pos->name, '\t');
518 if (module) {
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200519 if (!symbol_conf.use_modules)
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200520 goto discard_symbol;
521
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300522 *module++ = '\0';
523
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200524 if (strcmp(curr_map->dso->short_name, module)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800525 if (curr_map != map &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300526 self->kernel == DSO_TYPE_GUEST_KERNEL &&
527 machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800528 /*
529 * We assume all symbols of a module are
530 * continuous in * kallsyms, so curr_map
531 * points to a module and all its
532 * symbols are in its kmap. Mark it as
533 * loaded.
534 */
535 dso__set_loaded(curr_map->dso,
536 curr_map->type);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300537 }
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200538
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800539 curr_map = map_groups__find_by_name(kmaps,
540 map->type, module);
541 if (curr_map == NULL) {
Arnaldo Carvalho de Melo2f519032010-05-17 17:57:59 -0300542 pr_debug("%s/proc/{kallsyms,modules} "
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800543 "inconsistency while looking "
544 "for \"%s\" module!\n",
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300545 machine->root_dir, module);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800546 curr_map = map;
547 goto discard_symbol;
548 }
549
550 if (curr_map->dso->loaded &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300551 !machine__is_default_guest(machine))
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200552 goto discard_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300553 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300554 /*
555 * So that we look just like we get from .ko files,
556 * i.e. not prelinked, relative to map->start.
557 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200558 pos->start = curr_map->map_ip(curr_map, pos->start);
559 pos->end = curr_map->map_ip(curr_map, pos->end);
560 } else if (curr_map != map) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300561 char dso_name[PATH_MAX];
562 struct dso *dso;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300563
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800564 if (self->kernel == DSO_TYPE_GUEST_KERNEL)
565 snprintf(dso_name, sizeof(dso_name),
566 "[guest.kernel].%d",
567 kernel_range++);
568 else
569 snprintf(dso_name, sizeof(dso_name),
570 "[kernel].%d",
571 kernel_range++);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300572
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200573 dso = dso__new(dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300574 if (dso == NULL)
575 return -1;
576
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800577 dso->kernel = self->kernel;
578
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200579 curr_map = map__new2(pos->start, dso, map->type);
Zhang, Yanmin37fe5fc2010-02-25 11:00:51 +0800580 if (curr_map == NULL) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300581 dso__delete(dso);
582 return -1;
583 }
584
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200585 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200586 map_groups__insert(kmaps, curr_map);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300587 ++kernel_range;
588 }
589
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200590 if (filter && filter(curr_map, pos)) {
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200591discard_symbol: rb_erase(&pos->rb_node, root);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200592 symbol__delete(pos);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300593 } else {
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200594 if (curr_map != map) {
595 rb_erase(&pos->rb_node, root);
596 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300597 }
Mike Galbraith9974f492009-07-02 08:05:58 +0200598 count++;
599 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300600 }
601
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800602 if (curr_map != map &&
603 self->kernel == DSO_TYPE_GUEST_KERNEL &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300604 machine__is_default_guest(kmaps->machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800605 dso__set_loaded(curr_map->dso, curr_map->type);
606 }
607
Mike Galbraith9974f492009-07-02 08:05:58 +0200608 return count;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300609}
610
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200611int dso__load_kallsyms(struct dso *self, const char *filename,
612 struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300613{
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200614 if (dso__load_all_kallsyms(self, filename, map) < 0)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300615 return -1;
616
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200617 symbols__fixup_end(&self->symbols[map->type]);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800618 if (self->kernel == DSO_TYPE_GUEST_KERNEL)
619 self->origin = DSO__ORIG_GUEST_KERNEL;
620 else
621 self->origin = DSO__ORIG_KERNEL;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300622
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200623 return dso__split_kallsyms(self, map, filter);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300624}
625
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300626static int dso__load_perf_map(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200627 symbol_filter_t filter)
Pekka Enberg80d496b2009-06-08 21:12:48 +0300628{
629 char *line = NULL;
630 size_t n;
631 FILE *file;
632 int nr_syms = 0;
633
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300634 file = fopen(self->long_name, "r");
Pekka Enberg80d496b2009-06-08 21:12:48 +0300635 if (file == NULL)
636 goto out_failure;
637
638 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000639 u64 start, size;
Pekka Enberg80d496b2009-06-08 21:12:48 +0300640 struct symbol *sym;
641 int line_len, len;
642
643 line_len = getline(&line, &n, file);
644 if (line_len < 0)
645 break;
646
647 if (!line)
648 goto out_failure;
649
650 line[--line_len] = '\0'; /* \n */
651
652 len = hex2u64(line, &start);
653
654 len++;
655 if (len + 2 >= line_len)
656 continue;
657
658 len += hex2u64(line + len, &size);
659
660 len++;
661 if (len + 2 >= line_len)
662 continue;
663
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200664 sym = symbol__new(start, size, line + len);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300665
666 if (sym == NULL)
667 goto out_delete_line;
668
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300669 if (filter && filter(map, sym))
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200670 symbol__delete(sym);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300671 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200672 symbols__insert(&self->symbols[map->type], sym);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300673 nr_syms++;
674 }
675 }
676
677 free(line);
678 fclose(file);
679
680 return nr_syms;
681
682out_delete_line:
683 free(line);
684out_failure:
685 return -1;
686}
687
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300688/**
689 * elf_symtab__for_each_symbol - iterate thru all the symbols
690 *
691 * @self: struct elf_symtab instance to iterate
Ingo Molnar83a09442009-08-15 12:26:57 +0200692 * @idx: uint32_t idx
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300693 * @sym: GElf_Sym iterator
694 */
Ingo Molnar83a09442009-08-15 12:26:57 +0200695#define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
696 for (idx = 0, gelf_getsym(syms, idx, &sym);\
697 idx < nr_syms; \
698 idx++, gelf_getsym(syms, idx, &sym))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300699
700static inline uint8_t elf_sym__type(const GElf_Sym *sym)
701{
702 return GELF_ST_TYPE(sym->st_info);
703}
704
705static inline int elf_sym__is_function(const GElf_Sym *sym)
706{
707 return elf_sym__type(sym) == STT_FUNC &&
708 sym->st_name != 0 &&
Arnaldo Carvalho de Melo81833132009-10-05 23:35:03 -0300709 sym->st_shndx != SHN_UNDEF;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300710}
711
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200712static inline bool elf_sym__is_object(const GElf_Sym *sym)
713{
714 return elf_sym__type(sym) == STT_OBJECT &&
715 sym->st_name != 0 &&
716 sym->st_shndx != SHN_UNDEF;
717}
718
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200719static inline int elf_sym__is_label(const GElf_Sym *sym)
720{
721 return elf_sym__type(sym) == STT_NOTYPE &&
722 sym->st_name != 0 &&
723 sym->st_shndx != SHN_UNDEF &&
724 sym->st_shndx != SHN_ABS;
725}
726
727static inline const char *elf_sec__name(const GElf_Shdr *shdr,
728 const Elf_Data *secstrs)
729{
730 return secstrs->d_buf + shdr->sh_name;
731}
732
733static inline int elf_sec__is_text(const GElf_Shdr *shdr,
734 const Elf_Data *secstrs)
735{
736 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
737}
738
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200739static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
740 const Elf_Data *secstrs)
741{
742 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
743}
744
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300745static inline const char *elf_sym__name(const GElf_Sym *sym,
746 const Elf_Data *symstrs)
747{
748 return symstrs->d_buf + sym->st_name;
749}
750
751static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
752 GElf_Shdr *shp, const char *name,
Ingo Molnar83a09442009-08-15 12:26:57 +0200753 size_t *idx)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300754{
755 Elf_Scn *sec = NULL;
756 size_t cnt = 1;
757
758 while ((sec = elf_nextscn(elf, sec)) != NULL) {
759 char *str;
760
761 gelf_getshdr(sec, shp);
762 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
763 if (!strcmp(name, str)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200764 if (idx)
765 *idx = cnt;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300766 break;
767 }
768 ++cnt;
769 }
770
771 return sec;
772}
773
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300774#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
775 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
776 idx < nr_entries; \
777 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
778
779#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
780 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
781 idx < nr_entries; \
782 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
783
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300784/*
785 * We need to check if we have a .dynsym, so that we can handle the
786 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
787 * .dynsym or .symtab).
788 * And always look at the original dso, not at debuginfo packages, that
789 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
790 */
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200791static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
792 symbol_filter_t filter)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300793{
794 uint32_t nr_rel_entries, idx;
795 GElf_Sym sym;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000796 u64 plt_offset;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300797 GElf_Shdr shdr_plt;
798 struct symbol *f;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300799 GElf_Shdr shdr_rel_plt, shdr_dynsym;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300800 Elf_Data *reldata, *syms, *symstrs;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300801 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
802 size_t dynsym_idx;
803 GElf_Ehdr ehdr;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300804 char sympltname[1024];
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300805 Elf *elf;
806 int nr = 0, symidx, fd, err = 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300807
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300808 fd = open(self->long_name, O_RDONLY);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300809 if (fd < 0)
810 goto out;
811
Marti Raudsepp84087122009-10-24 19:10:36 +0300812 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300813 if (elf == NULL)
814 goto out_close;
815
816 if (gelf_getehdr(elf, &ehdr) == NULL)
817 goto out_elf_end;
818
819 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
820 ".dynsym", &dynsym_idx);
821 if (scn_dynsym == NULL)
822 goto out_elf_end;
823
824 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300825 ".rela.plt", NULL);
826 if (scn_plt_rel == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300827 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300828 ".rel.plt", NULL);
829 if (scn_plt_rel == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300830 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300831 }
832
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300833 err = -1;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300834
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300835 if (shdr_rel_plt.sh_link != dynsym_idx)
836 goto out_elf_end;
837
838 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
839 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300840
841 /*
Ingo Molnar83a09442009-08-15 12:26:57 +0200842 * Fetch the relocation section to find the idxes to the GOT
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300843 * and the symbols in the .dynsym they refer to.
844 */
845 reldata = elf_getdata(scn_plt_rel, NULL);
846 if (reldata == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300847 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300848
849 syms = elf_getdata(scn_dynsym, NULL);
850 if (syms == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300851 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300852
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300853 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300854 if (scn_symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300855 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300856
857 symstrs = elf_getdata(scn_symstrs, NULL);
858 if (symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300859 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300860
861 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
862 plt_offset = shdr_plt.sh_offset;
863
864 if (shdr_rel_plt.sh_type == SHT_RELA) {
865 GElf_Rela pos_mem, *pos;
866
867 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
868 nr_rel_entries) {
869 symidx = GELF_R_SYM(pos->r_info);
870 plt_offset += shdr_plt.sh_entsize;
871 gelf_getsym(syms, symidx, &sym);
872 snprintf(sympltname, sizeof(sympltname),
873 "%s@plt", elf_sym__name(&sym, symstrs));
874
875 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200876 sympltname);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300877 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300878 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300879
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200880 if (filter && filter(map, f))
881 symbol__delete(f);
882 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200883 symbols__insert(&self->symbols[map->type], f);
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200884 ++nr;
885 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300886 }
887 } else if (shdr_rel_plt.sh_type == SHT_REL) {
888 GElf_Rel pos_mem, *pos;
889 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
890 nr_rel_entries) {
891 symidx = GELF_R_SYM(pos->r_info);
892 plt_offset += shdr_plt.sh_entsize;
893 gelf_getsym(syms, symidx, &sym);
894 snprintf(sympltname, sizeof(sympltname),
895 "%s@plt", elf_sym__name(&sym, symstrs));
896
897 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200898 sympltname);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300899 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300900 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300901
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200902 if (filter && filter(map, f))
903 symbol__delete(f);
904 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200905 symbols__insert(&self->symbols[map->type], f);
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200906 ++nr;
907 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300908 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300909 }
910
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300911 err = 0;
912out_elf_end:
913 elf_end(elf);
914out_close:
915 close(fd);
916
917 if (err == 0)
918 return nr;
919out:
Arnaldo Carvalho de Melofe2197b2010-03-11 20:12:40 -0300920 pr_debug("%s: problems reading %s PLT info.\n",
921 __func__, self->long_name);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300922 return 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300923}
924
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -0200925static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
926{
927 switch (type) {
928 case MAP__FUNCTION:
929 return elf_sym__is_function(self);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200930 case MAP__VARIABLE:
931 return elf_sym__is_object(self);
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -0200932 default:
933 return false;
934 }
935}
936
937static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
938{
939 switch (type) {
940 case MAP__FUNCTION:
941 return elf_sec__is_text(self, secstrs);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200942 case MAP__VARIABLE:
943 return elf_sec__is_data(self, secstrs);
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -0200944 default:
945 return false;
946 }
947}
948
Eric B Munson70c38562010-06-14 14:56:33 +0100949static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
950{
951 Elf_Scn *sec = NULL;
952 GElf_Shdr shdr;
953 size_t cnt = 1;
954
955 while ((sec = elf_nextscn(elf, sec)) != NULL) {
956 gelf_getshdr(sec, &shdr);
957
958 if ((addr >= shdr.sh_addr) &&
959 (addr < (shdr.sh_addr + shdr.sh_size)))
960 return cnt;
961
962 ++cnt;
963 }
964
965 return -1;
966}
967
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200968static int dso__load_sym(struct dso *self, struct map *map, const char *name,
969 int fd, symbol_filter_t filter, int kmodule)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300970{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200971 struct kmap *kmap = self->kernel ? map__kmap(map) : NULL;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300972 struct map *curr_map = map;
973 struct dso *curr_dso = self;
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200974 Elf_Data *symstrs, *secstrs;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300975 uint32_t nr_syms;
976 int err = -1;
Ingo Molnar83a09442009-08-15 12:26:57 +0200977 uint32_t idx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300978 GElf_Ehdr ehdr;
Eric B Munson70c38562010-06-14 14:56:33 +0100979 GElf_Shdr shdr, opdshdr;
980 Elf_Data *syms, *opddata = NULL;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300981 GElf_Sym sym;
Eric B Munson70c38562010-06-14 14:56:33 +0100982 Elf_Scn *sec, *sec_strndx, *opdsec;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300983 Elf *elf;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300984 int nr = 0;
Eric B Munson70c38562010-06-14 14:56:33 +0100985 size_t opdidx = 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300986
Marti Raudsepp84087122009-10-24 19:10:36 +0300987 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300988 if (elf == NULL) {
Dave Martin8b1389e2010-07-30 09:36:08 -0300989 pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300990 goto out_close;
991 }
992
993 if (gelf_getehdr(elf, &ehdr) == NULL) {
Dave Martin8b1389e2010-07-30 09:36:08 -0300994 pr_debug("%s: cannot get elf header.\n", __func__);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300995 goto out_elf_end;
996 }
997
Dave Martin21916c32010-07-30 09:08:08 -0300998 if (self->has_build_id) {
999 u8 build_id[BUILD_ID_SIZE];
1000
1001 if (elf_read_build_id(elf, build_id,
1002 BUILD_ID_SIZE) != BUILD_ID_SIZE)
1003 goto out_elf_end;
1004
1005 if (!dso__build_id_equal(self, build_id))
1006 goto out_elf_end;
1007 }
1008
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001009 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
1010 if (sec == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001011 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
1012 if (sec == NULL)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001013 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -03001014 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001015
Eric B Munson70c38562010-06-14 14:56:33 +01001016 opdsec = elf_section_by_name(elf, &ehdr, &opdshdr, ".opd", &opdidx);
1017 if (opdsec)
1018 opddata = elf_rawdata(opdsec, NULL);
1019
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001020 syms = elf_getdata(sec, NULL);
1021 if (syms == NULL)
1022 goto out_elf_end;
1023
1024 sec = elf_getscn(elf, shdr.sh_link);
1025 if (sec == NULL)
1026 goto out_elf_end;
1027
1028 symstrs = elf_getdata(sec, NULL);
1029 if (symstrs == NULL)
1030 goto out_elf_end;
1031
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001032 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
1033 if (sec_strndx == NULL)
1034 goto out_elf_end;
1035
1036 secstrs = elf_getdata(sec_strndx, NULL);
Stoyan Gaydarov9b30a262009-07-30 05:25:29 -05001037 if (secstrs == NULL)
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001038 goto out_elf_end;
1039
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001040 nr_syms = shdr.sh_size / shdr.sh_entsize;
1041
Arjan van de Vene9fbc9d2009-06-06 21:22:33 +02001042 memset(&sym, 0, sizeof(sym));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001043 if (self->kernel == DSO_TYPE_USER) {
Mike Galbraithd20ff6b2009-07-20 14:01:38 +02001044 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -03001045 elf_section_by_name(elf, &ehdr, &shdr,
1046 ".gnu.prelink_undo",
1047 NULL) != NULL);
Mike Galbraithd20ff6b2009-07-20 14:01:38 +02001048 } else self->adjust_symbols = 0;
1049
Ingo Molnar83a09442009-08-15 12:26:57 +02001050 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001051 struct symbol *f;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001052 const char *elf_name = elf_sym__name(&sym, symstrs);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001053 char *demangled = NULL;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001054 int is_label = elf_sym__is_label(&sym);
1055 const char *section_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001056
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001057 if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
1058 strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
1059 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001060
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -02001061 if (!is_label && !elf_sym__is_a(&sym, map->type))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001062 continue;
1063
Eric B Munson70c38562010-06-14 14:56:33 +01001064 if (opdsec && sym.st_shndx == opdidx) {
1065 u32 offset = sym.st_value - opdshdr.sh_addr;
1066 u64 *opd = opddata->d_buf + offset;
1067 sym.st_value = *opd;
1068 sym.st_shndx = elf_addr_to_index(elf, sym.st_value);
1069 }
1070
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001071 sec = elf_getscn(elf, sym.st_shndx);
1072 if (!sec)
1073 goto out_elf_end;
1074
1075 gelf_getshdr(sec, &shdr);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001076
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -02001077 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001078 continue;
1079
1080 section_name = elf_sec__name(&shdr, secstrs);
Ingo Molnar0b73da32009-06-06 15:48:52 +02001081
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001082 if (self->kernel != DSO_TYPE_USER || kmodule) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001083 char dso_name[PATH_MAX];
1084
1085 if (strcmp(section_name,
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -03001086 (curr_dso->short_name +
1087 self->short_name_len)) == 0)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001088 goto new_symbol;
1089
1090 if (strcmp(section_name, ".text") == 0) {
1091 curr_map = map;
1092 curr_dso = self;
1093 goto new_symbol;
1094 }
1095
1096 snprintf(dso_name, sizeof(dso_name),
1097 "%s%s", self->short_name, section_name);
1098
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001099 curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001100 if (curr_map == NULL) {
1101 u64 start = sym.st_value;
1102
1103 if (kmodule)
1104 start += map->start + shdr.sh_offset;
1105
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001106 curr_dso = dso__new(dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001107 if (curr_dso == NULL)
1108 goto out_elf_end;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001109 curr_dso->kernel = self->kernel;
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001110 curr_map = map__new2(start, curr_dso,
Arnaldo Carvalho de Melo6275ce22010-02-03 16:52:01 -02001111 map->type);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001112 if (curr_map == NULL) {
1113 dso__delete(curr_dso);
1114 goto out_elf_end;
1115 }
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -02001116 curr_map->map_ip = identity__map_ip;
1117 curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -03001118 curr_dso->origin = self->origin;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001119 map_groups__insert(kmap->kmaps, curr_map);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001120 dsos__add(&self->node, curr_dso);
Arnaldo Carvalho de Melo6275ce22010-02-03 16:52:01 -02001121 dso__set_loaded(curr_dso, map->type);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001122 } else
1123 curr_dso = curr_map->dso;
1124
1125 goto new_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001126 }
1127
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001128 if (curr_dso->adjust_symbols) {
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -02001129 pr_debug4("%s: adjusting symbol: st_value: %#Lx "
1130 "sh_addr: %#Lx sh_offset: %#Lx\n", __func__,
1131 (u64)sym.st_value, (u64)shdr.sh_addr,
1132 (u64)shdr.sh_offset);
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001133 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001134 }
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -03001135 /*
1136 * We need to figure out if the object was created from C++ sources
1137 * DWARF DW_compile_unit has this, but we don't always have access
1138 * to it...
1139 */
Ingo Molnar83a09442009-08-15 12:26:57 +02001140 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -03001141 if (demangled != NULL)
Ingo Molnar83a09442009-08-15 12:26:57 +02001142 elf_name = demangled;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001143new_symbol:
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001144 f = symbol__new(sym.st_value, sym.st_size, elf_name);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -03001145 free(demangled);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001146 if (!f)
1147 goto out_elf_end;
1148
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001149 if (filter && filter(curr_map, f))
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001150 symbol__delete(f);
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -03001151 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001152 symbols__insert(&curr_dso->symbols[curr_map->type], f);
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -03001153 nr++;
1154 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001155 }
1156
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001157 /*
1158 * For misannotated, zeroed, ASM function sizes.
1159 */
Arnaldo Carvalho de Melo6275ce22010-02-03 16:52:01 -02001160 if (nr > 0) {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001161 symbols__fixup_end(&self->symbols[map->type]);
Arnaldo Carvalho de Melo6275ce22010-02-03 16:52:01 -02001162 if (kmap) {
1163 /*
1164 * We need to fixup this here too because we create new
1165 * maps here, for things like vsyscall sections.
1166 */
1167 __map_groups__fixup_end(kmap->kmaps, map->type);
1168 }
1169 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001170 err = nr;
1171out_elf_end:
1172 elf_end(elf);
1173out_close:
1174 return err;
1175}
1176
Arnaldo Carvalho de Melo78075ca2009-11-20 20:51:26 -02001177static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1178{
1179 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1180}
1181
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001182bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001183{
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -02001184 bool have_build_id = false;
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001185 struct dso *pos;
1186
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001187 list_for_each_entry(pos, head, node) {
1188 if (with_hits && !pos->hit)
1189 continue;
Arnaldo Carvalho de Melof6e14672010-05-19 11:13:02 -03001190 if (pos->has_build_id) {
1191 have_build_id = true;
1192 continue;
1193 }
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -02001194 if (filename__read_build_id(pos->long_name, pos->build_id,
1195 sizeof(pos->build_id)) > 0) {
1196 have_build_id = true;
1197 pos->has_build_id = true;
1198 }
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001199 }
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001200
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -02001201 return have_build_id;
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001202}
1203
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001204/*
1205 * Align offset to 4 bytes as needed for note name and descriptor data.
1206 */
1207#define NOTE_ALIGN(n) (((n) + 3) & -4U)
1208
Dave Martin21916c32010-07-30 09:08:08 -03001209static int elf_read_build_id(Elf *elf, void *bf, size_t size)
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001210{
Dave Martin21916c32010-07-30 09:08:08 -03001211 int err = -1;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001212 GElf_Ehdr ehdr;
1213 GElf_Shdr shdr;
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001214 Elf_Data *data;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001215 Elf_Scn *sec;
Pekka Enberge57cfcd2009-11-22 12:29:44 +02001216 Elf_Kind ek;
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001217 void *ptr;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001218
Arnaldo Carvalho de Melo2643ce1142009-11-03 21:46:10 -02001219 if (size < BUILD_ID_SIZE)
1220 goto out;
1221
Pekka Enberge57cfcd2009-11-22 12:29:44 +02001222 ek = elf_kind(elf);
1223 if (ek != ELF_K_ELF)
Dave Martin21916c32010-07-30 09:08:08 -03001224 goto out;
Pekka Enberge57cfcd2009-11-22 12:29:44 +02001225
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001226 if (gelf_getehdr(elf, &ehdr) == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001227 pr_err("%s: cannot get elf header.\n", __func__);
Dave Martin21916c32010-07-30 09:08:08 -03001228 goto out;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001229 }
1230
Arnaldo Carvalho de Melo2643ce1142009-11-03 21:46:10 -02001231 sec = elf_section_by_name(elf, &ehdr, &shdr,
1232 ".note.gnu.build-id", NULL);
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001233 if (sec == NULL) {
1234 sec = elf_section_by_name(elf, &ehdr, &shdr,
1235 ".notes", NULL);
1236 if (sec == NULL)
Dave Martin21916c32010-07-30 09:08:08 -03001237 goto out;
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001238 }
1239
1240 data = elf_getdata(sec, NULL);
1241 if (data == NULL)
Dave Martin21916c32010-07-30 09:08:08 -03001242 goto out;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001243
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001244 ptr = data->d_buf;
1245 while (ptr < (data->d_buf + data->d_size)) {
1246 GElf_Nhdr *nhdr = ptr;
1247 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1248 descsz = NOTE_ALIGN(nhdr->n_descsz);
1249 const char *name;
1250
1251 ptr += sizeof(*nhdr);
1252 name = ptr;
1253 ptr += namesz;
1254 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1255 nhdr->n_namesz == sizeof("GNU")) {
1256 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1257 memcpy(bf, ptr, BUILD_ID_SIZE);
1258 err = BUILD_ID_SIZE;
1259 break;
1260 }
1261 }
1262 ptr += descsz;
1263 }
Dave Martin21916c32010-07-30 09:08:08 -03001264
1265out:
1266 return err;
1267}
1268
1269int filename__read_build_id(const char *filename, void *bf, size_t size)
1270{
1271 int fd, err = -1;
1272 Elf *elf;
1273
1274 if (size < BUILD_ID_SIZE)
1275 goto out;
1276
1277 fd = open(filename, O_RDONLY);
1278 if (fd < 0)
1279 goto out;
1280
1281 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
1282 if (elf == NULL) {
1283 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
1284 goto out_close;
1285 }
1286
1287 err = elf_read_build_id(elf, bf, size);
1288
Arnaldo Carvalho de Melo2643ce1142009-11-03 21:46:10 -02001289 elf_end(elf);
1290out_close:
1291 close(fd);
1292out:
1293 return err;
1294}
1295
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001296int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1297{
1298 int fd, err = -1;
1299
1300 if (size < BUILD_ID_SIZE)
1301 goto out;
1302
1303 fd = open(filename, O_RDONLY);
1304 if (fd < 0)
1305 goto out;
1306
1307 while (1) {
1308 char bf[BUFSIZ];
1309 GElf_Nhdr nhdr;
1310 int namesz, descsz;
1311
1312 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1313 break;
1314
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001315 namesz = NOTE_ALIGN(nhdr.n_namesz);
1316 descsz = NOTE_ALIGN(nhdr.n_descsz);
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001317 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1318 nhdr.n_namesz == sizeof("GNU")) {
1319 if (read(fd, bf, namesz) != namesz)
1320 break;
1321 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1322 if (read(fd, build_id,
1323 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1324 err = 0;
1325 break;
1326 }
1327 } else if (read(fd, bf, descsz) != descsz)
1328 break;
1329 } else {
1330 int n = namesz + descsz;
1331 if (read(fd, bf, n) != n)
1332 break;
1333 }
1334 }
1335 close(fd);
1336out:
1337 return err;
1338}
1339
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001340char dso__symtab_origin(const struct dso *self)
1341{
1342 static const char origin[] = {
1343 [DSO__ORIG_KERNEL] = 'k',
1344 [DSO__ORIG_JAVA_JIT] = 'j',
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02001345 [DSO__ORIG_BUILD_ID_CACHE] = 'B',
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001346 [DSO__ORIG_FEDORA] = 'f',
1347 [DSO__ORIG_UBUNTU] = 'u',
1348 [DSO__ORIG_BUILDID] = 'b',
1349 [DSO__ORIG_DSO] = 'd',
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001350 [DSO__ORIG_KMODULE] = 'K',
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001351 [DSO__ORIG_GUEST_KERNEL] = 'g',
1352 [DSO__ORIG_GUEST_KMODULE] = 'G',
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001353 };
1354
1355 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1356 return '!';
1357 return origin[self->origin];
1358}
1359
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001360int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001361{
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001362 int size = PATH_MAX;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001363 char *name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001364 int ret = -1;
1365 int fd;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001366 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001367 const char *root_dir;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001368
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001369 dso__set_loaded(self, map->type);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001370
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001371 if (self->kernel == DSO_TYPE_KERNEL)
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001372 return dso__load_kernel_sym(self, map, filter);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001373 else if (self->kernel == DSO_TYPE_GUEST_KERNEL)
1374 return dso__load_guest_kernel_sym(self, map, filter);
1375
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001376 if (map->groups && map->groups->machine)
1377 machine = map->groups->machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001378 else
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001379 machine = NULL;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001380
1381 name = malloc(size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001382 if (!name)
1383 return -1;
1384
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -03001385 self->adjust_symbols = 0;
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001386
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001387 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001388 ret = dso__load_perf_map(self, map, filter);
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001389 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1390 DSO__ORIG_NOT_FOUND;
1391 return ret;
1392 }
1393
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02001394 self->origin = DSO__ORIG_BUILD_ID_CACHE;
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001395 if (dso__build_id_filename(self, name, size) != NULL)
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02001396 goto open_file;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001397more:
1398 do {
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001399 self->origin++;
1400 switch (self->origin) {
1401 case DSO__ORIG_FEDORA:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001402 snprintf(name, size, "/usr/lib/debug%s.debug",
1403 self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001404 break;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001405 case DSO__ORIG_UBUNTU:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001406 snprintf(name, size, "/usr/lib/debug%s",
1407 self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001408 break;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001409 case DSO__ORIG_BUILDID:
Dave Martin21916c32010-07-30 09:08:08 -03001410 if (self->has_build_id) {
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001411 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
Dave Martin21916c32010-07-30 09:08:08 -03001412 build_id__sprintf(self->build_id,
1413 sizeof(self->build_id),
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001414 build_id_hex);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001415 snprintf(name, size,
1416 "/usr/lib/debug/.build-id/%.2s/%s.debug",
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001417 build_id_hex, build_id_hex + 2);
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001418 break;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001419 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001420 self->origin++;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001421 /* Fall thru */
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001422 case DSO__ORIG_DSO:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001423 snprintf(name, size, "%s", self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001424 break;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001425 case DSO__ORIG_GUEST_KMODULE:
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001426 if (map->groups && map->groups->machine)
1427 root_dir = map->groups->machine->root_dir;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001428 else
1429 root_dir = "";
1430 snprintf(name, size, "%s%s", root_dir, self->long_name);
1431 break;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001432
1433 default:
1434 goto out;
1435 }
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02001436open_file:
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001437 fd = open(name, O_RDONLY);
1438 } while (fd < 0);
1439
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001440 ret = dso__load_sym(self, map, name, fd, filter, 0);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001441 close(fd);
1442
1443 /*
1444 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1445 */
1446 if (!ret)
1447 goto more;
1448
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001449 if (ret > 0) {
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -02001450 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001451 if (nr_plt > 0)
1452 ret += nr_plt;
1453 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001454out:
1455 free(name);
Arnaldo Carvalho de Melo1340e6b2009-08-11 17:04:36 -03001456 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1457 return 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001458 return ret;
1459}
1460
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -02001461struct map *map_groups__find_by_name(struct map_groups *self,
1462 enum map_type type, const char *name)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001463{
1464 struct rb_node *nd;
1465
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -02001466 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001467 struct map *map = rb_entry(nd, struct map, rb_node);
1468
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001469 if (map->dso && strcmp(map->dso->short_name, name) == 0)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001470 return map;
1471 }
1472
1473 return NULL;
1474}
1475
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001476static int dso__kernel_module_get_build_id(struct dso *self,
1477 const char *root_dir)
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001478{
1479 char filename[PATH_MAX];
1480 /*
1481 * kernel module short names are of the form "[module]" and
1482 * we need just "module" here.
1483 */
1484 const char *name = self->short_name + 1;
1485
1486 snprintf(filename, sizeof(filename),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001487 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1488 root_dir, (int)strlen(name) - 1, name);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001489
1490 if (sysfs__read_build_id(filename, self->build_id,
1491 sizeof(self->build_id)) == 0)
1492 self->has_build_id = true;
1493
1494 return 0;
1495}
1496
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001497static int map_groups__set_modules_path_dir(struct map_groups *self,
1498 const char *dir_name)
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001499{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001500 struct dirent *dent;
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03001501 DIR *dir = opendir(dir_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001502
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001503 if (!dir) {
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03001504 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001505 return -1;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001506 }
1507
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001508 while ((dent = readdir(dir)) != NULL) {
1509 char path[PATH_MAX];
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001510 struct stat st;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001511
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001512 /*sshfs might return bad dent->d_type, so we have to stat*/
1513 sprintf(path, "%s/%s", dir_name, dent->d_name);
1514 if (stat(path, &st))
1515 continue;
1516
1517 if (S_ISDIR(st.st_mode)) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001518 if (!strcmp(dent->d_name, ".") ||
1519 !strcmp(dent->d_name, ".."))
1520 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001521
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001522 snprintf(path, sizeof(path), "%s/%s",
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03001523 dir_name, dent->d_name);
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001524 if (map_groups__set_modules_path_dir(self, path) < 0)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001525 goto failure;
1526 } else {
1527 char *dot = strrchr(dent->d_name, '.'),
1528 dso_name[PATH_MAX];
1529 struct map *map;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001530 char *long_name;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001531
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001532 if (dot == NULL || strcmp(dot, ".ko"))
1533 continue;
1534 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1535 (int)(dot - dent->d_name), dent->d_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001536
Arnaldo Carvalho de Meloa2a99e82009-10-05 14:26:18 -03001537 strxfrchar(dso_name, '-', '_');
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001538 map = map_groups__find_by_name(self, MAP__FUNCTION, dso_name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001539 if (map == NULL)
1540 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001541
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001542 snprintf(path, sizeof(path), "%s/%s",
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03001543 dir_name, dent->d_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001544
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001545 long_name = strdup(path);
1546 if (long_name == NULL)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001547 goto failure;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001548 dso__set_long_name(map->dso, long_name);
Arnaldo Carvalho de Melo6e406252010-07-29 15:11:30 -03001549 map->dso->lname_alloc = 1;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001550 dso__kernel_module_get_build_id(map->dso, "");
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001551 }
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001552 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001553
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001554 return 0;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001555failure:
1556 closedir(dir);
1557 return -1;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001558}
1559
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001560static char *get_kernel_version(const char *root_dir)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001561{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001562 char version[PATH_MAX];
1563 FILE *file;
1564 char *name, *tmp;
1565 const char *prefix = "Linux version ";
1566
1567 sprintf(version, "%s/proc/version", root_dir);
1568 file = fopen(version, "r");
1569 if (!file)
1570 return NULL;
1571
1572 version[0] = '\0';
1573 tmp = fgets(version, sizeof(version), file);
1574 fclose(file);
1575
1576 name = strstr(version, prefix);
1577 if (!name)
1578 return NULL;
1579 name += strlen(prefix);
1580 tmp = strchr(name, ' ');
1581 if (tmp)
1582 *tmp = '\0';
1583
1584 return strdup(name);
1585}
1586
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001587static int machine__set_modules_path(struct machine *self)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001588{
1589 char *version;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001590 char modules_path[PATH_MAX];
1591
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001592 version = get_kernel_version(self->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001593 if (!version)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001594 return -1;
1595
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001596 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001597 self->root_dir, version);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001598 free(version);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001599
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001600 return map_groups__set_modules_path_dir(&self->kmaps, modules_path);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001601}
1602
1603/*
1604 * Constructor variant for modules (where we know from /proc/modules where
1605 * they are loaded) and for vmlinux, where only after we load all the
1606 * symbols we'll know where it starts and ends.
1607 */
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001608static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001609{
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03001610 struct map *self = calloc(1, (sizeof(*self) +
1611 (dso->kernel ? sizeof(struct kmap) : 0)));
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001612 if (self != NULL) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001613 /*
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -02001614 * ->end will be filled after we load all the symbols
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001615 */
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001616 map__init(self, type, start, 0, 0, dso);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001617 }
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -02001618
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001619 return self;
1620}
1621
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001622struct map *machine__new_module(struct machine *self, u64 start,
1623 const char *filename)
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001624{
1625 struct map *map;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001626 struct dso *dso = __dsos__findnew(&self->kernel_dsos, filename);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001627
1628 if (dso == NULL)
1629 return NULL;
1630
1631 map = map__new2(start, dso, MAP__FUNCTION);
1632 if (map == NULL)
1633 return NULL;
1634
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001635 if (machine__is_host(self))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001636 dso->origin = DSO__ORIG_KMODULE;
1637 else
1638 dso->origin = DSO__ORIG_GUEST_KMODULE;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001639 map_groups__insert(&self->kmaps, map);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001640 return map;
1641}
1642
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001643static int machine__create_modules(struct machine *self)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001644{
1645 char *line = NULL;
1646 size_t n;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001647 FILE *file;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001648 struct map *map;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001649 const char *modules;
1650 char path[PATH_MAX];
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001651
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001652 if (machine__is_default_guest(self))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001653 modules = symbol_conf.default_guest_modules;
1654 else {
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001655 sprintf(path, "%s/proc/modules", self->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001656 modules = path;
1657 }
1658
1659 file = fopen(modules, "r");
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001660 if (file == NULL)
1661 return -1;
1662
1663 while (!feof(file)) {
1664 char name[PATH_MAX];
1665 u64 start;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001666 char *sep;
1667 int line_len;
1668
1669 line_len = getline(&line, &n, file);
1670 if (line_len < 0)
1671 break;
1672
1673 if (!line)
1674 goto out_failure;
1675
1676 line[--line_len] = '\0'; /* \n */
1677
1678 sep = strrchr(line, 'x');
1679 if (sep == NULL)
1680 continue;
1681
1682 hex2u64(sep + 1, &start);
1683
1684 sep = strchr(line, ' ');
1685 if (sep == NULL)
1686 continue;
1687
1688 *sep = '\0';
1689
1690 snprintf(name, sizeof(name), "[%s]", line);
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001691 map = machine__new_module(self, start, name);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001692 if (map == NULL)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001693 goto out_delete_line;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001694 dso__kernel_module_get_build_id(map->dso, self->root_dir);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001695 }
1696
1697 free(line);
1698 fclose(file);
1699
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001700 return machine__set_modules_path(self);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001701
1702out_delete_line:
1703 free(line);
1704out_failure:
1705 return -1;
1706}
1707
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -02001708static int dso__load_vmlinux(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001709 const char *vmlinux, symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001710{
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001711 int err = -1, fd;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001712
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001713 fd = open(vmlinux, O_RDONLY);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001714 if (fd < 0)
1715 return -1;
1716
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001717 dso__set_loaded(self, map->type);
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001718 err = dso__load_sym(self, map, vmlinux, fd, filter, 0);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001719 close(fd);
1720
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001721 if (err > 0)
1722 pr_debug("Using %s for symbols\n", vmlinux);
1723
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001724 return err;
1725}
1726
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001727int dso__load_vmlinux_path(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001728 symbol_filter_t filter)
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001729{
1730 int i, err = 0;
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001731 char *filename;
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001732
1733 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001734 vmlinux_path__nr_entries + 1);
1735
1736 filename = dso__build_id_filename(self, NULL, 0);
1737 if (filename != NULL) {
1738 err = dso__load_vmlinux(self, map, filename, filter);
1739 if (err > 0) {
1740 dso__set_long_name(self, filename);
1741 goto out;
1742 }
1743 free(filename);
1744 }
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001745
1746 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001747 err = dso__load_vmlinux(self, map, vmlinux_path[i], filter);
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001748 if (err > 0) {
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001749 dso__set_long_name(self, strdup(vmlinux_path[i]));
1750 break;
1751 }
1752 }
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001753out:
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001754 return err;
1755}
1756
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001757static int dso__load_kernel_sym(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001758 symbol_filter_t filter)
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001759{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001760 int err;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001761 const char *kallsyms_filename = NULL;
1762 char *kallsyms_allocated_filename = NULL;
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001763 /*
1764 * Step 1: if the user specified a vmlinux filename, use it and only
1765 * it, reporting errors to the user if it cannot be used.
1766 *
1767 * For instance, try to analyse an ARM perf.data file _without_ a
1768 * build-id, or if the user specifies the wrong path to the right
1769 * vmlinux file, obviously we can't fallback to another vmlinux (a
1770 * x86_86 one, on the machine where analysis is being performed, say),
1771 * or worse, /proc/kallsyms.
1772 *
1773 * If the specified file _has_ a build-id and there is a build-id
1774 * section in the perf.data file, we will still do the expected
1775 * validation in dso__load_vmlinux and will bail out if they don't
1776 * match.
1777 */
1778 if (symbol_conf.vmlinux_name != NULL) {
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001779 err = dso__load_vmlinux(self, map,
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001780 symbol_conf.vmlinux_name, filter);
Arnaldo Carvalho de Meloe7dadc02010-06-03 18:35:55 -03001781 if (err > 0) {
1782 dso__set_long_name(self,
1783 strdup(symbol_conf.vmlinux_name));
1784 goto out_fixup;
1785 }
1786 return err;
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001787 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001788
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001789 if (vmlinux_path != NULL) {
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001790 err = dso__load_vmlinux_path(self, map, filter);
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001791 if (err > 0)
1792 goto out_fixup;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001793 }
1794
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001795 /*
1796 * Say the kernel DSO was created when processing the build-id header table,
1797 * we have a build-id, so check if it is the same as the running kernel,
1798 * using it if it is.
1799 */
1800 if (self->has_build_id) {
1801 u8 kallsyms_build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001802 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001803
1804 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
Arnaldo Carvalho de Melo8d0591f62010-01-14 18:30:05 -02001805 sizeof(kallsyms_build_id)) == 0) {
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001806 if (dso__build_id_equal(self, kallsyms_build_id)) {
1807 kallsyms_filename = "/proc/kallsyms";
Arnaldo Carvalho de Melo8d0591f62010-01-14 18:30:05 -02001808 goto do_kallsyms;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001809 }
Arnaldo Carvalho de Melo8d0591f62010-01-14 18:30:05 -02001810 }
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001811 /*
1812 * Now look if we have it on the build-id cache in
1813 * $HOME/.debug/[kernel.kallsyms].
1814 */
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001815 build_id__sprintf(self->build_id, sizeof(self->build_id),
1816 sbuild_id);
1817
1818 if (asprintf(&kallsyms_allocated_filename,
1819 "%s/.debug/[kernel.kallsyms]/%s",
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001820 getenv("HOME"), sbuild_id) == -1) {
1821 pr_err("Not enough memory for kallsyms file lookup\n");
Arnaldo Carvalho de Melo8d0591f62010-01-14 18:30:05 -02001822 return -1;
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001823 }
Arnaldo Carvalho de Melo8d0591f62010-01-14 18:30:05 -02001824
Arnaldo Carvalho de Melo19fc2de2010-01-22 14:35:02 -02001825 kallsyms_filename = kallsyms_allocated_filename;
1826
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001827 if (access(kallsyms_filename, F_OK)) {
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001828 pr_err("No kallsyms or vmlinux with build-id %s "
1829 "was found\n", sbuild_id);
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001830 free(kallsyms_allocated_filename);
1831 return -1;
1832 }
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001833 } else {
1834 /*
1835 * Last resort, if we don't have a build-id and couldn't find
1836 * any vmlinux file, try the running kernel kallsyms table.
1837 */
1838 kallsyms_filename = "/proc/kallsyms";
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -02001839 }
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001840
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001841do_kallsyms:
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001842 err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001843 if (err > 0)
1844 pr_debug("Using %s for symbols\n", kallsyms_filename);
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001845 free(kallsyms_allocated_filename);
1846
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001847 if (err > 0) {
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001848out_fixup:
Arnaldo Carvalho de Meloe1c7c6a2010-01-22 14:35:01 -02001849 if (kallsyms_filename != NULL)
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001850 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001851 map__fixup_start(map);
1852 map__fixup_end(map);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001853 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001854
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001855 return err;
1856}
1857
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001858static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
1859 symbol_filter_t filter)
1860{
1861 int err;
1862 const char *kallsyms_filename = NULL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001863 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001864 char path[PATH_MAX];
1865
1866 if (!map->groups) {
1867 pr_debug("Guest kernel map hasn't the point to groups\n");
1868 return -1;
1869 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001870 machine = map->groups->machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001871
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001872 if (machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001873 /*
1874 * if the user specified a vmlinux filename, use it and only
1875 * it, reporting errors to the user if it cannot be used.
1876 * Or use file guest_kallsyms inputted by user on commandline
1877 */
1878 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1879 err = dso__load_vmlinux(self, map,
1880 symbol_conf.default_guest_vmlinux_name, filter);
1881 goto out_try_fixup;
1882 }
1883
1884 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1885 if (!kallsyms_filename)
1886 return -1;
1887 } else {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001888 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001889 kallsyms_filename = path;
1890 }
1891
1892 err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
1893 if (err > 0)
1894 pr_debug("Using %s for symbols\n", kallsyms_filename);
1895
1896out_try_fixup:
1897 if (err > 0) {
1898 if (kallsyms_filename != NULL) {
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -03001899 machine__mmap_name(machine, path, sizeof(path));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001900 dso__set_long_name(self, strdup(path));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001901 }
1902 map__fixup_start(map);
1903 map__fixup_end(map);
1904 }
1905
1906 return err;
1907}
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001908
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001909static void dsos__add(struct list_head *head, struct dso *dso)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001910{
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001911 list_add_tail(&dso->node, head);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001912}
1913
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001914static struct dso *dsos__find(struct list_head *head, const char *name)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001915{
1916 struct dso *pos;
1917
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001918 list_for_each_entry(pos, head, node)
Arnaldo Carvalho de Melocf4e5b02010-01-14 23:45:27 -02001919 if (strcmp(pos->long_name, name) == 0)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001920 return pos;
1921 return NULL;
1922}
1923
Arnaldo Carvalho de Meloa89e5abe2010-01-07 19:59:39 -02001924struct dso *__dsos__findnew(struct list_head *head, const char *name)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001925{
Arnaldo Carvalho de Meloa89e5abe2010-01-07 19:59:39 -02001926 struct dso *dso = dsos__find(head, name);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001927
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -02001928 if (!dso) {
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001929 dso = dso__new(name);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001930 if (dso != NULL) {
Arnaldo Carvalho de Meloa89e5abe2010-01-07 19:59:39 -02001931 dsos__add(head, dso);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001932 dso__set_basename(dso);
1933 }
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001934 }
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001935
1936 return dso;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001937}
1938
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001939size_t __dsos__fprintf(struct list_head *head, FILE *fp)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001940{
1941 struct dso *pos;
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001942 size_t ret = 0;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001943
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001944 list_for_each_entry(pos, head, node) {
1945 int i;
1946 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001947 ret += dso__fprintf(pos, i, fp);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001948 }
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001949
1950 return ret;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001951}
1952
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001953size_t machines__fprintf_dsos(struct rb_root *self, FILE *fp)
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001954{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001955 struct rb_node *nd;
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001956 size_t ret = 0;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001957
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001958 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001959 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001960 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
1961 ret += __dsos__fprintf(&pos->user_dsos, fp);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001962 }
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001963
1964 return ret;
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001965}
1966
Arnaldo Carvalho de Melo88d3d9b72010-01-14 23:45:30 -02001967static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1968 bool with_hits)
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001969{
1970 struct dso *pos;
1971 size_t ret = 0;
1972
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001973 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo88d3d9b72010-01-14 23:45:30 -02001974 if (with_hits && !pos->hit)
1975 continue;
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001976 ret += dso__fprintf_buildid(pos, fp);
Arnaldo Carvalho de Melo1124ba72009-11-16 21:45:25 -02001977 ret += fprintf(fp, " %s\n", pos->long_name);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001978 }
1979 return ret;
1980}
1981
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001982size_t machine__fprintf_dsos_buildid(struct machine *self, FILE *fp, bool with_hits)
1983{
1984 return __dsos__fprintf_buildid(&self->kernel_dsos, fp, with_hits) +
1985 __dsos__fprintf_buildid(&self->user_dsos, fp, with_hits);
1986}
1987
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001988size_t machines__fprintf_dsos_buildid(struct rb_root *self, FILE *fp, bool with_hits)
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001989{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001990 struct rb_node *nd;
1991 size_t ret = 0;
1992
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001993 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001994 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001995 ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001996 }
1997 return ret;
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001998}
1999
Arnaldo Carvalho de Melofd1d9082010-01-27 21:05:51 -02002000struct dso *dso__new_kernel(const char *name)
2001{
2002 struct dso *self = dso__new(name ?: "[kernel.kallsyms]");
2003
2004 if (self != NULL) {
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -03002005 dso__set_short_name(self, "[kernel]");
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002006 self->kernel = DSO_TYPE_KERNEL;
Arnaldo Carvalho de Melofd1d9082010-01-27 21:05:51 -02002007 }
2008
2009 return self;
2010}
2011
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002012static struct dso *dso__new_guest_kernel(struct machine *machine,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002013 const char *name)
Arnaldo Carvalho de Melofd1d9082010-01-27 21:05:51 -02002014{
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -03002015 char bf[PATH_MAX];
2016 struct dso *self = dso__new(name ?: machine__mmap_name(machine, bf, sizeof(bf)));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002017
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002018 if (self != NULL) {
2019 dso__set_short_name(self, "[guest.kernel]");
2020 self->kernel = DSO_TYPE_GUEST_KERNEL;
2021 }
2022
2023 return self;
2024}
2025
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002026void dso__read_running_kernel_build_id(struct dso *self, struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002027{
2028 char path[PATH_MAX];
2029
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002030 if (machine__is_default_guest(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002031 return;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002032 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002033 if (sysfs__read_build_id(path, self->build_id,
Arnaldo Carvalho de Melofd1d9082010-01-27 21:05:51 -02002034 sizeof(self->build_id)) == 0)
2035 self->has_build_id = true;
2036}
2037
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002038static struct dso *machine__create_kernel(struct machine *self)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002039{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002040 const char *vmlinux_name = NULL;
2041 struct dso *kernel;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002042
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002043 if (machine__is_host(self)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002044 vmlinux_name = symbol_conf.vmlinux_name;
2045 kernel = dso__new_kernel(vmlinux_name);
2046 } else {
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002047 if (machine__is_default_guest(self))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002048 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002049 kernel = dso__new_guest_kernel(self, vmlinux_name);
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -02002050 }
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002051
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002052 if (kernel != NULL) {
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002053 dso__read_running_kernel_build_id(kernel, self);
2054 dsos__add(&self->kernel_dsos, kernel);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002055 }
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002056 return kernel;
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002057}
2058
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002059int __machine__create_kernel_maps(struct machine *self, struct dso *kernel)
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002060{
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02002061 enum map_type type;
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002062
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02002063 for (type = 0; type < MAP__NR_TYPES; ++type) {
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02002064 struct kmap *kmap;
2065
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002066 self->vmlinux_maps[type] = map__new2(0, kernel, type);
2067 if (self->vmlinux_maps[type] == NULL)
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02002068 return -1;
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002069
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002070 self->vmlinux_maps[type]->map_ip =
2071 self->vmlinux_maps[type]->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02002072
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002073 kmap = map__kmap(self->vmlinux_maps[type]);
2074 kmap->kmaps = &self->kmaps;
2075 map_groups__insert(&self->kmaps, self->vmlinux_maps[type]);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002076 }
2077
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002078 return 0;
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02002079}
2080
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002081int machine__create_kernel_maps(struct machine *self)
2082{
2083 struct dso *kernel = machine__create_kernel(self);
2084
2085 if (kernel == NULL ||
2086 __machine__create_kernel_maps(self, kernel) < 0)
2087 return -1;
2088
2089 if (symbol_conf.use_modules && machine__create_modules(self) < 0)
2090 pr_debug("Problems creating module maps, continuing anyway...\n");
2091 /*
2092 * Now that we have all the maps created, just set the ->end of them:
2093 */
2094 map_groups__fixup_end(&self->kmaps);
2095 return 0;
2096}
2097
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002098static void vmlinux_path__exit(void)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02002099{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002100 while (--vmlinux_path__nr_entries >= 0) {
2101 free(vmlinux_path[vmlinux_path__nr_entries]);
2102 vmlinux_path[vmlinux_path__nr_entries] = NULL;
2103 }
2104
2105 free(vmlinux_path);
2106 vmlinux_path = NULL;
2107}
2108
2109static int vmlinux_path__init(void)
2110{
2111 struct utsname uts;
2112 char bf[PATH_MAX];
2113
2114 if (uname(&uts) < 0)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02002115 return -1;
2116
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002117 vmlinux_path = malloc(sizeof(char *) * 5);
2118 if (vmlinux_path == NULL)
2119 return -1;
2120
2121 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
2122 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2123 goto out_fail;
2124 ++vmlinux_path__nr_entries;
2125 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
2126 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2127 goto out_fail;
2128 ++vmlinux_path__nr_entries;
2129 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
2130 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2131 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2132 goto out_fail;
2133 ++vmlinux_path__nr_entries;
2134 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
2135 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2136 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2137 goto out_fail;
2138 ++vmlinux_path__nr_entries;
2139 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
2140 uts.release);
2141 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2142 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2143 goto out_fail;
2144 ++vmlinux_path__nr_entries;
2145
2146 return 0;
2147
2148out_fail:
2149 vmlinux_path__exit();
2150 return -1;
2151}
2152
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03002153size_t machine__fprintf_vmlinux_path(struct machine *self, FILE *fp)
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -03002154{
2155 int i;
2156 size_t printed = 0;
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03002157 struct dso *kdso = self->vmlinux_maps[MAP__FUNCTION]->dso;
2158
2159 if (kdso->has_build_id) {
2160 char filename[PATH_MAX];
2161 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
2162 printed += fprintf(fp, "[0] %s\n", filename);
2163 }
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -03002164
2165 for (i = 0; i < vmlinux_path__nr_entries; ++i)
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03002166 printed += fprintf(fp, "[%d] %s\n",
2167 i + kdso->has_build_id, vmlinux_path[i]);
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -03002168
2169 return printed;
2170}
2171
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002172static int setup_list(struct strlist **list, const char *list_str,
2173 const char *list_name)
2174{
2175 if (list_str == NULL)
2176 return 0;
2177
2178 *list = strlist__new(true, list_str);
2179 if (!*list) {
2180 pr_err("problems parsing %s list\n", list_name);
2181 return -1;
2182 }
2183 return 0;
2184}
2185
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02002186int symbol__init(void)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002187{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02002188 elf_version(EV_CURRENT);
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02002189 if (symbol_conf.sort_by_name)
2190 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2191 sizeof(struct symbol));
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02002192
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02002193 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002194 return -1;
2195
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02002196 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2197 pr_err("'.' is the only non valid --field-separator argument\n");
2198 return -1;
2199 }
2200
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002201 if (setup_list(&symbol_conf.dso_list,
2202 symbol_conf.dso_list_str, "dso") < 0)
2203 return -1;
2204
2205 if (setup_list(&symbol_conf.comm_list,
2206 symbol_conf.comm_list_str, "comm") < 0)
2207 goto out_free_dso_list;
2208
2209 if (setup_list(&symbol_conf.sym_list,
2210 symbol_conf.sym_list_str, "symbol") < 0)
2211 goto out_free_comm_list;
2212
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002213 return 0;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002214
2215out_free_dso_list:
2216 strlist__delete(symbol_conf.dso_list);
2217out_free_comm_list:
2218 strlist__delete(symbol_conf.comm_list);
2219 return -1;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002220}
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002221
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002222int machines__create_kernel_maps(struct rb_root *self, pid_t pid)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002223{
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002224 struct machine *machine = machines__findnew(self, pid);
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02002225
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002226 if (machine == NULL)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002227 return -1;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002228
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002229 return machine__create_kernel_maps(machine);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002230}
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03002231
2232static int hex(char ch)
2233{
2234 if ((ch >= '0') && (ch <= '9'))
2235 return ch - '0';
2236 if ((ch >= 'a') && (ch <= 'f'))
2237 return ch - 'a' + 10;
2238 if ((ch >= 'A') && (ch <= 'F'))
2239 return ch - 'A' + 10;
2240 return -1;
2241}
2242
2243/*
2244 * While we find nice hex chars, build a long_val.
2245 * Return number of chars processed.
2246 */
2247int hex2u64(const char *ptr, u64 *long_val)
2248{
2249 const char *p = ptr;
2250 *long_val = 0;
2251
2252 while (*p) {
2253 const int hex_val = hex(*p);
2254
2255 if (hex_val < 0)
2256 break;
2257
2258 *long_val = (*long_val << 4) | hex_val;
2259 p++;
2260 }
2261
2262 return p - ptr;
2263}
2264
2265char *strxfrchar(char *s, char from, char to)
2266{
2267 char *p = s;
2268
2269 while ((p = strchr(p, from)) != NULL)
2270 *p++ = to;
2271
2272 return s;
2273}
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002274
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002275int machines__create_guest_kernel_maps(struct rb_root *self)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002276{
2277 int ret = 0;
2278 struct dirent **namelist = NULL;
2279 int i, items = 0;
2280 char path[PATH_MAX];
2281 pid_t pid;
2282
2283 if (symbol_conf.default_guest_vmlinux_name ||
2284 symbol_conf.default_guest_modules ||
2285 symbol_conf.default_guest_kallsyms) {
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002286 machines__create_kernel_maps(self, DEFAULT_GUEST_KERNEL_ID);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002287 }
2288
2289 if (symbol_conf.guestmount) {
2290 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
2291 if (items <= 0)
2292 return -ENOENT;
2293 for (i = 0; i < items; i++) {
2294 if (!isdigit(namelist[i]->d_name[0])) {
2295 /* Filter out . and .. */
2296 continue;
2297 }
2298 pid = atoi(namelist[i]->d_name);
2299 sprintf(path, "%s/%s/proc/kallsyms",
2300 symbol_conf.guestmount,
2301 namelist[i]->d_name);
2302 ret = access(path, R_OK);
2303 if (ret) {
2304 pr_debug("Can't access file %s\n", path);
2305 goto failure;
2306 }
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002307 machines__create_kernel_maps(self, pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002308 }
2309failure:
2310 free(namelist);
2311 }
2312
2313 return ret;
2314}
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002315
2316int machine__load_kallsyms(struct machine *self, const char *filename,
2317 enum map_type type, symbol_filter_t filter)
2318{
2319 struct map *map = self->vmlinux_maps[type];
2320 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
2321
2322 if (ret > 0) {
2323 dso__set_loaded(map->dso, type);
2324 /*
2325 * Since /proc/kallsyms will have multiple sessions for the
2326 * kernel, with modules between them, fixup the end of all
2327 * sections.
2328 */
2329 __map_groups__fixup_end(&self->kmaps, type);
2330 }
2331
2332 return ret;
2333}
2334
2335int machine__load_vmlinux_path(struct machine *self, enum map_type type,
2336 symbol_filter_t filter)
2337{
2338 struct map *map = self->vmlinux_maps[type];
2339 int ret = dso__load_vmlinux_path(map->dso, map, filter);
2340
2341 if (ret > 0) {
2342 dso__set_loaded(map->dso, type);
2343 map__reloc_vmlinux(map);
2344 }
2345
2346 return ret;
2347}