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 "mutex.h"
14
15struct machine;
16struct map;
17struct perf_env;
18
19#define DSO__NAME_KALLSYMS	"[kernel.kallsyms]"
20#define DSO__NAME_KCORE		"[kernel.kcore]"
21
22enum dso_binary_type {
23	DSO_BINARY_TYPE__KALLSYMS = 0,
24	DSO_BINARY_TYPE__GUEST_KALLSYMS,
25	DSO_BINARY_TYPE__VMLINUX,
26	DSO_BINARY_TYPE__GUEST_VMLINUX,
27	DSO_BINARY_TYPE__JAVA_JIT,
28	DSO_BINARY_TYPE__DEBUGLINK,
29	DSO_BINARY_TYPE__BUILD_ID_CACHE,
30	DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO,
31	DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
32	DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
33	DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO,
34	DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
35	DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
36	DSO_BINARY_TYPE__GUEST_KMODULE,
37	DSO_BINARY_TYPE__GUEST_KMODULE_COMP,
38	DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
39	DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP,
40	DSO_BINARY_TYPE__KCORE,
41	DSO_BINARY_TYPE__GUEST_KCORE,
42	DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
43	DSO_BINARY_TYPE__BPF_PROG_INFO,
44	DSO_BINARY_TYPE__BPF_IMAGE,
45	DSO_BINARY_TYPE__OOL,
46	DSO_BINARY_TYPE__NOT_FOUND,
47};
48
49enum dso_space_type {
50	DSO_SPACE__USER = 0,
51	DSO_SPACE__KERNEL,
52	DSO_SPACE__KERNEL_GUEST
53};
54
55enum dso_swap_type {
56	DSO_SWAP__UNSET,
57	DSO_SWAP__NO,
58	DSO_SWAP__YES,
59};
60
61enum dso_data_status {
62	DSO_DATA_STATUS_ERROR	= -1,
63	DSO_DATA_STATUS_UNKNOWN	= 0,
64	DSO_DATA_STATUS_OK	= 1,
65};
66
67enum dso_data_status_seen {
68	DSO_DATA_STATUS_SEEN_ITRACE,
69};
70
71enum dso_type {
72	DSO__TYPE_UNKNOWN,
73	DSO__TYPE_64BIT,
74	DSO__TYPE_32BIT,
75	DSO__TYPE_X32BIT,
76};
77
78enum dso_load_errno {
79	DSO_LOAD_ERRNO__SUCCESS		= 0,
80
81	/*
82	 * Choose an arbitrary negative big number not to clash with standard
83	 * errno since SUS requires the errno has distinct positive values.
84	 * See 'Issue 6' in the link below.
85	 *
86	 * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html
87	 */
88	__DSO_LOAD_ERRNO__START		= -10000,
89
90	DSO_LOAD_ERRNO__INTERNAL_ERROR	= __DSO_LOAD_ERRNO__START,
91
92	/* for symsrc__init() */
93	DSO_LOAD_ERRNO__INVALID_ELF,
94	DSO_LOAD_ERRNO__CANNOT_READ_BUILDID,
95	DSO_LOAD_ERRNO__MISMATCHING_BUILDID,
96
97	/* for decompress_kmodule */
98	DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE,
99
100	__DSO_LOAD_ERRNO__END,
101};
102
103#define DSO__SWAP(dso, type, val)			\
104({							\
105	type ____r = val;				\
106	BUG_ON(dso->needs_swap == DSO_SWAP__UNSET);	\
107	if (dso->needs_swap == DSO_SWAP__YES) {		\
108		switch (sizeof(____r)) {		\
109		case 2:					\
110			____r = bswap_16(val);		\
111			break;				\
112		case 4:					\
113			____r = bswap_32(val);		\
114			break;				\
115		case 8:					\
116			____r = bswap_64(val);		\
117			break;				\
118		default:				\
119			BUG_ON(1);			\
120		}					\
121	}						\
122	____r;						\
123})
124
125#define DSO__DATA_CACHE_SIZE 4096
126#define DSO__DATA_CACHE_MASK ~(DSO__DATA_CACHE_SIZE - 1)
127
128/*
129 * Data about backing storage DSO, comes from PERF_RECORD_MMAP2 meta events
130 */
131struct dso_id {
132	u32	maj;
133	u32	min;
134	u64	ino;
135	u64	ino_generation;
136};
137
138struct dso_cache {
139	struct rb_node	rb_node;
140	u64 offset;
141	u64 size;
142	char data[];
143};
144
145struct auxtrace_cache;
146
147struct dso {
148	struct mutex	 lock;
149	struct list_head node;
150	struct rb_node	 rb_node;	/* rbtree node sorted by long name */
151	struct rb_root	 *root;		/* root of rbtree that rb_node is in */
152	struct rb_root_cached symbols;
153	struct symbol	 **symbol_names;
154	size_t		 symbol_names_len;
155	struct rb_root_cached inlined_nodes;
156	struct rb_root_cached srclines;
157	struct rb_root	data_types;
158
159	struct {
160		u64		addr;
161		struct symbol	*symbol;
162	} last_find_result;
163	void		 *a2l;
164	char		 *symsrc_filename;
165	unsigned int	 a2l_fails;
166	enum dso_space_type	kernel;
167	bool			is_kmod;
168	enum dso_swap_type	needs_swap;
169	enum dso_binary_type	symtab_type;
170	enum dso_binary_type	binary_type;
171	enum dso_load_errno	load_errno;
172	u8		 adjust_symbols:1;
173	u8		 has_build_id:1;
174	u8		 header_build_id:1;
175	u8		 has_srcline:1;
176	u8		 hit:1;
177	u8		 annotate_warned:1;
178	u8		 auxtrace_warned:1;
179	u8		 short_name_allocated:1;
180	u8		 long_name_allocated:1;
181	u8		 is_64_bit:1;
182	bool		 sorted_by_name;
183	bool		 loaded;
184	u8		 rel;
185	struct build_id	 bid;
186	u64		 text_offset;
187	u64		 text_end;
188	const char	 *short_name;
189	const char	 *long_name;
190	u16		 long_name_len;
191	u16		 short_name_len;
192	void		*dwfl;			/* DWARF debug info */
193	struct auxtrace_cache *auxtrace_cache;
194	int		 comp;
195
196	/* dso data file */
197	struct {
198		struct rb_root	 cache;
199		int		 fd;
200		int		 status;
201		u32		 status_seen;
202		u64		 file_size;
203		struct list_head open_entry;
204		u64		 elf_base_addr;
205		u64		 debug_frame_offset;
206		u64		 eh_frame_hdr_addr;
207		u64		 eh_frame_hdr_offset;
208	} data;
209	/* bpf prog information */
210	struct {
211		u32		id;
212		u32		sub_id;
213		struct perf_env	*env;
214	} bpf_prog;
215
216	union { /* Tool specific area */
217		void	 *priv;
218		u64	 db_id;
219	};
220	struct nsinfo	*nsinfo;
221	struct dso_id	 id;
222	refcount_t	 refcnt;
223	char		 name[];
224};
225
226/* dso__for_each_symbol - iterate over the symbols of given type
227 *
228 * @dso: the 'struct dso *' in which symbols are iterated
229 * @pos: the 'struct symbol *' to use as a loop cursor
230 * @n: the 'struct rb_node *' to use as a temporary storage
231 */
232#define dso__for_each_symbol(dso, pos, n)	\
233	symbols__for_each_entry(&(dso)->symbols, pos, n)
234
235#define dsos__for_each_with_build_id(pos, head)	\
236	list_for_each_entry(pos, head, node)	\
237		if (!pos->has_build_id)		\
238			continue;		\
239		else
240
241static inline void dso__set_loaded(struct dso *dso)
242{
243	dso->loaded = true;
244}
245
246struct dso *dso__new_id(const char *name, struct dso_id *id);
247struct dso *dso__new(const char *name);
248void dso__delete(struct dso *dso);
249
250int dso__cmp_id(struct dso *a, struct dso *b);
251void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated);
252void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated);
253
254int dso__name_len(const struct dso *dso);
255
256struct dso *dso__get(struct dso *dso);
257void dso__put(struct dso *dso);
258
259static inline void __dso__zput(struct dso **dso)
260{
261	dso__put(*dso);
262	*dso = NULL;
263}
264
265#define dso__zput(dso) __dso__zput(&dso)
266
267bool dso__loaded(const struct dso *dso);
268
269static inline bool dso__has_symbols(const struct dso *dso)
270{
271	return !RB_EMPTY_ROOT(&dso->symbols.rb_root);
272}
273
274char *dso__filename_with_chroot(const struct dso *dso, const char *filename);
275
276bool dso__sorted_by_name(const struct dso *dso);
277void dso__set_sorted_by_name(struct dso *dso);
278void dso__sort_by_name(struct dso *dso);
279
280void dso__set_build_id(struct dso *dso, struct build_id *bid);
281bool dso__build_id_equal(const struct dso *dso, struct build_id *bid);
282void dso__read_running_kernel_build_id(struct dso *dso,
283				       struct machine *machine);
284int dso__kernel_module_get_build_id(struct dso *dso, const char *root_dir);
285
286char dso__symtab_origin(const struct dso *dso);
287int dso__read_binary_type_filename(const struct dso *dso, enum dso_binary_type type,
288				   char *root_dir, char *filename, size_t size);
289bool is_kernel_module(const char *pathname, int cpumode);
290bool dso__needs_decompress(struct dso *dso);
291int dso__decompress_kmodule_fd(struct dso *dso, const char *name);
292int dso__decompress_kmodule_path(struct dso *dso, const char *name,
293				 char *pathname, size_t len);
294int filename__decompress(const char *name, char *pathname,
295			 size_t len, int comp, int *err);
296
297#define KMOD_DECOMP_NAME  "/tmp/perf-kmod-XXXXXX"
298#define KMOD_DECOMP_LEN   sizeof(KMOD_DECOMP_NAME)
299
300struct kmod_path {
301	char *name;
302	int   comp;
303	bool  kmod;
304};
305
306int __kmod_path__parse(struct kmod_path *m, const char *path,
307		     bool alloc_name);
308
309#define kmod_path__parse(__m, __p)      __kmod_path__parse(__m, __p, false)
310#define kmod_path__parse_name(__m, __p) __kmod_path__parse(__m, __p, true)
311
312void dso__set_module_info(struct dso *dso, struct kmod_path *m,
313			  struct machine *machine);
314
315/*
316 * The dso__data_* external interface provides following functions:
317 *   dso__data_get_fd
318 *   dso__data_put_fd
319 *   dso__data_close
320 *   dso__data_size
321 *   dso__data_read_offset
322 *   dso__data_read_addr
323 *   dso__data_write_cache_offs
324 *   dso__data_write_cache_addr
325 *
326 * Please refer to the dso.c object code for each function and
327 * arguments documentation. Following text tries to explain the
328 * dso file descriptor caching.
329 *
330 * The dso__data* interface allows caching of opened file descriptors
331 * to speed up the dso data accesses. The idea is to leave the file
332 * descriptor opened ideally for the whole life of the dso object.
333 *
334 * The current usage of the dso__data_* interface is as follows:
335 *
336 * Get DSO's fd:
337 *   int fd = dso__data_get_fd(dso, machine);
338 *   if (fd >= 0) {
339 *       USE 'fd' SOMEHOW
340 *       dso__data_put_fd(dso);
341 *   }
342 *
343 * Read DSO's data:
344 *   n = dso__data_read_offset(dso_0, &machine, 0, buf, BUFSIZE);
345 *   n = dso__data_read_addr(dso_0, &machine, 0, buf, BUFSIZE);
346 *
347 * Eventually close DSO's fd:
348 *   dso__data_close(dso);
349 *
350 * It is not necessary to close the DSO object data file. Each time new
351 * DSO data file is opened, the limit (RLIMIT_NOFILE/2) is checked. Once
352 * it is crossed, the oldest opened DSO object is closed.
353 *
354 * The dso__delete function calls close_dso function to ensure the
355 * data file descriptor gets closed/unmapped before the dso object
356 * is freed.
357 *
358 * TODO
359*/
360int dso__data_get_fd(struct dso *dso, struct machine *machine);
361void dso__data_put_fd(struct dso *dso);
362void dso__data_close(struct dso *dso);
363
364int dso__data_file_size(struct dso *dso, struct machine *machine);
365off_t dso__data_size(struct dso *dso, struct machine *machine);
366ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
367			      u64 offset, u8 *data, ssize_t size);
368ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
369			    struct machine *machine, u64 addr,
370			    u8 *data, ssize_t size);
371bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by);
372ssize_t dso__data_write_cache_offs(struct dso *dso, struct machine *machine,
373				   u64 offset, const u8 *data, ssize_t size);
374ssize_t dso__data_write_cache_addr(struct dso *dso, struct map *map,
375				   struct machine *machine, u64 addr,
376				   const u8 *data, ssize_t size);
377
378struct map *dso__new_map(const char *name);
379struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
380				    const char *short_name, int dso_type);
381
382void dso__reset_find_symbol_cache(struct dso *dso);
383
384size_t dso__fprintf_symbols_by_name(struct dso *dso, FILE *fp);
385size_t dso__fprintf(struct dso *dso, FILE *fp);
386
387static inline bool dso__is_vmlinux(const struct dso *dso)
388{
389	return dso->binary_type == DSO_BINARY_TYPE__VMLINUX ||
390	       dso->binary_type == DSO_BINARY_TYPE__GUEST_VMLINUX;
391}
392
393static inline bool dso__is_kcore(const struct dso *dso)
394{
395	return dso->binary_type == DSO_BINARY_TYPE__KCORE ||
396	       dso->binary_type == DSO_BINARY_TYPE__GUEST_KCORE;
397}
398
399static inline bool dso__is_kallsyms(const struct dso *dso)
400{
401	return dso->kernel && dso->long_name[0] != '/';
402}
403
404bool dso__is_object_file(const struct dso *dso);
405
406void dso__free_a2l(struct dso *dso);
407
408enum dso_type dso__type(struct dso *dso, struct machine *machine);
409
410int dso__strerror_load(struct dso *dso, char *buf, size_t buflen);
411
412void reset_fd_limit(void);
413
414#endif /* __PERF_DSO */
415