builtin-script.c 31 KB

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