]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - tools/perf/builtin-annotate.c
perf ui: Add ui_helpline methods
[mirror_ubuntu-eoan-kernel.git] / tools / perf / builtin-annotate.c
CommitLineData
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 29static char const *input_name = "perf.data";
8035e428 30
c0555642 31static bool force;
8035e428 32
c0555642 33static bool full_paths;
42976487 34
c0555642 35static bool print_line;
301406b9 36
e4204992
ACM
37struct sym_hist {
38 u64 sum;
39 u64 ip[0];
40};
41
301406b9 42struct sym_ext {
971738f3 43 struct rb_node node;
301406b9
FW
44 double percent;
45 char *path;
46};
47
e4204992
ACM
48struct sym_priv {
49 struct sym_hist *hist;
50 struct sym_ext *ext;
51};
52
53static const char *sym_hist_filter;
54
628ada0c 55static int sym__alloc_hist(struct symbol *self)
e4204992 56{
628ada0c
ACM
57 struct sym_priv *priv = symbol__priv(self);
58 const int size = (sizeof(*priv->hist) +
59 (self->end - self->start) * sizeof(u64));
60
61 priv->hist = zalloc(size);
62 return priv->hist == NULL ? -1 : 0;
e4204992 63}
8035e428 64
0b73da3f
IM
65/*
66 * collect histogram counts
67 */
628ada0c 68static int annotate__hist_hit(struct hist_entry *he, u64 ip)
8035e428 69{
0b73da3f 70 unsigned int sym_size, offset;
59fd5306 71 struct symbol *sym = he->ms.sym;
e4204992
ACM
72 struct sym_priv *priv;
73 struct sym_hist *h;
8035e428 74
59fd5306 75 if (!sym || !he->ms.map)
628ada0c 76 return 0;
e4204992 77
00a192b3 78 priv = symbol__priv(sym);
628ada0c
ACM
79 if (priv->hist == NULL && sym__alloc_hist(sym) < 0)
80 return -ENOMEM;
8035e428 81
0b73da3f
IM
82 sym_size = sym->end - sym->start;
83 offset = ip - sym->start;
8035e428 84
59fd5306 85 pr_debug3("%s: ip=%#Lx\n", __func__, he->ms.map->unmap_ip(he->ms.map, ip));
ed52ce2e 86
0b73da3f 87 if (offset >= sym_size)
628ada0c 88 return 0;
8035e428 89
e4204992
ACM
90 h = priv->hist;
91 h->sum++;
92 h->ip[offset]++;
8035e428 93
59fd5306
ACM
94 pr_debug3("%#Lx %s: count++ [ip: %#Lx, %#Lx] => %Ld\n", he->ms.sym->start,
95 he->ms.sym->name, ip, ip - he->ms.sym->start, h->ip[offset]);
628ada0c 96 return 0;
8035e428
IM
97}
98
1c02c4d2 99static int hists__add_entry(struct hists *self, struct addr_location *al)
8035e428 100{
628ada0c
ACM
101 struct hist_entry *he;
102
103 if (sym_hist_filter != NULL &&
104 (al->sym == NULL || strcmp(sym_hist_filter, al->sym->name) != 0)) {
105 /* We're only interested in a symbol named sym_hist_filter */
106 if (al->sym != NULL) {
107 rb_erase(&al->sym->rb_node,
108 &al->map->dso->symbols[al->map->type]);
109 symbol__delete(al->sym);
110 }
111 return 0;
112 }
113
1c02c4d2 114 he = __hists__add_entry(self, al, NULL, 1);
9735abf1 115 if (he == NULL)
8035e428 116 return -ENOMEM;
628ada0c
ACM
117
118 return annotate__hist_hit(he, al->addr);
8035e428
IM
119}
120
b3165f41 121static int process_sample_event(event_t *event, struct perf_session *session)
8035e428 122{
1ed091c4 123 struct addr_location al;
6baa0a5a 124
0d755034
ACM
125 dump_printf("(IP, %d): %d: %#Lx\n", event->header.misc,
126 event->ip.pid, event->ip.ip);
8035e428 127
628ada0c 128 if (event__preprocess_sample(event, session, &al, NULL) < 0) {
29a9f66d
ACM
129 pr_warning("problem processing %d event, skipping it.\n",
130 event->header.type);
8035e428
IM
131 return -1;
132 }
133
1c02c4d2 134 if (!al.filtered && hists__add_entry(&session->hists, &al)) {
29a9f66d
ACM
135 pr_warning("problem incrementing symbol count, "
136 "skipping event\n");
ec218fc4 137 return -1;
8035e428 138 }
8035e428
IM
139
140 return 0;
141}
142
48fb4fdd
ACM
143struct objdump_line {
144 struct list_head node;
145 s64 offset;
146 char *line;
147};
148
149static struct objdump_line *objdump_line__new(s64 offset, char *line)
150{
151 struct objdump_line *self = malloc(sizeof(*self));
152
153 if (self != NULL) {
154 self->offset = offset;
155 self->line = line;
156 }
157
158 return self;
159}
160
161static void objdump_line__free(struct objdump_line *self)
162{
163 free(self->line);
164 free(self);
165}
166
167static void objdump__add_line(struct list_head *head, struct objdump_line *line)
168{
169 list_add_tail(&line->node, head);
170}
171
172static struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
173 struct objdump_line *pos)
174{
175 list_for_each_entry_continue(pos, head, node)
176 if (pos->offset >= 0)
177 return pos;
178
179 return NULL;
180}
181
182static int parse_line(FILE *file, struct hist_entry *he,
183 struct list_head *head)
0b73da3f 184{
59fd5306 185 struct symbol *sym = he->ms.sym;
48fb4fdd 186 struct objdump_line *objdump_line;
0b73da3f 187 char *line = NULL, *tmp, *tmp2;
0b73da3f 188 size_t line_len;
48fb4fdd 189 s64 line_ip, offset = -1;
0b73da3f
IM
190 char *c;
191
192 if (getline(&line, &line_len, file) < 0)
193 return -1;
48fb4fdd 194
0b73da3f
IM
195 if (!line)
196 return -1;
197
198 c = strchr(line, '\n');
199 if (c)
200 *c = 0;
201
202 line_ip = -1;
0b73da3f
IM
203
204 /*
205 * Strip leading spaces:
206 */
207 tmp = line;
208 while (*tmp) {
209 if (*tmp != ' ')
210 break;
211 tmp++;
212 }
213
214 if (*tmp) {
215 /*
216 * Parse hexa addresses followed by ':'
217 */
218 line_ip = strtoull(tmp, &tmp2, 16);
219 if (*tmp2 != ':')
220 line_ip = -1;
221 }
222
223 if (line_ip != -1) {
59fd5306 224 u64 start = map__rip_2objdump(he->ms.map, sym->start);
48fb4fdd
ACM
225 offset = line_ip - start;
226 }
227
228 objdump_line = objdump_line__new(offset, line);
229 if (objdump_line == NULL) {
230 free(line);
231 return -1;
232 }
233 objdump__add_line(head, objdump_line);
234
235 return 0;
236}
237
238static int objdump_line__print(struct objdump_line *self,
239 struct list_head *head,
240 struct hist_entry *he, u64 len)
241{
59fd5306 242 struct symbol *sym = he->ms.sym;
48fb4fdd
ACM
243 static const char *prev_line;
244 static const char *prev_color;
245
246 if (self->offset != -1) {
301406b9 247 const char *path = NULL;
0b73da3f
IM
248 unsigned int hits = 0;
249 double percent = 0.0;
83a0944f 250 const char *color;
00a192b3 251 struct sym_priv *priv = symbol__priv(sym);
e4204992
ACM
252 struct sym_ext *sym_ext = priv->ext;
253 struct sym_hist *h = priv->hist;
48fb4fdd
ACM
254 s64 offset = self->offset;
255 struct objdump_line *next = objdump__get_next_ip_line(head, self);
256
257 while (offset < (s64)len &&
258 (next == NULL || offset < next->offset)) {
259 if (sym_ext) {
260 if (path == NULL)
261 path = sym_ext[offset].path;
262 percent += sym_ext[offset].percent;
263 } else
264 hits += h->ip[offset];
265
266 ++offset;
267 }
0b73da3f 268
48fb4fdd 269 if (sym_ext == NULL && h->sum)
e4204992 270 percent = 100.0 * hits / h->sum;
0b73da3f 271
1e11fd82 272 color = get_percent_color(percent);
0b73da3f 273
301406b9
FW
274 /*
275 * Also color the filename and line if needed, with
276 * the same color than the percentage. Don't print it
277 * twice for close colored ip with the same filename:line
278 */
279 if (path) {
280 if (!prev_line || strcmp(prev_line, path)
281 || color != prev_color) {
282 color_fprintf(stdout, color, " %s", path);
283 prev_line = path;
284 prev_color = color;
285 }
286 }
287
0b73da3f
IM
288 color_fprintf(stdout, color, " %7.2f", percent);
289 printf(" : ");
48fb4fdd 290 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", self->line);
0b73da3f 291 } else {
48fb4fdd 292 if (!*self->line)
0b73da3f
IM
293 printf(" :\n");
294 else
48fb4fdd 295 printf(" : %s\n", self->line);
0b73da3f
IM
296 }
297
298 return 0;
299}
300
971738f3
FW
301static struct rb_root root_sym_ext;
302
303static void insert_source_line(struct sym_ext *sym_ext)
304{
305 struct sym_ext *iter;
306 struct rb_node **p = &root_sym_ext.rb_node;
307 struct rb_node *parent = NULL;
308
309 while (*p != NULL) {
310 parent = *p;
311 iter = rb_entry(parent, struct sym_ext, node);
312
313 if (sym_ext->percent > iter->percent)
314 p = &(*p)->rb_left;
315 else
316 p = &(*p)->rb_right;
317 }
318
319 rb_link_node(&sym_ext->node, parent, p);
320 rb_insert_color(&sym_ext->node, &root_sym_ext);
321}
322
e4204992 323static void free_source_line(struct hist_entry *he, int len)
301406b9 324{
59fd5306 325 struct sym_priv *priv = symbol__priv(he->ms.sym);
e4204992 326 struct sym_ext *sym_ext = priv->ext;
301406b9
FW
327 int i;
328
329 if (!sym_ext)
330 return;
331
332 for (i = 0; i < len; i++)
333 free(sym_ext[i].path);
334 free(sym_ext);
335
e4204992 336 priv->ext = NULL;
971738f3 337 root_sym_ext = RB_ROOT;
301406b9
FW
338}
339
340/* Get the filename:line for the colored entries */
c17c2db1 341static void
ed52ce2e 342get_source_line(struct hist_entry *he, int len, const char *filename)
301406b9 343{
59fd5306 344 struct symbol *sym = he->ms.sym;
ed52ce2e 345 u64 start;
301406b9
FW
346 int i;
347 char cmd[PATH_MAX * 2];
348 struct sym_ext *sym_ext;
00a192b3 349 struct sym_priv *priv = symbol__priv(sym);
e4204992 350 struct sym_hist *h = priv->hist;
301406b9 351
e4204992 352 if (!h->sum)
301406b9
FW
353 return;
354
e4204992
ACM
355 sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
356 if (!priv->ext)
301406b9
FW
357 return;
358
59fd5306 359 start = he->ms.map->unmap_ip(he->ms.map, sym->start);
301406b9
FW
360
361 for (i = 0; i < len; i++) {
362 char *path = NULL;
363 size_t line_len;
9cffa8d5 364 u64 offset;
301406b9
FW
365 FILE *fp;
366
e4204992 367 sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
301406b9
FW
368 if (sym_ext[i].percent <= 0.5)
369 continue;
370
ed52ce2e 371 offset = start + i;
c17c2db1 372 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
301406b9
FW
373 fp = popen(cmd, "r");
374 if (!fp)
375 continue;
376
377 if (getline(&path, &line_len, fp) < 0 || !line_len)
378 goto next;
379
c17c2db1 380 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
301406b9
FW
381 if (!sym_ext[i].path)
382 goto next;
383
384 strcpy(sym_ext[i].path, path);
971738f3 385 insert_source_line(&sym_ext[i]);
301406b9
FW
386
387 next:
388 pclose(fp);
389 }
390}
391
83a0944f 392static void print_summary(const char *filename)
971738f3
FW
393{
394 struct sym_ext *sym_ext;
395 struct rb_node *node;
396
397 printf("\nSorted summary for file %s\n", filename);
398 printf("----------------------------------------------\n\n");
399
400 if (RB_EMPTY_ROOT(&root_sym_ext)) {
401 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
402 return;
403 }
404
405 node = rb_first(&root_sym_ext);
406 while (node) {
407 double percent;
83a0944f 408 const char *color;
971738f3
FW
409 char *path;
410
411 sym_ext = rb_entry(node, struct sym_ext, node);
412 percent = sym_ext->percent;
1e11fd82 413 color = get_percent_color(percent);
971738f3
FW
414 path = sym_ext->path;
415
416 color_fprintf(stdout, color, " %7.2f %s", percent, path);
417 node = rb_next(node);
418 }
419}
420
48fb4fdd
ACM
421static void hist_entry__print_hits(struct hist_entry *self)
422{
59fd5306 423 struct symbol *sym = self->ms.sym;
48fb4fdd
ACM
424 struct sym_priv *priv = symbol__priv(sym);
425 struct sym_hist *h = priv->hist;
426 u64 len = sym->end - sym->start, offset;
427
428 for (offset = 0; offset < len; ++offset)
429 if (h->ip[offset] != 0)
430 printf("%*Lx: %Lu\n", BITS_PER_LONG / 2,
431 sym->start + offset, h->ip[offset]);
432 printf("%*s: %Lu\n", BITS_PER_LONG / 2, "h->sum", h->sum);
433}
434
ed52ce2e 435static void annotate_sym(struct hist_entry *he)
0b73da3f 436{
59fd5306 437 struct map *map = he->ms.map;
ed52ce2e 438 struct dso *dso = map->dso;
59fd5306 439 struct symbol *sym = he->ms.sym;
439d473b
ACM
440 const char *filename = dso->long_name, *d_filename;
441 u64 len;
0b73da3f
IM
442 char command[PATH_MAX*2];
443 FILE *file;
48fb4fdd
ACM
444 LIST_HEAD(head);
445 struct objdump_line *pos, *n;
0b73da3f
IM
446
447 if (!filename)
448 return;
439d473b 449
d06d92b7
ACM
450 if (dso->origin == DSO__ORIG_KERNEL) {
451 if (dso->annotate_warned)
452 return;
453 dso->annotate_warned = 1;
454 pr_err("Can't annotate %s: No vmlinux file was found in the "
455 "path:\n", sym->name);
456 vmlinux_path__fprintf(stderr);
457 return;
458 }
459
29a9f66d
ACM
460 pr_debug("%s: filename=%s, sym=%s, start=%#Lx, end=%#Lx\n", __func__,
461 filename, sym->name, map->unmap_ip(map, sym->start),
462 map->unmap_ip(map, sym->end));
ed52ce2e 463
42976487
MG
464 if (full_paths)
465 d_filename = filename;
466 else
467 d_filename = basename(filename);
0b73da3f 468
0b73da3f
IM
469 len = sym->end - sym->start;
470
971738f3 471 if (print_line) {
ed52ce2e 472 get_source_line(he, len, filename);
971738f3
FW
473 print_summary(filename);
474 }
475
476 printf("\n\n------------------------------------------------\n");
42976487 477 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
971738f3
FW
478 printf("------------------------------------------------\n");
479
480 if (verbose >= 2)
439d473b
ACM
481 printf("annotating [%p] %30s : [%p] %30s\n",
482 dso, dso->long_name, sym, sym->name);
301406b9 483
42976487 484 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
7a2b6209
KS
485 map__rip_2objdump(map, sym->start),
486 map__rip_2objdump(map, sym->end),
ed52ce2e 487 filename, filename);
0b73da3f
IM
488
489 if (verbose >= 3)
490 printf("doing: %s\n", command);
491
492 file = popen(command, "r");
493 if (!file)
494 return;
495
496 while (!feof(file)) {
48fb4fdd 497 if (parse_line(file, he, &head) < 0)
0b73da3f
IM
498 break;
499 }
500
501 pclose(file);
48fb4fdd
ACM
502
503 if (verbose)
504 hist_entry__print_hits(he);
505
506 list_for_each_entry_safe(pos, n, &head, node) {
507 objdump_line__print(pos, &head, he, len);
508 list_del(&pos->node);
509 objdump_line__free(pos);
510 }
511
971738f3 512 if (print_line)
e4204992 513 free_source_line(he, len);
0b73da3f
IM
514}
515
1c02c4d2 516static void hists__find_annotations(struct hists *self)
0b73da3f
IM
517{
518 struct rb_node *nd;
0b73da3f 519
1c02c4d2 520 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
ed52ce2e 521 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
e4204992 522 struct sym_priv *priv;
0b73da3f 523
59fd5306 524 if (he->ms.sym == NULL)
e4204992 525 continue;
0b73da3f 526
59fd5306 527 priv = symbol__priv(he->ms.sym);
e4204992
ACM
528 if (priv->hist == NULL)
529 continue;
530
531 annotate_sym(he);
e4204992
ACM
532 /*
533 * Since we have a hist_entry per IP for the same symbol, free
59fd5306 534 * he->ms.sym->hist to signal we already processed this symbol.
e4204992
ACM
535 */
536 free(priv->hist);
537 priv->hist = NULL;
0b73da3f 538 }
0b73da3f
IM
539}
540
301a0b02 541static struct perf_event_ops event_ops = {
55aa640f
ACM
542 .sample = process_sample_event,
543 .mmap = event__process_mmap,
544 .comm = event__process_comm,
545 .fork = event__process_task,
bab81b62
LZ
546};
547
8035e428
IM
548static int __cmd_annotate(void)
549{
bab81b62 550 int ret;
75be6cf4 551 struct perf_session *session;
8035e428 552
454c407e 553 session = perf_session__new(input_name, O_RDONLY, force, false);
94c744b6
ACM
554 if (session == NULL)
555 return -ENOMEM;
556
ec913369 557 ret = perf_session__process_events(session, &event_ops);
bab81b62 558 if (ret)
94c744b6 559 goto out_delete;
8035e428 560
62daacb5
ACM
561 if (dump_trace) {
562 event__print_totals();
94c744b6 563 goto out_delete;
62daacb5 564 }
8035e428 565
da21d1b5 566 if (verbose > 3)
b3165f41 567 perf_session__fprintf(session, stdout);
8035e428 568
da21d1b5 569 if (verbose > 2)
cbf69680 570 perf_session__fprintf_dsos(session, stdout);
8035e428 571
1c02c4d2
ACM
572 hists__collapse_resort(&session->hists);
573 hists__output_resort(&session->hists);
574 hists__find_annotations(&session->hists);
94c744b6
ACM
575out_delete:
576 perf_session__delete(session);
8035e428 577
bab81b62 578 return ret;
8035e428
IM
579}
580
581static const char * const annotate_usage[] = {
582 "perf annotate [<options>] <command>",
583 NULL
584};
585
586static const struct option options[] = {
587 OPT_STRING('i', "input", &input_name, "file",
588 "input file name"),
ac73c5a9
ACM
589 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
590 "only consider symbols in these dsos"),
23b87116 591 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
0b73da3f 592 "symbol to annotate"),
fa6963b2 593 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
c0555642 594 OPT_INCR('v', "verbose", &verbose,
8035e428
IM
595 "be more verbose (show symbol address, etc)"),
596 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
597 "dump raw trace in ASCII"),
b32d133a
ACM
598 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
599 "file", "vmlinux pathname"),
600 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
42976487 601 "load module symbols - WARNING: use only with -k and LIVE kernel"),
301406b9
FW
602 OPT_BOOLEAN('l', "print-line", &print_line,
603 "print matching source lines (may be slow)"),
42976487
MG
604 OPT_BOOLEAN('P', "full-paths", &full_paths,
605 "Don't shorten the displayed pathnames"),
8035e428
IM
606 OPT_END()
607};
608
f37a291c 609int cmd_annotate(int argc, const char **argv, const char *prefix __used)
8035e428 610{
655000e7
ACM
611 argc = parse_options(argc, argv, options, annotate_usage, 0);
612
75be6cf4
ACM
613 symbol_conf.priv_size = sizeof(struct sym_priv);
614 symbol_conf.try_vmlinux_path = true;
615
616 if (symbol__init() < 0)
b32d133a 617 return -1;
8035e428 618
c8829c7a 619 setup_sorting(annotate_usage, options);
8035e428 620
0b73da3f
IM
621 if (argc) {
622 /*
623 * Special case: if there's an argument left then assume tha
624 * it's a symbol filter:
625 */
626 if (argc > 1)
627 usage_with_options(annotate_usage, options);
628
629 sym_hist_filter = argv[0];
630 }
631
8035e428
IM
632 setup_pager();
633
dd68ada2 634 if (field_sep && *field_sep == '.') {
29a9f66d
ACM
635 pr_err("'.' is the only non valid --field-separator argument\n");
636 return -1;
dd68ada2
JK
637 }
638
8035e428
IM
639 return __cmd_annotate();
640}