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