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 __unused,
17 const char **argv __unused)
22 static int default_stop_script(void)
27 static int default_generate_script(const char *outfile __unused)
32 static struct scripting_ops default_scripting_ops = {
33 .start_script = default_start_script,
34 .stop_script = default_stop_script,
35 .process_event = print_event,
36 .generate_script = default_generate_script,
39 static struct scripting_ops *scripting_ops;
41 static void setup_scripting(void)
43 /* make sure PERF_EXEC_PATH is set for scripts */
44 perf_set_argv_exec_path(perf_exec_path());
46 setup_perl_scripting();
48 scripting_ops = &default_scripting_ops;
51 static int cleanup_scripting(void)
53 return scripting_ops->stop_script();
56 #include "util/parse-options.h"
59 #include "util/debug.h"
61 #include "util/trace-event.h"
62 #include "util/exec_cmd.h"
64 static char const *input_name = "perf.data";
66 static int process_sample_event(event_t *event, struct perf_session *session)
68 struct sample_data data;
69 struct thread *thread;
71 memset(&data, 0, sizeof(data));
76 event__parse_sample(event, session->sample_type, &data);
78 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
81 (void *)(long)data.ip,
82 (long long)data.period);
84 thread = perf_session__findnew(session, event->ip.pid);
86 pr_debug("problem processing %d event, skipping it.\n",
91 if (session->sample_type & PERF_SAMPLE_RAW) {
93 * FIXME: better resolve from pid from the struct trace_entry
94 * field, although it should be the same than this perf
97 scripting_ops->process_event(data.cpu, data.raw_data,
99 data.time, thread->comm);
102 session->events_stats.total += data.period;
106 static int sample_type_check(struct perf_session *session)
108 if (!(session->sample_type & PERF_SAMPLE_RAW)) {
110 "No trace sample to read. Did you call perf record "
118 static struct perf_event_ops event_ops = {
119 .process_sample_event = process_sample_event,
120 .process_comm_event = event__process_comm,
121 .sample_type_check = sample_type_check,
124 static int __cmd_trace(struct perf_session *session)
126 return perf_session__process_events(session, &event_ops);
130 struct list_head node;
131 struct scripting_ops *ops;
135 LIST_HEAD(script_specs);
137 static struct script_spec *script_spec__new(const char *spec,
138 struct scripting_ops *ops)
140 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
143 strcpy(s->spec, spec);
150 static void script_spec__delete(struct script_spec *s)
156 static void script_spec__add(struct script_spec *s)
158 list_add_tail(&s->node, &script_specs);
161 static struct script_spec *script_spec__find(const char *spec)
163 struct script_spec *s;
165 list_for_each_entry(s, &script_specs, node)
166 if (strcasecmp(s->spec, spec) == 0)
171 static struct script_spec *script_spec__findnew(const char *spec,
172 struct scripting_ops *ops)
174 struct script_spec *s = script_spec__find(spec);
179 s = script_spec__new(spec, ops);
181 goto out_delete_spec;
188 script_spec__delete(s);
193 int script_spec_register(const char *spec, struct scripting_ops *ops)
195 struct script_spec *s;
197 s = script_spec__find(spec);
201 s = script_spec__findnew(spec, ops);
208 static struct scripting_ops *script_spec__lookup(const char *spec)
210 struct script_spec *s = script_spec__find(spec);
217 static void list_available_languages(void)
219 struct script_spec *s;
221 fprintf(stderr, "\n");
222 fprintf(stderr, "Scripting language extensions (used in "
223 "perf trace -s [spec:]script.[spec]):\n\n");
225 list_for_each_entry(s, &script_specs, node)
226 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
228 fprintf(stderr, "\n");
231 static int parse_scriptname(const struct option *opt __used,
232 const char *str, int unset __used)
235 const char *script, *ext;
238 if (strcmp(str, "list") == 0) {
239 list_available_languages();
243 script = strchr(str, ':');
246 if (len >= PATH_MAX) {
247 fprintf(stderr, "invalid language specifier");
250 strncpy(spec, str, len);
252 scripting_ops = script_spec__lookup(spec);
253 if (!scripting_ops) {
254 fprintf(stderr, "invalid language specifier");
260 ext = strchr(script, '.');
262 fprintf(stderr, "invalid script extension");
265 scripting_ops = script_spec__lookup(++ext);
266 if (!scripting_ops) {
267 fprintf(stderr, "invalid script extension");
272 script_name = strdup(script);
277 static const char * const annotate_usage[] = {
278 "perf trace [<options>] <command>",
282 static const struct option options[] = {
283 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
284 "dump raw trace in ASCII"),
285 OPT_BOOLEAN('v', "verbose", &verbose,
286 "be more verbose (show symbol address, etc)"),
287 OPT_BOOLEAN('l', "latency", &latency_format,
288 "show latency attributes (irqs/preemption disabled, etc)"),
289 OPT_CALLBACK('s', "script", NULL, "name",
290 "script file name (lang:script name, script name, or *)",
292 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
293 "generate perf-trace.xx script in specified language"),
298 int cmd_trace(int argc, const char **argv, const char *prefix __used)
301 struct perf_session *session;
307 argc = parse_options(argc, argv, options, annotate_usage,
308 PARSE_OPT_STOP_AT_NON_OPTION);
312 session = perf_session__new(input_name, O_RDONLY, 0, NULL);
316 if (generate_script_lang) {
317 struct stat perf_stat;
319 int input = open(input_name, O_RDONLY);
321 perror("failed to open file");
325 err = fstat(input, &perf_stat);
327 perror("failed to stat file");
331 if (!perf_stat.st_size) {
332 fprintf(stderr, "zero-sized file, nothing to do!\n");
336 scripting_ops = script_spec__lookup(generate_script_lang);
337 if (!scripting_ops) {
338 fprintf(stderr, "invalid language specifier");
342 perf_header__read(&session->header, input);
343 err = scripting_ops->generate_script("perf-trace");
348 err = scripting_ops->start_script(script_name, argc, argv);
353 err = __cmd_trace(session);
355 perf_session__delete(session);