| 1 | #include "builtin.h" |
| 2 | |
| 3 | #include "perf.h" |
| 4 | #include "util/cache.h" |
| 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" |
| 10 | #include "util/symbol.h" |
| 11 | #include "util/thread.h" |
| 12 | #include "util/trace-event.h" |
| 13 | #include "util/parse-options.h" |
| 14 | #include "util/util.h" |
| 15 | |
| 16 | static char const *script_name; |
| 17 | static char const *generate_script_lang; |
| 18 | static bool debug_mode; |
| 19 | static u64 last_timestamp; |
| 20 | static u64 nr_unordered; |
| 21 | extern const struct option record_options[]; |
| 22 | |
| 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 | |
| 37 | static int default_start_script(const char *script __unused, |
| 38 | int argc __unused, |
| 39 | const char **argv __unused) |
| 40 | { |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | static int default_stop_script(void) |
| 45 | { |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | static int default_generate_script(const char *outfile __unused) |
| 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, |
| 57 | .process_event = process_event, |
| 58 | .generate_script = default_generate_script, |
| 59 | }; |
| 60 | |
| 61 | static struct scripting_ops *scripting_ops; |
| 62 | |
| 63 | static void setup_scripting(void) |
| 64 | { |
| 65 | setup_perl_scripting(); |
| 66 | setup_python_scripting(); |
| 67 | |
| 68 | scripting_ops = &default_scripting_ops; |
| 69 | } |
| 70 | |
| 71 | static int cleanup_scripting(void) |
| 72 | { |
| 73 | pr_debug("\nperf script stopped\n"); |
| 74 | |
| 75 | return scripting_ops->stop_script(); |
| 76 | } |
| 77 | |
| 78 | static char const *input_name = "perf.data"; |
| 79 | |
| 80 | static int process_sample_event(union perf_event *event, |
| 81 | struct perf_sample *sample, |
| 82 | struct perf_session *session) |
| 83 | { |
| 84 | struct thread *thread = perf_session__findnew(session, event->ip.pid); |
| 85 | |
| 86 | if (thread == NULL) { |
| 87 | pr_debug("problem processing %d event, skipping it.\n", |
| 88 | event->header.type); |
| 89 | return -1; |
| 90 | } |
| 91 | |
| 92 | if (session->sample_type & PERF_SAMPLE_RAW) { |
| 93 | if (debug_mode) { |
| 94 | if (sample->time < last_timestamp) { |
| 95 | pr_err("Samples misordered, previous: %" PRIu64 |
| 96 | " this: %" PRIu64 "\n", last_timestamp, |
| 97 | sample->time); |
| 98 | nr_unordered++; |
| 99 | } |
| 100 | last_timestamp = sample->time; |
| 101 | return 0; |
| 102 | } |
| 103 | scripting_ops->process_event(event, sample, session, thread); |
| 104 | } |
| 105 | |
| 106 | session->hists.stats.total_period += sample->period; |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static struct perf_event_ops event_ops = { |
| 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, |
| 117 | .ordered_samples = true, |
| 118 | .ordering_requires_timestamps = true, |
| 119 | }; |
| 120 | |
| 121 | extern volatile int session_done; |
| 122 | |
| 123 | static void sig_handler(int sig __unused) |
| 124 | { |
| 125 | session_done = 1; |
| 126 | } |
| 127 | |
| 128 | static int __cmd_script(struct perf_session *session) |
| 129 | { |
| 130 | int ret; |
| 131 | |
| 132 | signal(SIGINT, sig_handler); |
| 133 | |
| 134 | ret = perf_session__process_events(session, &event_ops); |
| 135 | |
| 136 | if (debug_mode) |
| 137 | pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered); |
| 138 | |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | struct script_spec { |
| 143 | struct list_head node; |
| 144 | struct scripting_ops *ops; |
| 145 | char spec[0]; |
| 146 | }; |
| 147 | |
| 148 | static LIST_HEAD(script_specs); |
| 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 " |
| 236 | "perf script -s [spec:]script.[spec]):\n\n"); |
| 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 | |
| 251 | if (strcmp(str, "lang") == 0) { |
| 252 | list_available_languages(); |
| 253 | exit(0); |
| 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; |
| 273 | ext = strrchr(script, '.'); |
| 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 | |
| 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)\ |
| 304 | while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \ |
| 305 | lang_next) \ |
| 306 | if ((lang_dirent.d_type == DT_DIR || \ |
| 307 | (lang_dirent.d_type == DT_UNKNOWN && \ |
| 308 | is_directory(scripts_path, &lang_dirent))) && \ |
| 309 | (strcmp(lang_dirent.d_name, ".")) && \ |
| 310 | (strcmp(lang_dirent.d_name, ".."))) |
| 311 | |
| 312 | #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\ |
| 313 | while (!readdir_r(lang_dir, &script_dirent, &script_next) && \ |
| 314 | script_next) \ |
| 315 | if (script_dirent.d_type != DT_DIR && \ |
| 316 | (script_dirent.d_type != DT_UNKNOWN || \ |
| 317 | !is_directory(lang_path, &script_dirent))) |
| 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 | |
| 330 | static LIST_HEAD(script_descs); |
| 331 | |
| 332 | static struct script_desc *script_desc__new(const char *name) |
| 333 | { |
| 334 | struct script_desc *s = zalloc(sizeof(*s)); |
| 335 | |
| 336 | if (s != NULL && name) |
| 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); |
| 345 | free(s->half_liner); |
| 346 | free(s->args); |
| 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 | |
| 386 | static const char *ends_with(const char *str, const char *suffix) |
| 387 | { |
| 388 | size_t suffix_len = strlen(suffix); |
| 389 | const char *p = str; |
| 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 | |
| 472 | for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) { |
| 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 | |
| 479 | for_each_script(lang_path, lang_dir, script_dirent, script_next) { |
| 480 | script_root = strdup(script_dirent.d_name); |
| 481 | str = (char *)ends_with(script_root, REPORT_SUFFIX); |
| 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 | |
| 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 | |
| 520 | for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) { |
| 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 | |
| 527 | for_each_script(lang_path, lang_dir, script_dirent, script_next) { |
| 528 | __script_root = strdup(script_dirent.d_name); |
| 529 | str = (char *)ends_with(__script_root, suffix); |
| 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 | |
| 547 | static bool is_top_script(const char *script_path) |
| 548 | { |
| 549 | return ends_with(script_path, "top") == NULL ? false : true; |
| 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 | |
| 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]", |
| 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"), |
| 587 | OPT_INCR('v', "verbose", &verbose, |
| 588 | "be more verbose (show symbol address, etc)"), |
| 589 | OPT_BOOLEAN('L', "Latency", &latency_format, |
| 590 | "show latency attributes (irqs/preemption disabled, etc)"), |
| 591 | OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts", |
| 592 | list_available_scripts), |
| 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", |
| 597 | "generate perf-script.xx script in specified language"), |
| 598 | OPT_STRING('i', "input", &input_name, "file", |
| 599 | "input file name"), |
| 600 | OPT_BOOLEAN('d', "debug-mode", &debug_mode, |
| 601 | "do various checks like samples ordering and lost events"), |
| 602 | |
| 603 | OPT_END() |
| 604 | }; |
| 605 | |
| 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 | |
| 620 | int cmd_script(int argc, const char **argv, const char *prefix __used) |
| 621 | { |
| 622 | char *rec_script_path = NULL; |
| 623 | char *rep_script_path = NULL; |
| 624 | struct perf_session *session; |
| 625 | char *script_path = NULL; |
| 626 | const char **__argv; |
| 627 | bool system_wide; |
| 628 | int i, j, err; |
| 629 | |
| 630 | setup_scripting(); |
| 631 | |
| 632 | argc = parse_options(argc, argv, options, script_usage, |
| 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); |
| 639 | } |
| 640 | |
| 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) { |
| 644 | fprintf(stderr, |
| 645 | "Please specify a valid report script" |
| 646 | "(see 'perf script -l' for listing)\n"); |
| 647 | return -1; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | /* make sure PERF_EXEC_PATH is set for scripts */ |
| 652 | perf_set_argv_exec_path(perf_exec_path()); |
| 653 | |
| 654 | if (argc && !script_name && !rec_script_path && !rep_script_path) { |
| 655 | int live_pipe[2]; |
| 656 | int rep_args; |
| 657 | pid_t pid; |
| 658 | |
| 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" |
| 664 | " script -l for available scripts.\n", argv[0]); |
| 665 | usage_with_options(script_usage, options); |
| 666 | } |
| 667 | |
| 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." |
| 677 | "\n\n See perf script -l for available " |
| 678 | "scripts and options.\n", argv[0]); |
| 679 | usage_with_options(script_usage, options); |
| 680 | } |
| 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) { |
| 695 | system_wide = true; |
| 696 | j = 0; |
| 697 | |
| 698 | dup2(live_pipe[1], 1); |
| 699 | close(live_pipe[0]); |
| 700 | |
| 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 *)); |
| 706 | if (!__argv) |
| 707 | die("malloc"); |
| 708 | |
| 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; |
| 719 | |
| 720 | execvp("/bin/sh", (char **)__argv); |
| 721 | free(__argv); |
| 722 | exit(-1); |
| 723 | } |
| 724 | |
| 725 | dup2(live_pipe[0], 0); |
| 726 | close(live_pipe[1]); |
| 727 | |
| 728 | __argv = malloc((argc + 4) * sizeof(const char *)); |
| 729 | if (!__argv) |
| 730 | die("malloc"); |
| 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; |
| 739 | |
| 740 | execvp("/bin/sh", (char **)__argv); |
| 741 | free(__argv); |
| 742 | exit(-1); |
| 743 | } |
| 744 | |
| 745 | if (rec_script_path) |
| 746 | script_path = rec_script_path; |
| 747 | if (rep_script_path) |
| 748 | script_path = rep_script_path; |
| 749 | |
| 750 | if (script_path) { |
| 751 | system_wide = false; |
| 752 | j = 0; |
| 753 | |
| 754 | if (rec_script_path) |
| 755 | system_wide = !have_cmd(argc - 1, &argv[1]); |
| 756 | |
| 757 | __argv = malloc((argc + 2) * sizeof(const char *)); |
| 758 | if (!__argv) |
| 759 | die("malloc"); |
| 760 | __argv[j++] = "/bin/sh"; |
| 761 | __argv[j++] = script_path; |
| 762 | if (system_wide) |
| 763 | __argv[j++] = "-a"; |
| 764 | for (i = 2; i < argc; i++) |
| 765 | __argv[j++] = argv[i]; |
| 766 | __argv[j++] = NULL; |
| 767 | |
| 768 | execvp("/bin/sh", (char **)__argv); |
| 769 | free(__argv); |
| 770 | exit(-1); |
| 771 | } |
| 772 | |
| 773 | if (symbol__init() < 0) |
| 774 | return -1; |
| 775 | if (!script_name) |
| 776 | setup_pager(); |
| 777 | |
| 778 | session = perf_session__new(input_name, O_RDONLY, 0, false, &event_ops); |
| 779 | if (session == NULL) |
| 780 | return -ENOMEM; |
| 781 | |
| 782 | if (strcmp(input_name, "-") && |
| 783 | !perf_session__has_traces(session, "record -R")) |
| 784 | return -EINVAL; |
| 785 | |
| 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 | |
| 812 | err = scripting_ops->generate_script("perf-script"); |
| 813 | goto out; |
| 814 | } |
| 815 | |
| 816 | if (script_name) { |
| 817 | err = scripting_ops->start_script(script_name, argc, argv); |
| 818 | if (err) |
| 819 | goto out; |
| 820 | pr_debug("perf script started with script %s\n\n", script_name); |
| 821 | } |
| 822 | |
| 823 | err = __cmd_script(session); |
| 824 | |
| 825 | perf_session__delete(session); |
| 826 | cleanup_scripting(); |
| 827 | out: |
| 828 | return err; |
| 829 | } |