builtin-script.c 18 KB

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