Commit | Line | Data |
---|---|---|
5f9c39dc FW |
1 | #include "builtin.h" |
2 | ||
b7eead86 | 3 | #include "perf.h" |
5f9c39dc | 4 | #include "util/cache.h" |
b7eead86 AG |
5 | #include "util/debug.h" |
6 | #include "util/exec_cmd.h" | |
7 | #include "util/header.h" | |
8 | #include "util/parse-options.h" | |
9 | #include "util/session.h" | |
5f9c39dc FW |
10 | #include "util/symbol.h" |
11 | #include "util/thread.h" | |
cf72344d | 12 | #include "util/trace-event.h" |
34c86ea9 | 13 | #include "util/parse-options.h" |
b7eead86 | 14 | #include "util/util.h" |
5f9c39dc | 15 | |
956ffd02 TZ |
16 | static char const *script_name; |
17 | static char const *generate_script_lang; | |
ffabd99e | 18 | static bool debug_mode; |
e1889d75 | 19 | static u64 last_timestamp; |
6fcf7ddb | 20 | static u64 nr_unordered; |
34c86ea9 | 21 | extern const struct option record_options[]; |
956ffd02 | 22 | |
be6d842a DA |
23 | static void process_event(union perf_event *event __unused, |
24 | struct perf_sample *sample, | |
25 | struct perf_session *session __unused, | |
26 | struct thread *thread) | |
27 | { | |
28 | /* | |
29 | * FIXME: better resolve from pid from the struct trace_entry | |
30 | * field, although it should be the same than this perf | |
31 | * event pid | |
32 | */ | |
33 | print_event(sample->cpu, sample->raw_data, sample->raw_size, | |
34 | sample->time, thread->comm); | |
35 | } | |
36 | ||
586bc5cc TZ |
37 | static int default_start_script(const char *script __unused, |
38 | int argc __unused, | |
39 | const char **argv __unused) | |
956ffd02 TZ |
40 | { |
41 | return 0; | |
42 | } | |
43 | ||
44 | static int default_stop_script(void) | |
45 | { | |
46 | return 0; | |
47 | } | |
48 | ||
586bc5cc | 49 | static int default_generate_script(const char *outfile __unused) |
956ffd02 TZ |
50 | { |
51 | return 0; | |
52 | } | |
53 | ||
54 | static struct scripting_ops default_scripting_ops = { | |
55 | .start_script = default_start_script, | |
56 | .stop_script = default_stop_script, | |
be6d842a | 57 | .process_event = process_event, |
956ffd02 TZ |
58 | .generate_script = default_generate_script, |
59 | }; | |
60 | ||
61 | static struct scripting_ops *scripting_ops; | |
62 | ||
63 | static void setup_scripting(void) | |
64 | { | |
16c632de | 65 | setup_perl_scripting(); |
7e4b21b8 | 66 | setup_python_scripting(); |
16c632de | 67 | |
956ffd02 TZ |
68 | scripting_ops = &default_scripting_ops; |
69 | } | |
70 | ||
71 | static int cleanup_scripting(void) | |
72 | { | |
133dc4c3 | 73 | pr_debug("\nperf script stopped\n"); |
3824a4e8 | 74 | |
956ffd02 TZ |
75 | return scripting_ops->stop_script(); |
76 | } | |
77 | ||
956ffd02 | 78 | static char const *input_name = "perf.data"; |
5f9c39dc | 79 | |
8115d60c ACM |
80 | static int process_sample_event(union perf_event *event, |
81 | struct perf_sample *sample, | |
640c03ce | 82 | struct perf_session *session) |
5f9c39dc | 83 | { |
640c03ce | 84 | struct thread *thread = perf_session__findnew(session, event->ip.pid); |
6ddf259d | 85 | |
5f9c39dc | 86 | if (thread == NULL) { |
6beba7ad ACM |
87 | pr_debug("problem processing %d event, skipping it.\n", |
88 | event->header.type); | |
5f9c39dc FW |
89 | return -1; |
90 | } | |
91 | ||
c019879b | 92 | if (session->sample_type & PERF_SAMPLE_RAW) { |
ffabd99e | 93 | if (debug_mode) { |
640c03ce | 94 | if (sample->time < last_timestamp) { |
9486aa38 ACM |
95 | pr_err("Samples misordered, previous: %" PRIu64 |
96 | " this: %" PRIu64 "\n", last_timestamp, | |
640c03ce | 97 | sample->time); |
6fcf7ddb | 98 | nr_unordered++; |
e1889d75 | 99 | } |
640c03ce | 100 | last_timestamp = sample->time; |
6fcf7ddb | 101 | return 0; |
e1889d75 | 102 | } |
be6d842a | 103 | scripting_ops->process_event(event, sample, session, thread); |
5f9c39dc | 104 | } |
5f9c39dc | 105 | |
640c03ce | 106 | session->hists.stats.total_period += sample->period; |
5f9c39dc FW |
107 | return 0; |
108 | } | |
109 | ||
301a0b02 | 110 | static struct perf_event_ops event_ops = { |
8115d60c ACM |
111 | .sample = process_sample_event, |
112 | .comm = perf_event__process_comm, | |
113 | .attr = perf_event__process_attr, | |
114 | .event_type = perf_event__process_event_type, | |
115 | .tracing_data = perf_event__process_tracing_data, | |
116 | .build_id = perf_event__process_build_id, | |
e0a808c6 | 117 | .ordered_samples = true, |
8115d60c | 118 | .ordering_requires_timestamps = true, |
016e92fb FW |
119 | }; |
120 | ||
c239da3b TZ |
121 | extern volatile int session_done; |
122 | ||
123 | static void sig_handler(int sig __unused) | |
124 | { | |
125 | session_done = 1; | |
126 | } | |
127 | ||
133dc4c3 | 128 | static int __cmd_script(struct perf_session *session) |
5f9c39dc | 129 | { |
6fcf7ddb FW |
130 | int ret; |
131 | ||
c239da3b TZ |
132 | signal(SIGINT, sig_handler); |
133 | ||
6fcf7ddb FW |
134 | ret = perf_session__process_events(session, &event_ops); |
135 | ||
6d8afb56 | 136 | if (debug_mode) |
9486aa38 | 137 | pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered); |
6fcf7ddb FW |
138 | |
139 | return ret; | |
5f9c39dc FW |
140 | } |
141 | ||
956ffd02 TZ |
142 | struct script_spec { |
143 | struct list_head node; | |
144 | struct scripting_ops *ops; | |
145 | char spec[0]; | |
146 | }; | |
147 | ||
eccdfe2d | 148 | static LIST_HEAD(script_specs); |
956ffd02 TZ |
149 | |
150 | static struct script_spec *script_spec__new(const char *spec, | |
151 | struct scripting_ops *ops) | |
152 | { | |
153 | struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1); | |
154 | ||
155 | if (s != NULL) { | |
156 | strcpy(s->spec, spec); | |
157 | s->ops = ops; | |
158 | } | |
159 | ||
160 | return s; | |
161 | } | |
162 | ||
163 | static void script_spec__delete(struct script_spec *s) | |
164 | { | |
165 | free(s->spec); | |
166 | free(s); | |
167 | } | |
168 | ||
169 | static void script_spec__add(struct script_spec *s) | |
170 | { | |
171 | list_add_tail(&s->node, &script_specs); | |
172 | } | |
173 | ||
174 | static struct script_spec *script_spec__find(const char *spec) | |
175 | { | |
176 | struct script_spec *s; | |
177 | ||
178 | list_for_each_entry(s, &script_specs, node) | |
179 | if (strcasecmp(s->spec, spec) == 0) | |
180 | return s; | |
181 | return NULL; | |
182 | } | |
183 | ||
184 | static struct script_spec *script_spec__findnew(const char *spec, | |
185 | struct scripting_ops *ops) | |
186 | { | |
187 | struct script_spec *s = script_spec__find(spec); | |
188 | ||
189 | if (s) | |
190 | return s; | |
191 | ||
192 | s = script_spec__new(spec, ops); | |
193 | if (!s) | |
194 | goto out_delete_spec; | |
195 | ||
196 | script_spec__add(s); | |
197 | ||
198 | return s; | |
199 | ||
200 | out_delete_spec: | |
201 | script_spec__delete(s); | |
202 | ||
203 | return NULL; | |
204 | } | |
205 | ||
206 | int script_spec_register(const char *spec, struct scripting_ops *ops) | |
207 | { | |
208 | struct script_spec *s; | |
209 | ||
210 | s = script_spec__find(spec); | |
211 | if (s) | |
212 | return -1; | |
213 | ||
214 | s = script_spec__findnew(spec, ops); | |
215 | if (!s) | |
216 | return -1; | |
217 | ||
218 | return 0; | |
219 | } | |
220 | ||
221 | static struct scripting_ops *script_spec__lookup(const char *spec) | |
222 | { | |
223 | struct script_spec *s = script_spec__find(spec); | |
224 | if (!s) | |
225 | return NULL; | |
226 | ||
227 | return s->ops; | |
228 | } | |
229 | ||
230 | static void list_available_languages(void) | |
231 | { | |
232 | struct script_spec *s; | |
233 | ||
234 | fprintf(stderr, "\n"); | |
235 | fprintf(stderr, "Scripting language extensions (used in " | |
133dc4c3 | 236 | "perf script -s [spec:]script.[spec]):\n\n"); |
956ffd02 TZ |
237 | |
238 | list_for_each_entry(s, &script_specs, node) | |
239 | fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name); | |
240 | ||
241 | fprintf(stderr, "\n"); | |
242 | } | |
243 | ||
244 | static int parse_scriptname(const struct option *opt __used, | |
245 | const char *str, int unset __used) | |
246 | { | |
247 | char spec[PATH_MAX]; | |
248 | const char *script, *ext; | |
249 | int len; | |
250 | ||
f526d68b | 251 | if (strcmp(str, "lang") == 0) { |
956ffd02 | 252 | list_available_languages(); |
f526d68b | 253 | exit(0); |
956ffd02 TZ |
254 | } |
255 | ||
256 | script = strchr(str, ':'); | |
257 | if (script) { | |
258 | len = script - str; | |
259 | if (len >= PATH_MAX) { | |
260 | fprintf(stderr, "invalid language specifier"); | |
261 | return -1; | |
262 | } | |
263 | strncpy(spec, str, len); | |
264 | spec[len] = '\0'; | |
265 | scripting_ops = script_spec__lookup(spec); | |
266 | if (!scripting_ops) { | |
267 | fprintf(stderr, "invalid language specifier"); | |
268 | return -1; | |
269 | } | |
270 | script++; | |
271 | } else { | |
272 | script = str; | |
d1e95bb5 | 273 | ext = strrchr(script, '.'); |
956ffd02 TZ |
274 | if (!ext) { |
275 | fprintf(stderr, "invalid script extension"); | |
276 | return -1; | |
277 | } | |
278 | scripting_ops = script_spec__lookup(++ext); | |
279 | if (!scripting_ops) { | |
280 | fprintf(stderr, "invalid script extension"); | |
281 | return -1; | |
282 | } | |
283 | } | |
284 | ||
285 | script_name = strdup(script); | |
286 | ||
287 | return 0; | |
288 | } | |
289 | ||
008f29d3 SB |
290 | /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */ |
291 | static int is_directory(const char *base_path, const struct dirent *dent) | |
292 | { | |
293 | char path[PATH_MAX]; | |
294 | struct stat st; | |
295 | ||
296 | sprintf(path, "%s/%s", base_path, dent->d_name); | |
297 | if (stat(path, &st)) | |
298 | return 0; | |
299 | ||
300 | return S_ISDIR(st.st_mode); | |
301 | } | |
302 | ||
303 | #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\ | |
4b9c0c59 TZ |
304 | while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \ |
305 | lang_next) \ | |
008f29d3 SB |
306 | if ((lang_dirent.d_type == DT_DIR || \ |
307 | (lang_dirent.d_type == DT_UNKNOWN && \ | |
308 | is_directory(scripts_path, &lang_dirent))) && \ | |
4b9c0c59 TZ |
309 | (strcmp(lang_dirent.d_name, ".")) && \ |
310 | (strcmp(lang_dirent.d_name, ".."))) | |
311 | ||
008f29d3 | 312 | #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\ |
4b9c0c59 TZ |
313 | while (!readdir_r(lang_dir, &script_dirent, &script_next) && \ |
314 | script_next) \ | |
008f29d3 SB |
315 | if (script_dirent.d_type != DT_DIR && \ |
316 | (script_dirent.d_type != DT_UNKNOWN || \ | |
317 | !is_directory(lang_path, &script_dirent))) | |
4b9c0c59 TZ |
318 | |
319 | ||
320 | #define RECORD_SUFFIX "-record" | |
321 | #define REPORT_SUFFIX "-report" | |
322 | ||
323 | struct script_desc { | |
324 | struct list_head node; | |
325 | char *name; | |
326 | char *half_liner; | |
327 | char *args; | |
328 | }; | |
329 | ||
eccdfe2d | 330 | static LIST_HEAD(script_descs); |
4b9c0c59 TZ |
331 | |
332 | static struct script_desc *script_desc__new(const char *name) | |
333 | { | |
334 | struct script_desc *s = zalloc(sizeof(*s)); | |
335 | ||
b5b87312 | 336 | if (s != NULL && name) |
4b9c0c59 TZ |
337 | s->name = strdup(name); |
338 | ||
339 | return s; | |
340 | } | |
341 | ||
342 | static void script_desc__delete(struct script_desc *s) | |
343 | { | |
344 | free(s->name); | |
e8719adf TZ |
345 | free(s->half_liner); |
346 | free(s->args); | |
4b9c0c59 TZ |
347 | free(s); |
348 | } | |
349 | ||
350 | static void script_desc__add(struct script_desc *s) | |
351 | { | |
352 | list_add_tail(&s->node, &script_descs); | |
353 | } | |
354 | ||
355 | static struct script_desc *script_desc__find(const char *name) | |
356 | { | |
357 | struct script_desc *s; | |
358 | ||
359 | list_for_each_entry(s, &script_descs, node) | |
360 | if (strcasecmp(s->name, name) == 0) | |
361 | return s; | |
362 | return NULL; | |
363 | } | |
364 | ||
365 | static struct script_desc *script_desc__findnew(const char *name) | |
366 | { | |
367 | struct script_desc *s = script_desc__find(name); | |
368 | ||
369 | if (s) | |
370 | return s; | |
371 | ||
372 | s = script_desc__new(name); | |
373 | if (!s) | |
374 | goto out_delete_desc; | |
375 | ||
376 | script_desc__add(s); | |
377 | ||
378 | return s; | |
379 | ||
380 | out_delete_desc: | |
381 | script_desc__delete(s); | |
382 | ||
383 | return NULL; | |
384 | } | |
385 | ||
965bb6be | 386 | static const char *ends_with(const char *str, const char *suffix) |
4b9c0c59 TZ |
387 | { |
388 | size_t suffix_len = strlen(suffix); | |
965bb6be | 389 | const char *p = str; |
4b9c0c59 TZ |
390 | |
391 | if (strlen(str) > suffix_len) { | |
392 | p = str + strlen(str) - suffix_len; | |
393 | if (!strncmp(p, suffix, suffix_len)) | |
394 | return p; | |
395 | } | |
396 | ||
397 | return NULL; | |
398 | } | |
399 | ||
400 | static char *ltrim(char *str) | |
401 | { | |
402 | int len = strlen(str); | |
403 | ||
404 | while (len && isspace(*str)) { | |
405 | len--; | |
406 | str++; | |
407 | } | |
408 | ||
409 | return str; | |
410 | } | |
411 | ||
412 | static int read_script_info(struct script_desc *desc, const char *filename) | |
413 | { | |
414 | char line[BUFSIZ], *p; | |
415 | FILE *fp; | |
416 | ||
417 | fp = fopen(filename, "r"); | |
418 | if (!fp) | |
419 | return -1; | |
420 | ||
421 | while (fgets(line, sizeof(line), fp)) { | |
422 | p = ltrim(line); | |
423 | if (strlen(p) == 0) | |
424 | continue; | |
425 | if (*p != '#') | |
426 | continue; | |
427 | p++; | |
428 | if (strlen(p) && *p == '!') | |
429 | continue; | |
430 | ||
431 | p = ltrim(p); | |
432 | if (strlen(p) && p[strlen(p) - 1] == '\n') | |
433 | p[strlen(p) - 1] = '\0'; | |
434 | ||
435 | if (!strncmp(p, "description:", strlen("description:"))) { | |
436 | p += strlen("description:"); | |
437 | desc->half_liner = strdup(ltrim(p)); | |
438 | continue; | |
439 | } | |
440 | ||
441 | if (!strncmp(p, "args:", strlen("args:"))) { | |
442 | p += strlen("args:"); | |
443 | desc->args = strdup(ltrim(p)); | |
444 | continue; | |
445 | } | |
446 | } | |
447 | ||
448 | fclose(fp); | |
449 | ||
450 | return 0; | |
451 | } | |
452 | ||
453 | static int list_available_scripts(const struct option *opt __used, | |
454 | const char *s __used, int unset __used) | |
455 | { | |
456 | struct dirent *script_next, *lang_next, script_dirent, lang_dirent; | |
457 | char scripts_path[MAXPATHLEN]; | |
458 | DIR *scripts_dir, *lang_dir; | |
459 | char script_path[MAXPATHLEN]; | |
460 | char lang_path[MAXPATHLEN]; | |
461 | struct script_desc *desc; | |
462 | char first_half[BUFSIZ]; | |
463 | char *script_root; | |
464 | char *str; | |
465 | ||
466 | snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); | |
467 | ||
468 | scripts_dir = opendir(scripts_path); | |
469 | if (!scripts_dir) | |
470 | return -1; | |
471 | ||
008f29d3 | 472 | for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) { |
4b9c0c59 TZ |
473 | snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, |
474 | lang_dirent.d_name); | |
475 | lang_dir = opendir(lang_path); | |
476 | if (!lang_dir) | |
477 | continue; | |
478 | ||
008f29d3 | 479 | for_each_script(lang_path, lang_dir, script_dirent, script_next) { |
4b9c0c59 | 480 | script_root = strdup(script_dirent.d_name); |
965bb6be | 481 | str = (char *)ends_with(script_root, REPORT_SUFFIX); |
4b9c0c59 TZ |
482 | if (str) { |
483 | *str = '\0'; | |
484 | desc = script_desc__findnew(script_root); | |
485 | snprintf(script_path, MAXPATHLEN, "%s/%s", | |
486 | lang_path, script_dirent.d_name); | |
487 | read_script_info(desc, script_path); | |
488 | } | |
489 | free(script_root); | |
490 | } | |
491 | } | |
492 | ||
493 | fprintf(stdout, "List of available trace scripts:\n"); | |
494 | list_for_each_entry(desc, &script_descs, node) { | |
495 | sprintf(first_half, "%s %s", desc->name, | |
496 | desc->args ? desc->args : ""); | |
497 | fprintf(stdout, " %-36s %s\n", first_half, | |
498 | desc->half_liner ? desc->half_liner : ""); | |
499 | } | |
500 | ||
501 | exit(0); | |
502 | } | |
503 | ||
3875294f TZ |
504 | static char *get_script_path(const char *script_root, const char *suffix) |
505 | { | |
506 | struct dirent *script_next, *lang_next, script_dirent, lang_dirent; | |
507 | char scripts_path[MAXPATHLEN]; | |
508 | char script_path[MAXPATHLEN]; | |
509 | DIR *scripts_dir, *lang_dir; | |
510 | char lang_path[MAXPATHLEN]; | |
511 | char *str, *__script_root; | |
512 | char *path = NULL; | |
513 | ||
514 | snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); | |
515 | ||
516 | scripts_dir = opendir(scripts_path); | |
517 | if (!scripts_dir) | |
518 | return NULL; | |
519 | ||
008f29d3 | 520 | for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) { |
3875294f TZ |
521 | snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, |
522 | lang_dirent.d_name); | |
523 | lang_dir = opendir(lang_path); | |
524 | if (!lang_dir) | |
525 | continue; | |
526 | ||
008f29d3 | 527 | for_each_script(lang_path, lang_dir, script_dirent, script_next) { |
3875294f | 528 | __script_root = strdup(script_dirent.d_name); |
965bb6be | 529 | str = (char *)ends_with(__script_root, suffix); |
3875294f TZ |
530 | if (str) { |
531 | *str = '\0'; | |
532 | if (strcmp(__script_root, script_root)) | |
533 | continue; | |
534 | snprintf(script_path, MAXPATHLEN, "%s/%s", | |
535 | lang_path, script_dirent.d_name); | |
536 | path = strdup(script_path); | |
537 | free(__script_root); | |
538 | break; | |
539 | } | |
540 | free(__script_root); | |
541 | } | |
542 | } | |
543 | ||
544 | return path; | |
545 | } | |
546 | ||
b5b87312 TZ |
547 | static bool is_top_script(const char *script_path) |
548 | { | |
965bb6be | 549 | return ends_with(script_path, "top") == NULL ? false : true; |
b5b87312 TZ |
550 | } |
551 | ||
552 | static int has_required_arg(char *script_path) | |
553 | { | |
554 | struct script_desc *desc; | |
555 | int n_args = 0; | |
556 | char *p; | |
557 | ||
558 | desc = script_desc__new(NULL); | |
559 | ||
560 | if (read_script_info(desc, script_path)) | |
561 | goto out; | |
562 | ||
563 | if (!desc->args) | |
564 | goto out; | |
565 | ||
566 | for (p = desc->args; *p; p++) | |
567 | if (*p == '<') | |
568 | n_args++; | |
569 | out: | |
570 | script_desc__delete(desc); | |
571 | ||
572 | return n_args; | |
573 | } | |
574 | ||
133dc4c3 IM |
575 | static const char * const script_usage[] = { |
576 | "perf script [<options>]", | |
577 | "perf script [<options>] record <script> [<record-options>] <command>", | |
578 | "perf script [<options>] report <script> [script-args]", | |
579 | "perf script [<options>] <script> [<record-options>] <command>", | |
580 | "perf script [<options>] <top-script> [script-args]", | |
5f9c39dc FW |
581 | NULL |
582 | }; | |
583 | ||
584 | static const struct option options[] = { | |
585 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, | |
586 | "dump raw trace in ASCII"), | |
c0555642 | 587 | OPT_INCR('v', "verbose", &verbose, |
5f9c39dc | 588 | "be more verbose (show symbol address, etc)"), |
4b9c0c59 | 589 | OPT_BOOLEAN('L', "Latency", &latency_format, |
cda48461 | 590 | "show latency attributes (irqs/preemption disabled, etc)"), |
4b9c0c59 TZ |
591 | OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts", |
592 | list_available_scripts), | |
956ffd02 TZ |
593 | OPT_CALLBACK('s', "script", NULL, "name", |
594 | "script file name (lang:script name, script name, or *)", | |
595 | parse_scriptname), | |
596 | OPT_STRING('g', "gen-script", &generate_script_lang, "lang", | |
133dc4c3 | 597 | "generate perf-script.xx script in specified language"), |
408f0d18 HM |
598 | OPT_STRING('i', "input", &input_name, "file", |
599 | "input file name"), | |
ffabd99e FW |
600 | OPT_BOOLEAN('d', "debug-mode", &debug_mode, |
601 | "do various checks like samples ordering and lost events"), | |
956ffd02 | 602 | |
1909629f | 603 | OPT_END() |
5f9c39dc FW |
604 | }; |
605 | ||
34c86ea9 TZ |
606 | static bool have_cmd(int argc, const char **argv) |
607 | { | |
608 | char **__argv = malloc(sizeof(const char *) * argc); | |
609 | ||
610 | if (!__argv) | |
611 | die("malloc"); | |
612 | memcpy(__argv, argv, sizeof(const char *) * argc); | |
613 | argc = parse_options(argc, (const char **)__argv, record_options, | |
614 | NULL, PARSE_OPT_STOP_AT_NON_OPTION); | |
615 | free(__argv); | |
616 | ||
617 | return argc != 0; | |
618 | } | |
619 | ||
133dc4c3 | 620 | int cmd_script(int argc, const char **argv, const char *prefix __used) |
5f9c39dc | 621 | { |
b5b87312 TZ |
622 | char *rec_script_path = NULL; |
623 | char *rep_script_path = NULL; | |
d8f66248 | 624 | struct perf_session *session; |
b5b87312 | 625 | char *script_path = NULL; |
3875294f | 626 | const char **__argv; |
b5b87312 TZ |
627 | bool system_wide; |
628 | int i, j, err; | |
3875294f | 629 | |
b5b87312 TZ |
630 | setup_scripting(); |
631 | ||
133dc4c3 | 632 | argc = parse_options(argc, argv, options, script_usage, |
b5b87312 TZ |
633 | PARSE_OPT_STOP_AT_NON_OPTION); |
634 | ||
635 | if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) { | |
636 | rec_script_path = get_script_path(argv[1], RECORD_SUFFIX); | |
637 | if (!rec_script_path) | |
638 | return cmd_record(argc, argv, NULL); | |
3875294f TZ |
639 | } |
640 | ||
b5b87312 TZ |
641 | if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) { |
642 | rep_script_path = get_script_path(argv[1], REPORT_SUFFIX); | |
643 | if (!rep_script_path) { | |
3875294f | 644 | fprintf(stderr, |
b5b87312 | 645 | "Please specify a valid report script" |
133dc4c3 | 646 | "(see 'perf script -l' for listing)\n"); |
3875294f TZ |
647 | return -1; |
648 | } | |
3875294f TZ |
649 | } |
650 | ||
44e668c6 BH |
651 | /* make sure PERF_EXEC_PATH is set for scripts */ |
652 | perf_set_argv_exec_path(perf_exec_path()); | |
653 | ||
b5b87312 | 654 | if (argc && !script_name && !rec_script_path && !rep_script_path) { |
a0cccc2e | 655 | int live_pipe[2]; |
b5b87312 | 656 | int rep_args; |
a0cccc2e TZ |
657 | pid_t pid; |
658 | ||
b5b87312 TZ |
659 | rec_script_path = get_script_path(argv[0], RECORD_SUFFIX); |
660 | rep_script_path = get_script_path(argv[0], REPORT_SUFFIX); | |
661 | ||
662 | if (!rec_script_path && !rep_script_path) { | |
663 | fprintf(stderr, " Couldn't find script %s\n\n See perf" | |
133dc4c3 IM |
664 | " script -l for available scripts.\n", argv[0]); |
665 | usage_with_options(script_usage, options); | |
a0cccc2e TZ |
666 | } |
667 | ||
b5b87312 TZ |
668 | if (is_top_script(argv[0])) { |
669 | rep_args = argc - 1; | |
670 | } else { | |
671 | int rec_args; | |
672 | ||
673 | rep_args = has_required_arg(rep_script_path); | |
674 | rec_args = (argc - 1) - rep_args; | |
675 | if (rec_args < 0) { | |
676 | fprintf(stderr, " %s script requires options." | |
133dc4c3 | 677 | "\n\n See perf script -l for available " |
b5b87312 | 678 | "scripts and options.\n", argv[0]); |
133dc4c3 | 679 | usage_with_options(script_usage, options); |
b5b87312 | 680 | } |
a0cccc2e TZ |
681 | } |
682 | ||
683 | if (pipe(live_pipe) < 0) { | |
684 | perror("failed to create pipe"); | |
685 | exit(-1); | |
686 | } | |
687 | ||
688 | pid = fork(); | |
689 | if (pid < 0) { | |
690 | perror("failed to fork"); | |
691 | exit(-1); | |
692 | } | |
693 | ||
694 | if (!pid) { | |
b5b87312 TZ |
695 | system_wide = true; |
696 | j = 0; | |
697 | ||
a0cccc2e TZ |
698 | dup2(live_pipe[1], 1); |
699 | close(live_pipe[0]); | |
700 | ||
b5b87312 TZ |
701 | if (!is_top_script(argv[0])) |
702 | system_wide = !have_cmd(argc - rep_args, | |
703 | &argv[rep_args]); | |
704 | ||
705 | __argv = malloc((argc + 6) * sizeof(const char *)); | |
e8719adf TZ |
706 | if (!__argv) |
707 | die("malloc"); | |
708 | ||
b5b87312 TZ |
709 | __argv[j++] = "/bin/sh"; |
710 | __argv[j++] = rec_script_path; | |
711 | if (system_wide) | |
712 | __argv[j++] = "-a"; | |
713 | __argv[j++] = "-q"; | |
714 | __argv[j++] = "-o"; | |
715 | __argv[j++] = "-"; | |
716 | for (i = rep_args + 1; i < argc; i++) | |
717 | __argv[j++] = argv[i]; | |
718 | __argv[j++] = NULL; | |
a0cccc2e TZ |
719 | |
720 | execvp("/bin/sh", (char **)__argv); | |
e8719adf | 721 | free(__argv); |
a0cccc2e TZ |
722 | exit(-1); |
723 | } | |
724 | ||
725 | dup2(live_pipe[0], 0); | |
726 | close(live_pipe[1]); | |
727 | ||
b5b87312 | 728 | __argv = malloc((argc + 4) * sizeof(const char *)); |
e8719adf TZ |
729 | if (!__argv) |
730 | die("malloc"); | |
b5b87312 TZ |
731 | j = 0; |
732 | __argv[j++] = "/bin/sh"; | |
733 | __argv[j++] = rep_script_path; | |
734 | for (i = 1; i < rep_args + 1; i++) | |
735 | __argv[j++] = argv[i]; | |
736 | __argv[j++] = "-i"; | |
737 | __argv[j++] = "-"; | |
738 | __argv[j++] = NULL; | |
a0cccc2e TZ |
739 | |
740 | execvp("/bin/sh", (char **)__argv); | |
e8719adf | 741 | free(__argv); |
a0cccc2e TZ |
742 | exit(-1); |
743 | } | |
744 | ||
b5b87312 TZ |
745 | if (rec_script_path) |
746 | script_path = rec_script_path; | |
747 | if (rep_script_path) | |
748 | script_path = rep_script_path; | |
34c86ea9 | 749 | |
b5b87312 TZ |
750 | if (script_path) { |
751 | system_wide = false; | |
752 | j = 0; | |
3875294f | 753 | |
b5b87312 TZ |
754 | if (rec_script_path) |
755 | system_wide = !have_cmd(argc - 1, &argv[1]); | |
34c86ea9 | 756 | |
b5b87312 | 757 | __argv = malloc((argc + 2) * sizeof(const char *)); |
e8719adf TZ |
758 | if (!__argv) |
759 | die("malloc"); | |
34c86ea9 TZ |
760 | __argv[j++] = "/bin/sh"; |
761 | __argv[j++] = script_path; | |
762 | if (system_wide) | |
763 | __argv[j++] = "-a"; | |
b5b87312 | 764 | for (i = 2; i < argc; i++) |
34c86ea9 TZ |
765 | __argv[j++] = argv[i]; |
766 | __argv[j++] = NULL; | |
3875294f TZ |
767 | |
768 | execvp("/bin/sh", (char **)__argv); | |
e8719adf | 769 | free(__argv); |
3875294f TZ |
770 | exit(-1); |
771 | } | |
956ffd02 | 772 | |
655000e7 ACM |
773 | if (symbol__init() < 0) |
774 | return -1; | |
cf4fee50 TZ |
775 | if (!script_name) |
776 | setup_pager(); | |
5f9c39dc | 777 | |
21ef97f0 | 778 | session = perf_session__new(input_name, O_RDONLY, 0, false, &event_ops); |
d8f66248 ACM |
779 | if (session == NULL) |
780 | return -ENOMEM; | |
781 | ||
c239da3b TZ |
782 | if (strcmp(input_name, "-") && |
783 | !perf_session__has_traces(session, "record -R")) | |
d549c769 ACM |
784 | return -EINVAL; |
785 | ||
956ffd02 TZ |
786 | if (generate_script_lang) { |
787 | struct stat perf_stat; | |
788 | ||
789 | int input = open(input_name, O_RDONLY); | |
790 | if (input < 0) { | |
791 | perror("failed to open file"); | |
792 | exit(-1); | |
793 | } | |
794 | ||
795 | err = fstat(input, &perf_stat); | |
796 | if (err < 0) { | |
797 | perror("failed to stat file"); | |
798 | exit(-1); | |
799 | } | |
800 | ||
801 | if (!perf_stat.st_size) { | |
802 | fprintf(stderr, "zero-sized file, nothing to do!\n"); | |
803 | exit(0); | |
804 | } | |
805 | ||
806 | scripting_ops = script_spec__lookup(generate_script_lang); | |
807 | if (!scripting_ops) { | |
808 | fprintf(stderr, "invalid language specifier"); | |
809 | return -1; | |
810 | } | |
811 | ||
133dc4c3 | 812 | err = scripting_ops->generate_script("perf-script"); |
956ffd02 TZ |
813 | goto out; |
814 | } | |
815 | ||
816 | if (script_name) { | |
586bc5cc | 817 | err = scripting_ops->start_script(script_name, argc, argv); |
956ffd02 TZ |
818 | if (err) |
819 | goto out; | |
133dc4c3 | 820 | pr_debug("perf script started with script %s\n\n", script_name); |
956ffd02 TZ |
821 | } |
822 | ||
133dc4c3 | 823 | err = __cmd_script(session); |
956ffd02 | 824 | |
d8f66248 | 825 | perf_session__delete(session); |
956ffd02 TZ |
826 | cleanup_scripting(); |
827 | out: | |
828 | return err; | |
5f9c39dc | 829 | } |