builtin-script.c 24 KB

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