perf annotate: Introduce global annotation_options
[linux-2.6-microblaze.git] / tools / perf / builtin-annotate.c
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
11 #include "util/color.h"
12 #include <linux/list.h>
13 #include "util/cache.h"
14 #include <linux/rbtree.h>
15 #include <linux/zalloc.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/event.h"
24 #include <subcmd/parse-options.h>
25 #include "util/parse-events.h"
26 #include "util/sort.h"
27 #include "util/hist.h"
28 #include "util/dso.h"
29 #include "util/machine.h"
30 #include "util/map.h"
31 #include "util/session.h"
32 #include "util/tool.h"
33 #include "util/data.h"
34 #include "arch/common.h"
35 #include "util/block-range.h"
36 #include "util/map_symbol.h"
37 #include "util/branch.h"
38 #include "util/util.h"
39
40 #include <dlfcn.h>
41 #include <errno.h>
42 #include <linux/bitmap.h>
43 #include <linux/err.h>
44
45 struct perf_annotate {
46         struct perf_tool tool;
47         struct perf_session *session;
48 #ifdef HAVE_SLANG_SUPPORT
49         bool       use_tui;
50 #endif
51         bool       use_stdio, use_stdio2;
52 #ifdef HAVE_GTK2_SUPPORT
53         bool       use_gtk;
54 #endif
55         bool       skip_missing;
56         bool       has_br_stack;
57         bool       group_set;
58         float      min_percent;
59         const char *sym_hist_filter;
60         const char *cpu_list;
61         DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
62 };
63
64 /*
65  * Given one basic block:
66  *
67  *      from    to              branch_i
68  *      * ----> *
69  *              |
70  *              | block
71  *              v
72  *              * ----> *
73  *              from    to      branch_i+1
74  *
75  * where the horizontal are the branches and the vertical is the executed
76  * block of instructions.
77  *
78  * We count, for each 'instruction', the number of blocks that covered it as
79  * well as count the ratio each branch is taken.
80  *
81  * We can do this without knowing the actual instruction stream by keeping
82  * track of the address ranges. We break down ranges such that there is no
83  * overlap and iterate from the start until the end.
84  *
85  * @acme: once we parse the objdump output _before_ processing the samples,
86  * we can easily fold the branch.cycles IPC bits in.
87  */
88 static void process_basic_block(struct addr_map_symbol *start,
89                                 struct addr_map_symbol *end,
90                                 struct branch_flags *flags)
91 {
92         struct symbol *sym = start->ms.sym;
93         struct annotation *notes = sym ? symbol__annotation(sym) : NULL;
94         struct block_range_iter iter;
95         struct block_range *entry;
96         struct annotated_branch *branch;
97
98         /*
99          * Sanity; NULL isn't executable and the CPU cannot execute backwards
100          */
101         if (!start->addr || start->addr > end->addr)
102                 return;
103
104         iter = block_range__create(start->addr, end->addr);
105         if (!block_range_iter__valid(&iter))
106                 return;
107
108         branch = annotation__get_branch(notes);
109
110         /*
111          * First block in range is a branch target.
112          */
113         entry = block_range_iter(&iter);
114         assert(entry->is_target);
115         entry->entry++;
116
117         do {
118                 entry = block_range_iter(&iter);
119
120                 entry->coverage++;
121                 entry->sym = sym;
122
123                 if (branch)
124                         branch->max_coverage = max(branch->max_coverage, entry->coverage);
125
126         } while (block_range_iter__next(&iter));
127
128         /*
129          * Last block in rage is a branch.
130          */
131         entry = block_range_iter(&iter);
132         assert(entry->is_branch);
133         entry->taken++;
134         if (flags->predicted)
135                 entry->pred++;
136 }
137
138 static void process_branch_stack(struct branch_stack *bs, struct addr_location *al,
139                                  struct perf_sample *sample)
140 {
141         struct addr_map_symbol *prev = NULL;
142         struct branch_info *bi;
143         int i;
144
145         if (!bs || !bs->nr)
146                 return;
147
148         bi = sample__resolve_bstack(sample, al);
149         if (!bi)
150                 return;
151
152         for (i = bs->nr - 1; i >= 0; i--) {
153                 /*
154                  * XXX filter against symbol
155                  */
156                 if (prev)
157                         process_basic_block(prev, &bi[i].from, &bi[i].flags);
158                 prev = &bi[i].to;
159         }
160
161         free(bi);
162 }
163
164 static int hist_iter__branch_callback(struct hist_entry_iter *iter,
165                                       struct addr_location *al __maybe_unused,
166                                       bool single __maybe_unused,
167                                       void *arg __maybe_unused)
168 {
169         struct hist_entry *he = iter->he;
170         struct branch_info *bi;
171         struct perf_sample *sample = iter->sample;
172         struct evsel *evsel = iter->evsel;
173         int err;
174
175         bi = he->branch_info;
176         err = addr_map_symbol__inc_samples(&bi->from, sample, evsel);
177
178         if (err)
179                 goto out;
180
181         err = addr_map_symbol__inc_samples(&bi->to, sample, evsel);
182
183 out:
184         return err;
185 }
186
187 static int process_branch_callback(struct evsel *evsel,
188                                    struct perf_sample *sample,
189                                    struct addr_location *al,
190                                    struct perf_annotate *ann,
191                                    struct machine *machine)
192 {
193         struct hist_entry_iter iter = {
194                 .evsel          = evsel,
195                 .sample         = sample,
196                 .add_entry_cb   = hist_iter__branch_callback,
197                 .hide_unresolved        = symbol_conf.hide_unresolved,
198                 .ops            = &hist_iter_branch,
199         };
200         struct addr_location a;
201         int ret;
202
203         addr_location__init(&a);
204         if (machine__resolve(machine, &a, sample) < 0) {
205                 ret = -1;
206                 goto out;
207         }
208
209         if (a.sym == NULL) {
210                 ret = 0;
211                 goto out;
212         }
213
214         if (a.map != NULL)
215                 map__dso(a.map)->hit = 1;
216
217         hist__account_cycles(sample->branch_stack, al, sample, false, NULL);
218
219         ret = hist_entry_iter__add(&iter, &a, PERF_MAX_STACK_DEPTH, ann);
220 out:
221         addr_location__exit(&a);
222         return ret;
223 }
224
225 static bool has_annotation(struct perf_annotate *ann)
226 {
227         return ui__has_annotation() || ann->use_stdio2;
228 }
229
230 static int evsel__add_sample(struct evsel *evsel, struct perf_sample *sample,
231                              struct addr_location *al, struct perf_annotate *ann,
232                              struct machine *machine)
233 {
234         struct hists *hists = evsel__hists(evsel);
235         struct hist_entry *he;
236         int ret;
237
238         if ((!ann->has_br_stack || !has_annotation(ann)) &&
239             ann->sym_hist_filter != NULL &&
240             (al->sym == NULL ||
241              strcmp(ann->sym_hist_filter, al->sym->name) != 0)) {
242                 /* We're only interested in a symbol named sym_hist_filter */
243                 /*
244                  * FIXME: why isn't this done in the symbol_filter when loading
245                  * the DSO?
246                  */
247                 if (al->sym != NULL) {
248                         struct dso *dso = map__dso(al->map);
249
250                         rb_erase_cached(&al->sym->rb_node, &dso->symbols);
251                         symbol__delete(al->sym);
252                         dso__reset_find_symbol_cache(dso);
253                 }
254                 return 0;
255         }
256
257         /*
258          * XXX filtered samples can still have branch entries pointing into our
259          * symbol and are missed.
260          */
261         process_branch_stack(sample->branch_stack, al, sample);
262
263         if (ann->has_br_stack && has_annotation(ann))
264                 return process_branch_callback(evsel, sample, al, ann, machine);
265
266         he = hists__add_entry(hists, al, NULL, NULL, NULL, NULL, sample, true);
267         if (he == NULL)
268                 return -ENOMEM;
269
270         ret = hist_entry__inc_addr_samples(he, sample, evsel, al->addr);
271         hists__inc_nr_samples(hists, true);
272         return ret;
273 }
274
275 static int process_sample_event(struct perf_tool *tool,
276                                 union perf_event *event,
277                                 struct perf_sample *sample,
278                                 struct evsel *evsel,
279                                 struct machine *machine)
280 {
281         struct perf_annotate *ann = container_of(tool, struct perf_annotate, tool);
282         struct addr_location al;
283         int ret = 0;
284
285         addr_location__init(&al);
286         if (machine__resolve(machine, &al, sample) < 0) {
287                 pr_warning("problem processing %d event, skipping it.\n",
288                            event->header.type);
289                 ret = -1;
290                 goto out_put;
291         }
292
293         if (ann->cpu_list && !test_bit(sample->cpu, ann->cpu_bitmap))
294                 goto out_put;
295
296         if (!al.filtered &&
297             evsel__add_sample(evsel, sample, &al, ann, machine)) {
298                 pr_warning("problem incrementing symbol count, "
299                            "skipping event\n");
300                 ret = -1;
301         }
302 out_put:
303         addr_location__exit(&al);
304         return ret;
305 }
306
307 static int process_feature_event(struct perf_session *session,
308                                  union perf_event *event)
309 {
310         if (event->feat.feat_id < HEADER_LAST_FEATURE)
311                 return perf_event__process_feature(session, event);
312         return 0;
313 }
314
315 static int hist_entry__tty_annotate(struct hist_entry *he,
316                                     struct evsel *evsel,
317                                     struct perf_annotate *ann)
318 {
319         if (!ann->use_stdio2)
320                 return symbol__tty_annotate(&he->ms, evsel, &annotate_opts);
321
322         return symbol__tty_annotate2(&he->ms, evsel, &annotate_opts);
323 }
324
325 static void hists__find_annotations(struct hists *hists,
326                                     struct evsel *evsel,
327                                     struct perf_annotate *ann)
328 {
329         struct rb_node *nd = rb_first_cached(&hists->entries), *next;
330         int key = K_RIGHT;
331
332         while (nd) {
333                 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
334                 struct annotation *notes;
335
336                 if (he->ms.sym == NULL || map__dso(he->ms.map)->annotate_warned)
337                         goto find_next;
338
339                 if (ann->sym_hist_filter &&
340                     (strcmp(he->ms.sym->name, ann->sym_hist_filter) != 0))
341                         goto find_next;
342
343                 if (ann->min_percent) {
344                         float percent = 0;
345                         u64 total = hists__total_period(hists);
346
347                         if (total)
348                                 percent = 100.0 * he->stat.period / total;
349
350                         if (percent < ann->min_percent)
351                                 goto find_next;
352                 }
353
354                 notes = symbol__annotation(he->ms.sym);
355                 if (notes->src == NULL) {
356 find_next:
357                         if (key == K_LEFT || key == '<')
358                                 nd = rb_prev(nd);
359                         else
360                                 nd = rb_next(nd);
361                         continue;
362                 }
363
364                 if (use_browser == 2) {
365                         int ret;
366                         int (*annotate)(struct hist_entry *he,
367                                         struct evsel *evsel,
368                                         struct annotation_options *options,
369                                         struct hist_browser_timer *hbt);
370
371                         annotate = dlsym(perf_gtk_handle,
372                                          "hist_entry__gtk_annotate");
373                         if (annotate == NULL) {
374                                 ui__error("GTK browser not found!\n");
375                                 return;
376                         }
377
378                         ret = annotate(he, evsel, &annotate_opts, NULL);
379                         if (!ret || !ann->skip_missing)
380                                 return;
381
382                         /* skip missing symbols */
383                         nd = rb_next(nd);
384                 } else if (use_browser == 1) {
385                         key = hist_entry__tui_annotate(he, evsel, NULL, &annotate_opts);
386
387                         switch (key) {
388                         case -1:
389                                 if (!ann->skip_missing)
390                                         return;
391                                 /* fall through */
392                         case K_RIGHT:
393                         case '>':
394                                 next = rb_next(nd);
395                                 break;
396                         case K_LEFT:
397                         case '<':
398                                 next = rb_prev(nd);
399                                 break;
400                         default:
401                                 return;
402                         }
403
404                         if (next != NULL)
405                                 nd = next;
406                 } else {
407                         hist_entry__tty_annotate(he, evsel, ann);
408                         nd = rb_next(nd);
409                 }
410         }
411 }
412
413 static int __cmd_annotate(struct perf_annotate *ann)
414 {
415         int ret;
416         struct perf_session *session = ann->session;
417         struct evsel *pos;
418         u64 total_nr_samples;
419
420         if (ann->cpu_list) {
421                 ret = perf_session__cpu_bitmap(session, ann->cpu_list,
422                                                ann->cpu_bitmap);
423                 if (ret)
424                         goto out;
425         }
426
427         if (!annotate_opts.objdump_path) {
428                 ret = perf_env__lookup_objdump(&session->header.env,
429                                                &annotate_opts.objdump_path);
430                 if (ret)
431                         goto out;
432         }
433
434         ret = perf_session__process_events(session);
435         if (ret)
436                 goto out;
437
438         if (dump_trace) {
439                 perf_session__fprintf_nr_events(session, stdout, false);
440                 evlist__fprintf_nr_events(session->evlist, stdout, false);
441                 goto out;
442         }
443
444         if (verbose > 3)
445                 perf_session__fprintf(session, stdout);
446
447         if (verbose > 2)
448                 perf_session__fprintf_dsos(session, stdout);
449
450         total_nr_samples = 0;
451         evlist__for_each_entry(session->evlist, pos) {
452                 struct hists *hists = evsel__hists(pos);
453                 u32 nr_samples = hists->stats.nr_samples;
454
455                 if (nr_samples > 0) {
456                         total_nr_samples += nr_samples;
457                         hists__collapse_resort(hists, NULL);
458                         /* Don't sort callchain */
459                         evsel__reset_sample_bit(pos, CALLCHAIN);
460                         evsel__output_resort(pos, NULL);
461
462                         if (symbol_conf.event_group && !evsel__is_group_leader(pos))
463                                 continue;
464
465                         hists__find_annotations(hists, pos, ann);
466                 }
467         }
468
469         if (total_nr_samples == 0) {
470                 ui__error("The %s data has no samples!\n", session->data->path);
471                 goto out;
472         }
473
474         if (use_browser == 2) {
475                 void (*show_annotations)(void);
476
477                 show_annotations = dlsym(perf_gtk_handle,
478                                          "perf_gtk__show_annotations");
479                 if (show_annotations == NULL) {
480                         ui__error("GTK browser not found!\n");
481                         goto out;
482                 }
483                 show_annotations();
484         }
485
486 out:
487         return ret;
488 }
489
490 static int parse_percent_limit(const struct option *opt, const char *str,
491                                int unset __maybe_unused)
492 {
493         struct perf_annotate *ann = opt->value;
494         double pcnt = strtof(str, NULL);
495
496         ann->min_percent = pcnt;
497         return 0;
498 }
499
500 static const char * const annotate_usage[] = {
501         "perf annotate [<options>]",
502         NULL
503 };
504
505 int cmd_annotate(int argc, const char **argv)
506 {
507         struct perf_annotate annotate = {
508                 .tool = {
509                         .sample = process_sample_event,
510                         .mmap   = perf_event__process_mmap,
511                         .mmap2  = perf_event__process_mmap2,
512                         .comm   = perf_event__process_comm,
513                         .exit   = perf_event__process_exit,
514                         .fork   = perf_event__process_fork,
515                         .namespaces = perf_event__process_namespaces,
516                         .attr   = perf_event__process_attr,
517                         .build_id = perf_event__process_build_id,
518 #ifdef HAVE_LIBTRACEEVENT
519                         .tracing_data   = perf_event__process_tracing_data,
520 #endif
521                         .id_index       = perf_event__process_id_index,
522                         .auxtrace_info  = perf_event__process_auxtrace_info,
523                         .auxtrace       = perf_event__process_auxtrace,
524                         .feature        = process_feature_event,
525                         .ordered_events = true,
526                         .ordering_requires_timestamps = true,
527                 },
528         };
529         struct perf_data data = {
530                 .mode  = PERF_DATA_MODE_READ,
531         };
532         struct itrace_synth_opts itrace_synth_opts = {
533                 .set = 0,
534         };
535         const char *disassembler_style = NULL, *objdump_path = NULL, *addr2line_path = NULL;
536         struct option options[] = {
537         OPT_STRING('i', "input", &input_name, "file",
538                     "input file name"),
539         OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
540                    "only consider symbols in these dsos"),
541         OPT_STRING('s', "symbol", &annotate.sym_hist_filter, "symbol",
542                     "symbol to annotate"),
543         OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"),
544         OPT_INCR('v', "verbose", &verbose,
545                     "be more verbose (show symbol address, etc)"),
546         OPT_BOOLEAN('q', "quiet", &quiet, "do now show any warnings or messages"),
547         OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
548                     "dump raw trace in ASCII"),
549 #ifdef HAVE_GTK2_SUPPORT
550         OPT_BOOLEAN(0, "gtk", &annotate.use_gtk, "Use the GTK interface"),
551 #endif
552 #ifdef HAVE_SLANG_SUPPORT
553         OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"),
554 #endif
555         OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
556         OPT_BOOLEAN(0, "stdio2", &annotate.use_stdio2, "Use the stdio interface"),
557         OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
558                     "don't load vmlinux even if found"),
559         OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
560                    "file", "vmlinux pathname"),
561         OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
562                     "load module symbols - WARNING: use only with -k and LIVE kernel"),
563         OPT_BOOLEAN('l', "print-line", &annotate_opts.print_lines,
564                     "print matching source lines (may be slow)"),
565         OPT_BOOLEAN('P', "full-paths", &annotate_opts.full_path,
566                     "Don't shorten the displayed pathnames"),
567         OPT_BOOLEAN(0, "skip-missing", &annotate.skip_missing,
568                     "Skip symbols that cannot be annotated"),
569         OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group,
570                         &annotate.group_set,
571                         "Show event group information together"),
572         OPT_STRING('C', "cpu", &annotate.cpu_list, "cpu", "list of cpus to profile"),
573         OPT_CALLBACK(0, "symfs", NULL, "directory",
574                      "Look for files with symbols relative to this directory",
575                      symbol__config_symfs),
576         OPT_BOOLEAN(0, "source", &annotate_opts.annotate_src,
577                     "Interleave source code with assembly code (default)"),
578         OPT_BOOLEAN(0, "asm-raw", &annotate_opts.show_asm_raw,
579                     "Display raw encoding of assembly instructions (default)"),
580         OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
581                    "Specify disassembler style (e.g. -M intel for intel syntax)"),
582         OPT_STRING(0, "prefix", &annotate_opts.prefix, "prefix",
583                     "Add prefix to source file path names in programs (with --prefix-strip)"),
584         OPT_STRING(0, "prefix-strip", &annotate_opts.prefix_strip, "N",
585                     "Strip first N entries of source file path name in programs (with --prefix)"),
586         OPT_STRING(0, "objdump", &objdump_path, "path",
587                    "objdump binary to use for disassembly and annotations"),
588         OPT_STRING(0, "addr2line", &addr2line_path, "path",
589                    "addr2line binary to use for line numbers"),
590         OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
591                     "Enable symbol demangling"),
592         OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
593                     "Enable kernel symbol demangling"),
594         OPT_BOOLEAN(0, "group", &symbol_conf.event_group,
595                     "Show event group information together"),
596         OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
597                     "Show a column with the sum of periods"),
598         OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
599                     "Show a column with the number of samples"),
600         OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
601                              "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
602                              stdio__config_color, "always"),
603         OPT_CALLBACK(0, "percent-type", &annotate_opts, "local-period",
604                      "Set percent type local/global-period/hits",
605                      annotate_parse_percent_type),
606         OPT_CALLBACK(0, "percent-limit", &annotate, "percent",
607                      "Don't show entries under that percent", parse_percent_limit),
608         OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
609                             "Instruction Tracing options\n" ITRACE_HELP,
610                             itrace_parse_synth_opts),
611
612         OPT_END()
613         };
614         int ret;
615
616         set_option_flag(options, 0, "show-total-period", PARSE_OPT_EXCLUSIVE);
617         set_option_flag(options, 0, "show-nr-samples", PARSE_OPT_EXCLUSIVE);
618
619         annotation_options__init(&annotate_opts);
620
621         ret = hists__init();
622         if (ret < 0)
623                 return ret;
624
625         annotation_config__init(&annotate_opts);
626
627         argc = parse_options(argc, argv, options, annotate_usage, 0);
628         if (argc) {
629                 /*
630                  * Special case: if there's an argument left then assume that
631                  * it's a symbol filter:
632                  */
633                 if (argc > 1)
634                         usage_with_options(annotate_usage, options);
635
636                 annotate.sym_hist_filter = argv[0];
637         }
638
639         if (disassembler_style) {
640                 annotate_opts.disassembler_style = strdup(disassembler_style);
641                 if (!annotate_opts.disassembler_style)
642                         return -ENOMEM;
643         }
644         if (objdump_path) {
645                 annotate_opts.objdump_path = strdup(objdump_path);
646                 if (!annotate_opts.objdump_path)
647                         return -ENOMEM;
648         }
649         if (addr2line_path) {
650                 symbol_conf.addr2line_path = strdup(addr2line_path);
651                 if (!symbol_conf.addr2line_path)
652                         return -ENOMEM;
653         }
654
655         if (annotate_check_args(&annotate_opts) < 0)
656                 return -EINVAL;
657
658 #ifdef HAVE_GTK2_SUPPORT
659         if (symbol_conf.show_nr_samples && annotate.use_gtk) {
660                 pr_err("--show-nr-samples is not available in --gtk mode at this time\n");
661                 return ret;
662         }
663 #endif
664
665         ret = symbol__validate_sym_arguments();
666         if (ret)
667                 return ret;
668
669         if (quiet)
670                 perf_quiet_option();
671
672         data.path = input_name;
673
674         annotate.session = perf_session__new(&data, &annotate.tool);
675         if (IS_ERR(annotate.session))
676                 return PTR_ERR(annotate.session);
677
678         annotate.session->itrace_synth_opts = &itrace_synth_opts;
679
680         annotate.has_br_stack = perf_header__has_feat(&annotate.session->header,
681                                                       HEADER_BRANCH_STACK);
682
683         if (annotate.group_set)
684                 evlist__force_leader(annotate.session->evlist);
685
686         ret = symbol__annotation_init();
687         if (ret < 0)
688                 goto out_delete;
689
690         symbol_conf.try_vmlinux_path = true;
691
692         ret = symbol__init(&annotate.session->header.env);
693         if (ret < 0)
694                 goto out_delete;
695
696         if (annotate.use_stdio || annotate.use_stdio2)
697                 use_browser = 0;
698 #ifdef HAVE_SLANG_SUPPORT
699         else if (annotate.use_tui)
700                 use_browser = 1;
701 #endif
702 #ifdef HAVE_GTK2_SUPPORT
703         else if (annotate.use_gtk)
704                 use_browser = 2;
705 #endif
706
707         setup_browser(true);
708
709         /*
710          * Events of different processes may correspond to the same
711          * symbol, we do not care about the processes in annotate,
712          * set sort order to avoid repeated output.
713          */
714         sort_order = "dso,symbol";
715
716         /*
717          * Set SORT_MODE__BRANCH so that annotate display IPC/Cycle
718          * if branch info is in perf data in TUI mode.
719          */
720         if ((use_browser == 1 || annotate.use_stdio2) && annotate.has_br_stack)
721                 sort__mode = SORT_MODE__BRANCH;
722
723         if (setup_sorting(NULL) < 0)
724                 usage_with_options(annotate_usage, options);
725
726         ret = __cmd_annotate(&annotate);
727
728 out_delete:
729         /*
730          * Speed up the exit process by only deleting for debug builds. For
731          * large files this can save time.
732          */
733 #ifndef NDEBUG
734         perf_session__delete(annotate.session);
735 #endif
736         annotation_options__exit(&annotate_opts);
737
738         return ret;
739 }