blob: c0c36965fff0f0060b2f2a077edc17c569648bf2 [file] [log] [blame]
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03001#include <dirent.h>
2#include <errno.h>
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03003#include <stdlib.h>
4#include <stdio.h>
5#include <string.h>
6#include <sys/types.h>
7#include <sys/stat.h>
8#include <sys/param.h>
9#include <fcntl.h>
10#include <unistd.h>
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -020011#include <inttypes.h>
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -030012#include "build-id.h"
Namhyung Kime334c722012-02-10 10:10:17 +090013#include "util.h"
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030014#include "debug.h"
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030015#include "machine.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030016#include "symbol.h"
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -030017#include "strlist.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030018
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030019#include <elf.h>
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -020020#include <limits.h>
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030021#include <sys/utsname.h>
Peter Zijlstra2cdbc462009-08-05 14:05:16 +020022
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -020023#ifndef KSYM_NAME_LEN
Ricardo Ribalda Delgadoc752d042011-10-20 09:43:26 +020024#define KSYM_NAME_LEN 256
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -020025#endif
26
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030027static int dso__load_kernel_sym(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -020028 symbol_filter_t filter);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -030029static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +080030 symbol_filter_t filter);
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030031int vmlinux_path__nr_entries;
32char **vmlinux_path;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030033
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -020034struct symbol_conf symbol_conf = {
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020035 .use_modules = true,
36 .try_vmlinux_path = true,
Stephane Eranian3e6a2a72011-05-17 17:32:07 +020037 .annotate_src = true,
Namhyung Kim328ccda2013-03-25 18:18:18 +090038 .demangle = true,
David Ahernec5761e2010-12-09 13:27:07 -070039 .symfs = "",
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020040};
41
Jiri Olsa44f24cb2012-07-22 14:14:32 +020042static enum dso_binary_type binary_type_symtab[] = {
43 DSO_BINARY_TYPE__KALLSYMS,
44 DSO_BINARY_TYPE__GUEST_KALLSYMS,
45 DSO_BINARY_TYPE__JAVA_JIT,
46 DSO_BINARY_TYPE__DEBUGLINK,
47 DSO_BINARY_TYPE__BUILD_ID_CACHE,
48 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
49 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
50 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
51 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
52 DSO_BINARY_TYPE__GUEST_KMODULE,
53 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020054 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
Jiri Olsa44f24cb2012-07-22 14:14:32 +020055 DSO_BINARY_TYPE__NOT_FOUND,
56};
57
Jiri Olsa028df762012-08-01 14:47:57 +020058#define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
Jiri Olsa44f24cb2012-07-22 14:14:32 +020059
Arnaldo Carvalho de Melo36a3e642010-01-04 16:19:27 -020060bool symbol_type__is_a(char symbol_type, enum map_type map_type)
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020061{
Anton Blanchard31877902011-08-24 16:40:16 +100062 symbol_type = toupper(symbol_type);
63
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020064 switch (map_type) {
65 case MAP__FUNCTION:
66 return symbol_type == 'T' || symbol_type == 'W';
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -020067 case MAP__VARIABLE:
Anton Blanchard31877902011-08-24 16:40:16 +100068 return symbol_type == 'D';
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020069 default:
70 return false;
71 }
72}
73
Anton Blanchard694bf402011-08-24 16:40:17 +100074static int prefix_underscores_count(const char *str)
75{
76 const char *tail = str;
77
78 while (*tail == '_')
79 tail++;
80
81 return tail - str;
82}
83
84#define SYMBOL_A 0
85#define SYMBOL_B 1
86
87static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
88{
89 s64 a;
90 s64 b;
Adrian Hunter34454322013-08-07 14:38:49 +030091 size_t na, nb;
Anton Blanchard694bf402011-08-24 16:40:17 +100092
93 /* Prefer a symbol with non zero length */
94 a = syma->end - syma->start;
95 b = symb->end - symb->start;
96 if ((b == 0) && (a > 0))
97 return SYMBOL_A;
98 else if ((a == 0) && (b > 0))
99 return SYMBOL_B;
100
101 /* Prefer a non weak symbol over a weak one */
102 a = syma->binding == STB_WEAK;
103 b = symb->binding == STB_WEAK;
104 if (b && !a)
105 return SYMBOL_A;
106 if (a && !b)
107 return SYMBOL_B;
108
109 /* Prefer a global symbol over a non global one */
110 a = syma->binding == STB_GLOBAL;
111 b = symb->binding == STB_GLOBAL;
112 if (a && !b)
113 return SYMBOL_A;
114 if (b && !a)
115 return SYMBOL_B;
116
117 /* Prefer a symbol with less underscores */
118 a = prefix_underscores_count(syma->name);
119 b = prefix_underscores_count(symb->name);
120 if (b > a)
121 return SYMBOL_A;
122 else if (a > b)
123 return SYMBOL_B;
124
Adrian Hunter34454322013-08-07 14:38:49 +0300125 /* Choose the symbol with the longest name */
126 na = strlen(syma->name);
127 nb = strlen(symb->name);
128 if (na > nb)
Anton Blanchard694bf402011-08-24 16:40:17 +1000129 return SYMBOL_A;
Adrian Hunter34454322013-08-07 14:38:49 +0300130 else if (na < nb)
Anton Blanchard694bf402011-08-24 16:40:17 +1000131 return SYMBOL_B;
Adrian Hunter34454322013-08-07 14:38:49 +0300132
133 /* Avoid "SyS" kernel syscall aliases */
134 if (na >= 3 && !strncmp(syma->name, "SyS", 3))
135 return SYMBOL_B;
136 if (na >= 10 && !strncmp(syma->name, "compat_SyS", 10))
137 return SYMBOL_B;
138
139 return SYMBOL_A;
Anton Blanchard694bf402011-08-24 16:40:17 +1000140}
141
Namhyung Kime5a18452012-08-06 13:41:20 +0900142void symbols__fixup_duplicate(struct rb_root *symbols)
Anton Blanchard694bf402011-08-24 16:40:17 +1000143{
144 struct rb_node *nd;
145 struct symbol *curr, *next;
146
147 nd = rb_first(symbols);
148
149 while (nd) {
150 curr = rb_entry(nd, struct symbol, rb_node);
151again:
152 nd = rb_next(&curr->rb_node);
153 next = rb_entry(nd, struct symbol, rb_node);
154
155 if (!nd)
156 break;
157
158 if (curr->start != next->start)
159 continue;
160
161 if (choose_best_symbol(curr, next) == SYMBOL_A) {
162 rb_erase(&next->rb_node, symbols);
Chenggang Qind4f74eb2013-10-11 08:27:59 +0800163 symbol__delete(next);
Anton Blanchard694bf402011-08-24 16:40:17 +1000164 goto again;
165 } else {
166 nd = rb_next(&curr->rb_node);
167 rb_erase(&curr->rb_node, symbols);
Chenggang Qind4f74eb2013-10-11 08:27:59 +0800168 symbol__delete(curr);
Anton Blanchard694bf402011-08-24 16:40:17 +1000169 }
170 }
171}
172
Namhyung Kime5a18452012-08-06 13:41:20 +0900173void symbols__fixup_end(struct rb_root *symbols)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300174{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300175 struct rb_node *nd, *prevnd = rb_first(symbols);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300176 struct symbol *curr, *prev;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300177
178 if (prevnd == NULL)
179 return;
180
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300181 curr = rb_entry(prevnd, struct symbol, rb_node);
182
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300183 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300184 prev = curr;
185 curr = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300186
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200187 if (prev->end == prev->start && prev->end != curr->start)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300188 prev->end = curr->start - 1;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300189 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300190
191 /* Last entry */
192 if (curr->end == curr->start)
193 curr->end = roundup(curr->start, 4096);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300194}
195
Namhyung Kime5a18452012-08-06 13:41:20 +0900196void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300197{
198 struct map *prev, *curr;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300199 struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300200
201 if (prevnd == NULL)
202 return;
203
204 curr = rb_entry(prevnd, struct map, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300205
206 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
207 prev = curr;
208 curr = rb_entry(nd, struct map, rb_node);
209 prev->end = curr->start - 1;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300210 }
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -0200211
212 /*
213 * We still haven't the actual symbols, so guess the
214 * last map final address.
215 */
Ian Munsie9d1faba2010-11-25 15:12:53 +1100216 curr->end = ~0ULL;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300217}
218
Namhyung Kime5a18452012-08-06 13:41:20 +0900219struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300220{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300221 size_t namelen = strlen(name) + 1;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300222 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
223 sizeof(*sym) + namelen));
224 if (sym == NULL)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200225 return NULL;
226
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200227 if (symbol_conf.priv_size)
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300228 sym = ((void *)sym) + symbol_conf.priv_size;
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200229
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300230 sym->start = start;
231 sym->end = len ? start + len - 1 : start;
232 sym->binding = binding;
233 sym->namelen = namelen - 1;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200234
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300235 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
236 __func__, name, start, sym->end);
237 memcpy(sym->name, name, namelen);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200238
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300239 return sym;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300240}
241
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300242void symbol__delete(struct symbol *sym)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300243{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300244 free(((void *)sym) - symbol_conf.priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300245}
246
Jiri Olsacdd059d2012-10-27 23:18:32 +0200247size_t symbol__fprintf(struct symbol *sym, FILE *fp)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300248{
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200249 return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300250 sym->start, sym->end,
251 sym->binding == STB_GLOBAL ? 'g' :
252 sym->binding == STB_LOCAL ? 'l' : 'w',
253 sym->name);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300254}
255
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900256size_t symbol__fprintf_symname_offs(const struct symbol *sym,
257 const struct addr_location *al, FILE *fp)
258{
259 unsigned long offset;
260 size_t length;
261
262 if (sym && sym->name) {
263 length = fprintf(fp, "%s", sym->name);
264 if (al) {
David Ahern0b8c25d2013-07-28 09:48:32 -0600265 if (al->addr < sym->end)
266 offset = al->addr - sym->start;
267 else
268 offset = al->addr - al->map->start - sym->start;
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900269 length += fprintf(fp, "+0x%lx", offset);
270 }
271 return length;
272 } else
273 return fprintf(fp, "[unknown]");
274}
275
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900276size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
277{
Akihiro Nagaia978f2a2012-01-30 13:43:15 +0900278 return symbol__fprintf_symname_offs(sym, NULL, fp);
Akihiro Nagai547a92e2012-01-30 13:42:57 +0900279}
280
Jiri Olsacdd059d2012-10-27 23:18:32 +0200281void symbols__delete(struct rb_root *symbols)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300282{
283 struct symbol *pos;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300284 struct rb_node *next = rb_first(symbols);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300285
286 while (next) {
287 pos = rb_entry(next, struct symbol, rb_node);
288 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300289 rb_erase(&pos->rb_node, symbols);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200290 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300291 }
292}
293
Namhyung Kime5a18452012-08-06 13:41:20 +0900294void symbols__insert(struct rb_root *symbols, struct symbol *sym)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300295{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300296 struct rb_node **p = &symbols->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300297 struct rb_node *parent = NULL;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000298 const u64 ip = sym->start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300299 struct symbol *s;
300
301 while (*p != NULL) {
302 parent = *p;
303 s = rb_entry(parent, struct symbol, rb_node);
304 if (ip < s->start)
305 p = &(*p)->rb_left;
306 else
307 p = &(*p)->rb_right;
308 }
309 rb_link_node(&sym->rb_node, parent, p);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300310 rb_insert_color(&sym->rb_node, symbols);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300311}
312
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300313static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300314{
315 struct rb_node *n;
316
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300317 if (symbols == NULL)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300318 return NULL;
319
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300320 n = symbols->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300321
322 while (n) {
323 struct symbol *s = rb_entry(n, struct symbol, rb_node);
324
325 if (ip < s->start)
326 n = n->rb_left;
327 else if (ip > s->end)
328 n = n->rb_right;
329 else
330 return s;
331 }
332
333 return NULL;
334}
335
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300336static struct symbol *symbols__first(struct rb_root *symbols)
337{
338 struct rb_node *n = rb_first(symbols);
339
340 if (n)
341 return rb_entry(n, struct symbol, rb_node);
342
343 return NULL;
344}
345
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200346struct symbol_name_rb_node {
347 struct rb_node rb_node;
348 struct symbol sym;
349};
350
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300351static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200352{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300353 struct rb_node **p = &symbols->rb_node;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200354 struct rb_node *parent = NULL;
Rabin Vincent02a9d032010-11-23 22:08:18 +0530355 struct symbol_name_rb_node *symn, *s;
356
357 symn = container_of(sym, struct symbol_name_rb_node, sym);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200358
359 while (*p != NULL) {
360 parent = *p;
361 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
362 if (strcmp(sym->name, s->sym.name) < 0)
363 p = &(*p)->rb_left;
364 else
365 p = &(*p)->rb_right;
366 }
367 rb_link_node(&symn->rb_node, parent, p);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300368 rb_insert_color(&symn->rb_node, symbols);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200369}
370
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300371static void symbols__sort_by_name(struct rb_root *symbols,
372 struct rb_root *source)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200373{
374 struct rb_node *nd;
375
376 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
377 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300378 symbols__insert_by_name(symbols, pos);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200379 }
380}
381
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300382static struct symbol *symbols__find_by_name(struct rb_root *symbols,
383 const char *name)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200384{
385 struct rb_node *n;
386
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300387 if (symbols == NULL)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200388 return NULL;
389
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300390 n = symbols->rb_node;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200391
392 while (n) {
393 struct symbol_name_rb_node *s;
394 int cmp;
395
396 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
397 cmp = strcmp(name, s->sym.name);
398
399 if (cmp < 0)
400 n = n->rb_left;
401 else if (cmp > 0)
402 n = n->rb_right;
403 else
404 return &s->sym;
405 }
406
407 return NULL;
408}
409
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300410struct symbol *dso__find_symbol(struct dso *dso,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200411 enum map_type type, u64 addr)
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200412{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300413 return symbols__find(&dso->symbols[type], addr);
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200414}
415
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300416struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
417{
418 return symbols__first(&dso->symbols[type]);
419}
420
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300421struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200422 const char *name)
423{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300424 return symbols__find_by_name(&dso->symbol_names[type], name);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200425}
426
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300427void dso__sort_by_name(struct dso *dso, enum map_type type)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200428{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300429 dso__set_sorted_by_name(dso, type);
430 return symbols__sort_by_name(&dso->symbol_names[type],
431 &dso->symbols[type]);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200432}
433
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300434size_t dso__fprintf_symbols_by_name(struct dso *dso,
435 enum map_type type, FILE *fp)
Srikar Dronamraju90f18e62010-08-25 19:13:29 +0530436{
437 size_t ret = 0;
438 struct rb_node *nd;
439 struct symbol_name_rb_node *pos;
440
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300441 for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
Srikar Dronamraju90f18e62010-08-25 19:13:29 +0530442 pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
443 fprintf(fp, "%s\n", pos->sym.name);
444 }
445
446 return ret;
447}
448
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200449int kallsyms__parse(const char *filename, void *arg,
450 int (*process_symbol)(void *arg, const char *name,
Cody P Schafer82151522012-08-10 15:22:48 -0700451 char type, u64 start))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300452{
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300453 char *line = NULL;
454 size_t n;
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200455 int err = -1;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200456 FILE *file = fopen(filename, "r");
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300457
458 if (file == NULL)
459 goto out_failure;
460
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200461 err = 0;
462
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300463 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000464 u64 start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300465 int line_len, len;
466 char symbol_type;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300467 char *symbol_name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300468
469 line_len = getline(&line, &n, file);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800470 if (line_len < 0 || !line)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300471 break;
472
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300473 line[--line_len] = '\0'; /* \n */
474
Arnaldo Carvalho de Meloa0055ae2009-06-01 17:50:19 -0300475 len = hex2u64(line, &start);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300476
477 len++;
478 if (len + 2 >= line_len)
479 continue;
480
Anton Blanchard31877902011-08-24 16:40:16 +1000481 symbol_type = line[len];
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200482 len += 2;
483 symbol_name = line + len;
484 len = line_len - len;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300485
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200486 if (len >= KSYM_NAME_LEN) {
487 err = -1;
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200488 break;
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200489 }
490
Anton Blanchard3f5a4272011-08-24 16:40:15 +1000491 err = process_symbol(arg, symbol_name,
Cody P Schafer82151522012-08-10 15:22:48 -0700492 symbol_type, start);
Anton Blanchard3f5a4272011-08-24 16:40:15 +1000493 if (err)
494 break;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300495 }
496
497 free(line);
498 fclose(file);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200499 return err;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300500
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300501out_failure:
502 return -1;
503}
504
Adrian Hunter316d70d2013-10-08 11:45:48 +0300505int modules__parse(const char *filename, void *arg,
506 int (*process_module)(void *arg, const char *name,
507 u64 start))
508{
509 char *line = NULL;
510 size_t n;
511 FILE *file;
512 int err = 0;
513
514 file = fopen(filename, "r");
515 if (file == NULL)
516 return -1;
517
518 while (1) {
519 char name[PATH_MAX];
520 u64 start;
521 char *sep;
522 ssize_t line_len;
523
524 line_len = getline(&line, &n, file);
525 if (line_len < 0) {
526 if (feof(file))
527 break;
528 err = -1;
529 goto out;
530 }
531
532 if (!line) {
533 err = -1;
534 goto out;
535 }
536
537 line[--line_len] = '\0'; /* \n */
538
539 sep = strrchr(line, 'x');
540 if (sep == NULL)
541 continue;
542
543 hex2u64(sep + 1, &start);
544
545 sep = strchr(line, ' ');
546 if (sep == NULL)
547 continue;
548
549 *sep = '\0';
550
551 scnprintf(name, sizeof(name), "[%s]", line);
552
553 err = process_module(arg, name, start);
554 if (err)
555 break;
556 }
557out:
558 free(line);
559 fclose(file);
560 return err;
561}
562
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200563struct process_kallsyms_args {
564 struct map *map;
565 struct dso *dso;
566};
567
Arnaldo Carvalho de Meloc408fed2010-08-05 12:59:47 -0300568static u8 kallsyms2elf_type(char type)
569{
570 if (type == 'W')
571 return STB_WEAK;
572
573 return isupper(type) ? STB_GLOBAL : STB_LOCAL;
574}
575
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200576static int map__process_kallsym_symbol(void *arg, const char *name,
Cody P Schafer82151522012-08-10 15:22:48 -0700577 char type, u64 start)
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200578{
579 struct symbol *sym;
580 struct process_kallsyms_args *a = arg;
581 struct rb_root *root = &a->dso->symbols[a->map->type];
582
583 if (!symbol_type__is_a(type, a->map->type))
584 return 0;
585
Cody P Schafer82151522012-08-10 15:22:48 -0700586 /*
587 * module symbols are not sorted so we add all
588 * symbols, setting length to 0, and rely on
589 * symbols__fixup_end() to fix it up.
590 */
591 sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200592 if (sym == NULL)
593 return -ENOMEM;
594 /*
595 * We will pass the symbols to the filter later, in
596 * map__split_kallsyms, when we have split the maps per module
597 */
598 symbols__insert(root, sym);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800599
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200600 return 0;
601}
602
603/*
604 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
605 * so that we can in the next step set the symbol ->end address and then
606 * call kernel_maps__split_kallsyms.
607 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300608static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200609 struct map *map)
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200610{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300611 struct process_kallsyms_args args = { .map = map, .dso = dso, };
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200612 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200613}
614
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300615static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map,
616 symbol_filter_t filter)
617{
618 struct map_groups *kmaps = map__kmap(map)->kmaps;
619 struct map *curr_map;
620 struct symbol *pos;
621 int count = 0, moved = 0;
622 struct rb_root *root = &dso->symbols[map->type];
623 struct rb_node *next = rb_first(root);
624
625 while (next) {
626 char *module;
627
628 pos = rb_entry(next, struct symbol, rb_node);
629 next = rb_next(&pos->rb_node);
630
631 module = strchr(pos->name, '\t');
632 if (module)
633 *module = '\0';
634
635 curr_map = map_groups__find(kmaps, map->type, pos->start);
636
637 if (!curr_map || (filter && filter(curr_map, pos))) {
638 rb_erase(&pos->rb_node, root);
639 symbol__delete(pos);
640 } else {
641 pos->start -= curr_map->start - curr_map->pgoff;
642 if (pos->end)
643 pos->end -= curr_map->start - curr_map->pgoff;
644 if (curr_map != map) {
645 rb_erase(&pos->rb_node, root);
646 symbols__insert(
647 &curr_map->dso->symbols[curr_map->type],
648 pos);
649 ++moved;
650 } else {
651 ++count;
652 }
653 }
654 }
655
656 /* Symbols have been adjusted */
657 dso->adjust_symbols = 1;
658
659 return count + moved;
660}
661
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300662/*
663 * Split the symbols into maps, making sure there are no overlaps, i.e. the
664 * kernel range is broken in several maps, named [kernel].N, as we don't have
665 * the original ELF section names vmlinux have.
666 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300667static int dso__split_kallsyms(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200668 symbol_filter_t filter)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300669{
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200670 struct map_groups *kmaps = map__kmap(map)->kmaps;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300671 struct machine *machine = kmaps->machine;
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200672 struct map *curr_map = map;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300673 struct symbol *pos;
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200674 int count = 0, moved = 0;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300675 struct rb_root *root = &dso->symbols[map->type];
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200676 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300677 int kernel_range = 0;
678
679 while (next) {
680 char *module;
681
682 pos = rb_entry(next, struct symbol, rb_node);
683 next = rb_next(&pos->rb_node);
684
685 module = strchr(pos->name, '\t');
686 if (module) {
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200687 if (!symbol_conf.use_modules)
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200688 goto discard_symbol;
689
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300690 *module++ = '\0';
691
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200692 if (strcmp(curr_map->dso->short_name, module)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800693 if (curr_map != map &&
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300694 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300695 machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800696 /*
697 * We assume all symbols of a module are
698 * continuous in * kallsyms, so curr_map
699 * points to a module and all its
700 * symbols are in its kmap. Mark it as
701 * loaded.
702 */
703 dso__set_loaded(curr_map->dso,
704 curr_map->type);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300705 }
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200706
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800707 curr_map = map_groups__find_by_name(kmaps,
708 map->type, module);
709 if (curr_map == NULL) {
Arnaldo Carvalho de Melo2f519032010-05-17 17:57:59 -0300710 pr_debug("%s/proc/{kallsyms,modules} "
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800711 "inconsistency while looking "
712 "for \"%s\" module!\n",
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300713 machine->root_dir, module);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800714 curr_map = map;
715 goto discard_symbol;
716 }
717
718 if (curr_map->dso->loaded &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300719 !machine__is_default_guest(machine))
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200720 goto discard_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300721 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300722 /*
723 * So that we look just like we get from .ko files,
724 * i.e. not prelinked, relative to map->start.
725 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200726 pos->start = curr_map->map_ip(curr_map, pos->start);
727 pos->end = curr_map->map_ip(curr_map, pos->end);
728 } else if (curr_map != map) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300729 char dso_name[PATH_MAX];
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300730 struct dso *ndso;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300731
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200732 if (count == 0) {
733 curr_map = map;
734 goto filter_symbol;
735 }
736
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300737 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800738 snprintf(dso_name, sizeof(dso_name),
739 "[guest.kernel].%d",
740 kernel_range++);
741 else
742 snprintf(dso_name, sizeof(dso_name),
743 "[kernel].%d",
744 kernel_range++);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300745
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300746 ndso = dso__new(dso_name);
747 if (ndso == NULL)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300748 return -1;
749
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300750 ndso->kernel = dso->kernel;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800751
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300752 curr_map = map__new2(pos->start, ndso, map->type);
Zhang, Yanmin37fe5fc2010-02-25 11:00:51 +0800753 if (curr_map == NULL) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300754 dso__delete(ndso);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300755 return -1;
756 }
757
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200758 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200759 map_groups__insert(kmaps, curr_map);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300760 ++kernel_range;
761 }
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200762filter_symbol:
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200763 if (filter && filter(curr_map, pos)) {
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200764discard_symbol: rb_erase(&pos->rb_node, root);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200765 symbol__delete(pos);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300766 } else {
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200767 if (curr_map != map) {
768 rb_erase(&pos->rb_node, root);
769 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200770 ++moved;
771 } else
772 ++count;
Mike Galbraith9974f492009-07-02 08:05:58 +0200773 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300774 }
775
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800776 if (curr_map != map &&
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300777 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300778 machine__is_default_guest(kmaps->machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800779 dso__set_loaded(curr_map->dso, curr_map->type);
780 }
781
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200782 return count + moved;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300783}
784
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300785bool symbol__restricted_filename(const char *filename,
786 const char *restricted_filename)
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -0300787{
788 bool restricted = false;
789
790 if (symbol_conf.kptr_restrict) {
791 char *r = realpath(filename, NULL);
792
793 if (r != NULL) {
794 restricted = strcmp(r, restricted_filename) == 0;
795 free(r);
796 return restricted;
797 }
798 }
799
800 return restricted;
801}
802
Adrian Hunter52afdaf2013-10-09 15:01:11 +0300803struct module_info {
804 struct rb_node rb_node;
805 char *name;
806 u64 start;
807};
808
809static void add_module(struct module_info *mi, struct rb_root *modules)
810{
811 struct rb_node **p = &modules->rb_node;
812 struct rb_node *parent = NULL;
813 struct module_info *m;
814
815 while (*p != NULL) {
816 parent = *p;
817 m = rb_entry(parent, struct module_info, rb_node);
818 if (strcmp(mi->name, m->name) < 0)
819 p = &(*p)->rb_left;
820 else
821 p = &(*p)->rb_right;
822 }
823 rb_link_node(&mi->rb_node, parent, p);
824 rb_insert_color(&mi->rb_node, modules);
825}
826
827static void delete_modules(struct rb_root *modules)
828{
829 struct module_info *mi;
830 struct rb_node *next = rb_first(modules);
831
832 while (next) {
833 mi = rb_entry(next, struct module_info, rb_node);
834 next = rb_next(&mi->rb_node);
835 rb_erase(&mi->rb_node, modules);
836 free(mi->name);
837 free(mi);
838 }
839}
840
841static struct module_info *find_module(const char *name,
842 struct rb_root *modules)
843{
844 struct rb_node *n = modules->rb_node;
845
846 while (n) {
847 struct module_info *m;
848 int cmp;
849
850 m = rb_entry(n, struct module_info, rb_node);
851 cmp = strcmp(name, m->name);
852 if (cmp < 0)
853 n = n->rb_left;
854 else if (cmp > 0)
855 n = n->rb_right;
856 else
857 return m;
858 }
859
860 return NULL;
861}
862
863static int __read_proc_modules(void *arg, const char *name, u64 start)
864{
865 struct rb_root *modules = arg;
866 struct module_info *mi;
867
868 mi = zalloc(sizeof(struct module_info));
869 if (!mi)
870 return -ENOMEM;
871
872 mi->name = strdup(name);
873 mi->start = start;
874
875 if (!mi->name) {
876 free(mi);
877 return -ENOMEM;
878 }
879
880 add_module(mi, modules);
881
882 return 0;
883}
884
885static int read_proc_modules(const char *filename, struct rb_root *modules)
886{
887 if (symbol__restricted_filename(filename, "/proc/modules"))
888 return -1;
889
890 if (modules__parse(filename, modules, __read_proc_modules)) {
891 delete_modules(modules);
892 return -1;
893 }
894
895 return 0;
896}
897
Adrian Hunterfc1b6912013-10-14 16:57:29 +0300898int compare_proc_modules(const char *from, const char *to)
899{
900 struct rb_root from_modules = RB_ROOT;
901 struct rb_root to_modules = RB_ROOT;
902 struct rb_node *from_node, *to_node;
903 struct module_info *from_m, *to_m;
904 int ret = -1;
905
906 if (read_proc_modules(from, &from_modules))
907 return -1;
908
909 if (read_proc_modules(to, &to_modules))
910 goto out_delete_from;
911
912 from_node = rb_first(&from_modules);
913 to_node = rb_first(&to_modules);
914 while (from_node) {
915 if (!to_node)
916 break;
917
918 from_m = rb_entry(from_node, struct module_info, rb_node);
919 to_m = rb_entry(to_node, struct module_info, rb_node);
920
921 if (from_m->start != to_m->start ||
922 strcmp(from_m->name, to_m->name))
923 break;
924
925 from_node = rb_next(from_node);
926 to_node = rb_next(to_node);
927 }
928
929 if (!from_node && !to_node)
930 ret = 0;
931
932 delete_modules(&to_modules);
933out_delete_from:
934 delete_modules(&from_modules);
935
936 return ret;
937}
938
Adrian Hunter52afdaf2013-10-09 15:01:11 +0300939static int do_validate_kcore_modules(const char *filename, struct map *map,
940 struct map_groups *kmaps)
941{
942 struct rb_root modules = RB_ROOT;
943 struct map *old_map;
944 int err;
945
946 err = read_proc_modules(filename, &modules);
947 if (err)
948 return err;
949
950 old_map = map_groups__first(kmaps, map->type);
951 while (old_map) {
952 struct map *next = map_groups__next(old_map);
953 struct module_info *mi;
954
955 if (old_map == map || old_map->start == map->start) {
956 /* The kernel map */
957 old_map = next;
958 continue;
959 }
960
961 /* Module must be in memory at the same address */
962 mi = find_module(old_map->dso->short_name, &modules);
963 if (!mi || mi->start != old_map->start) {
964 err = -EINVAL;
965 goto out;
966 }
967
968 old_map = next;
969 }
970out:
971 delete_modules(&modules);
972 return err;
973}
974
975/*
976 * If kallsyms is referenced by name then we look for filename in the same
977 * directory.
978 */
979static bool filename_from_kallsyms_filename(char *filename,
980 const char *base_name,
981 const char *kallsyms_filename)
982{
983 char *name;
984
985 strcpy(filename, kallsyms_filename);
986 name = strrchr(filename, '/');
987 if (!name)
988 return false;
989
990 name += 1;
991
992 if (!strcmp(name, "kallsyms")) {
993 strcpy(name, base_name);
994 return true;
995 }
996
997 return false;
998}
999
1000static int validate_kcore_modules(const char *kallsyms_filename,
1001 struct map *map)
1002{
1003 struct map_groups *kmaps = map__kmap(map)->kmaps;
1004 char modules_filename[PATH_MAX];
1005
1006 if (!filename_from_kallsyms_filename(modules_filename, "modules",
1007 kallsyms_filename))
1008 return -EINVAL;
1009
1010 if (do_validate_kcore_modules(modules_filename, map, kmaps))
1011 return -EINVAL;
1012
1013 return 0;
1014}
1015
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001016struct kcore_mapfn_data {
1017 struct dso *dso;
1018 enum map_type type;
1019 struct list_head maps;
1020};
1021
1022static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1023{
1024 struct kcore_mapfn_data *md = data;
1025 struct map *map;
1026
1027 map = map__new2(start, md->dso, md->type);
1028 if (map == NULL)
1029 return -ENOMEM;
1030
1031 map->end = map->start + len;
1032 map->pgoff = pgoff;
1033
1034 list_add(&map->node, &md->maps);
1035
1036 return 0;
1037}
1038
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001039static int dso__load_kcore(struct dso *dso, struct map *map,
1040 const char *kallsyms_filename)
1041{
1042 struct map_groups *kmaps = map__kmap(map)->kmaps;
1043 struct machine *machine = kmaps->machine;
1044 struct kcore_mapfn_data md;
1045 struct map *old_map, *new_map, *replacement_map = NULL;
1046 bool is_64_bit;
1047 int err, fd;
1048 char kcore_filename[PATH_MAX];
1049 struct symbol *sym;
1050
1051 /* This function requires that the map is the kernel map */
1052 if (map != machine->vmlinux_maps[map->type])
1053 return -EINVAL;
1054
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001055 if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1056 kallsyms_filename))
1057 return -EINVAL;
1058
1059 /* All modules must be present at their original addresses */
1060 if (validate_kcore_modules(kallsyms_filename, map))
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001061 return -EINVAL;
1062
1063 md.dso = dso;
1064 md.type = map->type;
1065 INIT_LIST_HEAD(&md.maps);
1066
1067 fd = open(kcore_filename, O_RDONLY);
1068 if (fd < 0)
1069 return -EINVAL;
1070
1071 /* Read new maps into temporary lists */
1072 err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
1073 &is_64_bit);
1074 if (err)
1075 goto out_err;
1076
1077 if (list_empty(&md.maps)) {
1078 err = -EINVAL;
1079 goto out_err;
1080 }
1081
1082 /* Remove old maps */
1083 old_map = map_groups__first(kmaps, map->type);
1084 while (old_map) {
1085 struct map *next = map_groups__next(old_map);
1086
1087 if (old_map != map)
1088 map_groups__remove(kmaps, old_map);
1089 old_map = next;
1090 }
1091
1092 /* Find the kernel map using the first symbol */
1093 sym = dso__first_symbol(dso, map->type);
1094 list_for_each_entry(new_map, &md.maps, node) {
1095 if (sym && sym->start >= new_map->start &&
1096 sym->start < new_map->end) {
1097 replacement_map = new_map;
1098 break;
1099 }
1100 }
1101
1102 if (!replacement_map)
1103 replacement_map = list_entry(md.maps.next, struct map, node);
1104
1105 /* Add new maps */
1106 while (!list_empty(&md.maps)) {
1107 new_map = list_entry(md.maps.next, struct map, node);
1108 list_del(&new_map->node);
1109 if (new_map == replacement_map) {
1110 map->start = new_map->start;
1111 map->end = new_map->end;
1112 map->pgoff = new_map->pgoff;
1113 map->map_ip = new_map->map_ip;
1114 map->unmap_ip = new_map->unmap_ip;
1115 map__delete(new_map);
1116 /* Ensure maps are correctly ordered */
1117 map_groups__remove(kmaps, map);
1118 map_groups__insert(kmaps, map);
1119 } else {
1120 map_groups__insert(kmaps, new_map);
1121 }
1122 }
1123
1124 /*
1125 * Set the data type and long name so that kcore can be read via
1126 * dso__data_read_addr().
1127 */
1128 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1129 dso->data_type = DSO_BINARY_TYPE__GUEST_KCORE;
1130 else
1131 dso->data_type = DSO_BINARY_TYPE__KCORE;
1132 dso__set_long_name(dso, strdup(kcore_filename));
1133
1134 close(fd);
1135
1136 if (map->type == MAP__FUNCTION)
1137 pr_debug("Using %s for kernel object code\n", kcore_filename);
1138 else
1139 pr_debug("Using %s for kernel data\n", kcore_filename);
1140
1141 return 0;
1142
1143out_err:
1144 while (!list_empty(&md.maps)) {
1145 map = list_entry(md.maps.next, struct map, node);
1146 list_del(&map->node);
1147 map__delete(map);
1148 }
1149 close(fd);
1150 return -EINVAL;
1151}
1152
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001153int dso__load_kallsyms(struct dso *dso, const char *filename,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001154 struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001155{
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03001156 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1157 return -1;
1158
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001159 if (dso__load_all_kallsyms(dso, filename, map) < 0)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001160 return -1;
1161
Anton Blanchard694bf402011-08-24 16:40:17 +10001162 symbols__fixup_duplicate(&dso->symbols[map->type]);
Anton Blanchard3f5a4272011-08-24 16:40:15 +10001163 symbols__fixup_end(&dso->symbols[map->type]);
1164
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001165 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001166 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001167 else
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001168 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001169
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001170 if (!dso__load_kcore(dso, map, filename))
1171 return dso__split_kallsyms_for_kcore(dso, map, filter);
1172 else
1173 return dso__split_kallsyms(dso, map, filter);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001174}
1175
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001176static int dso__load_perf_map(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -02001177 symbol_filter_t filter)
Pekka Enberg80d496b2009-06-08 21:12:48 +03001178{
1179 char *line = NULL;
1180 size_t n;
1181 FILE *file;
1182 int nr_syms = 0;
1183
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001184 file = fopen(dso->long_name, "r");
Pekka Enberg80d496b2009-06-08 21:12:48 +03001185 if (file == NULL)
1186 goto out_failure;
1187
1188 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001189 u64 start, size;
Pekka Enberg80d496b2009-06-08 21:12:48 +03001190 struct symbol *sym;
1191 int line_len, len;
1192
1193 line_len = getline(&line, &n, file);
1194 if (line_len < 0)
1195 break;
1196
1197 if (!line)
1198 goto out_failure;
1199
1200 line[--line_len] = '\0'; /* \n */
1201
1202 len = hex2u64(line, &start);
1203
1204 len++;
1205 if (len + 2 >= line_len)
1206 continue;
1207
1208 len += hex2u64(line + len, &size);
1209
1210 len++;
1211 if (len + 2 >= line_len)
1212 continue;
1213
Arnaldo Carvalho de Meloc408fed2010-08-05 12:59:47 -03001214 sym = symbol__new(start, size, STB_GLOBAL, line + len);
Pekka Enberg80d496b2009-06-08 21:12:48 +03001215
1216 if (sym == NULL)
1217 goto out_delete_line;
1218
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001219 if (filter && filter(map, sym))
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001220 symbol__delete(sym);
Pekka Enberg80d496b2009-06-08 21:12:48 +03001221 else {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001222 symbols__insert(&dso->symbols[map->type], sym);
Pekka Enberg80d496b2009-06-08 21:12:48 +03001223 nr_syms++;
1224 }
1225 }
1226
1227 free(line);
1228 fclose(file);
1229
1230 return nr_syms;
1231
1232out_delete_line:
1233 free(line);
1234out_failure:
1235 return -1;
1236}
1237
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001238int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001239{
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001240 char *name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001241 int ret = -1;
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001242 u_int i;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001243 struct machine *machine;
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001244 char *root_dir = (char *) "";
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001245 int ss_pos = 0;
1246 struct symsrc ss_[2];
1247 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001248
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001249 dso__set_loaded(dso, map->type);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001250
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001251 if (dso->kernel == DSO_TYPE_KERNEL)
1252 return dso__load_kernel_sym(dso, map, filter);
1253 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1254 return dso__load_guest_kernel_sym(dso, map, filter);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001255
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001256 if (map->groups && map->groups->machine)
1257 machine = map->groups->machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001258 else
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001259 machine = NULL;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001260
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001261 dso->adjust_symbols = 0;
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001262
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001263 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
Pekka Enberg981c1252011-08-09 22:54:18 +03001264 struct stat st;
1265
Vasiliy Kulikove9b52ef2011-08-12 00:55:37 +04001266 if (lstat(dso->name, &st) < 0)
Pekka Enberg981c1252011-08-09 22:54:18 +03001267 return -1;
1268
1269 if (st.st_uid && (st.st_uid != geteuid())) {
1270 pr_warning("File %s not owned by current user or root, "
1271 "ignoring it.\n", dso->name);
1272 return -1;
1273 }
1274
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001275 ret = dso__load_perf_map(dso, map, filter);
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001276 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1277 DSO_BINARY_TYPE__NOT_FOUND;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001278 return ret;
1279 }
1280
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001281 if (machine)
1282 root_dir = machine->root_dir;
1283
David Ahern164c8002013-01-14 10:46:47 -07001284 name = malloc(PATH_MAX);
1285 if (!name)
1286 return -1;
1287
Dave Martin6da80ce2010-07-30 09:50:09 -03001288 /* Iterate over candidate debug images.
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001289 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1290 * and/or opd section) for processing.
Dave Martin6da80ce2010-07-30 09:50:09 -03001291 */
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001292 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001293 struct symsrc *ss = &ss_[ss_pos];
1294 bool next_slot = false;
Dave Martin6da80ce2010-07-30 09:50:09 -03001295
Cody P Schafer005f9292012-08-10 15:22:58 -07001296 enum dso_binary_type symtab_type = binary_type_symtab[i];
Dave Martin6da80ce2010-07-30 09:50:09 -03001297
Cody P Schafer005f9292012-08-10 15:22:58 -07001298 if (dso__binary_type_file(dso, symtab_type,
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001299 root_dir, name, PATH_MAX))
1300 continue;
Dave Martin6da80ce2010-07-30 09:50:09 -03001301
1302 /* Name is now the name of the next image to try */
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001303 if (symsrc__init(ss, dso, name, symtab_type) < 0)
Dave Martin6da80ce2010-07-30 09:50:09 -03001304 continue;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001305
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001306 if (!syms_ss && symsrc__has_symtab(ss)) {
1307 syms_ss = ss;
1308 next_slot = true;
Cody P Schaferd26cd122012-08-10 15:23:00 -07001309 }
1310
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001311 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1312 runtime_ss = ss;
1313 next_slot = true;
Cody P Schafera44f6052012-08-10 15:22:59 -07001314 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001315
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001316 if (next_slot) {
1317 ss_pos++;
Jiri Olsa33ff5812012-04-18 15:46:58 +02001318
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001319 if (syms_ss && runtime_ss)
1320 break;
Dave Martin6da80ce2010-07-30 09:50:09 -03001321 }
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001322
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001323 }
Dave Martin6da80ce2010-07-30 09:50:09 -03001324
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001325 if (!runtime_ss && !syms_ss)
1326 goto out_free;
1327
1328 if (runtime_ss && !syms_ss) {
1329 syms_ss = runtime_ss;
Arnaldo Carvalho de Melo60e4b102011-03-22 15:42:14 -03001330 }
1331
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001332 /* We'll have to hope for the best */
1333 if (!runtime_ss && syms_ss)
1334 runtime_ss = syms_ss;
1335
Adrian Hunter0131c4e2013-08-07 14:38:50 +03001336 if (syms_ss) {
1337 int km;
1338
1339 km = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1340 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1341 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, km);
1342 } else {
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001343 ret = -1;
Adrian Hunter0131c4e2013-08-07 14:38:50 +03001344 }
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001345
David Ahernf47b58b2012-08-19 09:47:14 -06001346 if (ret > 0) {
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001347 int nr_plt;
1348
1349 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter);
1350 if (nr_plt > 0)
1351 ret += nr_plt;
1352 }
1353
1354 for (; ss_pos > 0; ss_pos--)
1355 symsrc__destroy(&ss_[ss_pos - 1]);
1356out_free:
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001357 free(name);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001358 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
Arnaldo Carvalho de Melo1340e6b2009-08-11 17:04:36 -03001359 return 0;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001360 return ret;
1361}
1362
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001363struct map *map_groups__find_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -02001364 enum map_type type, const char *name)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001365{
1366 struct rb_node *nd;
1367
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001368 for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001369 struct map *map = rb_entry(nd, struct map, rb_node);
1370
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001371 if (map->dso && strcmp(map->dso->short_name, name) == 0)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001372 return map;
1373 }
1374
1375 return NULL;
1376}
1377
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001378int dso__load_vmlinux(struct dso *dso, struct map *map,
Franck Bui-Huufd930ff2010-12-10 14:06:03 +01001379 const char *vmlinux, symbol_filter_t filter)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001380{
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001381 int err = -1;
1382 struct symsrc ss;
David Ahernec5761e2010-12-09 13:27:07 -07001383 char symfs_vmlinux[PATH_MAX];
Cody P Schafer005f9292012-08-10 15:22:58 -07001384 enum dso_binary_type symtab_type;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001385
Namhyung Kim5698d2c2013-07-17 17:08:15 +09001386 if (vmlinux[0] == '/')
1387 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1388 else
1389 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
1390 symbol_conf.symfs, vmlinux);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001391
Cody P Schafer21ea4532012-08-10 15:22:56 -07001392 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Cody P Schafer005f9292012-08-10 15:22:58 -07001393 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
Cody P Schafer21ea4532012-08-10 15:22:56 -07001394 else
Cody P Schafer005f9292012-08-10 15:22:58 -07001395 symtab_type = DSO_BINARY_TYPE__VMLINUX;
Cody P Schafer21ea4532012-08-10 15:22:56 -07001396
Cody P Schafer005f9292012-08-10 15:22:58 -07001397 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001398 return -1;
1399
Cody P Schafer261360b2012-08-10 15:23:01 -07001400 err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001401 symsrc__destroy(&ss);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001402
Cody P Schafer515850e2012-08-10 15:22:54 -07001403 if (err > 0) {
Adrian Hunter39b12f782013-08-07 14:38:47 +03001404 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
1405 dso->data_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
1406 else
1407 dso->data_type = DSO_BINARY_TYPE__VMLINUX;
Cody P Schafer515850e2012-08-10 15:22:54 -07001408 dso__set_long_name(dso, (char *)vmlinux);
1409 dso__set_loaded(dso, map->type);
David Ahernec5761e2010-12-09 13:27:07 -07001410 pr_debug("Using %s for symbols\n", symfs_vmlinux);
Cody P Schafer515850e2012-08-10 15:22:54 -07001411 }
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001412
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001413 return err;
1414}
1415
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001416int dso__load_vmlinux_path(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001417 symbol_filter_t filter)
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001418{
1419 int i, err = 0;
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001420 char *filename;
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001421
1422 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001423 vmlinux_path__nr_entries + 1);
1424
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001425 filename = dso__build_id_filename(dso, NULL, 0);
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001426 if (filename != NULL) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001427 err = dso__load_vmlinux(dso, map, filename, filter);
David Ahernb7c14a02013-01-14 10:47:30 -07001428 if (err > 0) {
1429 dso->lname_alloc = 1;
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001430 goto out;
David Ahernb7c14a02013-01-14 10:47:30 -07001431 }
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001432 free(filename);
1433 }
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001434
1435 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001436 err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001437 if (err > 0) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001438 dso__set_long_name(dso, strdup(vmlinux_path[i]));
David Ahernb7c14a02013-01-14 10:47:30 -07001439 dso->lname_alloc = 1;
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001440 break;
1441 }
1442 }
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001443out:
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001444 return err;
1445}
1446
Adrian Hunter0544d422013-10-14 13:43:43 +03001447static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1448{
1449 char kallsyms_filename[PATH_MAX];
1450 struct dirent *dent;
1451 int ret = -1;
1452 DIR *d;
1453
1454 d = opendir(dir);
1455 if (!d)
1456 return -1;
1457
1458 while (1) {
1459 dent = readdir(d);
1460 if (!dent)
1461 break;
1462 if (dent->d_type != DT_DIR)
1463 continue;
1464 scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
1465 "%s/%s/kallsyms", dir, dent->d_name);
1466 if (!validate_kcore_modules(kallsyms_filename, map)) {
1467 strlcpy(dir, kallsyms_filename, dir_sz);
1468 ret = 0;
1469 break;
1470 }
1471 }
1472
1473 closedir(d);
1474
1475 return ret;
1476}
1477
1478static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1479{
1480 u8 host_build_id[BUILD_ID_SIZE];
1481 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1482 bool is_host = false;
1483 char path[PATH_MAX];
1484
1485 if (!dso->has_build_id) {
1486 /*
1487 * Last resort, if we don't have a build-id and couldn't find
1488 * any vmlinux file, try the running kernel kallsyms table.
1489 */
1490 goto proc_kallsyms;
1491 }
1492
1493 if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1494 sizeof(host_build_id)) == 0)
1495 is_host = dso__build_id_equal(dso, host_build_id);
1496
1497 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1498
1499 /* Use /proc/kallsyms if possible */
1500 if (is_host) {
1501 DIR *d;
1502 int fd;
1503
1504 /* If no cached kcore go with /proc/kallsyms */
1505 scnprintf(path, sizeof(path), "%s/[kernel.kcore]/%s",
1506 buildid_dir, sbuild_id);
1507 d = opendir(path);
1508 if (!d)
1509 goto proc_kallsyms;
1510 closedir(d);
1511
1512 /*
1513 * Do not check the build-id cache, until we know we cannot use
1514 * /proc/kcore.
1515 */
1516 fd = open("/proc/kcore", O_RDONLY);
1517 if (fd != -1) {
1518 close(fd);
1519 /* If module maps match go with /proc/kallsyms */
1520 if (!validate_kcore_modules("/proc/kallsyms", map))
1521 goto proc_kallsyms;
1522 }
1523
1524 /* Find kallsyms in build-id cache with kcore */
1525 if (!find_matching_kcore(map, path, sizeof(path)))
1526 return strdup(path);
1527
1528 goto proc_kallsyms;
1529 }
1530
1531 scnprintf(path, sizeof(path), "%s/[kernel.kallsyms]/%s",
1532 buildid_dir, sbuild_id);
1533
1534 if (access(path, F_OK)) {
1535 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1536 sbuild_id);
1537 return NULL;
1538 }
1539
1540 return strdup(path);
1541
1542proc_kallsyms:
1543 return strdup("/proc/kallsyms");
1544}
1545
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001546static int dso__load_kernel_sym(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -02001547 symbol_filter_t filter)
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001548{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001549 int err;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001550 const char *kallsyms_filename = NULL;
1551 char *kallsyms_allocated_filename = NULL;
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001552 /*
David Ahernb226a5a72010-12-07 19:39:46 -07001553 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1554 * it and only it, reporting errors to the user if it cannot be used.
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001555 *
1556 * For instance, try to analyse an ARM perf.data file _without_ a
1557 * build-id, or if the user specifies the wrong path to the right
1558 * vmlinux file, obviously we can't fallback to another vmlinux (a
1559 * x86_86 one, on the machine where analysis is being performed, say),
1560 * or worse, /proc/kallsyms.
1561 *
1562 * If the specified file _has_ a build-id and there is a build-id
1563 * section in the perf.data file, we will still do the expected
1564 * validation in dso__load_vmlinux and will bail out if they don't
1565 * match.
1566 */
David Ahernb226a5a72010-12-07 19:39:46 -07001567 if (symbol_conf.kallsyms_name != NULL) {
1568 kallsyms_filename = symbol_conf.kallsyms_name;
1569 goto do_kallsyms;
1570 }
1571
Willy Tarreaufc2be692013-09-14 10:32:59 +02001572 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001573 err = dso__load_vmlinux(dso, map,
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001574 symbol_conf.vmlinux_name, filter);
Arnaldo Carvalho de Meloe7dadc02010-06-03 18:35:55 -03001575 if (err > 0) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001576 dso__set_long_name(dso,
Arnaldo Carvalho de Meloe7dadc02010-06-03 18:35:55 -03001577 strdup(symbol_conf.vmlinux_name));
David Ahernb7c14a02013-01-14 10:47:30 -07001578 dso->lname_alloc = 1;
Adrian Hunter39b12f782013-08-07 14:38:47 +03001579 return err;
Arnaldo Carvalho de Meloe7dadc02010-06-03 18:35:55 -03001580 }
1581 return err;
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001582 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001583
Willy Tarreaufc2be692013-09-14 10:32:59 +02001584 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001585 err = dso__load_vmlinux_path(dso, map, filter);
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001586 if (err > 0)
Adrian Hunter39b12f782013-08-07 14:38:47 +03001587 return err;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001588 }
1589
David Ahernec5761e2010-12-09 13:27:07 -07001590 /* do not try local files if a symfs was given */
1591 if (symbol_conf.symfs[0] != 0)
1592 return -1;
1593
Adrian Hunter0544d422013-10-14 13:43:43 +03001594 kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
1595 if (!kallsyms_allocated_filename)
1596 return -1;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001597
Adrian Hunter0544d422013-10-14 13:43:43 +03001598 kallsyms_filename = kallsyms_allocated_filename;
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001599
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001600do_kallsyms:
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001601 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001602 if (err > 0)
1603 pr_debug("Using %s for symbols\n", kallsyms_filename);
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001604 free(kallsyms_allocated_filename);
1605
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001606 if (err > 0 && !dso__is_kcore(dso)) {
Cody P Schafer0a0317b2012-08-10 15:22:53 -07001607 dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001608 map__fixup_start(map);
1609 map__fixup_end(map);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001610 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001611
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001612 return err;
1613}
1614
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001615static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
1616 symbol_filter_t filter)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001617{
1618 int err;
1619 const char *kallsyms_filename = NULL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001620 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001621 char path[PATH_MAX];
1622
1623 if (!map->groups) {
1624 pr_debug("Guest kernel map hasn't the point to groups\n");
1625 return -1;
1626 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001627 machine = map->groups->machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001628
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001629 if (machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001630 /*
1631 * if the user specified a vmlinux filename, use it and only
1632 * it, reporting errors to the user if it cannot be used.
1633 * Or use file guest_kallsyms inputted by user on commandline
1634 */
1635 if (symbol_conf.default_guest_vmlinux_name != NULL) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001636 err = dso__load_vmlinux(dso, map,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001637 symbol_conf.default_guest_vmlinux_name, filter);
Adrian Hunter39b12f782013-08-07 14:38:47 +03001638 return err;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001639 }
1640
1641 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1642 if (!kallsyms_filename)
1643 return -1;
1644 } else {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001645 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001646 kallsyms_filename = path;
1647 }
1648
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001649 err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001650 if (err > 0)
Adrian Hunter39b12f782013-08-07 14:38:47 +03001651 pr_debug("Using %s for symbols\n", kallsyms_filename);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001652 if (err > 0 && !dso__is_kcore(dso)) {
Adrian Hunter39b12f782013-08-07 14:38:47 +03001653 machine__mmap_name(machine, path, sizeof(path));
1654 dso__set_long_name(dso, strdup(path));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001655 map__fixup_start(map);
1656 map__fixup_end(map);
1657 }
1658
1659 return err;
1660}
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001661
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001662static void vmlinux_path__exit(void)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001663{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001664 while (--vmlinux_path__nr_entries >= 0) {
1665 free(vmlinux_path[vmlinux_path__nr_entries]);
1666 vmlinux_path[vmlinux_path__nr_entries] = NULL;
1667 }
1668
1669 free(vmlinux_path);
1670 vmlinux_path = NULL;
1671}
1672
1673static int vmlinux_path__init(void)
1674{
1675 struct utsname uts;
1676 char bf[PATH_MAX];
1677
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001678 vmlinux_path = malloc(sizeof(char *) * 5);
1679 if (vmlinux_path == NULL)
1680 return -1;
1681
1682 vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
1683 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1684 goto out_fail;
1685 ++vmlinux_path__nr_entries;
1686 vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
1687 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1688 goto out_fail;
1689 ++vmlinux_path__nr_entries;
David Ahernec5761e2010-12-09 13:27:07 -07001690
1691 /* only try running kernel version if no symfs was given */
1692 if (symbol_conf.symfs[0] != 0)
1693 return 0;
1694
1695 if (uname(&uts) < 0)
1696 return -1;
1697
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001698 snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
1699 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1700 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1701 goto out_fail;
1702 ++vmlinux_path__nr_entries;
1703 snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
1704 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1705 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1706 goto out_fail;
1707 ++vmlinux_path__nr_entries;
1708 snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
1709 uts.release);
1710 vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
1711 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1712 goto out_fail;
1713 ++vmlinux_path__nr_entries;
1714
1715 return 0;
1716
1717out_fail:
1718 vmlinux_path__exit();
1719 return -1;
1720}
1721
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001722static int setup_list(struct strlist **list, const char *list_str,
1723 const char *list_name)
1724{
1725 if (list_str == NULL)
1726 return 0;
1727
1728 *list = strlist__new(true, list_str);
1729 if (!*list) {
1730 pr_err("problems parsing %s list\n", list_name);
1731 return -1;
1732 }
1733 return 0;
1734}
1735
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03001736static bool symbol__read_kptr_restrict(void)
1737{
1738 bool value = false;
1739
1740 if (geteuid() != 0) {
1741 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
1742 if (fp != NULL) {
1743 char line[8];
1744
1745 if (fgets(line, sizeof(line), fp) != NULL)
1746 value = atoi(line) != 0;
1747
1748 fclose(fp);
1749 }
1750 }
1751
1752 return value;
1753}
1754
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001755int symbol__init(void)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001756{
David Ahernec5761e2010-12-09 13:27:07 -07001757 const char *symfs;
1758
Jovi Zhang85e00b52010-09-09 13:30:59 -03001759 if (symbol_conf.initialized)
1760 return 0;
1761
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001762 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
David S. Miller4d439512011-03-29 14:18:39 -03001763
Namhyung Kim166ccc92012-08-06 13:41:19 +09001764 symbol__elf_init();
1765
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001766 if (symbol_conf.sort_by_name)
1767 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1768 sizeof(struct symbol));
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001769
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001770 if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001771 return -1;
1772
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02001773 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1774 pr_err("'.' is the only non valid --field-separator argument\n");
1775 return -1;
1776 }
1777
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001778 if (setup_list(&symbol_conf.dso_list,
1779 symbol_conf.dso_list_str, "dso") < 0)
1780 return -1;
1781
1782 if (setup_list(&symbol_conf.comm_list,
1783 symbol_conf.comm_list_str, "comm") < 0)
1784 goto out_free_dso_list;
1785
1786 if (setup_list(&symbol_conf.sym_list,
1787 symbol_conf.sym_list_str, "symbol") < 0)
1788 goto out_free_comm_list;
1789
David Ahernec5761e2010-12-09 13:27:07 -07001790 /*
1791 * A path to symbols of "/" is identical to ""
1792 * reset here for simplicity.
1793 */
1794 symfs = realpath(symbol_conf.symfs, NULL);
1795 if (symfs == NULL)
1796 symfs = symbol_conf.symfs;
1797 if (strcmp(symfs, "/") == 0)
1798 symbol_conf.symfs = "";
1799 if (symfs != symbol_conf.symfs)
1800 free((void *)symfs);
1801
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03001802 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
1803
Jovi Zhang85e00b52010-09-09 13:30:59 -03001804 symbol_conf.initialized = true;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001805 return 0;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001806
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001807out_free_comm_list:
1808 strlist__delete(symbol_conf.comm_list);
Namhyung Kimd74c8962011-12-13 00:16:52 +09001809out_free_dso_list:
1810 strlist__delete(symbol_conf.dso_list);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001811 return -1;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02001812}
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001813
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03001814void symbol__exit(void)
1815{
Jovi Zhang85e00b52010-09-09 13:30:59 -03001816 if (!symbol_conf.initialized)
1817 return;
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03001818 strlist__delete(symbol_conf.sym_list);
1819 strlist__delete(symbol_conf.dso_list);
1820 strlist__delete(symbol_conf.comm_list);
1821 vmlinux_path__exit();
1822 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
Jovi Zhang85e00b52010-09-09 13:30:59 -03001823 symbol_conf.initialized = false;
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03001824}