builtin-script.c 28 KB

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