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