aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf
diff options
context:
space:
mode:
Diffstat (limited to 'tools/perf')
-rw-r--r--tools/perf/builtin-trace.c255
-rw-r--r--tools/perf/util/trace-event.h11
2 files changed, 261 insertions, 5 deletions
diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
index a7750256c408..e96bb534b948 100644
--- a/tools/perf/builtin-trace.c
+++ b/tools/perf/builtin-trace.c
@@ -6,6 +6,46 @@
6#include "util/thread.h" 6#include "util/thread.h"
7#include "util/header.h" 7#include "util/header.h"
8 8
9static char const *script_name;
10static char const *generate_script_lang;
11
12static int default_start_script(const char *script __attribute((unused)))
13{
14 return 0;
15}
16
17static int default_stop_script(void)
18{
19 return 0;
20}
21
22static int default_generate_script(const char *outfile __attribute ((unused)))
23{
24 return 0;
25}
26
27static 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,
32};
33
34static struct scripting_ops *scripting_ops;
35
36static void setup_scripting(void)
37{
38 /* make sure PERF_EXEC_PATH is set for scripts */
39 perf_set_argv_exec_path(perf_exec_path());
40
41 scripting_ops = &default_scripting_ops;
42}
43
44static int cleanup_scripting(void)
45{
46 return scripting_ops->stop_script();
47}
48
9#include "util/parse-options.h" 49#include "util/parse-options.h"
10 50
11#include "perf.h" 51#include "perf.h"
@@ -13,11 +53,12 @@
13 53
14#include "util/trace-event.h" 54#include "util/trace-event.h"
15#include "util/data_map.h" 55#include "util/data_map.h"
56#include "util/exec_cmd.h"
16 57
17static char const *input_name = "perf.data"; 58static char const *input_name = "perf.data";
18 59
19static struct perf_header *header; 60static struct perf_header *header;
20static u64 sample_type; 61static u64 sample_type;
21 62
22static int process_sample_event(event_t *event) 63static int process_sample_event(event_t *event)
23{ 64{
@@ -69,7 +110,8 @@ static int process_sample_event(event_t *event)
69 * field, although it should be the same than this perf 110 * field, although it should be the same than this perf
70 * event pid 111 * event pid
71 */ 112 */
72 print_event(cpu, raw->data, raw->size, timestamp, thread->comm); 113 scripting_ops->process_event(cpu, raw->data, raw->size,
114 timestamp, thread->comm);
73 } 115 }
74 event__stats.total += period; 116 event__stats.total += period;
75 117
@@ -105,6 +147,154 @@ static int __cmd_trace(void)
105 0, 0, &event__cwdlen, &event__cwd); 147 0, 0, &event__cwdlen, &event__cwd);
106} 148}
107 149
150struct script_spec {
151 struct list_head node;
152 struct scripting_ops *ops;
153 char spec[0];
154};
155
156LIST_HEAD(script_specs);
157
158static struct script_spec *script_spec__new(const char *spec,
159 struct scripting_ops *ops)
160{
161 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
162
163 if (s != NULL) {
164 strcpy(s->spec, spec);
165 s->ops = ops;
166 }
167
168 return s;
169}
170
171static void script_spec__delete(struct script_spec *s)
172{
173 free(s->spec);
174 free(s);
175}
176
177static void script_spec__add(struct script_spec *s)
178{
179 list_add_tail(&s->node, &script_specs);
180}
181
182static struct script_spec *script_spec__find(const char *spec)
183{
184 struct script_spec *s;
185
186 list_for_each_entry(s, &script_specs, node)
187 if (strcasecmp(s->spec, spec) == 0)
188 return s;
189 return NULL;
190}
191
192static struct script_spec *script_spec__findnew(const char *spec,
193 struct scripting_ops *ops)
194{
195 struct script_spec *s = script_spec__find(spec);
196
197 if (s)
198 return s;
199
200 s = script_spec__new(spec, ops);
201 if (!s)
202 goto out_delete_spec;
203
204 script_spec__add(s);
205
206 return s;
207
208out_delete_spec:
209 script_spec__delete(s);
210
211 return NULL;
212}
213
214int script_spec_register(const char *spec, struct scripting_ops *ops)
215{
216 struct script_spec *s;
217
218 s = script_spec__find(spec);
219 if (s)
220 return -1;
221
222 s = script_spec__findnew(spec, ops);
223 if (!s)
224 return -1;
225
226 return 0;
227}
228
229static struct scripting_ops *script_spec__lookup(const char *spec)
230{
231 struct script_spec *s = script_spec__find(spec);
232 if (!s)
233 return NULL;
234
235 return s->ops;
236}
237
238static void list_available_languages(void)
239{
240 struct script_spec *s;
241
242 fprintf(stderr, "\n");
243 fprintf(stderr, "Scripting language extensions (used in "
244 "perf trace -s [spec:]script.[spec]):\n\n");
245
246 list_for_each_entry(s, &script_specs, node)
247 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
248
249 fprintf(stderr, "\n");
250}
251
252static int parse_scriptname(const struct option *opt __used,
253 const char *str, int unset __used)
254{
255 char spec[PATH_MAX];
256 const char *script, *ext;
257 int len;
258
259 if (strcmp(str, "list") == 0) {
260 list_available_languages();
261 return 0;
262 }
263
264 script = strchr(str, ':');
265 if (script) {
266 len = script - str;
267 if (len >= PATH_MAX) {
268 fprintf(stderr, "invalid language specifier");
269 return -1;
270 }
271 strncpy(spec, str, len);
272 spec[len] = '\0';
273 scripting_ops = script_spec__lookup(spec);
274 if (!scripting_ops) {
275 fprintf(stderr, "invalid language specifier");
276 return -1;
277 }
278 script++;
279 } else {
280 script = str;
281 ext = strchr(script, '.');
282 if (!ext) {
283 fprintf(stderr, "invalid script extension");
284 return -1;
285 }
286 scripting_ops = script_spec__lookup(++ext);
287 if (!scripting_ops) {
288 fprintf(stderr, "invalid script extension");
289 return -1;
290 }
291 }
292
293 script_name = strdup(script);
294
295 return 0;
296}
297
108static const char * const annotate_usage[] = { 298static const char * const annotate_usage[] = {
109 "perf trace [<options>] <command>", 299 "perf trace [<options>] <command>",
110 NULL 300 NULL
@@ -117,13 +307,23 @@ static const struct option options[] = {
117 "be more verbose (show symbol address, etc)"), 307 "be more verbose (show symbol address, etc)"),
118 OPT_BOOLEAN('l', "latency", &latency_format, 308 OPT_BOOLEAN('l', "latency", &latency_format,
119 "show latency attributes (irqs/preemption disabled, etc)"), 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 *)",
312 parse_scriptname),
313 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
314 "generate perf-trace.xx script in specified language"),
315
120 OPT_END() 316 OPT_END()
121}; 317};
122 318
123int cmd_trace(int argc, const char **argv, const char *prefix __used) 319int cmd_trace(int argc, const char **argv, const char *prefix __used)
124{ 320{
321 int err;
322
125 symbol__init(0); 323 symbol__init(0);
126 324
325 setup_scripting();
326
127 argc = parse_options(argc, argv, options, annotate_usage, 0); 327 argc = parse_options(argc, argv, options, annotate_usage, 0);
128 if (argc) { 328 if (argc) {
129 /* 329 /*
@@ -136,5 +336,50 @@ int cmd_trace(int argc, const char **argv, const char *prefix __used)
136 336
137 setup_pager(); 337 setup_pager();
138 338
139 return __cmd_trace(); 339 if (generate_script_lang) {
340 struct stat perf_stat;
341
342 int input = open(input_name, O_RDONLY);
343 if (input < 0) {
344 perror("failed to open file");
345 exit(-1);
346 }
347
348 err = fstat(input, &perf_stat);
349 if (err < 0) {
350 perror("failed to stat file");
351 exit(-1);
352 }
353
354 if (!perf_stat.st_size) {
355 fprintf(stderr, "zero-sized file, nothing to do!\n");
356 exit(0);
357 }
358
359 scripting_ops = script_spec__lookup(generate_script_lang);
360 if (!scripting_ops) {
361 fprintf(stderr, "invalid language specifier");
362 return -1;
363 }
364
365 header = perf_header__new();
366 if (header == NULL)
367 return -1;
368
369 perf_header__read(header, input);
370 err = scripting_ops->generate_script("perf-trace");
371 goto out;
372 }
373
374 if (script_name) {
375 err = scripting_ops->start_script(script_name);
376 if (err)
377 goto out;
378 }
379
380 err = __cmd_trace();
381
382 cleanup_scripting();
383out:
384 return err;
140} 385}
diff --git a/tools/perf/util/trace-event.h b/tools/perf/util/trace-event.h
index dd51c6872a15..e7aaf002e667 100644
--- a/tools/perf/util/trace-event.h
+++ b/tools/perf/util/trace-event.h
@@ -259,4 +259,15 @@ enum trace_flag_type {
259 TRACE_FLAG_SOFTIRQ = 0x10, 259 TRACE_FLAG_SOFTIRQ = 0x10,
260}; 260};
261 261
262struct scripting_ops {
263 const char *name;
264 int (*start_script) (const char *);
265 int (*stop_script) (void);
266 void (*process_event) (int cpu, void *data, int size,
267 unsigned long long nsecs, char *comm);
268 int (*generate_script) (const char *outfile);
269};
270
271int script_spec_register(const char *spec, struct scripting_ops *ops);
272
262#endif /* __PERF_TRACE_EVENTS_H */ 273#endif /* __PERF_TRACE_EVENTS_H */