xref: /linux/tools/perf/util/annotate.h (revision 0d456bad36d42d16022be045c8a53ddbb59ee478)
1 #ifndef __PERF_ANNOTATE_H
2 #define __PERF_ANNOTATE_H
3 
4 #include <stdbool.h>
5 #include <stdint.h>
6 #include "types.h"
7 #include "symbol.h"
8 #include "hist.h"
9 #include <linux/list.h>
10 #include <linux/rbtree.h>
11 #include <pthread.h>
12 
13 struct ins;
14 
15 struct ins_operands {
16 	char	*raw;
17 	struct {
18 		char	*raw;
19 		char	*name;
20 		u64	addr;
21 		u64	offset;
22 	} target;
23 	union {
24 		struct {
25 			char	*raw;
26 			char	*name;
27 			u64	addr;
28 		} source;
29 		struct {
30 			struct ins *ins;
31 			struct ins_operands *ops;
32 		} locked;
33 	};
34 };
35 
36 struct ins_ops {
37 	void (*free)(struct ins_operands *ops);
38 	int (*parse)(struct ins_operands *ops);
39 	int (*scnprintf)(struct ins *ins, char *bf, size_t size,
40 			 struct ins_operands *ops);
41 };
42 
43 struct ins {
44 	const char     *name;
45 	struct ins_ops *ops;
46 };
47 
48 bool ins__is_jump(const struct ins *ins);
49 bool ins__is_call(const struct ins *ins);
50 int ins__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops);
51 
52 struct disasm_line {
53 	struct list_head    node;
54 	s64		    offset;
55 	char		    *line;
56 	char		    *name;
57 	struct ins	    *ins;
58 	struct ins_operands ops;
59 };
60 
61 static inline bool disasm_line__has_offset(const struct disasm_line *dl)
62 {
63 	return dl->ops.target.offset != UINT64_MAX;
64 }
65 
66 void disasm_line__free(struct disasm_line *dl);
67 struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos);
68 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw);
69 size_t disasm__fprintf(struct list_head *head, FILE *fp);
70 
71 struct sym_hist {
72 	u64		sum;
73 	u64		addr[0];
74 };
75 
76 struct source_line {
77 	struct rb_node	node;
78 	double		percent;
79 	double		percent_sum;
80 	char		*path;
81 };
82 
83 /** struct annotated_source - symbols with hits have this attached as in sannotation
84  *
85  * @histogram: Array of addr hit histograms per event being monitored
86  * @lines: If 'print_lines' is specified, per source code line percentages
87  * @source: source parsed from a disassembler like objdump -dS
88  *
89  * lines is allocated, percentages calculated and all sorted by percentage
90  * when the annotation is about to be presented, so the percentages are for
91  * one of the entries in the histogram array, i.e. for the event/counter being
92  * presented. It is deallocated right after symbol__{tui,tty,etc}_annotate
93  * returns.
94  */
95 struct annotated_source {
96 	struct list_head   source;
97 	struct source_line *lines;
98 	int    		   nr_histograms;
99 	int    		   sizeof_sym_hist;
100 	struct sym_hist	   histograms[0];
101 };
102 
103 struct annotation {
104 	pthread_mutex_t		lock;
105 	struct annotated_source *src;
106 };
107 
108 struct sannotation {
109 	struct annotation annotation;
110 	struct symbol	  symbol;
111 };
112 
113 static inline struct sym_hist *annotation__histogram(struct annotation *notes, int idx)
114 {
115 	return (((void *)&notes->src->histograms) +
116 	 	(notes->src->sizeof_sym_hist * idx));
117 }
118 
119 static inline struct annotation *symbol__annotation(struct symbol *sym)
120 {
121 	struct sannotation *a = container_of(sym, struct sannotation, symbol);
122 	return &a->annotation;
123 }
124 
125 int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
126 			     int evidx, u64 addr);
127 int symbol__alloc_hist(struct symbol *sym);
128 void symbol__annotate_zero_histograms(struct symbol *sym);
129 
130 int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize);
131 int symbol__annotate_init(struct map *map __maybe_unused, struct symbol *sym);
132 int symbol__annotate_printf(struct symbol *sym, struct map *map, int evidx,
133 			    bool full_paths, int min_pcnt, int max_lines,
134 			    int context);
135 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx);
136 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx);
137 void disasm__purge(struct list_head *head);
138 
139 int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx,
140 			 bool print_lines, bool full_paths, int min_pcnt,
141 			 int max_lines);
142 
143 #ifdef NEWT_SUPPORT
144 int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
145 			 struct hist_browser_timer *hbt);
146 #else
147 static inline int symbol__tui_annotate(struct symbol *sym __maybe_unused,
148 				       struct map *map __maybe_unused,
149 				       int evidx __maybe_unused,
150 				       struct hist_browser_timer *hbt
151 				       __maybe_unused)
152 {
153 	return 0;
154 }
155 #endif
156 
157 extern const char	*disassembler_style;
158 
159 #endif	/* __PERF_ANNOTATE_H */
160