blob: 971d0a05d6b44c85c7490f97bcd159acd4a87b25 [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 Meloa2928c42009-05-28 14:55:04 -030015#include "symbol.h"
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -030016#include "strlist.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030017
18#include <libelf.h>
19#include <gelf.h>
20#include <elf.h>
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -020021#include <limits.h>
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030022#include <sys/utsname.h>
Peter Zijlstra2cdbc462009-08-05 14:05:16 +020023
Arnaldo Carvalho de Meloc12e15e2009-11-21 14:31:25 -020024#ifndef NT_GNU_BUILD_ID
25#define NT_GNU_BUILD_ID 3
26#endif
27
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -020028static void dsos__add(struct list_head *head, struct dso *dso);
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020029static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020030static int dso__load_kernel_sym(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -020031 symbol_filter_t filter);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080032static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
33 symbol_filter_t filter);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -020034static int vmlinux_path__nr_entries;
35static char **vmlinux_path;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030036
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -020037struct symbol_conf symbol_conf = {
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -020038 .exclude_other = true,
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020039 .use_modules = true,
40 .try_vmlinux_path = true,
41};
42
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020043bool dso__loaded(const struct dso *self, enum map_type type)
44{
45 return self->loaded & (1 << type);
46}
47
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -020048bool dso__sorted_by_name(const struct dso *self, enum map_type type)
49{
50 return self->sorted_by_name & (1 << type);
51}
52
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -020053static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
54{
55 self->sorted_by_name |= (1 << type);
56}
57
Arnaldo Carvalho de Melo36a3e642010-01-04 16:19:27 -020058bool symbol_type__is_a(char symbol_type, enum map_type map_type)
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020059{
60 switch (map_type) {
61 case MAP__FUNCTION:
62 return symbol_type == 'T' || symbol_type == 'W';
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -020063 case MAP__VARIABLE:
64 return symbol_type == 'D' || symbol_type == 'd';
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020065 default:
66 return false;
67 }
68}
69
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -020070static void symbols__fixup_end(struct rb_root *self)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030071{
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -020072 struct rb_node *nd, *prevnd = rb_first(self);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030073 struct symbol *curr, *prev;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030074
75 if (prevnd == NULL)
76 return;
77
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030078 curr = rb_entry(prevnd, struct symbol, rb_node);
79
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030080 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030081 prev = curr;
82 curr = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030083
84 if (prev->end == prev->start)
85 prev->end = curr->start - 1;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030086 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030087
88 /* Last entry */
89 if (curr->end == curr->start)
90 curr->end = roundup(curr->start, 4096);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030091}
92
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020093static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030094{
95 struct map *prev, *curr;
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020096 struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030097
98 if (prevnd == NULL)
99 return;
100
101 curr = rb_entry(prevnd, struct map, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300102
103 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
104 prev = curr;
105 curr = rb_entry(nd, struct map, rb_node);
106 prev->end = curr->start - 1;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300107 }
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -0200108
109 /*
110 * We still haven't the actual symbols, so guess the
111 * last map final address.
112 */
113 curr->end = ~0UL;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300114}
115
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200116static void map_groups__fixup_end(struct map_groups *self)
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -0200117{
118 int i;
119 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200120 __map_groups__fixup_end(self, i);
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -0200121}
122
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200123static struct symbol *symbol__new(u64 start, u64 len, const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300124{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300125 size_t namelen = strlen(name) + 1;
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -0300126 struct symbol *self = calloc(1, (symbol_conf.priv_size +
127 sizeof(*self) + namelen));
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200128 if (self == NULL)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200129 return NULL;
130
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200131 if (symbol_conf.priv_size)
132 self = ((void *)self) + symbol_conf.priv_size;
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200133
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300134 self->start = start;
135 self->end = len ? start + len - 1 : start;
136 self->namelen = namelen - 1;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200137
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -0200138 pr_debug4("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200139
Ingo Molnar0b73da32009-06-06 15:48:52 +0200140 memcpy(self->name, name, namelen);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300141
142 return self;
143}
144
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -0300145void symbol__delete(struct symbol *self)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300146{
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200147 free(((void *)self) - symbol_conf.priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300148}
149
150static size_t symbol__fprintf(struct symbol *self, FILE *fp)
151{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300152 return fprintf(fp, " %llx-%llx %s\n",
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300153 self->start, self->end, self->name);
154}
155
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200156void dso__set_long_name(struct dso *self, char *name)
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200157{
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -0200158 if (name == NULL)
159 return;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200160 self->long_name = name;
161 self->long_name_len = strlen(name);
162}
163
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -0300164static void dso__set_short_name(struct dso *self, const char *name)
165{
166 if (name == NULL)
167 return;
168 self->short_name = name;
169 self->short_name_len = strlen(name);
170}
171
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200172static void dso__set_basename(struct dso *self)
173{
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -0300174 dso__set_short_name(self, basename(self->long_name));
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200175}
176
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200177struct dso *dso__new(const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300178{
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -0300179 struct dso *self = calloc(1, sizeof(*self) + strlen(name) + 1);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300180
181 if (self != NULL) {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200182 int i;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300183 strcpy(self->name, name);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200184 dso__set_long_name(self, self->name);
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -0300185 dso__set_short_name(self, self->name);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200186 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200187 self->symbols[i] = self->symbol_names[i] = RB_ROOT;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300188 self->slen_calculated = 0;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300189 self->origin = DSO__ORIG_NOT_FOUND;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200190 self->loaded = 0;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200191 self->sorted_by_name = 0;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200192 self->has_build_id = 0;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800193 self->kernel = DSO_TYPE_USER;
Masami Hiramatsu0ab061c2010-04-21 15:56:16 -0400194 INIT_LIST_HEAD(&self->node);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300195 }
196
197 return self;
198}
199
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200200static void symbols__delete(struct rb_root *self)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300201{
202 struct symbol *pos;
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200203 struct rb_node *next = rb_first(self);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300204
205 while (next) {
206 pos = rb_entry(next, struct symbol, rb_node);
207 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200208 rb_erase(&pos->rb_node, self);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200209 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300210 }
211}
212
213void dso__delete(struct dso *self)
214{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200215 int i;
216 for (i = 0; i < MAP__NR_TYPES; ++i)
217 symbols__delete(&self->symbols[i]);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300218 if (self->long_name != self->name)
219 free(self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300220 free(self);
221}
222
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200223void dso__set_build_id(struct dso *self, void *build_id)
224{
225 memcpy(self->build_id, build_id, sizeof(self->build_id));
226 self->has_build_id = 1;
227}
228
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200229static void symbols__insert(struct rb_root *self, struct symbol *sym)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300230{
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200231 struct rb_node **p = &self->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300232 struct rb_node *parent = NULL;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000233 const u64 ip = sym->start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300234 struct symbol *s;
235
236 while (*p != NULL) {
237 parent = *p;
238 s = rb_entry(parent, struct symbol, rb_node);
239 if (ip < s->start)
240 p = &(*p)->rb_left;
241 else
242 p = &(*p)->rb_right;
243 }
244 rb_link_node(&sym->rb_node, parent, p);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200245 rb_insert_color(&sym->rb_node, self);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300246}
247
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200248static struct symbol *symbols__find(struct rb_root *self, u64 ip)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300249{
250 struct rb_node *n;
251
252 if (self == NULL)
253 return NULL;
254
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200255 n = self->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300256
257 while (n) {
258 struct symbol *s = rb_entry(n, struct symbol, rb_node);
259
260 if (ip < s->start)
261 n = n->rb_left;
262 else if (ip > s->end)
263 n = n->rb_right;
264 else
265 return s;
266 }
267
268 return NULL;
269}
270
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200271struct symbol_name_rb_node {
272 struct rb_node rb_node;
273 struct symbol sym;
274};
275
276static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
277{
278 struct rb_node **p = &self->rb_node;
279 struct rb_node *parent = NULL;
280 struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
281
282 while (*p != NULL) {
283 parent = *p;
284 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
285 if (strcmp(sym->name, s->sym.name) < 0)
286 p = &(*p)->rb_left;
287 else
288 p = &(*p)->rb_right;
289 }
290 rb_link_node(&symn->rb_node, parent, p);
291 rb_insert_color(&symn->rb_node, self);
292}
293
294static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
295{
296 struct rb_node *nd;
297
298 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
299 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
300 symbols__insert_by_name(self, pos);
301 }
302}
303
304static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
305{
306 struct rb_node *n;
307
308 if (self == NULL)
309 return NULL;
310
311 n = self->rb_node;
312
313 while (n) {
314 struct symbol_name_rb_node *s;
315 int cmp;
316
317 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
318 cmp = strcmp(name, s->sym.name);
319
320 if (cmp < 0)
321 n = n->rb_left;
322 else if (cmp > 0)
323 n = n->rb_right;
324 else
325 return &s->sym;
326 }
327
328 return NULL;
329}
330
331struct symbol *dso__find_symbol(struct dso *self,
332 enum map_type type, u64 addr)
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200333{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200334 return symbols__find(&self->symbols[type], addr);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200335}
336
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200337struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
338 const char *name)
339{
340 return symbols__find_by_name(&self->symbol_names[type], name);
341}
342
343void dso__sort_by_name(struct dso *self, enum map_type type)
344{
345 dso__set_sorted_by_name(self, type);
346 return symbols__sort_by_name(&self->symbol_names[type],
347 &self->symbols[type]);
348}
349
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200350int build_id__sprintf(const u8 *self, int len, char *bf)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200351{
352 char *bid = bf;
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200353 const u8 *raw = self;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200354 int i;
355
356 for (i = 0; i < len; ++i) {
357 sprintf(bid, "%02x", *raw);
358 ++raw;
359 bid += 2;
360 }
361
362 return raw - self;
363}
364
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200365size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300366{
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200367 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200368
369 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200370 return fprintf(fp, "%s", sbuild_id);
371}
372
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200373size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200374{
375 struct rb_node *nd;
376 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
377
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -0300378 if (self->short_name != self->long_name)
379 ret += fprintf(fp, "%s, ", self->long_name);
380 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
381 self->loaded ? "" : "NOT ");
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200382 ret += dso__fprintf_buildid(self, fp);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200383 ret += fprintf(fp, ")\n");
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200384 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
385 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
386 ret += symbol__fprintf(pos, fp);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300387 }
388
389 return ret;
390}
391
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200392int kallsyms__parse(const char *filename, void *arg,
393 int (*process_symbol)(void *arg, const char *name,
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200394 char type, u64 start))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300395{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300396 char *line = NULL;
397 size_t n;
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200398 int err = 0;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200399 FILE *file = fopen(filename, "r");
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300400
401 if (file == NULL)
402 goto out_failure;
403
404 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000405 u64 start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300406 int line_len, len;
407 char symbol_type;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300408 char *symbol_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300409
410 line_len = getline(&line, &n, file);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800411 if (line_len < 0 || !line)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300412 break;
413
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300414 line[--line_len] = '\0'; /* \n */
415
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300416 len = hex2u64(line, &start);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300417
418 len++;
419 if (len + 2 >= line_len)
420 continue;
421
422 symbol_type = toupper(line[len]);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300423 symbol_name = line + len + 2;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300424
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200425 err = process_symbol(arg, symbol_name, symbol_type, start);
426 if (err)
427 break;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300428 }
429
430 free(line);
431 fclose(file);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200432 return err;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300433
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300434out_failure:
435 return -1;
436}
437
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200438struct process_kallsyms_args {
439 struct map *map;
440 struct dso *dso;
441};
442
443static int map__process_kallsym_symbol(void *arg, const char *name,
444 char type, u64 start)
445{
446 struct symbol *sym;
447 struct process_kallsyms_args *a = arg;
448 struct rb_root *root = &a->dso->symbols[a->map->type];
449
450 if (!symbol_type__is_a(type, a->map->type))
451 return 0;
452
453 /*
454 * Will fix up the end later, when we have all symbols sorted.
455 */
456 sym = symbol__new(start, 0, name);
457
458 if (sym == NULL)
459 return -ENOMEM;
460 /*
461 * We will pass the symbols to the filter later, in
462 * map__split_kallsyms, when we have split the maps per module
463 */
464 symbols__insert(root, sym);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800465
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200466 return 0;
467}
468
469/*
470 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
471 * so that we can in the next step set the symbol ->end address and then
472 * call kernel_maps__split_kallsyms.
473 */
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200474static int dso__load_all_kallsyms(struct dso *self, const char *filename,
475 struct map *map)
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200476{
477 struct process_kallsyms_args args = { .map = map, .dso = self, };
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200478 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200479}
480
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300481/*
482 * Split the symbols into maps, making sure there are no overlaps, i.e. the
483 * kernel range is broken in several maps, named [kernel].N, as we don't have
484 * the original ELF section names vmlinux have.
485 */
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200486static int dso__split_kallsyms(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200487 symbol_filter_t filter)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300488{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200489 struct map_groups *kmaps = map__kmap(map)->kmaps;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300490 struct machine *machine = kmaps->machine;
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200491 struct map *curr_map = map;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300492 struct symbol *pos;
493 int count = 0;
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200494 struct rb_root *root = &self->symbols[map->type];
495 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300496 int kernel_range = 0;
497
498 while (next) {
499 char *module;
500
501 pos = rb_entry(next, struct symbol, rb_node);
502 next = rb_next(&pos->rb_node);
503
504 module = strchr(pos->name, '\t');
505 if (module) {
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200506 if (!symbol_conf.use_modules)
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200507 goto discard_symbol;
508
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300509 *module++ = '\0';
510
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200511 if (strcmp(curr_map->dso->short_name, module)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800512 if (curr_map != map &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300513 self->kernel == DSO_TYPE_GUEST_KERNEL &&
514 machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800515 /*
516 * We assume all symbols of a module are
517 * continuous in * kallsyms, so curr_map
518 * points to a module and all its
519 * symbols are in its kmap. Mark it as
520 * loaded.
521 */
522 dso__set_loaded(curr_map->dso,
523 curr_map->type);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300524 }
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200525
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800526 curr_map = map_groups__find_by_name(kmaps,
527 map->type, module);
528 if (curr_map == NULL) {
Arnaldo Carvalho de Melo2f519032010-05-17 17:57:59 -0300529 pr_debug("%s/proc/{kallsyms,modules} "
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800530 "inconsistency while looking "
531 "for \"%s\" module!\n",
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300532 machine->root_dir, module);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800533 curr_map = map;
534 goto discard_symbol;
535 }
536
537 if (curr_map->dso->loaded &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300538 !machine__is_default_guest(machine))
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200539 goto discard_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300540 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300541 /*
542 * So that we look just like we get from .ko files,
543 * i.e. not prelinked, relative to map->start.
544 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200545 pos->start = curr_map->map_ip(curr_map, pos->start);
546 pos->end = curr_map->map_ip(curr_map, pos->end);
547 } else if (curr_map != map) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300548 char dso_name[PATH_MAX];
549 struct dso *dso;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300550
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800551 if (self->kernel == DSO_TYPE_GUEST_KERNEL)
552 snprintf(dso_name, sizeof(dso_name),
553 "[guest.kernel].%d",
554 kernel_range++);
555 else
556 snprintf(dso_name, sizeof(dso_name),
557 "[kernel].%d",
558 kernel_range++);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300559
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200560 dso = dso__new(dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300561 if (dso == NULL)
562 return -1;
563
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800564 dso->kernel = self->kernel;
565
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200566 curr_map = map__new2(pos->start, dso, map->type);
Zhang, Yanmin37fe5fc2010-02-25 11:00:51 +0800567 if (curr_map == NULL) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300568 dso__delete(dso);
569 return -1;
570 }
571
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200572 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200573 map_groups__insert(kmaps, curr_map);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300574 ++kernel_range;
575 }
576
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200577 if (filter && filter(curr_map, pos)) {
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200578discard_symbol: rb_erase(&pos->rb_node, root);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200579 symbol__delete(pos);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300580 } else {
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200581 if (curr_map != map) {
582 rb_erase(&pos->rb_node, root);
583 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300584 }
Mike Galbraith9974f492009-07-02 08:05:58 +0200585 count++;
586 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300587 }
588
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800589 if (curr_map != map &&
590 self->kernel == DSO_TYPE_GUEST_KERNEL &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300591 machine__is_default_guest(kmaps->machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800592 dso__set_loaded(curr_map->dso, curr_map->type);
593 }
594
Mike Galbraith9974f492009-07-02 08:05:58 +0200595 return count;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300596}
597
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200598int dso__load_kallsyms(struct dso *self, const char *filename,
599 struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300600{
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200601 if (dso__load_all_kallsyms(self, filename, map) < 0)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300602 return -1;
603
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200604 symbols__fixup_end(&self->symbols[map->type]);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800605 if (self->kernel == DSO_TYPE_GUEST_KERNEL)
606 self->origin = DSO__ORIG_GUEST_KERNEL;
607 else
608 self->origin = DSO__ORIG_KERNEL;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300609
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200610 return dso__split_kallsyms(self, map, filter);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300611}
612
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300613static int dso__load_perf_map(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200614 symbol_filter_t filter)
Pekka Enberg80d496b2009-06-08 21:12:48 +0300615{
616 char *line = NULL;
617 size_t n;
618 FILE *file;
619 int nr_syms = 0;
620
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300621 file = fopen(self->long_name, "r");
Pekka Enberg80d496b2009-06-08 21:12:48 +0300622 if (file == NULL)
623 goto out_failure;
624
625 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000626 u64 start, size;
Pekka Enberg80d496b2009-06-08 21:12:48 +0300627 struct symbol *sym;
628 int line_len, len;
629
630 line_len = getline(&line, &n, file);
631 if (line_len < 0)
632 break;
633
634 if (!line)
635 goto out_failure;
636
637 line[--line_len] = '\0'; /* \n */
638
639 len = hex2u64(line, &start);
640
641 len++;
642 if (len + 2 >= line_len)
643 continue;
644
645 len += hex2u64(line + len, &size);
646
647 len++;
648 if (len + 2 >= line_len)
649 continue;
650
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200651 sym = symbol__new(start, size, line + len);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300652
653 if (sym == NULL)
654 goto out_delete_line;
655
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300656 if (filter && filter(map, sym))
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200657 symbol__delete(sym);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300658 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200659 symbols__insert(&self->symbols[map->type], sym);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300660 nr_syms++;
661 }
662 }
663
664 free(line);
665 fclose(file);
666
667 return nr_syms;
668
669out_delete_line:
670 free(line);
671out_failure:
672 return -1;
673}
674
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300675/**
676 * elf_symtab__for_each_symbol - iterate thru all the symbols
677 *
678 * @self: struct elf_symtab instance to iterate
Ingo Molnar83a09442009-08-15 12:26:57 +0200679 * @idx: uint32_t idx
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300680 * @sym: GElf_Sym iterator
681 */
Ingo Molnar83a09442009-08-15 12:26:57 +0200682#define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
683 for (idx = 0, gelf_getsym(syms, idx, &sym);\
684 idx < nr_syms; \
685 idx++, gelf_getsym(syms, idx, &sym))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300686
687static inline uint8_t elf_sym__type(const GElf_Sym *sym)
688{
689 return GELF_ST_TYPE(sym->st_info);
690}
691
692static inline int elf_sym__is_function(const GElf_Sym *sym)
693{
694 return elf_sym__type(sym) == STT_FUNC &&
695 sym->st_name != 0 &&
Arnaldo Carvalho de Melo81833132009-10-05 23:35:03 -0300696 sym->st_shndx != SHN_UNDEF;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300697}
698
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200699static inline bool elf_sym__is_object(const GElf_Sym *sym)
700{
701 return elf_sym__type(sym) == STT_OBJECT &&
702 sym->st_name != 0 &&
703 sym->st_shndx != SHN_UNDEF;
704}
705
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200706static inline int elf_sym__is_label(const GElf_Sym *sym)
707{
708 return elf_sym__type(sym) == STT_NOTYPE &&
709 sym->st_name != 0 &&
710 sym->st_shndx != SHN_UNDEF &&
711 sym->st_shndx != SHN_ABS;
712}
713
714static inline const char *elf_sec__name(const GElf_Shdr *shdr,
715 const Elf_Data *secstrs)
716{
717 return secstrs->d_buf + shdr->sh_name;
718}
719
720static inline int elf_sec__is_text(const GElf_Shdr *shdr,
721 const Elf_Data *secstrs)
722{
723 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
724}
725
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200726static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
727 const Elf_Data *secstrs)
728{
729 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
730}
731
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300732static inline const char *elf_sym__name(const GElf_Sym *sym,
733 const Elf_Data *symstrs)
734{
735 return symstrs->d_buf + sym->st_name;
736}
737
738static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
739 GElf_Shdr *shp, const char *name,
Ingo Molnar83a09442009-08-15 12:26:57 +0200740 size_t *idx)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300741{
742 Elf_Scn *sec = NULL;
743 size_t cnt = 1;
744
745 while ((sec = elf_nextscn(elf, sec)) != NULL) {
746 char *str;
747
748 gelf_getshdr(sec, shp);
749 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
750 if (!strcmp(name, str)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200751 if (idx)
752 *idx = cnt;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300753 break;
754 }
755 ++cnt;
756 }
757
758 return sec;
759}
760
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300761#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
762 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
763 idx < nr_entries; \
764 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
765
766#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
767 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
768 idx < nr_entries; \
769 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
770
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300771/*
772 * We need to check if we have a .dynsym, so that we can handle the
773 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
774 * .dynsym or .symtab).
775 * And always look at the original dso, not at debuginfo packages, that
776 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
777 */
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200778static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
779 symbol_filter_t filter)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300780{
781 uint32_t nr_rel_entries, idx;
782 GElf_Sym sym;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000783 u64 plt_offset;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300784 GElf_Shdr shdr_plt;
785 struct symbol *f;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300786 GElf_Shdr shdr_rel_plt, shdr_dynsym;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300787 Elf_Data *reldata, *syms, *symstrs;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300788 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
789 size_t dynsym_idx;
790 GElf_Ehdr ehdr;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300791 char sympltname[1024];
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300792 Elf *elf;
793 int nr = 0, symidx, fd, err = 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300794
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300795 fd = open(self->long_name, O_RDONLY);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300796 if (fd < 0)
797 goto out;
798
Marti Raudsepp84087122009-10-24 19:10:36 +0300799 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300800 if (elf == NULL)
801 goto out_close;
802
803 if (gelf_getehdr(elf, &ehdr) == NULL)
804 goto out_elf_end;
805
806 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
807 ".dynsym", &dynsym_idx);
808 if (scn_dynsym == NULL)
809 goto out_elf_end;
810
811 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300812 ".rela.plt", NULL);
813 if (scn_plt_rel == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300814 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300815 ".rel.plt", NULL);
816 if (scn_plt_rel == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300817 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300818 }
819
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300820 err = -1;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300821
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300822 if (shdr_rel_plt.sh_link != dynsym_idx)
823 goto out_elf_end;
824
825 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
826 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300827
828 /*
Ingo Molnar83a09442009-08-15 12:26:57 +0200829 * Fetch the relocation section to find the idxes to the GOT
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300830 * and the symbols in the .dynsym they refer to.
831 */
832 reldata = elf_getdata(scn_plt_rel, NULL);
833 if (reldata == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300834 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300835
836 syms = elf_getdata(scn_dynsym, NULL);
837 if (syms == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300838 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300839
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300840 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300841 if (scn_symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300842 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300843
844 symstrs = elf_getdata(scn_symstrs, NULL);
845 if (symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300846 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300847
848 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
849 plt_offset = shdr_plt.sh_offset;
850
851 if (shdr_rel_plt.sh_type == SHT_RELA) {
852 GElf_Rela pos_mem, *pos;
853
854 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
855 nr_rel_entries) {
856 symidx = GELF_R_SYM(pos->r_info);
857 plt_offset += shdr_plt.sh_entsize;
858 gelf_getsym(syms, symidx, &sym);
859 snprintf(sympltname, sizeof(sympltname),
860 "%s@plt", elf_sym__name(&sym, symstrs));
861
862 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200863 sympltname);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300864 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300865 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300866
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200867 if (filter && filter(map, f))
868 symbol__delete(f);
869 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200870 symbols__insert(&self->symbols[map->type], f);
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200871 ++nr;
872 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300873 }
874 } else if (shdr_rel_plt.sh_type == SHT_REL) {
875 GElf_Rel pos_mem, *pos;
876 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
877 nr_rel_entries) {
878 symidx = GELF_R_SYM(pos->r_info);
879 plt_offset += shdr_plt.sh_entsize;
880 gelf_getsym(syms, symidx, &sym);
881 snprintf(sympltname, sizeof(sympltname),
882 "%s@plt", elf_sym__name(&sym, symstrs));
883
884 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200885 sympltname);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300886 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300887 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300888
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200889 if (filter && filter(map, f))
890 symbol__delete(f);
891 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200892 symbols__insert(&self->symbols[map->type], f);
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200893 ++nr;
894 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300895 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300896 }
897
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300898 err = 0;
899out_elf_end:
900 elf_end(elf);
901out_close:
902 close(fd);
903
904 if (err == 0)
905 return nr;
906out:
Arnaldo Carvalho de Melofe2197b2010-03-11 20:12:40 -0300907 pr_debug("%s: problems reading %s PLT info.\n",
908 __func__, self->long_name);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300909 return 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300910}
911
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -0200912static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
913{
914 switch (type) {
915 case MAP__FUNCTION:
916 return elf_sym__is_function(self);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200917 case MAP__VARIABLE:
918 return elf_sym__is_object(self);
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -0200919 default:
920 return false;
921 }
922}
923
924static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
925{
926 switch (type) {
927 case MAP__FUNCTION:
928 return elf_sec__is_text(self, secstrs);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200929 case MAP__VARIABLE:
930 return elf_sec__is_data(self, secstrs);
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -0200931 default:
932 return false;
933 }
934}
935
Eric B Munson70c38562010-06-14 14:56:33 +0100936static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
937{
938 Elf_Scn *sec = NULL;
939 GElf_Shdr shdr;
940 size_t cnt = 1;
941
942 while ((sec = elf_nextscn(elf, sec)) != NULL) {
943 gelf_getshdr(sec, &shdr);
944
945 if ((addr >= shdr.sh_addr) &&
946 (addr < (shdr.sh_addr + shdr.sh_size)))
947 return cnt;
948
949 ++cnt;
950 }
951
952 return -1;
953}
954
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200955static int dso__load_sym(struct dso *self, struct map *map, const char *name,
956 int fd, symbol_filter_t filter, int kmodule)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300957{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200958 struct kmap *kmap = self->kernel ? map__kmap(map) : NULL;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300959 struct map *curr_map = map;
960 struct dso *curr_dso = self;
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200961 Elf_Data *symstrs, *secstrs;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300962 uint32_t nr_syms;
963 int err = -1;
Ingo Molnar83a09442009-08-15 12:26:57 +0200964 uint32_t idx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300965 GElf_Ehdr ehdr;
Eric B Munson70c38562010-06-14 14:56:33 +0100966 GElf_Shdr shdr, opdshdr;
967 Elf_Data *syms, *opddata = NULL;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300968 GElf_Sym sym;
Eric B Munson70c38562010-06-14 14:56:33 +0100969 Elf_Scn *sec, *sec_strndx, *opdsec;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300970 Elf *elf;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300971 int nr = 0;
Eric B Munson70c38562010-06-14 14:56:33 +0100972 size_t opdidx = 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300973
Marti Raudsepp84087122009-10-24 19:10:36 +0300974 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300975 if (elf == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200976 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300977 goto out_close;
978 }
979
980 if (gelf_getehdr(elf, &ehdr) == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200981 pr_err("%s: cannot get elf header.\n", __func__);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300982 goto out_elf_end;
983 }
984
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300985 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
986 if (sec == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300987 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
988 if (sec == NULL)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300989 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300990 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300991
Eric B Munson70c38562010-06-14 14:56:33 +0100992 opdsec = elf_section_by_name(elf, &ehdr, &opdshdr, ".opd", &opdidx);
993 if (opdsec)
994 opddata = elf_rawdata(opdsec, NULL);
995
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300996 syms = elf_getdata(sec, NULL);
997 if (syms == NULL)
998 goto out_elf_end;
999
1000 sec = elf_getscn(elf, shdr.sh_link);
1001 if (sec == NULL)
1002 goto out_elf_end;
1003
1004 symstrs = elf_getdata(sec, NULL);
1005 if (symstrs == NULL)
1006 goto out_elf_end;
1007
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001008 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
1009 if (sec_strndx == NULL)
1010 goto out_elf_end;
1011
1012 secstrs = elf_getdata(sec_strndx, NULL);
Stoyan Gaydarov9b30a262009-07-30 05:25:29 -05001013 if (secstrs == NULL)
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001014 goto out_elf_end;
1015
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001016 nr_syms = shdr.sh_size / shdr.sh_entsize;
1017
Arjan van de Vene9fbc9d2009-06-06 21:22:33 +02001018 memset(&sym, 0, sizeof(sym));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001019 if (self->kernel == DSO_TYPE_USER) {
Mike Galbraithd20ff6b2009-07-20 14:01:38 +02001020 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -03001021 elf_section_by_name(elf, &ehdr, &shdr,
1022 ".gnu.prelink_undo",
1023 NULL) != NULL);
Mike Galbraithd20ff6b2009-07-20 14:01:38 +02001024 } else self->adjust_symbols = 0;
1025
Ingo Molnar83a09442009-08-15 12:26:57 +02001026 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001027 struct symbol *f;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001028 const char *elf_name = elf_sym__name(&sym, symstrs);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001029 char *demangled = NULL;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001030 int is_label = elf_sym__is_label(&sym);
1031 const char *section_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001032
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001033 if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
1034 strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
1035 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001036
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -02001037 if (!is_label && !elf_sym__is_a(&sym, map->type))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001038 continue;
1039
Eric B Munson70c38562010-06-14 14:56:33 +01001040 if (opdsec && sym.st_shndx == opdidx) {
1041 u32 offset = sym.st_value - opdshdr.sh_addr;
1042 u64 *opd = opddata->d_buf + offset;
1043 sym.st_value = *opd;
1044 sym.st_shndx = elf_addr_to_index(elf, sym.st_value);
1045 }
1046
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001047 sec = elf_getscn(elf, sym.st_shndx);
1048 if (!sec)
1049 goto out_elf_end;
1050
1051 gelf_getshdr(sec, &shdr);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001052
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -02001053 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001054 continue;
1055
1056 section_name = elf_sec__name(&shdr, secstrs);
Ingo Molnar0b73da32009-06-06 15:48:52 +02001057
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001058 if (self->kernel != DSO_TYPE_USER || kmodule) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001059 char dso_name[PATH_MAX];
1060
1061 if (strcmp(section_name,
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -03001062 (curr_dso->short_name +
1063 self->short_name_len)) == 0)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001064 goto new_symbol;
1065
1066 if (strcmp(section_name, ".text") == 0) {
1067 curr_map = map;
1068 curr_dso = self;
1069 goto new_symbol;
1070 }
1071
1072 snprintf(dso_name, sizeof(dso_name),
1073 "%s%s", self->short_name, section_name);
1074
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001075 curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001076 if (curr_map == NULL) {
1077 u64 start = sym.st_value;
1078
1079 if (kmodule)
1080 start += map->start + shdr.sh_offset;
1081
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001082 curr_dso = dso__new(dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001083 if (curr_dso == NULL)
1084 goto out_elf_end;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001085 curr_dso->kernel = self->kernel;
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001086 curr_map = map__new2(start, curr_dso,
Arnaldo Carvalho de Melo6275ce22010-02-03 16:52:01 -02001087 map->type);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001088 if (curr_map == NULL) {
1089 dso__delete(curr_dso);
1090 goto out_elf_end;
1091 }
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -02001092 curr_map->map_ip = identity__map_ip;
1093 curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -03001094 curr_dso->origin = self->origin;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001095 map_groups__insert(kmap->kmaps, curr_map);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001096 dsos__add(&self->node, curr_dso);
Arnaldo Carvalho de Melo6275ce22010-02-03 16:52:01 -02001097 dso__set_loaded(curr_dso, map->type);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001098 } else
1099 curr_dso = curr_map->dso;
1100
1101 goto new_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001102 }
1103
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001104 if (curr_dso->adjust_symbols) {
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -02001105 pr_debug4("%s: adjusting symbol: st_value: %#Lx "
1106 "sh_addr: %#Lx sh_offset: %#Lx\n", __func__,
1107 (u64)sym.st_value, (u64)shdr.sh_addr,
1108 (u64)shdr.sh_offset);
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001109 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001110 }
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -03001111 /*
1112 * We need to figure out if the object was created from C++ sources
1113 * DWARF DW_compile_unit has this, but we don't always have access
1114 * to it...
1115 */
Ingo Molnar83a09442009-08-15 12:26:57 +02001116 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -03001117 if (demangled != NULL)
Ingo Molnar83a09442009-08-15 12:26:57 +02001118 elf_name = demangled;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001119new_symbol:
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001120 f = symbol__new(sym.st_value, sym.st_size, elf_name);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -03001121 free(demangled);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001122 if (!f)
1123 goto out_elf_end;
1124
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001125 if (filter && filter(curr_map, f))
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001126 symbol__delete(f);
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -03001127 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001128 symbols__insert(&curr_dso->symbols[curr_map->type], f);
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -03001129 nr++;
1130 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001131 }
1132
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001133 /*
1134 * For misannotated, zeroed, ASM function sizes.
1135 */
Arnaldo Carvalho de Melo6275ce22010-02-03 16:52:01 -02001136 if (nr > 0) {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001137 symbols__fixup_end(&self->symbols[map->type]);
Arnaldo Carvalho de Melo6275ce22010-02-03 16:52:01 -02001138 if (kmap) {
1139 /*
1140 * We need to fixup this here too because we create new
1141 * maps here, for things like vsyscall sections.
1142 */
1143 __map_groups__fixup_end(kmap->kmaps, map->type);
1144 }
1145 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001146 err = nr;
1147out_elf_end:
1148 elf_end(elf);
1149out_close:
1150 return err;
1151}
1152
Arnaldo Carvalho de Melo78075ca2009-11-20 20:51:26 -02001153static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1154{
1155 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1156}
1157
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001158bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001159{
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -02001160 bool have_build_id = false;
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001161 struct dso *pos;
1162
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001163 list_for_each_entry(pos, head, node) {
1164 if (with_hits && !pos->hit)
1165 continue;
Arnaldo Carvalho de Melof6e14672010-05-19 11:13:02 -03001166 if (pos->has_build_id) {
1167 have_build_id = true;
1168 continue;
1169 }
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -02001170 if (filename__read_build_id(pos->long_name, pos->build_id,
1171 sizeof(pos->build_id)) > 0) {
1172 have_build_id = true;
1173 pos->has_build_id = true;
1174 }
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001175 }
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001176
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -02001177 return have_build_id;
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001178}
1179
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001180/*
1181 * Align offset to 4 bytes as needed for note name and descriptor data.
1182 */
1183#define NOTE_ALIGN(n) (((n) + 3) & -4U)
1184
Arnaldo Carvalho de Melo2643ce1142009-11-03 21:46:10 -02001185int filename__read_build_id(const char *filename, void *bf, size_t size)
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001186{
Arnaldo Carvalho de Melo2643ce1142009-11-03 21:46:10 -02001187 int fd, err = -1;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001188 GElf_Ehdr ehdr;
1189 GElf_Shdr shdr;
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001190 Elf_Data *data;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001191 Elf_Scn *sec;
Pekka Enberge57cfcd2009-11-22 12:29:44 +02001192 Elf_Kind ek;
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001193 void *ptr;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001194 Elf *elf;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001195
Arnaldo Carvalho de Melo2643ce1142009-11-03 21:46:10 -02001196 if (size < BUILD_ID_SIZE)
1197 goto out;
1198
1199 fd = open(filename, O_RDONLY);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001200 if (fd < 0)
1201 goto out;
1202
Marti Raudsepp84087122009-10-24 19:10:36 +03001203 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001204 if (elf == NULL) {
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001205 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001206 goto out_close;
1207 }
1208
Pekka Enberge57cfcd2009-11-22 12:29:44 +02001209 ek = elf_kind(elf);
1210 if (ek != ELF_K_ELF)
1211 goto out_elf_end;
1212
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001213 if (gelf_getehdr(elf, &ehdr) == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001214 pr_err("%s: cannot get elf header.\n", __func__);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001215 goto out_elf_end;
1216 }
1217
Arnaldo Carvalho de Melo2643ce1142009-11-03 21:46:10 -02001218 sec = elf_section_by_name(elf, &ehdr, &shdr,
1219 ".note.gnu.build-id", NULL);
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001220 if (sec == NULL) {
1221 sec = elf_section_by_name(elf, &ehdr, &shdr,
1222 ".notes", NULL);
1223 if (sec == NULL)
1224 goto out_elf_end;
1225 }
1226
1227 data = elf_getdata(sec, NULL);
1228 if (data == NULL)
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001229 goto out_elf_end;
1230
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001231 ptr = data->d_buf;
1232 while (ptr < (data->d_buf + data->d_size)) {
1233 GElf_Nhdr *nhdr = ptr;
1234 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1235 descsz = NOTE_ALIGN(nhdr->n_descsz);
1236 const char *name;
1237
1238 ptr += sizeof(*nhdr);
1239 name = ptr;
1240 ptr += namesz;
1241 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1242 nhdr->n_namesz == sizeof("GNU")) {
1243 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1244 memcpy(bf, ptr, BUILD_ID_SIZE);
1245 err = BUILD_ID_SIZE;
1246 break;
1247 }
1248 }
1249 ptr += descsz;
1250 }
Arnaldo Carvalho de Melo2643ce1142009-11-03 21:46:10 -02001251out_elf_end:
1252 elf_end(elf);
1253out_close:
1254 close(fd);
1255out:
1256 return err;
1257}
1258
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001259int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1260{
1261 int fd, err = -1;
1262
1263 if (size < BUILD_ID_SIZE)
1264 goto out;
1265
1266 fd = open(filename, O_RDONLY);
1267 if (fd < 0)
1268 goto out;
1269
1270 while (1) {
1271 char bf[BUFSIZ];
1272 GElf_Nhdr nhdr;
1273 int namesz, descsz;
1274
1275 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1276 break;
1277
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001278 namesz = NOTE_ALIGN(nhdr.n_namesz);
1279 descsz = NOTE_ALIGN(nhdr.n_descsz);
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001280 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1281 nhdr.n_namesz == sizeof("GNU")) {
1282 if (read(fd, bf, namesz) != namesz)
1283 break;
1284 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1285 if (read(fd, build_id,
1286 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1287 err = 0;
1288 break;
1289 }
1290 } else if (read(fd, bf, descsz) != descsz)
1291 break;
1292 } else {
1293 int n = namesz + descsz;
1294 if (read(fd, bf, n) != n)
1295 break;
1296 }
1297 }
1298 close(fd);
1299out:
1300 return err;
1301}
1302
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001303char dso__symtab_origin(const struct dso *self)
1304{
1305 static const char origin[] = {
1306 [DSO__ORIG_KERNEL] = 'k',
1307 [DSO__ORIG_JAVA_JIT] = 'j',
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02001308 [DSO__ORIG_BUILD_ID_CACHE] = 'B',
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001309 [DSO__ORIG_FEDORA] = 'f',
1310 [DSO__ORIG_UBUNTU] = 'u',
1311 [DSO__ORIG_BUILDID] = 'b',
1312 [DSO__ORIG_DSO] = 'd',
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001313 [DSO__ORIG_KMODULE] = 'K',
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001314 [DSO__ORIG_GUEST_KERNEL] = 'g',
1315 [DSO__ORIG_GUEST_KMODULE] = 'G',
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001316 };
1317
1318 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1319 return '!';
1320 return origin[self->origin];
1321}
1322
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001323int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001324{
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001325 int size = PATH_MAX;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001326 char *name;
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001327 u8 build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001328 int ret = -1;
1329 int fd;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001330 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001331 const char *root_dir;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001332
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001333 dso__set_loaded(self, map->type);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001334
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001335 if (self->kernel == DSO_TYPE_KERNEL)
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001336 return dso__load_kernel_sym(self, map, filter);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001337 else if (self->kernel == DSO_TYPE_GUEST_KERNEL)
1338 return dso__load_guest_kernel_sym(self, map, filter);
1339
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001340 if (map->groups && map->groups->machine)
1341 machine = map->groups->machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001342 else
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001343 machine = NULL;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001344
1345 name = malloc(size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001346 if (!name)
1347 return -1;
1348
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -03001349 self->adjust_symbols = 0;
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001350
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001351 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001352 ret = dso__load_perf_map(self, map, filter);
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001353 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1354 DSO__ORIG_NOT_FOUND;
1355 return ret;
1356 }
1357
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02001358 self->origin = DSO__ORIG_BUILD_ID_CACHE;
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001359 if (dso__build_id_filename(self, name, size) != NULL)
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02001360 goto open_file;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001361more:
1362 do {
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001363 self->origin++;
1364 switch (self->origin) {
1365 case DSO__ORIG_FEDORA:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001366 snprintf(name, size, "/usr/lib/debug%s.debug",
1367 self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001368 break;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001369 case DSO__ORIG_UBUNTU:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001370 snprintf(name, size, "/usr/lib/debug%s",
1371 self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001372 break;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001373 case DSO__ORIG_BUILDID:
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001374 if (filename__read_build_id(self->long_name, build_id,
1375 sizeof(build_id))) {
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -03001376 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001377 build_id__sprintf(build_id, sizeof(build_id),
1378 build_id_hex);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001379 snprintf(name, size,
1380 "/usr/lib/debug/.build-id/%.2s/%s.debug",
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001381 build_id_hex, build_id_hex + 2);
1382 if (self->has_build_id)
1383 goto compare_build_id;
1384 break;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001385 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001386 self->origin++;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001387 /* Fall thru */
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001388 case DSO__ORIG_DSO:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001389 snprintf(name, size, "%s", self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001390 break;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001391 case DSO__ORIG_GUEST_KMODULE:
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001392 if (map->groups && map->groups->machine)
1393 root_dir = map->groups->machine->root_dir;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001394 else
1395 root_dir = "";
1396 snprintf(name, size, "%s%s", root_dir, self->long_name);
1397 break;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001398
1399 default:
1400 goto out;
1401 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001402
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001403 if (self->has_build_id) {
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001404 if (filename__read_build_id(name, build_id,
1405 sizeof(build_id)) < 0)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001406 goto more;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001407compare_build_id:
Arnaldo Carvalho de Melo78075ca2009-11-20 20:51:26 -02001408 if (!dso__build_id_equal(self, build_id))
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001409 goto more;
1410 }
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02001411open_file:
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001412 fd = open(name, O_RDONLY);
1413 } while (fd < 0);
1414
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001415 ret = dso__load_sym(self, map, name, fd, filter, 0);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001416 close(fd);
1417
1418 /*
1419 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1420 */
1421 if (!ret)
1422 goto more;
1423
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001424 if (ret > 0) {
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -02001425 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001426 if (nr_plt > 0)
1427 ret += nr_plt;
1428 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001429out:
1430 free(name);
Arnaldo Carvalho de Melo1340e6b2009-08-11 17:04:36 -03001431 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1432 return 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001433 return ret;
1434}
1435
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -02001436struct map *map_groups__find_by_name(struct map_groups *self,
1437 enum map_type type, const char *name)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001438{
1439 struct rb_node *nd;
1440
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -02001441 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001442 struct map *map = rb_entry(nd, struct map, rb_node);
1443
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001444 if (map->dso && strcmp(map->dso->short_name, name) == 0)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001445 return map;
1446 }
1447
1448 return NULL;
1449}
1450
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001451static int dso__kernel_module_get_build_id(struct dso *self,
1452 const char *root_dir)
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001453{
1454 char filename[PATH_MAX];
1455 /*
1456 * kernel module short names are of the form "[module]" and
1457 * we need just "module" here.
1458 */
1459 const char *name = self->short_name + 1;
1460
1461 snprintf(filename, sizeof(filename),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001462 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1463 root_dir, (int)strlen(name) - 1, name);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001464
1465 if (sysfs__read_build_id(filename, self->build_id,
1466 sizeof(self->build_id)) == 0)
1467 self->has_build_id = true;
1468
1469 return 0;
1470}
1471
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001472static int map_groups__set_modules_path_dir(struct map_groups *self,
1473 const char *dir_name)
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001474{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001475 struct dirent *dent;
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03001476 DIR *dir = opendir(dir_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001477
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001478 if (!dir) {
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03001479 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001480 return -1;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001481 }
1482
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001483 while ((dent = readdir(dir)) != NULL) {
1484 char path[PATH_MAX];
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001485 struct stat st;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001486
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001487 /*sshfs might return bad dent->d_type, so we have to stat*/
1488 sprintf(path, "%s/%s", dir_name, dent->d_name);
1489 if (stat(path, &st))
1490 continue;
1491
1492 if (S_ISDIR(st.st_mode)) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001493 if (!strcmp(dent->d_name, ".") ||
1494 !strcmp(dent->d_name, ".."))
1495 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001496
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001497 snprintf(path, sizeof(path), "%s/%s",
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03001498 dir_name, dent->d_name);
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001499 if (map_groups__set_modules_path_dir(self, path) < 0)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001500 goto failure;
1501 } else {
1502 char *dot = strrchr(dent->d_name, '.'),
1503 dso_name[PATH_MAX];
1504 struct map *map;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001505 char *long_name;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001506
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001507 if (dot == NULL || strcmp(dot, ".ko"))
1508 continue;
1509 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1510 (int)(dot - dent->d_name), dent->d_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001511
Arnaldo Carvalho de Meloa2a99e82009-10-05 14:26:18 -03001512 strxfrchar(dso_name, '-', '_');
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001513 map = map_groups__find_by_name(self, MAP__FUNCTION, dso_name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001514 if (map == NULL)
1515 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001516
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001517 snprintf(path, sizeof(path), "%s/%s",
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03001518 dir_name, dent->d_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001519
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001520 long_name = strdup(path);
1521 if (long_name == NULL)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001522 goto failure;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001523 dso__set_long_name(map->dso, long_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001524 dso__kernel_module_get_build_id(map->dso, "");
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001525 }
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001526 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001527
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001528 return 0;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001529failure:
1530 closedir(dir);
1531 return -1;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001532}
1533
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001534static char *get_kernel_version(const char *root_dir)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001535{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001536 char version[PATH_MAX];
1537 FILE *file;
1538 char *name, *tmp;
1539 const char *prefix = "Linux version ";
1540
1541 sprintf(version, "%s/proc/version", root_dir);
1542 file = fopen(version, "r");
1543 if (!file)
1544 return NULL;
1545
1546 version[0] = '\0';
1547 tmp = fgets(version, sizeof(version), file);
1548 fclose(file);
1549
1550 name = strstr(version, prefix);
1551 if (!name)
1552 return NULL;
1553 name += strlen(prefix);
1554 tmp = strchr(name, ' ');
1555 if (tmp)
1556 *tmp = '\0';
1557
1558 return strdup(name);
1559}
1560
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001561static int machine__set_modules_path(struct machine *self)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001562{
1563 char *version;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001564 char modules_path[PATH_MAX];
1565
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001566 version = get_kernel_version(self->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001567 if (!version)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001568 return -1;
1569
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001570 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001571 self->root_dir, version);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001572 free(version);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001573
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001574 return map_groups__set_modules_path_dir(&self->kmaps, modules_path);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001575}
1576
1577/*
1578 * Constructor variant for modules (where we know from /proc/modules where
1579 * they are loaded) and for vmlinux, where only after we load all the
1580 * symbols we'll know where it starts and ends.
1581 */
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001582static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001583{
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03001584 struct map *self = calloc(1, (sizeof(*self) +
1585 (dso->kernel ? sizeof(struct kmap) : 0)));
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001586 if (self != NULL) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001587 /*
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -02001588 * ->end will be filled after we load all the symbols
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001589 */
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001590 map__init(self, type, start, 0, 0, dso);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001591 }
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -02001592
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001593 return self;
1594}
1595
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001596struct map *machine__new_module(struct machine *self, u64 start,
1597 const char *filename)
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001598{
1599 struct map *map;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001600 struct dso *dso = __dsos__findnew(&self->kernel_dsos, filename);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001601
1602 if (dso == NULL)
1603 return NULL;
1604
1605 map = map__new2(start, dso, MAP__FUNCTION);
1606 if (map == NULL)
1607 return NULL;
1608
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001609 if (machine__is_host(self))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001610 dso->origin = DSO__ORIG_KMODULE;
1611 else
1612 dso->origin = DSO__ORIG_GUEST_KMODULE;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001613 map_groups__insert(&self->kmaps, map);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001614 return map;
1615}
1616
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001617static int machine__create_modules(struct machine *self)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001618{
1619 char *line = NULL;
1620 size_t n;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001621 FILE *file;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001622 struct map *map;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001623 const char *modules;
1624 char path[PATH_MAX];
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001625
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001626 if (machine__is_default_guest(self))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001627 modules = symbol_conf.default_guest_modules;
1628 else {
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001629 sprintf(path, "%s/proc/modules", self->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001630 modules = path;
1631 }
1632
1633 file = fopen(modules, "r");
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001634 if (file == NULL)
1635 return -1;
1636
1637 while (!feof(file)) {
1638 char name[PATH_MAX];
1639 u64 start;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001640 char *sep;
1641 int line_len;
1642
1643 line_len = getline(&line, &n, file);
1644 if (line_len < 0)
1645 break;
1646
1647 if (!line)
1648 goto out_failure;
1649
1650 line[--line_len] = '\0'; /* \n */
1651
1652 sep = strrchr(line, 'x');
1653 if (sep == NULL)
1654 continue;
1655
1656 hex2u64(sep + 1, &start);
1657
1658 sep = strchr(line, ' ');
1659 if (sep == NULL)
1660 continue;
1661
1662 *sep = '\0';
1663
1664 snprintf(name, sizeof(name), "[%s]", line);
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001665 map = machine__new_module(self, start, name);
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001666 if (map == NULL)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001667 goto out_delete_line;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001668 dso__kernel_module_get_build_id(map->dso, self->root_dir);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001669 }
1670
1671 free(line);
1672 fclose(file);
1673
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001674 return machine__set_modules_path(self);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001675
1676out_delete_line:
1677 free(line);
1678out_failure:
1679 return -1;
1680}
1681
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -02001682static int dso__load_vmlinux(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001683 const char *vmlinux, symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001684{
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001685 int err = -1, fd;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001686
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001687 if (self->has_build_id) {
1688 u8 build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001689
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001690 if (filename__read_build_id(vmlinux, build_id,
1691 sizeof(build_id)) < 0) {
1692 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1693 return -1;
1694 }
1695 if (!dso__build_id_equal(self, build_id)) {
1696 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1697 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1698
1699 build_id__sprintf(self->build_id,
1700 sizeof(self->build_id),
1701 expected_build_id);
1702 build_id__sprintf(build_id, sizeof(build_id),
1703 vmlinux_build_id);
1704 pr_debug("build_id in %s is %s while expected is %s, "
1705 "ignoring it\n", vmlinux, vmlinux_build_id,
1706 expected_build_id);
1707 return -1;
1708 }
1709 }
1710
1711 fd = open(vmlinux, O_RDONLY);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001712 if (fd < 0)
1713 return -1;
1714
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001715 dso__set_loaded(self, map->type);
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001716 err = dso__load_sym(self, map, vmlinux, fd, filter, 0);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001717 close(fd);
1718
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001719 if (err > 0)
1720 pr_debug("Using %s for symbols\n", vmlinux);
1721
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001722 return err;
1723}
1724
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001725int dso__load_vmlinux_path(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001726 symbol_filter_t filter)
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001727{
1728 int i, err = 0;
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001729 char *filename;
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001730
1731 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001732 vmlinux_path__nr_entries + 1);
1733
1734 filename = dso__build_id_filename(self, NULL, 0);
1735 if (filename != NULL) {
1736 err = dso__load_vmlinux(self, map, filename, filter);
1737 if (err > 0) {
1738 dso__set_long_name(self, filename);
1739 goto out;
1740 }
1741 free(filename);
1742 }
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001743
1744 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001745 err = dso__load_vmlinux(self, map, vmlinux_path[i], filter);
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001746 if (err > 0) {
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001747 dso__set_long_name(self, strdup(vmlinux_path[i]));
1748 break;
1749 }
1750 }
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001751out:
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001752 return err;
1753}
1754
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001755static int dso__load_kernel_sym(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001756 symbol_filter_t filter)
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001757{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001758 int err;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001759 const char *kallsyms_filename = NULL;
1760 char *kallsyms_allocated_filename = NULL;
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001761 /*
1762 * Step 1: if the user specified a vmlinux filename, use it and only
1763 * it, reporting errors to the user if it cannot be used.
1764 *
1765 * For instance, try to analyse an ARM perf.data file _without_ a
1766 * build-id, or if the user specifies the wrong path to the right
1767 * vmlinux file, obviously we can't fallback to another vmlinux (a
1768 * x86_86 one, on the machine where analysis is being performed, say),
1769 * or worse, /proc/kallsyms.
1770 *
1771 * If the specified file _has_ a build-id and there is a build-id
1772 * section in the perf.data file, we will still do the expected
1773 * validation in dso__load_vmlinux and will bail out if they don't
1774 * match.
1775 */
1776 if (symbol_conf.vmlinux_name != NULL) {
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001777 err = dso__load_vmlinux(self, map,
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001778 symbol_conf.vmlinux_name, filter);
Arnaldo Carvalho de Meloe7dadc02010-06-03 18:35:55 -03001779 if (err > 0) {
1780 dso__set_long_name(self,
1781 strdup(symbol_conf.vmlinux_name));
1782 goto out_fixup;
1783 }
1784 return err;
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001785 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001786
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001787 if (vmlinux_path != NULL) {
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001788 err = dso__load_vmlinux_path(self, map, filter);
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001789 if (err > 0)
1790 goto out_fixup;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001791 }
1792
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001793 /*
1794 * Say the kernel DSO was created when processing the build-id header table,
1795 * we have a build-id, so check if it is the same as the running kernel,
1796 * using it if it is.
1797 */
1798 if (self->has_build_id) {
1799 u8 kallsyms_build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001800 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001801
1802 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
Arnaldo Carvalho de Melo8d0591f62010-01-14 18:30:05 -02001803 sizeof(kallsyms_build_id)) == 0) {
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001804 if (dso__build_id_equal(self, kallsyms_build_id)) {
1805 kallsyms_filename = "/proc/kallsyms";
Arnaldo Carvalho de Melo8d0591f62010-01-14 18:30:05 -02001806 goto do_kallsyms;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001807 }
Arnaldo Carvalho de Melo8d0591f62010-01-14 18:30:05 -02001808 }
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001809 /*
1810 * Now look if we have it on the build-id cache in
1811 * $HOME/.debug/[kernel.kallsyms].
1812 */
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001813 build_id__sprintf(self->build_id, sizeof(self->build_id),
1814 sbuild_id);
1815
1816 if (asprintf(&kallsyms_allocated_filename,
1817 "%s/.debug/[kernel.kallsyms]/%s",
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001818 getenv("HOME"), sbuild_id) == -1) {
1819 pr_err("Not enough memory for kallsyms file lookup\n");
Arnaldo Carvalho de Melo8d0591f62010-01-14 18:30:05 -02001820 return -1;
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001821 }
Arnaldo Carvalho de Melo8d0591f62010-01-14 18:30:05 -02001822
Arnaldo Carvalho de Melo19fc2de2010-01-22 14:35:02 -02001823 kallsyms_filename = kallsyms_allocated_filename;
1824
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001825 if (access(kallsyms_filename, F_OK)) {
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001826 pr_err("No kallsyms or vmlinux with build-id %s "
1827 "was found\n", sbuild_id);
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001828 free(kallsyms_allocated_filename);
1829 return -1;
1830 }
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001831 } else {
1832 /*
1833 * Last resort, if we don't have a build-id and couldn't find
1834 * any vmlinux file, try the running kernel kallsyms table.
1835 */
1836 kallsyms_filename = "/proc/kallsyms";
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -02001837 }
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001838
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001839do_kallsyms:
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001840 err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001841 if (err > 0)
1842 pr_debug("Using %s for symbols\n", kallsyms_filename);
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001843 free(kallsyms_allocated_filename);
1844
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001845 if (err > 0) {
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001846out_fixup:
Arnaldo Carvalho de Meloe1c7c6a2010-01-22 14:35:01 -02001847 if (kallsyms_filename != NULL)
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001848 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001849 map__fixup_start(map);
1850 map__fixup_end(map);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001851 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001852
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001853 return err;
1854}
1855
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001856static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
1857 symbol_filter_t filter)
1858{
1859 int err;
1860 const char *kallsyms_filename = NULL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001861 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001862 char path[PATH_MAX];
1863
1864 if (!map->groups) {
1865 pr_debug("Guest kernel map hasn't the point to groups\n");
1866 return -1;
1867 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001868 machine = map->groups->machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001869
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001870 if (machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001871 /*
1872 * if the user specified a vmlinux filename, use it and only
1873 * it, reporting errors to the user if it cannot be used.
1874 * Or use file guest_kallsyms inputted by user on commandline
1875 */
1876 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1877 err = dso__load_vmlinux(self, map,
1878 symbol_conf.default_guest_vmlinux_name, filter);
1879 goto out_try_fixup;
1880 }
1881
1882 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1883 if (!kallsyms_filename)
1884 return -1;
1885 } else {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001886 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001887 kallsyms_filename = path;
1888 }
1889
1890 err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
1891 if (err > 0)
1892 pr_debug("Using %s for symbols\n", kallsyms_filename);
1893
1894out_try_fixup:
1895 if (err > 0) {
1896 if (kallsyms_filename != NULL) {
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -03001897 machine__mmap_name(machine, path, sizeof(path));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001898 dso__set_long_name(self, strdup(path));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001899 }
1900 map__fixup_start(map);
1901 map__fixup_end(map);
1902 }
1903
1904 return err;
1905}
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001906
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001907static void dsos__add(struct list_head *head, struct dso *dso)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001908{
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001909 list_add_tail(&dso->node, head);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001910}
1911
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001912static struct dso *dsos__find(struct list_head *head, const char *name)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001913{
1914 struct dso *pos;
1915
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001916 list_for_each_entry(pos, head, node)
Arnaldo Carvalho de Melocf4e5b02010-01-14 23:45:27 -02001917 if (strcmp(pos->long_name, name) == 0)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001918 return pos;
1919 return NULL;
1920}
1921
Arnaldo Carvalho de Meloa89e5abe2010-01-07 19:59:39 -02001922struct dso *__dsos__findnew(struct list_head *head, const char *name)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001923{
Arnaldo Carvalho de Meloa89e5abe2010-01-07 19:59:39 -02001924 struct dso *dso = dsos__find(head, name);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001925
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -02001926 if (!dso) {
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001927 dso = dso__new(name);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001928 if (dso != NULL) {
Arnaldo Carvalho de Meloa89e5abe2010-01-07 19:59:39 -02001929 dsos__add(head, dso);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001930 dso__set_basename(dso);
1931 }
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001932 }
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001933
1934 return dso;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001935}
1936
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001937size_t __dsos__fprintf(struct list_head *head, FILE *fp)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001938{
1939 struct dso *pos;
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001940 size_t ret = 0;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001941
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001942 list_for_each_entry(pos, head, node) {
1943 int i;
1944 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001945 ret += dso__fprintf(pos, i, fp);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001946 }
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001947
1948 return ret;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001949}
1950
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001951size_t machines__fprintf_dsos(struct rb_root *self, FILE *fp)
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001952{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001953 struct rb_node *nd;
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001954 size_t ret = 0;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001955
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001956 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001957 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001958 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
1959 ret += __dsos__fprintf(&pos->user_dsos, fp);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001960 }
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001961
1962 return ret;
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001963}
1964
Arnaldo Carvalho de Melo88d3d9b72010-01-14 23:45:30 -02001965static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1966 bool with_hits)
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001967{
1968 struct dso *pos;
1969 size_t ret = 0;
1970
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001971 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo88d3d9b72010-01-14 23:45:30 -02001972 if (with_hits && !pos->hit)
1973 continue;
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001974 ret += dso__fprintf_buildid(pos, fp);
Arnaldo Carvalho de Melo1124ba72009-11-16 21:45:25 -02001975 ret += fprintf(fp, " %s\n", pos->long_name);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001976 }
1977 return ret;
1978}
1979
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001980size_t machine__fprintf_dsos_buildid(struct machine *self, FILE *fp, bool with_hits)
1981{
1982 return __dsos__fprintf_buildid(&self->kernel_dsos, fp, with_hits) +
1983 __dsos__fprintf_buildid(&self->user_dsos, fp, with_hits);
1984}
1985
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001986size_t machines__fprintf_dsos_buildid(struct rb_root *self, FILE *fp, bool with_hits)
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001987{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001988 struct rb_node *nd;
1989 size_t ret = 0;
1990
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001991 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001992 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001993 ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001994 }
1995 return ret;
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001996}
1997
Arnaldo Carvalho de Melofd1d9082010-01-27 21:05:51 -02001998struct dso *dso__new_kernel(const char *name)
1999{
2000 struct dso *self = dso__new(name ?: "[kernel.kallsyms]");
2001
2002 if (self != NULL) {
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -03002003 dso__set_short_name(self, "[kernel]");
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002004 self->kernel = DSO_TYPE_KERNEL;
Arnaldo Carvalho de Melofd1d9082010-01-27 21:05:51 -02002005 }
2006
2007 return self;
2008}
2009
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002010static struct dso *dso__new_guest_kernel(struct machine *machine,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002011 const char *name)
Arnaldo Carvalho de Melofd1d9082010-01-27 21:05:51 -02002012{
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -03002013 char bf[PATH_MAX];
2014 struct dso *self = dso__new(name ?: machine__mmap_name(machine, bf, sizeof(bf)));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002015
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002016 if (self != NULL) {
2017 dso__set_short_name(self, "[guest.kernel]");
2018 self->kernel = DSO_TYPE_GUEST_KERNEL;
2019 }
2020
2021 return self;
2022}
2023
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002024void dso__read_running_kernel_build_id(struct dso *self, struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002025{
2026 char path[PATH_MAX];
2027
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002028 if (machine__is_default_guest(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002029 return;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002030 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002031 if (sysfs__read_build_id(path, self->build_id,
Arnaldo Carvalho de Melofd1d9082010-01-27 21:05:51 -02002032 sizeof(self->build_id)) == 0)
2033 self->has_build_id = true;
2034}
2035
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002036static struct dso *machine__create_kernel(struct machine *self)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002037{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002038 const char *vmlinux_name = NULL;
2039 struct dso *kernel;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002040
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002041 if (machine__is_host(self)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002042 vmlinux_name = symbol_conf.vmlinux_name;
2043 kernel = dso__new_kernel(vmlinux_name);
2044 } else {
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002045 if (machine__is_default_guest(self))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002046 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002047 kernel = dso__new_guest_kernel(self, vmlinux_name);
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -02002048 }
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002049
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002050 if (kernel != NULL) {
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002051 dso__read_running_kernel_build_id(kernel, self);
2052 dsos__add(&self->kernel_dsos, kernel);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002053 }
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002054 return kernel;
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002055}
2056
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002057int __machine__create_kernel_maps(struct machine *self, struct dso *kernel)
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002058{
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02002059 enum map_type type;
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002060
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02002061 for (type = 0; type < MAP__NR_TYPES; ++type) {
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02002062 struct kmap *kmap;
2063
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002064 self->vmlinux_maps[type] = map__new2(0, kernel, type);
2065 if (self->vmlinux_maps[type] == NULL)
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02002066 return -1;
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002067
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002068 self->vmlinux_maps[type]->map_ip =
2069 self->vmlinux_maps[type]->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02002070
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002071 kmap = map__kmap(self->vmlinux_maps[type]);
2072 kmap->kmaps = &self->kmaps;
2073 map_groups__insert(&self->kmaps, self->vmlinux_maps[type]);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002074 }
2075
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002076 return 0;
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02002077}
2078
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002079int machine__create_kernel_maps(struct machine *self)
2080{
2081 struct dso *kernel = machine__create_kernel(self);
2082
2083 if (kernel == NULL ||
2084 __machine__create_kernel_maps(self, kernel) < 0)
2085 return -1;
2086
2087 if (symbol_conf.use_modules && machine__create_modules(self) < 0)
2088 pr_debug("Problems creating module maps, continuing anyway...\n");
2089 /*
2090 * Now that we have all the maps created, just set the ->end of them:
2091 */
2092 map_groups__fixup_end(&self->kmaps);
2093 return 0;
2094}
2095
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002096static void vmlinux_path__exit(void)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02002097{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002098 while (--vmlinux_path__nr_entries >= 0) {
2099 free(vmlinux_path[vmlinux_path__nr_entries]);
2100 vmlinux_path[vmlinux_path__nr_entries] = NULL;
2101 }
2102
2103 free(vmlinux_path);
2104 vmlinux_path = NULL;
2105}
2106
2107static int vmlinux_path__init(void)
2108{
2109 struct utsname uts;
2110 char bf[PATH_MAX];
2111
2112 if (uname(&uts) < 0)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02002113 return -1;
2114
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002115 vmlinux_path = malloc(sizeof(char *) * 5);
2116 if (vmlinux_path == NULL)
2117 return -1;
2118
2119 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
2120 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2121 goto out_fail;
2122 ++vmlinux_path__nr_entries;
2123 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
2124 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2125 goto out_fail;
2126 ++vmlinux_path__nr_entries;
2127 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
2128 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2129 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2130 goto out_fail;
2131 ++vmlinux_path__nr_entries;
2132 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
2133 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2134 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2135 goto out_fail;
2136 ++vmlinux_path__nr_entries;
2137 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
2138 uts.release);
2139 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2140 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2141 goto out_fail;
2142 ++vmlinux_path__nr_entries;
2143
2144 return 0;
2145
2146out_fail:
2147 vmlinux_path__exit();
2148 return -1;
2149}
2150
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03002151size_t machine__fprintf_vmlinux_path(struct machine *self, FILE *fp)
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -03002152{
2153 int i;
2154 size_t printed = 0;
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03002155 struct dso *kdso = self->vmlinux_maps[MAP__FUNCTION]->dso;
2156
2157 if (kdso->has_build_id) {
2158 char filename[PATH_MAX];
2159 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
2160 printed += fprintf(fp, "[0] %s\n", filename);
2161 }
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -03002162
2163 for (i = 0; i < vmlinux_path__nr_entries; ++i)
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03002164 printed += fprintf(fp, "[%d] %s\n",
2165 i + kdso->has_build_id, vmlinux_path[i]);
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -03002166
2167 return printed;
2168}
2169
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002170static int setup_list(struct strlist **list, const char *list_str,
2171 const char *list_name)
2172{
2173 if (list_str == NULL)
2174 return 0;
2175
2176 *list = strlist__new(true, list_str);
2177 if (!*list) {
2178 pr_err("problems parsing %s list\n", list_name);
2179 return -1;
2180 }
2181 return 0;
2182}
2183
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02002184int symbol__init(void)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002185{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02002186 elf_version(EV_CURRENT);
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02002187 if (symbol_conf.sort_by_name)
2188 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2189 sizeof(struct symbol));
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02002190
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02002191 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002192 return -1;
2193
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02002194 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2195 pr_err("'.' is the only non valid --field-separator argument\n");
2196 return -1;
2197 }
2198
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002199 if (setup_list(&symbol_conf.dso_list,
2200 symbol_conf.dso_list_str, "dso") < 0)
2201 return -1;
2202
2203 if (setup_list(&symbol_conf.comm_list,
2204 symbol_conf.comm_list_str, "comm") < 0)
2205 goto out_free_dso_list;
2206
2207 if (setup_list(&symbol_conf.sym_list,
2208 symbol_conf.sym_list_str, "symbol") < 0)
2209 goto out_free_comm_list;
2210
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002211 return 0;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002212
2213out_free_dso_list:
2214 strlist__delete(symbol_conf.dso_list);
2215out_free_comm_list:
2216 strlist__delete(symbol_conf.comm_list);
2217 return -1;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002218}
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002219
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002220int machines__create_kernel_maps(struct rb_root *self, pid_t pid)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002221{
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002222 struct machine *machine = machines__findnew(self, pid);
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02002223
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002224 if (machine == NULL)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002225 return -1;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002226
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002227 return machine__create_kernel_maps(machine);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002228}
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03002229
2230static int hex(char ch)
2231{
2232 if ((ch >= '0') && (ch <= '9'))
2233 return ch - '0';
2234 if ((ch >= 'a') && (ch <= 'f'))
2235 return ch - 'a' + 10;
2236 if ((ch >= 'A') && (ch <= 'F'))
2237 return ch - 'A' + 10;
2238 return -1;
2239}
2240
2241/*
2242 * While we find nice hex chars, build a long_val.
2243 * Return number of chars processed.
2244 */
2245int hex2u64(const char *ptr, u64 *long_val)
2246{
2247 const char *p = ptr;
2248 *long_val = 0;
2249
2250 while (*p) {
2251 const int hex_val = hex(*p);
2252
2253 if (hex_val < 0)
2254 break;
2255
2256 *long_val = (*long_val << 4) | hex_val;
2257 p++;
2258 }
2259
2260 return p - ptr;
2261}
2262
2263char *strxfrchar(char *s, char from, char to)
2264{
2265 char *p = s;
2266
2267 while ((p = strchr(p, from)) != NULL)
2268 *p++ = to;
2269
2270 return s;
2271}
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002272
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002273int machines__create_guest_kernel_maps(struct rb_root *self)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002274{
2275 int ret = 0;
2276 struct dirent **namelist = NULL;
2277 int i, items = 0;
2278 char path[PATH_MAX];
2279 pid_t pid;
2280
2281 if (symbol_conf.default_guest_vmlinux_name ||
2282 symbol_conf.default_guest_modules ||
2283 symbol_conf.default_guest_kallsyms) {
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002284 machines__create_kernel_maps(self, DEFAULT_GUEST_KERNEL_ID);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002285 }
2286
2287 if (symbol_conf.guestmount) {
2288 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
2289 if (items <= 0)
2290 return -ENOENT;
2291 for (i = 0; i < items; i++) {
2292 if (!isdigit(namelist[i]->d_name[0])) {
2293 /* Filter out . and .. */
2294 continue;
2295 }
2296 pid = atoi(namelist[i]->d_name);
2297 sprintf(path, "%s/%s/proc/kallsyms",
2298 symbol_conf.guestmount,
2299 namelist[i]->d_name);
2300 ret = access(path, R_OK);
2301 if (ret) {
2302 pr_debug("Can't access file %s\n", path);
2303 goto failure;
2304 }
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002305 machines__create_kernel_maps(self, pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002306 }
2307failure:
2308 free(namelist);
2309 }
2310
2311 return ret;
2312}
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002313
2314int machine__load_kallsyms(struct machine *self, const char *filename,
2315 enum map_type type, symbol_filter_t filter)
2316{
2317 struct map *map = self->vmlinux_maps[type];
2318 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
2319
2320 if (ret > 0) {
2321 dso__set_loaded(map->dso, type);
2322 /*
2323 * Since /proc/kallsyms will have multiple sessions for the
2324 * kernel, with modules between them, fixup the end of all
2325 * sections.
2326 */
2327 __map_groups__fixup_end(&self->kmaps, type);
2328 }
2329
2330 return ret;
2331}
2332
2333int machine__load_vmlinux_path(struct machine *self, enum map_type type,
2334 symbol_filter_t filter)
2335{
2336 struct map *map = self->vmlinux_maps[type];
2337 int ret = dso__load_vmlinux_path(map->dso, map, filter);
2338
2339 if (ret > 0) {
2340 dso__set_loaded(map->dso, type);
2341 map__reloc_vmlinux(map);
2342 }
2343
2344 return ret;
2345}