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