blob: c2fcc34486f5c7447b6c5030c8144b35b7bd2d68 [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"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020010
Tom Zanussi956ffd02009-11-25 01:15:46 -060011static char const *script_name;
12static char const *generate_script_lang;
13
14static int default_start_script(const char *script __attribute((unused)))
15{
16 return 0;
17}
18
19static int default_stop_script(void)
20{
21 return 0;
22}
23
24static int default_generate_script(const char *outfile __attribute ((unused)))
25{
26 return 0;
27}
28
29static struct scripting_ops default_scripting_ops = {
30 .start_script = default_start_script,
31 .stop_script = default_stop_script,
32 .process_event = print_event,
33 .generate_script = default_generate_script,
34};
35
36static struct scripting_ops *scripting_ops;
37
38static void setup_scripting(void)
39{
40 /* make sure PERF_EXEC_PATH is set for scripts */
41 perf_set_argv_exec_path(perf_exec_path());
42
Tom Zanussi16c632d2009-11-25 01:15:48 -060043 setup_perl_scripting();
44
Tom Zanussi956ffd02009-11-25 01:15:46 -060045 scripting_ops = &default_scripting_ops;
46}
47
48static int cleanup_scripting(void)
49{
50 return scripting_ops->stop_script();
51}
52
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020053#include "util/parse-options.h"
54
55#include "perf.h"
56#include "util/debug.h"
57
58#include "util/trace-event.h"
Frederic Weisbecker016e92f2009-10-07 12:47:31 +020059#include "util/data_map.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 struct perf_header *header;
65static u64 sample_type;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020066
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -020067static int process_sample_event(event_t *event)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020068{
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090069 struct sample_data data;
70 struct thread *thread;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020071
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090072 memset(&data, 0, sizeof(data));
73 data.time = -1;
74 data.cpu = -1;
75 data.period = 1;
Ingo Molnar6ddf2592009-09-03 12:00:22 +020076
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090077 event__parse_sample(event, sample_type, &data);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020078
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -020079 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020080 event->header.misc,
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090081 data.pid, data.tid,
82 (void *)(long)data.ip,
83 (long long)data.period);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020084
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090085 thread = threads__findnew(event->ip.pid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020086 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -020087 pr_debug("problem processing %d event, skipping it.\n",
88 event->header.type);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020089 return -1;
90 }
91
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020092 if (sample_type & PERF_SAMPLE_RAW) {
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020093 /*
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 */
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090098 scripting_ops->process_event(data.cpu, data.raw_data,
99 data.raw_size,
100 data.time, thread->comm);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200101 }
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900102 event__stats.total += data.period;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200103
104 return 0;
105}
106
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200107static int sample_type_check(u64 type)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200108{
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200109 sample_type = type;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200110
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200111 if (!(sample_type & PERF_SAMPLE_RAW)) {
112 fprintf(stderr,
113 "No trace sample to read. Did you call perf record "
114 "without -R?");
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200115 return -1;
116 }
117
118 return 0;
119}
120
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200121static struct perf_file_handler file_handler = {
122 .process_sample_event = process_sample_event,
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200123 .process_comm_event = event__process_comm,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200124 .sample_type_check = sample_type_check,
125};
126
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200127static int __cmd_trace(void)
128{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300129 register_idle_thread();
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200130 register_perf_file_handler(&file_handler);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200131
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200132 return mmap_dispatch_perf_file(&header, input_name,
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200133 0, 0, &event__cwdlen, &event__cwd);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200134}
135
Tom Zanussi956ffd02009-11-25 01:15:46 -0600136struct script_spec {
137 struct list_head node;
138 struct scripting_ops *ops;
139 char spec[0];
140};
141
142LIST_HEAD(script_specs);
143
144static struct script_spec *script_spec__new(const char *spec,
145 struct scripting_ops *ops)
146{
147 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
148
149 if (s != NULL) {
150 strcpy(s->spec, spec);
151 s->ops = ops;
152 }
153
154 return s;
155}
156
157static void script_spec__delete(struct script_spec *s)
158{
159 free(s->spec);
160 free(s);
161}
162
163static void script_spec__add(struct script_spec *s)
164{
165 list_add_tail(&s->node, &script_specs);
166}
167
168static struct script_spec *script_spec__find(const char *spec)
169{
170 struct script_spec *s;
171
172 list_for_each_entry(s, &script_specs, node)
173 if (strcasecmp(s->spec, spec) == 0)
174 return s;
175 return NULL;
176}
177
178static struct script_spec *script_spec__findnew(const char *spec,
179 struct scripting_ops *ops)
180{
181 struct script_spec *s = script_spec__find(spec);
182
183 if (s)
184 return s;
185
186 s = script_spec__new(spec, ops);
187 if (!s)
188 goto out_delete_spec;
189
190 script_spec__add(s);
191
192 return s;
193
194out_delete_spec:
195 script_spec__delete(s);
196
197 return NULL;
198}
199
200int script_spec_register(const char *spec, struct scripting_ops *ops)
201{
202 struct script_spec *s;
203
204 s = script_spec__find(spec);
205 if (s)
206 return -1;
207
208 s = script_spec__findnew(spec, ops);
209 if (!s)
210 return -1;
211
212 return 0;
213}
214
215static struct scripting_ops *script_spec__lookup(const char *spec)
216{
217 struct script_spec *s = script_spec__find(spec);
218 if (!s)
219 return NULL;
220
221 return s->ops;
222}
223
224static void list_available_languages(void)
225{
226 struct script_spec *s;
227
228 fprintf(stderr, "\n");
229 fprintf(stderr, "Scripting language extensions (used in "
230 "perf trace -s [spec:]script.[spec]):\n\n");
231
232 list_for_each_entry(s, &script_specs, node)
233 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
234
235 fprintf(stderr, "\n");
236}
237
238static int parse_scriptname(const struct option *opt __used,
239 const char *str, int unset __used)
240{
241 char spec[PATH_MAX];
242 const char *script, *ext;
243 int len;
244
245 if (strcmp(str, "list") == 0) {
246 list_available_languages();
247 return 0;
248 }
249
250 script = strchr(str, ':');
251 if (script) {
252 len = script - str;
253 if (len >= PATH_MAX) {
254 fprintf(stderr, "invalid language specifier");
255 return -1;
256 }
257 strncpy(spec, str, len);
258 spec[len] = '\0';
259 scripting_ops = script_spec__lookup(spec);
260 if (!scripting_ops) {
261 fprintf(stderr, "invalid language specifier");
262 return -1;
263 }
264 script++;
265 } else {
266 script = str;
267 ext = strchr(script, '.');
268 if (!ext) {
269 fprintf(stderr, "invalid script extension");
270 return -1;
271 }
272 scripting_ops = script_spec__lookup(++ext);
273 if (!scripting_ops) {
274 fprintf(stderr, "invalid script extension");
275 return -1;
276 }
277 }
278
279 script_name = strdup(script);
280
281 return 0;
282}
283
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200284static const char * const annotate_usage[] = {
285 "perf trace [<options>] <command>",
286 NULL
287};
288
289static const struct option options[] = {
290 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
291 "dump raw trace in ASCII"),
292 OPT_BOOLEAN('v', "verbose", &verbose,
293 "be more verbose (show symbol address, etc)"),
Steven Rostedtcda48462009-10-14 15:43:42 -0400294 OPT_BOOLEAN('l', "latency", &latency_format,
295 "show latency attributes (irqs/preemption disabled, etc)"),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600296 OPT_CALLBACK('s', "script", NULL, "name",
297 "script file name (lang:script name, script name, or *)",
298 parse_scriptname),
299 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
300 "generate perf-trace.xx script in specified language"),
301
Masami Hiramatsu19096292009-08-21 14:56:03 -0400302 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200303};
304
305int cmd_trace(int argc, const char **argv, const char *prefix __used)
306{
Tom Zanussi956ffd02009-11-25 01:15:46 -0600307 int err;
308
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200309 symbol__init(0);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200310
Tom Zanussi956ffd02009-11-25 01:15:46 -0600311 setup_scripting();
312
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200313 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
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200323 setup_pager();
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
351 header = perf_header__new();
352 if (header == NULL)
353 return -1;
354
355 perf_header__read(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();
367
368 cleanup_scripting();
369out:
370 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200371}