builtin-script.c 26 KB

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