blob: 581db4c432511d1d6f76200711b8817d970b4078 [file] [log] [blame]
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001#include "util.h"
2#include "../perf.h"
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -03003#include "string.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03004#include "symbol.h"
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03005#include "thread.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03006
Frederic Weisbecker8f288272009-08-16 22:05:48 +02007#include "debug.h"
8
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02009#include <asm/bug.h>
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030010#include <libelf.h>
11#include <gelf.h>
12#include <elf.h>
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -020013#include <limits.h>
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030014#include <sys/utsname.h>
Peter Zijlstra2cdbc462009-08-05 14:05:16 +020015
Arnaldo Carvalho de Meloc12e15e2009-11-21 14:31:25 -020016#ifndef NT_GNU_BUILD_ID
17#define NT_GNU_BUILD_ID 3
18#endif
19
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -030020enum dso_origin {
21 DSO__ORIG_KERNEL = 0,
22 DSO__ORIG_JAVA_JIT,
23 DSO__ORIG_FEDORA,
24 DSO__ORIG_UBUNTU,
25 DSO__ORIG_BUILDID,
26 DSO__ORIG_DSO,
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030027 DSO__ORIG_KMODULE,
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -030028 DSO__ORIG_NOT_FOUND,
29};
30
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -020031static void dsos__add(struct list_head *head, struct dso *dso);
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -020032static struct map *kernel_maps__find_by_dso_name(const char *name);
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020033static struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030034static void kernel_maps__insert(struct map *map);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -020035struct symbol *dso__find_symbol(struct dso *self, enum map_type type, u64 addr);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020036static int dso__load_kernel_sym(struct dso *self, struct map *map,
37 symbol_filter_t filter);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020038unsigned int symbol__priv_size;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -020039static int vmlinux_path__nr_entries;
40static char **vmlinux_path;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030041
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020042static struct symbol_conf symbol_conf__defaults = {
43 .use_modules = true,
44 .try_vmlinux_path = true,
45};
46
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -020047static struct rb_root kernel_maps[MAP__NR_TYPES];
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030048
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020049bool dso__loaded(const struct dso *self, enum map_type type)
50{
51 return self->loaded & (1 << type);
52}
53
54static void dso__set_loaded(struct dso *self, enum map_type type)
55{
56 self->loaded |= (1 << type);
57}
58
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -020059static void symbols__fixup_end(struct rb_root *self)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030060{
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -020061 struct rb_node *nd, *prevnd = rb_first(self);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030062 struct symbol *curr, *prev;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030063
64 if (prevnd == NULL)
65 return;
66
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030067 curr = rb_entry(prevnd, struct symbol, rb_node);
68
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030069 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030070 prev = curr;
71 curr = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030072
73 if (prev->end == prev->start)
74 prev->end = curr->start - 1;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030075 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030076
77 /* Last entry */
78 if (curr->end == curr->start)
79 curr->end = roundup(curr->start, 4096);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030080}
81
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -020082static void __kernel_maps__fixup_end(struct rb_root *root)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030083{
84 struct map *prev, *curr;
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -020085 struct rb_node *nd, *prevnd = rb_first(root);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030086
87 if (prevnd == NULL)
88 return;
89
90 curr = rb_entry(prevnd, struct map, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030091
92 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
93 prev = curr;
94 curr = rb_entry(nd, struct map, rb_node);
95 prev->end = curr->start - 1;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030096 }
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -020097
98 /*
99 * We still haven't the actual symbols, so guess the
100 * last map final address.
101 */
102 curr->end = ~0UL;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300103}
104
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -0200105static void kernel_maps__fixup_end(void)
106{
107 int i;
108 for (i = 0; i < MAP__NR_TYPES; ++i)
109 __kernel_maps__fixup_end(&kernel_maps[i]);
110}
111
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200112static struct symbol *symbol__new(u64 start, u64 len, const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300113{
Arnaldo Carvalho de Melo0085c952009-05-28 14:55:13 -0300114 size_t namelen = strlen(name) + 1;
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200115 struct symbol *self = zalloc(symbol__priv_size +
116 sizeof(*self) + namelen);
117 if (self == NULL)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200118 return NULL;
119
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200120 if (symbol__priv_size)
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200121 self = ((void *)self) + symbol__priv_size;
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200122
Ingo Molnar0b73da32009-06-06 15:48:52 +0200123 self->start = start;
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200124 self->end = len ? start + len - 1 : start;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200125
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200126 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200127
Ingo Molnar0b73da32009-06-06 15:48:52 +0200128 memcpy(self->name, name, namelen);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300129
130 return self;
131}
132
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200133static void symbol__delete(struct symbol *self)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300134{
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200135 free(((void *)self) - symbol__priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300136}
137
138static size_t symbol__fprintf(struct symbol *self, FILE *fp)
139{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300140 return fprintf(fp, " %llx-%llx %s\n",
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300141 self->start, self->end, self->name);
142}
143
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200144static void dso__set_long_name(struct dso *self, char *name)
145{
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -0200146 if (name == NULL)
147 return;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200148 self->long_name = name;
149 self->long_name_len = strlen(name);
150}
151
152static void dso__set_basename(struct dso *self)
153{
154 self->short_name = basename(self->long_name);
155}
156
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200157struct dso *dso__new(const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300158{
159 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
160
161 if (self != NULL) {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200162 int i;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300163 strcpy(self->name, name);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200164 dso__set_long_name(self, self->name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300165 self->short_name = self->name;
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200166 for (i = 0; i < MAP__NR_TYPES; ++i)
167 self->symbols[i] = RB_ROOT;
168 self->find_symbol = dso__find_symbol;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300169 self->slen_calculated = 0;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300170 self->origin = DSO__ORIG_NOT_FOUND;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200171 self->loaded = 0;
172 self->has_build_id = 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300173 }
174
175 return self;
176}
177
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200178static void symbols__delete(struct rb_root *self)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300179{
180 struct symbol *pos;
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200181 struct rb_node *next = rb_first(self);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300182
183 while (next) {
184 pos = rb_entry(next, struct symbol, rb_node);
185 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200186 rb_erase(&pos->rb_node, self);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200187 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300188 }
189}
190
191void dso__delete(struct dso *self)
192{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200193 int i;
194 for (i = 0; i < MAP__NR_TYPES; ++i)
195 symbols__delete(&self->symbols[i]);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300196 if (self->long_name != self->name)
197 free(self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300198 free(self);
199}
200
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200201void dso__set_build_id(struct dso *self, void *build_id)
202{
203 memcpy(self->build_id, build_id, sizeof(self->build_id));
204 self->has_build_id = 1;
205}
206
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200207static void symbols__insert(struct rb_root *self, struct symbol *sym)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300208{
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200209 struct rb_node **p = &self->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300210 struct rb_node *parent = NULL;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000211 const u64 ip = sym->start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300212 struct symbol *s;
213
214 while (*p != NULL) {
215 parent = *p;
216 s = rb_entry(parent, struct symbol, rb_node);
217 if (ip < s->start)
218 p = &(*p)->rb_left;
219 else
220 p = &(*p)->rb_right;
221 }
222 rb_link_node(&sym->rb_node, parent, p);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200223 rb_insert_color(&sym->rb_node, self);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300224}
225
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200226static struct symbol *symbols__find(struct rb_root *self, u64 ip)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300227{
228 struct rb_node *n;
229
230 if (self == NULL)
231 return NULL;
232
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200233 n = self->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300234
235 while (n) {
236 struct symbol *s = rb_entry(n, struct symbol, rb_node);
237
238 if (ip < s->start)
239 n = n->rb_left;
240 else if (ip > s->end)
241 n = n->rb_right;
242 else
243 return s;
244 }
245
246 return NULL;
247}
248
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200249struct symbol *dso__find_symbol(struct dso *self, enum map_type type, u64 addr)
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200250{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200251 return symbols__find(&self->symbols[type], addr);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200252}
253
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200254int build_id__sprintf(u8 *self, int len, char *bf)
255{
256 char *bid = bf;
257 u8 *raw = self;
258 int i;
259
260 for (i = 0; i < len; ++i) {
261 sprintf(bid, "%02x", *raw);
262 ++raw;
263 bid += 2;
264 }
265
266 return raw - self;
267}
268
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200269size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300270{
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200271 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200272
273 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200274 return fprintf(fp, "%s", sbuild_id);
275}
276
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200277static const char * map_type__name[MAP__NR_TYPES] = {
278 [MAP__FUNCTION] = "Functions",
279};
280
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200281size_t dso__fprintf(struct dso *self, FILE *fp)
282{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200283 int i;
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200284 struct rb_node *nd;
285 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
286
287 ret += dso__fprintf_buildid(self, fp);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200288 ret += fprintf(fp, ")\n");
289 for (i = 0; i < MAP__NR_TYPES; ++i) {
290 ret += fprintf(fp, "%s:\n", map_type__name[i]);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200291
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200292 for (nd = rb_first(&self->symbols[i]); nd; nd = rb_next(nd)) {
293 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
294 ret += symbol__fprintf(pos, fp);
295 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300296 }
297
298 return ret;
299}
300
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300301/*
302 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
303 * so that we can in the next step set the symbol ->end address and then
304 * call kernel_maps__split_kallsyms.
305 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200306static int dso__load_all_kallsyms(struct dso *self, struct map *map)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300307{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300308 char *line = NULL;
309 size_t n;
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200310 struct rb_root *root = &self->symbols[map->type];
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300311 FILE *file = fopen("/proc/kallsyms", "r");
312
313 if (file == NULL)
314 goto out_failure;
315
316 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000317 u64 start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300318 struct symbol *sym;
319 int line_len, len;
320 char symbol_type;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300321 char *symbol_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300322
323 line_len = getline(&line, &n, file);
324 if (line_len < 0)
325 break;
326
327 if (!line)
328 goto out_failure;
329
330 line[--line_len] = '\0'; /* \n */
331
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300332 len = hex2u64(line, &start);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300333
334 len++;
335 if (len + 2 >= line_len)
336 continue;
337
338 symbol_type = toupper(line[len]);
339 /*
340 * We're interested only in code ('T'ext)
341 */
342 if (symbol_type != 'T' && symbol_type != 'W')
343 continue;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300344
345 symbol_name = line + len + 2;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300346 /*
347 * Will fix up the end later, when we have all symbols sorted.
348 */
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200349 sym = symbol__new(start, 0, symbol_name);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300350
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300351 if (sym == NULL)
352 goto out_delete_line;
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200353 /*
354 * We will pass the symbols to the filter later, in
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200355 * map__split_kallsyms, when we have split the maps per module
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200356 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200357 symbols__insert(root, sym);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300358 }
359
360 free(line);
361 fclose(file);
362
363 return 0;
364
365out_delete_line:
366 free(line);
367out_failure:
368 return -1;
369}
370
371/*
372 * Split the symbols into maps, making sure there are no overlaps, i.e. the
373 * kernel range is broken in several maps, named [kernel].N, as we don't have
374 * the original ELF section names vmlinux have.
375 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200376static int dso__split_kallsyms(struct dso *self, struct map *map,
377 symbol_filter_t filter)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300378{
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200379 struct map *curr_map = map;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300380 struct symbol *pos;
381 int count = 0;
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200382 struct rb_root *root = &self->symbols[map->type];
383 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300384 int kernel_range = 0;
385
386 while (next) {
387 char *module;
388
389 pos = rb_entry(next, struct symbol, rb_node);
390 next = rb_next(&pos->rb_node);
391
392 module = strchr(pos->name, '\t');
393 if (module) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300394 *module++ = '\0';
395
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200396 if (strcmp(self->name, module)) {
397 curr_map = kernel_maps__find_by_dso_name(module);
398 if (curr_map == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200399 pr_err("/proc/{kallsyms,modules} "
400 "inconsistency!\n");
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300401 return -1;
402 }
403 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300404 /*
405 * So that we look just like we get from .ko files,
406 * i.e. not prelinked, relative to map->start.
407 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200408 pos->start = curr_map->map_ip(curr_map, pos->start);
409 pos->end = curr_map->map_ip(curr_map, pos->end);
410 } else if (curr_map != map) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300411 char dso_name[PATH_MAX];
412 struct dso *dso;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300413
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300414 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
415 kernel_range++);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300416
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200417 dso = dso__new(dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300418 if (dso == NULL)
419 return -1;
420
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200421 curr_map = map__new2(pos->start, dso, map->type);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300422 if (map == NULL) {
423 dso__delete(dso);
424 return -1;
425 }
426
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200427 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
428 kernel_maps__insert(curr_map);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300429 ++kernel_range;
430 }
431
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200432 if (filter && filter(curr_map, pos)) {
433 rb_erase(&pos->rb_node, root);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200434 symbol__delete(pos);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300435 } else {
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200436 if (curr_map != map) {
437 rb_erase(&pos->rb_node, root);
438 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300439 }
Mike Galbraith9974f492009-07-02 08:05:58 +0200440 count++;
441 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300442 }
443
Mike Galbraith9974f492009-07-02 08:05:58 +0200444 return count;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300445}
446
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300447
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200448static int dso__load_kallsyms(struct dso *self, struct map *map,
449 symbol_filter_t filter)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300450{
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200451 if (dso__load_all_kallsyms(self, map) < 0)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300452 return -1;
453
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200454 symbols__fixup_end(&self->symbols[map->type]);
455 self->origin = DSO__ORIG_KERNEL;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300456
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200457 return dso__split_kallsyms(self, map, filter);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300458}
459
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -0200460static size_t __kernel_maps__fprintf(enum map_type type, FILE *fp)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300461{
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -0200462 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300463 struct rb_node *nd;
464
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -0200465 for (nd = rb_first(&kernel_maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300466 struct map *pos = rb_entry(nd, struct map, rb_node);
467
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300468 printed += fprintf(fp, "Map:");
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300469 printed += map__fprintf(pos, fp);
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200470 if (verbose > 1) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300471 printed += dso__fprintf(pos->dso, fp);
472 printed += fprintf(fp, "--\n");
473 }
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300474 }
475
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -0200476 return printed;
477}
478
479size_t kernel_maps__fprintf(FILE *fp)
480{
481 size_t printed = fprintf(fp, "Kernel maps:\n");
482 int i;
483 for (i = 0; i < MAP__NR_TYPES; ++i)
484 printed += __kernel_maps__fprintf(i, fp);
485
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200486 return printed + fprintf(fp, "END kernel maps\n");
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300487}
488
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300489static int dso__load_perf_map(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200490 symbol_filter_t filter)
Pekka Enberg80d496b2009-06-08 21:12:48 +0300491{
492 char *line = NULL;
493 size_t n;
494 FILE *file;
495 int nr_syms = 0;
496
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300497 file = fopen(self->long_name, "r");
Pekka Enberg80d496b2009-06-08 21:12:48 +0300498 if (file == NULL)
499 goto out_failure;
500
501 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000502 u64 start, size;
Pekka Enberg80d496b2009-06-08 21:12:48 +0300503 struct symbol *sym;
504 int line_len, len;
505
506 line_len = getline(&line, &n, file);
507 if (line_len < 0)
508 break;
509
510 if (!line)
511 goto out_failure;
512
513 line[--line_len] = '\0'; /* \n */
514
515 len = hex2u64(line, &start);
516
517 len++;
518 if (len + 2 >= line_len)
519 continue;
520
521 len += hex2u64(line + len, &size);
522
523 len++;
524 if (len + 2 >= line_len)
525 continue;
526
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200527 sym = symbol__new(start, size, line + len);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300528
529 if (sym == NULL)
530 goto out_delete_line;
531
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300532 if (filter && filter(map, sym))
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200533 symbol__delete(sym);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300534 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200535 symbols__insert(&self->symbols[map->type], sym);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300536 nr_syms++;
537 }
538 }
539
540 free(line);
541 fclose(file);
542
543 return nr_syms;
544
545out_delete_line:
546 free(line);
547out_failure:
548 return -1;
549}
550
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300551/**
552 * elf_symtab__for_each_symbol - iterate thru all the symbols
553 *
554 * @self: struct elf_symtab instance to iterate
Ingo Molnar83a09442009-08-15 12:26:57 +0200555 * @idx: uint32_t idx
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300556 * @sym: GElf_Sym iterator
557 */
Ingo Molnar83a09442009-08-15 12:26:57 +0200558#define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
559 for (idx = 0, gelf_getsym(syms, idx, &sym);\
560 idx < nr_syms; \
561 idx++, gelf_getsym(syms, idx, &sym))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300562
563static inline uint8_t elf_sym__type(const GElf_Sym *sym)
564{
565 return GELF_ST_TYPE(sym->st_info);
566}
567
568static inline int elf_sym__is_function(const GElf_Sym *sym)
569{
570 return elf_sym__type(sym) == STT_FUNC &&
571 sym->st_name != 0 &&
Arnaldo Carvalho de Melo81833132009-10-05 23:35:03 -0300572 sym->st_shndx != SHN_UNDEF;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300573}
574
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200575static inline int elf_sym__is_label(const GElf_Sym *sym)
576{
577 return elf_sym__type(sym) == STT_NOTYPE &&
578 sym->st_name != 0 &&
579 sym->st_shndx != SHN_UNDEF &&
580 sym->st_shndx != SHN_ABS;
581}
582
583static inline const char *elf_sec__name(const GElf_Shdr *shdr,
584 const Elf_Data *secstrs)
585{
586 return secstrs->d_buf + shdr->sh_name;
587}
588
589static inline int elf_sec__is_text(const GElf_Shdr *shdr,
590 const Elf_Data *secstrs)
591{
592 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
593}
594
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300595static inline const char *elf_sym__name(const GElf_Sym *sym,
596 const Elf_Data *symstrs)
597{
598 return symstrs->d_buf + sym->st_name;
599}
600
601static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
602 GElf_Shdr *shp, const char *name,
Ingo Molnar83a09442009-08-15 12:26:57 +0200603 size_t *idx)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300604{
605 Elf_Scn *sec = NULL;
606 size_t cnt = 1;
607
608 while ((sec = elf_nextscn(elf, sec)) != NULL) {
609 char *str;
610
611 gelf_getshdr(sec, shp);
612 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
613 if (!strcmp(name, str)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200614 if (idx)
615 *idx = cnt;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300616 break;
617 }
618 ++cnt;
619 }
620
621 return sec;
622}
623
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300624#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
625 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
626 idx < nr_entries; \
627 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
628
629#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
630 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
631 idx < nr_entries; \
632 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
633
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300634/*
635 * We need to check if we have a .dynsym, so that we can handle the
636 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
637 * .dynsym or .symtab).
638 * And always look at the original dso, not at debuginfo packages, that
639 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
640 */
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200641static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
642 symbol_filter_t filter)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300643{
644 uint32_t nr_rel_entries, idx;
645 GElf_Sym sym;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000646 u64 plt_offset;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300647 GElf_Shdr shdr_plt;
648 struct symbol *f;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300649 GElf_Shdr shdr_rel_plt, shdr_dynsym;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300650 Elf_Data *reldata, *syms, *symstrs;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300651 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
652 size_t dynsym_idx;
653 GElf_Ehdr ehdr;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300654 char sympltname[1024];
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300655 Elf *elf;
656 int nr = 0, symidx, fd, err = 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300657
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300658 fd = open(self->long_name, O_RDONLY);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300659 if (fd < 0)
660 goto out;
661
Marti Raudsepp84087122009-10-24 19:10:36 +0300662 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300663 if (elf == NULL)
664 goto out_close;
665
666 if (gelf_getehdr(elf, &ehdr) == NULL)
667 goto out_elf_end;
668
669 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
670 ".dynsym", &dynsym_idx);
671 if (scn_dynsym == NULL)
672 goto out_elf_end;
673
674 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300675 ".rela.plt", NULL);
676 if (scn_plt_rel == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300677 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300678 ".rel.plt", NULL);
679 if (scn_plt_rel == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300680 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300681 }
682
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300683 err = -1;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300684
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300685 if (shdr_rel_plt.sh_link != dynsym_idx)
686 goto out_elf_end;
687
688 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
689 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300690
691 /*
Ingo Molnar83a09442009-08-15 12:26:57 +0200692 * Fetch the relocation section to find the idxes to the GOT
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300693 * and the symbols in the .dynsym they refer to.
694 */
695 reldata = elf_getdata(scn_plt_rel, NULL);
696 if (reldata == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300697 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300698
699 syms = elf_getdata(scn_dynsym, NULL);
700 if (syms == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300701 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300702
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300703 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300704 if (scn_symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300705 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300706
707 symstrs = elf_getdata(scn_symstrs, NULL);
708 if (symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300709 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300710
711 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
712 plt_offset = shdr_plt.sh_offset;
713
714 if (shdr_rel_plt.sh_type == SHT_RELA) {
715 GElf_Rela pos_mem, *pos;
716
717 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
718 nr_rel_entries) {
719 symidx = GELF_R_SYM(pos->r_info);
720 plt_offset += shdr_plt.sh_entsize;
721 gelf_getsym(syms, symidx, &sym);
722 snprintf(sympltname, sizeof(sympltname),
723 "%s@plt", elf_sym__name(&sym, symstrs));
724
725 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200726 sympltname);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300727 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300728 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300729
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200730 if (filter && filter(map, f))
731 symbol__delete(f);
732 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200733 symbols__insert(&self->symbols[map->type], f);
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200734 ++nr;
735 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300736 }
737 } else if (shdr_rel_plt.sh_type == SHT_REL) {
738 GElf_Rel pos_mem, *pos;
739 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
740 nr_rel_entries) {
741 symidx = GELF_R_SYM(pos->r_info);
742 plt_offset += shdr_plt.sh_entsize;
743 gelf_getsym(syms, symidx, &sym);
744 snprintf(sympltname, sizeof(sympltname),
745 "%s@plt", elf_sym__name(&sym, symstrs));
746
747 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200748 sympltname);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300749 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300750 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300751
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200752 if (filter && filter(map, f))
753 symbol__delete(f);
754 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200755 symbols__insert(&self->symbols[map->type], f);
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200756 ++nr;
757 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300758 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300759 }
760
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300761 err = 0;
762out_elf_end:
763 elf_end(elf);
764out_close:
765 close(fd);
766
767 if (err == 0)
768 return nr;
769out:
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200770 pr_warning("%s: problems reading %s PLT info.\n",
771 __func__, self->long_name);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300772 return 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300773}
774
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300775static int dso__load_sym(struct dso *self, struct map *map, const char *name,
776 int fd, symbol_filter_t filter, int kernel,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200777 int kmodule)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300778{
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300779 struct map *curr_map = map;
780 struct dso *curr_dso = self;
781 size_t dso_name_len = strlen(self->short_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200782 Elf_Data *symstrs, *secstrs;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300783 uint32_t nr_syms;
784 int err = -1;
Ingo Molnar83a09442009-08-15 12:26:57 +0200785 uint32_t idx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300786 GElf_Ehdr ehdr;
787 GElf_Shdr shdr;
788 Elf_Data *syms;
789 GElf_Sym sym;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300790 Elf_Scn *sec, *sec_strndx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300791 Elf *elf;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300792 int nr = 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300793
Marti Raudsepp84087122009-10-24 19:10:36 +0300794 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300795 if (elf == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200796 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300797 goto out_close;
798 }
799
800 if (gelf_getehdr(elf, &ehdr) == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200801 pr_err("%s: cannot get elf header.\n", __func__);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300802 goto out_elf_end;
803 }
804
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300805 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
806 if (sec == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300807 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
808 if (sec == NULL)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300809 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300810 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300811
812 syms = elf_getdata(sec, NULL);
813 if (syms == NULL)
814 goto out_elf_end;
815
816 sec = elf_getscn(elf, shdr.sh_link);
817 if (sec == NULL)
818 goto out_elf_end;
819
820 symstrs = elf_getdata(sec, NULL);
821 if (symstrs == NULL)
822 goto out_elf_end;
823
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200824 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
825 if (sec_strndx == NULL)
826 goto out_elf_end;
827
828 secstrs = elf_getdata(sec_strndx, NULL);
Stoyan Gaydarov9b30a262009-07-30 05:25:29 -0500829 if (secstrs == NULL)
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200830 goto out_elf_end;
831
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300832 nr_syms = shdr.sh_size / shdr.sh_entsize;
833
Arjan van de Vene9fbc9d2009-06-06 21:22:33 +0200834 memset(&sym, 0, sizeof(sym));
Mike Galbraithd20ff6b2009-07-20 14:01:38 +0200835 if (!kernel) {
836 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -0300837 elf_section_by_name(elf, &ehdr, &shdr,
838 ".gnu.prelink_undo",
839 NULL) != NULL);
Mike Galbraithd20ff6b2009-07-20 14:01:38 +0200840 } else self->adjust_symbols = 0;
841
Ingo Molnar83a09442009-08-15 12:26:57 +0200842 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300843 struct symbol *f;
Ingo Molnar83a09442009-08-15 12:26:57 +0200844 const char *elf_name;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300845 char *demangled = NULL;
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200846 int is_label = elf_sym__is_label(&sym);
847 const char *section_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300848
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200849 if (!is_label && !elf_sym__is_function(&sym))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300850 continue;
851
852 sec = elf_getscn(elf, sym.st_shndx);
853 if (!sec)
854 goto out_elf_end;
855
856 gelf_getshdr(sec, &shdr);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200857
858 if (is_label && !elf_sec__is_text(&shdr, secstrs))
859 continue;
860
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300861 elf_name = elf_sym__name(&sym, symstrs);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200862 section_name = elf_sec__name(&shdr, secstrs);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200863
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300864 if (kernel || kmodule) {
865 char dso_name[PATH_MAX];
866
867 if (strcmp(section_name,
868 curr_dso->short_name + dso_name_len) == 0)
869 goto new_symbol;
870
871 if (strcmp(section_name, ".text") == 0) {
872 curr_map = map;
873 curr_dso = self;
874 goto new_symbol;
875 }
876
877 snprintf(dso_name, sizeof(dso_name),
878 "%s%s", self->short_name, section_name);
879
880 curr_map = kernel_maps__find_by_dso_name(dso_name);
881 if (curr_map == NULL) {
882 u64 start = sym.st_value;
883
884 if (kmodule)
885 start += map->start + shdr.sh_offset;
886
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200887 curr_dso = dso__new(dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300888 if (curr_dso == NULL)
889 goto out_elf_end;
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -0200890 curr_map = map__new2(start, curr_dso,
891 MAP__FUNCTION);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300892 if (curr_map == NULL) {
893 dso__delete(curr_dso);
894 goto out_elf_end;
895 }
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200896 curr_map->map_ip = identity__map_ip;
897 curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300898 curr_dso->origin = DSO__ORIG_KERNEL;
899 kernel_maps__insert(curr_map);
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200900 dsos__add(&dsos__kernel, curr_dso);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300901 } else
902 curr_dso = curr_map->dso;
903
904 goto new_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300905 }
906
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300907 if (curr_dso->adjust_symbols) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200908 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
909 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
910 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -0300911 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300912 }
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -0300913 /*
914 * We need to figure out if the object was created from C++ sources
915 * DWARF DW_compile_unit has this, but we don't always have access
916 * to it...
917 */
Ingo Molnar83a09442009-08-15 12:26:57 +0200918 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -0300919 if (demangled != NULL)
Ingo Molnar83a09442009-08-15 12:26:57 +0200920 elf_name = demangled;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300921new_symbol:
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200922 f = symbol__new(sym.st_value, sym.st_size, elf_name);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -0300923 free(demangled);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300924 if (!f)
925 goto out_elf_end;
926
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300927 if (filter && filter(curr_map, f))
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200928 symbol__delete(f);
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300929 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200930 symbols__insert(&curr_dso->symbols[curr_map->type], f);
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300931 nr++;
932 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300933 }
934
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300935 /*
936 * For misannotated, zeroed, ASM function sizes.
937 */
938 if (nr > 0)
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200939 symbols__fixup_end(&self->symbols[map->type]);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300940 err = nr;
941out_elf_end:
942 elf_end(elf);
943out_close:
944 return err;
945}
946
Arnaldo Carvalho de Melo78075ca2009-11-20 20:51:26 -0200947static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
948{
949 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
950}
951
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200952static bool __dsos__read_build_ids(struct list_head *head)
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100953{
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200954 bool have_build_id = false;
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100955 struct dso *pos;
956
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200957 list_for_each_entry(pos, head, node)
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200958 if (filename__read_build_id(pos->long_name, pos->build_id,
959 sizeof(pos->build_id)) > 0) {
960 have_build_id = true;
961 pos->has_build_id = true;
962 }
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100963
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200964 return have_build_id;
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100965}
966
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200967bool dsos__read_build_ids(void)
968{
969 return __dsos__read_build_ids(&dsos__kernel) ||
970 __dsos__read_build_ids(&dsos__user);
971}
972
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -0200973/*
974 * Align offset to 4 bytes as needed for note name and descriptor data.
975 */
976#define NOTE_ALIGN(n) (((n) + 3) & -4U)
977
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -0200978int filename__read_build_id(const char *filename, void *bf, size_t size)
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300979{
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -0200980 int fd, err = -1;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300981 GElf_Ehdr ehdr;
982 GElf_Shdr shdr;
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -0200983 Elf_Data *data;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300984 Elf_Scn *sec;
Pekka Enberge57cfcd2009-11-22 12:29:44 +0200985 Elf_Kind ek;
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -0200986 void *ptr;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300987 Elf *elf;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300988
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -0200989 if (size < BUILD_ID_SIZE)
990 goto out;
991
992 fd = open(filename, O_RDONLY);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300993 if (fd < 0)
994 goto out;
995
Marti Raudsepp84087122009-10-24 19:10:36 +0300996 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300997 if (elf == NULL) {
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200998 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300999 goto out_close;
1000 }
1001
Pekka Enberge57cfcd2009-11-22 12:29:44 +02001002 ek = elf_kind(elf);
1003 if (ek != ELF_K_ELF)
1004 goto out_elf_end;
1005
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001006 if (gelf_getehdr(elf, &ehdr) == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001007 pr_err("%s: cannot get elf header.\n", __func__);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001008 goto out_elf_end;
1009 }
1010
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -02001011 sec = elf_section_by_name(elf, &ehdr, &shdr,
1012 ".note.gnu.build-id", NULL);
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001013 if (sec == NULL) {
1014 sec = elf_section_by_name(elf, &ehdr, &shdr,
1015 ".notes", NULL);
1016 if (sec == NULL)
1017 goto out_elf_end;
1018 }
1019
1020 data = elf_getdata(sec, NULL);
1021 if (data == NULL)
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001022 goto out_elf_end;
1023
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001024 ptr = data->d_buf;
1025 while (ptr < (data->d_buf + data->d_size)) {
1026 GElf_Nhdr *nhdr = ptr;
1027 int namesz = NOTE_ALIGN(nhdr->n_namesz),
1028 descsz = NOTE_ALIGN(nhdr->n_descsz);
1029 const char *name;
1030
1031 ptr += sizeof(*nhdr);
1032 name = ptr;
1033 ptr += namesz;
1034 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1035 nhdr->n_namesz == sizeof("GNU")) {
1036 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1037 memcpy(bf, ptr, BUILD_ID_SIZE);
1038 err = BUILD_ID_SIZE;
1039 break;
1040 }
1041 }
1042 ptr += descsz;
1043 }
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -02001044out_elf_end:
1045 elf_end(elf);
1046out_close:
1047 close(fd);
1048out:
1049 return err;
1050}
1051
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001052int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1053{
1054 int fd, err = -1;
1055
1056 if (size < BUILD_ID_SIZE)
1057 goto out;
1058
1059 fd = open(filename, O_RDONLY);
1060 if (fd < 0)
1061 goto out;
1062
1063 while (1) {
1064 char bf[BUFSIZ];
1065 GElf_Nhdr nhdr;
1066 int namesz, descsz;
1067
1068 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1069 break;
1070
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001071 namesz = NOTE_ALIGN(nhdr.n_namesz);
1072 descsz = NOTE_ALIGN(nhdr.n_descsz);
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001073 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1074 nhdr.n_namesz == sizeof("GNU")) {
1075 if (read(fd, bf, namesz) != namesz)
1076 break;
1077 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1078 if (read(fd, build_id,
1079 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1080 err = 0;
1081 break;
1082 }
1083 } else if (read(fd, bf, descsz) != descsz)
1084 break;
1085 } else {
1086 int n = namesz + descsz;
1087 if (read(fd, bf, n) != n)
1088 break;
1089 }
1090 }
1091 close(fd);
1092out:
1093 return err;
1094}
1095
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001096char dso__symtab_origin(const struct dso *self)
1097{
1098 static const char origin[] = {
1099 [DSO__ORIG_KERNEL] = 'k',
1100 [DSO__ORIG_JAVA_JIT] = 'j',
1101 [DSO__ORIG_FEDORA] = 'f',
1102 [DSO__ORIG_UBUNTU] = 'u',
1103 [DSO__ORIG_BUILDID] = 'b',
1104 [DSO__ORIG_DSO] = 'd',
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001105 [DSO__ORIG_KMODULE] = 'K',
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001106 };
1107
1108 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1109 return '!';
1110 return origin[self->origin];
1111}
1112
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001113int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001114{
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001115 int size = PATH_MAX;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001116 char *name;
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001117 u8 build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001118 int ret = -1;
1119 int fd;
1120
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001121 dso__set_loaded(self, map->type);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001122
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001123 if (self->kernel)
1124 return dso__load_kernel_sym(self, map, filter);
1125
1126 name = malloc(size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001127 if (!name)
1128 return -1;
1129
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -03001130 self->adjust_symbols = 0;
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001131
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001132 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001133 ret = dso__load_perf_map(self, map, filter);
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001134 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1135 DSO__ORIG_NOT_FOUND;
1136 return ret;
1137 }
1138
1139 self->origin = DSO__ORIG_FEDORA - 1;
Pekka Enberg80d496b2009-06-08 21:12:48 +03001140
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001141more:
1142 do {
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001143 self->origin++;
1144 switch (self->origin) {
1145 case DSO__ORIG_FEDORA:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001146 snprintf(name, size, "/usr/lib/debug%s.debug",
1147 self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001148 break;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001149 case DSO__ORIG_UBUNTU:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001150 snprintf(name, size, "/usr/lib/debug%s",
1151 self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001152 break;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001153 case DSO__ORIG_BUILDID:
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001154 if (filename__read_build_id(self->long_name, build_id,
1155 sizeof(build_id))) {
1156 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1157
1158 build_id__sprintf(build_id, sizeof(build_id),
1159 build_id_hex);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001160 snprintf(name, size,
1161 "/usr/lib/debug/.build-id/%.2s/%s.debug",
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001162 build_id_hex, build_id_hex + 2);
1163 if (self->has_build_id)
1164 goto compare_build_id;
1165 break;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001166 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001167 self->origin++;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001168 /* Fall thru */
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001169 case DSO__ORIG_DSO:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001170 snprintf(name, size, "%s", self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001171 break;
1172
1173 default:
1174 goto out;
1175 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001176
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001177 if (self->has_build_id) {
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001178 if (filename__read_build_id(name, build_id,
1179 sizeof(build_id)) < 0)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001180 goto more;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001181compare_build_id:
Arnaldo Carvalho de Melo78075ca2009-11-20 20:51:26 -02001182 if (!dso__build_id_equal(self, build_id))
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001183 goto more;
1184 }
1185
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001186 fd = open(name, O_RDONLY);
1187 } while (fd < 0);
1188
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001189 ret = dso__load_sym(self, map, name, fd, filter, 0, 0);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001190 close(fd);
1191
1192 /*
1193 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1194 */
1195 if (!ret)
1196 goto more;
1197
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001198 if (ret > 0) {
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -02001199 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001200 if (nr_plt > 0)
1201 ret += nr_plt;
1202 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001203out:
1204 free(name);
Arnaldo Carvalho de Melo1340e6b2009-08-11 17:04:36 -03001205 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1206 return 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001207 return ret;
1208}
1209
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001210static void kernel_maps__insert(struct map *map)
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001211{
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -02001212 maps__insert(&kernel_maps[map->type], map);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001213}
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001214
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -02001215static struct symbol *kernel_maps__find_symbol(u64 ip, enum map_type type,
1216 struct map **mapp,
1217 symbol_filter_t filter)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001218{
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -02001219 struct map *map = maps__find(&kernel_maps[type], ip);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001220
1221 if (mapp)
1222 *mapp = map;
1223
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001224 if (map) {
1225 ip = map->map_ip(map, ip);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001226 return map__find_symbol(map, ip, filter);
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001227 } else
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -02001228 WARN_ONCE(RB_EMPTY_ROOT(&kernel_maps[type]),
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001229 "Empty kernel_maps, was symbol__init() called?\n");
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001230
1231 return NULL;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001232}
1233
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -02001234struct symbol *kernel_maps__find_function(u64 ip, struct map **mapp,
1235 symbol_filter_t filter)
1236{
1237 return kernel_maps__find_symbol(ip, MAP__FUNCTION, mapp, filter);
1238}
1239
1240static struct map *kernel_maps__find_by_dso_name(const char *name)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001241{
1242 struct rb_node *nd;
1243
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -02001244 for (nd = rb_first(&kernel_maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001245 struct map *map = rb_entry(nd, struct map, rb_node);
1246
1247 if (map->dso && strcmp(map->dso->name, name) == 0)
1248 return map;
1249 }
1250
1251 return NULL;
1252}
1253
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001254static int dsos__set_modules_path_dir(char *dirname)
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001255{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001256 struct dirent *dent;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001257 DIR *dir = opendir(dirname);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001258
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001259 if (!dir) {
Arnaldo Carvalho de Melo87f8ea42009-11-22 13:21:41 -02001260 pr_debug("%s: cannot open %s dir\n", __func__, dirname);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001261 return -1;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001262 }
1263
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001264 while ((dent = readdir(dir)) != NULL) {
1265 char path[PATH_MAX];
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001266
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001267 if (dent->d_type == DT_DIR) {
1268 if (!strcmp(dent->d_name, ".") ||
1269 !strcmp(dent->d_name, ".."))
1270 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001271
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001272 snprintf(path, sizeof(path), "%s/%s",
1273 dirname, dent->d_name);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001274 if (dsos__set_modules_path_dir(path) < 0)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001275 goto failure;
1276 } else {
1277 char *dot = strrchr(dent->d_name, '.'),
1278 dso_name[PATH_MAX];
1279 struct map *map;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001280 char *long_name;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001281
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001282 if (dot == NULL || strcmp(dot, ".ko"))
1283 continue;
1284 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1285 (int)(dot - dent->d_name), dent->d_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001286
Arnaldo Carvalho de Meloa2a99e82009-10-05 14:26:18 -03001287 strxfrchar(dso_name, '-', '_');
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001288 map = kernel_maps__find_by_dso_name(dso_name);
1289 if (map == NULL)
1290 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001291
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001292 snprintf(path, sizeof(path), "%s/%s",
1293 dirname, dent->d_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001294
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001295 long_name = strdup(path);
1296 if (long_name == NULL)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001297 goto failure;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001298 dso__set_long_name(map->dso, long_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001299 }
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001300 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001301
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001302 return 0;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001303failure:
1304 closedir(dir);
1305 return -1;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001306}
1307
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001308static int dsos__set_modules_path(void)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001309{
1310 struct utsname uts;
1311 char modules_path[PATH_MAX];
1312
1313 if (uname(&uts) < 0)
1314 return -1;
1315
1316 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1317 uts.release);
1318
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001319 return dsos__set_modules_path_dir(modules_path);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001320}
1321
1322/*
1323 * Constructor variant for modules (where we know from /proc/modules where
1324 * they are loaded) and for vmlinux, where only after we load all the
1325 * symbols we'll know where it starts and ends.
1326 */
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001327static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001328{
1329 struct map *self = malloc(sizeof(*self));
1330
1331 if (self != NULL) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001332 /*
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -02001333 * ->end will be filled after we load all the symbols
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001334 */
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001335 map__init(self, type, start, 0, 0, dso);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001336 }
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -02001337
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001338 return self;
1339}
1340
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001341static int kernel_maps__create_module_maps(void)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001342{
1343 char *line = NULL;
1344 size_t n;
1345 FILE *file = fopen("/proc/modules", "r");
1346 struct map *map;
1347
1348 if (file == NULL)
1349 return -1;
1350
1351 while (!feof(file)) {
1352 char name[PATH_MAX];
1353 u64 start;
1354 struct dso *dso;
1355 char *sep;
1356 int line_len;
1357
1358 line_len = getline(&line, &n, file);
1359 if (line_len < 0)
1360 break;
1361
1362 if (!line)
1363 goto out_failure;
1364
1365 line[--line_len] = '\0'; /* \n */
1366
1367 sep = strrchr(line, 'x');
1368 if (sep == NULL)
1369 continue;
1370
1371 hex2u64(sep + 1, &start);
1372
1373 sep = strchr(line, ' ');
1374 if (sep == NULL)
1375 continue;
1376
1377 *sep = '\0';
1378
1379 snprintf(name, sizeof(name), "[%s]", line);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001380 dso = dso__new(name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001381
1382 if (dso == NULL)
1383 goto out_delete_line;
1384
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001385 map = map__new2(start, dso, MAP__FUNCTION);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001386 if (map == NULL) {
1387 dso__delete(dso);
1388 goto out_delete_line;
1389 }
1390
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001391 snprintf(name, sizeof(name),
1392 "/sys/module/%s/notes/.note.gnu.build-id", line);
1393 if (sysfs__read_build_id(name, dso->build_id,
1394 sizeof(dso->build_id)) == 0)
1395 dso->has_build_id = true;
1396
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001397 dso->origin = DSO__ORIG_KMODULE;
1398 kernel_maps__insert(map);
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001399 dsos__add(&dsos__kernel, dso);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001400 }
1401
1402 free(line);
1403 fclose(file);
1404
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001405 return dsos__set_modules_path();
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001406
1407out_delete_line:
1408 free(line);
1409out_failure:
1410 return -1;
1411}
1412
1413static int dso__load_vmlinux(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001414 const char *vmlinux, symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001415{
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001416 int err = -1, fd;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001417
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001418 if (self->has_build_id) {
1419 u8 build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001420
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001421 if (filename__read_build_id(vmlinux, build_id,
1422 sizeof(build_id)) < 0) {
1423 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1424 return -1;
1425 }
1426 if (!dso__build_id_equal(self, build_id)) {
1427 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1428 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1429
1430 build_id__sprintf(self->build_id,
1431 sizeof(self->build_id),
1432 expected_build_id);
1433 build_id__sprintf(build_id, sizeof(build_id),
1434 vmlinux_build_id);
1435 pr_debug("build_id in %s is %s while expected is %s, "
1436 "ignoring it\n", vmlinux, vmlinux_build_id,
1437 expected_build_id);
1438 return -1;
1439 }
1440 }
1441
1442 fd = open(vmlinux, O_RDONLY);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001443 if (fd < 0)
1444 return -1;
1445
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001446 dso__set_loaded(self, map->type);
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001447 err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001448
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001449 close(fd);
1450
1451 return err;
1452}
1453
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001454static int dso__load_kernel_sym(struct dso *self, struct map *map,
1455 symbol_filter_t filter)
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001456{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001457 int err;
1458 bool is_kallsyms;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001459
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001460 if (vmlinux_path != NULL) {
1461 int i;
1462 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1463 vmlinux_path__nr_entries);
1464 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1465 err = dso__load_vmlinux(self, map, vmlinux_path[i],
1466 filter);
1467 if (err > 0) {
1468 pr_debug("Using %s for symbols\n",
1469 vmlinux_path[i]);
1470 dso__set_long_name(self,
1471 strdup(vmlinux_path[i]));
1472 goto out_fixup;
1473 }
1474 }
1475 }
1476
1477 is_kallsyms = self->long_name[0] == '[';
1478 if (is_kallsyms)
1479 goto do_kallsyms;
1480
1481 err = dso__load_vmlinux(self, map, self->long_name, filter);
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -02001482 if (err <= 0) {
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001483 pr_info("The file %s cannot be used, "
1484 "trying to use /proc/kallsyms...", self->long_name);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001485do_kallsyms:
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -02001486 err = dso__load_kallsyms(self, map, filter);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001487 if (err > 0 && !is_kallsyms)
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -02001488 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1489 }
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001490
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001491 if (err > 0) {
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001492out_fixup:
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001493 map__fixup_start(map);
1494 map__fixup_end(map);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001495 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001496
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001497 return err;
1498}
1499
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001500LIST_HEAD(dsos__user);
1501LIST_HEAD(dsos__kernel);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001502struct dso *vdso;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001503
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001504static void dsos__add(struct list_head *head, struct dso *dso)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001505{
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001506 list_add_tail(&dso->node, head);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001507}
1508
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001509static struct dso *dsos__find(struct list_head *head, const char *name)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001510{
1511 struct dso *pos;
1512
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001513 list_for_each_entry(pos, head, node)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001514 if (strcmp(pos->name, name) == 0)
1515 return pos;
1516 return NULL;
1517}
1518
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001519struct dso *dsos__findnew(const char *name)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001520{
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001521 struct dso *dso = dsos__find(&dsos__user, name);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001522
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -02001523 if (!dso) {
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001524 dso = dso__new(name);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001525 if (dso != NULL) {
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001526 dsos__add(&dsos__user, dso);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001527 dso__set_basename(dso);
1528 }
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001529 }
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001530
1531 return dso;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001532}
1533
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001534static void __dsos__fprintf(struct list_head *head, FILE *fp)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001535{
1536 struct dso *pos;
1537
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001538 list_for_each_entry(pos, head, node)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001539 dso__fprintf(pos, fp);
1540}
1541
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001542void dsos__fprintf(FILE *fp)
1543{
1544 __dsos__fprintf(&dsos__kernel, fp);
1545 __dsos__fprintf(&dsos__user, fp);
1546}
1547
1548static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp)
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001549{
1550 struct dso *pos;
1551 size_t ret = 0;
1552
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001553 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001554 ret += dso__fprintf_buildid(pos, fp);
Arnaldo Carvalho de Melo1124ba72009-11-16 21:45:25 -02001555 ret += fprintf(fp, " %s\n", pos->long_name);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001556 }
1557 return ret;
1558}
1559
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001560size_t dsos__fprintf_buildid(FILE *fp)
1561{
1562 return (__dsos__fprintf_buildid(&dsos__kernel, fp) +
1563 __dsos__fprintf_buildid(&dsos__user, fp));
1564}
1565
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001566static int kernel_maps__create_kernel_map(const struct symbol_conf *conf)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001567{
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -02001568 struct map *kmap;
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001569 struct dso *kernel = dso__new(conf->vmlinux_name ?: "[kernel.kallsyms]");
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001570
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001571 if (kernel == NULL)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001572 return -1;
1573
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -02001574 kmap = map__new2(0, kernel, MAP__FUNCTION);
1575 if (kmap == NULL)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001576 goto out_delete_kernel_dso;
1577
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -02001578 kmap->map_ip = kmap->unmap_ip = identity__map_ip;
1579 kernel->short_name = "[kernel]";
1580 kernel->kernel = 1;
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001581
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001582 vdso = dso__new("[vdso]");
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001583 if (vdso == NULL)
1584 goto out_delete_kernel_map;
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001585 dso__set_loaded(vdso, MAP__FUNCTION);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001586
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001587 if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1588 sizeof(kernel->build_id)) == 0)
1589 kernel->has_build_id = true;
1590
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -02001591 kernel_maps__insert(kmap);
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001592 dsos__add(&dsos__kernel, kernel);
1593 dsos__add(&dsos__user, vdso);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001594
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001595 return 0;
1596
1597out_delete_kernel_map:
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -02001598 map__delete(kmap);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001599out_delete_kernel_dso:
1600 dso__delete(kernel);
1601 return -1;
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001602}
1603
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001604static void vmlinux_path__exit(void)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001605{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001606 while (--vmlinux_path__nr_entries >= 0) {
1607 free(vmlinux_path[vmlinux_path__nr_entries]);
1608 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1609 }
1610
1611 free(vmlinux_path);
1612 vmlinux_path = NULL;
1613}
1614
1615static int vmlinux_path__init(void)
1616{
1617 struct utsname uts;
1618 char bf[PATH_MAX];
1619
1620 if (uname(&uts) < 0)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001621 return -1;
1622
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001623 vmlinux_path = malloc(sizeof(char *) * 5);
1624 if (vmlinux_path == NULL)
1625 return -1;
1626
1627 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1628 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1629 goto out_fail;
1630 ++vmlinux_path__nr_entries;
1631 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1632 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1633 goto out_fail;
1634 ++vmlinux_path__nr_entries;
1635 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1636 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1637 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1638 goto out_fail;
1639 ++vmlinux_path__nr_entries;
1640 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1641 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1642 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1643 goto out_fail;
1644 ++vmlinux_path__nr_entries;
1645 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1646 uts.release);
1647 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1648 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1649 goto out_fail;
1650 ++vmlinux_path__nr_entries;
1651
1652 return 0;
1653
1654out_fail:
1655 vmlinux_path__exit();
1656 return -1;
1657}
1658
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001659static int kernel_maps__init(const struct symbol_conf *conf)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001660{
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001661 const struct symbol_conf *pconf = conf ?: &symbol_conf__defaults;
1662
1663 symbol__priv_size = pconf->priv_size;
1664
1665 if (pconf->try_vmlinux_path && vmlinux_path__init() < 0)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001666 return -1;
1667
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001668 if (kernel_maps__create_kernel_map(pconf) < 0) {
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001669 vmlinux_path__exit();
1670 return -1;
1671 }
1672
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001673 if (pconf->use_modules && kernel_maps__create_module_maps() < 0)
Arnaldo Carvalho de Melo87f8ea42009-11-22 13:21:41 -02001674 pr_debug("Failed to load list of modules in use, "
1675 "continuing...\n");
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -02001676 /*
1677 * Now that we have all the maps created, just set the ->end of them:
1678 */
1679 kernel_maps__fixup_end();
Arnaldo Carvalho de Melo6671cb12009-11-20 20:51:24 -02001680 return 0;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001681}
1682
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001683int symbol__init(struct symbol_conf *conf)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001684{
1685 elf_version(EV_CURRENT);
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001686 return kernel_maps__init(conf);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001687}