40cb896581dd2b227c63b35e4e868b1bbd167858
[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 #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         return perf_session__process_events(session, &event_ops, 0,
129                                             &event__cwdlen, &event__cwd);
130 }
131
132 struct script_spec {
133         struct list_head        node;
134         struct scripting_ops    *ops;
135         char                    spec[0];
136 };
137
138 LIST_HEAD(script_specs);
139
140 static 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
153 static void script_spec__delete(struct script_spec *s)
154 {
155         free(s->spec);
156         free(s);
157 }
158
159 static void script_spec__add(struct script_spec *s)
160 {
161         list_add_tail(&s->node, &script_specs);
162 }
163
164 static 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
174 static 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
190 out_delete_spec:
191         script_spec__delete(s);
192
193         return NULL;
194 }
195
196 int 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
211 static 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
220 static 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
234 static 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
280 static const char * const annotate_usage[] = {
281         "perf trace [<options>] <command>",
282         NULL
283 };
284
285 static 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)"),
290         OPT_BOOLEAN('l', "latency", &latency_format,
291                     "show latency attributes (irqs/preemption disabled, etc)"),
292         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
298         OPT_END()
299 };
300
301 int cmd_trace(int argc, const char **argv, const char *prefix __used)
302 {
303         int err;
304         struct perf_session *session;
305
306         symbol__init(0);
307
308         setup_scripting();
309
310         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
320         setup_pager();
321
322         session = perf_session__new(input_name, O_RDONLY, 0);
323         if (session == NULL)
324                 return -ENOMEM;
325
326         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
352                 perf_header__read(&session->header, input);
353                 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
363         err = __cmd_trace(session);
364
365         perf_session__delete(session);
366         cleanup_scripting();
367 out:
368         return err;
369 }