builtin-script.c 18 KB

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