blob: 87d9b1b8b6bb6151307caeebabe613d808242ff2 [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 Meloa2928c42009-05-28 14:55:04 -030014#include "symbol.h"
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -030015#include "strlist.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030016
17#include <libelf.h>
18#include <gelf.h>
19#include <elf.h>
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -020020#include <limits.h>
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030021#include <sys/utsname.h>
Peter Zijlstra2cdbc462009-08-05 14:05:16 +020022
Arnaldo Carvalho de Meloc12e15e2009-11-21 14:31:25 -020023#ifndef NT_GNU_BUILD_ID
24#define NT_GNU_BUILD_ID 3
25#endif
26
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -020027static void dsos__add(struct list_head *head, struct dso *dso);
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020028static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020029static int dso__load_kernel_sym(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -020030 symbol_filter_t filter);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080031static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
32 symbol_filter_t filter);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -020033static int vmlinux_path__nr_entries;
34static char **vmlinux_path;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030035
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -020036struct symbol_conf symbol_conf = {
Arnaldo Carvalho de Melod599db32009-12-15 20:04:42 -020037 .exclude_other = true,
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020038 .use_modules = true,
39 .try_vmlinux_path = true,
40};
41
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020042bool dso__loaded(const struct dso *self, enum map_type type)
43{
44 return self->loaded & (1 << type);
45}
46
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -020047bool dso__sorted_by_name(const struct dso *self, enum map_type type)
48{
49 return self->sorted_by_name & (1 << type);
50}
51
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -020052static void dso__set_sorted_by_name(struct dso *self, enum map_type type)
53{
54 self->sorted_by_name |= (1 << type);
55}
56
Arnaldo Carvalho de Melo36a3e642010-01-04 16:19:27 -020057bool symbol_type__is_a(char symbol_type, enum map_type map_type)
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020058{
59 switch (map_type) {
60 case MAP__FUNCTION:
61 return symbol_type == 'T' || symbol_type == 'W';
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -020062 case MAP__VARIABLE:
63 return symbol_type == 'D' || symbol_type == 'd';
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020064 default:
65 return false;
66 }
67}
68
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -020069static void symbols__fixup_end(struct rb_root *self)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030070{
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -020071 struct rb_node *nd, *prevnd = rb_first(self);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030072 struct symbol *curr, *prev;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030073
74 if (prevnd == NULL)
75 return;
76
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030077 curr = rb_entry(prevnd, struct symbol, rb_node);
78
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030079 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030080 prev = curr;
81 curr = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030082
83 if (prev->end == prev->start)
84 prev->end = curr->start - 1;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030085 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030086
87 /* Last entry */
88 if (curr->end == curr->start)
89 curr->end = roundup(curr->start, 4096);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030090}
91
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -020092static void __map_groups__fixup_end(struct map_groups *self, enum map_type type)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030093{
94 struct map *prev, *curr;
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020095 struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030096
97 if (prevnd == NULL)
98 return;
99
100 curr = rb_entry(prevnd, struct map, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300101
102 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
103 prev = curr;
104 curr = rb_entry(nd, struct map, rb_node);
105 prev->end = curr->start - 1;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300106 }
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -0200107
108 /*
109 * We still haven't the actual symbols, so guess the
110 * last map final address.
111 */
112 curr->end = ~0UL;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300113}
114
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200115static void map_groups__fixup_end(struct map_groups *self)
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -0200116{
117 int i;
118 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200119 __map_groups__fixup_end(self, i);
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -0200120}
121
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200122static struct symbol *symbol__new(u64 start, u64 len, const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300123{
Arnaldo Carvalho de Melo0085c952009-05-28 14:55:13 -0300124 size_t namelen = strlen(name) + 1;
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -0300125 struct symbol *self = calloc(1, (symbol_conf.priv_size +
126 sizeof(*self) + namelen));
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200127 if (self == NULL)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200128 return NULL;
129
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200130 if (symbol_conf.priv_size)
131 self = ((void *)self) + symbol_conf.priv_size;
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200132
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -0300133 self->start = start;
134 self->end = len ? start + len - 1 : start;
135 self->namelen = namelen - 1;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200136
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -0200137 pr_debug4("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200138
Ingo Molnar0b73da32009-06-06 15:48:52 +0200139 memcpy(self->name, name, namelen);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300140
141 return self;
142}
143
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -0300144void symbol__delete(struct symbol *self)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300145{
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200146 free(((void *)self) - symbol_conf.priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300147}
148
149static size_t symbol__fprintf(struct symbol *self, FILE *fp)
150{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300151 return fprintf(fp, " %llx-%llx %s\n",
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300152 self->start, self->end, self->name);
153}
154
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200155void dso__set_long_name(struct dso *self, char *name)
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200156{
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -0200157 if (name == NULL)
158 return;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200159 self->long_name = name;
160 self->long_name_len = strlen(name);
161}
162
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -0300163static void dso__set_short_name(struct dso *self, const char *name)
164{
165 if (name == NULL)
166 return;
167 self->short_name = name;
168 self->short_name_len = strlen(name);
169}
170
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200171static void dso__set_basename(struct dso *self)
172{
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -0300173 dso__set_short_name(self, basename(self->long_name));
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200174}
175
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200176struct dso *dso__new(const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300177{
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -0300178 struct dso *self = calloc(1, sizeof(*self) + strlen(name) + 1);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300179
180 if (self != NULL) {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200181 int i;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300182 strcpy(self->name, name);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200183 dso__set_long_name(self, self->name);
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -0300184 dso__set_short_name(self, self->name);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200185 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200186 self->symbols[i] = self->symbol_names[i] = RB_ROOT;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300187 self->slen_calculated = 0;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300188 self->origin = DSO__ORIG_NOT_FOUND;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200189 self->loaded = 0;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200190 self->sorted_by_name = 0;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200191 self->has_build_id = 0;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800192 self->kernel = DSO_TYPE_USER;
Masami Hiramatsu0ab061c2010-04-21 15:56:16 -0400193 INIT_LIST_HEAD(&self->node);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300194 }
195
196 return self;
197}
198
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200199static void symbols__delete(struct rb_root *self)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300200{
201 struct symbol *pos;
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200202 struct rb_node *next = rb_first(self);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300203
204 while (next) {
205 pos = rb_entry(next, struct symbol, rb_node);
206 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200207 rb_erase(&pos->rb_node, self);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200208 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300209 }
210}
211
212void dso__delete(struct dso *self)
213{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200214 int i;
215 for (i = 0; i < MAP__NR_TYPES; ++i)
216 symbols__delete(&self->symbols[i]);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300217 if (self->long_name != self->name)
218 free(self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300219 free(self);
220}
221
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200222void dso__set_build_id(struct dso *self, void *build_id)
223{
224 memcpy(self->build_id, build_id, sizeof(self->build_id));
225 self->has_build_id = 1;
226}
227
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200228static void symbols__insert(struct rb_root *self, struct symbol *sym)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300229{
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200230 struct rb_node **p = &self->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300231 struct rb_node *parent = NULL;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000232 const u64 ip = sym->start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300233 struct symbol *s;
234
235 while (*p != NULL) {
236 parent = *p;
237 s = rb_entry(parent, struct symbol, rb_node);
238 if (ip < s->start)
239 p = &(*p)->rb_left;
240 else
241 p = &(*p)->rb_right;
242 }
243 rb_link_node(&sym->rb_node, parent, p);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200244 rb_insert_color(&sym->rb_node, self);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300245}
246
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200247static struct symbol *symbols__find(struct rb_root *self, u64 ip)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300248{
249 struct rb_node *n;
250
251 if (self == NULL)
252 return NULL;
253
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200254 n = self->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300255
256 while (n) {
257 struct symbol *s = rb_entry(n, struct symbol, rb_node);
258
259 if (ip < s->start)
260 n = n->rb_left;
261 else if (ip > s->end)
262 n = n->rb_right;
263 else
264 return s;
265 }
266
267 return NULL;
268}
269
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200270struct symbol_name_rb_node {
271 struct rb_node rb_node;
272 struct symbol sym;
273};
274
275static void symbols__insert_by_name(struct rb_root *self, struct symbol *sym)
276{
277 struct rb_node **p = &self->rb_node;
278 struct rb_node *parent = NULL;
279 struct symbol_name_rb_node *symn = ((void *)sym) - sizeof(*parent), *s;
280
281 while (*p != NULL) {
282 parent = *p;
283 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
284 if (strcmp(sym->name, s->sym.name) < 0)
285 p = &(*p)->rb_left;
286 else
287 p = &(*p)->rb_right;
288 }
289 rb_link_node(&symn->rb_node, parent, p);
290 rb_insert_color(&symn->rb_node, self);
291}
292
293static void symbols__sort_by_name(struct rb_root *self, struct rb_root *source)
294{
295 struct rb_node *nd;
296
297 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
298 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
299 symbols__insert_by_name(self, pos);
300 }
301}
302
303static struct symbol *symbols__find_by_name(struct rb_root *self, const char *name)
304{
305 struct rb_node *n;
306
307 if (self == NULL)
308 return NULL;
309
310 n = self->rb_node;
311
312 while (n) {
313 struct symbol_name_rb_node *s;
314 int cmp;
315
316 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
317 cmp = strcmp(name, s->sym.name);
318
319 if (cmp < 0)
320 n = n->rb_left;
321 else if (cmp > 0)
322 n = n->rb_right;
323 else
324 return &s->sym;
325 }
326
327 return NULL;
328}
329
330struct symbol *dso__find_symbol(struct dso *self,
331 enum map_type type, u64 addr)
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200332{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200333 return symbols__find(&self->symbols[type], addr);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200334}
335
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200336struct symbol *dso__find_symbol_by_name(struct dso *self, enum map_type type,
337 const char *name)
338{
339 return symbols__find_by_name(&self->symbol_names[type], name);
340}
341
342void dso__sort_by_name(struct dso *self, enum map_type type)
343{
344 dso__set_sorted_by_name(self, type);
345 return symbols__sort_by_name(&self->symbol_names[type],
346 &self->symbols[type]);
347}
348
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200349int build_id__sprintf(const u8 *self, int len, char *bf)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200350{
351 char *bid = bf;
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200352 const u8 *raw = self;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200353 int i;
354
355 for (i = 0; i < len; ++i) {
356 sprintf(bid, "%02x", *raw);
357 ++raw;
358 bid += 2;
359 }
360
361 return raw - self;
362}
363
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200364size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300365{
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200366 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200367
368 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200369 return fprintf(fp, "%s", sbuild_id);
370}
371
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200372size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200373{
374 struct rb_node *nd;
375 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
376
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -0300377 if (self->short_name != self->long_name)
378 ret += fprintf(fp, "%s, ", self->long_name);
379 ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type],
380 self->loaded ? "" : "NOT ");
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200381 ret += dso__fprintf_buildid(self, fp);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200382 ret += fprintf(fp, ")\n");
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200383 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
384 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
385 ret += symbol__fprintf(pos, fp);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300386 }
387
388 return ret;
389}
390
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200391int kallsyms__parse(const char *filename, void *arg,
392 int (*process_symbol)(void *arg, const char *name,
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200393 char type, u64 start))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300394{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300395 char *line = NULL;
396 size_t n;
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200397 int err = 0;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200398 FILE *file = fopen(filename, "r");
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300399
400 if (file == NULL)
401 goto out_failure;
402
403 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000404 u64 start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300405 int line_len, len;
406 char symbol_type;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300407 char *symbol_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300408
409 line_len = getline(&line, &n, file);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800410 if (line_len < 0 || !line)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300411 break;
412
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300413 line[--line_len] = '\0'; /* \n */
414
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300415 len = hex2u64(line, &start);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300416
417 len++;
418 if (len + 2 >= line_len)
419 continue;
420
421 symbol_type = toupper(line[len]);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300422 symbol_name = line + len + 2;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300423
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200424 err = process_symbol(arg, symbol_name, symbol_type, start);
425 if (err)
426 break;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300427 }
428
429 free(line);
430 fclose(file);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200431 return err;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300432
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300433out_failure:
434 return -1;
435}
436
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200437struct process_kallsyms_args {
438 struct map *map;
439 struct dso *dso;
440};
441
442static int map__process_kallsym_symbol(void *arg, const char *name,
443 char type, u64 start)
444{
445 struct symbol *sym;
446 struct process_kallsyms_args *a = arg;
447 struct rb_root *root = &a->dso->symbols[a->map->type];
448
449 if (!symbol_type__is_a(type, a->map->type))
450 return 0;
451
452 /*
453 * Will fix up the end later, when we have all symbols sorted.
454 */
455 sym = symbol__new(start, 0, name);
456
457 if (sym == NULL)
458 return -ENOMEM;
459 /*
460 * We will pass the symbols to the filter later, in
461 * map__split_kallsyms, when we have split the maps per module
462 */
463 symbols__insert(root, sym);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800464
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200465 return 0;
466}
467
468/*
469 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
470 * so that we can in the next step set the symbol ->end address and then
471 * call kernel_maps__split_kallsyms.
472 */
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200473static int dso__load_all_kallsyms(struct dso *self, const char *filename,
474 struct map *map)
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200475{
476 struct process_kallsyms_args args = { .map = map, .dso = self, };
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200477 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200478}
479
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300480/*
481 * Split the symbols into maps, making sure there are no overlaps, i.e. the
482 * kernel range is broken in several maps, named [kernel].N, as we don't have
483 * the original ELF section names vmlinux have.
484 */
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -0200485static int dso__split_kallsyms(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200486 symbol_filter_t filter)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300487{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200488 struct map_groups *kmaps = map__kmap(map)->kmaps;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300489 struct machine *machine = kmaps->machine;
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200490 struct map *curr_map = map;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300491 struct symbol *pos;
492 int count = 0;
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200493 struct rb_root *root = &self->symbols[map->type];
494 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300495 int kernel_range = 0;
496
497 while (next) {
498 char *module;
499
500 pos = rb_entry(next, struct symbol, rb_node);
501 next = rb_next(&pos->rb_node);
502
503 module = strchr(pos->name, '\t');
504 if (module) {
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200505 if (!symbol_conf.use_modules)
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200506 goto discard_symbol;
507
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300508 *module++ = '\0';
509
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200510 if (strcmp(curr_map->dso->short_name, module)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800511 if (curr_map != map &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300512 self->kernel == DSO_TYPE_GUEST_KERNEL &&
513 machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800514 /*
515 * We assume all symbols of a module are
516 * continuous in * kallsyms, so curr_map
517 * points to a module and all its
518 * symbols are in its kmap. Mark it as
519 * loaded.
520 */
521 dso__set_loaded(curr_map->dso,
522 curr_map->type);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300523 }
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200524
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800525 curr_map = map_groups__find_by_name(kmaps,
526 map->type, module);
527 if (curr_map == NULL) {
Arnaldo Carvalho de Melo2f519032010-05-17 17:57:59 -0300528 pr_debug("%s/proc/{kallsyms,modules} "
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800529 "inconsistency while looking "
530 "for \"%s\" module!\n",
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300531 machine->root_dir, module);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800532 curr_map = map;
533 goto discard_symbol;
534 }
535
536 if (curr_map->dso->loaded &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300537 !machine__is_default_guest(machine))
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200538 goto discard_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300539 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300540 /*
541 * So that we look just like we get from .ko files,
542 * i.e. not prelinked, relative to map->start.
543 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200544 pos->start = curr_map->map_ip(curr_map, pos->start);
545 pos->end = curr_map->map_ip(curr_map, pos->end);
546 } else if (curr_map != map) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300547 char dso_name[PATH_MAX];
548 struct dso *dso;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300549
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800550 if (self->kernel == DSO_TYPE_GUEST_KERNEL)
551 snprintf(dso_name, sizeof(dso_name),
552 "[guest.kernel].%d",
553 kernel_range++);
554 else
555 snprintf(dso_name, sizeof(dso_name),
556 "[kernel].%d",
557 kernel_range++);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300558
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200559 dso = dso__new(dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300560 if (dso == NULL)
561 return -1;
562
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800563 dso->kernel = self->kernel;
564
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200565 curr_map = map__new2(pos->start, dso, map->type);
Zhang, Yanmin37fe5fc2010-02-25 11:00:51 +0800566 if (curr_map == NULL) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300567 dso__delete(dso);
568 return -1;
569 }
570
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200571 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200572 map_groups__insert(kmaps, curr_map);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300573 ++kernel_range;
574 }
575
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200576 if (filter && filter(curr_map, pos)) {
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200577discard_symbol: rb_erase(&pos->rb_node, root);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200578 symbol__delete(pos);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300579 } else {
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200580 if (curr_map != map) {
581 rb_erase(&pos->rb_node, root);
582 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300583 }
Mike Galbraith9974f492009-07-02 08:05:58 +0200584 count++;
585 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300586 }
587
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800588 if (curr_map != map &&
589 self->kernel == DSO_TYPE_GUEST_KERNEL &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300590 machine__is_default_guest(kmaps->machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800591 dso__set_loaded(curr_map->dso, curr_map->type);
592 }
593
Mike Galbraith9974f492009-07-02 08:05:58 +0200594 return count;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300595}
596
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200597int dso__load_kallsyms(struct dso *self, const char *filename,
598 struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300599{
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200600 if (dso__load_all_kallsyms(self, filename, map) < 0)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300601 return -1;
602
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200603 symbols__fixup_end(&self->symbols[map->type]);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800604 if (self->kernel == DSO_TYPE_GUEST_KERNEL)
605 self->origin = DSO__ORIG_GUEST_KERNEL;
606 else
607 self->origin = DSO__ORIG_KERNEL;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300608
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200609 return dso__split_kallsyms(self, map, filter);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300610}
611
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300612static int dso__load_perf_map(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200613 symbol_filter_t filter)
Pekka Enberg80d496b2009-06-08 21:12:48 +0300614{
615 char *line = NULL;
616 size_t n;
617 FILE *file;
618 int nr_syms = 0;
619
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300620 file = fopen(self->long_name, "r");
Pekka Enberg80d496b2009-06-08 21:12:48 +0300621 if (file == NULL)
622 goto out_failure;
623
624 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000625 u64 start, size;
Pekka Enberg80d496b2009-06-08 21:12:48 +0300626 struct symbol *sym;
627 int line_len, len;
628
629 line_len = getline(&line, &n, file);
630 if (line_len < 0)
631 break;
632
633 if (!line)
634 goto out_failure;
635
636 line[--line_len] = '\0'; /* \n */
637
638 len = hex2u64(line, &start);
639
640 len++;
641 if (len + 2 >= line_len)
642 continue;
643
644 len += hex2u64(line + len, &size);
645
646 len++;
647 if (len + 2 >= line_len)
648 continue;
649
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200650 sym = symbol__new(start, size, line + len);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300651
652 if (sym == NULL)
653 goto out_delete_line;
654
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300655 if (filter && filter(map, sym))
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200656 symbol__delete(sym);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300657 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200658 symbols__insert(&self->symbols[map->type], sym);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300659 nr_syms++;
660 }
661 }
662
663 free(line);
664 fclose(file);
665
666 return nr_syms;
667
668out_delete_line:
669 free(line);
670out_failure:
671 return -1;
672}
673
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300674/**
675 * elf_symtab__for_each_symbol - iterate thru all the symbols
676 *
677 * @self: struct elf_symtab instance to iterate
Ingo Molnar83a09442009-08-15 12:26:57 +0200678 * @idx: uint32_t idx
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300679 * @sym: GElf_Sym iterator
680 */
Ingo Molnar83a09442009-08-15 12:26:57 +0200681#define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
682 for (idx = 0, gelf_getsym(syms, idx, &sym);\
683 idx < nr_syms; \
684 idx++, gelf_getsym(syms, idx, &sym))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300685
686static inline uint8_t elf_sym__type(const GElf_Sym *sym)
687{
688 return GELF_ST_TYPE(sym->st_info);
689}
690
691static inline int elf_sym__is_function(const GElf_Sym *sym)
692{
693 return elf_sym__type(sym) == STT_FUNC &&
694 sym->st_name != 0 &&
Arnaldo Carvalho de Melo81833132009-10-05 23:35:03 -0300695 sym->st_shndx != SHN_UNDEF;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300696}
697
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200698static inline bool elf_sym__is_object(const GElf_Sym *sym)
699{
700 return elf_sym__type(sym) == STT_OBJECT &&
701 sym->st_name != 0 &&
702 sym->st_shndx != SHN_UNDEF;
703}
704
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200705static inline int elf_sym__is_label(const GElf_Sym *sym)
706{
707 return elf_sym__type(sym) == STT_NOTYPE &&
708 sym->st_name != 0 &&
709 sym->st_shndx != SHN_UNDEF &&
710 sym->st_shndx != SHN_ABS;
711}
712
713static inline const char *elf_sec__name(const GElf_Shdr *shdr,
714 const Elf_Data *secstrs)
715{
716 return secstrs->d_buf + shdr->sh_name;
717}
718
719static inline int elf_sec__is_text(const GElf_Shdr *shdr,
720 const Elf_Data *secstrs)
721{
722 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
723}
724
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200725static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
726 const Elf_Data *secstrs)
727{
728 return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
729}
730
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300731static inline const char *elf_sym__name(const GElf_Sym *sym,
732 const Elf_Data *symstrs)
733{
734 return symstrs->d_buf + sym->st_name;
735}
736
737static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
738 GElf_Shdr *shp, const char *name,
Ingo Molnar83a09442009-08-15 12:26:57 +0200739 size_t *idx)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300740{
741 Elf_Scn *sec = NULL;
742 size_t cnt = 1;
743
744 while ((sec = elf_nextscn(elf, sec)) != NULL) {
745 char *str;
746
747 gelf_getshdr(sec, shp);
748 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
749 if (!strcmp(name, str)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200750 if (idx)
751 *idx = cnt;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300752 break;
753 }
754 ++cnt;
755 }
756
757 return sec;
758}
759
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300760#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
761 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
762 idx < nr_entries; \
763 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
764
765#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
766 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
767 idx < nr_entries; \
768 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
769
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300770/*
771 * We need to check if we have a .dynsym, so that we can handle the
772 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
773 * .dynsym or .symtab).
774 * And always look at the original dso, not at debuginfo packages, that
775 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
776 */
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200777static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
778 symbol_filter_t filter)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300779{
780 uint32_t nr_rel_entries, idx;
781 GElf_Sym sym;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000782 u64 plt_offset;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300783 GElf_Shdr shdr_plt;
784 struct symbol *f;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300785 GElf_Shdr shdr_rel_plt, shdr_dynsym;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300786 Elf_Data *reldata, *syms, *symstrs;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300787 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
788 size_t dynsym_idx;
789 GElf_Ehdr ehdr;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300790 char sympltname[1024];
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300791 Elf *elf;
792 int nr = 0, symidx, fd, err = 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300793
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300794 fd = open(self->long_name, O_RDONLY);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300795 if (fd < 0)
796 goto out;
797
Marti Raudsepp84087122009-10-24 19:10:36 +0300798 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300799 if (elf == NULL)
800 goto out_close;
801
802 if (gelf_getehdr(elf, &ehdr) == NULL)
803 goto out_elf_end;
804
805 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
806 ".dynsym", &dynsym_idx);
807 if (scn_dynsym == NULL)
808 goto out_elf_end;
809
810 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300811 ".rela.plt", NULL);
812 if (scn_plt_rel == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300813 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300814 ".rel.plt", NULL);
815 if (scn_plt_rel == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300816 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300817 }
818
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300819 err = -1;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300820
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300821 if (shdr_rel_plt.sh_link != dynsym_idx)
822 goto out_elf_end;
823
824 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
825 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300826
827 /*
Ingo Molnar83a09442009-08-15 12:26:57 +0200828 * Fetch the relocation section to find the idxes to the GOT
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300829 * and the symbols in the .dynsym they refer to.
830 */
831 reldata = elf_getdata(scn_plt_rel, NULL);
832 if (reldata == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300833 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300834
835 syms = elf_getdata(scn_dynsym, NULL);
836 if (syms == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300837 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300838
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300839 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300840 if (scn_symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300841 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300842
843 symstrs = elf_getdata(scn_symstrs, NULL);
844 if (symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300845 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300846
847 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
848 plt_offset = shdr_plt.sh_offset;
849
850 if (shdr_rel_plt.sh_type == SHT_RELA) {
851 GElf_Rela pos_mem, *pos;
852
853 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
854 nr_rel_entries) {
855 symidx = GELF_R_SYM(pos->r_info);
856 plt_offset += shdr_plt.sh_entsize;
857 gelf_getsym(syms, symidx, &sym);
858 snprintf(sympltname, sizeof(sympltname),
859 "%s@plt", elf_sym__name(&sym, symstrs));
860
861 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200862 sympltname);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300863 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300864 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300865
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200866 if (filter && filter(map, f))
867 symbol__delete(f);
868 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200869 symbols__insert(&self->symbols[map->type], f);
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200870 ++nr;
871 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300872 }
873 } else if (shdr_rel_plt.sh_type == SHT_REL) {
874 GElf_Rel pos_mem, *pos;
875 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
876 nr_rel_entries) {
877 symidx = GELF_R_SYM(pos->r_info);
878 plt_offset += shdr_plt.sh_entsize;
879 gelf_getsym(syms, symidx, &sym);
880 snprintf(sympltname, sizeof(sympltname),
881 "%s@plt", elf_sym__name(&sym, symstrs));
882
883 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200884 sympltname);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300885 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300886 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300887
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200888 if (filter && filter(map, f))
889 symbol__delete(f);
890 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200891 symbols__insert(&self->symbols[map->type], f);
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200892 ++nr;
893 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300894 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300895 }
896
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300897 err = 0;
898out_elf_end:
899 elf_end(elf);
900out_close:
901 close(fd);
902
903 if (err == 0)
904 return nr;
905out:
Arnaldo Carvalho de Melofe2197b2010-03-11 20:12:40 -0300906 pr_debug("%s: problems reading %s PLT info.\n",
907 __func__, self->long_name);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300908 return 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300909}
910
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -0200911static bool elf_sym__is_a(GElf_Sym *self, enum map_type type)
912{
913 switch (type) {
914 case MAP__FUNCTION:
915 return elf_sym__is_function(self);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200916 case MAP__VARIABLE:
917 return elf_sym__is_object(self);
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -0200918 default:
919 return false;
920 }
921}
922
923static bool elf_sec__is_a(GElf_Shdr *self, Elf_Data *secstrs, enum map_type type)
924{
925 switch (type) {
926 case MAP__FUNCTION:
927 return elf_sec__is_text(self, secstrs);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -0200928 case MAP__VARIABLE:
929 return elf_sec__is_data(self, secstrs);
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -0200930 default:
931 return false;
932 }
933}
934
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200935static int dso__load_sym(struct dso *self, struct map *map, const char *name,
936 int fd, symbol_filter_t filter, int kmodule)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300937{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200938 struct kmap *kmap = self->kernel ? map__kmap(map) : NULL;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300939 struct map *curr_map = map;
940 struct dso *curr_dso = self;
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200941 Elf_Data *symstrs, *secstrs;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300942 uint32_t nr_syms;
943 int err = -1;
Ingo Molnar83a09442009-08-15 12:26:57 +0200944 uint32_t idx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300945 GElf_Ehdr ehdr;
946 GElf_Shdr shdr;
947 Elf_Data *syms;
948 GElf_Sym sym;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300949 Elf_Scn *sec, *sec_strndx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300950 Elf *elf;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300951 int nr = 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300952
Marti Raudsepp84087122009-10-24 19:10:36 +0300953 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300954 if (elf == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200955 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300956 goto out_close;
957 }
958
959 if (gelf_getehdr(elf, &ehdr) == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200960 pr_err("%s: cannot get elf header.\n", __func__);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300961 goto out_elf_end;
962 }
963
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300964 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
965 if (sec == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300966 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
967 if (sec == NULL)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300968 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300969 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300970
971 syms = elf_getdata(sec, NULL);
972 if (syms == NULL)
973 goto out_elf_end;
974
975 sec = elf_getscn(elf, shdr.sh_link);
976 if (sec == NULL)
977 goto out_elf_end;
978
979 symstrs = elf_getdata(sec, NULL);
980 if (symstrs == NULL)
981 goto out_elf_end;
982
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200983 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
984 if (sec_strndx == NULL)
985 goto out_elf_end;
986
987 secstrs = elf_getdata(sec_strndx, NULL);
Stoyan Gaydarov9b30a262009-07-30 05:25:29 -0500988 if (secstrs == NULL)
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200989 goto out_elf_end;
990
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300991 nr_syms = shdr.sh_size / shdr.sh_entsize;
992
Arjan van de Vene9fbc9d2009-06-06 21:22:33 +0200993 memset(&sym, 0, sizeof(sym));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800994 if (self->kernel == DSO_TYPE_USER) {
Mike Galbraithd20ff6b2009-07-20 14:01:38 +0200995 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -0300996 elf_section_by_name(elf, &ehdr, &shdr,
997 ".gnu.prelink_undo",
998 NULL) != NULL);
Mike Galbraithd20ff6b2009-07-20 14:01:38 +0200999 } else self->adjust_symbols = 0;
1000
Ingo Molnar83a09442009-08-15 12:26:57 +02001001 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001002 struct symbol *f;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001003 const char *elf_name = elf_sym__name(&sym, symstrs);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001004 char *demangled = NULL;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001005 int is_label = elf_sym__is_label(&sym);
1006 const char *section_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001007
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001008 if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
1009 strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
1010 kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
Arnaldo Carvalho de Melo56b03f32010-01-05 16:50:31 -02001011
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -02001012 if (!is_label && !elf_sym__is_a(&sym, map->type))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001013 continue;
1014
1015 sec = elf_getscn(elf, sym.st_shndx);
1016 if (!sec)
1017 goto out_elf_end;
1018
1019 gelf_getshdr(sec, &shdr);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001020
Arnaldo Carvalho de Melod45868d2009-12-11 14:50:38 -02001021 if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001022 continue;
1023
1024 section_name = elf_sec__name(&shdr, secstrs);
Ingo Molnar0b73da32009-06-06 15:48:52 +02001025
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001026 if (self->kernel != DSO_TYPE_USER || kmodule) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001027 char dso_name[PATH_MAX];
1028
1029 if (strcmp(section_name,
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -03001030 (curr_dso->short_name +
1031 self->short_name_len)) == 0)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001032 goto new_symbol;
1033
1034 if (strcmp(section_name, ".text") == 0) {
1035 curr_map = map;
1036 curr_dso = self;
1037 goto new_symbol;
1038 }
1039
1040 snprintf(dso_name, sizeof(dso_name),
1041 "%s%s", self->short_name, section_name);
1042
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001043 curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001044 if (curr_map == NULL) {
1045 u64 start = sym.st_value;
1046
1047 if (kmodule)
1048 start += map->start + shdr.sh_offset;
1049
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001050 curr_dso = dso__new(dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001051 if (curr_dso == NULL)
1052 goto out_elf_end;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001053 curr_dso->kernel = self->kernel;
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001054 curr_map = map__new2(start, curr_dso,
Arnaldo Carvalho de Melo6275ce22010-02-03 16:52:01 -02001055 map->type);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001056 if (curr_map == NULL) {
1057 dso__delete(curr_dso);
1058 goto out_elf_end;
1059 }
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -02001060 curr_map->map_ip = identity__map_ip;
1061 curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -03001062 curr_dso->origin = self->origin;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001063 map_groups__insert(kmap->kmaps, curr_map);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001064 dsos__add(&self->node, curr_dso);
Arnaldo Carvalho de Melo6275ce22010-02-03 16:52:01 -02001065 dso__set_loaded(curr_dso, map->type);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001066 } else
1067 curr_dso = curr_map->dso;
1068
1069 goto new_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001070 }
1071
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001072 if (curr_dso->adjust_symbols) {
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -02001073 pr_debug4("%s: adjusting symbol: st_value: %#Lx "
1074 "sh_addr: %#Lx sh_offset: %#Lx\n", __func__,
1075 (u64)sym.st_value, (u64)shdr.sh_addr,
1076 (u64)shdr.sh_offset);
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001077 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001078 }
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -03001079 /*
1080 * We need to figure out if the object was created from C++ sources
1081 * DWARF DW_compile_unit has this, but we don't always have access
1082 * to it...
1083 */
Ingo Molnar83a09442009-08-15 12:26:57 +02001084 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -03001085 if (demangled != NULL)
Ingo Molnar83a09442009-08-15 12:26:57 +02001086 elf_name = demangled;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001087new_symbol:
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001088 f = symbol__new(sym.st_value, sym.st_size, elf_name);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -03001089 free(demangled);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001090 if (!f)
1091 goto out_elf_end;
1092
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001093 if (filter && filter(curr_map, f))
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001094 symbol__delete(f);
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -03001095 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001096 symbols__insert(&curr_dso->symbols[curr_map->type], f);
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -03001097 nr++;
1098 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001099 }
1100
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001101 /*
1102 * For misannotated, zeroed, ASM function sizes.
1103 */
Arnaldo Carvalho de Melo6275ce22010-02-03 16:52:01 -02001104 if (nr > 0) {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001105 symbols__fixup_end(&self->symbols[map->type]);
Arnaldo Carvalho de Melo6275ce22010-02-03 16:52:01 -02001106 if (kmap) {
1107 /*
1108 * We need to fixup this here too because we create new
1109 * maps here, for things like vsyscall sections.
1110 */
1111 __map_groups__fixup_end(kmap->kmaps, map->type);
1112 }
1113 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001114 err = nr;
1115out_elf_end:
1116 elf_end(elf);
1117out_close:
1118 return err;
1119}
1120
Arnaldo Carvalho de Melo78075ca2009-11-20 20:51:26 -02001121static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
1122{
1123 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
1124}
1125
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001126bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001127{
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -02001128 bool have_build_id = false;
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001129 struct dso *pos;
1130
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001131 list_for_each_entry(pos, head, node) {
1132 if (with_hits && !pos->hit)
1133 continue;
Arnaldo Carvalho de Melof6e14672010-05-19 11:13:02 -03001134 if (pos->has_build_id) {
1135 have_build_id = true;
1136 continue;
1137 }
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -02001138 if (filename__read_build_id(pos->long_name, pos->build_id,
1139 sizeof(pos->build_id)) > 0) {
1140 have_build_id = true;
1141 pos->has_build_id = true;
1142 }
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -02001143 }
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001144
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -02001145 return have_build_id;
Frederic Weisbecker57f395a2009-11-11 04:51:04 +01001146}
1147
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001148/*
1149 * Align offset to 4 bytes as needed for note name and descriptor data.
1150 */
1151#define NOTE_ALIGN(n) (((n) + 3) & -4U)
1152
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -02001153int filename__read_build_id(const char *filename, void *bf, size_t size)
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001154{
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -02001155 int fd, err = -1;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001156 GElf_Ehdr ehdr;
1157 GElf_Shdr shdr;
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001158 Elf_Data *data;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001159 Elf_Scn *sec;
Pekka Enberge57cfcd2009-11-22 12:29:44 +02001160 Elf_Kind ek;
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001161 void *ptr;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001162 Elf *elf;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001163
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -02001164 if (size < BUILD_ID_SIZE)
1165 goto out;
1166
1167 fd = open(filename, O_RDONLY);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001168 if (fd < 0)
1169 goto out;
1170
Marti Raudsepp84087122009-10-24 19:10:36 +03001171 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001172 if (elf == NULL) {
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001173 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001174 goto out_close;
1175 }
1176
Pekka Enberge57cfcd2009-11-22 12:29:44 +02001177 ek = elf_kind(elf);
1178 if (ek != ELF_K_ELF)
1179 goto out_elf_end;
1180
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001181 if (gelf_getehdr(elf, &ehdr) == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001182 pr_err("%s: cannot get elf header.\n", __func__);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001183 goto out_elf_end;
1184 }
1185
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -02001186 sec = elf_section_by_name(elf, &ehdr, &shdr,
1187 ".note.gnu.build-id", NULL);
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001188 if (sec == NULL) {
1189 sec = elf_section_by_name(elf, &ehdr, &shdr,
1190 ".notes", NULL);
1191 if (sec == NULL)
1192 goto out_elf_end;
1193 }
1194
1195 data = elf_getdata(sec, NULL);
1196 if (data == NULL)
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001197 goto out_elf_end;
1198
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001199 ptr = data->d_buf;
1200 while (ptr < (data->d_buf + data->d_size)) {
1201 GElf_Nhdr *nhdr = ptr;
1202 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1203 descsz = NOTE_ALIGN(nhdr->n_descsz);
1204 const char *name;
1205
1206 ptr += sizeof(*nhdr);
1207 name = ptr;
1208 ptr += namesz;
1209 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1210 nhdr->n_namesz == sizeof("GNU")) {
1211 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1212 memcpy(bf, ptr, BUILD_ID_SIZE);
1213 err = BUILD_ID_SIZE;
1214 break;
1215 }
1216 }
1217 ptr += descsz;
1218 }
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -02001219out_elf_end:
1220 elf_end(elf);
1221out_close:
1222 close(fd);
1223out:
1224 return err;
1225}
1226
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001227int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1228{
1229 int fd, err = -1;
1230
1231 if (size < BUILD_ID_SIZE)
1232 goto out;
1233
1234 fd = open(filename, O_RDONLY);
1235 if (fd < 0)
1236 goto out;
1237
1238 while (1) {
1239 char bf[BUFSIZ];
1240 GElf_Nhdr nhdr;
1241 int namesz, descsz;
1242
1243 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1244 break;
1245
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001246 namesz = NOTE_ALIGN(nhdr.n_namesz);
1247 descsz = NOTE_ALIGN(nhdr.n_descsz);
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001248 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1249 nhdr.n_namesz == sizeof("GNU")) {
1250 if (read(fd, bf, namesz) != namesz)
1251 break;
1252 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1253 if (read(fd, build_id,
1254 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1255 err = 0;
1256 break;
1257 }
1258 } else if (read(fd, bf, descsz) != descsz)
1259 break;
1260 } else {
1261 int n = namesz + descsz;
1262 if (read(fd, bf, n) != n)
1263 break;
1264 }
1265 }
1266 close(fd);
1267out:
1268 return err;
1269}
1270
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001271char dso__symtab_origin(const struct dso *self)
1272{
1273 static const char origin[] = {
1274 [DSO__ORIG_KERNEL] = 'k',
1275 [DSO__ORIG_JAVA_JIT] = 'j',
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02001276 [DSO__ORIG_BUILD_ID_CACHE] = 'B',
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001277 [DSO__ORIG_FEDORA] = 'f',
1278 [DSO__ORIG_UBUNTU] = 'u',
1279 [DSO__ORIG_BUILDID] = 'b',
1280 [DSO__ORIG_DSO] = 'd',
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001281 [DSO__ORIG_KMODULE] = 'K',
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001282 [DSO__ORIG_GUEST_KERNEL] = 'g',
1283 [DSO__ORIG_GUEST_KMODULE] = 'G',
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001284 };
1285
1286 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1287 return '!';
1288 return origin[self->origin];
1289}
1290
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001291int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001292{
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001293 int size = PATH_MAX;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001294 char *name;
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001295 u8 build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02001296 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001297 int ret = -1;
1298 int fd;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001299 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001300 const char *root_dir;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001301
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001302 dso__set_loaded(self, map->type);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001303
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001304 if (self->kernel == DSO_TYPE_KERNEL)
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001305 return dso__load_kernel_sym(self, map, filter);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001306 else if (self->kernel == DSO_TYPE_GUEST_KERNEL)
1307 return dso__load_guest_kernel_sym(self, map, filter);
1308
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001309 if (map->groups && map->groups->machine)
1310 machine = map->groups->machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001311 else
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001312 machine = NULL;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001313
1314 name = malloc(size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001315 if (!name)
1316 return -1;
1317
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -03001318 self->adjust_symbols = 0;
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001319
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001320 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001321 ret = dso__load_perf_map(self, map, filter);
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001322 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1323 DSO__ORIG_NOT_FOUND;
1324 return ret;
1325 }
1326
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02001327 self->origin = DSO__ORIG_BUILD_ID_CACHE;
Pekka Enberg80d496b2009-06-08 21:12:48 +03001328
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02001329 if (self->has_build_id) {
1330 build_id__sprintf(self->build_id, sizeof(self->build_id),
1331 build_id_hex);
1332 snprintf(name, size, "%s/%s/.build-id/%.2s/%s",
1333 getenv("HOME"), DEBUG_CACHE_DIR,
1334 build_id_hex, build_id_hex + 2);
1335 goto open_file;
1336 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001337more:
1338 do {
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001339 self->origin++;
1340 switch (self->origin) {
1341 case DSO__ORIG_FEDORA:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001342 snprintf(name, size, "/usr/lib/debug%s.debug",
1343 self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001344 break;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001345 case DSO__ORIG_UBUNTU:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001346 snprintf(name, size, "/usr/lib/debug%s",
1347 self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001348 break;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001349 case DSO__ORIG_BUILDID:
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001350 if (filename__read_build_id(self->long_name, build_id,
1351 sizeof(build_id))) {
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001352 build_id__sprintf(build_id, sizeof(build_id),
1353 build_id_hex);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001354 snprintf(name, size,
1355 "/usr/lib/debug/.build-id/%.2s/%s.debug",
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001356 build_id_hex, build_id_hex + 2);
1357 if (self->has_build_id)
1358 goto compare_build_id;
1359 break;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001360 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001361 self->origin++;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001362 /* Fall thru */
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001363 case DSO__ORIG_DSO:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001364 snprintf(name, size, "%s", self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001365 break;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001366 case DSO__ORIG_GUEST_KMODULE:
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001367 if (map->groups && map->groups->machine)
1368 root_dir = map->groups->machine->root_dir;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001369 else
1370 root_dir = "";
1371 snprintf(name, size, "%s%s", root_dir, self->long_name);
1372 break;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001373
1374 default:
1375 goto out;
1376 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001377
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001378 if (self->has_build_id) {
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001379 if (filename__read_build_id(name, build_id,
1380 sizeof(build_id)) < 0)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001381 goto more;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001382compare_build_id:
Arnaldo Carvalho de Melo78075ca2009-11-20 20:51:26 -02001383 if (!dso__build_id_equal(self, build_id))
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001384 goto more;
1385 }
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02001386open_file:
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001387 fd = open(name, O_RDONLY);
1388 } while (fd < 0);
1389
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001390 ret = dso__load_sym(self, map, name, fd, filter, 0);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001391 close(fd);
1392
1393 /*
1394 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1395 */
1396 if (!ret)
1397 goto more;
1398
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001399 if (ret > 0) {
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -02001400 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001401 if (nr_plt > 0)
1402 ret += nr_plt;
1403 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001404out:
1405 free(name);
Arnaldo Carvalho de Melo1340e6b2009-08-11 17:04:36 -03001406 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1407 return 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001408 return ret;
1409}
1410
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -02001411struct map *map_groups__find_by_name(struct map_groups *self,
1412 enum map_type type, const char *name)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001413{
1414 struct rb_node *nd;
1415
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -02001416 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001417 struct map *map = rb_entry(nd, struct map, rb_node);
1418
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001419 if (map->dso && strcmp(map->dso->short_name, name) == 0)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001420 return map;
1421 }
1422
1423 return NULL;
1424}
1425
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001426static int dso__kernel_module_get_build_id(struct dso *self,
1427 const char *root_dir)
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001428{
1429 char filename[PATH_MAX];
1430 /*
1431 * kernel module short names are of the form "[module]" and
1432 * we need just "module" here.
1433 */
1434 const char *name = self->short_name + 1;
1435
1436 snprintf(filename, sizeof(filename),
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001437 "%s/sys/module/%.*s/notes/.note.gnu.build-id",
1438 root_dir, (int)strlen(name) - 1, name);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001439
1440 if (sysfs__read_build_id(filename, self->build_id,
1441 sizeof(self->build_id)) == 0)
1442 self->has_build_id = true;
1443
1444 return 0;
1445}
1446
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001447static int map_groups__set_modules_path_dir(struct map_groups *self,
1448 const char *dir_name)
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001449{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001450 struct dirent *dent;
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03001451 DIR *dir = opendir(dir_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001452
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001453 if (!dir) {
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03001454 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001455 return -1;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001456 }
1457
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001458 while ((dent = readdir(dir)) != NULL) {
1459 char path[PATH_MAX];
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001460 struct stat st;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001461
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001462 /*sshfs might return bad dent->d_type, so we have to stat*/
1463 sprintf(path, "%s/%s", dir_name, dent->d_name);
1464 if (stat(path, &st))
1465 continue;
1466
1467 if (S_ISDIR(st.st_mode)) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001468 if (!strcmp(dent->d_name, ".") ||
1469 !strcmp(dent->d_name, ".."))
1470 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001471
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001472 snprintf(path, sizeof(path), "%s/%s",
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03001473 dir_name, dent->d_name);
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001474 if (map_groups__set_modules_path_dir(self, path) < 0)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001475 goto failure;
1476 } else {
1477 char *dot = strrchr(dent->d_name, '.'),
1478 dso_name[PATH_MAX];
1479 struct map *map;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001480 char *long_name;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001481
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001482 if (dot == NULL || strcmp(dot, ".ko"))
1483 continue;
1484 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1485 (int)(dot - dent->d_name), dent->d_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001486
Arnaldo Carvalho de Meloa2a99e82009-10-05 14:26:18 -03001487 strxfrchar(dso_name, '-', '_');
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001488 map = map_groups__find_by_name(self, MAP__FUNCTION, dso_name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001489 if (map == NULL)
1490 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001491
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001492 snprintf(path, sizeof(path), "%s/%s",
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03001493 dir_name, dent->d_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001494
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001495 long_name = strdup(path);
1496 if (long_name == NULL)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001497 goto failure;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001498 dso__set_long_name(map->dso, long_name);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001499 dso__kernel_module_get_build_id(map->dso, "");
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001500 }
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001501 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001502
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001503 return 0;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001504failure:
1505 closedir(dir);
1506 return -1;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001507}
1508
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001509static char *get_kernel_version(const char *root_dir)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001510{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001511 char version[PATH_MAX];
1512 FILE *file;
1513 char *name, *tmp;
1514 const char *prefix = "Linux version ";
1515
1516 sprintf(version, "%s/proc/version", root_dir);
1517 file = fopen(version, "r");
1518 if (!file)
1519 return NULL;
1520
1521 version[0] = '\0';
1522 tmp = fgets(version, sizeof(version), file);
1523 fclose(file);
1524
1525 name = strstr(version, prefix);
1526 if (!name)
1527 return NULL;
1528 name += strlen(prefix);
1529 tmp = strchr(name, ' ');
1530 if (tmp)
1531 *tmp = '\0';
1532
1533 return strdup(name);
1534}
1535
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001536static int machine__set_modules_path(struct machine *self)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001537{
1538 char *version;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001539 char modules_path[PATH_MAX];
1540
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001541 version = get_kernel_version(self->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001542 if (!version)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001543 return -1;
1544
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001545 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001546 self->root_dir, version);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001547 free(version);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001548
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001549 return map_groups__set_modules_path_dir(&self->kmaps, modules_path);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001550}
1551
1552/*
1553 * Constructor variant for modules (where we know from /proc/modules where
1554 * they are loaded) and for vmlinux, where only after we load all the
1555 * symbols we'll know where it starts and ends.
1556 */
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001557static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001558{
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03001559 struct map *self = calloc(1, (sizeof(*self) +
1560 (dso->kernel ? sizeof(struct kmap) : 0)));
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001561 if (self != NULL) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001562 /*
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -02001563 * ->end will be filled after we load all the symbols
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001564 */
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001565 map__init(self, type, start, 0, 0, dso);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001566 }
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -02001567
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001568 return self;
1569}
1570
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001571struct map *machine__new_module(struct machine *self, u64 start,
1572 const char *filename)
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001573{
1574 struct map *map;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001575 struct dso *dso = __dsos__findnew(&self->kernel_dsos, filename);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001576
1577 if (dso == NULL)
1578 return NULL;
1579
1580 map = map__new2(start, dso, MAP__FUNCTION);
1581 if (map == NULL)
1582 return NULL;
1583
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001584 if (machine__is_host(self))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001585 dso->origin = DSO__ORIG_KMODULE;
1586 else
1587 dso->origin = DSO__ORIG_GUEST_KMODULE;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001588 map_groups__insert(&self->kmaps, map);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001589 return map;
1590}
1591
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001592static int machine__create_modules(struct machine *self)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001593{
1594 char *line = NULL;
1595 size_t n;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001596 FILE *file;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001597 struct map *map;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001598 const char *modules;
1599 char path[PATH_MAX];
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001600
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001601 if (machine__is_default_guest(self))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001602 modules = symbol_conf.default_guest_modules;
1603 else {
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001604 sprintf(path, "%s/proc/modules", self->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001605 modules = path;
1606 }
1607
1608 file = fopen(modules, "r");
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001609 if (file == NULL)
1610 return -1;
1611
1612 while (!feof(file)) {
1613 char name[PATH_MAX];
1614 u64 start;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001615 char *sep;
1616 int line_len;
1617
1618 line_len = getline(&line, &n, file);
1619 if (line_len < 0)
1620 break;
1621
1622 if (!line)
1623 goto out_failure;
1624
1625 line[--line_len] = '\0'; /* \n */
1626
1627 sep = strrchr(line, 'x');
1628 if (sep == NULL)
1629 continue;
1630
1631 hex2u64(sep + 1, &start);
1632
1633 sep = strchr(line, ' ');
1634 if (sep == NULL)
1635 continue;
1636
1637 *sep = '\0';
1638
1639 snprintf(name, sizeof(name), "[%s]", line);
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001640 map = machine__new_module(self, start, name);
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001641 if (map == NULL)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001642 goto out_delete_line;
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001643 dso__kernel_module_get_build_id(map->dso, self->root_dir);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001644 }
1645
1646 free(line);
1647 fclose(file);
1648
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03001649 return machine__set_modules_path(self);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001650
1651out_delete_line:
1652 free(line);
1653out_failure:
1654 return -1;
1655}
1656
Arnaldo Carvalho de Melo9958e1f2009-12-11 14:50:36 -02001657static int dso__load_vmlinux(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001658 const char *vmlinux, symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001659{
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001660 int err = -1, fd;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001661
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001662 if (self->has_build_id) {
1663 u8 build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001664
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001665 if (filename__read_build_id(vmlinux, build_id,
1666 sizeof(build_id)) < 0) {
1667 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1668 return -1;
1669 }
1670 if (!dso__build_id_equal(self, build_id)) {
1671 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1672 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1673
1674 build_id__sprintf(self->build_id,
1675 sizeof(self->build_id),
1676 expected_build_id);
1677 build_id__sprintf(build_id, sizeof(build_id),
1678 vmlinux_build_id);
1679 pr_debug("build_id in %s is %s while expected is %s, "
1680 "ignoring it\n", vmlinux, vmlinux_build_id,
1681 expected_build_id);
1682 return -1;
1683 }
1684 }
1685
1686 fd = open(vmlinux, O_RDONLY);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001687 if (fd < 0)
1688 return -1;
1689
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001690 dso__set_loaded(self, map->type);
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001691 err = dso__load_sym(self, map, vmlinux, fd, filter, 0);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001692 close(fd);
1693
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001694 if (err > 0)
1695 pr_debug("Using %s for symbols\n", vmlinux);
1696
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001697 return err;
1698}
1699
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001700int dso__load_vmlinux_path(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001701 symbol_filter_t filter)
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001702{
1703 int i, err = 0;
1704
1705 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1706 vmlinux_path__nr_entries);
1707
1708 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001709 err = dso__load_vmlinux(self, map, vmlinux_path[i], filter);
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001710 if (err > 0) {
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001711 dso__set_long_name(self, strdup(vmlinux_path[i]));
1712 break;
1713 }
1714 }
1715
1716 return err;
1717}
1718
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001719static int dso__load_kernel_sym(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001720 symbol_filter_t filter)
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001721{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001722 int err;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001723 const char *kallsyms_filename = NULL;
1724 char *kallsyms_allocated_filename = NULL;
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001725 /*
1726 * Step 1: if the user specified a vmlinux filename, use it and only
1727 * it, reporting errors to the user if it cannot be used.
1728 *
1729 * For instance, try to analyse an ARM perf.data file _without_ a
1730 * build-id, or if the user specifies the wrong path to the right
1731 * vmlinux file, obviously we can't fallback to another vmlinux (a
1732 * x86_86 one, on the machine where analysis is being performed, say),
1733 * or worse, /proc/kallsyms.
1734 *
1735 * If the specified file _has_ a build-id and there is a build-id
1736 * section in the perf.data file, we will still do the expected
1737 * validation in dso__load_vmlinux and will bail out if they don't
1738 * match.
1739 */
1740 if (symbol_conf.vmlinux_name != NULL) {
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001741 err = dso__load_vmlinux(self, map,
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001742 symbol_conf.vmlinux_name, filter);
1743 goto out_try_fixup;
1744 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001745
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001746 if (vmlinux_path != NULL) {
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001747 err = dso__load_vmlinux_path(self, map, filter);
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001748 if (err > 0)
1749 goto out_fixup;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001750 }
1751
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001752 /*
1753 * Say the kernel DSO was created when processing the build-id header table,
1754 * we have a build-id, so check if it is the same as the running kernel,
1755 * using it if it is.
1756 */
1757 if (self->has_build_id) {
1758 u8 kallsyms_build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001759 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001760
1761 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
Arnaldo Carvalho de Melo8d0591f2010-01-14 18:30:05 -02001762 sizeof(kallsyms_build_id)) == 0) {
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001763 if (dso__build_id_equal(self, kallsyms_build_id)) {
1764 kallsyms_filename = "/proc/kallsyms";
Arnaldo Carvalho de Melo8d0591f2010-01-14 18:30:05 -02001765 goto do_kallsyms;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001766 }
Arnaldo Carvalho de Melo8d0591f2010-01-14 18:30:05 -02001767 }
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001768 /*
1769 * Now look if we have it on the build-id cache in
1770 * $HOME/.debug/[kernel.kallsyms].
1771 */
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001772 build_id__sprintf(self->build_id, sizeof(self->build_id),
1773 sbuild_id);
1774
1775 if (asprintf(&kallsyms_allocated_filename,
1776 "%s/.debug/[kernel.kallsyms]/%s",
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001777 getenv("HOME"), sbuild_id) == -1) {
1778 pr_err("Not enough memory for kallsyms file lookup\n");
Arnaldo Carvalho de Melo8d0591f2010-01-14 18:30:05 -02001779 return -1;
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001780 }
Arnaldo Carvalho de Melo8d0591f2010-01-14 18:30:05 -02001781
Arnaldo Carvalho de Melo19fc2de2010-01-22 14:35:02 -02001782 kallsyms_filename = kallsyms_allocated_filename;
1783
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001784 if (access(kallsyms_filename, F_OK)) {
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001785 pr_err("No kallsyms or vmlinux with build-id %s "
1786 "was found\n", sbuild_id);
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001787 free(kallsyms_allocated_filename);
1788 return -1;
1789 }
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001790 } else {
1791 /*
1792 * Last resort, if we don't have a build-id and couldn't find
1793 * any vmlinux file, try the running kernel kallsyms table.
1794 */
1795 kallsyms_filename = "/proc/kallsyms";
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -02001796 }
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001797
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001798do_kallsyms:
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001799 err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001800 if (err > 0)
1801 pr_debug("Using %s for symbols\n", kallsyms_filename);
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001802 free(kallsyms_allocated_filename);
1803
1804out_try_fixup:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001805 if (err > 0) {
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001806out_fixup:
Arnaldo Carvalho de Meloe1c7c6a2010-01-22 14:35:01 -02001807 if (kallsyms_filename != NULL)
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001808 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001809 map__fixup_start(map);
1810 map__fixup_end(map);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001811 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001812
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001813 return err;
1814}
1815
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001816static int dso__load_guest_kernel_sym(struct dso *self, struct map *map,
1817 symbol_filter_t filter)
1818{
1819 int err;
1820 const char *kallsyms_filename = NULL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001821 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001822 char path[PATH_MAX];
1823
1824 if (!map->groups) {
1825 pr_debug("Guest kernel map hasn't the point to groups\n");
1826 return -1;
1827 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001828 machine = map->groups->machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001829
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001830 if (machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001831 /*
1832 * if the user specified a vmlinux filename, use it and only
1833 * it, reporting errors to the user if it cannot be used.
1834 * Or use file guest_kallsyms inputted by user on commandline
1835 */
1836 if (symbol_conf.default_guest_vmlinux_name != NULL) {
1837 err = dso__load_vmlinux(self, map,
1838 symbol_conf.default_guest_vmlinux_name, filter);
1839 goto out_try_fixup;
1840 }
1841
1842 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1843 if (!kallsyms_filename)
1844 return -1;
1845 } else {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001846 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001847 kallsyms_filename = path;
1848 }
1849
1850 err = dso__load_kallsyms(self, kallsyms_filename, map, filter);
1851 if (err > 0)
1852 pr_debug("Using %s for symbols\n", kallsyms_filename);
1853
1854out_try_fixup:
1855 if (err > 0) {
1856 if (kallsyms_filename != NULL) {
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -03001857 machine__mmap_name(machine, path, sizeof(path));
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001858 dso__set_long_name(self, strdup(path));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001859 }
1860 map__fixup_start(map);
1861 map__fixup_end(map);
1862 }
1863
1864 return err;
1865}
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001866
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001867static void dsos__add(struct list_head *head, struct dso *dso)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001868{
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001869 list_add_tail(&dso->node, head);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001870}
1871
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001872static struct dso *dsos__find(struct list_head *head, const char *name)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001873{
1874 struct dso *pos;
1875
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001876 list_for_each_entry(pos, head, node)
Arnaldo Carvalho de Melocf4e5b02010-01-14 23:45:27 -02001877 if (strcmp(pos->long_name, name) == 0)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001878 return pos;
1879 return NULL;
1880}
1881
Arnaldo Carvalho de Meloa89e5ab2010-01-07 19:59:39 -02001882struct dso *__dsos__findnew(struct list_head *head, const char *name)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001883{
Arnaldo Carvalho de Meloa89e5ab2010-01-07 19:59:39 -02001884 struct dso *dso = dsos__find(head, name);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001885
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -02001886 if (!dso) {
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001887 dso = dso__new(name);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001888 if (dso != NULL) {
Arnaldo Carvalho de Meloa89e5ab2010-01-07 19:59:39 -02001889 dsos__add(head, dso);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001890 dso__set_basename(dso);
1891 }
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001892 }
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001893
1894 return dso;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001895}
1896
Arnaldo Carvalho de Melo1f626bc2010-05-09 19:57:08 -03001897size_t __dsos__fprintf(struct list_head *head, FILE *fp)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001898{
1899 struct dso *pos;
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001900 size_t ret = 0;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001901
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001902 list_for_each_entry(pos, head, node) {
1903 int i;
1904 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001905 ret += dso__fprintf(pos, i, fp);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001906 }
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001907
1908 return ret;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001909}
1910
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001911size_t machines__fprintf_dsos(struct rb_root *self, FILE *fp)
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001912{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001913 struct rb_node *nd;
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001914 size_t ret = 0;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001915
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001916 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001917 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001918 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
1919 ret += __dsos__fprintf(&pos->user_dsos, fp);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001920 }
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001921
1922 return ret;
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001923}
1924
Arnaldo Carvalho de Melo88d3d9b2010-01-14 23:45:30 -02001925static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1926 bool with_hits)
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001927{
1928 struct dso *pos;
1929 size_t ret = 0;
1930
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001931 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo88d3d9b2010-01-14 23:45:30 -02001932 if (with_hits && !pos->hit)
1933 continue;
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001934 ret += dso__fprintf_buildid(pos, fp);
Arnaldo Carvalho de Melo1124ba72009-11-16 21:45:25 -02001935 ret += fprintf(fp, " %s\n", pos->long_name);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001936 }
1937 return ret;
1938}
1939
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001940size_t machines__fprintf_dsos_buildid(struct rb_root *self, FILE *fp, bool with_hits)
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001941{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001942 struct rb_node *nd;
1943 size_t ret = 0;
1944
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -03001945 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001946 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1947 ret += __dsos__fprintf_buildid(&pos->kernel_dsos, fp, with_hits);
1948 ret += __dsos__fprintf_buildid(&pos->user_dsos, fp, with_hits);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001949 }
1950 return ret;
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001951}
1952
Arnaldo Carvalho de Melofd1d9082010-01-27 21:05:51 -02001953struct dso *dso__new_kernel(const char *name)
1954{
1955 struct dso *self = dso__new(name ?: "[kernel.kallsyms]");
1956
1957 if (self != NULL) {
Arnaldo Carvalho de Melob63be8d2010-03-15 15:03:50 -03001958 dso__set_short_name(self, "[kernel]");
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001959 self->kernel = DSO_TYPE_KERNEL;
Arnaldo Carvalho de Melofd1d9082010-01-27 21:05:51 -02001960 }
1961
1962 return self;
1963}
1964
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001965static struct dso *dso__new_guest_kernel(struct machine *machine,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001966 const char *name)
Arnaldo Carvalho de Melofd1d9082010-01-27 21:05:51 -02001967{
Arnaldo Carvalho de Melo48ea8f52010-04-27 21:19:05 -03001968 char bf[PATH_MAX];
1969 struct dso *self = dso__new(name ?: machine__mmap_name(machine, bf, sizeof(bf)));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001970
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001971 if (self != NULL) {
1972 dso__set_short_name(self, "[guest.kernel]");
1973 self->kernel = DSO_TYPE_GUEST_KERNEL;
1974 }
1975
1976 return self;
1977}
1978
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001979void dso__read_running_kernel_build_id(struct dso *self, struct machine *machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001980{
1981 char path[PATH_MAX];
1982
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001983 if (machine__is_default_guest(machine))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001984 return;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001985 sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001986 if (sysfs__read_build_id(path, self->build_id,
Arnaldo Carvalho de Melofd1d9082010-01-27 21:05:51 -02001987 sizeof(self->build_id)) == 0)
1988 self->has_build_id = true;
1989}
1990
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03001991static struct dso *machine__create_kernel(struct machine *self)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001992{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001993 const char *vmlinux_name = NULL;
1994 struct dso *kernel;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001995
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03001996 if (machine__is_host(self)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001997 vmlinux_name = symbol_conf.vmlinux_name;
1998 kernel = dso__new_kernel(vmlinux_name);
1999 } else {
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002000 if (machine__is_default_guest(self))
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002001 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002002 kernel = dso__new_guest_kernel(self, vmlinux_name);
Arnaldo Carvalho de Melo8d92c022010-02-03 16:52:02 -02002003 }
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002004
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002005 if (kernel != NULL) {
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002006 dso__read_running_kernel_build_id(kernel, self);
2007 dsos__add(&self->kernel_dsos, kernel);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002008 }
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002009 return kernel;
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002010}
2011
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002012int __machine__create_kernel_maps(struct machine *self, struct dso *kernel)
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002013{
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02002014 enum map_type type;
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002015
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02002016 for (type = 0; type < MAP__NR_TYPES; ++type) {
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02002017 struct kmap *kmap;
2018
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002019 self->vmlinux_maps[type] = map__new2(0, kernel, type);
2020 if (self->vmlinux_maps[type] == NULL)
Arnaldo Carvalho de Melode176482010-01-04 16:19:29 -02002021 return -1;
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002022
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002023 self->vmlinux_maps[type]->map_ip =
2024 self->vmlinux_maps[type]->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02002025
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002026 kmap = map__kmap(self->vmlinux_maps[type]);
2027 kmap->kmaps = &self->kmaps;
2028 map_groups__insert(&self->kmaps, self->vmlinux_maps[type]);
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002029 }
2030
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -02002031 return 0;
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02002032}
2033
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002034int machine__create_kernel_maps(struct machine *self)
2035{
2036 struct dso *kernel = machine__create_kernel(self);
2037
2038 if (kernel == NULL ||
2039 __machine__create_kernel_maps(self, kernel) < 0)
2040 return -1;
2041
2042 if (symbol_conf.use_modules && machine__create_modules(self) < 0)
2043 pr_debug("Problems creating module maps, continuing anyway...\n");
2044 /*
2045 * Now that we have all the maps created, just set the ->end of them:
2046 */
2047 map_groups__fixup_end(&self->kmaps);
2048 return 0;
2049}
2050
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002051static void vmlinux_path__exit(void)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02002052{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002053 while (--vmlinux_path__nr_entries >= 0) {
2054 free(vmlinux_path[vmlinux_path__nr_entries]);
2055 vmlinux_path[vmlinux_path__nr_entries] = NULL;
2056 }
2057
2058 free(vmlinux_path);
2059 vmlinux_path = NULL;
2060}
2061
2062static int vmlinux_path__init(void)
2063{
2064 struct utsname uts;
2065 char bf[PATH_MAX];
2066
2067 if (uname(&uts) < 0)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02002068 return -1;
2069
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002070 vmlinux_path = malloc(sizeof(char *) * 5);
2071 if (vmlinux_path == NULL)
2072 return -1;
2073
2074 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
2075 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2076 goto out_fail;
2077 ++vmlinux_path__nr_entries;
2078 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
2079 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2080 goto out_fail;
2081 ++vmlinux_path__nr_entries;
2082 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
2083 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2084 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2085 goto out_fail;
2086 ++vmlinux_path__nr_entries;
2087 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
2088 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2089 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2090 goto out_fail;
2091 ++vmlinux_path__nr_entries;
2092 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
2093 uts.release);
2094 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
2095 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2096 goto out_fail;
2097 ++vmlinux_path__nr_entries;
2098
2099 return 0;
2100
2101out_fail:
2102 vmlinux_path__exit();
2103 return -1;
2104}
2105
Arnaldo Carvalho de Melob0a9ab62010-03-15 11:46:58 -03002106size_t vmlinux_path__fprintf(FILE *fp)
2107{
2108 int i;
2109 size_t printed = 0;
2110
2111 for (i = 0; i < vmlinux_path__nr_entries; ++i)
2112 printed += fprintf(fp, "[%d] %s\n", i, vmlinux_path[i]);
2113
2114 return printed;
2115}
2116
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002117static int setup_list(struct strlist **list, const char *list_str,
2118 const char *list_name)
2119{
2120 if (list_str == NULL)
2121 return 0;
2122
2123 *list = strlist__new(true, list_str);
2124 if (!*list) {
2125 pr_err("problems parsing %s list\n", list_name);
2126 return -1;
2127 }
2128 return 0;
2129}
2130
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02002131int symbol__init(void)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002132{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02002133 elf_version(EV_CURRENT);
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02002134 if (symbol_conf.sort_by_name)
2135 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2136 sizeof(struct symbol));
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02002137
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02002138 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002139 return -1;
2140
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02002141 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2142 pr_err("'.' is the only non valid --field-separator argument\n");
2143 return -1;
2144 }
2145
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002146 if (setup_list(&symbol_conf.dso_list,
2147 symbol_conf.dso_list_str, "dso") < 0)
2148 return -1;
2149
2150 if (setup_list(&symbol_conf.comm_list,
2151 symbol_conf.comm_list_str, "comm") < 0)
2152 goto out_free_dso_list;
2153
2154 if (setup_list(&symbol_conf.sym_list,
2155 symbol_conf.sym_list_str, "symbol") < 0)
2156 goto out_free_comm_list;
2157
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002158 return 0;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002159
2160out_free_dso_list:
2161 strlist__delete(symbol_conf.dso_list);
2162out_free_comm_list:
2163 strlist__delete(symbol_conf.comm_list);
2164 return -1;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002165}
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002166
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002167int machines__create_kernel_maps(struct rb_root *self, pid_t pid)
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002168{
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002169 struct machine *machine = machines__findnew(self, pid);
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02002170
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002171 if (machine == NULL)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002172 return -1;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002173
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002174 return machine__create_kernel_maps(machine);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002175}
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03002176
2177static int hex(char ch)
2178{
2179 if ((ch >= '0') && (ch <= '9'))
2180 return ch - '0';
2181 if ((ch >= 'a') && (ch <= 'f'))
2182 return ch - 'a' + 10;
2183 if ((ch >= 'A') && (ch <= 'F'))
2184 return ch - 'A' + 10;
2185 return -1;
2186}
2187
2188/*
2189 * While we find nice hex chars, build a long_val.
2190 * Return number of chars processed.
2191 */
2192int hex2u64(const char *ptr, u64 *long_val)
2193{
2194 const char *p = ptr;
2195 *long_val = 0;
2196
2197 while (*p) {
2198 const int hex_val = hex(*p);
2199
2200 if (hex_val < 0)
2201 break;
2202
2203 *long_val = (*long_val << 4) | hex_val;
2204 p++;
2205 }
2206
2207 return p - ptr;
2208}
2209
2210char *strxfrchar(char *s, char from, char to)
2211{
2212 char *p = s;
2213
2214 while ((p = strchr(p, from)) != NULL)
2215 *p++ = to;
2216
2217 return s;
2218}
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002219
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002220int machines__create_guest_kernel_maps(struct rb_root *self)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002221{
2222 int ret = 0;
2223 struct dirent **namelist = NULL;
2224 int i, items = 0;
2225 char path[PATH_MAX];
2226 pid_t pid;
2227
2228 if (symbol_conf.default_guest_vmlinux_name ||
2229 symbol_conf.default_guest_modules ||
2230 symbol_conf.default_guest_kallsyms) {
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002231 machines__create_kernel_maps(self, DEFAULT_GUEST_KERNEL_ID);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002232 }
2233
2234 if (symbol_conf.guestmount) {
2235 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
2236 if (items <= 0)
2237 return -ENOENT;
2238 for (i = 0; i < items; i++) {
2239 if (!isdigit(namelist[i]->d_name[0])) {
2240 /* Filter out . and .. */
2241 continue;
2242 }
2243 pid = atoi(namelist[i]->d_name);
2244 sprintf(path, "%s/%s/proc/kallsyms",
2245 symbol_conf.guestmount,
2246 namelist[i]->d_name);
2247 ret = access(path, R_OK);
2248 if (ret) {
2249 pr_debug("Can't access file %s\n", path);
2250 goto failure;
2251 }
Arnaldo Carvalho de Melod28c6222010-04-27 21:20:43 -03002252 machines__create_kernel_maps(self, pid);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002253 }
2254failure:
2255 free(namelist);
2256 }
2257
2258 return ret;
2259}
Arnaldo Carvalho de Melo5c0541d2010-04-29 15:25:23 -03002260
2261int machine__load_kallsyms(struct machine *self, const char *filename,
2262 enum map_type type, symbol_filter_t filter)
2263{
2264 struct map *map = self->vmlinux_maps[type];
2265 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
2266
2267 if (ret > 0) {
2268 dso__set_loaded(map->dso, type);
2269 /*
2270 * Since /proc/kallsyms will have multiple sessions for the
2271 * kernel, with modules between them, fixup the end of all
2272 * sections.
2273 */
2274 __map_groups__fixup_end(&self->kmaps, type);
2275 }
2276
2277 return ret;
2278}
2279
2280int machine__load_vmlinux_path(struct machine *self, enum map_type type,
2281 symbol_filter_t filter)
2282{
2283 struct map *map = self->vmlinux_maps[type];
2284 int ret = dso__load_vmlinux_path(map->dso, map, filter);
2285
2286 if (ret > 0) {
2287 dso__set_loaded(map->dso, type);
2288 map__reloc_vmlinux(map);
2289 }
2290
2291 return ret;
2292}