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 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(void)
129 register_idle_thread();
130 register_perf_file_handler(&file_handler
);
132 return mmap_dispatch_perf_file(&header
, input_name
,
133 0, 0, &event__cwdlen
, &event__cwd
);
137 struct list_head node
;
138 struct scripting_ops
*ops
;
142 LIST_HEAD(script_specs
);
144 static struct script_spec
*script_spec__new(const char *spec
,
145 struct scripting_ops
*ops
)
147 struct script_spec
*s
= malloc(sizeof(*s
) + strlen(spec
) + 1);
150 strcpy(s
->spec
, spec
);
157 static void script_spec__delete(struct script_spec
*s
)
163 static void script_spec__add(struct script_spec
*s
)
165 list_add_tail(&s
->node
, &script_specs
);
168 static struct script_spec
*script_spec__find(const char *spec
)
170 struct script_spec
*s
;
172 list_for_each_entry(s
, &script_specs
, node
)
173 if (strcasecmp(s
->spec
, spec
) == 0)
178 static struct script_spec
*script_spec__findnew(const char *spec
,
179 struct scripting_ops
*ops
)
181 struct script_spec
*s
= script_spec__find(spec
);
186 s
= script_spec__new(spec
, ops
);
188 goto out_delete_spec
;
195 script_spec__delete(s
);
200 int script_spec_register(const char *spec
, struct scripting_ops
*ops
)
202 struct script_spec
*s
;
204 s
= script_spec__find(spec
);
208 s
= script_spec__findnew(spec
, ops
);
215 static struct scripting_ops
*script_spec__lookup(const char *spec
)
217 struct script_spec
*s
= script_spec__find(spec
);
224 static void list_available_languages(void)
226 struct script_spec
*s
;
228 fprintf(stderr
, "\n");
229 fprintf(stderr
, "Scripting language extensions (used in "
230 "perf trace -s [spec:]script.[spec]):\n\n");
232 list_for_each_entry(s
, &script_specs
, node
)
233 fprintf(stderr
, " %-42s [%s]\n", s
->spec
, s
->ops
->name
);
235 fprintf(stderr
, "\n");
238 static int parse_scriptname(const struct option
*opt __used
,
239 const char *str
, int unset __used
)
242 const char *script
, *ext
;
245 if (strcmp(str
, "list") == 0) {
246 list_available_languages();
250 script
= strchr(str
, ':');
253 if (len
>= PATH_MAX
) {
254 fprintf(stderr
, "invalid language specifier");
257 strncpy(spec
, str
, len
);
259 scripting_ops
= script_spec__lookup(spec
);
260 if (!scripting_ops
) {
261 fprintf(stderr
, "invalid language specifier");
267 ext
= strchr(script
, '.');
269 fprintf(stderr
, "invalid script extension");
272 scripting_ops
= script_spec__lookup(++ext
);
273 if (!scripting_ops
) {
274 fprintf(stderr
, "invalid script extension");
279 script_name
= strdup(script
);
284 static const char * const annotate_usage
[] = {
285 "perf trace [<options>] <command>",
289 static const struct option options
[] = {
290 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
291 "dump raw trace in ASCII"),
292 OPT_BOOLEAN('v', "verbose", &verbose
,
293 "be more verbose (show symbol address, etc)"),
294 OPT_BOOLEAN('l', "latency", &latency_format
,
295 "show latency attributes (irqs/preemption disabled, etc)"),
296 OPT_CALLBACK('s', "script", NULL
, "name",
297 "script file name (lang:script name, script name, or *)",
299 OPT_STRING('g', "gen-script", &generate_script_lang
, "lang",
300 "generate perf-trace.xx script in specified language"),
305 int cmd_trace(int argc
, const char **argv
, const char *prefix __used
)
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 if (generate_script_lang
) {
326 struct stat perf_stat
;
328 int input
= open(input_name
, O_RDONLY
);
330 perror("failed to open file");
334 err
= fstat(input
, &perf_stat
);
336 perror("failed to stat file");
340 if (!perf_stat
.st_size
) {
341 fprintf(stderr
, "zero-sized file, nothing to do!\n");
345 scripting_ops
= script_spec__lookup(generate_script_lang
);
346 if (!scripting_ops
) {
347 fprintf(stderr
, "invalid language specifier");
351 header
= perf_header__new();
355 perf_header__read(header
, input
);
356 err
= scripting_ops
->generate_script("perf-trace");
361 err
= scripting_ops
->start_script(script_name
);