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.c245
1 files changed, 29 insertions, 216 deletions
diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c
index 6ad7148451c5..77bcc9b130f5 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,11 +51,11 @@ 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)
@@ -136,7 +71,7 @@ static int process_sample_event(event_t *event, struct perf_session *session)
136 return -1; 71 return -1;
137 } 72 }
138 73
139 if (!al.filtered && perf_session__add_hist_entry(session, &al, 1)) { 74 if (!al.filtered && hists__add_entry(&session->hists, &al)) {
140 pr_warning("problem incrementing symbol count, " 75 pr_warning("problem incrementing symbol count, "
141 "skipping event\n"); 76 "skipping event\n");
142 return -1; 77 return -1;
@@ -145,106 +80,11 @@ static int process_sample_event(event_t *event, struct perf_session *session)
145 return 0; 80 return 0;
146} 81}
147 82
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, 83static int objdump_line__print(struct objdump_line *self,
244 struct list_head *head, 84 struct list_head *head,
245 struct hist_entry *he, u64 len) 85 struct hist_entry *he, u64 len)
246{ 86{
247 struct symbol *sym = he->sym; 87 struct symbol *sym = he->ms.sym;
248 static const char *prev_line; 88 static const char *prev_line;
249 static const char *prev_color; 89 static const char *prev_color;
250 90
@@ -327,7 +167,7 @@ static void insert_source_line(struct sym_ext *sym_ext)
327 167
328static void free_source_line(struct hist_entry *he, int len) 168static void free_source_line(struct hist_entry *he, int len)
329{ 169{
330 struct sym_priv *priv = symbol__priv(he->sym); 170 struct sym_priv *priv = symbol__priv(he->ms.sym);
331 struct sym_ext *sym_ext = priv->ext; 171 struct sym_ext *sym_ext = priv->ext;
332 int i; 172 int i;
333 173
@@ -346,7 +186,7 @@ static void free_source_line(struct hist_entry *he, int len)
346static void 186static void
347get_source_line(struct hist_entry *he, int len, const char *filename) 187get_source_line(struct hist_entry *he, int len, const char *filename)
348{ 188{
349 struct symbol *sym = he->sym; 189 struct symbol *sym = he->ms.sym;
350 u64 start; 190 u64 start;
351 int i; 191 int i;
352 char cmd[PATH_MAX * 2]; 192 char cmd[PATH_MAX * 2];
@@ -361,7 +201,7 @@ get_source_line(struct hist_entry *he, int len, const char *filename)
361 if (!priv->ext) 201 if (!priv->ext)
362 return; 202 return;
363 203
364 start = he->map->unmap_ip(he->map, sym->start); 204 start = he->ms.map->unmap_ip(he->ms.map, sym->start);
365 205
366 for (i = 0; i < len; i++) { 206 for (i = 0; i < len; i++) {
367 char *path = NULL; 207 char *path = NULL;
@@ -425,7 +265,7 @@ static void print_summary(const char *filename)
425 265
426static void hist_entry__print_hits(struct hist_entry *self) 266static void hist_entry__print_hits(struct hist_entry *self)
427{ 267{
428 struct symbol *sym = self->sym; 268 struct symbol *sym = self->ms.sym;
429 struct sym_priv *priv = symbol__priv(sym); 269 struct sym_priv *priv = symbol__priv(sym);
430 struct sym_hist *h = priv->hist; 270 struct sym_hist *h = priv->hist;
431 u64 len = sym->end - sym->start, offset; 271 u64 len = sym->end - sym->start, offset;
@@ -439,23 +279,17 @@ static void hist_entry__print_hits(struct hist_entry *self)
439 279
440static void annotate_sym(struct hist_entry *he) 280static void annotate_sym(struct hist_entry *he)
441{ 281{
442 struct map *map = he->map; 282 struct map *map = he->ms.map;
443 struct dso *dso = map->dso; 283 struct dso *dso = map->dso;
444 struct symbol *sym = he->sym; 284 struct symbol *sym = he->ms.sym;
445 const char *filename = dso->long_name, *d_filename; 285 const char *filename = dso->long_name, *d_filename;
446 u64 len; 286 u64 len;
447 char command[PATH_MAX*2];
448 FILE *file;
449 LIST_HEAD(head); 287 LIST_HEAD(head);
450 struct objdump_line *pos, *n; 288 struct objdump_line *pos, *n;
451 289
452 if (!filename) 290 if (hist_entry__annotate(he, &head) < 0)
453 return; 291 return;
454 292
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
459 if (full_paths) 293 if (full_paths)
460 d_filename = filename; 294 d_filename = filename;
461 else 295 else
@@ -472,29 +306,6 @@ static void annotate_sym(struct hist_entry *he)
472 printf(" Percent | Source code & Disassembly of %s\n", d_filename); 306 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
473 printf("------------------------------------------------\n"); 307 printf("------------------------------------------------\n");
474 308
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) 309 if (verbose)
499 hist_entry__print_hits(he); 310 hist_entry__print_hits(he);
500 311
@@ -508,25 +319,25 @@ static void annotate_sym(struct hist_entry *he)
508 free_source_line(he, len); 319 free_source_line(he, len);
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 *nd;
514 325
515 for (nd = rb_first(&self->hists); nd; nd = rb_next(nd)) { 326 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
516 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node); 327 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
517 struct sym_priv *priv; 328 struct sym_priv *priv;
518 329
519 if (he->sym == NULL) 330 if (he->ms.sym == NULL)
520 continue; 331 continue;
521 332
522 priv = symbol__priv(he->sym); 333 priv = symbol__priv(he->ms.sym);
523 if (priv->hist == NULL) 334 if (priv->hist == NULL)
524 continue; 335 continue;
525 336
526 annotate_sym(he); 337 annotate_sym(he);
527 /* 338 /*
528 * Since we have a hist_entry per IP for the same symbol, free 339 * Since we have a hist_entry per IP for the same symbol, free
529 * he->sym->hist to signal we already processed this symbol. 340 * he->ms.sym->hist to signal we already processed this symbol.
530 */ 341 */
531 free(priv->hist); 342 free(priv->hist);
532 priv->hist = NULL; 343 priv->hist = NULL;
@@ -545,7 +356,7 @@ static int __cmd_annotate(void)
545 int ret; 356 int ret;
546 struct perf_session *session; 357 struct perf_session *session;
547 358
548 session = perf_session__new(input_name, O_RDONLY, force); 359 session = perf_session__new(input_name, O_RDONLY, force, false);
549 if (session == NULL) 360 if (session == NULL)
550 return -ENOMEM; 361 return -ENOMEM;
551 362
@@ -554,7 +365,7 @@ static int __cmd_annotate(void)
554 goto out_delete; 365 goto out_delete;
555 366
556 if (dump_trace) { 367 if (dump_trace) {
557 event__print_totals(); 368 perf_session__fprintf_nr_events(session, stdout);
558 goto out_delete; 369 goto out_delete;
559 } 370 }
560 371
@@ -562,11 +373,11 @@ static int __cmd_annotate(void)
562 perf_session__fprintf(session, stdout); 373 perf_session__fprintf(session, stdout);
563 374
564 if (verbose > 2) 375 if (verbose > 2)
565 dsos__fprintf(stdout); 376 perf_session__fprintf_dsos(session, stdout);
566 377
567 perf_session__collapse_resort(&session->hists); 378 hists__collapse_resort(&session->hists);
568 perf_session__output_resort(&session->hists, session->event_total[0]); 379 hists__output_resort(&session->hists);
569 perf_session__find_annotations(session); 380 hists__find_annotations(&session->hists);
570out_delete: 381out_delete:
571 perf_session__delete(session); 382 perf_session__delete(session);
572 383
@@ -581,10 +392,12 @@ static const char * const annotate_usage[] = {
581static const struct option options[] = { 392static const struct option options[] = {
582 OPT_STRING('i', "input", &input_name, "file", 393 OPT_STRING('i', "input", &input_name, "file",
583 "input file name"), 394 "input file name"),
395 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
396 "only consider symbols in these dsos"),
584 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol", 397 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
585 "symbol to annotate"), 398 "symbol to annotate"),
586 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), 399 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
587 OPT_BOOLEAN('v', "verbose", &verbose, 400 OPT_INCR('v', "verbose", &verbose,
588 "be more verbose (show symbol address, etc)"), 401 "be more verbose (show symbol address, etc)"),
589 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, 402 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
590 "dump raw trace in ASCII"), 403 "dump raw trace in ASCII"),