builtin-script.c 37 KB

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