xref: /linux/tools/perf/util/annotate-data.h (revision 76a411f90bd7bf8b109216fd0b72249b10f76a0c)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _PERF_ANNOTATE_DATA_H
3 #define _PERF_ANNOTATE_DATA_H
4 
5 #include <errno.h>
6 #include <linux/compiler.h>
7 #include <linux/rbtree.h>
8 #include <linux/types.h>
9 #include "dwarf-regs.h"
10 #include "annotate.h"
11 
12 #ifdef HAVE_LIBDW_SUPPORT
13 #include "debuginfo.h"
14 #endif
15 
16 struct annotated_op_loc;
17 struct debuginfo;
18 struct evsel;
19 struct hist_browser_timer;
20 struct hist_entry;
21 struct map_symbol;
22 struct thread;
23 
24 #define pr_debug_dtp(fmt, ...)					\
25 do {								\
26 	if (debug_type_profile)					\
27 		pr_info(fmt, ##__VA_ARGS__);			\
28 	else							\
29 		pr_debug3(fmt, ##__VA_ARGS__);			\
30 } while (0)
31 
32 enum type_state_kind {
33 	TSR_KIND_INVALID = 0,
34 	TSR_KIND_TYPE,
35 	TSR_KIND_PERCPU_BASE,
36 	TSR_KIND_CONST,
37 	TSR_KIND_PERCPU_POINTER,
38 	TSR_KIND_POINTER,
39 	TSR_KIND_CANARY,
40 	TSR_KIND_GLOBAL_ADDR,
41 };
42 
43 /**
44  * struct annotated_member - Type of member field
45  * @node: List entry in the parent list
46  * @children: List head for child nodes
47  * @type_name: Name of the member type
48  * @var_name: Name of the member variable
49  * @offset: Offset from the outer data type
50  * @size: Size of the member field
51  * @is_union: Whether it's an union type
52  * @is_flex_array: Whether it's a flex array
53  *
54  * This represents a member type in a data type.
55  */
56 struct annotated_member {
57 	struct list_head node;
58 	struct list_head children;
59 	char *type_name;
60 	char *var_name;
61 	int offset;
62 	int size;
63 	bool is_union;
64 	bool is_flex_array;
65 	/* Children not expanded because the nesting limit was reached */
66 	bool truncated;
67 	u8 depth;
68 };
69 
70 /**
71  * struct type_hist_entry - Histogram entry per offset
72  * @nr_samples: Number of samples
73  * @period: Count of event
74  */
75 struct type_hist_entry {
76 	int nr_samples;
77 	u64 period;
78 };
79 
80 /**
81  * struct type_hist - Type histogram for each event
82  * @nr_samples: Total number of samples in this data type
83  * @period: Total count of the event in this data type
84  * @samples: Hashmap of (offset, type_hist_entry)
85  */
86 struct type_hist {
87 	u64			nr_samples;
88 	u64			period;
89 	struct hashmap		samples;
90 };
91 
92 /**
93  * struct annotated_data_type - Data type to profile
94  * @node: RB-tree node for dso->type_tree
95  * @self: Actual type information
96  * @flex_array: Whether it has a flex array
97  * @nr_histogram: Number of histogram entries
98  * @histograms: An array of histograms
99  *
100  * This represents a data type accessed by samples in the profile data.
101  */
102 struct annotated_data_type {
103 	struct rb_node node;
104 	struct annotated_member self;
105 	bool flex_array;
106 	int nr_histograms;
107 	struct type_hist *histograms;
108 };
109 
110 extern struct annotated_data_type unknown_type;
111 extern struct annotated_data_type stackop_type;
112 extern struct annotated_data_type canary_type;
113 
114 /**
115  * struct data_loc_info - Data location information
116  * @arch: CPU architecture info
117  * @thread: Thread info
118  * @ms: Map and Symbol info
119  * @ip: Instruction address
120  * @var_addr: Data address (for global variables)
121  * @cpumode: CPU execution mode
122  * @op: Instruction operand location (regs and offset)
123  * @di: Debug info
124  * @fbreg: Frame base register
125  * @fb_cfa: Whether the frame needs to check CFA
126  * @type_offset: Final offset in the type
127  */
128 struct data_loc_info {
129 	/* These are input field, should be filled by caller */
130 	const struct arch *arch;
131 	struct thread *thread;
132 	struct map_symbol *ms;
133 	u64 ip;
134 	u64 var_addr;
135 	u8 cpumode;
136 	struct annotated_op_loc *op;
137 	struct debuginfo *di;
138 
139 	/* These are used internally */
140 	int fbreg;
141 	bool fb_cfa;
142 
143 	/* This is for the result */
144 	int type_offset;
145 };
146 
147 /**
148  * struct annotated_data_stat - Debug statistics
149  * @total: Total number of entry
150  * @no_sym: No symbol or map found
151  * @no_insn: Failed to get disasm line
152  * @no_insn_ops: The instruction has no operands
153  * @no_mem_ops: The instruction has no memory operands
154  * @no_reg: Failed to extract a register from the operand
155  * @no_dbginfo: The binary has no debug information
156  * @no_cuinfo: Failed to find a compile_unit
157  * @no_var: Failed to find a matching variable
158  * @no_typeinfo: Failed to get a type info for the variable
159  * @invalid_size: Failed to get a size info of the type
160  * @bad_offset: The access offset is out of the type
161  */
162 struct annotated_data_stat {
163 	int total;
164 	int no_sym;
165 	int no_insn;
166 	int no_insn_ops;
167 	int no_mem_ops;
168 	int no_reg;
169 	int no_dbginfo;
170 	int no_cuinfo;
171 	int no_var;
172 	int no_typeinfo;
173 	int invalid_size;
174 	int bad_offset;
175 	int insn_track;
176 };
177 extern struct annotated_data_stat ann_data_stat;
178 
179 #ifdef HAVE_LIBDW_SUPPORT
180 /*
181  * Type information in a register, valid when @ok is true.
182  * The @caller_saved registers are invalidated after a function call.
183  */
184 struct type_state_reg {
185 	Dwarf_Die type;
186 	u64 imm_value;
187 	/*
188 	 * The offset within the struct that the register points to.
189 	 * A value of 0 means the register points to the beginning.
190 	 * type_offset = op->offset + reg->offset
191 	 */
192 	s32 offset;
193 	bool ok;
194 	bool caller_saved;
195 	/* DWARF location range tracking for register lifetime */
196 	bool lifetime_active;
197 	u64 lifetime_end;
198 	u8 kind;
199 	u8 copied_from;
200 };
201 
202 /* Type information in a stack location, dynamically allocated */
203 struct type_state_stack {
204 	struct list_head list;
205 	Dwarf_Die type;
206 	int offset;
207 	/* pointer offset, saves tsr->offset on the stack state */
208 	int ptr_offset;
209 	int size;
210 	bool compound;
211 	u8 kind;
212 	u64 imm_value;
213 };
214 
215 /*
216  * Maximum number of registers tracked in type_state.
217  *
218  * This limit must cover all supported architectures, since perf
219  * may analyze perf.data files generated on systems with a different
220  * register set. Use 32 as a safe upper bound instead of relying on
221  * build-arch specific values.
222  */
223 #define TYPE_STATE_MAX_REGS  32
224 
225 /*
226  * State table to maintain type info in each register and stack location.
227  * It'll be updated when new variable is allocated or type info is moved
228  * to a new location (register or stack).  As it'd be used with the
229  * shortest path of basic blocks, it only maintains a single table.
230  */
231 struct type_state {
232 	/* state of general purpose registers */
233 	struct type_state_reg regs[TYPE_STATE_MAX_REGS];
234 	/* state of stack location */
235 	struct list_head stack_vars;
236 	/* return value register */
237 	int ret_reg;
238 	/* stack pointer register */
239 	int stack_reg;
240 };
241 
242 /* Returns data type at the location (ip, reg, offset) */
243 struct annotated_data_type *find_data_type(struct data_loc_info *dloc);
244 
245 /* Update type access histogram at the given offset */
246 int annotated_data_type__update_samples(struct annotated_data_type *adt,
247 					struct evsel *evsel, int offset,
248 					int nr_samples, u64 period);
249 
250 /* Release all data type information in the tree */
251 void annotated_data_type__tree_delete(struct rb_root *root);
252 
253 /* Release all global variable information in the tree */
254 void global_var_type__tree_delete(struct rb_root *root);
255 
256 /* Print data type annotation (including members) on stdout */
257 int hist_entry__annotate_data_tty(struct hist_entry *he, struct evsel *evsel);
258 
259 /* Get name of member field at the given offset in the data type */
260 int annotated_data_type__get_member_name(struct annotated_data_type *adt,
261 					 char *buf, size_t sz, int member_offset);
262 
263 bool has_reg_type(struct type_state *state, int reg);
264 struct type_state_stack *findnew_stack_state(struct type_state *state,
265 						int offset, u8 kind,
266 						Dwarf_Die *type_die,
267 						int ptr_offset, u64 imm_value);
268 void set_stack_state(struct type_state_stack *stack, int offset, u8 kind,
269 				Dwarf_Die *type_die, int ptr_offset, u64 imm_value);
270 struct type_state_stack *find_stack_state(struct type_state *state,
271 						int offset);
272 void delete_stack_state(struct type_state *state, int offset);
273 void invalidate_reg_state(struct type_state_reg *reg);
274 bool get_global_var_type(Dwarf_Die *cu_die, struct data_loc_info *dloc,
275 				u64 ip, u64 var_addr, int *var_offset,
276 				Dwarf_Die *type_die);
277 bool get_global_var_info(struct data_loc_info *dloc, u64 addr,
278 				const char **var_name, int *var_offset);
279 void pr_debug_type_name(Dwarf_Die *die, enum type_state_kind kind);
280 int arch_get_reg_offset(const struct arch *arch, struct annotated_op_loc *op_loc,
281 			int breg, struct type_state *state, bool apply_index,
282 			int *offset);
283 #else /* HAVE_LIBDW_SUPPORT */
284 
285 static inline struct annotated_data_type *
286 find_data_type(struct data_loc_info *dloc __maybe_unused)
287 {
288 	return NULL;
289 }
290 
291 static inline int
292 annotated_data_type__update_samples(struct annotated_data_type *adt __maybe_unused,
293 				    struct evsel *evsel __maybe_unused,
294 				    int offset __maybe_unused,
295 				    int nr_samples __maybe_unused,
296 				    u64 period __maybe_unused)
297 {
298 	return -1;
299 }
300 
301 static inline void annotated_data_type__tree_delete(struct rb_root *root __maybe_unused)
302 {
303 }
304 
305 static inline void global_var_type__tree_delete(struct rb_root *root __maybe_unused)
306 {
307 }
308 
309 static inline int hist_entry__annotate_data_tty(struct hist_entry *he __maybe_unused,
310 						struct evsel *evsel __maybe_unused)
311 {
312 	return -1;
313 }
314 
315 static inline int annotated_data_type__get_member_name(struct annotated_data_type *adt __maybe_unused,
316 						       char *buf __maybe_unused,
317 						       size_t sz __maybe_unused,
318 						       int member_offset __maybe_unused)
319 {
320 	return -1;
321 }
322 
323 #endif /* HAVE_LIBDW_SUPPORT */
324 
325 #ifdef HAVE_SLANG_SUPPORT
326 int hist_entry__annotate_data_tui(struct hist_entry *he, struct evsel *evsel,
327 				  struct hist_browser_timer *hbt);
328 #else
329 static inline int hist_entry__annotate_data_tui(struct hist_entry *he __maybe_unused,
330 						struct evsel *evsel __maybe_unused,
331 						struct hist_browser_timer *hbt __maybe_unused)
332 {
333 	return -1;
334 }
335 #endif /* HAVE_SLANG_SUPPORT */
336 
337 #endif /* _PERF_ANNOTATE_DATA_H */
338