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"
11 static char const *script_name;
12 static char const *generate_script_lang;
14 static int default_start_script(const char *script __attribute((unused)))
19 static int default_stop_script(void)
24 static int default_generate_script(const char *outfile __attribute ((unused)))
29 static struct scripting_ops default_scripting_ops = {
30 .start_script = default_start_script,
31 .stop_script = default_stop_script,
32 .process_event = print_event,
33 .generate_script = default_generate_script,
36 static struct scripting_ops *scripting_ops;
38 static void setup_scripting(void)
40 /* make sure PERF_EXEC_PATH is set for scripts */
41 perf_set_argv_exec_path(perf_exec_path());
43 setup_perl_scripting();
45 scripting_ops = &default_scripting_ops;
48 static int cleanup_scripting(void)
50 return scripting_ops->stop_script();
53 #include "util/parse-options.h"
56 #include "util/debug.h"
58 #include "util/trace-event.h"
59 #include "util/data_map.h"
60 #include "util/exec_cmd.h"
62 static char const *input_name = "perf.data";
64 static struct perf_header *header;
65 static u64 sample_type;
67 static int process_sample_event(event_t *event)
69 u64 ip = event->ip.ip;
73 void *more_data = event->ip.__more_data;
74 struct thread *thread = threads__findnew(event->ip.pid);
76 if (sample_type & PERF_SAMPLE_TIME) {
77 timestamp = *(u64 *)more_data;
78 more_data += sizeof(u64);
81 if (sample_type & PERF_SAMPLE_CPU) {
82 cpu = *(u32 *)more_data;
83 more_data += sizeof(u32);
84 more_data += sizeof(u32); /* reserved */
87 if (sample_type & PERF_SAMPLE_PERIOD) {
88 period = *(u64 *)more_data;
89 more_data += sizeof(u64);
92 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
94 event->ip.pid, event->ip.tid,
99 pr_debug("problem processing %d event, skipping it.\n",
104 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
106 if (sample_type & PERF_SAMPLE_RAW) {
113 * FIXME: better resolve from pid from the struct trace_entry
114 * field, although it should be the same than this perf
117 scripting_ops->process_event(cpu, raw->data, raw->size,
118 timestamp, thread->comm);
120 event__stats.total += period;
125 static int sample_type_check(u64 type)
129 if (!(sample_type & PERF_SAMPLE_RAW)) {
131 "No trace sample to read. Did you call perf record "
139 static struct perf_file_handler file_handler = {
140 .process_sample_event = process_sample_event,
141 .process_comm_event = event__process_comm,
142 .sample_type_check = sample_type_check,
145 static int __cmd_trace(void)
147 register_idle_thread();
148 register_perf_file_handler(&file_handler);
150 return mmap_dispatch_perf_file(&header, input_name,
151 0, 0, &event__cwdlen, &event__cwd);
155 struct list_head node;
156 struct scripting_ops *ops;
160 LIST_HEAD(script_specs);
162 static struct script_spec *script_spec__new(const char *spec,
163 struct scripting_ops *ops)
165 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
168 strcpy(s->spec, spec);
175 static void script_spec__delete(struct script_spec *s)
181 static void script_spec__add(struct script_spec *s)
183 list_add_tail(&s->node, &script_specs);
186 static struct script_spec *script_spec__find(const char *spec)
188 struct script_spec *s;
190 list_for_each_entry(s, &script_specs, node)
191 if (strcasecmp(s->spec, spec) == 0)
196 static struct script_spec *script_spec__findnew(const char *spec,
197 struct scripting_ops *ops)
199 struct script_spec *s = script_spec__find(spec);
204 s = script_spec__new(spec, ops);
206 goto out_delete_spec;
213 script_spec__delete(s);
218 int script_spec_register(const char *spec, struct scripting_ops *ops)
220 struct script_spec *s;
222 s = script_spec__find(spec);
226 s = script_spec__findnew(spec, ops);
233 static struct scripting_ops *script_spec__lookup(const char *spec)
235 struct script_spec *s = script_spec__find(spec);
242 static void list_available_languages(void)
244 struct script_spec *s;
246 fprintf(stderr, "\n");
247 fprintf(stderr, "Scripting language extensions (used in "
248 "perf trace -s [spec:]script.[spec]):\n\n");
250 list_for_each_entry(s, &script_specs, node)
251 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
253 fprintf(stderr, "\n");
256 static int parse_scriptname(const struct option *opt __used,
257 const char *str, int unset __used)
260 const char *script, *ext;
263 if (strcmp(str, "list") == 0) {
264 list_available_languages();
268 script = strchr(str, ':');
271 if (len >= PATH_MAX) {
272 fprintf(stderr, "invalid language specifier");
275 strncpy(spec, str, len);
277 scripting_ops = script_spec__lookup(spec);
278 if (!scripting_ops) {
279 fprintf(stderr, "invalid language specifier");
285 ext = strchr(script, '.');
287 fprintf(stderr, "invalid script extension");
290 scripting_ops = script_spec__lookup(++ext);
291 if (!scripting_ops) {
292 fprintf(stderr, "invalid script extension");
297 script_name = strdup(script);
302 static const char * const annotate_usage[] = {
303 "perf trace [<options>] <command>",
307 static const struct option options[] = {
308 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
309 "dump raw trace in ASCII"),
310 OPT_BOOLEAN('v', "verbose", &verbose,
311 "be more verbose (show symbol address, etc)"),
312 OPT_BOOLEAN('l', "latency", &latency_format,
313 "show latency attributes (irqs/preemption disabled, etc)"),
314 OPT_CALLBACK('s', "script", NULL, "name",
315 "script file name (lang:script name, script name, or *)",
317 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
318 "generate perf-trace.xx script in specified language"),
323 int cmd_trace(int argc, const char **argv, const char *prefix __used)
331 argc = parse_options(argc, argv, options, annotate_usage, 0);
334 * Special case: if there's an argument left then assume tha
335 * it's a symbol filter:
338 usage_with_options(annotate_usage, options);
343 if (generate_script_lang) {
344 struct stat perf_stat;
346 int input = open(input_name, O_RDONLY);
348 perror("failed to open file");
352 err = fstat(input, &perf_stat);
354 perror("failed to stat file");
358 if (!perf_stat.st_size) {
359 fprintf(stderr, "zero-sized file, nothing to do!\n");
363 scripting_ops = script_spec__lookup(generate_script_lang);
364 if (!scripting_ops) {
365 fprintf(stderr, "invalid language specifier");
369 header = perf_header__new();
373 perf_header__read(header, input);
374 err = scripting_ops->generate_script("perf-trace");
379 err = scripting_ops->start_script(script_name);