perf session: Pass the perf_session to the event handling operations
[deliverable/linux.git] / tools / perf / builtin-trace.c
1 #include "builtin.h"
2
3 #include "util/util.h"
4 #include "util/cache.h"
5 #include "util/symbol.h"
6 #include "util/thread.h"
7 #include "util/header.h"
8 #include "util/exec_cmd.h"
9 #include "util/trace-event.h"
10 #include "util/session.h"
11
12 static char const *script_name;
13 static char const *generate_script_lang;
14
15 static int default_start_script(const char *script __attribute((unused)))
16 {
17 return 0;
18 }
19
20 static int default_stop_script(void)
21 {
22 return 0;
23 }
24
25 static int default_generate_script(const char *outfile __attribute ((unused)))
26 {
27 return 0;
28 }
29
30 static struct scripting_ops default_scripting_ops = {
31 .start_script = default_start_script,
32 .stop_script = default_stop_script,
33 .process_event = print_event,
34 .generate_script = default_generate_script,
35 };
36
37 static struct scripting_ops *scripting_ops;
38
39 static void setup_scripting(void)
40 {
41 /* make sure PERF_EXEC_PATH is set for scripts */
42 perf_set_argv_exec_path(perf_exec_path());
43
44 setup_perl_scripting();
45
46 scripting_ops = &default_scripting_ops;
47 }
48
49 static int cleanup_scripting(void)
50 {
51 return scripting_ops->stop_script();
52 }
53
54 #include "util/parse-options.h"
55
56 #include "perf.h"
57 #include "util/debug.h"
58
59 #include "util/trace-event.h"
60 #include "util/data_map.h"
61 #include "util/exec_cmd.h"
62
63 static char const *input_name = "perf.data";
64
65 static u64 sample_type;
66
67 static int process_sample_event(event_t *event, struct perf_session *session __used)
68 {
69 struct sample_data data;
70 struct thread *thread;
71
72 memset(&data, 0, sizeof(data));
73 data.time = -1;
74 data.cpu = -1;
75 data.period = 1;
76
77 event__parse_sample(event, sample_type, &data);
78
79 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
80 event->header.misc,
81 data.pid, data.tid,
82 (void *)(long)data.ip,
83 (long long)data.period);
84
85 thread = threads__findnew(event->ip.pid);
86 if (thread == NULL) {
87 pr_debug("problem processing %d event, skipping it.\n",
88 event->header.type);
89 return -1;
90 }
91
92 if (sample_type & PERF_SAMPLE_RAW) {
93 /*
94 * FIXME: better resolve from pid from the struct trace_entry
95 * field, although it should be the same than this perf
96 * event pid
97 */
98 scripting_ops->process_event(data.cpu, data.raw_data,
99 data.raw_size,
100 data.time, thread->comm);
101 }
102 event__stats.total += data.period;
103
104 return 0;
105 }
106
107 static int sample_type_check(u64 type)
108 {
109 sample_type = type;
110
111 if (!(sample_type & PERF_SAMPLE_RAW)) {
112 fprintf(stderr,
113 "No trace sample to read. Did you call perf record "
114 "without -R?");
115 return -1;
116 }
117
118 return 0;
119 }
120
121 static struct perf_file_handler file_handler = {
122 .process_sample_event = process_sample_event,
123 .process_comm_event = event__process_comm,
124 .sample_type_check = sample_type_check,
125 };
126
127 static int __cmd_trace(struct perf_session *session)
128 {
129 register_idle_thread();
130 register_perf_file_handler(&file_handler);
131
132 return perf_session__process_events(session, 0, &event__cwdlen, &event__cwd);
133 }
134
135 struct script_spec {
136 struct list_head node;
137 struct scripting_ops *ops;
138 char spec[0];
139 };
140
141 LIST_HEAD(script_specs);
142
143 static struct script_spec *script_spec__new(const char *spec,
144 struct scripting_ops *ops)
145 {
146 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
147
148 if (s != NULL) {
149 strcpy(s->spec, spec);
150 s->ops = ops;
151 }
152
153 return s;
154 }
155
156 static void script_spec__delete(struct script_spec *s)
157 {
158 free(s->spec);
159 free(s);
160 }
161
162 static void script_spec__add(struct script_spec *s)
163 {
164 list_add_tail(&s->node, &script_specs);
165 }
166
167 static struct script_spec *script_spec__find(const char *spec)
168 {
169 struct script_spec *s;
170
171 list_for_each_entry(s, &script_specs, node)
172 if (strcasecmp(s->spec, spec) == 0)
173 return s;
174 return NULL;
175 }
176
177 static struct script_spec *script_spec__findnew(const char *spec,
178 struct scripting_ops *ops)
179 {
180 struct script_spec *s = script_spec__find(spec);
181
182 if (s)
183 return s;
184
185 s = script_spec__new(spec, ops);
186 if (!s)
187 goto out_delete_spec;
188
189 script_spec__add(s);
190
191 return s;
192
193 out_delete_spec:
194 script_spec__delete(s);
195
196 return NULL;
197 }
198
199 int script_spec_register(const char *spec, struct scripting_ops *ops)
200 {
201 struct script_spec *s;
202
203 s = script_spec__find(spec);
204 if (s)
205 return -1;
206
207 s = script_spec__findnew(spec, ops);
208 if (!s)
209 return -1;
210
211 return 0;
212 }
213
214 static struct scripting_ops *script_spec__lookup(const char *spec)
215 {
216 struct script_spec *s = script_spec__find(spec);
217 if (!s)
218 return NULL;
219
220 return s->ops;
221 }
222
223 static void list_available_languages(void)
224 {
225 struct script_spec *s;
226
227 fprintf(stderr, "\n");
228 fprintf(stderr, "Scripting language extensions (used in "
229 "perf trace -s [spec:]script.[spec]):\n\n");
230
231 list_for_each_entry(s, &script_specs, node)
232 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
233
234 fprintf(stderr, "\n");
235 }
236
237 static int parse_scriptname(const struct option *opt __used,
238 const char *str, int unset __used)
239 {
240 char spec[PATH_MAX];
241 const char *script, *ext;
242 int len;
243
244 if (strcmp(str, "list") == 0) {
245 list_available_languages();
246 return 0;
247 }
248
249 script = strchr(str, ':');
250 if (script) {
251 len = script - str;
252 if (len >= PATH_MAX) {
253 fprintf(stderr, "invalid language specifier");
254 return -1;
255 }
256 strncpy(spec, str, len);
257 spec[len] = '\0';
258 scripting_ops = script_spec__lookup(spec);
259 if (!scripting_ops) {
260 fprintf(stderr, "invalid language specifier");
261 return -1;
262 }
263 script++;
264 } else {
265 script = str;
266 ext = strchr(script, '.');
267 if (!ext) {
268 fprintf(stderr, "invalid script extension");
269 return -1;
270 }
271 scripting_ops = script_spec__lookup(++ext);
272 if (!scripting_ops) {
273 fprintf(stderr, "invalid script extension");
274 return -1;
275 }
276 }
277
278 script_name = strdup(script);
279
280 return 0;
281 }
282
283 static const char * const annotate_usage[] = {
284 "perf trace [<options>] <command>",
285 NULL
286 };
287
288 static const struct option options[] = {
289 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
290 "dump raw trace in ASCII"),
291 OPT_BOOLEAN('v', "verbose", &verbose,
292 "be more verbose (show symbol address, etc)"),
293 OPT_BOOLEAN('l', "latency", &latency_format,
294 "show latency attributes (irqs/preemption disabled, etc)"),
295 OPT_CALLBACK('s', "script", NULL, "name",
296 "script file name (lang:script name, script name, or *)",
297 parse_scriptname),
298 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
299 "generate perf-trace.xx script in specified language"),
300
301 OPT_END()
302 };
303
304 int cmd_trace(int argc, const char **argv, const char *prefix __used)
305 {
306 int err;
307 struct perf_session *session;
308
309 symbol__init(0);
310
311 setup_scripting();
312
313 argc = parse_options(argc, argv, options, annotate_usage, 0);
314 if (argc) {
315 /*
316 * Special case: if there's an argument left then assume tha
317 * it's a symbol filter:
318 */
319 if (argc > 1)
320 usage_with_options(annotate_usage, options);
321 }
322
323 setup_pager();
324
325 session = perf_session__new(input_name, O_RDONLY, 0);
326 if (session == NULL)
327 return -ENOMEM;
328
329 if (generate_script_lang) {
330 struct stat perf_stat;
331
332 int input = open(input_name, O_RDONLY);
333 if (input < 0) {
334 perror("failed to open file");
335 exit(-1);
336 }
337
338 err = fstat(input, &perf_stat);
339 if (err < 0) {
340 perror("failed to stat file");
341 exit(-1);
342 }
343
344 if (!perf_stat.st_size) {
345 fprintf(stderr, "zero-sized file, nothing to do!\n");
346 exit(0);
347 }
348
349 scripting_ops = script_spec__lookup(generate_script_lang);
350 if (!scripting_ops) {
351 fprintf(stderr, "invalid language specifier");
352 return -1;
353 }
354
355 perf_header__read(&session->header, input);
356 err = scripting_ops->generate_script("perf-trace");
357 goto out;
358 }
359
360 if (script_name) {
361 err = scripting_ops->start_script(script_name);
362 if (err)
363 goto out;
364 }
365
366 err = __cmd_trace(session);
367
368 perf_session__delete(session);
369 cleanup_scripting();
370 out:
371 return err;
372 }
This page took 0.078381 seconds and 5 git commands to generate.