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 struct perf_session
*session
;
66 static u64 sample_type
;
68 static int process_sample_event(event_t
*event
)
70 struct sample_data data
;
71 struct thread
*thread
;
73 memset(&data
, 0, sizeof(data
));
78 event__parse_sample(event
, sample_type
, &data
);
80 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
83 (void *)(long)data
.ip
,
84 (long long)data
.period
);
86 thread
= threads__findnew(event
->ip
.pid
);
88 pr_debug("problem processing %d event, skipping it.\n",
93 if (sample_type
& PERF_SAMPLE_RAW
) {
95 * FIXME: better resolve from pid from the struct trace_entry
96 * field, although it should be the same than this perf
99 scripting_ops
->process_event(data
.cpu
, data
.raw_data
,
101 data
.time
, thread
->comm
);
103 event__stats
.total
+= data
.period
;
108 static int sample_type_check(u64 type
)
112 if (!(sample_type
& PERF_SAMPLE_RAW
)) {
114 "No trace sample to read. Did you call perf record "
122 static struct perf_file_handler file_handler
= {
123 .process_sample_event
= process_sample_event
,
124 .process_comm_event
= event__process_comm
,
125 .sample_type_check
= sample_type_check
,
128 static int __cmd_trace(void)
132 session
= perf_session__new(input_name
, O_RDONLY
, 0);
136 register_idle_thread();
137 register_perf_file_handler(&file_handler
);
139 err
= perf_session__process_events(session
, 0, &event__cwdlen
, &event__cwd
);
140 perf_session__delete(session
);
145 struct list_head node
;
146 struct scripting_ops
*ops
;
150 LIST_HEAD(script_specs
);
152 static struct script_spec
*script_spec__new(const char *spec
,
153 struct scripting_ops
*ops
)
155 struct script_spec
*s
= malloc(sizeof(*s
) + strlen(spec
) + 1);
158 strcpy(s
->spec
, spec
);
165 static void script_spec__delete(struct script_spec
*s
)
171 static void script_spec__add(struct script_spec
*s
)
173 list_add_tail(&s
->node
, &script_specs
);
176 static struct script_spec
*script_spec__find(const char *spec
)
178 struct script_spec
*s
;
180 list_for_each_entry(s
, &script_specs
, node
)
181 if (strcasecmp(s
->spec
, spec
) == 0)
186 static struct script_spec
*script_spec__findnew(const char *spec
,
187 struct scripting_ops
*ops
)
189 struct script_spec
*s
= script_spec__find(spec
);
194 s
= script_spec__new(spec
, ops
);
196 goto out_delete_spec
;
203 script_spec__delete(s
);
208 int script_spec_register(const char *spec
, struct scripting_ops
*ops
)
210 struct script_spec
*s
;
212 s
= script_spec__find(spec
);
216 s
= script_spec__findnew(spec
, ops
);
223 static struct scripting_ops
*script_spec__lookup(const char *spec
)
225 struct script_spec
*s
= script_spec__find(spec
);
232 static void list_available_languages(void)
234 struct script_spec
*s
;
236 fprintf(stderr
, "\n");
237 fprintf(stderr
, "Scripting language extensions (used in "
238 "perf trace -s [spec:]script.[spec]):\n\n");
240 list_for_each_entry(s
, &script_specs
, node
)
241 fprintf(stderr
, " %-42s [%s]\n", s
->spec
, s
->ops
->name
);
243 fprintf(stderr
, "\n");
246 static int parse_scriptname(const struct option
*opt __used
,
247 const char *str
, int unset __used
)
250 const char *script
, *ext
;
253 if (strcmp(str
, "list") == 0) {
254 list_available_languages();
258 script
= strchr(str
, ':');
261 if (len
>= PATH_MAX
) {
262 fprintf(stderr
, "invalid language specifier");
265 strncpy(spec
, str
, len
);
267 scripting_ops
= script_spec__lookup(spec
);
268 if (!scripting_ops
) {
269 fprintf(stderr
, "invalid language specifier");
275 ext
= strchr(script
, '.');
277 fprintf(stderr
, "invalid script extension");
280 scripting_ops
= script_spec__lookup(++ext
);
281 if (!scripting_ops
) {
282 fprintf(stderr
, "invalid script extension");
287 script_name
= strdup(script
);
292 static const char * const annotate_usage
[] = {
293 "perf trace [<options>] <command>",
297 static const struct option options
[] = {
298 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
299 "dump raw trace in ASCII"),
300 OPT_BOOLEAN('v', "verbose", &verbose
,
301 "be more verbose (show symbol address, etc)"),
302 OPT_BOOLEAN('l', "latency", &latency_format
,
303 "show latency attributes (irqs/preemption disabled, etc)"),
304 OPT_CALLBACK('s', "script", NULL
, "name",
305 "script file name (lang:script name, script name, or *)",
307 OPT_STRING('g', "gen-script", &generate_script_lang
, "lang",
308 "generate perf-trace.xx script in specified language"),
313 int cmd_trace(int argc
, const char **argv
, const char *prefix __used
)
321 argc
= parse_options(argc
, argv
, options
, annotate_usage
, 0);
324 * Special case: if there's an argument left then assume tha
325 * it's a symbol filter:
328 usage_with_options(annotate_usage
, options
);
333 if (generate_script_lang
) {
334 struct stat perf_stat
;
336 int input
= open(input_name
, O_RDONLY
);
338 perror("failed to open file");
342 err
= fstat(input
, &perf_stat
);
344 perror("failed to stat file");
348 if (!perf_stat
.st_size
) {
349 fprintf(stderr
, "zero-sized file, nothing to do!\n");
353 scripting_ops
= script_spec__lookup(generate_script_lang
);
354 if (!scripting_ops
) {
355 fprintf(stderr
, "invalid language specifier");
359 perf_header__read(&session
->header
, input
);
360 err
= scripting_ops
->generate_script("perf-trace");
365 err
= scripting_ops
->start_script(script_name
);