xref: /linux/tools/perf/util/annotate.h (revision a23e1966932464e1c5226cb9ac4ce1d5fc10ba22)
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 #include "hashmap.h"
16 
17 struct hist_browser_timer;
18 struct hist_entry;
19 struct ins_ops;
20 struct map;
21 struct map_symbol;
22 struct addr_map_symbol;
23 struct option;
24 struct perf_sample;
25 struct evsel;
26 struct symbol;
27 struct annotated_data_type;
28 
29 struct ins {
30 	const char     *name;
31 	struct ins_ops *ops;
32 };
33 
34 struct ins_operands {
35 	char	*raw;
36 	struct {
37 		char	*raw;
38 		char	*name;
39 		struct symbol *sym;
40 		u64	addr;
41 		s64	offset;
42 		bool	offset_avail;
43 		bool	outside;
44 		bool	multi_regs;
45 	} target;
46 	union {
47 		struct {
48 			char	*raw;
49 			char	*name;
50 			u64	addr;
51 			bool	multi_regs;
52 		} source;
53 		struct {
54 			struct ins	    ins;
55 			struct ins_operands *ops;
56 		} locked;
57 		struct {
58 			char	*raw_comment;
59 			char	*raw_func_start;
60 		} jump;
61 	};
62 };
63 
64 struct arch;
65 
66 bool arch__is(struct arch *arch, const char *name);
67 
68 struct ins_ops {
69 	void (*free)(struct ins_operands *ops);
70 	int (*parse)(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms);
71 	int (*scnprintf)(struct ins *ins, char *bf, size_t size,
72 			 struct ins_operands *ops, int max_ins_name);
73 };
74 
75 bool ins__is_jump(const struct ins *ins);
76 bool ins__is_call(const struct ins *ins);
77 bool ins__is_ret(const struct ins *ins);
78 bool ins__is_lock(const struct ins *ins);
79 int ins__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops, int max_ins_name);
80 bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2);
81 
82 #define ANNOTATION__IPC_WIDTH 6
83 #define ANNOTATION__CYCLES_WIDTH 6
84 #define ANNOTATION__MINMAX_CYCLES_WIDTH 19
85 #define ANNOTATION__AVG_IPC_WIDTH 36
86 #define ANNOTATION_DUMMY_LEN	256
87 
88 struct annotation_options {
89 	bool hide_src_code,
90 	     use_offset,
91 	     jump_arrows,
92 	     print_lines,
93 	     full_path,
94 	     show_linenr,
95 	     show_fileloc,
96 	     show_nr_jumps,
97 	     show_minmax_cycle,
98 	     show_asm_raw,
99 	     annotate_src,
100 	     full_addr;
101 	u8   offset_level;
102 	int  min_pcnt;
103 	int  max_lines;
104 	int  context;
105 	char *objdump_path;
106 	char *disassembler_style;
107 	const char *prefix;
108 	const char *prefix_strip;
109 	unsigned int percent_type;
110 };
111 
112 extern struct annotation_options annotate_opts;
113 
114 enum {
115 	ANNOTATION__OFFSET_JUMP_TARGETS = 1,
116 	ANNOTATION__OFFSET_CALL,
117 	ANNOTATION__MAX_OFFSET_LEVEL,
118 };
119 
120 #define ANNOTATION__MIN_OFFSET_LEVEL ANNOTATION__OFFSET_JUMP_TARGETS
121 
122 struct annotation;
123 
124 struct sym_hist_entry {
125 	u64		nr_samples;
126 	u64		period;
127 };
128 
129 enum {
130 	PERCENT_HITS_LOCAL,
131 	PERCENT_HITS_GLOBAL,
132 	PERCENT_PERIOD_LOCAL,
133 	PERCENT_PERIOD_GLOBAL,
134 	PERCENT_MAX,
135 };
136 
137 struct annotation_data {
138 	double			 percent[PERCENT_MAX];
139 	double			 percent_sum;
140 	struct sym_hist_entry	 he;
141 };
142 
143 struct cycles_info {
144 	float			 ipc;
145 	u64			 avg;
146 	u64			 max;
147 	u64			 min;
148 };
149 
150 struct annotation_line {
151 	struct list_head	 node;
152 	struct rb_node		 rb_node;
153 	s64			 offset;
154 	char			*line;
155 	int			 line_nr;
156 	char			*fileloc;
157 	char			*path;
158 	struct cycles_info	*cycles;
159 	int			 jump_sources;
160 	u32			 idx;
161 	int			 idx_asm;
162 	int			 data_nr;
163 	struct annotation_data	 data[];
164 };
165 
166 struct disasm_line {
167 	struct ins		 ins;
168 	struct ins_operands	 ops;
169 
170 	/* This needs to be at the end. */
171 	struct annotation_line	 al;
172 };
173 
174 static inline double annotation_data__percent(struct annotation_data *data,
175 					      unsigned int which)
176 {
177 	return which < PERCENT_MAX ? data->percent[which] : -1;
178 }
179 
180 static inline const char *percent_type_str(unsigned int type)
181 {
182 	static const char *str[PERCENT_MAX] = {
183 		"local hits",
184 		"global hits",
185 		"local period",
186 		"global period",
187 	};
188 
189 	if (WARN_ON(type >= PERCENT_MAX))
190 		return "N/A";
191 
192 	return str[type];
193 }
194 
195 static inline struct disasm_line *disasm_line(struct annotation_line *al)
196 {
197 	return al ? container_of(al, struct disasm_line, al) : NULL;
198 }
199 
200 /*
201  * Is this offset in the same function as the line it is used?
202  * asm functions jump to other functions, for instance.
203  */
204 static inline bool disasm_line__has_local_offset(const struct disasm_line *dl)
205 {
206 	return dl->ops.target.offset_avail && !dl->ops.target.outside;
207 }
208 
209 /*
210  * Can we draw an arrow from the jump to its target, for instance? I.e.
211  * is the jump and its target in the same function?
212  */
213 bool disasm_line__is_valid_local_jump(struct disasm_line *dl, struct symbol *sym);
214 
215 void disasm_line__free(struct disasm_line *dl);
216 struct annotation_line *
217 annotation_line__next(struct annotation_line *pos, struct list_head *head);
218 
219 struct annotation_write_ops {
220 	bool first_line, current_entry, change_color;
221 	int  width;
222 	void *obj;
223 	int  (*set_color)(void *obj, int color);
224 	void (*set_percent_color)(void *obj, double percent, bool current);
225 	int  (*set_jumps_percent_color)(void *obj, int nr, bool current);
226 	void (*printf)(void *obj, const char *fmt, ...);
227 	void (*write_graph)(void *obj, int graph);
228 };
229 
230 void annotation_line__write(struct annotation_line *al, struct annotation *notes,
231 			    struct annotation_write_ops *ops);
232 
233 int __annotation__scnprintf_samples_period(struct annotation *notes,
234 					   char *bf, size_t size,
235 					   struct evsel *evsel,
236 					   bool show_freq);
237 
238 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw, int max_ins_name);
239 size_t disasm__fprintf(struct list_head *head, FILE *fp);
240 void symbol__calc_percent(struct symbol *sym, struct evsel *evsel);
241 
242 /**
243  * struct sym_hist - symbol histogram information for an event
244  *
245  * @nr_samples: Total number of samples.
246  * @period: Sum of sample periods.
247  */
248 struct sym_hist {
249 	u64		      nr_samples;
250 	u64		      period;
251 };
252 
253 /**
254  * struct cyc_hist - (CPU) cycle histogram for a basic block
255  *
256  * @start: Start address of current block (if known).
257  * @cycles: Sum of cycles for the longest basic block.
258  * @cycles_aggr: Total cycles for this address.
259  * @cycles_max: Max cycles for this address.
260  * @cycles_min: Min cycles for this address.
261  * @cycles_spark: History of cycles for the longest basic block.
262  * @num: Number of samples for the longest basic block.
263  * @num_aggr: Total number of samples for this address.
264  * @have_start: Whether the current branch info has a start address.
265  * @reset: Number of resets due to a different start address.
266  *
267  * If sample has branch_stack and cycles info, it can construct basic blocks
268  * between two adjacent branches.  It'd have start and end addresses but
269  * sometimes the start address may not be available.  So the cycles are
270  * accounted at the end address.  If multiple basic blocks end at the same
271  * address, it will take the longest one.
272  *
273  * The @start, @cycles, @cycles_spark and @num fields are used for the longest
274  * block only.  Other fields are used for all cases.
275  *
276  * See __symbol__account_cycles().
277  */
278 struct cyc_hist {
279 	u64	start;
280 	u64	cycles;
281 	u64	cycles_aggr;
282 	u64	cycles_max;
283 	u64	cycles_min;
284 	s64	cycles_spark[NUM_SPARKS];
285 	u32	num;
286 	u32	num_aggr;
287 	u8	have_start;
288 	/* 1 byte padding */
289 	u16	reset;
290 };
291 
292 /**
293  * struct annotated_source - symbols with hits have this attached as in annotation
294  *
295  * @source: List head for annotated_line (embeded in disasm_line).
296  * @histograms: Array of symbol histograms per event to maintain the total number
297  * 		of samples and period.
298  * @nr_histograms: This may not be the same as evsel->evlist->core.nr_entries if
299  * 		  we have more than a group in a evlist, where we will want
300  * 		  to see each group separately, that is why symbol__annotate2()
301  * 		  sets src->nr_histograms to evsel->nr_members.
302  * @offsets: Array of annotation_line to be accessed by offset.
303  * @samples: Hash map of sym_hist_entry.  Keyed by event index and offset in symbol.
304  * @nr_entries: Number of annotated_line in the source list.
305  * @nr_asm_entries: Number of annotated_line with actual asm instruction in the
306  * 		    source list.
307  * @max_line_len: Maximum length of objdump output in an annotated_line.
308  *
309  * disasm_lines are allocated, percentages calculated and all sorted by percentage
310  * when the annotation is about to be presented, so the percentages are for
311  * one of the entries in the histogram array, i.e. for the event/counter being
312  * presented. It is deallocated right after symbol__{tui,tty,etc}_annotate
313  * returns.
314  */
315 struct annotated_source {
316 	struct list_head	source;
317 	struct sym_hist		*histograms;
318 	struct annotation_line	**offsets;
319 	struct hashmap	   	*samples;
320 	int    			nr_histograms;
321 	int			nr_entries;
322 	int			nr_asm_entries;
323 	u16			max_line_len;
324 };
325 
326 /**
327  * struct annotated_branch - basic block and IPC information for a symbol.
328  *
329  * @hit_cycles: Total executed cycles.
330  * @hit_insn: Total number of instructions executed.
331  * @total_insn: Number of instructions in the function.
332  * @cover_insn: Number of distinct, actually executed instructions.
333  * @cycles_hist: Array of cyc_hist for each instruction.
334  * @max_coverage: Maximum number of covered basic block (used for block-range).
335  *
336  * This struct is used by two different codes when the sample has branch stack
337  * and cycles information.  annotation__compute_ipc() calculates average IPC
338  * using @hit_insn / @hit_cycles.  The actual coverage can be calculated using
339  * @cover_insn / @total_insn.  The @cycles_hist can give IPC for each (longest)
340  * basic block ends at the given address.
341  * process_basic_block() calculates coverage of instructions (or basic blocks)
342  * in the function.
343  */
344 struct annotated_branch {
345 	u64			hit_cycles;
346 	u64			hit_insn;
347 	unsigned int		total_insn;
348 	unsigned int		cover_insn;
349 	struct cyc_hist		*cycles_hist;
350 	u64			max_coverage;
351 };
352 
353 struct LOCKABLE annotation {
354 	u64			start;
355 	int			nr_events;
356 	int			max_jump_sources;
357 	struct {
358 		u8		addr;
359 		u8		jumps;
360 		u8		target;
361 		u8		min_addr;
362 		u8		max_addr;
363 		u8		max_ins_name;
364 	} widths;
365 	struct annotated_source *src;
366 	struct annotated_branch *branch;
367 };
368 
369 static inline void annotation__init(struct annotation *notes __maybe_unused)
370 {
371 }
372 void annotation__exit(struct annotation *notes);
373 
374 void annotation__lock(struct annotation *notes) EXCLUSIVE_LOCK_FUNCTION(*notes);
375 void annotation__unlock(struct annotation *notes) UNLOCK_FUNCTION(*notes);
376 bool annotation__trylock(struct annotation *notes) EXCLUSIVE_TRYLOCK_FUNCTION(true, *notes);
377 
378 static inline int annotation__cycles_width(struct annotation *notes)
379 {
380 	if (notes->branch && annotate_opts.show_minmax_cycle)
381 		return ANNOTATION__IPC_WIDTH + ANNOTATION__MINMAX_CYCLES_WIDTH;
382 
383 	return notes->branch ? ANNOTATION__IPC_WIDTH + ANNOTATION__CYCLES_WIDTH : 0;
384 }
385 
386 static inline int annotation__pcnt_width(struct annotation *notes)
387 {
388 	return (symbol_conf.show_total_period ? 12 : 7) * notes->nr_events;
389 }
390 
391 static inline bool annotation_line__filter(struct annotation_line *al)
392 {
393 	return annotate_opts.hide_src_code && al->offset == -1;
394 }
395 
396 void annotation__set_offsets(struct annotation *notes, s64 size);
397 void annotation__mark_jump_targets(struct annotation *notes, struct symbol *sym);
398 void annotation__update_column_widths(struct annotation *notes);
399 void annotation__init_column_widths(struct annotation *notes, struct symbol *sym);
400 void annotation__toggle_full_addr(struct annotation *notes, struct map_symbol *ms);
401 
402 static inline struct sym_hist *annotated_source__histogram(struct annotated_source *src, int idx)
403 {
404 	return &src->histograms[idx];
405 }
406 
407 static inline struct sym_hist *annotation__histogram(struct annotation *notes, int idx)
408 {
409 	return annotated_source__histogram(notes->src, idx);
410 }
411 
412 static inline struct sym_hist_entry *
413 annotated_source__hist_entry(struct annotated_source *src, int idx, u64 offset)
414 {
415 	struct sym_hist_entry *entry;
416 	long key = offset << 16 | idx;
417 
418 	if (!hashmap__find(src->samples, key, &entry))
419 		return NULL;
420 	return entry;
421 }
422 
423 static inline struct annotation *symbol__annotation(struct symbol *sym)
424 {
425 	return (void *)sym - symbol_conf.priv_size;
426 }
427 
428 int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample,
429 				 struct evsel *evsel);
430 
431 struct annotated_branch *annotation__get_branch(struct annotation *notes);
432 
433 int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
434 				    struct addr_map_symbol *start,
435 				    unsigned cycles);
436 
437 int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample,
438 				 struct evsel *evsel, u64 addr);
439 
440 struct annotated_source *symbol__hists(struct symbol *sym, int nr_hists);
441 void symbol__annotate_zero_histograms(struct symbol *sym);
442 
443 int symbol__annotate(struct map_symbol *ms,
444 		     struct evsel *evsel,
445 		     struct arch **parch);
446 int symbol__annotate2(struct map_symbol *ms,
447 		      struct evsel *evsel,
448 		      struct arch **parch);
449 
450 enum symbol_disassemble_errno {
451 	SYMBOL_ANNOTATE_ERRNO__SUCCESS		= 0,
452 
453 	/*
454 	 * Choose an arbitrary negative big number not to clash with standard
455 	 * errno since SUS requires the errno has distinct positive values.
456 	 * See 'Issue 6' in the link below.
457 	 *
458 	 * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html
459 	 */
460 	__SYMBOL_ANNOTATE_ERRNO__START		= -10000,
461 
462 	SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX	= __SYMBOL_ANNOTATE_ERRNO__START,
463 	SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF,
464 	SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING,
465 	SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP,
466 	SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE,
467 	SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF,
468 
469 	__SYMBOL_ANNOTATE_ERRNO__END,
470 };
471 
472 int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *buf, size_t buflen);
473 
474 int symbol__annotate_printf(struct map_symbol *ms, struct evsel *evsel);
475 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx);
476 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx);
477 void annotated_source__purge(struct annotated_source *as);
478 
479 int map_symbol__annotation_dump(struct map_symbol *ms, struct evsel *evsel);
480 
481 bool ui__has_annotation(void);
482 
483 int symbol__tty_annotate(struct map_symbol *ms, struct evsel *evsel);
484 
485 int symbol__tty_annotate2(struct map_symbol *ms, struct evsel *evsel);
486 
487 #ifdef HAVE_SLANG_SUPPORT
488 int symbol__tui_annotate(struct map_symbol *ms, struct evsel *evsel,
489 			 struct hist_browser_timer *hbt);
490 #else
491 static inline int symbol__tui_annotate(struct map_symbol *ms __maybe_unused,
492 				struct evsel *evsel  __maybe_unused,
493 				struct hist_browser_timer *hbt __maybe_unused)
494 {
495 	return 0;
496 }
497 #endif
498 
499 void annotation_options__init(void);
500 void annotation_options__exit(void);
501 
502 void annotation_config__init(void);
503 
504 int annotate_parse_percent_type(const struct option *opt, const char *_str,
505 				int unset);
506 
507 int annotate_check_args(void);
508 
509 /**
510  * struct annotated_op_loc - Location info of instruction operand
511  * @reg1: First register in the operand
512  * @reg2: Second register in the operand
513  * @offset: Memory access offset in the operand
514  * @mem_ref: Whether the operand accesses memory
515  * @multi_regs: Whether the second register is used
516  */
517 struct annotated_op_loc {
518 	int reg1;
519 	int reg2;
520 	int offset;
521 	bool mem_ref;
522 	bool multi_regs;
523 };
524 
525 enum annotated_insn_ops {
526 	INSN_OP_SOURCE = 0,
527 	INSN_OP_TARGET = 1,
528 
529 	INSN_OP_MAX,
530 };
531 
532 /**
533  * struct annotated_insn_loc - Location info of instruction
534  * @ops: Array of location info for source and target operands
535  */
536 struct annotated_insn_loc {
537 	struct annotated_op_loc ops[INSN_OP_MAX];
538 };
539 
540 #define for_each_insn_op_loc(insn_loc, i, op_loc)			\
541 	for (i = INSN_OP_SOURCE, op_loc = &(insn_loc)->ops[i];		\
542 	     i < INSN_OP_MAX;						\
543 	     i++, op_loc++)
544 
545 /* Get detailed location info in the instruction */
546 int annotate_get_insn_location(struct arch *arch, struct disasm_line *dl,
547 			       struct annotated_insn_loc *loc);
548 
549 /* Returns a data type from the sample instruction (if any) */
550 struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he);
551 
552 struct annotated_item_stat {
553 	struct list_head list;
554 	char *name;
555 	int good;
556 	int bad;
557 };
558 extern struct list_head ann_insn_stat;
559 
560 /* Calculate PC-relative address */
561 u64 annotate_calc_pcrel(struct map_symbol *ms, u64 ip, int offset,
562 			struct disasm_line *dl);
563 
564 #endif	/* __PERF_ANNOTATE_H */
565