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.c644
1 files changed, 150 insertions, 494 deletions
diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c
index 1ec741615814..77d50a6d6802 100644
--- a/tools/perf/builtin-annotate.c
+++ b/tools/perf/builtin-annotate.c
@@ -22,15 +22,13 @@
22#include "util/parse-options.h" 22#include "util/parse-options.h"
23#include "util/parse-events.h" 23#include "util/parse-events.h"
24#include "util/thread.h" 24#include "util/thread.h"
25#include "util/sort.h"
26#include "util/hist.h"
25 27
26static char const *input_name = "perf.data"; 28static char const *input_name = "perf.data";
27 29
28static char default_sort_order[] = "comm,symbol";
29static char *sort_order = default_sort_order;
30
31static int force; 30static int force;
32static int input; 31static int input;
33static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
34 32
35static int full_paths; 33static int full_paths;
36 34
@@ -39,9 +37,10 @@ static int print_line;
39static unsigned long page_size; 37static unsigned long page_size;
40static unsigned long mmap_window = 32; 38static unsigned long mmap_window = 32;
41 39
42static struct rb_root threads; 40struct sym_hist {
43static struct thread *last_match; 41 u64 sum;
44 42 u64 ip[0];
43};
45 44
46struct sym_ext { 45struct sym_ext {
47 struct rb_node node; 46 struct rb_node node;
@@ -49,247 +48,33 @@ struct sym_ext {
49 char *path; 48 char *path;
50}; 49};
51 50
52/* 51struct sym_priv {
53 * histogram, sorted on item, collects counts 52 struct sym_hist *hist;
54 */ 53 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};
226
227static struct sort_dimension sort_dimensions[] = {
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}; 54};
233 55
234static LIST_HEAD(hist_entry__sort_list); 56static const char *sym_hist_filter;
235 57
236static int sort_dimension__add(char *tok) 58static int symbol_filter(struct map *map __used, struct symbol *sym)
237{ 59{
238 unsigned int i; 60 if (sym_hist_filter == NULL ||
239 61 strcmp(sym->name, sym_hist_filter) == 0) {
240 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) { 62 struct sym_priv *priv = symbol__priv(sym);
241 struct sort_dimension *sd = &sort_dimensions[i]; 63 const int size = (sizeof(*priv->hist) +
242 64 (sym->end - sym->start) * sizeof(u64));
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 65
66 priv->hist = malloc(size);
67 if (priv->hist)
68 memset(priv->hist, 0, size);
255 return 0; 69 return 0;
256 } 70 }
257 71 /*
258 return -ESRCH; 72 * FIXME: We should really filter it out, as we don't want to go thru symbols
259} 73 * we're not interested, and if a DSO ends up with no symbols, delete it too,
260 74 * but right now the kernel loading routines in symbol.c bail out if no symbols
261static int64_t 75 * are found, fix it later.
262hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) 76 */
263{ 77 return 0;
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
276static int64_t
277hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
278{
279 struct sort_entry *se;
280 int64_t cmp = 0;
281
282 list_for_each_entry(se, &hist_entry__sort_list, list) {
283 int64_t (*f)(struct hist_entry *, struct hist_entry *);
284
285 f = se->collapse ?: se->cmp;
286
287 cmp = f(left, right);
288 if (cmp)
289 break;
290 }
291
292 return cmp;
293} 78}
294 79
295/* 80/*
@@ -299,196 +84,60 @@ static void hist_hit(struct hist_entry *he, u64 ip)
299{ 84{
300 unsigned int sym_size, offset; 85 unsigned int sym_size, offset;
301 struct symbol *sym = he->sym; 86 struct symbol *sym = he->sym;
87 struct sym_priv *priv;
88 struct sym_hist *h;
302 89
303 he->count++; 90 he->count++;
304 91
305 if (!sym || !sym->hist) 92 if (!sym || !he->map)
93 return;
94
95 priv = symbol__priv(sym);
96 if (!priv->hist)
306 return; 97 return;
307 98
308 sym_size = sym->end - sym->start; 99 sym_size = sym->end - sym->start;
309 offset = ip - sym->start; 100 offset = ip - sym->start;
310 101
102 if (verbose)
103 fprintf(stderr, "%s: ip=%Lx\n", __func__,
104 he->map->unmap_ip(he->map, ip));
105
311 if (offset >= sym_size) 106 if (offset >= sym_size)
312 return; 107 return;
313 108
314 sym->hist_sum++; 109 h = priv->hist;
315 sym->hist[offset]++; 110 h->sum++;
111 h->ip[offset]++;
316 112
317 if (verbose >= 3) 113 if (verbose >= 3)
318 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n", 114 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
319 (void *)(unsigned long)he->sym->start, 115 (void *)(unsigned long)he->sym->start,
320 he->sym->name, 116 he->sym->name,
321 (void *)(unsigned long)ip, ip - he->sym->start, 117 (void *)(unsigned long)ip, ip - he->sym->start,
322 sym->hist[offset]); 118 h->ip[offset]);
323} 119}
324 120
325static int 121static int hist_entry__add(struct thread *thread, struct map *map,
326hist_entry__add(struct thread *thread, struct map *map, struct dso *dso, 122 struct symbol *sym, u64 ip, u64 count, char level)
327 struct symbol *sym, u64 ip, char level)
328{ 123{
329 struct rb_node **p = &hist.rb_node; 124 bool hit;
330 struct rb_node *parent = NULL; 125 struct hist_entry *he = __hist_entry__add(thread, map, sym, NULL, ip,
331 struct hist_entry *he; 126 count, level, &hit);
332 struct hist_entry entry = { 127 if (he == NULL)
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
352 return 0;
353 }
354
355 if (cmp < 0)
356 p = &(*p)->rb_left;
357 else
358 p = &(*p)->rb_right;
359 }
360
361 he = malloc(sizeof(*he));
362 if (!he)
363 return -ENOMEM; 128 return -ENOMEM;
364 *he = entry; 129 hist_hit(he, ip);
365 rb_link_node(&he->rb_node, parent, p);
366 rb_insert_color(&he->rb_node, &hist);
367
368 return 0; 130 return 0;
369} 131}
370 132
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
451 rb_link_node(&he->rb_node, parent, p);
452 rb_insert_color(&he->rb_node, &output_hists);
453}
454
455static void output__resort(void)
456{
457 struct rb_node *next;
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
475static unsigned long total = 0,
476 total_mmap = 0,
477 total_comm = 0,
478 total_fork = 0,
479 total_unknown = 0;
480
481static int 133static int
482process_sample_event(event_t *event, unsigned long offset, unsigned long head) 134process_sample_event(event_t *event, unsigned long offset, unsigned long head)
483{ 135{
484 char level; 136 char level;
485 int show = 0;
486 struct dso *dso = NULL;
487 struct thread *thread;
488 u64 ip = event->ip.ip; 137 u64 ip = event->ip.ip;
489 struct map *map = NULL; 138 struct map *map = NULL;
490 139 struct symbol *sym = NULL;
491 thread = threads__findnew(event->ip.pid, &threads, &last_match); 140 struct thread *thread = threads__findnew(event->ip.pid);
492 141
493 dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n", 142 dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
494 (void *)(offset + head), 143 (void *)(offset + head),
@@ -497,60 +146,53 @@ process_sample_event(event_t *event, unsigned long offset, unsigned long head)
497 event->ip.pid, 146 event->ip.pid,
498 (void *)(long)ip); 147 (void *)(long)ip);
499 148
500 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
501
502 if (thread == NULL) { 149 if (thread == NULL) {
503 fprintf(stderr, "problem processing %d event, skipping it.\n", 150 fprintf(stderr, "problem processing %d event, skipping it.\n",
504 event->header.type); 151 event->header.type);
505 return -1; 152 return -1;
506 } 153 }
507 154
155 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
156
508 if (event->header.misc & PERF_RECORD_MISC_KERNEL) { 157 if (event->header.misc & PERF_RECORD_MISC_KERNEL) {
509 show = SHOW_KERNEL;
510 level = 'k'; 158 level = 'k';
511 159 sym = kernel_maps__find_symbol(ip, &map);
512 dso = kernel_dso; 160 dump_printf(" ...... dso: %s\n",
513 161 map ? map->dso->long_name : "<not found>");
514 dump_printf(" ...... dso: %s\n", dso->name);
515
516 } else if (event->header.misc & PERF_RECORD_MISC_USER) { 162 } else if (event->header.misc & PERF_RECORD_MISC_USER) {
517
518 show = SHOW_USER;
519 level = '.'; 163 level = '.';
520
521 map = thread__find_map(thread, ip); 164 map = thread__find_map(thread, ip);
522 if (map != NULL) { 165 if (map != NULL) {
166got_map:
523 ip = map->map_ip(map, ip); 167 ip = map->map_ip(map, ip);
524 dso = map->dso; 168 sym = map__find_symbol(map, ip, symbol_filter);
525 } else { 169 } else {
526 /* 170 /*
527 * If this is outside of all known maps, 171 * If this is outside of all known maps,
528 * and is a negative address, try to look it 172 * and is a negative address, try to look it
529 * up in the kernel dso, as it might be a 173 * up in the kernel dso, as it might be a
530 * vsyscall (which executes in user-mode): 174 * vsyscall or vdso (which executes in user-mode).
175 *
176 * XXX This is nasty, we should have a symbol list in
177 * the "[vdso]" dso, but for now lets use the old
178 * trick of looking in the whole kernel symbol list.
531 */ 179 */
532 if ((long long)ip < 0) 180 if ((long long)ip < 0) {
533 dso = kernel_dso; 181 map = kernel_map;
182 goto got_map;
183 }
534 } 184 }
535 dump_printf(" ...... dso: %s\n", dso ? dso->name : "<not found>"); 185 dump_printf(" ...... dso: %s\n",
536 186 map ? map->dso->long_name : "<not found>");
537 } else { 187 } else {
538 show = SHOW_HV;
539 level = 'H'; 188 level = 'H';
540 dump_printf(" ...... dso: [hypervisor]\n"); 189 dump_printf(" ...... dso: [hypervisor]\n");
541 } 190 }
542 191
543 if (show & show_mask) { 192 if (hist_entry__add(thread, map, sym, ip, 1, level)) {
544 struct symbol *sym = NULL; 193 fprintf(stderr, "problem incrementing symbol count, "
545 194 "skipping event\n");
546 if (dso) 195 return -1;
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 } 196 }
555 total++; 197 total++;
556 198
@@ -560,10 +202,8 @@ process_sample_event(event_t *event, unsigned long offset, unsigned long head)
560static int 202static int
561process_mmap_event(event_t *event, unsigned long offset, unsigned long head) 203process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
562{ 204{
563 struct thread *thread;
564 struct map *map = map__new(&event->mmap, NULL, 0); 205 struct map *map = map__new(&event->mmap, NULL, 0);
565 206 struct thread *thread = threads__findnew(event->mmap.pid);
566 thread = threads__findnew(event->mmap.pid, &threads, &last_match);
567 207
568 dump_printf("%p [%p]: PERF_RECORD_MMAP %d: [%p(%p) @ %p]: %s\n", 208 dump_printf("%p [%p]: PERF_RECORD_MMAP %d: [%p(%p) @ %p]: %s\n",
569 (void *)(offset + head), 209 (void *)(offset + head),
@@ -588,9 +228,8 @@ process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
588static int 228static int
589process_comm_event(event_t *event, unsigned long offset, unsigned long head) 229process_comm_event(event_t *event, unsigned long offset, unsigned long head)
590{ 230{
591 struct thread *thread; 231 struct thread *thread = threads__findnew(event->comm.pid);
592 232
593 thread = threads__findnew(event->comm.pid, &threads, &last_match);
594 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n", 233 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
595 (void *)(offset + head), 234 (void *)(offset + head),
596 (void *)(long)(event->header.size), 235 (void *)(long)(event->header.size),
@@ -609,11 +248,9 @@ process_comm_event(event_t *event, unsigned long offset, unsigned long head)
609static int 248static int
610process_fork_event(event_t *event, unsigned long offset, unsigned long head) 249process_fork_event(event_t *event, unsigned long offset, unsigned long head)
611{ 250{
612 struct thread *thread; 251 struct thread *thread = threads__findnew(event->fork.pid);
613 struct thread *parent; 252 struct thread *parent = threads__findnew(event->fork.ppid);
614 253
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", 254 dump_printf("%p [%p]: PERF_RECORD_FORK: %d:%d\n",
618 (void *)(offset + head), 255 (void *)(offset + head),
619 (void *)(long)(event->header.size), 256 (void *)(long)(event->header.size),
@@ -665,14 +302,15 @@ process_event(event_t *event, unsigned long offset, unsigned long head)
665 return 0; 302 return 0;
666} 303}
667 304
668static int 305static int parse_line(FILE *file, struct hist_entry *he, u64 len)
669parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
670{ 306{
307 struct symbol *sym = he->sym;
671 char *line = NULL, *tmp, *tmp2; 308 char *line = NULL, *tmp, *tmp2;
672 static const char *prev_line; 309 static const char *prev_line;
673 static const char *prev_color; 310 static const char *prev_color;
674 unsigned int offset; 311 unsigned int offset;
675 size_t line_len; 312 size_t line_len;
313 u64 start;
676 s64 line_ip; 314 s64 line_ip;
677 int ret; 315 int ret;
678 char *c; 316 char *c;
@@ -709,22 +347,26 @@ parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
709 line_ip = -1; 347 line_ip = -1;
710 } 348 }
711 349
350 start = he->map->unmap_ip(he->map, sym->start);
351
712 if (line_ip != -1) { 352 if (line_ip != -1) {
713 const char *path = NULL; 353 const char *path = NULL;
714 unsigned int hits = 0; 354 unsigned int hits = 0;
715 double percent = 0.0; 355 double percent = 0.0;
716 const char *color; 356 const char *color;
717 struct sym_ext *sym_ext = sym->priv; 357 struct sym_priv *priv = symbol__priv(sym);
358 struct sym_ext *sym_ext = priv->ext;
359 struct sym_hist *h = priv->hist;
718 360
719 offset = line_ip - start; 361 offset = line_ip - start;
720 if (offset < len) 362 if (offset < len)
721 hits = sym->hist[offset]; 363 hits = h->ip[offset];
722 364
723 if (offset < len && sym_ext) { 365 if (offset < len && sym_ext) {
724 path = sym_ext[offset].path; 366 path = sym_ext[offset].path;
725 percent = sym_ext[offset].percent; 367 percent = sym_ext[offset].percent;
726 } else if (sym->hist_sum) 368 } else if (h->sum)
727 percent = 100.0 * hits / sym->hist_sum; 369 percent = 100.0 * hits / h->sum;
728 370
729 color = get_percent_color(percent); 371 color = get_percent_color(percent);
730 372
@@ -777,9 +419,10 @@ static void insert_source_line(struct sym_ext *sym_ext)
777 rb_insert_color(&sym_ext->node, &root_sym_ext); 419 rb_insert_color(&sym_ext->node, &root_sym_ext);
778} 420}
779 421
780static void free_source_line(struct symbol *sym, int len) 422static void free_source_line(struct hist_entry *he, int len)
781{ 423{
782 struct sym_ext *sym_ext = sym->priv; 424 struct sym_priv *priv = symbol__priv(he->sym);
425 struct sym_ext *sym_ext = priv->ext;
783 int i; 426 int i;
784 427
785 if (!sym_ext) 428 if (!sym_ext)
@@ -789,26 +432,30 @@ static void free_source_line(struct symbol *sym, int len)
789 free(sym_ext[i].path); 432 free(sym_ext[i].path);
790 free(sym_ext); 433 free(sym_ext);
791 434
792 sym->priv = NULL; 435 priv->ext = NULL;
793 root_sym_ext = RB_ROOT; 436 root_sym_ext = RB_ROOT;
794} 437}
795 438
796/* Get the filename:line for the colored entries */ 439/* Get the filename:line for the colored entries */
797static void 440static void
798get_source_line(struct symbol *sym, u64 start, int len, const char *filename) 441get_source_line(struct hist_entry *he, int len, const char *filename)
799{ 442{
443 struct symbol *sym = he->sym;
444 u64 start;
800 int i; 445 int i;
801 char cmd[PATH_MAX * 2]; 446 char cmd[PATH_MAX * 2];
802 struct sym_ext *sym_ext; 447 struct sym_ext *sym_ext;
448 struct sym_priv *priv = symbol__priv(sym);
449 struct sym_hist *h = priv->hist;
803 450
804 if (!sym->hist_sum) 451 if (!h->sum)
805 return; 452 return;
806 453
807 sym->priv = calloc(len, sizeof(struct sym_ext)); 454 sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
808 if (!sym->priv) 455 if (!priv->ext)
809 return; 456 return;
810 457
811 sym_ext = sym->priv; 458 start = he->map->unmap_ip(he->map, sym->start);
812 459
813 for (i = 0; i < len; i++) { 460 for (i = 0; i < len; i++) {
814 char *path = NULL; 461 char *path = NULL;
@@ -816,7 +463,7 @@ get_source_line(struct symbol *sym, u64 start, int len, const char *filename)
816 u64 offset; 463 u64 offset;
817 FILE *fp; 464 FILE *fp;
818 465
819 sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum; 466 sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
820 if (sym_ext[i].percent <= 0.5) 467 if (sym_ext[i].percent <= 0.5)
821 continue; 468 continue;
822 469
@@ -870,33 +517,34 @@ static void print_summary(const char *filename)
870 } 517 }
871} 518}
872 519
873static void annotate_sym(struct dso *dso, struct symbol *sym) 520static void annotate_sym(struct hist_entry *he)
874{ 521{
875 const char *filename = dso->name, *d_filename; 522 struct map *map = he->map;
876 u64 start, end, len; 523 struct dso *dso = map->dso;
524 struct symbol *sym = he->sym;
525 const char *filename = dso->long_name, *d_filename;
526 u64 len;
877 char command[PATH_MAX*2]; 527 char command[PATH_MAX*2];
878 FILE *file; 528 FILE *file;
879 529
880 if (!filename) 530 if (!filename)
881 return; 531 return;
882 if (sym->module) 532
883 filename = sym->module->path; 533 if (verbose)
884 else if (dso == kernel_dso) 534 fprintf(stderr, "%s: filename=%s, sym=%s, start=%Lx, end=%Lx\n",
885 filename = vmlinux_name; 535 __func__, filename, sym->name,
886 536 map->unmap_ip(map, sym->start),
887 start = sym->obj_start; 537 map->unmap_ip(map, sym->end));
888 if (!start) 538
889 start = sym->start;
890 if (full_paths) 539 if (full_paths)
891 d_filename = filename; 540 d_filename = filename;
892 else 541 else
893 d_filename = basename(filename); 542 d_filename = basename(filename);
894 543
895 end = start + sym->end - sym->start + 1;
896 len = sym->end - sym->start; 544 len = sym->end - sym->start;
897 545
898 if (print_line) { 546 if (print_line) {
899 get_source_line(sym, start, len, filename); 547 get_source_line(he, len, filename);
900 print_summary(filename); 548 print_summary(filename);
901 } 549 }
902 550
@@ -905,10 +553,12 @@ static void annotate_sym(struct dso *dso, struct symbol *sym)
905 printf("------------------------------------------------\n"); 553 printf("------------------------------------------------\n");
906 554
907 if (verbose >= 2) 555 if (verbose >= 2)
908 printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name); 556 printf("annotating [%p] %30s : [%p] %30s\n",
557 dso, dso->long_name, sym, sym->name);
909 558
910 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s", 559 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
911 (u64)start, (u64)end, filename, filename); 560 map->unmap_ip(map, sym->start), map->unmap_ip(map, sym->end),
561 filename, filename);
912 562
913 if (verbose >= 3) 563 if (verbose >= 3)
914 printf("doing: %s\n", command); 564 printf("doing: %s\n", command);
@@ -918,35 +568,38 @@ static void annotate_sym(struct dso *dso, struct symbol *sym)
918 return; 568 return;
919 569
920 while (!feof(file)) { 570 while (!feof(file)) {
921 if (parse_line(file, sym, start, len) < 0) 571 if (parse_line(file, he, len) < 0)
922 break; 572 break;
923 } 573 }
924 574
925 pclose(file); 575 pclose(file);
926 if (print_line) 576 if (print_line)
927 free_source_line(sym, len); 577 free_source_line(he, len);
928} 578}
929 579
930static void find_annotations(void) 580static void find_annotations(void)
931{ 581{
932 struct rb_node *nd; 582 struct rb_node *nd;
933 struct dso *dso;
934 int count = 0;
935 583
936 list_for_each_entry(dso, &dsos, node) { 584 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
585 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
586 struct sym_priv *priv;
937 587
938 for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) { 588 if (he->sym == NULL)
939 struct symbol *sym = rb_entry(nd, struct symbol, rb_node); 589 continue;
940 590
941 if (sym->hist) { 591 priv = symbol__priv(he->sym);
942 annotate_sym(dso, sym); 592 if (priv->hist == NULL)
943 count++; 593 continue;
944 }
945 }
946 }
947 594
948 if (!count) 595 annotate_sym(he);
949 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter); 596 /*
597 * Since we have a hist_entry per IP for the same symbol, free
598 * he->sym->hist to signal we already processed this symbol.
599 */
600 free(priv->hist);
601 priv->hist = NULL;
602 }
950} 603}
951 604
952static int __cmd_annotate(void) 605static int __cmd_annotate(void)
@@ -959,7 +612,7 @@ static int __cmd_annotate(void)
959 uint32_t size; 612 uint32_t size;
960 char *buf; 613 char *buf;
961 614
962 register_idle_thread(&threads, &last_match); 615 register_idle_thread();
963 616
964 input = open(input_name, O_RDONLY); 617 input = open(input_name, O_RDONLY);
965 if (input < 0) { 618 if (input < 0) {
@@ -983,7 +636,7 @@ static int __cmd_annotate(void)
983 exit(0); 636 exit(0);
984 } 637 }
985 638
986 if (load_kernel() < 0) { 639 if (load_kernel(symbol_filter) < 0) {
987 perror("failed to load kernel symbols"); 640 perror("failed to load kernel symbols");
988 return EXIT_FAILURE; 641 return EXIT_FAILURE;
989 } 642 }
@@ -1059,14 +712,14 @@ more:
1059 if (dump_trace) 712 if (dump_trace)
1060 return 0; 713 return 0;
1061 714
1062 if (verbose >= 3) 715 if (verbose > 3)
1063 threads__fprintf(stdout, &threads); 716 threads__fprintf(stdout);
1064 717
1065 if (verbose >= 2) 718 if (verbose > 2)
1066 dsos__fprintf(stdout); 719 dsos__fprintf(stdout);
1067 720
1068 collapse__resort(); 721 collapse__resort();
1069 output__resort(); 722 output__resort(total);
1070 723
1071 find_annotations(); 724 find_annotations();
1072 725
@@ -1115,7 +768,7 @@ static void setup_sorting(void)
1115 768
1116int cmd_annotate(int argc, const char **argv, const char *prefix __used) 769int cmd_annotate(int argc, const char **argv, const char *prefix __used)
1117{ 770{
1118 symbol__init(); 771 symbol__init(sizeof(struct sym_priv));
1119 772
1120 page_size = getpagesize(); 773 page_size = getpagesize();
1121 774
@@ -1134,10 +787,13 @@ int cmd_annotate(int argc, const char **argv, const char *prefix __used)
1134 sym_hist_filter = argv[0]; 787 sym_hist_filter = argv[0];
1135 } 788 }
1136 789
1137 if (!sym_hist_filter)
1138 usage_with_options(annotate_usage, options);
1139
1140 setup_pager(); 790 setup_pager();
1141 791
792 if (field_sep && *field_sep == '.') {
793 fputs("'.' is the only non valid --field-separator argument\n",
794 stderr);
795 exit(129);
796 }
797
1142 return __cmd_annotate(); 798 return __cmd_annotate();
1143} 799}