Commit | Line | Data |
---|---|---|
8035e428 IM |
1 | /* |
2 | * builtin-annotate.c | |
3 | * | |
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. | |
7 | */ | |
8 | #include "builtin.h" | |
9 | ||
10 | #include "util/util.h" | |
11 | ||
12 | #include "util/color.h" | |
5da50258 | 13 | #include <linux/list.h> |
8035e428 | 14 | #include "util/cache.h" |
43cbcd8a | 15 | #include <linux/rbtree.h> |
8035e428 | 16 | #include "util/symbol.h" |
8035e428 IM |
17 | |
18 | #include "perf.h" | |
8f28827a | 19 | #include "util/debug.h" |
8035e428 | 20 | |
62daacb5 | 21 | #include "util/event.h" |
8035e428 IM |
22 | #include "util/parse-options.h" |
23 | #include "util/parse-events.h" | |
6baa0a5a | 24 | #include "util/thread.h" |
dd68ada2 | 25 | #include "util/sort.h" |
3d1d07ec | 26 | #include "util/hist.h" |
94c744b6 | 27 | #include "util/session.h" |
8035e428 | 28 | |
8035e428 | 29 | static char const *input_name = "perf.data"; |
8035e428 | 30 | |
8b9e74eb | 31 | static bool force, use_tui, use_stdio; |
8035e428 | 32 | |
c0555642 | 33 | static bool full_paths; |
42976487 | 34 | |
c0555642 | 35 | static bool print_line; |
301406b9 | 36 | |
e4204992 ACM |
37 | static const char *sym_hist_filter; |
38 | ||
1c02c4d2 | 39 | static int hists__add_entry(struct hists *self, struct addr_location *al) |
8035e428 | 40 | { |
628ada0c ACM |
41 | struct hist_entry *he; |
42 | ||
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); | |
50 | } | |
51 | return 0; | |
52 | } | |
53 | ||
1c02c4d2 | 54 | he = __hists__add_entry(self, al, NULL, 1); |
9735abf1 | 55 | if (he == NULL) |
8035e428 | 56 | return -ENOMEM; |
628ada0c | 57 | |
ef7b93a1 | 58 | return hist_entry__inc_addr_samples(he, al->addr); |
8035e428 IM |
59 | } |
60 | ||
8115d60c ACM |
61 | static int process_sample_event(union perf_event *event, |
62 | struct perf_sample *sample, | |
640c03ce | 63 | struct perf_session *session) |
8035e428 | 64 | { |
1ed091c4 | 65 | struct addr_location al; |
6baa0a5a | 66 | |
8115d60c | 67 | if (perf_event__preprocess_sample(event, session, &al, sample, NULL) < 0) { |
29a9f66d ACM |
68 | pr_warning("problem processing %d event, skipping it.\n", |
69 | event->header.type); | |
8035e428 IM |
70 | return -1; |
71 | } | |
72 | ||
1c02c4d2 | 73 | if (!al.filtered && hists__add_entry(&session->hists, &al)) { |
29a9f66d ACM |
74 | pr_warning("problem incrementing symbol count, " |
75 | "skipping event\n"); | |
ec218fc4 | 76 | return -1; |
8035e428 | 77 | } |
8035e428 IM |
78 | |
79 | return 0; | |
80 | } | |
81 | ||
48fb4fdd ACM |
82 | static int objdump_line__print(struct objdump_line *self, |
83 | struct list_head *head, | |
84 | struct hist_entry *he, u64 len) | |
85 | { | |
59fd5306 | 86 | struct symbol *sym = he->ms.sym; |
48fb4fdd ACM |
87 | static const char *prev_line; |
88 | static const char *prev_color; | |
89 | ||
90 | if (self->offset != -1) { | |
301406b9 | 91 | const char *path = NULL; |
0b73da3f IM |
92 | unsigned int hits = 0; |
93 | double percent = 0.0; | |
83a0944f | 94 | const char *color; |
00a192b3 | 95 | struct sym_priv *priv = symbol__priv(sym); |
e4204992 ACM |
96 | struct sym_ext *sym_ext = priv->ext; |
97 | struct sym_hist *h = priv->hist; | |
48fb4fdd ACM |
98 | s64 offset = self->offset; |
99 | struct objdump_line *next = objdump__get_next_ip_line(head, self); | |
100 | ||
101 | while (offset < (s64)len && | |
102 | (next == NULL || offset < next->offset)) { | |
103 | if (sym_ext) { | |
104 | if (path == NULL) | |
105 | path = sym_ext[offset].path; | |
106 | percent += sym_ext[offset].percent; | |
107 | } else | |
108 | hits += h->ip[offset]; | |
109 | ||
110 | ++offset; | |
111 | } | |
0b73da3f | 112 | |
48fb4fdd | 113 | if (sym_ext == NULL && h->sum) |
e4204992 | 114 | percent = 100.0 * hits / h->sum; |
0b73da3f | 115 | |
1e11fd82 | 116 | color = get_percent_color(percent); |
0b73da3f | 117 | |
301406b9 FW |
118 | /* |
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 | |
122 | */ | |
123 | if (path) { | |
124 | if (!prev_line || strcmp(prev_line, path) | |
125 | || color != prev_color) { | |
126 | color_fprintf(stdout, color, " %s", path); | |
127 | prev_line = path; | |
128 | prev_color = color; | |
129 | } | |
130 | } | |
131 | ||
0b73da3f IM |
132 | color_fprintf(stdout, color, " %7.2f", percent); |
133 | printf(" : "); | |
48fb4fdd | 134 | color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", self->line); |
0b73da3f | 135 | } else { |
48fb4fdd | 136 | if (!*self->line) |
0b73da3f IM |
137 | printf(" :\n"); |
138 | else | |
48fb4fdd | 139 | printf(" : %s\n", self->line); |
0b73da3f IM |
140 | } |
141 | ||
142 | return 0; | |
143 | } | |
144 | ||
971738f3 FW |
145 | static struct rb_root root_sym_ext; |
146 | ||
147 | static void insert_source_line(struct sym_ext *sym_ext) | |
148 | { | |
149 | struct sym_ext *iter; | |
150 | struct rb_node **p = &root_sym_ext.rb_node; | |
151 | struct rb_node *parent = NULL; | |
152 | ||
153 | while (*p != NULL) { | |
154 | parent = *p; | |
155 | iter = rb_entry(parent, struct sym_ext, node); | |
156 | ||
157 | if (sym_ext->percent > iter->percent) | |
158 | p = &(*p)->rb_left; | |
159 | else | |
160 | p = &(*p)->rb_right; | |
161 | } | |
162 | ||
163 | rb_link_node(&sym_ext->node, parent, p); | |
164 | rb_insert_color(&sym_ext->node, &root_sym_ext); | |
165 | } | |
166 | ||
e4204992 | 167 | static void free_source_line(struct hist_entry *he, int len) |
301406b9 | 168 | { |
59fd5306 | 169 | struct sym_priv *priv = symbol__priv(he->ms.sym); |
e4204992 | 170 | struct sym_ext *sym_ext = priv->ext; |
301406b9 FW |
171 | int i; |
172 | ||
173 | if (!sym_ext) | |
174 | return; | |
175 | ||
176 | for (i = 0; i < len; i++) | |
177 | free(sym_ext[i].path); | |
178 | free(sym_ext); | |
179 | ||
e4204992 | 180 | priv->ext = NULL; |
971738f3 | 181 | root_sym_ext = RB_ROOT; |
301406b9 FW |
182 | } |
183 | ||
184 | /* Get the filename:line for the colored entries */ | |
c17c2db1 | 185 | static void |
ed52ce2e | 186 | get_source_line(struct hist_entry *he, int len, const char *filename) |
301406b9 | 187 | { |
59fd5306 | 188 | struct symbol *sym = he->ms.sym; |
ed52ce2e | 189 | u64 start; |
301406b9 FW |
190 | int i; |
191 | char cmd[PATH_MAX * 2]; | |
192 | struct sym_ext *sym_ext; | |
00a192b3 | 193 | struct sym_priv *priv = symbol__priv(sym); |
e4204992 | 194 | struct sym_hist *h = priv->hist; |
301406b9 | 195 | |
e4204992 | 196 | if (!h->sum) |
301406b9 FW |
197 | return; |
198 | ||
e4204992 ACM |
199 | sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext)); |
200 | if (!priv->ext) | |
301406b9 FW |
201 | return; |
202 | ||
59fd5306 | 203 | start = he->ms.map->unmap_ip(he->ms.map, sym->start); |
301406b9 FW |
204 | |
205 | for (i = 0; i < len; i++) { | |
206 | char *path = NULL; | |
207 | size_t line_len; | |
9cffa8d5 | 208 | u64 offset; |
301406b9 FW |
209 | FILE *fp; |
210 | ||
e4204992 | 211 | sym_ext[i].percent = 100.0 * h->ip[i] / h->sum; |
301406b9 FW |
212 | if (sym_ext[i].percent <= 0.5) |
213 | continue; | |
214 | ||
ed52ce2e | 215 | offset = start + i; |
9486aa38 | 216 | sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset); |
301406b9 FW |
217 | fp = popen(cmd, "r"); |
218 | if (!fp) | |
219 | continue; | |
220 | ||
221 | if (getline(&path, &line_len, fp) < 0 || !line_len) | |
222 | goto next; | |
223 | ||
c17c2db1 | 224 | sym_ext[i].path = malloc(sizeof(char) * line_len + 1); |
301406b9 FW |
225 | if (!sym_ext[i].path) |
226 | goto next; | |
227 | ||
228 | strcpy(sym_ext[i].path, path); | |
971738f3 | 229 | insert_source_line(&sym_ext[i]); |
301406b9 FW |
230 | |
231 | next: | |
232 | pclose(fp); | |
233 | } | |
234 | } | |
235 | ||
83a0944f | 236 | static void print_summary(const char *filename) |
971738f3 FW |
237 | { |
238 | struct sym_ext *sym_ext; | |
239 | struct rb_node *node; | |
240 | ||
241 | printf("\nSorted summary for file %s\n", filename); | |
242 | printf("----------------------------------------------\n\n"); | |
243 | ||
244 | if (RB_EMPTY_ROOT(&root_sym_ext)) { | |
245 | printf(" Nothing higher than %1.1f%%\n", MIN_GREEN); | |
246 | return; | |
247 | } | |
248 | ||
249 | node = rb_first(&root_sym_ext); | |
250 | while (node) { | |
251 | double percent; | |
83a0944f | 252 | const char *color; |
971738f3 FW |
253 | char *path; |
254 | ||
255 | sym_ext = rb_entry(node, struct sym_ext, node); | |
256 | percent = sym_ext->percent; | |
1e11fd82 | 257 | color = get_percent_color(percent); |
971738f3 FW |
258 | path = sym_ext->path; |
259 | ||
260 | color_fprintf(stdout, color, " %7.2f %s", percent, path); | |
261 | node = rb_next(node); | |
262 | } | |
263 | } | |
264 | ||
48fb4fdd ACM |
265 | static void hist_entry__print_hits(struct hist_entry *self) |
266 | { | |
59fd5306 | 267 | struct symbol *sym = self->ms.sym; |
48fb4fdd ACM |
268 | struct sym_priv *priv = symbol__priv(sym); |
269 | struct sym_hist *h = priv->hist; | |
270 | u64 len = sym->end - sym->start, offset; | |
271 | ||
272 | for (offset = 0; offset < len; ++offset) | |
273 | if (h->ip[offset] != 0) | |
9486aa38 | 274 | printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2, |
48fb4fdd | 275 | sym->start + offset, h->ip[offset]); |
9486aa38 | 276 | printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum); |
48fb4fdd ACM |
277 | } |
278 | ||
46e3e055 | 279 | static int hist_entry__tty_annotate(struct hist_entry *he) |
0b73da3f | 280 | { |
59fd5306 | 281 | struct map *map = he->ms.map; |
ed52ce2e | 282 | struct dso *dso = map->dso; |
59fd5306 | 283 | struct symbol *sym = he->ms.sym; |
439d473b ACM |
284 | const char *filename = dso->long_name, *d_filename; |
285 | u64 len; | |
48fb4fdd ACM |
286 | LIST_HEAD(head); |
287 | struct objdump_line *pos, *n; | |
0b73da3f | 288 | |
92221162 | 289 | if (hist_entry__annotate(he, &head, 0) < 0) |
46e3e055 | 290 | return -1; |
ed52ce2e | 291 | |
42976487 MG |
292 | if (full_paths) |
293 | d_filename = filename; | |
294 | else | |
295 | d_filename = basename(filename); | |
0b73da3f | 296 | |
0b73da3f IM |
297 | len = sym->end - sym->start; |
298 | ||
971738f3 | 299 | if (print_line) { |
ed52ce2e | 300 | get_source_line(he, len, filename); |
971738f3 FW |
301 | print_summary(filename); |
302 | } | |
303 | ||
304 | printf("\n\n------------------------------------------------\n"); | |
42976487 | 305 | printf(" Percent | Source code & Disassembly of %s\n", d_filename); |
971738f3 FW |
306 | printf("------------------------------------------------\n"); |
307 | ||
48fb4fdd ACM |
308 | if (verbose) |
309 | hist_entry__print_hits(he); | |
310 | ||
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); | |
315 | } | |
316 | ||
971738f3 | 317 | if (print_line) |
e4204992 | 318 | free_source_line(he, len); |
46e3e055 ACM |
319 | |
320 | return 0; | |
0b73da3f IM |
321 | } |
322 | ||
1c02c4d2 | 323 | static void hists__find_annotations(struct hists *self) |
0b73da3f | 324 | { |
b50e003d | 325 | struct rb_node *nd = rb_first(&self->entries), *next; |
46e3e055 | 326 | int key = KEY_RIGHT; |
0b73da3f | 327 | |
46e3e055 | 328 | while (nd) { |
ed52ce2e | 329 | struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node); |
e4204992 | 330 | struct sym_priv *priv; |
0b73da3f | 331 | |
46e3e055 ACM |
332 | if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned) |
333 | goto find_next; | |
0b73da3f | 334 | |
59fd5306 | 335 | priv = symbol__priv(he->ms.sym); |
46e3e055 ACM |
336 | if (priv->hist == NULL) { |
337 | find_next: | |
338 | if (key == KEY_LEFT) | |
339 | nd = rb_prev(nd); | |
340 | else | |
341 | nd = rb_next(nd); | |
e4204992 | 342 | continue; |
46e3e055 | 343 | } |
e4204992 | 344 | |
62e3436b | 345 | if (use_browser > 0) { |
46e3e055 | 346 | key = hist_entry__tui_annotate(he); |
46e3e055 ACM |
347 | switch (key) { |
348 | case KEY_RIGHT: | |
b50e003d | 349 | next = rb_next(nd); |
46e3e055 ACM |
350 | break; |
351 | case KEY_LEFT: | |
b50e003d | 352 | next = rb_prev(nd); |
46e3e055 | 353 | break; |
b50e003d ACM |
354 | default: |
355 | return; | |
46e3e055 | 356 | } |
b50e003d ACM |
357 | |
358 | if (next != NULL) | |
359 | nd = next; | |
46e3e055 ACM |
360 | } else { |
361 | hist_entry__tty_annotate(he); | |
362 | nd = rb_next(nd); | |
363 | /* | |
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. | |
367 | */ | |
368 | free(priv->hist); | |
369 | priv->hist = NULL; | |
370 | } | |
0b73da3f | 371 | } |
0b73da3f IM |
372 | } |
373 | ||
301a0b02 | 374 | static struct perf_event_ops event_ops = { |
55aa640f | 375 | .sample = process_sample_event, |
8115d60c ACM |
376 | .mmap = perf_event__process_mmap, |
377 | .comm = perf_event__process_comm, | |
378 | .fork = perf_event__process_task, | |
eac23d1c IM |
379 | .ordered_samples = true, |
380 | .ordering_requires_timestamps = true, | |
bab81b62 LZ |
381 | }; |
382 | ||
8035e428 IM |
383 | static int __cmd_annotate(void) |
384 | { | |
bab81b62 | 385 | int ret; |
75be6cf4 | 386 | struct perf_session *session; |
8035e428 | 387 | |
21ef97f0 | 388 | session = perf_session__new(input_name, O_RDONLY, force, false, &event_ops); |
94c744b6 ACM |
389 | if (session == NULL) |
390 | return -ENOMEM; | |
391 | ||
ec913369 | 392 | ret = perf_session__process_events(session, &event_ops); |
bab81b62 | 393 | if (ret) |
94c744b6 | 394 | goto out_delete; |
8035e428 | 395 | |
62daacb5 | 396 | if (dump_trace) { |
c8446b9b | 397 | perf_session__fprintf_nr_events(session, stdout); |
94c744b6 | 398 | goto out_delete; |
62daacb5 | 399 | } |
8035e428 | 400 | |
da21d1b5 | 401 | if (verbose > 3) |
b3165f41 | 402 | perf_session__fprintf(session, stdout); |
8035e428 | 403 | |
da21d1b5 | 404 | if (verbose > 2) |
cbf69680 | 405 | perf_session__fprintf_dsos(session, stdout); |
8035e428 | 406 | |
1c02c4d2 ACM |
407 | hists__collapse_resort(&session->hists); |
408 | hists__output_resort(&session->hists); | |
409 | hists__find_annotations(&session->hists); | |
94c744b6 ACM |
410 | out_delete: |
411 | perf_session__delete(session); | |
8035e428 | 412 | |
bab81b62 | 413 | return ret; |
8035e428 IM |
414 | } |
415 | ||
416 | static const char * const annotate_usage[] = { | |
417 | "perf annotate [<options>] <command>", | |
418 | NULL | |
419 | }; | |
420 | ||
421 | static const struct option options[] = { | |
422 | OPT_STRING('i', "input", &input_name, "file", | |
423 | "input file name"), | |
ac73c5a9 ACM |
424 | OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]", |
425 | "only consider symbols in these dsos"), | |
23b87116 | 426 | OPT_STRING('s', "symbol", &sym_hist_filter, "symbol", |
0b73da3f | 427 | "symbol to annotate"), |
fa6963b2 | 428 | OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), |
c0555642 | 429 | OPT_INCR('v', "verbose", &verbose, |
8035e428 IM |
430 | "be more verbose (show symbol address, etc)"), |
431 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, | |
432 | "dump raw trace in ASCII"), | |
8b9e74eb ACM |
433 | OPT_BOOLEAN(0, "tui", &use_tui, "Use the TUI interface"), |
434 | OPT_BOOLEAN(0, "stdio", &use_stdio, "Use the stdio interface"), | |
b32d133a ACM |
435 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, |
436 | "file", "vmlinux pathname"), | |
437 | OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, | |
42976487 | 438 | "load module symbols - WARNING: use only with -k and LIVE kernel"), |
301406b9 FW |
439 | OPT_BOOLEAN('l', "print-line", &print_line, |
440 | "print matching source lines (may be slow)"), | |
42976487 MG |
441 | OPT_BOOLEAN('P', "full-paths", &full_paths, |
442 | "Don't shorten the displayed pathnames"), | |
8035e428 IM |
443 | OPT_END() |
444 | }; | |
445 | ||
f37a291c | 446 | int cmd_annotate(int argc, const char **argv, const char *prefix __used) |
8035e428 | 447 | { |
655000e7 ACM |
448 | argc = parse_options(argc, argv, options, annotate_usage, 0); |
449 | ||
8b9e74eb ACM |
450 | if (use_stdio) |
451 | use_browser = 0; | |
452 | else if (use_tui) | |
453 | use_browser = 1; | |
454 | ||
229ade9b | 455 | setup_browser(true); |
46e3e055 | 456 | |
75be6cf4 ACM |
457 | symbol_conf.priv_size = sizeof(struct sym_priv); |
458 | symbol_conf.try_vmlinux_path = true; | |
459 | ||
460 | if (symbol__init() < 0) | |
b32d133a | 461 | return -1; |
8035e428 | 462 | |
c8829c7a | 463 | setup_sorting(annotate_usage, options); |
8035e428 | 464 | |
0b73da3f IM |
465 | if (argc) { |
466 | /* | |
467 | * Special case: if there's an argument left then assume tha | |
468 | * it's a symbol filter: | |
469 | */ | |
470 | if (argc > 1) | |
471 | usage_with_options(annotate_usage, options); | |
472 | ||
473 | sym_hist_filter = argv[0]; | |
474 | } | |
475 | ||
dd68ada2 | 476 | if (field_sep && *field_sep == '.') { |
29a9f66d ACM |
477 | pr_err("'.' is the only non valid --field-separator argument\n"); |
478 | return -1; | |
dd68ada2 JK |
479 | } |
480 | ||
8035e428 IM |
481 | return __cmd_annotate(); |
482 | } |