builtin-script.c 18 KB

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