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.c1012
1 files changed, 251 insertions, 761 deletions
diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c
index 1ec741615814..6ad7148451c5 100644
--- a/tools/perf/builtin-annotate.c
+++ b/tools/perf/builtin-annotate.c
@@ -19,29 +19,26 @@
19#include "perf.h" 19#include "perf.h"
20#include "util/debug.h" 20#include "util/debug.h"
21 21
22#include "util/event.h"
22#include "util/parse-options.h" 23#include "util/parse-options.h"
23#include "util/parse-events.h" 24#include "util/parse-events.h"
24#include "util/thread.h" 25#include "util/thread.h"
26#include "util/sort.h"
27#include "util/hist.h"
28#include "util/session.h"
25 29
26static char const *input_name = "perf.data"; 30static char const *input_name = "perf.data";
27 31
28static char default_sort_order[] = "comm,symbol";
29static char *sort_order = default_sort_order;
30
31static int force; 32static int force;
32static int input;
33static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
34 33
35static int full_paths; 34static int full_paths;
36 35
37static int print_line; 36static int print_line;
38 37
39static unsigned long page_size; 38struct sym_hist {
40static unsigned long mmap_window = 32; 39 u64 sum;
41 40 u64 ip[0];
42static struct rb_root threads; 41};
43static struct thread *last_match;
44
45 42
46struct sym_ext { 43struct sym_ext {
47 struct rb_node node; 44 struct rb_node node;
@@ -49,636 +46,157 @@ struct sym_ext {
49 char *path; 46 char *path;
50}; 47};
51 48
52/* 49struct sym_priv {
53 * histogram, sorted on item, collects counts 50 struct sym_hist *hist;
54 */ 51 struct sym_ext *ext;
55
56static struct rb_root hist;
57
58struct hist_entry {
59 struct rb_node rb_node;
60
61 struct thread *thread;
62 struct map *map;
63 struct dso *dso;
64 struct symbol *sym;
65 u64 ip;
66 char level;
67
68 uint32_t count;
69};
70
71/*
72 * configurable sorting bits
73 */
74
75struct sort_entry {
76 struct list_head list;
77
78 const char *header;
79
80 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
81 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
82 size_t (*print)(FILE *fp, struct hist_entry *);
83};
84
85/* --sort pid */
86
87static int64_t
88sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
89{
90 return right->thread->pid - left->thread->pid;
91}
92
93static size_t
94sort__thread_print(FILE *fp, struct hist_entry *self)
95{
96 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
97}
98
99static struct sort_entry sort_thread = {
100 .header = " Command: Pid",
101 .cmp = sort__thread_cmp,
102 .print = sort__thread_print,
103};
104
105/* --sort comm */
106
107static int64_t
108sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
109{
110 return right->thread->pid - left->thread->pid;
111}
112
113static int64_t
114sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
115{
116 char *comm_l = left->thread->comm;
117 char *comm_r = right->thread->comm;
118
119 if (!comm_l || !comm_r) {
120 if (!comm_l && !comm_r)
121 return 0;
122 else if (!comm_l)
123 return -1;
124 else
125 return 1;
126 }
127
128 return strcmp(comm_l, comm_r);
129}
130
131static size_t
132sort__comm_print(FILE *fp, struct hist_entry *self)
133{
134 return fprintf(fp, "%16s", self->thread->comm);
135}
136
137static struct sort_entry sort_comm = {
138 .header = " Command",
139 .cmp = sort__comm_cmp,
140 .collapse = sort__comm_collapse,
141 .print = sort__comm_print,
142};
143
144/* --sort dso */
145
146static int64_t
147sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
148{
149 struct dso *dso_l = left->dso;
150 struct dso *dso_r = right->dso;
151
152 if (!dso_l || !dso_r) {
153 if (!dso_l && !dso_r)
154 return 0;
155 else if (!dso_l)
156 return -1;
157 else
158 return 1;
159 }
160
161 return strcmp(dso_l->name, dso_r->name);
162}
163
164static size_t
165sort__dso_print(FILE *fp, struct hist_entry *self)
166{
167 if (self->dso)
168 return fprintf(fp, "%-25s", self->dso->name);
169
170 return fprintf(fp, "%016llx ", (u64)self->ip);
171}
172
173static struct sort_entry sort_dso = {
174 .header = "Shared Object ",
175 .cmp = sort__dso_cmp,
176 .print = sort__dso_print,
177};
178
179/* --sort symbol */
180
181static int64_t
182sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
183{
184 u64 ip_l, ip_r;
185
186 if (left->sym == right->sym)
187 return 0;
188
189 ip_l = left->sym ? left->sym->start : left->ip;
190 ip_r = right->sym ? right->sym->start : right->ip;
191
192 return (int64_t)(ip_r - ip_l);
193}
194
195static size_t
196sort__sym_print(FILE *fp, struct hist_entry *self)
197{
198 size_t ret = 0;
199
200 if (verbose)
201 ret += fprintf(fp, "%#018llx ", (u64)self->ip);
202
203 if (self->sym) {
204 ret += fprintf(fp, "[%c] %s",
205 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
206 } else {
207 ret += fprintf(fp, "%#016llx", (u64)self->ip);
208 }
209
210 return ret;
211}
212
213static struct sort_entry sort_sym = {
214 .header = "Symbol",
215 .cmp = sort__sym_cmp,
216 .print = sort__sym_print,
217};
218
219static int sort__need_collapse = 0;
220
221struct sort_dimension {
222 const char *name;
223 struct sort_entry *entry;
224 int taken;
225}; 52};
226 53
227static struct sort_dimension sort_dimensions[] = { 54static const char *sym_hist_filter;
228 { .name = "pid", .entry = &sort_thread, },
229 { .name = "comm", .entry = &sort_comm, },
230 { .name = "dso", .entry = &sort_dso, },
231 { .name = "symbol", .entry = &sort_sym, },
232};
233
234static LIST_HEAD(hist_entry__sort_list);
235
236static int sort_dimension__add(char *tok)
237{
238 unsigned int i;
239
240 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
241 struct sort_dimension *sd = &sort_dimensions[i];
242
243 if (sd->taken)
244 continue;
245
246 if (strncasecmp(tok, sd->name, strlen(tok)))
247 continue;
248
249 if (sd->entry->collapse)
250 sort__need_collapse = 1;
251
252 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
253 sd->taken = 1;
254
255 return 0;
256 }
257
258 return -ESRCH;
259}
260
261static int64_t
262hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
263{
264 struct sort_entry *se;
265 int64_t cmp = 0;
266
267 list_for_each_entry(se, &hist_entry__sort_list, list) {
268 cmp = se->cmp(left, right);
269 if (cmp)
270 break;
271 }
272
273 return cmp;
274}
275 55
276static int64_t 56static int sym__alloc_hist(struct symbol *self)
277hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
278{ 57{
279 struct sort_entry *se; 58 struct sym_priv *priv = symbol__priv(self);
280 int64_t cmp = 0; 59 const int size = (sizeof(*priv->hist) +
60 (self->end - self->start) * sizeof(u64));
281 61
282 list_for_each_entry(se, &hist_entry__sort_list, list) { 62 priv->hist = zalloc(size);
283 int64_t (*f)(struct hist_entry *, struct hist_entry *); 63 return priv->hist == NULL ? -1 : 0;
284
285 f = se->collapse ?: se->cmp;
286
287 cmp = f(left, right);
288 if (cmp)
289 break;
290 }
291
292 return cmp;
293} 64}
294 65
295/* 66/*
296 * collect histogram counts 67 * collect histogram counts
297 */ 68 */
298static void hist_hit(struct hist_entry *he, u64 ip) 69static int annotate__hist_hit(struct hist_entry *he, u64 ip)
299{ 70{
300 unsigned int sym_size, offset; 71 unsigned int sym_size, offset;
301 struct symbol *sym = he->sym; 72 struct symbol *sym = he->sym;
73 struct sym_priv *priv;
74 struct sym_hist *h;
302 75
303 he->count++; 76 he->count++;
304 77
305 if (!sym || !sym->hist) 78 if (!sym || !he->map)
306 return; 79 return 0;
80
81 priv = symbol__priv(sym);
82 if (priv->hist == NULL && sym__alloc_hist(sym) < 0)
83 return -ENOMEM;
307 84
308 sym_size = sym->end - sym->start; 85 sym_size = sym->end - sym->start;
309 offset = ip - sym->start; 86 offset = ip - sym->start;
310 87
88 pr_debug3("%s: ip=%#Lx\n", __func__, he->map->unmap_ip(he->map, ip));
89
311 if (offset >= sym_size) 90 if (offset >= sym_size)
312 return; 91 return 0;
313 92
314 sym->hist_sum++; 93 h = priv->hist;
315 sym->hist[offset]++; 94 h->sum++;
95 h->ip[offset]++;
316 96
317 if (verbose >= 3) 97 pr_debug3("%#Lx %s: count++ [ip: %#Lx, %#Lx] => %Ld\n", he->sym->start,
318 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n", 98 he->sym->name, ip, ip - he->sym->start, h->ip[offset]);
319 (void *)(unsigned long)he->sym->start, 99 return 0;
320 he->sym->name,
321 (void *)(unsigned long)ip, ip - he->sym->start,
322 sym->hist[offset]);
323} 100}
324 101
325static int 102static int perf_session__add_hist_entry(struct perf_session *self,
326hist_entry__add(struct thread *thread, struct map *map, struct dso *dso, 103 struct addr_location *al, u64 count)
327 struct symbol *sym, u64 ip, char level)
328{ 104{
329 struct rb_node **p = &hist.rb_node; 105 bool hit;
330 struct rb_node *parent = NULL;
331 struct hist_entry *he; 106 struct hist_entry *he;
332 struct hist_entry entry = {
333 .thread = thread,
334 .map = map,
335 .dso = dso,
336 .sym = sym,
337 .ip = ip,
338 .level = level,
339 .count = 1,
340 };
341 int cmp;
342
343 while (*p != NULL) {
344 parent = *p;
345 he = rb_entry(parent, struct hist_entry, rb_node);
346
347 cmp = hist_entry__cmp(&entry, he);
348
349 if (!cmp) {
350 hist_hit(he, ip);
351 107
352 return 0; 108 if (sym_hist_filter != NULL &&
109 (al->sym == NULL || strcmp(sym_hist_filter, al->sym->name) != 0)) {
110 /* We're only interested in a symbol named sym_hist_filter */
111 if (al->sym != NULL) {
112 rb_erase(&al->sym->rb_node,
113 &al->map->dso->symbols[al->map->type]);
114 symbol__delete(al->sym);
353 } 115 }
354 116 return 0;
355 if (cmp < 0)
356 p = &(*p)->rb_left;
357 else
358 p = &(*p)->rb_right;
359 } 117 }
360 118
361 he = malloc(sizeof(*he)); 119 he = __perf_session__add_hist_entry(&self->hists, al, NULL, count, &hit);
362 if (!he) 120 if (he == NULL)
363 return -ENOMEM; 121 return -ENOMEM;
364 *he = entry;
365 rb_link_node(&he->rb_node, parent, p);
366 rb_insert_color(&he->rb_node, &hist);
367
368 return 0;
369}
370
371static void hist_entry__free(struct hist_entry *he)
372{
373 free(he);
374}
375
376/*
377 * collapse the histogram
378 */
379
380static struct rb_root collapse_hists;
381
382static void collapse__insert_entry(struct hist_entry *he)
383{
384 struct rb_node **p = &collapse_hists.rb_node;
385 struct rb_node *parent = NULL;
386 struct hist_entry *iter;
387 int64_t cmp;
388
389 while (*p != NULL) {
390 parent = *p;
391 iter = rb_entry(parent, struct hist_entry, rb_node);
392
393 cmp = hist_entry__collapse(iter, he);
394
395 if (!cmp) {
396 iter->count += he->count;
397 hist_entry__free(he);
398 return;
399 }
400
401 if (cmp < 0)
402 p = &(*p)->rb_left;
403 else
404 p = &(*p)->rb_right;
405 }
406
407 rb_link_node(&he->rb_node, parent, p);
408 rb_insert_color(&he->rb_node, &collapse_hists);
409}
410
411static void collapse__resort(void)
412{
413 struct rb_node *next;
414 struct hist_entry *n;
415
416 if (!sort__need_collapse)
417 return;
418
419 next = rb_first(&hist);
420 while (next) {
421 n = rb_entry(next, struct hist_entry, rb_node);
422 next = rb_next(&n->rb_node);
423
424 rb_erase(&n->rb_node, &hist);
425 collapse__insert_entry(n);
426 }
427}
428
429/*
430 * reverse the map, sort on count.
431 */
432
433static struct rb_root output_hists;
434
435static void output__insert_entry(struct hist_entry *he)
436{
437 struct rb_node **p = &output_hists.rb_node;
438 struct rb_node *parent = NULL;
439 struct hist_entry *iter;
440
441 while (*p != NULL) {
442 parent = *p;
443 iter = rb_entry(parent, struct hist_entry, rb_node);
444
445 if (he->count > iter->count)
446 p = &(*p)->rb_left;
447 else
448 p = &(*p)->rb_right;
449 }
450 122
451 rb_link_node(&he->rb_node, parent, p); 123 return annotate__hist_hit(he, al->addr);
452 rb_insert_color(&he->rb_node, &output_hists);
453} 124}
454 125
455static void output__resort(void) 126static int process_sample_event(event_t *event, struct perf_session *session)
456{ 127{
457 struct rb_node *next; 128 struct addr_location al;
458 struct hist_entry *n;
459 struct rb_root *tree = &hist;
460
461 if (sort__need_collapse)
462 tree = &collapse_hists;
463
464 next = rb_first(tree);
465
466 while (next) {
467 n = rb_entry(next, struct hist_entry, rb_node);
468 next = rb_next(&n->rb_node);
469
470 rb_erase(&n->rb_node, tree);
471 output__insert_entry(n);
472 }
473}
474 129
475static unsigned long total = 0, 130 dump_printf("(IP, %d): %d: %#Lx\n", event->header.misc,
476 total_mmap = 0, 131 event->ip.pid, event->ip.ip);
477 total_comm = 0,
478 total_fork = 0,
479 total_unknown = 0;
480 132
481static int 133 if (event__preprocess_sample(event, session, &al, NULL) < 0) {
482process_sample_event(event_t *event, unsigned long offset, unsigned long head) 134 pr_warning("problem processing %d event, skipping it.\n",
483{ 135 event->header.type);
484 char level;
485 int show = 0;
486 struct dso *dso = NULL;
487 struct thread *thread;
488 u64 ip = event->ip.ip;
489 struct map *map = NULL;
490
491 thread = threads__findnew(event->ip.pid, &threads, &last_match);
492
493 dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
494 (void *)(offset + head),
495 (void *)(long)(event->header.size),
496 event->header.misc,
497 event->ip.pid,
498 (void *)(long)ip);
499
500 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
501
502 if (thread == NULL) {
503 fprintf(stderr, "problem processing %d event, skipping it.\n",
504 event->header.type);
505 return -1; 136 return -1;
506 } 137 }
507 138
508 if (event->header.misc & PERF_RECORD_MISC_KERNEL) { 139 if (!al.filtered && perf_session__add_hist_entry(session, &al, 1)) {
509 show = SHOW_KERNEL; 140 pr_warning("problem incrementing symbol count, "
510 level = 'k'; 141 "skipping event\n");
511 142 return -1;
512 dso = kernel_dso;
513
514 dump_printf(" ...... dso: %s\n", dso->name);
515
516 } else if (event->header.misc & PERF_RECORD_MISC_USER) {
517
518 show = SHOW_USER;
519 level = '.';
520
521 map = thread__find_map(thread, ip);
522 if (map != NULL) {
523 ip = map->map_ip(map, ip);
524 dso = map->dso;
525 } else {
526 /*
527 * If this is outside of all known maps,
528 * and is a negative address, try to look it
529 * up in the kernel dso, as it might be a
530 * vsyscall (which executes in user-mode):
531 */
532 if ((long long)ip < 0)
533 dso = kernel_dso;
534 }
535 dump_printf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
536
537 } else {
538 show = SHOW_HV;
539 level = 'H';
540 dump_printf(" ...... dso: [hypervisor]\n");
541 }
542
543 if (show & show_mask) {
544 struct symbol *sym = NULL;
545
546 if (dso)
547 sym = dso->find_symbol(dso, ip);
548
549 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
550 fprintf(stderr,
551 "problem incrementing symbol count, skipping event\n");
552 return -1;
553 }
554 } 143 }
555 total++;
556 144
557 return 0; 145 return 0;
558} 146}
559 147
560static int 148struct objdump_line {
561process_mmap_event(event_t *event, unsigned long offset, unsigned long head) 149 struct list_head node;
562{ 150 s64 offset;
563 struct thread *thread; 151 char *line;
564 struct map *map = map__new(&event->mmap, NULL, 0); 152};
565
566 thread = threads__findnew(event->mmap.pid, &threads, &last_match);
567
568 dump_printf("%p [%p]: PERF_RECORD_MMAP %d: [%p(%p) @ %p]: %s\n",
569 (void *)(offset + head),
570 (void *)(long)(event->header.size),
571 event->mmap.pid,
572 (void *)(long)event->mmap.start,
573 (void *)(long)event->mmap.len,
574 (void *)(long)event->mmap.pgoff,
575 event->mmap.filename);
576
577 if (thread == NULL || map == NULL) {
578 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
579 return 0;
580 }
581
582 thread__insert_map(thread, map);
583 total_mmap++;
584
585 return 0;
586}
587 153
588static int 154static struct objdump_line *objdump_line__new(s64 offset, char *line)
589process_comm_event(event_t *event, unsigned long offset, unsigned long head)
590{ 155{
591 struct thread *thread; 156 struct objdump_line *self = malloc(sizeof(*self));
592 157
593 thread = threads__findnew(event->comm.pid, &threads, &last_match); 158 if (self != NULL) {
594 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n", 159 self->offset = offset;
595 (void *)(offset + head), 160 self->line = line;
596 (void *)(long)(event->header.size),
597 event->comm.comm, event->comm.pid);
598
599 if (thread == NULL ||
600 thread__set_comm(thread, event->comm.comm)) {
601 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
602 return -1;
603 } 161 }
604 total_comm++;
605 162
606 return 0; 163 return self;
607} 164}
608 165
609static int 166static void objdump_line__free(struct objdump_line *self)
610process_fork_event(event_t *event, unsigned long offset, unsigned long head)
611{ 167{
612 struct thread *thread; 168 free(self->line);
613 struct thread *parent; 169 free(self);
614
615 thread = threads__findnew(event->fork.pid, &threads, &last_match);
616 parent = threads__findnew(event->fork.ppid, &threads, &last_match);
617 dump_printf("%p [%p]: PERF_RECORD_FORK: %d:%d\n",
618 (void *)(offset + head),
619 (void *)(long)(event->header.size),
620 event->fork.pid, event->fork.ppid);
621
622 /*
623 * A thread clone will have the same PID for both
624 * parent and child.
625 */
626 if (thread == parent)
627 return 0;
628
629 if (!thread || !parent || thread__fork(thread, parent)) {
630 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
631 return -1;
632 }
633 total_fork++;
634
635 return 0;
636} 170}
637 171
638static int 172static void objdump__add_line(struct list_head *head, struct objdump_line *line)
639process_event(event_t *event, unsigned long offset, unsigned long head)
640{ 173{
641 switch (event->header.type) { 174 list_add_tail(&line->node, head);
642 case PERF_RECORD_SAMPLE: 175}
643 return process_sample_event(event, offset, head);
644
645 case PERF_RECORD_MMAP:
646 return process_mmap_event(event, offset, head);
647
648 case PERF_RECORD_COMM:
649 return process_comm_event(event, offset, head);
650
651 case PERF_RECORD_FORK:
652 return process_fork_event(event, offset, head);
653 /*
654 * We dont process them right now but they are fine:
655 */
656
657 case PERF_RECORD_THROTTLE:
658 case PERF_RECORD_UNTHROTTLE:
659 return 0;
660 176
661 default: 177static struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
662 return -1; 178 struct objdump_line *pos)
663 } 179{
180 list_for_each_entry_continue(pos, head, node)
181 if (pos->offset >= 0)
182 return pos;
664 183
665 return 0; 184 return NULL;
666} 185}
667 186
668static int 187static int parse_line(FILE *file, struct hist_entry *he,
669parse_line(FILE *file, struct symbol *sym, u64 start, u64 len) 188 struct list_head *head)
670{ 189{
190 struct symbol *sym = he->sym;
191 struct objdump_line *objdump_line;
671 char *line = NULL, *tmp, *tmp2; 192 char *line = NULL, *tmp, *tmp2;
672 static const char *prev_line;
673 static const char *prev_color;
674 unsigned int offset;
675 size_t line_len; 193 size_t line_len;
676 s64 line_ip; 194 s64 line_ip, offset = -1;
677 int ret;
678 char *c; 195 char *c;
679 196
680 if (getline(&line, &line_len, file) < 0) 197 if (getline(&line, &line_len, file) < 0)
681 return -1; 198 return -1;
199
682 if (!line) 200 if (!line)
683 return -1; 201 return -1;
684 202
@@ -687,8 +205,6 @@ parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
687 *c = 0; 205 *c = 0;
688 206
689 line_ip = -1; 207 line_ip = -1;
690 offset = 0;
691 ret = -2;
692 208
693 /* 209 /*
694 * Strip leading spaces: 210 * Strip leading spaces:
@@ -710,21 +226,53 @@ parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
710 } 226 }
711 227
712 if (line_ip != -1) { 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,
244 struct list_head *head,
245 struct hist_entry *he, u64 len)
246{
247 struct symbol *sym = he->sym;
248 static const char *prev_line;
249 static const char *prev_color;
250
251 if (self->offset != -1) {
713 const char *path = NULL; 252 const char *path = NULL;
714 unsigned int hits = 0; 253 unsigned int hits = 0;
715 double percent = 0.0; 254 double percent = 0.0;
716 const char *color; 255 const char *color;
717 struct sym_ext *sym_ext = sym->priv; 256 struct sym_priv *priv = symbol__priv(sym);
718 257 struct sym_ext *sym_ext = priv->ext;
719 offset = line_ip - start; 258 struct sym_hist *h = priv->hist;
720 if (offset < len) 259 s64 offset = self->offset;
721 hits = sym->hist[offset]; 260 struct objdump_line *next = objdump__get_next_ip_line(head, self);
261
262 while (offset < (s64)len &&
263 (next == NULL || offset < next->offset)) {
264 if (sym_ext) {
265 if (path == NULL)
266 path = sym_ext[offset].path;
267 percent += sym_ext[offset].percent;
268 } else
269 hits += h->ip[offset];
270
271 ++offset;
272 }
722 273
723 if (offset < len && sym_ext) { 274 if (sym_ext == NULL && h->sum)
724 path = sym_ext[offset].path; 275 percent = 100.0 * hits / h->sum;
725 percent = sym_ext[offset].percent;
726 } else if (sym->hist_sum)
727 percent = 100.0 * hits / sym->hist_sum;
728 276
729 color = get_percent_color(percent); 277 color = get_percent_color(percent);
730 278
@@ -744,12 +292,12 @@ parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
744 292
745 color_fprintf(stdout, color, " %7.2f", percent); 293 color_fprintf(stdout, color, " %7.2f", percent);
746 printf(" : "); 294 printf(" : ");
747 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line); 295 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", self->line);
748 } else { 296 } else {
749 if (!*line) 297 if (!*self->line)
750 printf(" :\n"); 298 printf(" :\n");
751 else 299 else
752 printf(" : %s\n", line); 300 printf(" : %s\n", self->line);
753 } 301 }
754 302
755 return 0; 303 return 0;
@@ -777,9 +325,10 @@ static void insert_source_line(struct sym_ext *sym_ext)
777 rb_insert_color(&sym_ext->node, &root_sym_ext); 325 rb_insert_color(&sym_ext->node, &root_sym_ext);
778} 326}
779 327
780static void free_source_line(struct symbol *sym, int len) 328static void free_source_line(struct hist_entry *he, int len)
781{ 329{
782 struct sym_ext *sym_ext = sym->priv; 330 struct sym_priv *priv = symbol__priv(he->sym);
331 struct sym_ext *sym_ext = priv->ext;
783 int i; 332 int i;
784 333
785 if (!sym_ext) 334 if (!sym_ext)
@@ -789,26 +338,30 @@ static void free_source_line(struct symbol *sym, int len)
789 free(sym_ext[i].path); 338 free(sym_ext[i].path);
790 free(sym_ext); 339 free(sym_ext);
791 340
792 sym->priv = NULL; 341 priv->ext = NULL;
793 root_sym_ext = RB_ROOT; 342 root_sym_ext = RB_ROOT;
794} 343}
795 344
796/* Get the filename:line for the colored entries */ 345/* Get the filename:line for the colored entries */
797static void 346static void
798get_source_line(struct symbol *sym, u64 start, int len, const char *filename) 347get_source_line(struct hist_entry *he, int len, const char *filename)
799{ 348{
349 struct symbol *sym = he->sym;
350 u64 start;
800 int i; 351 int i;
801 char cmd[PATH_MAX * 2]; 352 char cmd[PATH_MAX * 2];
802 struct sym_ext *sym_ext; 353 struct sym_ext *sym_ext;
354 struct sym_priv *priv = symbol__priv(sym);
355 struct sym_hist *h = priv->hist;
803 356
804 if (!sym->hist_sum) 357 if (!h->sum)
805 return; 358 return;
806 359
807 sym->priv = calloc(len, sizeof(struct sym_ext)); 360 sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
808 if (!sym->priv) 361 if (!priv->ext)
809 return; 362 return;
810 363
811 sym_ext = sym->priv; 364 start = he->map->unmap_ip(he->map, sym->start);
812 365
813 for (i = 0; i < len; i++) { 366 for (i = 0; i < len; i++) {
814 char *path = NULL; 367 char *path = NULL;
@@ -816,7 +369,7 @@ get_source_line(struct symbol *sym, u64 start, int len, const char *filename)
816 u64 offset; 369 u64 offset;
817 FILE *fp; 370 FILE *fp;
818 371
819 sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum; 372 sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
820 if (sym_ext[i].percent <= 0.5) 373 if (sym_ext[i].percent <= 0.5)
821 continue; 374 continue;
822 375
@@ -870,33 +423,48 @@ static void print_summary(const char *filename)
870 } 423 }
871} 424}
872 425
873static void annotate_sym(struct dso *dso, struct symbol *sym) 426static void hist_entry__print_hits(struct hist_entry *self)
427{
428 struct symbol *sym = self->sym;
429 struct sym_priv *priv = symbol__priv(sym);
430 struct sym_hist *h = priv->hist;
431 u64 len = sym->end - sym->start, offset;
432
433 for (offset = 0; offset < len; ++offset)
434 if (h->ip[offset] != 0)
435 printf("%*Lx: %Lu\n", BITS_PER_LONG / 2,
436 sym->start + offset, h->ip[offset]);
437 printf("%*s: %Lu\n", BITS_PER_LONG / 2, "h->sum", h->sum);
438}
439
440static void annotate_sym(struct hist_entry *he)
874{ 441{
875 const char *filename = dso->name, *d_filename; 442 struct map *map = he->map;
876 u64 start, end, len; 443 struct dso *dso = map->dso;
444 struct symbol *sym = he->sym;
445 const char *filename = dso->long_name, *d_filename;
446 u64 len;
877 char command[PATH_MAX*2]; 447 char command[PATH_MAX*2];
878 FILE *file; 448 FILE *file;
449 LIST_HEAD(head);
450 struct objdump_line *pos, *n;
879 451
880 if (!filename) 452 if (!filename)
881 return; 453 return;
882 if (sym->module) 454
883 filename = sym->module->path; 455 pr_debug("%s: filename=%s, sym=%s, start=%#Lx, end=%#Lx\n", __func__,
884 else if (dso == kernel_dso) 456 filename, sym->name, map->unmap_ip(map, sym->start),
885 filename = vmlinux_name; 457 map->unmap_ip(map, sym->end));
886 458
887 start = sym->obj_start;
888 if (!start)
889 start = sym->start;
890 if (full_paths) 459 if (full_paths)
891 d_filename = filename; 460 d_filename = filename;
892 else 461 else
893 d_filename = basename(filename); 462 d_filename = basename(filename);
894 463
895 end = start + sym->end - sym->start + 1;
896 len = sym->end - sym->start; 464 len = sym->end - sym->start;
897 465
898 if (print_line) { 466 if (print_line) {
899 get_source_line(sym, start, len, filename); 467 get_source_line(he, len, filename);
900 print_summary(filename); 468 print_summary(filename);
901 } 469 }
902 470
@@ -905,10 +473,13 @@ static void annotate_sym(struct dso *dso, struct symbol *sym)
905 printf("------------------------------------------------\n"); 473 printf("------------------------------------------------\n");
906 474
907 if (verbose >= 2) 475 if (verbose >= 2)
908 printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name); 476 printf("annotating [%p] %30s : [%p] %30s\n",
477 dso, dso->long_name, sym, sym->name);
909 478
910 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s", 479 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
911 (u64)start, (u64)end, filename, filename); 480 map__rip_2objdump(map, sym->start),
481 map__rip_2objdump(map, sym->end),
482 filename, filename);
912 483
913 if (verbose >= 3) 484 if (verbose >= 3)
914 printf("doing: %s\n", command); 485 printf("doing: %s\n", command);
@@ -918,159 +489,88 @@ static void annotate_sym(struct dso *dso, struct symbol *sym)
918 return; 489 return;
919 490
920 while (!feof(file)) { 491 while (!feof(file)) {
921 if (parse_line(file, sym, start, len) < 0) 492 if (parse_line(file, he, &head) < 0)
922 break; 493 break;
923 } 494 }
924 495
925 pclose(file); 496 pclose(file);
926 if (print_line)
927 free_source_line(sym, len);
928}
929 497
930static void find_annotations(void) 498 if (verbose)
931{ 499 hist_entry__print_hits(he);
932 struct rb_node *nd;
933 struct dso *dso;
934 int count = 0;
935
936 list_for_each_entry(dso, &dsos, node) {
937
938 for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) {
939 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
940 500
941 if (sym->hist) { 501 list_for_each_entry_safe(pos, n, &head, node) {
942 annotate_sym(dso, sym); 502 objdump_line__print(pos, &head, he, len);
943 count++; 503 list_del(&pos->node);
944 } 504 objdump_line__free(pos);
945 }
946 } 505 }
947 506
948 if (!count) 507 if (print_line)
949 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter); 508 free_source_line(he, len);
950} 509}
951 510
952static int __cmd_annotate(void) 511static void perf_session__find_annotations(struct perf_session *self)
953{ 512{
954 int ret, rc = EXIT_FAILURE; 513 struct rb_node *nd;
955 unsigned long offset = 0;
956 unsigned long head = 0;
957 struct stat input_stat;
958 event_t *event;
959 uint32_t size;
960 char *buf;
961
962 register_idle_thread(&threads, &last_match);
963
964 input = open(input_name, O_RDONLY);
965 if (input < 0) {
966 perror("failed to open file");
967 exit(-1);
968 }
969
970 ret = fstat(input, &input_stat);
971 if (ret < 0) {
972 perror("failed to stat file");
973 exit(-1);
974 }
975
976 if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
977 fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
978 exit(-1);
979 }
980
981 if (!input_stat.st_size) {
982 fprintf(stderr, "zero-sized file, nothing to do!\n");
983 exit(0);
984 }
985
986 if (load_kernel() < 0) {
987 perror("failed to load kernel symbols");
988 return EXIT_FAILURE;
989 }
990
991remap:
992 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
993 MAP_SHARED, input, offset);
994 if (buf == MAP_FAILED) {
995 perror("failed to mmap file");
996 exit(-1);
997 }
998
999more:
1000 event = (event_t *)(buf + head);
1001
1002 size = event->header.size;
1003 if (!size)
1004 size = 8;
1005
1006 if (head + event->header.size >= page_size * mmap_window) {
1007 unsigned long shift = page_size * (head / page_size);
1008 int munmap_ret;
1009
1010 munmap_ret = munmap(buf, page_size * mmap_window);
1011 assert(munmap_ret == 0);
1012
1013 offset += shift;
1014 head -= shift;
1015 goto remap;
1016 }
1017
1018 size = event->header.size;
1019
1020 dump_printf("%p [%p]: event: %d\n",
1021 (void *)(offset + head),
1022 (void *)(long)event->header.size,
1023 event->header.type);
1024 514
1025 if (!size || process_event(event, offset, head) < 0) { 515 for (nd = rb_first(&self->hists); nd; nd = rb_next(nd)) {
516 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
517 struct sym_priv *priv;
1026 518
1027 dump_printf("%p [%p]: skipping unknown header type: %d\n", 519 if (he->sym == NULL)
1028 (void *)(offset + head), 520 continue;
1029 (void *)(long)(event->header.size),
1030 event->header.type);
1031 521
1032 total_unknown++; 522 priv = symbol__priv(he->sym);
523 if (priv->hist == NULL)
524 continue;
1033 525
526 annotate_sym(he);
1034 /* 527 /*
1035 * assume we lost track of the stream, check alignment, and 528 * Since we have a hist_entry per IP for the same symbol, free
1036 * increment a single u64 in the hope to catch on again 'soon'. 529 * he->sym->hist to signal we already processed this symbol.
1037 */ 530 */
1038 531 free(priv->hist);
1039 if (unlikely(head & 7)) 532 priv->hist = NULL;
1040 head &= ~7ULL;
1041
1042 size = 8;
1043 } 533 }
534}
1044 535
1045 head += size; 536static struct perf_event_ops event_ops = {
537 .sample = process_sample_event,
538 .mmap = event__process_mmap,
539 .comm = event__process_comm,
540 .fork = event__process_task,
541};
1046 542
1047 if (offset + head < (unsigned long)input_stat.st_size) 543static int __cmd_annotate(void)
1048 goto more; 544{
545 int ret;
546 struct perf_session *session;
1049 547
1050 rc = EXIT_SUCCESS; 548 session = perf_session__new(input_name, O_RDONLY, force);
1051 close(input); 549 if (session == NULL)
550 return -ENOMEM;
1052 551
1053 dump_printf(" IP events: %10ld\n", total); 552 ret = perf_session__process_events(session, &event_ops);
1054 dump_printf(" mmap events: %10ld\n", total_mmap); 553 if (ret)
1055 dump_printf(" comm events: %10ld\n", total_comm); 554 goto out_delete;
1056 dump_printf(" fork events: %10ld\n", total_fork);
1057 dump_printf(" unknown events: %10ld\n", total_unknown);
1058 555
1059 if (dump_trace) 556 if (dump_trace) {
1060 return 0; 557 event__print_totals();
558 goto out_delete;
559 }
1061 560
1062 if (verbose >= 3) 561 if (verbose > 3)
1063 threads__fprintf(stdout, &threads); 562 perf_session__fprintf(session, stdout);
1064 563
1065 if (verbose >= 2) 564 if (verbose > 2)
1066 dsos__fprintf(stdout); 565 dsos__fprintf(stdout);
1067 566
1068 collapse__resort(); 567 perf_session__collapse_resort(&session->hists);
1069 output__resort(); 568 perf_session__output_resort(&session->hists, session->event_total[0]);
569 perf_session__find_annotations(session);
570out_delete:
571 perf_session__delete(session);
1070 572
1071 find_annotations(); 573 return ret;
1072
1073 return rc;
1074} 574}
1075 575
1076static const char * const annotate_usage[] = { 576static const char * const annotate_usage[] = {
@@ -1088,8 +588,9 @@ static const struct option options[] = {
1088 "be more verbose (show symbol address, etc)"), 588 "be more verbose (show symbol address, etc)"),
1089 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, 589 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1090 "dump raw trace in ASCII"), 590 "dump raw trace in ASCII"),
1091 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"), 591 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1092 OPT_BOOLEAN('m', "modules", &modules, 592 "file", "vmlinux pathname"),
593 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
1093 "load module symbols - WARNING: use only with -k and LIVE kernel"), 594 "load module symbols - WARNING: use only with -k and LIVE kernel"),
1094 OPT_BOOLEAN('l', "print-line", &print_line, 595 OPT_BOOLEAN('l', "print-line", &print_line,
1095 "print matching source lines (may be slow)"), 596 "print matching source lines (may be slow)"),
@@ -1098,30 +599,17 @@ static const struct option options[] = {
1098 OPT_END() 599 OPT_END()
1099}; 600};
1100 601
1101static void setup_sorting(void)
1102{
1103 char *tmp, *tok, *str = strdup(sort_order);
1104
1105 for (tok = strtok_r(str, ", ", &tmp);
1106 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1107 if (sort_dimension__add(tok) < 0) {
1108 error("Unknown --sort key: `%s'", tok);
1109 usage_with_options(annotate_usage, options);
1110 }
1111 }
1112
1113 free(str);
1114}
1115
1116int cmd_annotate(int argc, const char **argv, const char *prefix __used) 602int cmd_annotate(int argc, const char **argv, const char *prefix __used)
1117{ 603{
1118 symbol__init(); 604 argc = parse_options(argc, argv, options, annotate_usage, 0);
1119 605
1120 page_size = getpagesize(); 606 symbol_conf.priv_size = sizeof(struct sym_priv);
607 symbol_conf.try_vmlinux_path = true;
1121 608
1122 argc = parse_options(argc, argv, options, annotate_usage, 0); 609 if (symbol__init() < 0)
610 return -1;
1123 611
1124 setup_sorting(); 612 setup_sorting(annotate_usage, options);
1125 613
1126 if (argc) { 614 if (argc) {
1127 /* 615 /*
@@ -1134,10 +622,12 @@ int cmd_annotate(int argc, const char **argv, const char *prefix __used)
1134 sym_hist_filter = argv[0]; 622 sym_hist_filter = argv[0];
1135 } 623 }
1136 624
1137 if (!sym_hist_filter)
1138 usage_with_options(annotate_usage, options);
1139
1140 setup_pager(); 625 setup_pager();
1141 626
627 if (field_sep && *field_sep == '.') {
628 pr_err("'.' is the only non valid --field-separator argument\n");
629 return -1;
630 }
631
1142 return __cmd_annotate(); 632 return __cmd_annotate();
1143} 633}