builtin-script.c 19 KB

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