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
;
36 static int show_mask
= SHOW_KERNEL
| SHOW_USER
| SHOW_HV
;
38 static int dump_trace
= 0;
39 #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
45 static int full_paths
;
47 static int print_line
;
49 static unsigned long page_size
;
50 static unsigned long mmap_window
= 32;
53 struct perf_event_header header
;
59 struct perf_event_header header
;
64 char filename
[PATH_MAX
];
68 struct perf_event_header header
;
74 struct perf_event_header header
;
78 typedef union event_union
{
79 struct perf_event_header header
;
81 struct mmap_event mmap
;
82 struct comm_event comm
;
83 struct fork_event fork
;
93 static LIST_HEAD(dsos
);
94 static struct dso
*kernel_dso
;
95 static struct dso
*vdso
;
98 static void dsos__add(struct dso
*dso
)
100 list_add_tail(&dso
->node
, &dsos
);
103 static struct dso
*dsos__find(const char *name
)
107 list_for_each_entry(pos
, &dsos
, node
)
108 if (strcmp(pos
->name
, name
) == 0)
113 static struct dso
*dsos__findnew(const char *name
)
115 struct dso
*dso
= dsos__find(name
);
121 dso
= dso__new(name
, 0);
125 nr
= dso__load(dso
, NULL
, verbose
);
128 fprintf(stderr
, "Failed to open: %s\n", name
);
131 if (!nr
&& verbose
) {
133 "No symbols found in: %s, maybe install a debug package?\n",
146 static void dsos__fprintf(FILE *fp
)
150 list_for_each_entry(pos
, &dsos
, node
)
151 dso__fprintf(pos
, fp
);
154 static struct symbol
*vdso__find_symbol(struct dso
*dso
, u64 ip
)
156 return dso__find_symbol(dso
, ip
);
159 static int load_kernel(void)
163 kernel_dso
= dso__new("[kernel]", 0);
167 err
= dso__load_kernel(kernel_dso
, vmlinux
, NULL
, verbose
, modules
);
169 dso__delete(kernel_dso
);
172 dsos__add(kernel_dso
);
174 vdso
= dso__new("[vdso]", 0);
178 vdso
->find_symbol
= vdso__find_symbol
;
186 struct list_head node
;
190 u64 (*map_ip
)(struct map
*, u64
);
194 static u64
map__map_ip(struct map
*map
, u64 ip
)
196 return ip
- map
->start
+ map
->pgoff
;
199 static u64
vdso__map_ip(struct map
*map __used
, u64 ip
)
204 static struct map
*map__new(struct mmap_event
*event
)
206 struct map
*self
= malloc(sizeof(*self
));
209 const char *filename
= event
->filename
;
211 self
->start
= event
->start
;
212 self
->end
= event
->start
+ event
->len
;
213 self
->pgoff
= event
->pgoff
;
215 self
->dso
= dsos__findnew(filename
);
216 if (self
->dso
== NULL
)
219 if (self
->dso
== vdso
)
220 self
->map_ip
= vdso__map_ip
;
222 self
->map_ip
= map__map_ip
;
230 static struct map
*map__clone(struct map
*self
)
232 struct map
*map
= malloc(sizeof(*self
));
237 memcpy(map
, self
, sizeof(*self
));
242 static int map__overlap(struct map
*l
, struct map
*r
)
244 if (l
->start
> r
->start
) {
250 if (l
->end
> r
->start
)
256 static size_t map__fprintf(struct map
*self
, FILE *fp
)
258 return fprintf(fp
, " %Lx-%Lx %Lx %s\n",
259 self
->start
, self
->end
, self
->pgoff
, self
->dso
->name
);
264 struct rb_node rb_node
;
265 struct list_head maps
;
270 static struct thread
*thread__new(pid_t pid
)
272 struct thread
*self
= malloc(sizeof(*self
));
276 self
->comm
= malloc(32);
278 snprintf(self
->comm
, 32, ":%d", self
->pid
);
279 INIT_LIST_HEAD(&self
->maps
);
285 static int thread__set_comm(struct thread
*self
, const char *comm
)
289 self
->comm
= strdup(comm
);
290 return self
->comm
? 0 : -ENOMEM
;
293 static size_t thread__fprintf(struct thread
*self
, FILE *fp
)
296 size_t ret
= fprintf(fp
, "Thread %d %s\n", self
->pid
, self
->comm
);
298 list_for_each_entry(pos
, &self
->maps
, node
)
299 ret
+= map__fprintf(pos
, fp
);
305 static struct rb_root threads
;
306 static struct thread
*last_match
;
308 static struct thread
*threads__findnew(pid_t pid
)
310 struct rb_node
**p
= &threads
.rb_node
;
311 struct rb_node
*parent
= NULL
;
315 * Font-end cache - PID lookups come in blocks,
316 * so most of the time we dont have to look up
319 if (last_match
&& last_match
->pid
== pid
)
324 th
= rb_entry(parent
, struct thread
, rb_node
);
326 if (th
->pid
== pid
) {
337 th
= thread__new(pid
);
339 rb_link_node(&th
->rb_node
, parent
, p
);
340 rb_insert_color(&th
->rb_node
, &threads
);
347 static void thread__insert_map(struct thread
*self
, struct map
*map
)
349 struct map
*pos
, *tmp
;
351 list_for_each_entry_safe(pos
, tmp
, &self
->maps
, node
) {
352 if (map__overlap(pos
, map
)) {
353 list_del_init(&pos
->node
);
359 list_add_tail(&map
->node
, &self
->maps
);
362 static int thread__fork(struct thread
*self
, struct thread
*parent
)
368 self
->comm
= strdup(parent
->comm
);
372 list_for_each_entry(map
, &parent
->maps
, node
) {
373 struct map
*new = map__clone(map
);
376 thread__insert_map(self
, new);
382 static struct map
*thread__find_map(struct thread
*self
, u64 ip
)
389 list_for_each_entry(pos
, &self
->maps
, node
)
390 if (ip
>= pos
->start
&& ip
<= pos
->end
)
396 static size_t threads__fprintf(FILE *fp
)
401 for (nd
= rb_first(&threads
); nd
; nd
= rb_next(nd
)) {
402 struct thread
*pos
= rb_entry(nd
, struct thread
, rb_node
);
404 ret
+= thread__fprintf(pos
, fp
);
411 * histogram, sorted on item, collects counts
414 static struct rb_root hist
;
417 struct rb_node rb_node
;
419 struct thread
*thread
;
430 * configurable sorting bits
434 struct list_head list
;
438 int64_t (*cmp
)(struct hist_entry
*, struct hist_entry
*);
439 int64_t (*collapse
)(struct hist_entry
*, struct hist_entry
*);
440 size_t (*print
)(FILE *fp
, struct hist_entry
*);
446 sort__thread_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
448 return right
->thread
->pid
- left
->thread
->pid
;
452 sort__thread_print(FILE *fp
, struct hist_entry
*self
)
454 return fprintf(fp
, "%16s:%5d", self
->thread
->comm
?: "", self
->thread
->pid
);
457 static struct sort_entry sort_thread
= {
458 .header
= " Command: Pid",
459 .cmp
= sort__thread_cmp
,
460 .print
= sort__thread_print
,
466 sort__comm_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
468 return right
->thread
->pid
- left
->thread
->pid
;
472 sort__comm_collapse(struct hist_entry
*left
, struct hist_entry
*right
)
474 char *comm_l
= left
->thread
->comm
;
475 char *comm_r
= right
->thread
->comm
;
477 if (!comm_l
|| !comm_r
) {
478 if (!comm_l
&& !comm_r
)
486 return strcmp(comm_l
, comm_r
);
490 sort__comm_print(FILE *fp
, struct hist_entry
*self
)
492 return fprintf(fp
, "%16s", self
->thread
->comm
);
495 static struct sort_entry sort_comm
= {
496 .header
= " Command",
497 .cmp
= sort__comm_cmp
,
498 .collapse
= sort__comm_collapse
,
499 .print
= sort__comm_print
,
505 sort__dso_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
507 struct dso
*dso_l
= left
->dso
;
508 struct dso
*dso_r
= right
->dso
;
510 if (!dso_l
|| !dso_r
) {
511 if (!dso_l
&& !dso_r
)
519 return strcmp(dso_l
->name
, dso_r
->name
);
523 sort__dso_print(FILE *fp
, struct hist_entry
*self
)
526 return fprintf(fp
, "%-25s", self
->dso
->name
);
528 return fprintf(fp
, "%016llx ", (u64
)self
->ip
);
531 static struct sort_entry sort_dso
= {
532 .header
= "Shared Object ",
533 .cmp
= sort__dso_cmp
,
534 .print
= sort__dso_print
,
540 sort__sym_cmp(struct hist_entry
*left
, struct hist_entry
*right
)
544 if (left
->sym
== right
->sym
)
547 ip_l
= left
->sym
? left
->sym
->start
: left
->ip
;
548 ip_r
= right
->sym
? right
->sym
->start
: right
->ip
;
550 return (int64_t)(ip_r
- ip_l
);
554 sort__sym_print(FILE *fp
, struct hist_entry
*self
)
559 ret
+= fprintf(fp
, "%#018llx ", (u64
)self
->ip
);
562 ret
+= fprintf(fp
, "[%c] %s",
563 self
->dso
== kernel_dso
? 'k' : '.', self
->sym
->name
);
565 ret
+= fprintf(fp
, "%#016llx", (u64
)self
->ip
);
571 static struct sort_entry sort_sym
= {
573 .cmp
= sort__sym_cmp
,
574 .print
= sort__sym_print
,
577 static int sort__need_collapse
= 0;
579 struct sort_dimension
{
581 struct sort_entry
*entry
;
585 static struct sort_dimension sort_dimensions
[] = {
586 { .name
= "pid", .entry
= &sort_thread
, },
587 { .name
= "comm", .entry
= &sort_comm
, },
588 { .name
= "dso", .entry
= &sort_dso
, },
589 { .name
= "symbol", .entry
= &sort_sym
, },
592 static LIST_HEAD(hist_entry__sort_list
);
594 static int sort_dimension__add(char *tok
)
598 for (i
= 0; i
< ARRAY_SIZE(sort_dimensions
); i
++) {
599 struct sort_dimension
*sd
= &sort_dimensions
[i
];
604 if (strncasecmp(tok
, sd
->name
, strlen(tok
)))
607 if (sd
->entry
->collapse
)
608 sort__need_collapse
= 1;
610 list_add_tail(&sd
->entry
->list
, &hist_entry__sort_list
);
620 hist_entry__cmp(struct hist_entry
*left
, struct hist_entry
*right
)
622 struct sort_entry
*se
;
625 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
626 cmp
= se
->cmp(left
, right
);
635 hist_entry__collapse(struct hist_entry
*left
, struct hist_entry
*right
)
637 struct sort_entry
*se
;
640 list_for_each_entry(se
, &hist_entry__sort_list
, list
) {
641 int64_t (*f
)(struct hist_entry
*, struct hist_entry
*);
643 f
= se
->collapse
?: se
->cmp
;
645 cmp
= f(left
, right
);
654 * collect histogram counts
656 static void hist_hit(struct hist_entry
*he
, u64 ip
)
658 unsigned int sym_size
, offset
;
659 struct symbol
*sym
= he
->sym
;
663 if (!sym
|| !sym
->hist
)
666 sym_size
= sym
->end
- sym
->start
;
667 offset
= ip
- sym
->start
;
669 if (offset
>= sym_size
)
676 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
677 (void *)(unsigned long)he
->sym
->start
,
679 (void *)(unsigned long)ip
, ip
- he
->sym
->start
,
684 hist_entry__add(struct thread
*thread
, struct map
*map
, struct dso
*dso
,
685 struct symbol
*sym
, u64 ip
, char level
)
687 struct rb_node
**p
= &hist
.rb_node
;
688 struct rb_node
*parent
= NULL
;
689 struct hist_entry
*he
;
690 struct hist_entry entry
= {
703 he
= rb_entry(parent
, struct hist_entry
, rb_node
);
705 cmp
= hist_entry__cmp(&entry
, he
);
719 he
= malloc(sizeof(*he
));
723 rb_link_node(&he
->rb_node
, parent
, p
);
724 rb_insert_color(&he
->rb_node
, &hist
);
729 static void hist_entry__free(struct hist_entry
*he
)
735 * collapse the histogram
738 static struct rb_root collapse_hists
;
740 static void collapse__insert_entry(struct hist_entry
*he
)
742 struct rb_node
**p
= &collapse_hists
.rb_node
;
743 struct rb_node
*parent
= NULL
;
744 struct hist_entry
*iter
;
749 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
751 cmp
= hist_entry__collapse(iter
, he
);
754 iter
->count
+= he
->count
;
755 hist_entry__free(he
);
765 rb_link_node(&he
->rb_node
, parent
, p
);
766 rb_insert_color(&he
->rb_node
, &collapse_hists
);
769 static void collapse__resort(void)
771 struct rb_node
*next
;
772 struct hist_entry
*n
;
774 if (!sort__need_collapse
)
777 next
= rb_first(&hist
);
779 n
= rb_entry(next
, struct hist_entry
, rb_node
);
780 next
= rb_next(&n
->rb_node
);
782 rb_erase(&n
->rb_node
, &hist
);
783 collapse__insert_entry(n
);
788 * reverse the map, sort on count.
791 static struct rb_root output_hists
;
793 static void output__insert_entry(struct hist_entry
*he
)
795 struct rb_node
**p
= &output_hists
.rb_node
;
796 struct rb_node
*parent
= NULL
;
797 struct hist_entry
*iter
;
801 iter
= rb_entry(parent
, struct hist_entry
, rb_node
);
803 if (he
->count
> iter
->count
)
809 rb_link_node(&he
->rb_node
, parent
, p
);
810 rb_insert_color(&he
->rb_node
, &output_hists
);
813 static void output__resort(void)
815 struct rb_node
*next
;
816 struct hist_entry
*n
;
817 struct rb_root
*tree
= &hist
;
819 if (sort__need_collapse
)
820 tree
= &collapse_hists
;
822 next
= rb_first(tree
);
825 n
= rb_entry(next
, struct hist_entry
, rb_node
);
826 next
= rb_next(&n
->rb_node
);
828 rb_erase(&n
->rb_node
, tree
);
829 output__insert_entry(n
);
833 static void register_idle_thread(void)
835 struct thread
*thread
= threads__findnew(0);
837 if (thread
== NULL
||
838 thread__set_comm(thread
, "[idle]")) {
839 fprintf(stderr
, "problem inserting idle task.\n");
844 static unsigned long total
= 0,
851 process_sample_event(event_t
*event
, unsigned long offset
, unsigned long head
)
855 struct dso
*dso
= NULL
;
856 struct thread
*thread
= threads__findnew(event
->ip
.pid
);
857 u64 ip
= event
->ip
.ip
;
858 struct map
*map
= NULL
;
860 dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
861 (void *)(offset
+ head
),
862 (void *)(long)(event
->header
.size
),
867 dprintf(" ... thread: %s:%d\n", thread
->comm
, thread
->pid
);
869 if (thread
== NULL
) {
870 fprintf(stderr
, "problem processing %d event, skipping it.\n",
875 if (event
->header
.misc
& PERF_EVENT_MISC_KERNEL
) {
881 dprintf(" ...... dso: %s\n", dso
->name
);
883 } else if (event
->header
.misc
& PERF_EVENT_MISC_USER
) {
888 map
= thread__find_map(thread
, ip
);
890 ip
= map
->map_ip(map
, ip
);
894 * If this is outside of all known maps,
895 * and is a negative address, try to look it
896 * up in the kernel dso, as it might be a
897 * vsyscall (which executes in user-mode):
899 if ((long long)ip
< 0)
902 dprintf(" ...... dso: %s\n", dso
? dso
->name
: "<not found>");
907 dprintf(" ...... dso: [hypervisor]\n");
910 if (show
& show_mask
) {
911 struct symbol
*sym
= NULL
;
914 sym
= dso
->find_symbol(dso
, ip
);
916 if (hist_entry__add(thread
, map
, dso
, sym
, ip
, level
)) {
918 "problem incrementing symbol count, skipping event\n");
928 process_mmap_event(event_t
*event
, unsigned long offset
, unsigned long head
)
930 struct thread
*thread
= threads__findnew(event
->mmap
.pid
);
931 struct map
*map
= map__new(&event
->mmap
);
933 dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
934 (void *)(offset
+ head
),
935 (void *)(long)(event
->header
.size
),
937 (void *)(long)event
->mmap
.start
,
938 (void *)(long)event
->mmap
.len
,
939 (void *)(long)event
->mmap
.pgoff
,
940 event
->mmap
.filename
);
942 if (thread
== NULL
|| map
== NULL
) {
943 dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
947 thread__insert_map(thread
, map
);
954 process_comm_event(event_t
*event
, unsigned long offset
, unsigned long head
)
956 struct thread
*thread
= threads__findnew(event
->comm
.pid
);
958 dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
959 (void *)(offset
+ head
),
960 (void *)(long)(event
->header
.size
),
961 event
->comm
.comm
, event
->comm
.pid
);
963 if (thread
== NULL
||
964 thread__set_comm(thread
, event
->comm
.comm
)) {
965 dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
974 process_fork_event(event_t
*event
, unsigned long offset
, unsigned long head
)
976 struct thread
*thread
= threads__findnew(event
->fork
.pid
);
977 struct thread
*parent
= threads__findnew(event
->fork
.ppid
);
979 dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
980 (void *)(offset
+ head
),
981 (void *)(long)(event
->header
.size
),
982 event
->fork
.pid
, event
->fork
.ppid
);
985 * A thread clone will have the same PID for both
988 if (thread
== parent
)
991 if (!thread
|| !parent
|| thread__fork(thread
, parent
)) {
992 dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
1001 process_event(event_t
*event
, unsigned long offset
, unsigned long head
)
1003 switch (event
->header
.type
) {
1004 case PERF_EVENT_SAMPLE
:
1005 return process_sample_event(event
, offset
, head
);
1007 case PERF_EVENT_MMAP
:
1008 return process_mmap_event(event
, offset
, head
);
1010 case PERF_EVENT_COMM
:
1011 return process_comm_event(event
, offset
, head
);
1013 case PERF_EVENT_FORK
:
1014 return process_fork_event(event
, offset
, head
);
1016 * We dont process them right now but they are fine:
1019 case PERF_EVENT_THROTTLE
:
1020 case PERF_EVENT_UNTHROTTLE
:
1031 parse_line(FILE *file
, struct symbol
*sym
, u64 start
, u64 len
)
1033 char *line
= NULL
, *tmp
, *tmp2
;
1034 static const char *prev_line
;
1035 static const char *prev_color
;
1036 unsigned int offset
;
1042 if (getline(&line
, &line_len
, file
) < 0)
1047 c
= strchr(line
, '\n');
1056 * Strip leading spaces:
1067 * Parse hexa addresses followed by ':'
1069 line_ip
= strtoull(tmp
, &tmp2
, 16);
1074 if (line_ip
!= -1) {
1075 const char *path
= NULL
;
1076 unsigned int hits
= 0;
1077 double percent
= 0.0;
1079 struct sym_ext
*sym_ext
= sym
->priv
;
1081 offset
= line_ip
- start
;
1083 hits
= sym
->hist
[offset
];
1085 if (offset
< len
&& sym_ext
) {
1086 path
= sym_ext
[offset
].path
;
1087 percent
= sym_ext
[offset
].percent
;
1088 } else if (sym
->hist_sum
)
1089 percent
= 100.0 * hits
/ sym
->hist_sum
;
1091 color
= get_percent_color(percent
);
1094 * Also color the filename and line if needed, with
1095 * the same color than the percentage. Don't print it
1096 * twice for close colored ip with the same filename:line
1099 if (!prev_line
|| strcmp(prev_line
, path
)
1100 || color
!= prev_color
) {
1101 color_fprintf(stdout
, color
, " %s", path
);
1107 color_fprintf(stdout
, color
, " %7.2f", percent
);
1109 color_fprintf(stdout
, PERF_COLOR_BLUE
, "%s\n", line
);
1114 printf(" : %s\n", line
);
1120 static struct rb_root root_sym_ext
;
1122 static void insert_source_line(struct sym_ext
*sym_ext
)
1124 struct sym_ext
*iter
;
1125 struct rb_node
**p
= &root_sym_ext
.rb_node
;
1126 struct rb_node
*parent
= NULL
;
1128 while (*p
!= NULL
) {
1130 iter
= rb_entry(parent
, struct sym_ext
, node
);
1132 if (sym_ext
->percent
> iter
->percent
)
1135 p
= &(*p
)->rb_right
;
1138 rb_link_node(&sym_ext
->node
, parent
, p
);
1139 rb_insert_color(&sym_ext
->node
, &root_sym_ext
);
1142 static void free_source_line(struct symbol
*sym
, int len
)
1144 struct sym_ext
*sym_ext
= sym
->priv
;
1150 for (i
= 0; i
< len
; i
++)
1151 free(sym_ext
[i
].path
);
1155 root_sym_ext
= RB_ROOT
;
1158 /* Get the filename:line for the colored entries */
1160 get_source_line(struct symbol
*sym
, u64 start
, int len
, char *filename
)
1163 char cmd
[PATH_MAX
* 2];
1164 struct sym_ext
*sym_ext
;
1169 sym
->priv
= calloc(len
, sizeof(struct sym_ext
));
1173 sym_ext
= sym
->priv
;
1175 for (i
= 0; i
< len
; i
++) {
1181 sym_ext
[i
].percent
= 100.0 * sym
->hist
[i
] / sym
->hist_sum
;
1182 if (sym_ext
[i
].percent
<= 0.5)
1186 sprintf(cmd
, "addr2line -e %s %016llx", filename
, offset
);
1187 fp
= popen(cmd
, "r");
1191 if (getline(&path
, &line_len
, fp
) < 0 || !line_len
)
1194 sym_ext
[i
].path
= malloc(sizeof(char) * line_len
+ 1);
1195 if (!sym_ext
[i
].path
)
1198 strcpy(sym_ext
[i
].path
, path
);
1199 insert_source_line(&sym_ext
[i
]);
1206 static void print_summary(char *filename
)
1208 struct sym_ext
*sym_ext
;
1209 struct rb_node
*node
;
1211 printf("\nSorted summary for file %s\n", filename
);
1212 printf("----------------------------------------------\n\n");
1214 if (RB_EMPTY_ROOT(&root_sym_ext
)) {
1215 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN
);
1219 node
= rb_first(&root_sym_ext
);
1225 sym_ext
= rb_entry(node
, struct sym_ext
, node
);
1226 percent
= sym_ext
->percent
;
1227 color
= get_percent_color(percent
);
1228 path
= sym_ext
->path
;
1230 color_fprintf(stdout
, color
, " %7.2f %s", percent
, path
);
1231 node
= rb_next(node
);
1235 static void annotate_sym(struct dso
*dso
, struct symbol
*sym
)
1237 char *filename
= dso
->name
, *d_filename
;
1238 u64 start
, end
, len
;
1239 char command
[PATH_MAX
*2];
1245 filename
= sym
->module
->path
;
1246 else if (dso
== kernel_dso
)
1249 start
= sym
->obj_start
;
1253 d_filename
= filename
;
1255 d_filename
= basename(filename
);
1257 end
= start
+ sym
->end
- sym
->start
+ 1;
1258 len
= sym
->end
- sym
->start
;
1261 get_source_line(sym
, start
, len
, filename
);
1262 print_summary(filename
);
1265 printf("\n\n------------------------------------------------\n");
1266 printf(" Percent | Source code & Disassembly of %s\n", d_filename
);
1267 printf("------------------------------------------------\n");
1270 printf("annotating [%p] %30s : [%p] %30s\n", dso
, dso
->name
, sym
, sym
->name
);
1272 sprintf(command
, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
1273 (u64
)start
, (u64
)end
, filename
, filename
);
1276 printf("doing: %s\n", command
);
1278 file
= popen(command
, "r");
1282 while (!feof(file
)) {
1283 if (parse_line(file
, sym
, start
, len
) < 0)
1289 free_source_line(sym
, len
);
1292 static void find_annotations(void)
1298 list_for_each_entry(dso
, &dsos
, node
) {
1300 for (nd
= rb_first(&dso
->syms
); nd
; nd
= rb_next(nd
)) {
1301 struct symbol
*sym
= rb_entry(nd
, struct symbol
, rb_node
);
1304 annotate_sym(dso
, sym
);
1311 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter
);
1314 static int __cmd_annotate(void)
1316 int ret
, rc
= EXIT_FAILURE
;
1317 unsigned long offset
= 0;
1318 unsigned long head
= 0;
1324 register_idle_thread();
1326 input
= open(input_name
, O_RDONLY
);
1328 perror("failed to open file");
1332 ret
= fstat(input
, &stat
);
1334 perror("failed to stat file");
1338 if (!force
&& stat
.st_uid
&& (stat
.st_uid
!= geteuid())) {
1339 fprintf(stderr
, "file: %s not owned by current user or root\n", input_name
);
1343 if (!stat
.st_size
) {
1344 fprintf(stderr
, "zero-sized file, nothing to do!\n");
1348 if (load_kernel() < 0) {
1349 perror("failed to load kernel symbols");
1350 return EXIT_FAILURE
;
1354 buf
= (char *)mmap(NULL
, page_size
* mmap_window
, PROT_READ
,
1355 MAP_SHARED
, input
, offset
);
1356 if (buf
== MAP_FAILED
) {
1357 perror("failed to mmap file");
1362 event
= (event_t
*)(buf
+ head
);
1364 size
= event
->header
.size
;
1368 if (head
+ event
->header
.size
>= page_size
* mmap_window
) {
1369 unsigned long shift
= page_size
* (head
/ page_size
);
1372 ret
= munmap(buf
, page_size
* mmap_window
);
1380 size
= event
->header
.size
;
1382 dprintf("%p [%p]: event: %d\n",
1383 (void *)(offset
+ head
),
1384 (void *)(long)event
->header
.size
,
1385 event
->header
.type
);
1387 if (!size
|| process_event(event
, offset
, head
) < 0) {
1389 dprintf("%p [%p]: skipping unknown header type: %d\n",
1390 (void *)(offset
+ head
),
1391 (void *)(long)(event
->header
.size
),
1392 event
->header
.type
);
1397 * assume we lost track of the stream, check alignment, and
1398 * increment a single u64 in the hope to catch on again 'soon'.
1401 if (unlikely(head
& 7))
1409 if (offset
+ head
< (unsigned long)stat
.st_size
)
1415 dprintf(" IP events: %10ld\n", total
);
1416 dprintf(" mmap events: %10ld\n", total_mmap
);
1417 dprintf(" comm events: %10ld\n", total_comm
);
1418 dprintf(" fork events: %10ld\n", total_fork
);
1419 dprintf(" unknown events: %10ld\n", total_unknown
);
1425 threads__fprintf(stdout
);
1428 dsos__fprintf(stdout
);
1438 static const char * const annotate_usage
[] = {
1439 "perf annotate [<options>] <command>",
1443 static const struct option options
[] = {
1444 OPT_STRING('i', "input", &input_name
, "file",
1446 OPT_STRING('s', "symbol", &sym_hist_filter
, "symbol",
1447 "symbol to annotate"),
1448 OPT_BOOLEAN('f', "force", &force
, "don't complain, do it"),
1449 OPT_BOOLEAN('v', "verbose", &verbose
,
1450 "be more verbose (show symbol address, etc)"),
1451 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
1452 "dump raw trace in ASCII"),
1453 OPT_STRING('k', "vmlinux", &vmlinux
, "file", "vmlinux pathname"),
1454 OPT_BOOLEAN('m', "modules", &modules
,
1455 "load module symbols - WARNING: use only with -k and LIVE kernel"),
1456 OPT_BOOLEAN('l', "print-line", &print_line
,
1457 "print matching source lines (may be slow)"),
1458 OPT_BOOLEAN('P', "full-paths", &full_paths
,
1459 "Don't shorten the displayed pathnames"),
1463 static void setup_sorting(void)
1465 char *tmp
, *tok
, *str
= strdup(sort_order
);
1467 for (tok
= strtok_r(str
, ", ", &tmp
);
1468 tok
; tok
= strtok_r(NULL
, ", ", &tmp
)) {
1469 if (sort_dimension__add(tok
) < 0) {
1470 error("Unknown --sort key: `%s'", tok
);
1471 usage_with_options(annotate_usage
, options
);
1478 int cmd_annotate(int argc
, const char **argv
, const char *prefix __used
)
1482 page_size
= getpagesize();
1484 argc
= parse_options(argc
, argv
, options
, annotate_usage
, 0);
1490 * Special case: if there's an argument left then assume tha
1491 * it's a symbol filter:
1494 usage_with_options(annotate_usage
, options
);
1496 sym_hist_filter
= argv
[0];
1499 if (!sym_hist_filter
)
1500 usage_with_options(annotate_usage
, options
);
1504 return __cmd_annotate();