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