blob: b788c2f5d672d8c69e075b9548f881b4ea086ef1 [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 Melo95011c62009-11-27 16:29:20 -020032static struct map *thread__find_map_by_name(struct thread *self, 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 Melo6a4694a2009-11-27 16:29:17 -020034struct symbol *dso__find_symbol(struct dso *self, enum map_type type, u64 addr);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -020035static int dso__load_kernel_sym(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020036 struct thread *thread, symbol_filter_t filter);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -020037unsigned int symbol__priv_size;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -020038static int vmlinux_path__nr_entries;
39static char **vmlinux_path;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030040
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020041static struct symbol_conf symbol_conf__defaults = {
42 .use_modules = true,
43 .try_vmlinux_path = true,
44};
45
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020046static struct thread kthread_mem, *kthread = &kthread_mem;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030047
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -020048bool dso__loaded(const struct dso *self, enum map_type type)
49{
50 return self->loaded & (1 << type);
51}
52
53static void dso__set_loaded(struct dso *self, enum map_type type)
54{
55 self->loaded |= (1 << type);
56}
57
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -020058static void symbols__fixup_end(struct rb_root *self)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030059{
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -020060 struct rb_node *nd, *prevnd = rb_first(self);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030061 struct symbol *curr, *prev;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030062
63 if (prevnd == NULL)
64 return;
65
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030066 curr = rb_entry(prevnd, struct symbol, rb_node);
67
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030068 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030069 prev = curr;
70 curr = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030071
72 if (prev->end == prev->start)
73 prev->end = curr->start - 1;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030074 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030075
76 /* Last entry */
77 if (curr->end == curr->start)
78 curr->end = roundup(curr->start, 4096);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030079}
80
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020081static void __thread__fixup_maps_end(struct thread *self, enum map_type type)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030082{
83 struct map *prev, *curr;
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -020084 struct rb_node *nd, *prevnd = rb_first(&self->maps[type]);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030085
86 if (prevnd == NULL)
87 return;
88
89 curr = rb_entry(prevnd, struct map, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -030090
91 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
92 prev = curr;
93 curr = rb_entry(nd, struct map, rb_node);
94 prev->end = curr->start - 1;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -030095 }
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -020096
97 /*
98 * We still haven't the actual symbols, so guess the
99 * last map final address.
100 */
101 curr->end = ~0UL;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300102}
103
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200104static void thread__fixup_maps_end(struct thread *self)
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -0200105{
106 int i;
107 for (i = 0; i < MAP__NR_TYPES; ++i)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200108 __thread__fixup_maps_end(self, i);
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -0200109}
110
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200111static struct symbol *symbol__new(u64 start, u64 len, const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300112{
Arnaldo Carvalho de Melo0085c952009-05-28 14:55:13 -0300113 size_t namelen = strlen(name) + 1;
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200114 struct symbol *self = zalloc(symbol__priv_size +
115 sizeof(*self) + namelen);
116 if (self == NULL)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200117 return NULL;
118
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200119 if (symbol__priv_size)
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200120 self = ((void *)self) + symbol__priv_size;
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200121
Ingo Molnar0b73da32009-06-06 15:48:52 +0200122 self->start = start;
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200123 self->end = len ? start + len - 1 : start;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200124
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200125 pr_debug3("%s: %s %#Lx-%#Lx\n", __func__, name, start, self->end);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200126
Ingo Molnar0b73da32009-06-06 15:48:52 +0200127 memcpy(self->name, name, namelen);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300128
129 return self;
130}
131
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200132static void symbol__delete(struct symbol *self)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300133{
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200134 free(((void *)self) - symbol__priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300135}
136
137static size_t symbol__fprintf(struct symbol *self, FILE *fp)
138{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300139 return fprintf(fp, " %llx-%llx %s\n",
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300140 self->start, self->end, self->name);
141}
142
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200143static void dso__set_long_name(struct dso *self, char *name)
144{
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -0200145 if (name == NULL)
146 return;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200147 self->long_name = name;
148 self->long_name_len = strlen(name);
149}
150
151static void dso__set_basename(struct dso *self)
152{
153 self->short_name = basename(self->long_name);
154}
155
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200156struct dso *dso__new(const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300157{
158 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
159
160 if (self != NULL) {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200161 int i;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300162 strcpy(self->name, name);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -0200163 dso__set_long_name(self, self->name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300164 self->short_name = self->name;
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200165 for (i = 0; i < MAP__NR_TYPES; ++i)
166 self->symbols[i] = RB_ROOT;
167 self->find_symbol = dso__find_symbol;
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -0300168 self->slen_calculated = 0;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -0300169 self->origin = DSO__ORIG_NOT_FOUND;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200170 self->loaded = 0;
171 self->has_build_id = 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300172 }
173
174 return self;
175}
176
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200177static void symbols__delete(struct rb_root *self)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300178{
179 struct symbol *pos;
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200180 struct rb_node *next = rb_first(self);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300181
182 while (next) {
183 pos = rb_entry(next, struct symbol, rb_node);
184 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200185 rb_erase(&pos->rb_node, self);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200186 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300187 }
188}
189
190void dso__delete(struct dso *self)
191{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200192 int i;
193 for (i = 0; i < MAP__NR_TYPES; ++i)
194 symbols__delete(&self->symbols[i]);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300195 if (self->long_name != self->name)
196 free(self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300197 free(self);
198}
199
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200200void dso__set_build_id(struct dso *self, void *build_id)
201{
202 memcpy(self->build_id, build_id, sizeof(self->build_id));
203 self->has_build_id = 1;
204}
205
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200206static void symbols__insert(struct rb_root *self, struct symbol *sym)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300207{
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200208 struct rb_node **p = &self->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300209 struct rb_node *parent = NULL;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000210 const u64 ip = sym->start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300211 struct symbol *s;
212
213 while (*p != NULL) {
214 parent = *p;
215 s = rb_entry(parent, struct symbol, rb_node);
216 if (ip < s->start)
217 p = &(*p)->rb_left;
218 else
219 p = &(*p)->rb_right;
220 }
221 rb_link_node(&sym->rb_node, parent, p);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200222 rb_insert_color(&sym->rb_node, self);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300223}
224
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200225static struct symbol *symbols__find(struct rb_root *self, u64 ip)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300226{
227 struct rb_node *n;
228
229 if (self == NULL)
230 return NULL;
231
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200232 n = self->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300233
234 while (n) {
235 struct symbol *s = rb_entry(n, struct symbol, rb_node);
236
237 if (ip < s->start)
238 n = n->rb_left;
239 else if (ip > s->end)
240 n = n->rb_right;
241 else
242 return s;
243 }
244
245 return NULL;
246}
247
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200248struct symbol *dso__find_symbol(struct dso *self, enum map_type type, u64 addr)
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200249{
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200250 return symbols__find(&self->symbols[type], addr);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200251}
252
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200253int build_id__sprintf(u8 *self, int len, char *bf)
254{
255 char *bid = bf;
256 u8 *raw = self;
257 int i;
258
259 for (i = 0; i < len; ++i) {
260 sprintf(bid, "%02x", *raw);
261 ++raw;
262 bid += 2;
263 }
264
265 return raw - self;
266}
267
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200268size_t dso__fprintf_buildid(struct dso *self, FILE *fp)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300269{
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200270 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200271
272 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200273 return fprintf(fp, "%s", sbuild_id);
274}
275
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200276size_t dso__fprintf(struct dso *self, enum map_type type, FILE *fp)
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -0200277{
278 struct rb_node *nd;
279 size_t ret = fprintf(fp, "dso: %s (", self->short_name);
280
281 ret += dso__fprintf_buildid(self, fp);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200282 ret += fprintf(fp, ")\n");
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200283 for (nd = rb_first(&self->symbols[type]); nd; nd = rb_next(nd)) {
284 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
285 ret += symbol__fprintf(pos, fp);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300286 }
287
288 return ret;
289}
290
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300291/*
292 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
293 * so that we can in the next step set the symbol ->end address and then
294 * call kernel_maps__split_kallsyms.
295 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200296static int dso__load_all_kallsyms(struct dso *self, struct map *map)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300297{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300298 char *line = NULL;
299 size_t n;
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200300 struct rb_root *root = &self->symbols[map->type];
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300301 FILE *file = fopen("/proc/kallsyms", "r");
302
303 if (file == NULL)
304 goto out_failure;
305
306 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000307 u64 start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300308 struct symbol *sym;
309 int line_len, len;
310 char symbol_type;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300311 char *symbol_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300312
313 line_len = getline(&line, &n, file);
314 if (line_len < 0)
315 break;
316
317 if (!line)
318 goto out_failure;
319
320 line[--line_len] = '\0'; /* \n */
321
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300322 len = hex2u64(line, &start);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300323
324 len++;
325 if (len + 2 >= line_len)
326 continue;
327
328 symbol_type = toupper(line[len]);
329 /*
330 * We're interested only in code ('T'ext)
331 */
332 if (symbol_type != 'T' && symbol_type != 'W')
333 continue;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300334
335 symbol_name = line + len + 2;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300336 /*
337 * Will fix up the end later, when we have all symbols sorted.
338 */
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200339 sym = symbol__new(start, 0, symbol_name);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300340
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300341 if (sym == NULL)
342 goto out_delete_line;
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200343 /*
344 * We will pass the symbols to the filter later, in
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200345 * map__split_kallsyms, when we have split the maps per module
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200346 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200347 symbols__insert(root, sym);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300348 }
349
350 free(line);
351 fclose(file);
352
353 return 0;
354
355out_delete_line:
356 free(line);
357out_failure:
358 return -1;
359}
360
361/*
362 * Split the symbols into maps, making sure there are no overlaps, i.e. the
363 * kernel range is broken in several maps, named [kernel].N, as we don't have
364 * the original ELF section names vmlinux have.
365 */
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200366static int dso__split_kallsyms(struct dso *self, struct map *map, struct thread *thread,
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200367 symbol_filter_t filter)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300368{
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200369 struct map *curr_map = map;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300370 struct symbol *pos;
371 int count = 0;
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200372 struct rb_root *root = &self->symbols[map->type];
373 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300374 int kernel_range = 0;
375
376 while (next) {
377 char *module;
378
379 pos = rb_entry(next, struct symbol, rb_node);
380 next = rb_next(&pos->rb_node);
381
382 module = strchr(pos->name, '\t');
383 if (module) {
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200384 if (!thread->use_modules)
385 goto discard_symbol;
386
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300387 *module++ = '\0';
388
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200389 if (strcmp(self->name, module)) {
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200390 curr_map = thread__find_map_by_name(thread, module);
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200391 if (curr_map == NULL) {
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200392 pr_debug("/proc/{kallsyms,modules} "
393 "inconsistency!\n");
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300394 return -1;
395 }
396 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300397 /*
398 * So that we look just like we get from .ko files,
399 * i.e. not prelinked, relative to map->start.
400 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200401 pos->start = curr_map->map_ip(curr_map, pos->start);
402 pos->end = curr_map->map_ip(curr_map, pos->end);
403 } else if (curr_map != map) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300404 char dso_name[PATH_MAX];
405 struct dso *dso;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300406
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300407 snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
408 kernel_range++);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300409
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200410 dso = dso__new(dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300411 if (dso == NULL)
412 return -1;
413
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200414 curr_map = map__new2(pos->start, dso, map->type);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300415 if (map == NULL) {
416 dso__delete(dso);
417 return -1;
418 }
419
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200420 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200421 __thread__insert_map(thread, curr_map);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300422 ++kernel_range;
423 }
424
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200425 if (filter && filter(curr_map, pos)) {
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200426discard_symbol: rb_erase(&pos->rb_node, root);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200427 symbol__delete(pos);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300428 } else {
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200429 if (curr_map != map) {
430 rb_erase(&pos->rb_node, root);
431 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300432 }
Mike Galbraith9974f492009-07-02 08:05:58 +0200433 count++;
434 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300435 }
436
Mike Galbraith9974f492009-07-02 08:05:58 +0200437 return count;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300438}
439
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300440
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200441static int dso__load_kallsyms(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200442 struct thread *thread, symbol_filter_t filter)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300443{
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200444 if (dso__load_all_kallsyms(self, map) < 0)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300445 return -1;
446
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200447 symbols__fixup_end(&self->symbols[map->type]);
448 self->origin = DSO__ORIG_KERNEL;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300449
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200450 return dso__split_kallsyms(self, map, thread, filter);
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -0200451}
452
453size_t kernel_maps__fprintf(FILE *fp)
454{
455 size_t printed = fprintf(fp, "Kernel maps:\n");
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200456 printed += thread__fprintf_maps(kthread, fp);
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200457 return printed + fprintf(fp, "END kernel maps\n");
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300458}
459
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300460static int dso__load_perf_map(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200461 symbol_filter_t filter)
Pekka Enberg80d496b2009-06-08 21:12:48 +0300462{
463 char *line = NULL;
464 size_t n;
465 FILE *file;
466 int nr_syms = 0;
467
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300468 file = fopen(self->long_name, "r");
Pekka Enberg80d496b2009-06-08 21:12:48 +0300469 if (file == NULL)
470 goto out_failure;
471
472 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000473 u64 start, size;
Pekka Enberg80d496b2009-06-08 21:12:48 +0300474 struct symbol *sym;
475 int line_len, len;
476
477 line_len = getline(&line, &n, file);
478 if (line_len < 0)
479 break;
480
481 if (!line)
482 goto out_failure;
483
484 line[--line_len] = '\0'; /* \n */
485
486 len = hex2u64(line, &start);
487
488 len++;
489 if (len + 2 >= line_len)
490 continue;
491
492 len += hex2u64(line + len, &size);
493
494 len++;
495 if (len + 2 >= line_len)
496 continue;
497
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200498 sym = symbol__new(start, size, line + len);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300499
500 if (sym == NULL)
501 goto out_delete_line;
502
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300503 if (filter && filter(map, sym))
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200504 symbol__delete(sym);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300505 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200506 symbols__insert(&self->symbols[map->type], sym);
Pekka Enberg80d496b2009-06-08 21:12:48 +0300507 nr_syms++;
508 }
509 }
510
511 free(line);
512 fclose(file);
513
514 return nr_syms;
515
516out_delete_line:
517 free(line);
518out_failure:
519 return -1;
520}
521
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300522/**
523 * elf_symtab__for_each_symbol - iterate thru all the symbols
524 *
525 * @self: struct elf_symtab instance to iterate
Ingo Molnar83a09442009-08-15 12:26:57 +0200526 * @idx: uint32_t idx
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300527 * @sym: GElf_Sym iterator
528 */
Ingo Molnar83a09442009-08-15 12:26:57 +0200529#define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
530 for (idx = 0, gelf_getsym(syms, idx, &sym);\
531 idx < nr_syms; \
532 idx++, gelf_getsym(syms, idx, &sym))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300533
534static inline uint8_t elf_sym__type(const GElf_Sym *sym)
535{
536 return GELF_ST_TYPE(sym->st_info);
537}
538
539static inline int elf_sym__is_function(const GElf_Sym *sym)
540{
541 return elf_sym__type(sym) == STT_FUNC &&
542 sym->st_name != 0 &&
Arnaldo Carvalho de Melo81833132009-10-05 23:35:03 -0300543 sym->st_shndx != SHN_UNDEF;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300544}
545
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200546static inline int elf_sym__is_label(const GElf_Sym *sym)
547{
548 return elf_sym__type(sym) == STT_NOTYPE &&
549 sym->st_name != 0 &&
550 sym->st_shndx != SHN_UNDEF &&
551 sym->st_shndx != SHN_ABS;
552}
553
554static inline const char *elf_sec__name(const GElf_Shdr *shdr,
555 const Elf_Data *secstrs)
556{
557 return secstrs->d_buf + shdr->sh_name;
558}
559
560static inline int elf_sec__is_text(const GElf_Shdr *shdr,
561 const Elf_Data *secstrs)
562{
563 return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
564}
565
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300566static inline const char *elf_sym__name(const GElf_Sym *sym,
567 const Elf_Data *symstrs)
568{
569 return symstrs->d_buf + sym->st_name;
570}
571
572static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
573 GElf_Shdr *shp, const char *name,
Ingo Molnar83a09442009-08-15 12:26:57 +0200574 size_t *idx)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300575{
576 Elf_Scn *sec = NULL;
577 size_t cnt = 1;
578
579 while ((sec = elf_nextscn(elf, sec)) != NULL) {
580 char *str;
581
582 gelf_getshdr(sec, shp);
583 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
584 if (!strcmp(name, str)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200585 if (idx)
586 *idx = cnt;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300587 break;
588 }
589 ++cnt;
590 }
591
592 return sec;
593}
594
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300595#define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
596 for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
597 idx < nr_entries; \
598 ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
599
600#define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
601 for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
602 idx < nr_entries; \
603 ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
604
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300605/*
606 * We need to check if we have a .dynsym, so that we can handle the
607 * .plt, synthesizing its symbols, that aren't on the symtabs (be it
608 * .dynsym or .symtab).
609 * And always look at the original dso, not at debuginfo packages, that
610 * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
611 */
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200612static int dso__synthesize_plt_symbols(struct dso *self, struct map *map,
613 symbol_filter_t filter)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300614{
615 uint32_t nr_rel_entries, idx;
616 GElf_Sym sym;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000617 u64 plt_offset;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300618 GElf_Shdr shdr_plt;
619 struct symbol *f;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300620 GElf_Shdr shdr_rel_plt, shdr_dynsym;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300621 Elf_Data *reldata, *syms, *symstrs;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300622 Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
623 size_t dynsym_idx;
624 GElf_Ehdr ehdr;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300625 char sympltname[1024];
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300626 Elf *elf;
627 int nr = 0, symidx, fd, err = 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300628
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300629 fd = open(self->long_name, O_RDONLY);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300630 if (fd < 0)
631 goto out;
632
Marti Raudsepp84087122009-10-24 19:10:36 +0300633 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300634 if (elf == NULL)
635 goto out_close;
636
637 if (gelf_getehdr(elf, &ehdr) == NULL)
638 goto out_elf_end;
639
640 scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
641 ".dynsym", &dynsym_idx);
642 if (scn_dynsym == NULL)
643 goto out_elf_end;
644
645 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300646 ".rela.plt", NULL);
647 if (scn_plt_rel == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300648 scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300649 ".rel.plt", NULL);
650 if (scn_plt_rel == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300651 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300652 }
653
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300654 err = -1;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300655
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300656 if (shdr_rel_plt.sh_link != dynsym_idx)
657 goto out_elf_end;
658
659 if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
660 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300661
662 /*
Ingo Molnar83a09442009-08-15 12:26:57 +0200663 * Fetch the relocation section to find the idxes to the GOT
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300664 * and the symbols in the .dynsym they refer to.
665 */
666 reldata = elf_getdata(scn_plt_rel, NULL);
667 if (reldata == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300668 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300669
670 syms = elf_getdata(scn_dynsym, NULL);
671 if (syms == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300672 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300673
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300674 scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300675 if (scn_symstrs == NULL)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300676 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300677
678 symstrs = elf_getdata(scn_symstrs, NULL);
679 if (symstrs == 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 nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
683 plt_offset = shdr_plt.sh_offset;
684
685 if (shdr_rel_plt.sh_type == SHT_RELA) {
686 GElf_Rela pos_mem, *pos;
687
688 elf_section__for_each_rela(reldata, pos, pos_mem, idx,
689 nr_rel_entries) {
690 symidx = GELF_R_SYM(pos->r_info);
691 plt_offset += shdr_plt.sh_entsize;
692 gelf_getsym(syms, symidx, &sym);
693 snprintf(sympltname, sizeof(sympltname),
694 "%s@plt", elf_sym__name(&sym, symstrs));
695
696 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200697 sympltname);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300698 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300699 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300700
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200701 if (filter && filter(map, f))
702 symbol__delete(f);
703 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200704 symbols__insert(&self->symbols[map->type], f);
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200705 ++nr;
706 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300707 }
708 } else if (shdr_rel_plt.sh_type == SHT_REL) {
709 GElf_Rel pos_mem, *pos;
710 elf_section__for_each_rel(reldata, pos, pos_mem, idx,
711 nr_rel_entries) {
712 symidx = GELF_R_SYM(pos->r_info);
713 plt_offset += shdr_plt.sh_entsize;
714 gelf_getsym(syms, symidx, &sym);
715 snprintf(sympltname, sizeof(sympltname),
716 "%s@plt", elf_sym__name(&sym, symstrs));
717
718 f = symbol__new(plt_offset, shdr_plt.sh_entsize,
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200719 sympltname);
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300720 if (!f)
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300721 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300722
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200723 if (filter && filter(map, f))
724 symbol__delete(f);
725 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200726 symbols__insert(&self->symbols[map->type], f);
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -0200727 ++nr;
728 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300729 }
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300730 }
731
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300732 err = 0;
733out_elf_end:
734 elf_end(elf);
735out_close:
736 close(fd);
737
738 if (err == 0)
739 return nr;
740out:
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200741 pr_warning("%s: problems reading %s PLT info.\n",
742 __func__, self->long_name);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300743 return 0;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300744}
745
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200746static int dso__load_sym(struct dso *self, struct map *map,
747 struct thread *thread, const char *name, int fd,
748 symbol_filter_t filter, int kernel, int kmodule)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300749{
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300750 struct map *curr_map = map;
751 struct dso *curr_dso = self;
752 size_t dso_name_len = strlen(self->short_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200753 Elf_Data *symstrs, *secstrs;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300754 uint32_t nr_syms;
755 int err = -1;
Ingo Molnar83a09442009-08-15 12:26:57 +0200756 uint32_t idx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300757 GElf_Ehdr ehdr;
758 GElf_Shdr shdr;
759 Elf_Data *syms;
760 GElf_Sym sym;
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300761 Elf_Scn *sec, *sec_strndx;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300762 Elf *elf;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300763 int nr = 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300764
Marti Raudsepp84087122009-10-24 19:10:36 +0300765 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300766 if (elf == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200767 pr_err("%s: cannot read %s ELF file.\n", __func__, name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300768 goto out_close;
769 }
770
771 if (gelf_getehdr(elf, &ehdr) == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200772 pr_err("%s: cannot get elf header.\n", __func__);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300773 goto out_elf_end;
774 }
775
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300776 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
777 if (sec == NULL) {
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -0300778 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
779 if (sec == NULL)
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300780 goto out_elf_end;
Arnaldo Carvalho de Melo8ce998d2009-06-03 00:54:33 -0300781 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300782
783 syms = elf_getdata(sec, NULL);
784 if (syms == NULL)
785 goto out_elf_end;
786
787 sec = elf_getscn(elf, shdr.sh_link);
788 if (sec == NULL)
789 goto out_elf_end;
790
791 symstrs = elf_getdata(sec, NULL);
792 if (symstrs == NULL)
793 goto out_elf_end;
794
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200795 sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
796 if (sec_strndx == NULL)
797 goto out_elf_end;
798
799 secstrs = elf_getdata(sec_strndx, NULL);
Stoyan Gaydarov9b30a262009-07-30 05:25:29 -0500800 if (secstrs == NULL)
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200801 goto out_elf_end;
802
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300803 nr_syms = shdr.sh_size / shdr.sh_entsize;
804
Arjan van de Vene9fbc9d2009-06-06 21:22:33 +0200805 memset(&sym, 0, sizeof(sym));
Mike Galbraithd20ff6b2009-07-20 14:01:38 +0200806 if (!kernel) {
807 self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -0300808 elf_section_by_name(elf, &ehdr, &shdr,
809 ".gnu.prelink_undo",
810 NULL) != NULL);
Mike Galbraithd20ff6b2009-07-20 14:01:38 +0200811 } else self->adjust_symbols = 0;
812
Ingo Molnar83a09442009-08-15 12:26:57 +0200813 elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300814 struct symbol *f;
Ingo Molnar83a09442009-08-15 12:26:57 +0200815 const char *elf_name;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300816 char *demangled = NULL;
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200817 int is_label = elf_sym__is_label(&sym);
818 const char *section_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300819
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200820 if (!is_label && !elf_sym__is_function(&sym))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300821 continue;
822
823 sec = elf_getscn(elf, sym.st_shndx);
824 if (!sec)
825 goto out_elf_end;
826
827 gelf_getshdr(sec, &shdr);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200828
829 if (is_label && !elf_sec__is_text(&shdr, secstrs))
830 continue;
831
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300832 elf_name = elf_sym__name(&sym, symstrs);
Mike Galbraith6cfcc532009-07-02 08:08:36 +0200833 section_name = elf_sec__name(&shdr, secstrs);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200834
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300835 if (kernel || kmodule) {
836 char dso_name[PATH_MAX];
837
838 if (strcmp(section_name,
839 curr_dso->short_name + dso_name_len) == 0)
840 goto new_symbol;
841
842 if (strcmp(section_name, ".text") == 0) {
843 curr_map = map;
844 curr_dso = self;
845 goto new_symbol;
846 }
847
848 snprintf(dso_name, sizeof(dso_name),
849 "%s%s", self->short_name, section_name);
850
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200851 curr_map = thread__find_map_by_name(thread, dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300852 if (curr_map == NULL) {
853 u64 start = sym.st_value;
854
855 if (kmodule)
856 start += map->start + shdr.sh_offset;
857
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200858 curr_dso = dso__new(dso_name);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300859 if (curr_dso == NULL)
860 goto out_elf_end;
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -0200861 curr_map = map__new2(start, curr_dso,
862 MAP__FUNCTION);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300863 if (curr_map == NULL) {
864 dso__delete(curr_dso);
865 goto out_elf_end;
866 }
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200867 curr_map->map_ip = identity__map_ip;
868 curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300869 curr_dso->origin = DSO__ORIG_KERNEL;
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200870 __thread__insert_map(kthread, curr_map);
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200871 dsos__add(&dsos__kernel, curr_dso);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300872 } else
873 curr_dso = curr_map->dso;
874
875 goto new_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300876 }
877
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300878 if (curr_dso->adjust_symbols) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200879 pr_debug2("adjusting symbol: st_value: %Lx sh_addr: "
880 "%Lx sh_offset: %Lx\n", (u64)sym.st_value,
881 (u64)shdr.sh_addr, (u64)shdr.sh_offset);
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -0300882 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300883 }
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -0300884 /*
885 * We need to figure out if the object was created from C++ sources
886 * DWARF DW_compile_unit has this, but we don't always have access
887 * to it...
888 */
Ingo Molnar83a09442009-08-15 12:26:57 +0200889 demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -0300890 if (demangled != NULL)
Ingo Molnar83a09442009-08-15 12:26:57 +0200891 elf_name = demangled;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300892new_symbol:
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200893 f = symbol__new(sym.st_value, sym.st_size, elf_name);
Arnaldo Carvalho de Melo28ac9092009-07-20 14:14:12 -0300894 free(demangled);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300895 if (!f)
896 goto out_elf_end;
897
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300898 if (filter && filter(curr_map, f))
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200899 symbol__delete(f);
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300900 else {
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200901 symbols__insert(&curr_dso->symbols[curr_map->type], f);
Arnaldo Carvalho de Melo69ee69f2009-05-28 14:55:26 -0300902 nr++;
903 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300904 }
905
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300906 /*
907 * For misannotated, zeroed, ASM function sizes.
908 */
909 if (nr > 0)
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -0200910 symbols__fixup_end(&self->symbols[map->type]);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300911 err = nr;
912out_elf_end:
913 elf_end(elf);
914out_close:
915 return err;
916}
917
Arnaldo Carvalho de Melo78075ca2009-11-20 20:51:26 -0200918static bool dso__build_id_equal(const struct dso *self, u8 *build_id)
919{
920 return memcmp(self->build_id, build_id, sizeof(self->build_id)) == 0;
921}
922
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200923static bool __dsos__read_build_ids(struct list_head *head)
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100924{
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200925 bool have_build_id = false;
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100926 struct dso *pos;
927
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200928 list_for_each_entry(pos, head, node)
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200929 if (filename__read_build_id(pos->long_name, pos->build_id,
930 sizeof(pos->build_id)) > 0) {
931 have_build_id = true;
932 pos->has_build_id = true;
933 }
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100934
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200935 return have_build_id;
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100936}
937
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200938bool dsos__read_build_ids(void)
939{
940 return __dsos__read_build_ids(&dsos__kernel) ||
941 __dsos__read_build_ids(&dsos__user);
942}
943
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -0200944/*
945 * Align offset to 4 bytes as needed for note name and descriptor data.
946 */
947#define NOTE_ALIGN(n) (((n) + 3) & -4U)
948
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -0200949int filename__read_build_id(const char *filename, void *bf, size_t size)
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300950{
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -0200951 int fd, err = -1;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300952 GElf_Ehdr ehdr;
953 GElf_Shdr shdr;
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -0200954 Elf_Data *data;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300955 Elf_Scn *sec;
Pekka Enberge57cfcd2009-11-22 12:29:44 +0200956 Elf_Kind ek;
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -0200957 void *ptr;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300958 Elf *elf;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300959
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -0200960 if (size < BUILD_ID_SIZE)
961 goto out;
962
963 fd = open(filename, O_RDONLY);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300964 if (fd < 0)
965 goto out;
966
Marti Raudsepp84087122009-10-24 19:10:36 +0300967 elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300968 if (elf == NULL) {
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200969 pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300970 goto out_close;
971 }
972
Pekka Enberge57cfcd2009-11-22 12:29:44 +0200973 ek = elf_kind(elf);
974 if (ek != ELF_K_ELF)
975 goto out_elf_end;
976
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300977 if (gelf_getehdr(elf, &ehdr) == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200978 pr_err("%s: cannot get elf header.\n", __func__);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300979 goto out_elf_end;
980 }
981
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -0200982 sec = elf_section_by_name(elf, &ehdr, &shdr,
983 ".note.gnu.build-id", NULL);
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -0200984 if (sec == NULL) {
985 sec = elf_section_by_name(elf, &ehdr, &shdr,
986 ".notes", NULL);
987 if (sec == NULL)
988 goto out_elf_end;
989 }
990
991 data = elf_getdata(sec, NULL);
992 if (data == NULL)
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -0300993 goto out_elf_end;
994
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -0200995 ptr = data->d_buf;
996 while (ptr < (data->d_buf + data->d_size)) {
997 GElf_Nhdr *nhdr = ptr;
998 int namesz = NOTE_ALIGN(nhdr->n_namesz),
999 descsz = NOTE_ALIGN(nhdr->n_descsz);
1000 const char *name;
1001
1002 ptr += sizeof(*nhdr);
1003 name = ptr;
1004 ptr += namesz;
1005 if (nhdr->n_type == NT_GNU_BUILD_ID &&
1006 nhdr->n_namesz == sizeof("GNU")) {
1007 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
1008 memcpy(bf, ptr, BUILD_ID_SIZE);
1009 err = BUILD_ID_SIZE;
1010 break;
1011 }
1012 }
1013 ptr += descsz;
1014 }
Arnaldo Carvalho de Melo2643ce12009-11-03 21:46:10 -02001015out_elf_end:
1016 elf_end(elf);
1017out_close:
1018 close(fd);
1019out:
1020 return err;
1021}
1022
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001023int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
1024{
1025 int fd, err = -1;
1026
1027 if (size < BUILD_ID_SIZE)
1028 goto out;
1029
1030 fd = open(filename, O_RDONLY);
1031 if (fd < 0)
1032 goto out;
1033
1034 while (1) {
1035 char bf[BUFSIZ];
1036 GElf_Nhdr nhdr;
1037 int namesz, descsz;
1038
1039 if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
1040 break;
1041
Arnaldo Carvalho de Melofd7a3462009-11-20 20:51:25 -02001042 namesz = NOTE_ALIGN(nhdr.n_namesz);
1043 descsz = NOTE_ALIGN(nhdr.n_descsz);
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001044 if (nhdr.n_type == NT_GNU_BUILD_ID &&
1045 nhdr.n_namesz == sizeof("GNU")) {
1046 if (read(fd, bf, namesz) != namesz)
1047 break;
1048 if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
1049 if (read(fd, build_id,
1050 BUILD_ID_SIZE) == BUILD_ID_SIZE) {
1051 err = 0;
1052 break;
1053 }
1054 } else if (read(fd, bf, descsz) != descsz)
1055 break;
1056 } else {
1057 int n = namesz + descsz;
1058 if (read(fd, bf, n) != n)
1059 break;
1060 }
1061 }
1062 close(fd);
1063out:
1064 return err;
1065}
1066
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001067char dso__symtab_origin(const struct dso *self)
1068{
1069 static const char origin[] = {
1070 [DSO__ORIG_KERNEL] = 'k',
1071 [DSO__ORIG_JAVA_JIT] = 'j',
1072 [DSO__ORIG_FEDORA] = 'f',
1073 [DSO__ORIG_UBUNTU] = 'u',
1074 [DSO__ORIG_BUILDID] = 'b',
1075 [DSO__ORIG_DSO] = 'd',
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001076 [DSO__ORIG_KMODULE] = 'K',
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001077 };
1078
1079 if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
1080 return '!';
1081 return origin[self->origin];
1082}
1083
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001084int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001085{
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001086 int size = PATH_MAX;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001087 char *name;
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001088 u8 build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001089 int ret = -1;
1090 int fd;
1091
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001092 dso__set_loaded(self, map->type);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001093
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001094 if (self->kernel)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001095 return dso__load_kernel_sym(self, map, kthread, filter);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001096
1097 name = malloc(size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001098 if (!name)
1099 return -1;
1100
Arnaldo Carvalho de Melo30d7a772009-07-02 21:24:14 -03001101 self->adjust_symbols = 0;
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001102
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001103 if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001104 ret = dso__load_perf_map(self, map, filter);
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001105 self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
1106 DSO__ORIG_NOT_FOUND;
1107 return ret;
1108 }
1109
1110 self->origin = DSO__ORIG_FEDORA - 1;
Pekka Enberg80d496b2009-06-08 21:12:48 +03001111
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001112more:
1113 do {
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001114 self->origin++;
1115 switch (self->origin) {
1116 case DSO__ORIG_FEDORA:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001117 snprintf(name, size, "/usr/lib/debug%s.debug",
1118 self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001119 break;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001120 case DSO__ORIG_UBUNTU:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001121 snprintf(name, size, "/usr/lib/debug%s",
1122 self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001123 break;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001124 case DSO__ORIG_BUILDID:
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001125 if (filename__read_build_id(self->long_name, build_id,
1126 sizeof(build_id))) {
1127 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1128
1129 build_id__sprintf(build_id, sizeof(build_id),
1130 build_id_hex);
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001131 snprintf(name, size,
1132 "/usr/lib/debug/.build-id/%.2s/%s.debug",
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001133 build_id_hex, build_id_hex + 2);
1134 if (self->has_build_id)
1135 goto compare_build_id;
1136 break;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001137 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001138 self->origin++;
Arnaldo Carvalho de Melo4d1e00a2009-08-05 19:02:49 -03001139 /* Fall thru */
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001140 case DSO__ORIG_DSO:
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001141 snprintf(name, size, "%s", self->long_name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001142 break;
1143
1144 default:
1145 goto out;
1146 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001147
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001148 if (self->has_build_id) {
Arnaldo Carvalho de Melod3379ab2009-11-18 20:20:50 -02001149 if (filename__read_build_id(name, build_id,
1150 sizeof(build_id)) < 0)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001151 goto more;
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001152compare_build_id:
Arnaldo Carvalho de Melo78075ca2009-11-20 20:51:26 -02001153 if (!dso__build_id_equal(self, build_id))
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -02001154 goto more;
1155 }
1156
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001157 fd = open(name, O_RDONLY);
1158 } while (fd < 0);
1159
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001160 ret = dso__load_sym(self, map, NULL, name, fd, filter, 0, 0);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001161 close(fd);
1162
1163 /*
1164 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
1165 */
1166 if (!ret)
1167 goto more;
1168
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001169 if (ret > 0) {
Arnaldo Carvalho de Melo82164162009-11-16 13:48:11 -02001170 int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001171 if (nr_plt > 0)
1172 ret += nr_plt;
1173 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001174out:
1175 free(name);
Arnaldo Carvalho de Melo1340e6b2009-08-11 17:04:36 -03001176 if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
1177 return 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001178 return ret;
1179}
1180
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001181static struct symbol *thread__find_symbol(struct thread *self, u64 ip,
1182 enum map_type type, struct map **mapp,
1183 symbol_filter_t filter)
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001184{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001185 struct map *map = thread__find_map(self, type, ip);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001186
1187 if (mapp)
1188 *mapp = map;
1189
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001190 if (map) {
1191 ip = map->map_ip(map, ip);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001192 return map__find_symbol(map, ip, filter);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001193 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001194
1195 return NULL;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001196}
1197
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -02001198struct symbol *kernel_maps__find_function(u64 ip, struct map **mapp,
1199 symbol_filter_t filter)
1200{
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001201 return thread__find_symbol(kthread, ip, MAP__FUNCTION, mapp, filter);
Arnaldo Carvalho de Melo23ea4a32009-11-27 16:29:19 -02001202}
1203
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001204static struct map *thread__find_map_by_name(struct thread *self, char *name)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001205{
1206 struct rb_node *nd;
1207
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001208 for (nd = rb_first(&self->maps[MAP__FUNCTION]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001209 struct map *map = rb_entry(nd, struct map, rb_node);
1210
1211 if (map->dso && strcmp(map->dso->name, name) == 0)
1212 return map;
1213 }
1214
1215 return NULL;
1216}
1217
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001218static int dsos__set_modules_path_dir(char *dirname)
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001219{
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001220 struct dirent *dent;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001221 DIR *dir = opendir(dirname);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001222
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001223 if (!dir) {
Arnaldo Carvalho de Melo87f8ea42009-11-22 13:21:41 -02001224 pr_debug("%s: cannot open %s dir\n", __func__, dirname);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001225 return -1;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001226 }
1227
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001228 while ((dent = readdir(dir)) != NULL) {
1229 char path[PATH_MAX];
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001230
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001231 if (dent->d_type == DT_DIR) {
1232 if (!strcmp(dent->d_name, ".") ||
1233 !strcmp(dent->d_name, ".."))
1234 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001235
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001236 snprintf(path, sizeof(path), "%s/%s",
1237 dirname, dent->d_name);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001238 if (dsos__set_modules_path_dir(path) < 0)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001239 goto failure;
1240 } else {
1241 char *dot = strrchr(dent->d_name, '.'),
1242 dso_name[PATH_MAX];
1243 struct map *map;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001244 char *long_name;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001245
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001246 if (dot == NULL || strcmp(dot, ".ko"))
1247 continue;
1248 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
1249 (int)(dot - dent->d_name), dent->d_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001250
Arnaldo Carvalho de Meloa2a99e82009-10-05 14:26:18 -03001251 strxfrchar(dso_name, '-', '_');
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001252 map = thread__find_map_by_name(kthread, dso_name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001253 if (map == NULL)
1254 continue;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001255
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001256 snprintf(path, sizeof(path), "%s/%s",
1257 dirname, dent->d_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001258
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001259 long_name = strdup(path);
1260 if (long_name == NULL)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001261 goto failure;
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001262 dso__set_long_name(map->dso, long_name);
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001263 }
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001264 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001265
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001266 return 0;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001267failure:
1268 closedir(dir);
1269 return -1;
Mike Galbraith6cfcc532009-07-02 08:08:36 +02001270}
1271
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001272static int dsos__set_modules_path(void)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001273{
1274 struct utsname uts;
1275 char modules_path[PATH_MAX];
1276
1277 if (uname(&uts) < 0)
1278 return -1;
1279
1280 snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
1281 uts.release);
1282
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001283 return dsos__set_modules_path_dir(modules_path);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001284}
1285
1286/*
1287 * Constructor variant for modules (where we know from /proc/modules where
1288 * they are loaded) and for vmlinux, where only after we load all the
1289 * symbols we'll know where it starts and ends.
1290 */
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001291static struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001292{
1293 struct map *self = malloc(sizeof(*self));
1294
1295 if (self != NULL) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001296 /*
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -02001297 * ->end will be filled after we load all the symbols
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001298 */
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001299 map__init(self, type, start, 0, 0, dso);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001300 }
Arnaldo Carvalho de Meloafb7b4f2009-10-30 16:28:23 -02001301
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001302 return self;
1303}
1304
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001305static int thread__create_module_maps(struct thread *self)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001306{
1307 char *line = NULL;
1308 size_t n;
1309 FILE *file = fopen("/proc/modules", "r");
1310 struct map *map;
1311
1312 if (file == NULL)
1313 return -1;
1314
1315 while (!feof(file)) {
1316 char name[PATH_MAX];
1317 u64 start;
1318 struct dso *dso;
1319 char *sep;
1320 int line_len;
1321
1322 line_len = getline(&line, &n, file);
1323 if (line_len < 0)
1324 break;
1325
1326 if (!line)
1327 goto out_failure;
1328
1329 line[--line_len] = '\0'; /* \n */
1330
1331 sep = strrchr(line, 'x');
1332 if (sep == NULL)
1333 continue;
1334
1335 hex2u64(sep + 1, &start);
1336
1337 sep = strchr(line, ' ');
1338 if (sep == NULL)
1339 continue;
1340
1341 *sep = '\0';
1342
1343 snprintf(name, sizeof(name), "[%s]", line);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001344 dso = dso__new(name);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001345
1346 if (dso == NULL)
1347 goto out_delete_line;
1348
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001349 map = map__new2(start, dso, MAP__FUNCTION);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001350 if (map == NULL) {
1351 dso__delete(dso);
1352 goto out_delete_line;
1353 }
1354
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -02001355 snprintf(name, sizeof(name),
1356 "/sys/module/%s/notes/.note.gnu.build-id", line);
1357 if (sysfs__read_build_id(name, dso->build_id,
1358 sizeof(dso->build_id)) == 0)
1359 dso->has_build_id = true;
1360
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001361 dso->origin = DSO__ORIG_KMODULE;
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001362 __thread__insert_map(self, map);
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001363 dsos__add(&dsos__kernel, dso);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001364 }
1365
1366 free(line);
1367 fclose(file);
1368
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001369 return dsos__set_modules_path();
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001370
1371out_delete_line:
1372 free(line);
1373out_failure:
1374 return -1;
1375}
1376
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001377static int dso__load_vmlinux(struct dso *self, struct map *map, struct thread *thread,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001378 const char *vmlinux, symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001379{
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001380 int err = -1, fd;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001381
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001382 if (self->has_build_id) {
1383 u8 build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001384
Arnaldo Carvalho de Melofbd733b2009-11-20 20:51:28 -02001385 if (filename__read_build_id(vmlinux, build_id,
1386 sizeof(build_id)) < 0) {
1387 pr_debug("No build_id in %s, ignoring it\n", vmlinux);
1388 return -1;
1389 }
1390 if (!dso__build_id_equal(self, build_id)) {
1391 char expected_build_id[BUILD_ID_SIZE * 2 + 1],
1392 vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
1393
1394 build_id__sprintf(self->build_id,
1395 sizeof(self->build_id),
1396 expected_build_id);
1397 build_id__sprintf(build_id, sizeof(build_id),
1398 vmlinux_build_id);
1399 pr_debug("build_id in %s is %s while expected is %s, "
1400 "ignoring it\n", vmlinux, vmlinux_build_id,
1401 expected_build_id);
1402 return -1;
1403 }
1404 }
1405
1406 fd = open(vmlinux, O_RDONLY);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001407 if (fd < 0)
1408 return -1;
1409
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001410 dso__set_loaded(self, map->type);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001411 err = dso__load_sym(self, map, thread, self->long_name, fd, filter, 1, 0);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001412 close(fd);
1413
1414 return err;
1415}
1416
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001417static int dso__load_kernel_sym(struct dso *self, struct map *map,
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001418 struct thread *thread, symbol_filter_t filter)
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001419{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001420 int err;
1421 bool is_kallsyms;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001422
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001423 if (vmlinux_path != NULL) {
1424 int i;
1425 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1426 vmlinux_path__nr_entries);
1427 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001428 err = dso__load_vmlinux(self, map, thread,
1429 vmlinux_path[i], filter);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001430 if (err > 0) {
1431 pr_debug("Using %s for symbols\n",
1432 vmlinux_path[i]);
1433 dso__set_long_name(self,
1434 strdup(vmlinux_path[i]));
1435 goto out_fixup;
1436 }
1437 }
1438 }
1439
1440 is_kallsyms = self->long_name[0] == '[';
1441 if (is_kallsyms)
1442 goto do_kallsyms;
1443
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001444 err = dso__load_vmlinux(self, map, thread, self->long_name, filter);
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -02001445 if (err <= 0) {
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001446 pr_info("The file %s cannot be used, "
1447 "trying to use /proc/kallsyms...", self->long_name);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001448do_kallsyms:
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001449 err = dso__load_kallsyms(self, map, thread, filter);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001450 if (err > 0 && !is_kallsyms)
Arnaldo Carvalho de Meloef6ae722009-11-20 20:51:29 -02001451 dso__set_long_name(self, strdup("[kernel.kallsyms]"));
1452 }
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001453
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001454 if (err > 0) {
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001455out_fixup:
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001456 map__fixup_start(map);
1457 map__fixup_end(map);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001458 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001459
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001460 return err;
1461}
1462
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001463LIST_HEAD(dsos__user);
1464LIST_HEAD(dsos__kernel);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001465struct dso *vdso;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001466
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001467static void dsos__add(struct list_head *head, struct dso *dso)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001468{
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001469 list_add_tail(&dso->node, head);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001470}
1471
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001472static struct dso *dsos__find(struct list_head *head, const char *name)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001473{
1474 struct dso *pos;
1475
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001476 list_for_each_entry(pos, head, node)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001477 if (strcmp(pos->name, name) == 0)
1478 return pos;
1479 return NULL;
1480}
1481
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001482struct dso *dsos__findnew(const char *name)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001483{
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001484 struct dso *dso = dsos__find(&dsos__user, name);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001485
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -02001486 if (!dso) {
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001487 dso = dso__new(name);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001488 if (dso != NULL) {
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001489 dsos__add(&dsos__user, dso);
Arnaldo Carvalho de Melocfc10d32009-11-17 15:40:53 -02001490 dso__set_basename(dso);
1491 }
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001492 }
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001493
1494 return dso;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001495}
1496
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001497static void __dsos__fprintf(struct list_head *head, FILE *fp)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001498{
1499 struct dso *pos;
1500
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001501 list_for_each_entry(pos, head, node) {
1502 int i;
1503 for (i = 0; i < MAP__NR_TYPES; ++i)
1504 dso__fprintf(pos, i, fp);
1505 }
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001506}
1507
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001508void dsos__fprintf(FILE *fp)
1509{
1510 __dsos__fprintf(&dsos__kernel, fp);
1511 __dsos__fprintf(&dsos__user, fp);
1512}
1513
1514static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp)
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001515{
1516 struct dso *pos;
1517 size_t ret = 0;
1518
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001519 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001520 ret += dso__fprintf_buildid(pos, fp);
Arnaldo Carvalho de Melo1124ba72009-11-16 21:45:25 -02001521 ret += fprintf(fp, " %s\n", pos->long_name);
Arnaldo Carvalho de Melo9e03eb22009-11-16 16:32:44 -02001522 }
1523 return ret;
1524}
1525
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001526size_t dsos__fprintf_buildid(FILE *fp)
1527{
1528 return (__dsos__fprintf_buildid(&dsos__kernel, fp) +
1529 __dsos__fprintf_buildid(&dsos__user, fp));
1530}
1531
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001532static int thread__create_kernel_map(struct thread *self, const char *vmlinux)
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001533{
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -02001534 struct map *kmap;
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001535 struct dso *kernel = dso__new(vmlinux ?: "[kernel.kallsyms]");
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001536
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001537 if (kernel == NULL)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001538 return -1;
1539
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -02001540 kmap = map__new2(0, kernel, MAP__FUNCTION);
1541 if (kmap == NULL)
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001542 goto out_delete_kernel_dso;
1543
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -02001544 kmap->map_ip = kmap->unmap_ip = identity__map_ip;
1545 kernel->short_name = "[kernel]";
1546 kernel->kernel = 1;
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001547
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001548 vdso = dso__new("[vdso]");
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001549 if (vdso == NULL)
1550 goto out_delete_kernel_map;
Arnaldo Carvalho de Melo36105832009-11-27 16:29:16 -02001551 dso__set_loaded(vdso, MAP__FUNCTION);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001552
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001553 if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
1554 sizeof(kernel->build_id)) == 0)
1555 kernel->has_build_id = true;
1556
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001557 __thread__insert_map(self, kmap);
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -02001558 dsos__add(&dsos__kernel, kernel);
1559 dsos__add(&dsos__user, vdso);
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001560
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001561 return 0;
1562
1563out_delete_kernel_map:
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -02001564 map__delete(kmap);
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001565out_delete_kernel_dso:
1566 dso__delete(kernel);
1567 return -1;
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001568}
1569
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001570static void vmlinux_path__exit(void)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001571{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001572 while (--vmlinux_path__nr_entries >= 0) {
1573 free(vmlinux_path[vmlinux_path__nr_entries]);
1574 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1575 }
1576
1577 free(vmlinux_path);
1578 vmlinux_path = NULL;
1579}
1580
1581static int vmlinux_path__init(void)
1582{
1583 struct utsname uts;
1584 char bf[PATH_MAX];
1585
1586 if (uname(&uts) < 0)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001587 return -1;
1588
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001589 vmlinux_path = malloc(sizeof(char *) * 5);
1590 if (vmlinux_path == NULL)
1591 return -1;
1592
1593 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1594 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1595 goto out_fail;
1596 ++vmlinux_path__nr_entries;
1597 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1598 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1599 goto out_fail;
1600 ++vmlinux_path__nr_entries;
1601 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1602 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1603 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1604 goto out_fail;
1605 ++vmlinux_path__nr_entries;
1606 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1607 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1608 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1609 goto out_fail;
1610 ++vmlinux_path__nr_entries;
1611 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1612 uts.release);
1613 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1614 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1615 goto out_fail;
1616 ++vmlinux_path__nr_entries;
1617
1618 return 0;
1619
1620out_fail:
1621 vmlinux_path__exit();
1622 return -1;
1623}
1624
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001625int symbol__init(struct symbol_conf *conf)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001626{
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001627 const struct symbol_conf *pconf = conf ?: &symbol_conf__defaults;
1628
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001629 elf_version(EV_CURRENT);
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001630 symbol__priv_size = pconf->priv_size;
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001631 thread__init(kthread, 0);
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001632
1633 if (pconf->try_vmlinux_path && vmlinux_path__init() < 0)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001634 return -1;
1635
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001636 if (thread__create_kernel_map(kthread, pconf->vmlinux_name) < 0) {
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001637 vmlinux_path__exit();
1638 return -1;
1639 }
1640
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -02001641 kthread->use_modules = pconf->use_modules;
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001642 if (pconf->use_modules && thread__create_module_maps(kthread) < 0)
Arnaldo Carvalho de Melo87f8ea42009-11-22 13:21:41 -02001643 pr_debug("Failed to load list of modules in use, "
1644 "continuing...\n");
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -02001645 /*
1646 * Now that we have all the maps created, just set the ->end of them:
1647 */
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -02001648 thread__fixup_maps_end(kthread);
Arnaldo Carvalho de Melo6671cb12009-11-20 20:51:24 -02001649 return 0;
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001650}