builtin-script.c 24 KB

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