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