blob: 21c97cc41cfc74ed06af89f1ec84dd8739d19523 [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"
Krister Johansen843ff372017-07-05 18:48:08 -070021#include "namespaces.h"
Namhyung Kim0a7e6d12014-08-12 15:40:45 +090022#include "header.h"
Arnaldo Carvalho de Melo9a3993d2017-04-18 11:33:48 -030023#include "path.h"
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030024#include "sane_ctype.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030025
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030026#include <elf.h>
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -020027#include <limits.h>
Arnaldo Carvalho de Meloc506c962013-12-11 09:15:00 -030028#include <symbol/kallsyms.h>
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030029#include <sys/utsname.h>
Peter Zijlstra2cdbc462009-08-05 14:05:16 +020030
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -030031static int dso__load_kernel_sym(struct dso *dso, struct map *map);
32static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map);
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -030033static bool symbol__is_idle(const char *name);
34
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030035int vmlinux_path__nr_entries;
36char **vmlinux_path;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030037
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -020038struct symbol_conf symbol_conf = {
Namhyung Kime511db52013-12-24 16:19:25 +090039 .use_modules = true,
40 .try_vmlinux_path = true,
41 .annotate_src = true,
42 .demangle = true,
Avi Kivity763122a2014-09-13 07:15:05 +030043 .demangle_kernel = false,
Namhyung Kime511db52013-12-24 16:19:25 +090044 .cumulate_callchain = true,
Jiri Olsac8302362014-06-27 18:26:58 +020045 .show_hist_headers = true,
Namhyung Kime511db52013-12-24 16:19:25 +090046 .symfs = "",
Namhyung Kim1e9abf82015-11-29 23:24:17 +090047 .event_group = true,
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020048};
49
Jiri Olsa44f24cb2012-07-22 14:14:32 +020050static enum dso_binary_type binary_type_symtab[] = {
51 DSO_BINARY_TYPE__KALLSYMS,
52 DSO_BINARY_TYPE__GUEST_KALLSYMS,
53 DSO_BINARY_TYPE__JAVA_JIT,
54 DSO_BINARY_TYPE__DEBUGLINK,
55 DSO_BINARY_TYPE__BUILD_ID_CACHE,
56 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
57 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
58 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
59 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
60 DSO_BINARY_TYPE__GUEST_KMODULE,
Namhyung Kimc00c48f2014-11-04 10:14:27 +090061 DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
Jiri Olsa44f24cb2012-07-22 14:14:32 +020062 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
Namhyung Kimc00c48f2014-11-04 10:14:27 +090063 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020064 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
Jiri Olsa44f24cb2012-07-22 14:14:32 +020065 DSO_BINARY_TYPE__NOT_FOUND,
66};
67
Jiri Olsa028df762012-08-01 14:47:57 +020068#define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
Jiri Olsa44f24cb2012-07-22 14:14:32 +020069
Arnaldo Carvalho de Melo36a3e642010-01-04 16:19:27 -020070bool symbol_type__is_a(char symbol_type, enum map_type map_type)
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020071{
Anton Blanchard31877902011-08-24 16:40:16 +100072 symbol_type = toupper(symbol_type);
73
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020074 switch (map_type) {
75 case MAP__FUNCTION:
76 return symbol_type == 'T' || symbol_type == 'W';
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -020077 case MAP__VARIABLE:
Anton Blanchard31877902011-08-24 16:40:16 +100078 return symbol_type == 'D';
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020079 default:
80 return false;
81 }
82}
83
Anton Blanchard694bf402011-08-24 16:40:17 +100084static int prefix_underscores_count(const char *str)
85{
86 const char *tail = str;
87
88 while (*tail == '_')
89 tail++;
90
91 return tail - str;
92}
93
Paul Clarked80406452017-04-25 13:15:49 -050094int __weak arch__compare_symbol_names(const char *namea, const char *nameb)
95{
96 return strcmp(namea, nameb);
97}
98
99int __weak arch__compare_symbol_names_n(const char *namea, const char *nameb,
100 unsigned int n)
101{
102 return strncmp(namea, nameb, n);
103}
104
Naveen N. Raofb6d5942015-04-28 17:35:36 +0530105int __weak arch__choose_best_symbol(struct symbol *syma,
106 struct symbol *symb __maybe_unused)
107{
108 /* Avoid "SyS" kernel syscall aliases */
109 if (strlen(syma->name) >= 3 && !strncmp(syma->name, "SyS", 3))
110 return SYMBOL_B;
111 if (strlen(syma->name) >= 10 && !strncmp(syma->name, "compat_SyS", 10))
112 return SYMBOL_B;
113
114 return SYMBOL_A;
115}
Anton Blanchard694bf402011-08-24 16:40:17 +1000116
117static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
118{
119 s64 a;
120 s64 b;
Adrian Hunter34454322013-08-07 14:38:49 +0300121 size_t na, nb;
Anton Blanchard694bf402011-08-24 16:40:17 +1000122
123 /* Prefer a symbol with non zero length */
124 a = syma->end - syma->start;
125 b = symb->end - symb->start;
126 if ((b == 0) && (a > 0))
127 return SYMBOL_A;
128 else if ((a == 0) && (b > 0))
129 return SYMBOL_B;
130
131 /* Prefer a non weak symbol over a weak one */
132 a = syma->binding == STB_WEAK;
133 b = symb->binding == STB_WEAK;
134 if (b && !a)
135 return SYMBOL_A;
136 if (a && !b)
137 return SYMBOL_B;
138
139 /* Prefer a global symbol over a non global one */
140 a = syma->binding == STB_GLOBAL;
141 b = symb->binding == STB_GLOBAL;
142 if (a && !b)
143 return SYMBOL_A;
144 if (b && !a)
145 return SYMBOL_B;
146
147 /* Prefer a symbol with less underscores */
148 a = prefix_underscores_count(syma->name);
149 b = prefix_underscores_count(symb->name);
150 if (b > a)
151 return SYMBOL_A;
152 else if (a > b)
153 return SYMBOL_B;
154
Adrian Hunter34454322013-08-07 14:38:49 +0300155 /* Choose the symbol with the longest name */
156 na = strlen(syma->name);
157 nb = strlen(symb->name);
158 if (na > nb)
Anton Blanchard694bf402011-08-24 16:40:17 +1000159 return SYMBOL_A;
Adrian Hunter34454322013-08-07 14:38:49 +0300160 else if (na < nb)
Anton Blanchard694bf402011-08-24 16:40:17 +1000161 return SYMBOL_B;
Adrian Hunter34454322013-08-07 14:38:49 +0300162
Naveen N. Raofb6d5942015-04-28 17:35:36 +0530163 return arch__choose_best_symbol(syma, symb);
Anton Blanchard694bf402011-08-24 16:40:17 +1000164}
165
Namhyung Kime5a18452012-08-06 13:41:20 +0900166void symbols__fixup_duplicate(struct rb_root *symbols)
Anton Blanchard694bf402011-08-24 16:40:17 +1000167{
168 struct rb_node *nd;
169 struct symbol *curr, *next;
170
Arnaldo Carvalho de Meloc97b40e2016-09-01 10:56:06 -0300171 if (symbol_conf.allow_aliases)
172 return;
173
Anton Blanchard694bf402011-08-24 16:40:17 +1000174 nd = rb_first(symbols);
175
176 while (nd) {
177 curr = rb_entry(nd, struct symbol, rb_node);
178again:
179 nd = rb_next(&curr->rb_node);
180 next = rb_entry(nd, struct symbol, rb_node);
181
182 if (!nd)
183 break;
184
185 if (curr->start != next->start)
186 continue;
187
188 if (choose_best_symbol(curr, next) == SYMBOL_A) {
189 rb_erase(&next->rb_node, symbols);
Chenggang Qind4f74eb2013-10-11 08:27:59 +0800190 symbol__delete(next);
Anton Blanchard694bf402011-08-24 16:40:17 +1000191 goto again;
192 } else {
193 nd = rb_next(&curr->rb_node);
194 rb_erase(&curr->rb_node, symbols);
Chenggang Qind4f74eb2013-10-11 08:27:59 +0800195 symbol__delete(curr);
Anton Blanchard694bf402011-08-24 16:40:17 +1000196 }
197 }
198}
199
Namhyung Kime5a18452012-08-06 13:41:20 +0900200void symbols__fixup_end(struct rb_root *symbols)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300201{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300202 struct rb_node *nd, *prevnd = rb_first(symbols);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300203 struct symbol *curr, *prev;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300204
205 if (prevnd == NULL)
206 return;
207
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300208 curr = rb_entry(prevnd, struct symbol, rb_node);
209
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300210 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300211 prev = curr;
212 curr = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300213
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200214 if (prev->end == prev->start && prev->end != curr->start)
Arnaldo Carvalho de Melo2c241bd2014-10-14 17:19:44 -0300215 prev->end = curr->start;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300216 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300217
218 /* Last entry */
219 if (curr->end == curr->start)
Daniel Borkmanne7ede722017-03-15 22:53:37 +0100220 curr->end = roundup(curr->start, 4096) + 4096;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300221}
222
Namhyung Kime5a18452012-08-06 13:41:20 +0900223void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300224{
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300225 struct maps *maps = &mg->maps[type];
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300226 struct map *next, *curr;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300227
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300228 pthread_rwlock_wrlock(&maps->lock);
229
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300230 curr = maps__first(maps);
231 if (curr == NULL)
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300232 goto out_unlock;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300233
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300234 for (next = map__next(curr); next; next = map__next(curr)) {
235 curr->end = next->start;
236 curr = next;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300237 }
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -0200238
239 /*
240 * We still haven't the actual symbols, so guess the
241 * last map final address.
242 */
Ian Munsie9d1faba2010-11-25 15:12:53 +1100243 curr->end = ~0ULL;
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300244
245out_unlock:
246 pthread_rwlock_unlock(&maps->lock);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300247}
248
Namhyung Kime5a18452012-08-06 13:41:20 +0900249struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300250{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300251 size_t namelen = strlen(name) + 1;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300252 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
253 sizeof(*sym) + namelen));
254 if (sym == NULL)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200255 return NULL;
256
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -0300257 if (symbol_conf.priv_size) {
258 if (symbol_conf.init_annotation) {
259 struct annotation *notes = (void *)sym;
260 pthread_mutex_init(&notes->lock, NULL);
261 }
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300262 sym = ((void *)sym) + symbol_conf.priv_size;
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -0300263 }
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200264
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300265 sym->start = start;
Arnaldo Carvalho de Melo2c241bd2014-10-14 17:19:44 -0300266 sym->end = len ? start + len : start;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300267 sym->binding = binding;
268 sym->namelen = namelen - 1;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200269
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300270 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
271 __func__, name, start, sym->end);
272 memcpy(sym->name, name, namelen);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200273
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300274 return sym;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300275}
276
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300277void symbol__delete(struct symbol *sym)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300278{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300279 free(((void *)sym) - symbol_conf.priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300280}
281
Jiri Olsacdd059d2012-10-27 23:18:32 +0200282void symbols__delete(struct rb_root *symbols)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300283{
284 struct symbol *pos;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300285 struct rb_node *next = rb_first(symbols);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300286
287 while (next) {
288 pos = rb_entry(next, struct symbol, rb_node);
289 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300290 rb_erase(&pos->rb_node, symbols);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200291 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300292 }
293}
294
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300295void __symbols__insert(struct rb_root *symbols, struct symbol *sym, bool kernel)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300296{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300297 struct rb_node **p = &symbols->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300298 struct rb_node *parent = NULL;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000299 const u64 ip = sym->start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300300 struct symbol *s;
301
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300302 if (kernel) {
303 const char *name = sym->name;
304 /*
305 * ppc64 uses function descriptors and appends a '.' to the
306 * start of every instruction address. Remove it.
307 */
308 if (name[0] == '.')
309 name++;
310 sym->idle = symbol__is_idle(name);
311 }
312
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300313 while (*p != NULL) {
314 parent = *p;
315 s = rb_entry(parent, struct symbol, rb_node);
316 if (ip < s->start)
317 p = &(*p)->rb_left;
318 else
319 p = &(*p)->rb_right;
320 }
321 rb_link_node(&sym->rb_node, parent, p);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300322 rb_insert_color(&sym->rb_node, symbols);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300323}
324
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300325void symbols__insert(struct rb_root *symbols, struct symbol *sym)
326{
327 __symbols__insert(symbols, sym, false);
328}
329
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300330static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300331{
332 struct rb_node *n;
333
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300334 if (symbols == NULL)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300335 return NULL;
336
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300337 n = symbols->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300338
339 while (n) {
340 struct symbol *s = rb_entry(n, struct symbol, rb_node);
341
342 if (ip < s->start)
343 n = n->rb_left;
Chris Phlipot9c7b37c2016-05-07 02:16:59 -0700344 else if (ip > s->end || (ip == s->end && ip != s->start))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300345 n = n->rb_right;
346 else
347 return s;
348 }
349
350 return NULL;
351}
352
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300353static struct symbol *symbols__first(struct rb_root *symbols)
354{
355 struct rb_node *n = rb_first(symbols);
356
357 if (n)
358 return rb_entry(n, struct symbol, rb_node);
359
360 return NULL;
361}
362
Adrian Huntercd67f992016-09-23 17:38:38 +0300363static struct symbol *symbols__last(struct rb_root *symbols)
364{
365 struct rb_node *n = rb_last(symbols);
366
367 if (n)
368 return rb_entry(n, struct symbol, rb_node);
369
370 return NULL;
371}
372
Adrian Hunter9c00a812014-07-14 13:02:50 +0300373static struct symbol *symbols__next(struct symbol *sym)
374{
375 struct rb_node *n = rb_next(&sym->rb_node);
376
377 if (n)
378 return rb_entry(n, struct symbol, rb_node);
379
380 return NULL;
381}
382
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300383static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200384{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300385 struct rb_node **p = &symbols->rb_node;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200386 struct rb_node *parent = NULL;
Rabin Vincent02a9d032010-11-23 22:08:18 +0530387 struct symbol_name_rb_node *symn, *s;
388
389 symn = container_of(sym, struct symbol_name_rb_node, sym);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200390
391 while (*p != NULL) {
392 parent = *p;
393 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
394 if (strcmp(sym->name, s->sym.name) < 0)
395 p = &(*p)->rb_left;
396 else
397 p = &(*p)->rb_right;
398 }
399 rb_link_node(&symn->rb_node, parent, p);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300400 rb_insert_color(&symn->rb_node, symbols);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200401}
402
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300403static void symbols__sort_by_name(struct rb_root *symbols,
404 struct rb_root *source)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200405{
406 struct rb_node *nd;
407
408 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
409 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300410 symbols__insert_by_name(symbols, pos);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200411 }
412}
413
Paul Clarked80406452017-04-25 13:15:49 -0500414int symbol__match_symbol_name(const char *name, const char *str,
415 enum symbol_tag_include includes)
416{
417 const char *versioning;
418
419 if (includes == SYMBOL_TAG_INCLUDE__DEFAULT_ONLY &&
420 (versioning = strstr(name, "@@"))) {
421 int len = strlen(str);
422
423 if (len < versioning - name)
424 len = versioning - name;
425
426 return arch__compare_symbol_names_n(name, str, len);
427 } else
428 return arch__compare_symbol_names(name, str);
429}
430
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300431static struct symbol *symbols__find_by_name(struct rb_root *symbols,
Paul Clarked80406452017-04-25 13:15:49 -0500432 const char *name,
433 enum symbol_tag_include includes)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200434{
435 struct rb_node *n;
Martin Liška5bcaaca2015-05-26 11:41:37 -0300436 struct symbol_name_rb_node *s = NULL;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200437
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300438 if (symbols == NULL)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200439 return NULL;
440
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300441 n = symbols->rb_node;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200442
443 while (n) {
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200444 int cmp;
445
446 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
Paul Clarked80406452017-04-25 13:15:49 -0500447 cmp = symbol__match_symbol_name(s->sym.name, name, includes);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200448
Paul Clarked80406452017-04-25 13:15:49 -0500449 if (cmp > 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200450 n = n->rb_left;
Paul Clarked80406452017-04-25 13:15:49 -0500451 else if (cmp < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200452 n = n->rb_right;
453 else
Namhyung Kimde480992015-01-16 15:31:28 -0300454 break;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200455 }
456
Namhyung Kimde480992015-01-16 15:31:28 -0300457 if (n == NULL)
458 return NULL;
459
Paul Clarked80406452017-04-25 13:15:49 -0500460 if (includes != SYMBOL_TAG_INCLUDE__DEFAULT_ONLY)
461 /* return first symbol that has same name (if any) */
462 for (n = rb_prev(n); n; n = rb_prev(n)) {
463 struct symbol_name_rb_node *tmp;
Namhyung Kimde480992015-01-16 15:31:28 -0300464
Paul Clarked80406452017-04-25 13:15:49 -0500465 tmp = rb_entry(n, struct symbol_name_rb_node, rb_node);
466 if (arch__compare_symbol_names(tmp->sym.name, s->sym.name))
467 break;
Namhyung Kimde480992015-01-16 15:31:28 -0300468
Paul Clarked80406452017-04-25 13:15:49 -0500469 s = tmp;
470 }
Namhyung Kimde480992015-01-16 15:31:28 -0300471
472 return &s->sym;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200473}
474
Arnaldo Carvalho de Meloc0b4dff2015-08-24 13:33:14 -0300475void dso__reset_find_symbol_cache(struct dso *dso)
476{
477 enum map_type type;
478
479 for (type = MAP__FUNCTION; type <= MAP__VARIABLE; ++type) {
480 dso->last_find_result[type].addr = 0;
481 dso->last_find_result[type].symbol = NULL;
482 }
483}
484
Chris Phlipotae93a6c2016-05-10 20:26:46 -0700485void dso__insert_symbol(struct dso *dso, enum map_type type, struct symbol *sym)
486{
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300487 __symbols__insert(&dso->symbols[type], sym, dso->kernel);
Chris Phlipotae93a6c2016-05-10 20:26:46 -0700488
489 /* update the symbol cache if necessary */
490 if (dso->last_find_result[type].addr >= sym->start &&
491 (dso->last_find_result[type].addr < sym->end ||
492 sym->start == sym->end)) {
493 dso->last_find_result[type].symbol = sym;
494 }
495}
496
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300497struct symbol *dso__find_symbol(struct dso *dso,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200498 enum map_type type, u64 addr)
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200499{
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300500 if (dso->last_find_result[type].addr != addr || dso->last_find_result[type].symbol == NULL) {
Arnaldo Carvalho de Melob685ac22015-07-22 12:52:17 -0300501 dso->last_find_result[type].addr = addr;
502 dso->last_find_result[type].symbol = symbols__find(&dso->symbols[type], addr);
503 }
504
505 return dso->last_find_result[type].symbol;
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200506}
507
Adrian Hunter9c00a812014-07-14 13:02:50 +0300508struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300509{
510 return symbols__first(&dso->symbols[type]);
511}
512
Adrian Huntercd67f992016-09-23 17:38:38 +0300513struct symbol *dso__last_symbol(struct dso *dso, enum map_type type)
514{
515 return symbols__last(&dso->symbols[type]);
516}
517
Adrian Hunter9c00a812014-07-14 13:02:50 +0300518struct symbol *dso__next_symbol(struct symbol *sym)
519{
520 return symbols__next(sym);
521}
522
Arnaldo Carvalho de Melo18bd7262015-01-16 15:39:53 -0300523struct symbol *symbol__next_by_name(struct symbol *sym)
524{
525 struct symbol_name_rb_node *s = container_of(sym, struct symbol_name_rb_node, sym);
526 struct rb_node *n = rb_next(&s->rb_node);
527
528 return n ? &rb_entry(n, struct symbol_name_rb_node, rb_node)->sym : NULL;
529}
530
531 /*
532 * Teturns first symbol that matched with @name.
533 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300534struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200535 const char *name)
536{
Paul Clarked80406452017-04-25 13:15:49 -0500537 struct symbol *s = symbols__find_by_name(&dso->symbol_names[type], name,
538 SYMBOL_TAG_INCLUDE__NONE);
539 if (!s)
540 s = symbols__find_by_name(&dso->symbol_names[type], name,
541 SYMBOL_TAG_INCLUDE__DEFAULT_ONLY);
542 return s;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200543}
544
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300545void dso__sort_by_name(struct dso *dso, enum map_type type)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200546{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300547 dso__set_sorted_by_name(dso, type);
548 return symbols__sort_by_name(&dso->symbol_names[type],
549 &dso->symbols[type]);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200550}
551
Adrian Hunter316d70d2013-10-08 11:45:48 +0300552int modules__parse(const char *filename, void *arg,
553 int (*process_module)(void *arg, const char *name,
554 u64 start))
555{
556 char *line = NULL;
557 size_t n;
558 FILE *file;
559 int err = 0;
560
561 file = fopen(filename, "r");
562 if (file == NULL)
563 return -1;
564
565 while (1) {
566 char name[PATH_MAX];
567 u64 start;
568 char *sep;
569 ssize_t line_len;
570
571 line_len = getline(&line, &n, file);
572 if (line_len < 0) {
573 if (feof(file))
574 break;
575 err = -1;
576 goto out;
577 }
578
579 if (!line) {
580 err = -1;
581 goto out;
582 }
583
584 line[--line_len] = '\0'; /* \n */
585
586 sep = strrchr(line, 'x');
587 if (sep == NULL)
588 continue;
589
590 hex2u64(sep + 1, &start);
591
592 sep = strchr(line, ' ');
593 if (sep == NULL)
594 continue;
595
596 *sep = '\0';
597
598 scnprintf(name, sizeof(name), "[%s]", line);
599
600 err = process_module(arg, name, start);
601 if (err)
602 break;
603 }
604out:
605 free(line);
606 fclose(file);
607 return err;
608}
609
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200610struct process_kallsyms_args {
611 struct map *map;
612 struct dso *dso;
613};
614
Arnaldo Carvalho de Meloe7110b92014-08-08 18:00:39 -0300615/*
616 * These are symbols in the kernel image, so make sure that
617 * sym is from a kernel DSO.
618 */
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300619static bool symbol__is_idle(const char *name)
David Ahern82d1deb2013-11-18 13:32:45 -0700620{
621 const char * const idle_symbols[] = {
622 "cpu_idle",
Arnaldo Carvalho de Meloe0336ed2014-08-08 18:02:41 -0300623 "cpu_startup_entry",
David Ahern82d1deb2013-11-18 13:32:45 -0700624 "intel_idle",
625 "default_idle",
626 "native_safe_halt",
627 "enter_idle",
628 "exit_idle",
629 "mwait_idle",
630 "mwait_idle_with_hints",
631 "poll_idle",
632 "ppc64_runlatch_off",
633 "pseries_dedicated_idle_sleep",
634 NULL
635 };
David Ahern82d1deb2013-11-18 13:32:45 -0700636 int i;
637
David Ahern82d1deb2013-11-18 13:32:45 -0700638 for (i = 0; idle_symbols[i]; i++) {
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300639 if (!strcmp(idle_symbols[i], name))
David Ahern82d1deb2013-11-18 13:32:45 -0700640 return true;
641 }
642
643 return false;
644}
645
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200646static int map__process_kallsym_symbol(void *arg, const char *name,
Cody P Schafer82151522012-08-10 15:22:48 -0700647 char type, u64 start)
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200648{
649 struct symbol *sym;
650 struct process_kallsyms_args *a = arg;
651 struct rb_root *root = &a->dso->symbols[a->map->type];
652
653 if (!symbol_type__is_a(type, a->map->type))
654 return 0;
655
Cody P Schafer82151522012-08-10 15:22:48 -0700656 /*
657 * module symbols are not sorted so we add all
658 * symbols, setting length to 0, and rely on
659 * symbols__fixup_end() to fix it up.
660 */
Arnaldo Carvalho de Melo8e947f12015-09-30 12:42:07 -0300661 sym = symbol__new(start, 0, kallsyms2elf_binding(type), name);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200662 if (sym == NULL)
663 return -ENOMEM;
664 /*
665 * We will pass the symbols to the filter later, in
666 * map__split_kallsyms, when we have split the maps per module
667 */
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300668 __symbols__insert(root, sym, !strchr(name, '['));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800669
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200670 return 0;
671}
672
673/*
674 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
675 * so that we can in the next step set the symbol ->end address and then
676 * call kernel_maps__split_kallsyms.
677 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300678static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200679 struct map *map)
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200680{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300681 struct process_kallsyms_args args = { .map = map, .dso = dso, };
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200682 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200683}
684
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300685static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map)
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300686{
Wang Nanba927322015-04-07 08:22:45 +0000687 struct map_groups *kmaps = map__kmaps(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300688 struct map *curr_map;
689 struct symbol *pos;
Adrian Hunter866548d2015-11-06 15:59:29 +0200690 int count = 0;
691 struct rb_root old_root = dso->symbols[map->type];
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300692 struct rb_root *root = &dso->symbols[map->type];
693 struct rb_node *next = rb_first(root);
694
Wang Nanba927322015-04-07 08:22:45 +0000695 if (!kmaps)
696 return -1;
697
Adrian Hunter866548d2015-11-06 15:59:29 +0200698 *root = RB_ROOT;
699
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300700 while (next) {
701 char *module;
702
703 pos = rb_entry(next, struct symbol, rb_node);
704 next = rb_next(&pos->rb_node);
705
Adrian Hunter866548d2015-11-06 15:59:29 +0200706 rb_erase_init(&pos->rb_node, &old_root);
707
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300708 module = strchr(pos->name, '\t');
709 if (module)
710 *module = '\0';
711
712 curr_map = map_groups__find(kmaps, map->type, pos->start);
713
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300714 if (!curr_map) {
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300715 symbol__delete(pos);
Adrian Hunter866548d2015-11-06 15:59:29 +0200716 continue;
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300717 }
Adrian Hunter866548d2015-11-06 15:59:29 +0200718
719 pos->start -= curr_map->start - curr_map->pgoff;
720 if (pos->end)
721 pos->end -= curr_map->start - curr_map->pgoff;
722 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
723 ++count;
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300724 }
725
726 /* Symbols have been adjusted */
727 dso->adjust_symbols = 1;
728
Adrian Hunter866548d2015-11-06 15:59:29 +0200729 return count;
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300730}
731
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300732/*
733 * Split the symbols into maps, making sure there are no overlaps, i.e. the
734 * kernel range is broken in several maps, named [kernel].N, as we don't have
735 * the original ELF section names vmlinux have.
736 */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300737static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300738{
Wang Nanba927322015-04-07 08:22:45 +0000739 struct map_groups *kmaps = map__kmaps(map);
740 struct machine *machine;
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200741 struct map *curr_map = map;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300742 struct symbol *pos;
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300743 int count = 0, moved = 0;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300744 struct rb_root *root = &dso->symbols[map->type];
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200745 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300746 int kernel_range = 0;
747
Wang Nanba927322015-04-07 08:22:45 +0000748 if (!kmaps)
749 return -1;
750
751 machine = kmaps->machine;
752
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300753 while (next) {
754 char *module;
755
756 pos = rb_entry(next, struct symbol, rb_node);
757 next = rb_next(&pos->rb_node);
758
759 module = strchr(pos->name, '\t');
760 if (module) {
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200761 if (!symbol_conf.use_modules)
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200762 goto discard_symbol;
763
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300764 *module++ = '\0';
765
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200766 if (strcmp(curr_map->dso->short_name, module)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800767 if (curr_map != map &&
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300768 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300769 machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800770 /*
771 * We assume all symbols of a module are
772 * continuous in * kallsyms, so curr_map
773 * points to a module and all its
774 * symbols are in its kmap. Mark it as
775 * loaded.
776 */
777 dso__set_loaded(curr_map->dso,
778 curr_map->type);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300779 }
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200780
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800781 curr_map = map_groups__find_by_name(kmaps,
782 map->type, module);
783 if (curr_map == NULL) {
Arnaldo Carvalho de Melo2f519032010-05-17 17:57:59 -0300784 pr_debug("%s/proc/{kallsyms,modules} "
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800785 "inconsistency while looking "
786 "for \"%s\" module!\n",
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300787 machine->root_dir, module);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800788 curr_map = map;
789 goto discard_symbol;
790 }
791
792 if (curr_map->dso->loaded &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300793 !machine__is_default_guest(machine))
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200794 goto discard_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300795 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300796 /*
797 * So that we look just like we get from .ko files,
798 * i.e. not prelinked, relative to map->start.
799 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200800 pos->start = curr_map->map_ip(curr_map, pos->start);
801 pos->end = curr_map->map_ip(curr_map, pos->end);
802 } else if (curr_map != map) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300803 char dso_name[PATH_MAX];
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300804 struct dso *ndso;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300805
Adrian Hunterd9b62ab2014-01-29 16:14:43 +0200806 if (delta) {
807 /* Kernel was relocated at boot time */
808 pos->start -= delta;
809 pos->end -= delta;
810 }
811
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200812 if (count == 0) {
813 curr_map = map;
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300814 goto add_symbol;
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200815 }
816
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300817 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800818 snprintf(dso_name, sizeof(dso_name),
819 "[guest.kernel].%d",
820 kernel_range++);
821 else
822 snprintf(dso_name, sizeof(dso_name),
823 "[kernel].%d",
824 kernel_range++);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300825
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300826 ndso = dso__new(dso_name);
827 if (ndso == NULL)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300828 return -1;
829
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300830 ndso->kernel = dso->kernel;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800831
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300832 curr_map = map__new2(pos->start, ndso, map->type);
Zhang, Yanmin37fe5fc2010-02-25 11:00:51 +0800833 if (curr_map == NULL) {
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300834 dso__put(ndso);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300835 return -1;
836 }
837
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200838 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200839 map_groups__insert(kmaps, curr_map);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300840 ++kernel_range;
Adrian Hunterd9b62ab2014-01-29 16:14:43 +0200841 } else if (delta) {
842 /* Kernel was relocated at boot time */
843 pos->start -= delta;
844 pos->end -= delta;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300845 }
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300846add_symbol:
847 if (curr_map != map) {
848 rb_erase(&pos->rb_node, root);
849 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
850 ++moved;
851 } else
852 ++count;
853
854 continue;
855discard_symbol:
856 rb_erase(&pos->rb_node, root);
857 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300858 }
859
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800860 if (curr_map != map &&
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300861 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300862 machine__is_default_guest(kmaps->machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800863 dso__set_loaded(curr_map->dso, curr_map->type);
864 }
865
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200866 return count + moved;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300867}
868
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300869bool symbol__restricted_filename(const char *filename,
870 const char *restricted_filename)
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -0300871{
872 bool restricted = false;
873
874 if (symbol_conf.kptr_restrict) {
875 char *r = realpath(filename, NULL);
876
877 if (r != NULL) {
878 restricted = strcmp(r, restricted_filename) == 0;
879 free(r);
880 return restricted;
881 }
882 }
883
884 return restricted;
885}
886
Adrian Hunter52afdaf2013-10-09 15:01:11 +0300887struct module_info {
888 struct rb_node rb_node;
889 char *name;
890 u64 start;
891};
892
893static void add_module(struct module_info *mi, struct rb_root *modules)
894{
895 struct rb_node **p = &modules->rb_node;
896 struct rb_node *parent = NULL;
897 struct module_info *m;
898
899 while (*p != NULL) {
900 parent = *p;
901 m = rb_entry(parent, struct module_info, rb_node);
902 if (strcmp(mi->name, m->name) < 0)
903 p = &(*p)->rb_left;
904 else
905 p = &(*p)->rb_right;
906 }
907 rb_link_node(&mi->rb_node, parent, p);
908 rb_insert_color(&mi->rb_node, modules);
909}
910
911static void delete_modules(struct rb_root *modules)
912{
913 struct module_info *mi;
914 struct rb_node *next = rb_first(modules);
915
916 while (next) {
917 mi = rb_entry(next, struct module_info, rb_node);
918 next = rb_next(&mi->rb_node);
919 rb_erase(&mi->rb_node, modules);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300920 zfree(&mi->name);
Adrian Hunter52afdaf2013-10-09 15:01:11 +0300921 free(mi);
922 }
923}
924
925static struct module_info *find_module(const char *name,
926 struct rb_root *modules)
927{
928 struct rb_node *n = modules->rb_node;
929
930 while (n) {
931 struct module_info *m;
932 int cmp;
933
934 m = rb_entry(n, struct module_info, rb_node);
935 cmp = strcmp(name, m->name);
936 if (cmp < 0)
937 n = n->rb_left;
938 else if (cmp > 0)
939 n = n->rb_right;
940 else
941 return m;
942 }
943
944 return NULL;
945}
946
947static int __read_proc_modules(void *arg, const char *name, u64 start)
948{
949 struct rb_root *modules = arg;
950 struct module_info *mi;
951
952 mi = zalloc(sizeof(struct module_info));
953 if (!mi)
954 return -ENOMEM;
955
956 mi->name = strdup(name);
957 mi->start = start;
958
959 if (!mi->name) {
960 free(mi);
961 return -ENOMEM;
962 }
963
964 add_module(mi, modules);
965
966 return 0;
967}
968
969static int read_proc_modules(const char *filename, struct rb_root *modules)
970{
971 if (symbol__restricted_filename(filename, "/proc/modules"))
972 return -1;
973
974 if (modules__parse(filename, modules, __read_proc_modules)) {
975 delete_modules(modules);
976 return -1;
977 }
978
979 return 0;
980}
981
Adrian Hunterfc1b6912013-10-14 16:57:29 +0300982int compare_proc_modules(const char *from, const char *to)
983{
984 struct rb_root from_modules = RB_ROOT;
985 struct rb_root to_modules = RB_ROOT;
986 struct rb_node *from_node, *to_node;
987 struct module_info *from_m, *to_m;
988 int ret = -1;
989
990 if (read_proc_modules(from, &from_modules))
991 return -1;
992
993 if (read_proc_modules(to, &to_modules))
994 goto out_delete_from;
995
996 from_node = rb_first(&from_modules);
997 to_node = rb_first(&to_modules);
998 while (from_node) {
999 if (!to_node)
1000 break;
1001
1002 from_m = rb_entry(from_node, struct module_info, rb_node);
1003 to_m = rb_entry(to_node, struct module_info, rb_node);
1004
1005 if (from_m->start != to_m->start ||
1006 strcmp(from_m->name, to_m->name))
1007 break;
1008
1009 from_node = rb_next(from_node);
1010 to_node = rb_next(to_node);
1011 }
1012
1013 if (!from_node && !to_node)
1014 ret = 0;
1015
1016 delete_modules(&to_modules);
1017out_delete_from:
1018 delete_modules(&from_modules);
1019
1020 return ret;
1021}
1022
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001023static int do_validate_kcore_modules(const char *filename, struct map *map,
1024 struct map_groups *kmaps)
1025{
1026 struct rb_root modules = RB_ROOT;
1027 struct map *old_map;
1028 int err;
1029
1030 err = read_proc_modules(filename, &modules);
1031 if (err)
1032 return err;
1033
1034 old_map = map_groups__first(kmaps, map->type);
1035 while (old_map) {
1036 struct map *next = map_groups__next(old_map);
1037 struct module_info *mi;
1038
1039 if (old_map == map || old_map->start == map->start) {
1040 /* The kernel map */
1041 old_map = next;
1042 continue;
1043 }
1044
1045 /* Module must be in memory at the same address */
1046 mi = find_module(old_map->dso->short_name, &modules);
1047 if (!mi || mi->start != old_map->start) {
1048 err = -EINVAL;
1049 goto out;
1050 }
1051
1052 old_map = next;
1053 }
1054out:
1055 delete_modules(&modules);
1056 return err;
1057}
1058
1059/*
1060 * If kallsyms is referenced by name then we look for filename in the same
1061 * directory.
1062 */
1063static bool filename_from_kallsyms_filename(char *filename,
1064 const char *base_name,
1065 const char *kallsyms_filename)
1066{
1067 char *name;
1068
1069 strcpy(filename, kallsyms_filename);
1070 name = strrchr(filename, '/');
1071 if (!name)
1072 return false;
1073
1074 name += 1;
1075
1076 if (!strcmp(name, "kallsyms")) {
1077 strcpy(name, base_name);
1078 return true;
1079 }
1080
1081 return false;
1082}
1083
1084static int validate_kcore_modules(const char *kallsyms_filename,
1085 struct map *map)
1086{
Wang Nanba927322015-04-07 08:22:45 +00001087 struct map_groups *kmaps = map__kmaps(map);
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001088 char modules_filename[PATH_MAX];
1089
Wang Nanba927322015-04-07 08:22:45 +00001090 if (!kmaps)
1091 return -EINVAL;
1092
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001093 if (!filename_from_kallsyms_filename(modules_filename, "modules",
1094 kallsyms_filename))
1095 return -EINVAL;
1096
1097 if (do_validate_kcore_modules(modules_filename, map, kmaps))
1098 return -EINVAL;
1099
1100 return 0;
1101}
1102
Adrian Huntera00d28c2014-01-29 16:14:41 +02001103static int validate_kcore_addresses(const char *kallsyms_filename,
1104 struct map *map)
1105{
1106 struct kmap *kmap = map__kmap(map);
1107
Wang Nanba927322015-04-07 08:22:45 +00001108 if (!kmap)
1109 return -EINVAL;
1110
Adrian Huntera00d28c2014-01-29 16:14:41 +02001111 if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
1112 u64 start;
1113
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001114 if (kallsyms__get_function_start(kallsyms_filename,
1115 kmap->ref_reloc_sym->name, &start))
1116 return -ENOENT;
Adrian Huntera00d28c2014-01-29 16:14:41 +02001117 if (start != kmap->ref_reloc_sym->addr)
1118 return -EINVAL;
1119 }
1120
1121 return validate_kcore_modules(kallsyms_filename, map);
1122}
1123
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001124struct kcore_mapfn_data {
1125 struct dso *dso;
1126 enum map_type type;
1127 struct list_head maps;
1128};
1129
1130static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1131{
1132 struct kcore_mapfn_data *md = data;
1133 struct map *map;
1134
1135 map = map__new2(start, md->dso, md->type);
1136 if (map == NULL)
1137 return -ENOMEM;
1138
1139 map->end = map->start + len;
1140 map->pgoff = pgoff;
1141
1142 list_add(&map->node, &md->maps);
1143
1144 return 0;
1145}
1146
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001147static int dso__load_kcore(struct dso *dso, struct map *map,
1148 const char *kallsyms_filename)
1149{
Wang Nanba927322015-04-07 08:22:45 +00001150 struct map_groups *kmaps = map__kmaps(map);
1151 struct machine *machine;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001152 struct kcore_mapfn_data md;
1153 struct map *old_map, *new_map, *replacement_map = NULL;
1154 bool is_64_bit;
1155 int err, fd;
1156 char kcore_filename[PATH_MAX];
1157 struct symbol *sym;
1158
Wang Nanba927322015-04-07 08:22:45 +00001159 if (!kmaps)
1160 return -EINVAL;
1161
1162 machine = kmaps->machine;
1163
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001164 /* This function requires that the map is the kernel map */
1165 if (map != machine->vmlinux_maps[map->type])
1166 return -EINVAL;
1167
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001168 if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1169 kallsyms_filename))
1170 return -EINVAL;
1171
Adrian Huntera00d28c2014-01-29 16:14:41 +02001172 /* Modules and kernel must be present at their original addresses */
1173 if (validate_kcore_addresses(kallsyms_filename, map))
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001174 return -EINVAL;
1175
1176 md.dso = dso;
1177 md.type = map->type;
1178 INIT_LIST_HEAD(&md.maps);
1179
1180 fd = open(kcore_filename, O_RDONLY);
Li Zhang36c8bb52015-06-19 16:57:33 +08001181 if (fd < 0) {
Adrian Hunter133de942015-08-20 13:07:40 +03001182 pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n",
1183 kcore_filename);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001184 return -EINVAL;
Li Zhang36c8bb52015-06-19 16:57:33 +08001185 }
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001186
1187 /* Read new maps into temporary lists */
1188 err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
1189 &is_64_bit);
1190 if (err)
1191 goto out_err;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +03001192 dso->is_64_bit = is_64_bit;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001193
1194 if (list_empty(&md.maps)) {
1195 err = -EINVAL;
1196 goto out_err;
1197 }
1198
1199 /* Remove old maps */
1200 old_map = map_groups__first(kmaps, map->type);
1201 while (old_map) {
1202 struct map *next = map_groups__next(old_map);
1203
1204 if (old_map != map)
1205 map_groups__remove(kmaps, old_map);
1206 old_map = next;
1207 }
1208
1209 /* Find the kernel map using the first symbol */
1210 sym = dso__first_symbol(dso, map->type);
1211 list_for_each_entry(new_map, &md.maps, node) {
1212 if (sym && sym->start >= new_map->start &&
1213 sym->start < new_map->end) {
1214 replacement_map = new_map;
1215 break;
1216 }
1217 }
1218
1219 if (!replacement_map)
1220 replacement_map = list_entry(md.maps.next, struct map, node);
1221
1222 /* Add new maps */
1223 while (!list_empty(&md.maps)) {
1224 new_map = list_entry(md.maps.next, struct map, node);
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -03001225 list_del_init(&new_map->node);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001226 if (new_map == replacement_map) {
1227 map->start = new_map->start;
1228 map->end = new_map->end;
1229 map->pgoff = new_map->pgoff;
1230 map->map_ip = new_map->map_ip;
1231 map->unmap_ip = new_map->unmap_ip;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001232 /* Ensure maps are correctly ordered */
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001233 map__get(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001234 map_groups__remove(kmaps, map);
1235 map_groups__insert(kmaps, map);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001236 map__put(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001237 } else {
1238 map_groups__insert(kmaps, new_map);
1239 }
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001240
1241 map__put(new_map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001242 }
1243
1244 /*
1245 * Set the data type and long name so that kcore can be read via
1246 * dso__data_read_addr().
1247 */
1248 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001249 dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001250 else
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001251 dso->binary_type = DSO_BINARY_TYPE__KCORE;
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001252 dso__set_long_name(dso, strdup(kcore_filename), true);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001253
1254 close(fd);
1255
1256 if (map->type == MAP__FUNCTION)
1257 pr_debug("Using %s for kernel object code\n", kcore_filename);
1258 else
1259 pr_debug("Using %s for kernel data\n", kcore_filename);
1260
1261 return 0;
1262
1263out_err:
1264 while (!list_empty(&md.maps)) {
1265 map = list_entry(md.maps.next, struct map, node);
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -03001266 list_del_init(&map->node);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001267 map__put(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001268 }
1269 close(fd);
1270 return -EINVAL;
1271}
1272
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001273/*
1274 * If the kernel is relocated at boot time, kallsyms won't match. Compute the
1275 * delta based on the relocation reference symbol.
1276 */
1277static int kallsyms__delta(struct map *map, const char *filename, u64 *delta)
1278{
1279 struct kmap *kmap = map__kmap(map);
1280 u64 addr;
1281
Wang Nanba927322015-04-07 08:22:45 +00001282 if (!kmap)
1283 return -1;
1284
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001285 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1286 return 0;
1287
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001288 if (kallsyms__get_function_start(filename, kmap->ref_reloc_sym->name, &addr))
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001289 return -1;
1290
1291 *delta = addr - kmap->ref_reloc_sym->addr;
1292 return 0;
1293}
1294
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001295int __dso__load_kallsyms(struct dso *dso, const char *filename,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001296 struct map *map, bool no_kcore)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001297{
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001298 u64 delta = 0;
1299
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03001300 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1301 return -1;
1302
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001303 if (dso__load_all_kallsyms(dso, filename, map) < 0)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001304 return -1;
1305
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001306 if (kallsyms__delta(map, filename, &delta))
1307 return -1;
1308
Anton Blanchard3f5a4272011-08-24 16:40:15 +10001309 symbols__fixup_end(&dso->symbols[map->type]);
Arnaldo Carvalho de Melo432746f2016-09-01 11:00:23 -03001310 symbols__fixup_duplicate(&dso->symbols[map->type]);
Anton Blanchard3f5a4272011-08-24 16:40:15 +10001311
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001312 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001313 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001314 else
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001315 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001316
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001317 if (!no_kcore && !dso__load_kcore(dso, map, filename))
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001318 return dso__split_kallsyms_for_kcore(dso, map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001319 else
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001320 return dso__split_kallsyms(dso, map, delta);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001321}
1322
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001323int dso__load_kallsyms(struct dso *dso, const char *filename,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001324 struct map *map)
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001325{
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001326 return __dso__load_kallsyms(dso, filename, map, false);
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001327}
1328
Krister Johansenbf2e7102017-07-05 18:48:09 -07001329static int dso__load_perf_map(const char *map_path, struct dso *dso,
1330 struct map *map)
Pekka Enberg80d496b2009-06-08 21:12:48 +03001331{
1332 char *line = NULL;
1333 size_t n;
1334 FILE *file;
1335 int nr_syms = 0;
1336
Krister Johansenbf2e7102017-07-05 18:48:09 -07001337 file = fopen(map_path, "r");
Pekka Enberg80d496b2009-06-08 21:12:48 +03001338 if (file == NULL)
1339 goto out_failure;
1340
1341 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001342 u64 start, size;
Pekka Enberg80d496b2009-06-08 21:12:48 +03001343 struct symbol *sym;
1344 int line_len, len;
1345
1346 line_len = getline(&line, &n, file);
1347 if (line_len < 0)
1348 break;
1349
1350 if (!line)
1351 goto out_failure;
1352
1353 line[--line_len] = '\0'; /* \n */
1354
1355 len = hex2u64(line, &start);
1356
1357 len++;
1358 if (len + 2 >= line_len)
1359 continue;
1360
1361 len += hex2u64(line + len, &size);
1362
1363 len++;
1364 if (len + 2 >= line_len)
1365 continue;
1366
Arnaldo Carvalho de Meloc408fed2010-08-05 12:59:47 -03001367 sym = symbol__new(start, size, STB_GLOBAL, line + len);
Pekka Enberg80d496b2009-06-08 21:12:48 +03001368
1369 if (sym == NULL)
1370 goto out_delete_line;
1371
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001372 symbols__insert(&dso->symbols[map->type], sym);
1373 nr_syms++;
Pekka Enberg80d496b2009-06-08 21:12:48 +03001374 }
1375
1376 free(line);
1377 fclose(file);
1378
1379 return nr_syms;
1380
1381out_delete_line:
1382 free(line);
1383out_failure:
1384 return -1;
1385}
1386
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001387static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1388 enum dso_binary_type type)
1389{
1390 switch (type) {
1391 case DSO_BINARY_TYPE__JAVA_JIT:
1392 case DSO_BINARY_TYPE__DEBUGLINK:
1393 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1394 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1395 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1396 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1397 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1398 return !kmod && dso->kernel == DSO_TYPE_USER;
1399
1400 case DSO_BINARY_TYPE__KALLSYMS:
1401 case DSO_BINARY_TYPE__VMLINUX:
1402 case DSO_BINARY_TYPE__KCORE:
1403 return dso->kernel == DSO_TYPE_KERNEL;
1404
1405 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1406 case DSO_BINARY_TYPE__GUEST_VMLINUX:
1407 case DSO_BINARY_TYPE__GUEST_KCORE:
1408 return dso->kernel == DSO_TYPE_GUEST_KERNEL;
1409
1410 case DSO_BINARY_TYPE__GUEST_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +09001411 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001412 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +09001413 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001414 /*
1415 * kernel modules know their symtab type - it's set when
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001416 * creating a module dso in machine__findnew_module_map().
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001417 */
1418 return kmod && dso->symtab_type == type;
1419
1420 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
1421 return true;
1422
1423 case DSO_BINARY_TYPE__NOT_FOUND:
1424 default:
1425 return false;
1426 }
1427}
1428
Krister Johansenbf2e7102017-07-05 18:48:09 -07001429/* Checks for the existence of the perf-<pid>.map file in two different
1430 * locations. First, if the process is a separate mount namespace, check in
1431 * that namespace using the pid of the innermost pid namespace. If's not in a
1432 * namespace, or the file can't be found there, try in the mount namespace of
1433 * the tracing process using our view of its pid.
1434 */
1435static int dso__find_perf_map(char *filebuf, size_t bufsz,
1436 struct nsinfo **nsip)
1437{
1438 struct nscookie nsc;
1439 struct nsinfo *nsi;
1440 struct nsinfo *nnsi;
1441 int rc = -1;
1442
1443 nsi = *nsip;
1444
1445 if (nsi->need_setns) {
1446 snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsi->nstgid);
1447 nsinfo__mountns_enter(nsi, &nsc);
1448 rc = access(filebuf, R_OK);
1449 nsinfo__mountns_exit(&nsc);
1450 if (rc == 0)
1451 return rc;
1452 }
1453
1454 nnsi = nsinfo__copy(nsi);
1455 if (nnsi) {
1456 nsinfo__put(nsi);
1457
1458 nnsi->need_setns = false;
1459 snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nnsi->tgid);
1460 *nsip = nnsi;
1461 rc = 0;
1462 }
1463
1464 return rc;
1465}
1466
1467
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001468int dso__load(struct dso *dso, struct map *map)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001469{
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001470 char *name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001471 int ret = -1;
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001472 u_int i;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001473 struct machine *machine;
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001474 char *root_dir = (char *) "";
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001475 int ss_pos = 0;
1476 struct symsrc ss_[2];
1477 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001478 bool kmod;
Krister Johansenbf2e7102017-07-05 18:48:09 -07001479 bool perfmap;
Dima Kogan5baecbc2015-09-07 17:34:19 -07001480 unsigned char build_id[BUILD_ID_SIZE];
Krister Johansen843ff372017-07-05 18:48:08 -07001481 struct nscookie nsc;
Krister Johansenbf2e7102017-07-05 18:48:09 -07001482 char newmapname[PATH_MAX];
1483 const char *map_path = dso->long_name;
1484
1485 perfmap = strncmp(dso->name, "/tmp/perf-", 10) == 0;
1486 if (perfmap) {
1487 if (dso->nsinfo && (dso__find_perf_map(newmapname,
1488 sizeof(newmapname), &dso->nsinfo) == 0)) {
1489 map_path = newmapname;
1490 }
1491 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001492
Krister Johansen843ff372017-07-05 18:48:08 -07001493 nsinfo__mountns_enter(dso->nsinfo, &nsc);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001494 pthread_mutex_lock(&dso->lock);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001495
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001496 /* check again under the dso->lock */
1497 if (dso__loaded(dso, map->type)) {
1498 ret = 1;
1499 goto out;
1500 }
1501
1502 if (dso->kernel) {
1503 if (dso->kernel == DSO_TYPE_KERNEL)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001504 ret = dso__load_kernel_sym(dso, map);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001505 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001506 ret = dso__load_guest_kernel_sym(dso, map);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001507
1508 goto out;
1509 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001510
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001511 if (map->groups && map->groups->machine)
1512 machine = map->groups->machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001513 else
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001514 machine = NULL;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001515
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001516 dso->adjust_symbols = 0;
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001517
Krister Johansenbf2e7102017-07-05 18:48:09 -07001518 if (perfmap) {
Pekka Enberg981c1252011-08-09 22:54:18 +03001519 struct stat st;
1520
Krister Johansenbf2e7102017-07-05 18:48:09 -07001521 if (lstat(map_path, &st) < 0)
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001522 goto out;
Pekka Enberg981c1252011-08-09 22:54:18 +03001523
Arnaldo Carvalho de Melo2059fc72015-11-12 16:50:13 -03001524 if (!symbol_conf.force && st.st_uid && (st.st_uid != geteuid())) {
Pekka Enberg981c1252011-08-09 22:54:18 +03001525 pr_warning("File %s not owned by current user or root, "
Krister Johansenbf2e7102017-07-05 18:48:09 -07001526 "ignoring it (use -f to override).\n", map_path);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001527 goto out;
Pekka Enberg981c1252011-08-09 22:54:18 +03001528 }
1529
Krister Johansenbf2e7102017-07-05 18:48:09 -07001530 ret = dso__load_perf_map(map_path, dso, map);
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001531 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1532 DSO_BINARY_TYPE__NOT_FOUND;
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001533 goto out;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001534 }
1535
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001536 if (machine)
1537 root_dir = machine->root_dir;
1538
David Ahern164c8002013-01-14 10:46:47 -07001539 name = malloc(PATH_MAX);
1540 if (!name)
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001541 goto out;
David Ahern164c8002013-01-14 10:46:47 -07001542
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001543 kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
Namhyung Kimc00c48f2014-11-04 10:14:27 +09001544 dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
1545 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE ||
1546 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001547
Dima Kogan5baecbc2015-09-07 17:34:19 -07001548
1549 /*
1550 * Read the build id if possible. This is required for
1551 * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work
1552 */
Kan Lianged6c1662016-12-13 10:29:44 -05001553 if (!dso->has_build_id &&
Victor Kamensky9b200652017-02-06 15:48:28 -08001554 is_regular_file(dso->long_name)) {
1555 __symbol__join_symfs(name, PATH_MAX, dso->long_name);
1556 if (filename__read_build_id(name, build_id, BUILD_ID_SIZE) > 0)
Dima Kogan5baecbc2015-09-07 17:34:19 -07001557 dso__set_build_id(dso, build_id);
Victor Kamensky9b200652017-02-06 15:48:28 -08001558 }
Dima Kogan5baecbc2015-09-07 17:34:19 -07001559
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001560 /*
1561 * Iterate over candidate debug images.
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001562 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1563 * and/or opd section) for processing.
Dave Martin6da80ce2010-07-30 09:50:09 -03001564 */
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001565 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001566 struct symsrc *ss = &ss_[ss_pos];
1567 bool next_slot = false;
Dave Martin6da80ce2010-07-30 09:50:09 -03001568
Cody P Schafer005f9292012-08-10 15:22:58 -07001569 enum dso_binary_type symtab_type = binary_type_symtab[i];
Dave Martin6da80ce2010-07-30 09:50:09 -03001570
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001571 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1572 continue;
1573
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -03001574 if (dso__read_binary_type_filename(dso, symtab_type,
1575 root_dir, name, PATH_MAX))
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001576 continue;
Dave Martin6da80ce2010-07-30 09:50:09 -03001577
Jiri Olsa40356722016-01-20 12:56:32 +01001578 if (!is_regular_file(name))
1579 continue;
1580
Dave Martin6da80ce2010-07-30 09:50:09 -03001581 /* Name is now the name of the next image to try */
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001582 if (symsrc__init(ss, dso, name, symtab_type) < 0)
Dave Martin6da80ce2010-07-30 09:50:09 -03001583 continue;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001584
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001585 if (!syms_ss && symsrc__has_symtab(ss)) {
1586 syms_ss = ss;
1587 next_slot = true;
Adrian Hunter0058aef2013-12-03 09:23:08 +02001588 if (!dso->symsrc_filename)
1589 dso->symsrc_filename = strdup(name);
Cody P Schaferd26cd122012-08-10 15:23:00 -07001590 }
1591
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001592 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1593 runtime_ss = ss;
1594 next_slot = true;
Cody P Schafera44f6052012-08-10 15:22:59 -07001595 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001596
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001597 if (next_slot) {
1598 ss_pos++;
Jiri Olsa33ff5812012-04-18 15:46:58 +02001599
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001600 if (syms_ss && runtime_ss)
1601 break;
Namhyung Kim98e9f032014-02-20 10:32:54 +09001602 } else {
1603 symsrc__destroy(ss);
Dave Martin6da80ce2010-07-30 09:50:09 -03001604 }
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001605
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001606 }
Dave Martin6da80ce2010-07-30 09:50:09 -03001607
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001608 if (!runtime_ss && !syms_ss)
1609 goto out_free;
1610
1611 if (runtime_ss && !syms_ss) {
1612 syms_ss = runtime_ss;
Arnaldo Carvalho de Melo60e4b102011-03-22 15:42:14 -03001613 }
1614
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001615 /* We'll have to hope for the best */
1616 if (!runtime_ss && syms_ss)
1617 runtime_ss = syms_ss;
1618
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001619 if (syms_ss)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001620 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, kmod);
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001621 else
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001622 ret = -1;
1623
David Ahernf47b58b2012-08-19 09:47:14 -06001624 if (ret > 0) {
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001625 int nr_plt;
1626
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001627 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map);
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001628 if (nr_plt > 0)
1629 ret += nr_plt;
1630 }
1631
1632 for (; ss_pos > 0; ss_pos--)
1633 symsrc__destroy(&ss_[ss_pos - 1]);
1634out_free:
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001635 free(name);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001636 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001637 ret = 0;
1638out:
1639 dso__set_loaded(dso, map->type);
1640 pthread_mutex_unlock(&dso->lock);
Krister Johansen843ff372017-07-05 18:48:08 -07001641 nsinfo__mountns_exit(&nsc);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001642
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001643 return ret;
1644}
1645
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001646struct map *map_groups__find_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -02001647 enum map_type type, const char *name)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001648{
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -03001649 struct maps *maps = &mg->maps[type];
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -03001650 struct map *map;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001651
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -03001652 pthread_rwlock_rdlock(&maps->lock);
1653
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -03001654 for (map = maps__first(maps); map; map = map__next(map)) {
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001655 if (map->dso && strcmp(map->dso->short_name, name) == 0)
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -03001656 goto out_unlock;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001657 }
1658
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -03001659 map = NULL;
1660
1661out_unlock:
1662 pthread_rwlock_unlock(&maps->lock);
1663 return map;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001664}
1665
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001666int dso__load_vmlinux(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001667 const char *vmlinux, bool vmlinux_allocated)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001668{
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001669 int err = -1;
1670 struct symsrc ss;
David Ahernec5761e2010-12-09 13:27:07 -07001671 char symfs_vmlinux[PATH_MAX];
Cody P Schafer005f9292012-08-10 15:22:58 -07001672 enum dso_binary_type symtab_type;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001673
Namhyung Kim5698d2c2013-07-17 17:08:15 +09001674 if (vmlinux[0] == '/')
1675 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1676 else
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -03001677 symbol__join_symfs(symfs_vmlinux, vmlinux);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001678
Cody P Schafer21ea4532012-08-10 15:22:56 -07001679 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Cody P Schafer005f9292012-08-10 15:22:58 -07001680 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
Cody P Schafer21ea4532012-08-10 15:22:56 -07001681 else
Cody P Schafer005f9292012-08-10 15:22:58 -07001682 symtab_type = DSO_BINARY_TYPE__VMLINUX;
Cody P Schafer21ea4532012-08-10 15:22:56 -07001683
Cody P Schafer005f9292012-08-10 15:22:58 -07001684 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001685 return -1;
1686
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001687 err = dso__load_sym(dso, map, &ss, &ss, 0);
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001688 symsrc__destroy(&ss);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001689
Cody P Schafer515850e2012-08-10 15:22:54 -07001690 if (err > 0) {
Adrian Hunter39b12f782013-08-07 14:38:47 +03001691 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001692 dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
Adrian Hunter39b12f782013-08-07 14:38:47 +03001693 else
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001694 dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -03001695 dso__set_long_name(dso, vmlinux, vmlinux_allocated);
Cody P Schafer515850e2012-08-10 15:22:54 -07001696 dso__set_loaded(dso, map->type);
David Ahernec5761e2010-12-09 13:27:07 -07001697 pr_debug("Using %s for symbols\n", symfs_vmlinux);
Cody P Schafer515850e2012-08-10 15:22:54 -07001698 }
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001699
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001700 return err;
1701}
1702
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001703int dso__load_vmlinux_path(struct dso *dso, struct map *map)
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001704{
1705 int i, err = 0;
Namhyung Kim00dc8652014-11-04 10:14:32 +09001706 char *filename = NULL;
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001707
Arnaldo Carvalho de Melodc382182015-10-11 17:17:24 -03001708 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1709 vmlinux_path__nr_entries + 1);
1710
1711 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001712 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false);
Arnaldo Carvalho de Melodc382182015-10-11 17:17:24 -03001713 if (err > 0)
1714 goto out;
1715 }
1716
Namhyung Kim00dc8652014-11-04 10:14:32 +09001717 if (!symbol_conf.ignore_vmlinux_buildid)
1718 filename = dso__build_id_filename(dso, NULL, 0);
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001719 if (filename != NULL) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001720 err = dso__load_vmlinux(dso, map, filename, true);
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -03001721 if (err > 0)
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001722 goto out;
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001723 free(filename);
1724 }
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001725out:
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001726 return err;
1727}
1728
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001729static bool visible_dir_filter(const char *name, struct dirent *d)
1730{
1731 if (d->d_type != DT_DIR)
1732 return false;
1733 return lsdir_no_dot_filter(name, d);
1734}
1735
Adrian Hunter0544d422013-10-14 13:43:43 +03001736static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1737{
1738 char kallsyms_filename[PATH_MAX];
Adrian Hunter0544d422013-10-14 13:43:43 +03001739 int ret = -1;
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001740 struct strlist *dirs;
1741 struct str_node *nd;
Adrian Hunter0544d422013-10-14 13:43:43 +03001742
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001743 dirs = lsdir(dir, visible_dir_filter);
1744 if (!dirs)
Adrian Hunter0544d422013-10-14 13:43:43 +03001745 return -1;
1746
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03001747 strlist__for_each_entry(nd, dirs) {
Adrian Hunter0544d422013-10-14 13:43:43 +03001748 scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001749 "%s/%s/kallsyms", dir, nd->s);
Adrian Huntera00d28c2014-01-29 16:14:41 +02001750 if (!validate_kcore_addresses(kallsyms_filename, map)) {
Adrian Hunter0544d422013-10-14 13:43:43 +03001751 strlcpy(dir, kallsyms_filename, dir_sz);
1752 ret = 0;
1753 break;
1754 }
1755 }
1756
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001757 strlist__delete(dirs);
Adrian Hunter0544d422013-10-14 13:43:43 +03001758
1759 return ret;
1760}
1761
Masami Hiramatsu11870d72016-05-29 00:15:13 +09001762/*
1763 * Use open(O_RDONLY) to check readability directly instead of access(R_OK)
1764 * since access(R_OK) only checks with real UID/GID but open() use effective
1765 * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO).
1766 */
1767static bool filename__readable(const char *file)
1768{
1769 int fd = open(file, O_RDONLY);
1770 if (fd < 0)
1771 return false;
1772 close(fd);
1773 return true;
1774}
1775
Adrian Hunter0544d422013-10-14 13:43:43 +03001776static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1777{
1778 u8 host_build_id[BUILD_ID_SIZE];
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001779 char sbuild_id[SBUILD_ID_SIZE];
Adrian Hunter0544d422013-10-14 13:43:43 +03001780 bool is_host = false;
1781 char path[PATH_MAX];
1782
1783 if (!dso->has_build_id) {
1784 /*
1785 * Last resort, if we don't have a build-id and couldn't find
1786 * any vmlinux file, try the running kernel kallsyms table.
1787 */
1788 goto proc_kallsyms;
1789 }
1790
1791 if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1792 sizeof(host_build_id)) == 0)
1793 is_host = dso__build_id_equal(dso, host_build_id);
1794
Masami Hiramatsu4e4b6c02016-05-29 00:15:28 +09001795 /* Try a fast path for /proc/kallsyms if possible */
Adrian Hunter0544d422013-10-14 13:43:43 +03001796 if (is_host) {
Adrian Hunter0544d422013-10-14 13:43:43 +03001797 /*
Masami Hiramatsu11870d72016-05-29 00:15:13 +09001798 * Do not check the build-id cache, unless we know we cannot use
1799 * /proc/kcore or module maps don't match to /proc/kallsyms.
1800 * To check readability of /proc/kcore, do not use access(R_OK)
1801 * since /proc/kcore requires CAP_SYS_RAWIO to read and access
1802 * can't check it.
Adrian Hunter0544d422013-10-14 13:43:43 +03001803 */
Masami Hiramatsu11870d72016-05-29 00:15:13 +09001804 if (filename__readable("/proc/kcore") &&
1805 !validate_kcore_addresses("/proc/kallsyms", map))
1806 goto proc_kallsyms;
Adrian Hunter0544d422013-10-14 13:43:43 +03001807 }
1808
Masami Hiramatsu4e4b6c02016-05-29 00:15:28 +09001809 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1810
Adrian Hunter449867e2013-11-26 15:19:24 +02001811 /* Find kallsyms in build-id cache with kcore */
Masami Hiramatsu4e4b6c02016-05-29 00:15:28 +09001812 scnprintf(path, sizeof(path), "%s/%s/%s",
1813 buildid_dir, DSO__NAME_KCORE, sbuild_id);
1814
Adrian Hunter449867e2013-11-26 15:19:24 +02001815 if (!find_matching_kcore(map, path, sizeof(path)))
1816 return strdup(path);
1817
Masami Hiramatsu4e4b6c02016-05-29 00:15:28 +09001818 /* Use current /proc/kallsyms if possible */
1819 if (is_host) {
1820proc_kallsyms:
1821 return strdup("/proc/kallsyms");
1822 }
1823
1824 /* Finally, find a cache of kallsyms */
Masami Hiramatsu01412262016-05-29 00:15:37 +09001825 if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) {
Adrian Hunter0544d422013-10-14 13:43:43 +03001826 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1827 sbuild_id);
1828 return NULL;
1829 }
1830
1831 return strdup(path);
Adrian Hunter0544d422013-10-14 13:43:43 +03001832}
1833
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001834static int dso__load_kernel_sym(struct dso *dso, struct map *map)
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001835{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001836 int err;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001837 const char *kallsyms_filename = NULL;
1838 char *kallsyms_allocated_filename = NULL;
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001839 /*
David Ahernb226a5a72010-12-07 19:39:46 -07001840 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1841 * it and only it, reporting errors to the user if it cannot be used.
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001842 *
1843 * For instance, try to analyse an ARM perf.data file _without_ a
1844 * build-id, or if the user specifies the wrong path to the right
1845 * vmlinux file, obviously we can't fallback to another vmlinux (a
1846 * x86_86 one, on the machine where analysis is being performed, say),
1847 * or worse, /proc/kallsyms.
1848 *
1849 * If the specified file _has_ a build-id and there is a build-id
1850 * section in the perf.data file, we will still do the expected
1851 * validation in dso__load_vmlinux and will bail out if they don't
1852 * match.
1853 */
David Ahernb226a5a72010-12-07 19:39:46 -07001854 if (symbol_conf.kallsyms_name != NULL) {
1855 kallsyms_filename = symbol_conf.kallsyms_name;
1856 goto do_kallsyms;
1857 }
1858
Willy Tarreaufc2be692013-09-14 10:32:59 +02001859 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001860 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, false);
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001861 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001862
Willy Tarreaufc2be692013-09-14 10:32:59 +02001863 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001864 err = dso__load_vmlinux_path(dso, map);
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001865 if (err > 0)
Adrian Hunter39b12f782013-08-07 14:38:47 +03001866 return err;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001867 }
1868
David Ahernec5761e2010-12-09 13:27:07 -07001869 /* do not try local files if a symfs was given */
1870 if (symbol_conf.symfs[0] != 0)
1871 return -1;
1872
Adrian Hunter0544d422013-10-14 13:43:43 +03001873 kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
1874 if (!kallsyms_allocated_filename)
1875 return -1;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001876
Adrian Hunter0544d422013-10-14 13:43:43 +03001877 kallsyms_filename = kallsyms_allocated_filename;
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001878
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001879do_kallsyms:
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001880 err = dso__load_kallsyms(dso, kallsyms_filename, map);
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001881 if (err > 0)
1882 pr_debug("Using %s for symbols\n", kallsyms_filename);
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001883 free(kallsyms_allocated_filename);
1884
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001885 if (err > 0 && !dso__is_kcore(dso)) {
Adrian Hunterbdac0bc2014-07-14 13:02:43 +03001886 dso->binary_type = DSO_BINARY_TYPE__KALLSYMS;
Masami Hiramatsu0a775822016-05-15 12:19:40 +09001887 dso__set_long_name(dso, DSO__NAME_KALLSYMS, false);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001888 map__fixup_start(map);
1889 map__fixup_end(map);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001890 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001891
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001892 return err;
1893}
1894
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001895static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001896{
1897 int err;
1898 const char *kallsyms_filename = NULL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001899 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001900 char path[PATH_MAX];
1901
1902 if (!map->groups) {
1903 pr_debug("Guest kernel map hasn't the point to groups\n");
1904 return -1;
1905 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001906 machine = map->groups->machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001907
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001908 if (machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001909 /*
1910 * if the user specified a vmlinux filename, use it and only
1911 * it, reporting errors to the user if it cannot be used.
1912 * Or use file guest_kallsyms inputted by user on commandline
1913 */
1914 if (symbol_conf.default_guest_vmlinux_name != NULL) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001915 err = dso__load_vmlinux(dso, map,
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -03001916 symbol_conf.default_guest_vmlinux_name,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001917 false);
Adrian Hunter39b12f782013-08-07 14:38:47 +03001918 return err;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001919 }
1920
1921 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1922 if (!kallsyms_filename)
1923 return -1;
1924 } else {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001925 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001926 kallsyms_filename = path;
1927 }
1928
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001929 err = dso__load_kallsyms(dso, kallsyms_filename, map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001930 if (err > 0)
Adrian Hunter39b12f782013-08-07 14:38:47 +03001931 pr_debug("Using %s for symbols\n", kallsyms_filename);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001932 if (err > 0 && !dso__is_kcore(dso)) {
Adrian Hunterbdac0bc2014-07-14 13:02:43 +03001933 dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
Adrian Hunter39b12f782013-08-07 14:38:47 +03001934 machine__mmap_name(machine, path, sizeof(path));
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001935 dso__set_long_name(dso, strdup(path), true);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001936 map__fixup_start(map);
1937 map__fixup_end(map);
1938 }
1939
1940 return err;
1941}
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001942
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001943static void vmlinux_path__exit(void)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001944{
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001945 while (--vmlinux_path__nr_entries >= 0)
1946 zfree(&vmlinux_path[vmlinux_path__nr_entries]);
Wang Nanc4f03542015-05-17 10:56:27 +00001947 vmlinux_path__nr_entries = 0;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001948
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001949 zfree(&vmlinux_path);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001950}
1951
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01001952static const char * const vmlinux_paths[] = {
1953 "vmlinux",
1954 "/boot/vmlinux"
1955};
1956
1957static const char * const vmlinux_paths_upd[] = {
1958 "/boot/vmlinux-%s",
1959 "/usr/lib/debug/boot/vmlinux-%s",
1960 "/lib/modules/%s/build/vmlinux",
Ekaterina Tumanovaf55ae952015-11-25 17:32:46 +01001961 "/usr/lib/debug/lib/modules/%s/vmlinux",
1962 "/usr/lib/debug/boot/vmlinux-%s.debug"
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01001963};
1964
1965static int vmlinux_path__add(const char *new_entry)
1966{
1967 vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry);
1968 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1969 return -1;
1970 ++vmlinux_path__nr_entries;
1971
1972 return 0;
1973}
1974
Kan Liangce80d3b2015-08-28 05:48:04 -04001975static int vmlinux_path__init(struct perf_env *env)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001976{
1977 struct utsname uts;
1978 char bf[PATH_MAX];
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001979 char *kernel_version;
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01001980 unsigned int i;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001981
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01001982 vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
1983 ARRAY_SIZE(vmlinux_paths_upd)));
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001984 if (vmlinux_path == NULL)
1985 return -1;
1986
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01001987 for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++)
1988 if (vmlinux_path__add(vmlinux_paths[i]) < 0)
1989 goto out_fail;
David Ahernec5761e2010-12-09 13:27:07 -07001990
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001991 /* only try kernel version if no symfs was given */
David Ahernec5761e2010-12-09 13:27:07 -07001992 if (symbol_conf.symfs[0] != 0)
1993 return 0;
1994
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001995 if (env) {
1996 kernel_version = env->os_release;
1997 } else {
1998 if (uname(&uts) < 0)
1999 goto out_fail;
David Ahernec5761e2010-12-09 13:27:07 -07002000
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002001 kernel_version = uts.release;
2002 }
2003
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01002004 for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) {
2005 snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version);
2006 if (vmlinux_path__add(bf) < 0)
2007 goto out_fail;
2008 }
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002009
2010 return 0;
2011
2012out_fail:
2013 vmlinux_path__exit();
2014 return -1;
2015}
2016
David Ahern3bfe5f82013-11-18 13:32:48 -07002017int setup_list(struct strlist **list, const char *list_str,
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002018 const char *list_name)
2019{
2020 if (list_str == NULL)
2021 return 0;
2022
Arnaldo Carvalho de Melo4a77e212015-07-20 12:13:34 -03002023 *list = strlist__new(list_str, NULL);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002024 if (!*list) {
2025 pr_err("problems parsing %s list\n", list_name);
2026 return -1;
2027 }
Arnaldo Carvalho de Melo0bc2f2f2015-07-13 08:21:57 -03002028
2029 symbol_conf.has_filter = true;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002030 return 0;
2031}
2032
David Aherne03eaa42015-03-24 09:52:41 -06002033int setup_intlist(struct intlist **list, const char *list_str,
2034 const char *list_name)
2035{
2036 if (list_str == NULL)
2037 return 0;
2038
2039 *list = intlist__new(list_str);
2040 if (!*list) {
2041 pr_err("problems parsing %s list\n", list_name);
2042 return -1;
2043 }
2044 return 0;
2045}
2046
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002047static bool symbol__read_kptr_restrict(void)
2048{
2049 bool value = false;
Wang Nan38272dc2016-05-24 09:21:27 +00002050 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002051
Wang Nan38272dc2016-05-24 09:21:27 +00002052 if (fp != NULL) {
2053 char line[8];
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002054
Wang Nan38272dc2016-05-24 09:21:27 +00002055 if (fgets(line, sizeof(line), fp) != NULL)
Wang Nan3dbe46c2016-11-15 04:05:45 +00002056 value = ((geteuid() != 0) || (getuid() != 0)) ?
Wang Nan38272dc2016-05-24 09:21:27 +00002057 (atoi(line) != 0) :
2058 (atoi(line) == 2);
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002059
Wang Nan38272dc2016-05-24 09:21:27 +00002060 fclose(fp);
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002061 }
2062
2063 return value;
2064}
2065
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -03002066int symbol__annotation_init(void)
2067{
2068 if (symbol_conf.initialized) {
2069 pr_err("Annotation needs to be init before symbol__init()\n");
2070 return -1;
2071 }
2072
2073 if (symbol_conf.init_annotation) {
2074 pr_warning("Annotation being initialized multiple times\n");
2075 return 0;
2076 }
2077
2078 symbol_conf.priv_size += sizeof(struct annotation);
2079 symbol_conf.init_annotation = true;
2080 return 0;
2081}
2082
Kan Liangce80d3b2015-08-28 05:48:04 -04002083int symbol__init(struct perf_env *env)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002084{
David Ahernec5761e2010-12-09 13:27:07 -07002085 const char *symfs;
2086
Jovi Zhang85e00b52010-09-09 13:30:59 -03002087 if (symbol_conf.initialized)
2088 return 0;
2089
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002090 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
David S. Miller4d439512011-03-29 14:18:39 -03002091
Namhyung Kim166ccc92012-08-06 13:41:19 +09002092 symbol__elf_init();
2093
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02002094 if (symbol_conf.sort_by_name)
2095 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2096 sizeof(struct symbol));
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02002097
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002098 if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002099 return -1;
2100
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02002101 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2102 pr_err("'.' is the only non valid --field-separator argument\n");
2103 return -1;
2104 }
2105
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002106 if (setup_list(&symbol_conf.dso_list,
2107 symbol_conf.dso_list_str, "dso") < 0)
2108 return -1;
2109
2110 if (setup_list(&symbol_conf.comm_list,
2111 symbol_conf.comm_list_str, "comm") < 0)
2112 goto out_free_dso_list;
2113
David Aherne03eaa42015-03-24 09:52:41 -06002114 if (setup_intlist(&symbol_conf.pid_list,
2115 symbol_conf.pid_list_str, "pid") < 0)
2116 goto out_free_comm_list;
2117
2118 if (setup_intlist(&symbol_conf.tid_list,
2119 symbol_conf.tid_list_str, "tid") < 0)
2120 goto out_free_pid_list;
2121
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002122 if (setup_list(&symbol_conf.sym_list,
2123 symbol_conf.sym_list_str, "symbol") < 0)
David Aherne03eaa42015-03-24 09:52:41 -06002124 goto out_free_tid_list;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002125
David Ahern64eff7d2016-11-25 13:00:21 -07002126 if (setup_list(&symbol_conf.bt_stop_list,
2127 symbol_conf.bt_stop_list_str, "symbol") < 0)
2128 goto out_free_sym_list;
2129
David Ahernec5761e2010-12-09 13:27:07 -07002130 /*
2131 * A path to symbols of "/" is identical to ""
2132 * reset here for simplicity.
2133 */
2134 symfs = realpath(symbol_conf.symfs, NULL);
2135 if (symfs == NULL)
2136 symfs = symbol_conf.symfs;
2137 if (strcmp(symfs, "/") == 0)
2138 symbol_conf.symfs = "";
2139 if (symfs != symbol_conf.symfs)
2140 free((void *)symfs);
2141
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002142 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2143
Jovi Zhang85e00b52010-09-09 13:30:59 -03002144 symbol_conf.initialized = true;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002145 return 0;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002146
David Ahern64eff7d2016-11-25 13:00:21 -07002147out_free_sym_list:
2148 strlist__delete(symbol_conf.sym_list);
David Aherne03eaa42015-03-24 09:52:41 -06002149out_free_tid_list:
2150 intlist__delete(symbol_conf.tid_list);
2151out_free_pid_list:
2152 intlist__delete(symbol_conf.pid_list);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002153out_free_comm_list:
2154 strlist__delete(symbol_conf.comm_list);
Namhyung Kimd74c8962011-12-13 00:16:52 +09002155out_free_dso_list:
2156 strlist__delete(symbol_conf.dso_list);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002157 return -1;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002158}
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002159
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002160void symbol__exit(void)
2161{
Jovi Zhang85e00b52010-09-09 13:30:59 -03002162 if (!symbol_conf.initialized)
2163 return;
David Ahern64eff7d2016-11-25 13:00:21 -07002164 strlist__delete(symbol_conf.bt_stop_list);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002165 strlist__delete(symbol_conf.sym_list);
2166 strlist__delete(symbol_conf.dso_list);
2167 strlist__delete(symbol_conf.comm_list);
David Aherne03eaa42015-03-24 09:52:41 -06002168 intlist__delete(symbol_conf.tid_list);
2169 intlist__delete(symbol_conf.pid_list);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002170 vmlinux_path__exit();
2171 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
David Ahern64eff7d2016-11-25 13:00:21 -07002172 symbol_conf.bt_stop_list = NULL;
Jovi Zhang85e00b52010-09-09 13:30:59 -03002173 symbol_conf.initialized = false;
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002174}
He Kuanga7066702016-05-19 11:47:37 +00002175
2176int symbol__config_symfs(const struct option *opt __maybe_unused,
2177 const char *dir, int unset __maybe_unused)
2178{
2179 char *bf = NULL;
2180 int ret;
2181
2182 symbol_conf.symfs = strdup(dir);
2183 if (symbol_conf.symfs == NULL)
2184 return -ENOMEM;
2185
2186 /* skip the locally configured cache if a symfs is given, and
2187 * config buildid dir to symfs/.debug
2188 */
2189 ret = asprintf(&bf, "%s/%s", dir, ".debug");
2190 if (ret < 0)
2191 return -ENOMEM;
2192
2193 set_buildid_dir(bf);
2194
2195 free(bf);
2196 return 0;
2197}