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