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