blob: f93c805473f9f467c1addb08fc92e1c08ac0032f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02002#ifndef __PERF_ANNOTATE_H
3#define __PERF_ANNOTATE_H
4
5#include <stdbool.h>
Arnaldo Carvalho de Melofb29fa52012-04-25 14:16:03 -03006#include <stdint.h>
Borislav Petkovd944c4e2014-04-25 21:31:02 +02007#include <linux/types.h>
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02008#include "symbol.h"
Namhyung Kim9783adf2012-11-02 14:50:05 +09009#include "hist.h"
Namhyung Kim2b676bf2013-02-07 18:02:08 +090010#include "sort.h"
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020011#include <linux/list.h>
12#include <linux/rbtree.h>
Irina Tirdea27683dc2012-09-08 03:43:19 +030013#include <pthread.h>
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020014
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -030015struct ins_ops;
16
17struct ins {
18 const char *name;
19 struct ins_ops *ops;
20};
Arnaldo Carvalho de Melo28548d72012-04-19 10:16:27 -030021
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -030022struct ins_operands {
23 char *raw;
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -030024 struct {
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -030025 char *raw;
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -030026 char *name;
Arnaldo Carvalho de Melo696703a2018-03-02 11:59:36 -030027 struct symbol *sym;
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -030028 u64 addr;
Ravi Bangoriae2168742016-12-05 21:26:47 +053029 s64 offset;
30 bool offset_avail;
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -030031 } target;
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -030032 union {
33 struct {
34 char *raw;
35 char *name;
36 u64 addr;
37 } source;
38 struct {
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -030039 struct ins ins;
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -030040 struct ins_operands *ops;
41 } locked;
42 };
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -030043};
44
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -030045struct arch;
46
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -030047struct ins_ops {
Arnaldo Carvalho de Meloc46219ac2012-05-12 13:26:20 -030048 void (*free)(struct ins_operands *ops);
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -030049 int (*parse)(struct arch *arch, struct ins_operands *ops, struct map *map);
Arnaldo Carvalho de Melo28548d72012-04-19 10:16:27 -030050 int (*scnprintf)(struct ins *ins, char *bf, size_t size,
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -030051 struct ins_operands *ops);
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -030052};
53
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -030054bool ins__is_jump(const struct ins *ins);
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -030055bool ins__is_call(const struct ins *ins);
Naveen N. Rao6ef94922016-06-24 17:23:58 +053056bool ins__is_ret(const struct ins *ins);
Jin Yao7e63a132017-07-07 13:06:35 +080057bool ins__is_lock(const struct ins *ins);
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -030058int ins__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops);
Jin Yao69fb09f2017-07-07 13:06:34 +080059bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2);
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -030060
Arnaldo Carvalho de Meloc426e582018-03-15 10:26:17 -030061#define ANNOTATION__IPC_WIDTH 6
62#define ANNOTATION__CYCLES_WIDTH 6
63
Arnaldo Carvalho de Melo98bc80b2018-03-15 10:03:34 -030064struct annotation_options {
65 bool hide_src_code,
66 use_offset,
67 jump_arrows,
68 show_linenr,
69 show_nr_jumps,
70 show_nr_samples,
71 show_total_period;
72};
73
Namhyung Kime64aa752013-03-05 14:53:30 +090074struct annotation;
75
Jiri Olsa7e304552017-10-11 17:01:39 +020076struct sym_hist_entry {
77 u64 nr_samples;
78 u64 period;
79};
80
81struct annotation_data {
82 double percent;
Jiri Olsa8b4c74d2017-10-11 17:01:41 +020083 double percent_sum;
Jiri Olsa7e304552017-10-11 17:01:39 +020084 struct sym_hist_entry he;
85};
86
Jiri Olsaa17c4ca2017-10-11 17:01:25 +020087struct annotation_line {
88 struct list_head node;
Jiri Olsa5b12adc2017-10-11 17:01:36 +020089 struct rb_node rb_node;
Jiri Olsad5490b92017-10-11 17:01:26 +020090 s64 offset;
91 char *line;
92 int line_nr;
Arnaldo Carvalho de Melo0db45bc2018-03-15 15:31:56 -030093 int jump_sources;
Jiri Olsa37236d52017-10-11 17:01:27 +020094 float ipc;
95 u64 cycles;
Jiri Olsac835e192017-10-11 17:01:37 +020096 size_t privsize;
Jiri Olsa8b4c74d2017-10-11 17:01:41 +020097 char *path;
Arnaldo Carvalho de Melo4850c922018-03-15 15:43:18 -030098 u32 idx;
99 int idx_asm;
Jiri Olsa7e304552017-10-11 17:01:39 +0200100 int samples_nr;
101 struct annotation_data samples[0];
Jiri Olsaa17c4ca2017-10-11 17:01:25 +0200102};
103
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300104struct disasm_line {
Jiri Olsaa17c4ca2017-10-11 17:01:25 +0200105 struct ins ins;
Jiri Olsaa17c4ca2017-10-11 17:01:25 +0200106 struct ins_operands ops;
Jiri Olsac835e192017-10-11 17:01:37 +0200107
108 /* This needs to be at the end. */
109 struct annotation_line al;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200110};
111
Jiri Olsac835e192017-10-11 17:01:37 +0200112static inline struct disasm_line *disasm_line(struct annotation_line *al)
113{
114 return al ? container_of(al, struct disasm_line, al) : NULL;
115}
116
Arnaldo Carvalho de Melofb29fa52012-04-25 14:16:03 -0300117static inline bool disasm_line__has_offset(const struct disasm_line *dl)
118{
Ravi Bangoriae2168742016-12-05 21:26:47 +0530119 return dl->ops.target.offset_avail;
Arnaldo Carvalho de Melofb29fa52012-04-25 14:16:03 -0300120}
121
Arnaldo Carvalho de Melo0db45bc2018-03-15 15:31:56 -0300122bool disasm_line__is_valid_jump(struct disasm_line *dl, struct symbol *sym);
123
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300124void disasm_line__free(struct disasm_line *dl);
Jiri Olsac4c72432017-10-11 17:01:34 +0200125struct annotation_line *
126annotation_line__next(struct annotation_line *pos, struct list_head *head);
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -0300127int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw);
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300128size_t disasm__fprintf(struct list_head *head, FILE *fp);
Jiri Olsa9e4e0a92017-11-15 12:05:59 +0100129void symbol__calc_percent(struct symbol *sym, struct perf_evsel *evsel);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200130
131struct sym_hist {
Taeung Song8158683d2017-07-20 06:36:51 +0900132 u64 nr_samples;
Taeung Song461c17f2017-07-20 17:18:05 -0300133 u64 period;
Taeung Song896bccd2017-07-20 06:36:45 +0900134 struct sym_hist_entry addr[0];
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200135};
136
Andi Kleend4957632015-07-18 08:24:48 -0700137struct cyc_hist {
138 u64 start;
139 u64 cycles;
140 u64 cycles_aggr;
141 u32 num;
142 u32 num_aggr;
143 u8 have_start;
144 /* 1 byte padding */
145 u16 reset;
146};
147
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200148/** struct annotated_source - symbols with hits have this attached as in sannotation
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200149 *
150 * @histogram: Array of addr hit histograms per event being monitored
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200151 * @lines: If 'print_lines' is specified, per source code line percentages
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300152 * @source: source parsed from a disassembler like objdump -dS
Andi Kleend4957632015-07-18 08:24:48 -0700153 * @cyc_hist: Average cycles per basic block
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200154 *
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200155 * lines is allocated, percentages calculated and all sorted by percentage
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200156 * when the annotation is about to be presented, so the percentages are for
157 * one of the entries in the histogram array, i.e. for the event/counter being
158 * presented. It is deallocated right after symbol__{tui,tty,etc}_annotate
159 * returns.
160 */
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200161struct annotated_source {
162 struct list_head source;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200163 int nr_histograms;
Jiri Olsa5ec45022015-10-05 20:06:03 +0200164 size_t sizeof_sym_hist;
Andi Kleend4957632015-07-18 08:24:48 -0700165 struct cyc_hist *cycles_hist;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200166 struct sym_hist histograms[0];
167};
168
169struct annotation {
170 pthread_mutex_t lock;
Peter Zijlstra70fbe052016-09-05 16:08:12 -0300171 u64 max_coverage;
Arnaldo Carvalho de Melo0ca693b2018-03-15 15:46:22 -0300172 u64 start;
Arnaldo Carvalho de Melo16932d72018-03-15 12:33:56 -0300173 struct annotation_options *options;
Arnaldo Carvalho de Melo9d6bb412018-03-15 10:47:54 -0300174 struct annotation_line **offsets;
Arnaldo Carvalho de Melo0553e832018-03-15 11:52:32 -0300175 int nr_events;
Arnaldo Carvalho de Melo6dcd57e2018-03-15 15:20:12 -0300176 int nr_jumps;
Arnaldo Carvalho de Melobc1c0f32018-03-15 15:10:28 -0300177 int max_jump_sources;
Arnaldo Carvalho de Melo1cf5f982018-03-15 15:52:33 -0300178 int nr_entries;
179 int nr_asm_entries;
Arnaldo Carvalho de Melo5bc49f62018-03-15 15:59:01 -0300180 u16 max_line_len;
Arnaldo Carvalho de Melo9761e862018-03-15 16:18:21 -0300181 struct {
182 u8 addr;
183 u8 jumps;
184 u8 target;
185 u8 min_addr;
186 u8 max_addr;
187 } widths;
Arnaldo Carvalho de Melo0e83a7e2018-03-15 10:35:04 -0300188 bool have_cycles;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200189 struct annotated_source *src;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200190};
191
Arnaldo Carvalho de Melo0e83a7e2018-03-15 10:35:04 -0300192static inline int annotation__cycles_width(struct annotation *notes)
193{
194 return notes->have_cycles ? ANNOTATION__IPC_WIDTH + ANNOTATION__CYCLES_WIDTH : 0;
195}
196
Arnaldo Carvalho de Melo6af612d2018-03-15 12:41:39 -0300197static inline int annotation__pcnt_width(struct annotation *notes)
198{
199 return (notes->options->show_total_period ? 12 : 7) * notes->nr_events;
200}
201
Arnaldo Carvalho de Melo5bc49f62018-03-15 15:59:01 -0300202
203void annotation__set_offsets(struct annotation *notes, s64 size);
Arnaldo Carvalho de Melof56c0832018-03-15 11:46:23 -0300204void annotation__compute_ipc(struct annotation *notes, size_t size);
Arnaldo Carvalho de Melo0db45bc2018-03-15 15:31:56 -0300205void annotation__mark_jump_targets(struct annotation *notes, struct symbol *sym);
Arnaldo Carvalho de Melo7232bf72018-03-15 16:19:59 -0300206void annotation__update_column_widths(struct annotation *notes);
Arnaldo Carvalho de Melob8b0d812018-03-15 16:26:29 -0300207void annotation__init_column_widths(struct annotation *notes, struct symbol *sym);
Arnaldo Carvalho de Melof56c0832018-03-15 11:46:23 -0300208
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200209static inline struct sym_hist *annotation__histogram(struct annotation *notes, int idx)
210{
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200211 return (((void *)&notes->src->histograms) +
212 (notes->src->sizeof_sym_hist * idx));
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200213}
214
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200215static inline struct annotation *symbol__annotation(struct symbol *sym)
216{
Namhyung Kim813ccd12015-01-14 20:18:05 +0900217 return (void *)sym - symbol_conf.priv_size;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200218}
219
Taeung Songbab89f62017-07-20 16:28:53 -0300220int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample,
221 int evidx);
Arnaldo Carvalho de Melo0f4e7a22013-12-18 16:48:29 -0300222
Andi Kleend4957632015-07-18 08:24:48 -0700223int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
224 struct addr_map_symbol *start,
225 unsigned cycles);
226
Taeung Songbab89f62017-07-20 16:28:53 -0300227int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample,
228 int evidx, u64 addr);
Arnaldo Carvalho de Melof626adf2013-12-18 17:10:15 -0300229
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200230int symbol__alloc_hist(struct symbol *sym);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200231void symbol__annotate_zero_histograms(struct symbol *sym);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200232
Jiri Olsac34df252017-10-11 17:01:28 +0200233int symbol__annotate(struct symbol *sym, struct map *map,
Jiri Olsad03a6862017-10-11 17:01:33 +0200234 struct perf_evsel *evsel, size_t privsize,
Arnaldo Carvalho de Melo5449f132017-12-11 12:46:11 -0300235 struct arch **parch);
Arnaldo Carvalho de Meloecda45b2018-03-15 16:54:11 -0300236int symbol__annotate2(struct symbol *sym, struct map *map,
237 struct perf_evsel *evsel,
238 struct annotation_options *options,
239 struct arch **parch);
Arnaldo Carvalho de Melof626adf2013-12-18 17:10:15 -0300240
Arnaldo Carvalho de Meloee51d852016-07-29 16:27:18 -0300241enum symbol_disassemble_errno {
242 SYMBOL_ANNOTATE_ERRNO__SUCCESS = 0,
243
244 /*
245 * Choose an arbitrary negative big number not to clash with standard
246 * errno since SUS requires the errno has distinct positive values.
247 * See 'Issue 6' in the link below.
248 *
249 * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html
250 */
251 __SYMBOL_ANNOTATE_ERRNO__START = -10000,
252
253 SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX = __SYMBOL_ANNOTATE_ERRNO__START,
254
255 __SYMBOL_ANNOTATE_ERRNO__END,
256};
257
258int symbol__strerror_disassemble(struct symbol *sym, struct map *map,
259 int errnum, char *buf, size_t buflen);
260
Namhyung Kimdb8fd072013-03-05 14:53:21 +0900261int symbol__annotate_printf(struct symbol *sym, struct map *map,
262 struct perf_evsel *evsel, bool full_paths,
263 int min_pcnt, int max_lines, int context);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200264void symbol__annotate_zero_histogram(struct symbol *sym, int evidx);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200265void symbol__annotate_decay_histogram(struct symbol *sym, int evidx);
Jiri Olsaf8eb37b2017-10-11 17:01:38 +0200266void annotated_source__purge(struct annotated_source *as);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200267
Namhyung Kim48c65bd2014-02-20 10:32:53 +0900268bool ui__has_annotation(void);
269
Namhyung Kimdb8fd072013-03-05 14:53:21 +0900270int symbol__tty_annotate(struct symbol *sym, struct map *map,
271 struct perf_evsel *evsel, bool print_lines,
272 bool full_paths, int min_pcnt, int max_lines);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200273
Ingo Molnar89fe8082013-09-30 12:07:11 +0200274#ifdef HAVE_SLANG_SUPPORT
Namhyung Kimdb8fd072013-03-05 14:53:21 +0900275int symbol__tui_annotate(struct symbol *sym, struct map *map,
276 struct perf_evsel *evsel,
Namhyung Kim9783adf2012-11-02 14:50:05 +0900277 struct hist_browser_timer *hbt);
Namhyung Kim1254b51e2012-09-28 18:32:02 +0900278#else
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300279static inline int symbol__tui_annotate(struct symbol *sym __maybe_unused,
Namhyung Kimdb8fd072013-03-05 14:53:21 +0900280 struct map *map __maybe_unused,
281 struct perf_evsel *evsel __maybe_unused,
282 struct hist_browser_timer *hbt
283 __maybe_unused)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200284{
285 return 0;
286}
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200287#endif
288
Andi Kleenf69b64f2011-09-15 14:31:41 -0700289extern const char *disassembler_style;
290
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200291#endif /* __PERF_ANNOTATE_H */