blob: 68f14d770083b724f137af1f43bc3af4d3ddccfb [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 Melo3d689ed2017-04-17 16:10:49 -030022#include "sane_ctype.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030023
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030024#include <elf.h>
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -020025#include <limits.h>
Arnaldo Carvalho de Meloc506c962013-12-11 09:15:00 -030026#include <symbol/kallsyms.h>
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030027#include <sys/utsname.h>
Peter Zijlstra2cdbc462009-08-05 14:05:16 +020028
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -030029static int dso__load_kernel_sym(struct dso *dso, struct map *map);
30static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map);
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -030031static bool symbol__is_idle(const char *name);
32
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030033int vmlinux_path__nr_entries;
34char **vmlinux_path;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030035
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -020036struct symbol_conf symbol_conf = {
Namhyung Kime511db52013-12-24 16:19:25 +090037 .use_modules = true,
38 .try_vmlinux_path = true,
39 .annotate_src = true,
40 .demangle = true,
Avi Kivity763122a2014-09-13 07:15:05 +030041 .demangle_kernel = false,
Namhyung Kime511db52013-12-24 16:19:25 +090042 .cumulate_callchain = true,
Jiri Olsac8302362014-06-27 18:26:58 +020043 .show_hist_headers = true,
Namhyung Kime511db52013-12-24 16:19:25 +090044 .symfs = "",
Namhyung Kim1e9abf82015-11-29 23:24:17 +090045 .event_group = true,
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020046};
47
Jiri Olsa44f24cb2012-07-22 14:14:32 +020048static enum dso_binary_type binary_type_symtab[] = {
49 DSO_BINARY_TYPE__KALLSYMS,
50 DSO_BINARY_TYPE__GUEST_KALLSYMS,
51 DSO_BINARY_TYPE__JAVA_JIT,
52 DSO_BINARY_TYPE__DEBUGLINK,
53 DSO_BINARY_TYPE__BUILD_ID_CACHE,
54 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
55 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
56 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
57 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
58 DSO_BINARY_TYPE__GUEST_KMODULE,
Namhyung Kimc00c48f2014-11-04 10:14:27 +090059 DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
Jiri Olsa44f24cb2012-07-22 14:14:32 +020060 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
Namhyung Kimc00c48f2014-11-04 10:14:27 +090061 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020062 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
Jiri Olsa44f24cb2012-07-22 14:14:32 +020063 DSO_BINARY_TYPE__NOT_FOUND,
64};
65
Jiri Olsa028df762012-08-01 14:47:57 +020066#define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
Jiri Olsa44f24cb2012-07-22 14:14:32 +020067
Arnaldo Carvalho de Melo36a3e642010-01-04 16:19:27 -020068bool symbol_type__is_a(char symbol_type, enum map_type map_type)
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020069{
Anton Blanchard31877902011-08-24 16:40:16 +100070 symbol_type = toupper(symbol_type);
71
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020072 switch (map_type) {
73 case MAP__FUNCTION:
74 return symbol_type == 'T' || symbol_type == 'W';
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -020075 case MAP__VARIABLE:
Anton Blanchard31877902011-08-24 16:40:16 +100076 return symbol_type == 'D';
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020077 default:
78 return false;
79 }
80}
81
Anton Blanchard694bf402011-08-24 16:40:17 +100082static int prefix_underscores_count(const char *str)
83{
84 const char *tail = str;
85
86 while (*tail == '_')
87 tail++;
88
89 return tail - str;
90}
91
Naveen N. Raofb6d5942015-04-28 17:35:36 +053092int __weak arch__choose_best_symbol(struct symbol *syma,
93 struct symbol *symb __maybe_unused)
94{
95 /* Avoid "SyS" kernel syscall aliases */
96 if (strlen(syma->name) >= 3 && !strncmp(syma->name, "SyS", 3))
97 return SYMBOL_B;
98 if (strlen(syma->name) >= 10 && !strncmp(syma->name, "compat_SyS", 10))
99 return SYMBOL_B;
100
101 return SYMBOL_A;
102}
Anton Blanchard694bf402011-08-24 16:40:17 +1000103
104static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
105{
106 s64 a;
107 s64 b;
Adrian Hunter34454322013-08-07 14:38:49 +0300108 size_t na, nb;
Anton Blanchard694bf402011-08-24 16:40:17 +1000109
110 /* Prefer a symbol with non zero length */
111 a = syma->end - syma->start;
112 b = symb->end - symb->start;
113 if ((b == 0) && (a > 0))
114 return SYMBOL_A;
115 else if ((a == 0) && (b > 0))
116 return SYMBOL_B;
117
118 /* Prefer a non weak symbol over a weak one */
119 a = syma->binding == STB_WEAK;
120 b = symb->binding == STB_WEAK;
121 if (b && !a)
122 return SYMBOL_A;
123 if (a && !b)
124 return SYMBOL_B;
125
126 /* Prefer a global symbol over a non global one */
127 a = syma->binding == STB_GLOBAL;
128 b = symb->binding == STB_GLOBAL;
129 if (a && !b)
130 return SYMBOL_A;
131 if (b && !a)
132 return SYMBOL_B;
133
134 /* Prefer a symbol with less underscores */
135 a = prefix_underscores_count(syma->name);
136 b = prefix_underscores_count(symb->name);
137 if (b > a)
138 return SYMBOL_A;
139 else if (a > b)
140 return SYMBOL_B;
141
Adrian Hunter34454322013-08-07 14:38:49 +0300142 /* Choose the symbol with the longest name */
143 na = strlen(syma->name);
144 nb = strlen(symb->name);
145 if (na > nb)
Anton Blanchard694bf402011-08-24 16:40:17 +1000146 return SYMBOL_A;
Adrian Hunter34454322013-08-07 14:38:49 +0300147 else if (na < nb)
Anton Blanchard694bf402011-08-24 16:40:17 +1000148 return SYMBOL_B;
Adrian Hunter34454322013-08-07 14:38:49 +0300149
Naveen N. Raofb6d5942015-04-28 17:35:36 +0530150 return arch__choose_best_symbol(syma, symb);
Anton Blanchard694bf402011-08-24 16:40:17 +1000151}
152
Namhyung Kime5a18452012-08-06 13:41:20 +0900153void symbols__fixup_duplicate(struct rb_root *symbols)
Anton Blanchard694bf402011-08-24 16:40:17 +1000154{
155 struct rb_node *nd;
156 struct symbol *curr, *next;
157
Arnaldo Carvalho de Meloc97b40e2016-09-01 10:56:06 -0300158 if (symbol_conf.allow_aliases)
159 return;
160
Anton Blanchard694bf402011-08-24 16:40:17 +1000161 nd = rb_first(symbols);
162
163 while (nd) {
164 curr = rb_entry(nd, struct symbol, rb_node);
165again:
166 nd = rb_next(&curr->rb_node);
167 next = rb_entry(nd, struct symbol, rb_node);
168
169 if (!nd)
170 break;
171
172 if (curr->start != next->start)
173 continue;
174
175 if (choose_best_symbol(curr, next) == SYMBOL_A) {
176 rb_erase(&next->rb_node, symbols);
Chenggang Qind4f74eb2013-10-11 08:27:59 +0800177 symbol__delete(next);
Anton Blanchard694bf402011-08-24 16:40:17 +1000178 goto again;
179 } else {
180 nd = rb_next(&curr->rb_node);
181 rb_erase(&curr->rb_node, symbols);
Chenggang Qind4f74eb2013-10-11 08:27:59 +0800182 symbol__delete(curr);
Anton Blanchard694bf402011-08-24 16:40:17 +1000183 }
184 }
185}
186
Namhyung Kime5a18452012-08-06 13:41:20 +0900187void symbols__fixup_end(struct rb_root *symbols)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300188{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300189 struct rb_node *nd, *prevnd = rb_first(symbols);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300190 struct symbol *curr, *prev;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300191
192 if (prevnd == NULL)
193 return;
194
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300195 curr = rb_entry(prevnd, struct symbol, rb_node);
196
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300197 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300198 prev = curr;
199 curr = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300200
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200201 if (prev->end == prev->start && prev->end != curr->start)
Arnaldo Carvalho de Melo2c241bd2014-10-14 17:19:44 -0300202 prev->end = curr->start;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300203 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300204
205 /* Last entry */
206 if (curr->end == curr->start)
Daniel Borkmanne7ede722017-03-15 22:53:37 +0100207 curr->end = roundup(curr->start, 4096) + 4096;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300208}
209
Namhyung Kime5a18452012-08-06 13:41:20 +0900210void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300211{
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300212 struct maps *maps = &mg->maps[type];
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300213 struct map *next, *curr;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300214
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300215 pthread_rwlock_wrlock(&maps->lock);
216
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300217 curr = maps__first(maps);
218 if (curr == NULL)
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300219 goto out_unlock;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300220
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300221 for (next = map__next(curr); next; next = map__next(curr)) {
222 curr->end = next->start;
223 curr = next;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300224 }
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -0200225
226 /*
227 * We still haven't the actual symbols, so guess the
228 * last map final address.
229 */
Ian Munsie9d1faba2010-11-25 15:12:53 +1100230 curr->end = ~0ULL;
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300231
232out_unlock:
233 pthread_rwlock_unlock(&maps->lock);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300234}
235
Namhyung Kime5a18452012-08-06 13:41:20 +0900236struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300237{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300238 size_t namelen = strlen(name) + 1;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300239 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
240 sizeof(*sym) + namelen));
241 if (sym == NULL)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200242 return NULL;
243
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -0300244 if (symbol_conf.priv_size) {
245 if (symbol_conf.init_annotation) {
246 struct annotation *notes = (void *)sym;
247 pthread_mutex_init(&notes->lock, NULL);
248 }
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300249 sym = ((void *)sym) + symbol_conf.priv_size;
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -0300250 }
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200251
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300252 sym->start = start;
Arnaldo Carvalho de Melo2c241bd2014-10-14 17:19:44 -0300253 sym->end = len ? start + len : start;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300254 sym->binding = binding;
255 sym->namelen = namelen - 1;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200256
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300257 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
258 __func__, name, start, sym->end);
259 memcpy(sym->name, name, namelen);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200260
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300261 return sym;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300262}
263
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300264void symbol__delete(struct symbol *sym)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300265{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300266 free(((void *)sym) - symbol_conf.priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300267}
268
Jiri Olsacdd059d2012-10-27 23:18:32 +0200269void symbols__delete(struct rb_root *symbols)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300270{
271 struct symbol *pos;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300272 struct rb_node *next = rb_first(symbols);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300273
274 while (next) {
275 pos = rb_entry(next, struct symbol, rb_node);
276 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300277 rb_erase(&pos->rb_node, symbols);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200278 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300279 }
280}
281
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300282void __symbols__insert(struct rb_root *symbols, struct symbol *sym, bool kernel)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300283{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300284 struct rb_node **p = &symbols->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300285 struct rb_node *parent = NULL;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000286 const u64 ip = sym->start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300287 struct symbol *s;
288
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300289 if (kernel) {
290 const char *name = sym->name;
291 /*
292 * ppc64 uses function descriptors and appends a '.' to the
293 * start of every instruction address. Remove it.
294 */
295 if (name[0] == '.')
296 name++;
297 sym->idle = symbol__is_idle(name);
298 }
299
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300300 while (*p != NULL) {
301 parent = *p;
302 s = rb_entry(parent, struct symbol, rb_node);
303 if (ip < s->start)
304 p = &(*p)->rb_left;
305 else
306 p = &(*p)->rb_right;
307 }
308 rb_link_node(&sym->rb_node, parent, p);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300309 rb_insert_color(&sym->rb_node, symbols);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300310}
311
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300312void symbols__insert(struct rb_root *symbols, struct symbol *sym)
313{
314 __symbols__insert(symbols, sym, false);
315}
316
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300317static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300318{
319 struct rb_node *n;
320
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300321 if (symbols == NULL)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300322 return NULL;
323
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300324 n = symbols->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300325
326 while (n) {
327 struct symbol *s = rb_entry(n, struct symbol, rb_node);
328
329 if (ip < s->start)
330 n = n->rb_left;
Chris Phlipot9c7b37c2016-05-07 02:16:59 -0700331 else if (ip > s->end || (ip == s->end && ip != s->start))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300332 n = n->rb_right;
333 else
334 return s;
335 }
336
337 return NULL;
338}
339
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300340static struct symbol *symbols__first(struct rb_root *symbols)
341{
342 struct rb_node *n = rb_first(symbols);
343
344 if (n)
345 return rb_entry(n, struct symbol, rb_node);
346
347 return NULL;
348}
349
Adrian Huntercd67f992016-09-23 17:38:38 +0300350static struct symbol *symbols__last(struct rb_root *symbols)
351{
352 struct rb_node *n = rb_last(symbols);
353
354 if (n)
355 return rb_entry(n, struct symbol, rb_node);
356
357 return NULL;
358}
359
Adrian Hunter9c00a812014-07-14 13:02:50 +0300360static struct symbol *symbols__next(struct symbol *sym)
361{
362 struct rb_node *n = rb_next(&sym->rb_node);
363
364 if (n)
365 return rb_entry(n, struct symbol, rb_node);
366
367 return NULL;
368}
369
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300370static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200371{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300372 struct rb_node **p = &symbols->rb_node;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200373 struct rb_node *parent = NULL;
Rabin Vincent02a9d032010-11-23 22:08:18 +0530374 struct symbol_name_rb_node *symn, *s;
375
376 symn = container_of(sym, struct symbol_name_rb_node, sym);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200377
378 while (*p != NULL) {
379 parent = *p;
380 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
381 if (strcmp(sym->name, s->sym.name) < 0)
382 p = &(*p)->rb_left;
383 else
384 p = &(*p)->rb_right;
385 }
386 rb_link_node(&symn->rb_node, parent, p);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300387 rb_insert_color(&symn->rb_node, symbols);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200388}
389
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300390static void symbols__sort_by_name(struct rb_root *symbols,
391 struct rb_root *source)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200392{
393 struct rb_node *nd;
394
395 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
396 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300397 symbols__insert_by_name(symbols, pos);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200398 }
399}
400
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300401static struct symbol *symbols__find_by_name(struct rb_root *symbols,
402 const char *name)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200403{
404 struct rb_node *n;
Martin Liška5bcaaca2015-05-26 11:41:37 -0300405 struct symbol_name_rb_node *s = NULL;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200406
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300407 if (symbols == NULL)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200408 return NULL;
409
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300410 n = symbols->rb_node;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200411
412 while (n) {
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200413 int cmp;
414
415 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
Naveen N. Rao031b84c2015-04-28 17:35:37 +0530416 cmp = arch__compare_symbol_names(name, s->sym.name);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200417
418 if (cmp < 0)
419 n = n->rb_left;
420 else if (cmp > 0)
421 n = n->rb_right;
422 else
Namhyung Kimde480992015-01-16 15:31:28 -0300423 break;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200424 }
425
Namhyung Kimde480992015-01-16 15:31:28 -0300426 if (n == NULL)
427 return NULL;
428
429 /* return first symbol that has same name (if any) */
430 for (n = rb_prev(n); n; n = rb_prev(n)) {
431 struct symbol_name_rb_node *tmp;
432
433 tmp = rb_entry(n, struct symbol_name_rb_node, rb_node);
Naveen N. Rao031b84c2015-04-28 17:35:37 +0530434 if (arch__compare_symbol_names(tmp->sym.name, s->sym.name))
Namhyung Kimde480992015-01-16 15:31:28 -0300435 break;
436
437 s = tmp;
438 }
439
440 return &s->sym;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200441}
442
Arnaldo Carvalho de Meloc0b4dff2015-08-24 13:33:14 -0300443void dso__reset_find_symbol_cache(struct dso *dso)
444{
445 enum map_type type;
446
447 for (type = MAP__FUNCTION; type <= MAP__VARIABLE; ++type) {
448 dso->last_find_result[type].addr = 0;
449 dso->last_find_result[type].symbol = NULL;
450 }
451}
452
Chris Phlipotae93a6c2016-05-10 20:26:46 -0700453void dso__insert_symbol(struct dso *dso, enum map_type type, struct symbol *sym)
454{
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300455 __symbols__insert(&dso->symbols[type], sym, dso->kernel);
Chris Phlipotae93a6c2016-05-10 20:26:46 -0700456
457 /* update the symbol cache if necessary */
458 if (dso->last_find_result[type].addr >= sym->start &&
459 (dso->last_find_result[type].addr < sym->end ||
460 sym->start == sym->end)) {
461 dso->last_find_result[type].symbol = sym;
462 }
463}
464
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300465struct symbol *dso__find_symbol(struct dso *dso,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200466 enum map_type type, u64 addr)
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200467{
Arnaldo Carvalho de Melob685ac22015-07-22 12:52:17 -0300468 if (dso->last_find_result[type].addr != addr) {
469 dso->last_find_result[type].addr = addr;
470 dso->last_find_result[type].symbol = symbols__find(&dso->symbols[type], addr);
471 }
472
473 return dso->last_find_result[type].symbol;
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200474}
475
Adrian Hunter9c00a812014-07-14 13:02:50 +0300476struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300477{
478 return symbols__first(&dso->symbols[type]);
479}
480
Adrian Huntercd67f992016-09-23 17:38:38 +0300481struct symbol *dso__last_symbol(struct dso *dso, enum map_type type)
482{
483 return symbols__last(&dso->symbols[type]);
484}
485
Adrian Hunter9c00a812014-07-14 13:02:50 +0300486struct symbol *dso__next_symbol(struct symbol *sym)
487{
488 return symbols__next(sym);
489}
490
Arnaldo Carvalho de Melo18bd7262015-01-16 15:39:53 -0300491struct symbol *symbol__next_by_name(struct symbol *sym)
492{
493 struct symbol_name_rb_node *s = container_of(sym, struct symbol_name_rb_node, sym);
494 struct rb_node *n = rb_next(&s->rb_node);
495
496 return n ? &rb_entry(n, struct symbol_name_rb_node, rb_node)->sym : NULL;
497}
498
499 /*
500 * Teturns first symbol that matched with @name.
501 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300502struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200503 const char *name)
504{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300505 return symbols__find_by_name(&dso->symbol_names[type], name);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200506}
507
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300508void dso__sort_by_name(struct dso *dso, enum map_type type)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200509{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300510 dso__set_sorted_by_name(dso, type);
511 return symbols__sort_by_name(&dso->symbol_names[type],
512 &dso->symbols[type]);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200513}
514
Adrian Hunter316d70d2013-10-08 11:45:48 +0300515int modules__parse(const char *filename, void *arg,
516 int (*process_module)(void *arg, const char *name,
517 u64 start))
518{
519 char *line = NULL;
520 size_t n;
521 FILE *file;
522 int err = 0;
523
524 file = fopen(filename, "r");
525 if (file == NULL)
526 return -1;
527
528 while (1) {
529 char name[PATH_MAX];
530 u64 start;
531 char *sep;
532 ssize_t line_len;
533
534 line_len = getline(&line, &n, file);
535 if (line_len < 0) {
536 if (feof(file))
537 break;
538 err = -1;
539 goto out;
540 }
541
542 if (!line) {
543 err = -1;
544 goto out;
545 }
546
547 line[--line_len] = '\0'; /* \n */
548
549 sep = strrchr(line, 'x');
550 if (sep == NULL)
551 continue;
552
553 hex2u64(sep + 1, &start);
554
555 sep = strchr(line, ' ');
556 if (sep == NULL)
557 continue;
558
559 *sep = '\0';
560
561 scnprintf(name, sizeof(name), "[%s]", line);
562
563 err = process_module(arg, name, start);
564 if (err)
565 break;
566 }
567out:
568 free(line);
569 fclose(file);
570 return err;
571}
572
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200573struct process_kallsyms_args {
574 struct map *map;
575 struct dso *dso;
576};
577
Arnaldo Carvalho de Meloe7110b92014-08-08 18:00:39 -0300578/*
579 * These are symbols in the kernel image, so make sure that
580 * sym is from a kernel DSO.
581 */
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300582static bool symbol__is_idle(const char *name)
David Ahern82d1deb2013-11-18 13:32:45 -0700583{
584 const char * const idle_symbols[] = {
585 "cpu_idle",
Arnaldo Carvalho de Meloe0336ed2014-08-08 18:02:41 -0300586 "cpu_startup_entry",
David Ahern82d1deb2013-11-18 13:32:45 -0700587 "intel_idle",
588 "default_idle",
589 "native_safe_halt",
590 "enter_idle",
591 "exit_idle",
592 "mwait_idle",
593 "mwait_idle_with_hints",
594 "poll_idle",
595 "ppc64_runlatch_off",
596 "pseries_dedicated_idle_sleep",
597 NULL
598 };
David Ahern82d1deb2013-11-18 13:32:45 -0700599 int i;
600
David Ahern82d1deb2013-11-18 13:32:45 -0700601 for (i = 0; idle_symbols[i]; i++) {
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300602 if (!strcmp(idle_symbols[i], name))
David Ahern82d1deb2013-11-18 13:32:45 -0700603 return true;
604 }
605
606 return false;
607}
608
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200609static int map__process_kallsym_symbol(void *arg, const char *name,
Cody P Schafer82151522012-08-10 15:22:48 -0700610 char type, u64 start)
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200611{
612 struct symbol *sym;
613 struct process_kallsyms_args *a = arg;
614 struct rb_root *root = &a->dso->symbols[a->map->type];
615
616 if (!symbol_type__is_a(type, a->map->type))
617 return 0;
618
Cody P Schafer82151522012-08-10 15:22:48 -0700619 /*
620 * module symbols are not sorted so we add all
621 * symbols, setting length to 0, and rely on
622 * symbols__fixup_end() to fix it up.
623 */
Arnaldo Carvalho de Melo8e947f12015-09-30 12:42:07 -0300624 sym = symbol__new(start, 0, kallsyms2elf_binding(type), name);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200625 if (sym == NULL)
626 return -ENOMEM;
627 /*
628 * We will pass the symbols to the filter later, in
629 * map__split_kallsyms, when we have split the maps per module
630 */
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300631 __symbols__insert(root, sym, !strchr(name, '['));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800632
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200633 return 0;
634}
635
636/*
637 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
638 * so that we can in the next step set the symbol ->end address and then
639 * call kernel_maps__split_kallsyms.
640 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300641static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200642 struct map *map)
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200643{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300644 struct process_kallsyms_args args = { .map = map, .dso = dso, };
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200645 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200646}
647
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300648static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map)
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300649{
Wang Nanba927322015-04-07 08:22:45 +0000650 struct map_groups *kmaps = map__kmaps(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300651 struct map *curr_map;
652 struct symbol *pos;
Adrian Hunter866548d2015-11-06 15:59:29 +0200653 int count = 0;
654 struct rb_root old_root = dso->symbols[map->type];
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300655 struct rb_root *root = &dso->symbols[map->type];
656 struct rb_node *next = rb_first(root);
657
Wang Nanba927322015-04-07 08:22:45 +0000658 if (!kmaps)
659 return -1;
660
Adrian Hunter866548d2015-11-06 15:59:29 +0200661 *root = RB_ROOT;
662
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300663 while (next) {
664 char *module;
665
666 pos = rb_entry(next, struct symbol, rb_node);
667 next = rb_next(&pos->rb_node);
668
Adrian Hunter866548d2015-11-06 15:59:29 +0200669 rb_erase_init(&pos->rb_node, &old_root);
670
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300671 module = strchr(pos->name, '\t');
672 if (module)
673 *module = '\0';
674
675 curr_map = map_groups__find(kmaps, map->type, pos->start);
676
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300677 if (!curr_map) {
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300678 symbol__delete(pos);
Adrian Hunter866548d2015-11-06 15:59:29 +0200679 continue;
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300680 }
Adrian Hunter866548d2015-11-06 15:59:29 +0200681
682 pos->start -= curr_map->start - curr_map->pgoff;
683 if (pos->end)
684 pos->end -= curr_map->start - curr_map->pgoff;
685 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
686 ++count;
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300687 }
688
689 /* Symbols have been adjusted */
690 dso->adjust_symbols = 1;
691
Adrian Hunter866548d2015-11-06 15:59:29 +0200692 return count;
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300693}
694
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300695/*
696 * Split the symbols into maps, making sure there are no overlaps, i.e. the
697 * kernel range is broken in several maps, named [kernel].N, as we don't have
698 * the original ELF section names vmlinux have.
699 */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300700static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300701{
Wang Nanba927322015-04-07 08:22:45 +0000702 struct map_groups *kmaps = map__kmaps(map);
703 struct machine *machine;
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200704 struct map *curr_map = map;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300705 struct symbol *pos;
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300706 int count = 0, moved = 0;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300707 struct rb_root *root = &dso->symbols[map->type];
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200708 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300709 int kernel_range = 0;
710
Wang Nanba927322015-04-07 08:22:45 +0000711 if (!kmaps)
712 return -1;
713
714 machine = kmaps->machine;
715
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300716 while (next) {
717 char *module;
718
719 pos = rb_entry(next, struct symbol, rb_node);
720 next = rb_next(&pos->rb_node);
721
722 module = strchr(pos->name, '\t');
723 if (module) {
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200724 if (!symbol_conf.use_modules)
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200725 goto discard_symbol;
726
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300727 *module++ = '\0';
728
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200729 if (strcmp(curr_map->dso->short_name, module)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800730 if (curr_map != map &&
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300731 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300732 machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800733 /*
734 * We assume all symbols of a module are
735 * continuous in * kallsyms, so curr_map
736 * points to a module and all its
737 * symbols are in its kmap. Mark it as
738 * loaded.
739 */
740 dso__set_loaded(curr_map->dso,
741 curr_map->type);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300742 }
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200743
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800744 curr_map = map_groups__find_by_name(kmaps,
745 map->type, module);
746 if (curr_map == NULL) {
Arnaldo Carvalho de Melo2f519032010-05-17 17:57:59 -0300747 pr_debug("%s/proc/{kallsyms,modules} "
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800748 "inconsistency while looking "
749 "for \"%s\" module!\n",
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300750 machine->root_dir, module);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800751 curr_map = map;
752 goto discard_symbol;
753 }
754
755 if (curr_map->dso->loaded &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300756 !machine__is_default_guest(machine))
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200757 goto discard_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300758 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300759 /*
760 * So that we look just like we get from .ko files,
761 * i.e. not prelinked, relative to map->start.
762 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200763 pos->start = curr_map->map_ip(curr_map, pos->start);
764 pos->end = curr_map->map_ip(curr_map, pos->end);
765 } else if (curr_map != map) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300766 char dso_name[PATH_MAX];
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300767 struct dso *ndso;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300768
Adrian Hunterd9b62ab2014-01-29 16:14:43 +0200769 if (delta) {
770 /* Kernel was relocated at boot time */
771 pos->start -= delta;
772 pos->end -= delta;
773 }
774
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200775 if (count == 0) {
776 curr_map = map;
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300777 goto add_symbol;
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200778 }
779
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300780 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800781 snprintf(dso_name, sizeof(dso_name),
782 "[guest.kernel].%d",
783 kernel_range++);
784 else
785 snprintf(dso_name, sizeof(dso_name),
786 "[kernel].%d",
787 kernel_range++);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300788
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300789 ndso = dso__new(dso_name);
790 if (ndso == NULL)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300791 return -1;
792
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300793 ndso->kernel = dso->kernel;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800794
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300795 curr_map = map__new2(pos->start, ndso, map->type);
Zhang, Yanmin37fe5fc2010-02-25 11:00:51 +0800796 if (curr_map == NULL) {
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300797 dso__put(ndso);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300798 return -1;
799 }
800
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200801 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200802 map_groups__insert(kmaps, curr_map);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300803 ++kernel_range;
Adrian Hunterd9b62ab2014-01-29 16:14:43 +0200804 } else if (delta) {
805 /* Kernel was relocated at boot time */
806 pos->start -= delta;
807 pos->end -= delta;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300808 }
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300809add_symbol:
810 if (curr_map != map) {
811 rb_erase(&pos->rb_node, root);
812 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
813 ++moved;
814 } else
815 ++count;
816
817 continue;
818discard_symbol:
819 rb_erase(&pos->rb_node, root);
820 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300821 }
822
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800823 if (curr_map != map &&
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300824 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300825 machine__is_default_guest(kmaps->machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800826 dso__set_loaded(curr_map->dso, curr_map->type);
827 }
828
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200829 return count + moved;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300830}
831
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300832bool symbol__restricted_filename(const char *filename,
833 const char *restricted_filename)
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -0300834{
835 bool restricted = false;
836
837 if (symbol_conf.kptr_restrict) {
838 char *r = realpath(filename, NULL);
839
840 if (r != NULL) {
841 restricted = strcmp(r, restricted_filename) == 0;
842 free(r);
843 return restricted;
844 }
845 }
846
847 return restricted;
848}
849
Adrian Hunter52afdaf2013-10-09 15:01:11 +0300850struct module_info {
851 struct rb_node rb_node;
852 char *name;
853 u64 start;
854};
855
856static void add_module(struct module_info *mi, struct rb_root *modules)
857{
858 struct rb_node **p = &modules->rb_node;
859 struct rb_node *parent = NULL;
860 struct module_info *m;
861
862 while (*p != NULL) {
863 parent = *p;
864 m = rb_entry(parent, struct module_info, rb_node);
865 if (strcmp(mi->name, m->name) < 0)
866 p = &(*p)->rb_left;
867 else
868 p = &(*p)->rb_right;
869 }
870 rb_link_node(&mi->rb_node, parent, p);
871 rb_insert_color(&mi->rb_node, modules);
872}
873
874static void delete_modules(struct rb_root *modules)
875{
876 struct module_info *mi;
877 struct rb_node *next = rb_first(modules);
878
879 while (next) {
880 mi = rb_entry(next, struct module_info, rb_node);
881 next = rb_next(&mi->rb_node);
882 rb_erase(&mi->rb_node, modules);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300883 zfree(&mi->name);
Adrian Hunter52afdaf2013-10-09 15:01:11 +0300884 free(mi);
885 }
886}
887
888static struct module_info *find_module(const char *name,
889 struct rb_root *modules)
890{
891 struct rb_node *n = modules->rb_node;
892
893 while (n) {
894 struct module_info *m;
895 int cmp;
896
897 m = rb_entry(n, struct module_info, rb_node);
898 cmp = strcmp(name, m->name);
899 if (cmp < 0)
900 n = n->rb_left;
901 else if (cmp > 0)
902 n = n->rb_right;
903 else
904 return m;
905 }
906
907 return NULL;
908}
909
910static int __read_proc_modules(void *arg, const char *name, u64 start)
911{
912 struct rb_root *modules = arg;
913 struct module_info *mi;
914
915 mi = zalloc(sizeof(struct module_info));
916 if (!mi)
917 return -ENOMEM;
918
919 mi->name = strdup(name);
920 mi->start = start;
921
922 if (!mi->name) {
923 free(mi);
924 return -ENOMEM;
925 }
926
927 add_module(mi, modules);
928
929 return 0;
930}
931
932static int read_proc_modules(const char *filename, struct rb_root *modules)
933{
934 if (symbol__restricted_filename(filename, "/proc/modules"))
935 return -1;
936
937 if (modules__parse(filename, modules, __read_proc_modules)) {
938 delete_modules(modules);
939 return -1;
940 }
941
942 return 0;
943}
944
Adrian Hunterfc1b6912013-10-14 16:57:29 +0300945int compare_proc_modules(const char *from, const char *to)
946{
947 struct rb_root from_modules = RB_ROOT;
948 struct rb_root to_modules = RB_ROOT;
949 struct rb_node *from_node, *to_node;
950 struct module_info *from_m, *to_m;
951 int ret = -1;
952
953 if (read_proc_modules(from, &from_modules))
954 return -1;
955
956 if (read_proc_modules(to, &to_modules))
957 goto out_delete_from;
958
959 from_node = rb_first(&from_modules);
960 to_node = rb_first(&to_modules);
961 while (from_node) {
962 if (!to_node)
963 break;
964
965 from_m = rb_entry(from_node, struct module_info, rb_node);
966 to_m = rb_entry(to_node, struct module_info, rb_node);
967
968 if (from_m->start != to_m->start ||
969 strcmp(from_m->name, to_m->name))
970 break;
971
972 from_node = rb_next(from_node);
973 to_node = rb_next(to_node);
974 }
975
976 if (!from_node && !to_node)
977 ret = 0;
978
979 delete_modules(&to_modules);
980out_delete_from:
981 delete_modules(&from_modules);
982
983 return ret;
984}
985
Adrian Hunter52afdaf2013-10-09 15:01:11 +0300986static int do_validate_kcore_modules(const char *filename, struct map *map,
987 struct map_groups *kmaps)
988{
989 struct rb_root modules = RB_ROOT;
990 struct map *old_map;
991 int err;
992
993 err = read_proc_modules(filename, &modules);
994 if (err)
995 return err;
996
997 old_map = map_groups__first(kmaps, map->type);
998 while (old_map) {
999 struct map *next = map_groups__next(old_map);
1000 struct module_info *mi;
1001
1002 if (old_map == map || old_map->start == map->start) {
1003 /* The kernel map */
1004 old_map = next;
1005 continue;
1006 }
1007
1008 /* Module must be in memory at the same address */
1009 mi = find_module(old_map->dso->short_name, &modules);
1010 if (!mi || mi->start != old_map->start) {
1011 err = -EINVAL;
1012 goto out;
1013 }
1014
1015 old_map = next;
1016 }
1017out:
1018 delete_modules(&modules);
1019 return err;
1020}
1021
1022/*
1023 * If kallsyms is referenced by name then we look for filename in the same
1024 * directory.
1025 */
1026static bool filename_from_kallsyms_filename(char *filename,
1027 const char *base_name,
1028 const char *kallsyms_filename)
1029{
1030 char *name;
1031
1032 strcpy(filename, kallsyms_filename);
1033 name = strrchr(filename, '/');
1034 if (!name)
1035 return false;
1036
1037 name += 1;
1038
1039 if (!strcmp(name, "kallsyms")) {
1040 strcpy(name, base_name);
1041 return true;
1042 }
1043
1044 return false;
1045}
1046
1047static int validate_kcore_modules(const char *kallsyms_filename,
1048 struct map *map)
1049{
Wang Nanba927322015-04-07 08:22:45 +00001050 struct map_groups *kmaps = map__kmaps(map);
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001051 char modules_filename[PATH_MAX];
1052
Wang Nanba927322015-04-07 08:22:45 +00001053 if (!kmaps)
1054 return -EINVAL;
1055
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001056 if (!filename_from_kallsyms_filename(modules_filename, "modules",
1057 kallsyms_filename))
1058 return -EINVAL;
1059
1060 if (do_validate_kcore_modules(modules_filename, map, kmaps))
1061 return -EINVAL;
1062
1063 return 0;
1064}
1065
Adrian Huntera00d28c2014-01-29 16:14:41 +02001066static int validate_kcore_addresses(const char *kallsyms_filename,
1067 struct map *map)
1068{
1069 struct kmap *kmap = map__kmap(map);
1070
Wang Nanba927322015-04-07 08:22:45 +00001071 if (!kmap)
1072 return -EINVAL;
1073
Adrian Huntera00d28c2014-01-29 16:14:41 +02001074 if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
1075 u64 start;
1076
1077 start = kallsyms__get_function_start(kallsyms_filename,
1078 kmap->ref_reloc_sym->name);
1079 if (start != kmap->ref_reloc_sym->addr)
1080 return -EINVAL;
1081 }
1082
1083 return validate_kcore_modules(kallsyms_filename, map);
1084}
1085
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001086struct kcore_mapfn_data {
1087 struct dso *dso;
1088 enum map_type type;
1089 struct list_head maps;
1090};
1091
1092static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1093{
1094 struct kcore_mapfn_data *md = data;
1095 struct map *map;
1096
1097 map = map__new2(start, md->dso, md->type);
1098 if (map == NULL)
1099 return -ENOMEM;
1100
1101 map->end = map->start + len;
1102 map->pgoff = pgoff;
1103
1104 list_add(&map->node, &md->maps);
1105
1106 return 0;
1107}
1108
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001109static int dso__load_kcore(struct dso *dso, struct map *map,
1110 const char *kallsyms_filename)
1111{
Wang Nanba927322015-04-07 08:22:45 +00001112 struct map_groups *kmaps = map__kmaps(map);
1113 struct machine *machine;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001114 struct kcore_mapfn_data md;
1115 struct map *old_map, *new_map, *replacement_map = NULL;
1116 bool is_64_bit;
1117 int err, fd;
1118 char kcore_filename[PATH_MAX];
1119 struct symbol *sym;
1120
Wang Nanba927322015-04-07 08:22:45 +00001121 if (!kmaps)
1122 return -EINVAL;
1123
1124 machine = kmaps->machine;
1125
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001126 /* This function requires that the map is the kernel map */
1127 if (map != machine->vmlinux_maps[map->type])
1128 return -EINVAL;
1129
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001130 if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1131 kallsyms_filename))
1132 return -EINVAL;
1133
Adrian Huntera00d28c2014-01-29 16:14:41 +02001134 /* Modules and kernel must be present at their original addresses */
1135 if (validate_kcore_addresses(kallsyms_filename, map))
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001136 return -EINVAL;
1137
1138 md.dso = dso;
1139 md.type = map->type;
1140 INIT_LIST_HEAD(&md.maps);
1141
1142 fd = open(kcore_filename, O_RDONLY);
Li Zhang36c8bb52015-06-19 16:57:33 +08001143 if (fd < 0) {
Adrian Hunter133de942015-08-20 13:07:40 +03001144 pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n",
1145 kcore_filename);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001146 return -EINVAL;
Li Zhang36c8bb52015-06-19 16:57:33 +08001147 }
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001148
1149 /* Read new maps into temporary lists */
1150 err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
1151 &is_64_bit);
1152 if (err)
1153 goto out_err;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +03001154 dso->is_64_bit = is_64_bit;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001155
1156 if (list_empty(&md.maps)) {
1157 err = -EINVAL;
1158 goto out_err;
1159 }
1160
1161 /* Remove old maps */
1162 old_map = map_groups__first(kmaps, map->type);
1163 while (old_map) {
1164 struct map *next = map_groups__next(old_map);
1165
1166 if (old_map != map)
1167 map_groups__remove(kmaps, old_map);
1168 old_map = next;
1169 }
1170
1171 /* Find the kernel map using the first symbol */
1172 sym = dso__first_symbol(dso, map->type);
1173 list_for_each_entry(new_map, &md.maps, node) {
1174 if (sym && sym->start >= new_map->start &&
1175 sym->start < new_map->end) {
1176 replacement_map = new_map;
1177 break;
1178 }
1179 }
1180
1181 if (!replacement_map)
1182 replacement_map = list_entry(md.maps.next, struct map, node);
1183
1184 /* Add new maps */
1185 while (!list_empty(&md.maps)) {
1186 new_map = list_entry(md.maps.next, struct map, node);
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -03001187 list_del_init(&new_map->node);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001188 if (new_map == replacement_map) {
1189 map->start = new_map->start;
1190 map->end = new_map->end;
1191 map->pgoff = new_map->pgoff;
1192 map->map_ip = new_map->map_ip;
1193 map->unmap_ip = new_map->unmap_ip;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001194 /* Ensure maps are correctly ordered */
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001195 map__get(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001196 map_groups__remove(kmaps, map);
1197 map_groups__insert(kmaps, map);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001198 map__put(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001199 } else {
1200 map_groups__insert(kmaps, new_map);
1201 }
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001202
1203 map__put(new_map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001204 }
1205
1206 /*
1207 * Set the data type and long name so that kcore can be read via
1208 * dso__data_read_addr().
1209 */
1210 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001211 dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001212 else
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001213 dso->binary_type = DSO_BINARY_TYPE__KCORE;
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001214 dso__set_long_name(dso, strdup(kcore_filename), true);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001215
1216 close(fd);
1217
1218 if (map->type == MAP__FUNCTION)
1219 pr_debug("Using %s for kernel object code\n", kcore_filename);
1220 else
1221 pr_debug("Using %s for kernel data\n", kcore_filename);
1222
1223 return 0;
1224
1225out_err:
1226 while (!list_empty(&md.maps)) {
1227 map = list_entry(md.maps.next, struct map, node);
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -03001228 list_del_init(&map->node);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001229 map__put(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001230 }
1231 close(fd);
1232 return -EINVAL;
1233}
1234
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001235/*
1236 * If the kernel is relocated at boot time, kallsyms won't match. Compute the
1237 * delta based on the relocation reference symbol.
1238 */
1239static int kallsyms__delta(struct map *map, const char *filename, u64 *delta)
1240{
1241 struct kmap *kmap = map__kmap(map);
1242 u64 addr;
1243
Wang Nanba927322015-04-07 08:22:45 +00001244 if (!kmap)
1245 return -1;
1246
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001247 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1248 return 0;
1249
1250 addr = kallsyms__get_function_start(filename,
1251 kmap->ref_reloc_sym->name);
1252 if (!addr)
1253 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 Melob7cece72010-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 Melob7cece72010-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}