blob: ce6993bebf8c1d744e2e1bea74f1bbb8d44640c0 [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,
Milian Wolffd8a88dd2017-10-19 13:38:36 +020048 .inline_name = true,
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020049};
50
Jiri Olsa44f24cb2012-07-22 14:14:32 +020051static enum dso_binary_type binary_type_symtab[] = {
52 DSO_BINARY_TYPE__KALLSYMS,
53 DSO_BINARY_TYPE__GUEST_KALLSYMS,
54 DSO_BINARY_TYPE__JAVA_JIT,
55 DSO_BINARY_TYPE__DEBUGLINK,
56 DSO_BINARY_TYPE__BUILD_ID_CACHE,
Krister Johansend2396992017-07-05 18:48:13 -070057 DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO,
Jiri Olsa44f24cb2012-07-22 14:14:32 +020058 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
59 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
60 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
61 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
62 DSO_BINARY_TYPE__GUEST_KMODULE,
Namhyung Kimc00c48f2014-11-04 10:14:27 +090063 DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
Jiri Olsa44f24cb2012-07-22 14:14:32 +020064 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
Namhyung Kimc00c48f2014-11-04 10:14:27 +090065 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020066 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
Jiri Olsa44f24cb2012-07-22 14:14:32 +020067 DSO_BINARY_TYPE__NOT_FOUND,
68};
69
Jiri Olsa028df762012-08-01 14:47:57 +020070#define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
Jiri Olsa44f24cb2012-07-22 14:14:32 +020071
Arnaldo Carvalho de Melo36a3e642010-01-04 16:19:27 -020072bool symbol_type__is_a(char symbol_type, enum map_type map_type)
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020073{
Anton Blanchard31877902011-08-24 16:40:16 +100074 symbol_type = toupper(symbol_type);
75
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020076 switch (map_type) {
77 case MAP__FUNCTION:
78 return symbol_type == 'T' || symbol_type == 'W';
Arnaldo Carvalho de Melof1dfa0b2009-12-11 14:50:39 -020079 case MAP__VARIABLE:
Anton Blanchard31877902011-08-24 16:40:16 +100080 return symbol_type == 'D';
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020081 default:
82 return false;
83 }
84}
85
Anton Blanchard694bf402011-08-24 16:40:17 +100086static int prefix_underscores_count(const char *str)
87{
88 const char *tail = str;
89
90 while (*tail == '_')
91 tail++;
92
93 return tail - str;
94}
95
Paul Clarked80406452017-04-25 13:15:49 -050096int __weak arch__compare_symbol_names(const char *namea, const char *nameb)
97{
98 return strcmp(namea, nameb);
99}
100
101int __weak arch__compare_symbol_names_n(const char *namea, const char *nameb,
102 unsigned int n)
103{
104 return strncmp(namea, nameb, n);
105}
106
Naveen N. Raofb6d5942015-04-28 17:35:36 +0530107int __weak arch__choose_best_symbol(struct symbol *syma,
108 struct symbol *symb __maybe_unused)
109{
110 /* Avoid "SyS" kernel syscall aliases */
111 if (strlen(syma->name) >= 3 && !strncmp(syma->name, "SyS", 3))
112 return SYMBOL_B;
113 if (strlen(syma->name) >= 10 && !strncmp(syma->name, "compat_SyS", 10))
114 return SYMBOL_B;
115
116 return SYMBOL_A;
117}
Anton Blanchard694bf402011-08-24 16:40:17 +1000118
119static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
120{
121 s64 a;
122 s64 b;
Adrian Hunter34454322013-08-07 14:38:49 +0300123 size_t na, nb;
Anton Blanchard694bf402011-08-24 16:40:17 +1000124
125 /* Prefer a symbol with non zero length */
126 a = syma->end - syma->start;
127 b = symb->end - symb->start;
128 if ((b == 0) && (a > 0))
129 return SYMBOL_A;
130 else if ((a == 0) && (b > 0))
131 return SYMBOL_B;
132
133 /* Prefer a non weak symbol over a weak one */
134 a = syma->binding == STB_WEAK;
135 b = symb->binding == STB_WEAK;
136 if (b && !a)
137 return SYMBOL_A;
138 if (a && !b)
139 return SYMBOL_B;
140
141 /* Prefer a global symbol over a non global one */
142 a = syma->binding == STB_GLOBAL;
143 b = symb->binding == STB_GLOBAL;
144 if (a && !b)
145 return SYMBOL_A;
146 if (b && !a)
147 return SYMBOL_B;
148
149 /* Prefer a symbol with less underscores */
150 a = prefix_underscores_count(syma->name);
151 b = prefix_underscores_count(symb->name);
152 if (b > a)
153 return SYMBOL_A;
154 else if (a > b)
155 return SYMBOL_B;
156
Adrian Hunter34454322013-08-07 14:38:49 +0300157 /* Choose the symbol with the longest name */
158 na = strlen(syma->name);
159 nb = strlen(symb->name);
160 if (na > nb)
Anton Blanchard694bf402011-08-24 16:40:17 +1000161 return SYMBOL_A;
Adrian Hunter34454322013-08-07 14:38:49 +0300162 else if (na < nb)
Anton Blanchard694bf402011-08-24 16:40:17 +1000163 return SYMBOL_B;
Adrian Hunter34454322013-08-07 14:38:49 +0300164
Naveen N. Raofb6d5942015-04-28 17:35:36 +0530165 return arch__choose_best_symbol(syma, symb);
Anton Blanchard694bf402011-08-24 16:40:17 +1000166}
167
Namhyung Kime5a18452012-08-06 13:41:20 +0900168void symbols__fixup_duplicate(struct rb_root *symbols)
Anton Blanchard694bf402011-08-24 16:40:17 +1000169{
170 struct rb_node *nd;
171 struct symbol *curr, *next;
172
Arnaldo Carvalho de Meloc97b40e2016-09-01 10:56:06 -0300173 if (symbol_conf.allow_aliases)
174 return;
175
Anton Blanchard694bf402011-08-24 16:40:17 +1000176 nd = rb_first(symbols);
177
178 while (nd) {
179 curr = rb_entry(nd, struct symbol, rb_node);
180again:
181 nd = rb_next(&curr->rb_node);
182 next = rb_entry(nd, struct symbol, rb_node);
183
184 if (!nd)
185 break;
186
187 if (curr->start != next->start)
188 continue;
189
190 if (choose_best_symbol(curr, next) == SYMBOL_A) {
191 rb_erase(&next->rb_node, symbols);
Chenggang Qind4f74eb2013-10-11 08:27:59 +0800192 symbol__delete(next);
Anton Blanchard694bf402011-08-24 16:40:17 +1000193 goto again;
194 } else {
195 nd = rb_next(&curr->rb_node);
196 rb_erase(&curr->rb_node, symbols);
Chenggang Qind4f74eb2013-10-11 08:27:59 +0800197 symbol__delete(curr);
Anton Blanchard694bf402011-08-24 16:40:17 +1000198 }
199 }
200}
201
Namhyung Kime5a18452012-08-06 13:41:20 +0900202void symbols__fixup_end(struct rb_root *symbols)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300203{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300204 struct rb_node *nd, *prevnd = rb_first(symbols);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300205 struct symbol *curr, *prev;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300206
207 if (prevnd == NULL)
208 return;
209
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300210 curr = rb_entry(prevnd, struct symbol, rb_node);
211
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300212 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300213 prev = curr;
214 curr = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300215
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200216 if (prev->end == prev->start && prev->end != curr->start)
Arnaldo Carvalho de Melo2c241bd2014-10-14 17:19:44 -0300217 prev->end = curr->start;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300218 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300219
220 /* Last entry */
221 if (curr->end == curr->start)
Daniel Borkmanne7ede722017-03-15 22:53:37 +0100222 curr->end = roundup(curr->start, 4096) + 4096;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300223}
224
Namhyung Kime5a18452012-08-06 13:41:20 +0900225void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300226{
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -0300227 struct maps *maps = &mg->maps[type];
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300228 struct map *next, *curr;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300229
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300230 down_write(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300231
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300232 curr = maps__first(maps);
233 if (curr == NULL)
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300234 goto out_unlock;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300235
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300236 for (next = map__next(curr); next; next = map__next(curr)) {
Thomas Richter9ad46522017-08-03 15:49:02 +0200237 if (!curr->end)
238 curr->end = next->start;
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300239 curr = next;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300240 }
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -0200241
242 /*
243 * We still haven't the actual symbols, so guess the
244 * last map final address.
245 */
Thomas Richter9ad46522017-08-03 15:49:02 +0200246 if (!curr->end)
247 curr->end = ~0ULL;
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300248
249out_unlock:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300250 up_write(&maps->lock);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300251}
252
Namhyung Kime5a18452012-08-06 13:41:20 +0900253struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300254{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300255 size_t namelen = strlen(name) + 1;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300256 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
257 sizeof(*sym) + namelen));
258 if (sym == NULL)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200259 return NULL;
260
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -0300261 if (symbol_conf.priv_size) {
262 if (symbol_conf.init_annotation) {
263 struct annotation *notes = (void *)sym;
264 pthread_mutex_init(&notes->lock, NULL);
265 }
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300266 sym = ((void *)sym) + symbol_conf.priv_size;
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -0300267 }
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200268
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300269 sym->start = start;
Arnaldo Carvalho de Melo2c241bd2014-10-14 17:19:44 -0300270 sym->end = len ? start + len : start;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300271 sym->binding = binding;
272 sym->namelen = namelen - 1;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200273
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300274 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
275 __func__, name, start, sym->end);
276 memcpy(sym->name, name, namelen);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200277
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300278 return sym;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300279}
280
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300281void symbol__delete(struct symbol *sym)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300282{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300283 free(((void *)sym) - symbol_conf.priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300284}
285
Jiri Olsacdd059d2012-10-27 23:18:32 +0200286void symbols__delete(struct rb_root *symbols)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300287{
288 struct symbol *pos;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300289 struct rb_node *next = rb_first(symbols);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300290
291 while (next) {
292 pos = rb_entry(next, struct symbol, rb_node);
293 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300294 rb_erase(&pos->rb_node, symbols);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200295 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300296 }
297}
298
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300299void __symbols__insert(struct rb_root *symbols, struct symbol *sym, bool kernel)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300300{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300301 struct rb_node **p = &symbols->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300302 struct rb_node *parent = NULL;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000303 const u64 ip = sym->start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300304 struct symbol *s;
305
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300306 if (kernel) {
307 const char *name = sym->name;
308 /*
309 * ppc64 uses function descriptors and appends a '.' to the
310 * start of every instruction address. Remove it.
311 */
312 if (name[0] == '.')
313 name++;
314 sym->idle = symbol__is_idle(name);
315 }
316
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300317 while (*p != NULL) {
318 parent = *p;
319 s = rb_entry(parent, struct symbol, rb_node);
320 if (ip < s->start)
321 p = &(*p)->rb_left;
322 else
323 p = &(*p)->rb_right;
324 }
325 rb_link_node(&sym->rb_node, parent, p);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300326 rb_insert_color(&sym->rb_node, symbols);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300327}
328
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300329void symbols__insert(struct rb_root *symbols, struct symbol *sym)
330{
331 __symbols__insert(symbols, sym, false);
332}
333
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300334static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300335{
336 struct rb_node *n;
337
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300338 if (symbols == NULL)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300339 return NULL;
340
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300341 n = symbols->rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300342
343 while (n) {
344 struct symbol *s = rb_entry(n, struct symbol, rb_node);
345
346 if (ip < s->start)
347 n = n->rb_left;
Chris Phlipot9c7b37c2016-05-07 02:16:59 -0700348 else if (ip > s->end || (ip == s->end && ip != s->start))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300349 n = n->rb_right;
350 else
351 return s;
352 }
353
354 return NULL;
355}
356
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300357static struct symbol *symbols__first(struct rb_root *symbols)
358{
359 struct rb_node *n = rb_first(symbols);
360
361 if (n)
362 return rb_entry(n, struct symbol, rb_node);
363
364 return NULL;
365}
366
Adrian Huntercd67f992016-09-23 17:38:38 +0300367static struct symbol *symbols__last(struct rb_root *symbols)
368{
369 struct rb_node *n = rb_last(symbols);
370
371 if (n)
372 return rb_entry(n, struct symbol, rb_node);
373
374 return NULL;
375}
376
Adrian Hunter9c00a812014-07-14 13:02:50 +0300377static struct symbol *symbols__next(struct symbol *sym)
378{
379 struct rb_node *n = rb_next(&sym->rb_node);
380
381 if (n)
382 return rb_entry(n, struct symbol, rb_node);
383
384 return NULL;
385}
386
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300387static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200388{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300389 struct rb_node **p = &symbols->rb_node;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200390 struct rb_node *parent = NULL;
Rabin Vincent02a9d032010-11-23 22:08:18 +0530391 struct symbol_name_rb_node *symn, *s;
392
393 symn = container_of(sym, struct symbol_name_rb_node, sym);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200394
395 while (*p != NULL) {
396 parent = *p;
397 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
398 if (strcmp(sym->name, s->sym.name) < 0)
399 p = &(*p)->rb_left;
400 else
401 p = &(*p)->rb_right;
402 }
403 rb_link_node(&symn->rb_node, parent, p);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300404 rb_insert_color(&symn->rb_node, symbols);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200405}
406
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300407static void symbols__sort_by_name(struct rb_root *symbols,
408 struct rb_root *source)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200409{
410 struct rb_node *nd;
411
412 for (nd = rb_first(source); nd; nd = rb_next(nd)) {
413 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300414 symbols__insert_by_name(symbols, pos);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200415 }
416}
417
Paul Clarked80406452017-04-25 13:15:49 -0500418int symbol__match_symbol_name(const char *name, const char *str,
419 enum symbol_tag_include includes)
420{
421 const char *versioning;
422
423 if (includes == SYMBOL_TAG_INCLUDE__DEFAULT_ONLY &&
424 (versioning = strstr(name, "@@"))) {
425 int len = strlen(str);
426
427 if (len < versioning - name)
428 len = versioning - name;
429
430 return arch__compare_symbol_names_n(name, str, len);
431 } else
432 return arch__compare_symbol_names(name, str);
433}
434
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300435static struct symbol *symbols__find_by_name(struct rb_root *symbols,
Paul Clarked80406452017-04-25 13:15:49 -0500436 const char *name,
437 enum symbol_tag_include includes)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200438{
439 struct rb_node *n;
Martin Liška5bcaaca2015-05-26 11:41:37 -0300440 struct symbol_name_rb_node *s = NULL;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200441
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300442 if (symbols == NULL)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200443 return NULL;
444
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300445 n = symbols->rb_node;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200446
447 while (n) {
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200448 int cmp;
449
450 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
Paul Clarked80406452017-04-25 13:15:49 -0500451 cmp = symbol__match_symbol_name(s->sym.name, name, includes);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200452
Paul Clarked80406452017-04-25 13:15:49 -0500453 if (cmp > 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200454 n = n->rb_left;
Paul Clarked80406452017-04-25 13:15:49 -0500455 else if (cmp < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200456 n = n->rb_right;
457 else
Namhyung Kimde480992015-01-16 15:31:28 -0300458 break;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200459 }
460
Namhyung Kimde480992015-01-16 15:31:28 -0300461 if (n == NULL)
462 return NULL;
463
Paul Clarked80406452017-04-25 13:15:49 -0500464 if (includes != SYMBOL_TAG_INCLUDE__DEFAULT_ONLY)
465 /* return first symbol that has same name (if any) */
466 for (n = rb_prev(n); n; n = rb_prev(n)) {
467 struct symbol_name_rb_node *tmp;
Namhyung Kimde480992015-01-16 15:31:28 -0300468
Paul Clarked80406452017-04-25 13:15:49 -0500469 tmp = rb_entry(n, struct symbol_name_rb_node, rb_node);
470 if (arch__compare_symbol_names(tmp->sym.name, s->sym.name))
471 break;
Namhyung Kimde480992015-01-16 15:31:28 -0300472
Paul Clarked80406452017-04-25 13:15:49 -0500473 s = tmp;
474 }
Namhyung Kimde480992015-01-16 15:31:28 -0300475
476 return &s->sym;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200477}
478
Arnaldo Carvalho de Meloc0b4dff2015-08-24 13:33:14 -0300479void dso__reset_find_symbol_cache(struct dso *dso)
480{
481 enum map_type type;
482
483 for (type = MAP__FUNCTION; type <= MAP__VARIABLE; ++type) {
484 dso->last_find_result[type].addr = 0;
485 dso->last_find_result[type].symbol = NULL;
486 }
487}
488
Chris Phlipotae93a6c2016-05-10 20:26:46 -0700489void dso__insert_symbol(struct dso *dso, enum map_type type, struct symbol *sym)
490{
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300491 __symbols__insert(&dso->symbols[type], sym, dso->kernel);
Chris Phlipotae93a6c2016-05-10 20:26:46 -0700492
493 /* update the symbol cache if necessary */
494 if (dso->last_find_result[type].addr >= sym->start &&
495 (dso->last_find_result[type].addr < sym->end ||
496 sym->start == sym->end)) {
497 dso->last_find_result[type].symbol = sym;
498 }
499}
500
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300501struct symbol *dso__find_symbol(struct dso *dso,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200502 enum map_type type, u64 addr)
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200503{
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -0300504 if (dso->last_find_result[type].addr != addr || dso->last_find_result[type].symbol == NULL) {
Arnaldo Carvalho de Melob685ac22015-07-22 12:52:17 -0300505 dso->last_find_result[type].addr = addr;
506 dso->last_find_result[type].symbol = symbols__find(&dso->symbols[type], addr);
507 }
508
509 return dso->last_find_result[type].symbol;
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200510}
511
Adrian Hunter9c00a812014-07-14 13:02:50 +0300512struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300513{
514 return symbols__first(&dso->symbols[type]);
515}
516
Adrian Huntercd67f992016-09-23 17:38:38 +0300517struct symbol *dso__last_symbol(struct dso *dso, enum map_type type)
518{
519 return symbols__last(&dso->symbols[type]);
520}
521
Adrian Hunter9c00a812014-07-14 13:02:50 +0300522struct symbol *dso__next_symbol(struct symbol *sym)
523{
524 return symbols__next(sym);
525}
526
Arnaldo Carvalho de Melo18bd7262015-01-16 15:39:53 -0300527struct symbol *symbol__next_by_name(struct symbol *sym)
528{
529 struct symbol_name_rb_node *s = container_of(sym, struct symbol_name_rb_node, sym);
530 struct rb_node *n = rb_next(&s->rb_node);
531
532 return n ? &rb_entry(n, struct symbol_name_rb_node, rb_node)->sym : NULL;
533}
534
535 /*
536 * Teturns first symbol that matched with @name.
537 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300538struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200539 const char *name)
540{
Paul Clarked80406452017-04-25 13:15:49 -0500541 struct symbol *s = symbols__find_by_name(&dso->symbol_names[type], name,
542 SYMBOL_TAG_INCLUDE__NONE);
543 if (!s)
544 s = symbols__find_by_name(&dso->symbol_names[type], name,
545 SYMBOL_TAG_INCLUDE__DEFAULT_ONLY);
546 return s;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200547}
548
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300549void dso__sort_by_name(struct dso *dso, enum map_type type)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200550{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300551 dso__set_sorted_by_name(dso, type);
552 return symbols__sort_by_name(&dso->symbol_names[type],
553 &dso->symbols[type]);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200554}
555
Adrian Hunter316d70d2013-10-08 11:45:48 +0300556int modules__parse(const char *filename, void *arg,
557 int (*process_module)(void *arg, const char *name,
Thomas Richter9ad46522017-08-03 15:49:02 +0200558 u64 start, u64 size))
Adrian Hunter316d70d2013-10-08 11:45:48 +0300559{
560 char *line = NULL;
561 size_t n;
562 FILE *file;
563 int err = 0;
564
565 file = fopen(filename, "r");
566 if (file == NULL)
567 return -1;
568
569 while (1) {
570 char name[PATH_MAX];
Thomas Richter9ad46522017-08-03 15:49:02 +0200571 u64 start, size;
572 char *sep, *endptr;
Adrian Hunter316d70d2013-10-08 11:45:48 +0300573 ssize_t line_len;
574
575 line_len = getline(&line, &n, file);
576 if (line_len < 0) {
577 if (feof(file))
578 break;
579 err = -1;
580 goto out;
581 }
582
583 if (!line) {
584 err = -1;
585 goto out;
586 }
587
588 line[--line_len] = '\0'; /* \n */
589
590 sep = strrchr(line, 'x');
591 if (sep == NULL)
592 continue;
593
594 hex2u64(sep + 1, &start);
595
596 sep = strchr(line, ' ');
597 if (sep == NULL)
598 continue;
599
600 *sep = '\0';
601
602 scnprintf(name, sizeof(name), "[%s]", line);
603
Thomas Richter9ad46522017-08-03 15:49:02 +0200604 size = strtoul(sep + 1, &endptr, 0);
605 if (*endptr != ' ' && *endptr != '\t')
606 continue;
607
608 err = process_module(arg, name, start, size);
Adrian Hunter316d70d2013-10-08 11:45:48 +0300609 if (err)
610 break;
611 }
612out:
613 free(line);
614 fclose(file);
615 return err;
616}
617
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200618struct process_kallsyms_args {
619 struct map *map;
620 struct dso *dso;
621};
622
Arnaldo Carvalho de Meloe7110b92014-08-08 18:00:39 -0300623/*
624 * These are symbols in the kernel image, so make sure that
625 * sym is from a kernel DSO.
626 */
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300627static bool symbol__is_idle(const char *name)
David Ahern82d1deb2013-11-18 13:32:45 -0700628{
629 const char * const idle_symbols[] = {
630 "cpu_idle",
Arnaldo Carvalho de Meloe0336ed2014-08-08 18:02:41 -0300631 "cpu_startup_entry",
David Ahern82d1deb2013-11-18 13:32:45 -0700632 "intel_idle",
633 "default_idle",
634 "native_safe_halt",
635 "enter_idle",
636 "exit_idle",
637 "mwait_idle",
638 "mwait_idle_with_hints",
639 "poll_idle",
640 "ppc64_runlatch_off",
641 "pseries_dedicated_idle_sleep",
642 NULL
643 };
David Ahern82d1deb2013-11-18 13:32:45 -0700644 int i;
645
David Ahern82d1deb2013-11-18 13:32:45 -0700646 for (i = 0; idle_symbols[i]; i++) {
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300647 if (!strcmp(idle_symbols[i], name))
David Ahern82d1deb2013-11-18 13:32:45 -0700648 return true;
649 }
650
651 return false;
652}
653
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200654static int map__process_kallsym_symbol(void *arg, const char *name,
Cody P Schafer82151522012-08-10 15:22:48 -0700655 char type, u64 start)
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200656{
657 struct symbol *sym;
658 struct process_kallsyms_args *a = arg;
659 struct rb_root *root = &a->dso->symbols[a->map->type];
660
661 if (!symbol_type__is_a(type, a->map->type))
662 return 0;
663
Cody P Schafer82151522012-08-10 15:22:48 -0700664 /*
665 * module symbols are not sorted so we add all
666 * symbols, setting length to 0, and rely on
667 * symbols__fixup_end() to fix it up.
668 */
Arnaldo Carvalho de Melo8e947f12015-09-30 12:42:07 -0300669 sym = symbol__new(start, 0, kallsyms2elf_binding(type), name);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200670 if (sym == NULL)
671 return -ENOMEM;
672 /*
673 * We will pass the symbols to the filter later, in
674 * map__split_kallsyms, when we have split the maps per module
675 */
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300676 __symbols__insert(root, sym, !strchr(name, '['));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800677
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200678 return 0;
679}
680
681/*
682 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
683 * so that we can in the next step set the symbol ->end address and then
684 * call kernel_maps__split_kallsyms.
685 */
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300686static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200687 struct map *map)
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200688{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300689 struct process_kallsyms_args args = { .map = map, .dso = dso, };
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200690 return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200691}
692
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300693static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map)
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300694{
Wang Nanba927322015-04-07 08:22:45 +0000695 struct map_groups *kmaps = map__kmaps(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300696 struct map *curr_map;
697 struct symbol *pos;
Adrian Hunter866548d2015-11-06 15:59:29 +0200698 int count = 0;
699 struct rb_root old_root = dso->symbols[map->type];
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300700 struct rb_root *root = &dso->symbols[map->type];
701 struct rb_node *next = rb_first(root);
702
Wang Nanba927322015-04-07 08:22:45 +0000703 if (!kmaps)
704 return -1;
705
Adrian Hunter866548d2015-11-06 15:59:29 +0200706 *root = RB_ROOT;
707
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300708 while (next) {
709 char *module;
710
711 pos = rb_entry(next, struct symbol, rb_node);
712 next = rb_next(&pos->rb_node);
713
Adrian Hunter866548d2015-11-06 15:59:29 +0200714 rb_erase_init(&pos->rb_node, &old_root);
715
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300716 module = strchr(pos->name, '\t');
717 if (module)
718 *module = '\0';
719
720 curr_map = map_groups__find(kmaps, map->type, pos->start);
721
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300722 if (!curr_map) {
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300723 symbol__delete(pos);
Adrian Hunter866548d2015-11-06 15:59:29 +0200724 continue;
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300725 }
Adrian Hunter866548d2015-11-06 15:59:29 +0200726
727 pos->start -= curr_map->start - curr_map->pgoff;
728 if (pos->end)
729 pos->end -= curr_map->start - curr_map->pgoff;
730 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
731 ++count;
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300732 }
733
734 /* Symbols have been adjusted */
735 dso->adjust_symbols = 1;
736
Adrian Hunter866548d2015-11-06 15:59:29 +0200737 return count;
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300738}
739
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300740/*
741 * Split the symbols into maps, making sure there are no overlaps, i.e. the
742 * kernel range is broken in several maps, named [kernel].N, as we don't have
743 * the original ELF section names vmlinux have.
744 */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300745static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300746{
Wang Nanba927322015-04-07 08:22:45 +0000747 struct map_groups *kmaps = map__kmaps(map);
748 struct machine *machine;
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200749 struct map *curr_map = map;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300750 struct symbol *pos;
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300751 int count = 0, moved = 0;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300752 struct rb_root *root = &dso->symbols[map->type];
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200753 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300754 int kernel_range = 0;
755
Wang Nanba927322015-04-07 08:22:45 +0000756 if (!kmaps)
757 return -1;
758
759 machine = kmaps->machine;
760
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300761 while (next) {
762 char *module;
763
764 pos = rb_entry(next, struct symbol, rb_node);
765 next = rb_next(&pos->rb_node);
766
767 module = strchr(pos->name, '\t');
768 if (module) {
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200769 if (!symbol_conf.use_modules)
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200770 goto discard_symbol;
771
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300772 *module++ = '\0';
773
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200774 if (strcmp(curr_map->dso->short_name, module)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800775 if (curr_map != map &&
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300776 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300777 machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800778 /*
779 * We assume all symbols of a module are
780 * continuous in * kallsyms, so curr_map
781 * points to a module and all its
782 * symbols are in its kmap. Mark it as
783 * loaded.
784 */
785 dso__set_loaded(curr_map->dso,
786 curr_map->type);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300787 }
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200788
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800789 curr_map = map_groups__find_by_name(kmaps,
790 map->type, module);
791 if (curr_map == NULL) {
Arnaldo Carvalho de Melo2f519032010-05-17 17:57:59 -0300792 pr_debug("%s/proc/{kallsyms,modules} "
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800793 "inconsistency while looking "
794 "for \"%s\" module!\n",
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300795 machine->root_dir, module);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800796 curr_map = map;
797 goto discard_symbol;
798 }
799
800 if (curr_map->dso->loaded &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300801 !machine__is_default_guest(machine))
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -0200802 goto discard_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300803 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300804 /*
805 * So that we look just like we get from .ko files,
806 * i.e. not prelinked, relative to map->start.
807 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200808 pos->start = curr_map->map_ip(curr_map, pos->start);
809 pos->end = curr_map->map_ip(curr_map, pos->end);
810 } else if (curr_map != map) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300811 char dso_name[PATH_MAX];
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300812 struct dso *ndso;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300813
Adrian Hunterd9b62ab2014-01-29 16:14:43 +0200814 if (delta) {
815 /* Kernel was relocated at boot time */
816 pos->start -= delta;
817 pos->end -= delta;
818 }
819
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200820 if (count == 0) {
821 curr_map = map;
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300822 goto add_symbol;
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200823 }
824
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300825 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800826 snprintf(dso_name, sizeof(dso_name),
827 "[guest.kernel].%d",
828 kernel_range++);
829 else
830 snprintf(dso_name, sizeof(dso_name),
831 "[kernel].%d",
832 kernel_range++);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300833
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300834 ndso = dso__new(dso_name);
835 if (ndso == NULL)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300836 return -1;
837
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300838 ndso->kernel = dso->kernel;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800839
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300840 curr_map = map__new2(pos->start, ndso, map->type);
Zhang, Yanmin37fe5fc2010-02-25 11:00:51 +0800841 if (curr_map == NULL) {
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300842 dso__put(ndso);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300843 return -1;
844 }
845
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200846 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200847 map_groups__insert(kmaps, curr_map);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300848 ++kernel_range;
Adrian Hunterd9b62ab2014-01-29 16:14:43 +0200849 } else if (delta) {
850 /* Kernel was relocated at boot time */
851 pos->start -= delta;
852 pos->end -= delta;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300853 }
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300854add_symbol:
855 if (curr_map != map) {
856 rb_erase(&pos->rb_node, root);
857 symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
858 ++moved;
859 } else
860 ++count;
861
862 continue;
863discard_symbol:
864 rb_erase(&pos->rb_node, root);
865 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300866 }
867
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800868 if (curr_map != map &&
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300869 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300870 machine__is_default_guest(kmaps->machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800871 dso__set_loaded(curr_map->dso, curr_map->type);
872 }
873
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200874 return count + moved;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300875}
876
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300877bool symbol__restricted_filename(const char *filename,
878 const char *restricted_filename)
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -0300879{
880 bool restricted = false;
881
882 if (symbol_conf.kptr_restrict) {
883 char *r = realpath(filename, NULL);
884
885 if (r != NULL) {
886 restricted = strcmp(r, restricted_filename) == 0;
887 free(r);
888 return restricted;
889 }
890 }
891
892 return restricted;
893}
894
Adrian Hunter52afdaf2013-10-09 15:01:11 +0300895struct module_info {
896 struct rb_node rb_node;
897 char *name;
898 u64 start;
899};
900
901static void add_module(struct module_info *mi, struct rb_root *modules)
902{
903 struct rb_node **p = &modules->rb_node;
904 struct rb_node *parent = NULL;
905 struct module_info *m;
906
907 while (*p != NULL) {
908 parent = *p;
909 m = rb_entry(parent, struct module_info, rb_node);
910 if (strcmp(mi->name, m->name) < 0)
911 p = &(*p)->rb_left;
912 else
913 p = &(*p)->rb_right;
914 }
915 rb_link_node(&mi->rb_node, parent, p);
916 rb_insert_color(&mi->rb_node, modules);
917}
918
919static void delete_modules(struct rb_root *modules)
920{
921 struct module_info *mi;
922 struct rb_node *next = rb_first(modules);
923
924 while (next) {
925 mi = rb_entry(next, struct module_info, rb_node);
926 next = rb_next(&mi->rb_node);
927 rb_erase(&mi->rb_node, modules);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300928 zfree(&mi->name);
Adrian Hunter52afdaf2013-10-09 15:01:11 +0300929 free(mi);
930 }
931}
932
933static struct module_info *find_module(const char *name,
934 struct rb_root *modules)
935{
936 struct rb_node *n = modules->rb_node;
937
938 while (n) {
939 struct module_info *m;
940 int cmp;
941
942 m = rb_entry(n, struct module_info, rb_node);
943 cmp = strcmp(name, m->name);
944 if (cmp < 0)
945 n = n->rb_left;
946 else if (cmp > 0)
947 n = n->rb_right;
948 else
949 return m;
950 }
951
952 return NULL;
953}
954
Thomas Richter9ad46522017-08-03 15:49:02 +0200955static int __read_proc_modules(void *arg, const char *name, u64 start,
956 u64 size __maybe_unused)
Adrian Hunter52afdaf2013-10-09 15:01:11 +0300957{
958 struct rb_root *modules = arg;
959 struct module_info *mi;
960
961 mi = zalloc(sizeof(struct module_info));
962 if (!mi)
963 return -ENOMEM;
964
965 mi->name = strdup(name);
966 mi->start = start;
967
968 if (!mi->name) {
969 free(mi);
970 return -ENOMEM;
971 }
972
973 add_module(mi, modules);
974
975 return 0;
976}
977
978static int read_proc_modules(const char *filename, struct rb_root *modules)
979{
980 if (symbol__restricted_filename(filename, "/proc/modules"))
981 return -1;
982
983 if (modules__parse(filename, modules, __read_proc_modules)) {
984 delete_modules(modules);
985 return -1;
986 }
987
988 return 0;
989}
990
Adrian Hunterfc1b6912013-10-14 16:57:29 +0300991int compare_proc_modules(const char *from, const char *to)
992{
993 struct rb_root from_modules = RB_ROOT;
994 struct rb_root to_modules = RB_ROOT;
995 struct rb_node *from_node, *to_node;
996 struct module_info *from_m, *to_m;
997 int ret = -1;
998
999 if (read_proc_modules(from, &from_modules))
1000 return -1;
1001
1002 if (read_proc_modules(to, &to_modules))
1003 goto out_delete_from;
1004
1005 from_node = rb_first(&from_modules);
1006 to_node = rb_first(&to_modules);
1007 while (from_node) {
1008 if (!to_node)
1009 break;
1010
1011 from_m = rb_entry(from_node, struct module_info, rb_node);
1012 to_m = rb_entry(to_node, struct module_info, rb_node);
1013
1014 if (from_m->start != to_m->start ||
1015 strcmp(from_m->name, to_m->name))
1016 break;
1017
1018 from_node = rb_next(from_node);
1019 to_node = rb_next(to_node);
1020 }
1021
1022 if (!from_node && !to_node)
1023 ret = 0;
1024
1025 delete_modules(&to_modules);
1026out_delete_from:
1027 delete_modules(&from_modules);
1028
1029 return ret;
1030}
1031
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001032static int do_validate_kcore_modules(const char *filename, struct map *map,
1033 struct map_groups *kmaps)
1034{
1035 struct rb_root modules = RB_ROOT;
1036 struct map *old_map;
1037 int err;
1038
1039 err = read_proc_modules(filename, &modules);
1040 if (err)
1041 return err;
1042
1043 old_map = map_groups__first(kmaps, map->type);
1044 while (old_map) {
1045 struct map *next = map_groups__next(old_map);
1046 struct module_info *mi;
1047
1048 if (old_map == map || old_map->start == map->start) {
1049 /* The kernel map */
1050 old_map = next;
1051 continue;
1052 }
1053
1054 /* Module must be in memory at the same address */
1055 mi = find_module(old_map->dso->short_name, &modules);
1056 if (!mi || mi->start != old_map->start) {
1057 err = -EINVAL;
1058 goto out;
1059 }
1060
1061 old_map = next;
1062 }
1063out:
1064 delete_modules(&modules);
1065 return err;
1066}
1067
1068/*
1069 * If kallsyms is referenced by name then we look for filename in the same
1070 * directory.
1071 */
1072static bool filename_from_kallsyms_filename(char *filename,
1073 const char *base_name,
1074 const char *kallsyms_filename)
1075{
1076 char *name;
1077
1078 strcpy(filename, kallsyms_filename);
1079 name = strrchr(filename, '/');
1080 if (!name)
1081 return false;
1082
1083 name += 1;
1084
1085 if (!strcmp(name, "kallsyms")) {
1086 strcpy(name, base_name);
1087 return true;
1088 }
1089
1090 return false;
1091}
1092
1093static int validate_kcore_modules(const char *kallsyms_filename,
1094 struct map *map)
1095{
Wang Nanba927322015-04-07 08:22:45 +00001096 struct map_groups *kmaps = map__kmaps(map);
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001097 char modules_filename[PATH_MAX];
1098
Wang Nanba927322015-04-07 08:22:45 +00001099 if (!kmaps)
1100 return -EINVAL;
1101
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001102 if (!filename_from_kallsyms_filename(modules_filename, "modules",
1103 kallsyms_filename))
1104 return -EINVAL;
1105
1106 if (do_validate_kcore_modules(modules_filename, map, kmaps))
1107 return -EINVAL;
1108
1109 return 0;
1110}
1111
Adrian Huntera00d28c2014-01-29 16:14:41 +02001112static int validate_kcore_addresses(const char *kallsyms_filename,
1113 struct map *map)
1114{
1115 struct kmap *kmap = map__kmap(map);
1116
Wang Nanba927322015-04-07 08:22:45 +00001117 if (!kmap)
1118 return -EINVAL;
1119
Adrian Huntera00d28c2014-01-29 16:14:41 +02001120 if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
1121 u64 start;
1122
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001123 if (kallsyms__get_function_start(kallsyms_filename,
1124 kmap->ref_reloc_sym->name, &start))
1125 return -ENOENT;
Adrian Huntera00d28c2014-01-29 16:14:41 +02001126 if (start != kmap->ref_reloc_sym->addr)
1127 return -EINVAL;
1128 }
1129
1130 return validate_kcore_modules(kallsyms_filename, map);
1131}
1132
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001133struct kcore_mapfn_data {
1134 struct dso *dso;
1135 enum map_type type;
1136 struct list_head maps;
1137};
1138
1139static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1140{
1141 struct kcore_mapfn_data *md = data;
1142 struct map *map;
1143
1144 map = map__new2(start, md->dso, md->type);
1145 if (map == NULL)
1146 return -ENOMEM;
1147
1148 map->end = map->start + len;
1149 map->pgoff = pgoff;
1150
1151 list_add(&map->node, &md->maps);
1152
1153 return 0;
1154}
1155
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001156static int dso__load_kcore(struct dso *dso, struct map *map,
1157 const char *kallsyms_filename)
1158{
Wang Nanba927322015-04-07 08:22:45 +00001159 struct map_groups *kmaps = map__kmaps(map);
1160 struct machine *machine;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001161 struct kcore_mapfn_data md;
1162 struct map *old_map, *new_map, *replacement_map = NULL;
1163 bool is_64_bit;
1164 int err, fd;
1165 char kcore_filename[PATH_MAX];
1166 struct symbol *sym;
1167
Wang Nanba927322015-04-07 08:22:45 +00001168 if (!kmaps)
1169 return -EINVAL;
1170
1171 machine = kmaps->machine;
1172
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001173 /* This function requires that the map is the kernel map */
1174 if (map != machine->vmlinux_maps[map->type])
1175 return -EINVAL;
1176
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001177 if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1178 kallsyms_filename))
1179 return -EINVAL;
1180
Adrian Huntera00d28c2014-01-29 16:14:41 +02001181 /* Modules and kernel must be present at their original addresses */
1182 if (validate_kcore_addresses(kallsyms_filename, map))
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001183 return -EINVAL;
1184
1185 md.dso = dso;
1186 md.type = map->type;
1187 INIT_LIST_HEAD(&md.maps);
1188
1189 fd = open(kcore_filename, O_RDONLY);
Li Zhang36c8bb52015-06-19 16:57:33 +08001190 if (fd < 0) {
Adrian Hunter133de942015-08-20 13:07:40 +03001191 pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n",
1192 kcore_filename);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001193 return -EINVAL;
Li Zhang36c8bb52015-06-19 16:57:33 +08001194 }
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001195
1196 /* Read new maps into temporary lists */
1197 err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
1198 &is_64_bit);
1199 if (err)
1200 goto out_err;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +03001201 dso->is_64_bit = is_64_bit;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001202
1203 if (list_empty(&md.maps)) {
1204 err = -EINVAL;
1205 goto out_err;
1206 }
1207
1208 /* Remove old maps */
1209 old_map = map_groups__first(kmaps, map->type);
1210 while (old_map) {
1211 struct map *next = map_groups__next(old_map);
1212
1213 if (old_map != map)
1214 map_groups__remove(kmaps, old_map);
1215 old_map = next;
1216 }
1217
1218 /* Find the kernel map using the first symbol */
1219 sym = dso__first_symbol(dso, map->type);
1220 list_for_each_entry(new_map, &md.maps, node) {
1221 if (sym && sym->start >= new_map->start &&
1222 sym->start < new_map->end) {
1223 replacement_map = new_map;
1224 break;
1225 }
1226 }
1227
1228 if (!replacement_map)
1229 replacement_map = list_entry(md.maps.next, struct map, node);
1230
1231 /* Add new maps */
1232 while (!list_empty(&md.maps)) {
1233 new_map = list_entry(md.maps.next, struct map, node);
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -03001234 list_del_init(&new_map->node);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001235 if (new_map == replacement_map) {
1236 map->start = new_map->start;
1237 map->end = new_map->end;
1238 map->pgoff = new_map->pgoff;
1239 map->map_ip = new_map->map_ip;
1240 map->unmap_ip = new_map->unmap_ip;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001241 /* Ensure maps are correctly ordered */
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001242 map__get(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001243 map_groups__remove(kmaps, map);
1244 map_groups__insert(kmaps, map);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001245 map__put(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001246 } else {
1247 map_groups__insert(kmaps, new_map);
1248 }
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001249
1250 map__put(new_map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001251 }
1252
1253 /*
1254 * Set the data type and long name so that kcore can be read via
1255 * dso__data_read_addr().
1256 */
1257 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001258 dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001259 else
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001260 dso->binary_type = DSO_BINARY_TYPE__KCORE;
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001261 dso__set_long_name(dso, strdup(kcore_filename), true);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001262
1263 close(fd);
1264
1265 if (map->type == MAP__FUNCTION)
1266 pr_debug("Using %s for kernel object code\n", kcore_filename);
1267 else
1268 pr_debug("Using %s for kernel data\n", kcore_filename);
1269
1270 return 0;
1271
1272out_err:
1273 while (!list_empty(&md.maps)) {
1274 map = list_entry(md.maps.next, struct map, node);
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -03001275 list_del_init(&map->node);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001276 map__put(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001277 }
1278 close(fd);
1279 return -EINVAL;
1280}
1281
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001282/*
1283 * If the kernel is relocated at boot time, kallsyms won't match. Compute the
1284 * delta based on the relocation reference symbol.
1285 */
1286static int kallsyms__delta(struct map *map, const char *filename, u64 *delta)
1287{
1288 struct kmap *kmap = map__kmap(map);
1289 u64 addr;
1290
Wang Nanba927322015-04-07 08:22:45 +00001291 if (!kmap)
1292 return -1;
1293
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001294 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1295 return 0;
1296
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001297 if (kallsyms__get_function_start(filename, kmap->ref_reloc_sym->name, &addr))
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001298 return -1;
1299
1300 *delta = addr - kmap->ref_reloc_sym->addr;
1301 return 0;
1302}
1303
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001304int __dso__load_kallsyms(struct dso *dso, const char *filename,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001305 struct map *map, bool no_kcore)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001306{
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001307 u64 delta = 0;
1308
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03001309 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1310 return -1;
1311
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001312 if (dso__load_all_kallsyms(dso, filename, map) < 0)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001313 return -1;
1314
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001315 if (kallsyms__delta(map, filename, &delta))
1316 return -1;
1317
Anton Blanchard3f5a4272011-08-24 16:40:15 +10001318 symbols__fixup_end(&dso->symbols[map->type]);
Arnaldo Carvalho de Melo432746f2016-09-01 11:00:23 -03001319 symbols__fixup_duplicate(&dso->symbols[map->type]);
Anton Blanchard3f5a4272011-08-24 16:40:15 +10001320
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001321 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001322 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001323 else
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001324 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001325
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001326 if (!no_kcore && !dso__load_kcore(dso, map, filename))
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001327 return dso__split_kallsyms_for_kcore(dso, map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001328 else
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001329 return dso__split_kallsyms(dso, map, delta);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001330}
1331
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001332int dso__load_kallsyms(struct dso *dso, const char *filename,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001333 struct map *map)
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001334{
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001335 return __dso__load_kallsyms(dso, filename, map, false);
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001336}
1337
Krister Johansenbf2e7102017-07-05 18:48:09 -07001338static int dso__load_perf_map(const char *map_path, struct dso *dso,
1339 struct map *map)
Pekka Enberg80d496b2009-06-08 21:12:48 +03001340{
1341 char *line = NULL;
1342 size_t n;
1343 FILE *file;
1344 int nr_syms = 0;
1345
Krister Johansenbf2e7102017-07-05 18:48:09 -07001346 file = fopen(map_path, "r");
Pekka Enberg80d496b2009-06-08 21:12:48 +03001347 if (file == NULL)
1348 goto out_failure;
1349
1350 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001351 u64 start, size;
Pekka Enberg80d496b2009-06-08 21:12:48 +03001352 struct symbol *sym;
1353 int line_len, len;
1354
1355 line_len = getline(&line, &n, file);
1356 if (line_len < 0)
1357 break;
1358
1359 if (!line)
1360 goto out_failure;
1361
1362 line[--line_len] = '\0'; /* \n */
1363
1364 len = hex2u64(line, &start);
1365
1366 len++;
1367 if (len + 2 >= line_len)
1368 continue;
1369
1370 len += hex2u64(line + len, &size);
1371
1372 len++;
1373 if (len + 2 >= line_len)
1374 continue;
1375
Arnaldo Carvalho de Meloc408fed2010-08-05 12:59:47 -03001376 sym = symbol__new(start, size, STB_GLOBAL, line + len);
Pekka Enberg80d496b2009-06-08 21:12:48 +03001377
1378 if (sym == NULL)
1379 goto out_delete_line;
1380
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001381 symbols__insert(&dso->symbols[map->type], sym);
1382 nr_syms++;
Pekka Enberg80d496b2009-06-08 21:12:48 +03001383 }
1384
1385 free(line);
1386 fclose(file);
1387
1388 return nr_syms;
1389
1390out_delete_line:
1391 free(line);
1392out_failure:
1393 return -1;
1394}
1395
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001396static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1397 enum dso_binary_type type)
1398{
1399 switch (type) {
1400 case DSO_BINARY_TYPE__JAVA_JIT:
1401 case DSO_BINARY_TYPE__DEBUGLINK:
1402 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1403 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1404 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1405 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1406 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1407 return !kmod && dso->kernel == DSO_TYPE_USER;
1408
1409 case DSO_BINARY_TYPE__KALLSYMS:
1410 case DSO_BINARY_TYPE__VMLINUX:
1411 case DSO_BINARY_TYPE__KCORE:
1412 return dso->kernel == DSO_TYPE_KERNEL;
1413
1414 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1415 case DSO_BINARY_TYPE__GUEST_VMLINUX:
1416 case DSO_BINARY_TYPE__GUEST_KCORE:
1417 return dso->kernel == DSO_TYPE_GUEST_KERNEL;
1418
1419 case DSO_BINARY_TYPE__GUEST_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +09001420 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001421 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +09001422 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001423 /*
1424 * kernel modules know their symtab type - it's set when
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001425 * creating a module dso in machine__findnew_module_map().
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001426 */
1427 return kmod && dso->symtab_type == type;
1428
1429 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
Krister Johansend2396992017-07-05 18:48:13 -07001430 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001431 return true;
1432
1433 case DSO_BINARY_TYPE__NOT_FOUND:
1434 default:
1435 return false;
1436 }
1437}
1438
Krister Johansenbf2e7102017-07-05 18:48:09 -07001439/* Checks for the existence of the perf-<pid>.map file in two different
1440 * locations. First, if the process is a separate mount namespace, check in
1441 * that namespace using the pid of the innermost pid namespace. If's not in a
1442 * namespace, or the file can't be found there, try in the mount namespace of
1443 * the tracing process using our view of its pid.
1444 */
1445static int dso__find_perf_map(char *filebuf, size_t bufsz,
1446 struct nsinfo **nsip)
1447{
1448 struct nscookie nsc;
1449 struct nsinfo *nsi;
1450 struct nsinfo *nnsi;
1451 int rc = -1;
1452
1453 nsi = *nsip;
1454
1455 if (nsi->need_setns) {
1456 snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsi->nstgid);
1457 nsinfo__mountns_enter(nsi, &nsc);
1458 rc = access(filebuf, R_OK);
1459 nsinfo__mountns_exit(&nsc);
1460 if (rc == 0)
1461 return rc;
1462 }
1463
1464 nnsi = nsinfo__copy(nsi);
1465 if (nnsi) {
1466 nsinfo__put(nsi);
1467
1468 nnsi->need_setns = false;
1469 snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nnsi->tgid);
1470 *nsip = nnsi;
1471 rc = 0;
1472 }
1473
1474 return rc;
1475}
1476
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001477int dso__load(struct dso *dso, struct map *map)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001478{
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001479 char *name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001480 int ret = -1;
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001481 u_int i;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001482 struct machine *machine;
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001483 char *root_dir = (char *) "";
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001484 int ss_pos = 0;
1485 struct symsrc ss_[2];
1486 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001487 bool kmod;
Krister Johansenbf2e7102017-07-05 18:48:09 -07001488 bool perfmap;
Dima Kogan5baecbc2015-09-07 17:34:19 -07001489 unsigned char build_id[BUILD_ID_SIZE];
Krister Johansen843ff372017-07-05 18:48:08 -07001490 struct nscookie nsc;
Krister Johansenbf2e7102017-07-05 18:48:09 -07001491 char newmapname[PATH_MAX];
1492 const char *map_path = dso->long_name;
1493
1494 perfmap = strncmp(dso->name, "/tmp/perf-", 10) == 0;
1495 if (perfmap) {
1496 if (dso->nsinfo && (dso__find_perf_map(newmapname,
1497 sizeof(newmapname), &dso->nsinfo) == 0)) {
1498 map_path = newmapname;
1499 }
1500 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001501
Krister Johansen843ff372017-07-05 18:48:08 -07001502 nsinfo__mountns_enter(dso->nsinfo, &nsc);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001503 pthread_mutex_lock(&dso->lock);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001504
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001505 /* check again under the dso->lock */
1506 if (dso__loaded(dso, map->type)) {
1507 ret = 1;
1508 goto out;
1509 }
1510
1511 if (dso->kernel) {
1512 if (dso->kernel == DSO_TYPE_KERNEL)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001513 ret = dso__load_kernel_sym(dso, map);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001514 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001515 ret = dso__load_guest_kernel_sym(dso, map);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001516
1517 goto out;
1518 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001519
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001520 if (map->groups && map->groups->machine)
1521 machine = map->groups->machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001522 else
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001523 machine = NULL;
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001524
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001525 dso->adjust_symbols = 0;
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001526
Krister Johansenbf2e7102017-07-05 18:48:09 -07001527 if (perfmap) {
Pekka Enberg981c1252011-08-09 22:54:18 +03001528 struct stat st;
1529
Krister Johansenbf2e7102017-07-05 18:48:09 -07001530 if (lstat(map_path, &st) < 0)
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001531 goto out;
Pekka Enberg981c1252011-08-09 22:54:18 +03001532
Arnaldo Carvalho de Melo2059fc72015-11-12 16:50:13 -03001533 if (!symbol_conf.force && st.st_uid && (st.st_uid != geteuid())) {
Pekka Enberg981c1252011-08-09 22:54:18 +03001534 pr_warning("File %s not owned by current user or root, "
Krister Johansenbf2e7102017-07-05 18:48:09 -07001535 "ignoring it (use -f to override).\n", map_path);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001536 goto out;
Pekka Enberg981c1252011-08-09 22:54:18 +03001537 }
1538
Krister Johansenbf2e7102017-07-05 18:48:09 -07001539 ret = dso__load_perf_map(map_path, dso, map);
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001540 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1541 DSO_BINARY_TYPE__NOT_FOUND;
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001542 goto out;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001543 }
1544
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001545 if (machine)
1546 root_dir = machine->root_dir;
1547
David Ahern164c8002013-01-14 10:46:47 -07001548 name = malloc(PATH_MAX);
1549 if (!name)
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001550 goto out;
David Ahern164c8002013-01-14 10:46:47 -07001551
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001552 kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
Namhyung Kimc00c48f2014-11-04 10:14:27 +09001553 dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
1554 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE ||
1555 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001556
Dima Kogan5baecbc2015-09-07 17:34:19 -07001557
1558 /*
1559 * Read the build id if possible. This is required for
1560 * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work
1561 */
Kan Lianged6c1662016-12-13 10:29:44 -05001562 if (!dso->has_build_id &&
Victor Kamensky9b200652017-02-06 15:48:28 -08001563 is_regular_file(dso->long_name)) {
1564 __symbol__join_symfs(name, PATH_MAX, dso->long_name);
1565 if (filename__read_build_id(name, build_id, BUILD_ID_SIZE) > 0)
Dima Kogan5baecbc2015-09-07 17:34:19 -07001566 dso__set_build_id(dso, build_id);
Victor Kamensky9b200652017-02-06 15:48:28 -08001567 }
Dima Kogan5baecbc2015-09-07 17:34:19 -07001568
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001569 /*
1570 * Iterate over candidate debug images.
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001571 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1572 * and/or opd section) for processing.
Dave Martin6da80ce2010-07-30 09:50:09 -03001573 */
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001574 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001575 struct symsrc *ss = &ss_[ss_pos];
1576 bool next_slot = false;
Krister Johansenf045b8c2017-07-05 18:48:11 -07001577 bool is_reg;
Krister Johansend2396992017-07-05 18:48:13 -07001578 bool nsexit;
Krister Johansenf045b8c2017-07-05 18:48:11 -07001579 int sirc;
Dave Martin6da80ce2010-07-30 09:50:09 -03001580
Cody P Schafer005f9292012-08-10 15:22:58 -07001581 enum dso_binary_type symtab_type = binary_type_symtab[i];
Dave Martin6da80ce2010-07-30 09:50:09 -03001582
Krister Johansend2396992017-07-05 18:48:13 -07001583 nsexit = (symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE ||
1584 symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO);
1585
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001586 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1587 continue;
1588
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -03001589 if (dso__read_binary_type_filename(dso, symtab_type,
1590 root_dir, name, PATH_MAX))
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001591 continue;
Dave Martin6da80ce2010-07-30 09:50:09 -03001592
Krister Johansend2396992017-07-05 18:48:13 -07001593 if (nsexit)
Krister Johansenf045b8c2017-07-05 18:48:11 -07001594 nsinfo__mountns_exit(&nsc);
Jiri Olsa40356722016-01-20 12:56:32 +01001595
Krister Johansenf045b8c2017-07-05 18:48:11 -07001596 is_reg = is_regular_file(name);
1597 sirc = symsrc__init(ss, dso, name, symtab_type);
1598
Krister Johansend2396992017-07-05 18:48:13 -07001599 if (nsexit)
Krister Johansenf045b8c2017-07-05 18:48:11 -07001600 nsinfo__mountns_enter(dso->nsinfo, &nsc);
1601
1602 if (!is_reg || sirc < 0) {
1603 if (sirc >= 0)
1604 symsrc__destroy(ss);
Dave Martin6da80ce2010-07-30 09:50:09 -03001605 continue;
Krister Johansenf045b8c2017-07-05 18:48:11 -07001606 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001607
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001608 if (!syms_ss && symsrc__has_symtab(ss)) {
1609 syms_ss = ss;
1610 next_slot = true;
Adrian Hunter0058aef2013-12-03 09:23:08 +02001611 if (!dso->symsrc_filename)
1612 dso->symsrc_filename = strdup(name);
Cody P Schaferd26cd122012-08-10 15:23:00 -07001613 }
1614
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001615 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1616 runtime_ss = ss;
1617 next_slot = true;
Cody P Schafera44f6052012-08-10 15:22:59 -07001618 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001619
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001620 if (next_slot) {
1621 ss_pos++;
Jiri Olsa33ff5812012-04-18 15:46:58 +02001622
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001623 if (syms_ss && runtime_ss)
1624 break;
Namhyung Kim98e9f032014-02-20 10:32:54 +09001625 } else {
1626 symsrc__destroy(ss);
Dave Martin6da80ce2010-07-30 09:50:09 -03001627 }
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001628
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001629 }
Dave Martin6da80ce2010-07-30 09:50:09 -03001630
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001631 if (!runtime_ss && !syms_ss)
1632 goto out_free;
1633
1634 if (runtime_ss && !syms_ss) {
1635 syms_ss = runtime_ss;
Arnaldo Carvalho de Melo60e4b102011-03-22 15:42:14 -03001636 }
1637
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001638 /* We'll have to hope for the best */
1639 if (!runtime_ss && syms_ss)
1640 runtime_ss = syms_ss;
1641
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001642 if (syms_ss)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001643 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, kmod);
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001644 else
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001645 ret = -1;
1646
David Ahernf47b58b2012-08-19 09:47:14 -06001647 if (ret > 0) {
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001648 int nr_plt;
1649
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001650 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map);
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001651 if (nr_plt > 0)
1652 ret += nr_plt;
1653 }
1654
1655 for (; ss_pos > 0; ss_pos--)
1656 symsrc__destroy(&ss_[ss_pos - 1]);
1657out_free:
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001658 free(name);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001659 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001660 ret = 0;
1661out:
1662 dso__set_loaded(dso, map->type);
1663 pthread_mutex_unlock(&dso->lock);
Krister Johansen843ff372017-07-05 18:48:08 -07001664 nsinfo__mountns_exit(&nsc);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001665
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001666 return ret;
1667}
1668
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001669struct map *map_groups__find_by_name(struct map_groups *mg,
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -02001670 enum map_type type, const char *name)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001671{
Arnaldo Carvalho de Melo1eee78a2015-05-22 12:58:53 -03001672 struct maps *maps = &mg->maps[type];
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -03001673 struct map *map;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001674
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001675 down_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -03001676
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -03001677 for (map = maps__first(maps); map; map = map__next(map)) {
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001678 if (map->dso && strcmp(map->dso->short_name, name) == 0)
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -03001679 goto out_unlock;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001680 }
1681
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -03001682 map = NULL;
1683
1684out_unlock:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001685 up_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -03001686 return map;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001687}
1688
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001689int dso__load_vmlinux(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001690 const char *vmlinux, bool vmlinux_allocated)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001691{
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001692 int err = -1;
1693 struct symsrc ss;
David Ahernec5761e2010-12-09 13:27:07 -07001694 char symfs_vmlinux[PATH_MAX];
Cody P Schafer005f9292012-08-10 15:22:58 -07001695 enum dso_binary_type symtab_type;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001696
Namhyung Kim5698d2c2013-07-17 17:08:15 +09001697 if (vmlinux[0] == '/')
1698 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1699 else
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -03001700 symbol__join_symfs(symfs_vmlinux, vmlinux);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001701
Cody P Schafer21ea4532012-08-10 15:22:56 -07001702 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Cody P Schafer005f9292012-08-10 15:22:58 -07001703 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
Cody P Schafer21ea4532012-08-10 15:22:56 -07001704 else
Cody P Schafer005f9292012-08-10 15:22:58 -07001705 symtab_type = DSO_BINARY_TYPE__VMLINUX;
Cody P Schafer21ea4532012-08-10 15:22:56 -07001706
Cody P Schafer005f9292012-08-10 15:22:58 -07001707 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001708 return -1;
1709
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001710 err = dso__load_sym(dso, map, &ss, &ss, 0);
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001711 symsrc__destroy(&ss);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001712
Cody P Schafer515850e2012-08-10 15:22:54 -07001713 if (err > 0) {
Adrian Hunter39b12f782013-08-07 14:38:47 +03001714 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001715 dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
Adrian Hunter39b12f782013-08-07 14:38:47 +03001716 else
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001717 dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -03001718 dso__set_long_name(dso, vmlinux, vmlinux_allocated);
Cody P Schafer515850e2012-08-10 15:22:54 -07001719 dso__set_loaded(dso, map->type);
David Ahernec5761e2010-12-09 13:27:07 -07001720 pr_debug("Using %s for symbols\n", symfs_vmlinux);
Cody P Schafer515850e2012-08-10 15:22:54 -07001721 }
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001722
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001723 return err;
1724}
1725
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001726int dso__load_vmlinux_path(struct dso *dso, struct map *map)
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001727{
1728 int i, err = 0;
Namhyung Kim00dc8652014-11-04 10:14:32 +09001729 char *filename = NULL;
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001730
Arnaldo Carvalho de Melodc382182015-10-11 17:17:24 -03001731 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1732 vmlinux_path__nr_entries + 1);
1733
1734 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001735 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false);
Arnaldo Carvalho de Melodc382182015-10-11 17:17:24 -03001736 if (err > 0)
1737 goto out;
1738 }
1739
Namhyung Kim00dc8652014-11-04 10:14:32 +09001740 if (!symbol_conf.ignore_vmlinux_buildid)
Krister Johansend2396992017-07-05 18:48:13 -07001741 filename = dso__build_id_filename(dso, NULL, 0, false);
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001742 if (filename != NULL) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001743 err = dso__load_vmlinux(dso, map, filename, true);
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -03001744 if (err > 0)
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001745 goto out;
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001746 free(filename);
1747 }
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001748out:
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001749 return err;
1750}
1751
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001752static bool visible_dir_filter(const char *name, struct dirent *d)
1753{
1754 if (d->d_type != DT_DIR)
1755 return false;
1756 return lsdir_no_dot_filter(name, d);
1757}
1758
Adrian Hunter0544d422013-10-14 13:43:43 +03001759static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1760{
1761 char kallsyms_filename[PATH_MAX];
Adrian Hunter0544d422013-10-14 13:43:43 +03001762 int ret = -1;
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001763 struct strlist *dirs;
1764 struct str_node *nd;
Adrian Hunter0544d422013-10-14 13:43:43 +03001765
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001766 dirs = lsdir(dir, visible_dir_filter);
1767 if (!dirs)
Adrian Hunter0544d422013-10-14 13:43:43 +03001768 return -1;
1769
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03001770 strlist__for_each_entry(nd, dirs) {
Adrian Hunter0544d422013-10-14 13:43:43 +03001771 scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001772 "%s/%s/kallsyms", dir, nd->s);
Adrian Huntera00d28c2014-01-29 16:14:41 +02001773 if (!validate_kcore_addresses(kallsyms_filename, map)) {
Adrian Hunter0544d422013-10-14 13:43:43 +03001774 strlcpy(dir, kallsyms_filename, dir_sz);
1775 ret = 0;
1776 break;
1777 }
1778 }
1779
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001780 strlist__delete(dirs);
Adrian Hunter0544d422013-10-14 13:43:43 +03001781
1782 return ret;
1783}
1784
Masami Hiramatsu11870d72016-05-29 00:15:13 +09001785/*
1786 * Use open(O_RDONLY) to check readability directly instead of access(R_OK)
1787 * since access(R_OK) only checks with real UID/GID but open() use effective
1788 * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO).
1789 */
1790static bool filename__readable(const char *file)
1791{
1792 int fd = open(file, O_RDONLY);
1793 if (fd < 0)
1794 return false;
1795 close(fd);
1796 return true;
1797}
1798
Adrian Hunter0544d422013-10-14 13:43:43 +03001799static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1800{
1801 u8 host_build_id[BUILD_ID_SIZE];
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001802 char sbuild_id[SBUILD_ID_SIZE];
Adrian Hunter0544d422013-10-14 13:43:43 +03001803 bool is_host = false;
1804 char path[PATH_MAX];
1805
1806 if (!dso->has_build_id) {
1807 /*
1808 * Last resort, if we don't have a build-id and couldn't find
1809 * any vmlinux file, try the running kernel kallsyms table.
1810 */
1811 goto proc_kallsyms;
1812 }
1813
1814 if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1815 sizeof(host_build_id)) == 0)
1816 is_host = dso__build_id_equal(dso, host_build_id);
1817
Masami Hiramatsu4e4b6c02016-05-29 00:15:28 +09001818 /* Try a fast path for /proc/kallsyms if possible */
Adrian Hunter0544d422013-10-14 13:43:43 +03001819 if (is_host) {
Adrian Hunter0544d422013-10-14 13:43:43 +03001820 /*
Masami Hiramatsu11870d72016-05-29 00:15:13 +09001821 * Do not check the build-id cache, unless we know we cannot use
1822 * /proc/kcore or module maps don't match to /proc/kallsyms.
1823 * To check readability of /proc/kcore, do not use access(R_OK)
1824 * since /proc/kcore requires CAP_SYS_RAWIO to read and access
1825 * can't check it.
Adrian Hunter0544d422013-10-14 13:43:43 +03001826 */
Masami Hiramatsu11870d72016-05-29 00:15:13 +09001827 if (filename__readable("/proc/kcore") &&
1828 !validate_kcore_addresses("/proc/kallsyms", map))
1829 goto proc_kallsyms;
Adrian Hunter0544d422013-10-14 13:43:43 +03001830 }
1831
Masami Hiramatsu4e4b6c02016-05-29 00:15:28 +09001832 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1833
Adrian Hunter449867e2013-11-26 15:19:24 +02001834 /* Find kallsyms in build-id cache with kcore */
Masami Hiramatsu4e4b6c02016-05-29 00:15:28 +09001835 scnprintf(path, sizeof(path), "%s/%s/%s",
1836 buildid_dir, DSO__NAME_KCORE, sbuild_id);
1837
Adrian Hunter449867e2013-11-26 15:19:24 +02001838 if (!find_matching_kcore(map, path, sizeof(path)))
1839 return strdup(path);
1840
Masami Hiramatsu4e4b6c02016-05-29 00:15:28 +09001841 /* Use current /proc/kallsyms if possible */
1842 if (is_host) {
1843proc_kallsyms:
1844 return strdup("/proc/kallsyms");
1845 }
1846
1847 /* Finally, find a cache of kallsyms */
Masami Hiramatsu01412262016-05-29 00:15:37 +09001848 if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) {
Adrian Hunter0544d422013-10-14 13:43:43 +03001849 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1850 sbuild_id);
1851 return NULL;
1852 }
1853
1854 return strdup(path);
Adrian Hunter0544d422013-10-14 13:43:43 +03001855}
1856
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001857static int dso__load_kernel_sym(struct dso *dso, struct map *map)
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001858{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001859 int err;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001860 const char *kallsyms_filename = NULL;
1861 char *kallsyms_allocated_filename = NULL;
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001862 /*
David Ahernb226a5a72010-12-07 19:39:46 -07001863 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1864 * it and only it, reporting errors to the user if it cannot be used.
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001865 *
1866 * For instance, try to analyse an ARM perf.data file _without_ a
1867 * build-id, or if the user specifies the wrong path to the right
1868 * vmlinux file, obviously we can't fallback to another vmlinux (a
1869 * x86_86 one, on the machine where analysis is being performed, say),
1870 * or worse, /proc/kallsyms.
1871 *
1872 * If the specified file _has_ a build-id and there is a build-id
1873 * section in the perf.data file, we will still do the expected
1874 * validation in dso__load_vmlinux and will bail out if they don't
1875 * match.
1876 */
David Ahernb226a5a72010-12-07 19:39:46 -07001877 if (symbol_conf.kallsyms_name != NULL) {
1878 kallsyms_filename = symbol_conf.kallsyms_name;
1879 goto do_kallsyms;
1880 }
1881
Willy Tarreaufc2be692013-09-14 10:32:59 +02001882 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001883 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, false);
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001884 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001885
Willy Tarreaufc2be692013-09-14 10:32:59 +02001886 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001887 err = dso__load_vmlinux_path(dso, map);
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001888 if (err > 0)
Adrian Hunter39b12f782013-08-07 14:38:47 +03001889 return err;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001890 }
1891
David Ahernec5761e2010-12-09 13:27:07 -07001892 /* do not try local files if a symfs was given */
1893 if (symbol_conf.symfs[0] != 0)
1894 return -1;
1895
Adrian Hunter0544d422013-10-14 13:43:43 +03001896 kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
1897 if (!kallsyms_allocated_filename)
1898 return -1;
Arnaldo Carvalho de Melob7cece72010-01-13 13:22:17 -02001899
Adrian Hunter0544d422013-10-14 13:43:43 +03001900 kallsyms_filename = kallsyms_allocated_filename;
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001901
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001902do_kallsyms:
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001903 err = dso__load_kallsyms(dso, kallsyms_filename, map);
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001904 if (err > 0)
1905 pr_debug("Using %s for symbols\n", kallsyms_filename);
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001906 free(kallsyms_allocated_filename);
1907
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001908 if (err > 0 && !dso__is_kcore(dso)) {
Adrian Hunterbdac0bc2014-07-14 13:02:43 +03001909 dso->binary_type = DSO_BINARY_TYPE__KALLSYMS;
Masami Hiramatsu0a775822016-05-15 12:19:40 +09001910 dso__set_long_name(dso, DSO__NAME_KALLSYMS, false);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001911 map__fixup_start(map);
1912 map__fixup_end(map);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001913 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001914
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001915 return err;
1916}
1917
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001918static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001919{
1920 int err;
1921 const char *kallsyms_filename = NULL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001922 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001923 char path[PATH_MAX];
1924
1925 if (!map->groups) {
1926 pr_debug("Guest kernel map hasn't the point to groups\n");
1927 return -1;
1928 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001929 machine = map->groups->machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001930
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001931 if (machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001932 /*
1933 * if the user specified a vmlinux filename, use it and only
1934 * it, reporting errors to the user if it cannot be used.
1935 * Or use file guest_kallsyms inputted by user on commandline
1936 */
1937 if (symbol_conf.default_guest_vmlinux_name != NULL) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001938 err = dso__load_vmlinux(dso, map,
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -03001939 symbol_conf.default_guest_vmlinux_name,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001940 false);
Adrian Hunter39b12f782013-08-07 14:38:47 +03001941 return err;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001942 }
1943
1944 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1945 if (!kallsyms_filename)
1946 return -1;
1947 } else {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001948 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001949 kallsyms_filename = path;
1950 }
1951
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001952 err = dso__load_kallsyms(dso, kallsyms_filename, map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001953 if (err > 0)
Adrian Hunter39b12f782013-08-07 14:38:47 +03001954 pr_debug("Using %s for symbols\n", kallsyms_filename);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001955 if (err > 0 && !dso__is_kcore(dso)) {
Adrian Hunterbdac0bc2014-07-14 13:02:43 +03001956 dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
Adrian Hunter39b12f782013-08-07 14:38:47 +03001957 machine__mmap_name(machine, path, sizeof(path));
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001958 dso__set_long_name(dso, strdup(path), true);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001959 map__fixup_start(map);
1960 map__fixup_end(map);
1961 }
1962
1963 return err;
1964}
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001965
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001966static void vmlinux_path__exit(void)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001967{
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001968 while (--vmlinux_path__nr_entries >= 0)
1969 zfree(&vmlinux_path[vmlinux_path__nr_entries]);
Wang Nanc4f03542015-05-17 10:56:27 +00001970 vmlinux_path__nr_entries = 0;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001971
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001972 zfree(&vmlinux_path);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001973}
1974
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01001975static const char * const vmlinux_paths[] = {
1976 "vmlinux",
1977 "/boot/vmlinux"
1978};
1979
1980static const char * const vmlinux_paths_upd[] = {
1981 "/boot/vmlinux-%s",
1982 "/usr/lib/debug/boot/vmlinux-%s",
1983 "/lib/modules/%s/build/vmlinux",
Ekaterina Tumanovaf55ae952015-11-25 17:32:46 +01001984 "/usr/lib/debug/lib/modules/%s/vmlinux",
1985 "/usr/lib/debug/boot/vmlinux-%s.debug"
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01001986};
1987
1988static int vmlinux_path__add(const char *new_entry)
1989{
1990 vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry);
1991 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1992 return -1;
1993 ++vmlinux_path__nr_entries;
1994
1995 return 0;
1996}
1997
Kan Liangce80d3b2015-08-28 05:48:04 -04001998static int vmlinux_path__init(struct perf_env *env)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001999{
2000 struct utsname uts;
2001 char bf[PATH_MAX];
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002002 char *kernel_version;
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01002003 unsigned int i;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002004
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01002005 vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
2006 ARRAY_SIZE(vmlinux_paths_upd)));
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002007 if (vmlinux_path == NULL)
2008 return -1;
2009
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01002010 for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++)
2011 if (vmlinux_path__add(vmlinux_paths[i]) < 0)
2012 goto out_fail;
David Ahernec5761e2010-12-09 13:27:07 -07002013
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002014 /* only try kernel version if no symfs was given */
David Ahernec5761e2010-12-09 13:27:07 -07002015 if (symbol_conf.symfs[0] != 0)
2016 return 0;
2017
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002018 if (env) {
2019 kernel_version = env->os_release;
2020 } else {
2021 if (uname(&uts) < 0)
2022 goto out_fail;
David Ahernec5761e2010-12-09 13:27:07 -07002023
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002024 kernel_version = uts.release;
2025 }
2026
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01002027 for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) {
2028 snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version);
2029 if (vmlinux_path__add(bf) < 0)
2030 goto out_fail;
2031 }
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002032
2033 return 0;
2034
2035out_fail:
2036 vmlinux_path__exit();
2037 return -1;
2038}
2039
David Ahern3bfe5f82013-11-18 13:32:48 -07002040int setup_list(struct strlist **list, const char *list_str,
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002041 const char *list_name)
2042{
2043 if (list_str == NULL)
2044 return 0;
2045
Arnaldo Carvalho de Melo4a77e212015-07-20 12:13:34 -03002046 *list = strlist__new(list_str, NULL);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002047 if (!*list) {
2048 pr_err("problems parsing %s list\n", list_name);
2049 return -1;
2050 }
Arnaldo Carvalho de Melo0bc2f2f2015-07-13 08:21:57 -03002051
2052 symbol_conf.has_filter = true;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002053 return 0;
2054}
2055
David Aherne03eaa42015-03-24 09:52:41 -06002056int setup_intlist(struct intlist **list, const char *list_str,
2057 const char *list_name)
2058{
2059 if (list_str == NULL)
2060 return 0;
2061
2062 *list = intlist__new(list_str);
2063 if (!*list) {
2064 pr_err("problems parsing %s list\n", list_name);
2065 return -1;
2066 }
2067 return 0;
2068}
2069
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002070static bool symbol__read_kptr_restrict(void)
2071{
2072 bool value = false;
Wang Nan38272dc2016-05-24 09:21:27 +00002073 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002074
Wang Nan38272dc2016-05-24 09:21:27 +00002075 if (fp != NULL) {
2076 char line[8];
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002077
Wang Nan38272dc2016-05-24 09:21:27 +00002078 if (fgets(line, sizeof(line), fp) != NULL)
Wang Nan3dbe46c2016-11-15 04:05:45 +00002079 value = ((geteuid() != 0) || (getuid() != 0)) ?
Wang Nan38272dc2016-05-24 09:21:27 +00002080 (atoi(line) != 0) :
2081 (atoi(line) == 2);
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002082
Wang Nan38272dc2016-05-24 09:21:27 +00002083 fclose(fp);
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002084 }
2085
2086 return value;
2087}
2088
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -03002089int symbol__annotation_init(void)
2090{
2091 if (symbol_conf.initialized) {
2092 pr_err("Annotation needs to be init before symbol__init()\n");
2093 return -1;
2094 }
2095
2096 if (symbol_conf.init_annotation) {
2097 pr_warning("Annotation being initialized multiple times\n");
2098 return 0;
2099 }
2100
2101 symbol_conf.priv_size += sizeof(struct annotation);
2102 symbol_conf.init_annotation = true;
2103 return 0;
2104}
2105
Kan Liangce80d3b2015-08-28 05:48:04 -04002106int symbol__init(struct perf_env *env)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002107{
David Ahernec5761e2010-12-09 13:27:07 -07002108 const char *symfs;
2109
Jovi Zhang85e00b52010-09-09 13:30:59 -03002110 if (symbol_conf.initialized)
2111 return 0;
2112
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002113 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
David S. Miller4d439512011-03-29 14:18:39 -03002114
Namhyung Kim166ccc92012-08-06 13:41:19 +09002115 symbol__elf_init();
2116
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02002117 if (symbol_conf.sort_by_name)
2118 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2119 sizeof(struct symbol));
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02002120
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002121 if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002122 return -1;
2123
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02002124 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2125 pr_err("'.' is the only non valid --field-separator argument\n");
2126 return -1;
2127 }
2128
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002129 if (setup_list(&symbol_conf.dso_list,
2130 symbol_conf.dso_list_str, "dso") < 0)
2131 return -1;
2132
2133 if (setup_list(&symbol_conf.comm_list,
2134 symbol_conf.comm_list_str, "comm") < 0)
2135 goto out_free_dso_list;
2136
David Aherne03eaa42015-03-24 09:52:41 -06002137 if (setup_intlist(&symbol_conf.pid_list,
2138 symbol_conf.pid_list_str, "pid") < 0)
2139 goto out_free_comm_list;
2140
2141 if (setup_intlist(&symbol_conf.tid_list,
2142 symbol_conf.tid_list_str, "tid") < 0)
2143 goto out_free_pid_list;
2144
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002145 if (setup_list(&symbol_conf.sym_list,
2146 symbol_conf.sym_list_str, "symbol") < 0)
David Aherne03eaa42015-03-24 09:52:41 -06002147 goto out_free_tid_list;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002148
David Ahern64eff7d2016-11-25 13:00:21 -07002149 if (setup_list(&symbol_conf.bt_stop_list,
2150 symbol_conf.bt_stop_list_str, "symbol") < 0)
2151 goto out_free_sym_list;
2152
David Ahernec5761e2010-12-09 13:27:07 -07002153 /*
2154 * A path to symbols of "/" is identical to ""
2155 * reset here for simplicity.
2156 */
2157 symfs = realpath(symbol_conf.symfs, NULL);
2158 if (symfs == NULL)
2159 symfs = symbol_conf.symfs;
2160 if (strcmp(symfs, "/") == 0)
2161 symbol_conf.symfs = "";
2162 if (symfs != symbol_conf.symfs)
2163 free((void *)symfs);
2164
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002165 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2166
Jovi Zhang85e00b52010-09-09 13:30:59 -03002167 symbol_conf.initialized = true;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002168 return 0;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002169
David Ahern64eff7d2016-11-25 13:00:21 -07002170out_free_sym_list:
2171 strlist__delete(symbol_conf.sym_list);
David Aherne03eaa42015-03-24 09:52:41 -06002172out_free_tid_list:
2173 intlist__delete(symbol_conf.tid_list);
2174out_free_pid_list:
2175 intlist__delete(symbol_conf.pid_list);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002176out_free_comm_list:
2177 strlist__delete(symbol_conf.comm_list);
Namhyung Kimd74c8962011-12-13 00:16:52 +09002178out_free_dso_list:
2179 strlist__delete(symbol_conf.dso_list);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002180 return -1;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002181}
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002182
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002183void symbol__exit(void)
2184{
Jovi Zhang85e00b52010-09-09 13:30:59 -03002185 if (!symbol_conf.initialized)
2186 return;
David Ahern64eff7d2016-11-25 13:00:21 -07002187 strlist__delete(symbol_conf.bt_stop_list);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002188 strlist__delete(symbol_conf.sym_list);
2189 strlist__delete(symbol_conf.dso_list);
2190 strlist__delete(symbol_conf.comm_list);
David Aherne03eaa42015-03-24 09:52:41 -06002191 intlist__delete(symbol_conf.tid_list);
2192 intlist__delete(symbol_conf.pid_list);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002193 vmlinux_path__exit();
2194 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
David Ahern64eff7d2016-11-25 13:00:21 -07002195 symbol_conf.bt_stop_list = NULL;
Jovi Zhang85e00b52010-09-09 13:30:59 -03002196 symbol_conf.initialized = false;
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002197}
He Kuanga7066702016-05-19 11:47:37 +00002198
2199int symbol__config_symfs(const struct option *opt __maybe_unused,
2200 const char *dir, int unset __maybe_unused)
2201{
2202 char *bf = NULL;
2203 int ret;
2204
2205 symbol_conf.symfs = strdup(dir);
2206 if (symbol_conf.symfs == NULL)
2207 return -ENOMEM;
2208
2209 /* skip the locally configured cache if a symfs is given, and
2210 * config buildid dir to symfs/.debug
2211 */
2212 ret = asprintf(&bf, "%s/%s", dir, ".debug");
2213 if (ret < 0)
2214 return -ENOMEM;
2215
2216 set_buildid_dir(bf);
2217
2218 free(bf);
2219 return 0;
2220}