xref: /linux/tools/perf/util/probe-finder.h (revision ba2290b1b7505b28912092a0976e071a447ee18c)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _PROBE_FINDER_H
3 #define _PROBE_FINDER_H
4 
5 #include <stdbool.h>
6 #include "intlist.h"
7 #include "build-id.h"
8 #include "probe-event.h"
9 #include <linux/ctype.h>
10 
11 #define MAX_PROBE_BUFFER	1024
12 #define MAX_PROBES		 128
13 #define MAX_PROBE_ARGS		 128
14 
15 #define PROBE_ARG_VARS		"$vars"
16 #define PROBE_ARG_PARAMS	"$params"
17 
18 static inline int is_c_varname(const char *name)
19 {
20 	/* TODO */
21 	return isalpha(name[0]) || name[0] == '_';
22 }
23 
24 #ifdef HAVE_DWARF_SUPPORT
25 
26 #include "dwarf-aux.h"
27 
28 /* TODO: export debuginfo data structure even if no dwarf support */
29 
30 /* debug information structure */
31 struct debuginfo {
32 	Dwarf		*dbg;
33 	Dwfl_Module	*mod;
34 	Dwfl		*dwfl;
35 	Dwarf_Addr	bias;
36 	const unsigned char	*build_id;
37 };
38 
39 /* This also tries to open distro debuginfo */
40 struct debuginfo *debuginfo__new(const char *path);
41 void debuginfo__delete(struct debuginfo *dbg);
42 
43 /* Find probe_trace_events specified by perf_probe_event from debuginfo */
44 int debuginfo__find_trace_events(struct debuginfo *dbg,
45 				 struct perf_probe_event *pev,
46 				 struct probe_trace_event **tevs);
47 
48 /* Find a perf_probe_point from debuginfo */
49 int debuginfo__find_probe_point(struct debuginfo *dbg, unsigned long addr,
50 				struct perf_probe_point *ppt);
51 
52 int debuginfo__get_text_offset(struct debuginfo *dbg, Dwarf_Addr *offs,
53 			       bool adjust_offset);
54 
55 /* Find a line range */
56 int debuginfo__find_line_range(struct debuginfo *dbg, struct line_range *lr);
57 
58 /* Find available variables */
59 int debuginfo__find_available_vars_at(struct debuginfo *dbg,
60 				      struct perf_probe_event *pev,
61 				      struct variable_list **vls);
62 
63 /* Find a src file from a DWARF tag path */
64 int find_source_path(const char *raw_path, const char *sbuild_id,
65 		     const char *comp_dir, char **new_path);
66 
67 struct probe_finder {
68 	struct perf_probe_event	*pev;		/* Target probe event */
69 	struct debuginfo	*dbg;
70 
71 	/* Callback when a probe point is found */
72 	int (*callback)(Dwarf_Die *sc_die, struct probe_finder *pf);
73 
74 	/* For function searching */
75 	int			lno;		/* Line number */
76 	Dwarf_Addr		addr;		/* Address */
77 	const char		*fname;		/* Real file name */
78 	Dwarf_Die		cu_die;		/* Current CU */
79 	Dwarf_Die		sp_die;
80 	struct intlist		*lcache;	/* Line cache for lazy match */
81 
82 	/* For variable searching */
83 #if _ELFUTILS_PREREQ(0, 142)
84 	/* Call Frame Information from .eh_frame */
85 	Dwarf_CFI		*cfi_eh;
86 	/* Call Frame Information from .debug_frame */
87 	Dwarf_CFI		*cfi_dbg;
88 #endif
89 	Dwarf_Op		*fb_ops;	/* Frame base attribute */
90 	unsigned int		machine;	/* Target machine arch */
91 	struct perf_probe_arg	*pvar;		/* Current target variable */
92 	struct probe_trace_arg	*tvar;		/* Current result variable */
93 	bool			skip_empty_arg;	/* Skip non-exist args */
94 };
95 
96 struct trace_event_finder {
97 	struct probe_finder	pf;
98 	Dwfl_Module		*mod;		/* For solving symbols */
99 	struct probe_trace_event *tevs;		/* Found trace events */
100 	int			ntevs;		/* Number of trace events */
101 	int			max_tevs;	/* Max number of trace events */
102 };
103 
104 struct available_var_finder {
105 	struct probe_finder	pf;
106 	Dwfl_Module		*mod;		/* For solving symbols */
107 	struct variable_list	*vls;		/* Found variable lists */
108 	int			nvls;		/* Number of variable lists */
109 	int			max_vls;	/* Max no. of variable lists */
110 	bool			child;		/* Search child scopes */
111 };
112 
113 struct line_finder {
114 	struct line_range	*lr;		/* Target line range */
115 
116 	const char		*fname;		/* File name */
117 	int			lno_s;		/* Start line number */
118 	int			lno_e;		/* End line number */
119 	Dwarf_Die		cu_die;		/* Current CU */
120 	Dwarf_Die		sp_die;
121 	int			found;
122 };
123 
124 #endif /* HAVE_DWARF_SUPPORT */
125 
126 #endif /*_PROBE_FINDER_H */
127