Commit | Line | Data |
---|---|---|
8035e428 IM |
1 | /* |
2 | * builtin-annotate.c | |
3 | * | |
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. | |
7 | */ | |
8 | #include "builtin.h" | |
9 | ||
10 | #include "util/util.h" | |
11 | ||
78f7defe | 12 | #include "util/util.h" |
8035e428 | 13 | #include "util/color.h" |
5da50258 | 14 | #include <linux/list.h> |
8035e428 | 15 | #include "util/cache.h" |
43cbcd8a | 16 | #include <linux/rbtree.h> |
8035e428 | 17 | #include "util/symbol.h" |
8035e428 IM |
18 | |
19 | #include "perf.h" | |
8f28827a | 20 | #include "util/debug.h" |
8035e428 | 21 | |
78f7defe | 22 | #include "util/annotate.h" |
62daacb5 | 23 | #include "util/event.h" |
8035e428 IM |
24 | #include "util/parse-options.h" |
25 | #include "util/parse-events.h" | |
6baa0a5a | 26 | #include "util/thread.h" |
dd68ada2 | 27 | #include "util/sort.h" |
3d1d07ec | 28 | #include "util/hist.h" |
94c744b6 | 29 | #include "util/session.h" |
8035e428 | 30 | |
8035e428 | 31 | static char const *input_name = "perf.data"; |
8035e428 | 32 | |
8b9e74eb | 33 | static bool force, use_tui, use_stdio; |
8035e428 | 34 | |
c0555642 | 35 | static bool full_paths; |
42976487 | 36 | |
c0555642 | 37 | static bool print_line; |
301406b9 | 38 | |
e4204992 ACM |
39 | static const char *sym_hist_filter; |
40 | ||
1c02c4d2 | 41 | static int hists__add_entry(struct hists *self, struct addr_location *al) |
8035e428 | 42 | { |
628ada0c ACM |
43 | struct hist_entry *he; |
44 | ||
45 | if (sym_hist_filter != NULL && | |
46 | (al->sym == NULL || strcmp(sym_hist_filter, al->sym->name) != 0)) { | |
47 | /* We're only interested in a symbol named sym_hist_filter */ | |
48 | if (al->sym != NULL) { | |
49 | rb_erase(&al->sym->rb_node, | |
50 | &al->map->dso->symbols[al->map->type]); | |
51 | symbol__delete(al->sym); | |
52 | } | |
53 | return 0; | |
54 | } | |
55 | ||
1c02c4d2 | 56 | he = __hists__add_entry(self, al, NULL, 1); |
9735abf1 | 57 | if (he == NULL) |
8035e428 | 58 | return -ENOMEM; |
628ada0c | 59 | |
2f525d01 ACM |
60 | if (he->ms.sym != NULL) { |
61 | /* | |
62 | * All aggregated on the first sym_hist. | |
63 | */ | |
64 | struct annotation *notes = symbol__annotation(he->ms.sym); | |
65 | if (notes->histograms == NULL && symbol__alloc_hist(he->ms.sym, 1) < 0) | |
66 | return -ENOMEM; | |
67 | ||
68 | return hist_entry__inc_addr_samples(he, 0, al->addr); | |
69 | } | |
70 | ||
71 | return 0; | |
8035e428 IM |
72 | } |
73 | ||
8115d60c ACM |
74 | static int process_sample_event(union perf_event *event, |
75 | struct perf_sample *sample, | |
640c03ce | 76 | struct perf_session *session) |
8035e428 | 77 | { |
1ed091c4 | 78 | struct addr_location al; |
6baa0a5a | 79 | |
8115d60c | 80 | if (perf_event__preprocess_sample(event, session, &al, sample, NULL) < 0) { |
29a9f66d ACM |
81 | pr_warning("problem processing %d event, skipping it.\n", |
82 | event->header.type); | |
8035e428 IM |
83 | return -1; |
84 | } | |
85 | ||
1c02c4d2 | 86 | if (!al.filtered && hists__add_entry(&session->hists, &al)) { |
29a9f66d ACM |
87 | pr_warning("problem incrementing symbol count, " |
88 | "skipping event\n"); | |
ec218fc4 | 89 | return -1; |
8035e428 | 90 | } |
8035e428 IM |
91 | |
92 | return 0; | |
93 | } | |
94 | ||
2f525d01 | 95 | static int hist_entry__tty_annotate(struct hist_entry *he, int evidx) |
0b73da3f | 96 | { |
2f525d01 | 97 | return symbol__tty_annotate(he->ms.sym, he->ms.map, evidx, |
78f7defe | 98 | print_line, full_paths); |
0b73da3f IM |
99 | } |
100 | ||
1c02c4d2 | 101 | static void hists__find_annotations(struct hists *self) |
0b73da3f | 102 | { |
b50e003d | 103 | struct rb_node *nd = rb_first(&self->entries), *next; |
46e3e055 | 104 | int key = KEY_RIGHT; |
0b73da3f | 105 | |
46e3e055 | 106 | while (nd) { |
ed52ce2e | 107 | struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node); |
78f7defe | 108 | struct annotation *notes; |
0b73da3f | 109 | |
46e3e055 ACM |
110 | if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned) |
111 | goto find_next; | |
0b73da3f | 112 | |
78f7defe | 113 | notes = symbol__annotation(he->ms.sym); |
2f525d01 | 114 | if (notes->histograms == NULL) { |
46e3e055 ACM |
115 | find_next: |
116 | if (key == KEY_LEFT) | |
117 | nd = rb_prev(nd); | |
118 | else | |
119 | nd = rb_next(nd); | |
e4204992 | 120 | continue; |
46e3e055 | 121 | } |
e4204992 | 122 | |
62e3436b | 123 | if (use_browser > 0) { |
2f525d01 ACM |
124 | /* For now all is aggregated on the first */ |
125 | key = hist_entry__tui_annotate(he, 0); | |
46e3e055 ACM |
126 | switch (key) { |
127 | case KEY_RIGHT: | |
b50e003d | 128 | next = rb_next(nd); |
46e3e055 ACM |
129 | break; |
130 | case KEY_LEFT: | |
b50e003d | 131 | next = rb_prev(nd); |
46e3e055 | 132 | break; |
b50e003d ACM |
133 | default: |
134 | return; | |
46e3e055 | 135 | } |
b50e003d ACM |
136 | |
137 | if (next != NULL) | |
138 | nd = next; | |
46e3e055 | 139 | } else { |
2f525d01 ACM |
140 | /* For now all is aggregated on the first */ |
141 | hist_entry__tty_annotate(he, 0); | |
46e3e055 ACM |
142 | nd = rb_next(nd); |
143 | /* | |
144 | * Since we have a hist_entry per IP for the same | |
78f7defe | 145 | * symbol, free he->ms.sym->histogram to signal we already |
46e3e055 ACM |
146 | * processed this symbol. |
147 | */ | |
2f525d01 ACM |
148 | free(notes->histograms); |
149 | notes->histograms = NULL; | |
46e3e055 | 150 | } |
0b73da3f | 151 | } |
0b73da3f IM |
152 | } |
153 | ||
301a0b02 | 154 | static struct perf_event_ops event_ops = { |
55aa640f | 155 | .sample = process_sample_event, |
8115d60c ACM |
156 | .mmap = perf_event__process_mmap, |
157 | .comm = perf_event__process_comm, | |
158 | .fork = perf_event__process_task, | |
eac23d1c IM |
159 | .ordered_samples = true, |
160 | .ordering_requires_timestamps = true, | |
bab81b62 LZ |
161 | }; |
162 | ||
8035e428 IM |
163 | static int __cmd_annotate(void) |
164 | { | |
bab81b62 | 165 | int ret; |
75be6cf4 | 166 | struct perf_session *session; |
8035e428 | 167 | |
21ef97f0 | 168 | session = perf_session__new(input_name, O_RDONLY, force, false, &event_ops); |
94c744b6 ACM |
169 | if (session == NULL) |
170 | return -ENOMEM; | |
171 | ||
ec913369 | 172 | ret = perf_session__process_events(session, &event_ops); |
bab81b62 | 173 | if (ret) |
94c744b6 | 174 | goto out_delete; |
8035e428 | 175 | |
62daacb5 | 176 | if (dump_trace) { |
c8446b9b | 177 | perf_session__fprintf_nr_events(session, stdout); |
94c744b6 | 178 | goto out_delete; |
62daacb5 | 179 | } |
8035e428 | 180 | |
da21d1b5 | 181 | if (verbose > 3) |
b3165f41 | 182 | perf_session__fprintf(session, stdout); |
8035e428 | 183 | |
da21d1b5 | 184 | if (verbose > 2) |
cbf69680 | 185 | perf_session__fprintf_dsos(session, stdout); |
8035e428 | 186 | |
1c02c4d2 ACM |
187 | hists__collapse_resort(&session->hists); |
188 | hists__output_resort(&session->hists); | |
189 | hists__find_annotations(&session->hists); | |
94c744b6 ACM |
190 | out_delete: |
191 | perf_session__delete(session); | |
8035e428 | 192 | |
bab81b62 | 193 | return ret; |
8035e428 IM |
194 | } |
195 | ||
196 | static const char * const annotate_usage[] = { | |
197 | "perf annotate [<options>] <command>", | |
198 | NULL | |
199 | }; | |
200 | ||
201 | static const struct option options[] = { | |
202 | OPT_STRING('i', "input", &input_name, "file", | |
203 | "input file name"), | |
ac73c5a9 ACM |
204 | OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]", |
205 | "only consider symbols in these dsos"), | |
23b87116 | 206 | OPT_STRING('s', "symbol", &sym_hist_filter, "symbol", |
0b73da3f | 207 | "symbol to annotate"), |
fa6963b2 | 208 | OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), |
c0555642 | 209 | OPT_INCR('v', "verbose", &verbose, |
8035e428 IM |
210 | "be more verbose (show symbol address, etc)"), |
211 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, | |
212 | "dump raw trace in ASCII"), | |
8b9e74eb ACM |
213 | OPT_BOOLEAN(0, "tui", &use_tui, "Use the TUI interface"), |
214 | OPT_BOOLEAN(0, "stdio", &use_stdio, "Use the stdio interface"), | |
b32d133a ACM |
215 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, |
216 | "file", "vmlinux pathname"), | |
217 | OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, | |
42976487 | 218 | "load module symbols - WARNING: use only with -k and LIVE kernel"), |
301406b9 FW |
219 | OPT_BOOLEAN('l', "print-line", &print_line, |
220 | "print matching source lines (may be slow)"), | |
42976487 MG |
221 | OPT_BOOLEAN('P', "full-paths", &full_paths, |
222 | "Don't shorten the displayed pathnames"), | |
8035e428 IM |
223 | OPT_END() |
224 | }; | |
225 | ||
f37a291c | 226 | int cmd_annotate(int argc, const char **argv, const char *prefix __used) |
8035e428 | 227 | { |
655000e7 ACM |
228 | argc = parse_options(argc, argv, options, annotate_usage, 0); |
229 | ||
8b9e74eb ACM |
230 | if (use_stdio) |
231 | use_browser = 0; | |
232 | else if (use_tui) | |
233 | use_browser = 1; | |
234 | ||
229ade9b | 235 | setup_browser(true); |
46e3e055 | 236 | |
78f7defe | 237 | symbol_conf.priv_size = sizeof(struct annotation); |
75be6cf4 ACM |
238 | symbol_conf.try_vmlinux_path = true; |
239 | ||
240 | if (symbol__init() < 0) | |
b32d133a | 241 | return -1; |
8035e428 | 242 | |
c8829c7a | 243 | setup_sorting(annotate_usage, options); |
8035e428 | 244 | |
0b73da3f IM |
245 | if (argc) { |
246 | /* | |
247 | * Special case: if there's an argument left then assume tha | |
248 | * it's a symbol filter: | |
249 | */ | |
250 | if (argc > 1) | |
251 | usage_with_options(annotate_usage, options); | |
252 | ||
253 | sym_hist_filter = argv[0]; | |
254 | } | |
255 | ||
dd68ada2 | 256 | if (field_sep && *field_sep == '.') { |
29a9f66d ACM |
257 | pr_err("'.' is the only non valid --field-separator argument\n"); |
258 | return -1; | |
dd68ada2 JK |
259 | } |
260 | ||
8035e428 IM |
261 | return __cmd_annotate(); |
262 | } |