blob: 8f4300492dc7423db4b03888fcd5409570c568d4 [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>
Igor Lubashev8859aed2019-08-26 21:39:15 -04007#include <linux/capability.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -03008#include <linux/kernel.h>
Arnaldo Carvalho de Meloe9814df2018-04-26 16:11:47 -03009#include <linux/mman.h>
Arnaldo Carvalho de Melo8520a982019-08-29 16:18:59 -030010#include <linux/string.h>
Andi Kleen2a1292c2019-03-05 06:47:48 -080011#include <linux/time64.h>
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -030012#include <sys/types.h>
13#include <sys/stat.h>
14#include <sys/param.h>
15#include <fcntl.h>
16#include <unistd.h>
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -020017#include <inttypes.h>
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -030018#include "annotate.h"
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -030019#include "build-id.h"
Igor Lubashev8859aed2019-08-26 21:39:15 -040020#include "cap.h"
Arnaldo Carvalho de Melofac583f2019-08-30 09:43:25 -030021#include "dso.h"
Arnaldo Carvalho de Melofb71c86c2019-09-03 10:56:06 -030022#include "util.h" // lsdir()
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030023#include "debug.h"
Igor Lubashev8859aed2019-08-26 21:39:15 -040024#include "event.h"
Arnaldo Carvalho de Melo69d25912012-11-09 11:32:52 -030025#include "machine.h"
Arnaldo Carvalho de Melo1101f692019-01-27 13:42:37 +010026#include "map.h"
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030027#include "symbol.h"
Arnaldo Carvalho de Melod3300a32019-08-30 15:09:54 -030028#include "map_symbol.h"
29#include "mem-events.h"
Arnaldo Carvalho de Melob1d1b092019-08-30 10:26:37 -030030#include "symsrc.h"
Arnaldo Carvalho de Melo5aab6212010-03-25 19:59:00 -030031#include "strlist.h"
David Aherne03eaa42015-03-24 09:52:41 -060032#include "intlist.h"
Krister Johansen843ff372017-07-05 18:48:08 -070033#include "namespaces.h"
Namhyung Kim0a7e6d12014-08-12 15:40:45 +090034#include "header.h"
Arnaldo Carvalho de Melo9a3993d2017-04-18 11:33:48 -030035#include "path.h"
Arnaldo Carvalho de Melo3052ba52019-06-25 17:27:31 -030036#include <linux/ctype.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030037#include <linux/zalloc.h>
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030038
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -030039#include <elf.h>
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -020040#include <limits.h>
Arnaldo Carvalho de Meloc506c962013-12-11 09:15:00 -030041#include <symbol/kallsyms.h>
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030042#include <sys/utsname.h>
Peter Zijlstra2cdbc462009-08-05 14:05:16 +020043
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -030044static int dso__load_kernel_sym(struct dso *dso, struct map *map);
45static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map);
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -030046static bool symbol__is_idle(const char *name);
47
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -030048int vmlinux_path__nr_entries;
49char **vmlinux_path;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030050
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -020051struct symbol_conf symbol_conf = {
Andi Kleen52bab882019-03-05 06:47:47 -080052 .nanosecs = false,
Namhyung Kime511db52013-12-24 16:19:25 +090053 .use_modules = true,
54 .try_vmlinux_path = true,
Namhyung Kime511db52013-12-24 16:19:25 +090055 .demangle = true,
Avi Kivity763122a2014-09-13 07:15:05 +030056 .demangle_kernel = false,
Namhyung Kime511db52013-12-24 16:19:25 +090057 .cumulate_callchain = true,
Andi Kleen2a1292c2019-03-05 06:47:48 -080058 .time_quantum = 100 * NSEC_PER_MSEC, /* 100ms */
Jiri Olsac8302362014-06-27 18:26:58 +020059 .show_hist_headers = true,
Namhyung Kime511db52013-12-24 16:19:25 +090060 .symfs = "",
Namhyung Kim1e9abf82015-11-29 23:24:17 +090061 .event_group = true,
Milian Wolffd8a88dd2017-10-19 13:38:36 +020062 .inline_name = true,
Andi Kleen4968ac82019-03-11 07:44:58 -070063 .res_sample = 0,
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -020064};
65
Jiri Olsa44f24cb2012-07-22 14:14:32 +020066static enum dso_binary_type binary_type_symtab[] = {
67 DSO_BINARY_TYPE__KALLSYMS,
68 DSO_BINARY_TYPE__GUEST_KALLSYMS,
69 DSO_BINARY_TYPE__JAVA_JIT,
70 DSO_BINARY_TYPE__DEBUGLINK,
71 DSO_BINARY_TYPE__BUILD_ID_CACHE,
Krister Johansend2396992017-07-05 18:48:13 -070072 DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO,
Jiri Olsa44f24cb2012-07-22 14:14:32 +020073 DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
74 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
75 DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
76 DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
77 DSO_BINARY_TYPE__GUEST_KMODULE,
Namhyung Kimc00c48f2014-11-04 10:14:27 +090078 DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
Jiri Olsa44f24cb2012-07-22 14:14:32 +020079 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
Namhyung Kimc00c48f2014-11-04 10:14:27 +090080 DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
Ricardo Ribalda Delgado9cd00942013-09-18 15:56:14 +020081 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
Jiri Olsa44f24cb2012-07-22 14:14:32 +020082 DSO_BINARY_TYPE__NOT_FOUND,
83};
84
Jiri Olsa028df762012-08-01 14:47:57 +020085#define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
Jiri Olsa44f24cb2012-07-22 14:14:32 +020086
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -030087static bool symbol_type__filter(char symbol_type)
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020088{
Anton Blanchard31877902011-08-24 16:40:16 +100089 symbol_type = toupper(symbol_type);
Arnaldo Carvalho de Melo2be732c2018-06-05 17:06:57 -030090 return symbol_type == 'T' || symbol_type == 'W' || symbol_type == 'D' || symbol_type == 'B';
Arnaldo Carvalho de Melo6893d4e2009-12-11 14:50:37 -020091}
92
Anton Blanchard694bf402011-08-24 16:40:17 +100093static int prefix_underscores_count(const char *str)
94{
95 const char *tail = str;
96
97 while (*tail == '_')
98 tail++;
99
100 return tail - str;
101}
102
Thomas Richterb9c0a642019-07-24 14:27:03 +0200103void __weak arch__symbols__fixup_end(struct symbol *p, struct symbol *c)
104{
105 p->end = c->start;
106}
107
Masami Hiramatsu4b3a2712017-12-09 01:28:12 +0900108const char * __weak arch__normalize_symbol_name(const char *name)
109{
110 return name;
111}
112
Paul Clarked80406452017-04-25 13:15:49 -0500113int __weak arch__compare_symbol_names(const char *namea, const char *nameb)
114{
115 return strcmp(namea, nameb);
116}
117
118int __weak arch__compare_symbol_names_n(const char *namea, const char *nameb,
119 unsigned int n)
120{
121 return strncmp(namea, nameb, n);
122}
123
Naveen N. Raofb6d5942015-04-28 17:35:36 +0530124int __weak arch__choose_best_symbol(struct symbol *syma,
125 struct symbol *symb __maybe_unused)
126{
127 /* Avoid "SyS" kernel syscall aliases */
128 if (strlen(syma->name) >= 3 && !strncmp(syma->name, "SyS", 3))
129 return SYMBOL_B;
130 if (strlen(syma->name) >= 10 && !strncmp(syma->name, "compat_SyS", 10))
131 return SYMBOL_B;
132
133 return SYMBOL_A;
134}
Anton Blanchard694bf402011-08-24 16:40:17 +1000135
136static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
137{
138 s64 a;
139 s64 b;
Adrian Hunter34454322013-08-07 14:38:49 +0300140 size_t na, nb;
Anton Blanchard694bf402011-08-24 16:40:17 +1000141
142 /* Prefer a symbol with non zero length */
143 a = syma->end - syma->start;
144 b = symb->end - symb->start;
145 if ((b == 0) && (a > 0))
146 return SYMBOL_A;
147 else if ((a == 0) && (b > 0))
148 return SYMBOL_B;
149
150 /* Prefer a non weak symbol over a weak one */
151 a = syma->binding == STB_WEAK;
152 b = symb->binding == STB_WEAK;
153 if (b && !a)
154 return SYMBOL_A;
155 if (a && !b)
156 return SYMBOL_B;
157
158 /* Prefer a global symbol over a non global one */
159 a = syma->binding == STB_GLOBAL;
160 b = symb->binding == STB_GLOBAL;
161 if (a && !b)
162 return SYMBOL_A;
163 if (b && !a)
164 return SYMBOL_B;
165
166 /* Prefer a symbol with less underscores */
167 a = prefix_underscores_count(syma->name);
168 b = prefix_underscores_count(symb->name);
169 if (b > a)
170 return SYMBOL_A;
171 else if (a > b)
172 return SYMBOL_B;
173
Adrian Hunter34454322013-08-07 14:38:49 +0300174 /* Choose the symbol with the longest name */
175 na = strlen(syma->name);
176 nb = strlen(symb->name);
177 if (na > nb)
Anton Blanchard694bf402011-08-24 16:40:17 +1000178 return SYMBOL_A;
Adrian Hunter34454322013-08-07 14:38:49 +0300179 else if (na < nb)
Anton Blanchard694bf402011-08-24 16:40:17 +1000180 return SYMBOL_B;
Adrian Hunter34454322013-08-07 14:38:49 +0300181
Naveen N. Raofb6d5942015-04-28 17:35:36 +0530182 return arch__choose_best_symbol(syma, symb);
Anton Blanchard694bf402011-08-24 16:40:17 +1000183}
184
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800185void symbols__fixup_duplicate(struct rb_root_cached *symbols)
Anton Blanchard694bf402011-08-24 16:40:17 +1000186{
187 struct rb_node *nd;
188 struct symbol *curr, *next;
189
Arnaldo Carvalho de Meloc97b40e2016-09-01 10:56:06 -0300190 if (symbol_conf.allow_aliases)
191 return;
192
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800193 nd = rb_first_cached(symbols);
Anton Blanchard694bf402011-08-24 16:40:17 +1000194
195 while (nd) {
196 curr = rb_entry(nd, struct symbol, rb_node);
197again:
198 nd = rb_next(&curr->rb_node);
199 next = rb_entry(nd, struct symbol, rb_node);
200
201 if (!nd)
202 break;
203
204 if (curr->start != next->start)
205 continue;
206
207 if (choose_best_symbol(curr, next) == SYMBOL_A) {
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800208 rb_erase_cached(&next->rb_node, symbols);
Chenggang Qind4f74eb2013-10-11 08:27:59 +0800209 symbol__delete(next);
Anton Blanchard694bf402011-08-24 16:40:17 +1000210 goto again;
211 } else {
212 nd = rb_next(&curr->rb_node);
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800213 rb_erase_cached(&curr->rb_node, symbols);
Chenggang Qind4f74eb2013-10-11 08:27:59 +0800214 symbol__delete(curr);
Anton Blanchard694bf402011-08-24 16:40:17 +1000215 }
216 }
217}
218
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800219void symbols__fixup_end(struct rb_root_cached *symbols)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300220{
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800221 struct rb_node *nd, *prevnd = rb_first_cached(symbols);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300222 struct symbol *curr, *prev;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300223
224 if (prevnd == NULL)
225 return;
226
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300227 curr = rb_entry(prevnd, struct symbol, rb_node);
228
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300229 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300230 prev = curr;
231 curr = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300232
Arnaldo Carvalho de Melo3b01a412010-12-22 01:08:36 -0200233 if (prev->end == prev->start && prev->end != curr->start)
Thomas Richterb9c0a642019-07-24 14:27:03 +0200234 arch__symbols__fixup_end(prev, curr);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300235 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300236
237 /* Last entry */
238 if (curr->end == curr->start)
Daniel Borkmanne7ede722017-03-15 22:53:37 +0100239 curr->end = roundup(curr->start, 4096) + 4096;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300240}
241
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -0300242void maps__fixup_end(struct maps *maps)
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300243{
Arnaldo Carvalho de Melo8efc4f02019-10-28 11:31:38 -0300244 struct map *prev = NULL, *curr;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300245
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300246 down_write(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300247
Arnaldo Carvalho de Melo8efc4f02019-10-28 11:31:38 -0300248 maps__for_each_entry(maps, curr) {
249 if (prev != NULL && !prev->end)
250 prev->end = curr->start;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300251
Arnaldo Carvalho de Melo8efc4f02019-10-28 11:31:38 -0300252 prev = curr;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300253 }
Arnaldo Carvalho de Melo90c83212009-11-21 14:31:24 -0200254
255 /*
256 * We still haven't the actual symbols, so guess the
257 * last map final address.
258 */
Arnaldo Carvalho de Melo8efc4f02019-10-28 11:31:38 -0300259 if (curr && !curr->end)
Thomas Richter9ad46522017-08-03 15:49:02 +0200260 curr->end = ~0ULL;
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -0300261
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -0300262 up_write(&maps->lock);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300263}
264
Arnaldo Carvalho de Meloaf30bff2018-04-26 11:09:10 -0300265struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300266{
Arnaldo Carvalho de Melo0085c9542009-05-28 14:55:13 -0300267 size_t namelen = strlen(name) + 1;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300268 struct symbol *sym = calloc(1, (symbol_conf.priv_size +
269 sizeof(*sym) + namelen));
270 if (sym == NULL)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200271 return NULL;
272
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -0300273 if (symbol_conf.priv_size) {
274 if (symbol_conf.init_annotation) {
275 struct annotation *notes = (void *)sym;
276 pthread_mutex_init(&notes->lock, NULL);
277 }
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300278 sym = ((void *)sym) + symbol_conf.priv_size;
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -0300279 }
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200280
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300281 sym->start = start;
Arnaldo Carvalho de Melo2c241bd2014-10-14 17:19:44 -0300282 sym->end = len ? start + len : start;
Arnaldo Carvalho de Meloaf30bff2018-04-26 11:09:10 -0300283 sym->type = type;
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300284 sym->binding = binding;
285 sym->namelen = namelen - 1;
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200286
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300287 pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
288 __func__, name, start, sym->end);
289 memcpy(sym->name, name, namelen);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200290
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300291 return sym;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300292}
293
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300294void symbol__delete(struct symbol *sym)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300295{
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300296 free(((void *)sym) - symbol_conf.priv_size);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300297}
298
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800299void symbols__delete(struct rb_root_cached *symbols)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300300{
301 struct symbol *pos;
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800302 struct rb_node *next = rb_first_cached(symbols);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300303
304 while (next) {
305 pos = rb_entry(next, struct symbol, rb_node);
306 next = rb_next(&pos->rb_node);
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800307 rb_erase_cached(&pos->rb_node, symbols);
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200308 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300309 }
310}
311
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800312void __symbols__insert(struct rb_root_cached *symbols,
313 struct symbol *sym, bool kernel)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300314{
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800315 struct rb_node **p = &symbols->rb_root.rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300316 struct rb_node *parent = NULL;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000317 const u64 ip = sym->start;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300318 struct symbol *s;
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800319 bool leftmost = true;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300320
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300321 if (kernel) {
322 const char *name = sym->name;
323 /*
324 * ppc64 uses function descriptors and appends a '.' to the
325 * start of every instruction address. Remove it.
326 */
327 if (name[0] == '.')
328 name++;
329 sym->idle = symbol__is_idle(name);
330 }
331
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300332 while (*p != NULL) {
333 parent = *p;
334 s = rb_entry(parent, struct symbol, rb_node);
335 if (ip < s->start)
336 p = &(*p)->rb_left;
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800337 else {
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300338 p = &(*p)->rb_right;
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800339 leftmost = false;
340 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300341 }
342 rb_link_node(&sym->rb_node, parent, p);
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800343 rb_insert_color_cached(&sym->rb_node, symbols, leftmost);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300344}
345
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800346void symbols__insert(struct rb_root_cached *symbols, struct symbol *sym)
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300347{
348 __symbols__insert(symbols, sym, false);
349}
350
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800351static struct symbol *symbols__find(struct rb_root_cached *symbols, u64 ip)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300352{
353 struct rb_node *n;
354
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300355 if (symbols == NULL)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300356 return NULL;
357
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800358 n = symbols->rb_root.rb_node;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300359
360 while (n) {
361 struct symbol *s = rb_entry(n, struct symbol, rb_node);
362
363 if (ip < s->start)
364 n = n->rb_left;
Chris Phlipot9c7b37c2016-05-07 02:16:59 -0700365 else if (ip > s->end || (ip == s->end && ip != s->start))
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300366 n = n->rb_right;
367 else
368 return s;
369 }
370
371 return NULL;
372}
373
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800374static struct symbol *symbols__first(struct rb_root_cached *symbols)
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300375{
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800376 struct rb_node *n = rb_first_cached(symbols);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300377
378 if (n)
379 return rb_entry(n, struct symbol, rb_node);
380
381 return NULL;
382}
383
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800384static struct symbol *symbols__last(struct rb_root_cached *symbols)
Adrian Huntercd67f992016-09-23 17:38:38 +0300385{
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800386 struct rb_node *n = rb_last(&symbols->rb_root);
Adrian Huntercd67f992016-09-23 17:38:38 +0300387
388 if (n)
389 return rb_entry(n, struct symbol, rb_node);
390
391 return NULL;
392}
393
Adrian Hunter9c00a812014-07-14 13:02:50 +0300394static struct symbol *symbols__next(struct symbol *sym)
395{
396 struct rb_node *n = rb_next(&sym->rb_node);
397
398 if (n)
399 return rb_entry(n, struct symbol, rb_node);
400
401 return NULL;
402}
403
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800404static void symbols__insert_by_name(struct rb_root_cached *symbols, struct symbol *sym)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200405{
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800406 struct rb_node **p = &symbols->rb_root.rb_node;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200407 struct rb_node *parent = NULL;
Rabin Vincent02a9d032010-11-23 22:08:18 +0530408 struct symbol_name_rb_node *symn, *s;
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800409 bool leftmost = true;
Rabin Vincent02a9d032010-11-23 22:08:18 +0530410
411 symn = container_of(sym, struct symbol_name_rb_node, sym);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200412
413 while (*p != NULL) {
414 parent = *p;
415 s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
416 if (strcmp(sym->name, s->sym.name) < 0)
417 p = &(*p)->rb_left;
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800418 else {
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200419 p = &(*p)->rb_right;
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800420 leftmost = false;
421 }
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200422 }
423 rb_link_node(&symn->rb_node, parent, p);
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800424 rb_insert_color_cached(&symn->rb_node, symbols, leftmost);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200425}
426
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800427static void symbols__sort_by_name(struct rb_root_cached *symbols,
428 struct rb_root_cached *source)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200429{
430 struct rb_node *nd;
431
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800432 for (nd = rb_first_cached(source); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200433 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300434 symbols__insert_by_name(symbols, pos);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200435 }
436}
437
Paul Clarked80406452017-04-25 13:15:49 -0500438int symbol__match_symbol_name(const char *name, const char *str,
439 enum symbol_tag_include includes)
440{
441 const char *versioning;
442
443 if (includes == SYMBOL_TAG_INCLUDE__DEFAULT_ONLY &&
444 (versioning = strstr(name, "@@"))) {
445 int len = strlen(str);
446
447 if (len < versioning - name)
448 len = versioning - name;
449
450 return arch__compare_symbol_names_n(name, str, len);
451 } else
452 return arch__compare_symbol_names(name, str);
453}
454
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800455static struct symbol *symbols__find_by_name(struct rb_root_cached *symbols,
Paul Clarked80406452017-04-25 13:15:49 -0500456 const char *name,
457 enum symbol_tag_include includes)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200458{
459 struct rb_node *n;
Martin Liška5bcaaca2015-05-26 11:41:37 -0300460 struct symbol_name_rb_node *s = NULL;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200461
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300462 if (symbols == NULL)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200463 return NULL;
464
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800465 n = symbols->rb_root.rb_node;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200466
467 while (n) {
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200468 int cmp;
469
470 s = rb_entry(n, struct symbol_name_rb_node, rb_node);
Paul Clarked80406452017-04-25 13:15:49 -0500471 cmp = symbol__match_symbol_name(s->sym.name, name, includes);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200472
Paul Clarked80406452017-04-25 13:15:49 -0500473 if (cmp > 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200474 n = n->rb_left;
Paul Clarked80406452017-04-25 13:15:49 -0500475 else if (cmp < 0)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200476 n = n->rb_right;
477 else
Namhyung Kimde480992015-01-16 15:31:28 -0300478 break;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200479 }
480
Namhyung Kimde480992015-01-16 15:31:28 -0300481 if (n == NULL)
482 return NULL;
483
Paul Clarked80406452017-04-25 13:15:49 -0500484 if (includes != SYMBOL_TAG_INCLUDE__DEFAULT_ONLY)
485 /* return first symbol that has same name (if any) */
486 for (n = rb_prev(n); n; n = rb_prev(n)) {
487 struct symbol_name_rb_node *tmp;
Namhyung Kimde480992015-01-16 15:31:28 -0300488
Paul Clarked80406452017-04-25 13:15:49 -0500489 tmp = rb_entry(n, struct symbol_name_rb_node, rb_node);
490 if (arch__compare_symbol_names(tmp->sym.name, s->sym.name))
491 break;
Namhyung Kimde480992015-01-16 15:31:28 -0300492
Paul Clarked80406452017-04-25 13:15:49 -0500493 s = tmp;
494 }
Namhyung Kimde480992015-01-16 15:31:28 -0300495
496 return &s->sym;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200497}
498
Arnaldo Carvalho de Meloc0b4dff2015-08-24 13:33:14 -0300499void dso__reset_find_symbol_cache(struct dso *dso)
500{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300501 dso->last_find_result.addr = 0;
502 dso->last_find_result.symbol = NULL;
Arnaldo Carvalho de Meloc0b4dff2015-08-24 13:33:14 -0300503}
504
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300505void dso__insert_symbol(struct dso *dso, struct symbol *sym)
Chris Phlipotae93a6c2016-05-10 20:26:46 -0700506{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300507 __symbols__insert(&dso->symbols, sym, dso->kernel);
Chris Phlipotae93a6c2016-05-10 20:26:46 -0700508
509 /* update the symbol cache if necessary */
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300510 if (dso->last_find_result.addr >= sym->start &&
511 (dso->last_find_result.addr < sym->end ||
Chris Phlipotae93a6c2016-05-10 20:26:46 -0700512 sym->start == sym->end)) {
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300513 dso->last_find_result.symbol = sym;
Chris Phlipotae93a6c2016-05-10 20:26:46 -0700514 }
515}
516
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300517struct symbol *dso__find_symbol(struct dso *dso, u64 addr)
Arnaldo Carvalho de Melofcf12032009-11-24 13:01:52 -0200518{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300519 if (dso->last_find_result.addr != addr || dso->last_find_result.symbol == NULL) {
520 dso->last_find_result.addr = addr;
521 dso->last_find_result.symbol = symbols__find(&dso->symbols, addr);
Arnaldo Carvalho de Melob685ac22015-07-22 12:52:17 -0300522 }
523
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300524 return dso->last_find_result.symbol;
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300525}
526
Arnaldo Carvalho de Melo5cf88a62018-04-25 17:01:46 -0300527struct symbol *dso__first_symbol(struct dso *dso)
528{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300529 return symbols__first(&dso->symbols);
Adrian Huntercd67f992016-09-23 17:38:38 +0300530}
531
Arnaldo Carvalho de Melo5cf88a62018-04-25 17:01:46 -0300532struct symbol *dso__last_symbol(struct dso *dso)
533{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300534 return symbols__last(&dso->symbols);
Arnaldo Carvalho de Melo5cf88a62018-04-25 17:01:46 -0300535}
536
Adrian Hunter9c00a812014-07-14 13:02:50 +0300537struct symbol *dso__next_symbol(struct symbol *sym)
538{
539 return symbols__next(sym);
540}
541
Arnaldo Carvalho de Melo18bd7262015-01-16 15:39:53 -0300542struct symbol *symbol__next_by_name(struct symbol *sym)
543{
544 struct symbol_name_rb_node *s = container_of(sym, struct symbol_name_rb_node, sym);
545 struct rb_node *n = rb_next(&s->rb_node);
546
547 return n ? &rb_entry(n, struct symbol_name_rb_node, rb_node)->sym : NULL;
548}
549
550 /*
Arnaldo Carvalho de Meloaf07eeb2018-04-25 17:46:28 -0300551 * Returns first symbol that matched with @name.
Arnaldo Carvalho de Melo18bd7262015-01-16 15:39:53 -0300552 */
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300553struct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200554{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300555 struct symbol *s = symbols__find_by_name(&dso->symbol_names, name,
Paul Clarked80406452017-04-25 13:15:49 -0500556 SYMBOL_TAG_INCLUDE__NONE);
557 if (!s)
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300558 s = symbols__find_by_name(&dso->symbol_names, name,
Paul Clarked80406452017-04-25 13:15:49 -0500559 SYMBOL_TAG_INCLUDE__DEFAULT_ONLY);
560 return s;
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200561}
562
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300563void dso__sort_by_name(struct dso *dso)
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200564{
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300565 dso__set_sorted_by_name(dso);
566 return symbols__sort_by_name(&dso->symbol_names, &dso->symbols);
Arnaldo Carvalho de Melo79406cd2009-12-11 18:50:22 -0200567}
568
Adrian Hunter316d70d2013-10-08 11:45:48 +0300569int modules__parse(const char *filename, void *arg,
570 int (*process_module)(void *arg, const char *name,
Thomas Richter9ad46522017-08-03 15:49:02 +0200571 u64 start, u64 size))
Adrian Hunter316d70d2013-10-08 11:45:48 +0300572{
573 char *line = NULL;
574 size_t n;
575 FILE *file;
576 int err = 0;
577
578 file = fopen(filename, "r");
579 if (file == NULL)
580 return -1;
581
582 while (1) {
583 char name[PATH_MAX];
Thomas Richter9ad46522017-08-03 15:49:02 +0200584 u64 start, size;
585 char *sep, *endptr;
Adrian Hunter316d70d2013-10-08 11:45:48 +0300586 ssize_t line_len;
587
588 line_len = getline(&line, &n, file);
589 if (line_len < 0) {
590 if (feof(file))
591 break;
592 err = -1;
593 goto out;
594 }
595
596 if (!line) {
597 err = -1;
598 goto out;
599 }
600
601 line[--line_len] = '\0'; /* \n */
602
603 sep = strrchr(line, 'x');
604 if (sep == NULL)
605 continue;
606
607 hex2u64(sep + 1, &start);
608
609 sep = strchr(line, ' ');
610 if (sep == NULL)
611 continue;
612
613 *sep = '\0';
614
615 scnprintf(name, sizeof(name), "[%s]", line);
616
Thomas Richter9ad46522017-08-03 15:49:02 +0200617 size = strtoul(sep + 1, &endptr, 0);
618 if (*endptr != ' ' && *endptr != '\t')
619 continue;
620
621 err = process_module(arg, name, start, size);
Adrian Hunter316d70d2013-10-08 11:45:48 +0300622 if (err)
623 break;
624 }
625out:
626 free(line);
627 fclose(file);
628 return err;
629}
630
Arnaldo Carvalho de Meloe7110b92014-08-08 18:00:39 -0300631/*
632 * These are symbols in the kernel image, so make sure that
633 * sym is from a kernel DSO.
634 */
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300635static bool symbol__is_idle(const char *name)
David Ahern82d1deb2013-11-18 13:32:45 -0700636{
637 const char * const idle_symbols[] = {
Kim Phillips0e714592020-02-07 17:06:12 -0600638 "acpi_idle_do_entry",
639 "acpi_processor_ffh_cstate_enter",
Arnaldo Carvalho de Melo549aff72019-01-09 16:19:24 -0300640 "arch_cpu_idle",
David Ahern82d1deb2013-11-18 13:32:45 -0700641 "cpu_idle",
Arnaldo Carvalho de Meloe0336ed2014-08-08 18:02:41 -0300642 "cpu_startup_entry",
Kim Phillips0e714592020-02-07 17:06:12 -0600643 "idle_cpu",
David Ahern82d1deb2013-11-18 13:32:45 -0700644 "intel_idle",
645 "default_idle",
646 "native_safe_halt",
647 "enter_idle",
648 "exit_idle",
649 "mwait_idle",
650 "mwait_idle_with_hints",
651 "poll_idle",
652 "ppc64_runlatch_off",
653 "pseries_dedicated_idle_sleep",
654 NULL
655 };
David Ahern82d1deb2013-11-18 13:32:45 -0700656 int i;
Kim Phillipsbc5f15b2020-02-10 10:31:47 -0600657 static struct strlist *idle_symbols_list;
David Ahern82d1deb2013-11-18 13:32:45 -0700658
Kim Phillipsbc5f15b2020-02-10 10:31:47 -0600659 if (idle_symbols_list)
660 return strlist__has_entry(idle_symbols_list, name);
David Ahern82d1deb2013-11-18 13:32:45 -0700661
Kim Phillipsbc5f15b2020-02-10 10:31:47 -0600662 idle_symbols_list = strlist__new(NULL, NULL);
663
664 for (i = 0; idle_symbols[i]; i++)
665 strlist__add(idle_symbols_list, idle_symbols[i]);
666
667 return strlist__has_entry(idle_symbols_list, name);
David Ahern82d1deb2013-11-18 13:32:45 -0700668}
669
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200670static int map__process_kallsym_symbol(void *arg, const char *name,
Cody P Schafer82151522012-08-10 15:22:48 -0700671 char type, u64 start)
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200672{
673 struct symbol *sym;
Arnaldo Carvalho de Melo333cc762018-04-27 15:36:15 -0300674 struct dso *dso = arg;
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800675 struct rb_root_cached *root = &dso->symbols;
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200676
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300677 if (!symbol_type__filter(type))
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200678 return 0;
679
Cody P Schafer82151522012-08-10 15:22:48 -0700680 /*
681 * module symbols are not sorted so we add all
682 * symbols, setting length to 0, and rely on
683 * symbols__fixup_end() to fix it up.
684 */
Arnaldo Carvalho de Meloaf30bff2018-04-26 11:09:10 -0300685 sym = symbol__new(start, 0, kallsyms2elf_binding(type), kallsyms2elf_type(type), name);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200686 if (sym == NULL)
687 return -ENOMEM;
688 /*
689 * We will pass the symbols to the filter later, in
690 * map__split_kallsyms, when we have split the maps per module
691 */
Arnaldo Carvalho de Melo608c34d2016-09-01 17:54:31 -0300692 __symbols__insert(root, sym, !strchr(name, '['));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800693
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200694 return 0;
695}
696
697/*
698 * Loads the function entries in /proc/kallsyms into kernel_map->dso,
699 * so that we can in the next step set the symbol ->end address and then
700 * call kernel_maps__split_kallsyms.
701 */
Arnaldo Carvalho de Melo333cc762018-04-27 15:36:15 -0300702static int dso__load_all_kallsyms(struct dso *dso, const char *filename)
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200703{
Arnaldo Carvalho de Melo333cc762018-04-27 15:36:15 -0300704 return kallsyms__parse(filename, dso, map__process_kallsym_symbol);
Arnaldo Carvalho de Melo682b3352010-01-04 16:19:26 -0200705}
706
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300707static int maps__split_kallsyms_for_kcore(struct maps *kmaps, struct dso *dso)
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300708{
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300709 struct map *curr_map;
710 struct symbol *pos;
Adrian Hunter866548d2015-11-06 15:59:29 +0200711 int count = 0;
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800712 struct rb_root_cached old_root = dso->symbols;
713 struct rb_root_cached *root = &dso->symbols;
714 struct rb_node *next = rb_first_cached(root);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300715
Wang Nanba927322015-04-07 08:22:45 +0000716 if (!kmaps)
717 return -1;
718
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800719 *root = RB_ROOT_CACHED;
Adrian Hunter866548d2015-11-06 15:59:29 +0200720
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300721 while (next) {
722 char *module;
723
724 pos = rb_entry(next, struct symbol, rb_node);
725 next = rb_next(&pos->rb_node);
726
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800727 rb_erase_cached(&pos->rb_node, &old_root);
728 RB_CLEAR_NODE(&pos->rb_node);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300729 module = strchr(pos->name, '\t');
730 if (module)
731 *module = '\0';
732
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300733 curr_map = maps__find(kmaps, pos->start);
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300734
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300735 if (!curr_map) {
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300736 symbol__delete(pos);
Adrian Hunter866548d2015-11-06 15:59:29 +0200737 continue;
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300738 }
Adrian Hunter866548d2015-11-06 15:59:29 +0200739
740 pos->start -= curr_map->start - curr_map->pgoff;
Adrian Hunterd6d45742019-01-09 11:18:30 +0200741 if (pos->end > curr_map->end)
742 pos->end = curr_map->end;
Adrian Hunter866548d2015-11-06 15:59:29 +0200743 if (pos->end)
744 pos->end -= curr_map->start - curr_map->pgoff;
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300745 symbols__insert(&curr_map->dso->symbols, pos);
Adrian Hunter866548d2015-11-06 15:59:29 +0200746 ++count;
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300747 }
748
749 /* Symbols have been adjusted */
750 dso->adjust_symbols = 1;
751
Adrian Hunter866548d2015-11-06 15:59:29 +0200752 return count;
Adrian Hunter8e0cf962013-08-07 14:38:51 +0300753}
754
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300755/*
756 * Split the symbols into maps, making sure there are no overlaps, i.e. the
757 * kernel range is broken in several maps, named [kernel].N, as we don't have
758 * the original ELF section names vmlinux have.
759 */
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300760static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta,
761 struct map *initial_map)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300762{
Wang Nanba927322015-04-07 08:22:45 +0000763 struct machine *machine;
Arnaldo Carvalho de Melo15e0e2d2018-04-27 15:59:32 -0300764 struct map *curr_map = initial_map;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300765 struct symbol *pos;
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300766 int count = 0, moved = 0;
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800767 struct rb_root_cached *root = &dso->symbols;
768 struct rb_node *next = rb_first_cached(root);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300769 int kernel_range = 0;
Adrian Hunter4d004362018-05-22 13:54:34 +0300770 bool x86_64;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300771
Wang Nanba927322015-04-07 08:22:45 +0000772 if (!kmaps)
773 return -1;
774
775 machine = kmaps->machine;
776
Adrian Hunter4d004362018-05-22 13:54:34 +0300777 x86_64 = machine__is(machine, "x86_64");
778
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300779 while (next) {
780 char *module;
781
782 pos = rb_entry(next, struct symbol, rb_node);
783 next = rb_next(&pos->rb_node);
784
785 module = strchr(pos->name, '\t');
786 if (module) {
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200787 if (!symbol_conf.use_modules)
Arnaldo Carvalho de Melo1de8e242009-11-27 16:29:21 -0200788 goto discard_symbol;
789
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300790 *module++ = '\0';
791
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200792 if (strcmp(curr_map->dso->short_name, module)) {
Arnaldo Carvalho de Melo15e0e2d2018-04-27 15:59:32 -0300793 if (curr_map != initial_map &&
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300794 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300795 machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800796 /*
797 * We assume all symbols of a module are
798 * continuous in * kallsyms, so curr_map
799 * points to a module and all its
800 * symbols are in its kmap. Mark it as
801 * loaded.
802 */
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300803 dso__set_loaded(curr_map->dso);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300804 }
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200805
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300806 curr_map = maps__find_by_name(kmaps, module);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800807 if (curr_map == NULL) {
Arnaldo Carvalho de Melo2f519032010-05-17 17:57:59 -0300808 pr_debug("%s/proc/{kallsyms,modules} "
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800809 "inconsistency while looking "
810 "for \"%s\" module!\n",
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300811 machine->root_dir, module);
Arnaldo Carvalho de Melo15e0e2d2018-04-27 15:59:32 -0300812 curr_map = initial_map;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800813 goto discard_symbol;
814 }
815
816 if (curr_map->dso->loaded &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300817 !machine__is_default_guest(machine))
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -0200818 goto discard_symbol;
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -0300819 }
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300820 /*
821 * So that we look just like we get from .ko files,
Arnaldo Carvalho de Melo15e0e2d2018-04-27 15:59:32 -0300822 * i.e. not prelinked, relative to initial_map->start.
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300823 */
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200824 pos->start = curr_map->map_ip(curr_map, pos->start);
825 pos->end = curr_map->map_ip(curr_map, pos->end);
Adrian Hunter4d004362018-05-22 13:54:34 +0300826 } else if (x86_64 && is_entry_trampoline(pos->name)) {
827 /*
828 * These symbols are not needed anymore since the
829 * trampoline maps refer to the text section and it's
830 * symbols instead. Avoid having to deal with
831 * relocations, and the assumption that the first symbol
832 * is the start of kernel text, by simply removing the
833 * symbols at this point.
834 */
835 goto discard_symbol;
Arnaldo Carvalho de Melo15e0e2d2018-04-27 15:59:32 -0300836 } else if (curr_map != initial_map) {
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300837 char dso_name[PATH_MAX];
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300838 struct dso *ndso;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300839
Adrian Hunterd9b62ab2014-01-29 16:14:43 +0200840 if (delta) {
841 /* Kernel was relocated at boot time */
842 pos->start -= delta;
843 pos->end -= delta;
844 }
845
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200846 if (count == 0) {
Arnaldo Carvalho de Melo15e0e2d2018-04-27 15:59:32 -0300847 curr_map = initial_map;
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300848 goto add_symbol;
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200849 }
850
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300851 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800852 snprintf(dso_name, sizeof(dso_name),
853 "[guest.kernel].%d",
854 kernel_range++);
855 else
856 snprintf(dso_name, sizeof(dso_name),
857 "[kernel].%d",
858 kernel_range++);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300859
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300860 ndso = dso__new(dso_name);
861 if (ndso == NULL)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300862 return -1;
863
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300864 ndso->kernel = dso->kernel;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800865
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300866 curr_map = map__new2(pos->start, ndso);
Zhang, Yanmin37fe5fc2010-02-25 11:00:51 +0800867 if (curr_map == NULL) {
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -0300868 dso__put(ndso);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300869 return -1;
870 }
871
Arnaldo Carvalho de Melo4e062552009-11-27 16:29:18 -0200872 curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300873 maps__insert(kmaps, curr_map);
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300874 ++kernel_range;
Adrian Hunterd9b62ab2014-01-29 16:14:43 +0200875 } else if (delta) {
876 /* Kernel was relocated at boot time */
877 pos->start -= delta;
878 pos->end -= delta;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -0300879 }
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300880add_symbol:
Arnaldo Carvalho de Melo15e0e2d2018-04-27 15:59:32 -0300881 if (curr_map != initial_map) {
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800882 rb_erase_cached(&pos->rb_node, root);
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300883 symbols__insert(&curr_map->dso->symbols, pos);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300884 ++moved;
885 } else
886 ++count;
887
888 continue;
889discard_symbol:
Davidlohr Bueso7137ff52018-12-06 11:18:17 -0800890 rb_erase_cached(&pos->rb_node, root);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300891 symbol__delete(pos);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300892 }
893
Arnaldo Carvalho de Melo15e0e2d2018-04-27 15:59:32 -0300894 if (curr_map != initial_map &&
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -0300895 dso->kernel == DSO_TYPE_GUEST_KERNEL &&
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300896 machine__is_default_guest(kmaps->machine)) {
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -0300897 dso__set_loaded(curr_map->dso);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800898 }
899
Arnaldo Carvalho de Melo8a953312010-11-29 12:44:15 -0200900 return count + moved;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -0300901}
902
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300903bool symbol__restricted_filename(const char *filename,
904 const char *restricted_filename)
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -0300905{
906 bool restricted = false;
907
908 if (symbol_conf.kptr_restrict) {
909 char *r = realpath(filename, NULL);
910
911 if (r != NULL) {
912 restricted = strcmp(r, restricted_filename) == 0;
913 free(r);
914 return restricted;
915 }
916 }
917
918 return restricted;
919}
920
Adrian Hunter52afdaf2013-10-09 15:01:11 +0300921struct module_info {
922 struct rb_node rb_node;
923 char *name;
924 u64 start;
925};
926
927static void add_module(struct module_info *mi, struct rb_root *modules)
928{
929 struct rb_node **p = &modules->rb_node;
930 struct rb_node *parent = NULL;
931 struct module_info *m;
932
933 while (*p != NULL) {
934 parent = *p;
935 m = rb_entry(parent, struct module_info, rb_node);
936 if (strcmp(mi->name, m->name) < 0)
937 p = &(*p)->rb_left;
938 else
939 p = &(*p)->rb_right;
940 }
941 rb_link_node(&mi->rb_node, parent, p);
942 rb_insert_color(&mi->rb_node, modules);
943}
944
945static void delete_modules(struct rb_root *modules)
946{
947 struct module_info *mi;
948 struct rb_node *next = rb_first(modules);
949
950 while (next) {
951 mi = rb_entry(next, struct module_info, rb_node);
952 next = rb_next(&mi->rb_node);
953 rb_erase(&mi->rb_node, modules);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300954 zfree(&mi->name);
Adrian Hunter52afdaf2013-10-09 15:01:11 +0300955 free(mi);
956 }
957}
958
959static struct module_info *find_module(const char *name,
960 struct rb_root *modules)
961{
962 struct rb_node *n = modules->rb_node;
963
964 while (n) {
965 struct module_info *m;
966 int cmp;
967
968 m = rb_entry(n, struct module_info, rb_node);
969 cmp = strcmp(name, m->name);
970 if (cmp < 0)
971 n = n->rb_left;
972 else if (cmp > 0)
973 n = n->rb_right;
974 else
975 return m;
976 }
977
978 return NULL;
979}
980
Thomas Richter9ad46522017-08-03 15:49:02 +0200981static int __read_proc_modules(void *arg, const char *name, u64 start,
982 u64 size __maybe_unused)
Adrian Hunter52afdaf2013-10-09 15:01:11 +0300983{
984 struct rb_root *modules = arg;
985 struct module_info *mi;
986
987 mi = zalloc(sizeof(struct module_info));
988 if (!mi)
989 return -ENOMEM;
990
991 mi->name = strdup(name);
992 mi->start = start;
993
994 if (!mi->name) {
995 free(mi);
996 return -ENOMEM;
997 }
998
999 add_module(mi, modules);
1000
1001 return 0;
1002}
1003
1004static int read_proc_modules(const char *filename, struct rb_root *modules)
1005{
1006 if (symbol__restricted_filename(filename, "/proc/modules"))
1007 return -1;
1008
1009 if (modules__parse(filename, modules, __read_proc_modules)) {
1010 delete_modules(modules);
1011 return -1;
1012 }
1013
1014 return 0;
1015}
1016
Adrian Hunterfc1b6912013-10-14 16:57:29 +03001017int compare_proc_modules(const char *from, const char *to)
1018{
1019 struct rb_root from_modules = RB_ROOT;
1020 struct rb_root to_modules = RB_ROOT;
1021 struct rb_node *from_node, *to_node;
1022 struct module_info *from_m, *to_m;
1023 int ret = -1;
1024
1025 if (read_proc_modules(from, &from_modules))
1026 return -1;
1027
1028 if (read_proc_modules(to, &to_modules))
1029 goto out_delete_from;
1030
1031 from_node = rb_first(&from_modules);
1032 to_node = rb_first(&to_modules);
1033 while (from_node) {
1034 if (!to_node)
1035 break;
1036
1037 from_m = rb_entry(from_node, struct module_info, rb_node);
1038 to_m = rb_entry(to_node, struct module_info, rb_node);
1039
1040 if (from_m->start != to_m->start ||
1041 strcmp(from_m->name, to_m->name))
1042 break;
1043
1044 from_node = rb_next(from_node);
1045 to_node = rb_next(to_node);
1046 }
1047
1048 if (!from_node && !to_node)
1049 ret = 0;
1050
1051 delete_modules(&to_modules);
1052out_delete_from:
1053 delete_modules(&from_modules);
1054
1055 return ret;
1056}
1057
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001058static int do_validate_kcore_modules(const char *filename, struct maps *kmaps)
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001059{
1060 struct rb_root modules = RB_ROOT;
1061 struct map *old_map;
1062 int err;
1063
1064 err = read_proc_modules(filename, &modules);
1065 if (err)
1066 return err;
1067
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001068 maps__for_each_entry(kmaps, old_map) {
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001069 struct module_info *mi;
1070
Adrian Hunter5759a682018-05-22 13:54:35 +03001071 if (!__map__is_kmodule(old_map)) {
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001072 continue;
1073 }
1074
1075 /* Module must be in memory at the same address */
1076 mi = find_module(old_map->dso->short_name, &modules);
1077 if (!mi || mi->start != old_map->start) {
1078 err = -EINVAL;
1079 goto out;
1080 }
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001081 }
1082out:
1083 delete_modules(&modules);
1084 return err;
1085}
1086
1087/*
1088 * If kallsyms is referenced by name then we look for filename in the same
1089 * directory.
1090 */
1091static bool filename_from_kallsyms_filename(char *filename,
1092 const char *base_name,
1093 const char *kallsyms_filename)
1094{
1095 char *name;
1096
1097 strcpy(filename, kallsyms_filename);
1098 name = strrchr(filename, '/');
1099 if (!name)
1100 return false;
1101
1102 name += 1;
1103
1104 if (!strcmp(name, "kallsyms")) {
1105 strcpy(name, base_name);
1106 return true;
1107 }
1108
1109 return false;
1110}
1111
1112static int validate_kcore_modules(const char *kallsyms_filename,
1113 struct map *map)
1114{
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001115 struct maps *kmaps = map__kmaps(map);
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001116 char modules_filename[PATH_MAX];
1117
Wang Nanba927322015-04-07 08:22:45 +00001118 if (!kmaps)
1119 return -EINVAL;
1120
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001121 if (!filename_from_kallsyms_filename(modules_filename, "modules",
1122 kallsyms_filename))
1123 return -EINVAL;
1124
Adrian Hunter5759a682018-05-22 13:54:35 +03001125 if (do_validate_kcore_modules(modules_filename, kmaps))
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001126 return -EINVAL;
1127
1128 return 0;
1129}
1130
Adrian Huntera00d28c2014-01-29 16:14:41 +02001131static int validate_kcore_addresses(const char *kallsyms_filename,
1132 struct map *map)
1133{
1134 struct kmap *kmap = map__kmap(map);
1135
Wang Nanba927322015-04-07 08:22:45 +00001136 if (!kmap)
1137 return -EINVAL;
1138
Adrian Huntera00d28c2014-01-29 16:14:41 +02001139 if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
1140 u64 start;
1141
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001142 if (kallsyms__get_function_start(kallsyms_filename,
1143 kmap->ref_reloc_sym->name, &start))
1144 return -ENOENT;
Adrian Huntera00d28c2014-01-29 16:14:41 +02001145 if (start != kmap->ref_reloc_sym->addr)
1146 return -EINVAL;
1147 }
1148
1149 return validate_kcore_modules(kallsyms_filename, map);
1150}
1151
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001152struct kcore_mapfn_data {
1153 struct dso *dso;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001154 struct list_head maps;
1155};
1156
1157static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
1158{
1159 struct kcore_mapfn_data *md = data;
1160 struct map *map;
1161
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001162 map = map__new2(start, md->dso);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001163 if (map == NULL)
1164 return -ENOMEM;
1165
1166 map->end = map->start + len;
1167 map->pgoff = pgoff;
1168
1169 list_add(&map->node, &md->maps);
1170
1171 return 0;
1172}
1173
Jiri Olsafb5a88d2019-05-08 15:20:06 +02001174/*
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001175 * Merges map into maps by splitting the new map within the existing map
1176 * regions.
Jiri Olsafb5a88d2019-05-08 15:20:06 +02001177 */
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001178int maps__merge_in(struct maps *kmaps, struct map *new_map)
Jiri Olsafb5a88d2019-05-08 15:20:06 +02001179{
1180 struct map *old_map;
1181 LIST_HEAD(merged);
1182
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001183 maps__for_each_entry(kmaps, old_map) {
Jiri Olsafb5a88d2019-05-08 15:20:06 +02001184 /* no overload with this one */
1185 if (new_map->end < old_map->start ||
1186 new_map->start >= old_map->end)
1187 continue;
1188
1189 if (new_map->start < old_map->start) {
1190 /*
1191 * |new......
1192 * |old....
1193 */
1194 if (new_map->end < old_map->end) {
1195 /*
1196 * |new......| -> |new..|
1197 * |old....| -> |old....|
1198 */
1199 new_map->end = old_map->start;
1200 } else {
1201 /*
1202 * |new.............| -> |new..| |new..|
1203 * |old....| -> |old....|
1204 */
1205 struct map *m = map__clone(new_map);
1206
1207 if (!m)
1208 return -ENOMEM;
1209
1210 m->end = old_map->start;
1211 list_add_tail(&m->node, &merged);
1212 new_map->start = old_map->end;
1213 }
1214 } else {
1215 /*
1216 * |new......
1217 * |old....
1218 */
1219 if (new_map->end < old_map->end) {
1220 /*
1221 * |new..| -> x
1222 * |old.........| -> |old.........|
1223 */
1224 map__put(new_map);
1225 new_map = NULL;
1226 break;
1227 } else {
1228 /*
1229 * |new......| -> |new...|
1230 * |old....| -> |old....|
1231 */
1232 new_map->start = old_map->end;
1233 }
1234 }
1235 }
1236
1237 while (!list_empty(&merged)) {
1238 old_map = list_entry(merged.next, struct map, node);
1239 list_del_init(&old_map->node);
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001240 maps__insert(kmaps, old_map);
Jiri Olsafb5a88d2019-05-08 15:20:06 +02001241 map__put(old_map);
1242 }
1243
1244 if (new_map) {
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001245 maps__insert(kmaps, new_map);
Jiri Olsafb5a88d2019-05-08 15:20:06 +02001246 map__put(new_map);
1247 }
1248 return 0;
1249}
1250
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001251static int dso__load_kcore(struct dso *dso, struct map *map,
1252 const char *kallsyms_filename)
1253{
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001254 struct maps *kmaps = map__kmaps(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001255 struct kcore_mapfn_data md;
Arnaldo Carvalho de Melo50481462019-10-28 11:55:28 -03001256 struct map *old_map, *new_map, *replacement_map = NULL, *next;
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001257 struct machine *machine;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001258 bool is_64_bit;
1259 int err, fd;
1260 char kcore_filename[PATH_MAX];
Adrian Hunter565499782018-05-09 14:43:34 +03001261 u64 stext;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001262
Wang Nanba927322015-04-07 08:22:45 +00001263 if (!kmaps)
1264 return -EINVAL;
1265
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001266 machine = kmaps->machine;
1267
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001268 /* This function requires that the map is the kernel map */
Arnaldo Carvalho de Meloefdd5c62018-04-23 16:43:47 -03001269 if (!__map__is_kernel(map))
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001270 return -EINVAL;
1271
Adrian Hunter52afdaf2013-10-09 15:01:11 +03001272 if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
1273 kallsyms_filename))
1274 return -EINVAL;
1275
Adrian Huntera00d28c2014-01-29 16:14:41 +02001276 /* Modules and kernel must be present at their original addresses */
1277 if (validate_kcore_addresses(kallsyms_filename, map))
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001278 return -EINVAL;
1279
1280 md.dso = dso;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001281 INIT_LIST_HEAD(&md.maps);
1282
1283 fd = open(kcore_filename, O_RDONLY);
Li Zhang36c8bb52015-06-19 16:57:33 +08001284 if (fd < 0) {
Adrian Hunter133de942015-08-20 13:07:40 +03001285 pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n",
1286 kcore_filename);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001287 return -EINVAL;
Li Zhang36c8bb52015-06-19 16:57:33 +08001288 }
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001289
1290 /* Read new maps into temporary lists */
Arnaldo Carvalho de Meloe9814df2018-04-26 16:11:47 -03001291 err = file__read_maps(fd, map->prot & PROT_EXEC, kcore_mapfn, &md,
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001292 &is_64_bit);
1293 if (err)
1294 goto out_err;
Adrian Hunterc6d8f2a2014-07-14 13:02:41 +03001295 dso->is_64_bit = is_64_bit;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001296
1297 if (list_empty(&md.maps)) {
1298 err = -EINVAL;
1299 goto out_err;
1300 }
1301
1302 /* Remove old maps */
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001303 maps__for_each_entry_safe(kmaps, old_map, next) {
Jiri Olsafb5a88d2019-05-08 15:20:06 +02001304 /*
1305 * We need to preserve eBPF maps even if they are
1306 * covered by kcore, because we need to access
1307 * eBPF dso for source data.
1308 */
1309 if (old_map != map && !__map__is_bpf_prog(old_map))
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001310 maps__remove(kmaps, old_map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001311 }
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001312 machine->trampolines_mapped = false;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001313
Adrian Hunter565499782018-05-09 14:43:34 +03001314 /* Find the kernel map using the '_stext' symbol */
1315 if (!kallsyms__get_function_start(kallsyms_filename, "_stext", &stext)) {
1316 list_for_each_entry(new_map, &md.maps, node) {
1317 if (stext >= new_map->start && stext < new_map->end) {
1318 replacement_map = new_map;
1319 break;
1320 }
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001321 }
1322 }
1323
1324 if (!replacement_map)
1325 replacement_map = list_entry(md.maps.next, struct map, node);
1326
1327 /* Add new maps */
1328 while (!list_empty(&md.maps)) {
1329 new_map = list_entry(md.maps.next, struct map, node);
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -03001330 list_del_init(&new_map->node);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001331 if (new_map == replacement_map) {
1332 map->start = new_map->start;
1333 map->end = new_map->end;
1334 map->pgoff = new_map->pgoff;
1335 map->map_ip = new_map->map_ip;
1336 map->unmap_ip = new_map->unmap_ip;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001337 /* Ensure maps are correctly ordered */
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001338 map__get(map);
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001339 maps__remove(kmaps, map);
1340 maps__insert(kmaps, map);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001341 map__put(map);
Jiri Olsafb5a88d2019-05-08 15:20:06 +02001342 map__put(new_map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001343 } else {
Jiri Olsafb5a88d2019-05-08 15:20:06 +02001344 /*
1345 * Merge kcore map into existing maps,
1346 * and ensure that current maps (eBPF)
1347 * stay intact.
1348 */
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001349 if (maps__merge_in(kmaps, new_map))
Jiri Olsafb5a88d2019-05-08 15:20:06 +02001350 goto out_err;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001351 }
1352 }
1353
Adrian Hunter1c5aae72018-05-22 13:54:36 +03001354 if (machine__is(machine, "x86_64")) {
1355 u64 addr;
1356
1357 /*
1358 * If one of the corresponding symbols is there, assume the
1359 * entry trampoline maps are too.
1360 */
1361 if (!kallsyms__get_function_start(kallsyms_filename,
1362 ENTRY_TRAMPOLINE_NAME,
1363 &addr))
1364 machine->trampolines_mapped = true;
1365 }
1366
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001367 /*
1368 * Set the data type and long name so that kcore can be read via
1369 * dso__data_read_addr().
1370 */
1371 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001372 dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001373 else
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001374 dso->binary_type = DSO_BINARY_TYPE__KCORE;
Arnaldo Carvalho de Melo7e155d42013-12-10 15:08:44 -03001375 dso__set_long_name(dso, strdup(kcore_filename), true);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001376
1377 close(fd);
1378
Arnaldo Carvalho de Meloe9814df2018-04-26 16:11:47 -03001379 if (map->prot & PROT_EXEC)
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001380 pr_debug("Using %s for kernel object code\n", kcore_filename);
1381 else
1382 pr_debug("Using %s for kernel data\n", kcore_filename);
1383
1384 return 0;
1385
1386out_err:
1387 while (!list_empty(&md.maps)) {
1388 map = list_entry(md.maps.next, struct map, node);
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -03001389 list_del_init(&map->node);
Arnaldo Carvalho de Melo84c2caf2015-05-25 16:59:56 -03001390 map__put(map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001391 }
1392 close(fd);
1393 return -EINVAL;
1394}
1395
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001396/*
1397 * If the kernel is relocated at boot time, kallsyms won't match. Compute the
1398 * delta based on the relocation reference symbol.
1399 */
Arnaldo Carvalho de Melo019c6822018-04-27 15:47:13 -03001400static int kallsyms__delta(struct kmap *kmap, const char *filename, u64 *delta)
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001401{
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001402 u64 addr;
1403
1404 if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
1405 return 0;
1406
Arnaldo Carvalho de Melob843f622017-04-27 21:21:09 -03001407 if (kallsyms__get_function_start(filename, kmap->ref_reloc_sym->name, &addr))
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001408 return -1;
1409
1410 *delta = addr - kmap->ref_reloc_sym->addr;
1411 return 0;
1412}
1413
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001414int __dso__load_kallsyms(struct dso *dso, const char *filename,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001415 struct map *map, bool no_kcore)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001416{
Arnaldo Carvalho de Melo019c6822018-04-27 15:47:13 -03001417 struct kmap *kmap = map__kmap(map);
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001418 u64 delta = 0;
1419
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03001420 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1421 return -1;
1422
Arnaldo Carvalho de Melo019c6822018-04-27 15:47:13 -03001423 if (!kmap || !kmap->kmaps)
1424 return -1;
1425
Arnaldo Carvalho de Melo333cc762018-04-27 15:36:15 -03001426 if (dso__load_all_kallsyms(dso, filename) < 0)
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001427 return -1;
1428
Arnaldo Carvalho de Melo019c6822018-04-27 15:47:13 -03001429 if (kallsyms__delta(kmap, filename, &delta))
Adrian Hunterd9b62ab2014-01-29 16:14:43 +02001430 return -1;
1431
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001432 symbols__fixup_end(&dso->symbols);
1433 symbols__fixup_duplicate(&dso->symbols);
Anton Blanchard3f5a4272011-08-24 16:40:15 +10001434
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001435 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001436 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001437 else
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001438 dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
Arnaldo Carvalho de Melo2e538c42009-10-07 13:48:56 -03001439
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001440 if (!no_kcore && !dso__load_kcore(dso, map, filename))
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001441 return maps__split_kallsyms_for_kcore(kmap->kmaps, dso);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03001442 else
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -03001443 return maps__split_kallsyms(kmap->kmaps, dso, delta, map);
Arnaldo Carvalho de Meloaf427bf2009-10-05 14:26:17 -03001444}
1445
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001446int dso__load_kallsyms(struct dso *dso, const char *filename,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001447 struct map *map)
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001448{
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001449 return __dso__load_kallsyms(dso, filename, map, false);
Arnaldo Carvalho de Meloe02092b2016-04-19 12:12:49 -03001450}
1451
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001452static int dso__load_perf_map(const char *map_path, struct dso *dso)
Pekka Enberg80d496b2009-06-08 21:12:48 +03001453{
1454 char *line = NULL;
1455 size_t n;
1456 FILE *file;
1457 int nr_syms = 0;
1458
Krister Johansenbf2e7102017-07-05 18:48:09 -07001459 file = fopen(map_path, "r");
Pekka Enberg80d496b2009-06-08 21:12:48 +03001460 if (file == NULL)
1461 goto out_failure;
1462
1463 while (!feof(file)) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +10001464 u64 start, size;
Pekka Enberg80d496b2009-06-08 21:12:48 +03001465 struct symbol *sym;
1466 int line_len, len;
1467
1468 line_len = getline(&line, &n, file);
1469 if (line_len < 0)
1470 break;
1471
1472 if (!line)
1473 goto out_failure;
1474
1475 line[--line_len] = '\0'; /* \n */
1476
1477 len = hex2u64(line, &start);
1478
1479 len++;
1480 if (len + 2 >= line_len)
1481 continue;
1482
1483 len += hex2u64(line + len, &size);
1484
1485 len++;
1486 if (len + 2 >= line_len)
1487 continue;
1488
Arnaldo Carvalho de Meloaf30bff2018-04-26 11:09:10 -03001489 sym = symbol__new(start, size, STB_GLOBAL, STT_FUNC, line + len);
Pekka Enberg80d496b2009-06-08 21:12:48 +03001490
1491 if (sym == NULL)
1492 goto out_delete_line;
1493
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001494 symbols__insert(&dso->symbols, sym);
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001495 nr_syms++;
Pekka Enberg80d496b2009-06-08 21:12:48 +03001496 }
1497
1498 free(line);
1499 fclose(file);
1500
1501 return nr_syms;
1502
1503out_delete_line:
1504 free(line);
1505out_failure:
1506 return -1;
1507}
1508
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001509static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
1510 enum dso_binary_type type)
1511{
1512 switch (type) {
1513 case DSO_BINARY_TYPE__JAVA_JIT:
1514 case DSO_BINARY_TYPE__DEBUGLINK:
1515 case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
1516 case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
1517 case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1518 case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
1519 case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
1520 return !kmod && dso->kernel == DSO_TYPE_USER;
1521
1522 case DSO_BINARY_TYPE__KALLSYMS:
1523 case DSO_BINARY_TYPE__VMLINUX:
1524 case DSO_BINARY_TYPE__KCORE:
1525 return dso->kernel == DSO_TYPE_KERNEL;
1526
1527 case DSO_BINARY_TYPE__GUEST_KALLSYMS:
1528 case DSO_BINARY_TYPE__GUEST_VMLINUX:
1529 case DSO_BINARY_TYPE__GUEST_KCORE:
1530 return dso->kernel == DSO_TYPE_GUEST_KERNEL;
1531
1532 case DSO_BINARY_TYPE__GUEST_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +09001533 case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001534 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
Namhyung Kimc00c48f2014-11-04 10:14:27 +09001535 case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001536 /*
1537 * kernel modules know their symtab type - it's set when
Arnaldo Carvalho de Meloa94ab912019-11-14 12:28:41 -03001538 * creating a module dso in machine__addnew_module_map().
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001539 */
1540 return kmod && dso->symtab_type == type;
1541
1542 case DSO_BINARY_TYPE__BUILD_ID_CACHE:
Krister Johansend2396992017-07-05 18:48:13 -07001543 case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001544 return true;
1545
Song Liu9b86d042019-03-11 22:30:48 -07001546 case DSO_BINARY_TYPE__BPF_PROG_INFO:
Jiri Olsa3c29d442020-03-12 20:56:10 +01001547 case DSO_BINARY_TYPE__BPF_IMAGE:
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001548 case DSO_BINARY_TYPE__NOT_FOUND:
1549 default:
1550 return false;
1551 }
1552}
1553
Krister Johansenbf2e7102017-07-05 18:48:09 -07001554/* Checks for the existence of the perf-<pid>.map file in two different
1555 * locations. First, if the process is a separate mount namespace, check in
1556 * that namespace using the pid of the innermost pid namespace. If's not in a
1557 * namespace, or the file can't be found there, try in the mount namespace of
1558 * the tracing process using our view of its pid.
1559 */
1560static int dso__find_perf_map(char *filebuf, size_t bufsz,
1561 struct nsinfo **nsip)
1562{
1563 struct nscookie nsc;
1564 struct nsinfo *nsi;
1565 struct nsinfo *nnsi;
1566 int rc = -1;
1567
1568 nsi = *nsip;
1569
1570 if (nsi->need_setns) {
1571 snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsi->nstgid);
1572 nsinfo__mountns_enter(nsi, &nsc);
1573 rc = access(filebuf, R_OK);
1574 nsinfo__mountns_exit(&nsc);
1575 if (rc == 0)
1576 return rc;
1577 }
1578
1579 nnsi = nsinfo__copy(nsi);
1580 if (nnsi) {
1581 nsinfo__put(nsi);
1582
1583 nnsi->need_setns = false;
1584 snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nnsi->tgid);
1585 *nsip = nnsi;
1586 rc = 0;
1587 }
1588
1589 return rc;
1590}
1591
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001592int dso__load(struct dso *dso, struct map *map)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001593{
Arnaldo Carvalho de Meloc338aee2009-11-20 20:51:27 -02001594 char *name;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001595 int ret = -1;
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001596 u_int i;
Arnaldo Carvalho de Melo93fcce92019-11-04 16:25:11 -03001597 struct machine *machine = NULL;
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001598 char *root_dir = (char *) "";
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001599 int ss_pos = 0;
1600 struct symsrc ss_[2];
1601 struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001602 bool kmod;
Krister Johansenbf2e7102017-07-05 18:48:09 -07001603 bool perfmap;
Dima Kogan5baecbc2015-09-07 17:34:19 -07001604 unsigned char build_id[BUILD_ID_SIZE];
Krister Johansen843ff372017-07-05 18:48:08 -07001605 struct nscookie nsc;
Krister Johansenbf2e7102017-07-05 18:48:09 -07001606 char newmapname[PATH_MAX];
1607 const char *map_path = dso->long_name;
1608
1609 perfmap = strncmp(dso->name, "/tmp/perf-", 10) == 0;
1610 if (perfmap) {
1611 if (dso->nsinfo && (dso__find_perf_map(newmapname,
1612 sizeof(newmapname), &dso->nsinfo) == 0)) {
1613 map_path = newmapname;
1614 }
1615 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001616
Krister Johansen843ff372017-07-05 18:48:08 -07001617 nsinfo__mountns_enter(dso->nsinfo, &nsc);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001618 pthread_mutex_lock(&dso->lock);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -02001619
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001620 /* check again under the dso->lock */
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001621 if (dso__loaded(dso)) {
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001622 ret = 1;
1623 goto out;
1624 }
1625
Arnaldo Carvalho de Melob5c09512020-03-02 16:03:34 -03001626 kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1627 dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
1628 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE ||
1629 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
1630
1631 if (dso->kernel && !kmod) {
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001632 if (dso->kernel == DSO_TYPE_KERNEL)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001633 ret = dso__load_kernel_sym(dso, map);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001634 else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001635 ret = dso__load_guest_kernel_sym(dso, map);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001636
Arnaldo Carvalho de Melo93fcce92019-11-04 16:25:11 -03001637 machine = map__kmaps(map)->machine;
Adrian Hunter4d99e412018-05-22 13:54:33 +03001638 if (machine__is(machine, "x86_64"))
1639 machine__map_x86_64_entry_trampolines(machine, dso);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001640 goto out;
1641 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001642
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001643 dso->adjust_symbols = 0;
Arnaldo Carvalho de Melof5812a72009-06-30 11:43:17 -03001644
Krister Johansenbf2e7102017-07-05 18:48:09 -07001645 if (perfmap) {
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001646 ret = dso__load_perf_map(map_path, dso);
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001647 dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
1648 DSO_BINARY_TYPE__NOT_FOUND;
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001649 goto out;
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03001650 }
1651
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001652 if (machine)
1653 root_dir = machine->root_dir;
1654
David Ahern164c8002013-01-14 10:46:47 -07001655 name = malloc(PATH_MAX);
1656 if (!name)
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001657 goto out;
David Ahern164c8002013-01-14 10:46:47 -07001658
Dima Kogan5baecbc2015-09-07 17:34:19 -07001659 /*
1660 * Read the build id if possible. This is required for
1661 * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work
1662 */
Kan Lianged6c1662016-12-13 10:29:44 -05001663 if (!dso->has_build_id &&
Victor Kamensky9b200652017-02-06 15:48:28 -08001664 is_regular_file(dso->long_name)) {
1665 __symbol__join_symfs(name, PATH_MAX, dso->long_name);
1666 if (filename__read_build_id(name, build_id, BUILD_ID_SIZE) > 0)
Dima Kogan5baecbc2015-09-07 17:34:19 -07001667 dso__set_build_id(dso, build_id);
Victor Kamensky9b200652017-02-06 15:48:28 -08001668 }
Dima Kogan5baecbc2015-09-07 17:34:19 -07001669
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001670 /*
1671 * Iterate over candidate debug images.
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001672 * Keep track of "interesting" ones (those which have a symtab, dynsym,
1673 * and/or opd section) for processing.
Dave Martin6da80ce2010-07-30 09:50:09 -03001674 */
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001675 for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001676 struct symsrc *ss = &ss_[ss_pos];
1677 bool next_slot = false;
Krister Johansenf045b8c2017-07-05 18:48:11 -07001678 bool is_reg;
Krister Johansend2396992017-07-05 18:48:13 -07001679 bool nsexit;
Jiri Olsac3962962018-02-15 13:26:28 +01001680 int sirc = -1;
Dave Martin6da80ce2010-07-30 09:50:09 -03001681
Cody P Schafer005f9292012-08-10 15:22:58 -07001682 enum dso_binary_type symtab_type = binary_type_symtab[i];
Dave Martin6da80ce2010-07-30 09:50:09 -03001683
Krister Johansend2396992017-07-05 18:48:13 -07001684 nsexit = (symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE ||
1685 symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO);
1686
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001687 if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type))
1688 continue;
1689
Arnaldo Carvalho de Meloee4e9622013-12-16 17:03:18 -03001690 if (dso__read_binary_type_filename(dso, symtab_type,
1691 root_dir, name, PATH_MAX))
Jiri Olsa44f24cb2012-07-22 14:14:32 +02001692 continue;
Dave Martin6da80ce2010-07-30 09:50:09 -03001693
Krister Johansend2396992017-07-05 18:48:13 -07001694 if (nsexit)
Krister Johansenf045b8c2017-07-05 18:48:11 -07001695 nsinfo__mountns_exit(&nsc);
Jiri Olsa40356722016-01-20 12:56:32 +01001696
Krister Johansenf045b8c2017-07-05 18:48:11 -07001697 is_reg = is_regular_file(name);
Jiri Olsac3962962018-02-15 13:26:28 +01001698 if (is_reg)
1699 sirc = symsrc__init(ss, dso, name, symtab_type);
Krister Johansenf045b8c2017-07-05 18:48:11 -07001700
Krister Johansend2396992017-07-05 18:48:13 -07001701 if (nsexit)
Krister Johansenf045b8c2017-07-05 18:48:11 -07001702 nsinfo__mountns_enter(dso->nsinfo, &nsc);
1703
Jiri Olsac3962962018-02-15 13:26:28 +01001704 if (!is_reg || sirc < 0)
Dave Martin6da80ce2010-07-30 09:50:09 -03001705 continue;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001706
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001707 if (!syms_ss && symsrc__has_symtab(ss)) {
1708 syms_ss = ss;
1709 next_slot = true;
Adrian Hunter0058aef2013-12-03 09:23:08 +02001710 if (!dso->symsrc_filename)
1711 dso->symsrc_filename = strdup(name);
Cody P Schaferd26cd122012-08-10 15:23:00 -07001712 }
1713
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001714 if (!runtime_ss && symsrc__possibly_runtime(ss)) {
1715 runtime_ss = ss;
1716 next_slot = true;
Cody P Schafera44f6052012-08-10 15:22:59 -07001717 }
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001718
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001719 if (next_slot) {
1720 ss_pos++;
Jiri Olsa33ff5812012-04-18 15:46:58 +02001721
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001722 if (syms_ss && runtime_ss)
1723 break;
Namhyung Kim98e9f032014-02-20 10:32:54 +09001724 } else {
1725 symsrc__destroy(ss);
Dave Martin6da80ce2010-07-30 09:50:09 -03001726 }
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001727
Arnaldo Carvalho de Meloa25e46c2009-07-11 12:18:36 -03001728 }
Dave Martin6da80ce2010-07-30 09:50:09 -03001729
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001730 if (!runtime_ss && !syms_ss)
1731 goto out_free;
1732
1733 if (runtime_ss && !syms_ss) {
1734 syms_ss = runtime_ss;
Arnaldo Carvalho de Melo60e4b102011-03-22 15:42:14 -03001735 }
1736
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001737 /* We'll have to hope for the best */
1738 if (!runtime_ss && syms_ss)
1739 runtime_ss = syms_ss;
1740
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001741 if (syms_ss)
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001742 ret = dso__load_sym(dso, map, syms_ss, runtime_ss, kmod);
Namhyung Kim1029f9f2014-02-20 10:32:56 +09001743 else
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001744 ret = -1;
1745
David Ahernf47b58b2012-08-19 09:47:14 -06001746 if (ret > 0) {
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001747 int nr_plt;
1748
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001749 nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss);
Cody P Schafer3aafe5a2012-08-10 15:23:02 -07001750 if (nr_plt > 0)
1751 ret += nr_plt;
1752 }
1753
1754 for (; ss_pos > 0; ss_pos--)
1755 symsrc__destroy(&ss_[ss_pos - 1]);
1756out_free:
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001757 free(name);
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001758 if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001759 ret = 0;
1760out:
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001761 dso__set_loaded(dso);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001762 pthread_mutex_unlock(&dso->lock);
Krister Johansen843ff372017-07-05 18:48:08 -07001763 nsinfo__mountns_exit(&nsc);
Namhyung Kim4a936ed2015-05-18 09:30:40 +09001764
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001765 return ret;
1766}
1767
Arnaldo Carvalho de Meloa7c2b572019-11-17 11:38:13 -03001768static int map__strcmp(const void *a, const void *b)
1769{
1770 const struct map *ma = *(const struct map **)a, *mb = *(const struct map **)b;
1771 return strcmp(ma->dso->short_name, mb->dso->short_name);
1772}
1773
1774static int map__strcmp_name(const void *name, const void *b)
1775{
1776 const struct map *map = *(const struct map **)b;
1777 return strcmp(name, map->dso->short_name);
1778}
1779
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001780void __maps__sort_by_name(struct maps *maps)
Arnaldo Carvalho de Meloa7c2b572019-11-17 11:38:13 -03001781{
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001782 qsort(maps->maps_by_name, maps->nr_maps, sizeof(struct map *), map__strcmp);
Arnaldo Carvalho de Meloa7c2b572019-11-17 11:38:13 -03001783}
1784
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001785static int map__groups__sort_by_name_from_rbtree(struct maps *maps)
Arnaldo Carvalho de Meloa7c2b572019-11-17 11:38:13 -03001786{
1787 struct map *map;
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001788 struct map **maps_by_name = realloc(maps->maps_by_name, maps->nr_maps * sizeof(map));
Arnaldo Carvalho de Meloa7c2b572019-11-17 11:38:13 -03001789 int i = 0;
1790
1791 if (maps_by_name == NULL)
1792 return -1;
1793
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001794 maps->maps_by_name = maps_by_name;
1795 maps->nr_maps_allocated = maps->nr_maps;
Arnaldo Carvalho de Meloa7c2b572019-11-17 11:38:13 -03001796
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001797 maps__for_each_entry(maps, map)
Arnaldo Carvalho de Meloa7c2b572019-11-17 11:38:13 -03001798 maps_by_name[i++] = map;
1799
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001800 __maps__sort_by_name(maps);
Arnaldo Carvalho de Meloa7c2b572019-11-17 11:38:13 -03001801 return 0;
1802}
1803
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001804static struct map *__maps__find_by_name(struct maps *maps, const char *name)
Arnaldo Carvalho de Meloa7c2b572019-11-17 11:38:13 -03001805{
1806 struct map **mapp;
1807
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001808 if (maps->maps_by_name == NULL &&
1809 map__groups__sort_by_name_from_rbtree(maps))
Arnaldo Carvalho de Meloa7c2b572019-11-17 11:38:13 -03001810 return NULL;
1811
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001812 mapp = bsearch(name, maps->maps_by_name, maps->nr_maps, sizeof(*mapp), map__strcmp_name);
Arnaldo Carvalho de Meloa7c2b572019-11-17 11:38:13 -03001813 if (mapp)
1814 return *mapp;
1815 return NULL;
1816}
1817
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001818struct map *maps__find_by_name(struct maps *maps, const char *name)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001819{
Arnaldo Carvalho de Melo4bb7123d2015-05-22 11:52:22 -03001820 struct map *map;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001821
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001822 down_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -03001823
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001824 if (maps->last_search_by_name && strcmp(maps->last_search_by_name->dso->short_name, name) == 0) {
1825 map = maps->last_search_by_name;
Arnaldo Carvalho de Melo1ae14512019-11-13 16:33:33 -03001826 goto out_unlock;
1827 }
Arnaldo Carvalho de Meloa7c2b572019-11-17 11:38:13 -03001828 /*
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001829 * If we have maps->maps_by_name, then the name isn't in the rbtree,
1830 * as maps->maps_by_name mirrors the rbtree when lookups by name are
Arnaldo Carvalho de Meloa7c2b572019-11-17 11:38:13 -03001831 * made.
1832 */
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001833 map = __maps__find_by_name(maps, name);
1834 if (map || maps->maps_by_name != NULL)
Arnaldo Carvalho de Meloa7c2b572019-11-17 11:38:13 -03001835 goto out_unlock;
Arnaldo Carvalho de Melo1ae14512019-11-13 16:33:33 -03001836
Arnaldo Carvalho de Meloa7c2b572019-11-17 11:38:13 -03001837 /* Fallback to traversing the rbtree... */
Arnaldo Carvalho de Meloc5c584d2019-11-13 16:16:25 -03001838 maps__for_each_entry(maps, map)
Arnaldo Carvalho de Melo1ae14512019-11-13 16:33:33 -03001839 if (strcmp(map->dso->short_name, name) == 0) {
Arnaldo Carvalho de Melo9a29cee2019-11-25 22:21:28 -03001840 maps->last_search_by_name = map;
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -03001841 goto out_unlock;
Arnaldo Carvalho de Melo1ae14512019-11-13 16:33:33 -03001842 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001843
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -03001844 map = NULL;
1845
1846out_unlock:
Arnaldo Carvalho de Melo0a7c74e2017-04-04 13:15:04 -03001847 up_read(&maps->lock);
Arnaldo Carvalho de Melo6a2ffcd2015-05-22 13:45:24 -03001848 return map;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03001849}
1850
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03001851int dso__load_vmlinux(struct dso *dso, struct map *map,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001852 const char *vmlinux, bool vmlinux_allocated)
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001853{
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001854 int err = -1;
1855 struct symsrc ss;
David Ahernec5761e2010-12-09 13:27:07 -07001856 char symfs_vmlinux[PATH_MAX];
Cody P Schafer005f9292012-08-10 15:22:58 -07001857 enum dso_binary_type symtab_type;
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001858
Namhyung Kim5698d2c2013-07-17 17:08:15 +09001859 if (vmlinux[0] == '/')
1860 snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
1861 else
Arnaldo Carvalho de Melo972f3932014-07-29 10:21:58 -03001862 symbol__join_symfs(symfs_vmlinux, vmlinux);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001863
Cody P Schafer21ea4532012-08-10 15:22:56 -07001864 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Cody P Schafer005f9292012-08-10 15:22:58 -07001865 symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
Cody P Schafer21ea4532012-08-10 15:22:56 -07001866 else
Cody P Schafer005f9292012-08-10 15:22:58 -07001867 symtab_type = DSO_BINARY_TYPE__VMLINUX;
Cody P Schafer21ea4532012-08-10 15:22:56 -07001868
Cody P Schafer005f9292012-08-10 15:22:58 -07001869 if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001870 return -1;
1871
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001872 err = dso__load_sym(dso, map, &ss, &ss, 0);
Cody P Schaferb68e2f92012-08-10 15:22:57 -07001873 symsrc__destroy(&ss);
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001874
Cody P Schafer515850e2012-08-10 15:22:54 -07001875 if (err > 0) {
Adrian Hunter39b12f782013-08-07 14:38:47 +03001876 if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001877 dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
Adrian Hunter39b12f782013-08-07 14:38:47 +03001878 else
Arnaldo Carvalho de Melo5f706192013-12-17 16:14:07 -03001879 dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -03001880 dso__set_long_name(dso, vmlinux, vmlinux_allocated);
Arnaldo Carvalho de Melo3183f8c2018-04-26 16:52:34 -03001881 dso__set_loaded(dso);
David Ahernec5761e2010-12-09 13:27:07 -07001882 pr_debug("Using %s for symbols\n", symfs_vmlinux);
Cody P Schafer515850e2012-08-10 15:22:54 -07001883 }
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03001884
Arnaldo Carvalho de Meloa2928c42009-05-28 14:55:04 -03001885 return err;
1886}
1887
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001888int dso__load_vmlinux_path(struct dso *dso, struct map *map)
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001889{
1890 int i, err = 0;
Namhyung Kim00dc8652014-11-04 10:14:32 +09001891 char *filename = NULL;
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001892
Arnaldo Carvalho de Melodc382182015-10-11 17:17:24 -03001893 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1894 vmlinux_path__nr_entries + 1);
1895
1896 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001897 err = dso__load_vmlinux(dso, map, vmlinux_path[i], false);
Arnaldo Carvalho de Melodc382182015-10-11 17:17:24 -03001898 if (err > 0)
1899 goto out;
1900 }
1901
Namhyung Kim00dc8652014-11-04 10:14:32 +09001902 if (!symbol_conf.ignore_vmlinux_buildid)
Krister Johansend2396992017-07-05 18:48:13 -07001903 filename = dso__build_id_filename(dso, NULL, 0, false);
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001904 if (filename != NULL) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001905 err = dso__load_vmlinux(dso, map, filename, true);
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -03001906 if (err > 0)
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001907 goto out;
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001908 free(filename);
1909 }
Arnaldo Carvalho de Melo5ad90e42010-05-26 13:26:02 -03001910out:
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02001911 return err;
1912}
1913
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001914static bool visible_dir_filter(const char *name, struct dirent *d)
1915{
1916 if (d->d_type != DT_DIR)
1917 return false;
1918 return lsdir_no_dot_filter(name, d);
1919}
1920
Adrian Hunter0544d422013-10-14 13:43:43 +03001921static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
1922{
1923 char kallsyms_filename[PATH_MAX];
Adrian Hunter0544d422013-10-14 13:43:43 +03001924 int ret = -1;
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001925 struct strlist *dirs;
1926 struct str_node *nd;
Adrian Hunter0544d422013-10-14 13:43:43 +03001927
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001928 dirs = lsdir(dir, visible_dir_filter);
1929 if (!dirs)
Adrian Hunter0544d422013-10-14 13:43:43 +03001930 return -1;
1931
Arnaldo Carvalho de Melo602a1f42016-06-23 11:31:20 -03001932 strlist__for_each_entry(nd, dirs) {
Adrian Hunter0544d422013-10-14 13:43:43 +03001933 scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001934 "%s/%s/kallsyms", dir, nd->s);
Adrian Huntera00d28c2014-01-29 16:14:41 +02001935 if (!validate_kcore_addresses(kallsyms_filename, map)) {
Adrian Hunter0544d422013-10-14 13:43:43 +03001936 strlcpy(dir, kallsyms_filename, dir_sz);
1937 ret = 0;
1938 break;
1939 }
1940 }
1941
Masami Hiramatsuc48903b2016-05-11 22:52:08 +09001942 strlist__delete(dirs);
Adrian Hunter0544d422013-10-14 13:43:43 +03001943
1944 return ret;
1945}
1946
Masami Hiramatsu11870d72016-05-29 00:15:13 +09001947/*
1948 * Use open(O_RDONLY) to check readability directly instead of access(R_OK)
1949 * since access(R_OK) only checks with real UID/GID but open() use effective
1950 * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO).
1951 */
1952static bool filename__readable(const char *file)
1953{
1954 int fd = open(file, O_RDONLY);
1955 if (fd < 0)
1956 return false;
1957 close(fd);
1958 return true;
1959}
1960
Adrian Hunter0544d422013-10-14 13:43:43 +03001961static char *dso__find_kallsyms(struct dso *dso, struct map *map)
1962{
1963 u8 host_build_id[BUILD_ID_SIZE];
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001964 char sbuild_id[SBUILD_ID_SIZE];
Adrian Hunter0544d422013-10-14 13:43:43 +03001965 bool is_host = false;
1966 char path[PATH_MAX];
1967
1968 if (!dso->has_build_id) {
1969 /*
1970 * Last resort, if we don't have a build-id and couldn't find
1971 * any vmlinux file, try the running kernel kallsyms table.
1972 */
1973 goto proc_kallsyms;
1974 }
1975
1976 if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
1977 sizeof(host_build_id)) == 0)
1978 is_host = dso__build_id_equal(dso, host_build_id);
1979
Masami Hiramatsu4e4b6c02016-05-29 00:15:28 +09001980 /* Try a fast path for /proc/kallsyms if possible */
Adrian Hunter0544d422013-10-14 13:43:43 +03001981 if (is_host) {
Adrian Hunter0544d422013-10-14 13:43:43 +03001982 /*
Masami Hiramatsu11870d72016-05-29 00:15:13 +09001983 * Do not check the build-id cache, unless we know we cannot use
1984 * /proc/kcore or module maps don't match to /proc/kallsyms.
1985 * To check readability of /proc/kcore, do not use access(R_OK)
1986 * since /proc/kcore requires CAP_SYS_RAWIO to read and access
1987 * can't check it.
Adrian Hunter0544d422013-10-14 13:43:43 +03001988 */
Masami Hiramatsu11870d72016-05-29 00:15:13 +09001989 if (filename__readable("/proc/kcore") &&
1990 !validate_kcore_addresses("/proc/kallsyms", map))
1991 goto proc_kallsyms;
Adrian Hunter0544d422013-10-14 13:43:43 +03001992 }
1993
Masami Hiramatsu4e4b6c02016-05-29 00:15:28 +09001994 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
1995
Adrian Hunter449867e2013-11-26 15:19:24 +02001996 /* Find kallsyms in build-id cache with kcore */
Masami Hiramatsu4e4b6c02016-05-29 00:15:28 +09001997 scnprintf(path, sizeof(path), "%s/%s/%s",
1998 buildid_dir, DSO__NAME_KCORE, sbuild_id);
1999
Adrian Hunter449867e2013-11-26 15:19:24 +02002000 if (!find_matching_kcore(map, path, sizeof(path)))
2001 return strdup(path);
2002
Masami Hiramatsu4e4b6c02016-05-29 00:15:28 +09002003 /* Use current /proc/kallsyms if possible */
2004 if (is_host) {
2005proc_kallsyms:
2006 return strdup("/proc/kallsyms");
2007 }
2008
2009 /* Finally, find a cache of kallsyms */
Masami Hiramatsu01412262016-05-29 00:15:37 +09002010 if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) {
Adrian Hunter0544d422013-10-14 13:43:43 +03002011 pr_err("No kallsyms or vmlinux with build-id %s was found\n",
2012 sbuild_id);
2013 return NULL;
2014 }
2015
2016 return strdup(path);
Adrian Hunter0544d422013-10-14 13:43:43 +03002017}
2018
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002019static int dso__load_kernel_sym(struct dso *dso, struct map *map)
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03002020{
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002021 int err;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -02002022 const char *kallsyms_filename = NULL;
2023 char *kallsyms_allocated_filename = NULL;
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02002024 /*
David Ahernb226a5a72010-12-07 19:39:46 -07002025 * Step 1: if the user specified a kallsyms or vmlinux filename, use
2026 * it and only it, reporting errors to the user if it cannot be used.
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02002027 *
2028 * For instance, try to analyse an ARM perf.data file _without_ a
2029 * build-id, or if the user specifies the wrong path to the right
2030 * vmlinux file, obviously we can't fallback to another vmlinux (a
2031 * x86_86 one, on the machine where analysis is being performed, say),
2032 * or worse, /proc/kallsyms.
2033 *
2034 * If the specified file _has_ a build-id and there is a build-id
2035 * section in the perf.data file, we will still do the expected
2036 * validation in dso__load_vmlinux and will bail out if they don't
2037 * match.
2038 */
David Ahernb226a5a72010-12-07 19:39:46 -07002039 if (symbol_conf.kallsyms_name != NULL) {
2040 kallsyms_filename = symbol_conf.kallsyms_name;
2041 goto do_kallsyms;
2042 }
2043
Willy Tarreaufc2be692013-09-14 10:32:59 +02002044 if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002045 return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, false);
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02002046 }
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03002047
Willy Tarreaufc2be692013-09-14 10:32:59 +02002048 if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002049 err = dso__load_vmlinux_path(dso, map);
Arnaldo Carvalho de Meloa19afe42010-01-27 21:05:50 -02002050 if (err > 0)
Adrian Hunter39b12f782013-08-07 14:38:47 +03002051 return err;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002052 }
2053
David Ahernec5761e2010-12-09 13:27:07 -07002054 /* do not try local files if a symfs was given */
2055 if (symbol_conf.symfs[0] != 0)
2056 return -1;
2057
Adrian Hunter0544d422013-10-14 13:43:43 +03002058 kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
2059 if (!kallsyms_allocated_filename)
2060 return -1;
Arnaldo Carvalho de Melob7cece762010-01-13 13:22:17 -02002061
Adrian Hunter0544d422013-10-14 13:43:43 +03002062 kallsyms_filename = kallsyms_allocated_filename;
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03002063
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02002064do_kallsyms:
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002065 err = dso__load_kallsyms(dso, kallsyms_filename, map);
Arnaldo Carvalho de Melo3846df22010-02-22 16:15:39 -03002066 if (err > 0)
2067 pr_debug("Using %s for symbols\n", kallsyms_filename);
Arnaldo Carvalho de Melodc8d6ab2010-01-19 10:36:14 -02002068 free(kallsyms_allocated_filename);
2069
Adrian Hunter8e0cf962013-08-07 14:38:51 +03002070 if (err > 0 && !dso__is_kcore(dso)) {
Adrian Hunterbdac0bc2014-07-14 13:02:43 +03002071 dso->binary_type = DSO_BINARY_TYPE__KALLSYMS;
Masami Hiramatsu0a775822016-05-15 12:19:40 +09002072 dso__set_long_name(dso, DSO__NAME_KALLSYMS, false);
Arnaldo Carvalho de Melo6a4694a2009-11-27 16:29:17 -02002073 map__fixup_start(map);
2074 map__fixup_end(map);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -03002075 }
Arnaldo Carvalho de Melo94cb9e32009-08-06 14:43:17 -03002076
Arnaldo Carvalho de Meloa827c872009-05-28 14:55:19 -03002077 return err;
2078}
2079
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002080static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002081{
2082 int err;
2083 const char *kallsyms_filename = NULL;
Arnaldo Carvalho de Melo93fcce92019-11-04 16:25:11 -03002084 struct machine *machine = map__kmaps(map)->machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002085 char path[PATH_MAX];
2086
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002087 if (machine__is_default_guest(machine)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002088 /*
2089 * if the user specified a vmlinux filename, use it and only
2090 * it, reporting errors to the user if it cannot be used.
2091 * Or use file guest_kallsyms inputted by user on commandline
2092 */
2093 if (symbol_conf.default_guest_vmlinux_name != NULL) {
Arnaldo Carvalho de Meloaeafcba2011-03-31 10:56:28 -03002094 err = dso__load_vmlinux(dso, map,
Arnaldo Carvalho de Melo5230fb72013-12-10 11:58:52 -03002095 symbol_conf.default_guest_vmlinux_name,
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002096 false);
Adrian Hunter39b12f782013-08-07 14:38:47 +03002097 return err;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002098 }
2099
2100 kallsyms_filename = symbol_conf.default_guest_kallsyms;
2101 if (!kallsyms_filename)
2102 return -1;
2103 } else {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002104 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002105 kallsyms_filename = path;
2106 }
2107
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002108 err = dso__load_kallsyms(dso, kallsyms_filename, map);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03002109 if (err > 0)
Adrian Hunter39b12f782013-08-07 14:38:47 +03002110 pr_debug("Using %s for symbols\n", kallsyms_filename);
Adrian Hunter8e0cf962013-08-07 14:38:51 +03002111 if (err > 0 && !dso__is_kcore(dso)) {
Adrian Hunterbdac0bc2014-07-14 13:02:43 +03002112 dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
Jiri Olsa8c7f1bb2018-02-15 13:26:30 +01002113 dso__set_long_name(dso, machine->mmap_name, false);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002114 map__fixup_start(map);
2115 map__fixup_end(map);
2116 }
2117
2118 return err;
2119}
Frederic Weisbeckercd84c2a2009-08-12 10:03:49 +02002120
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002121static void vmlinux_path__exit(void)
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -02002122{
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03002123 while (--vmlinux_path__nr_entries >= 0)
2124 zfree(&vmlinux_path[vmlinux_path__nr_entries]);
Wang Nanc4f03542015-05-17 10:56:27 +00002125 vmlinux_path__nr_entries = 0;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002126
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03002127 zfree(&vmlinux_path);
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002128}
2129
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01002130static const char * const vmlinux_paths[] = {
2131 "vmlinux",
2132 "/boot/vmlinux"
2133};
2134
2135static const char * const vmlinux_paths_upd[] = {
2136 "/boot/vmlinux-%s",
2137 "/usr/lib/debug/boot/vmlinux-%s",
2138 "/lib/modules/%s/build/vmlinux",
Ekaterina Tumanovaf55ae952015-11-25 17:32:46 +01002139 "/usr/lib/debug/lib/modules/%s/vmlinux",
2140 "/usr/lib/debug/boot/vmlinux-%s.debug"
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01002141};
2142
2143static int vmlinux_path__add(const char *new_entry)
2144{
2145 vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry);
2146 if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
2147 return -1;
2148 ++vmlinux_path__nr_entries;
2149
2150 return 0;
2151}
2152
Kan Liangce80d3b2015-08-28 05:48:04 -04002153static int vmlinux_path__init(struct perf_env *env)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002154{
2155 struct utsname uts;
2156 char bf[PATH_MAX];
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002157 char *kernel_version;
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01002158 unsigned int i;
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002159
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01002160 vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
2161 ARRAY_SIZE(vmlinux_paths_upd)));
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002162 if (vmlinux_path == NULL)
2163 return -1;
2164
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01002165 for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++)
2166 if (vmlinux_path__add(vmlinux_paths[i]) < 0)
2167 goto out_fail;
David Ahernec5761e2010-12-09 13:27:07 -07002168
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002169 /* only try kernel version if no symfs was given */
David Ahernec5761e2010-12-09 13:27:07 -07002170 if (symbol_conf.symfs[0] != 0)
2171 return 0;
2172
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002173 if (env) {
2174 kernel_version = env->os_release;
2175 } else {
2176 if (uname(&uts) < 0)
2177 goto out_fail;
David Ahernec5761e2010-12-09 13:27:07 -07002178
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002179 kernel_version = uts.release;
2180 }
2181
Ekaterina Tumanovaaac48642015-11-25 17:32:45 +01002182 for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) {
2183 snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version);
2184 if (vmlinux_path__add(bf) < 0)
2185 goto out_fail;
2186 }
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002187
2188 return 0;
2189
2190out_fail:
2191 vmlinux_path__exit();
2192 return -1;
2193}
2194
David Ahern3bfe5f82013-11-18 13:32:48 -07002195int setup_list(struct strlist **list, const char *list_str,
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002196 const char *list_name)
2197{
2198 if (list_str == NULL)
2199 return 0;
2200
Arnaldo Carvalho de Melo4a77e212015-07-20 12:13:34 -03002201 *list = strlist__new(list_str, NULL);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002202 if (!*list) {
2203 pr_err("problems parsing %s list\n", list_name);
2204 return -1;
2205 }
Arnaldo Carvalho de Melo0bc2f2f2015-07-13 08:21:57 -03002206
2207 symbol_conf.has_filter = true;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002208 return 0;
2209}
2210
David Aherne03eaa42015-03-24 09:52:41 -06002211int setup_intlist(struct intlist **list, const char *list_str,
2212 const char *list_name)
2213{
2214 if (list_str == NULL)
2215 return 0;
2216
2217 *list = intlist__new(list_str);
2218 if (!*list) {
2219 pr_err("problems parsing %s list\n", list_name);
2220 return -1;
2221 }
2222 return 0;
2223}
2224
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002225static bool symbol__read_kptr_restrict(void)
2226{
2227 bool value = false;
Wang Nan38272dc2016-05-24 09:21:27 +00002228 FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002229
Wang Nan38272dc2016-05-24 09:21:27 +00002230 if (fp != NULL) {
2231 char line[8];
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002232
Wang Nan38272dc2016-05-24 09:21:27 +00002233 if (fgets(line, sizeof(line), fp) != NULL)
Igor Lubashev8859aed2019-08-26 21:39:15 -04002234 value = perf_cap__capable(CAP_SYSLOG) ?
2235 (atoi(line) >= 2) :
2236 (atoi(line) != 0);
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002237
Wang Nan38272dc2016-05-24 09:21:27 +00002238 fclose(fp);
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002239 }
2240
Igor Lubashev8859aed2019-08-26 21:39:15 -04002241 /* Per kernel/kallsyms.c:
2242 * we also restrict when perf_event_paranoid > 1 w/o CAP_SYSLOG
2243 */
2244 if (perf_event_paranoid() > 1 && !perf_cap__capable(CAP_SYSLOG))
2245 value = true;
2246
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002247 return value;
2248}
2249
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -03002250int symbol__annotation_init(void)
2251{
Arnaldo Carvalho de Melo7b366142018-04-12 14:58:24 -03002252 if (symbol_conf.init_annotation)
2253 return 0;
2254
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -03002255 if (symbol_conf.initialized) {
2256 pr_err("Annotation needs to be init before symbol__init()\n");
2257 return -1;
2258 }
2259
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -03002260 symbol_conf.priv_size += sizeof(struct annotation);
2261 symbol_conf.init_annotation = true;
2262 return 0;
2263}
2264
Kan Liangce80d3b2015-08-28 05:48:04 -04002265int symbol__init(struct perf_env *env)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002266{
David Ahernec5761e2010-12-09 13:27:07 -07002267 const char *symfs;
2268
Jovi Zhang85e00b52010-09-09 13:30:59 -03002269 if (symbol_conf.initialized)
2270 return 0;
2271
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002272 symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
David S. Miller4d439512011-03-29 14:18:39 -03002273
Namhyung Kim166ccc92012-08-06 13:41:19 +09002274 symbol__elf_init();
2275
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -02002276 if (symbol_conf.sort_by_name)
2277 symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
2278 sizeof(struct symbol));
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -02002279
Namhyung Kim0a7e6d12014-08-12 15:40:45 +09002280 if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002281 return -1;
2282
Arnaldo Carvalho de Meloc410a332009-12-15 20:04:41 -02002283 if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
2284 pr_err("'.' is the only non valid --field-separator argument\n");
2285 return -1;
2286 }
2287
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002288 if (setup_list(&symbol_conf.dso_list,
2289 symbol_conf.dso_list_str, "dso") < 0)
2290 return -1;
2291
2292 if (setup_list(&symbol_conf.comm_list,
2293 symbol_conf.comm_list_str, "comm") < 0)
2294 goto out_free_dso_list;
2295
David Aherne03eaa42015-03-24 09:52:41 -06002296 if (setup_intlist(&symbol_conf.pid_list,
2297 symbol_conf.pid_list_str, "pid") < 0)
2298 goto out_free_comm_list;
2299
2300 if (setup_intlist(&symbol_conf.tid_list,
2301 symbol_conf.tid_list_str, "tid") < 0)
2302 goto out_free_pid_list;
2303
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002304 if (setup_list(&symbol_conf.sym_list,
2305 symbol_conf.sym_list_str, "symbol") < 0)
David Aherne03eaa42015-03-24 09:52:41 -06002306 goto out_free_tid_list;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002307
David Ahern64eff7d2016-11-25 13:00:21 -07002308 if (setup_list(&symbol_conf.bt_stop_list,
2309 symbol_conf.bt_stop_list_str, "symbol") < 0)
2310 goto out_free_sym_list;
2311
David Ahernec5761e2010-12-09 13:27:07 -07002312 /*
2313 * A path to symbols of "/" is identical to ""
2314 * reset here for simplicity.
2315 */
2316 symfs = realpath(symbol_conf.symfs, NULL);
2317 if (symfs == NULL)
2318 symfs = symbol_conf.symfs;
2319 if (strcmp(symfs, "/") == 0)
2320 symbol_conf.symfs = "";
2321 if (symfs != symbol_conf.symfs)
2322 free((void *)symfs);
2323
Arnaldo Carvalho de Meloec80fde2011-05-26 09:53:51 -03002324 symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
2325
Jovi Zhang85e00b52010-09-09 13:30:59 -03002326 symbol_conf.initialized = true;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002327 return 0;
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002328
David Ahern64eff7d2016-11-25 13:00:21 -07002329out_free_sym_list:
2330 strlist__delete(symbol_conf.sym_list);
David Aherne03eaa42015-03-24 09:52:41 -06002331out_free_tid_list:
2332 intlist__delete(symbol_conf.tid_list);
2333out_free_pid_list:
2334 intlist__delete(symbol_conf.pid_list);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002335out_free_comm_list:
2336 strlist__delete(symbol_conf.comm_list);
Namhyung Kimd74c8962011-12-13 00:16:52 +09002337out_free_dso_list:
2338 strlist__delete(symbol_conf.dso_list);
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -02002339 return -1;
Arnaldo Carvalho de Melo4aa65632009-12-13 19:50:29 -02002340}
Arnaldo Carvalho de Melocc612d82009-11-23 16:39:10 -02002341
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002342void symbol__exit(void)
2343{
Jovi Zhang85e00b52010-09-09 13:30:59 -03002344 if (!symbol_conf.initialized)
2345 return;
David Ahern64eff7d2016-11-25 13:00:21 -07002346 strlist__delete(symbol_conf.bt_stop_list);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002347 strlist__delete(symbol_conf.sym_list);
2348 strlist__delete(symbol_conf.dso_list);
2349 strlist__delete(symbol_conf.comm_list);
David Aherne03eaa42015-03-24 09:52:41 -06002350 intlist__delete(symbol_conf.tid_list);
2351 intlist__delete(symbol_conf.pid_list);
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002352 vmlinux_path__exit();
2353 symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
David Ahern64eff7d2016-11-25 13:00:21 -07002354 symbol_conf.bt_stop_list = NULL;
Jovi Zhang85e00b52010-09-09 13:30:59 -03002355 symbol_conf.initialized = false;
Arnaldo Carvalho de Melod65a4582010-07-30 18:31:28 -03002356}
He Kuanga7066702016-05-19 11:47:37 +00002357
2358int symbol__config_symfs(const struct option *opt __maybe_unused,
2359 const char *dir, int unset __maybe_unused)
2360{
2361 char *bf = NULL;
2362 int ret;
2363
2364 symbol_conf.symfs = strdup(dir);
2365 if (symbol_conf.symfs == NULL)
2366 return -ENOMEM;
2367
2368 /* skip the locally configured cache if a symfs is given, and
2369 * config buildid dir to symfs/.debug
2370 */
2371 ret = asprintf(&bf, "%s/%s", dir, ".debug");
2372 if (ret < 0)
2373 return -ENOMEM;
2374
2375 set_buildid_dir(bf);
2376
2377 free(bf);
2378 return 0;
2379}
Jiri Olsa9f874982018-03-07 16:50:06 +01002380
2381struct mem_info *mem_info__get(struct mem_info *mi)
2382{
2383 if (mi)
2384 refcount_inc(&mi->refcnt);
2385 return mi;
2386}
2387
2388void mem_info__put(struct mem_info *mi)
2389{
2390 if (mi && refcount_dec_and_test(&mi->refcnt))
2391 free(mi);
2392}
2393
2394struct mem_info *mem_info__new(void)
2395{
2396 struct mem_info *mi = zalloc(sizeof(*mi));
2397
2398 if (mi)
2399 refcount_set(&mi->refcnt, 1);
2400 return mi;
2401}