Commit | Line | Data |
---|---|---|
8035e428 IM |
1 | /* |
2 | * builtin-annotate.c | |
3 | * | |
4 | * Builtin annotate command: Analyze the perf.data input file, | |
5 | * look up and read DSOs and symbol information and display | |
6 | * a histogram of results, along various sorting keys. | |
7 | */ | |
8 | #include "builtin.h" | |
9 | ||
78f7defe | 10 | #include "util/util.h" |
8035e428 | 11 | #include "util/color.h" |
5da50258 | 12 | #include <linux/list.h> |
8035e428 | 13 | #include "util/cache.h" |
43cbcd8a | 14 | #include <linux/rbtree.h> |
8035e428 | 15 | #include "util/symbol.h" |
8035e428 IM |
16 | |
17 | #include "perf.h" | |
8f28827a | 18 | #include "util/debug.h" |
8035e428 | 19 | |
e248de33 ACM |
20 | #include "util/evlist.h" |
21 | #include "util/evsel.h" | |
78f7defe | 22 | #include "util/annotate.h" |
62daacb5 | 23 | #include "util/event.h" |
8035e428 IM |
24 | #include "util/parse-options.h" |
25 | #include "util/parse-events.h" | |
6baa0a5a | 26 | #include "util/thread.h" |
dd68ada2 | 27 | #include "util/sort.h" |
3d1d07ec | 28 | #include "util/hist.h" |
94c744b6 | 29 | #include "util/session.h" |
8035e428 | 30 | |
5d67be97 AB |
31 | #include <linux/bitmap.h> |
32 | ||
8035e428 | 33 | static char const *input_name = "perf.data"; |
8035e428 | 34 | |
8b9e74eb | 35 | static bool force, use_tui, use_stdio; |
8035e428 | 36 | |
c0555642 | 37 | static bool full_paths; |
42976487 | 38 | |
c0555642 | 39 | static bool print_line; |
301406b9 | 40 | |
e4204992 ACM |
41 | static const char *sym_hist_filter; |
42 | ||
5d67be97 AB |
43 | static const char *cpu_list; |
44 | static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); | |
45 | ||
d04b35f8 ACM |
46 | static int perf_evsel__add_sample(struct perf_evsel *evsel, |
47 | struct perf_sample *sample, | |
48 | struct addr_location *al) | |
8035e428 | 49 | { |
628ada0c | 50 | struct hist_entry *he; |
e248de33 | 51 | int ret; |
628ada0c ACM |
52 | |
53 | if (sym_hist_filter != NULL && | |
54 | (al->sym == NULL || strcmp(sym_hist_filter, al->sym->name) != 0)) { | |
55 | /* We're only interested in a symbol named sym_hist_filter */ | |
56 | if (al->sym != NULL) { | |
57 | rb_erase(&al->sym->rb_node, | |
58 | &al->map->dso->symbols[al->map->type]); | |
59 | symbol__delete(al->sym); | |
60 | } | |
61 | return 0; | |
62 | } | |
63 | ||
e248de33 | 64 | he = __hists__add_entry(&evsel->hists, al, NULL, 1); |
9735abf1 | 65 | if (he == NULL) |
8035e428 | 66 | return -ENOMEM; |
628ada0c | 67 | |
e248de33 | 68 | ret = 0; |
2f525d01 | 69 | if (he->ms.sym != NULL) { |
2f525d01 | 70 | struct annotation *notes = symbol__annotation(he->ms.sym); |
d04b35f8 | 71 | if (notes->src == NULL && symbol__alloc_hist(he->ms.sym) < 0) |
2f525d01 ACM |
72 | return -ENOMEM; |
73 | ||
e248de33 | 74 | ret = hist_entry__inc_addr_samples(he, evsel->idx, al->addr); |
2f525d01 ACM |
75 | } |
76 | ||
e248de33 ACM |
77 | evsel->hists.stats.total_period += sample->period; |
78 | hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE); | |
79 | return ret; | |
8035e428 IM |
80 | } |
81 | ||
8115d60c ACM |
82 | static int process_sample_event(union perf_event *event, |
83 | struct perf_sample *sample, | |
9e69c210 | 84 | struct perf_evsel *evsel, |
640c03ce | 85 | struct perf_session *session) |
8035e428 | 86 | { |
1ed091c4 | 87 | struct addr_location al; |
6baa0a5a | 88 | |
ce6f4fab ACM |
89 | if (perf_event__preprocess_sample(event, session, &al, sample, |
90 | symbol__annotate_init) < 0) { | |
29a9f66d ACM |
91 | pr_warning("problem processing %d event, skipping it.\n", |
92 | event->header.type); | |
8035e428 IM |
93 | return -1; |
94 | } | |
95 | ||
5d67be97 AB |
96 | if (cpu_list && !test_bit(sample->cpu, cpu_bitmap)) |
97 | return 0; | |
98 | ||
d04b35f8 | 99 | if (!al.filtered && perf_evsel__add_sample(evsel, sample, &al)) { |
29a9f66d ACM |
100 | pr_warning("problem incrementing symbol count, " |
101 | "skipping event\n"); | |
ec218fc4 | 102 | return -1; |
8035e428 | 103 | } |
8035e428 IM |
104 | |
105 | return 0; | |
106 | } | |
107 | ||
2f525d01 | 108 | static int hist_entry__tty_annotate(struct hist_entry *he, int evidx) |
0b73da3f | 109 | { |
2f525d01 | 110 | return symbol__tty_annotate(he->ms.sym, he->ms.map, evidx, |
d040bd36 | 111 | print_line, full_paths, 0, 0); |
0b73da3f IM |
112 | } |
113 | ||
d04b35f8 | 114 | static void hists__find_annotations(struct hists *self, int evidx) |
0b73da3f | 115 | { |
b50e003d | 116 | struct rb_node *nd = rb_first(&self->entries), *next; |
cf958003 | 117 | int key = K_RIGHT; |
0b73da3f | 118 | |
46e3e055 | 119 | while (nd) { |
ed52ce2e | 120 | struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node); |
78f7defe | 121 | struct annotation *notes; |
0b73da3f | 122 | |
46e3e055 ACM |
123 | if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned) |
124 | goto find_next; | |
0b73da3f | 125 | |
78f7defe | 126 | notes = symbol__annotation(he->ms.sym); |
ce6f4fab | 127 | if (notes->src == NULL) { |
46e3e055 | 128 | find_next: |
cf958003 | 129 | if (key == K_LEFT) |
46e3e055 ACM |
130 | nd = rb_prev(nd); |
131 | else | |
132 | nd = rb_next(nd); | |
e4204992 | 133 | continue; |
46e3e055 | 134 | } |
e4204992 | 135 | |
62e3436b | 136 | if (use_browser > 0) { |
d04b35f8 | 137 | key = hist_entry__tui_annotate(he, evidx, NULL, NULL, 0); |
46e3e055 | 138 | switch (key) { |
cf958003 | 139 | case K_RIGHT: |
b50e003d | 140 | next = rb_next(nd); |
46e3e055 | 141 | break; |
cf958003 | 142 | case K_LEFT: |
b50e003d | 143 | next = rb_prev(nd); |
46e3e055 | 144 | break; |
b50e003d ACM |
145 | default: |
146 | return; | |
46e3e055 | 147 | } |
b50e003d ACM |
148 | |
149 | if (next != NULL) | |
150 | nd = next; | |
46e3e055 | 151 | } else { |
e248de33 | 152 | hist_entry__tty_annotate(he, evidx); |
46e3e055 ACM |
153 | nd = rb_next(nd); |
154 | /* | |
155 | * Since we have a hist_entry per IP for the same | |
ce6f4fab | 156 | * symbol, free he->ms.sym->src to signal we already |
46e3e055 ACM |
157 | * processed this symbol. |
158 | */ | |
ce6f4fab ACM |
159 | free(notes->src); |
160 | notes->src = NULL; | |
46e3e055 | 161 | } |
0b73da3f | 162 | } |
0b73da3f IM |
163 | } |
164 | ||
301a0b02 | 165 | static struct perf_event_ops event_ops = { |
55aa640f | 166 | .sample = process_sample_event, |
8115d60c ACM |
167 | .mmap = perf_event__process_mmap, |
168 | .comm = perf_event__process_comm, | |
169 | .fork = perf_event__process_task, | |
eac23d1c IM |
170 | .ordered_samples = true, |
171 | .ordering_requires_timestamps = true, | |
bab81b62 LZ |
172 | }; |
173 | ||
8035e428 IM |
174 | static int __cmd_annotate(void) |
175 | { | |
bab81b62 | 176 | int ret; |
75be6cf4 | 177 | struct perf_session *session; |
e248de33 ACM |
178 | struct perf_evsel *pos; |
179 | u64 total_nr_samples; | |
8035e428 | 180 | |
21ef97f0 | 181 | session = perf_session__new(input_name, O_RDONLY, force, false, &event_ops); |
94c744b6 ACM |
182 | if (session == NULL) |
183 | return -ENOMEM; | |
184 | ||
5d67be97 AB |
185 | if (cpu_list) { |
186 | ret = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap); | |
187 | if (ret) | |
188 | goto out_delete; | |
189 | } | |
190 | ||
ec913369 | 191 | ret = perf_session__process_events(session, &event_ops); |
bab81b62 | 192 | if (ret) |
94c744b6 | 193 | goto out_delete; |
8035e428 | 194 | |
62daacb5 | 195 | if (dump_trace) { |
c8446b9b | 196 | perf_session__fprintf_nr_events(session, stdout); |
94c744b6 | 197 | goto out_delete; |
62daacb5 | 198 | } |
8035e428 | 199 | |
da21d1b5 | 200 | if (verbose > 3) |
b3165f41 | 201 | perf_session__fprintf(session, stdout); |
8035e428 | 202 | |
da21d1b5 | 203 | if (verbose > 2) |
cbf69680 | 204 | perf_session__fprintf_dsos(session, stdout); |
8035e428 | 205 | |
e248de33 ACM |
206 | total_nr_samples = 0; |
207 | list_for_each_entry(pos, &session->evlist->entries, node) { | |
208 | struct hists *hists = &pos->hists; | |
209 | u32 nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE]; | |
210 | ||
211 | if (nr_samples > 0) { | |
212 | total_nr_samples += nr_samples; | |
213 | hists__collapse_resort(hists); | |
214 | hists__output_resort(hists); | |
d04b35f8 | 215 | hists__find_annotations(hists, pos->idx); |
e248de33 ACM |
216 | } |
217 | } | |
8035e428 | 218 | |
e248de33 ACM |
219 | if (total_nr_samples == 0) { |
220 | ui__warning("The %s file has no samples!\n", input_name); | |
221 | goto out_delete; | |
222 | } | |
223 | out_delete: | |
224 | /* | |
225 | * Speed up the exit process, for large files this can | |
226 | * take quite a while. | |
227 | * | |
228 | * XXX Enable this when using valgrind or if we ever | |
229 | * librarize this command. | |
230 | * | |
231 | * Also experiment with obstacks to see how much speed | |
232 | * up we'll get here. | |
233 | * | |
234 | * perf_session__delete(session); | |
235 | */ | |
bab81b62 | 236 | return ret; |
8035e428 IM |
237 | } |
238 | ||
239 | static const char * const annotate_usage[] = { | |
240 | "perf annotate [<options>] <command>", | |
241 | NULL | |
242 | }; | |
243 | ||
244 | static const struct option options[] = { | |
245 | OPT_STRING('i', "input", &input_name, "file", | |
246 | "input file name"), | |
ac73c5a9 ACM |
247 | OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]", |
248 | "only consider symbols in these dsos"), | |
23b87116 | 249 | OPT_STRING('s', "symbol", &sym_hist_filter, "symbol", |
0b73da3f | 250 | "symbol to annotate"), |
fa6963b2 | 251 | OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), |
c0555642 | 252 | OPT_INCR('v', "verbose", &verbose, |
8035e428 IM |
253 | "be more verbose (show symbol address, etc)"), |
254 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, | |
255 | "dump raw trace in ASCII"), | |
8b9e74eb ACM |
256 | OPT_BOOLEAN(0, "tui", &use_tui, "Use the TUI interface"), |
257 | OPT_BOOLEAN(0, "stdio", &use_stdio, "Use the stdio interface"), | |
b32d133a ACM |
258 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, |
259 | "file", "vmlinux pathname"), | |
260 | OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, | |
42976487 | 261 | "load module symbols - WARNING: use only with -k and LIVE kernel"), |
301406b9 FW |
262 | OPT_BOOLEAN('l', "print-line", &print_line, |
263 | "print matching source lines (may be slow)"), | |
42976487 MG |
264 | OPT_BOOLEAN('P', "full-paths", &full_paths, |
265 | "Don't shorten the displayed pathnames"), | |
5d67be97 | 266 | OPT_STRING('c', "cpu", &cpu_list, "cpu", "list of cpus to profile"), |
e71a0598 SE |
267 | OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory", |
268 | "Look for files with symbols relative to this directory"), | |
64c6f0c7 | 269 | OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src, |
3e6a2a7f | 270 | "Interleave source code with assembly code (default)"), |
64c6f0c7 | 271 | OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw, |
3e6a2a7f | 272 | "Display raw encoding of assembly instructions (default)"), |
f69b64f7 AK |
273 | OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style", |
274 | "Specify disassembler style (e.g. -M intel for intel syntax)"), | |
8035e428 IM |
275 | OPT_END() |
276 | }; | |
277 | ||
f37a291c | 278 | int cmd_annotate(int argc, const char **argv, const char *prefix __used) |
8035e428 | 279 | { |
655000e7 ACM |
280 | argc = parse_options(argc, argv, options, annotate_usage, 0); |
281 | ||
8b9e74eb ACM |
282 | if (use_stdio) |
283 | use_browser = 0; | |
284 | else if (use_tui) | |
285 | use_browser = 1; | |
286 | ||
229ade9b | 287 | setup_browser(true); |
46e3e055 | 288 | |
78f7defe | 289 | symbol_conf.priv_size = sizeof(struct annotation); |
75be6cf4 ACM |
290 | symbol_conf.try_vmlinux_path = true; |
291 | ||
292 | if (symbol__init() < 0) | |
b32d133a | 293 | return -1; |
8035e428 | 294 | |
c8829c7a | 295 | setup_sorting(annotate_usage, options); |
8035e428 | 296 | |
0b73da3f IM |
297 | if (argc) { |
298 | /* | |
299 | * Special case: if there's an argument left then assume tha | |
300 | * it's a symbol filter: | |
301 | */ | |
302 | if (argc > 1) | |
303 | usage_with_options(annotate_usage, options); | |
304 | ||
305 | sym_hist_filter = argv[0]; | |
306 | } | |
307 | ||
dd68ada2 | 308 | if (field_sep && *field_sep == '.') { |
29a9f66d ACM |
309 | pr_err("'.' is the only non valid --field-separator argument\n"); |
310 | return -1; | |
dd68ada2 JK |
311 | } |
312 | ||
8035e428 IM |
313 | return __cmd_annotate(); |
314 | } |