blob: b349e8eda0e2ed712d67813924dea6271bfab308 [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>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -03006#include <linux/kernel.h>
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03007#include <sys/types.h>
8#include <sys/stat.h>
9#include <sys/param.h>
10#include <fcntl.h>
11#include <unistd.h>
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -020012#include <inttypes.h>
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -030013#include "annotate.h"
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -030014#include "build-id.h"
Namhyung Kime334c722012-02-10 10:10:17 +090015#include "util.h"
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030016#include "debug.h"
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030017#include "machine.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030018#include "symbol.h"
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -030019#include "strlist.h"
David Aherne03eaa42015-03-24 09:52:41 -060020#include "intlist.h"
Namhyung Kim0a7e6d12014-08-12 15:40:45 +090021#include "header.h"
Arnaldo Carvalho de Melo9a3993d2017-04-18 11:33:48 -030022#include "path.h"
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030023#include "sane_ctype.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030024
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030025#include <elf.h>
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -020026#include <limits.h>
Arnaldo Carvalho de Meloc506c962013-12-11 09:15:00 -030027#include <symbol/kallsyms.h>
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030028#include <sys/utsname.h>
Peter Zijlstra2cdbc462009-08-05 14:05:16 +020029
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -030030static int dso__load_kernel_sym(struct dso *dso, struct map *map);
31static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map);
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -030032static bool symbol__is_idle(const char *name);
33
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030034int vmlinux_path__nr_entries;
35char **vmlinux_path;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030036
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -020037struct symbol_conf symbol_conf = {
Namhyung Kime511db52013-12-24 16:19:25 +090038 .use_modules = true,
39 .try_vmlinux_path = true,
40 .annotate_src = true,
41 .demangle = true,
Avi Kivity763122a2014-09-13 07:15:05 +030042 .demangle_kernel = false,
Namhyung Kime511db52013-12-24 16:19:25 +090043 .cumulate_callchain = true,
Jiri Olsac8302362014-06-27 18:26:58 +020044 .show_hist_headers = true,
Namhyung Kime511db52013-12-24 16:19:25 +090045 .symfs = "",
Namhyung Kim1e9abf82015-11-29 23:24:17 +090046 .event_group = true,
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020047};
48
Jiri Olsa44f24cb2012-07-22 14:14:32 +020049static enum dso_binary_type binary_type_symtab[] = {
50 DSO_BINARY_TYPE__KALLSYMS,
51 DSO_BINARY_TYPE__GUEST_KALLSYMS,
52 DSO_BINARY_TYPE__JAVA_JIT,
53 DSO_BINARY_TYPE__DEBUGLINK,
54 DSO_BINARY_TYPE__BUILD_ID_CACHE,
55 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
56 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
57 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
58 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
59 DSO_BINARY_TYPE__GUEST_KMODULE,
Namhyung Kimc00c48f2014-11-04 10:14:27 +090060 DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
Jiri Olsa44f24cb2012-07-22 14:14:32 +020061 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
Namhyung Kimc00c48f2014-11-04 10:14:27 +090062 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020063 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
Jiri Olsa44f24cb2012-07-22 14:14:32 +020064 DSO_BINARY_TYPE__NOT_FOUND,
65};
66
Jiri Olsa028df762012-08-01 14:47:57 +020067#define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
Jiri Olsa44f24cb2012-07-22 14:14:32 +020068
Arnaldo Carvalho de Melo36a3e642010-01-04 16:19:27 -020069bool symbol_type__is_a(char symbol_type, enum map_type map_type)
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020070{
Anton Blanchard31877902011-08-24 16:40:16 +100071 symbol_type = toupper(symbol_type);
72
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020073 switch (map_type) {
74 case MAP__FUNCTION:
75 return symbol_type == 'T' || symbol_type == 'W';
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -020076 case MAP__VARIABLE:
Anton Blanchard31877902011-08-24 16:40:16 +100077 return symbol_type == 'D';
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020078 default:
79 return false;
80 }
81}
82
Anton Blanchard694bf402011-08-24 16:40:17 +100083static int prefix_underscores_count(const char *str)
84{
85 const char *tail = str;
86
87 while (*tail == '_')
88 tail++;
89
90 return tail - str;
91}
92
Naveen N. Raofb6d5942015-04-28 17:35:36 +053093int __weak arch__choose_best_symbol(struct symbol *syma,
94 struct symbol *symb __maybe_unused)
95{
96 /* Avoid "SyS" kernel syscall aliases */
97 if (strlen(syma->name) >= 3 && !strncmp(syma->name, "SyS", 3))
98 return SYMBOL_B;
99 if (strlen(syma->name) >= 10 && !strncmp(syma->name, "compat_SyS", 10))
100 return SYMBOL_B;
101
102 return SYMBOL_A;
103}
Anton Blanchard694bf402011-08-24 16:40:17 +1000104
105static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
106{
107 s64 a;
108 s64 b;
Adrian Hunter34454322013-08-07 14:38:49 +0300109 size_t na, nb;
Anton Blanchard694bf402011-08-24 16:40:17 +1000110
111 /* Prefer a symbol with non zero length */
112 a = syma->end - syma->start;
113 b = symb->end - symb->start;
114 if ((b == 0) && (a > 0))
115 return SYMBOL_A;
116 else if ((a == 0) && (b > 0))
117 return SYMBOL_B;
118
119 /* Prefer a non weak symbol over a weak one */
120 a = syma->binding == STB_WEAK;
121 b = symb->binding == STB_WEAK;
122 if (b && !a)
123 return SYMBOL_A;
124 if (a && !b)
125 return SYMBOL_B;
126
127 /* Prefer a global symbol over a non global one */
128 a = syma->binding == STB_GLOBAL;
129 b = symb->binding == STB_GLOBAL;
130 if (a && !b)
131 return SYMBOL_A;
132 if (b && !a)
133 return SYMBOL_B;
134
135 /* Prefer a symbol with less underscores */
136 a = prefix_underscores_count(syma->name);
137 b = prefix_underscores_count(symb->name);
138 if (b > a)
139 return SYMBOL_A;
140 else if (a > b)
141 return SYMBOL_B;
142
Adrian Hunter34454322013-08-07 14:38:49 +0300143 /* Choose the symbol with the longest name */
144 na = strlen(syma->name);
145 nb = strlen(symb->name);
146 if (na > nb)
Anton Blanchard694bf402011-08-24 16:40:17 +1000147 return SYMBOL_A;
Adrian Hunter34454322013-08-07 14:38:49 +0300148 else if (na < nb)
Anton Blanchard694bf402011-08-24 16:40:17 +1000149 return SYMBOL_B;
Adrian Hunter34454322013-08-07 14:38:49 +0300150
Naveen N. Raofb6d5942015-04-28 17:35:36 +0530151 return arch__choose_best_symbol(syma, symb);
Anton Blanchard694bf402011-08-24 16:40:17 +1000152}
153
Namhyung Kime5a18452012-08-06 13:41:20 +0900154void symbols__fixup_duplicate(struct rb_root *symbols)
Anton Blanchard694bf402011-08-24 16:40:17 +1000155{
156 struct rb_node *nd;
157 struct symbol *curr, *next;
158
Arnaldo Carvalho de Meloc97b40e2016-09-01 10:56:06 -0300159 if (symbol_conf.allow_aliases)
160 return;
161
Anton Blanchard694bf402011-08-24 16:40:17 +1000162 nd = rb_first(symbols);
163
164 while (nd) {
165 curr = rb_entry(nd, struct symbol, rb_node);
166again:
167 nd = rb_next(&curr->rb_node);
168 next = rb_entry(nd, struct symbol, rb_node);
169
170 if (!nd)
171 break;
172
173 if (curr->start != next->start)
174 continue;
175
176 if (choose_best_symbol(curr, next) == SYMBOL_A) {
177 rb_erase(&next->rb_node, symbols);
Chenggang Qind4f74eb2013-10-11 08:27:59 +0800178 symbol__delete(next);
Anton Blanchard694bf402011-08-24 16:40:17 +1000179 goto again;
180 } else {
181 nd = rb_next(&curr->rb_node);
182 rb_erase(&curr->rb_node, symbols);
Chenggang Qind4f74eb2013-10-11 08:27:59 +0800183 symbol__delete(curr);
Anton Blanchard694bf402011-08-24 16:40:17 +1000184 }
185 }
186}
187
Namhyung Kime5a18452012-08-06 13:41:20 +0900188void symbols__fixup_end(struct rb_root *symbols)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300189{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300190 struct rb_node *nd, *prevnd = rb_first(symbols);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300191 struct symbol *curr, *prev;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300192
193 if (prevnd == NULL)
194 return;
195
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300196 curr = rb_entry(prevnd, struct symbol, rb_node);
197
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300198 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300199 prev = curr;
200 curr = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300201
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200202 if (prev->end == prev->start && prev->end != curr->start)
Arnaldo Carvalho de Melo2c241bd2014-10-14 17:19:44 -0300203 prev->end = curr->start;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300204 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300205
206 /* Last entry */
207 if (curr->end == curr->start)
Daniel Borkmanne7ede722017-03-15 22:53:37 +0100208 curr->end = roundup(curr->start, 4096) + 4096;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300209}
210
Namhyung Kime5a18452012-08-06 13:41:20 +0900211void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300212{
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300213 struct maps *maps = &mg->maps[type];
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300214 struct map *next, *curr;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300215
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300216 pthread_rwlock_wrlock(&maps->lock);
217
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300218 curr = maps__first(maps);
219 if (curr == NULL)
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300220 goto out_unlock;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300221
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300222 for (next = map__next(curr); next; next = map__next(curr)) {
223 curr->end = next->start;
224 curr = next;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300225 }
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -0200226
227 /*
228 * We still haven't the actual symbols, so guess the
229 * last map final address.
230 */
Ian Munsie9d1faba2010-11-25 15:12:53 +1100231 curr->end = ~0ULL;
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300232
233out_unlock:
234 pthread_rwlock_unlock(&maps->lock);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300235}
236
Namhyung Kime5a18452012-08-06 13:41:20 +0900237struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300238{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300239 size_t namelen = strlen(name) + 1;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300240 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
241 sizeof(*sym) + namelen));
242 if (sym == NULL)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200243 return NULL;
244
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -0300245 if (symbol_conf.priv_size) {
246 if (symbol_conf.init_annotation) {
247 struct annotation *notes = (void *)sym;
248 pthread_mutex_init(&notes->lock, NULL);
249 }
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300250 sym = ((void *)sym) + symbol_conf.priv_size;
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -0300251 }
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200252
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300253 sym->start = start;
Arnaldo Carvalho de Melo2c241bd2014-10-14 17:19:44 -0300254 sym->end = len ? start + len : start;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300255 sym->binding = binding;
256 sym->namelen = namelen - 1;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200257
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300258 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
259 __func__, name, start, sym->end);
260 memcpy(sym->name, name, namelen);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200261
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300262 return sym;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300263}
264
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300265void symbol__delete(struct symbol *sym)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300266{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300267 free(((void *)sym) - symbol_conf.priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300268}
269
Jiri Olsacdd059d2012-10-27 23:18:32 +0200270void symbols__delete(struct rb_root *symbols)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300271{
272 struct symbol *pos;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300273 struct rb_node *next = rb_first(symbols);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300274
275 while (next) {
276 pos = rb_entry(next, struct symbol, rb_node);
277 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300278 rb_erase(&pos->rb_node, symbols);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200279 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300280 }
281}
282
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300283void __symbols__insert(struct rb_root *symbols, struct symbol *sym, bool kernel)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300284{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300285 struct rb_node **p = &symbols->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300286 struct rb_node *parent = NULL;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000287 const u64 ip = sym->start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300288 struct symbol *s;
289
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300290 if (kernel) {
291 const char *name = sym->name;
292 /*
293 * ppc64 uses function descriptors and appends a '.' to the
294 * start of every instruction address. Remove it.
295 */
296 if (name[0] == '.')
297 name++;
298 sym->idle = symbol__is_idle(name);
299 }
300
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300301 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 Melo608c34d2016-09-01 17:54:31 -0300313void symbols__insert(struct rb_root *symbols, struct symbol *sym)
314{
315 __symbols__insert(symbols, sym, false);
316}
317
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300318static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300319{
320 struct rb_node *n;
321
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300322 if (symbols == NULL)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300323 return NULL;
324
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300325 n = symbols->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300326
327 while (n) {
328 struct symbol *s = rb_entry(n, struct symbol, rb_node);
329
330 if (ip < s->start)
331 n = n->rb_left;
Chris Phlipot9c7b37c2016-05-07 02:16:59 -0700332 else if (ip > s->end || (ip == s->end && ip != s->start))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300333 n = n->rb_right;
334 else
335 return s;
336 }
337
338 return NULL;
339}
340
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300341static struct symbol *symbols__first(struct rb_root *symbols)
342{
343 struct rb_node *n = rb_first(symbols);
344
345 if (n)
346 return rb_entry(n, struct symbol, rb_node);
347
348 return NULL;
349}
350
Adrian Huntercd67f992016-09-23 17:38:38 +0300351static struct symbol *symbols__last(struct rb_root *symbols)
352{
353 struct rb_node *n = rb_last(symbols);
354
355 if (n)
356 return rb_entry(n, struct symbol, rb_node);
357
358 return NULL;
359}
360
Adrian Hunter9c00a812014-07-14 13:02:50 +0300361static struct symbol *symbols__next(struct symbol *sym)
362{
363 struct rb_node *n = rb_next(&sym->rb_node);
364
365 if (n)
366 return rb_entry(n, struct symbol, rb_node);
367
368 return NULL;
369}
370
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300371static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200372{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300373 struct rb_node **p = &symbols->rb_node;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200374 struct rb_node *parent = NULL;
Rabin Vincent02a9d032010-11-23 22:08:18 +0530375 struct symbol_name_rb_node *symn, *s;
376
377 symn = container_of(sym, struct symbol_name_rb_node, sym);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200378
379 while (*p != NULL) {
380 parent = *p;
381 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
382 if (strcmp(sym->name, s->sym.name) < 0)
383 p = &(*p)->rb_left;
384 else
385 p = &(*p)->rb_right;
386 }
387 rb_link_node(&symn->rb_node, parent, p);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300388 rb_insert_color(&symn->rb_node, symbols);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200389}
390
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300391static void symbols__sort_by_name(struct rb_root *symbols,
392 struct rb_root *source)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200393{
394 struct rb_node *nd;
395
396 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
397 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300398 symbols__insert_by_name(symbols, pos);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200399 }
400}
401
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300402static struct symbol *symbols__find_by_name(struct rb_root *symbols,
403 const char *name)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200404{
405 struct rb_node *n;
Martin Liška5bcaaca2015-05-26 11:41:37 -0300406 struct symbol_name_rb_node *s = NULL;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200407
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300408 if (symbols == NULL)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200409 return NULL;
410
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300411 n = symbols->rb_node;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200412
413 while (n) {
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200414 int cmp;
415
416 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
Naveen N. Rao031b84c2015-04-28 17:35:37 +0530417 cmp = arch__compare_symbol_names(name, s->sym.name);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200418
419 if (cmp < 0)
420 n = n->rb_left;
421 else if (cmp > 0)
422 n = n->rb_right;
423 else
Namhyung Kimde480992015-01-16 15:31:28 -0300424 break;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200425 }
426
Namhyung Kimde480992015-01-16 15:31:28 -0300427 if (n == NULL)
428 return NULL;
429
430 /* return first symbol that has same name (if any) */
431 for (n = rb_prev(n); n; n = rb_prev(n)) {
432 struct symbol_name_rb_node *tmp;
433
434 tmp = rb_entry(n, struct symbol_name_rb_node, rb_node);
Naveen N. Rao031b84c2015-04-28 17:35:37 +0530435 if (arch__compare_symbol_names(tmp->sym.name, s->sym.name))
Namhyung Kimde480992015-01-16 15:31:28 -0300436 break;
437
438 s = tmp;
439 }
440
441 return &s->sym;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200442}
443
Arnaldo Carvalho de Meloc0b4dff2015-08-24 13:33:14 -0300444void dso__reset_find_symbol_cache(struct dso *dso)
445{
446 enum map_type type;
447
448 for (type = MAP__FUNCTION; type <= MAP__VARIABLE; ++type) {
449 dso->last_find_result[type].addr = 0;
450 dso->last_find_result[type].symbol = NULL;
451 }
452}
453
Chris Phlipotae93a6c2016-05-10 20:26:46 -0700454void dso__insert_symbol(struct dso *dso, enum map_type type, struct symbol *sym)
455{
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300456 __symbols__insert(&dso->symbols[type], sym, dso->kernel);
Chris Phlipotae93a6c2016-05-10 20:26:46 -0700457
458 /* update the symbol cache if necessary */
459 if (dso->last_find_result[type].addr >= sym->start &&
460 (dso->last_find_result[type].addr < sym->end ||
461 sym->start == sym->end)) {
462 dso->last_find_result[type].symbol = sym;
463 }
464}
465
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300466struct symbol *dso__find_symbol(struct dso *dso,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200467 enum map_type type, u64 addr)
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200468{
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300469 if (dso->last_find_result[type].addr != addr || dso->last_find_result[type].symbol == NULL) {
Arnaldo Carvalho de Melob685ac22015-07-22 12:52:17 -0300470 dso->last_find_result[type].addr = addr;
471 dso->last_find_result[type].symbol = symbols__find(&dso->symbols[type], addr);
472 }
473
474 return dso->last_find_result[type].symbol;
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200475}
476
Adrian Hunter9c00a812014-07-14 13:02:50 +0300477struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300478{
479 return symbols__first(&dso->symbols[type]);
480}
481
Adrian Huntercd67f992016-09-23 17:38:38 +0300482struct symbol *dso__last_symbol(struct dso *dso, enum map_type type)
483{
484 return symbols__last(&dso->symbols[type]);
485}
486
Adrian Hunter9c00a812014-07-14 13:02:50 +0300487struct symbol *dso__next_symbol(struct symbol *sym)
488{
489 return symbols__next(sym);
490}
491
Arnaldo Carvalho de Melo18bd7262015-01-16 15:39:53 -0300492struct symbol *symbol__next_by_name(struct symbol *sym)
493{
494 struct symbol_name_rb_node *s = container_of(sym, struct symbol_name_rb_node, sym);
495 struct rb_node *n = rb_next(&s->rb_node);
496
497 return n ? &rb_entry(n, struct symbol_name_rb_node, rb_node)->sym : NULL;
498}
499
500 /*
501 * Teturns first symbol that matched with @name.
502 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300503struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200504 const char *name)
505{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300506 return symbols__find_by_name(&dso->symbol_names[type], name);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200507}
508
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300509void dso__sort_by_name(struct dso *dso, enum map_type type)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200510{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300511 dso__set_sorted_by_name(dso, type);
512 return symbols__sort_by_name(&dso->symbol_names[type],
513 &dso->symbols[type]);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200514}
515
Adrian Hunter316d70d2013-10-08 11:45:48 +0300516int modules__parse(const char *filename, void *arg,
517 int (*process_module)(void *arg, const char *name,
518 u64 start))
519{
520 char *line = NULL;
521 size_t n;
522 FILE *file;
523 int err = 0;
524
525 file = fopen(filename, "r");
526 if (file == NULL)
527 return -1;
528
529 while (1) {
530 char name[PATH_MAX];
531 u64 start;
532 char *sep;
533 ssize_t line_len;
534
535 line_len = getline(&line, &n, file);
536 if (line_len < 0) {
537 if (feof(file))
538 break;
539 err = -1;
540 goto out;
541 }
542
543 if (!line) {
544 err = -1;
545 goto out;
546 }
547
548 line[--line_len] = '\0'; /* \n */
549
550 sep = strrchr(line, 'x');
551 if (sep == NULL)
552 continue;
553
554 hex2u64(sep + 1, &start);
555
556 sep = strchr(line, ' ');
557 if (sep == NULL)
558 continue;
559
560 *sep = '\0';
561
562 scnprintf(name, sizeof(name), "[%s]", line);
563
564 err = process_module(arg, name, start);
565 if (err)
566 break;
567 }
568out:
569 free(line);
570 fclose(file);
571 return err;
572}
573
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200574struct process_kallsyms_args {
575 struct map *map;
576 struct dso *dso;
577};
578
Arnaldo Carvalho de Meloe7110b92014-08-08 18:00:39 -0300579/*
580 * These are symbols in the kernel image, so make sure that
581 * sym is from a kernel DSO.
582 */
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300583static bool symbol__is_idle(const char *name)
David Ahern82d1deb2013-11-18 13:32:45 -0700584{
585 const char * const idle_symbols[] = {
586 "cpu_idle",
Arnaldo Carvalho de Meloe0336ed2014-08-08 18:02:41 -0300587 "cpu_startup_entry",
David Ahern82d1deb2013-11-18 13:32:45 -0700588 "intel_idle",
589 "default_idle",
590 "native_safe_halt",
591 "enter_idle",
592 "exit_idle",
593 "mwait_idle",
594 "mwait_idle_with_hints",
595 "poll_idle",
596 "ppc64_runlatch_off",
597 "pseries_dedicated_idle_sleep",
598 NULL
599 };
David Ahern82d1deb2013-11-18 13:32:45 -0700600 int i;
601
David Ahern82d1deb2013-11-18 13:32:45 -0700602 for (i = 0; idle_symbols[i]; i++) {
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300603 if (!strcmp(idle_symbols[i], name))
David Ahern82d1deb2013-11-18 13:32:45 -0700604 return true;
605 }
606
607 return false;
608}
609
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200610static int map__process_kallsym_symbol(void *arg, const char *name,
Cody P Schafer82151522012-08-10 15:22:48 -0700611 char type, u64 start)
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200612{
613 struct symbol *sym;
614 struct process_kallsyms_args *a = arg;
615 struct rb_root *root = &a->dso->symbols[a->map->type];
616
617 if (!symbol_type__is_a(type, a->map->type))
618 return 0;
619
Cody P Schafer82151522012-08-10 15:22:48 -0700620 /*
621 * module symbols are not sorted so we add all
622 * symbols, setting length to 0, and rely on
623 * symbols__fixup_end() to fix it up.
624 */
Arnaldo Carvalho de Melo8e947f12015-09-30 12:42:07 -0300625 sym = symbol__new(start, 0, kallsyms2elf_binding(type), name);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200626 if (sym == NULL)
627 return -ENOMEM;
628 /*
629 * We will pass the symbols to the filter later, in
630 * map__split_kallsyms, when we have split the maps per module
631 */
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300632 __symbols__insert(root, sym, !strchr(name, '['));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800633
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200634 return 0;
635}
636
637/*
638 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
639 * so that we can in the next step set the symbol ->end address and then
640 * call kernel_maps__split_kallsyms.
641 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300642static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200643 struct map *map)
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200644{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300645 struct process_kallsyms_args args = { .map = map, .dso = dso, };
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200646 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200647}
648
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300649static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map)
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300650{
Wang Nanba927322015-04-07 08:22:45 +0000651 struct map_groups *kmaps = map__kmaps(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300652 struct map *curr_map;
653 struct symbol *pos;
Adrian Hunter866548d2015-11-06 15:59:29 +0200654 int count = 0;
655 struct rb_root old_root = dso->symbols[map->type];
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300656 struct rb_root *root = &dso->symbols[map->type];
657 struct rb_node *next = rb_first(root);
658
Wang Nanba927322015-04-07 08:22:45 +0000659 if (!kmaps)
660 return -1;
661
Adrian Hunter866548d2015-11-06 15:59:29 +0200662 *root = RB_ROOT;
663
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300664 while (next) {
665 char *module;
666
667 pos = rb_entry(next, struct symbol, rb_node);
668 next = rb_next(&pos->rb_node);
669
Adrian Hunter866548d2015-11-06 15:59:29 +0200670 rb_erase_init(&pos->rb_node, &old_root);
671
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300672 module = strchr(pos->name, '\t');
673 if (module)
674 *module = '\0';
675
676 curr_map = map_groups__find(kmaps, map->type, pos->start);
677
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300678 if (!curr_map) {
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300679 symbol__delete(pos);
Adrian Hunter866548d2015-11-06 15:59:29 +0200680 continue;
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300681 }
Adrian Hunter866548d2015-11-06 15:59:29 +0200682
683 pos->start -= curr_map->start - curr_map->pgoff;
684 if (pos->end)
685 pos->end -= curr_map->start - curr_map->pgoff;
686 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
687 ++count;
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300688 }
689
690 /* Symbols have been adjusted */
691 dso->adjust_symbols = 1;
692
Adrian Hunter866548d2015-11-06 15:59:29 +0200693 return count;
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300694}
695
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300696/*
697 * Split the symbols into maps, making sure there are no overlaps, i.e. the
698 * kernel range is broken in several maps, named [kernel].N, as we don't have
699 * the original ELF section names vmlinux have.
700 */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300701static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300702{
Wang Nanba927322015-04-07 08:22:45 +0000703 struct map_groups *kmaps = map__kmaps(map);
704 struct machine *machine;
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200705 struct map *curr_map = map;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300706 struct symbol *pos;
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300707 int count = 0, moved = 0;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300708 struct rb_root *root = &dso->symbols[map->type];
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200709 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300710 int kernel_range = 0;
711
Wang Nanba927322015-04-07 08:22:45 +0000712 if (!kmaps)
713 return -1;
714
715 machine = kmaps->machine;
716
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300717 while (next) {
718 char *module;
719
720 pos = rb_entry(next, struct symbol, rb_node);
721 next = rb_next(&pos->rb_node);
722
723 module = strchr(pos->name, '\t');
724 if (module) {
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200725 if (!symbol_conf.use_modules)
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200726 goto discard_symbol;
727
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300728 *module++ = '\0';
729
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200730 if (strcmp(curr_map->dso->short_name, module)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800731 if (curr_map != map &&
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300732 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300733 machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800734 /*
735 * We assume all symbols of a module are
736 * continuous in * kallsyms, so curr_map
737 * points to a module and all its
738 * symbols are in its kmap. Mark it as
739 * loaded.
740 */
741 dso__set_loaded(curr_map->dso,
742 curr_map->type);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300743 }
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200744
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800745 curr_map = map_groups__find_by_name(kmaps,
746 map->type, module);
747 if (curr_map == NULL) {
Arnaldo Carvalho de Melo2f519032010-05-17 17:57:59 -0300748 pr_debug("%s/proc/{kallsyms,modules} "
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800749 "inconsistency while looking "
750 "for \"%s\" module!\n",
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300751 machine->root_dir, module);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800752 curr_map = map;
753 goto discard_symbol;
754 }
755
756 if (curr_map->dso->loaded &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300757 !machine__is_default_guest(machine))
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200758 goto discard_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300759 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300760 /*
761 * So that we look just like we get from .ko files,
762 * i.e. not prelinked, relative to map->start.
763 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200764 pos->start = curr_map->map_ip(curr_map, pos->start);
765 pos->end = curr_map->map_ip(curr_map, pos->end);
766 } else if (curr_map != map) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300767 char dso_name[PATH_MAX];
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300768 struct dso *ndso;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300769
Adrian Hunterd9b62ab2014-01-29 16:14:43 +0200770 if (delta) {
771 /* Kernel was relocated at boot time */
772 pos->start -= delta;
773 pos->end -= delta;
774 }
775
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200776 if (count == 0) {
777 curr_map = map;
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300778 goto add_symbol;
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200779 }
780
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300781 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800782 snprintf(dso_name, sizeof(dso_name),
783 "[guest.kernel].%d",
784 kernel_range++);
785 else
786 snprintf(dso_name, sizeof(dso_name),
787 "[kernel].%d",
788 kernel_range++);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300789
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300790 ndso = dso__new(dso_name);
791 if (ndso == NULL)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300792 return -1;
793
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300794 ndso->kernel = dso->kernel;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800795
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300796 curr_map = map__new2(pos->start, ndso, map->type);
Zhang, Yanmin37fe5fc2010-02-25 11:00:51 +0800797 if (curr_map == NULL) {
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300798 dso__put(ndso);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300799 return -1;
800 }
801
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200802 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200803 map_groups__insert(kmaps, curr_map);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300804 ++kernel_range;
Adrian Hunterd9b62ab2014-01-29 16:14:43 +0200805 } else if (delta) {
806 /* Kernel was relocated at boot time */
807 pos->start -= delta;
808 pos->end -= delta;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300809 }
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300810add_symbol:
811 if (curr_map != map) {
812 rb_erase(&pos->rb_node, root);
813 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
814 ++moved;
815 } else
816 ++count;
817
818 continue;
819discard_symbol:
820 rb_erase(&pos->rb_node, root);
821 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300822 }
823
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800824 if (curr_map != map &&
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300825 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300826 machine__is_default_guest(kmaps->machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800827 dso__set_loaded(curr_map->dso, curr_map->type);
828 }
829
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200830 return count + moved;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300831}
832
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300833bool symbol__restricted_filename(const char *filename,
834 const char *restricted_filename)
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -0300835{
836 bool restricted = false;
837
838 if (symbol_conf.kptr_restrict) {
839 char *r = realpath(filename, NULL);
840
841 if (r != NULL) {
842 restricted = strcmp(r, restricted_filename) == 0;
843 free(r);
844 return restricted;
845 }
846 }
847
848 return restricted;
849}
850
Adrian Hunter52afdaf2013-10-09 15:01:11 +0300851struct module_info {
852 struct rb_node rb_node;
853 char *name;
854 u64 start;
855};
856
857static void add_module(struct module_info *mi, struct rb_root *modules)
858{
859 struct rb_node **p = &modules->rb_node;
860 struct rb_node *parent = NULL;
861 struct module_info *m;
862
863 while (*p != NULL) {
864 parent = *p;
865 m = rb_entry(parent, struct module_info, rb_node);
866 if (strcmp(mi->name, m->name) < 0)
867 p = &(*p)->rb_left;
868 else
869 p = &(*p)->rb_right;
870 }
871 rb_link_node(&mi->rb_node, parent, p);
872 rb_insert_color(&mi->rb_node, modules);
873}
874
875static void delete_modules(struct rb_root *modules)
876{
877 struct module_info *mi;
878 struct rb_node *next = rb_first(modules);
879
880 while (next) {
881 mi = rb_entry(next, struct module_info, rb_node);
882 next = rb_next(&mi->rb_node);
883 rb_erase(&mi->rb_node, modules);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300884 zfree(&mi->name);
Adrian Hunter52afdaf2013-10-09 15:01:11 +0300885 free(mi);
886 }
887}
888
889static struct module_info *find_module(const char *name,
890 struct rb_root *modules)
891{
892 struct rb_node *n = modules->rb_node;
893
894 while (n) {
895 struct module_info *m;
896 int cmp;
897
898 m = rb_entry(n, struct module_info, rb_node);
899 cmp = strcmp(name, m->name);
900 if (cmp < 0)
901 n = n->rb_left;
902 else if (cmp > 0)
903 n = n->rb_right;
904 else
905 return m;
906 }
907
908 return NULL;
909}
910
911static int __read_proc_modules(void *arg, const char *name, u64 start)
912{
913 struct rb_root *modules = arg;
914 struct module_info *mi;
915
916 mi = zalloc(sizeof(struct module_info));
917 if (!mi)
918 return -ENOMEM;
919
920 mi->name = strdup(name);
921 mi->start = start;
922
923 if (!mi->name) {
924 free(mi);
925 return -ENOMEM;
926 }
927
928 add_module(mi, modules);
929
930 return 0;
931}
932
933static int read_proc_modules(const char *filename, struct rb_root *modules)
934{
935 if (symbol__restricted_filename(filename, "/proc/modules"))
936 return -1;
937
938 if (modules__parse(filename, modules, __read_proc_modules)) {
939 delete_modules(modules);
940 return -1;
941 }
942
943 return 0;
944}
945
Adrian Hunterfc1b6912013-10-14 16:57:29 +0300946int compare_proc_modules(const char *from, const char *to)
947{
948 struct rb_root from_modules = RB_ROOT;
949 struct rb_root to_modules = RB_ROOT;
950 struct rb_node *from_node, *to_node;
951 struct module_info *from_m, *to_m;
952 int ret = -1;
953
954 if (read_proc_modules(from, &from_modules))
955 return -1;
956
957 if (read_proc_modules(to, &to_modules))
958 goto out_delete_from;
959
960 from_node = rb_first(&from_modules);
961 to_node = rb_first(&to_modules);
962 while (from_node) {
963 if (!to_node)
964 break;
965
966 from_m = rb_entry(from_node, struct module_info, rb_node);
967 to_m = rb_entry(to_node, struct module_info, rb_node);
968
969 if (from_m->start != to_m->start ||
970 strcmp(from_m->name, to_m->name))
971 break;
972
973 from_node = rb_next(from_node);
974 to_node = rb_next(to_node);
975 }
976
977 if (!from_node && !to_node)
978 ret = 0;
979
980 delete_modules(&to_modules);
981out_delete_from:
982 delete_modules(&from_modules);
983
984 return ret;
985}
986
Adrian Hunter52afdaf2013-10-09 15:01:11 +0300987static int do_validate_kcore_modules(const char *filename, struct map *map,
988 struct map_groups *kmaps)
989{
990 struct rb_root modules = RB_ROOT;
991 struct map *old_map;
992 int err;
993
994 err = read_proc_modules(filename, &modules);
995 if (err)
996 return err;
997
998 old_map = map_groups__first(kmaps, map->type);
999 while (old_map) {
1000 struct map *next = map_groups__next(old_map);
1001 struct module_info *mi;
1002
1003 if (old_map == map || old_map->start == map->start) {
1004 /* The kernel map */
1005 old_map = next;
1006 continue;
1007 }
1008
1009 /* Module must be in memory at the same address */
1010 mi = find_module(old_map->dso->short_name, &modules);
1011 if (!mi || mi->start != old_map->start) {
1012 err = -EINVAL;
1013 goto out;
1014 }
1015
1016 old_map = next;
1017 }
1018out:
1019 delete_modules(&modules);
1020 return err;
1021}
1022
1023/*
1024 * If kallsyms is referenced by name then we look for filename in the same
1025 * directory.
1026 */
1027static bool filename_from_kallsyms_filename(char *filename,
1028 const char *base_name,
1029 const char *kallsyms_filename)
1030{
1031 char *name;
1032
1033 strcpy(filename, kallsyms_filename);
1034 name = strrchr(filename, '/');
1035 if (!name)
1036 return false;
1037
1038 name += 1;
1039
1040 if (!strcmp(name, "kallsyms")) {
1041 strcpy(name, base_name);
1042 return true;
1043 }
1044
1045 return false;
1046}
1047
1048static int validate_kcore_modules(const char *kallsyms_filename,
1049 struct map *map)
1050{
Wang Nanba927322015-04-07 08:22:45 +00001051 struct map_groups *kmaps = map__kmaps(map);
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001052 char modules_filename[PATH_MAX];
1053
Wang Nanba927322015-04-07 08:22:45 +00001054 if (!kmaps)
1055 return -EINVAL;
1056
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001057 if (!filename_from_kallsyms_filename(modules_filename, "modules",
1058 kallsyms_filename))
1059 return -EINVAL;
1060
1061 if (do_validate_kcore_modules(modules_filename, map, kmaps))
1062 return -EINVAL;
1063
1064 return 0;
1065}
1066
Adrian Huntera00d28c2014-01-29 16:14:41 +02001067static int validate_kcore_addresses(const char *kallsyms_filename,
1068 struct map *map)
1069{
1070 struct kmap *kmap = map__kmap(map);
1071
Wang Nanba927322015-04-07 08:22:45 +00001072 if (!kmap)
1073 return -EINVAL;
1074
Adrian Huntera00d28c2014-01-29 16:14:41 +02001075 if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
1076 u64 start;
1077
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001078 if (kallsyms__get_function_start(kallsyms_filename,
1079 kmap->ref_reloc_sym->name, &start))
1080 return -ENOENT;
Adrian Huntera00d28c2014-01-29 16:14:41 +02001081 if (start != kmap->ref_reloc_sym->addr)
1082 return -EINVAL;
1083 }
1084
1085 return validate_kcore_modules(kallsyms_filename, map);
1086}
1087
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001088struct kcore_mapfn_data {
1089 struct dso *dso;
1090 enum map_type type;
1091 struct list_head maps;
1092};
1093
1094static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1095{
1096 struct kcore_mapfn_data *md = data;
1097 struct map *map;
1098
1099 map = map__new2(start, md->dso, md->type);
1100 if (map == NULL)
1101 return -ENOMEM;
1102
1103 map->end = map->start + len;
1104 map->pgoff = pgoff;
1105
1106 list_add(&map->node, &md->maps);
1107
1108 return 0;
1109}
1110
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001111static int dso__load_kcore(struct dso *dso, struct map *map,
1112 const char *kallsyms_filename)
1113{
Wang Nanba927322015-04-07 08:22:45 +00001114 struct map_groups *kmaps = map__kmaps(map);
1115 struct machine *machine;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001116 struct kcore_mapfn_data md;
1117 struct map *old_map, *new_map, *replacement_map = NULL;
1118 bool is_64_bit;
1119 int err, fd;
1120 char kcore_filename[PATH_MAX];
1121 struct symbol *sym;
1122
Wang Nanba927322015-04-07 08:22:45 +00001123 if (!kmaps)
1124 return -EINVAL;
1125
1126 machine = kmaps->machine;
1127
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001128 /* This function requires that the map is the kernel map */
1129 if (map != machine->vmlinux_maps[map->type])
1130 return -EINVAL;
1131
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001132 if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1133 kallsyms_filename))
1134 return -EINVAL;
1135
Adrian Huntera00d28c2014-01-29 16:14:41 +02001136 /* Modules and kernel must be present at their original addresses */
1137 if (validate_kcore_addresses(kallsyms_filename, map))
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001138 return -EINVAL;
1139
1140 md.dso = dso;
1141 md.type = map->type;
1142 INIT_LIST_HEAD(&md.maps);
1143
1144 fd = open(kcore_filename, O_RDONLY);
Li Zhang36c8bb52015-06-19 16:57:33 +08001145 if (fd < 0) {
Adrian Hunter133de942015-08-20 13:07:40 +03001146 pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n",
1147 kcore_filename);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001148 return -EINVAL;
Li Zhang36c8bb52015-06-19 16:57:33 +08001149 }
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001150
1151 /* Read new maps into temporary lists */
1152 err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
1153 &is_64_bit);
1154 if (err)
1155 goto out_err;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +03001156 dso->is_64_bit = is_64_bit;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001157
1158 if (list_empty(&md.maps)) {
1159 err = -EINVAL;
1160 goto out_err;
1161 }
1162
1163 /* Remove old maps */
1164 old_map = map_groups__first(kmaps, map->type);
1165 while (old_map) {
1166 struct map *next = map_groups__next(old_map);
1167
1168 if (old_map != map)
1169 map_groups__remove(kmaps, old_map);
1170 old_map = next;
1171 }
1172
1173 /* Find the kernel map using the first symbol */
1174 sym = dso__first_symbol(dso, map->type);
1175 list_for_each_entry(new_map, &md.maps, node) {
1176 if (sym && sym->start >= new_map->start &&
1177 sym->start < new_map->end) {
1178 replacement_map = new_map;
1179 break;
1180 }
1181 }
1182
1183 if (!replacement_map)
1184 replacement_map = list_entry(md.maps.next, struct map, node);
1185
1186 /* Add new maps */
1187 while (!list_empty(&md.maps)) {
1188 new_map = list_entry(md.maps.next, struct map, node);
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -03001189 list_del_init(&new_map->node);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001190 if (new_map == replacement_map) {
1191 map->start = new_map->start;
1192 map->end = new_map->end;
1193 map->pgoff = new_map->pgoff;
1194 map->map_ip = new_map->map_ip;
1195 map->unmap_ip = new_map->unmap_ip;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001196 /* Ensure maps are correctly ordered */
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001197 map__get(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001198 map_groups__remove(kmaps, map);
1199 map_groups__insert(kmaps, map);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001200 map__put(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001201 } else {
1202 map_groups__insert(kmaps, new_map);
1203 }
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001204
1205 map__put(new_map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001206 }
1207
1208 /*
1209 * Set the data type and long name so that kcore can be read via
1210 * dso__data_read_addr().
1211 */
1212 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001213 dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001214 else
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001215 dso->binary_type = DSO_BINARY_TYPE__KCORE;
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001216 dso__set_long_name(dso, strdup(kcore_filename), true);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001217
1218 close(fd);
1219
1220 if (map->type == MAP__FUNCTION)
1221 pr_debug("Using %s for kernel object code\n", kcore_filename);
1222 else
1223 pr_debug("Using %s for kernel data\n", kcore_filename);
1224
1225 return 0;
1226
1227out_err:
1228 while (!list_empty(&md.maps)) {
1229 map = list_entry(md.maps.next, struct map, node);
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -03001230 list_del_init(&map->node);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001231 map__put(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001232 }
1233 close(fd);
1234 return -EINVAL;
1235}
1236
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001237/*
1238 * If the kernel is relocated at boot time, kallsyms won't match. Compute the
1239 * delta based on the relocation reference symbol.
1240 */
1241static int kallsyms__delta(struct map *map, const char *filename, u64 *delta)
1242{
1243 struct kmap *kmap = map__kmap(map);
1244 u64 addr;
1245
Wang Nanba927322015-04-07 08:22:45 +00001246 if (!kmap)
1247 return -1;
1248
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001249 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1250 return 0;
1251
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001252 if (kallsyms__get_function_start(filename, kmap->ref_reloc_sym->name, &addr))
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001253 return -1;
1254
1255 *delta = addr - kmap->ref_reloc_sym->addr;
1256 return 0;
1257}
1258
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001259int __dso__load_kallsyms(struct dso *dso, const char *filename,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001260 struct map *map, bool no_kcore)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001261{
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001262 u64 delta = 0;
1263
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03001264 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1265 return -1;
1266
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001267 if (dso__load_all_kallsyms(dso, filename, map) < 0)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001268 return -1;
1269
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001270 if (kallsyms__delta(map, filename, &delta))
1271 return -1;
1272
Anton Blanchard3f5a4272011-08-24 16:40:15 +10001273 symbols__fixup_end(&dso->symbols[map->type]);
Arnaldo Carvalho de Melo432746f2016-09-01 11:00:23 -03001274 symbols__fixup_duplicate(&dso->symbols[map->type]);
Anton Blanchard3f5a4272011-08-24 16:40:15 +10001275
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001276 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001277 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001278 else
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001279 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001280
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001281 if (!no_kcore && !dso__load_kcore(dso, map, filename))
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001282 return dso__split_kallsyms_for_kcore(dso, map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001283 else
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001284 return dso__split_kallsyms(dso, map, delta);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001285}
1286
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001287int dso__load_kallsyms(struct dso *dso, const char *filename,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001288 struct map *map)
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001289{
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001290 return __dso__load_kallsyms(dso, filename, map, false);
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001291}
1292
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001293static int dso__load_perf_map(struct dso *dso, struct map *map)
Pekka Enberg80d496b2009-06-08 21:12:48 +03001294{
1295 char *line = NULL;
1296 size_t n;
1297 FILE *file;
1298 int nr_syms = 0;
1299
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001300 file = fopen(dso->long_name, "r");
Pekka Enberg80d496b2009-06-08 21:12:48 +03001301 if (file == NULL)
1302 goto out_failure;
1303
1304 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001305 u64 start, size;
Pekka Enberg80d496b2009-06-08 21:12:48 +03001306 struct symbol *sym;
1307 int line_len, len;
1308
1309 line_len = getline(&line, &n, file);
1310 if (line_len < 0)
1311 break;
1312
1313 if (!line)
1314 goto out_failure;
1315
1316 line[--line_len] = '\0'; /* \n */
1317
1318 len = hex2u64(line, &start);
1319
1320 len++;
1321 if (len + 2 >= line_len)
1322 continue;
1323
1324 len += hex2u64(line + len, &size);
1325
1326 len++;
1327 if (len + 2 >= line_len)
1328 continue;
1329
Arnaldo Carvalho de Meloc408fed2010-08-05 12:59:47 -03001330 sym = symbol__new(start, size, STB_GLOBAL, line + len);
Pekka Enberg80d496b2009-06-08 21:12:48 +03001331
1332 if (sym == NULL)
1333 goto out_delete_line;
1334
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001335 symbols__insert(&dso->symbols[map->type], sym);
1336 nr_syms++;
Pekka Enberg80d496b2009-06-08 21:12:48 +03001337 }
1338
1339 free(line);
1340 fclose(file);
1341
1342 return nr_syms;
1343
1344out_delete_line:
1345 free(line);
1346out_failure:
1347 return -1;
1348}
1349
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001350static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1351 enum dso_binary_type type)
1352{
1353 switch (type) {
1354 case DSO_BINARY_TYPE__JAVA_JIT:
1355 case DSO_BINARY_TYPE__DEBUGLINK:
1356 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1357 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1358 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1359 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1360 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1361 return !kmod && dso->kernel == DSO_TYPE_USER;
1362
1363 case DSO_BINARY_TYPE__KALLSYMS:
1364 case DSO_BINARY_TYPE__VMLINUX:
1365 case DSO_BINARY_TYPE__KCORE:
1366 return dso->kernel == DSO_TYPE_KERNEL;
1367
1368 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1369 case DSO_BINARY_TYPE__GUEST_VMLINUX:
1370 case DSO_BINARY_TYPE__GUEST_KCORE:
1371 return dso->kernel == DSO_TYPE_GUEST_KERNEL;
1372
1373 case DSO_BINARY_TYPE__GUEST_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +09001374 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001375 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +09001376 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001377 /*
1378 * kernel modules know their symtab type - it's set when
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001379 * creating a module dso in machine__findnew_module_map().
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001380 */
1381 return kmod && dso->symtab_type == type;
1382
1383 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1384 return true;
1385
1386 case DSO_BINARY_TYPE__NOT_FOUND:
1387 default:
1388 return false;
1389 }
1390}
1391
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001392int dso__load(struct dso *dso, struct map *map)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001393{
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001394 char *name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001395 int ret = -1;
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001396 u_int i;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001397 struct machine *machine;
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001398 char *root_dir = (char *) "";
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001399 int ss_pos = 0;
1400 struct symsrc ss_[2];
1401 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001402 bool kmod;
Dima Kogan5baecbc2015-09-07 17:34:19 -07001403 unsigned char build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001404
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001405 pthread_mutex_lock(&dso->lock);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001406
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001407 /* check again under the dso->lock */
1408 if (dso__loaded(dso, map->type)) {
1409 ret = 1;
1410 goto out;
1411 }
1412
1413 if (dso->kernel) {
1414 if (dso->kernel == DSO_TYPE_KERNEL)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001415 ret = dso__load_kernel_sym(dso, map);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001416 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001417 ret = dso__load_guest_kernel_sym(dso, map);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001418
1419 goto out;
1420 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001421
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001422 if (map->groups && map->groups->machine)
1423 machine = map->groups->machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001424 else
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001425 machine = NULL;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001426
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001427 dso->adjust_symbols = 0;
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001428
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001429 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
Pekka Enberg981c1252011-08-09 22:54:18 +03001430 struct stat st;
1431
Vasiliy Kulikove9b52ef2011-08-12 00:55:37 +04001432 if (lstat(dso->name, &st) < 0)
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001433 goto out;
Pekka Enberg981c1252011-08-09 22:54:18 +03001434
Arnaldo Carvalho de Melo2059fc72015-11-12 16:50:13 -03001435 if (!symbol_conf.force && st.st_uid && (st.st_uid != geteuid())) {
Pekka Enberg981c1252011-08-09 22:54:18 +03001436 pr_warning("File %s not owned by current user or root, "
Arnaldo Carvalho de Melo2059fc72015-11-12 16:50:13 -03001437 "ignoring it (use -f to override).\n", dso->name);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001438 goto out;
Pekka Enberg981c1252011-08-09 22:54:18 +03001439 }
1440
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001441 ret = dso__load_perf_map(dso, map);
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001442 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1443 DSO_BINARY_TYPE__NOT_FOUND;
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001444 goto out;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001445 }
1446
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001447 if (machine)
1448 root_dir = machine->root_dir;
1449
David Ahern164c8002013-01-14 10:46:47 -07001450 name = malloc(PATH_MAX);
1451 if (!name)
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001452 goto out;
David Ahern164c8002013-01-14 10:46:47 -07001453
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001454 kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
Namhyung Kimc00c48f2014-11-04 10:14:27 +09001455 dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
1456 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE ||
1457 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001458
Dima Kogan5baecbc2015-09-07 17:34:19 -07001459
1460 /*
1461 * Read the build id if possible. This is required for
1462 * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work
1463 */
Kan Lianged6c1662016-12-13 10:29:44 -05001464 if (!dso->has_build_id &&
Victor Kamensky9b200652017-02-06 15:48:28 -08001465 is_regular_file(dso->long_name)) {
1466 __symbol__join_symfs(name, PATH_MAX, dso->long_name);
1467 if (filename__read_build_id(name, build_id, BUILD_ID_SIZE) > 0)
Dima Kogan5baecbc2015-09-07 17:34:19 -07001468 dso__set_build_id(dso, build_id);
Victor Kamensky9b200652017-02-06 15:48:28 -08001469 }
Dima Kogan5baecbc2015-09-07 17:34:19 -07001470
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001471 /*
1472 * Iterate over candidate debug images.
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001473 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1474 * and/or opd section) for processing.
Dave Martin6da80ce2010-07-30 09:50:09 -03001475 */
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001476 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001477 struct symsrc *ss = &ss_[ss_pos];
1478 bool next_slot = false;
Dave Martin6da80ce2010-07-30 09:50:09 -03001479
Cody P Schafer005f9292012-08-10 15:22:58 -07001480 enum dso_binary_type symtab_type = binary_type_symtab[i];
Dave Martin6da80ce2010-07-30 09:50:09 -03001481
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001482 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1483 continue;
1484
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -03001485 if (dso__read_binary_type_filename(dso, symtab_type,
1486 root_dir, name, PATH_MAX))
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001487 continue;
Dave Martin6da80ce2010-07-30 09:50:09 -03001488
Jiri Olsa40356722016-01-20 12:56:32 +01001489 if (!is_regular_file(name))
1490 continue;
1491
Dave Martin6da80ce2010-07-30 09:50:09 -03001492 /* Name is now the name of the next image to try */
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001493 if (symsrc__init(ss, dso, name, symtab_type) < 0)
Dave Martin6da80ce2010-07-30 09:50:09 -03001494 continue;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001495
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001496 if (!syms_ss && symsrc__has_symtab(ss)) {
1497 syms_ss = ss;
1498 next_slot = true;
Adrian Hunter0058aef2013-12-03 09:23:08 +02001499 if (!dso->symsrc_filename)
1500 dso->symsrc_filename = strdup(name);
Cody P Schaferd26cd122012-08-10 15:23:00 -07001501 }
1502
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001503 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1504 runtime_ss = ss;
1505 next_slot = true;
Cody P Schafera44f6052012-08-10 15:22:59 -07001506 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001507
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001508 if (next_slot) {
1509 ss_pos++;
Jiri Olsa33ff5812012-04-18 15:46:58 +02001510
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001511 if (syms_ss && runtime_ss)
1512 break;
Namhyung Kim98e9f032014-02-20 10:32:54 +09001513 } else {
1514 symsrc__destroy(ss);
Dave Martin6da80ce2010-07-30 09:50:09 -03001515 }
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001516
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001517 }
Dave Martin6da80ce2010-07-30 09:50:09 -03001518
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001519 if (!runtime_ss && !syms_ss)
1520 goto out_free;
1521
1522 if (runtime_ss && !syms_ss) {
1523 syms_ss = runtime_ss;
Arnaldo Carvalho de Melo60e4b102011-03-22 15:42:14 -03001524 }
1525
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001526 /* We'll have to hope for the best */
1527 if (!runtime_ss && syms_ss)
1528 runtime_ss = syms_ss;
1529
Wang Nane7ee4042016-02-05 14:01:27 +00001530 if (syms_ss && syms_ss->type == DSO_BINARY_TYPE__BUILD_ID_CACHE)
1531 if (dso__build_id_is_kmod(dso, name, PATH_MAX))
1532 kmod = true;
1533
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001534 if (syms_ss)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001535 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, kmod);
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001536 else
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001537 ret = -1;
1538
David Ahernf47b58b2012-08-19 09:47:14 -06001539 if (ret > 0) {
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001540 int nr_plt;
1541
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001542 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map);
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001543 if (nr_plt > 0)
1544 ret += nr_plt;
1545 }
1546
1547 for (; ss_pos > 0; ss_pos--)
1548 symsrc__destroy(&ss_[ss_pos - 1]);
1549out_free:
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001550 free(name);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001551 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001552 ret = 0;
1553out:
1554 dso__set_loaded(dso, map->type);
1555 pthread_mutex_unlock(&dso->lock);
1556
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001557 return ret;
1558}
1559
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001560struct map *map_groups__find_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -02001561 enum map_type type, const char *name)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001562{
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -03001563 struct maps *maps = &mg->maps[type];
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -03001564 struct map *map;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001565
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -03001566 pthread_rwlock_rdlock(&maps->lock);
1567
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -03001568 for (map = maps__first(maps); map; map = map__next(map)) {
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001569 if (map->dso && strcmp(map->dso->short_name, name) == 0)
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -03001570 goto out_unlock;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001571 }
1572
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -03001573 map = NULL;
1574
1575out_unlock:
1576 pthread_rwlock_unlock(&maps->lock);
1577 return map;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001578}
1579
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001580int dso__load_vmlinux(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001581 const char *vmlinux, bool vmlinux_allocated)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001582{
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001583 int err = -1;
1584 struct symsrc ss;
David Ahernec5761e2010-12-09 13:27:07 -07001585 char symfs_vmlinux[PATH_MAX];
Cody P Schafer005f9292012-08-10 15:22:58 -07001586 enum dso_binary_type symtab_type;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001587
Namhyung Kim5698d2c2013-07-17 17:08:15 +09001588 if (vmlinux[0] == '/')
1589 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1590 else
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -03001591 symbol__join_symfs(symfs_vmlinux, vmlinux);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001592
Cody P Schafer21ea4532012-08-10 15:22:56 -07001593 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Cody P Schafer005f9292012-08-10 15:22:58 -07001594 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
Cody P Schafer21ea4532012-08-10 15:22:56 -07001595 else
Cody P Schafer005f9292012-08-10 15:22:58 -07001596 symtab_type = DSO_BINARY_TYPE__VMLINUX;
Cody P Schafer21ea4532012-08-10 15:22:56 -07001597
Cody P Schafer005f9292012-08-10 15:22:58 -07001598 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001599 return -1;
1600
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001601 err = dso__load_sym(dso, map, &ss, &ss, 0);
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001602 symsrc__destroy(&ss);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001603
Cody P Schafer515850e2012-08-10 15:22:54 -07001604 if (err > 0) {
Adrian Hunter39b12f782013-08-07 14:38:47 +03001605 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001606 dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
Adrian Hunter39b12f782013-08-07 14:38:47 +03001607 else
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001608 dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -03001609 dso__set_long_name(dso, vmlinux, vmlinux_allocated);
Cody P Schafer515850e2012-08-10 15:22:54 -07001610 dso__set_loaded(dso, map->type);
David Ahernec5761e2010-12-09 13:27:07 -07001611 pr_debug("Using %s for symbols\n", symfs_vmlinux);
Cody P Schafer515850e2012-08-10 15:22:54 -07001612 }
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001613
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001614 return err;
1615}
1616
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001617int dso__load_vmlinux_path(struct dso *dso, struct map *map)
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001618{
1619 int i, err = 0;
Namhyung Kim00dc8652014-11-04 10:14:32 +09001620 char *filename = NULL;
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001621
Arnaldo Carvalho de Melodc382182015-10-11 17:17:24 -03001622 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1623 vmlinux_path__nr_entries + 1);
1624
1625 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001626 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false);
Arnaldo Carvalho de Melodc382182015-10-11 17:17:24 -03001627 if (err > 0)
1628 goto out;
1629 }
1630
Namhyung Kim00dc8652014-11-04 10:14:32 +09001631 if (!symbol_conf.ignore_vmlinux_buildid)
1632 filename = dso__build_id_filename(dso, NULL, 0);
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001633 if (filename != NULL) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001634 err = dso__load_vmlinux(dso, map, filename, true);
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -03001635 if (err > 0)
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001636 goto out;
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001637 free(filename);
1638 }
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001639out:
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001640 return err;
1641}
1642
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001643static bool visible_dir_filter(const char *name, struct dirent *d)
1644{
1645 if (d->d_type != DT_DIR)
1646 return false;
1647 return lsdir_no_dot_filter(name, d);
1648}
1649
Adrian Hunter0544d422013-10-14 13:43:43 +03001650static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1651{
1652 char kallsyms_filename[PATH_MAX];
Adrian Hunter0544d422013-10-14 13:43:43 +03001653 int ret = -1;
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001654 struct strlist *dirs;
1655 struct str_node *nd;
Adrian Hunter0544d422013-10-14 13:43:43 +03001656
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001657 dirs = lsdir(dir, visible_dir_filter);
1658 if (!dirs)
Adrian Hunter0544d422013-10-14 13:43:43 +03001659 return -1;
1660
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03001661 strlist__for_each_entry(nd, dirs) {
Adrian Hunter0544d422013-10-14 13:43:43 +03001662 scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001663 "%s/%s/kallsyms", dir, nd->s);
Adrian Huntera00d28c2014-01-29 16:14:41 +02001664 if (!validate_kcore_addresses(kallsyms_filename, map)) {
Adrian Hunter0544d422013-10-14 13:43:43 +03001665 strlcpy(dir, kallsyms_filename, dir_sz);
1666 ret = 0;
1667 break;
1668 }
1669 }
1670
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001671 strlist__delete(dirs);
Adrian Hunter0544d422013-10-14 13:43:43 +03001672
1673 return ret;
1674}
1675
Masami Hiramatsu11870d72016-05-29 00:15:13 +09001676/*
1677 * Use open(O_RDONLY) to check readability directly instead of access(R_OK)
1678 * since access(R_OK) only checks with real UID/GID but open() use effective
1679 * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO).
1680 */
1681static bool filename__readable(const char *file)
1682{
1683 int fd = open(file, O_RDONLY);
1684 if (fd < 0)
1685 return false;
1686 close(fd);
1687 return true;
1688}
1689
Adrian Hunter0544d422013-10-14 13:43:43 +03001690static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1691{
1692 u8 host_build_id[BUILD_ID_SIZE];
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001693 char sbuild_id[SBUILD_ID_SIZE];
Adrian Hunter0544d422013-10-14 13:43:43 +03001694 bool is_host = false;
1695 char path[PATH_MAX];
1696
1697 if (!dso->has_build_id) {
1698 /*
1699 * Last resort, if we don't have a build-id and couldn't find
1700 * any vmlinux file, try the running kernel kallsyms table.
1701 */
1702 goto proc_kallsyms;
1703 }
1704
1705 if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1706 sizeof(host_build_id)) == 0)
1707 is_host = dso__build_id_equal(dso, host_build_id);
1708
Masami Hiramatsu4e4b6c02016-05-29 00:15:28 +09001709 /* Try a fast path for /proc/kallsyms if possible */
Adrian Hunter0544d422013-10-14 13:43:43 +03001710 if (is_host) {
Adrian Hunter0544d422013-10-14 13:43:43 +03001711 /*
Masami Hiramatsu11870d72016-05-29 00:15:13 +09001712 * Do not check the build-id cache, unless we know we cannot use
1713 * /proc/kcore or module maps don't match to /proc/kallsyms.
1714 * To check readability of /proc/kcore, do not use access(R_OK)
1715 * since /proc/kcore requires CAP_SYS_RAWIO to read and access
1716 * can't check it.
Adrian Hunter0544d422013-10-14 13:43:43 +03001717 */
Masami Hiramatsu11870d72016-05-29 00:15:13 +09001718 if (filename__readable("/proc/kcore") &&
1719 !validate_kcore_addresses("/proc/kallsyms", map))
1720 goto proc_kallsyms;
Adrian Hunter0544d422013-10-14 13:43:43 +03001721 }
1722
Masami Hiramatsu4e4b6c02016-05-29 00:15:28 +09001723 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1724
Adrian Hunter449867e2013-11-26 15:19:24 +02001725 /* Find kallsyms in build-id cache with kcore */
Masami Hiramatsu4e4b6c02016-05-29 00:15:28 +09001726 scnprintf(path, sizeof(path), "%s/%s/%s",
1727 buildid_dir, DSO__NAME_KCORE, sbuild_id);
1728
Adrian Hunter449867e2013-11-26 15:19:24 +02001729 if (!find_matching_kcore(map, path, sizeof(path)))
1730 return strdup(path);
1731
Masami Hiramatsu4e4b6c02016-05-29 00:15:28 +09001732 /* Use current /proc/kallsyms if possible */
1733 if (is_host) {
1734proc_kallsyms:
1735 return strdup("/proc/kallsyms");
1736 }
1737
1738 /* Finally, find a cache of kallsyms */
Masami Hiramatsu01412262016-05-29 00:15:37 +09001739 if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) {
Adrian Hunter0544d422013-10-14 13:43:43 +03001740 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1741 sbuild_id);
1742 return NULL;
1743 }
1744
1745 return strdup(path);
Adrian Hunter0544d422013-10-14 13:43:43 +03001746}
1747
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001748static int dso__load_kernel_sym(struct dso *dso, struct map *map)
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001749{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001750 int err;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001751 const char *kallsyms_filename = NULL;
1752 char *kallsyms_allocated_filename = NULL;
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001753 /*
David Ahernb226a5a72010-12-07 19:39:46 -07001754 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1755 * it and only it, reporting errors to the user if it cannot be used.
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001756 *
1757 * For instance, try to analyse an ARM perf.data file _without_ a
1758 * build-id, or if the user specifies the wrong path to the right
1759 * vmlinux file, obviously we can't fallback to another vmlinux (a
1760 * x86_86 one, on the machine where analysis is being performed, say),
1761 * or worse, /proc/kallsyms.
1762 *
1763 * If the specified file _has_ a build-id and there is a build-id
1764 * section in the perf.data file, we will still do the expected
1765 * validation in dso__load_vmlinux and will bail out if they don't
1766 * match.
1767 */
David Ahernb226a5a72010-12-07 19:39:46 -07001768 if (symbol_conf.kallsyms_name != NULL) {
1769 kallsyms_filename = symbol_conf.kallsyms_name;
1770 goto do_kallsyms;
1771 }
1772
Willy Tarreaufc2be692013-09-14 10:32:59 +02001773 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001774 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, false);
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001775 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001776
Willy Tarreaufc2be692013-09-14 10:32:59 +02001777 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001778 err = dso__load_vmlinux_path(dso, map);
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001779 if (err > 0)
Adrian Hunter39b12f782013-08-07 14:38:47 +03001780 return err;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001781 }
1782
David Ahernec5761e2010-12-09 13:27:07 -07001783 /* do not try local files if a symfs was given */
1784 if (symbol_conf.symfs[0] != 0)
1785 return -1;
1786
Adrian Hunter0544d422013-10-14 13:43:43 +03001787 kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
1788 if (!kallsyms_allocated_filename)
1789 return -1;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001790
Adrian Hunter0544d422013-10-14 13:43:43 +03001791 kallsyms_filename = kallsyms_allocated_filename;
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001792
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001793do_kallsyms:
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001794 err = dso__load_kallsyms(dso, kallsyms_filename, map);
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001795 if (err > 0)
1796 pr_debug("Using %s for symbols\n", kallsyms_filename);
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001797 free(kallsyms_allocated_filename);
1798
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001799 if (err > 0 && !dso__is_kcore(dso)) {
Adrian Hunterbdac0bc2014-07-14 13:02:43 +03001800 dso->binary_type = DSO_BINARY_TYPE__KALLSYMS;
Masami Hiramatsu0a775822016-05-15 12:19:40 +09001801 dso__set_long_name(dso, DSO__NAME_KALLSYMS, false);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001802 map__fixup_start(map);
1803 map__fixup_end(map);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001804 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001805
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001806 return err;
1807}
1808
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001809static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001810{
1811 int err;
1812 const char *kallsyms_filename = NULL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001813 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001814 char path[PATH_MAX];
1815
1816 if (!map->groups) {
1817 pr_debug("Guest kernel map hasn't the point to groups\n");
1818 return -1;
1819 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001820 machine = map->groups->machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001821
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001822 if (machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001823 /*
1824 * if the user specified a vmlinux filename, use it and only
1825 * it, reporting errors to the user if it cannot be used.
1826 * Or use file guest_kallsyms inputted by user on commandline
1827 */
1828 if (symbol_conf.default_guest_vmlinux_name != NULL) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001829 err = dso__load_vmlinux(dso, map,
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -03001830 symbol_conf.default_guest_vmlinux_name,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001831 false);
Adrian Hunter39b12f782013-08-07 14:38:47 +03001832 return err;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001833 }
1834
1835 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1836 if (!kallsyms_filename)
1837 return -1;
1838 } else {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001839 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001840 kallsyms_filename = path;
1841 }
1842
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001843 err = dso__load_kallsyms(dso, kallsyms_filename, map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001844 if (err > 0)
Adrian Hunter39b12f782013-08-07 14:38:47 +03001845 pr_debug("Using %s for symbols\n", kallsyms_filename);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001846 if (err > 0 && !dso__is_kcore(dso)) {
Adrian Hunterbdac0bc2014-07-14 13:02:43 +03001847 dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
Adrian Hunter39b12f782013-08-07 14:38:47 +03001848 machine__mmap_name(machine, path, sizeof(path));
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001849 dso__set_long_name(dso, strdup(path), true);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001850 map__fixup_start(map);
1851 map__fixup_end(map);
1852 }
1853
1854 return err;
1855}
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001856
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001857static void vmlinux_path__exit(void)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001858{
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001859 while (--vmlinux_path__nr_entries >= 0)
1860 zfree(&vmlinux_path[vmlinux_path__nr_entries]);
Wang Nanc4f03542015-05-17 10:56:27 +00001861 vmlinux_path__nr_entries = 0;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001862
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001863 zfree(&vmlinux_path);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001864}
1865
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01001866static const char * const vmlinux_paths[] = {
1867 "vmlinux",
1868 "/boot/vmlinux"
1869};
1870
1871static const char * const vmlinux_paths_upd[] = {
1872 "/boot/vmlinux-%s",
1873 "/usr/lib/debug/boot/vmlinux-%s",
1874 "/lib/modules/%s/build/vmlinux",
Ekaterina Tumanovaf55ae952015-11-25 17:32:46 +01001875 "/usr/lib/debug/lib/modules/%s/vmlinux",
1876 "/usr/lib/debug/boot/vmlinux-%s.debug"
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01001877};
1878
1879static int vmlinux_path__add(const char *new_entry)
1880{
1881 vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry);
1882 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1883 return -1;
1884 ++vmlinux_path__nr_entries;
1885
1886 return 0;
1887}
1888
Kan Liangce80d3b2015-08-28 05:48:04 -04001889static int vmlinux_path__init(struct perf_env *env)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001890{
1891 struct utsname uts;
1892 char bf[PATH_MAX];
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001893 char *kernel_version;
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01001894 unsigned int i;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001895
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01001896 vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
1897 ARRAY_SIZE(vmlinux_paths_upd)));
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001898 if (vmlinux_path == NULL)
1899 return -1;
1900
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01001901 for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++)
1902 if (vmlinux_path__add(vmlinux_paths[i]) < 0)
1903 goto out_fail;
David Ahernec5761e2010-12-09 13:27:07 -07001904
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001905 /* only try kernel version if no symfs was given */
David Ahernec5761e2010-12-09 13:27:07 -07001906 if (symbol_conf.symfs[0] != 0)
1907 return 0;
1908
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001909 if (env) {
1910 kernel_version = env->os_release;
1911 } else {
1912 if (uname(&uts) < 0)
1913 goto out_fail;
David Ahernec5761e2010-12-09 13:27:07 -07001914
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001915 kernel_version = uts.release;
1916 }
1917
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01001918 for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) {
1919 snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version);
1920 if (vmlinux_path__add(bf) < 0)
1921 goto out_fail;
1922 }
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001923
1924 return 0;
1925
1926out_fail:
1927 vmlinux_path__exit();
1928 return -1;
1929}
1930
David Ahern3bfe5f82013-11-18 13:32:48 -07001931int setup_list(struct strlist **list, const char *list_str,
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001932 const char *list_name)
1933{
1934 if (list_str == NULL)
1935 return 0;
1936
Arnaldo Carvalho de Melo4a77e212015-07-20 12:13:34 -03001937 *list = strlist__new(list_str, NULL);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001938 if (!*list) {
1939 pr_err("problems parsing %s list\n", list_name);
1940 return -1;
1941 }
Arnaldo Carvalho de Melo0bc2f2f2015-07-13 08:21:57 -03001942
1943 symbol_conf.has_filter = true;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001944 return 0;
1945}
1946
David Aherne03eaa42015-03-24 09:52:41 -06001947int setup_intlist(struct intlist **list, const char *list_str,
1948 const char *list_name)
1949{
1950 if (list_str == NULL)
1951 return 0;
1952
1953 *list = intlist__new(list_str);
1954 if (!*list) {
1955 pr_err("problems parsing %s list\n", list_name);
1956 return -1;
1957 }
1958 return 0;
1959}
1960
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03001961static bool symbol__read_kptr_restrict(void)
1962{
1963 bool value = false;
Wang Nan38272dc2016-05-24 09:21:27 +00001964 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03001965
Wang Nan38272dc2016-05-24 09:21:27 +00001966 if (fp != NULL) {
1967 char line[8];
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03001968
Wang Nan38272dc2016-05-24 09:21:27 +00001969 if (fgets(line, sizeof(line), fp) != NULL)
Wang Nan3dbe46c2016-11-15 04:05:45 +00001970 value = ((geteuid() != 0) || (getuid() != 0)) ?
Wang Nan38272dc2016-05-24 09:21:27 +00001971 (atoi(line) != 0) :
1972 (atoi(line) == 2);
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03001973
Wang Nan38272dc2016-05-24 09:21:27 +00001974 fclose(fp);
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03001975 }
1976
1977 return value;
1978}
1979
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -03001980int symbol__annotation_init(void)
1981{
1982 if (symbol_conf.initialized) {
1983 pr_err("Annotation needs to be init before symbol__init()\n");
1984 return -1;
1985 }
1986
1987 if (symbol_conf.init_annotation) {
1988 pr_warning("Annotation being initialized multiple times\n");
1989 return 0;
1990 }
1991
1992 symbol_conf.priv_size += sizeof(struct annotation);
1993 symbol_conf.init_annotation = true;
1994 return 0;
1995}
1996
Kan Liangce80d3b2015-08-28 05:48:04 -04001997int symbol__init(struct perf_env *env)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001998{
David Ahernec5761e2010-12-09 13:27:07 -07001999 const char *symfs;
2000
Jovi Zhang85e00b52010-09-09 13:30:59 -03002001 if (symbol_conf.initialized)
2002 return 0;
2003
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002004 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
David S. Miller4d439512011-03-29 14:18:39 -03002005
Namhyung Kim166ccc92012-08-06 13:41:19 +09002006 symbol__elf_init();
2007
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02002008 if (symbol_conf.sort_by_name)
2009 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2010 sizeof(struct symbol));
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02002011
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002012 if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002013 return -1;
2014
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02002015 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2016 pr_err("'.' is the only non valid --field-separator argument\n");
2017 return -1;
2018 }
2019
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002020 if (setup_list(&symbol_conf.dso_list,
2021 symbol_conf.dso_list_str, "dso") < 0)
2022 return -1;
2023
2024 if (setup_list(&symbol_conf.comm_list,
2025 symbol_conf.comm_list_str, "comm") < 0)
2026 goto out_free_dso_list;
2027
David Aherne03eaa42015-03-24 09:52:41 -06002028 if (setup_intlist(&symbol_conf.pid_list,
2029 symbol_conf.pid_list_str, "pid") < 0)
2030 goto out_free_comm_list;
2031
2032 if (setup_intlist(&symbol_conf.tid_list,
2033 symbol_conf.tid_list_str, "tid") < 0)
2034 goto out_free_pid_list;
2035
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002036 if (setup_list(&symbol_conf.sym_list,
2037 symbol_conf.sym_list_str, "symbol") < 0)
David Aherne03eaa42015-03-24 09:52:41 -06002038 goto out_free_tid_list;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002039
David Ahern64eff7d2016-11-25 13:00:21 -07002040 if (setup_list(&symbol_conf.bt_stop_list,
2041 symbol_conf.bt_stop_list_str, "symbol") < 0)
2042 goto out_free_sym_list;
2043
David Ahernec5761e2010-12-09 13:27:07 -07002044 /*
2045 * A path to symbols of "/" is identical to ""
2046 * reset here for simplicity.
2047 */
2048 symfs = realpath(symbol_conf.symfs, NULL);
2049 if (symfs == NULL)
2050 symfs = symbol_conf.symfs;
2051 if (strcmp(symfs, "/") == 0)
2052 symbol_conf.symfs = "";
2053 if (symfs != symbol_conf.symfs)
2054 free((void *)symfs);
2055
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002056 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2057
Jovi Zhang85e00b52010-09-09 13:30:59 -03002058 symbol_conf.initialized = true;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002059 return 0;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002060
David Ahern64eff7d2016-11-25 13:00:21 -07002061out_free_sym_list:
2062 strlist__delete(symbol_conf.sym_list);
David Aherne03eaa42015-03-24 09:52:41 -06002063out_free_tid_list:
2064 intlist__delete(symbol_conf.tid_list);
2065out_free_pid_list:
2066 intlist__delete(symbol_conf.pid_list);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002067out_free_comm_list:
2068 strlist__delete(symbol_conf.comm_list);
Namhyung Kimd74c8962011-12-13 00:16:52 +09002069out_free_dso_list:
2070 strlist__delete(symbol_conf.dso_list);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002071 return -1;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002072}
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002073
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002074void symbol__exit(void)
2075{
Jovi Zhang85e00b52010-09-09 13:30:59 -03002076 if (!symbol_conf.initialized)
2077 return;
David Ahern64eff7d2016-11-25 13:00:21 -07002078 strlist__delete(symbol_conf.bt_stop_list);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002079 strlist__delete(symbol_conf.sym_list);
2080 strlist__delete(symbol_conf.dso_list);
2081 strlist__delete(symbol_conf.comm_list);
David Aherne03eaa42015-03-24 09:52:41 -06002082 intlist__delete(symbol_conf.tid_list);
2083 intlist__delete(symbol_conf.pid_list);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002084 vmlinux_path__exit();
2085 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
David Ahern64eff7d2016-11-25 13:00:21 -07002086 symbol_conf.bt_stop_list = NULL;
Jovi Zhang85e00b52010-09-09 13:30:59 -03002087 symbol_conf.initialized = false;
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002088}
He Kuanga7066702016-05-19 11:47:37 +00002089
2090int symbol__config_symfs(const struct option *opt __maybe_unused,
2091 const char *dir, int unset __maybe_unused)
2092{
2093 char *bf = NULL;
2094 int ret;
2095
2096 symbol_conf.symfs = strdup(dir);
2097 if (symbol_conf.symfs == NULL)
2098 return -ENOMEM;
2099
2100 /* skip the locally configured cache if a symfs is given, and
2101 * config buildid dir to symfs/.debug
2102 */
2103 ret = asprintf(&bf, "%s/%s", dir, ".debug");
2104 if (ret < 0)
2105 return -ENOMEM;
2106
2107 set_buildid_dir(bf);
2108
2109 free(bf);
2110 return 0;
2111}