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";
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(event_t
*event
, struct perf_session
*session
)
63 struct addr_location al
;
65 dump_printf("(IP, %d): %d: %#Lx\n", event
->header
.misc
,
66 event
->ip
.pid
, event
->ip
.ip
);
68 if (event__preprocess_sample(event
, session
, &al
, NULL
) < 0) {
69 pr_warning("problem processing %d event, skipping it.\n",
74 if (!al
.filtered
&& hists__add_entry(&session
->hists
, &al
)) {
75 pr_warning("problem incrementing symbol count, "
83 static int objdump_line__print(struct objdump_line
*self
,
84 struct list_head
*head
,
85 struct hist_entry
*he
, u64 len
)
87 struct symbol
*sym
= he
->ms
.sym
;
88 static const char *prev_line
;
89 static const char *prev_color
;
91 if (self
->offset
!= -1) {
92 const char *path
= NULL
;
93 unsigned int hits
= 0;
96 struct sym_priv
*priv
= symbol__priv(sym
);
97 struct sym_ext
*sym_ext
= priv
->ext
;
98 struct sym_hist
*h
= priv
->hist
;
99 s64 offset
= self
->offset
;
100 struct objdump_line
*next
= objdump__get_next_ip_line(head
, self
);
102 while (offset
< (s64
)len
&&
103 (next
== NULL
|| offset
< next
->offset
)) {
106 path
= sym_ext
[offset
].path
;
107 percent
+= sym_ext
[offset
].percent
;
109 hits
+= h
->ip
[offset
];
114 if (sym_ext
== NULL
&& h
->sum
)
115 percent
= 100.0 * hits
/ h
->sum
;
117 color
= get_percent_color(percent
);
120 * Also color the filename and line if needed, with
121 * the same color than the percentage. Don't print it
122 * twice for close colored ip with the same filename:line
125 if (!prev_line
|| strcmp(prev_line
, path
)
126 || color
!= prev_color
) {
127 color_fprintf(stdout
, color
, " %s", path
);
133 color_fprintf(stdout
, color
, " %7.2f", percent
);
135 color_fprintf(stdout
, PERF_COLOR_BLUE
, "%s\n", self
->line
);
140 printf(" : %s\n", self
->line
);
146 static struct rb_root root_sym_ext
;
148 static void insert_source_line(struct sym_ext
*sym_ext
)
150 struct sym_ext
*iter
;
151 struct rb_node
**p
= &root_sym_ext
.rb_node
;
152 struct rb_node
*parent
= NULL
;
156 iter
= rb_entry(parent
, struct sym_ext
, node
);
158 if (sym_ext
->percent
> iter
->percent
)
164 rb_link_node(&sym_ext
->node
, parent
, p
);
165 rb_insert_color(&sym_ext
->node
, &root_sym_ext
);
168 static void free_source_line(struct hist_entry
*he
, int len
)
170 struct sym_priv
*priv
= symbol__priv(he
->ms
.sym
);
171 struct sym_ext
*sym_ext
= priv
->ext
;
177 for (i
= 0; i
< len
; i
++)
178 free(sym_ext
[i
].path
);
182 root_sym_ext
= RB_ROOT
;
185 /* Get the filename:line for the colored entries */
187 get_source_line(struct hist_entry
*he
, int len
, const char *filename
)
189 struct symbol
*sym
= he
->ms
.sym
;
192 char cmd
[PATH_MAX
* 2];
193 struct sym_ext
*sym_ext
;
194 struct sym_priv
*priv
= symbol__priv(sym
);
195 struct sym_hist
*h
= priv
->hist
;
200 sym_ext
= priv
->ext
= calloc(len
, sizeof(struct sym_ext
));
204 start
= he
->ms
.map
->unmap_ip(he
->ms
.map
, sym
->start
);
206 for (i
= 0; i
< len
; i
++) {
212 sym_ext
[i
].percent
= 100.0 * h
->ip
[i
] / h
->sum
;
213 if (sym_ext
[i
].percent
<= 0.5)
217 sprintf(cmd
, "addr2line -e %s %016llx", filename
, offset
);
218 fp
= popen(cmd
, "r");
222 if (getline(&path
, &line_len
, fp
) < 0 || !line_len
)
225 sym_ext
[i
].path
= malloc(sizeof(char) * line_len
+ 1);
226 if (!sym_ext
[i
].path
)
229 strcpy(sym_ext
[i
].path
, path
);
230 insert_source_line(&sym_ext
[i
]);
237 static void print_summary(const char *filename
)
239 struct sym_ext
*sym_ext
;
240 struct rb_node
*node
;
242 printf("\nSorted summary for file %s\n", filename
);
243 printf("----------------------------------------------\n\n");
245 if (RB_EMPTY_ROOT(&root_sym_ext
)) {
246 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN
);
250 node
= rb_first(&root_sym_ext
);
256 sym_ext
= rb_entry(node
, struct sym_ext
, node
);
257 percent
= sym_ext
->percent
;
258 color
= get_percent_color(percent
);
259 path
= sym_ext
->path
;
261 color_fprintf(stdout
, color
, " %7.2f %s", percent
, path
);
262 node
= rb_next(node
);
266 static void hist_entry__print_hits(struct hist_entry
*self
)
268 struct symbol
*sym
= self
->ms
.sym
;
269 struct sym_priv
*priv
= symbol__priv(sym
);
270 struct sym_hist
*h
= priv
->hist
;
271 u64 len
= sym
->end
- sym
->start
, offset
;
273 for (offset
= 0; offset
< len
; ++offset
)
274 if (h
->ip
[offset
] != 0)
275 printf("%*Lx: %Lu\n", BITS_PER_LONG
/ 2,
276 sym
->start
+ offset
, h
->ip
[offset
]);
277 printf("%*s: %Lu\n", BITS_PER_LONG
/ 2, "h->sum", h
->sum
);
280 static int hist_entry__tty_annotate(struct hist_entry
*he
)
282 struct map
*map
= he
->ms
.map
;
283 struct dso
*dso
= map
->dso
;
284 struct symbol
*sym
= he
->ms
.sym
;
285 const char *filename
= dso
->long_name
, *d_filename
;
288 struct objdump_line
*pos
, *n
;
290 if (hist_entry__annotate(he
, &head
) < 0)
294 d_filename
= filename
;
296 d_filename
= basename(filename
);
298 len
= sym
->end
- sym
->start
;
301 get_source_line(he
, len
, filename
);
302 print_summary(filename
);
305 printf("\n\n------------------------------------------------\n");
306 printf(" Percent | Source code & Disassembly of %s\n", d_filename
);
307 printf("------------------------------------------------\n");
310 hist_entry__print_hits(he
);
312 list_for_each_entry_safe(pos
, n
, &head
, node
) {
313 objdump_line__print(pos
, &head
, he
, len
);
314 list_del(&pos
->node
);
315 objdump_line__free(pos
);
319 free_source_line(he
, len
);
324 static void hists__find_annotations(struct hists
*self
)
326 struct rb_node
*first
= rb_first(&self
->entries
), *nd
= first
;
330 struct hist_entry
*he
= rb_entry(nd
, struct hist_entry
, rb_node
);
331 struct sym_priv
*priv
;
333 if (he
->ms
.sym
== NULL
|| he
->ms
.map
->dso
->annotate_warned
)
336 priv
= symbol__priv(he
->ms
.sym
);
337 if (priv
->hist
== NULL
) {
346 if (use_browser
> 0) {
347 key
= hist_entry__tui_annotate(he
);
348 if (is_exit_key(key
))
363 hist_entry__tty_annotate(he
);
366 * Since we have a hist_entry per IP for the same
367 * symbol, free he->ms.sym->hist to signal we already
368 * processed this symbol.
376 static struct perf_event_ops event_ops
= {
377 .sample
= process_sample_event
,
378 .mmap
= event__process_mmap
,
379 .comm
= event__process_comm
,
380 .fork
= event__process_task
,
383 static int __cmd_annotate(void)
386 struct perf_session
*session
;
388 session
= perf_session__new(input_name
, O_RDONLY
, force
, false);
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_STRING('k', "vmlinux", &symbol_conf
.vmlinux_name
,
434 "file", "vmlinux pathname"),
435 OPT_BOOLEAN('m', "modules", &symbol_conf
.use_modules
,
436 "load module symbols - WARNING: use only with -k and LIVE kernel"),
437 OPT_BOOLEAN('l', "print-line", &print_line
,
438 "print matching source lines (may be slow)"),
439 OPT_BOOLEAN('P', "full-paths", &full_paths
,
440 "Don't shorten the displayed pathnames"),
444 int cmd_annotate(int argc
, const char **argv
, const char *prefix __used
)
446 argc
= parse_options(argc
, argv
, options
, annotate_usage
, 0);
450 symbol_conf
.priv_size
= sizeof(struct sym_priv
);
451 symbol_conf
.try_vmlinux_path
= true;
453 if (symbol__init() < 0)
456 setup_sorting(annotate_usage
, options
);
460 * Special case: if there's an argument left then assume tha
461 * it's a symbol filter:
464 usage_with_options(annotate_usage
, options
);
466 sym_hist_filter
= argv
[0];
469 if (field_sep
&& *field_sep
== '.') {
470 pr_err("'.' is the only non valid --field-separator argument\n");
474 return __cmd_annotate();