builtin-script.c 29 KB

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