builtin-script.c 28 KB

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