projects/linux·blob master · refs

953 lines · 25.1 KiB

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