aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/builtin-annotate.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/perf/builtin-annotate.c')
-rw-r--r--tools/perf/builtin-annotate.c306
1 files changed, 73 insertions, 233 deletions
diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c
index 6ad7148451c5..fd20670ce986 100644
--- a/tools/perf/builtin-annotate.c
+++ b/tools/perf/builtin-annotate.c
@@ -14,7 +14,6 @@
14#include "util/cache.h" 14#include "util/cache.h"
15#include <linux/rbtree.h> 15#include <linux/rbtree.h>
16#include "util/symbol.h" 16#include "util/symbol.h"
17#include "util/string.h"
18 17
19#include "perf.h" 18#include "perf.h"
20#include "util/debug.h" 19#include "util/debug.h"
@@ -29,80 +28,16 @@
29 28
30static char const *input_name = "perf.data"; 29static char const *input_name = "perf.data";
31 30
32static int force; 31static bool force;
33 32
34static int full_paths; 33static bool full_paths;
35 34
36static int print_line; 35static bool print_line;
37
38struct sym_hist {
39 u64 sum;
40 u64 ip[0];
41};
42
43struct sym_ext {
44 struct rb_node node;
45 double percent;
46 char *path;
47};
48
49struct sym_priv {
50 struct sym_hist *hist;
51 struct sym_ext *ext;
52};
53 36
54static const char *sym_hist_filter; 37static const char *sym_hist_filter;
55 38
56static int sym__alloc_hist(struct symbol *self) 39static int hists__add_entry(struct hists *self, struct addr_location *al)
57{
58 struct sym_priv *priv = symbol__priv(self);
59 const int size = (sizeof(*priv->hist) +
60 (self->end - self->start) * sizeof(u64));
61
62 priv->hist = zalloc(size);
63 return priv->hist == NULL ? -1 : 0;
64}
65
66/*
67 * collect histogram counts
68 */
69static int annotate__hist_hit(struct hist_entry *he, u64 ip)
70{
71 unsigned int sym_size, offset;
72 struct symbol *sym = he->sym;
73 struct sym_priv *priv;
74 struct sym_hist *h;
75
76 he->count++;
77
78 if (!sym || !he->map)
79 return 0;
80
81 priv = symbol__priv(sym);
82 if (priv->hist == NULL && sym__alloc_hist(sym) < 0)
83 return -ENOMEM;
84
85 sym_size = sym->end - sym->start;
86 offset = ip - sym->start;
87
88 pr_debug3("%s: ip=%#Lx\n", __func__, he->map->unmap_ip(he->map, ip));
89
90 if (offset >= sym_size)
91 return 0;
92
93 h = priv->hist;
94 h->sum++;
95 h->ip[offset]++;
96
97 pr_debug3("%#Lx %s: count++ [ip: %#Lx, %#Lx] => %Ld\n", he->sym->start,
98 he->sym->name, ip, ip - he->sym->start, h->ip[offset]);
99 return 0;
100}
101
102static int perf_session__add_hist_entry(struct perf_session *self,
103 struct addr_location *al, u64 count)
104{ 40{
105 bool hit;
106 struct hist_entry *he; 41 struct hist_entry *he;
107 42
108 if (sym_hist_filter != NULL && 43 if (sym_hist_filter != NULL &&
@@ -116,27 +51,25 @@ static int perf_session__add_hist_entry(struct perf_session *self,
116 return 0; 51 return 0;
117 } 52 }
118 53
119 he = __perf_session__add_hist_entry(&self->hists, al, NULL, count, &hit); 54 he = __hists__add_entry(self, al, NULL, 1);
120 if (he == NULL) 55 if (he == NULL)
121 return -ENOMEM; 56 return -ENOMEM;
122 57
123 return annotate__hist_hit(he, al->addr); 58 return hist_entry__inc_addr_samples(he, al->addr);
124} 59}
125 60
126static int process_sample_event(event_t *event, struct perf_session *session) 61static int process_sample_event(event_t *event, struct perf_session *session)
127{ 62{
128 struct addr_location al; 63 struct addr_location al;
64 struct sample_data data;
129 65
130 dump_printf("(IP, %d): %d: %#Lx\n", event->header.misc, 66 if (event__preprocess_sample(event, session, &al, &data, NULL) < 0) {
131 event->ip.pid, event->ip.ip);
132
133 if (event__preprocess_sample(event, session, &al, NULL) < 0) {
134 pr_warning("problem processing %d event, skipping it.\n", 67 pr_warning("problem processing %d event, skipping it.\n",
135 event->header.type); 68 event->header.type);
136 return -1; 69 return -1;
137 } 70 }
138 71
139 if (!al.filtered && perf_session__add_hist_entry(session, &al, 1)) { 72 if (!al.filtered && hists__add_entry(&session->hists, &al)) {
140 pr_warning("problem incrementing symbol count, " 73 pr_warning("problem incrementing symbol count, "
141 "skipping event\n"); 74 "skipping event\n");
142 return -1; 75 return -1;
@@ -145,106 +78,11 @@ static int process_sample_event(event_t *event, struct perf_session *session)
145 return 0; 78 return 0;
146} 79}
147 80
148struct objdump_line {
149 struct list_head node;
150 s64 offset;
151 char *line;
152};
153
154static struct objdump_line *objdump_line__new(s64 offset, char *line)
155{
156 struct objdump_line *self = malloc(sizeof(*self));
157
158 if (self != NULL) {
159 self->offset = offset;
160 self->line = line;
161 }
162
163 return self;
164}
165
166static void objdump_line__free(struct objdump_line *self)
167{
168 free(self->line);
169 free(self);
170}
171
172static void objdump__add_line(struct list_head *head, struct objdump_line *line)
173{
174 list_add_tail(&line->node, head);
175}
176
177static struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
178 struct objdump_line *pos)
179{
180 list_for_each_entry_continue(pos, head, node)
181 if (pos->offset >= 0)
182 return pos;
183
184 return NULL;
185}
186
187static int parse_line(FILE *file, struct hist_entry *he,
188 struct list_head *head)
189{
190 struct symbol *sym = he->sym;
191 struct objdump_line *objdump_line;
192 char *line = NULL, *tmp, *tmp2;
193 size_t line_len;
194 s64 line_ip, offset = -1;
195 char *c;
196
197 if (getline(&line, &line_len, file) < 0)
198 return -1;
199
200 if (!line)
201 return -1;
202
203 c = strchr(line, '\n');
204 if (c)
205 *c = 0;
206
207 line_ip = -1;
208
209 /*
210 * Strip leading spaces:
211 */
212 tmp = line;
213 while (*tmp) {
214 if (*tmp != ' ')
215 break;
216 tmp++;
217 }
218
219 if (*tmp) {
220 /*
221 * Parse hexa addresses followed by ':'
222 */
223 line_ip = strtoull(tmp, &tmp2, 16);
224 if (*tmp2 != ':')
225 line_ip = -1;
226 }
227
228 if (line_ip != -1) {
229 u64 start = map__rip_2objdump(he->map, sym->start);
230 offset = line_ip - start;
231 }
232
233 objdump_line = objdump_line__new(offset, line);
234 if (objdump_line == NULL) {
235 free(line);
236 return -1;
237 }
238 objdump__add_line(head, objdump_line);
239
240 return 0;
241}
242
243static int objdump_line__print(struct objdump_line *self, 81static int objdump_line__print(struct objdump_line *self,
244 struct list_head *head, 82 struct list_head *head,
245 struct hist_entry *he, u64 len) 83 struct hist_entry *he, u64 len)
246{ 84{
247 struct symbol *sym = he->sym; 85 struct symbol *sym = he->ms.sym;
248 static const char *prev_line; 86 static const char *prev_line;
249 static const char *prev_color; 87 static const char *prev_color;
250 88
@@ -327,7 +165,7 @@ static void insert_source_line(struct sym_ext *sym_ext)
327 165
328static void free_source_line(struct hist_entry *he, int len) 166static void free_source_line(struct hist_entry *he, int len)
329{ 167{
330 struct sym_priv *priv = symbol__priv(he->sym); 168 struct sym_priv *priv = symbol__priv(he->ms.sym);
331 struct sym_ext *sym_ext = priv->ext; 169 struct sym_ext *sym_ext = priv->ext;
332 int i; 170 int i;
333 171
@@ -346,7 +184,7 @@ static void free_source_line(struct hist_entry *he, int len)
346static void 184static void
347get_source_line(struct hist_entry *he, int len, const char *filename) 185get_source_line(struct hist_entry *he, int len, const char *filename)
348{ 186{
349 struct symbol *sym = he->sym; 187 struct symbol *sym = he->ms.sym;
350 u64 start; 188 u64 start;
351 int i; 189 int i;
352 char cmd[PATH_MAX * 2]; 190 char cmd[PATH_MAX * 2];
@@ -361,7 +199,7 @@ get_source_line(struct hist_entry *he, int len, const char *filename)
361 if (!priv->ext) 199 if (!priv->ext)
362 return; 200 return;
363 201
364 start = he->map->unmap_ip(he->map, sym->start); 202 start = he->ms.map->unmap_ip(he->ms.map, sym->start);
365 203
366 for (i = 0; i < len; i++) { 204 for (i = 0; i < len; i++) {
367 char *path = NULL; 205 char *path = NULL;
@@ -425,7 +263,7 @@ static void print_summary(const char *filename)
425 263
426static void hist_entry__print_hits(struct hist_entry *self) 264static void hist_entry__print_hits(struct hist_entry *self)
427{ 265{
428 struct symbol *sym = self->sym; 266 struct symbol *sym = self->ms.sym;
429 struct sym_priv *priv = symbol__priv(sym); 267 struct sym_priv *priv = symbol__priv(sym);
430 struct sym_hist *h = priv->hist; 268 struct sym_hist *h = priv->hist;
431 u64 len = sym->end - sym->start, offset; 269 u64 len = sym->end - sym->start, offset;
@@ -437,24 +275,18 @@ static void hist_entry__print_hits(struct hist_entry *self)
437 printf("%*s: %Lu\n", BITS_PER_LONG / 2, "h->sum", h->sum); 275 printf("%*s: %Lu\n", BITS_PER_LONG / 2, "h->sum", h->sum);
438} 276}
439 277
440static void annotate_sym(struct hist_entry *he) 278static int hist_entry__tty_annotate(struct hist_entry *he)
441{ 279{
442 struct map *map = he->map; 280 struct map *map = he->ms.map;
443 struct dso *dso = map->dso; 281 struct dso *dso = map->dso;
444 struct symbol *sym = he->sym; 282 struct symbol *sym = he->ms.sym;
445 const char *filename = dso->long_name, *d_filename; 283 const char *filename = dso->long_name, *d_filename;
446 u64 len; 284 u64 len;
447 char command[PATH_MAX*2];
448 FILE *file;
449 LIST_HEAD(head); 285 LIST_HEAD(head);
450 struct objdump_line *pos, *n; 286 struct objdump_line *pos, *n;
451 287
452 if (!filename) 288 if (hist_entry__annotate(he, &head) < 0)
453 return; 289 return -1;
454
455 pr_debug("%s: filename=%s, sym=%s, start=%#Lx, end=%#Lx\n", __func__,
456 filename, sym->name, map->unmap_ip(map, sym->start),
457 map->unmap_ip(map, sym->end));
458 290
459 if (full_paths) 291 if (full_paths)
460 d_filename = filename; 292 d_filename = filename;
@@ -472,29 +304,6 @@ static void annotate_sym(struct hist_entry *he)
472 printf(" Percent | Source code & Disassembly of %s\n", d_filename); 304 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
473 printf("------------------------------------------------\n"); 305 printf("------------------------------------------------\n");
474 306
475 if (verbose >= 2)
476 printf("annotating [%p] %30s : [%p] %30s\n",
477 dso, dso->long_name, sym, sym->name);
478
479 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
480 map__rip_2objdump(map, sym->start),
481 map__rip_2objdump(map, sym->end),
482 filename, filename);
483
484 if (verbose >= 3)
485 printf("doing: %s\n", command);
486
487 file = popen(command, "r");
488 if (!file)
489 return;
490
491 while (!feof(file)) {
492 if (parse_line(file, he, &head) < 0)
493 break;
494 }
495
496 pclose(file);
497
498 if (verbose) 307 if (verbose)
499 hist_entry__print_hits(he); 308 hist_entry__print_hits(he);
500 309
@@ -506,30 +315,59 @@ static void annotate_sym(struct hist_entry *he)
506 315
507 if (print_line) 316 if (print_line)
508 free_source_line(he, len); 317 free_source_line(he, len);
318
319 return 0;
509} 320}
510 321
511static void perf_session__find_annotations(struct perf_session *self) 322static void hists__find_annotations(struct hists *self)
512{ 323{
513 struct rb_node *nd; 324 struct rb_node *first = rb_first(&self->entries), *nd = first;
325 int key = KEY_RIGHT;
514 326
515 for (nd = rb_first(&self->hists); nd; nd = rb_next(nd)) { 327 while (nd) {
516 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node); 328 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
517 struct sym_priv *priv; 329 struct sym_priv *priv;
518 330
519 if (he->sym == NULL) 331 if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned)
520 continue; 332 goto find_next;
521 333
522 priv = symbol__priv(he->sym); 334 priv = symbol__priv(he->ms.sym);
523 if (priv->hist == NULL) 335 if (priv->hist == NULL) {
336find_next:
337 if (key == KEY_LEFT)
338 nd = rb_prev(nd);
339 else
340 nd = rb_next(nd);
524 continue; 341 continue;
342 }
525 343
526 annotate_sym(he); 344 if (use_browser > 0) {
527 /* 345 key = hist_entry__tui_annotate(he);
528 * Since we have a hist_entry per IP for the same symbol, free 346 if (is_exit_key(key))
529 * he->sym->hist to signal we already processed this symbol. 347 break;
530 */ 348 switch (key) {
531 free(priv->hist); 349 case KEY_RIGHT:
532 priv->hist = NULL; 350 case '\t':
351 nd = rb_next(nd);
352 break;
353 case KEY_LEFT:
354 if (nd == first)
355 continue;
356 nd = rb_prev(nd);
357 default:
358 break;
359 }
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 }
533 } 371 }
534} 372}
535 373
@@ -545,7 +383,7 @@ static int __cmd_annotate(void)
545 int ret; 383 int ret;
546 struct perf_session *session; 384 struct perf_session *session;
547 385
548 session = perf_session__new(input_name, O_RDONLY, force); 386 session = perf_session__new(input_name, O_RDONLY, force, false);
549 if (session == NULL) 387 if (session == NULL)
550 return -ENOMEM; 388 return -ENOMEM;
551 389
@@ -554,7 +392,7 @@ static int __cmd_annotate(void)
554 goto out_delete; 392 goto out_delete;
555 393
556 if (dump_trace) { 394 if (dump_trace) {
557 event__print_totals(); 395 perf_session__fprintf_nr_events(session, stdout);
558 goto out_delete; 396 goto out_delete;
559 } 397 }
560 398
@@ -562,11 +400,11 @@ static int __cmd_annotate(void)
562 perf_session__fprintf(session, stdout); 400 perf_session__fprintf(session, stdout);
563 401
564 if (verbose > 2) 402 if (verbose > 2)
565 dsos__fprintf(stdout); 403 perf_session__fprintf_dsos(session, stdout);
566 404
567 perf_session__collapse_resort(&session->hists); 405 hists__collapse_resort(&session->hists);
568 perf_session__output_resort(&session->hists, session->event_total[0]); 406 hists__output_resort(&session->hists);
569 perf_session__find_annotations(session); 407 hists__find_annotations(&session->hists);
570out_delete: 408out_delete:
571 perf_session__delete(session); 409 perf_session__delete(session);
572 410
@@ -581,10 +419,12 @@ static const char * const annotate_usage[] = {
581static const struct option options[] = { 419static const struct option options[] = {
582 OPT_STRING('i', "input", &input_name, "file", 420 OPT_STRING('i', "input", &input_name, "file",
583 "input file name"), 421 "input file name"),
422 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
423 "only consider symbols in these dsos"),
584 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol", 424 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
585 "symbol to annotate"), 425 "symbol to annotate"),
586 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), 426 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
587 OPT_BOOLEAN('v', "verbose", &verbose, 427 OPT_INCR('v', "verbose", &verbose,
588 "be more verbose (show symbol address, etc)"), 428 "be more verbose (show symbol address, etc)"),
589 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, 429 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
590 "dump raw trace in ASCII"), 430 "dump raw trace in ASCII"),
@@ -603,6 +443,8 @@ int cmd_annotate(int argc, const char **argv, const char *prefix __used)
603{ 443{
604 argc = parse_options(argc, argv, options, annotate_usage, 0); 444 argc = parse_options(argc, argv, options, annotate_usage, 0);
605 445
446 setup_browser();
447
606 symbol_conf.priv_size = sizeof(struct sym_priv); 448 symbol_conf.priv_size = sizeof(struct sym_priv);
607 symbol_conf.try_vmlinux_path = true; 449 symbol_conf.try_vmlinux_path = true;
608 450
@@ -622,8 +464,6 @@ int cmd_annotate(int argc, const char **argv, const char *prefix __used)
622 sym_hist_filter = argv[0]; 464 sym_hist_filter = argv[0];
623 } 465 }
624 466
625 setup_pager();
626
627 if (field_sep && *field_sep == '.') { 467 if (field_sep && *field_sep == '.') {
628 pr_err("'.' is the only non valid --field-separator argument\n"); 468 pr_err("'.' is the only non valid --field-separator argument\n");
629 return -1; 469 return -1;