blob: b7eb3fcc224e147605561db59f6c77cfb0bc24b2 [file] [log] [blame]
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001#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"
Ingo Molnarcf723442009-11-28 10:11:00 +01008#include "util/exec_cmd.h"
9#include "util/trace-event.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020010#include "util/session.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020011
Tom Zanussi956ffd02009-11-25 01:15:46 -060012static char const *script_name;
13static char const *generate_script_lang;
14
15static int default_start_script(const char *script __attribute((unused)))
16{
17 return 0;
18}
19
20static int default_stop_script(void)
21{
22 return 0;
23}
24
25static int default_generate_script(const char *outfile __attribute ((unused)))
26{
27 return 0;
28}
29
30static 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
37static struct scripting_ops *scripting_ops;
38
39static 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
Tom Zanussi16c632d2009-11-25 01:15:48 -060044 setup_perl_scripting();
45
Tom Zanussi956ffd02009-11-25 01:15:46 -060046 scripting_ops = &default_scripting_ops;
47}
48
49static int cleanup_scripting(void)
50{
51 return scripting_ops->stop_script();
52}
53
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020054#include "util/parse-options.h"
55
56#include "perf.h"
57#include "util/debug.h"
58
59#include "util/trace-event.h"
Tom Zanussi956ffd02009-11-25 01:15:46 -060060#include "util/exec_cmd.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020061
Tom Zanussi956ffd02009-11-25 01:15:46 -060062static char const *input_name = "perf.data";
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020063
Tom Zanussi956ffd02009-11-25 01:15:46 -060064static u64 sample_type;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020065
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -020066static int process_sample_event(event_t *event, struct perf_session *session __used)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020067{
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090068 struct sample_data data;
69 struct thread *thread;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020070
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090071 memset(&data, 0, sizeof(data));
72 data.time = -1;
73 data.cpu = -1;
74 data.period = 1;
Ingo Molnar6ddf2592009-09-03 12:00:22 +020075
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090076 event__parse_sample(event, sample_type, &data);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020077
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -020078 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020079 event->header.misc,
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090080 data.pid, data.tid,
81 (void *)(long)data.ip,
82 (long long)data.period);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020083
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090084 thread = threads__findnew(event->ip.pid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020085 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -020086 pr_debug("problem processing %d event, skipping it.\n",
87 event->header.type);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020088 return -1;
89 }
90
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020091 if (sample_type & PERF_SAMPLE_RAW) {
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020092 /*
93 * FIXME: better resolve from pid from the struct trace_entry
94 * field, although it should be the same than this perf
95 * event pid
96 */
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090097 scripting_ops->process_event(data.cpu, data.raw_data,
98 data.raw_size,
99 data.time, thread->comm);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200100 }
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900101 event__stats.total += data.period;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200102
103 return 0;
104}
105
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200106static int sample_type_check(u64 type)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200107{
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200108 sample_type = type;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200109
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200110 if (!(sample_type & PERF_SAMPLE_RAW)) {
111 fprintf(stderr,
112 "No trace sample to read. Did you call perf record "
113 "without -R?");
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200114 return -1;
115 }
116
117 return 0;
118}
119
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -0200120static struct perf_event_ops event_ops = {
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200121 .process_sample_event = process_sample_event,
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200122 .process_comm_event = event__process_comm,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200123 .sample_type_check = sample_type_check,
124};
125
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200126static int __cmd_trace(struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200127{
Arnaldo Carvalho de Meloec913362009-12-13 19:50:27 -0200128 return perf_session__process_events(session, &event_ops);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200129}
130
Tom Zanussi956ffd02009-11-25 01:15:46 -0600131struct script_spec {
132 struct list_head node;
133 struct scripting_ops *ops;
134 char spec[0];
135};
136
137LIST_HEAD(script_specs);
138
139static struct script_spec *script_spec__new(const char *spec,
140 struct scripting_ops *ops)
141{
142 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
143
144 if (s != NULL) {
145 strcpy(s->spec, spec);
146 s->ops = ops;
147 }
148
149 return s;
150}
151
152static void script_spec__delete(struct script_spec *s)
153{
154 free(s->spec);
155 free(s);
156}
157
158static void script_spec__add(struct script_spec *s)
159{
160 list_add_tail(&s->node, &script_specs);
161}
162
163static struct script_spec *script_spec__find(const char *spec)
164{
165 struct script_spec *s;
166
167 list_for_each_entry(s, &script_specs, node)
168 if (strcasecmp(s->spec, spec) == 0)
169 return s;
170 return NULL;
171}
172
173static struct script_spec *script_spec__findnew(const char *spec,
174 struct scripting_ops *ops)
175{
176 struct script_spec *s = script_spec__find(spec);
177
178 if (s)
179 return s;
180
181 s = script_spec__new(spec, ops);
182 if (!s)
183 goto out_delete_spec;
184
185 script_spec__add(s);
186
187 return s;
188
189out_delete_spec:
190 script_spec__delete(s);
191
192 return NULL;
193}
194
195int script_spec_register(const char *spec, struct scripting_ops *ops)
196{
197 struct script_spec *s;
198
199 s = script_spec__find(spec);
200 if (s)
201 return -1;
202
203 s = script_spec__findnew(spec, ops);
204 if (!s)
205 return -1;
206
207 return 0;
208}
209
210static struct scripting_ops *script_spec__lookup(const char *spec)
211{
212 struct script_spec *s = script_spec__find(spec);
213 if (!s)
214 return NULL;
215
216 return s->ops;
217}
218
219static void list_available_languages(void)
220{
221 struct script_spec *s;
222
223 fprintf(stderr, "\n");
224 fprintf(stderr, "Scripting language extensions (used in "
225 "perf trace -s [spec:]script.[spec]):\n\n");
226
227 list_for_each_entry(s, &script_specs, node)
228 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
229
230 fprintf(stderr, "\n");
231}
232
233static int parse_scriptname(const struct option *opt __used,
234 const char *str, int unset __used)
235{
236 char spec[PATH_MAX];
237 const char *script, *ext;
238 int len;
239
240 if (strcmp(str, "list") == 0) {
241 list_available_languages();
242 return 0;
243 }
244
245 script = strchr(str, ':');
246 if (script) {
247 len = script - str;
248 if (len >= PATH_MAX) {
249 fprintf(stderr, "invalid language specifier");
250 return -1;
251 }
252 strncpy(spec, str, len);
253 spec[len] = '\0';
254 scripting_ops = script_spec__lookup(spec);
255 if (!scripting_ops) {
256 fprintf(stderr, "invalid language specifier");
257 return -1;
258 }
259 script++;
260 } else {
261 script = str;
262 ext = strchr(script, '.');
263 if (!ext) {
264 fprintf(stderr, "invalid script extension");
265 return -1;
266 }
267 scripting_ops = script_spec__lookup(++ext);
268 if (!scripting_ops) {
269 fprintf(stderr, "invalid script extension");
270 return -1;
271 }
272 }
273
274 script_name = strdup(script);
275
276 return 0;
277}
278
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200279static const char * const annotate_usage[] = {
280 "perf trace [<options>] <command>",
281 NULL
282};
283
284static const struct option options[] = {
285 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
286 "dump raw trace in ASCII"),
287 OPT_BOOLEAN('v', "verbose", &verbose,
288 "be more verbose (show symbol address, etc)"),
Steven Rostedtcda48462009-10-14 15:43:42 -0400289 OPT_BOOLEAN('l', "latency", &latency_format,
290 "show latency attributes (irqs/preemption disabled, etc)"),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600291 OPT_CALLBACK('s', "script", NULL, "name",
292 "script file name (lang:script name, script name, or *)",
293 parse_scriptname),
294 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
295 "generate perf-trace.xx script in specified language"),
296
Masami Hiramatsu19096292009-08-21 14:56:03 -0400297 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200298};
299
300int cmd_trace(int argc, const char **argv, const char *prefix __used)
301{
Tom Zanussi956ffd02009-11-25 01:15:46 -0600302 int err;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200303 struct perf_session *session;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600304
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200305 symbol__init(0);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200306
Tom Zanussi956ffd02009-11-25 01:15:46 -0600307 setup_scripting();
308
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200309 argc = parse_options(argc, argv, options, annotate_usage, 0);
310 if (argc) {
311 /*
312 * Special case: if there's an argument left then assume tha
313 * it's a symbol filter:
314 */
315 if (argc > 1)
316 usage_with_options(annotate_usage, options);
317 }
318
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200319 setup_pager();
320
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200321 session = perf_session__new(input_name, O_RDONLY, 0);
322 if (session == NULL)
323 return -ENOMEM;
324
Tom Zanussi956ffd02009-11-25 01:15:46 -0600325 if (generate_script_lang) {
326 struct stat perf_stat;
327
328 int input = open(input_name, O_RDONLY);
329 if (input < 0) {
330 perror("failed to open file");
331 exit(-1);
332 }
333
334 err = fstat(input, &perf_stat);
335 if (err < 0) {
336 perror("failed to stat file");
337 exit(-1);
338 }
339
340 if (!perf_stat.st_size) {
341 fprintf(stderr, "zero-sized file, nothing to do!\n");
342 exit(0);
343 }
344
345 scripting_ops = script_spec__lookup(generate_script_lang);
346 if (!scripting_ops) {
347 fprintf(stderr, "invalid language specifier");
348 return -1;
349 }
350
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200351 perf_header__read(&session->header, input);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600352 err = scripting_ops->generate_script("perf-trace");
353 goto out;
354 }
355
356 if (script_name) {
357 err = scripting_ops->start_script(script_name);
358 if (err)
359 goto out;
360 }
361
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200362 err = __cmd_trace(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600363
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200364 perf_session__delete(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600365 cleanup_scripting();
366out:
367 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200368}