Merge tag 'pm-6.16-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[linux-2.6-block.git] / tools / perf / builtin-annotate.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
8035e428
IM
2/*
3 * builtin-annotate.c
4 *
5 * Builtin annotate command: Analyze the perf.data input file,
6 * look up and read DSOs and symbol information and display
7 * a histogram of results, along various sorting keys.
8 */
9#include "builtin.h"
02b5ed8a 10#include "perf.h"
8035e428 11
8035e428 12#include "util/color.h"
5da50258 13#include <linux/list.h>
8035e428 14#include "util/cache.h"
43cbcd8a 15#include <linux/rbtree.h>
7f7c536f 16#include <linux/zalloc.h>
8035e428 17#include "util/symbol.h"
8035e428 18
8f28827a 19#include "util/debug.h"
8035e428 20
e248de33
ACM
21#include "util/evlist.h"
22#include "util/evsel.h"
78f7defe 23#include "util/annotate.h"
263925bf 24#include "util/annotate-data.h"
62daacb5 25#include "util/event.h"
4b6ab94e 26#include <subcmd/parse-options.h>
8035e428 27#include "util/parse-events.h"
dd68ada2 28#include "util/sort.h"
3d1d07ec 29#include "util/hist.h"
4a3cec84 30#include "util/dso.h"
3f79132a 31#include "util/machine.h"
1101f69a 32#include "util/map.h"
94c744b6 33#include "util/session.h"
45694aa7 34#include "util/tool.h"
f5fc1412 35#include "util/data.h"
68e94f4e 36#include "arch/common.h"
70fbe057 37#include "util/block-range.h"
d3300a3c
ACM
38#include "util/map_symbol.h"
39#include "util/branch.h"
f12ad272 40#include "util/util.h"
d9aedc12 41#include "ui/progress.h"
8035e428 42
fc67297b 43#include <dlfcn.h>
a43783ae 44#include <errno.h>
5d67be97 45#include <linux/bitmap.h>
6ef81c55 46#include <linux/err.h>
bdeaf6ff 47#include <inttypes.h>
5d67be97 48
d20deb64 49struct perf_annotate {
45694aa7 50 struct perf_tool tool;
fa10f316 51 struct perf_session *session;
3402ae0a
IR
52#ifdef HAVE_SLANG_SUPPORT
53 bool use_tui;
54#endif
55 bool use_stdio, use_stdio2;
557cc18e 56#ifdef HAVE_GTK2_SUPPORT
3402ae0a 57 bool use_gtk;
557cc18e 58#endif
18c9e5c5 59 bool skip_missing;
bb848c14 60 bool has_br_stack;
7ebaf489 61 bool group_set;
263925bf 62 bool data_type;
61a9741e 63 bool type_stat;
58824fa0 64 bool insn_stat;
cad10ce3 65 float min_percent;
7009cc34
ACM
66 const char *sym_hist_filter;
67 const char *cpu_list;
263925bf 68 const char *target_data_type;
7009cc34 69 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
d20deb64 70};
5d67be97 71
70fbe057
PZ
72/*
73 * Given one basic block:
74 *
75 * from to branch_i
76 * * ----> *
77 * |
78 * | block
79 * v
80 * * ----> *
81 * from to branch_i+1
82 *
83 * where the horizontal are the branches and the vertical is the executed
84 * block of instructions.
85 *
86 * We count, for each 'instruction', the number of blocks that covered it as
87 * well as count the ratio each branch is taken.
88 *
89 * We can do this without knowing the actual instruction stream by keeping
90 * track of the address ranges. We break down ranges such that there is no
91 * overlap and iterate from the start until the end.
92 *
93 * @acme: once we parse the objdump output _before_ processing the samples,
94 * we can easily fold the branch.cycles IPC bits in.
95 */
96static void process_basic_block(struct addr_map_symbol *start,
97 struct addr_map_symbol *end,
98 struct branch_flags *flags)
99{
d46a4cdf 100 struct symbol *sym = start->ms.sym;
70fbe057
PZ
101 struct annotation *notes = sym ? symbol__annotation(sym) : NULL;
102 struct block_range_iter iter;
103 struct block_range *entry;
2b215ec7 104 struct annotated_branch *branch;
70fbe057
PZ
105
106 /*
107 * Sanity; NULL isn't executable and the CPU cannot execute backwards
108 */
109 if (!start->addr || start->addr > end->addr)
110 return;
111
112 iter = block_range__create(start->addr, end->addr);
113 if (!block_range_iter__valid(&iter))
114 return;
115
2b215ec7
NK
116 branch = annotation__get_branch(notes);
117
70fbe057
PZ
118 /*
119 * First block in range is a branch target.
120 */
121 entry = block_range_iter(&iter);
122 assert(entry->is_target);
123 entry->entry++;
124
125 do {
126 entry = block_range_iter(&iter);
127
128 entry->coverage++;
129 entry->sym = sym;
130
2b215ec7
NK
131 if (branch)
132 branch->max_coverage = max(branch->max_coverage, entry->coverage);
70fbe057
PZ
133
134 } while (block_range_iter__next(&iter));
135
136 /*
137 * Last block in rage is a branch.
138 */
139 entry = block_range_iter(&iter);
140 assert(entry->is_branch);
141 entry->taken++;
142 if (flags->predicted)
143 entry->pred++;
144}
145
146static void process_branch_stack(struct branch_stack *bs, struct addr_location *al,
147 struct perf_sample *sample)
148{
149 struct addr_map_symbol *prev = NULL;
150 struct branch_info *bi;
151 int i;
152
153 if (!bs || !bs->nr)
154 return;
155
156 bi = sample__resolve_bstack(sample, al);
157 if (!bi)
158 return;
159
160 for (i = bs->nr - 1; i >= 0; i--) {
161 /*
162 * XXX filter against symbol
163 */
164 if (prev)
165 process_basic_block(prev, &bi[i].from, &bi[i].flags);
166 prev = &bi[i].to;
167 }
168
169 free(bi);
170}
171
bb848c14
JY
172static int hist_iter__branch_callback(struct hist_entry_iter *iter,
173 struct addr_location *al __maybe_unused,
174 bool single __maybe_unused,
175 void *arg __maybe_unused)
176{
177 struct hist_entry *he = iter->he;
178 struct branch_info *bi;
179 struct perf_sample *sample = iter->sample;
32dcd021 180 struct evsel *evsel = iter->evsel;
bb848c14
JY
181 int err;
182
bb848c14 183 bi = he->branch_info;
e345f3bd 184 err = addr_map_symbol__inc_samples(&bi->from, sample, evsel);
bb848c14
JY
185
186 if (err)
187 goto out;
188
e345f3bd 189 err = addr_map_symbol__inc_samples(&bi->to, sample, evsel);
bb848c14
JY
190
191out:
192 return err;
193}
194
32dcd021 195static int process_branch_callback(struct evsel *evsel,
bb848c14 196 struct perf_sample *sample,
0dd5041c 197 struct addr_location *al,
bb848c14
JY
198 struct perf_annotate *ann,
199 struct machine *machine)
200{
201 struct hist_entry_iter iter = {
202 .evsel = evsel,
203 .sample = sample,
204 .add_entry_cb = hist_iter__branch_callback,
205 .hide_unresolved = symbol_conf.hide_unresolved,
206 .ops = &hist_iter_branch,
207 };
bb848c14 208 struct addr_location a;
0dd5041c 209 int ret;
bb848c14 210
0dd5041c
IR
211 addr_location__init(&a);
212 if (machine__resolve(machine, &a, sample) < 0) {
213 ret = -1;
214 goto out;
215 }
bb848c14 216
0dd5041c
IR
217 if (a.sym == NULL) {
218 ret = 0;
219 goto out;
220 }
bb848c14
JY
221
222 if (a.map != NULL)
ee756ef7 223 dso__set_hit(map__dso(a.map));
bb848c14 224
1f2b7fbb
KL
225 hist__account_cycles(sample->branch_stack, al, sample, false,
226 NULL, evsel);
bdd1666b 227
0dd5041c
IR
228 ret = hist_entry_iter__add(&iter, &a, PERF_MAX_STACK_DEPTH, ann);
229out:
230 addr_location__exit(&a);
231 return ret;
bb848c14
JY
232}
233
befd2a38
ACM
234static bool has_annotation(struct perf_annotate *ann)
235{
236 return ui__has_annotation() || ann->use_stdio2;
237}
238
74aa90e8
ACM
239static int evsel__add_sample(struct evsel *evsel, struct perf_sample *sample,
240 struct addr_location *al, struct perf_annotate *ann,
241 struct machine *machine)
8035e428 242{
4ea062ed 243 struct hists *hists = evsel__hists(evsel);
628ada0c 244 struct hist_entry *he;
e248de33 245 int ret;
628ada0c 246
befd2a38 247 if ((!ann->has_br_stack || !has_annotation(ann)) &&
bb848c14 248 ann->sym_hist_filter != NULL &&
7009cc34
ACM
249 (al->sym == NULL ||
250 strcmp(ann->sym_hist_filter, al->sym->name) != 0)) {
628ada0c 251 /* We're only interested in a symbol named sym_hist_filter */
facf3f06
ACM
252 /*
253 * FIXME: why isn't this done in the symbol_filter when loading
254 * the DSO?
255 */
628ada0c 256 if (al->sym != NULL) {
63df0e4b
IR
257 struct dso *dso = map__dso(al->map);
258
ee756ef7 259 rb_erase_cached(&al->sym->rb_node, dso__symbols(dso));
628ada0c 260 symbol__delete(al->sym);
63df0e4b 261 dso__reset_find_symbol_cache(dso);
628ada0c
ACM
262 }
263 return 0;
264 }
265
70fbe057 266 /*
4d39c89f 267 * XXX filtered samples can still have branch entries pointing into our
70fbe057
PZ
268 * symbol and are missed.
269 */
270 process_branch_stack(sample->branch_stack, al, sample);
271
befd2a38 272 if (ann->has_br_stack && has_annotation(ann))
bb848c14
JY
273 return process_branch_callback(evsel, sample, al, ann, machine);
274
ebf39d29 275 he = hists__add_entry(hists, al, NULL, NULL, NULL, NULL, sample, true);
9735abf1 276 if (he == NULL)
8035e428 277 return -ENOMEM;
628ada0c 278
e345f3bd 279 ret = hist_entry__inc_addr_samples(he, sample, evsel, al->addr);
4ea062ed 280 hists__inc_nr_samples(hists, true);
e248de33 281 return ret;
8035e428
IM
282}
283
30f29bae 284static int process_sample_event(const struct perf_tool *tool,
d20deb64 285 union perf_event *event,
8115d60c 286 struct perf_sample *sample,
32dcd021 287 struct evsel *evsel,
743eb868 288 struct machine *machine)
8035e428 289{
45694aa7 290 struct perf_annotate *ann = container_of(tool, struct perf_annotate, tool);
1ed091c4 291 struct addr_location al;
b91fc39f 292 int ret = 0;
6baa0a5a 293
0dd5041c 294 addr_location__init(&al);
bb3eb566 295 if (machine__resolve(machine, &al, sample) < 0) {
29a9f66d
ACM
296 pr_warning("problem processing %d event, skipping it.\n",
297 event->header.type);
0dd5041c
IR
298 ret = -1;
299 goto out_put;
8035e428
IM
300 }
301
7009cc34 302 if (ann->cpu_list && !test_bit(sample->cpu, ann->cpu_bitmap))
b91fc39f 303 goto out_put;
5d67be97 304
bb848c14 305 if (!al.filtered &&
74aa90e8 306 evsel__add_sample(evsel, sample, &al, ann, machine)) {
29a9f66d
ACM
307 pr_warning("problem incrementing symbol count, "
308 "skipping event\n");
b91fc39f 309 ret = -1;
8035e428 310 }
b91fc39f 311out_put:
0dd5041c 312 addr_location__exit(&al);
b91fc39f 313 return ret;
8035e428
IM
314}
315
89f1688a
JO
316static int process_feature_event(struct perf_session *session,
317 union perf_event *event)
92ead7ee
RB
318{
319 if (event->feat.feat_id < HEADER_LAST_FEATURE)
89f1688a 320 return perf_event__process_feature(session, event);
92ead7ee
RB
321 return 0;
322}
323
fe8da669 324static int hist_entry__stdio_annotate(struct hist_entry *he,
32dcd021 325 struct evsel *evsel,
d20deb64 326 struct perf_annotate *ann)
0b73da3f 327{
fe8da669
NK
328 if (ann->use_stdio2)
329 return hist_entry__tty_annotate2(he, evsel);
982d410b 330
fe8da669 331 return hist_entry__tty_annotate(he, evsel);
0b73da3f
IM
332}
333
61a9741e
NK
334static void print_annotate_data_stat(struct annotated_data_stat *s)
335{
336#define PRINT_STAT(fld) if (s->fld) printf("%10d : %s\n", s->fld, #fld)
337
338 int bad = s->no_sym +
339 s->no_insn +
340 s->no_insn_ops +
341 s->no_mem_ops +
342 s->no_reg +
343 s->no_dbginfo +
344 s->no_cuinfo +
345 s->no_var +
346 s->no_typeinfo +
347 s->invalid_size +
348 s->bad_offset;
349 int ok = s->total - bad;
350
351 printf("Annotate data type stats:\n");
352 printf("total %d, ok %d (%.1f%%), bad %d (%.1f%%)\n",
353 s->total, ok, 100.0 * ok / (s->total ?: 1), bad, 100.0 * bad / (s->total ?: 1));
354 printf("-----------------------------------------------------------\n");
355 PRINT_STAT(no_sym);
356 PRINT_STAT(no_insn);
357 PRINT_STAT(no_insn_ops);
358 PRINT_STAT(no_mem_ops);
359 PRINT_STAT(no_reg);
360 PRINT_STAT(no_dbginfo);
361 PRINT_STAT(no_cuinfo);
362 PRINT_STAT(no_var);
363 PRINT_STAT(no_typeinfo);
364 PRINT_STAT(invalid_size);
365 PRINT_STAT(bad_offset);
eb8a55e0 366 PRINT_STAT(insn_track);
61a9741e
NK
367 printf("\n");
368
369#undef PRINT_STAT
370}
371
58824fa0
NK
372static void print_annotate_item_stat(struct list_head *head, const char *title)
373{
374 struct annotated_item_stat *istat, *pos, *iter;
375 int total_good, total_bad, total;
376 int sum1, sum2;
377 LIST_HEAD(tmp);
378
379 /* sort the list by count */
380 list_splice_init(head, &tmp);
381 total_good = total_bad = 0;
382
383 list_for_each_entry_safe(istat, pos, &tmp, list) {
384 total_good += istat->good;
385 total_bad += istat->bad;
386 sum1 = istat->good + istat->bad;
387
388 list_for_each_entry(iter, head, list) {
389 sum2 = iter->good + iter->bad;
390 if (sum1 > sum2)
391 break;
392 }
393 list_move_tail(&istat->list, &iter->list);
394 }
395 total = total_good + total_bad;
396
397 printf("Annotate %s stats\n", title);
398 printf("total %d, ok %d (%.1f%%), bad %d (%.1f%%)\n\n", total,
399 total_good, 100.0 * total_good / (total ?: 1),
400 total_bad, 100.0 * total_bad / (total ?: 1));
2c9db747 401 printf(" %-20s: %5s %5s\n", "Name/opcode", "Good", "Bad");
58824fa0
NK
402 printf("-----------------------------------------------------------\n");
403 list_for_each_entry(istat, head, list)
2c9db747 404 printf(" %-20s: %5d %5d\n", istat->name, istat->good, istat->bad);
58824fa0
NK
405 printf("\n");
406}
407
c824c433 408static void hists__find_annotations(struct hists *hists,
32dcd021 409 struct evsel *evsel,
d20deb64 410 struct perf_annotate *ann)
0b73da3f 411{
2eb3d689 412 struct rb_node *nd = rb_first_cached(&hists->entries), *next;
cf958003 413 int key = K_RIGHT;
0b73da3f 414
61a9741e
NK
415 if (ann->type_stat)
416 print_annotate_data_stat(&ann_data_stat);
58824fa0
NK
417 if (ann->insn_stat)
418 print_annotate_item_stat(&ann_insn_stat, "Instruction");
61a9741e 419
46e3e055 420 while (nd) {
ed52ce2e 421 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
78f7defe 422 struct annotation *notes;
0b73da3f 423
ee756ef7 424 if (he->ms.sym == NULL || dso__annotate_warned(map__dso(he->ms.map)))
46e3e055 425 goto find_next;
0b73da3f 426
bb848c14
JY
427 if (ann->sym_hist_filter &&
428 (strcmp(he->ms.sym->name, ann->sym_hist_filter) != 0))
429 goto find_next;
430
cad10ce3
NK
431 if (ann->min_percent) {
432 float percent = 0;
433 u64 total = hists__total_period(hists);
434
435 if (total)
436 percent = 100.0 * he->stat.period / total;
437
438 if (percent < ann->min_percent)
439 goto find_next;
440 }
441
78f7defe 442 notes = symbol__annotation(he->ms.sym);
ce6f4fab 443 if (notes->src == NULL) {
46e3e055 444find_next:
6d491b37 445 if (key == K_LEFT || key == '<')
46e3e055
ACM
446 nd = rb_prev(nd);
447 else
448 nd = rb_next(nd);
e4204992 449 continue;
46e3e055 450 }
e4204992 451
263925bf 452 if (ann->data_type) {
263925bf
NK
453 /* skip unknown type */
454 if (he->mem_type->histograms == NULL)
455 goto find_next;
456
457 if (ann->target_data_type) {
458 const char *type_name = he->mem_type->self.type_name;
459
460 /* skip 'struct ' prefix in the type name */
461 if (strncmp(ann->target_data_type, "struct ", 7) &&
462 !strncmp(type_name, "struct ", 7))
463 type_name += 7;
464
465 /* skip 'union ' prefix in the type name */
466 if (strncmp(ann->target_data_type, "union ", 6) &&
467 !strncmp(type_name, "union ", 6))
468 type_name += 6;
469
470 if (strcmp(ann->target_data_type, type_name))
471 goto find_next;
472 }
473
d001c7a7
NK
474 if (use_browser == 1)
475 key = hist_entry__annotate_data_tui(he, evsel, NULL);
476 else
477 key = hist_entry__annotate_data_tty(he, evsel);
478
479 switch (key) {
480 case -1:
481 if (!ann->skip_missing)
482 return;
483 /* fall through */
484 case K_RIGHT:
485 case '>':
486 next = rb_next(nd);
487 break;
488 case K_LEFT:
489 case '<':
490 next = rb_prev(nd);
491 break;
492 default:
493 return;
494 }
495
2b87383c 496 if (use_browser == 0 || next != NULL)
d001c7a7
NK
497 nd = next;
498
499 continue;
263925bf
NK
500 }
501
2b676bf0 502 if (use_browser == 2) {
18c9e5c5 503 int ret;
fc67297b 504 int (*annotate)(struct hist_entry *he,
32dcd021 505 struct evsel *evsel,
fc67297b
NK
506 struct hist_browser_timer *hbt);
507
508 annotate = dlsym(perf_gtk_handle,
509 "hist_entry__gtk_annotate");
510 if (annotate == NULL) {
511 ui__error("GTK browser not found!\n");
512 return;
513 }
18c9e5c5 514
41fd3cac 515 ret = annotate(he, evsel, NULL);
18c9e5c5
NK
516 if (!ret || !ann->skip_missing)
517 return;
518
519 /* skip missing symbols */
520 nd = rb_next(nd);
2b676bf0 521 } else if (use_browser == 1) {
22197fb2 522 key = hist_entry__tui_annotate(he, evsel, NULL);
bb848c14 523
46e3e055 524 switch (key) {
18c9e5c5
NK
525 case -1:
526 if (!ann->skip_missing)
527 return;
528 /* fall through */
cf958003 529 case K_RIGHT:
6d491b37 530 case '>':
b50e003d 531 next = rb_next(nd);
46e3e055 532 break;
cf958003 533 case K_LEFT:
6d491b37 534 case '<':
b50e003d 535 next = rb_prev(nd);
46e3e055 536 break;
b50e003d
ACM
537 default:
538 return;
46e3e055 539 }
b50e003d
ACM
540
541 if (next != NULL)
542 nd = next;
46e3e055 543 } else {
fe8da669 544 hist_entry__stdio_annotate(he, evsel, ann);
46e3e055 545 nd = rb_next(nd);
46e3e055 546 }
0b73da3f 547 }
0b73da3f
IM
548}
549
d20deb64 550static int __cmd_annotate(struct perf_annotate *ann)
8035e428 551{
bab81b62 552 int ret;
fa10f316 553 struct perf_session *session = ann->session;
32dcd021 554 struct evsel *pos;
e248de33 555 u64 total_nr_samples;
94c744b6 556
7009cc34
ACM
557 if (ann->cpu_list) {
558 ret = perf_session__cpu_bitmap(session, ann->cpu_list,
559 ann->cpu_bitmap);
5d67be97 560 if (ret)
fa10f316 561 goto out;
5d67be97
AB
562 }
563
9d03194a 564 if (!annotate_opts.objdump_path) {
f178fd2d 565 ret = perf_env__lookup_objdump(&session->header.env,
9d03194a 566 &annotate_opts.objdump_path);
68e94f4e 567 if (ret)
fa10f316 568 goto out;
68e94f4e
IT
569 }
570
b7b61cbe 571 ret = perf_session__process_events(session);
bab81b62 572 if (ret)
fa10f316 573 goto out;
8035e428 574
62daacb5 575 if (dump_trace) {
411ee135
NK
576 perf_session__fprintf_nr_events(session, stdout);
577 evlist__fprintf_nr_events(session->evlist, stdout);
fa10f316 578 goto out;
62daacb5 579 }
8035e428 580
da21d1b5 581 if (verbose > 3)
b3165f41 582 perf_session__fprintf(session, stdout);
8035e428 583
da21d1b5 584 if (verbose > 2)
cbf69680 585 perf_session__fprintf_dsos(session, stdout);
8035e428 586
e248de33 587 total_nr_samples = 0;
e5cadb93 588 evlist__for_each_entry(session->evlist, pos) {
4ea062ed 589 struct hists *hists = evsel__hists(pos);
0f0abbac 590 u32 nr_samples = hists->stats.nr_samples;
d9aedc12 591 struct ui_progress prog;
e248de33
ACM
592
593 if (nr_samples > 0) {
594 total_nr_samples += nr_samples;
d9aedc12
NK
595
596 ui_progress__init(&prog, nr_samples,
597 "Merging related events...");
598 hists__collapse_resort(hists, &prog);
599 ui_progress__finish();
600
f9db0d0f 601 /* Don't sort callchain */
862b2f8f 602 evsel__reset_sample_bit(pos, CALLCHAIN);
d9aedc12
NK
603
604 ui_progress__init(&prog, nr_samples,
605 "Sorting events for output...");
606 evsel__output_resort(pos, &prog);
607 ui_progress__finish();
b1dd4432 608
227ad323
NK
609 /*
610 * An event group needs to display other events too.
611 * Let's delay printing until other events are processed.
612 */
613 if (symbol_conf.event_group) {
614 if (!evsel__is_group_leader(pos)) {
615 struct hists *leader_hists;
616
617 leader_hists = evsel__hists(evsel__leader(pos));
618 hists__match(leader_hists, hists);
619 hists__link(leader_hists, hists);
620 }
b1dd4432 621 continue;
227ad323 622 }
b1dd4432 623
db8fd07a 624 hists__find_annotations(hists, pos, ann);
e248de33
ACM
625 }
626 }
8035e428 627
e248de33 628 if (total_nr_samples == 0) {
2d4f2799 629 ui__error("The %s data has no samples!\n", session->data->path);
fa10f316 630 goto out;
e248de33 631 }
7a60ba94 632
227ad323
NK
633 /* Display group events together */
634 evlist__for_each_entry(session->evlist, pos) {
635 struct hists *hists = evsel__hists(pos);
636 u32 nr_samples = hists->stats.nr_samples;
336989d0
NK
637 struct ui_progress prog;
638 struct evsel *evsel;
227ad323 639
336989d0 640 if (!symbol_conf.event_group || !evsel__is_group_leader(pos))
227ad323
NK
641 continue;
642
336989d0
NK
643 for_each_group_member(evsel, pos)
644 nr_samples += evsel__hists(evsel)->stats.nr_samples;
645
646 if (nr_samples == 0)
227ad323
NK
647 continue;
648
336989d0
NK
649 ui_progress__init(&prog, nr_samples,
650 "Sorting group events for output...");
651 evsel__output_resort(pos, &prog);
652 ui_progress__finish();
653
227ad323
NK
654 hists__find_annotations(hists, pos, ann);
655 }
656
fc67297b
NK
657 if (use_browser == 2) {
658 void (*show_annotations)(void);
659
660 show_annotations = dlsym(perf_gtk_handle,
661 "perf_gtk__show_annotations");
662 if (show_annotations == NULL) {
663 ui__error("GTK browser not found!\n");
fa10f316 664 goto out;
fc67297b
NK
665 }
666 show_annotations();
667 }
7a60ba94 668
fa10f316 669out:
bab81b62 670 return ret;
8035e428
IM
671}
672
cad10ce3
NK
673static int parse_percent_limit(const struct option *opt, const char *str,
674 int unset __maybe_unused)
675{
676 struct perf_annotate *ann = opt->value;
677 double pcnt = strtof(str, NULL);
678
679 ann->min_percent = pcnt;
680 return 0;
681}
682
263925bf
NK
683static int parse_data_type(const struct option *opt, const char *str, int unset)
684{
685 struct perf_annotate *ann = opt->value;
686
687 ann->data_type = !unset;
688 if (str)
689 ann->target_data_type = strdup(str);
690
691 return 0;
692}
693
8035e428 694static const char * const annotate_usage[] = {
99345254 695 "perf annotate [<options>]",
8035e428
IM
696 NULL
697};
698
b0ad8ea6 699int cmd_annotate(int argc, const char **argv)
d20deb64 700{
d48940ca 701 struct perf_annotate annotate = {};
8ceb41d7 702 struct perf_data data = {
fa10f316
NK
703 .mode = PERF_DATA_MODE_READ,
704 };
4bcbe438
YJ
705 struct itrace_synth_opts itrace_synth_opts = {
706 .set = 0,
707 };
57594454 708 const char *disassembler_style = NULL, *objdump_path = NULL, *addr2line_path = NULL;
1ac39372 709 struct option options[] = {
70cb4e96 710 OPT_STRING('i', "input", &input_name, "file",
8035e428 711 "input file name"),
ac73c5a9
ACM
712 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
713 "only consider symbols in these dsos"),
7009cc34 714 OPT_STRING('s', "symbol", &annotate.sym_hist_filter, "symbol",
0b73da3f 715 "symbol to annotate"),
8ceb41d7 716 OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"),
c0555642 717 OPT_INCR('v', "verbose", &verbose,
8035e428 718 "be more verbose (show symbol address, etc)"),
a527c2c1 719 OPT_BOOLEAN('q', "quiet", &quiet, "do now show any warnings or messages"),
8035e428
IM
720 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
721 "dump raw trace in ASCII"),
557cc18e 722#ifdef HAVE_GTK2_SUPPORT
2b676bf0 723 OPT_BOOLEAN(0, "gtk", &annotate.use_gtk, "Use the GTK interface"),
557cc18e 724#endif
3402ae0a 725#ifdef HAVE_SLANG_SUPPORT
7009cc34 726 OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"),
3402ae0a 727#endif
7009cc34 728 OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
befd2a38 729 OPT_BOOLEAN(0, "stdio2", &annotate.use_stdio2, "Use the stdio interface"),
be316409
ACM
730 OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
731 "don't load vmlinux even if found"),
b32d133a
ACM
732 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
733 "file", "vmlinux pathname"),
734 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
42976487 735 "load module symbols - WARNING: use only with -k and LIVE kernel"),
9d03194a 736 OPT_BOOLEAN('l', "print-line", &annotate_opts.print_lines,
301406b9 737 "print matching source lines (may be slow)"),
9d03194a 738 OPT_BOOLEAN('P', "full-paths", &annotate_opts.full_path,
42976487 739 "Don't shorten the displayed pathnames"),
18c9e5c5
NK
740 OPT_BOOLEAN(0, "skip-missing", &annotate.skip_missing,
741 "Skip symbols that cannot be annotated"),
7ebaf489
JY
742 OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group,
743 &annotate.group_set,
744 "Show event group information together"),
c8e66720 745 OPT_STRING('C', "cpu", &annotate.cpu_list, "cpu", "list of cpus to profile"),
a7066709
HK
746 OPT_CALLBACK(0, "symfs", NULL, "directory",
747 "Look for files with symbols relative to this directory",
748 symbol__config_symfs),
9d03194a 749 OPT_BOOLEAN(0, "source", &annotate_opts.annotate_src,
3e6a2a7f 750 "Interleave source code with assembly code (default)"),
9d03194a 751 OPT_BOOLEAN(0, "asm-raw", &annotate_opts.show_asm_raw,
3e6a2a7f 752 "Display raw encoding of assembly instructions (default)"),
56d9117c 753 OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
f69b64f7 754 "Specify disassembler style (e.g. -M intel for intel syntax)"),
9d03194a 755 OPT_STRING(0, "prefix", &annotate_opts.prefix, "prefix",
3b0b16bf 756 "Add prefix to source file path names in programs (with --prefix-strip)"),
9d03194a 757 OPT_STRING(0, "prefix-strip", &annotate_opts.prefix_strip, "N",
3b0b16bf 758 "Strip first N entries of source file path name in programs (with --prefix)"),
56d9117c 759 OPT_STRING(0, "objdump", &objdump_path, "path",
7a4ec938 760 "objdump binary to use for disassembly and annotations"),
57594454
IR
761 OPT_STRING(0, "addr2line", &addr2line_path, "path",
762 "addr2line binary to use for line numbers"),
3406ac53
ML
763 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
764 "Enable symbol demangling"),
765 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
766 "Enable kernel symbol demangling"),
0c4a5bce
ML
767 OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
768 "Show a column with the sum of periods"),
1ac39372
TS
769 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
770 "Show a column with the number of samples"),
53fe4ba1
ACM
771 OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
772 "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
773 stdio__config_color, "always"),
9d03194a 774 OPT_CALLBACK(0, "percent-type", &annotate_opts, "local-period",
88c21190
JO
775 "Set percent type local/global-period/hits",
776 annotate_parse_percent_type),
cad10ce3
NK
777 OPT_CALLBACK(0, "percent-limit", &annotate, "percent",
778 "Don't show entries under that percent", parse_percent_limit),
4bcbe438
YJ
779 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
780 "Instruction Tracing options\n" ITRACE_HELP,
781 itrace_parse_synth_opts),
263925bf
NK
782 OPT_CALLBACK_OPTARG(0, "data-type", &annotate, NULL, "name",
783 "Show data type annotate for the memory accesses",
784 parse_data_type),
61a9741e
NK
785 OPT_BOOLEAN(0, "type-stat", &annotate.type_stat,
786 "Show stats for the data type annotation"),
58824fa0
NK
787 OPT_BOOLEAN(0, "insn-stat", &annotate.insn_stat,
788 "Show instruction stats for the data type annotation"),
ce533c9b
NK
789 OPT_BOOLEAN(0, "skip-empty", &symbol_conf.skip_empty,
790 "Do not display empty (or dummy) events in the output"),
bbf006d6
NK
791 OPT_BOOLEAN(0, "code-with-type", &annotate_opts.code_with_type,
792 "Show data type info in code annotation (memory instructions only)"),
8035e428 793 OPT_END()
d20deb64 794 };
1ac39372
TS
795 int ret;
796
797 set_option_flag(options, 0, "show-total-period", PARSE_OPT_EXCLUSIVE);
798 set_option_flag(options, 0, "show-nr-samples", PARSE_OPT_EXCLUSIVE);
799
7f929aea 800 annotation_options__init();
a635fc51 801
1ac39372 802 ret = hists__init();
a635fc51
ACM
803 if (ret < 0)
804 return ret;
8035e428 805
7f929aea 806 annotation_config__init();
812b0f52 807
655000e7 808 argc = parse_options(argc, argv, options, annotate_usage, 0);
50e19ef9
NK
809 if (argc) {
810 /*
811 * Special case: if there's an argument left then assume that
812 * it's a symbol filter:
813 */
814 if (argc > 1)
815 usage_with_options(annotate_usage, options);
816
817 annotate.sym_hist_filter = argv[0];
818 }
655000e7 819
56d9117c 820 if (disassembler_style) {
9d03194a
NK
821 annotate_opts.disassembler_style = strdup(disassembler_style);
822 if (!annotate_opts.disassembler_style)
56d9117c
IR
823 return -ENOMEM;
824 }
825 if (objdump_path) {
9d03194a
NK
826 annotate_opts.objdump_path = strdup(objdump_path);
827 if (!annotate_opts.objdump_path)
56d9117c
IR
828 return -ENOMEM;
829 }
57594454
IR
830 if (addr2line_path) {
831 symbol_conf.addr2line_path = strdup(addr2line_path);
832 if (!symbol_conf.addr2line_path)
833 return -ENOMEM;
834 }
56d9117c 835
7f929aea 836 if (annotate_check_args() < 0)
3b0b16bf
AK
837 return -EINVAL;
838
557cc18e 839#ifdef HAVE_GTK2_SUPPORT
9cef4b0b
TS
840 if (symbol_conf.show_nr_samples && annotate.use_gtk) {
841 pr_err("--show-nr-samples is not available in --gtk mode at this time\n");
1ac39372
TS
842 return ret;
843 }
557cc18e 844#endif
1ac39372 845
8838abf6 846#ifndef HAVE_LIBDW_SUPPORT
263925bf
NK
847 if (annotate.data_type) {
848 pr_err("Error: Data type profiling is disabled due to missing DWARF support\n");
849 return -ENOTSUP;
850 }
851#endif
852
7cc72553
JC
853 ret = symbol__validate_sym_arguments();
854 if (ret)
855 return ret;
856
eddaef88
NK
857 if (quiet)
858 perf_quiet_option();
859
2d4f2799 860 data.path = input_name;
44848cdb 861
d48940ca
IR
862 perf_tool__init(&annotate.tool, /*ordered_events=*/true);
863 annotate.tool.sample = process_sample_event;
864 annotate.tool.mmap = perf_event__process_mmap;
865 annotate.tool.mmap2 = perf_event__process_mmap2;
866 annotate.tool.comm = perf_event__process_comm;
867 annotate.tool.exit = perf_event__process_exit;
868 annotate.tool.fork = perf_event__process_fork;
869 annotate.tool.namespaces = perf_event__process_namespaces;
870 annotate.tool.attr = perf_event__process_attr;
871 annotate.tool.build_id = perf_event__process_build_id;
872#ifdef HAVE_LIBTRACEEVENT
873 annotate.tool.tracing_data = perf_event__process_tracing_data;
874#endif
875 annotate.tool.id_index = perf_event__process_id_index;
876 annotate.tool.auxtrace_info = perf_event__process_auxtrace_info;
877 annotate.tool.auxtrace = perf_event__process_auxtrace;
878 annotate.tool.feature = process_feature_event;
879 annotate.tool.ordering_requires_timestamps = true;
880
2681bd85 881 annotate.session = perf_session__new(&data, &annotate.tool);
6ef81c55
MI
882 if (IS_ERR(annotate.session))
883 return PTR_ERR(annotate.session);
fa10f316 884
4bcbe438
YJ
885 annotate.session->itrace_synth_opts = &itrace_synth_opts;
886
bb848c14
JY
887 annotate.has_br_stack = perf_header__has_feat(&annotate.session->header,
888 HEADER_BRANCH_STACK);
889
7ebaf489 890 if (annotate.group_set)
64b4778b 891 evlist__force_leader(annotate.session->evlist);
7ebaf489 892
b01141f4
ACM
893 ret = symbol__annotation_init();
894 if (ret < 0)
895 goto out_delete;
896
75be6cf4
ACM
897 symbol_conf.try_vmlinux_path = true;
898
0a7e6d1b 899 ret = symbol__init(&annotate.session->header.env);
fa10f316
NK
900 if (ret < 0)
901 goto out_delete;
8035e428 902
befd2a38 903 if (annotate.use_stdio || annotate.use_stdio2)
3df668e7 904 use_browser = 0;
3402ae0a 905#ifdef HAVE_SLANG_SUPPORT
3df668e7
NK
906 else if (annotate.use_tui)
907 use_browser = 1;
3402ae0a 908#endif
557cc18e 909#ifdef HAVE_GTK2_SUPPORT
3df668e7
NK
910 else if (annotate.use_gtk)
911 use_browser = 2;
557cc18e 912#endif
3df668e7 913
263925bf 914 if (annotate.data_type) {
263925bf
NK
915 annotate_opts.annotate_src = false;
916 symbol_conf.annotate_data_member = true;
917 symbol_conf.annotate_data_sample = true;
bbf006d6
NK
918 } else if (annotate_opts.code_with_type) {
919 symbol_conf.annotate_data_member = true;
920
921 if (!annotate.use_stdio) {
922 pr_err("--code-with-type only works with --stdio.\n");
923 goto out_delete;
924 }
263925bf
NK
925 }
926
3df668e7
NK
927 setup_browser(true);
928
5676dba7
YJ
929 /*
930 * Events of different processes may correspond to the same
931 * symbol, we do not care about the processes in annotate,
932 * set sort order to avoid repeated output.
933 */
263925bf
NK
934 if (annotate.data_type)
935 sort_order = "dso,type";
936 else
937 sort_order = "dso,symbol";
5676dba7
YJ
938
939 /*
e6952dce
KL
940 * Set SORT_MODE__BRANCH so that annotate displays IPC/Cycle and
941 * branch counters, if the corresponding branch info is available
942 * in the perf data in the TUI mode.
5676dba7 943 */
e6952dce 944 if ((use_browser == 1 || annotate.use_stdio2) && annotate.has_br_stack) {
bb848c14 945 sort__mode = SORT_MODE__BRANCH;
e6952dce
KL
946 if (annotate.session->evlist->nr_br_cntr > 0)
947 annotate_opts.show_br_cntr = true;
948 }
5676dba7
YJ
949
950 if (setup_sorting(NULL) < 0)
951 usage_with_options(annotate_usage, options);
bb848c14 952
fa10f316
NK
953 ret = __cmd_annotate(&annotate);
954
955out_delete:
956 /*
333b1b11
IR
957 * Speed up the exit process by only deleting for debug builds. For
958 * large files this can save time.
fa10f316 959 */
333b1b11
IR
960#ifndef NDEBUG
961 perf_session__delete(annotate.session);
962#endif
7f929aea 963 annotation_options__exit();
333b1b11 964
fa10f316 965 return ret;
8035e428 966}