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.
10 #include "util/util.h"
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"
19 #include "util/debug.h"
21 #include "util/event.h"
22 #include "util/parse-options.h"
23 #include "util/parse-events.h"
24 #include "util/thread.h"
25 #include "util/sort.h"
26 #include "util/hist.h"
27 #include "util/session.h"
29 static char const *input_name
= "perf.data";
31 static bool force
, use_tui
, use_stdio
;
33 static bool full_paths
;
35 static bool print_line
;
37 static const char *sym_hist_filter
;
39 static int hists__add_entry(struct hists
*self
, struct addr_location
*al
)
41 struct hist_entry
*he
;
43 if (sym_hist_filter
!= NULL
&&
44 (al
->sym
== NULL
|| strcmp(sym_hist_filter
, al
->sym
->name
) != 0)) {
45 /* We're only interested in a symbol named sym_hist_filter */
46 if (al
->sym
!= NULL
) {
47 rb_erase(&al
->sym
->rb_node
,
48 &al
->map
->dso
->symbols
[al
->map
->type
]);
49 symbol__delete(al
->sym
);
54 he
= __hists__add_entry(self
, al
, NULL
, 1);
58 return hist_entry__inc_addr_samples(he
, al
->addr
);
61 static int process_sample_event(union perf_event
*event
,
62 struct perf_sample
*sample
,
63 struct perf_session
*session
)
65 struct addr_location al
;
67 if (perf_event__preprocess_sample(event
, session
, &al
, sample
, NULL
) < 0) {
68 pr_warning("problem processing %d event, skipping it.\n",
73 if (!al
.filtered
&& hists__add_entry(&session
->hists
, &al
)) {
74 pr_warning("problem incrementing symbol count, "
82 static int objdump_line__print(struct objdump_line
*self
,
83 struct list_head
*head
,
84 struct hist_entry
*he
, u64 len
)
86 struct symbol
*sym
= he
->ms
.sym
;
87 static const char *prev_line
;
88 static const char *prev_color
;
90 if (self
->offset
!= -1) {
91 const char *path
= NULL
;
92 unsigned int hits
= 0;
95 struct sym_priv
*priv
= symbol__priv(sym
);
96 struct sym_ext
*sym_ext
= priv
->ext
;
97 struct sym_hist
*h
= priv
->hist
;
98 s64 offset
= self
->offset
;
99 struct objdump_line
*next
= objdump__get_next_ip_line(head
, self
);
101 while (offset
< (s64
)len
&&
102 (next
== NULL
|| offset
< next
->offset
)) {
105 path
= sym_ext
[offset
].path
;
106 percent
+= sym_ext
[offset
].percent
;
108 hits
+= h
->ip
[offset
];
113 if (sym_ext
== NULL
&& h
->sum
)
114 percent
= 100.0 * hits
/ h
->sum
;
116 color
= get_percent_color(percent
);
119 * Also color the filename and line if needed, with
120 * the same color than the percentage. Don't print it
121 * twice for close colored ip with the same filename:line
124 if (!prev_line
|| strcmp(prev_line
, path
)
125 || color
!= prev_color
) {
126 color_fprintf(stdout
, color
, " %s", path
);
132 color_fprintf(stdout
, color
, " %7.2f", percent
);
134 color_fprintf(stdout
, PERF_COLOR_BLUE
, "%s\n", self
->line
);
139 printf(" : %s\n", self
->line
);
145 static struct rb_root root_sym_ext
;
147 static void insert_source_line(struct sym_ext
*sym_ext
)
149 struct sym_ext
*iter
;
150 struct rb_node
**p
= &root_sym_ext
.rb_node
;
151 struct rb_node
*parent
= NULL
;
155 iter
= rb_entry(parent
, struct sym_ext
, node
);
157 if (sym_ext
->percent
> iter
->percent
)
163 rb_link_node(&sym_ext
->node
, parent
, p
);
164 rb_insert_color(&sym_ext
->node
, &root_sym_ext
);
167 static void free_source_line(struct hist_entry
*he
, int len
)
169 struct sym_priv
*priv
= symbol__priv(he
->ms
.sym
);
170 struct sym_ext
*sym_ext
= priv
->ext
;
176 for (i
= 0; i
< len
; i
++)
177 free(sym_ext
[i
].path
);
181 root_sym_ext
= RB_ROOT
;
184 /* Get the filename:line for the colored entries */
186 get_source_line(struct hist_entry
*he
, int len
, const char *filename
)
188 struct symbol
*sym
= he
->ms
.sym
;
191 char cmd
[PATH_MAX
* 2];
192 struct sym_ext
*sym_ext
;
193 struct sym_priv
*priv
= symbol__priv(sym
);
194 struct sym_hist
*h
= priv
->hist
;
199 sym_ext
= priv
->ext
= calloc(len
, sizeof(struct sym_ext
));
203 start
= he
->ms
.map
->unmap_ip(he
->ms
.map
, sym
->start
);
205 for (i
= 0; i
< len
; i
++) {
211 sym_ext
[i
].percent
= 100.0 * h
->ip
[i
] / h
->sum
;
212 if (sym_ext
[i
].percent
<= 0.5)
216 sprintf(cmd
, "addr2line -e %s %016" PRIx64
, filename
, offset
);
217 fp
= popen(cmd
, "r");
221 if (getline(&path
, &line_len
, fp
) < 0 || !line_len
)
224 sym_ext
[i
].path
= malloc(sizeof(char) * line_len
+ 1);
225 if (!sym_ext
[i
].path
)
228 strcpy(sym_ext
[i
].path
, path
);
229 insert_source_line(&sym_ext
[i
]);
236 static void print_summary(const char *filename
)
238 struct sym_ext
*sym_ext
;
239 struct rb_node
*node
;
241 printf("\nSorted summary for file %s\n", filename
);
242 printf("----------------------------------------------\n\n");
244 if (RB_EMPTY_ROOT(&root_sym_ext
)) {
245 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN
);
249 node
= rb_first(&root_sym_ext
);
255 sym_ext
= rb_entry(node
, struct sym_ext
, node
);
256 percent
= sym_ext
->percent
;
257 color
= get_percent_color(percent
);
258 path
= sym_ext
->path
;
260 color_fprintf(stdout
, color
, " %7.2f %s", percent
, path
);
261 node
= rb_next(node
);
265 static void hist_entry__print_hits(struct hist_entry
*self
)
267 struct symbol
*sym
= self
->ms
.sym
;
268 struct sym_priv
*priv
= symbol__priv(sym
);
269 struct sym_hist
*h
= priv
->hist
;
270 u64 len
= sym
->end
- sym
->start
, offset
;
272 for (offset
= 0; offset
< len
; ++offset
)
273 if (h
->ip
[offset
] != 0)
274 printf("%*" PRIx64
": %" PRIu64
"\n", BITS_PER_LONG
/ 2,
275 sym
->start
+ offset
, h
->ip
[offset
]);
276 printf("%*s: %" PRIu64
"\n", BITS_PER_LONG
/ 2, "h->sum", h
->sum
);
279 static int hist_entry__tty_annotate(struct hist_entry
*he
)
281 struct map
*map
= he
->ms
.map
;
282 struct dso
*dso
= map
->dso
;
283 struct symbol
*sym
= he
->ms
.sym
;
284 const char *filename
= dso
->long_name
, *d_filename
;
287 struct objdump_line
*pos
, *n
;
289 if (hist_entry__annotate(he
, &head
, 0) < 0)
293 d_filename
= filename
;
295 d_filename
= basename(filename
);
297 len
= sym
->end
- sym
->start
;
300 get_source_line(he
, len
, filename
);
301 print_summary(filename
);
304 printf("\n\n------------------------------------------------\n");
305 printf(" Percent | Source code & Disassembly of %s\n", d_filename
);
306 printf("------------------------------------------------\n");
309 hist_entry__print_hits(he
);
311 list_for_each_entry_safe(pos
, n
, &head
, node
) {
312 objdump_line__print(pos
, &head
, he
, len
);
313 list_del(&pos
->node
);
314 objdump_line__free(pos
);
318 free_source_line(he
, len
);
323 static void hists__find_annotations(struct hists
*self
)
325 struct rb_node
*nd
= rb_first(&self
->entries
), *next
;
329 struct hist_entry
*he
= rb_entry(nd
, struct hist_entry
, rb_node
);
330 struct sym_priv
*priv
;
332 if (he
->ms
.sym
== NULL
|| he
->ms
.map
->dso
->annotate_warned
)
335 priv
= symbol__priv(he
->ms
.sym
);
336 if (priv
->hist
== NULL
) {
345 if (use_browser
> 0) {
346 key
= hist_entry__tui_annotate(he
);
361 hist_entry__tty_annotate(he
);
364 * Since we have a hist_entry per IP for the same
365 * symbol, free he->ms.sym->hist to signal we already
366 * processed this symbol.
374 static struct perf_event_ops event_ops
= {
375 .sample
= process_sample_event
,
376 .mmap
= perf_event__process_mmap
,
377 .comm
= perf_event__process_comm
,
378 .fork
= perf_event__process_task
,
379 .ordered_samples
= true,
380 .ordering_requires_timestamps
= true,
383 static int __cmd_annotate(void)
386 struct perf_session
*session
;
388 session
= perf_session__new(input_name
, O_RDONLY
, force
, false, &event_ops
);
392 ret
= perf_session__process_events(session
, &event_ops
);
397 perf_session__fprintf_nr_events(session
, stdout
);
402 perf_session__fprintf(session
, stdout
);
405 perf_session__fprintf_dsos(session
, stdout
);
407 hists__collapse_resort(&session
->hists
);
408 hists__output_resort(&session
->hists
);
409 hists__find_annotations(&session
->hists
);
411 perf_session__delete(session
);
416 static const char * const annotate_usage
[] = {
417 "perf annotate [<options>] <command>",
421 static const struct option options
[] = {
422 OPT_STRING('i', "input", &input_name
, "file",
424 OPT_STRING('d', "dsos", &symbol_conf
.dso_list_str
, "dso[,dso...]",
425 "only consider symbols in these dsos"),
426 OPT_STRING('s', "symbol", &sym_hist_filter
, "symbol",
427 "symbol to annotate"),
428 OPT_BOOLEAN('f', "force", &force
, "don't complain, do it"),
429 OPT_INCR('v', "verbose", &verbose
,
430 "be more verbose (show symbol address, etc)"),
431 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
432 "dump raw trace in ASCII"),
433 OPT_BOOLEAN(0, "tui", &use_tui
, "Use the TUI interface"),
434 OPT_BOOLEAN(0, "stdio", &use_stdio
, "Use the stdio interface"),
435 OPT_STRING('k', "vmlinux", &symbol_conf
.vmlinux_name
,
436 "file", "vmlinux pathname"),
437 OPT_BOOLEAN('m', "modules", &symbol_conf
.use_modules
,
438 "load module symbols - WARNING: use only with -k and LIVE kernel"),
439 OPT_BOOLEAN('l', "print-line", &print_line
,
440 "print matching source lines (may be slow)"),
441 OPT_BOOLEAN('P', "full-paths", &full_paths
,
442 "Don't shorten the displayed pathnames"),
446 int cmd_annotate(int argc
, const char **argv
, const char *prefix __used
)
448 argc
= parse_options(argc
, argv
, options
, annotate_usage
, 0);
457 symbol_conf
.priv_size
= sizeof(struct sym_priv
);
458 symbol_conf
.try_vmlinux_path
= true;
460 if (symbol__init() < 0)
463 setup_sorting(annotate_usage
, options
);
467 * Special case: if there's an argument left then assume tha
468 * it's a symbol filter:
471 usage_with_options(annotate_usage
, options
);
473 sym_hist_filter
= argv
[0];
476 if (field_sep
&& *field_sep
== '.') {
477 pr_err("'.' is the only non valid --field-separator argument\n");
481 return __cmd_annotate();