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