builtin-script.c 21 KB

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