4 * Builtin annotate command: Analyze the perf.data input file,
5 * look up and read DSOs and symbol information and display
6 * a histogram of results, along various sorting keys.
10 #include "util/util.h"
12 #include "util/color.h"
13 #include <linux/list.h>
14 #include "util/cache.h"
15 #include <linux/rbtree.h>
16 #include "util/symbol.h"
17 #include "util/string.h"
21 #include "util/parse-options.h"
22 #include "util/parse-events.h"
28 static char const *input_name
= "perf.data";
29 static char *vmlinux
= "vmlinux";
31 static char default_sort_order
[] = "comm,symbol";
32 static char *sort_order
= default_sort_order
;
35 static int show_mask
= SHOW_KERNEL
| SHOW_USER
| SHOW_HV
;
37 static int dump_trace
= 0;
38 #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
44 static int full_paths
;
46 static int print_line
;
48 static unsigned long page_size
;
49 static unsigned long mmap_window
= 32;
52 struct perf_event_header header
;
58 struct perf_event_header header
;
63 char filename
[PATH_MAX
];
67 struct perf_event_header header
;
73 struct perf_event_header header
;
78 struct perf_event_header header
;
84 typedef union event_union
{
85 struct perf_event_header header
;
87 struct mmap_event mmap
;
88 struct comm_event comm
;
89 struct fork_event fork
;
90 struct period_event period
;
100 static LIST_HEAD(dsos
);
101 static struct dso
*kernel_dso
;
102 static struct dso
*vdso
;
105 static void dsos__add(struct dso
*dso
)
107 list_add_tail(&dso
->node
, &dsos
);
110 static struct dso
*dsos__find(const char *name
)
114 list_for_each_entry(pos
, &dsos
, node
)
115 if (strcmp(pos
->name
, name
) == 0)
120 static struct dso
*dsos__findnew(const char *name
)
122 struct dso
*dso
= dsos__find(name
);
128 dso
= dso__new(name
, 0);
132 nr
= dso__load(dso
, NULL
, verbose
);
135 fprintf(stderr
, "Failed to open: %s\n", name
);
138 if (!nr
&& verbose
) {
140 "No symbols found in: %s, maybe install a debug package?\n",
153 static void dsos__fprintf(FILE *fp
)
157 list_for_each_entry(pos
, &dsos
, node
)
158 dso__fprintf(pos
, fp
);
161 static struct symbol
*vdso__find_symbol(struct dso
*dso
, u64 ip
)
163 return dso__find_symbol(dso
, ip
);
166 static int load_kernel(void)
170 kernel_dso
= dso__new("[kernel]", 0);
174 err
= dso__load_kernel(kernel_dso
, vmlinux
, NULL
, verbose
, modules
);
176 dso__delete(kernel_dso
);
179 dsos__add(kernel_dso
);
181 vdso
= dso__new("[vdso]", 0);
185 vdso
->find_symbol
= vdso__find_symbol
;
193 struct list_head node
;
197 u64 (*map_ip
)(struct map
*, u64
);
201 static u64
map__map_ip(struct map
*map
, u64 ip
)
203 return ip
- map
->start
+ map
->pgoff
;
206 static u64
vdso__map_ip(struct map
*map __used
, u64 ip
)
211 static struct map
*map__new(struct mmap_event
*event
)
213 struct map
*self
= malloc(sizeof(*self
));
216 const char *filename
= event
->filename
;
218 self
->start
= event
->start
;
219 self
->end
= event
->start
+ event
->len
;
220 self
->pgoff
= event
->pgoff
;
222 self
->dso
= dsos__findnew(filename
);
223 if (self
->dso
== NULL
)
226 if (self
->dso
== vdso
)
227 self
->map_ip
= vdso__map_ip
;
229 self
->map_ip
= map__map_ip
;
237 static struct map
*map__clone(struct map
*self
)
239 struct map
*map
= malloc(sizeof(*self
));
244 memcpy(map
, self
, sizeof(*self
));
249 static int map__overlap(struct map
*l
, struct map
*r
)
251 if (l
->start
> r
->start
) {
257 if (l
->end
> r
->start
)
263 static size_t map__fprintf(struct map
*self
, FILE *fp
)
265 return fprintf(fp
, " %Lx-%Lx %Lx %s\n",
266 self
->start
, self
->end
, self
->pgoff
, self
->dso
->name
);
271 struct rb_node rb_node
;
272 struct list_head maps
;
277 static struct thread
*thread__new(pid_t pid
)
279 struct thread
*self
= malloc(sizeof(*self
));
283 self
->comm
= malloc(32);
285 snprintf(self
->comm
, 32, ":%d", self
->pid
);
286 INIT_LIST_HEAD(&self
->maps
);
292 static int thread__set_comm(struct thread
*self
, const char *comm
)
296 self
->comm
= strdup(comm
);
297 return self
->comm
? 0 : -ENOMEM
;
300 static size_t thread__fprintf(struct thread
*self
, FILE *fp
)
303 size_t ret
= fprintf(fp
, "Thread %d %s\n", self
->pid
, self
->comm
);
305 list_for_each_entry(pos
, &self
->maps
, node
)
306 ret
+= map__fprintf(pos
, fp
);
312 static struct rb_root threads
;
313 static struct thread
*last_match
;
315 static struct thread
*threads__findnew(pid_t pid
)
317 struct rb_node
**p
= &threads
.rb_node
;
318 struct rb_node
*parent
= NULL
;
322 * Font-end cache - PID lookups come in blocks,
323 * so most of the time we dont have to look up
326 if (last_match
&& last_match
->pid
== pid
)
331 th
= rb_entry(parent
, struct thread
, rb_node
);
333 if (th
->pid
== pid
) {
344 th
= thread__new(pid
);
346 rb_link_node(&th
->rb_node
, parent
, p
);
347 rb_insert_color(&th
->rb_node
, &threads
);
354 static void thread__insert_map(struct thread
*self
, struct map
*map
)
356 struct map
*pos
, *tmp
;
358 list_for_each_entry_safe(pos
, tmp
, &self
->maps
, node
) {
359 if (map__overlap(pos
, map
)) {
360 list_del_init(&pos
->node
);
366 list_add_tail(&map
->node
, &self
->maps
);
369 static int thread__fork(struct thread
*self
, struct thread
*parent
)
375 self
->comm
= strdup(parent
->comm
);
379 list_for_each_entry(map
, &parent
->maps
, node
) {
380 struct map
*new = map__clone(map
);
383 thread__insert_map(self
, new);
389 static struct map
*thread__find_map(struct thread
*self
, u64 ip
)
396 list_for_each_entry(pos
, &self
->maps
, node
)
397 if (ip
>= pos
->start
&& ip
<= pos
->end
)
403 static size_t threads__fprintf(FILE *fp
)
408 for (nd
= rb_first(&threads
); nd
; nd
= rb_next(nd
)) {
409 struct thread
*pos
= rb_entry(nd
, struct thread
, rb_node
);
411 ret
+= thread__fprintf(pos
, fp
);
418 * histogram, sorted on item, collects counts
421 static struct rb_root hist
;
424 struct rb_node rb_node
;
426 struct thread
*thread
;
437 * configurable sorting bits
441 struct list_head list
;
445 int64_t (*cmp
)(struct hist_entry
*, struct hist_entry
*);
446 int64_t (*collapse
)(struct hist_entry
*, struct hist_entry
*);
447 size_t (*print
)(FILE *fp
, struct hist_entry
*);
453 sort__thread_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
455 return right
->thread
->pid
- left
->thread
->pid
;
459 sort__thread_print(FILE *fp
, struct hist_entry
*self
)
461 return fprintf(fp
, "%16s:%5d", self
->thread
->comm
?: "", self
->thread
->pid
);
464 static struct sort_entry sort_thread
= {
465 .header
= " Command: Pid",
466 .cmp
= sort__thread_cmp
,
467 .print
= sort__thread_print
,
473 sort__comm_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
475 return right
->thread
->pid
- left
->thread
->pid
;
479 sort__comm_collapse(struct hist_entry
*left
, struct hist_entry
*right
)
481 char *comm_l
= left
->thread
->comm
;
482 char *comm_r
= right
->thread
->comm
;
484 if (!comm_l
|| !comm_r
) {
485 if (!comm_l
&& !comm_r
)
493 return strcmp(comm_l
, comm_r
);
497 sort__comm_print(FILE *fp
, struct hist_entry
*self
)
499 return fprintf(fp
, "%16s", self
->thread
->comm
);
502 static struct sort_entry sort_comm
= {
503 .header
= " Command",
504 .cmp
= sort__comm_cmp
,
505 .collapse
= sort__comm_collapse
,
506 .print
= sort__comm_print
,
512 sort__dso_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
514 struct dso
*dso_l
= left
->dso
;
515 struct dso
*dso_r
= right
->dso
;
517 if (!dso_l
|| !dso_r
) {
518 if (!dso_l
&& !dso_r
)
526 return strcmp(dso_l
->name
, dso_r
->name
);
530 sort__dso_print(FILE *fp
, struct hist_entry
*self
)
533 return fprintf(fp
, "%-25s", self
->dso
->name
);
535 return fprintf(fp
, "%016llx ", (u64
)self
->ip
);
538 static struct sort_entry sort_dso
= {
539 .header
= "Shared Object ",
540 .cmp
= sort__dso_cmp
,
541 .print
= sort__dso_print
,
547 sort__sym_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
551 if (left
->sym
== right
->sym
)
554 ip_l
= left
->sym
? left
->sym
->start
: left
->ip
;
555 ip_r
= right
->sym
? right
->sym
->start
: right
->ip
;
557 return (int64_t)(ip_r
- ip_l
);
561 sort__sym_print(FILE *fp
, struct hist_entry
*self
)
566 ret
+= fprintf(fp
, "%#018llx ", (u64
)self
->ip
);
569 ret
+= fprintf(fp
, "[%c] %s",
570 self
->dso
== kernel_dso
? 'k' : '.', self
->sym
->name
);
572 ret
+= fprintf(fp
, "%#016llx", (u64
)self
->ip
);
578 static struct sort_entry sort_sym
= {
580 .cmp
= sort__sym_cmp
,
581 .print
= sort__sym_print
,
584 static int sort__need_collapse
= 0;
586 struct sort_dimension
{
588 struct sort_entry
*entry
;
592 static struct sort_dimension sort_dimensions
[] = {
593 { .name
= "pid", .entry
= &sort_thread
, },
594 { .name
= "comm", .entry
= &sort_comm
, },
595 { .name
= "dso", .entry
= &sort_dso
, },
596 { .name
= "symbol", .entry
= &sort_sym
, },
599 static LIST_HEAD(hist_entry__sort_list
);
601 static int sort_dimension__add(char *tok
)
605 for (i
= 0; i
< ARRAY_SIZE(sort_dimensions
); i
++) {
606 struct sort_dimension
*sd
= &sort_dimensions
[i
];
611 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
614 if (sd
->entry
->collapse
)
615 sort__need_collapse
= 1;
617 list_add_tail(&sd
->entry
->list
, &hist_entry__sort_list
);
627 hist_entry__cmp(struct hist_entry
*left
, struct hist_entry
*right
)
629 struct sort_entry
*se
;
632 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
633 cmp
= se
->cmp(left
, right
);
642 hist_entry__collapse(struct hist_entry
*left
, struct hist_entry
*right
)
644 struct sort_entry
*se
;
647 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
648 int64_t (*f
)(struct hist_entry
*, struct hist_entry
*);
650 f
= se
->collapse
?: se
->cmp
;
652 cmp
= f(left
, right
);
661 * collect histogram counts
663 static void hist_hit(struct hist_entry
*he
, u64 ip
)
665 unsigned int sym_size
, offset
;
666 struct symbol
*sym
= he
->sym
;
670 if (!sym
|| !sym
->hist
)
673 sym_size
= sym
->end
- sym
->start
;
674 offset
= ip
- sym
->start
;
676 if (offset
>= sym_size
)
683 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
684 (void *)(unsigned long)he
->sym
->start
,
686 (void *)(unsigned long)ip
, ip
- he
->sym
->start
,
691 hist_entry__add(struct thread
*thread
, struct map
*map
, struct dso
*dso
,
692 struct symbol
*sym
, u64 ip
, char level
)
694 struct rb_node
**p
= &hist
.rb_node
;
695 struct rb_node
*parent
= NULL
;
696 struct hist_entry
*he
;
697 struct hist_entry entry
= {
710 he
= rb_entry(parent
, struct hist_entry
, rb_node
);
712 cmp
= hist_entry__cmp(&entry
, he
);
726 he
= malloc(sizeof(*he
));
730 rb_link_node(&he
->rb_node
, parent
, p
);
731 rb_insert_color(&he
->rb_node
, &hist
);
736 static void hist_entry__free(struct hist_entry
*he
)
742 * collapse the histogram
745 static struct rb_root collapse_hists
;
747 static void collapse__insert_entry(struct hist_entry
*he
)
749 struct rb_node
**p
= &collapse_hists
.rb_node
;
750 struct rb_node
*parent
= NULL
;
751 struct hist_entry
*iter
;
756 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
758 cmp
= hist_entry__collapse(iter
, he
);
761 iter
->count
+= he
->count
;
762 hist_entry__free(he
);
772 rb_link_node(&he
->rb_node
, parent
, p
);
773 rb_insert_color(&he
->rb_node
, &collapse_hists
);
776 static void collapse__resort(void)
778 struct rb_node
*next
;
779 struct hist_entry
*n
;
781 if (!sort__need_collapse
)
784 next
= rb_first(&hist
);
786 n
= rb_entry(next
, struct hist_entry
, rb_node
);
787 next
= rb_next(&n
->rb_node
);
789 rb_erase(&n
->rb_node
, &hist
);
790 collapse__insert_entry(n
);
795 * reverse the map, sort on count.
798 static struct rb_root output_hists
;
800 static void output__insert_entry(struct hist_entry
*he
)
802 struct rb_node
**p
= &output_hists
.rb_node
;
803 struct rb_node
*parent
= NULL
;
804 struct hist_entry
*iter
;
808 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
810 if (he
->count
> iter
->count
)
816 rb_link_node(&he
->rb_node
, parent
, p
);
817 rb_insert_color(&he
->rb_node
, &output_hists
);
820 static void output__resort(void)
822 struct rb_node
*next
;
823 struct hist_entry
*n
;
824 struct rb_root
*tree
= &hist
;
826 if (sort__need_collapse
)
827 tree
= &collapse_hists
;
829 next
= rb_first(tree
);
832 n
= rb_entry(next
, struct hist_entry
, rb_node
);
833 next
= rb_next(&n
->rb_node
);
835 rb_erase(&n
->rb_node
, tree
);
836 output__insert_entry(n
);
840 static void register_idle_thread(void)
842 struct thread
*thread
= threads__findnew(0);
844 if (thread
== NULL
||
845 thread__set_comm(thread
, "[idle]")) {
846 fprintf(stderr
, "problem inserting idle task.\n");
851 static unsigned long total
= 0,
858 process_sample_event(event_t
*event
, unsigned long offset
, unsigned long head
)
862 struct dso
*dso
= NULL
;
863 struct thread
*thread
= threads__findnew(event
->ip
.pid
);
864 u64 ip
= event
->ip
.ip
;
865 struct map
*map
= NULL
;
867 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
868 (void *)(offset
+ head
),
869 (void *)(long)(event
->header
.size
),
874 dprintf(" ... thread: %s:%d\n", thread
->comm
, thread
->pid
);
876 if (thread
== NULL
) {
877 fprintf(stderr
, "problem processing %d event, skipping it.\n",
882 if (event
->header
.misc
& PERF_EVENT_MISC_KERNEL
) {
888 dprintf(" ...... dso: %s\n", dso
->name
);
890 } else if (event
->header
.misc
& PERF_EVENT_MISC_USER
) {
895 map
= thread__find_map(thread
, ip
);
897 ip
= map
->map_ip(map
, ip
);
901 * If this is outside of all known maps,
902 * and is a negative address, try to look it
903 * up in the kernel dso, as it might be a
904 * vsyscall (which executes in user-mode):
906 if ((long long)ip
< 0)
909 dprintf(" ...... dso: %s\n", dso
? dso
->name
: "<not found>");
914 dprintf(" ...... dso: [hypervisor]\n");
917 if (show
& show_mask
) {
918 struct symbol
*sym
= NULL
;
921 sym
= dso
->find_symbol(dso
, ip
);
923 if (hist_entry__add(thread
, map
, dso
, sym
, ip
, level
)) {
925 "problem incrementing symbol count, skipping event\n");
935 process_mmap_event(event_t
*event
, unsigned long offset
, unsigned long head
)
937 struct thread
*thread
= threads__findnew(event
->mmap
.pid
);
938 struct map
*map
= map__new(&event
->mmap
);
940 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
941 (void *)(offset
+ head
),
942 (void *)(long)(event
->header
.size
),
944 (void *)(long)event
->mmap
.start
,
945 (void *)(long)event
->mmap
.len
,
946 (void *)(long)event
->mmap
.pgoff
,
947 event
->mmap
.filename
);
949 if (thread
== NULL
|| map
== NULL
) {
950 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
954 thread__insert_map(thread
, map
);
961 process_comm_event(event_t
*event
, unsigned long offset
, unsigned long head
)
963 struct thread
*thread
= threads__findnew(event
->comm
.pid
);
965 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
966 (void *)(offset
+ head
),
967 (void *)(long)(event
->header
.size
),
968 event
->comm
.comm
, event
->comm
.pid
);
970 if (thread
== NULL
||
971 thread__set_comm(thread
, event
->comm
.comm
)) {
972 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
981 process_fork_event(event_t
*event
, unsigned long offset
, unsigned long head
)
983 struct thread
*thread
= threads__findnew(event
->fork
.pid
);
984 struct thread
*parent
= threads__findnew(event
->fork
.ppid
);
986 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
987 (void *)(offset
+ head
),
988 (void *)(long)(event
->header
.size
),
989 event
->fork
.pid
, event
->fork
.ppid
);
991 if (!thread
|| !parent
|| thread__fork(thread
, parent
)) {
992 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1001 process_period_event(event_t
*event
, unsigned long offset
, unsigned long head
)
1003 dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
1004 (void *)(offset
+ head
),
1005 (void *)(long)(event
->header
.size
),
1008 event
->period
.sample_period
);
1014 process_event(event_t
*event
, unsigned long offset
, unsigned long head
)
1016 switch (event
->header
.type
) {
1017 case PERF_EVENT_SAMPLE
:
1018 return process_sample_event(event
, offset
, head
);
1020 case PERF_EVENT_MMAP
:
1021 return process_mmap_event(event
, offset
, head
);
1023 case PERF_EVENT_COMM
:
1024 return process_comm_event(event
, offset
, head
);
1026 case PERF_EVENT_FORK
:
1027 return process_fork_event(event
, offset
, head
);
1029 case PERF_EVENT_PERIOD
:
1030 return process_period_event(event
, offset
, head
);
1032 * We dont process them right now but they are fine:
1035 case PERF_EVENT_THROTTLE
:
1036 case PERF_EVENT_UNTHROTTLE
:
1047 parse_line(FILE *file
, struct symbol
*sym
, u64 start
, u64 len
)
1049 char *line
= NULL
, *tmp
, *tmp2
;
1050 static const char *prev_line
;
1051 static const char *prev_color
;
1052 unsigned int offset
;
1058 if (getline(&line
, &line_len
, file
) < 0)
1063 c
= strchr(line
, '\n');
1072 * Strip leading spaces:
1083 * Parse hexa addresses followed by ':'
1085 line_ip
= strtoull(tmp
, &tmp2
, 16);
1090 if (line_ip
!= -1) {
1091 const char *path
= NULL
;
1092 unsigned int hits
= 0;
1093 double percent
= 0.0;
1095 struct sym_ext
*sym_ext
= sym
->priv
;
1097 offset
= line_ip
- start
;
1099 hits
= sym
->hist
[offset
];
1101 if (offset
< len
&& sym_ext
) {
1102 path
= sym_ext
[offset
].path
;
1103 percent
= sym_ext
[offset
].percent
;
1104 } else if (sym
->hist_sum
)
1105 percent
= 100.0 * hits
/ sym
->hist_sum
;
1107 color
= get_percent_color(percent
);
1110 * Also color the filename and line if needed, with
1111 * the same color than the percentage. Don't print it
1112 * twice for close colored ip with the same filename:line
1115 if (!prev_line
|| strcmp(prev_line
, path
)
1116 || color
!= prev_color
) {
1117 color_fprintf(stdout
, color
, " %s", path
);
1123 color_fprintf(stdout
, color
, " %7.2f", percent
);
1125 color_fprintf(stdout
, PERF_COLOR_BLUE
, "%s\n", line
);
1130 printf(" : %s\n", line
);
1136 static struct rb_root root_sym_ext
;
1138 static void insert_source_line(struct sym_ext
*sym_ext
)
1140 struct sym_ext
*iter
;
1141 struct rb_node
**p
= &root_sym_ext
.rb_node
;
1142 struct rb_node
*parent
= NULL
;
1144 while (*p
!= NULL
) {
1146 iter
= rb_entry(parent
, struct sym_ext
, node
);
1148 if (sym_ext
->percent
> iter
->percent
)
1151 p
= &(*p
)->rb_right
;
1154 rb_link_node(&sym_ext
->node
, parent
, p
);
1155 rb_insert_color(&sym_ext
->node
, &root_sym_ext
);
1158 static void free_source_line(struct symbol
*sym
, int len
)
1160 struct sym_ext
*sym_ext
= sym
->priv
;
1166 for (i
= 0; i
< len
; i
++)
1167 free(sym_ext
[i
].path
);
1171 root_sym_ext
= RB_ROOT
;
1174 /* Get the filename:line for the colored entries */
1176 get_source_line(struct symbol
*sym
, u64 start
, int len
, char *filename
)
1179 char cmd
[PATH_MAX
* 2];
1180 struct sym_ext
*sym_ext
;
1185 sym
->priv
= calloc(len
, sizeof(struct sym_ext
));
1189 sym_ext
= sym
->priv
;
1191 for (i
= 0; i
< len
; i
++) {
1197 sym_ext
[i
].percent
= 100.0 * sym
->hist
[i
] / sym
->hist_sum
;
1198 if (sym_ext
[i
].percent
<= 0.5)
1202 sprintf(cmd
, "addr2line -e %s %016llx", filename
, offset
);
1203 fp
= popen(cmd
, "r");
1207 if (getline(&path
, &line_len
, fp
) < 0 || !line_len
)
1210 sym_ext
[i
].path
= malloc(sizeof(char) * line_len
+ 1);
1211 if (!sym_ext
[i
].path
)
1214 strcpy(sym_ext
[i
].path
, path
);
1215 insert_source_line(&sym_ext
[i
]);
1222 static void print_summary(char *filename
)
1224 struct sym_ext
*sym_ext
;
1225 struct rb_node
*node
;
1227 printf("\nSorted summary for file %s\n", filename
);
1228 printf("----------------------------------------------\n\n");
1230 if (RB_EMPTY_ROOT(&root_sym_ext
)) {
1231 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN
);
1235 node
= rb_first(&root_sym_ext
);
1241 sym_ext
= rb_entry(node
, struct sym_ext
, node
);
1242 percent
= sym_ext
->percent
;
1243 color
= get_percent_color(percent
);
1244 path
= sym_ext
->path
;
1246 color_fprintf(stdout
, color
, " %7.2f %s", percent
, path
);
1247 node
= rb_next(node
);
1251 static void annotate_sym(struct dso
*dso
, struct symbol
*sym
)
1253 char *filename
= dso
->name
, *d_filename
;
1254 u64 start
, end
, len
;
1255 char command
[PATH_MAX
*2];
1261 filename
= sym
->module
->path
;
1262 else if (dso
== kernel_dso
)
1265 start
= sym
->obj_start
;
1269 d_filename
= filename
;
1271 d_filename
= basename(filename
);
1273 end
= start
+ sym
->end
- sym
->start
+ 1;
1274 len
= sym
->end
- sym
->start
;
1277 get_source_line(sym
, start
, len
, filename
);
1278 print_summary(filename
);
1281 printf("\n\n------------------------------------------------\n");
1282 printf(" Percent | Source code & Disassembly of %s\n", d_filename
);
1283 printf("------------------------------------------------\n");
1286 printf("annotating [%p] %30s : [%p] %30s\n", dso
, dso
->name
, sym
, sym
->name
);
1288 sprintf(command
, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
1289 (u64
)start
, (u64
)end
, filename
, filename
);
1292 printf("doing: %s\n", command
);
1294 file
= popen(command
, "r");
1298 while (!feof(file
)) {
1299 if (parse_line(file
, sym
, start
, len
) < 0)
1305 free_source_line(sym
, len
);
1308 static void find_annotations(void)
1314 list_for_each_entry(dso
, &dsos
, node
) {
1316 for (nd
= rb_first(&dso
->syms
); nd
; nd
= rb_next(nd
)) {
1317 struct symbol
*sym
= rb_entry(nd
, struct symbol
, rb_node
);
1320 annotate_sym(dso
, sym
);
1327 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter
);
1330 static int __cmd_annotate(void)
1332 int ret
, rc
= EXIT_FAILURE
;
1333 unsigned long offset
= 0;
1334 unsigned long head
= 0;
1340 register_idle_thread();
1342 input
= open(input_name
, O_RDONLY
);
1344 perror("failed to open file");
1348 ret
= fstat(input
, &stat
);
1350 perror("failed to stat file");
1354 if (!stat
.st_size
) {
1355 fprintf(stderr
, "zero-sized file, nothing to do!\n");
1359 if (load_kernel() < 0) {
1360 perror("failed to load kernel symbols");
1361 return EXIT_FAILURE
;
1365 buf
= (char *)mmap(NULL
, page_size
* mmap_window
, PROT_READ
,
1366 MAP_SHARED
, input
, offset
);
1367 if (buf
== MAP_FAILED
) {
1368 perror("failed to mmap file");
1373 event
= (event_t
*)(buf
+ head
);
1375 size
= event
->header
.size
;
1379 if (head
+ event
->header
.size
>= page_size
* mmap_window
) {
1380 unsigned long shift
= page_size
* (head
/ page_size
);
1383 ret
= munmap(buf
, page_size
* mmap_window
);
1391 size
= event
->header
.size
;
1393 dprintf("%p [%p]: event: %d\n",
1394 (void *)(offset
+ head
),
1395 (void *)(long)event
->header
.size
,
1396 event
->header
.type
);
1398 if (!size
|| process_event(event
, offset
, head
) < 0) {
1400 dprintf("%p [%p]: skipping unknown header type: %d\n",
1401 (void *)(offset
+ head
),
1402 (void *)(long)(event
->header
.size
),
1403 event
->header
.type
);
1408 * assume we lost track of the stream, check alignment, and
1409 * increment a single u64 in the hope to catch on again 'soon'.
1412 if (unlikely(head
& 7))
1420 if (offset
+ head
< (unsigned long)stat
.st_size
)
1426 dprintf(" IP events: %10ld\n", total
);
1427 dprintf(" mmap events: %10ld\n", total_mmap
);
1428 dprintf(" comm events: %10ld\n", total_comm
);
1429 dprintf(" fork events: %10ld\n", total_fork
);
1430 dprintf(" unknown events: %10ld\n", total_unknown
);
1436 threads__fprintf(stdout
);
1439 dsos__fprintf(stdout
);
1449 static const char * const annotate_usage
[] = {
1450 "perf annotate [<options>] <command>",
1454 static const struct option options
[] = {
1455 OPT_STRING('i', "input", &input_name
, "file",
1457 OPT_STRING('s', "symbol", &sym_hist_filter
, "symbol",
1458 "symbol to annotate"),
1459 OPT_BOOLEAN('v', "verbose", &verbose
,
1460 "be more verbose (show symbol address, etc)"),
1461 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
1462 "dump raw trace in ASCII"),
1463 OPT_STRING('k', "vmlinux", &vmlinux
, "file", "vmlinux pathname"),
1464 OPT_BOOLEAN('m', "modules", &modules
,
1465 "load module symbols - WARNING: use only with -k and LIVE kernel"),
1466 OPT_BOOLEAN('l', "print-line", &print_line
,
1467 "print matching source lines (may be slow)"),
1468 OPT_BOOLEAN('P', "full-paths", &full_paths
,
1469 "Don't shorten the displayed pathnames"),
1473 static void setup_sorting(void)
1475 char *tmp
, *tok
, *str
= strdup(sort_order
);
1477 for (tok
= strtok_r(str
, ", ", &tmp
);
1478 tok
; tok
= strtok_r(NULL
, ", ", &tmp
)) {
1479 if (sort_dimension__add(tok
) < 0) {
1480 error("Unknown --sort key: `%s'", tok
);
1481 usage_with_options(annotate_usage
, options
);
1488 int cmd_annotate(int argc
, const char **argv
, const char *prefix __used
)
1492 page_size
= getpagesize();
1494 argc
= parse_options(argc
, argv
, options
, annotate_usage
, 0);
1500 * Special case: if there's an argument left then assume tha
1501 * it's a symbol filter:
1504 usage_with_options(annotate_usage
, options
);
1506 sym_hist_filter
= argv
[0];
1509 if (!sym_hist_filter
)
1510 usage_with_options(annotate_usage
, options
);
1514 return __cmd_annotate();