]>
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 | |
c0555642 | 31 | static bool force; |
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 | ||
b3165f41 | 61 | static int process_sample_event(event_t *event, struct perf_session *session) |
8035e428 | 62 | { |
1ed091c4 | 63 | struct addr_location al; |
6baa0a5a | 64 | |
0d755034 ACM |
65 | dump_printf("(IP, %d): %d: %#Lx\n", event->header.misc, |
66 | event->ip.pid, event->ip.ip); | |
8035e428 | 67 | |
628ada0c | 68 | if (event__preprocess_sample(event, session, &al, NULL) < 0) { |
29a9f66d ACM |
69 | pr_warning("problem processing %d event, skipping it.\n", |
70 | event->header.type); | |
8035e428 IM |
71 | return -1; |
72 | } | |
73 | ||
1c02c4d2 | 74 | if (!al.filtered && hists__add_entry(&session->hists, &al)) { |
29a9f66d ACM |
75 | pr_warning("problem incrementing symbol count, " |
76 | "skipping event\n"); | |
ec218fc4 | 77 | return -1; |
8035e428 | 78 | } |
8035e428 IM |
79 | |
80 | return 0; | |
81 | } | |
82 | ||
48fb4fdd ACM |
83 | static int objdump_line__print(struct objdump_line *self, |
84 | struct list_head *head, | |
85 | struct hist_entry *he, u64 len) | |
86 | { | |
59fd5306 | 87 | struct symbol *sym = he->ms.sym; |
48fb4fdd ACM |
88 | static const char *prev_line; |
89 | static const char *prev_color; | |
90 | ||
91 | if (self->offset != -1) { | |
301406b9 | 92 | const char *path = NULL; |
0b73da3f IM |
93 | unsigned int hits = 0; |
94 | double percent = 0.0; | |
83a0944f | 95 | const char *color; |
00a192b3 | 96 | struct sym_priv *priv = symbol__priv(sym); |
e4204992 ACM |
97 | struct sym_ext *sym_ext = priv->ext; |
98 | struct sym_hist *h = priv->hist; | |
48fb4fdd ACM |
99 | s64 offset = self->offset; |
100 | struct objdump_line *next = objdump__get_next_ip_line(head, self); | |
101 | ||
102 | while (offset < (s64)len && | |
103 | (next == NULL || offset < next->offset)) { | |
104 | if (sym_ext) { | |
105 | if (path == NULL) | |
106 | path = sym_ext[offset].path; | |
107 | percent += sym_ext[offset].percent; | |
108 | } else | |
109 | hits += h->ip[offset]; | |
110 | ||
111 | ++offset; | |
112 | } | |
0b73da3f | 113 | |
48fb4fdd | 114 | if (sym_ext == NULL && h->sum) |
e4204992 | 115 | percent = 100.0 * hits / h->sum; |
0b73da3f | 116 | |
1e11fd82 | 117 | color = get_percent_color(percent); |
0b73da3f | 118 | |
301406b9 FW |
119 | /* |
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 | |
123 | */ | |
124 | if (path) { | |
125 | if (!prev_line || strcmp(prev_line, path) | |
126 | || color != prev_color) { | |
127 | color_fprintf(stdout, color, " %s", path); | |
128 | prev_line = path; | |
129 | prev_color = color; | |
130 | } | |
131 | } | |
132 | ||
0b73da3f IM |
133 | color_fprintf(stdout, color, " %7.2f", percent); |
134 | printf(" : "); | |
48fb4fdd | 135 | color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", self->line); |
0b73da3f | 136 | } else { |
48fb4fdd | 137 | if (!*self->line) |
0b73da3f IM |
138 | printf(" :\n"); |
139 | else | |
48fb4fdd | 140 | printf(" : %s\n", self->line); |
0b73da3f IM |
141 | } |
142 | ||
143 | return 0; | |
144 | } | |
145 | ||
971738f3 FW |
146 | static struct rb_root root_sym_ext; |
147 | ||
148 | static void insert_source_line(struct sym_ext *sym_ext) | |
149 | { | |
150 | struct sym_ext *iter; | |
151 | struct rb_node **p = &root_sym_ext.rb_node; | |
152 | struct rb_node *parent = NULL; | |
153 | ||
154 | while (*p != NULL) { | |
155 | parent = *p; | |
156 | iter = rb_entry(parent, struct sym_ext, node); | |
157 | ||
158 | if (sym_ext->percent > iter->percent) | |
159 | p = &(*p)->rb_left; | |
160 | else | |
161 | p = &(*p)->rb_right; | |
162 | } | |
163 | ||
164 | rb_link_node(&sym_ext->node, parent, p); | |
165 | rb_insert_color(&sym_ext->node, &root_sym_ext); | |
166 | } | |
167 | ||
e4204992 | 168 | static void free_source_line(struct hist_entry *he, int len) |
301406b9 | 169 | { |
59fd5306 | 170 | struct sym_priv *priv = symbol__priv(he->ms.sym); |
e4204992 | 171 | struct sym_ext *sym_ext = priv->ext; |
301406b9 FW |
172 | int i; |
173 | ||
174 | if (!sym_ext) | |
175 | return; | |
176 | ||
177 | for (i = 0; i < len; i++) | |
178 | free(sym_ext[i].path); | |
179 | free(sym_ext); | |
180 | ||
e4204992 | 181 | priv->ext = NULL; |
971738f3 | 182 | root_sym_ext = RB_ROOT; |
301406b9 FW |
183 | } |
184 | ||
185 | /* Get the filename:line for the colored entries */ | |
c17c2db1 | 186 | static void |
ed52ce2e | 187 | get_source_line(struct hist_entry *he, int len, const char *filename) |
301406b9 | 188 | { |
59fd5306 | 189 | struct symbol *sym = he->ms.sym; |
ed52ce2e | 190 | u64 start; |
301406b9 FW |
191 | int i; |
192 | char cmd[PATH_MAX * 2]; | |
193 | struct sym_ext *sym_ext; | |
00a192b3 | 194 | struct sym_priv *priv = symbol__priv(sym); |
e4204992 | 195 | struct sym_hist *h = priv->hist; |
301406b9 | 196 | |
e4204992 | 197 | if (!h->sum) |
301406b9 FW |
198 | return; |
199 | ||
e4204992 ACM |
200 | sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext)); |
201 | if (!priv->ext) | |
301406b9 FW |
202 | return; |
203 | ||
59fd5306 | 204 | start = he->ms.map->unmap_ip(he->ms.map, sym->start); |
301406b9 FW |
205 | |
206 | for (i = 0; i < len; i++) { | |
207 | char *path = NULL; | |
208 | size_t line_len; | |
9cffa8d5 | 209 | u64 offset; |
301406b9 FW |
210 | FILE *fp; |
211 | ||
e4204992 | 212 | sym_ext[i].percent = 100.0 * h->ip[i] / h->sum; |
301406b9 FW |
213 | if (sym_ext[i].percent <= 0.5) |
214 | continue; | |
215 | ||
ed52ce2e | 216 | offset = start + i; |
c17c2db1 | 217 | sprintf(cmd, "addr2line -e %s %016llx", filename, offset); |
301406b9 FW |
218 | fp = popen(cmd, "r"); |
219 | if (!fp) | |
220 | continue; | |
221 | ||
222 | if (getline(&path, &line_len, fp) < 0 || !line_len) | |
223 | goto next; | |
224 | ||
c17c2db1 | 225 | sym_ext[i].path = malloc(sizeof(char) * line_len + 1); |
301406b9 FW |
226 | if (!sym_ext[i].path) |
227 | goto next; | |
228 | ||
229 | strcpy(sym_ext[i].path, path); | |
971738f3 | 230 | insert_source_line(&sym_ext[i]); |
301406b9 FW |
231 | |
232 | next: | |
233 | pclose(fp); | |
234 | } | |
235 | } | |
236 | ||
83a0944f | 237 | static void print_summary(const char *filename) |
971738f3 FW |
238 | { |
239 | struct sym_ext *sym_ext; | |
240 | struct rb_node *node; | |
241 | ||
242 | printf("\nSorted summary for file %s\n", filename); | |
243 | printf("----------------------------------------------\n\n"); | |
244 | ||
245 | if (RB_EMPTY_ROOT(&root_sym_ext)) { | |
246 | printf(" Nothing higher than %1.1f%%\n", MIN_GREEN); | |
247 | return; | |
248 | } | |
249 | ||
250 | node = rb_first(&root_sym_ext); | |
251 | while (node) { | |
252 | double percent; | |
83a0944f | 253 | const char *color; |
971738f3 FW |
254 | char *path; |
255 | ||
256 | sym_ext = rb_entry(node, struct sym_ext, node); | |
257 | percent = sym_ext->percent; | |
1e11fd82 | 258 | color = get_percent_color(percent); |
971738f3 FW |
259 | path = sym_ext->path; |
260 | ||
261 | color_fprintf(stdout, color, " %7.2f %s", percent, path); | |
262 | node = rb_next(node); | |
263 | } | |
264 | } | |
265 | ||
48fb4fdd ACM |
266 | static void hist_entry__print_hits(struct hist_entry *self) |
267 | { | |
59fd5306 | 268 | struct symbol *sym = self->ms.sym; |
48fb4fdd ACM |
269 | struct sym_priv *priv = symbol__priv(sym); |
270 | struct sym_hist *h = priv->hist; | |
271 | u64 len = sym->end - sym->start, offset; | |
272 | ||
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); | |
278 | } | |
279 | ||
46e3e055 | 280 | static int hist_entry__tty_annotate(struct hist_entry *he) |
0b73da3f | 281 | { |
59fd5306 | 282 | struct map *map = he->ms.map; |
ed52ce2e | 283 | struct dso *dso = map->dso; |
59fd5306 | 284 | struct symbol *sym = he->ms.sym; |
439d473b ACM |
285 | const char *filename = dso->long_name, *d_filename; |
286 | u64 len; | |
48fb4fdd ACM |
287 | LIST_HEAD(head); |
288 | struct objdump_line *pos, *n; | |
0b73da3f | 289 | |
ef7b93a1 | 290 | if (hist_entry__annotate(he, &head) < 0) |
46e3e055 | 291 | return -1; |
ed52ce2e | 292 | |
42976487 MG |
293 | if (full_paths) |
294 | d_filename = filename; | |
295 | else | |
296 | d_filename = basename(filename); | |
0b73da3f | 297 | |
0b73da3f IM |
298 | len = sym->end - sym->start; |
299 | ||
971738f3 | 300 | if (print_line) { |
ed52ce2e | 301 | get_source_line(he, len, filename); |
971738f3 FW |
302 | print_summary(filename); |
303 | } | |
304 | ||
305 | printf("\n\n------------------------------------------------\n"); | |
42976487 | 306 | printf(" Percent | Source code & Disassembly of %s\n", d_filename); |
971738f3 FW |
307 | printf("------------------------------------------------\n"); |
308 | ||
48fb4fdd ACM |
309 | if (verbose) |
310 | hist_entry__print_hits(he); | |
311 | ||
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); | |
316 | } | |
317 | ||
971738f3 | 318 | if (print_line) |
e4204992 | 319 | free_source_line(he, len); |
46e3e055 ACM |
320 | |
321 | return 0; | |
0b73da3f IM |
322 | } |
323 | ||
1c02c4d2 | 324 | static void hists__find_annotations(struct hists *self) |
0b73da3f | 325 | { |
46e3e055 ACM |
326 | struct rb_node *first = rb_first(&self->entries), *nd = first; |
327 | int key = KEY_RIGHT; | |
0b73da3f | 328 | |
46e3e055 | 329 | while (nd) { |
ed52ce2e | 330 | struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node); |
e4204992 | 331 | struct sym_priv *priv; |
0b73da3f | 332 | |
46e3e055 ACM |
333 | if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned) |
334 | goto find_next; | |
0b73da3f | 335 | |
59fd5306 | 336 | priv = symbol__priv(he->ms.sym); |
46e3e055 ACM |
337 | if (priv->hist == NULL) { |
338 | find_next: | |
339 | if (key == KEY_LEFT) | |
340 | nd = rb_prev(nd); | |
341 | else | |
342 | nd = rb_next(nd); | |
e4204992 | 343 | continue; |
46e3e055 | 344 | } |
e4204992 | 345 | |
62e3436b | 346 | if (use_browser > 0) { |
46e3e055 ACM |
347 | key = hist_entry__tui_annotate(he); |
348 | if (is_exit_key(key)) | |
349 | break; | |
350 | switch (key) { | |
351 | case KEY_RIGHT: | |
352 | case '\t': | |
353 | nd = rb_next(nd); | |
354 | break; | |
355 | case KEY_LEFT: | |
356 | if (nd == first) | |
357 | continue; | |
358 | nd = rb_prev(nd); | |
359 | default: | |
360 | break; | |
361 | } | |
362 | } else { | |
363 | hist_entry__tty_annotate(he); | |
364 | nd = rb_next(nd); | |
365 | /* | |
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. | |
369 | */ | |
370 | free(priv->hist); | |
371 | priv->hist = NULL; | |
372 | } | |
0b73da3f | 373 | } |
0b73da3f IM |
374 | } |
375 | ||
301a0b02 | 376 | static struct perf_event_ops event_ops = { |
55aa640f ACM |
377 | .sample = process_sample_event, |
378 | .mmap = event__process_mmap, | |
379 | .comm = event__process_comm, | |
380 | .fork = event__process_task, | |
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 | |
454c407e | 388 | session = perf_session__new(input_name, O_RDONLY, force, false); |
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"), | |
b32d133a ACM |
433 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, |
434 | "file", "vmlinux pathname"), | |
435 | OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, | |
42976487 | 436 | "load module symbols - WARNING: use only with -k and LIVE kernel"), |
301406b9 FW |
437 | OPT_BOOLEAN('l', "print-line", &print_line, |
438 | "print matching source lines (may be slow)"), | |
42976487 MG |
439 | OPT_BOOLEAN('P', "full-paths", &full_paths, |
440 | "Don't shorten the displayed pathnames"), | |
8035e428 IM |
441 | OPT_END() |
442 | }; | |
443 | ||
f37a291c | 444 | int cmd_annotate(int argc, const char **argv, const char *prefix __used) |
8035e428 | 445 | { |
655000e7 ACM |
446 | argc = parse_options(argc, argv, options, annotate_usage, 0); |
447 | ||
46e3e055 ACM |
448 | setup_browser(); |
449 | ||
75be6cf4 ACM |
450 | symbol_conf.priv_size = sizeof(struct sym_priv); |
451 | symbol_conf.try_vmlinux_path = true; | |
452 | ||
453 | if (symbol__init() < 0) | |
b32d133a | 454 | return -1; |
8035e428 | 455 | |
c8829c7a | 456 | setup_sorting(annotate_usage, options); |
8035e428 | 457 | |
0b73da3f IM |
458 | if (argc) { |
459 | /* | |
460 | * Special case: if there's an argument left then assume tha | |
461 | * it's a symbol filter: | |
462 | */ | |
463 | if (argc > 1) | |
464 | usage_with_options(annotate_usage, options); | |
465 | ||
466 | sym_hist_filter = argv[0]; | |
467 | } | |
468 | ||
dd68ada2 | 469 | if (field_sep && *field_sep == '.') { |
29a9f66d ACM |
470 | pr_err("'.' is the only non valid --field-separator argument\n"); |
471 | return -1; | |
dd68ada2 JK |
472 | } |
473 | ||
8035e428 IM |
474 | return __cmd_annotate(); |
475 | } |