blob: 40cb896581dd2b227c63b35e4e868b1bbd167858 [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 Melo301a0b02009-12-13 19:50:25 -0200128 return perf_session__process_events(session, &event_ops, 0,
129 &event__cwdlen, &event__cwd);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200130}
131
Tom Zanussi956ffd02009-11-25 01:15:46 -0600132struct script_spec {
133 struct list_head node;
134 struct scripting_ops *ops;
135 char spec[0];
136};
137
138LIST_HEAD(script_specs);
139
140static struct script_spec *script_spec__new(const char *spec,
141 struct scripting_ops *ops)
142{
143 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
144
145 if (s != NULL) {
146 strcpy(s->spec, spec);
147 s->ops = ops;
148 }
149
150 return s;
151}
152
153static void script_spec__delete(struct script_spec *s)
154{
155 free(s->spec);
156 free(s);
157}
158
159static void script_spec__add(struct script_spec *s)
160{
161 list_add_tail(&s->node, &script_specs);
162}
163
164static struct script_spec *script_spec__find(const char *spec)
165{
166 struct script_spec *s;
167
168 list_for_each_entry(s, &script_specs, node)
169 if (strcasecmp(s->spec, spec) == 0)
170 return s;
171 return NULL;
172}
173
174static struct script_spec *script_spec__findnew(const char *spec,
175 struct scripting_ops *ops)
176{
177 struct script_spec *s = script_spec__find(spec);
178
179 if (s)
180 return s;
181
182 s = script_spec__new(spec, ops);
183 if (!s)
184 goto out_delete_spec;
185
186 script_spec__add(s);
187
188 return s;
189
190out_delete_spec:
191 script_spec__delete(s);
192
193 return NULL;
194}
195
196int script_spec_register(const char *spec, struct scripting_ops *ops)
197{
198 struct script_spec *s;
199
200 s = script_spec__find(spec);
201 if (s)
202 return -1;
203
204 s = script_spec__findnew(spec, ops);
205 if (!s)
206 return -1;
207
208 return 0;
209}
210
211static struct scripting_ops *script_spec__lookup(const char *spec)
212{
213 struct script_spec *s = script_spec__find(spec);
214 if (!s)
215 return NULL;
216
217 return s->ops;
218}
219
220static void list_available_languages(void)
221{
222 struct script_spec *s;
223
224 fprintf(stderr, "\n");
225 fprintf(stderr, "Scripting language extensions (used in "
226 "perf trace -s [spec:]script.[spec]):\n\n");
227
228 list_for_each_entry(s, &script_specs, node)
229 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
230
231 fprintf(stderr, "\n");
232}
233
234static int parse_scriptname(const struct option *opt __used,
235 const char *str, int unset __used)
236{
237 char spec[PATH_MAX];
238 const char *script, *ext;
239 int len;
240
241 if (strcmp(str, "list") == 0) {
242 list_available_languages();
243 return 0;
244 }
245
246 script = strchr(str, ':');
247 if (script) {
248 len = script - str;
249 if (len >= PATH_MAX) {
250 fprintf(stderr, "invalid language specifier");
251 return -1;
252 }
253 strncpy(spec, str, len);
254 spec[len] = '\0';
255 scripting_ops = script_spec__lookup(spec);
256 if (!scripting_ops) {
257 fprintf(stderr, "invalid language specifier");
258 return -1;
259 }
260 script++;
261 } else {
262 script = str;
263 ext = strchr(script, '.');
264 if (!ext) {
265 fprintf(stderr, "invalid script extension");
266 return -1;
267 }
268 scripting_ops = script_spec__lookup(++ext);
269 if (!scripting_ops) {
270 fprintf(stderr, "invalid script extension");
271 return -1;
272 }
273 }
274
275 script_name = strdup(script);
276
277 return 0;
278}
279
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200280static const char * const annotate_usage[] = {
281 "perf trace [<options>] <command>",
282 NULL
283};
284
285static const struct option options[] = {
286 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
287 "dump raw trace in ASCII"),
288 OPT_BOOLEAN('v', "verbose", &verbose,
289 "be more verbose (show symbol address, etc)"),
Steven Rostedtcda48462009-10-14 15:43:42 -0400290 OPT_BOOLEAN('l', "latency", &latency_format,
291 "show latency attributes (irqs/preemption disabled, etc)"),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600292 OPT_CALLBACK('s', "script", NULL, "name",
293 "script file name (lang:script name, script name, or *)",
294 parse_scriptname),
295 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
296 "generate perf-trace.xx script in specified language"),
297
Masami Hiramatsu19096292009-08-21 14:56:03 -0400298 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200299};
300
301int cmd_trace(int argc, const char **argv, const char *prefix __used)
302{
Tom Zanussi956ffd02009-11-25 01:15:46 -0600303 int err;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200304 struct perf_session *session;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600305
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200306 symbol__init(0);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200307
Tom Zanussi956ffd02009-11-25 01:15:46 -0600308 setup_scripting();
309
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200310 argc = parse_options(argc, argv, options, annotate_usage, 0);
311 if (argc) {
312 /*
313 * Special case: if there's an argument left then assume tha
314 * it's a symbol filter:
315 */
316 if (argc > 1)
317 usage_with_options(annotate_usage, options);
318 }
319
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200320 setup_pager();
321
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200322 session = perf_session__new(input_name, O_RDONLY, 0);
323 if (session == NULL)
324 return -ENOMEM;
325
Tom Zanussi956ffd02009-11-25 01:15:46 -0600326 if (generate_script_lang) {
327 struct stat perf_stat;
328
329 int input = open(input_name, O_RDONLY);
330 if (input < 0) {
331 perror("failed to open file");
332 exit(-1);
333 }
334
335 err = fstat(input, &perf_stat);
336 if (err < 0) {
337 perror("failed to stat file");
338 exit(-1);
339 }
340
341 if (!perf_stat.st_size) {
342 fprintf(stderr, "zero-sized file, nothing to do!\n");
343 exit(0);
344 }
345
346 scripting_ops = script_spec__lookup(generate_script_lang);
347 if (!scripting_ops) {
348 fprintf(stderr, "invalid language specifier");
349 return -1;
350 }
351
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200352 perf_header__read(&session->header, input);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600353 err = scripting_ops->generate_script("perf-trace");
354 goto out;
355 }
356
357 if (script_name) {
358 err = scripting_ops->start_script(script_name);
359 if (err)
360 goto out;
361 }
362
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200363 err = __cmd_trace(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600364
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200365 perf_session__delete(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600366 cleanup_scripting();
367out:
368 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200369}