builtin-script.c 26 KB

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