perf session: Move kmaps to perf_session
[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)
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 = perf_session__findnew(session, 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);
129 }
130
131 struct script_spec {
132         struct list_head        node;
133         struct scripting_ops    *ops;
134         char                    spec[0];
135 };
136
137 LIST_HEAD(script_specs);
138
139 static struct script_spec *script_spec__new(const char *spec,
140                                             struct scripting_ops *ops)
141 {
142         struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
143
144         if (s != NULL) {
145                 strcpy(s->spec, spec);
146                 s->ops = ops;
147         }
148
149         return s;
150 }
151
152 static void script_spec__delete(struct script_spec *s)
153 {
154         free(s->spec);
155         free(s);
156 }
157
158 static void script_spec__add(struct script_spec *s)
159 {
160         list_add_tail(&s->node, &script_specs);
161 }
162
163 static struct script_spec *script_spec__find(const char *spec)
164 {
165         struct script_spec *s;
166
167         list_for_each_entry(s, &script_specs, node)
168                 if (strcasecmp(s->spec, spec) == 0)
169                         return s;
170         return NULL;
171 }
172
173 static struct script_spec *script_spec__findnew(const char *spec,
174                                                 struct scripting_ops *ops)
175 {
176         struct script_spec *s = script_spec__find(spec);
177
178         if (s)
179                 return s;
180
181         s = script_spec__new(spec, ops);
182         if (!s)
183                 goto out_delete_spec;
184
185         script_spec__add(s);
186
187         return s;
188
189 out_delete_spec:
190         script_spec__delete(s);
191
192         return NULL;
193 }
194
195 int script_spec_register(const char *spec, struct scripting_ops *ops)
196 {
197         struct script_spec *s;
198
199         s = script_spec__find(spec);
200         if (s)
201                 return -1;
202
203         s = script_spec__findnew(spec, ops);
204         if (!s)
205                 return -1;
206
207         return 0;
208 }
209
210 static struct scripting_ops *script_spec__lookup(const char *spec)
211 {
212         struct script_spec *s = script_spec__find(spec);
213         if (!s)
214                 return NULL;
215
216         return s->ops;
217 }
218
219 static void list_available_languages(void)
220 {
221         struct script_spec *s;
222
223         fprintf(stderr, "\n");
224         fprintf(stderr, "Scripting language extensions (used in "
225                 "perf trace -s [spec:]script.[spec]):\n\n");
226
227         list_for_each_entry(s, &script_specs, node)
228                 fprintf(stderr, "  %-42s [%s]\n", s->spec, s->ops->name);
229
230         fprintf(stderr, "\n");
231 }
232
233 static int parse_scriptname(const struct option *opt __used,
234                             const char *str, int unset __used)
235 {
236         char spec[PATH_MAX];
237         const char *script, *ext;
238         int len;
239
240         if (strcmp(str, "list") == 0) {
241                 list_available_languages();
242                 return 0;
243         }
244
245         script = strchr(str, ':');
246         if (script) {
247                 len = script - str;
248                 if (len >= PATH_MAX) {
249                         fprintf(stderr, "invalid language specifier");
250                         return -1;
251                 }
252                 strncpy(spec, str, len);
253                 spec[len] = '\0';
254                 scripting_ops = script_spec__lookup(spec);
255                 if (!scripting_ops) {
256                         fprintf(stderr, "invalid language specifier");
257                         return -1;
258                 }
259                 script++;
260         } else {
261                 script = str;
262                 ext = strchr(script, '.');
263                 if (!ext) {
264                         fprintf(stderr, "invalid script extension");
265                         return -1;
266                 }
267                 scripting_ops = script_spec__lookup(++ext);
268                 if (!scripting_ops) {
269                         fprintf(stderr, "invalid script extension");
270                         return -1;
271                 }
272         }
273
274         script_name = strdup(script);
275
276         return 0;
277 }
278
279 static const char * const annotate_usage[] = {
280         "perf trace [<options>] <command>",
281         NULL
282 };
283
284 static const struct option options[] = {
285         OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
286                     "dump raw trace in ASCII"),
287         OPT_BOOLEAN('v', "verbose", &verbose,
288                     "be more verbose (show symbol address, etc)"),
289         OPT_BOOLEAN('l', "latency", &latency_format,
290                     "show latency attributes (irqs/preemption disabled, etc)"),
291         OPT_CALLBACK('s', "script", NULL, "name",
292                      "script file name (lang:script name, script name, or *)",
293                      parse_scriptname),
294         OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
295                    "generate perf-trace.xx script in specified language"),
296
297         OPT_END()
298 };
299
300 int cmd_trace(int argc, const char **argv, const char *prefix __used)
301 {
302         int err;
303         struct perf_session *session;
304
305         symbol__init(0);
306
307         setup_scripting();
308
309         argc = parse_options(argc, argv, options, annotate_usage, 0);
310         if (argc) {
311                 /*
312                  * Special case: if there's an argument left then assume tha
313                  * it's a symbol filter:
314                  */
315                 if (argc > 1)
316                         usage_with_options(annotate_usage, options);
317         }
318
319         setup_pager();
320
321         session = perf_session__new(input_name, O_RDONLY, 0, NULL);
322         if (session == NULL)
323                 return -ENOMEM;
324
325         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                 perf_header__read(&session->header, input);
352                 err = scripting_ops->generate_script("perf-trace");
353                 goto out;
354         }
355
356         if (script_name) {
357                 err = scripting_ops->start_script(script_name);
358                 if (err)
359                         goto out;
360         }
361
362         err = __cmd_trace(session);
363
364         perf_session__delete(session);
365         cleanup_scripting();
366 out:
367         return err;
368 }