blob: 19c9c558454f671684938147b1061635b3f188f0 [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 Melob01141f2016-08-25 16:09:21 -030012#include "annotate.h"
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -030013#include "build-id.h"
Namhyung Kime334c722012-02-10 10:10:17 +090014#include "util.h"
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030015#include "debug.h"
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030016#include "machine.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030017#include "symbol.h"
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -030018#include "strlist.h"
David Aherne03eaa42015-03-24 09:52:41 -060019#include "intlist.h"
Namhyung Kim0a7e6d12014-08-12 15:40:45 +090020#include "header.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030021
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030022#include <elf.h>
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -020023#include <limits.h>
Arnaldo Carvalho de Meloc506c962013-12-11 09:15:00 -030024#include <symbol/kallsyms.h>
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030025#include <sys/utsname.h>
Peter Zijlstra2cdbc462009-08-05 14:05:16 +020026
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -030027static int dso__load_kernel_sym(struct dso *dso, struct map *map);
28static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map);
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -030029static bool symbol__is_idle(const char *name);
30
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 = {
Namhyung Kime511db52013-12-24 16:19:25 +090035 .use_modules = true,
36 .try_vmlinux_path = true,
37 .annotate_src = true,
38 .demangle = true,
Avi Kivity763122a2014-09-13 07:15:05 +030039 .demangle_kernel = false,
Namhyung Kime511db52013-12-24 16:19:25 +090040 .cumulate_callchain = true,
Jiri Olsac8302362014-06-27 18:26:58 +020041 .show_hist_headers = true,
Namhyung Kime511db52013-12-24 16:19:25 +090042 .symfs = "",
Namhyung Kim1e9abf82015-11-29 23:24:17 +090043 .event_group = true,
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020044};
45
Jiri Olsa44f24cb2012-07-22 14:14:32 +020046static enum dso_binary_type binary_type_symtab[] = {
47 DSO_BINARY_TYPE__KALLSYMS,
48 DSO_BINARY_TYPE__GUEST_KALLSYMS,
49 DSO_BINARY_TYPE__JAVA_JIT,
50 DSO_BINARY_TYPE__DEBUGLINK,
51 DSO_BINARY_TYPE__BUILD_ID_CACHE,
52 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
53 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
54 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
55 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
56 DSO_BINARY_TYPE__GUEST_KMODULE,
Namhyung Kimc00c48f2014-11-04 10:14:27 +090057 DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
Jiri Olsa44f24cb2012-07-22 14:14:32 +020058 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
Namhyung Kimc00c48f2014-11-04 10:14:27 +090059 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020060 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
Jiri Olsa44f24cb2012-07-22 14:14:32 +020061 DSO_BINARY_TYPE__NOT_FOUND,
62};
63
Jiri Olsa028df762012-08-01 14:47:57 +020064#define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
Jiri Olsa44f24cb2012-07-22 14:14:32 +020065
Arnaldo Carvalho de Melo36a3e642010-01-04 16:19:27 -020066bool symbol_type__is_a(char symbol_type, enum map_type map_type)
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020067{
Anton Blanchard31877902011-08-24 16:40:16 +100068 symbol_type = toupper(symbol_type);
69
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020070 switch (map_type) {
71 case MAP__FUNCTION:
72 return symbol_type == 'T' || symbol_type == 'W';
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -020073 case MAP__VARIABLE:
Anton Blanchard31877902011-08-24 16:40:16 +100074 return symbol_type == 'D';
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020075 default:
76 return false;
77 }
78}
79
Anton Blanchard694bf402011-08-24 16:40:17 +100080static int prefix_underscores_count(const char *str)
81{
82 const char *tail = str;
83
84 while (*tail == '_')
85 tail++;
86
87 return tail - str;
88}
89
Naveen N. Raofb6d5942015-04-28 17:35:36 +053090int __weak arch__choose_best_symbol(struct symbol *syma,
91 struct symbol *symb __maybe_unused)
92{
93 /* Avoid "SyS" kernel syscall aliases */
94 if (strlen(syma->name) >= 3 && !strncmp(syma->name, "SyS", 3))
95 return SYMBOL_B;
96 if (strlen(syma->name) >= 10 && !strncmp(syma->name, "compat_SyS", 10))
97 return SYMBOL_B;
98
99 return SYMBOL_A;
100}
Anton Blanchard694bf402011-08-24 16:40:17 +1000101
102static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
103{
104 s64 a;
105 s64 b;
Adrian Hunter34454322013-08-07 14:38:49 +0300106 size_t na, nb;
Anton Blanchard694bf402011-08-24 16:40:17 +1000107
108 /* Prefer a symbol with non zero length */
109 a = syma->end - syma->start;
110 b = symb->end - symb->start;
111 if ((b == 0) && (a > 0))
112 return SYMBOL_A;
113 else if ((a == 0) && (b > 0))
114 return SYMBOL_B;
115
116 /* Prefer a non weak symbol over a weak one */
117 a = syma->binding == STB_WEAK;
118 b = symb->binding == STB_WEAK;
119 if (b && !a)
120 return SYMBOL_A;
121 if (a && !b)
122 return SYMBOL_B;
123
124 /* Prefer a global symbol over a non global one */
125 a = syma->binding == STB_GLOBAL;
126 b = symb->binding == STB_GLOBAL;
127 if (a && !b)
128 return SYMBOL_A;
129 if (b && !a)
130 return SYMBOL_B;
131
132 /* Prefer a symbol with less underscores */
133 a = prefix_underscores_count(syma->name);
134 b = prefix_underscores_count(symb->name);
135 if (b > a)
136 return SYMBOL_A;
137 else if (a > b)
138 return SYMBOL_B;
139
Adrian Hunter34454322013-08-07 14:38:49 +0300140 /* Choose the symbol with the longest name */
141 na = strlen(syma->name);
142 nb = strlen(symb->name);
143 if (na > nb)
Anton Blanchard694bf402011-08-24 16:40:17 +1000144 return SYMBOL_A;
Adrian Hunter34454322013-08-07 14:38:49 +0300145 else if (na < nb)
Anton Blanchard694bf402011-08-24 16:40:17 +1000146 return SYMBOL_B;
Adrian Hunter34454322013-08-07 14:38:49 +0300147
Naveen N. Raofb6d5942015-04-28 17:35:36 +0530148 return arch__choose_best_symbol(syma, symb);
Anton Blanchard694bf402011-08-24 16:40:17 +1000149}
150
Namhyung Kime5a18452012-08-06 13:41:20 +0900151void symbols__fixup_duplicate(struct rb_root *symbols)
Anton Blanchard694bf402011-08-24 16:40:17 +1000152{
153 struct rb_node *nd;
154 struct symbol *curr, *next;
155
Arnaldo Carvalho de Meloc97b40e2016-09-01 10:56:06 -0300156 if (symbol_conf.allow_aliases)
157 return;
158
Anton Blanchard694bf402011-08-24 16:40:17 +1000159 nd = rb_first(symbols);
160
161 while (nd) {
162 curr = rb_entry(nd, struct symbol, rb_node);
163again:
164 nd = rb_next(&curr->rb_node);
165 next = rb_entry(nd, struct symbol, rb_node);
166
167 if (!nd)
168 break;
169
170 if (curr->start != next->start)
171 continue;
172
173 if (choose_best_symbol(curr, next) == SYMBOL_A) {
174 rb_erase(&next->rb_node, symbols);
Chenggang Qind4f74eb2013-10-11 08:27:59 +0800175 symbol__delete(next);
Anton Blanchard694bf402011-08-24 16:40:17 +1000176 goto again;
177 } else {
178 nd = rb_next(&curr->rb_node);
179 rb_erase(&curr->rb_node, symbols);
Chenggang Qind4f74eb2013-10-11 08:27:59 +0800180 symbol__delete(curr);
Anton Blanchard694bf402011-08-24 16:40:17 +1000181 }
182 }
183}
184
Namhyung Kime5a18452012-08-06 13:41:20 +0900185void symbols__fixup_end(struct rb_root *symbols)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300186{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300187 struct rb_node *nd, *prevnd = rb_first(symbols);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300188 struct symbol *curr, *prev;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300189
190 if (prevnd == NULL)
191 return;
192
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300193 curr = rb_entry(prevnd, struct symbol, rb_node);
194
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300195 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300196 prev = curr;
197 curr = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300198
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200199 if (prev->end == prev->start && prev->end != curr->start)
Arnaldo Carvalho de Melo2c241bd2014-10-14 17:19:44 -0300200 prev->end = curr->start;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300201 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300202
203 /* Last entry */
204 if (curr->end == curr->start)
205 curr->end = roundup(curr->start, 4096);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300206}
207
Namhyung Kime5a18452012-08-06 13:41:20 +0900208void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300209{
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300210 struct maps *maps = &mg->maps[type];
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300211 struct map *next, *curr;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300212
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300213 pthread_rwlock_wrlock(&maps->lock);
214
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300215 curr = maps__first(maps);
216 if (curr == NULL)
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300217 goto out_unlock;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300218
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300219 for (next = map__next(curr); next; next = map__next(curr)) {
220 curr->end = next->start;
221 curr = next;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300222 }
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -0200223
224 /*
225 * We still haven't the actual symbols, so guess the
226 * last map final address.
227 */
Ian Munsie9d1faba2010-11-25 15:12:53 +1100228 curr->end = ~0ULL;
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300229
230out_unlock:
231 pthread_rwlock_unlock(&maps->lock);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300232}
233
Namhyung Kime5a18452012-08-06 13:41:20 +0900234struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300235{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300236 size_t namelen = strlen(name) + 1;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300237 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
238 sizeof(*sym) + namelen));
239 if (sym == NULL)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200240 return NULL;
241
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -0300242 if (symbol_conf.priv_size) {
243 if (symbol_conf.init_annotation) {
244 struct annotation *notes = (void *)sym;
245 pthread_mutex_init(&notes->lock, NULL);
246 }
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300247 sym = ((void *)sym) + symbol_conf.priv_size;
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -0300248 }
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200249
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300250 sym->start = start;
Arnaldo Carvalho de Melo2c241bd2014-10-14 17:19:44 -0300251 sym->end = len ? start + len : start;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300252 sym->binding = binding;
253 sym->namelen = namelen - 1;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200254
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300255 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
256 __func__, name, start, sym->end);
257 memcpy(sym->name, name, namelen);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200258
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300259 return sym;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300260}
261
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300262void symbol__delete(struct symbol *sym)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300263{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300264 free(((void *)sym) - symbol_conf.priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300265}
266
Jiri Olsacdd059d2012-10-27 23:18:32 +0200267void symbols__delete(struct rb_root *symbols)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300268{
269 struct symbol *pos;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300270 struct rb_node *next = rb_first(symbols);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300271
272 while (next) {
273 pos = rb_entry(next, struct symbol, rb_node);
274 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300275 rb_erase(&pos->rb_node, symbols);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200276 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300277 }
278}
279
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300280void __symbols__insert(struct rb_root *symbols, struct symbol *sym, bool kernel)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300281{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300282 struct rb_node **p = &symbols->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300283 struct rb_node *parent = NULL;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000284 const u64 ip = sym->start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300285 struct symbol *s;
286
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300287 if (kernel) {
288 const char *name = sym->name;
289 /*
290 * ppc64 uses function descriptors and appends a '.' to the
291 * start of every instruction address. Remove it.
292 */
293 if (name[0] == '.')
294 name++;
295 sym->idle = symbol__is_idle(name);
296 }
297
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300298 while (*p != NULL) {
299 parent = *p;
300 s = rb_entry(parent, struct symbol, rb_node);
301 if (ip < s->start)
302 p = &(*p)->rb_left;
303 else
304 p = &(*p)->rb_right;
305 }
306 rb_link_node(&sym->rb_node, parent, p);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300307 rb_insert_color(&sym->rb_node, symbols);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300308}
309
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300310void symbols__insert(struct rb_root *symbols, struct symbol *sym)
311{
312 __symbols__insert(symbols, sym, false);
313}
314
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300315static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300316{
317 struct rb_node *n;
318
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300319 if (symbols == NULL)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300320 return NULL;
321
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300322 n = symbols->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300323
324 while (n) {
325 struct symbol *s = rb_entry(n, struct symbol, rb_node);
326
327 if (ip < s->start)
328 n = n->rb_left;
Chris Phlipot9c7b37c2016-05-07 02:16:59 -0700329 else if (ip > s->end || (ip == s->end && ip != s->start))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300330 n = n->rb_right;
331 else
332 return s;
333 }
334
335 return NULL;
336}
337
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300338static struct symbol *symbols__first(struct rb_root *symbols)
339{
340 struct rb_node *n = rb_first(symbols);
341
342 if (n)
343 return rb_entry(n, struct symbol, rb_node);
344
345 return NULL;
346}
347
Adrian Hunter9c00a812014-07-14 13:02:50 +0300348static struct symbol *symbols__next(struct symbol *sym)
349{
350 struct rb_node *n = rb_next(&sym->rb_node);
351
352 if (n)
353 return rb_entry(n, struct symbol, rb_node);
354
355 return NULL;
356}
357
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300358static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200359{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300360 struct rb_node **p = &symbols->rb_node;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200361 struct rb_node *parent = NULL;
Rabin Vincent02a9d032010-11-23 22:08:18 +0530362 struct symbol_name_rb_node *symn, *s;
363
364 symn = container_of(sym, struct symbol_name_rb_node, sym);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200365
366 while (*p != NULL) {
367 parent = *p;
368 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
369 if (strcmp(sym->name, s->sym.name) < 0)
370 p = &(*p)->rb_left;
371 else
372 p = &(*p)->rb_right;
373 }
374 rb_link_node(&symn->rb_node, parent, p);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300375 rb_insert_color(&symn->rb_node, symbols);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200376}
377
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300378static void symbols__sort_by_name(struct rb_root *symbols,
379 struct rb_root *source)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200380{
381 struct rb_node *nd;
382
383 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
384 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300385 symbols__insert_by_name(symbols, pos);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200386 }
387}
388
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300389static struct symbol *symbols__find_by_name(struct rb_root *symbols,
390 const char *name)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200391{
392 struct rb_node *n;
Martin Liška5bcaaca2015-05-26 11:41:37 -0300393 struct symbol_name_rb_node *s = NULL;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200394
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300395 if (symbols == NULL)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200396 return NULL;
397
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300398 n = symbols->rb_node;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200399
400 while (n) {
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200401 int cmp;
402
403 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
Naveen N. Rao031b84c2015-04-28 17:35:37 +0530404 cmp = arch__compare_symbol_names(name, s->sym.name);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200405
406 if (cmp < 0)
407 n = n->rb_left;
408 else if (cmp > 0)
409 n = n->rb_right;
410 else
Namhyung Kimde480992015-01-16 15:31:28 -0300411 break;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200412 }
413
Namhyung Kimde480992015-01-16 15:31:28 -0300414 if (n == NULL)
415 return NULL;
416
417 /* return first symbol that has same name (if any) */
418 for (n = rb_prev(n); n; n = rb_prev(n)) {
419 struct symbol_name_rb_node *tmp;
420
421 tmp = rb_entry(n, struct symbol_name_rb_node, rb_node);
Naveen N. Rao031b84c2015-04-28 17:35:37 +0530422 if (arch__compare_symbol_names(tmp->sym.name, s->sym.name))
Namhyung Kimde480992015-01-16 15:31:28 -0300423 break;
424
425 s = tmp;
426 }
427
428 return &s->sym;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200429}
430
Arnaldo Carvalho de Meloc0b4dff2015-08-24 13:33:14 -0300431void dso__reset_find_symbol_cache(struct dso *dso)
432{
433 enum map_type type;
434
435 for (type = MAP__FUNCTION; type <= MAP__VARIABLE; ++type) {
436 dso->last_find_result[type].addr = 0;
437 dso->last_find_result[type].symbol = NULL;
438 }
439}
440
Chris Phlipotae93a6c2016-05-10 20:26:46 -0700441void dso__insert_symbol(struct dso *dso, enum map_type type, struct symbol *sym)
442{
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300443 __symbols__insert(&dso->symbols[type], sym, dso->kernel);
Chris Phlipotae93a6c2016-05-10 20:26:46 -0700444
445 /* update the symbol cache if necessary */
446 if (dso->last_find_result[type].addr >= sym->start &&
447 (dso->last_find_result[type].addr < sym->end ||
448 sym->start == sym->end)) {
449 dso->last_find_result[type].symbol = sym;
450 }
451}
452
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300453struct symbol *dso__find_symbol(struct dso *dso,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200454 enum map_type type, u64 addr)
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200455{
Arnaldo Carvalho de Melob685ac22015-07-22 12:52:17 -0300456 if (dso->last_find_result[type].addr != addr) {
457 dso->last_find_result[type].addr = addr;
458 dso->last_find_result[type].symbol = symbols__find(&dso->symbols[type], addr);
459 }
460
461 return dso->last_find_result[type].symbol;
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200462}
463
Adrian Hunter9c00a812014-07-14 13:02:50 +0300464struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300465{
466 return symbols__first(&dso->symbols[type]);
467}
468
Adrian Hunter9c00a812014-07-14 13:02:50 +0300469struct symbol *dso__next_symbol(struct symbol *sym)
470{
471 return symbols__next(sym);
472}
473
Arnaldo Carvalho de Melo18bd7262015-01-16 15:39:53 -0300474struct symbol *symbol__next_by_name(struct symbol *sym)
475{
476 struct symbol_name_rb_node *s = container_of(sym, struct symbol_name_rb_node, sym);
477 struct rb_node *n = rb_next(&s->rb_node);
478
479 return n ? &rb_entry(n, struct symbol_name_rb_node, rb_node)->sym : NULL;
480}
481
482 /*
483 * Teturns first symbol that matched with @name.
484 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300485struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200486 const char *name)
487{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300488 return symbols__find_by_name(&dso->symbol_names[type], name);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200489}
490
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300491void dso__sort_by_name(struct dso *dso, enum map_type type)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200492{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300493 dso__set_sorted_by_name(dso, type);
494 return symbols__sort_by_name(&dso->symbol_names[type],
495 &dso->symbols[type]);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200496}
497
Adrian Hunter316d70d2013-10-08 11:45:48 +0300498int modules__parse(const char *filename, void *arg,
499 int (*process_module)(void *arg, const char *name,
500 u64 start))
501{
502 char *line = NULL;
503 size_t n;
504 FILE *file;
505 int err = 0;
506
507 file = fopen(filename, "r");
508 if (file == NULL)
509 return -1;
510
511 while (1) {
512 char name[PATH_MAX];
513 u64 start;
514 char *sep;
515 ssize_t line_len;
516
517 line_len = getline(&line, &n, file);
518 if (line_len < 0) {
519 if (feof(file))
520 break;
521 err = -1;
522 goto out;
523 }
524
525 if (!line) {
526 err = -1;
527 goto out;
528 }
529
530 line[--line_len] = '\0'; /* \n */
531
532 sep = strrchr(line, 'x');
533 if (sep == NULL)
534 continue;
535
536 hex2u64(sep + 1, &start);
537
538 sep = strchr(line, ' ');
539 if (sep == NULL)
540 continue;
541
542 *sep = '\0';
543
544 scnprintf(name, sizeof(name), "[%s]", line);
545
546 err = process_module(arg, name, start);
547 if (err)
548 break;
549 }
550out:
551 free(line);
552 fclose(file);
553 return err;
554}
555
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200556struct process_kallsyms_args {
557 struct map *map;
558 struct dso *dso;
559};
560
Arnaldo Carvalho de Meloe7110b92014-08-08 18:00:39 -0300561/*
562 * These are symbols in the kernel image, so make sure that
563 * sym is from a kernel DSO.
564 */
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300565static bool symbol__is_idle(const char *name)
David Ahern82d1deb2013-11-18 13:32:45 -0700566{
567 const char * const idle_symbols[] = {
568 "cpu_idle",
Arnaldo Carvalho de Meloe0336ed2014-08-08 18:02:41 -0300569 "cpu_startup_entry",
David Ahern82d1deb2013-11-18 13:32:45 -0700570 "intel_idle",
571 "default_idle",
572 "native_safe_halt",
573 "enter_idle",
574 "exit_idle",
575 "mwait_idle",
576 "mwait_idle_with_hints",
577 "poll_idle",
578 "ppc64_runlatch_off",
579 "pseries_dedicated_idle_sleep",
580 NULL
581 };
David Ahern82d1deb2013-11-18 13:32:45 -0700582 int i;
583
David Ahern82d1deb2013-11-18 13:32:45 -0700584 for (i = 0; idle_symbols[i]; i++) {
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300585 if (!strcmp(idle_symbols[i], name))
David Ahern82d1deb2013-11-18 13:32:45 -0700586 return true;
587 }
588
589 return false;
590}
591
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200592static int map__process_kallsym_symbol(void *arg, const char *name,
Cody P Schafer82151522012-08-10 15:22:48 -0700593 char type, u64 start)
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200594{
595 struct symbol *sym;
596 struct process_kallsyms_args *a = arg;
597 struct rb_root *root = &a->dso->symbols[a->map->type];
598
599 if (!symbol_type__is_a(type, a->map->type))
600 return 0;
601
Cody P Schafer82151522012-08-10 15:22:48 -0700602 /*
603 * module symbols are not sorted so we add all
604 * symbols, setting length to 0, and rely on
605 * symbols__fixup_end() to fix it up.
606 */
Arnaldo Carvalho de Melo8e947f12015-09-30 12:42:07 -0300607 sym = symbol__new(start, 0, kallsyms2elf_binding(type), name);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200608 if (sym == NULL)
609 return -ENOMEM;
610 /*
611 * We will pass the symbols to the filter later, in
612 * map__split_kallsyms, when we have split the maps per module
613 */
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300614 __symbols__insert(root, sym, !strchr(name, '['));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800615
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200616 return 0;
617}
618
619/*
620 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
621 * so that we can in the next step set the symbol ->end address and then
622 * call kernel_maps__split_kallsyms.
623 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300624static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200625 struct map *map)
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200626{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300627 struct process_kallsyms_args args = { .map = map, .dso = dso, };
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200628 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200629}
630
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300631static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map)
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300632{
Wang Nanba927322015-04-07 08:22:45 +0000633 struct map_groups *kmaps = map__kmaps(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300634 struct map *curr_map;
635 struct symbol *pos;
Adrian Hunter866548d2015-11-06 15:59:29 +0200636 int count = 0;
637 struct rb_root old_root = dso->symbols[map->type];
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300638 struct rb_root *root = &dso->symbols[map->type];
639 struct rb_node *next = rb_first(root);
640
Wang Nanba927322015-04-07 08:22:45 +0000641 if (!kmaps)
642 return -1;
643
Adrian Hunter866548d2015-11-06 15:59:29 +0200644 *root = RB_ROOT;
645
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300646 while (next) {
647 char *module;
648
649 pos = rb_entry(next, struct symbol, rb_node);
650 next = rb_next(&pos->rb_node);
651
Adrian Hunter866548d2015-11-06 15:59:29 +0200652 rb_erase_init(&pos->rb_node, &old_root);
653
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300654 module = strchr(pos->name, '\t');
655 if (module)
656 *module = '\0';
657
658 curr_map = map_groups__find(kmaps, map->type, pos->start);
659
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300660 if (!curr_map) {
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300661 symbol__delete(pos);
Adrian Hunter866548d2015-11-06 15:59:29 +0200662 continue;
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300663 }
Adrian Hunter866548d2015-11-06 15:59:29 +0200664
665 pos->start -= curr_map->start - curr_map->pgoff;
666 if (pos->end)
667 pos->end -= curr_map->start - curr_map->pgoff;
668 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
669 ++count;
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300670 }
671
672 /* Symbols have been adjusted */
673 dso->adjust_symbols = 1;
674
Adrian Hunter866548d2015-11-06 15:59:29 +0200675 return count;
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300676}
677
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300678/*
679 * Split the symbols into maps, making sure there are no overlaps, i.e. the
680 * kernel range is broken in several maps, named [kernel].N, as we don't have
681 * the original ELF section names vmlinux have.
682 */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300683static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300684{
Wang Nanba927322015-04-07 08:22:45 +0000685 struct map_groups *kmaps = map__kmaps(map);
686 struct machine *machine;
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200687 struct map *curr_map = map;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300688 struct symbol *pos;
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300689 int count = 0, moved = 0;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300690 struct rb_root *root = &dso->symbols[map->type];
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200691 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300692 int kernel_range = 0;
693
Wang Nanba927322015-04-07 08:22:45 +0000694 if (!kmaps)
695 return -1;
696
697 machine = kmaps->machine;
698
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300699 while (next) {
700 char *module;
701
702 pos = rb_entry(next, struct symbol, rb_node);
703 next = rb_next(&pos->rb_node);
704
705 module = strchr(pos->name, '\t');
706 if (module) {
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200707 if (!symbol_conf.use_modules)
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200708 goto discard_symbol;
709
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300710 *module++ = '\0';
711
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200712 if (strcmp(curr_map->dso->short_name, module)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800713 if (curr_map != map &&
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300714 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300715 machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800716 /*
717 * We assume all symbols of a module are
718 * continuous in * kallsyms, so curr_map
719 * points to a module and all its
720 * symbols are in its kmap. Mark it as
721 * loaded.
722 */
723 dso__set_loaded(curr_map->dso,
724 curr_map->type);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300725 }
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200726
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800727 curr_map = map_groups__find_by_name(kmaps,
728 map->type, module);
729 if (curr_map == NULL) {
Arnaldo Carvalho de Melo2f519032010-05-17 17:57:59 -0300730 pr_debug("%s/proc/{kallsyms,modules} "
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800731 "inconsistency while looking "
732 "for \"%s\" module!\n",
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300733 machine->root_dir, module);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800734 curr_map = map;
735 goto discard_symbol;
736 }
737
738 if (curr_map->dso->loaded &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300739 !machine__is_default_guest(machine))
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200740 goto discard_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300741 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300742 /*
743 * So that we look just like we get from .ko files,
744 * i.e. not prelinked, relative to map->start.
745 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200746 pos->start = curr_map->map_ip(curr_map, pos->start);
747 pos->end = curr_map->map_ip(curr_map, pos->end);
748 } else if (curr_map != map) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300749 char dso_name[PATH_MAX];
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300750 struct dso *ndso;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300751
Adrian Hunterd9b62ab2014-01-29 16:14:43 +0200752 if (delta) {
753 /* Kernel was relocated at boot time */
754 pos->start -= delta;
755 pos->end -= delta;
756 }
757
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200758 if (count == 0) {
759 curr_map = map;
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300760 goto add_symbol;
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200761 }
762
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300763 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800764 snprintf(dso_name, sizeof(dso_name),
765 "[guest.kernel].%d",
766 kernel_range++);
767 else
768 snprintf(dso_name, sizeof(dso_name),
769 "[kernel].%d",
770 kernel_range++);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300771
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300772 ndso = dso__new(dso_name);
773 if (ndso == NULL)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300774 return -1;
775
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300776 ndso->kernel = dso->kernel;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800777
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300778 curr_map = map__new2(pos->start, ndso, map->type);
Zhang, Yanmin37fe5fc2010-02-25 11:00:51 +0800779 if (curr_map == NULL) {
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300780 dso__put(ndso);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300781 return -1;
782 }
783
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200784 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200785 map_groups__insert(kmaps, curr_map);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300786 ++kernel_range;
Adrian Hunterd9b62ab2014-01-29 16:14:43 +0200787 } else if (delta) {
788 /* Kernel was relocated at boot time */
789 pos->start -= delta;
790 pos->end -= delta;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300791 }
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300792add_symbol:
793 if (curr_map != map) {
794 rb_erase(&pos->rb_node, root);
795 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
796 ++moved;
797 } else
798 ++count;
799
800 continue;
801discard_symbol:
802 rb_erase(&pos->rb_node, root);
803 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300804 }
805
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800806 if (curr_map != map &&
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300807 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300808 machine__is_default_guest(kmaps->machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800809 dso__set_loaded(curr_map->dso, curr_map->type);
810 }
811
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200812 return count + moved;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300813}
814
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300815bool symbol__restricted_filename(const char *filename,
816 const char *restricted_filename)
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -0300817{
818 bool restricted = false;
819
820 if (symbol_conf.kptr_restrict) {
821 char *r = realpath(filename, NULL);
822
823 if (r != NULL) {
824 restricted = strcmp(r, restricted_filename) == 0;
825 free(r);
826 return restricted;
827 }
828 }
829
830 return restricted;
831}
832
Adrian Hunter52afdaf2013-10-09 15:01:11 +0300833struct module_info {
834 struct rb_node rb_node;
835 char *name;
836 u64 start;
837};
838
839static void add_module(struct module_info *mi, struct rb_root *modules)
840{
841 struct rb_node **p = &modules->rb_node;
842 struct rb_node *parent = NULL;
843 struct module_info *m;
844
845 while (*p != NULL) {
846 parent = *p;
847 m = rb_entry(parent, struct module_info, rb_node);
848 if (strcmp(mi->name, m->name) < 0)
849 p = &(*p)->rb_left;
850 else
851 p = &(*p)->rb_right;
852 }
853 rb_link_node(&mi->rb_node, parent, p);
854 rb_insert_color(&mi->rb_node, modules);
855}
856
857static void delete_modules(struct rb_root *modules)
858{
859 struct module_info *mi;
860 struct rb_node *next = rb_first(modules);
861
862 while (next) {
863 mi = rb_entry(next, struct module_info, rb_node);
864 next = rb_next(&mi->rb_node);
865 rb_erase(&mi->rb_node, modules);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300866 zfree(&mi->name);
Adrian Hunter52afdaf2013-10-09 15:01:11 +0300867 free(mi);
868 }
869}
870
871static struct module_info *find_module(const char *name,
872 struct rb_root *modules)
873{
874 struct rb_node *n = modules->rb_node;
875
876 while (n) {
877 struct module_info *m;
878 int cmp;
879
880 m = rb_entry(n, struct module_info, rb_node);
881 cmp = strcmp(name, m->name);
882 if (cmp < 0)
883 n = n->rb_left;
884 else if (cmp > 0)
885 n = n->rb_right;
886 else
887 return m;
888 }
889
890 return NULL;
891}
892
893static int __read_proc_modules(void *arg, const char *name, u64 start)
894{
895 struct rb_root *modules = arg;
896 struct module_info *mi;
897
898 mi = zalloc(sizeof(struct module_info));
899 if (!mi)
900 return -ENOMEM;
901
902 mi->name = strdup(name);
903 mi->start = start;
904
905 if (!mi->name) {
906 free(mi);
907 return -ENOMEM;
908 }
909
910 add_module(mi, modules);
911
912 return 0;
913}
914
915static int read_proc_modules(const char *filename, struct rb_root *modules)
916{
917 if (symbol__restricted_filename(filename, "/proc/modules"))
918 return -1;
919
920 if (modules__parse(filename, modules, __read_proc_modules)) {
921 delete_modules(modules);
922 return -1;
923 }
924
925 return 0;
926}
927
Adrian Hunterfc1b6912013-10-14 16:57:29 +0300928int compare_proc_modules(const char *from, const char *to)
929{
930 struct rb_root from_modules = RB_ROOT;
931 struct rb_root to_modules = RB_ROOT;
932 struct rb_node *from_node, *to_node;
933 struct module_info *from_m, *to_m;
934 int ret = -1;
935
936 if (read_proc_modules(from, &from_modules))
937 return -1;
938
939 if (read_proc_modules(to, &to_modules))
940 goto out_delete_from;
941
942 from_node = rb_first(&from_modules);
943 to_node = rb_first(&to_modules);
944 while (from_node) {
945 if (!to_node)
946 break;
947
948 from_m = rb_entry(from_node, struct module_info, rb_node);
949 to_m = rb_entry(to_node, struct module_info, rb_node);
950
951 if (from_m->start != to_m->start ||
952 strcmp(from_m->name, to_m->name))
953 break;
954
955 from_node = rb_next(from_node);
956 to_node = rb_next(to_node);
957 }
958
959 if (!from_node && !to_node)
960 ret = 0;
961
962 delete_modules(&to_modules);
963out_delete_from:
964 delete_modules(&from_modules);
965
966 return ret;
967}
968
Adrian Hunter52afdaf2013-10-09 15:01:11 +0300969static int do_validate_kcore_modules(const char *filename, struct map *map,
970 struct map_groups *kmaps)
971{
972 struct rb_root modules = RB_ROOT;
973 struct map *old_map;
974 int err;
975
976 err = read_proc_modules(filename, &modules);
977 if (err)
978 return err;
979
980 old_map = map_groups__first(kmaps, map->type);
981 while (old_map) {
982 struct map *next = map_groups__next(old_map);
983 struct module_info *mi;
984
985 if (old_map == map || old_map->start == map->start) {
986 /* The kernel map */
987 old_map = next;
988 continue;
989 }
990
991 /* Module must be in memory at the same address */
992 mi = find_module(old_map->dso->short_name, &modules);
993 if (!mi || mi->start != old_map->start) {
994 err = -EINVAL;
995 goto out;
996 }
997
998 old_map = next;
999 }
1000out:
1001 delete_modules(&modules);
1002 return err;
1003}
1004
1005/*
1006 * If kallsyms is referenced by name then we look for filename in the same
1007 * directory.
1008 */
1009static bool filename_from_kallsyms_filename(char *filename,
1010 const char *base_name,
1011 const char *kallsyms_filename)
1012{
1013 char *name;
1014
1015 strcpy(filename, kallsyms_filename);
1016 name = strrchr(filename, '/');
1017 if (!name)
1018 return false;
1019
1020 name += 1;
1021
1022 if (!strcmp(name, "kallsyms")) {
1023 strcpy(name, base_name);
1024 return true;
1025 }
1026
1027 return false;
1028}
1029
1030static int validate_kcore_modules(const char *kallsyms_filename,
1031 struct map *map)
1032{
Wang Nanba927322015-04-07 08:22:45 +00001033 struct map_groups *kmaps = map__kmaps(map);
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001034 char modules_filename[PATH_MAX];
1035
Wang Nanba927322015-04-07 08:22:45 +00001036 if (!kmaps)
1037 return -EINVAL;
1038
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001039 if (!filename_from_kallsyms_filename(modules_filename, "modules",
1040 kallsyms_filename))
1041 return -EINVAL;
1042
1043 if (do_validate_kcore_modules(modules_filename, map, kmaps))
1044 return -EINVAL;
1045
1046 return 0;
1047}
1048
Adrian Huntera00d28c2014-01-29 16:14:41 +02001049static int validate_kcore_addresses(const char *kallsyms_filename,
1050 struct map *map)
1051{
1052 struct kmap *kmap = map__kmap(map);
1053
Wang Nanba927322015-04-07 08:22:45 +00001054 if (!kmap)
1055 return -EINVAL;
1056
Adrian Huntera00d28c2014-01-29 16:14:41 +02001057 if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
1058 u64 start;
1059
1060 start = kallsyms__get_function_start(kallsyms_filename,
1061 kmap->ref_reloc_sym->name);
1062 if (start != kmap->ref_reloc_sym->addr)
1063 return -EINVAL;
1064 }
1065
1066 return validate_kcore_modules(kallsyms_filename, map);
1067}
1068
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001069struct kcore_mapfn_data {
1070 struct dso *dso;
1071 enum map_type type;
1072 struct list_head maps;
1073};
1074
1075static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1076{
1077 struct kcore_mapfn_data *md = data;
1078 struct map *map;
1079
1080 map = map__new2(start, md->dso, md->type);
1081 if (map == NULL)
1082 return -ENOMEM;
1083
1084 map->end = map->start + len;
1085 map->pgoff = pgoff;
1086
1087 list_add(&map->node, &md->maps);
1088
1089 return 0;
1090}
1091
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001092static int dso__load_kcore(struct dso *dso, struct map *map,
1093 const char *kallsyms_filename)
1094{
Wang Nanba927322015-04-07 08:22:45 +00001095 struct map_groups *kmaps = map__kmaps(map);
1096 struct machine *machine;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001097 struct kcore_mapfn_data md;
1098 struct map *old_map, *new_map, *replacement_map = NULL;
1099 bool is_64_bit;
1100 int err, fd;
1101 char kcore_filename[PATH_MAX];
1102 struct symbol *sym;
1103
Wang Nanba927322015-04-07 08:22:45 +00001104 if (!kmaps)
1105 return -EINVAL;
1106
1107 machine = kmaps->machine;
1108
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001109 /* This function requires that the map is the kernel map */
1110 if (map != machine->vmlinux_maps[map->type])
1111 return -EINVAL;
1112
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001113 if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1114 kallsyms_filename))
1115 return -EINVAL;
1116
Adrian Huntera00d28c2014-01-29 16:14:41 +02001117 /* Modules and kernel must be present at their original addresses */
1118 if (validate_kcore_addresses(kallsyms_filename, map))
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001119 return -EINVAL;
1120
1121 md.dso = dso;
1122 md.type = map->type;
1123 INIT_LIST_HEAD(&md.maps);
1124
1125 fd = open(kcore_filename, O_RDONLY);
Li Zhang36c8bb52015-06-19 16:57:33 +08001126 if (fd < 0) {
Adrian Hunter133de942015-08-20 13:07:40 +03001127 pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n",
1128 kcore_filename);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001129 return -EINVAL;
Li Zhang36c8bb52015-06-19 16:57:33 +08001130 }
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001131
1132 /* Read new maps into temporary lists */
1133 err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
1134 &is_64_bit);
1135 if (err)
1136 goto out_err;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +03001137 dso->is_64_bit = is_64_bit;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001138
1139 if (list_empty(&md.maps)) {
1140 err = -EINVAL;
1141 goto out_err;
1142 }
1143
1144 /* Remove old maps */
1145 old_map = map_groups__first(kmaps, map->type);
1146 while (old_map) {
1147 struct map *next = map_groups__next(old_map);
1148
1149 if (old_map != map)
1150 map_groups__remove(kmaps, old_map);
1151 old_map = next;
1152 }
1153
1154 /* Find the kernel map using the first symbol */
1155 sym = dso__first_symbol(dso, map->type);
1156 list_for_each_entry(new_map, &md.maps, node) {
1157 if (sym && sym->start >= new_map->start &&
1158 sym->start < new_map->end) {
1159 replacement_map = new_map;
1160 break;
1161 }
1162 }
1163
1164 if (!replacement_map)
1165 replacement_map = list_entry(md.maps.next, struct map, node);
1166
1167 /* Add new maps */
1168 while (!list_empty(&md.maps)) {
1169 new_map = list_entry(md.maps.next, struct map, node);
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -03001170 list_del_init(&new_map->node);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001171 if (new_map == replacement_map) {
1172 map->start = new_map->start;
1173 map->end = new_map->end;
1174 map->pgoff = new_map->pgoff;
1175 map->map_ip = new_map->map_ip;
1176 map->unmap_ip = new_map->unmap_ip;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001177 /* Ensure maps are correctly ordered */
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001178 map__get(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001179 map_groups__remove(kmaps, map);
1180 map_groups__insert(kmaps, map);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001181 map__put(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001182 } else {
1183 map_groups__insert(kmaps, new_map);
1184 }
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001185
1186 map__put(new_map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001187 }
1188
1189 /*
1190 * Set the data type and long name so that kcore can be read via
1191 * dso__data_read_addr().
1192 */
1193 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001194 dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001195 else
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001196 dso->binary_type = DSO_BINARY_TYPE__KCORE;
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001197 dso__set_long_name(dso, strdup(kcore_filename), true);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001198
1199 close(fd);
1200
1201 if (map->type == MAP__FUNCTION)
1202 pr_debug("Using %s for kernel object code\n", kcore_filename);
1203 else
1204 pr_debug("Using %s for kernel data\n", kcore_filename);
1205
1206 return 0;
1207
1208out_err:
1209 while (!list_empty(&md.maps)) {
1210 map = list_entry(md.maps.next, struct map, node);
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -03001211 list_del_init(&map->node);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001212 map__put(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001213 }
1214 close(fd);
1215 return -EINVAL;
1216}
1217
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001218/*
1219 * If the kernel is relocated at boot time, kallsyms won't match. Compute the
1220 * delta based on the relocation reference symbol.
1221 */
1222static int kallsyms__delta(struct map *map, const char *filename, u64 *delta)
1223{
1224 struct kmap *kmap = map__kmap(map);
1225 u64 addr;
1226
Wang Nanba927322015-04-07 08:22:45 +00001227 if (!kmap)
1228 return -1;
1229
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001230 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1231 return 0;
1232
1233 addr = kallsyms__get_function_start(filename,
1234 kmap->ref_reloc_sym->name);
1235 if (!addr)
1236 return -1;
1237
1238 *delta = addr - kmap->ref_reloc_sym->addr;
1239 return 0;
1240}
1241
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001242int __dso__load_kallsyms(struct dso *dso, const char *filename,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001243 struct map *map, bool no_kcore)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001244{
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001245 u64 delta = 0;
1246
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03001247 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1248 return -1;
1249
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001250 if (dso__load_all_kallsyms(dso, filename, map) < 0)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001251 return -1;
1252
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001253 if (kallsyms__delta(map, filename, &delta))
1254 return -1;
1255
Anton Blanchard3f5a4272011-08-24 16:40:15 +10001256 symbols__fixup_end(&dso->symbols[map->type]);
Arnaldo Carvalho de Melo432746f2016-09-01 11:00:23 -03001257 symbols__fixup_duplicate(&dso->symbols[map->type]);
Anton Blanchard3f5a4272011-08-24 16:40:15 +10001258
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001259 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001260 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001261 else
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001262 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001263
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001264 if (!no_kcore && !dso__load_kcore(dso, map, filename))
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001265 return dso__split_kallsyms_for_kcore(dso, map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001266 else
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001267 return dso__split_kallsyms(dso, map, delta);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001268}
1269
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001270int dso__load_kallsyms(struct dso *dso, const char *filename,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001271 struct map *map)
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001272{
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001273 return __dso__load_kallsyms(dso, filename, map, false);
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001274}
1275
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001276static int dso__load_perf_map(struct dso *dso, struct map *map)
Pekka Enberg80d496b2009-06-08 21:12:48 +03001277{
1278 char *line = NULL;
1279 size_t n;
1280 FILE *file;
1281 int nr_syms = 0;
1282
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001283 file = fopen(dso->long_name, "r");
Pekka Enberg80d496b2009-06-08 21:12:48 +03001284 if (file == NULL)
1285 goto out_failure;
1286
1287 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001288 u64 start, size;
Pekka Enberg80d496b2009-06-08 21:12:48 +03001289 struct symbol *sym;
1290 int line_len, len;
1291
1292 line_len = getline(&line, &n, file);
1293 if (line_len < 0)
1294 break;
1295
1296 if (!line)
1297 goto out_failure;
1298
1299 line[--line_len] = '\0'; /* \n */
1300
1301 len = hex2u64(line, &start);
1302
1303 len++;
1304 if (len + 2 >= line_len)
1305 continue;
1306
1307 len += hex2u64(line + len, &size);
1308
1309 len++;
1310 if (len + 2 >= line_len)
1311 continue;
1312
Arnaldo Carvalho de Meloc408fed2010-08-05 12:59:47 -03001313 sym = symbol__new(start, size, STB_GLOBAL, line + len);
Pekka Enberg80d496b2009-06-08 21:12:48 +03001314
1315 if (sym == NULL)
1316 goto out_delete_line;
1317
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001318 symbols__insert(&dso->symbols[map->type], sym);
1319 nr_syms++;
Pekka Enberg80d496b2009-06-08 21:12:48 +03001320 }
1321
1322 free(line);
1323 fclose(file);
1324
1325 return nr_syms;
1326
1327out_delete_line:
1328 free(line);
1329out_failure:
1330 return -1;
1331}
1332
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001333static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1334 enum dso_binary_type type)
1335{
1336 switch (type) {
1337 case DSO_BINARY_TYPE__JAVA_JIT:
1338 case DSO_BINARY_TYPE__DEBUGLINK:
1339 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1340 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1341 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1342 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1343 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1344 return !kmod && dso->kernel == DSO_TYPE_USER;
1345
1346 case DSO_BINARY_TYPE__KALLSYMS:
1347 case DSO_BINARY_TYPE__VMLINUX:
1348 case DSO_BINARY_TYPE__KCORE:
1349 return dso->kernel == DSO_TYPE_KERNEL;
1350
1351 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1352 case DSO_BINARY_TYPE__GUEST_VMLINUX:
1353 case DSO_BINARY_TYPE__GUEST_KCORE:
1354 return dso->kernel == DSO_TYPE_GUEST_KERNEL;
1355
1356 case DSO_BINARY_TYPE__GUEST_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +09001357 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001358 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +09001359 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001360 /*
1361 * kernel modules know their symtab type - it's set when
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001362 * creating a module dso in machine__findnew_module_map().
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001363 */
1364 return kmod && dso->symtab_type == type;
1365
1366 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1367 return true;
1368
1369 case DSO_BINARY_TYPE__NOT_FOUND:
1370 default:
1371 return false;
1372 }
1373}
1374
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001375int dso__load(struct dso *dso, struct map *map)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001376{
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001377 char *name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001378 int ret = -1;
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001379 u_int i;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001380 struct machine *machine;
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001381 char *root_dir = (char *) "";
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001382 int ss_pos = 0;
1383 struct symsrc ss_[2];
1384 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001385 bool kmod;
Dima Kogan5baecbc2015-09-07 17:34:19 -07001386 unsigned char build_id[BUILD_ID_SIZE];
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001387
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001388 pthread_mutex_lock(&dso->lock);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001389
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001390 /* check again under the dso->lock */
1391 if (dso__loaded(dso, map->type)) {
1392 ret = 1;
1393 goto out;
1394 }
1395
1396 if (dso->kernel) {
1397 if (dso->kernel == DSO_TYPE_KERNEL)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001398 ret = dso__load_kernel_sym(dso, map);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001399 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001400 ret = dso__load_guest_kernel_sym(dso, map);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001401
1402 goto out;
1403 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001404
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001405 if (map->groups && map->groups->machine)
1406 machine = map->groups->machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001407 else
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001408 machine = NULL;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001409
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001410 dso->adjust_symbols = 0;
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001411
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001412 if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
Pekka Enberg981c1252011-08-09 22:54:18 +03001413 struct stat st;
1414
Vasiliy Kulikove9b52ef2011-08-12 00:55:37 +04001415 if (lstat(dso->name, &st) < 0)
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001416 goto out;
Pekka Enberg981c1252011-08-09 22:54:18 +03001417
Arnaldo Carvalho de Melo2059fc72015-11-12 16:50:13 -03001418 if (!symbol_conf.force && st.st_uid && (st.st_uid != geteuid())) {
Pekka Enberg981c1252011-08-09 22:54:18 +03001419 pr_warning("File %s not owned by current user or root, "
Arnaldo Carvalho de Melo2059fc72015-11-12 16:50:13 -03001420 "ignoring it (use -f to override).\n", dso->name);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001421 goto out;
Pekka Enberg981c1252011-08-09 22:54:18 +03001422 }
1423
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001424 ret = dso__load_perf_map(dso, map);
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001425 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1426 DSO_BINARY_TYPE__NOT_FOUND;
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001427 goto out;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001428 }
1429
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001430 if (machine)
1431 root_dir = machine->root_dir;
1432
David Ahern164c8002013-01-14 10:46:47 -07001433 name = malloc(PATH_MAX);
1434 if (!name)
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001435 goto out;
David Ahern164c8002013-01-14 10:46:47 -07001436
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001437 kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
Namhyung Kimc00c48f2014-11-04 10:14:27 +09001438 dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
1439 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE ||
1440 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001441
Dima Kogan5baecbc2015-09-07 17:34:19 -07001442
1443 /*
1444 * Read the build id if possible. This is required for
1445 * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work
1446 */
Jiri Olsaed7b6302016-06-24 14:40:25 +02001447 if (is_regular_file(dso->long_name) &&
Jiri Olsa40356722016-01-20 12:56:32 +01001448 filename__read_build_id(dso->long_name, build_id, BUILD_ID_SIZE) > 0)
Dima Kogan5baecbc2015-09-07 17:34:19 -07001449 dso__set_build_id(dso, build_id);
1450
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001451 /*
1452 * Iterate over candidate debug images.
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001453 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1454 * and/or opd section) for processing.
Dave Martin6da80ce2010-07-30 09:50:09 -03001455 */
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001456 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001457 struct symsrc *ss = &ss_[ss_pos];
1458 bool next_slot = false;
Dave Martin6da80ce2010-07-30 09:50:09 -03001459
Cody P Schafer005f9292012-08-10 15:22:58 -07001460 enum dso_binary_type symtab_type = binary_type_symtab[i];
Dave Martin6da80ce2010-07-30 09:50:09 -03001461
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001462 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1463 continue;
1464
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -03001465 if (dso__read_binary_type_filename(dso, symtab_type,
1466 root_dir, name, PATH_MAX))
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001467 continue;
Dave Martin6da80ce2010-07-30 09:50:09 -03001468
Jiri Olsa40356722016-01-20 12:56:32 +01001469 if (!is_regular_file(name))
1470 continue;
1471
Dave Martin6da80ce2010-07-30 09:50:09 -03001472 /* Name is now the name of the next image to try */
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001473 if (symsrc__init(ss, dso, name, symtab_type) < 0)
Dave Martin6da80ce2010-07-30 09:50:09 -03001474 continue;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001475
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001476 if (!syms_ss && symsrc__has_symtab(ss)) {
1477 syms_ss = ss;
1478 next_slot = true;
Adrian Hunter0058aef2013-12-03 09:23:08 +02001479 if (!dso->symsrc_filename)
1480 dso->symsrc_filename = strdup(name);
Cody P Schaferd26cd122012-08-10 15:23:00 -07001481 }
1482
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001483 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1484 runtime_ss = ss;
1485 next_slot = true;
Cody P Schafera44f6052012-08-10 15:22:59 -07001486 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001487
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001488 if (next_slot) {
1489 ss_pos++;
Jiri Olsa33ff5812012-04-18 15:46:58 +02001490
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001491 if (syms_ss && runtime_ss)
1492 break;
Namhyung Kim98e9f032014-02-20 10:32:54 +09001493 } else {
1494 symsrc__destroy(ss);
Dave Martin6da80ce2010-07-30 09:50:09 -03001495 }
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001496
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001497 }
Dave Martin6da80ce2010-07-30 09:50:09 -03001498
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001499 if (!runtime_ss && !syms_ss)
1500 goto out_free;
1501
1502 if (runtime_ss && !syms_ss) {
1503 syms_ss = runtime_ss;
Arnaldo Carvalho de Melo60e4b102011-03-22 15:42:14 -03001504 }
1505
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001506 /* We'll have to hope for the best */
1507 if (!runtime_ss && syms_ss)
1508 runtime_ss = syms_ss;
1509
Wang Nane7ee4042016-02-05 14:01:27 +00001510 if (syms_ss && syms_ss->type == DSO_BINARY_TYPE__BUILD_ID_CACHE)
1511 if (dso__build_id_is_kmod(dso, name, PATH_MAX))
1512 kmod = true;
1513
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001514 if (syms_ss)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001515 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, kmod);
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001516 else
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001517 ret = -1;
1518
David Ahernf47b58b2012-08-19 09:47:14 -06001519 if (ret > 0) {
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001520 int nr_plt;
1521
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001522 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map);
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001523 if (nr_plt > 0)
1524 ret += nr_plt;
1525 }
1526
1527 for (; ss_pos > 0; ss_pos--)
1528 symsrc__destroy(&ss_[ss_pos - 1]);
1529out_free:
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001530 free(name);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001531 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001532 ret = 0;
1533out:
1534 dso__set_loaded(dso, map->type);
1535 pthread_mutex_unlock(&dso->lock);
1536
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001537 return ret;
1538}
1539
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001540struct map *map_groups__find_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -02001541 enum map_type type, const char *name)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001542{
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -03001543 struct maps *maps = &mg->maps[type];
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -03001544 struct map *map;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001545
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -03001546 pthread_rwlock_rdlock(&maps->lock);
1547
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -03001548 for (map = maps__first(maps); map; map = map__next(map)) {
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001549 if (map->dso && strcmp(map->dso->short_name, name) == 0)
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -03001550 goto out_unlock;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001551 }
1552
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -03001553 map = NULL;
1554
1555out_unlock:
1556 pthread_rwlock_unlock(&maps->lock);
1557 return map;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001558}
1559
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001560int dso__load_vmlinux(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001561 const char *vmlinux, bool vmlinux_allocated)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001562{
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001563 int err = -1;
1564 struct symsrc ss;
David Ahernec5761e2010-12-09 13:27:07 -07001565 char symfs_vmlinux[PATH_MAX];
Cody P Schafer005f9292012-08-10 15:22:58 -07001566 enum dso_binary_type symtab_type;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001567
Namhyung Kim5698d2c2013-07-17 17:08:15 +09001568 if (vmlinux[0] == '/')
1569 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1570 else
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -03001571 symbol__join_symfs(symfs_vmlinux, vmlinux);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001572
Cody P Schafer21ea4532012-08-10 15:22:56 -07001573 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Cody P Schafer005f9292012-08-10 15:22:58 -07001574 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
Cody P Schafer21ea4532012-08-10 15:22:56 -07001575 else
Cody P Schafer005f9292012-08-10 15:22:58 -07001576 symtab_type = DSO_BINARY_TYPE__VMLINUX;
Cody P Schafer21ea4532012-08-10 15:22:56 -07001577
Cody P Schafer005f9292012-08-10 15:22:58 -07001578 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001579 return -1;
1580
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001581 err = dso__load_sym(dso, map, &ss, &ss, 0);
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001582 symsrc__destroy(&ss);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001583
Cody P Schafer515850e2012-08-10 15:22:54 -07001584 if (err > 0) {
Adrian Hunter39b12f782013-08-07 14:38:47 +03001585 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001586 dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
Adrian Hunter39b12f782013-08-07 14:38:47 +03001587 else
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001588 dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -03001589 dso__set_long_name(dso, vmlinux, vmlinux_allocated);
Cody P Schafer515850e2012-08-10 15:22:54 -07001590 dso__set_loaded(dso, map->type);
David Ahernec5761e2010-12-09 13:27:07 -07001591 pr_debug("Using %s for symbols\n", symfs_vmlinux);
Cody P Schafer515850e2012-08-10 15:22:54 -07001592 }
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001593
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001594 return err;
1595}
1596
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001597int dso__load_vmlinux_path(struct dso *dso, struct map *map)
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001598{
1599 int i, err = 0;
Namhyung Kim00dc8652014-11-04 10:14:32 +09001600 char *filename = NULL;
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001601
Arnaldo Carvalho de Melodc382182015-10-11 17:17:24 -03001602 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1603 vmlinux_path__nr_entries + 1);
1604
1605 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001606 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false);
Arnaldo Carvalho de Melodc382182015-10-11 17:17:24 -03001607 if (err > 0)
1608 goto out;
1609 }
1610
Namhyung Kim00dc8652014-11-04 10:14:32 +09001611 if (!symbol_conf.ignore_vmlinux_buildid)
1612 filename = dso__build_id_filename(dso, NULL, 0);
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001613 if (filename != NULL) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001614 err = dso__load_vmlinux(dso, map, filename, true);
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -03001615 if (err > 0)
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001616 goto out;
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001617 free(filename);
1618 }
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001619out:
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001620 return err;
1621}
1622
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001623static bool visible_dir_filter(const char *name, struct dirent *d)
1624{
1625 if (d->d_type != DT_DIR)
1626 return false;
1627 return lsdir_no_dot_filter(name, d);
1628}
1629
Adrian Hunter0544d422013-10-14 13:43:43 +03001630static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1631{
1632 char kallsyms_filename[PATH_MAX];
Adrian Hunter0544d422013-10-14 13:43:43 +03001633 int ret = -1;
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001634 struct strlist *dirs;
1635 struct str_node *nd;
Adrian Hunter0544d422013-10-14 13:43:43 +03001636
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001637 dirs = lsdir(dir, visible_dir_filter);
1638 if (!dirs)
Adrian Hunter0544d422013-10-14 13:43:43 +03001639 return -1;
1640
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03001641 strlist__for_each_entry(nd, dirs) {
Adrian Hunter0544d422013-10-14 13:43:43 +03001642 scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001643 "%s/%s/kallsyms", dir, nd->s);
Adrian Huntera00d28c2014-01-29 16:14:41 +02001644 if (!validate_kcore_addresses(kallsyms_filename, map)) {
Adrian Hunter0544d422013-10-14 13:43:43 +03001645 strlcpy(dir, kallsyms_filename, dir_sz);
1646 ret = 0;
1647 break;
1648 }
1649 }
1650
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001651 strlist__delete(dirs);
Adrian Hunter0544d422013-10-14 13:43:43 +03001652
1653 return ret;
1654}
1655
Masami Hiramatsu11870d72016-05-29 00:15:13 +09001656/*
1657 * Use open(O_RDONLY) to check readability directly instead of access(R_OK)
1658 * since access(R_OK) only checks with real UID/GID but open() use effective
1659 * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO).
1660 */
1661static bool filename__readable(const char *file)
1662{
1663 int fd = open(file, O_RDONLY);
1664 if (fd < 0)
1665 return false;
1666 close(fd);
1667 return true;
1668}
1669
Adrian Hunter0544d422013-10-14 13:43:43 +03001670static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1671{
1672 u8 host_build_id[BUILD_ID_SIZE];
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001673 char sbuild_id[SBUILD_ID_SIZE];
Adrian Hunter0544d422013-10-14 13:43:43 +03001674 bool is_host = false;
1675 char path[PATH_MAX];
1676
1677 if (!dso->has_build_id) {
1678 /*
1679 * Last resort, if we don't have a build-id and couldn't find
1680 * any vmlinux file, try the running kernel kallsyms table.
1681 */
1682 goto proc_kallsyms;
1683 }
1684
1685 if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1686 sizeof(host_build_id)) == 0)
1687 is_host = dso__build_id_equal(dso, host_build_id);
1688
Masami Hiramatsu4e4b6c02016-05-29 00:15:28 +09001689 /* Try a fast path for /proc/kallsyms if possible */
Adrian Hunter0544d422013-10-14 13:43:43 +03001690 if (is_host) {
Adrian Hunter0544d422013-10-14 13:43:43 +03001691 /*
Masami Hiramatsu11870d72016-05-29 00:15:13 +09001692 * Do not check the build-id cache, unless we know we cannot use
1693 * /proc/kcore or module maps don't match to /proc/kallsyms.
1694 * To check readability of /proc/kcore, do not use access(R_OK)
1695 * since /proc/kcore requires CAP_SYS_RAWIO to read and access
1696 * can't check it.
Adrian Hunter0544d422013-10-14 13:43:43 +03001697 */
Masami Hiramatsu11870d72016-05-29 00:15:13 +09001698 if (filename__readable("/proc/kcore") &&
1699 !validate_kcore_addresses("/proc/kallsyms", map))
1700 goto proc_kallsyms;
Adrian Hunter0544d422013-10-14 13:43:43 +03001701 }
1702
Masami Hiramatsu4e4b6c02016-05-29 00:15:28 +09001703 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1704
Adrian Hunter449867e2013-11-26 15:19:24 +02001705 /* Find kallsyms in build-id cache with kcore */
Masami Hiramatsu4e4b6c02016-05-29 00:15:28 +09001706 scnprintf(path, sizeof(path), "%s/%s/%s",
1707 buildid_dir, DSO__NAME_KCORE, sbuild_id);
1708
Adrian Hunter449867e2013-11-26 15:19:24 +02001709 if (!find_matching_kcore(map, path, sizeof(path)))
1710 return strdup(path);
1711
Masami Hiramatsu4e4b6c02016-05-29 00:15:28 +09001712 /* Use current /proc/kallsyms if possible */
1713 if (is_host) {
1714proc_kallsyms:
1715 return strdup("/proc/kallsyms");
1716 }
1717
1718 /* Finally, find a cache of kallsyms */
Masami Hiramatsu01412262016-05-29 00:15:37 +09001719 if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) {
Adrian Hunter0544d422013-10-14 13:43:43 +03001720 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1721 sbuild_id);
1722 return NULL;
1723 }
1724
1725 return strdup(path);
Adrian Hunter0544d422013-10-14 13:43:43 +03001726}
1727
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001728static int dso__load_kernel_sym(struct dso *dso, struct map *map)
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001729{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001730 int err;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001731 const char *kallsyms_filename = NULL;
1732 char *kallsyms_allocated_filename = NULL;
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001733 /*
David Ahernb226a5a72010-12-07 19:39:46 -07001734 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1735 * it and only it, reporting errors to the user if it cannot be used.
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001736 *
1737 * For instance, try to analyse an ARM perf.data file _without_ a
1738 * build-id, or if the user specifies the wrong path to the right
1739 * vmlinux file, obviously we can't fallback to another vmlinux (a
1740 * x86_86 one, on the machine where analysis is being performed, say),
1741 * or worse, /proc/kallsyms.
1742 *
1743 * If the specified file _has_ a build-id and there is a build-id
1744 * section in the perf.data file, we will still do the expected
1745 * validation in dso__load_vmlinux and will bail out if they don't
1746 * match.
1747 */
David Ahernb226a5a72010-12-07 19:39:46 -07001748 if (symbol_conf.kallsyms_name != NULL) {
1749 kallsyms_filename = symbol_conf.kallsyms_name;
1750 goto do_kallsyms;
1751 }
1752
Willy Tarreaufc2be692013-09-14 10:32:59 +02001753 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001754 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, false);
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001755 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001756
Willy Tarreaufc2be692013-09-14 10:32:59 +02001757 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001758 err = dso__load_vmlinux_path(dso, map);
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001759 if (err > 0)
Adrian Hunter39b12f782013-08-07 14:38:47 +03001760 return err;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001761 }
1762
David Ahernec5761e2010-12-09 13:27:07 -07001763 /* do not try local files if a symfs was given */
1764 if (symbol_conf.symfs[0] != 0)
1765 return -1;
1766
Adrian Hunter0544d422013-10-14 13:43:43 +03001767 kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
1768 if (!kallsyms_allocated_filename)
1769 return -1;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001770
Adrian Hunter0544d422013-10-14 13:43:43 +03001771 kallsyms_filename = kallsyms_allocated_filename;
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001772
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001773do_kallsyms:
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001774 err = dso__load_kallsyms(dso, kallsyms_filename, map);
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001775 if (err > 0)
1776 pr_debug("Using %s for symbols\n", kallsyms_filename);
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001777 free(kallsyms_allocated_filename);
1778
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001779 if (err > 0 && !dso__is_kcore(dso)) {
Adrian Hunterbdac0bc2014-07-14 13:02:43 +03001780 dso->binary_type = DSO_BINARY_TYPE__KALLSYMS;
Masami Hiramatsu0a775822016-05-15 12:19:40 +09001781 dso__set_long_name(dso, DSO__NAME_KALLSYMS, false);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001782 map__fixup_start(map);
1783 map__fixup_end(map);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001784 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001785
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001786 return err;
1787}
1788
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001789static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001790{
1791 int err;
1792 const char *kallsyms_filename = NULL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001793 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001794 char path[PATH_MAX];
1795
1796 if (!map->groups) {
1797 pr_debug("Guest kernel map hasn't the point to groups\n");
1798 return -1;
1799 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001800 machine = map->groups->machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001801
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001802 if (machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001803 /*
1804 * if the user specified a vmlinux filename, use it and only
1805 * it, reporting errors to the user if it cannot be used.
1806 * Or use file guest_kallsyms inputted by user on commandline
1807 */
1808 if (symbol_conf.default_guest_vmlinux_name != NULL) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001809 err = dso__load_vmlinux(dso, map,
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -03001810 symbol_conf.default_guest_vmlinux_name,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001811 false);
Adrian Hunter39b12f782013-08-07 14:38:47 +03001812 return err;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001813 }
1814
1815 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1816 if (!kallsyms_filename)
1817 return -1;
1818 } else {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001819 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001820 kallsyms_filename = path;
1821 }
1822
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001823 err = dso__load_kallsyms(dso, kallsyms_filename, map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001824 if (err > 0)
Adrian Hunter39b12f782013-08-07 14:38:47 +03001825 pr_debug("Using %s for symbols\n", kallsyms_filename);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001826 if (err > 0 && !dso__is_kcore(dso)) {
Adrian Hunterbdac0bc2014-07-14 13:02:43 +03001827 dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
Adrian Hunter39b12f782013-08-07 14:38:47 +03001828 machine__mmap_name(machine, path, sizeof(path));
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001829 dso__set_long_name(dso, strdup(path), true);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001830 map__fixup_start(map);
1831 map__fixup_end(map);
1832 }
1833
1834 return err;
1835}
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001836
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001837static void vmlinux_path__exit(void)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001838{
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001839 while (--vmlinux_path__nr_entries >= 0)
1840 zfree(&vmlinux_path[vmlinux_path__nr_entries]);
Wang Nanc4f03542015-05-17 10:56:27 +00001841 vmlinux_path__nr_entries = 0;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001842
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001843 zfree(&vmlinux_path);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001844}
1845
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01001846static const char * const vmlinux_paths[] = {
1847 "vmlinux",
1848 "/boot/vmlinux"
1849};
1850
1851static const char * const vmlinux_paths_upd[] = {
1852 "/boot/vmlinux-%s",
1853 "/usr/lib/debug/boot/vmlinux-%s",
1854 "/lib/modules/%s/build/vmlinux",
Ekaterina Tumanovaf55ae952015-11-25 17:32:46 +01001855 "/usr/lib/debug/lib/modules/%s/vmlinux",
1856 "/usr/lib/debug/boot/vmlinux-%s.debug"
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01001857};
1858
1859static int vmlinux_path__add(const char *new_entry)
1860{
1861 vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry);
1862 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1863 return -1;
1864 ++vmlinux_path__nr_entries;
1865
1866 return 0;
1867}
1868
Kan Liangce80d3b2015-08-28 05:48:04 -04001869static int vmlinux_path__init(struct perf_env *env)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001870{
1871 struct utsname uts;
1872 char bf[PATH_MAX];
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001873 char *kernel_version;
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01001874 unsigned int i;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001875
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01001876 vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
1877 ARRAY_SIZE(vmlinux_paths_upd)));
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001878 if (vmlinux_path == NULL)
1879 return -1;
1880
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01001881 for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++)
1882 if (vmlinux_path__add(vmlinux_paths[i]) < 0)
1883 goto out_fail;
David Ahernec5761e2010-12-09 13:27:07 -07001884
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001885 /* only try kernel version if no symfs was given */
David Ahernec5761e2010-12-09 13:27:07 -07001886 if (symbol_conf.symfs[0] != 0)
1887 return 0;
1888
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001889 if (env) {
1890 kernel_version = env->os_release;
1891 } else {
1892 if (uname(&uts) < 0)
1893 goto out_fail;
David Ahernec5761e2010-12-09 13:27:07 -07001894
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001895 kernel_version = uts.release;
1896 }
1897
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01001898 for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) {
1899 snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version);
1900 if (vmlinux_path__add(bf) < 0)
1901 goto out_fail;
1902 }
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001903
1904 return 0;
1905
1906out_fail:
1907 vmlinux_path__exit();
1908 return -1;
1909}
1910
David Ahern3bfe5f82013-11-18 13:32:48 -07001911int setup_list(struct strlist **list, const char *list_str,
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001912 const char *list_name)
1913{
1914 if (list_str == NULL)
1915 return 0;
1916
Arnaldo Carvalho de Melo4a77e212015-07-20 12:13:34 -03001917 *list = strlist__new(list_str, NULL);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001918 if (!*list) {
1919 pr_err("problems parsing %s list\n", list_name);
1920 return -1;
1921 }
Arnaldo Carvalho de Melo0bc2f2f2015-07-13 08:21:57 -03001922
1923 symbol_conf.has_filter = true;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02001924 return 0;
1925}
1926
David Aherne03eaa42015-03-24 09:52:41 -06001927int setup_intlist(struct intlist **list, const char *list_str,
1928 const char *list_name)
1929{
1930 if (list_str == NULL)
1931 return 0;
1932
1933 *list = intlist__new(list_str);
1934 if (!*list) {
1935 pr_err("problems parsing %s list\n", list_name);
1936 return -1;
1937 }
1938 return 0;
1939}
1940
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03001941static bool symbol__read_kptr_restrict(void)
1942{
1943 bool value = false;
Wang Nan38272dc2016-05-24 09:21:27 +00001944 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03001945
Wang Nan38272dc2016-05-24 09:21:27 +00001946 if (fp != NULL) {
1947 char line[8];
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03001948
Wang Nan38272dc2016-05-24 09:21:27 +00001949 if (fgets(line, sizeof(line), fp) != NULL)
1950 value = (geteuid() != 0) ?
1951 (atoi(line) != 0) :
1952 (atoi(line) == 2);
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03001953
Wang Nan38272dc2016-05-24 09:21:27 +00001954 fclose(fp);
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03001955 }
1956
1957 return value;
1958}
1959
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -03001960int symbol__annotation_init(void)
1961{
1962 if (symbol_conf.initialized) {
1963 pr_err("Annotation needs to be init before symbol__init()\n");
1964 return -1;
1965 }
1966
1967 if (symbol_conf.init_annotation) {
1968 pr_warning("Annotation being initialized multiple times\n");
1969 return 0;
1970 }
1971
1972 symbol_conf.priv_size += sizeof(struct annotation);
1973 symbol_conf.init_annotation = true;
1974 return 0;
1975}
1976
Kan Liangce80d3b2015-08-28 05:48:04 -04001977int symbol__init(struct perf_env *env)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001978{
David Ahernec5761e2010-12-09 13:27:07 -07001979 const char *symfs;
1980
Jovi Zhang85e00b52010-09-09 13:30:59 -03001981 if (symbol_conf.initialized)
1982 return 0;
1983
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001984 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
David S. Miller4d439512011-03-29 14:18:39 -03001985
Namhyung Kim166ccc92012-08-06 13:41:19 +09001986 symbol__elf_init();
1987
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02001988 if (symbol_conf.sort_by_name)
1989 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
1990 sizeof(struct symbol));
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02001991
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001992 if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001993 return -1;
1994
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02001995 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
1996 pr_err("'.' is the only non valid --field-separator argument\n");
1997 return -1;
1998 }
1999
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002000 if (setup_list(&symbol_conf.dso_list,
2001 symbol_conf.dso_list_str, "dso") < 0)
2002 return -1;
2003
2004 if (setup_list(&symbol_conf.comm_list,
2005 symbol_conf.comm_list_str, "comm") < 0)
2006 goto out_free_dso_list;
2007
David Aherne03eaa42015-03-24 09:52:41 -06002008 if (setup_intlist(&symbol_conf.pid_list,
2009 symbol_conf.pid_list_str, "pid") < 0)
2010 goto out_free_comm_list;
2011
2012 if (setup_intlist(&symbol_conf.tid_list,
2013 symbol_conf.tid_list_str, "tid") < 0)
2014 goto out_free_pid_list;
2015
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002016 if (setup_list(&symbol_conf.sym_list,
2017 symbol_conf.sym_list_str, "symbol") < 0)
David Aherne03eaa42015-03-24 09:52:41 -06002018 goto out_free_tid_list;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002019
David Ahernec5761e2010-12-09 13:27:07 -07002020 /*
2021 * A path to symbols of "/" is identical to ""
2022 * reset here for simplicity.
2023 */
2024 symfs = realpath(symbol_conf.symfs, NULL);
2025 if (symfs == NULL)
2026 symfs = symbol_conf.symfs;
2027 if (strcmp(symfs, "/") == 0)
2028 symbol_conf.symfs = "";
2029 if (symfs != symbol_conf.symfs)
2030 free((void *)symfs);
2031
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002032 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2033
Jovi Zhang85e00b52010-09-09 13:30:59 -03002034 symbol_conf.initialized = true;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002035 return 0;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002036
David Aherne03eaa42015-03-24 09:52:41 -06002037out_free_tid_list:
2038 intlist__delete(symbol_conf.tid_list);
2039out_free_pid_list:
2040 intlist__delete(symbol_conf.pid_list);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002041out_free_comm_list:
2042 strlist__delete(symbol_conf.comm_list);
Namhyung Kimd74c8962011-12-13 00:16:52 +09002043out_free_dso_list:
2044 strlist__delete(symbol_conf.dso_list);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002045 return -1;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002046}
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002047
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002048void symbol__exit(void)
2049{
Jovi Zhang85e00b52010-09-09 13:30:59 -03002050 if (!symbol_conf.initialized)
2051 return;
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002052 strlist__delete(symbol_conf.sym_list);
2053 strlist__delete(symbol_conf.dso_list);
2054 strlist__delete(symbol_conf.comm_list);
David Aherne03eaa42015-03-24 09:52:41 -06002055 intlist__delete(symbol_conf.tid_list);
2056 intlist__delete(symbol_conf.pid_list);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002057 vmlinux_path__exit();
2058 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
Jovi Zhang85e00b52010-09-09 13:30:59 -03002059 symbol_conf.initialized = false;
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002060}
He Kuanga7066702016-05-19 11:47:37 +00002061
2062int symbol__config_symfs(const struct option *opt __maybe_unused,
2063 const char *dir, int unset __maybe_unused)
2064{
2065 char *bf = NULL;
2066 int ret;
2067
2068 symbol_conf.symfs = strdup(dir);
2069 if (symbol_conf.symfs == NULL)
2070 return -ENOMEM;
2071
2072 /* skip the locally configured cache if a symfs is given, and
2073 * config buildid dir to symfs/.debug
2074 */
2075 ret = asprintf(&bf, "%s/%s", dir, ".debug");
2076 if (ret < 0)
2077 return -ENOMEM;
2078
2079 set_buildid_dir(bf);
2080
2081 free(bf);
2082 return 0;
2083}