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"
12 static char const *script_name;
13 static char const *generate_script_lang;
15 static int default_start_script(const char *script __attribute((unused)))
20 static int default_stop_script(void)
25 static int default_generate_script(const char *outfile __attribute ((unused)))
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,
37 static struct scripting_ops *scripting_ops;
39 static void setup_scripting(void)
41 /* make sure PERF_EXEC_PATH is set for scripts */
42 perf_set_argv_exec_path(perf_exec_path());
44 setup_perl_scripting();
46 scripting_ops = &default_scripting_ops;
49 static int cleanup_scripting(void)
51 return scripting_ops->stop_script();
54 #include "util/parse-options.h"
57 #include "util/debug.h"
59 #include "util/trace-event.h"
60 #include "util/data_map.h"
61 #include "util/exec_cmd.h"
63 static char const *input_name = "perf.data";
65 static u64 sample_type;
67 static int process_sample_event(event_t *event, struct perf_session *session __used)
69 struct sample_data data;
70 struct thread *thread;
72 memset(&data, 0, sizeof(data));
77 event__parse_sample(event, sample_type, &data);
79 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
82 (void *)(long)data.ip,
83 (long long)data.period);
85 thread = threads__findnew(event->ip.pid);
87 pr_debug("problem processing %d event, skipping it.\n",
92 if (sample_type & PERF_SAMPLE_RAW) {
94 * FIXME: better resolve from pid from the struct trace_entry
95 * field, although it should be the same than this perf
98 scripting_ops->process_event(data.cpu, data.raw_data,
100 data.time, thread->comm);
102 event__stats.total += data.period;
107 static int sample_type_check(u64 type)
111 if (!(sample_type & PERF_SAMPLE_RAW)) {
113 "No trace sample to read. Did you call perf record "
121 static struct perf_file_handler file_handler = {
122 .process_sample_event = process_sample_event,
123 .process_comm_event = event__process_comm,
124 .sample_type_check = sample_type_check,
127 static int __cmd_trace(struct perf_session *session)
129 register_idle_thread();
130 register_perf_file_handler(&file_handler);
132 return perf_session__process_events(session, 0, &event__cwdlen, &event__cwd);
136 struct list_head node;
137 struct scripting_ops *ops;
141 LIST_HEAD(script_specs);
143 static struct script_spec *script_spec__new(const char *spec,
144 struct scripting_ops *ops)
146 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
149 strcpy(s->spec, spec);
156 static void script_spec__delete(struct script_spec *s)
162 static void script_spec__add(struct script_spec *s)
164 list_add_tail(&s->node, &script_specs);
167 static struct script_spec *script_spec__find(const char *spec)
169 struct script_spec *s;
171 list_for_each_entry(s, &script_specs, node)
172 if (strcasecmp(s->spec, spec) == 0)
177 static struct script_spec *script_spec__findnew(const char *spec,
178 struct scripting_ops *ops)
180 struct script_spec *s = script_spec__find(spec);
185 s = script_spec__new(spec, ops);
187 goto out_delete_spec;
194 script_spec__delete(s);
199 int script_spec_register(const char *spec, struct scripting_ops *ops)
201 struct script_spec *s;
203 s = script_spec__find(spec);
207 s = script_spec__findnew(spec, ops);
214 static struct scripting_ops *script_spec__lookup(const char *spec)
216 struct script_spec *s = script_spec__find(spec);
223 static void list_available_languages(void)
225 struct script_spec *s;
227 fprintf(stderr, "\n");
228 fprintf(stderr, "Scripting language extensions (used in "
229 "perf trace -s [spec:]script.[spec]):\n\n");
231 list_for_each_entry(s, &script_specs, node)
232 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
234 fprintf(stderr, "\n");
237 static int parse_scriptname(const struct option *opt __used,
238 const char *str, int unset __used)
241 const char *script, *ext;
244 if (strcmp(str, "list") == 0) {
245 list_available_languages();
249 script = strchr(str, ':');
252 if (len >= PATH_MAX) {
253 fprintf(stderr, "invalid language specifier");
256 strncpy(spec, str, len);
258 scripting_ops = script_spec__lookup(spec);
259 if (!scripting_ops) {
260 fprintf(stderr, "invalid language specifier");
266 ext = strchr(script, '.');
268 fprintf(stderr, "invalid script extension");
271 scripting_ops = script_spec__lookup(++ext);
272 if (!scripting_ops) {
273 fprintf(stderr, "invalid script extension");
278 script_name = strdup(script);
283 static const char * const annotate_usage[] = {
284 "perf trace [<options>] <command>",
288 static 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)"),
293 OPT_BOOLEAN('l', "latency", &latency_format,
294 "show latency attributes (irqs/preemption disabled, etc)"),
295 OPT_CALLBACK('s', "script", NULL, "name",
296 "script file name (lang:script name, script name, or *)",
298 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
299 "generate perf-trace.xx script in specified language"),
304 int cmd_trace(int argc, const char **argv, const char *prefix __used)
307 struct perf_session *session;
313 argc = parse_options(argc, argv, options, annotate_usage, 0);
316 * Special case: if there's an argument left then assume tha
317 * it's a symbol filter:
320 usage_with_options(annotate_usage, options);
325 session = perf_session__new(input_name, O_RDONLY, 0);
329 if (generate_script_lang) {
330 struct stat perf_stat;
332 int input = open(input_name, O_RDONLY);
334 perror("failed to open file");
338 err = fstat(input, &perf_stat);
340 perror("failed to stat file");
344 if (!perf_stat.st_size) {
345 fprintf(stderr, "zero-sized file, nothing to do!\n");
349 scripting_ops = script_spec__lookup(generate_script_lang);
350 if (!scripting_ops) {
351 fprintf(stderr, "invalid language specifier");
355 perf_header__read(&session->header, input);
356 err = scripting_ops->generate_script("perf-trace");
361 err = scripting_ops->start_script(script_name);
366 err = __cmd_trace(session);
368 perf_session__delete(session);