xref: /linux/tools/perf/util/annotate.h (revision 41fd3cacd29f47f6b9c6474b27c5b0513786c4e9)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_ANNOTATE_H
3 #define __PERF_ANNOTATE_H
4 
5 #include <stdbool.h>
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <linux/types.h>
9 #include <linux/list.h>
10 #include <linux/rbtree.h>
11 #include <asm/bug.h>
12 #include "symbol_conf.h"
13 #include "mutex.h"
14 #include "spark.h"
15 
16 struct hist_browser_timer;
17 struct hist_entry;
18 struct ins_ops;
19 struct map;
20 struct map_symbol;
21 struct addr_map_symbol;
22 struct option;
23 struct perf_sample;
24 struct evsel;
25 struct symbol;
26 
27 struct ins {
28 	const char     *name;
29 	struct ins_ops *ops;
30 };
31 
32 struct ins_operands {
33 	char	*raw;
34 	struct {
35 		char	*raw;
36 		char	*name;
37 		struct symbol *sym;
38 		u64	addr;
39 		s64	offset;
40 		bool	offset_avail;
41 		bool	outside;
42 		bool	multi_regs;
43 	} target;
44 	union {
45 		struct {
46 			char	*raw;
47 			char	*name;
48 			u64	addr;
49 			bool	multi_regs;
50 		} source;
51 		struct {
52 			struct ins	    ins;
53 			struct ins_operands *ops;
54 		} locked;
55 		struct {
56 			char	*raw_comment;
57 			char	*raw_func_start;
58 		} jump;
59 	};
60 };
61 
62 struct arch;
63 
64 struct ins_ops {
65 	void (*free)(struct ins_operands *ops);
66 	int (*parse)(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms);
67 	int (*scnprintf)(struct ins *ins, char *bf, size_t size,
68 			 struct ins_operands *ops, int max_ins_name);
69 };
70 
71 bool ins__is_jump(const struct ins *ins);
72 bool ins__is_call(const struct ins *ins);
73 bool ins__is_ret(const struct ins *ins);
74 bool ins__is_lock(const struct ins *ins);
75 int ins__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops, int max_ins_name);
76 bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2);
77 
78 #define ANNOTATION__IPC_WIDTH 6
79 #define ANNOTATION__CYCLES_WIDTH 6
80 #define ANNOTATION__MINMAX_CYCLES_WIDTH 19
81 #define ANNOTATION__AVG_IPC_WIDTH 36
82 #define ANNOTATION_DUMMY_LEN	256
83 
84 struct annotation_options {
85 	bool hide_src_code,
86 	     use_offset,
87 	     jump_arrows,
88 	     print_lines,
89 	     full_path,
90 	     show_linenr,
91 	     show_fileloc,
92 	     show_nr_jumps,
93 	     show_minmax_cycle,
94 	     show_asm_raw,
95 	     annotate_src,
96 	     full_addr;
97 	u8   offset_level;
98 	int  min_pcnt;
99 	int  max_lines;
100 	int  context;
101 	char *objdump_path;
102 	char *disassembler_style;
103 	const char *prefix;
104 	const char *prefix_strip;
105 	unsigned int percent_type;
106 };
107 
108 extern struct annotation_options annotate_opts;
109 
110 enum {
111 	ANNOTATION__OFFSET_JUMP_TARGETS = 1,
112 	ANNOTATION__OFFSET_CALL,
113 	ANNOTATION__MAX_OFFSET_LEVEL,
114 };
115 
116 #define ANNOTATION__MIN_OFFSET_LEVEL ANNOTATION__OFFSET_JUMP_TARGETS
117 
118 struct annotation;
119 
120 struct sym_hist_entry {
121 	u64		nr_samples;
122 	u64		period;
123 };
124 
125 enum {
126 	PERCENT_HITS_LOCAL,
127 	PERCENT_HITS_GLOBAL,
128 	PERCENT_PERIOD_LOCAL,
129 	PERCENT_PERIOD_GLOBAL,
130 	PERCENT_MAX,
131 };
132 
133 struct annotation_data {
134 	double			 percent[PERCENT_MAX];
135 	double			 percent_sum;
136 	struct sym_hist_entry	 he;
137 };
138 
139 struct cycles_info {
140 	float			 ipc;
141 	u64			 avg;
142 	u64			 max;
143 	u64			 min;
144 };
145 
146 struct annotation_line {
147 	struct list_head	 node;
148 	struct rb_node		 rb_node;
149 	s64			 offset;
150 	char			*line;
151 	int			 line_nr;
152 	char			*fileloc;
153 	char			*path;
154 	struct cycles_info	*cycles;
155 	int			 jump_sources;
156 	u32			 idx;
157 	int			 idx_asm;
158 	int			 data_nr;
159 	struct annotation_data	 data[];
160 };
161 
162 struct disasm_line {
163 	struct ins		 ins;
164 	struct ins_operands	 ops;
165 
166 	/* This needs to be at the end. */
167 	struct annotation_line	 al;
168 };
169 
170 static inline double annotation_data__percent(struct annotation_data *data,
171 					      unsigned int which)
172 {
173 	return which < PERCENT_MAX ? data->percent[which] : -1;
174 }
175 
176 static inline const char *percent_type_str(unsigned int type)
177 {
178 	static const char *str[PERCENT_MAX] = {
179 		"local hits",
180 		"global hits",
181 		"local period",
182 		"global period",
183 	};
184 
185 	if (WARN_ON(type >= PERCENT_MAX))
186 		return "N/A";
187 
188 	return str[type];
189 }
190 
191 static inline struct disasm_line *disasm_line(struct annotation_line *al)
192 {
193 	return al ? container_of(al, struct disasm_line, al) : NULL;
194 }
195 
196 /*
197  * Is this offset in the same function as the line it is used?
198  * asm functions jump to other functions, for instance.
199  */
200 static inline bool disasm_line__has_local_offset(const struct disasm_line *dl)
201 {
202 	return dl->ops.target.offset_avail && !dl->ops.target.outside;
203 }
204 
205 /*
206  * Can we draw an arrow from the jump to its target, for instance? I.e.
207  * is the jump and its target in the same function?
208  */
209 bool disasm_line__is_valid_local_jump(struct disasm_line *dl, struct symbol *sym);
210 
211 void disasm_line__free(struct disasm_line *dl);
212 struct annotation_line *
213 annotation_line__next(struct annotation_line *pos, struct list_head *head);
214 
215 struct annotation_write_ops {
216 	bool first_line, current_entry, change_color;
217 	int  width;
218 	void *obj;
219 	int  (*set_color)(void *obj, int color);
220 	void (*set_percent_color)(void *obj, double percent, bool current);
221 	int  (*set_jumps_percent_color)(void *obj, int nr, bool current);
222 	void (*printf)(void *obj, const char *fmt, ...);
223 	void (*write_graph)(void *obj, int graph);
224 };
225 
226 void annotation_line__write(struct annotation_line *al, struct annotation *notes,
227 			    struct annotation_write_ops *ops);
228 
229 int __annotation__scnprintf_samples_period(struct annotation *notes,
230 					   char *bf, size_t size,
231 					   struct evsel *evsel,
232 					   bool show_freq);
233 
234 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw, int max_ins_name);
235 size_t disasm__fprintf(struct list_head *head, FILE *fp);
236 void symbol__calc_percent(struct symbol *sym, struct evsel *evsel);
237 
238 struct sym_hist {
239 	u64		      nr_samples;
240 	u64		      period;
241 	struct sym_hist_entry addr[];
242 };
243 
244 struct cyc_hist {
245 	u64	start;
246 	u64	cycles;
247 	u64	cycles_aggr;
248 	u64	cycles_max;
249 	u64	cycles_min;
250 	s64	cycles_spark[NUM_SPARKS];
251 	u32	num;
252 	u32	num_aggr;
253 	u8	have_start;
254 	/* 1 byte padding */
255 	u16	reset;
256 };
257 
258 /** struct annotated_source - symbols with hits have this attached as in sannotation
259  *
260  * @histograms: Array of addr hit histograms per event being monitored
261  * nr_histograms: This may not be the same as evsel->evlist->core.nr_entries if
262  * 		  we have more than a group in a evlist, where we will want
263  * 		  to see each group separately, that is why symbol__annotate2()
264  * 		  sets src->nr_histograms to evsel->nr_members.
265  * @lines: If 'print_lines' is specified, per source code line percentages
266  * @source: source parsed from a disassembler like objdump -dS
267  * @cyc_hist: Average cycles per basic block
268  *
269  * lines is allocated, percentages calculated and all sorted by percentage
270  * when the annotation is about to be presented, so the percentages are for
271  * one of the entries in the histogram array, i.e. for the event/counter being
272  * presented. It is deallocated right after symbol__{tui,tty,etc}_annotate
273  * returns.
274  */
275 struct annotated_source {
276 	struct list_head	source;
277 	size_t			sizeof_sym_hist;
278 	struct sym_hist		*histograms;
279 	struct annotation_line	**offsets;
280 	int    			nr_histograms;
281 	int			nr_entries;
282 	int			nr_asm_entries;
283 	u16			max_line_len;
284 };
285 
286 struct annotated_branch {
287 	u64			hit_cycles;
288 	u64			hit_insn;
289 	unsigned int		total_insn;
290 	unsigned int		cover_insn;
291 	struct cyc_hist		*cycles_hist;
292 	u64			max_coverage;
293 };
294 
295 struct LOCKABLE annotation {
296 	u64			start;
297 	struct annotation_options *options;
298 	int			nr_events;
299 	int			max_jump_sources;
300 	struct {
301 		u8		addr;
302 		u8		jumps;
303 		u8		target;
304 		u8		min_addr;
305 		u8		max_addr;
306 		u8		max_ins_name;
307 	} widths;
308 	struct annotated_source *src;
309 	struct annotated_branch *branch;
310 };
311 
312 static inline void annotation__init(struct annotation *notes __maybe_unused)
313 {
314 }
315 void annotation__exit(struct annotation *notes);
316 
317 void annotation__lock(struct annotation *notes) EXCLUSIVE_LOCK_FUNCTION(*notes);
318 void annotation__unlock(struct annotation *notes) UNLOCK_FUNCTION(*notes);
319 bool annotation__trylock(struct annotation *notes) EXCLUSIVE_TRYLOCK_FUNCTION(true, *notes);
320 
321 static inline int annotation__cycles_width(struct annotation *notes)
322 {
323 	if (notes->branch && notes->options->show_minmax_cycle)
324 		return ANNOTATION__IPC_WIDTH + ANNOTATION__MINMAX_CYCLES_WIDTH;
325 
326 	return notes->branch ? ANNOTATION__IPC_WIDTH + ANNOTATION__CYCLES_WIDTH : 0;
327 }
328 
329 static inline int annotation__pcnt_width(struct annotation *notes)
330 {
331 	return (symbol_conf.show_total_period ? 12 : 7) * notes->nr_events;
332 }
333 
334 static inline bool annotation_line__filter(struct annotation_line *al, struct annotation *notes)
335 {
336 	return notes->options->hide_src_code && al->offset == -1;
337 }
338 
339 void annotation__set_offsets(struct annotation *notes, s64 size);
340 void annotation__mark_jump_targets(struct annotation *notes, struct symbol *sym);
341 void annotation__update_column_widths(struct annotation *notes);
342 void annotation__init_column_widths(struct annotation *notes, struct symbol *sym);
343 void annotation__toggle_full_addr(struct annotation *notes, struct map_symbol *ms);
344 
345 static inline struct sym_hist *annotated_source__histogram(struct annotated_source *src, int idx)
346 {
347 	return ((void *)src->histograms) + (src->sizeof_sym_hist * idx);
348 }
349 
350 static inline struct sym_hist *annotation__histogram(struct annotation *notes, int idx)
351 {
352 	return annotated_source__histogram(notes->src, idx);
353 }
354 
355 static inline struct annotation *symbol__annotation(struct symbol *sym)
356 {
357 	return (void *)sym - symbol_conf.priv_size;
358 }
359 
360 int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample,
361 				 struct evsel *evsel);
362 
363 struct annotated_branch *annotation__get_branch(struct annotation *notes);
364 
365 int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
366 				    struct addr_map_symbol *start,
367 				    unsigned cycles);
368 
369 int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample,
370 				 struct evsel *evsel, u64 addr);
371 
372 struct annotated_source *symbol__hists(struct symbol *sym, int nr_hists);
373 void symbol__annotate_zero_histograms(struct symbol *sym);
374 
375 int symbol__annotate(struct map_symbol *ms,
376 		     struct evsel *evsel,
377 		     struct arch **parch);
378 int symbol__annotate2(struct map_symbol *ms,
379 		      struct evsel *evsel,
380 		      struct arch **parch);
381 
382 enum symbol_disassemble_errno {
383 	SYMBOL_ANNOTATE_ERRNO__SUCCESS		= 0,
384 
385 	/*
386 	 * Choose an arbitrary negative big number not to clash with standard
387 	 * errno since SUS requires the errno has distinct positive values.
388 	 * See 'Issue 6' in the link below.
389 	 *
390 	 * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html
391 	 */
392 	__SYMBOL_ANNOTATE_ERRNO__START		= -10000,
393 
394 	SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX	= __SYMBOL_ANNOTATE_ERRNO__START,
395 	SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF,
396 	SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING,
397 	SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP,
398 	SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE,
399 	SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF,
400 
401 	__SYMBOL_ANNOTATE_ERRNO__END,
402 };
403 
404 int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *buf, size_t buflen);
405 
406 int symbol__annotate_printf(struct map_symbol *ms, struct evsel *evsel);
407 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx);
408 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx);
409 void annotated_source__purge(struct annotated_source *as);
410 
411 int map_symbol__annotation_dump(struct map_symbol *ms, struct evsel *evsel);
412 
413 bool ui__has_annotation(void);
414 
415 int symbol__tty_annotate(struct map_symbol *ms, struct evsel *evsel);
416 
417 int symbol__tty_annotate2(struct map_symbol *ms, struct evsel *evsel);
418 
419 #ifdef HAVE_SLANG_SUPPORT
420 int symbol__tui_annotate(struct map_symbol *ms, struct evsel *evsel,
421 			 struct hist_browser_timer *hbt,
422 			 struct annotation_options *opts);
423 #else
424 static inline int symbol__tui_annotate(struct map_symbol *ms __maybe_unused,
425 				struct evsel *evsel  __maybe_unused,
426 				struct hist_browser_timer *hbt __maybe_unused,
427 				struct annotation_options *opts __maybe_unused)
428 {
429 	return 0;
430 }
431 #endif
432 
433 void annotation_options__init(struct annotation_options *opt);
434 void annotation_options__exit(struct annotation_options *opt);
435 
436 void annotation_config__init(struct annotation_options *opt);
437 
438 int annotate_parse_percent_type(const struct option *opt, const char *_str,
439 				int unset);
440 
441 int annotate_check_args(struct annotation_options *args);
442 
443 #endif	/* __PERF_ANNOTATE_H */
444