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.c579
1 files changed, 100 insertions, 479 deletions
diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c
index 7e58e3ad1508..1ec741615814 100644
--- a/tools/perf/builtin-annotate.c
+++ b/tools/perf/builtin-annotate.c
@@ -10,85 +10,37 @@
10#include "util/util.h" 10#include "util/util.h"
11 11
12#include "util/color.h" 12#include "util/color.h"
13#include "util/list.h" 13#include <linux/list.h>
14#include "util/cache.h" 14#include "util/cache.h"
15#include "util/rbtree.h" 15#include <linux/rbtree.h>
16#include "util/symbol.h" 16#include "util/symbol.h"
17#include "util/string.h" 17#include "util/string.h"
18 18
19#include "perf.h" 19#include "perf.h"
20#include "util/debug.h"
20 21
21#include "util/parse-options.h" 22#include "util/parse-options.h"
22#include "util/parse-events.h" 23#include "util/parse-events.h"
23 24#include "util/thread.h"
24#define SHOW_KERNEL 1
25#define SHOW_USER 2
26#define SHOW_HV 4
27
28#define MIN_GREEN 0.5
29#define MIN_RED 5.0
30
31 25
32static char const *input_name = "perf.data"; 26static char const *input_name = "perf.data";
33static char *vmlinux = "vmlinux";
34 27
35static char default_sort_order[] = "comm,symbol"; 28static char default_sort_order[] = "comm,symbol";
36static char *sort_order = default_sort_order; 29static char *sort_order = default_sort_order;
37 30
31static int force;
38static int input; 32static int input;
39static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV; 33static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
40 34
41static int dump_trace = 0; 35static int full_paths;
42#define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
43
44static int verbose;
45 36
46static int print_line; 37static int print_line;
47 38
48static unsigned long page_size; 39static unsigned long page_size;
49static unsigned long mmap_window = 32; 40static unsigned long mmap_window = 32;
50 41
51struct ip_event { 42static struct rb_root threads;
52 struct perf_event_header header; 43static struct thread *last_match;
53 u64 ip;
54 u32 pid, tid;
55};
56
57struct mmap_event {
58 struct perf_event_header header;
59 u32 pid, tid;
60 u64 start;
61 u64 len;
62 u64 pgoff;
63 char filename[PATH_MAX];
64};
65
66struct comm_event {
67 struct perf_event_header header;
68 u32 pid, tid;
69 char comm[16];
70};
71
72struct fork_event {
73 struct perf_event_header header;
74 u32 pid, ppid;
75};
76
77struct period_event {
78 struct perf_event_header header;
79 u64 time;
80 u64 id;
81 u64 sample_period;
82};
83
84typedef union event_union {
85 struct perf_event_header header;
86 struct ip_event ip;
87 struct mmap_event mmap;
88 struct comm_event comm;
89 struct fork_event fork;
90 struct period_event period;
91} event_t;
92 44
93 45
94struct sym_ext { 46struct sym_ext {
@@ -97,323 +49,6 @@ struct sym_ext {
97 char *path; 49 char *path;
98}; 50};
99 51
100static LIST_HEAD(dsos);
101static struct dso *kernel_dso;
102static struct dso *vdso;
103
104
105static void dsos__add(struct dso *dso)
106{
107 list_add_tail(&dso->node, &dsos);
108}
109
110static struct dso *dsos__find(const char *name)
111{
112 struct dso *pos;
113
114 list_for_each_entry(pos, &dsos, node)
115 if (strcmp(pos->name, name) == 0)
116 return pos;
117 return NULL;
118}
119
120static struct dso *dsos__findnew(const char *name)
121{
122 struct dso *dso = dsos__find(name);
123 int nr;
124
125 if (dso)
126 return dso;
127
128 dso = dso__new(name, 0);
129 if (!dso)
130 goto out_delete_dso;
131
132 nr = dso__load(dso, NULL, verbose);
133 if (nr < 0) {
134 if (verbose)
135 fprintf(stderr, "Failed to open: %s\n", name);
136 goto out_delete_dso;
137 }
138 if (!nr && verbose) {
139 fprintf(stderr,
140 "No symbols found in: %s, maybe install a debug package?\n",
141 name);
142 }
143
144 dsos__add(dso);
145
146 return dso;
147
148out_delete_dso:
149 dso__delete(dso);
150 return NULL;
151}
152
153static void dsos__fprintf(FILE *fp)
154{
155 struct dso *pos;
156
157 list_for_each_entry(pos, &dsos, node)
158 dso__fprintf(pos, fp);
159}
160
161static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
162{
163 return dso__find_symbol(kernel_dso, ip);
164}
165
166static int load_kernel(void)
167{
168 int err;
169
170 kernel_dso = dso__new("[kernel]", 0);
171 if (!kernel_dso)
172 return -1;
173
174 err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
175 if (err) {
176 dso__delete(kernel_dso);
177 kernel_dso = NULL;
178 } else
179 dsos__add(kernel_dso);
180
181 vdso = dso__new("[vdso]", 0);
182 if (!vdso)
183 return -1;
184
185 vdso->find_symbol = vdso__find_symbol;
186
187 dsos__add(vdso);
188
189 return err;
190}
191
192struct map {
193 struct list_head node;
194 u64 start;
195 u64 end;
196 u64 pgoff;
197 u64 (*map_ip)(struct map *, u64);
198 struct dso *dso;
199};
200
201static u64 map__map_ip(struct map *map, u64 ip)
202{
203 return ip - map->start + map->pgoff;
204}
205
206static u64 vdso__map_ip(struct map *map, u64 ip)
207{
208 return ip;
209}
210
211static struct map *map__new(struct mmap_event *event)
212{
213 struct map *self = malloc(sizeof(*self));
214
215 if (self != NULL) {
216 const char *filename = event->filename;
217
218 self->start = event->start;
219 self->end = event->start + event->len;
220 self->pgoff = event->pgoff;
221
222 self->dso = dsos__findnew(filename);
223 if (self->dso == NULL)
224 goto out_delete;
225
226 if (self->dso == vdso)
227 self->map_ip = vdso__map_ip;
228 else
229 self->map_ip = map__map_ip;
230 }
231 return self;
232out_delete:
233 free(self);
234 return NULL;
235}
236
237static struct map *map__clone(struct map *self)
238{
239 struct map *map = malloc(sizeof(*self));
240
241 if (!map)
242 return NULL;
243
244 memcpy(map, self, sizeof(*self));
245
246 return map;
247}
248
249static int map__overlap(struct map *l, struct map *r)
250{
251 if (l->start > r->start) {
252 struct map *t = l;
253 l = r;
254 r = t;
255 }
256
257 if (l->end > r->start)
258 return 1;
259
260 return 0;
261}
262
263static size_t map__fprintf(struct map *self, FILE *fp)
264{
265 return fprintf(fp, " %Lx-%Lx %Lx %s\n",
266 self->start, self->end, self->pgoff, self->dso->name);
267}
268
269
270struct thread {
271 struct rb_node rb_node;
272 struct list_head maps;
273 pid_t pid;
274 char *comm;
275};
276
277static struct thread *thread__new(pid_t pid)
278{
279 struct thread *self = malloc(sizeof(*self));
280
281 if (self != NULL) {
282 self->pid = pid;
283 self->comm = malloc(32);
284 if (self->comm)
285 snprintf(self->comm, 32, ":%d", self->pid);
286 INIT_LIST_HEAD(&self->maps);
287 }
288
289 return self;
290}
291
292static int thread__set_comm(struct thread *self, const char *comm)
293{
294 if (self->comm)
295 free(self->comm);
296 self->comm = strdup(comm);
297 return self->comm ? 0 : -ENOMEM;
298}
299
300static size_t thread__fprintf(struct thread *self, FILE *fp)
301{
302 struct map *pos;
303 size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
304
305 list_for_each_entry(pos, &self->maps, node)
306 ret += map__fprintf(pos, fp);
307
308 return ret;
309}
310
311
312static struct rb_root threads;
313static struct thread *last_match;
314
315static struct thread *threads__findnew(pid_t pid)
316{
317 struct rb_node **p = &threads.rb_node;
318 struct rb_node *parent = NULL;
319 struct thread *th;
320
321 /*
322 * Font-end cache - PID lookups come in blocks,
323 * so most of the time we dont have to look up
324 * the full rbtree:
325 */
326 if (last_match && last_match->pid == pid)
327 return last_match;
328
329 while (*p != NULL) {
330 parent = *p;
331 th = rb_entry(parent, struct thread, rb_node);
332
333 if (th->pid == pid) {
334 last_match = th;
335 return th;
336 }
337
338 if (pid < th->pid)
339 p = &(*p)->rb_left;
340 else
341 p = &(*p)->rb_right;
342 }
343
344 th = thread__new(pid);
345 if (th != NULL) {
346 rb_link_node(&th->rb_node, parent, p);
347 rb_insert_color(&th->rb_node, &threads);
348 last_match = th;
349 }
350
351 return th;
352}
353
354static void thread__insert_map(struct thread *self, struct map *map)
355{
356 struct map *pos, *tmp;
357
358 list_for_each_entry_safe(pos, tmp, &self->maps, node) {
359 if (map__overlap(pos, map)) {
360 list_del_init(&pos->node);
361 /* XXX leaks dsos */
362 free(pos);
363 }
364 }
365
366 list_add_tail(&map->node, &self->maps);
367}
368
369static int thread__fork(struct thread *self, struct thread *parent)
370{
371 struct map *map;
372
373 if (self->comm)
374 free(self->comm);
375 self->comm = strdup(parent->comm);
376 if (!self->comm)
377 return -ENOMEM;
378
379 list_for_each_entry(map, &parent->maps, node) {
380 struct map *new = map__clone(map);
381 if (!new)
382 return -ENOMEM;
383 thread__insert_map(self, new);
384 }
385
386 return 0;
387}
388
389static struct map *thread__find_map(struct thread *self, u64 ip)
390{
391 struct map *pos;
392
393 if (self == NULL)
394 return NULL;
395
396 list_for_each_entry(pos, &self->maps, node)
397 if (ip >= pos->start && ip <= pos->end)
398 return pos;
399
400 return NULL;
401}
402
403static size_t threads__fprintf(FILE *fp)
404{
405 size_t ret = 0;
406 struct rb_node *nd;
407
408 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
409 struct thread *pos = rb_entry(nd, struct thread, rb_node);
410
411 ret += thread__fprintf(pos, fp);
412 }
413
414 return ret;
415}
416
417/* 52/*
418 * histogram, sorted on item, collects counts 53 * histogram, sorted on item, collects counts
419 */ 54 */
@@ -440,7 +75,7 @@ struct hist_entry {
440struct sort_entry { 75struct sort_entry {
441 struct list_head list; 76 struct list_head list;
442 77
443 char *header; 78 const char *header;
444 79
445 int64_t (*cmp)(struct hist_entry *, struct hist_entry *); 80 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
446 int64_t (*collapse)(struct hist_entry *, struct hist_entry *); 81 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
@@ -584,7 +219,7 @@ static struct sort_entry sort_sym = {
584static int sort__need_collapse = 0; 219static int sort__need_collapse = 0;
585 220
586struct sort_dimension { 221struct sort_dimension {
587 char *name; 222 const char *name;
588 struct sort_entry *entry; 223 struct sort_entry *entry;
589 int taken; 224 int taken;
590}; 225};
@@ -600,7 +235,7 @@ static LIST_HEAD(hist_entry__sort_list);
600 235
601static int sort_dimension__add(char *tok) 236static int sort_dimension__add(char *tok)
602{ 237{
603 int i; 238 unsigned int i;
604 239
605 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) { 240 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
606 struct sort_dimension *sd = &sort_dimensions[i]; 241 struct sort_dimension *sd = &sort_dimensions[i];
@@ -837,17 +472,6 @@ static void output__resort(void)
837 } 472 }
838} 473}
839 474
840static void register_idle_thread(void)
841{
842 struct thread *thread = threads__findnew(0);
843
844 if (thread == NULL ||
845 thread__set_comm(thread, "[idle]")) {
846 fprintf(stderr, "problem inserting idle task.\n");
847 exit(-1);
848 }
849}
850
851static unsigned long total = 0, 475static unsigned long total = 0,
852 total_mmap = 0, 476 total_mmap = 0,
853 total_comm = 0, 477 total_comm = 0,
@@ -855,23 +479,25 @@ static unsigned long total = 0,
855 total_unknown = 0; 479 total_unknown = 0;
856 480
857static int 481static int
858process_overflow_event(event_t *event, unsigned long offset, unsigned long head) 482process_sample_event(event_t *event, unsigned long offset, unsigned long head)
859{ 483{
860 char level; 484 char level;
861 int show = 0; 485 int show = 0;
862 struct dso *dso = NULL; 486 struct dso *dso = NULL;
863 struct thread *thread = threads__findnew(event->ip.pid); 487 struct thread *thread;
864 u64 ip = event->ip.ip; 488 u64 ip = event->ip.ip;
865 struct map *map = NULL; 489 struct map *map = NULL;
866 490
867 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n", 491 thread = threads__findnew(event->ip.pid, &threads, &last_match);
492
493 dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
868 (void *)(offset + head), 494 (void *)(offset + head),
869 (void *)(long)(event->header.size), 495 (void *)(long)(event->header.size),
870 event->header.misc, 496 event->header.misc,
871 event->ip.pid, 497 event->ip.pid,
872 (void *)(long)ip); 498 (void *)(long)ip);
873 499
874 dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid); 500 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
875 501
876 if (thread == NULL) { 502 if (thread == NULL) {
877 fprintf(stderr, "problem processing %d event, skipping it.\n", 503 fprintf(stderr, "problem processing %d event, skipping it.\n",
@@ -879,15 +505,15 @@ process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
879 return -1; 505 return -1;
880 } 506 }
881 507
882 if (event->header.misc & PERF_EVENT_MISC_KERNEL) { 508 if (event->header.misc & PERF_RECORD_MISC_KERNEL) {
883 show = SHOW_KERNEL; 509 show = SHOW_KERNEL;
884 level = 'k'; 510 level = 'k';
885 511
886 dso = kernel_dso; 512 dso = kernel_dso;
887 513
888 dprintf(" ...... dso: %s\n", dso->name); 514 dump_printf(" ...... dso: %s\n", dso->name);
889 515
890 } else if (event->header.misc & PERF_EVENT_MISC_USER) { 516 } else if (event->header.misc & PERF_RECORD_MISC_USER) {
891 517
892 show = SHOW_USER; 518 show = SHOW_USER;
893 level = '.'; 519 level = '.';
@@ -906,12 +532,12 @@ process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
906 if ((long long)ip < 0) 532 if ((long long)ip < 0)
907 dso = kernel_dso; 533 dso = kernel_dso;
908 } 534 }
909 dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>"); 535 dump_printf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
910 536
911 } else { 537 } else {
912 show = SHOW_HV; 538 show = SHOW_HV;
913 level = 'H'; 539 level = 'H';
914 dprintf(" ...... dso: [hypervisor]\n"); 540 dump_printf(" ...... dso: [hypervisor]\n");
915 } 541 }
916 542
917 if (show & show_mask) { 543 if (show & show_mask) {
@@ -934,10 +560,12 @@ process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
934static int 560static int
935process_mmap_event(event_t *event, unsigned long offset, unsigned long head) 561process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
936{ 562{
937 struct thread *thread = threads__findnew(event->mmap.pid); 563 struct thread *thread;
938 struct map *map = map__new(&event->mmap); 564 struct map *map = map__new(&event->mmap, NULL, 0);
565
566 thread = threads__findnew(event->mmap.pid, &threads, &last_match);
939 567
940 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n", 568 dump_printf("%p [%p]: PERF_RECORD_MMAP %d: [%p(%p) @ %p]: %s\n",
941 (void *)(offset + head), 569 (void *)(offset + head),
942 (void *)(long)(event->header.size), 570 (void *)(long)(event->header.size),
943 event->mmap.pid, 571 event->mmap.pid,
@@ -947,7 +575,7 @@ process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
947 event->mmap.filename); 575 event->mmap.filename);
948 576
949 if (thread == NULL || map == NULL) { 577 if (thread == NULL || map == NULL) {
950 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n"); 578 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
951 return 0; 579 return 0;
952 } 580 }
953 581
@@ -960,16 +588,17 @@ process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
960static int 588static int
961process_comm_event(event_t *event, unsigned long offset, unsigned long head) 589process_comm_event(event_t *event, unsigned long offset, unsigned long head)
962{ 590{
963 struct thread *thread = threads__findnew(event->comm.pid); 591 struct thread *thread;
964 592
965 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n", 593 thread = threads__findnew(event->comm.pid, &threads, &last_match);
594 dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
966 (void *)(offset + head), 595 (void *)(offset + head),
967 (void *)(long)(event->header.size), 596 (void *)(long)(event->header.size),
968 event->comm.comm, event->comm.pid); 597 event->comm.comm, event->comm.pid);
969 598
970 if (thread == NULL || 599 if (thread == NULL ||
971 thread__set_comm(thread, event->comm.comm)) { 600 thread__set_comm(thread, event->comm.comm)) {
972 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n"); 601 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
973 return -1; 602 return -1;
974 } 603 }
975 total_comm++; 604 total_comm++;
@@ -980,16 +609,25 @@ process_comm_event(event_t *event, unsigned long offset, unsigned long head)
980static int 609static int
981process_fork_event(event_t *event, unsigned long offset, unsigned long head) 610process_fork_event(event_t *event, unsigned long offset, unsigned long head)
982{ 611{
983 struct thread *thread = threads__findnew(event->fork.pid); 612 struct thread *thread;
984 struct thread *parent = threads__findnew(event->fork.ppid); 613 struct thread *parent;
985 614
986 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n", 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",
987 (void *)(offset + head), 618 (void *)(offset + head),
988 (void *)(long)(event->header.size), 619 (void *)(long)(event->header.size),
989 event->fork.pid, event->fork.ppid); 620 event->fork.pid, event->fork.ppid);
990 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
991 if (!thread || !parent || thread__fork(thread, parent)) { 629 if (!thread || !parent || thread__fork(thread, parent)) {
992 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n"); 630 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
993 return -1; 631 return -1;
994 } 632 }
995 total_fork++; 633 total_fork++;
@@ -998,42 +636,26 @@ process_fork_event(event_t *event, unsigned long offset, unsigned long head)
998} 636}
999 637
1000static int 638static int
1001process_period_event(event_t *event, unsigned long offset, unsigned long head)
1002{
1003 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1004 (void *)(offset + head),
1005 (void *)(long)(event->header.size),
1006 event->period.time,
1007 event->period.id,
1008 event->period.sample_period);
1009
1010 return 0;
1011}
1012
1013static int
1014process_event(event_t *event, unsigned long offset, unsigned long head) 639process_event(event_t *event, unsigned long offset, unsigned long head)
1015{ 640{
1016 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
1017 return process_overflow_event(event, offset, head);
1018
1019 switch (event->header.type) { 641 switch (event->header.type) {
1020 case PERF_EVENT_MMAP: 642 case PERF_RECORD_SAMPLE:
643 return process_sample_event(event, offset, head);
644
645 case PERF_RECORD_MMAP:
1021 return process_mmap_event(event, offset, head); 646 return process_mmap_event(event, offset, head);
1022 647
1023 case PERF_EVENT_COMM: 648 case PERF_RECORD_COMM:
1024 return process_comm_event(event, offset, head); 649 return process_comm_event(event, offset, head);
1025 650
1026 case PERF_EVENT_FORK: 651 case PERF_RECORD_FORK:
1027 return process_fork_event(event, offset, head); 652 return process_fork_event(event, offset, head);
1028
1029 case PERF_EVENT_PERIOD:
1030 return process_period_event(event, offset, head);
1031 /* 653 /*
1032 * We dont process them right now but they are fine: 654 * We dont process them right now but they are fine:
1033 */ 655 */
1034 656
1035 case PERF_EVENT_THROTTLE: 657 case PERF_RECORD_THROTTLE:
1036 case PERF_EVENT_UNTHROTTLE: 658 case PERF_RECORD_UNTHROTTLE:
1037 return 0; 659 return 0;
1038 660
1039 default: 661 default:
@@ -1043,24 +665,6 @@ process_event(event_t *event, unsigned long offset, unsigned long head)
1043 return 0; 665 return 0;
1044} 666}
1045 667
1046static char *get_color(double percent)
1047{
1048 char *color = PERF_COLOR_NORMAL;
1049
1050 /*
1051 * We color high-overhead entries in red, mid-overhead
1052 * entries in green - and keep the low overhead places
1053 * normal:
1054 */
1055 if (percent >= MIN_RED)
1056 color = PERF_COLOR_RED;
1057 else {
1058 if (percent > MIN_GREEN)
1059 color = PERF_COLOR_GREEN;
1060 }
1061 return color;
1062}
1063
1064static int 668static int
1065parse_line(FILE *file, struct symbol *sym, u64 start, u64 len) 669parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
1066{ 670{
@@ -1069,7 +673,7 @@ parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
1069 static const char *prev_color; 673 static const char *prev_color;
1070 unsigned int offset; 674 unsigned int offset;
1071 size_t line_len; 675 size_t line_len;
1072 u64 line_ip; 676 s64 line_ip;
1073 int ret; 677 int ret;
1074 char *c; 678 char *c;
1075 679
@@ -1109,7 +713,7 @@ parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
1109 const char *path = NULL; 713 const char *path = NULL;
1110 unsigned int hits = 0; 714 unsigned int hits = 0;
1111 double percent = 0.0; 715 double percent = 0.0;
1112 char *color; 716 const char *color;
1113 struct sym_ext *sym_ext = sym->priv; 717 struct sym_ext *sym_ext = sym->priv;
1114 718
1115 offset = line_ip - start; 719 offset = line_ip - start;
@@ -1122,7 +726,7 @@ parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
1122 } else if (sym->hist_sum) 726 } else if (sym->hist_sum)
1123 percent = 100.0 * hits / sym->hist_sum; 727 percent = 100.0 * hits / sym->hist_sum;
1124 728
1125 color = get_color(percent); 729 color = get_percent_color(percent);
1126 730
1127 /* 731 /*
1128 * Also color the filename and line if needed, with 732 * Also color the filename and line if needed, with
@@ -1191,7 +795,7 @@ static void free_source_line(struct symbol *sym, int len)
1191 795
1192/* Get the filename:line for the colored entries */ 796/* Get the filename:line for the colored entries */
1193static void 797static void
1194get_source_line(struct symbol *sym, u64 start, int len, char *filename) 798get_source_line(struct symbol *sym, u64 start, int len, const char *filename)
1195{ 799{
1196 int i; 800 int i;
1197 char cmd[PATH_MAX * 2]; 801 char cmd[PATH_MAX * 2];
@@ -1237,7 +841,7 @@ get_source_line(struct symbol *sym, u64 start, int len, char *filename)
1237 } 841 }
1238} 842}
1239 843
1240static void print_summary(char *filename) 844static void print_summary(const char *filename)
1241{ 845{
1242 struct sym_ext *sym_ext; 846 struct sym_ext *sym_ext;
1243 struct rb_node *node; 847 struct rb_node *node;
@@ -1253,12 +857,12 @@ static void print_summary(char *filename)
1253 node = rb_first(&root_sym_ext); 857 node = rb_first(&root_sym_ext);
1254 while (node) { 858 while (node) {
1255 double percent; 859 double percent;
1256 char *color; 860 const char *color;
1257 char *path; 861 char *path;
1258 862
1259 sym_ext = rb_entry(node, struct sym_ext, node); 863 sym_ext = rb_entry(node, struct sym_ext, node);
1260 percent = sym_ext->percent; 864 percent = sym_ext->percent;
1261 color = get_color(percent); 865 color = get_percent_color(percent);
1262 path = sym_ext->path; 866 path = sym_ext->path;
1263 867
1264 color_fprintf(stdout, color, " %7.2f %s", percent, path); 868 color_fprintf(stdout, color, " %7.2f %s", percent, path);
@@ -1268,19 +872,25 @@ static void print_summary(char *filename)
1268 872
1269static void annotate_sym(struct dso *dso, struct symbol *sym) 873static void annotate_sym(struct dso *dso, struct symbol *sym)
1270{ 874{
1271 char *filename = dso->name; 875 const char *filename = dso->name, *d_filename;
1272 u64 start, end, len; 876 u64 start, end, len;
1273 char command[PATH_MAX*2]; 877 char command[PATH_MAX*2];
1274 FILE *file; 878 FILE *file;
1275 879
1276 if (!filename) 880 if (!filename)
1277 return; 881 return;
1278 if (dso == kernel_dso) 882 if (sym->module)
1279 filename = vmlinux; 883 filename = sym->module->path;
884 else if (dso == kernel_dso)
885 filename = vmlinux_name;
1280 886
1281 start = sym->obj_start; 887 start = sym->obj_start;
1282 if (!start) 888 if (!start)
1283 start = sym->start; 889 start = sym->start;
890 if (full_paths)
891 d_filename = filename;
892 else
893 d_filename = basename(filename);
1284 894
1285 end = start + sym->end - sym->start + 1; 895 end = start + sym->end - sym->start + 1;
1286 len = sym->end - sym->start; 896 len = sym->end - sym->start;
@@ -1291,13 +901,14 @@ static void annotate_sym(struct dso *dso, struct symbol *sym)
1291 } 901 }
1292 902
1293 printf("\n\n------------------------------------------------\n"); 903 printf("\n\n------------------------------------------------\n");
1294 printf(" Percent | Source code & Disassembly of %s\n", filename); 904 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
1295 printf("------------------------------------------------\n"); 905 printf("------------------------------------------------\n");
1296 906
1297 if (verbose >= 2) 907 if (verbose >= 2)
1298 printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name); 908 printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name);
1299 909
1300 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s", (u64)start, (u64)end, filename); 910 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
911 (u64)start, (u64)end, filename, filename);
1301 912
1302 if (verbose >= 3) 913 if (verbose >= 3)
1303 printf("doing: %s\n", command); 914 printf("doing: %s\n", command);
@@ -1343,12 +954,12 @@ static int __cmd_annotate(void)
1343 int ret, rc = EXIT_FAILURE; 954 int ret, rc = EXIT_FAILURE;
1344 unsigned long offset = 0; 955 unsigned long offset = 0;
1345 unsigned long head = 0; 956 unsigned long head = 0;
1346 struct stat stat; 957 struct stat input_stat;
1347 event_t *event; 958 event_t *event;
1348 uint32_t size; 959 uint32_t size;
1349 char *buf; 960 char *buf;
1350 961
1351 register_idle_thread(); 962 register_idle_thread(&threads, &last_match);
1352 963
1353 input = open(input_name, O_RDONLY); 964 input = open(input_name, O_RDONLY);
1354 if (input < 0) { 965 if (input < 0) {
@@ -1356,13 +967,18 @@ static int __cmd_annotate(void)
1356 exit(-1); 967 exit(-1);
1357 } 968 }
1358 969
1359 ret = fstat(input, &stat); 970 ret = fstat(input, &input_stat);
1360 if (ret < 0) { 971 if (ret < 0) {
1361 perror("failed to stat file"); 972 perror("failed to stat file");
1362 exit(-1); 973 exit(-1);
1363 } 974 }
1364 975
1365 if (!stat.st_size) { 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) {
1366 fprintf(stderr, "zero-sized file, nothing to do!\n"); 982 fprintf(stderr, "zero-sized file, nothing to do!\n");
1367 exit(0); 983 exit(0);
1368 } 984 }
@@ -1389,10 +1005,10 @@ more:
1389 1005
1390 if (head + event->header.size >= page_size * mmap_window) { 1006 if (head + event->header.size >= page_size * mmap_window) {
1391 unsigned long shift = page_size * (head / page_size); 1007 unsigned long shift = page_size * (head / page_size);
1392 int ret; 1008 int munmap_ret;
1393 1009
1394 ret = munmap(buf, page_size * mmap_window); 1010 munmap_ret = munmap(buf, page_size * mmap_window);
1395 assert(ret == 0); 1011 assert(munmap_ret == 0);
1396 1012
1397 offset += shift; 1013 offset += shift;
1398 head -= shift; 1014 head -= shift;
@@ -1401,14 +1017,14 @@ more:
1401 1017
1402 size = event->header.size; 1018 size = event->header.size;
1403 1019
1404 dprintf("%p [%p]: event: %d\n", 1020 dump_printf("%p [%p]: event: %d\n",
1405 (void *)(offset + head), 1021 (void *)(offset + head),
1406 (void *)(long)event->header.size, 1022 (void *)(long)event->header.size,
1407 event->header.type); 1023 event->header.type);
1408 1024
1409 if (!size || process_event(event, offset, head) < 0) { 1025 if (!size || process_event(event, offset, head) < 0) {
1410 1026
1411 dprintf("%p [%p]: skipping unknown header type: %d\n", 1027 dump_printf("%p [%p]: skipping unknown header type: %d\n",
1412 (void *)(offset + head), 1028 (void *)(offset + head),
1413 (void *)(long)(event->header.size), 1029 (void *)(long)(event->header.size),
1414 event->header.type); 1030 event->header.type);
@@ -1428,23 +1044,23 @@ more:
1428 1044
1429 head += size; 1045 head += size;
1430 1046
1431 if (offset + head < stat.st_size) 1047 if (offset + head < (unsigned long)input_stat.st_size)
1432 goto more; 1048 goto more;
1433 1049
1434 rc = EXIT_SUCCESS; 1050 rc = EXIT_SUCCESS;
1435 close(input); 1051 close(input);
1436 1052
1437 dprintf(" IP events: %10ld\n", total); 1053 dump_printf(" IP events: %10ld\n", total);
1438 dprintf(" mmap events: %10ld\n", total_mmap); 1054 dump_printf(" mmap events: %10ld\n", total_mmap);
1439 dprintf(" comm events: %10ld\n", total_comm); 1055 dump_printf(" comm events: %10ld\n", total_comm);
1440 dprintf(" fork events: %10ld\n", total_fork); 1056 dump_printf(" fork events: %10ld\n", total_fork);
1441 dprintf(" unknown events: %10ld\n", total_unknown); 1057 dump_printf(" unknown events: %10ld\n", total_unknown);
1442 1058
1443 if (dump_trace) 1059 if (dump_trace)
1444 return 0; 1060 return 0;
1445 1061
1446 if (verbose >= 3) 1062 if (verbose >= 3)
1447 threads__fprintf(stdout); 1063 threads__fprintf(stdout, &threads);
1448 1064
1449 if (verbose >= 2) 1065 if (verbose >= 2)
1450 dsos__fprintf(stdout); 1066 dsos__fprintf(stdout);
@@ -1467,13 +1083,18 @@ static const struct option options[] = {
1467 "input file name"), 1083 "input file name"),
1468 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol", 1084 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
1469 "symbol to annotate"), 1085 "symbol to annotate"),
1086 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
1470 OPT_BOOLEAN('v', "verbose", &verbose, 1087 OPT_BOOLEAN('v', "verbose", &verbose,
1471 "be more verbose (show symbol address, etc)"), 1088 "be more verbose (show symbol address, etc)"),
1472 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, 1089 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1473 "dump raw trace in ASCII"), 1090 "dump raw trace in ASCII"),
1474 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"), 1091 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
1092 OPT_BOOLEAN('m', "modules", &modules,
1093 "load module symbols - WARNING: use only with -k and LIVE kernel"),
1475 OPT_BOOLEAN('l', "print-line", &print_line, 1094 OPT_BOOLEAN('l', "print-line", &print_line,
1476 "print matching source lines (may be slow)"), 1095 "print matching source lines (may be slow)"),
1096 OPT_BOOLEAN('P', "full-paths", &full_paths,
1097 "Don't shorten the displayed pathnames"),
1477 OPT_END() 1098 OPT_END()
1478}; 1099};
1479 1100
@@ -1492,7 +1113,7 @@ static void setup_sorting(void)
1492 free(str); 1113 free(str);
1493} 1114}
1494 1115
1495int cmd_annotate(int argc, const char **argv, const char *prefix) 1116int cmd_annotate(int argc, const char **argv, const char *prefix __used)
1496{ 1117{
1497 symbol__init(); 1118 symbol__init();
1498 1119