builtin-script.c 18 KB

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