blob: 7011440941831d73b26b527678d416306264ef2a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03002#include <dirent.h>
3#include <errno.h>
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03004#include <stdlib.h>
5#include <stdio.h>
6#include <string.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -03007#include <linux/kernel.h>
Arnaldo Carvalho de Meloe9814df2018-04-26 16:11:47 -03008#include <linux/mman.h>
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -03009#include <sys/types.h>
10#include <sys/stat.h>
11#include <sys/param.h>
12#include <fcntl.h>
13#include <unistd.h>
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -020014#include <inttypes.h>
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -030015#include "annotate.h"
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -030016#include "build-id.h"
Namhyung Kime334c722012-02-10 10:10:17 +090017#include "util.h"
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030018#include "debug.h"
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030019#include "machine.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030020#include "symbol.h"
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -030021#include "strlist.h"
David Aherne03eaa42015-03-24 09:52:41 -060022#include "intlist.h"
Krister Johansen843ff372017-07-05 18:48:08 -070023#include "namespaces.h"
Namhyung Kim0a7e6d12014-08-12 15:40:45 +090024#include "header.h"
Arnaldo Carvalho de Melo9a3993d2017-04-18 11:33:48 -030025#include "path.h"
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030026#include "sane_ctype.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030027
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030028#include <elf.h>
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -020029#include <limits.h>
Arnaldo Carvalho de Meloc506c962013-12-11 09:15:00 -030030#include <symbol/kallsyms.h>
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030031#include <sys/utsname.h>
Peter Zijlstra2cdbc462009-08-05 14:05:16 +020032
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -030033static int dso__load_kernel_sym(struct dso *dso, struct map *map);
34static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map);
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -030035static bool symbol__is_idle(const char *name);
36
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030037int vmlinux_path__nr_entries;
38char **vmlinux_path;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030039
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -020040struct symbol_conf symbol_conf = {
Namhyung Kime511db52013-12-24 16:19:25 +090041 .use_modules = true,
42 .try_vmlinux_path = true,
43 .annotate_src = true,
44 .demangle = true,
Avi Kivity763122a2014-09-13 07:15:05 +030045 .demangle_kernel = false,
Namhyung Kime511db52013-12-24 16:19:25 +090046 .cumulate_callchain = true,
Jiri Olsac8302362014-06-27 18:26:58 +020047 .show_hist_headers = true,
Namhyung Kime511db52013-12-24 16:19:25 +090048 .symfs = "",
Namhyung Kim1e9abf82015-11-29 23:24:17 +090049 .event_group = true,
Milian Wolffd8a88dd2017-10-19 13:38:36 +020050 .inline_name = true,
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020051};
52
Jiri Olsa44f24cb2012-07-22 14:14:32 +020053static enum dso_binary_type binary_type_symtab[] = {
54 DSO_BINARY_TYPE__KALLSYMS,
55 DSO_BINARY_TYPE__GUEST_KALLSYMS,
56 DSO_BINARY_TYPE__JAVA_JIT,
57 DSO_BINARY_TYPE__DEBUGLINK,
58 DSO_BINARY_TYPE__BUILD_ID_CACHE,
Krister Johansend2396992017-07-05 18:48:13 -070059 DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO,
Jiri Olsa44f24cb2012-07-22 14:14:32 +020060 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
61 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
62 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
63 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
64 DSO_BINARY_TYPE__GUEST_KMODULE,
Namhyung Kimc00c48f2014-11-04 10:14:27 +090065 DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
Jiri Olsa44f24cb2012-07-22 14:14:32 +020066 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
Namhyung Kimc00c48f2014-11-04 10:14:27 +090067 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020068 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
Jiri Olsa44f24cb2012-07-22 14:14:32 +020069 DSO_BINARY_TYPE__NOT_FOUND,
70};
71
Jiri Olsa028df762012-08-01 14:47:57 +020072#define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
Jiri Olsa44f24cb2012-07-22 14:14:32 +020073
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -030074static bool symbol_type__filter(char symbol_type)
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020075{
Anton Blanchard31877902011-08-24 16:40:16 +100076 symbol_type = toupper(symbol_type);
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -030077 return symbol_type == 'T' || symbol_type == 'W' || symbol_type == 'D';
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020078}
79
Anton Blanchard694bf402011-08-24 16:40:17 +100080static int prefix_underscores_count(const char *str)
81{
82 const char *tail = str;
83
84 while (*tail == '_')
85 tail++;
86
87 return tail - str;
88}
89
Masami Hiramatsu4b3a2712017-12-09 01:28:12 +090090const char * __weak arch__normalize_symbol_name(const char *name)
91{
92 return name;
93}
94
Paul Clarked80406452017-04-25 13:15:49 -050095int __weak arch__compare_symbol_names(const char *namea, const char *nameb)
96{
97 return strcmp(namea, nameb);
98}
99
100int __weak arch__compare_symbol_names_n(const char *namea, const char *nameb,
101 unsigned int n)
102{
103 return strncmp(namea, nameb, n);
104}
105
Naveen N. Raofb6d5942015-04-28 17:35:36 +0530106int __weak arch__choose_best_symbol(struct symbol *syma,
107 struct symbol *symb __maybe_unused)
108{
109 /* Avoid "SyS" kernel syscall aliases */
110 if (strlen(syma->name) >= 3 && !strncmp(syma->name, "SyS", 3))
111 return SYMBOL_B;
112 if (strlen(syma->name) >= 10 && !strncmp(syma->name, "compat_SyS", 10))
113 return SYMBOL_B;
114
115 return SYMBOL_A;
116}
Anton Blanchard694bf402011-08-24 16:40:17 +1000117
118static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
119{
120 s64 a;
121 s64 b;
Adrian Hunter34454322013-08-07 14:38:49 +0300122 size_t na, nb;
Anton Blanchard694bf402011-08-24 16:40:17 +1000123
124 /* Prefer a symbol with non zero length */
125 a = syma->end - syma->start;
126 b = symb->end - symb->start;
127 if ((b == 0) && (a > 0))
128 return SYMBOL_A;
129 else if ((a == 0) && (b > 0))
130 return SYMBOL_B;
131
132 /* Prefer a non weak symbol over a weak one */
133 a = syma->binding == STB_WEAK;
134 b = symb->binding == STB_WEAK;
135 if (b && !a)
136 return SYMBOL_A;
137 if (a && !b)
138 return SYMBOL_B;
139
140 /* Prefer a global symbol over a non global one */
141 a = syma->binding == STB_GLOBAL;
142 b = symb->binding == STB_GLOBAL;
143 if (a && !b)
144 return SYMBOL_A;
145 if (b && !a)
146 return SYMBOL_B;
147
148 /* Prefer a symbol with less underscores */
149 a = prefix_underscores_count(syma->name);
150 b = prefix_underscores_count(symb->name);
151 if (b > a)
152 return SYMBOL_A;
153 else if (a > b)
154 return SYMBOL_B;
155
Adrian Hunter34454322013-08-07 14:38:49 +0300156 /* Choose the symbol with the longest name */
157 na = strlen(syma->name);
158 nb = strlen(symb->name);
159 if (na > nb)
Anton Blanchard694bf402011-08-24 16:40:17 +1000160 return SYMBOL_A;
Adrian Hunter34454322013-08-07 14:38:49 +0300161 else if (na < nb)
Anton Blanchard694bf402011-08-24 16:40:17 +1000162 return SYMBOL_B;
Adrian Hunter34454322013-08-07 14:38:49 +0300163
Naveen N. Raofb6d5942015-04-28 17:35:36 +0530164 return arch__choose_best_symbol(syma, symb);
Anton Blanchard694bf402011-08-24 16:40:17 +1000165}
166
Namhyung Kime5a18452012-08-06 13:41:20 +0900167void symbols__fixup_duplicate(struct rb_root *symbols)
Anton Blanchard694bf402011-08-24 16:40:17 +1000168{
169 struct rb_node *nd;
170 struct symbol *curr, *next;
171
Arnaldo Carvalho de Meloc97b40e2016-09-01 10:56:06 -0300172 if (symbol_conf.allow_aliases)
173 return;
174
Anton Blanchard694bf402011-08-24 16:40:17 +1000175 nd = rb_first(symbols);
176
177 while (nd) {
178 curr = rb_entry(nd, struct symbol, rb_node);
179again:
180 nd = rb_next(&curr->rb_node);
181 next = rb_entry(nd, struct symbol, rb_node);
182
183 if (!nd)
184 break;
185
186 if (curr->start != next->start)
187 continue;
188
189 if (choose_best_symbol(curr, next) == SYMBOL_A) {
190 rb_erase(&next->rb_node, symbols);
Chenggang Qind4f74eb2013-10-11 08:27:59 +0800191 symbol__delete(next);
Anton Blanchard694bf402011-08-24 16:40:17 +1000192 goto again;
193 } else {
194 nd = rb_next(&curr->rb_node);
195 rb_erase(&curr->rb_node, symbols);
Chenggang Qind4f74eb2013-10-11 08:27:59 +0800196 symbol__delete(curr);
Anton Blanchard694bf402011-08-24 16:40:17 +1000197 }
198 }
199}
200
Namhyung Kime5a18452012-08-06 13:41:20 +0900201void symbols__fixup_end(struct rb_root *symbols)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300202{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300203 struct rb_node *nd, *prevnd = rb_first(symbols);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300204 struct symbol *curr, *prev;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300205
206 if (prevnd == NULL)
207 return;
208
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300209 curr = rb_entry(prevnd, struct symbol, rb_node);
210
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300211 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300212 prev = curr;
213 curr = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300214
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200215 if (prev->end == prev->start && prev->end != curr->start)
Arnaldo Carvalho de Melo2c241bd2014-10-14 17:19:44 -0300216 prev->end = curr->start;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300217 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300218
219 /* Last entry */
220 if (curr->end == curr->start)
Daniel Borkmanne7ede722017-03-15 22:53:37 +0100221 curr->end = roundup(curr->start, 4096) + 4096;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300222}
223
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300224void map_groups__fixup_end(struct map_groups *mg)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300225{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300226 struct maps *maps = &mg->maps;
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300227 struct map *next, *curr;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300228
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300229 down_write(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300230
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300231 curr = maps__first(maps);
232 if (curr == NULL)
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300233 goto out_unlock;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300234
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300235 for (next = map__next(curr); next; next = map__next(curr)) {
Thomas Richter9ad46522017-08-03 15:49:02 +0200236 if (!curr->end)
237 curr->end = next->start;
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -0300238 curr = next;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300239 }
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -0200240
241 /*
242 * We still haven't the actual symbols, so guess the
243 * last map final address.
244 */
Thomas Richter9ad46522017-08-03 15:49:02 +0200245 if (!curr->end)
246 curr->end = ~0ULL;
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300247
248out_unlock:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300249 up_write(&maps->lock);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300250}
251
Arnaldo Carvalho de Meloaf30bff2018-04-26 11:09:10 -0300252struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300253{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300254 size_t namelen = strlen(name) + 1;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300255 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
256 sizeof(*sym) + namelen));
257 if (sym == NULL)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200258 return NULL;
259
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -0300260 if (symbol_conf.priv_size) {
261 if (symbol_conf.init_annotation) {
262 struct annotation *notes = (void *)sym;
263 pthread_mutex_init(&notes->lock, NULL);
264 }
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300265 sym = ((void *)sym) + symbol_conf.priv_size;
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -0300266 }
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200267
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300268 sym->start = start;
Arnaldo Carvalho de Melo2c241bd2014-10-14 17:19:44 -0300269 sym->end = len ? start + len : start;
Arnaldo Carvalho de Meloaf30bff2018-04-26 11:09:10 -0300270 sym->type = type;
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{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300481 dso->last_find_result.addr = 0;
482 dso->last_find_result.symbol = NULL;
Arnaldo Carvalho de Meloc0b4dff2015-08-24 13:33:14 -0300483}
484
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300485void dso__insert_symbol(struct dso *dso, struct symbol *sym)
Chris Phlipotae93a6c2016-05-10 20:26:46 -0700486{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300487 __symbols__insert(&dso->symbols, sym, dso->kernel);
Chris Phlipotae93a6c2016-05-10 20:26:46 -0700488
489 /* update the symbol cache if necessary */
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300490 if (dso->last_find_result.addr >= sym->start &&
491 (dso->last_find_result.addr < sym->end ||
Chris Phlipotae93a6c2016-05-10 20:26:46 -0700492 sym->start == sym->end)) {
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300493 dso->last_find_result.symbol = sym;
Chris Phlipotae93a6c2016-05-10 20:26:46 -0700494 }
495}
496
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300497struct symbol *dso__find_symbol(struct dso *dso, u64 addr)
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200498{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300499 if (dso->last_find_result.addr != addr || dso->last_find_result.symbol == NULL) {
500 dso->last_find_result.addr = addr;
501 dso->last_find_result.symbol = symbols__find(&dso->symbols, addr);
Arnaldo Carvalho de Melob685ac22015-07-22 12:52:17 -0300502 }
503
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300504 return dso->last_find_result.symbol;
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300505}
506
Arnaldo Carvalho de Melo5cf88a62018-04-25 17:01:46 -0300507struct symbol *dso__first_symbol(struct dso *dso)
508{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300509 return symbols__first(&dso->symbols);
Adrian Huntercd67f992016-09-23 17:38:38 +0300510}
511
Arnaldo Carvalho de Melo5cf88a62018-04-25 17:01:46 -0300512struct symbol *dso__last_symbol(struct dso *dso)
513{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300514 return symbols__last(&dso->symbols);
Arnaldo Carvalho de Melo5cf88a62018-04-25 17:01:46 -0300515}
516
Adrian Hunter9c00a812014-07-14 13:02:50 +0300517struct symbol *dso__next_symbol(struct symbol *sym)
518{
519 return symbols__next(sym);
520}
521
Arnaldo Carvalho de Melo18bd7262015-01-16 15:39:53 -0300522struct symbol *symbol__next_by_name(struct symbol *sym)
523{
524 struct symbol_name_rb_node *s = container_of(sym, struct symbol_name_rb_node, sym);
525 struct rb_node *n = rb_next(&s->rb_node);
526
527 return n ? &rb_entry(n, struct symbol_name_rb_node, rb_node)->sym : NULL;
528}
529
530 /*
Arnaldo Carvalho de Meloaf07eeb2018-04-25 17:46:28 -0300531 * Returns first symbol that matched with @name.
Arnaldo Carvalho de Melo18bd7262015-01-16 15:39:53 -0300532 */
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300533struct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200534{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300535 struct symbol *s = symbols__find_by_name(&dso->symbol_names, name,
Paul Clarked80406452017-04-25 13:15:49 -0500536 SYMBOL_TAG_INCLUDE__NONE);
537 if (!s)
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300538 s = symbols__find_by_name(&dso->symbol_names, name,
Paul Clarked80406452017-04-25 13:15:49 -0500539 SYMBOL_TAG_INCLUDE__DEFAULT_ONLY);
540 return s;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200541}
542
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300543void dso__sort_by_name(struct dso *dso)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200544{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300545 dso__set_sorted_by_name(dso);
546 return symbols__sort_by_name(&dso->symbol_names, &dso->symbols);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200547}
548
Adrian Hunter316d70d2013-10-08 11:45:48 +0300549int modules__parse(const char *filename, void *arg,
550 int (*process_module)(void *arg, const char *name,
Thomas Richter9ad46522017-08-03 15:49:02 +0200551 u64 start, u64 size))
Adrian Hunter316d70d2013-10-08 11:45:48 +0300552{
553 char *line = NULL;
554 size_t n;
555 FILE *file;
556 int err = 0;
557
558 file = fopen(filename, "r");
559 if (file == NULL)
560 return -1;
561
562 while (1) {
563 char name[PATH_MAX];
Thomas Richter9ad46522017-08-03 15:49:02 +0200564 u64 start, size;
565 char *sep, *endptr;
Adrian Hunter316d70d2013-10-08 11:45:48 +0300566 ssize_t line_len;
567
568 line_len = getline(&line, &n, file);
569 if (line_len < 0) {
570 if (feof(file))
571 break;
572 err = -1;
573 goto out;
574 }
575
576 if (!line) {
577 err = -1;
578 goto out;
579 }
580
581 line[--line_len] = '\0'; /* \n */
582
583 sep = strrchr(line, 'x');
584 if (sep == NULL)
585 continue;
586
587 hex2u64(sep + 1, &start);
588
589 sep = strchr(line, ' ');
590 if (sep == NULL)
591 continue;
592
593 *sep = '\0';
594
595 scnprintf(name, sizeof(name), "[%s]", line);
596
Thomas Richter9ad46522017-08-03 15:49:02 +0200597 size = strtoul(sep + 1, &endptr, 0);
598 if (*endptr != ' ' && *endptr != '\t')
599 continue;
600
601 err = process_module(arg, name, start, size);
Adrian Hunter316d70d2013-10-08 11:45:48 +0300602 if (err)
603 break;
604 }
605out:
606 free(line);
607 fclose(file);
608 return err;
609}
610
Arnaldo Carvalho de Meloe7110b92014-08-08 18:00:39 -0300611/*
612 * These are symbols in the kernel image, so make sure that
613 * sym is from a kernel DSO.
614 */
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300615static bool symbol__is_idle(const char *name)
David Ahern82d1deb2013-11-18 13:32:45 -0700616{
617 const char * const idle_symbols[] = {
618 "cpu_idle",
Arnaldo Carvalho de Meloe0336ed2014-08-08 18:02:41 -0300619 "cpu_startup_entry",
David Ahern82d1deb2013-11-18 13:32:45 -0700620 "intel_idle",
621 "default_idle",
622 "native_safe_halt",
623 "enter_idle",
624 "exit_idle",
625 "mwait_idle",
626 "mwait_idle_with_hints",
627 "poll_idle",
628 "ppc64_runlatch_off",
629 "pseries_dedicated_idle_sleep",
630 NULL
631 };
David Ahern82d1deb2013-11-18 13:32:45 -0700632 int i;
633
David Ahern82d1deb2013-11-18 13:32:45 -0700634 for (i = 0; idle_symbols[i]; i++) {
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300635 if (!strcmp(idle_symbols[i], name))
David Ahern82d1deb2013-11-18 13:32:45 -0700636 return true;
637 }
638
639 return false;
640}
641
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200642static int map__process_kallsym_symbol(void *arg, const char *name,
Cody P Schafer82151522012-08-10 15:22:48 -0700643 char type, u64 start)
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200644{
645 struct symbol *sym;
Arnaldo Carvalho de Melo333cc762018-04-27 15:36:15 -0300646 struct dso *dso = arg;
647 struct rb_root *root = &dso->symbols;
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200648
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300649 if (!symbol_type__filter(type))
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200650 return 0;
651
Cody P Schafer82151522012-08-10 15:22:48 -0700652 /*
653 * module symbols are not sorted so we add all
654 * symbols, setting length to 0, and rely on
655 * symbols__fixup_end() to fix it up.
656 */
Arnaldo Carvalho de Meloaf30bff2018-04-26 11:09:10 -0300657 sym = symbol__new(start, 0, kallsyms2elf_binding(type), kallsyms2elf_type(type), name);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200658 if (sym == NULL)
659 return -ENOMEM;
660 /*
661 * We will pass the symbols to the filter later, in
662 * map__split_kallsyms, when we have split the maps per module
663 */
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300664 __symbols__insert(root, sym, !strchr(name, '['));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800665
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200666 return 0;
667}
668
669/*
670 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
671 * so that we can in the next step set the symbol ->end address and then
672 * call kernel_maps__split_kallsyms.
673 */
Arnaldo Carvalho de Melo333cc762018-04-27 15:36:15 -0300674static int dso__load_all_kallsyms(struct dso *dso, const char *filename)
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200675{
Arnaldo Carvalho de Melo333cc762018-04-27 15:36:15 -0300676 return kallsyms__parse(filename, dso, map__process_kallsym_symbol);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200677}
678
Arnaldo Carvalho de Melo15e0e2d2018-04-27 15:59:32 -0300679static int map_groups__split_kallsyms_for_kcore(struct map_groups *kmaps, struct dso *dso)
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300680{
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300681 struct map *curr_map;
682 struct symbol *pos;
Adrian Hunter866548d2015-11-06 15:59:29 +0200683 int count = 0;
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300684 struct rb_root old_root = dso->symbols;
685 struct rb_root *root = &dso->symbols;
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300686 struct rb_node *next = rb_first(root);
687
Wang Nanba927322015-04-07 08:22:45 +0000688 if (!kmaps)
689 return -1;
690
Adrian Hunter866548d2015-11-06 15:59:29 +0200691 *root = RB_ROOT;
692
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300693 while (next) {
694 char *module;
695
696 pos = rb_entry(next, struct symbol, rb_node);
697 next = rb_next(&pos->rb_node);
698
Adrian Hunter866548d2015-11-06 15:59:29 +0200699 rb_erase_init(&pos->rb_node, &old_root);
700
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300701 module = strchr(pos->name, '\t');
702 if (module)
703 *module = '\0';
704
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300705 curr_map = map_groups__find(kmaps, pos->start);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300706
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300707 if (!curr_map) {
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300708 symbol__delete(pos);
Adrian Hunter866548d2015-11-06 15:59:29 +0200709 continue;
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300710 }
Adrian Hunter866548d2015-11-06 15:59:29 +0200711
712 pos->start -= curr_map->start - curr_map->pgoff;
713 if (pos->end)
714 pos->end -= curr_map->start - curr_map->pgoff;
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300715 symbols__insert(&curr_map->dso->symbols, pos);
Adrian Hunter866548d2015-11-06 15:59:29 +0200716 ++count;
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300717 }
718
719 /* Symbols have been adjusted */
720 dso->adjust_symbols = 1;
721
Adrian Hunter866548d2015-11-06 15:59:29 +0200722 return count;
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300723}
724
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300725/*
726 * Split the symbols into maps, making sure there are no overlaps, i.e. the
727 * kernel range is broken in several maps, named [kernel].N, as we don't have
728 * the original ELF section names vmlinux have.
729 */
Arnaldo Carvalho de Melo15e0e2d2018-04-27 15:59:32 -0300730static int map_groups__split_kallsyms(struct map_groups *kmaps, struct dso *dso, u64 delta,
731 struct map *initial_map)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300732{
Wang Nanba927322015-04-07 08:22:45 +0000733 struct machine *machine;
Arnaldo Carvalho de Melo15e0e2d2018-04-27 15:59:32 -0300734 struct map *curr_map = initial_map;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300735 struct symbol *pos;
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300736 int count = 0, moved = 0;
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300737 struct rb_root *root = &dso->symbols;
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200738 struct rb_node *next = rb_first(root);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300739 int kernel_range = 0;
740
Wang Nanba927322015-04-07 08:22:45 +0000741 if (!kmaps)
742 return -1;
743
744 machine = kmaps->machine;
745
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300746 while (next) {
747 char *module;
748
749 pos = rb_entry(next, struct symbol, rb_node);
750 next = rb_next(&pos->rb_node);
751
752 module = strchr(pos->name, '\t');
753 if (module) {
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200754 if (!symbol_conf.use_modules)
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200755 goto discard_symbol;
756
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300757 *module++ = '\0';
758
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200759 if (strcmp(curr_map->dso->short_name, module)) {
Arnaldo Carvalho de Melo15e0e2d2018-04-27 15:59:32 -0300760 if (curr_map != initial_map &&
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300761 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300762 machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800763 /*
764 * We assume all symbols of a module are
765 * continuous in * kallsyms, so curr_map
766 * points to a module and all its
767 * symbols are in its kmap. Mark it as
768 * loaded.
769 */
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300770 dso__set_loaded(curr_map->dso);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300771 }
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200772
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300773 curr_map = map_groups__find_by_name(kmaps, module);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800774 if (curr_map == NULL) {
Arnaldo Carvalho de Melo2f519032010-05-17 17:57:59 -0300775 pr_debug("%s/proc/{kallsyms,modules} "
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800776 "inconsistency while looking "
777 "for \"%s\" module!\n",
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300778 machine->root_dir, module);
Arnaldo Carvalho de Melo15e0e2d2018-04-27 15:59:32 -0300779 curr_map = initial_map;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800780 goto discard_symbol;
781 }
782
783 if (curr_map->dso->loaded &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300784 !machine__is_default_guest(machine))
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200785 goto discard_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300786 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300787 /*
788 * So that we look just like we get from .ko files,
Arnaldo Carvalho de Melo15e0e2d2018-04-27 15:59:32 -0300789 * i.e. not prelinked, relative to initial_map->start.
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300790 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200791 pos->start = curr_map->map_ip(curr_map, pos->start);
792 pos->end = curr_map->map_ip(curr_map, pos->end);
Arnaldo Carvalho de Melo15e0e2d2018-04-27 15:59:32 -0300793 } else if (curr_map != initial_map) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300794 char dso_name[PATH_MAX];
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300795 struct dso *ndso;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300796
Adrian Hunterd9b62ab2014-01-29 16:14:43 +0200797 if (delta) {
798 /* Kernel was relocated at boot time */
799 pos->start -= delta;
800 pos->end -= delta;
801 }
802
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200803 if (count == 0) {
Arnaldo Carvalho de Melo15e0e2d2018-04-27 15:59:32 -0300804 curr_map = initial_map;
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300805 goto add_symbol;
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200806 }
807
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300808 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800809 snprintf(dso_name, sizeof(dso_name),
810 "[guest.kernel].%d",
811 kernel_range++);
812 else
813 snprintf(dso_name, sizeof(dso_name),
814 "[kernel].%d",
815 kernel_range++);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300816
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300817 ndso = dso__new(dso_name);
818 if (ndso == NULL)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300819 return -1;
820
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300821 ndso->kernel = dso->kernel;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800822
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300823 curr_map = map__new2(pos->start, ndso);
Zhang, Yanmin37fe5fc2010-02-25 11:00:51 +0800824 if (curr_map == NULL) {
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300825 dso__put(ndso);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300826 return -1;
827 }
828
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200829 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo9de89fe2010-02-03 16:52:00 -0200830 map_groups__insert(kmaps, curr_map);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300831 ++kernel_range;
Adrian Hunterd9b62ab2014-01-29 16:14:43 +0200832 } else if (delta) {
833 /* Kernel was relocated at boot time */
834 pos->start -= delta;
835 pos->end -= delta;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300836 }
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300837add_symbol:
Arnaldo Carvalho de Melo15e0e2d2018-04-27 15:59:32 -0300838 if (curr_map != initial_map) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300839 rb_erase(&pos->rb_node, root);
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300840 symbols__insert(&curr_map->dso->symbols, pos);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300841 ++moved;
842 } else
843 ++count;
844
845 continue;
846discard_symbol:
847 rb_erase(&pos->rb_node, root);
848 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300849 }
850
Arnaldo Carvalho de Melo15e0e2d2018-04-27 15:59:32 -0300851 if (curr_map != initial_map &&
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300852 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300853 machine__is_default_guest(kmaps->machine)) {
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300854 dso__set_loaded(curr_map->dso);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800855 }
856
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200857 return count + moved;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300858}
859
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300860bool symbol__restricted_filename(const char *filename,
861 const char *restricted_filename)
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -0300862{
863 bool restricted = false;
864
865 if (symbol_conf.kptr_restrict) {
866 char *r = realpath(filename, NULL);
867
868 if (r != NULL) {
869 restricted = strcmp(r, restricted_filename) == 0;
870 free(r);
871 return restricted;
872 }
873 }
874
875 return restricted;
876}
877
Adrian Hunter52afdaf2013-10-09 15:01:11 +0300878struct module_info {
879 struct rb_node rb_node;
880 char *name;
881 u64 start;
882};
883
884static void add_module(struct module_info *mi, struct rb_root *modules)
885{
886 struct rb_node **p = &modules->rb_node;
887 struct rb_node *parent = NULL;
888 struct module_info *m;
889
890 while (*p != NULL) {
891 parent = *p;
892 m = rb_entry(parent, struct module_info, rb_node);
893 if (strcmp(mi->name, m->name) < 0)
894 p = &(*p)->rb_left;
895 else
896 p = &(*p)->rb_right;
897 }
898 rb_link_node(&mi->rb_node, parent, p);
899 rb_insert_color(&mi->rb_node, modules);
900}
901
902static void delete_modules(struct rb_root *modules)
903{
904 struct module_info *mi;
905 struct rb_node *next = rb_first(modules);
906
907 while (next) {
908 mi = rb_entry(next, struct module_info, rb_node);
909 next = rb_next(&mi->rb_node);
910 rb_erase(&mi->rb_node, modules);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300911 zfree(&mi->name);
Adrian Hunter52afdaf2013-10-09 15:01:11 +0300912 free(mi);
913 }
914}
915
916static struct module_info *find_module(const char *name,
917 struct rb_root *modules)
918{
919 struct rb_node *n = modules->rb_node;
920
921 while (n) {
922 struct module_info *m;
923 int cmp;
924
925 m = rb_entry(n, struct module_info, rb_node);
926 cmp = strcmp(name, m->name);
927 if (cmp < 0)
928 n = n->rb_left;
929 else if (cmp > 0)
930 n = n->rb_right;
931 else
932 return m;
933 }
934
935 return NULL;
936}
937
Thomas Richter9ad46522017-08-03 15:49:02 +0200938static int __read_proc_modules(void *arg, const char *name, u64 start,
939 u64 size __maybe_unused)
Adrian Hunter52afdaf2013-10-09 15:01:11 +0300940{
941 struct rb_root *modules = arg;
942 struct module_info *mi;
943
944 mi = zalloc(sizeof(struct module_info));
945 if (!mi)
946 return -ENOMEM;
947
948 mi->name = strdup(name);
949 mi->start = start;
950
951 if (!mi->name) {
952 free(mi);
953 return -ENOMEM;
954 }
955
956 add_module(mi, modules);
957
958 return 0;
959}
960
961static int read_proc_modules(const char *filename, struct rb_root *modules)
962{
963 if (symbol__restricted_filename(filename, "/proc/modules"))
964 return -1;
965
966 if (modules__parse(filename, modules, __read_proc_modules)) {
967 delete_modules(modules);
968 return -1;
969 }
970
971 return 0;
972}
973
Adrian Hunterfc1b6912013-10-14 16:57:29 +0300974int compare_proc_modules(const char *from, const char *to)
975{
976 struct rb_root from_modules = RB_ROOT;
977 struct rb_root to_modules = RB_ROOT;
978 struct rb_node *from_node, *to_node;
979 struct module_info *from_m, *to_m;
980 int ret = -1;
981
982 if (read_proc_modules(from, &from_modules))
983 return -1;
984
985 if (read_proc_modules(to, &to_modules))
986 goto out_delete_from;
987
988 from_node = rb_first(&from_modules);
989 to_node = rb_first(&to_modules);
990 while (from_node) {
991 if (!to_node)
992 break;
993
994 from_m = rb_entry(from_node, struct module_info, rb_node);
995 to_m = rb_entry(to_node, struct module_info, rb_node);
996
997 if (from_m->start != to_m->start ||
998 strcmp(from_m->name, to_m->name))
999 break;
1000
1001 from_node = rb_next(from_node);
1002 to_node = rb_next(to_node);
1003 }
1004
1005 if (!from_node && !to_node)
1006 ret = 0;
1007
1008 delete_modules(&to_modules);
1009out_delete_from:
1010 delete_modules(&from_modules);
1011
1012 return ret;
1013}
1014
Arnaldo Carvalho de Melodce04782018-04-25 17:28:55 -03001015struct map *map_groups__first(struct map_groups *mg)
1016{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001017 return maps__first(&mg->maps);
Arnaldo Carvalho de Melodce04782018-04-25 17:28:55 -03001018}
1019
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001020static int do_validate_kcore_modules(const char *filename, struct map *map,
1021 struct map_groups *kmaps)
1022{
1023 struct rb_root modules = RB_ROOT;
1024 struct map *old_map;
1025 int err;
1026
1027 err = read_proc_modules(filename, &modules);
1028 if (err)
1029 return err;
1030
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001031 old_map = map_groups__first(kmaps);
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001032 while (old_map) {
1033 struct map *next = map_groups__next(old_map);
1034 struct module_info *mi;
1035
1036 if (old_map == map || old_map->start == map->start) {
1037 /* The kernel map */
1038 old_map = next;
1039 continue;
1040 }
1041
1042 /* Module must be in memory at the same address */
1043 mi = find_module(old_map->dso->short_name, &modules);
1044 if (!mi || mi->start != old_map->start) {
1045 err = -EINVAL;
1046 goto out;
1047 }
1048
1049 old_map = next;
1050 }
1051out:
1052 delete_modules(&modules);
1053 return err;
1054}
1055
1056/*
1057 * If kallsyms is referenced by name then we look for filename in the same
1058 * directory.
1059 */
1060static bool filename_from_kallsyms_filename(char *filename,
1061 const char *base_name,
1062 const char *kallsyms_filename)
1063{
1064 char *name;
1065
1066 strcpy(filename, kallsyms_filename);
1067 name = strrchr(filename, '/');
1068 if (!name)
1069 return false;
1070
1071 name += 1;
1072
1073 if (!strcmp(name, "kallsyms")) {
1074 strcpy(name, base_name);
1075 return true;
1076 }
1077
1078 return false;
1079}
1080
1081static int validate_kcore_modules(const char *kallsyms_filename,
1082 struct map *map)
1083{
Wang Nanba927322015-04-07 08:22:45 +00001084 struct map_groups *kmaps = map__kmaps(map);
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001085 char modules_filename[PATH_MAX];
1086
Wang Nanba927322015-04-07 08:22:45 +00001087 if (!kmaps)
1088 return -EINVAL;
1089
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001090 if (!filename_from_kallsyms_filename(modules_filename, "modules",
1091 kallsyms_filename))
1092 return -EINVAL;
1093
1094 if (do_validate_kcore_modules(modules_filename, map, kmaps))
1095 return -EINVAL;
1096
1097 return 0;
1098}
1099
Adrian Huntera00d28c2014-01-29 16:14:41 +02001100static int validate_kcore_addresses(const char *kallsyms_filename,
1101 struct map *map)
1102{
1103 struct kmap *kmap = map__kmap(map);
1104
Wang Nanba927322015-04-07 08:22:45 +00001105 if (!kmap)
1106 return -EINVAL;
1107
Adrian Huntera00d28c2014-01-29 16:14:41 +02001108 if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
1109 u64 start;
1110
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001111 if (kallsyms__get_function_start(kallsyms_filename,
1112 kmap->ref_reloc_sym->name, &start))
1113 return -ENOENT;
Adrian Huntera00d28c2014-01-29 16:14:41 +02001114 if (start != kmap->ref_reloc_sym->addr)
1115 return -EINVAL;
1116 }
1117
1118 return validate_kcore_modules(kallsyms_filename, map);
1119}
1120
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001121struct kcore_mapfn_data {
1122 struct dso *dso;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001123 struct list_head maps;
1124};
1125
1126static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1127{
1128 struct kcore_mapfn_data *md = data;
1129 struct map *map;
1130
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001131 map = map__new2(start, md->dso);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001132 if (map == NULL)
1133 return -ENOMEM;
1134
1135 map->end = map->start + len;
1136 map->pgoff = pgoff;
1137
1138 list_add(&map->node, &md->maps);
1139
1140 return 0;
1141}
1142
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001143static int dso__load_kcore(struct dso *dso, struct map *map,
1144 const char *kallsyms_filename)
1145{
Wang Nanba927322015-04-07 08:22:45 +00001146 struct map_groups *kmaps = map__kmaps(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001147 struct kcore_mapfn_data md;
1148 struct map *old_map, *new_map, *replacement_map = NULL;
1149 bool is_64_bit;
1150 int err, fd;
1151 char kcore_filename[PATH_MAX];
Adrian Hunter565499782018-05-09 14:43:34 +03001152 u64 stext;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001153
Wang Nanba927322015-04-07 08:22:45 +00001154 if (!kmaps)
1155 return -EINVAL;
1156
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001157 /* This function requires that the map is the kernel map */
Arnaldo Carvalho de Meloefdd5c62018-04-23 16:43:47 -03001158 if (!__map__is_kernel(map))
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001159 return -EINVAL;
1160
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001161 if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1162 kallsyms_filename))
1163 return -EINVAL;
1164
Adrian Huntera00d28c2014-01-29 16:14:41 +02001165 /* Modules and kernel must be present at their original addresses */
1166 if (validate_kcore_addresses(kallsyms_filename, map))
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001167 return -EINVAL;
1168
1169 md.dso = dso;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001170 INIT_LIST_HEAD(&md.maps);
1171
1172 fd = open(kcore_filename, O_RDONLY);
Li Zhang36c8bb52015-06-19 16:57:33 +08001173 if (fd < 0) {
Adrian Hunter133de942015-08-20 13:07:40 +03001174 pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n",
1175 kcore_filename);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001176 return -EINVAL;
Li Zhang36c8bb52015-06-19 16:57:33 +08001177 }
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001178
1179 /* Read new maps into temporary lists */
Arnaldo Carvalho de Meloe9814df2018-04-26 16:11:47 -03001180 err = file__read_maps(fd, map->prot & PROT_EXEC, kcore_mapfn, &md,
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001181 &is_64_bit);
1182 if (err)
1183 goto out_err;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +03001184 dso->is_64_bit = is_64_bit;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001185
1186 if (list_empty(&md.maps)) {
1187 err = -EINVAL;
1188 goto out_err;
1189 }
1190
1191 /* Remove old maps */
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001192 old_map = map_groups__first(kmaps);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001193 while (old_map) {
1194 struct map *next = map_groups__next(old_map);
1195
1196 if (old_map != map)
1197 map_groups__remove(kmaps, old_map);
1198 old_map = next;
1199 }
1200
Adrian Hunter565499782018-05-09 14:43:34 +03001201 /* Find the kernel map using the '_stext' symbol */
1202 if (!kallsyms__get_function_start(kallsyms_filename, "_stext", &stext)) {
1203 list_for_each_entry(new_map, &md.maps, node) {
1204 if (stext >= new_map->start && stext < new_map->end) {
1205 replacement_map = new_map;
1206 break;
1207 }
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001208 }
1209 }
1210
1211 if (!replacement_map)
1212 replacement_map = list_entry(md.maps.next, struct map, node);
1213
1214 /* Add new maps */
1215 while (!list_empty(&md.maps)) {
1216 new_map = list_entry(md.maps.next, struct map, node);
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -03001217 list_del_init(&new_map->node);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001218 if (new_map == replacement_map) {
1219 map->start = new_map->start;
1220 map->end = new_map->end;
1221 map->pgoff = new_map->pgoff;
1222 map->map_ip = new_map->map_ip;
1223 map->unmap_ip = new_map->unmap_ip;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001224 /* Ensure maps are correctly ordered */
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001225 map__get(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001226 map_groups__remove(kmaps, map);
1227 map_groups__insert(kmaps, map);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001228 map__put(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001229 } else {
1230 map_groups__insert(kmaps, new_map);
1231 }
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001232
1233 map__put(new_map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001234 }
1235
1236 /*
1237 * Set the data type and long name so that kcore can be read via
1238 * dso__data_read_addr().
1239 */
1240 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001241 dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001242 else
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001243 dso->binary_type = DSO_BINARY_TYPE__KCORE;
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001244 dso__set_long_name(dso, strdup(kcore_filename), true);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001245
1246 close(fd);
1247
Arnaldo Carvalho de Meloe9814df2018-04-26 16:11:47 -03001248 if (map->prot & PROT_EXEC)
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001249 pr_debug("Using %s for kernel object code\n", kcore_filename);
1250 else
1251 pr_debug("Using %s for kernel data\n", kcore_filename);
1252
1253 return 0;
1254
1255out_err:
1256 while (!list_empty(&md.maps)) {
1257 map = list_entry(md.maps.next, struct map, node);
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -03001258 list_del_init(&map->node);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001259 map__put(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001260 }
1261 close(fd);
1262 return -EINVAL;
1263}
1264
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001265/*
1266 * If the kernel is relocated at boot time, kallsyms won't match. Compute the
1267 * delta based on the relocation reference symbol.
1268 */
Arnaldo Carvalho de Melo019c6822018-04-27 15:47:13 -03001269static int kallsyms__delta(struct kmap *kmap, const char *filename, u64 *delta)
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001270{
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001271 u64 addr;
1272
1273 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1274 return 0;
1275
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001276 if (kallsyms__get_function_start(filename, kmap->ref_reloc_sym->name, &addr))
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001277 return -1;
1278
1279 *delta = addr - kmap->ref_reloc_sym->addr;
1280 return 0;
1281}
1282
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001283int __dso__load_kallsyms(struct dso *dso, const char *filename,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001284 struct map *map, bool no_kcore)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001285{
Arnaldo Carvalho de Melo019c6822018-04-27 15:47:13 -03001286 struct kmap *kmap = map__kmap(map);
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001287 u64 delta = 0;
1288
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03001289 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1290 return -1;
1291
Arnaldo Carvalho de Melo019c6822018-04-27 15:47:13 -03001292 if (!kmap || !kmap->kmaps)
1293 return -1;
1294
Arnaldo Carvalho de Melo333cc762018-04-27 15:36:15 -03001295 if (dso__load_all_kallsyms(dso, filename) < 0)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001296 return -1;
1297
Arnaldo Carvalho de Melo019c6822018-04-27 15:47:13 -03001298 if (kallsyms__delta(kmap, filename, &delta))
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001299 return -1;
1300
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001301 symbols__fixup_end(&dso->symbols);
1302 symbols__fixup_duplicate(&dso->symbols);
Anton Blanchard3f5a4272011-08-24 16:40:15 +10001303
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001304 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001305 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001306 else
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001307 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001308
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001309 if (!no_kcore && !dso__load_kcore(dso, map, filename))
Arnaldo Carvalho de Melo15e0e2d2018-04-27 15:59:32 -03001310 return map_groups__split_kallsyms_for_kcore(kmap->kmaps, dso);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001311 else
Arnaldo Carvalho de Melo15e0e2d2018-04-27 15:59:32 -03001312 return map_groups__split_kallsyms(kmap->kmaps, dso, delta, map);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001313}
1314
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001315int dso__load_kallsyms(struct dso *dso, const char *filename,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001316 struct map *map)
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001317{
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001318 return __dso__load_kallsyms(dso, filename, map, false);
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001319}
1320
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001321static int dso__load_perf_map(const char *map_path, struct dso *dso)
Pekka Enberg80d496b2009-06-08 21:12:48 +03001322{
1323 char *line = NULL;
1324 size_t n;
1325 FILE *file;
1326 int nr_syms = 0;
1327
Krister Johansenbf2e7102017-07-05 18:48:09 -07001328 file = fopen(map_path, "r");
Pekka Enberg80d496b2009-06-08 21:12:48 +03001329 if (file == NULL)
1330 goto out_failure;
1331
1332 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001333 u64 start, size;
Pekka Enberg80d496b2009-06-08 21:12:48 +03001334 struct symbol *sym;
1335 int line_len, len;
1336
1337 line_len = getline(&line, &n, file);
1338 if (line_len < 0)
1339 break;
1340
1341 if (!line)
1342 goto out_failure;
1343
1344 line[--line_len] = '\0'; /* \n */
1345
1346 len = hex2u64(line, &start);
1347
1348 len++;
1349 if (len + 2 >= line_len)
1350 continue;
1351
1352 len += hex2u64(line + len, &size);
1353
1354 len++;
1355 if (len + 2 >= line_len)
1356 continue;
1357
Arnaldo Carvalho de Meloaf30bff2018-04-26 11:09:10 -03001358 sym = symbol__new(start, size, STB_GLOBAL, STT_FUNC, line + len);
Pekka Enberg80d496b2009-06-08 21:12:48 +03001359
1360 if (sym == NULL)
1361 goto out_delete_line;
1362
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001363 symbols__insert(&dso->symbols, sym);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001364 nr_syms++;
Pekka Enberg80d496b2009-06-08 21:12:48 +03001365 }
1366
1367 free(line);
1368 fclose(file);
1369
1370 return nr_syms;
1371
1372out_delete_line:
1373 free(line);
1374out_failure:
1375 return -1;
1376}
1377
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001378static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1379 enum dso_binary_type type)
1380{
1381 switch (type) {
1382 case DSO_BINARY_TYPE__JAVA_JIT:
1383 case DSO_BINARY_TYPE__DEBUGLINK:
1384 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1385 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1386 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1387 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1388 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1389 return !kmod && dso->kernel == DSO_TYPE_USER;
1390
1391 case DSO_BINARY_TYPE__KALLSYMS:
1392 case DSO_BINARY_TYPE__VMLINUX:
1393 case DSO_BINARY_TYPE__KCORE:
1394 return dso->kernel == DSO_TYPE_KERNEL;
1395
1396 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1397 case DSO_BINARY_TYPE__GUEST_VMLINUX:
1398 case DSO_BINARY_TYPE__GUEST_KCORE:
1399 return dso->kernel == DSO_TYPE_GUEST_KERNEL;
1400
1401 case DSO_BINARY_TYPE__GUEST_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +09001402 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001403 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +09001404 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001405 /*
1406 * kernel modules know their symtab type - it's set when
Arnaldo Carvalho de Melo9f2de312015-06-01 12:01:02 -03001407 * creating a module dso in machine__findnew_module_map().
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001408 */
1409 return kmod && dso->symtab_type == type;
1410
1411 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
Krister Johansend2396992017-07-05 18:48:13 -07001412 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001413 return true;
1414
1415 case DSO_BINARY_TYPE__NOT_FOUND:
1416 default:
1417 return false;
1418 }
1419}
1420
Krister Johansenbf2e7102017-07-05 18:48:09 -07001421/* Checks for the existence of the perf-<pid>.map file in two different
1422 * locations. First, if the process is a separate mount namespace, check in
1423 * that namespace using the pid of the innermost pid namespace. If's not in a
1424 * namespace, or the file can't be found there, try in the mount namespace of
1425 * the tracing process using our view of its pid.
1426 */
1427static int dso__find_perf_map(char *filebuf, size_t bufsz,
1428 struct nsinfo **nsip)
1429{
1430 struct nscookie nsc;
1431 struct nsinfo *nsi;
1432 struct nsinfo *nnsi;
1433 int rc = -1;
1434
1435 nsi = *nsip;
1436
1437 if (nsi->need_setns) {
1438 snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsi->nstgid);
1439 nsinfo__mountns_enter(nsi, &nsc);
1440 rc = access(filebuf, R_OK);
1441 nsinfo__mountns_exit(&nsc);
1442 if (rc == 0)
1443 return rc;
1444 }
1445
1446 nnsi = nsinfo__copy(nsi);
1447 if (nnsi) {
1448 nsinfo__put(nsi);
1449
1450 nnsi->need_setns = false;
1451 snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nnsi->tgid);
1452 *nsip = nnsi;
1453 rc = 0;
1454 }
1455
1456 return rc;
1457}
1458
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001459int dso__load(struct dso *dso, struct map *map)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001460{
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001461 char *name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001462 int ret = -1;
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001463 u_int i;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001464 struct machine *machine;
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001465 char *root_dir = (char *) "";
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001466 int ss_pos = 0;
1467 struct symsrc ss_[2];
1468 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001469 bool kmod;
Krister Johansenbf2e7102017-07-05 18:48:09 -07001470 bool perfmap;
Dima Kogan5baecbc2015-09-07 17:34:19 -07001471 unsigned char build_id[BUILD_ID_SIZE];
Krister Johansen843ff372017-07-05 18:48:08 -07001472 struct nscookie nsc;
Krister Johansenbf2e7102017-07-05 18:48:09 -07001473 char newmapname[PATH_MAX];
1474 const char *map_path = dso->long_name;
1475
1476 perfmap = strncmp(dso->name, "/tmp/perf-", 10) == 0;
1477 if (perfmap) {
1478 if (dso->nsinfo && (dso__find_perf_map(newmapname,
1479 sizeof(newmapname), &dso->nsinfo) == 0)) {
1480 map_path = newmapname;
1481 }
1482 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001483
Krister Johansen843ff372017-07-05 18:48:08 -07001484 nsinfo__mountns_enter(dso->nsinfo, &nsc);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001485 pthread_mutex_lock(&dso->lock);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001486
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001487 /* check again under the dso->lock */
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001488 if (dso__loaded(dso)) {
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001489 ret = 1;
1490 goto out;
1491 }
1492
Adrian Hunter4d99e412018-05-22 13:54:33 +03001493 if (map->groups && map->groups->machine)
1494 machine = map->groups->machine;
1495 else
1496 machine = NULL;
1497
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001498 if (dso->kernel) {
1499 if (dso->kernel == DSO_TYPE_KERNEL)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001500 ret = dso__load_kernel_sym(dso, map);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001501 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001502 ret = dso__load_guest_kernel_sym(dso, map);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001503
Adrian Hunter4d99e412018-05-22 13:54:33 +03001504 if (machine__is(machine, "x86_64"))
1505 machine__map_x86_64_entry_trampolines(machine, dso);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001506 goto out;
1507 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001508
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001509 dso->adjust_symbols = 0;
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001510
Krister Johansenbf2e7102017-07-05 18:48:09 -07001511 if (perfmap) {
Pekka Enberg981c1252011-08-09 22:54:18 +03001512 struct stat st;
1513
Krister Johansenbf2e7102017-07-05 18:48:09 -07001514 if (lstat(map_path, &st) < 0)
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001515 goto out;
Pekka Enberg981c1252011-08-09 22:54:18 +03001516
Arnaldo Carvalho de Melo2059fc72015-11-12 16:50:13 -03001517 if (!symbol_conf.force && st.st_uid && (st.st_uid != geteuid())) {
Pekka Enberg981c1252011-08-09 22:54:18 +03001518 pr_warning("File %s not owned by current user or root, "
Krister Johansenbf2e7102017-07-05 18:48:09 -07001519 "ignoring it (use -f to override).\n", map_path);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001520 goto out;
Pekka Enberg981c1252011-08-09 22:54:18 +03001521 }
1522
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001523 ret = dso__load_perf_map(map_path, dso);
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001524 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1525 DSO_BINARY_TYPE__NOT_FOUND;
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001526 goto out;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001527 }
1528
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001529 if (machine)
1530 root_dir = machine->root_dir;
1531
David Ahern164c8002013-01-14 10:46:47 -07001532 name = malloc(PATH_MAX);
1533 if (!name)
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001534 goto out;
David Ahern164c8002013-01-14 10:46:47 -07001535
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001536 kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
Namhyung Kimc00c48f2014-11-04 10:14:27 +09001537 dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
1538 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE ||
1539 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001540
Dima Kogan5baecbc2015-09-07 17:34:19 -07001541
1542 /*
1543 * Read the build id if possible. This is required for
1544 * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work
1545 */
Kan Lianged6c1662016-12-13 10:29:44 -05001546 if (!dso->has_build_id &&
Victor Kamensky9b200652017-02-06 15:48:28 -08001547 is_regular_file(dso->long_name)) {
1548 __symbol__join_symfs(name, PATH_MAX, dso->long_name);
1549 if (filename__read_build_id(name, build_id, BUILD_ID_SIZE) > 0)
Dima Kogan5baecbc2015-09-07 17:34:19 -07001550 dso__set_build_id(dso, build_id);
Victor Kamensky9b200652017-02-06 15:48:28 -08001551 }
Dima Kogan5baecbc2015-09-07 17:34:19 -07001552
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001553 /*
1554 * Iterate over candidate debug images.
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001555 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1556 * and/or opd section) for processing.
Dave Martin6da80ce2010-07-30 09:50:09 -03001557 */
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001558 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001559 struct symsrc *ss = &ss_[ss_pos];
1560 bool next_slot = false;
Krister Johansenf045b8c2017-07-05 18:48:11 -07001561 bool is_reg;
Krister Johansend2396992017-07-05 18:48:13 -07001562 bool nsexit;
Jiri Olsac3962962018-02-15 13:26:28 +01001563 int sirc = -1;
Dave Martin6da80ce2010-07-30 09:50:09 -03001564
Cody P Schafer005f9292012-08-10 15:22:58 -07001565 enum dso_binary_type symtab_type = binary_type_symtab[i];
Dave Martin6da80ce2010-07-30 09:50:09 -03001566
Krister Johansend2396992017-07-05 18:48:13 -07001567 nsexit = (symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE ||
1568 symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO);
1569
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001570 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1571 continue;
1572
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -03001573 if (dso__read_binary_type_filename(dso, symtab_type,
1574 root_dir, name, PATH_MAX))
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001575 continue;
Dave Martin6da80ce2010-07-30 09:50:09 -03001576
Krister Johansend2396992017-07-05 18:48:13 -07001577 if (nsexit)
Krister Johansenf045b8c2017-07-05 18:48:11 -07001578 nsinfo__mountns_exit(&nsc);
Jiri Olsa40356722016-01-20 12:56:32 +01001579
Krister Johansenf045b8c2017-07-05 18:48:11 -07001580 is_reg = is_regular_file(name);
Jiri Olsac3962962018-02-15 13:26:28 +01001581 if (is_reg)
1582 sirc = symsrc__init(ss, dso, name, symtab_type);
Krister Johansenf045b8c2017-07-05 18:48:11 -07001583
Krister Johansend2396992017-07-05 18:48:13 -07001584 if (nsexit)
Krister Johansenf045b8c2017-07-05 18:48:11 -07001585 nsinfo__mountns_enter(dso->nsinfo, &nsc);
1586
Jiri Olsac3962962018-02-15 13:26:28 +01001587 if (!is_reg || sirc < 0)
Dave Martin6da80ce2010-07-30 09:50:09 -03001588 continue;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001589
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001590 if (!syms_ss && symsrc__has_symtab(ss)) {
1591 syms_ss = ss;
1592 next_slot = true;
Adrian Hunter0058aef2013-12-03 09:23:08 +02001593 if (!dso->symsrc_filename)
1594 dso->symsrc_filename = strdup(name);
Cody P Schaferd26cd122012-08-10 15:23:00 -07001595 }
1596
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001597 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1598 runtime_ss = ss;
1599 next_slot = true;
Cody P Schafera44f6052012-08-10 15:22:59 -07001600 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001601
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001602 if (next_slot) {
1603 ss_pos++;
Jiri Olsa33ff5812012-04-18 15:46:58 +02001604
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001605 if (syms_ss && runtime_ss)
1606 break;
Namhyung Kim98e9f032014-02-20 10:32:54 +09001607 } else {
1608 symsrc__destroy(ss);
Dave Martin6da80ce2010-07-30 09:50:09 -03001609 }
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001610
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001611 }
Dave Martin6da80ce2010-07-30 09:50:09 -03001612
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001613 if (!runtime_ss && !syms_ss)
1614 goto out_free;
1615
1616 if (runtime_ss && !syms_ss) {
1617 syms_ss = runtime_ss;
Arnaldo Carvalho de Melo60e4b102011-03-22 15:42:14 -03001618 }
1619
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001620 /* We'll have to hope for the best */
1621 if (!runtime_ss && syms_ss)
1622 runtime_ss = syms_ss;
1623
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001624 if (syms_ss)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001625 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, kmod);
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001626 else
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001627 ret = -1;
1628
David Ahernf47b58b2012-08-19 09:47:14 -06001629 if (ret > 0) {
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001630 int nr_plt;
1631
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001632 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss);
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001633 if (nr_plt > 0)
1634 ret += nr_plt;
1635 }
1636
1637 for (; ss_pos > 0; ss_pos--)
1638 symsrc__destroy(&ss_[ss_pos - 1]);
1639out_free:
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001640 free(name);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001641 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001642 ret = 0;
1643out:
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001644 dso__set_loaded(dso);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001645 pthread_mutex_unlock(&dso->lock);
Krister Johansen843ff372017-07-05 18:48:08 -07001646 nsinfo__mountns_exit(&nsc);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001647
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001648 return ret;
1649}
1650
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001651struct map *map_groups__find_by_name(struct map_groups *mg, const char *name)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001652{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001653 struct maps *maps = &mg->maps;
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -03001654 struct map *map;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001655
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001656 down_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -03001657
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -03001658 for (map = maps__first(maps); map; map = map__next(map)) {
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001659 if (map->dso && strcmp(map->dso->short_name, name) == 0)
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -03001660 goto out_unlock;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001661 }
1662
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -03001663 map = NULL;
1664
1665out_unlock:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001666 up_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -03001667 return map;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001668}
1669
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001670int dso__load_vmlinux(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001671 const char *vmlinux, bool vmlinux_allocated)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001672{
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001673 int err = -1;
1674 struct symsrc ss;
David Ahernec5761e2010-12-09 13:27:07 -07001675 char symfs_vmlinux[PATH_MAX];
Cody P Schafer005f9292012-08-10 15:22:58 -07001676 enum dso_binary_type symtab_type;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001677
Namhyung Kim5698d2c2013-07-17 17:08:15 +09001678 if (vmlinux[0] == '/')
1679 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1680 else
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -03001681 symbol__join_symfs(symfs_vmlinux, vmlinux);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001682
Cody P Schafer21ea4532012-08-10 15:22:56 -07001683 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Cody P Schafer005f9292012-08-10 15:22:58 -07001684 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
Cody P Schafer21ea4532012-08-10 15:22:56 -07001685 else
Cody P Schafer005f9292012-08-10 15:22:58 -07001686 symtab_type = DSO_BINARY_TYPE__VMLINUX;
Cody P Schafer21ea4532012-08-10 15:22:56 -07001687
Cody P Schafer005f9292012-08-10 15:22:58 -07001688 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001689 return -1;
1690
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001691 err = dso__load_sym(dso, map, &ss, &ss, 0);
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001692 symsrc__destroy(&ss);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001693
Cody P Schafer515850e2012-08-10 15:22:54 -07001694 if (err > 0) {
Adrian Hunter39b12f782013-08-07 14:38:47 +03001695 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001696 dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
Adrian Hunter39b12f782013-08-07 14:38:47 +03001697 else
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001698 dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -03001699 dso__set_long_name(dso, vmlinux, vmlinux_allocated);
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001700 dso__set_loaded(dso);
David Ahernec5761e2010-12-09 13:27:07 -07001701 pr_debug("Using %s for symbols\n", symfs_vmlinux);
Cody P Schafer515850e2012-08-10 15:22:54 -07001702 }
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001703
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001704 return err;
1705}
1706
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001707int dso__load_vmlinux_path(struct dso *dso, struct map *map)
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001708{
1709 int i, err = 0;
Namhyung Kim00dc8652014-11-04 10:14:32 +09001710 char *filename = NULL;
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001711
Arnaldo Carvalho de Melodc382182015-10-11 17:17:24 -03001712 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1713 vmlinux_path__nr_entries + 1);
1714
1715 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001716 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false);
Arnaldo Carvalho de Melodc382182015-10-11 17:17:24 -03001717 if (err > 0)
1718 goto out;
1719 }
1720
Namhyung Kim00dc8652014-11-04 10:14:32 +09001721 if (!symbol_conf.ignore_vmlinux_buildid)
Krister Johansend2396992017-07-05 18:48:13 -07001722 filename = dso__build_id_filename(dso, NULL, 0, false);
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001723 if (filename != NULL) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001724 err = dso__load_vmlinux(dso, map, filename, true);
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -03001725 if (err > 0)
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001726 goto out;
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001727 free(filename);
1728 }
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001729out:
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001730 return err;
1731}
1732
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001733static bool visible_dir_filter(const char *name, struct dirent *d)
1734{
1735 if (d->d_type != DT_DIR)
1736 return false;
1737 return lsdir_no_dot_filter(name, d);
1738}
1739
Adrian Hunter0544d422013-10-14 13:43:43 +03001740static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1741{
1742 char kallsyms_filename[PATH_MAX];
Adrian Hunter0544d422013-10-14 13:43:43 +03001743 int ret = -1;
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001744 struct strlist *dirs;
1745 struct str_node *nd;
Adrian Hunter0544d422013-10-14 13:43:43 +03001746
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001747 dirs = lsdir(dir, visible_dir_filter);
1748 if (!dirs)
Adrian Hunter0544d422013-10-14 13:43:43 +03001749 return -1;
1750
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03001751 strlist__for_each_entry(nd, dirs) {
Adrian Hunter0544d422013-10-14 13:43:43 +03001752 scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001753 "%s/%s/kallsyms", dir, nd->s);
Adrian Huntera00d28c2014-01-29 16:14:41 +02001754 if (!validate_kcore_addresses(kallsyms_filename, map)) {
Adrian Hunter0544d422013-10-14 13:43:43 +03001755 strlcpy(dir, kallsyms_filename, dir_sz);
1756 ret = 0;
1757 break;
1758 }
1759 }
1760
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001761 strlist__delete(dirs);
Adrian Hunter0544d422013-10-14 13:43:43 +03001762
1763 return ret;
1764}
1765
Masami Hiramatsu11870d72016-05-29 00:15:13 +09001766/*
1767 * Use open(O_RDONLY) to check readability directly instead of access(R_OK)
1768 * since access(R_OK) only checks with real UID/GID but open() use effective
1769 * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO).
1770 */
1771static bool filename__readable(const char *file)
1772{
1773 int fd = open(file, O_RDONLY);
1774 if (fd < 0)
1775 return false;
1776 close(fd);
1777 return true;
1778}
1779
Adrian Hunter0544d422013-10-14 13:43:43 +03001780static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1781{
1782 u8 host_build_id[BUILD_ID_SIZE];
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001783 char sbuild_id[SBUILD_ID_SIZE];
Adrian Hunter0544d422013-10-14 13:43:43 +03001784 bool is_host = false;
1785 char path[PATH_MAX];
1786
1787 if (!dso->has_build_id) {
1788 /*
1789 * Last resort, if we don't have a build-id and couldn't find
1790 * any vmlinux file, try the running kernel kallsyms table.
1791 */
1792 goto proc_kallsyms;
1793 }
1794
1795 if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1796 sizeof(host_build_id)) == 0)
1797 is_host = dso__build_id_equal(dso, host_build_id);
1798
Masami Hiramatsu4e4b6c02016-05-29 00:15:28 +09001799 /* Try a fast path for /proc/kallsyms if possible */
Adrian Hunter0544d422013-10-14 13:43:43 +03001800 if (is_host) {
Adrian Hunter0544d422013-10-14 13:43:43 +03001801 /*
Masami Hiramatsu11870d72016-05-29 00:15:13 +09001802 * Do not check the build-id cache, unless we know we cannot use
1803 * /proc/kcore or module maps don't match to /proc/kallsyms.
1804 * To check readability of /proc/kcore, do not use access(R_OK)
1805 * since /proc/kcore requires CAP_SYS_RAWIO to read and access
1806 * can't check it.
Adrian Hunter0544d422013-10-14 13:43:43 +03001807 */
Masami Hiramatsu11870d72016-05-29 00:15:13 +09001808 if (filename__readable("/proc/kcore") &&
1809 !validate_kcore_addresses("/proc/kallsyms", map))
1810 goto proc_kallsyms;
Adrian Hunter0544d422013-10-14 13:43:43 +03001811 }
1812
Masami Hiramatsu4e4b6c02016-05-29 00:15:28 +09001813 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1814
Adrian Hunter449867e2013-11-26 15:19:24 +02001815 /* Find kallsyms in build-id cache with kcore */
Masami Hiramatsu4e4b6c02016-05-29 00:15:28 +09001816 scnprintf(path, sizeof(path), "%s/%s/%s",
1817 buildid_dir, DSO__NAME_KCORE, sbuild_id);
1818
Adrian Hunter449867e2013-11-26 15:19:24 +02001819 if (!find_matching_kcore(map, path, sizeof(path)))
1820 return strdup(path);
1821
Masami Hiramatsu4e4b6c02016-05-29 00:15:28 +09001822 /* Use current /proc/kallsyms if possible */
1823 if (is_host) {
1824proc_kallsyms:
1825 return strdup("/proc/kallsyms");
1826 }
1827
1828 /* Finally, find a cache of kallsyms */
Masami Hiramatsu01412262016-05-29 00:15:37 +09001829 if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) {
Adrian Hunter0544d422013-10-14 13:43:43 +03001830 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
1831 sbuild_id);
1832 return NULL;
1833 }
1834
1835 return strdup(path);
Adrian Hunter0544d422013-10-14 13:43:43 +03001836}
1837
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001838static int dso__load_kernel_sym(struct dso *dso, struct map *map)
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001839{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001840 int err;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02001841 const char *kallsyms_filename = NULL;
1842 char *kallsyms_allocated_filename = NULL;
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001843 /*
David Ahernb226a5a72010-12-07 19:39:46 -07001844 * Step 1: if the user specified a kallsyms or vmlinux filename, use
1845 * it and only it, reporting errors to the user if it cannot be used.
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001846 *
1847 * For instance, try to analyse an ARM perf.data file _without_ a
1848 * build-id, or if the user specifies the wrong path to the right
1849 * vmlinux file, obviously we can't fallback to another vmlinux (a
1850 * x86_86 one, on the machine where analysis is being performed, say),
1851 * or worse, /proc/kallsyms.
1852 *
1853 * If the specified file _has_ a build-id and there is a build-id
1854 * section in the perf.data file, we will still do the expected
1855 * validation in dso__load_vmlinux and will bail out if they don't
1856 * match.
1857 */
David Ahernb226a5a72010-12-07 19:39:46 -07001858 if (symbol_conf.kallsyms_name != NULL) {
1859 kallsyms_filename = symbol_conf.kallsyms_name;
1860 goto do_kallsyms;
1861 }
1862
Willy Tarreaufc2be692013-09-14 10:32:59 +02001863 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001864 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, false);
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001865 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001866
Willy Tarreaufc2be692013-09-14 10:32:59 +02001867 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001868 err = dso__load_vmlinux_path(dso, map);
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001869 if (err > 0)
Adrian Hunter39b12f782013-08-07 14:38:47 +03001870 return err;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001871 }
1872
David Ahernec5761e2010-12-09 13:27:07 -07001873 /* do not try local files if a symfs was given */
1874 if (symbol_conf.symfs[0] != 0)
1875 return -1;
1876
Adrian Hunter0544d422013-10-14 13:43:43 +03001877 kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
1878 if (!kallsyms_allocated_filename)
1879 return -1;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02001880
Adrian Hunter0544d422013-10-14 13:43:43 +03001881 kallsyms_filename = kallsyms_allocated_filename;
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001882
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001883do_kallsyms:
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001884 err = dso__load_kallsyms(dso, kallsyms_filename, map);
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001885 if (err > 0)
1886 pr_debug("Using %s for symbols\n", kallsyms_filename);
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02001887 free(kallsyms_allocated_filename);
1888
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001889 if (err > 0 && !dso__is_kcore(dso)) {
Adrian Hunterbdac0bc2014-07-14 13:02:43 +03001890 dso->binary_type = DSO_BINARY_TYPE__KALLSYMS;
Masami Hiramatsu0a775822016-05-15 12:19:40 +09001891 dso__set_long_name(dso, DSO__NAME_KALLSYMS, false);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02001892 map__fixup_start(map);
1893 map__fixup_end(map);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001894 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001895
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03001896 return err;
1897}
1898
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001899static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001900{
1901 int err;
1902 const char *kallsyms_filename = NULL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001903 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001904 char path[PATH_MAX];
1905
1906 if (!map->groups) {
1907 pr_debug("Guest kernel map hasn't the point to groups\n");
1908 return -1;
1909 }
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001910 machine = map->groups->machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001911
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001912 if (machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001913 /*
1914 * if the user specified a vmlinux filename, use it and only
1915 * it, reporting errors to the user if it cannot be used.
1916 * Or use file guest_kallsyms inputted by user on commandline
1917 */
1918 if (symbol_conf.default_guest_vmlinux_name != NULL) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001919 err = dso__load_vmlinux(dso, map,
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -03001920 symbol_conf.default_guest_vmlinux_name,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001921 false);
Adrian Hunter39b12f782013-08-07 14:38:47 +03001922 return err;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001923 }
1924
1925 kallsyms_filename = symbol_conf.default_guest_kallsyms;
1926 if (!kallsyms_filename)
1927 return -1;
1928 } else {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001929 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001930 kallsyms_filename = path;
1931 }
1932
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001933 err = dso__load_kallsyms(dso, kallsyms_filename, map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001934 if (err > 0)
Adrian Hunter39b12f782013-08-07 14:38:47 +03001935 pr_debug("Using %s for symbols\n", kallsyms_filename);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001936 if (err > 0 && !dso__is_kcore(dso)) {
Adrian Hunterbdac0bc2014-07-14 13:02:43 +03001937 dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +01001938 dso__set_long_name(dso, machine->mmap_name, false);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001939 map__fixup_start(map);
1940 map__fixup_end(map);
1941 }
1942
1943 return err;
1944}
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02001945
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001946static void vmlinux_path__exit(void)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02001947{
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001948 while (--vmlinux_path__nr_entries >= 0)
1949 zfree(&vmlinux_path[vmlinux_path__nr_entries]);
Wang Nanc4f03542015-05-17 10:56:27 +00001950 vmlinux_path__nr_entries = 0;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001951
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001952 zfree(&vmlinux_path);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001953}
1954
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01001955static const char * const vmlinux_paths[] = {
1956 "vmlinux",
1957 "/boot/vmlinux"
1958};
1959
1960static const char * const vmlinux_paths_upd[] = {
1961 "/boot/vmlinux-%s",
1962 "/usr/lib/debug/boot/vmlinux-%s",
1963 "/lib/modules/%s/build/vmlinux",
Ekaterina Tumanovaf55ae952015-11-25 17:32:46 +01001964 "/usr/lib/debug/lib/modules/%s/vmlinux",
1965 "/usr/lib/debug/boot/vmlinux-%s.debug"
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01001966};
1967
1968static int vmlinux_path__add(const char *new_entry)
1969{
1970 vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry);
1971 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
1972 return -1;
1973 ++vmlinux_path__nr_entries;
1974
1975 return 0;
1976}
1977
Kan Liangce80d3b2015-08-28 05:48:04 -04001978static int vmlinux_path__init(struct perf_env *env)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001979{
1980 struct utsname uts;
1981 char bf[PATH_MAX];
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001982 char *kernel_version;
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01001983 unsigned int i;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001984
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01001985 vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
1986 ARRAY_SIZE(vmlinux_paths_upd)));
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02001987 if (vmlinux_path == NULL)
1988 return -1;
1989
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01001990 for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++)
1991 if (vmlinux_path__add(vmlinux_paths[i]) < 0)
1992 goto out_fail;
David Ahernec5761e2010-12-09 13:27:07 -07001993
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001994 /* only try kernel version if no symfs was given */
David Ahernec5761e2010-12-09 13:27:07 -07001995 if (symbol_conf.symfs[0] != 0)
1996 return 0;
1997
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09001998 if (env) {
1999 kernel_version = env->os_release;
2000 } else {
2001 if (uname(&uts) < 0)
2002 goto out_fail;
David Ahernec5761e2010-12-09 13:27:07 -07002003
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002004 kernel_version = uts.release;
2005 }
2006
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01002007 for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) {
2008 snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version);
2009 if (vmlinux_path__add(bf) < 0)
2010 goto out_fail;
2011 }
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002012
2013 return 0;
2014
2015out_fail:
2016 vmlinux_path__exit();
2017 return -1;
2018}
2019
David Ahern3bfe5f82013-11-18 13:32:48 -07002020int setup_list(struct strlist **list, const char *list_str,
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002021 const char *list_name)
2022{
2023 if (list_str == NULL)
2024 return 0;
2025
Arnaldo Carvalho de Melo4a77e212015-07-20 12:13:34 -03002026 *list = strlist__new(list_str, NULL);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002027 if (!*list) {
2028 pr_err("problems parsing %s list\n", list_name);
2029 return -1;
2030 }
Arnaldo Carvalho de Melo0bc2f2f2015-07-13 08:21:57 -03002031
2032 symbol_conf.has_filter = true;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002033 return 0;
2034}
2035
David Aherne03eaa42015-03-24 09:52:41 -06002036int setup_intlist(struct intlist **list, const char *list_str,
2037 const char *list_name)
2038{
2039 if (list_str == NULL)
2040 return 0;
2041
2042 *list = intlist__new(list_str);
2043 if (!*list) {
2044 pr_err("problems parsing %s list\n", list_name);
2045 return -1;
2046 }
2047 return 0;
2048}
2049
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002050static bool symbol__read_kptr_restrict(void)
2051{
2052 bool value = false;
Wang Nan38272dc2016-05-24 09:21:27 +00002053 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002054
Wang Nan38272dc2016-05-24 09:21:27 +00002055 if (fp != NULL) {
2056 char line[8];
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002057
Wang Nan38272dc2016-05-24 09:21:27 +00002058 if (fgets(line, sizeof(line), fp) != NULL)
Wang Nan3dbe46c2016-11-15 04:05:45 +00002059 value = ((geteuid() != 0) || (getuid() != 0)) ?
Wang Nan38272dc2016-05-24 09:21:27 +00002060 (atoi(line) != 0) :
2061 (atoi(line) == 2);
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002062
Wang Nan38272dc2016-05-24 09:21:27 +00002063 fclose(fp);
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002064 }
2065
2066 return value;
2067}
2068
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -03002069int symbol__annotation_init(void)
2070{
Arnaldo Carvalho de Melo7b366142018-04-12 14:58:24 -03002071 if (symbol_conf.init_annotation)
2072 return 0;
2073
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -03002074 if (symbol_conf.initialized) {
2075 pr_err("Annotation needs to be init before symbol__init()\n");
2076 return -1;
2077 }
2078
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -03002079 symbol_conf.priv_size += sizeof(struct annotation);
2080 symbol_conf.init_annotation = true;
2081 return 0;
2082}
2083
Kan Liangce80d3b2015-08-28 05:48:04 -04002084int symbol__init(struct perf_env *env)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002085{
David Ahernec5761e2010-12-09 13:27:07 -07002086 const char *symfs;
2087
Jovi Zhang85e00b52010-09-09 13:30:59 -03002088 if (symbol_conf.initialized)
2089 return 0;
2090
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002091 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
David S. Miller4d439512011-03-29 14:18:39 -03002092
Namhyung Kim166ccc92012-08-06 13:41:19 +09002093 symbol__elf_init();
2094
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02002095 if (symbol_conf.sort_by_name)
2096 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2097 sizeof(struct symbol));
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02002098
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002099 if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002100 return -1;
2101
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02002102 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2103 pr_err("'.' is the only non valid --field-separator argument\n");
2104 return -1;
2105 }
2106
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002107 if (setup_list(&symbol_conf.dso_list,
2108 symbol_conf.dso_list_str, "dso") < 0)
2109 return -1;
2110
2111 if (setup_list(&symbol_conf.comm_list,
2112 symbol_conf.comm_list_str, "comm") < 0)
2113 goto out_free_dso_list;
2114
David Aherne03eaa42015-03-24 09:52:41 -06002115 if (setup_intlist(&symbol_conf.pid_list,
2116 symbol_conf.pid_list_str, "pid") < 0)
2117 goto out_free_comm_list;
2118
2119 if (setup_intlist(&symbol_conf.tid_list,
2120 symbol_conf.tid_list_str, "tid") < 0)
2121 goto out_free_pid_list;
2122
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002123 if (setup_list(&symbol_conf.sym_list,
2124 symbol_conf.sym_list_str, "symbol") < 0)
David Aherne03eaa42015-03-24 09:52:41 -06002125 goto out_free_tid_list;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002126
David Ahern64eff7d2016-11-25 13:00:21 -07002127 if (setup_list(&symbol_conf.bt_stop_list,
2128 symbol_conf.bt_stop_list_str, "symbol") < 0)
2129 goto out_free_sym_list;
2130
David Ahernec5761e2010-12-09 13:27:07 -07002131 /*
2132 * A path to symbols of "/" is identical to ""
2133 * reset here for simplicity.
2134 */
2135 symfs = realpath(symbol_conf.symfs, NULL);
2136 if (symfs == NULL)
2137 symfs = symbol_conf.symfs;
2138 if (strcmp(symfs, "/") == 0)
2139 symbol_conf.symfs = "";
2140 if (symfs != symbol_conf.symfs)
2141 free((void *)symfs);
2142
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002143 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2144
Jovi Zhang85e00b52010-09-09 13:30:59 -03002145 symbol_conf.initialized = true;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002146 return 0;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002147
David Ahern64eff7d2016-11-25 13:00:21 -07002148out_free_sym_list:
2149 strlist__delete(symbol_conf.sym_list);
David Aherne03eaa42015-03-24 09:52:41 -06002150out_free_tid_list:
2151 intlist__delete(symbol_conf.tid_list);
2152out_free_pid_list:
2153 intlist__delete(symbol_conf.pid_list);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002154out_free_comm_list:
2155 strlist__delete(symbol_conf.comm_list);
Namhyung Kimd74c8962011-12-13 00:16:52 +09002156out_free_dso_list:
2157 strlist__delete(symbol_conf.dso_list);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002158 return -1;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002159}
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002160
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002161void symbol__exit(void)
2162{
Jovi Zhang85e00b52010-09-09 13:30:59 -03002163 if (!symbol_conf.initialized)
2164 return;
David Ahern64eff7d2016-11-25 13:00:21 -07002165 strlist__delete(symbol_conf.bt_stop_list);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002166 strlist__delete(symbol_conf.sym_list);
2167 strlist__delete(symbol_conf.dso_list);
2168 strlist__delete(symbol_conf.comm_list);
David Aherne03eaa42015-03-24 09:52:41 -06002169 intlist__delete(symbol_conf.tid_list);
2170 intlist__delete(symbol_conf.pid_list);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002171 vmlinux_path__exit();
2172 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
David Ahern64eff7d2016-11-25 13:00:21 -07002173 symbol_conf.bt_stop_list = NULL;
Jovi Zhang85e00b52010-09-09 13:30:59 -03002174 symbol_conf.initialized = false;
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002175}
He Kuanga7066702016-05-19 11:47:37 +00002176
2177int symbol__config_symfs(const struct option *opt __maybe_unused,
2178 const char *dir, int unset __maybe_unused)
2179{
2180 char *bf = NULL;
2181 int ret;
2182
2183 symbol_conf.symfs = strdup(dir);
2184 if (symbol_conf.symfs == NULL)
2185 return -ENOMEM;
2186
2187 /* skip the locally configured cache if a symfs is given, and
2188 * config buildid dir to symfs/.debug
2189 */
2190 ret = asprintf(&bf, "%s/%s", dir, ".debug");
2191 if (ret < 0)
2192 return -ENOMEM;
2193
2194 set_buildid_dir(bf);
2195
2196 free(bf);
2197 return 0;
2198}
Jiri Olsa9f874982018-03-07 16:50:06 +01002199
2200struct mem_info *mem_info__get(struct mem_info *mi)
2201{
2202 if (mi)
2203 refcount_inc(&mi->refcnt);
2204 return mi;
2205}
2206
2207void mem_info__put(struct mem_info *mi)
2208{
2209 if (mi && refcount_dec_and_test(&mi->refcnt))
2210 free(mi);
2211}
2212
2213struct mem_info *mem_info__new(void)
2214{
2215 struct mem_info *mi = zalloc(sizeof(*mi));
2216
2217 if (mi)
2218 refcount_set(&mi->refcnt, 1);
2219 return mi;
2220}