4 #include "util/cache.h"
5 #include "util/symbol.h"
6 #include "util/thread.h"
7 #include "util/header.h"
9 static char const *script_name;
10 static char const *generate_script_lang;
12 static int default_start_script(const char *script __attribute((unused)))
17 static int default_stop_script(void)
22 static int default_generate_script(const char *outfile __attribute ((unused)))
27 static struct scripting_ops default_scripting_ops = {
28 .start_script = default_start_script,
29 .stop_script = default_stop_script,
30 .process_event = print_event,
31 .generate_script = default_generate_script,
34 static struct scripting_ops *scripting_ops;
36 static void setup_scripting(void)
38 /* make sure PERF_EXEC_PATH is set for scripts */
39 perf_set_argv_exec_path(perf_exec_path());
41 scripting_ops = &default_scripting_ops;
44 static int cleanup_scripting(void)
46 return scripting_ops->stop_script();
49 #include "util/parse-options.h"
52 #include "util/debug.h"
54 #include "util/trace-event.h"
55 #include "util/data_map.h"
56 #include "util/exec_cmd.h"
58 static char const *input_name = "perf.data";
60 static struct perf_header *header;
61 static u64 sample_type;
63 static int process_sample_event(event_t *event)
65 u64 ip = event->ip.ip;
69 void *more_data = event->ip.__more_data;
70 struct thread *thread = threads__findnew(event->ip.pid);
72 if (sample_type & PERF_SAMPLE_TIME) {
73 timestamp = *(u64 *)more_data;
74 more_data += sizeof(u64);
77 if (sample_type & PERF_SAMPLE_CPU) {
78 cpu = *(u32 *)more_data;
79 more_data += sizeof(u32);
80 more_data += sizeof(u32); /* reserved */
83 if (sample_type & PERF_SAMPLE_PERIOD) {
84 period = *(u64 *)more_data;
85 more_data += sizeof(u64);
88 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
90 event->ip.pid, event->ip.tid,
95 pr_debug("problem processing %d event, skipping it.\n",
100 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
102 if (sample_type & PERF_SAMPLE_RAW) {
109 * FIXME: better resolve from pid from the struct trace_entry
110 * field, although it should be the same than this perf
113 scripting_ops->process_event(cpu, raw->data, raw->size,
114 timestamp, thread->comm);
116 event__stats.total += period;
121 static int sample_type_check(u64 type)
125 if (!(sample_type & PERF_SAMPLE_RAW)) {
127 "No trace sample to read. Did you call perf record "
135 static struct perf_file_handler file_handler = {
136 .process_sample_event = process_sample_event,
137 .process_comm_event = event__process_comm,
138 .sample_type_check = sample_type_check,
141 static int __cmd_trace(void)
143 register_idle_thread();
144 register_perf_file_handler(&file_handler);
146 return mmap_dispatch_perf_file(&header, input_name,
147 0, 0, &event__cwdlen, &event__cwd);
151 struct list_head node;
152 struct scripting_ops *ops;
156 LIST_HEAD(script_specs);
158 static struct script_spec *script_spec__new(const char *spec,
159 struct scripting_ops *ops)
161 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
164 strcpy(s->spec, spec);
171 static void script_spec__delete(struct script_spec *s)
177 static void script_spec__add(struct script_spec *s)
179 list_add_tail(&s->node, &script_specs);
182 static struct script_spec *script_spec__find(const char *spec)
184 struct script_spec *s;
186 list_for_each_entry(s, &script_specs, node)
187 if (strcasecmp(s->spec, spec) == 0)
192 static struct script_spec *script_spec__findnew(const char *spec,
193 struct scripting_ops *ops)
195 struct script_spec *s = script_spec__find(spec);
200 s = script_spec__new(spec, ops);
202 goto out_delete_spec;
209 script_spec__delete(s);
214 int script_spec_register(const char *spec, struct scripting_ops *ops)
216 struct script_spec *s;
218 s = script_spec__find(spec);
222 s = script_spec__findnew(spec, ops);
229 static struct scripting_ops *script_spec__lookup(const char *spec)
231 struct script_spec *s = script_spec__find(spec);
238 static void list_available_languages(void)
240 struct script_spec *s;
242 fprintf(stderr, "\n");
243 fprintf(stderr, "Scripting language extensions (used in "
244 "perf trace -s [spec:]script.[spec]):\n\n");
246 list_for_each_entry(s, &script_specs, node)
247 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
249 fprintf(stderr, "\n");
252 static int parse_scriptname(const struct option *opt __used,
253 const char *str, int unset __used)
256 const char *script, *ext;
259 if (strcmp(str, "list") == 0) {
260 list_available_languages();
264 script = strchr(str, ':');
267 if (len >= PATH_MAX) {
268 fprintf(stderr, "invalid language specifier");
271 strncpy(spec, str, len);
273 scripting_ops = script_spec__lookup(spec);
274 if (!scripting_ops) {
275 fprintf(stderr, "invalid language specifier");
281 ext = strchr(script, '.');
283 fprintf(stderr, "invalid script extension");
286 scripting_ops = script_spec__lookup(++ext);
287 if (!scripting_ops) {
288 fprintf(stderr, "invalid script extension");
293 script_name = strdup(script);
298 static const char * const annotate_usage[] = {
299 "perf trace [<options>] <command>",
303 static const struct option options[] = {
304 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
305 "dump raw trace in ASCII"),
306 OPT_BOOLEAN('v', "verbose", &verbose,
307 "be more verbose (show symbol address, etc)"),
308 OPT_BOOLEAN('l', "latency", &latency_format,
309 "show latency attributes (irqs/preemption disabled, etc)"),
310 OPT_CALLBACK('s', "script", NULL, "name",
311 "script file name (lang:script name, script name, or *)",
313 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
314 "generate perf-trace.xx script in specified language"),
319 int cmd_trace(int argc, const char **argv, const char *prefix __used)
327 argc = parse_options(argc, argv, options, annotate_usage, 0);
330 * Special case: if there's an argument left then assume tha
331 * it's a symbol filter:
334 usage_with_options(annotate_usage, options);
339 if (generate_script_lang) {
340 struct stat perf_stat;
342 int input = open(input_name, O_RDONLY);
344 perror("failed to open file");
348 err = fstat(input, &perf_stat);
350 perror("failed to stat file");
354 if (!perf_stat.st_size) {
355 fprintf(stderr, "zero-sized file, nothing to do!\n");
359 scripting_ops = script_spec__lookup(generate_script_lang);
360 if (!scripting_ops) {
361 fprintf(stderr, "invalid language specifier");
365 header = perf_header__new();
369 perf_header__read(header, input);
370 err = scripting_ops->generate_script("perf-trace");
375 err = scripting_ops->start_script(script_name);