builtin-script.c 38 KB

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