builtin-script.c 36 KB

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