xref: /linux/tools/perf/util/dso.h (revision c7decec2f2d2ab0366567f9e30c0e1418cece43f)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_DSO
3 #define __PERF_DSO
4 
5 #include <linux/refcount.h>
6 #include <linux/types.h>
7 #include <linux/rbtree.h>
8 #include <sys/types.h>
9 #include <stdbool.h>
10 #include <stdio.h>
11 #include <linux/bitops.h>
12 #include "build-id.h"
13 #include "debuginfo.h"
14 #include "mutex.h"
15 #include <internal/rc_check.h>
16 
17 struct machine;
18 struct map;
19 struct perf_env;
20 
21 #define DSO__NAME_KALLSYMS	"[kernel.kallsyms]"
22 #define DSO__NAME_KCORE		"[kernel.kcore]"
23 
24 /**
25  * enum dso_binary_type - The kind of DSO generally associated with a memory
26  *                        region (struct map).
27  */
28 enum dso_binary_type {
29 	/** @DSO_BINARY_TYPE__KALLSYMS: Symbols from /proc/kallsyms file. */
30 	DSO_BINARY_TYPE__KALLSYMS = 0,
31 	/** @DSO_BINARY_TYPE__GUEST_KALLSYMS: Guest /proc/kallsyms file. */
32 	DSO_BINARY_TYPE__GUEST_KALLSYMS,
33 	/** @DSO_BINARY_TYPE__VMLINUX: Path to kernel /boot/vmlinux file. */
34 	DSO_BINARY_TYPE__VMLINUX,
35 	/** @DSO_BINARY_TYPE__GUEST_VMLINUX: Path to guest kernel /boot/vmlinux file. */
36 	DSO_BINARY_TYPE__GUEST_VMLINUX,
37 	/** @DSO_BINARY_TYPE__JAVA_JIT: Symbols from /tmp/perf.map file. */
38 	DSO_BINARY_TYPE__JAVA_JIT,
39 	/**
40 	 * @DSO_BINARY_TYPE__DEBUGLINK: Debug file readable from the file path
41 	 * in the .gnu_debuglink ELF section of the dso.
42 	 */
43 	DSO_BINARY_TYPE__DEBUGLINK,
44 	/**
45 	 * @DSO_BINARY_TYPE__BUILD_ID_CACHE: File named after buildid located in
46 	 * the buildid cache with an elf filename.
47 	 */
48 	DSO_BINARY_TYPE__BUILD_ID_CACHE,
49 	/**
50 	 * @DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO: File named after buildid
51 	 * located in the buildid cache with a debug filename.
52 	 */
53 	DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO,
54 	/**
55 	 * @DSO_BINARY_TYPE__FEDORA_DEBUGINFO: Debug file in /usr/lib/debug
56 	 * with .debug suffix.
57 	 */
58 	DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
59 	/** @DSO_BINARY_TYPE__UBUNTU_DEBUGINFO: Debug file in /usr/lib/debug. */
60 	DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
61 	/**
62 	 * @DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO: dso__long_name debuginfo
63 	 * file in /usr/lib/debug/lib rather than the expected
64 	 * /usr/lib/debug/usr/lib.
65 	 */
66 	DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO,
67 	/**
68 	 * @DSO_BINARY_TYPE__BUILDID_DEBUGINFO: File named after buildid located
69 	 * in /usr/lib/debug/.build-id/.
70 	 */
71 	DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
72 	/**
73 	 * @DSO_BINARY_TYPE__GNU_DEBUGDATA: MiniDebuginfo where a compressed
74 	 * ELF file is placed in a .gnu_debugdata section.
75 	 */
76 	DSO_BINARY_TYPE__GNU_DEBUGDATA,
77 	/** @DSO_BINARY_TYPE__SYSTEM_PATH_DSO: A regular executable/shared-object file. */
78 	DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
79 	/** @DSO_BINARY_TYPE__GUEST_KMODULE: Guest kernel module .ko file. */
80 	DSO_BINARY_TYPE__GUEST_KMODULE,
81 	/** @DSO_BINARY_TYPE__GUEST_KMODULE_COMP: Guest kernel module .ko.gz file. */
82 	DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
83 	/** @DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE: Kernel module .ko file. */
84 	DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
85 	/** @DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP: Kernel module .ko.gz file. */
86 	DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
87 	/** @DSO_BINARY_TYPE__KCORE: /proc/kcore file. */
88 	DSO_BINARY_TYPE__KCORE,
89 	/** @DSO_BINARY_TYPE__GUEST_KCORE: Guest /proc/kcore file. */
90 	DSO_BINARY_TYPE__GUEST_KCORE,
91 	/**
92 	 * @DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO: Openembedded/Yocto -dbg
93 	 * package debug info.
94 	 */
95 	DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
96 	/** @DSO_BINARY_TYPE__BPF_PROG_INFO: jitted BPF code. */
97 	DSO_BINARY_TYPE__BPF_PROG_INFO,
98 	/** @DSO_BINARY_TYPE__BPF_IMAGE: jitted BPF trampoline or dispatcher code. */
99 	DSO_BINARY_TYPE__BPF_IMAGE,
100 	/**
101 	 * @DSO_BINARY_TYPE__OOL: out of line code such as kprobe-replaced
102 	 * instructions or optimized kprobes or ftrace trampolines.
103 	 */
104 	DSO_BINARY_TYPE__OOL,
105 	/** @DSO_BINARY_TYPE__NOT_FOUND: Unknown DSO kind. */
106 	DSO_BINARY_TYPE__NOT_FOUND,
107 };
108 
109 enum dso_space_type {
110 	DSO_SPACE__USER = 0,
111 	DSO_SPACE__KERNEL,
112 	DSO_SPACE__KERNEL_GUEST
113 };
114 
115 enum dso_swap_type {
116 	DSO_SWAP__UNSET,
117 	DSO_SWAP__NO,
118 	DSO_SWAP__YES,
119 };
120 
121 enum dso_data_status {
122 	DSO_DATA_STATUS_ERROR	= -1,
123 	DSO_DATA_STATUS_UNKNOWN	= 0,
124 	DSO_DATA_STATUS_OK	= 1,
125 };
126 
127 enum dso_data_status_seen {
128 	DSO_DATA_STATUS_SEEN_ITRACE,
129 };
130 
131 enum dso_type {
132 	DSO__TYPE_UNKNOWN,
133 	DSO__TYPE_64BIT,
134 	DSO__TYPE_32BIT,
135 	DSO__TYPE_X32BIT,
136 };
137 
138 enum dso_load_errno {
139 	DSO_LOAD_ERRNO__SUCCESS		= 0,
140 
141 	/*
142 	 * Choose an arbitrary negative big number not to clash with standard
143 	 * errno since SUS requires the errno has distinct positive values.
144 	 * See 'Issue 6' in the link below.
145 	 *
146 	 * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html
147 	 */
148 	__DSO_LOAD_ERRNO__START		= -10000,
149 
150 	DSO_LOAD_ERRNO__INTERNAL_ERROR	= __DSO_LOAD_ERRNO__START,
151 
152 	/* for symsrc__init() */
153 	DSO_LOAD_ERRNO__INVALID_ELF,
154 	DSO_LOAD_ERRNO__CANNOT_READ_BUILDID,
155 	DSO_LOAD_ERRNO__MISMATCHING_BUILDID,
156 
157 	/* for decompress_kmodule */
158 	DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE,
159 
160 	__DSO_LOAD_ERRNO__END,
161 };
162 
163 #define DSO_SWAP_TYPE__SWAP(swap_type, type, val)		\
164 ({								\
165 	type ____r = val;					\
166 	BUG_ON(swap_type == DSO_SWAP__UNSET);			\
167 	if (swap_type == DSO_SWAP__YES) {			\
168 		switch (sizeof(____r)) {			\
169 		case 2:						\
170 			____r = bswap_16(val);			\
171 			break;					\
172 		case 4:						\
173 			____r = bswap_32(val);			\
174 			break;					\
175 		case 8:						\
176 			____r = bswap_64(val);			\
177 			break;					\
178 		default:					\
179 			BUG_ON(1);				\
180 		}						\
181 	}							\
182 	____r;							\
183 })
184 
185 #define DSO__SWAP(dso, type, val) DSO_SWAP_TYPE__SWAP(dso__needs_swap(dso), type, val)
186 
187 #define DSO__DATA_CACHE_SIZE 4096
188 #define DSO__DATA_CACHE_MASK ~(DSO__DATA_CACHE_SIZE - 1)
189 
190 /**
191  * struct dso_id
192  *
193  * Data about backing storage DSO, comes from PERF_RECORD_MMAP2 meta events,
194  * reading from /proc/pid/maps or synthesis of build_ids from DSOs. Possibly
195  * incomplete at any particular use.
196  */
197 struct dso_id {
198 	/* Data related to the mmap2 event or read from /proc/pid/maps. */
199 	struct {
200 		u32	maj;
201 		u32	min;
202 		u64	ino;
203 		u64	ino_generation;
204 	};
205 	/** @mmap2_valid: Are the maj, min and ino fields valid? */
206 	bool	mmap2_valid;
207 	/**
208 	 * @mmap2_ino_generation_valid: Is the ino_generation valid? Generally
209 	 * false for /proc/pid/maps mmap event.
210 	 */
211 	bool	mmap2_ino_generation_valid;
212 	/**
213 	 * @build_id: A possibly populated build_id. build_id__is_defined checks
214 	 * whether it is populated.
215 	 */
216 	struct build_id build_id;
217 };
218 
219 struct dso_cache {
220 	struct rb_node	rb_node;
221 	u64 offset;
222 	u64 size;
223 	char data[];
224 };
225 
226 struct dso_data {
227 	struct rb_root	 cache;
228 	struct list_head open_entry;
229 #ifdef REFCNT_CHECKING
230 	struct dso	 *dso;
231 #endif
232 	int		 fd;
233 	int		 status;
234 	u32		 status_seen;
235 	u64		 file_size;
236 #ifdef HAVE_LIBUNWIND_SUPPORT
237 	u64		 elf_base_addr;
238 	u64		 debug_frame_offset;
239 	u64		 eh_frame_hdr_addr;
240 	u64		 eh_frame_hdr_offset;
241 #endif
242 };
243 
244 struct dso_bpf_prog {
245 	u32		id;
246 	u32		sub_id;
247 	struct perf_env	*env;
248 };
249 
250 struct auxtrace_cache;
251 
DECLARE_RC_STRUCT(dso)252 DECLARE_RC_STRUCT(dso) {
253 	struct mutex	 lock;
254 	struct dsos	 *dsos;
255 	struct rb_root_cached symbols;
256 	struct symbol	 **symbol_names;
257 	size_t		 symbol_names_len;
258 	struct rb_root_cached inlined_nodes;
259 	struct rb_root_cached srclines;
260 	struct rb_root	 data_types;
261 	struct rb_root	 global_vars;
262 
263 	struct {
264 		u64		addr;
265 		struct symbol	*symbol;
266 	} last_find_result;
267 	u64		 text_offset;
268 	u64		 text_end;
269 	const char	 *short_name;
270 	const char	 *long_name;
271 	void		 *a2l;
272 	void		 *libdw;
273 	char		 *symsrc_filename;
274 	struct nsinfo	*nsinfo;
275 	struct auxtrace_cache *auxtrace_cache;
276 	union { /* Tool specific area */
277 		void	 *priv;
278 		u64	 db_id;
279 	};
280 	/* bpf prog information */
281 	struct dso_bpf_prog bpf_prog;
282 	/* dso data file */
283 	struct dso_data	 data;
284 	struct dso_id	 id;
285 	unsigned int	 a2l_fails;
286 	int		 comp;
287 	refcount_t	 refcnt;
288 	enum dso_load_errno	load_errno;
289 	u16		 long_name_len;
290 	u16		 short_name_len;
291 	enum dso_binary_type	symtab_type:8;
292 	enum dso_binary_type	binary_type:8;
293 	enum dso_space_type	kernel:2;
294 	enum dso_swap_type	needs_swap:2;
295 	bool			is_kmod:1;
296 	u8		 adjust_symbols:1;
297 	u8		 header_build_id:1;
298 	u8		 has_srcline:1;
299 	u8		 hit:1;
300 	u8		 annotate_warned:1;
301 	u8		 auxtrace_warned:1;
302 	u8		 debuginfo_warned:1;
303 	u8		 short_name_allocated:1;
304 	u8		 long_name_allocated:1;
305 	u8		 is_64_bit:1;
306 	bool		 sorted_by_name;
307 	bool		 loaded;
308 	u8		 rel;
309 	char		 name[];
310 };
311 
312 extern struct mutex _dso__data_open_lock;
313 extern const struct dso_id dso_id_empty;
314 
315 int dso_id__cmp(const struct dso_id *a, const struct dso_id *b);
316 
317 /* dso__for_each_symbol - iterate over the symbols of given type
318  *
319  * @dso: the 'struct dso *' in which symbols are iterated
320  * @pos: the 'struct symbol *' to use as a loop cursor
321  * @n: the 'struct rb_node *' to use as a temporary storage
322  */
323 #define dso__for_each_symbol(dso, pos, n)	\
324 	symbols__for_each_entry(dso__symbols(dso), pos, n)
325 
dso__a2l(const struct dso * dso)326 static inline void *dso__a2l(const struct dso *dso)
327 {
328 	return RC_CHK_ACCESS(dso)->a2l;
329 }
330 
dso__set_a2l(struct dso * dso,void * val)331 static inline void dso__set_a2l(struct dso *dso, void *val)
332 {
333 	RC_CHK_ACCESS(dso)->a2l = val;
334 }
335 
dso__libdw(const struct dso * dso)336 static inline void *dso__libdw(const struct dso *dso)
337 {
338 	return RC_CHK_ACCESS(dso)->libdw;
339 }
340 
dso__set_libdw(struct dso * dso,void * val)341 static inline void dso__set_libdw(struct dso *dso, void *val)
342 {
343 	RC_CHK_ACCESS(dso)->libdw = val;
344 }
345 
346 struct Dwfl;
347 #ifdef HAVE_LIBDW_SUPPORT
348 struct Dwfl *dso__libdw_dwfl(struct dso *dso);
349 #else
dso__libdw_dwfl(struct dso * dso __maybe_unused)350 static inline struct Dwfl *dso__libdw_dwfl(struct dso *dso __maybe_unused)
351 {
352 	return NULL;
353 }
354 #endif
355 
dso__a2l_fails(const struct dso * dso)356 static inline unsigned int dso__a2l_fails(const struct dso *dso)
357 {
358 	return RC_CHK_ACCESS(dso)->a2l_fails;
359 }
360 
dso__set_a2l_fails(struct dso * dso,unsigned int val)361 static inline void dso__set_a2l_fails(struct dso *dso, unsigned int val)
362 {
363 	RC_CHK_ACCESS(dso)->a2l_fails = val;
364 }
365 
dso__adjust_symbols(const struct dso * dso)366 static inline bool dso__adjust_symbols(const struct dso *dso)
367 {
368 	return RC_CHK_ACCESS(dso)->adjust_symbols;
369 }
370 
dso__set_adjust_symbols(struct dso * dso,bool val)371 static inline void dso__set_adjust_symbols(struct dso *dso, bool val)
372 {
373 	RC_CHK_ACCESS(dso)->adjust_symbols = val;
374 }
375 
dso__annotate_warned(const struct dso * dso)376 static inline bool dso__annotate_warned(const struct dso *dso)
377 {
378 	return RC_CHK_ACCESS(dso)->annotate_warned;
379 }
380 
dso__set_annotate_warned(struct dso * dso)381 static inline void dso__set_annotate_warned(struct dso *dso)
382 {
383 	RC_CHK_ACCESS(dso)->annotate_warned = 1;
384 }
385 
dso__debuginfo_warned(const struct dso * dso)386 static inline bool dso__debuginfo_warned(const struct dso *dso)
387 {
388 	return RC_CHK_ACCESS(dso)->debuginfo_warned;
389 }
390 
dso__set_debuginfo_warned(struct dso * dso)391 static inline void dso__set_debuginfo_warned(struct dso *dso)
392 {
393 	RC_CHK_ACCESS(dso)->debuginfo_warned = 1;
394 }
395 
dso__auxtrace_warned(const struct dso * dso)396 static inline bool dso__auxtrace_warned(const struct dso *dso)
397 {
398 	return RC_CHK_ACCESS(dso)->auxtrace_warned;
399 }
400 
dso__set_auxtrace_warned(struct dso * dso)401 static inline void dso__set_auxtrace_warned(struct dso *dso)
402 {
403 	RC_CHK_ACCESS(dso)->auxtrace_warned = 1;
404 }
405 
dso__auxtrace_cache(struct dso * dso)406 static inline struct auxtrace_cache *dso__auxtrace_cache(struct dso *dso)
407 {
408 	return RC_CHK_ACCESS(dso)->auxtrace_cache;
409 }
410 
dso__set_auxtrace_cache(struct dso * dso,struct auxtrace_cache * cache)411 static inline void dso__set_auxtrace_cache(struct dso *dso, struct auxtrace_cache *cache)
412 {
413 	RC_CHK_ACCESS(dso)->auxtrace_cache = cache;
414 }
415 
dso__bpf_prog(struct dso * dso)416 static inline struct dso_bpf_prog *dso__bpf_prog(struct dso *dso)
417 {
418 	return &RC_CHK_ACCESS(dso)->bpf_prog;
419 }
420 
dso__has_srcline(const struct dso * dso)421 static inline bool dso__has_srcline(const struct dso *dso)
422 {
423 	return RC_CHK_ACCESS(dso)->has_srcline;
424 }
425 
dso__set_has_srcline(struct dso * dso,bool val)426 static inline void dso__set_has_srcline(struct dso *dso, bool val)
427 {
428 	RC_CHK_ACCESS(dso)->has_srcline = val;
429 }
430 
dso__comp(const struct dso * dso)431 static inline int dso__comp(const struct dso *dso)
432 {
433 	return RC_CHK_ACCESS(dso)->comp;
434 }
435 
dso__set_comp(struct dso * dso,int comp)436 static inline void dso__set_comp(struct dso *dso, int comp)
437 {
438 	RC_CHK_ACCESS(dso)->comp = comp;
439 }
440 
dso__data(struct dso * dso)441 static inline struct dso_data *dso__data(struct dso *dso)
442 {
443 	return &RC_CHK_ACCESS(dso)->data;
444 }
445 
dso__db_id(const struct dso * dso)446 static inline u64 dso__db_id(const struct dso *dso)
447 {
448 	return RC_CHK_ACCESS(dso)->db_id;
449 }
450 
dso__set_db_id(struct dso * dso,u64 db_id)451 static inline void dso__set_db_id(struct dso *dso, u64 db_id)
452 {
453 	RC_CHK_ACCESS(dso)->db_id = db_id;
454 }
455 
dso__dsos(struct dso * dso)456 static inline struct dsos *dso__dsos(struct dso *dso)
457 {
458 	return RC_CHK_ACCESS(dso)->dsos;
459 }
460 
dso__set_dsos(struct dso * dso,struct dsos * dsos)461 static inline void dso__set_dsos(struct dso *dso, struct dsos *dsos)
462 {
463 	RC_CHK_ACCESS(dso)->dsos = dsos;
464 }
465 
dso__header_build_id(struct dso * dso)466 static inline bool dso__header_build_id(struct dso *dso)
467 {
468 	return RC_CHK_ACCESS(dso)->header_build_id;
469 }
470 
dso__set_header_build_id(struct dso * dso,bool val)471 static inline void dso__set_header_build_id(struct dso *dso, bool val)
472 {
473 	RC_CHK_ACCESS(dso)->header_build_id = val;
474 }
475 
dso__hit(const struct dso * dso)476 static inline bool dso__hit(const struct dso *dso)
477 {
478 	return RC_CHK_ACCESS(dso)->hit;
479 }
480 
dso__set_hit(struct dso * dso)481 static inline void dso__set_hit(struct dso *dso)
482 {
483 	RC_CHK_ACCESS(dso)->hit = 1;
484 }
485 
dso__id(struct dso * dso)486 static inline struct dso_id *dso__id(struct dso *dso)
487 {
488 	return &RC_CHK_ACCESS(dso)->id;
489 }
490 
dso__id_const(const struct dso * dso)491 static inline const struct dso_id *dso__id_const(const struct dso *dso)
492 {
493 	return &RC_CHK_ACCESS(dso)->id;
494 }
495 
dso__bid(const struct dso * dso)496 static inline const struct build_id *dso__bid(const struct dso *dso)
497 {
498 	return &dso__id_const(dso)->build_id;
499 }
500 
dso__has_build_id(const struct dso * dso)501 static inline bool dso__has_build_id(const struct dso *dso)
502 {
503 	return build_id__is_defined(dso__bid(dso));
504 }
505 
dso__inlined_nodes(struct dso * dso)506 static inline struct rb_root_cached *dso__inlined_nodes(struct dso *dso)
507 {
508 	return &RC_CHK_ACCESS(dso)->inlined_nodes;
509 }
510 
dso__is_64_bit(const struct dso * dso)511 static inline bool dso__is_64_bit(const struct dso *dso)
512 {
513 	return RC_CHK_ACCESS(dso)->is_64_bit;
514 }
515 
dso__set_is_64_bit(struct dso * dso,bool is)516 static inline void dso__set_is_64_bit(struct dso *dso, bool is)
517 {
518 	RC_CHK_ACCESS(dso)->is_64_bit = is;
519 }
520 
dso__is_kmod(const struct dso * dso)521 static inline bool dso__is_kmod(const struct dso *dso)
522 {
523 	return RC_CHK_ACCESS(dso)->is_kmod;
524 }
525 
dso__set_is_kmod(struct dso * dso)526 static inline void dso__set_is_kmod(struct dso *dso)
527 {
528 	RC_CHK_ACCESS(dso)->is_kmod = 1;
529 }
530 
dso__kernel(const struct dso * dso)531 static inline enum dso_space_type dso__kernel(const struct dso *dso)
532 {
533 	return RC_CHK_ACCESS(dso)->kernel;
534 }
535 
dso__set_kernel(struct dso * dso,enum dso_space_type kernel)536 static inline void dso__set_kernel(struct dso *dso, enum dso_space_type kernel)
537 {
538 	RC_CHK_ACCESS(dso)->kernel = kernel;
539 }
540 
dso__last_find_result_addr(const struct dso * dso)541 static inline u64 dso__last_find_result_addr(const struct dso *dso)
542 {
543 	return RC_CHK_ACCESS(dso)->last_find_result.addr;
544 }
545 
dso__set_last_find_result_addr(struct dso * dso,u64 addr)546 static inline void dso__set_last_find_result_addr(struct dso *dso, u64 addr)
547 {
548 	RC_CHK_ACCESS(dso)->last_find_result.addr = addr;
549 }
550 
dso__last_find_result_symbol(const struct dso * dso)551 static inline struct symbol *dso__last_find_result_symbol(const struct dso *dso)
552 {
553 	return RC_CHK_ACCESS(dso)->last_find_result.symbol;
554 }
555 
dso__set_last_find_result_symbol(struct dso * dso,struct symbol * symbol)556 static inline void dso__set_last_find_result_symbol(struct dso *dso, struct symbol *symbol)
557 {
558 	RC_CHK_ACCESS(dso)->last_find_result.symbol = symbol;
559 }
560 
dso__load_errno(struct dso * dso)561 static inline enum dso_load_errno *dso__load_errno(struct dso *dso)
562 {
563 	return &RC_CHK_ACCESS(dso)->load_errno;
564 }
565 
dso__set_loaded(struct dso * dso)566 static inline void dso__set_loaded(struct dso *dso)
567 {
568 	RC_CHK_ACCESS(dso)->loaded = true;
569 }
570 
dso__lock(struct dso * dso)571 static inline struct mutex *dso__lock(struct dso *dso)
572 {
573 	return &RC_CHK_ACCESS(dso)->lock;
574 }
575 
dso__long_name(const struct dso * dso)576 static inline const char *dso__long_name(const struct dso *dso)
577 {
578 	return RC_CHK_ACCESS(dso)->long_name;
579 }
580 
dso__long_name_allocated(const struct dso * dso)581 static inline bool dso__long_name_allocated(const struct dso *dso)
582 {
583 	return RC_CHK_ACCESS(dso)->long_name_allocated;
584 }
585 
dso__set_long_name_allocated(struct dso * dso,bool allocated)586 static inline void dso__set_long_name_allocated(struct dso *dso, bool allocated)
587 {
588 	RC_CHK_ACCESS(dso)->long_name_allocated = allocated;
589 }
590 
dso__long_name_len(const struct dso * dso)591 static inline u16 dso__long_name_len(const struct dso *dso)
592 {
593 	return RC_CHK_ACCESS(dso)->long_name_len;
594 }
595 
dso__name(const struct dso * dso)596 static inline const char *dso__name(const struct dso *dso)
597 {
598 	return RC_CHK_ACCESS(dso)->name;
599 }
600 
dso__needs_swap(const struct dso * dso)601 static inline enum dso_swap_type dso__needs_swap(const struct dso *dso)
602 {
603 	return RC_CHK_ACCESS(dso)->needs_swap;
604 }
605 
dso__set_needs_swap(struct dso * dso,enum dso_swap_type type)606 static inline void dso__set_needs_swap(struct dso *dso, enum dso_swap_type type)
607 {
608 	RC_CHK_ACCESS(dso)->needs_swap = type;
609 }
610 
dso__nsinfo(struct dso * dso)611 static inline struct nsinfo *dso__nsinfo(struct dso *dso)
612 {
613 	return RC_CHK_ACCESS(dso)->nsinfo;
614 }
615 
dso__nsinfo_const(const struct dso * dso)616 static inline const struct nsinfo *dso__nsinfo_const(const struct dso *dso)
617 {
618 	return RC_CHK_ACCESS(dso)->nsinfo;
619 }
620 
dso__nsinfo_ptr(struct dso * dso)621 static inline struct nsinfo **dso__nsinfo_ptr(struct dso *dso)
622 {
623 	return &RC_CHK_ACCESS(dso)->nsinfo;
624 }
625 
626 void dso__set_nsinfo(struct dso *dso, struct nsinfo *nsi);
627 
dso__rel(const struct dso * dso)628 static inline u8 dso__rel(const struct dso *dso)
629 {
630 	return RC_CHK_ACCESS(dso)->rel;
631 }
632 
dso__set_rel(struct dso * dso,u8 rel)633 static inline void dso__set_rel(struct dso *dso, u8 rel)
634 {
635 	RC_CHK_ACCESS(dso)->rel = rel;
636 }
637 
dso__short_name(const struct dso * dso)638 static inline const char *dso__short_name(const struct dso *dso)
639 {
640 	return RC_CHK_ACCESS(dso)->short_name;
641 }
642 
dso__short_name_allocated(const struct dso * dso)643 static inline bool dso__short_name_allocated(const struct dso *dso)
644 {
645 	return RC_CHK_ACCESS(dso)->short_name_allocated;
646 }
647 
dso__set_short_name_allocated(struct dso * dso,bool allocated)648 static inline void dso__set_short_name_allocated(struct dso *dso, bool allocated)
649 {
650 	RC_CHK_ACCESS(dso)->short_name_allocated = allocated;
651 }
652 
dso__short_name_len(const struct dso * dso)653 static inline u16 dso__short_name_len(const struct dso *dso)
654 {
655 	return RC_CHK_ACCESS(dso)->short_name_len;
656 }
657 
dso__srclines(struct dso * dso)658 static inline struct rb_root_cached *dso__srclines(struct dso *dso)
659 {
660 	return &RC_CHK_ACCESS(dso)->srclines;
661 }
662 
dso__data_types(struct dso * dso)663 static inline struct rb_root *dso__data_types(struct dso *dso)
664 {
665 	return &RC_CHK_ACCESS(dso)->data_types;
666 }
667 
dso__global_vars(struct dso * dso)668 static inline struct rb_root *dso__global_vars(struct dso *dso)
669 {
670 	return &RC_CHK_ACCESS(dso)->global_vars;
671 }
672 
dso__symbols(struct dso * dso)673 static inline struct rb_root_cached *dso__symbols(struct dso *dso)
674 {
675 	return &RC_CHK_ACCESS(dso)->symbols;
676 }
677 
dso__symbol_names(struct dso * dso)678 static inline struct symbol **dso__symbol_names(struct dso *dso)
679 {
680 	return RC_CHK_ACCESS(dso)->symbol_names;
681 }
682 
dso__set_symbol_names(struct dso * dso,struct symbol ** names)683 static inline void dso__set_symbol_names(struct dso *dso, struct symbol **names)
684 {
685 	RC_CHK_ACCESS(dso)->symbol_names = names;
686 }
687 
dso__symbol_names_len(struct dso * dso)688 static inline size_t dso__symbol_names_len(struct dso *dso)
689 {
690 	return RC_CHK_ACCESS(dso)->symbol_names_len;
691 }
692 
dso__set_symbol_names_len(struct dso * dso,size_t len)693 static inline void dso__set_symbol_names_len(struct dso *dso, size_t len)
694 {
695 	RC_CHK_ACCESS(dso)->symbol_names_len = len;
696 }
697 
dso__symsrc_filename(const struct dso * dso)698 static inline const char *dso__symsrc_filename(const struct dso *dso)
699 {
700 	return RC_CHK_ACCESS(dso)->symsrc_filename;
701 }
702 
dso__set_symsrc_filename(struct dso * dso,char * val)703 static inline void dso__set_symsrc_filename(struct dso *dso, char *val)
704 {
705 	RC_CHK_ACCESS(dso)->symsrc_filename = val;
706 }
707 
dso__free_symsrc_filename(struct dso * dso)708 static inline void dso__free_symsrc_filename(struct dso *dso)
709 {
710 	zfree(&RC_CHK_ACCESS(dso)->symsrc_filename);
711 }
712 
dso__symtab_type(const struct dso * dso)713 static inline enum dso_binary_type dso__symtab_type(const struct dso *dso)
714 {
715 	return RC_CHK_ACCESS(dso)->symtab_type;
716 }
717 
dso__set_symtab_type(struct dso * dso,enum dso_binary_type bt)718 static inline void dso__set_symtab_type(struct dso *dso, enum dso_binary_type bt)
719 {
720 	RC_CHK_ACCESS(dso)->symtab_type = bt;
721 }
722 
dso__text_end(const struct dso * dso)723 static inline u64 dso__text_end(const struct dso *dso)
724 {
725 	return RC_CHK_ACCESS(dso)->text_end;
726 }
727 
dso__set_text_end(struct dso * dso,u64 val)728 static inline void dso__set_text_end(struct dso *dso, u64 val)
729 {
730 	RC_CHK_ACCESS(dso)->text_end = val;
731 }
732 
dso__text_offset(const struct dso * dso)733 static inline u64 dso__text_offset(const struct dso *dso)
734 {
735 	return RC_CHK_ACCESS(dso)->text_offset;
736 }
737 
dso__set_text_offset(struct dso * dso,u64 val)738 static inline void dso__set_text_offset(struct dso *dso, u64 val)
739 {
740 	RC_CHK_ACCESS(dso)->text_offset = val;
741 }
742 
743 struct dso *dso__new_id(const char *name, const struct dso_id *id);
744 struct dso *dso__new(const char *name);
745 void dso__delete(struct dso *dso);
746 
747 int dso__cmp_id(struct dso *a, struct dso *b);
748 void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated);
749 void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated);
750 void __dso__improve_id(struct dso *dso, const struct dso_id *id);
751 
752 int dso__name_len(const struct dso *dso);
753 
754 struct dso *dso__get(struct dso *dso);
755 void dso__put(struct dso *dso) LOCKS_EXCLUDED(_dso__data_open_lock);
756 
__dso__zput(struct dso ** dso)757 static inline void __dso__zput(struct dso **dso)
758 {
759 	dso__put(*dso);
760 	*dso = NULL;
761 }
762 
763 #define dso__zput(dso) __dso__zput(&dso)
764 
765 bool dso__loaded(const struct dso *dso);
766 
dso__has_symbols(const struct dso * dso)767 static inline bool dso__has_symbols(const struct dso *dso)
768 {
769 	return !RB_EMPTY_ROOT(&RC_CHK_ACCESS(dso)->symbols.rb_root);
770 }
771 
772 char *dso__filename_with_chroot(const struct dso *dso, const char *filename);
773 
774 bool dso__sorted_by_name(const struct dso *dso);
775 void dso__set_sorted_by_name(struct dso *dso);
776 void dso__sort_by_name(struct dso *dso);
777 
778 int dso__swap_init(struct dso *dso, unsigned char eidata);
779 
780 void dso__set_build_id(struct dso *dso, const struct build_id *bid);
781 bool dso__build_id_equal(const struct dso *dso, const struct build_id *bid);
782 void dso__read_running_kernel_build_id(struct dso *dso,
783 				       struct machine *machine);
784 int dso__kernel_module_get_build_id(struct dso *dso, const char *root_dir);
785 
786 char dso__symtab_origin(const struct dso *dso);
787 int dso__read_binary_type_filename(const struct dso *dso, enum dso_binary_type type,
788 				   const char *root_dir, char *filename, size_t size);
789 bool is_kernel_module(const char *pathname, int cpumode);
790 bool dso__needs_decompress(struct dso *dso);
791 int dso__decompress_kmodule_fd(struct dso *dso, const char *name);
792 int dso__decompress_kmodule_path(struct dso *dso, const char *name,
793 				 char *pathname, size_t len);
794 int filename__decompress(const char *name, char *pathname,
795 			 size_t len, int comp, int *err);
796 
797 #define KMOD_DECOMP_NAME  "/tmp/perf-kmod-XXXXXX"
798 #define KMOD_DECOMP_LEN   sizeof(KMOD_DECOMP_NAME)
799 
800 struct kmod_path {
801 	char *name;
802 	int   comp;
803 	bool  kmod;
804 };
805 
806 int __kmod_path__parse(struct kmod_path *m, const char *path,
807 		     bool alloc_name);
808 
809 #define kmod_path__parse(__m, __p)      __kmod_path__parse(__m, __p, false)
810 #define kmod_path__parse_name(__m, __p) __kmod_path__parse(__m, __p, true)
811 
812 void dso__set_module_info(struct dso *dso, struct kmod_path *m,
813 			  struct machine *machine);
814 
815 /*
816  * The dso__data_* external interface provides following functions:
817  *   dso__data_get_fd
818  *   dso__data_put_fd
819  *   dso__data_close
820  *   dso__data_size
821  *   dso__data_read_offset
822  *   dso__data_read_addr
823  *   dso__data_write_cache_offs
824  *   dso__data_write_cache_addr
825  *
826  * Please refer to the dso.c object code for each function and
827  * arguments documentation. Following text tries to explain the
828  * dso file descriptor caching.
829  *
830  * The dso__data* interface allows caching of opened file descriptors
831  * to speed up the dso data accesses. The idea is to leave the file
832  * descriptor opened ideally for the whole life of the dso object.
833  *
834  * The current usage of the dso__data_* interface is as follows:
835  *
836  * Get DSO's fd:
837  *   int fd;
838  *   if (dso__data_get_fd(dso, machine, &fd)) {
839  *       USE 'fd' SOMEHOW
840  *       dso__data_put_fd(dso);
841  *   }
842  *
843  * Read DSO's data:
844  *   n = dso__data_read_offset(dso_0, &machine, 0, buf, BUFSIZE);
845  *   n = dso__data_read_addr(dso_0, &machine, 0, buf, BUFSIZE);
846  *
847  * Eventually close DSO's fd:
848  *   dso__data_close(dso);
849  *
850  * It is not necessary to close the DSO object data file. Each time new
851  * DSO data file is opened, the limit (RLIMIT_NOFILE/2) is checked. Once
852  * it is crossed, the oldest opened DSO object is closed.
853  *
854  * The dso__delete function calls close_dso function to ensure the
855  * data file descriptor gets closed/unmapped before the dso object
856  * is freed.
857  *
858  * TODO
859 */
860 bool dso__data_get_fd(struct dso *dso, struct machine *machine, int *fd)
861 	EXCLUSIVE_TRYLOCK_FUNCTION(true, _dso__data_open_lock);
862 void dso__data_put_fd(struct dso *dso) UNLOCK_FUNCTION(_dso__data_open_lock);
863 void dso__data_close(struct dso *dso) LOCKS_EXCLUDED(_dso__data_open_lock);
864 
865 int dso__data_file_size(struct dso *dso, struct machine *machine);
866 off_t dso__data_size(struct dso *dso, struct machine *machine);
867 ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
868 			      u64 offset, u8 *data, ssize_t size);
869 uint16_t dso__read_e_machine(struct dso *optional_dso, int fd, uint32_t *e_flags);
870 uint16_t dso__e_machine(struct dso *dso, struct machine *machine, uint32_t *e_flags);
871 ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
872 			    struct machine *machine, u64 addr,
873 			    u8 *data, ssize_t size);
874 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by);
875 ssize_t dso__data_write_cache_offs(struct dso *dso, struct machine *machine,
876 				   u64 offset, const u8 *data, ssize_t size);
877 ssize_t dso__data_write_cache_addr(struct dso *dso, struct map *map,
878 				   struct machine *machine, u64 addr,
879 				   const u8 *data, ssize_t size);
880 
881 struct map *dso__new_map(const char *name);
882 struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
883 				    const char *short_name, int dso_type);
884 
885 void dso__reset_find_symbol_cache(struct dso *dso);
886 
887 size_t dso__fprintf_symbols_by_name(struct dso *dso, FILE *fp);
888 size_t dso__fprintf(struct dso *dso, FILE *fp);
889 
dso__binary_type(const struct dso * dso)890 static inline enum dso_binary_type dso__binary_type(const struct dso *dso)
891 {
892 	return RC_CHK_ACCESS(dso)->binary_type;
893 }
894 
dso__set_binary_type(struct dso * dso,enum dso_binary_type bt)895 static inline void dso__set_binary_type(struct dso *dso, enum dso_binary_type bt)
896 {
897 	RC_CHK_ACCESS(dso)->binary_type = bt;
898 }
899 
dso__is_vmlinux(const struct dso * dso)900 static inline bool dso__is_vmlinux(const struct dso *dso)
901 {
902 	enum dso_binary_type bt = dso__binary_type(dso);
903 
904 	return bt == DSO_BINARY_TYPE__VMLINUX || bt == DSO_BINARY_TYPE__GUEST_VMLINUX;
905 }
906 
dso__is_kcore(const struct dso * dso)907 static inline bool dso__is_kcore(const struct dso *dso)
908 {
909 	enum dso_binary_type bt = dso__binary_type(dso);
910 
911 	return bt == DSO_BINARY_TYPE__KCORE || bt == DSO_BINARY_TYPE__GUEST_KCORE;
912 }
913 
dso__is_kallsyms(const struct dso * dso)914 static inline bool dso__is_kallsyms(const struct dso *dso)
915 {
916 	enum dso_binary_type bt = dso__binary_type(dso);
917 
918 	return bt == DSO_BINARY_TYPE__KALLSYMS || bt == DSO_BINARY_TYPE__GUEST_KALLSYMS;
919 }
920 
921 bool dso__is_object_file(const struct dso *dso);
922 
923 void dso__free_a2l(struct dso *dso);
924 
925 enum dso_type dso__type(struct dso *dso, struct machine *machine);
926 
927 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen);
928 
929 void reset_fd_limit(void);
930 
931 u64 dso__find_global_type(struct dso *dso, u64 addr);
932 u64 dso__findnew_global_type(struct dso *dso, u64 addr, u64 offset);
933 
934 /* Check if dso name is of format "/tmp/perf-%d.map" */
935 bool perf_pid_map_tid(const char *dso_name, int *tid);
936 bool is_perf_pid_map_name(const char *dso_name);
937 
938 struct debuginfo *dso__debuginfo(struct dso *dso);
939 
940 const u8 *dso__read_symbol(struct dso *dso, const char *symfs_filename,
941 			   const struct map *map, const struct symbol *sym,
942 			   u8 **out_buf, u64 *out_buf_len, bool *is_64bit);
943 
944 #endif /* __PERF_DSO */
945