builtin-script.c 31 KB

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