blob: 3057e1d387b9eb956447dbd0b55a32440927827a [file] [log] [blame]
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +02001#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"
Ingo Molnarcf723442009-11-28 10:11:00 +01008#include "util/exec_cmd.h"
9#include "util/trace-event.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020010#include "util/session.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020011
Tom Zanussi956ffd02009-11-25 01:15:46 -060012static char const *script_name;
13static char const *generate_script_lang;
14
15static int default_start_script(const char *script __attribute((unused)))
16{
17 return 0;
18}
19
20static int default_stop_script(void)
21{
22 return 0;
23}
24
25static int default_generate_script(const char *outfile __attribute ((unused)))
26{
27 return 0;
28}
29
30static 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
37static struct scripting_ops *scripting_ops;
38
39static 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
Tom Zanussi16c632d2009-11-25 01:15:48 -060044 setup_perl_scripting();
45
Tom Zanussi956ffd02009-11-25 01:15:46 -060046 scripting_ops = &default_scripting_ops;
47}
48
49static int cleanup_scripting(void)
50{
51 return scripting_ops->stop_script();
52}
53
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020054#include "util/parse-options.h"
55
56#include "perf.h"
57#include "util/debug.h"
58
59#include "util/trace-event.h"
Frederic Weisbecker016e92f2009-10-07 12:47:31 +020060#include "util/data_map.h"
Tom Zanussi956ffd02009-11-25 01:15:46 -060061#include "util/exec_cmd.h"
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020062
Tom Zanussi956ffd02009-11-25 01:15:46 -060063static char const *input_name = "perf.data";
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020064
Tom Zanussi956ffd02009-11-25 01:15:46 -060065static u64 sample_type;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020066
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -020067static int process_sample_event(event_t *event, struct perf_session *session __used)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020068{
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090069 struct sample_data data;
70 struct thread *thread;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020071
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090072 memset(&data, 0, sizeof(data));
73 data.time = -1;
74 data.cpu = -1;
75 data.period = 1;
Ingo Molnar6ddf2592009-09-03 12:00:22 +020076
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090077 event__parse_sample(event, sample_type, &data);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020078
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -020079 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020080 event->header.misc,
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090081 data.pid, data.tid,
82 (void *)(long)data.ip,
83 (long long)data.period);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020084
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090085 thread = threads__findnew(event->ip.pid);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020086 if (thread == NULL) {
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -020087 pr_debug("problem processing %d event, skipping it.\n",
88 event->header.type);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020089 return -1;
90 }
91
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020092 if (sample_type & PERF_SAMPLE_RAW) {
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +020093 /*
94 * FIXME: better resolve from pid from the struct trace_entry
95 * field, although it should be the same than this perf
96 * event pid
97 */
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +090098 scripting_ops->process_event(data.cpu, data.raw_data,
99 data.raw_size,
100 data.time, thread->comm);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200101 }
OGAWA Hirofumi180f95e2009-12-06 20:08:24 +0900102 event__stats.total += data.period;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200103
104 return 0;
105}
106
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200107static int sample_type_check(u64 type)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200108{
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200109 sample_type = type;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200110
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200111 if (!(sample_type & PERF_SAMPLE_RAW)) {
112 fprintf(stderr,
113 "No trace sample to read. Did you call perf record "
114 "without -R?");
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200115 return -1;
116 }
117
118 return 0;
119}
120
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200121static struct perf_file_handler file_handler = {
122 .process_sample_event = process_sample_event,
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200123 .process_comm_event = event__process_comm,
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200124 .sample_type_check = sample_type_check,
125};
126
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200127static int __cmd_trace(struct perf_session *session)
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200128{
Arnaldo Carvalho de Melod5b889f2009-10-13 11:16:29 -0300129 register_idle_thread();
Frederic Weisbecker016e92f2009-10-07 12:47:31 +0200130 register_perf_file_handler(&file_handler);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200131
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200132 return perf_session__process_events(session, 0, &event__cwdlen, &event__cwd);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200133}
134
Tom Zanussi956ffd02009-11-25 01:15:46 -0600135struct script_spec {
136 struct list_head node;
137 struct scripting_ops *ops;
138 char spec[0];
139};
140
141LIST_HEAD(script_specs);
142
143static struct script_spec *script_spec__new(const char *spec,
144 struct scripting_ops *ops)
145{
146 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
147
148 if (s != NULL) {
149 strcpy(s->spec, spec);
150 s->ops = ops;
151 }
152
153 return s;
154}
155
156static void script_spec__delete(struct script_spec *s)
157{
158 free(s->spec);
159 free(s);
160}
161
162static void script_spec__add(struct script_spec *s)
163{
164 list_add_tail(&s->node, &script_specs);
165}
166
167static struct script_spec *script_spec__find(const char *spec)
168{
169 struct script_spec *s;
170
171 list_for_each_entry(s, &script_specs, node)
172 if (strcasecmp(s->spec, spec) == 0)
173 return s;
174 return NULL;
175}
176
177static struct script_spec *script_spec__findnew(const char *spec,
178 struct scripting_ops *ops)
179{
180 struct script_spec *s = script_spec__find(spec);
181
182 if (s)
183 return s;
184
185 s = script_spec__new(spec, ops);
186 if (!s)
187 goto out_delete_spec;
188
189 script_spec__add(s);
190
191 return s;
192
193out_delete_spec:
194 script_spec__delete(s);
195
196 return NULL;
197}
198
199int script_spec_register(const char *spec, struct scripting_ops *ops)
200{
201 struct script_spec *s;
202
203 s = script_spec__find(spec);
204 if (s)
205 return -1;
206
207 s = script_spec__findnew(spec, ops);
208 if (!s)
209 return -1;
210
211 return 0;
212}
213
214static struct scripting_ops *script_spec__lookup(const char *spec)
215{
216 struct script_spec *s = script_spec__find(spec);
217 if (!s)
218 return NULL;
219
220 return s->ops;
221}
222
223static void list_available_languages(void)
224{
225 struct script_spec *s;
226
227 fprintf(stderr, "\n");
228 fprintf(stderr, "Scripting language extensions (used in "
229 "perf trace -s [spec:]script.[spec]):\n\n");
230
231 list_for_each_entry(s, &script_specs, node)
232 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
233
234 fprintf(stderr, "\n");
235}
236
237static int parse_scriptname(const struct option *opt __used,
238 const char *str, int unset __used)
239{
240 char spec[PATH_MAX];
241 const char *script, *ext;
242 int len;
243
244 if (strcmp(str, "list") == 0) {
245 list_available_languages();
246 return 0;
247 }
248
249 script = strchr(str, ':');
250 if (script) {
251 len = script - str;
252 if (len >= PATH_MAX) {
253 fprintf(stderr, "invalid language specifier");
254 return -1;
255 }
256 strncpy(spec, str, len);
257 spec[len] = '\0';
258 scripting_ops = script_spec__lookup(spec);
259 if (!scripting_ops) {
260 fprintf(stderr, "invalid language specifier");
261 return -1;
262 }
263 script++;
264 } else {
265 script = str;
266 ext = strchr(script, '.');
267 if (!ext) {
268 fprintf(stderr, "invalid script extension");
269 return -1;
270 }
271 scripting_ops = script_spec__lookup(++ext);
272 if (!scripting_ops) {
273 fprintf(stderr, "invalid script extension");
274 return -1;
275 }
276 }
277
278 script_name = strdup(script);
279
280 return 0;
281}
282
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200283static const char * const annotate_usage[] = {
284 "perf trace [<options>] <command>",
285 NULL
286};
287
288static const struct option options[] = {
289 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
290 "dump raw trace in ASCII"),
291 OPT_BOOLEAN('v', "verbose", &verbose,
292 "be more verbose (show symbol address, etc)"),
Steven Rostedtcda48462009-10-14 15:43:42 -0400293 OPT_BOOLEAN('l', "latency", &latency_format,
294 "show latency attributes (irqs/preemption disabled, etc)"),
Tom Zanussi956ffd02009-11-25 01:15:46 -0600295 OPT_CALLBACK('s', "script", NULL, "name",
296 "script file name (lang:script name, script name, or *)",
297 parse_scriptname),
298 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
299 "generate perf-trace.xx script in specified language"),
300
Masami Hiramatsu19096292009-08-21 14:56:03 -0400301 OPT_END()
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200302};
303
304int cmd_trace(int argc, const char **argv, const char *prefix __used)
305{
Tom Zanussi956ffd02009-11-25 01:15:46 -0600306 int err;
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200307 struct perf_session *session;
Tom Zanussi956ffd02009-11-25 01:15:46 -0600308
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200309 symbol__init(0);
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200310
Tom Zanussi956ffd02009-11-25 01:15:46 -0600311 setup_scripting();
312
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200313 argc = parse_options(argc, argv, options, annotate_usage, 0);
314 if (argc) {
315 /*
316 * Special case: if there's an argument left then assume tha
317 * it's a symbol filter:
318 */
319 if (argc > 1)
320 usage_with_options(annotate_usage, options);
321 }
322
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200323 setup_pager();
324
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200325 session = perf_session__new(input_name, O_RDONLY, 0);
326 if (session == NULL)
327 return -ENOMEM;
328
Tom Zanussi956ffd02009-11-25 01:15:46 -0600329 if (generate_script_lang) {
330 struct stat perf_stat;
331
332 int input = open(input_name, O_RDONLY);
333 if (input < 0) {
334 perror("failed to open file");
335 exit(-1);
336 }
337
338 err = fstat(input, &perf_stat);
339 if (err < 0) {
340 perror("failed to stat file");
341 exit(-1);
342 }
343
344 if (!perf_stat.st_size) {
345 fprintf(stderr, "zero-sized file, nothing to do!\n");
346 exit(0);
347 }
348
349 scripting_ops = script_spec__lookup(generate_script_lang);
350 if (!scripting_ops) {
351 fprintf(stderr, "invalid language specifier");
352 return -1;
353 }
354
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200355 perf_header__read(&session->header, input);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600356 err = scripting_ops->generate_script("perf-trace");
357 goto out;
358 }
359
360 if (script_name) {
361 err = scripting_ops->start_script(script_name);
362 if (err)
363 goto out;
364 }
365
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200366 err = __cmd_trace(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600367
Arnaldo Carvalho de Melod8f66242009-12-13 19:50:24 -0200368 perf_session__delete(session);
Tom Zanussi956ffd02009-11-25 01:15:46 -0600369 cleanup_scripting();
370out:
371 return err;
Frederic Weisbecker5f9c39d2009-08-17 16:18:08 +0200372}