builtin-script.c 22 KB

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