builtin-script.c 18 KB

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