builtin-script.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  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 show_full_info;
  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 addr_location *al)
  337. {
  338. struct perf_event_attr *attr = &evsel->attr;
  339. struct thread *thread = al->thread;
  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 const char *input_name;
  397. static int process_sample_event(struct perf_tool *tool __maybe_unused,
  398. union perf_event *event,
  399. struct perf_sample *sample,
  400. struct perf_evsel *evsel,
  401. struct machine *machine)
  402. {
  403. struct addr_location al;
  404. struct thread *thread = machine__findnew_thread(machine, event->ip.tid);
  405. if (thread == NULL) {
  406. pr_debug("problem processing %d event, skipping it.\n",
  407. event->header.type);
  408. return -1;
  409. }
  410. if (debug_mode) {
  411. if (sample->time < last_timestamp) {
  412. pr_err("Samples misordered, previous: %" PRIu64
  413. " this: %" PRIu64 "\n", last_timestamp,
  414. sample->time);
  415. nr_unordered++;
  416. }
  417. last_timestamp = sample->time;
  418. return 0;
  419. }
  420. if (perf_event__preprocess_sample(event, machine, &al, sample, 0) < 0) {
  421. pr_err("problem processing %d event, skipping it.\n",
  422. event->header.type);
  423. return -1;
  424. }
  425. if (al.filtered)
  426. return 0;
  427. if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
  428. return 0;
  429. scripting_ops->process_event(event, sample, evsel, machine, &al);
  430. evsel->hists.stats.total_period += sample->period;
  431. return 0;
  432. }
  433. static struct perf_tool perf_script = {
  434. .sample = process_sample_event,
  435. .mmap = perf_event__process_mmap,
  436. .comm = perf_event__process_comm,
  437. .exit = perf_event__process_task,
  438. .fork = perf_event__process_task,
  439. .attr = perf_event__process_attr,
  440. .event_type = perf_event__process_event_type,
  441. .tracing_data = perf_event__process_tracing_data,
  442. .build_id = perf_event__process_build_id,
  443. .ordered_samples = true,
  444. .ordering_requires_timestamps = true,
  445. };
  446. extern volatile int session_done;
  447. static void sig_handler(int sig __maybe_unused)
  448. {
  449. session_done = 1;
  450. }
  451. static int __cmd_script(struct perf_session *session)
  452. {
  453. int ret;
  454. signal(SIGINT, sig_handler);
  455. ret = perf_session__process_events(session, &perf_script);
  456. if (debug_mode)
  457. pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
  458. return ret;
  459. }
  460. struct script_spec {
  461. struct list_head node;
  462. struct scripting_ops *ops;
  463. char spec[0];
  464. };
  465. static LIST_HEAD(script_specs);
  466. static struct script_spec *script_spec__new(const char *spec,
  467. struct scripting_ops *ops)
  468. {
  469. struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
  470. if (s != NULL) {
  471. strcpy(s->spec, spec);
  472. s->ops = ops;
  473. }
  474. return s;
  475. }
  476. static void script_spec__add(struct script_spec *s)
  477. {
  478. list_add_tail(&s->node, &script_specs);
  479. }
  480. static struct script_spec *script_spec__find(const char *spec)
  481. {
  482. struct script_spec *s;
  483. list_for_each_entry(s, &script_specs, node)
  484. if (strcasecmp(s->spec, spec) == 0)
  485. return s;
  486. return NULL;
  487. }
  488. static struct script_spec *script_spec__findnew(const char *spec,
  489. struct scripting_ops *ops)
  490. {
  491. struct script_spec *s = script_spec__find(spec);
  492. if (s)
  493. return s;
  494. s = script_spec__new(spec, ops);
  495. if (!s)
  496. return NULL;
  497. script_spec__add(s);
  498. return s;
  499. }
  500. int script_spec_register(const char *spec, struct scripting_ops *ops)
  501. {
  502. struct script_spec *s;
  503. s = script_spec__find(spec);
  504. if (s)
  505. return -1;
  506. s = script_spec__findnew(spec, ops);
  507. if (!s)
  508. return -1;
  509. return 0;
  510. }
  511. static struct scripting_ops *script_spec__lookup(const char *spec)
  512. {
  513. struct script_spec *s = script_spec__find(spec);
  514. if (!s)
  515. return NULL;
  516. return s->ops;
  517. }
  518. static void list_available_languages(void)
  519. {
  520. struct script_spec *s;
  521. fprintf(stderr, "\n");
  522. fprintf(stderr, "Scripting language extensions (used in "
  523. "perf script -s [spec:]script.[spec]):\n\n");
  524. list_for_each_entry(s, &script_specs, node)
  525. fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
  526. fprintf(stderr, "\n");
  527. }
  528. static int parse_scriptname(const struct option *opt __maybe_unused,
  529. const char *str, int unset __maybe_unused)
  530. {
  531. char spec[PATH_MAX];
  532. const char *script, *ext;
  533. int len;
  534. if (strcmp(str, "lang") == 0) {
  535. list_available_languages();
  536. exit(0);
  537. }
  538. script = strchr(str, ':');
  539. if (script) {
  540. len = script - str;
  541. if (len >= PATH_MAX) {
  542. fprintf(stderr, "invalid language specifier");
  543. return -1;
  544. }
  545. strncpy(spec, str, len);
  546. spec[len] = '\0';
  547. scripting_ops = script_spec__lookup(spec);
  548. if (!scripting_ops) {
  549. fprintf(stderr, "invalid language specifier");
  550. return -1;
  551. }
  552. script++;
  553. } else {
  554. script = str;
  555. ext = strrchr(script, '.');
  556. if (!ext) {
  557. fprintf(stderr, "invalid script extension");
  558. return -1;
  559. }
  560. scripting_ops = script_spec__lookup(++ext);
  561. if (!scripting_ops) {
  562. fprintf(stderr, "invalid script extension");
  563. return -1;
  564. }
  565. }
  566. script_name = strdup(script);
  567. return 0;
  568. }
  569. static int parse_output_fields(const struct option *opt __maybe_unused,
  570. const char *arg, int unset __maybe_unused)
  571. {
  572. char *tok;
  573. int i, imax = sizeof(all_output_options) / sizeof(struct output_option);
  574. int j;
  575. int rc = 0;
  576. char *str = strdup(arg);
  577. int type = -1;
  578. if (!str)
  579. return -ENOMEM;
  580. /* first word can state for which event type the user is specifying
  581. * the fields. If no type exists, the specified fields apply to all
  582. * event types found in the file minus the invalid fields for a type.
  583. */
  584. tok = strchr(str, ':');
  585. if (tok) {
  586. *tok = '\0';
  587. tok++;
  588. if (!strcmp(str, "hw"))
  589. type = PERF_TYPE_HARDWARE;
  590. else if (!strcmp(str, "sw"))
  591. type = PERF_TYPE_SOFTWARE;
  592. else if (!strcmp(str, "trace"))
  593. type = PERF_TYPE_TRACEPOINT;
  594. else if (!strcmp(str, "raw"))
  595. type = PERF_TYPE_RAW;
  596. else {
  597. fprintf(stderr, "Invalid event type in field string.\n");
  598. rc = -EINVAL;
  599. goto out;
  600. }
  601. if (output[type].user_set)
  602. pr_warning("Overriding previous field request for %s events.\n",
  603. event_type(type));
  604. output[type].fields = 0;
  605. output[type].user_set = true;
  606. output[type].wildcard_set = false;
  607. } else {
  608. tok = str;
  609. if (strlen(str) == 0) {
  610. fprintf(stderr,
  611. "Cannot set fields to 'none' for all event types.\n");
  612. rc = -EINVAL;
  613. goto out;
  614. }
  615. if (output_set_by_user())
  616. pr_warning("Overriding previous field request for all events.\n");
  617. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  618. output[j].fields = 0;
  619. output[j].user_set = true;
  620. output[j].wildcard_set = true;
  621. }
  622. }
  623. tok = strtok(tok, ",");
  624. while (tok) {
  625. for (i = 0; i < imax; ++i) {
  626. if (strcmp(tok, all_output_options[i].str) == 0)
  627. break;
  628. }
  629. if (i == imax) {
  630. fprintf(stderr, "Invalid field requested.\n");
  631. rc = -EINVAL;
  632. goto out;
  633. }
  634. if (type == -1) {
  635. /* add user option to all events types for
  636. * which it is valid
  637. */
  638. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  639. if (output[j].invalid_fields & all_output_options[i].field) {
  640. pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
  641. all_output_options[i].str, event_type(j));
  642. } else
  643. output[j].fields |= all_output_options[i].field;
  644. }
  645. } else {
  646. if (output[type].invalid_fields & all_output_options[i].field) {
  647. fprintf(stderr, "\'%s\' not valid for %s events.\n",
  648. all_output_options[i].str, event_type(type));
  649. rc = -EINVAL;
  650. goto out;
  651. }
  652. output[type].fields |= all_output_options[i].field;
  653. }
  654. tok = strtok(NULL, ",");
  655. }
  656. if (type >= 0) {
  657. if (output[type].fields == 0) {
  658. pr_debug("No fields requested for %s type. "
  659. "Events will not be displayed.\n", event_type(type));
  660. }
  661. }
  662. out:
  663. free(str);
  664. return rc;
  665. }
  666. /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
  667. static int is_directory(const char *base_path, const struct dirent *dent)
  668. {
  669. char path[PATH_MAX];
  670. struct stat st;
  671. sprintf(path, "%s/%s", base_path, dent->d_name);
  672. if (stat(path, &st))
  673. return 0;
  674. return S_ISDIR(st.st_mode);
  675. }
  676. #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
  677. while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
  678. lang_next) \
  679. if ((lang_dirent.d_type == DT_DIR || \
  680. (lang_dirent.d_type == DT_UNKNOWN && \
  681. is_directory(scripts_path, &lang_dirent))) && \
  682. (strcmp(lang_dirent.d_name, ".")) && \
  683. (strcmp(lang_dirent.d_name, "..")))
  684. #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
  685. while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
  686. script_next) \
  687. if (script_dirent.d_type != DT_DIR && \
  688. (script_dirent.d_type != DT_UNKNOWN || \
  689. !is_directory(lang_path, &script_dirent)))
  690. #define RECORD_SUFFIX "-record"
  691. #define REPORT_SUFFIX "-report"
  692. struct script_desc {
  693. struct list_head node;
  694. char *name;
  695. char *half_liner;
  696. char *args;
  697. };
  698. static LIST_HEAD(script_descs);
  699. static struct script_desc *script_desc__new(const char *name)
  700. {
  701. struct script_desc *s = zalloc(sizeof(*s));
  702. if (s != NULL && name)
  703. s->name = strdup(name);
  704. return s;
  705. }
  706. static void script_desc__delete(struct script_desc *s)
  707. {
  708. free(s->name);
  709. free(s->half_liner);
  710. free(s->args);
  711. free(s);
  712. }
  713. static void script_desc__add(struct script_desc *s)
  714. {
  715. list_add_tail(&s->node, &script_descs);
  716. }
  717. static struct script_desc *script_desc__find(const char *name)
  718. {
  719. struct script_desc *s;
  720. list_for_each_entry(s, &script_descs, node)
  721. if (strcasecmp(s->name, name) == 0)
  722. return s;
  723. return NULL;
  724. }
  725. static struct script_desc *script_desc__findnew(const char *name)
  726. {
  727. struct script_desc *s = script_desc__find(name);
  728. if (s)
  729. return s;
  730. s = script_desc__new(name);
  731. if (!s)
  732. goto out_delete_desc;
  733. script_desc__add(s);
  734. return s;
  735. out_delete_desc:
  736. script_desc__delete(s);
  737. return NULL;
  738. }
  739. static const char *ends_with(const char *str, const char *suffix)
  740. {
  741. size_t suffix_len = strlen(suffix);
  742. const char *p = str;
  743. if (strlen(str) > suffix_len) {
  744. p = str + strlen(str) - suffix_len;
  745. if (!strncmp(p, suffix, suffix_len))
  746. return p;
  747. }
  748. return NULL;
  749. }
  750. static char *ltrim(char *str)
  751. {
  752. int len = strlen(str);
  753. while (len && isspace(*str)) {
  754. len--;
  755. str++;
  756. }
  757. return str;
  758. }
  759. static int read_script_info(struct script_desc *desc, const char *filename)
  760. {
  761. char line[BUFSIZ], *p;
  762. FILE *fp;
  763. fp = fopen(filename, "r");
  764. if (!fp)
  765. return -1;
  766. while (fgets(line, sizeof(line), fp)) {
  767. p = ltrim(line);
  768. if (strlen(p) == 0)
  769. continue;
  770. if (*p != '#')
  771. continue;
  772. p++;
  773. if (strlen(p) && *p == '!')
  774. continue;
  775. p = ltrim(p);
  776. if (strlen(p) && p[strlen(p) - 1] == '\n')
  777. p[strlen(p) - 1] = '\0';
  778. if (!strncmp(p, "description:", strlen("description:"))) {
  779. p += strlen("description:");
  780. desc->half_liner = strdup(ltrim(p));
  781. continue;
  782. }
  783. if (!strncmp(p, "args:", strlen("args:"))) {
  784. p += strlen("args:");
  785. desc->args = strdup(ltrim(p));
  786. continue;
  787. }
  788. }
  789. fclose(fp);
  790. return 0;
  791. }
  792. static char *get_script_root(struct dirent *script_dirent, const char *suffix)
  793. {
  794. char *script_root, *str;
  795. script_root = strdup(script_dirent->d_name);
  796. if (!script_root)
  797. return NULL;
  798. str = (char *)ends_with(script_root, suffix);
  799. if (!str) {
  800. free(script_root);
  801. return NULL;
  802. }
  803. *str = '\0';
  804. return script_root;
  805. }
  806. static int list_available_scripts(const struct option *opt __maybe_unused,
  807. const char *s __maybe_unused,
  808. int unset __maybe_unused)
  809. {
  810. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  811. char scripts_path[MAXPATHLEN];
  812. DIR *scripts_dir, *lang_dir;
  813. char script_path[MAXPATHLEN];
  814. char lang_path[MAXPATHLEN];
  815. struct script_desc *desc;
  816. char first_half[BUFSIZ];
  817. char *script_root;
  818. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  819. scripts_dir = opendir(scripts_path);
  820. if (!scripts_dir)
  821. return -1;
  822. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  823. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  824. lang_dirent.d_name);
  825. lang_dir = opendir(lang_path);
  826. if (!lang_dir)
  827. continue;
  828. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  829. script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
  830. if (script_root) {
  831. desc = script_desc__findnew(script_root);
  832. snprintf(script_path, MAXPATHLEN, "%s/%s",
  833. lang_path, script_dirent.d_name);
  834. read_script_info(desc, script_path);
  835. free(script_root);
  836. }
  837. }
  838. }
  839. fprintf(stdout, "List of available trace scripts:\n");
  840. list_for_each_entry(desc, &script_descs, node) {
  841. sprintf(first_half, "%s %s", desc->name,
  842. desc->args ? desc->args : "");
  843. fprintf(stdout, " %-36s %s\n", first_half,
  844. desc->half_liner ? desc->half_liner : "");
  845. }
  846. exit(0);
  847. }
  848. static char *get_script_path(const char *script_root, const char *suffix)
  849. {
  850. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  851. char scripts_path[MAXPATHLEN];
  852. char script_path[MAXPATHLEN];
  853. DIR *scripts_dir, *lang_dir;
  854. char lang_path[MAXPATHLEN];
  855. char *__script_root;
  856. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  857. scripts_dir = opendir(scripts_path);
  858. if (!scripts_dir)
  859. return NULL;
  860. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  861. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  862. lang_dirent.d_name);
  863. lang_dir = opendir(lang_path);
  864. if (!lang_dir)
  865. continue;
  866. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  867. __script_root = get_script_root(&script_dirent, suffix);
  868. if (__script_root && !strcmp(script_root, __script_root)) {
  869. free(__script_root);
  870. closedir(lang_dir);
  871. closedir(scripts_dir);
  872. snprintf(script_path, MAXPATHLEN, "%s/%s",
  873. lang_path, script_dirent.d_name);
  874. return strdup(script_path);
  875. }
  876. free(__script_root);
  877. }
  878. closedir(lang_dir);
  879. }
  880. closedir(scripts_dir);
  881. return NULL;
  882. }
  883. static bool is_top_script(const char *script_path)
  884. {
  885. return ends_with(script_path, "top") == NULL ? false : true;
  886. }
  887. static int has_required_arg(char *script_path)
  888. {
  889. struct script_desc *desc;
  890. int n_args = 0;
  891. char *p;
  892. desc = script_desc__new(NULL);
  893. if (read_script_info(desc, script_path))
  894. goto out;
  895. if (!desc->args)
  896. goto out;
  897. for (p = desc->args; *p; p++)
  898. if (*p == '<')
  899. n_args++;
  900. out:
  901. script_desc__delete(desc);
  902. return n_args;
  903. }
  904. static const char * const script_usage[] = {
  905. "perf script [<options>]",
  906. "perf script [<options>] record <script> [<record-options>] <command>",
  907. "perf script [<options>] report <script> [script-args]",
  908. "perf script [<options>] <script> [<record-options>] <command>",
  909. "perf script [<options>] <top-script> [script-args]",
  910. NULL
  911. };
  912. static const struct option options[] = {
  913. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  914. "dump raw trace in ASCII"),
  915. OPT_INCR('v', "verbose", &verbose,
  916. "be more verbose (show symbol address, etc)"),
  917. OPT_BOOLEAN('L', "Latency", &latency_format,
  918. "show latency attributes (irqs/preemption disabled, etc)"),
  919. OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
  920. list_available_scripts),
  921. OPT_CALLBACK('s', "script", NULL, "name",
  922. "script file name (lang:script name, script name, or *)",
  923. parse_scriptname),
  924. OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
  925. "generate perf-script.xx script in specified language"),
  926. OPT_STRING('i', "input", &input_name, "file",
  927. "input file name"),
  928. OPT_BOOLEAN('d', "debug-mode", &debug_mode,
  929. "do various checks like samples ordering and lost events"),
  930. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  931. "file", "vmlinux pathname"),
  932. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  933. "file", "kallsyms pathname"),
  934. OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
  935. "When printing symbols do not display call chain"),
  936. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  937. "Look for files with symbols relative to this directory"),
  938. OPT_CALLBACK('f', "fields", NULL, "str",
  939. "comma separated output fields prepend with 'type:'. "
  940. "Valid types: hw,sw,trace,raw. "
  941. "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
  942. "addr,symoff",
  943. parse_output_fields),
  944. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  945. "system-wide collection from all CPUs"),
  946. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  947. "only consider these symbols"),
  948. OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
  949. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  950. "only display events for these comms"),
  951. OPT_BOOLEAN('I', "show-info", &show_full_info,
  952. "display extended information from perf.data file"),
  953. OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
  954. "Show the path of [kernel.kallsyms]"),
  955. OPT_END()
  956. };
  957. static int have_cmd(int argc, const char **argv)
  958. {
  959. char **__argv = malloc(sizeof(const char *) * argc);
  960. if (!__argv) {
  961. pr_err("malloc failed\n");
  962. return -1;
  963. }
  964. memcpy(__argv, argv, sizeof(const char *) * argc);
  965. argc = parse_options(argc, (const char **)__argv, record_options,
  966. NULL, PARSE_OPT_STOP_AT_NON_OPTION);
  967. free(__argv);
  968. system_wide = (argc == 0);
  969. return 0;
  970. }
  971. int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
  972. {
  973. char *rec_script_path = NULL;
  974. char *rep_script_path = NULL;
  975. struct perf_session *session;
  976. char *script_path = NULL;
  977. const char **__argv;
  978. int i, j, err;
  979. setup_scripting();
  980. argc = parse_options(argc, argv, options, script_usage,
  981. PARSE_OPT_STOP_AT_NON_OPTION);
  982. if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
  983. rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
  984. if (!rec_script_path)
  985. return cmd_record(argc, argv, NULL);
  986. }
  987. if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
  988. rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
  989. if (!rep_script_path) {
  990. fprintf(stderr,
  991. "Please specify a valid report script"
  992. "(see 'perf script -l' for listing)\n");
  993. return -1;
  994. }
  995. }
  996. /* make sure PERF_EXEC_PATH is set for scripts */
  997. perf_set_argv_exec_path(perf_exec_path());
  998. if (argc && !script_name && !rec_script_path && !rep_script_path) {
  999. int live_pipe[2];
  1000. int rep_args;
  1001. pid_t pid;
  1002. rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
  1003. rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
  1004. if (!rec_script_path && !rep_script_path) {
  1005. fprintf(stderr, " Couldn't find script %s\n\n See perf"
  1006. " script -l for available scripts.\n", argv[0]);
  1007. usage_with_options(script_usage, options);
  1008. }
  1009. if (is_top_script(argv[0])) {
  1010. rep_args = argc - 1;
  1011. } else {
  1012. int rec_args;
  1013. rep_args = has_required_arg(rep_script_path);
  1014. rec_args = (argc - 1) - rep_args;
  1015. if (rec_args < 0) {
  1016. fprintf(stderr, " %s script requires options."
  1017. "\n\n See perf script -l for available "
  1018. "scripts and options.\n", argv[0]);
  1019. usage_with_options(script_usage, options);
  1020. }
  1021. }
  1022. if (pipe(live_pipe) < 0) {
  1023. perror("failed to create pipe");
  1024. return -1;
  1025. }
  1026. pid = fork();
  1027. if (pid < 0) {
  1028. perror("failed to fork");
  1029. return -1;
  1030. }
  1031. if (!pid) {
  1032. j = 0;
  1033. dup2(live_pipe[1], 1);
  1034. close(live_pipe[0]);
  1035. if (is_top_script(argv[0])) {
  1036. system_wide = true;
  1037. } else if (!system_wide) {
  1038. if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
  1039. err = -1;
  1040. goto out;
  1041. }
  1042. }
  1043. __argv = malloc((argc + 6) * sizeof(const char *));
  1044. if (!__argv) {
  1045. pr_err("malloc failed\n");
  1046. err = -ENOMEM;
  1047. goto out;
  1048. }
  1049. __argv[j++] = "/bin/sh";
  1050. __argv[j++] = rec_script_path;
  1051. if (system_wide)
  1052. __argv[j++] = "-a";
  1053. __argv[j++] = "-q";
  1054. __argv[j++] = "-o";
  1055. __argv[j++] = "-";
  1056. for (i = rep_args + 1; i < argc; i++)
  1057. __argv[j++] = argv[i];
  1058. __argv[j++] = NULL;
  1059. execvp("/bin/sh", (char **)__argv);
  1060. free(__argv);
  1061. exit(-1);
  1062. }
  1063. dup2(live_pipe[0], 0);
  1064. close(live_pipe[1]);
  1065. __argv = malloc((argc + 4) * sizeof(const char *));
  1066. if (!__argv) {
  1067. pr_err("malloc failed\n");
  1068. err = -ENOMEM;
  1069. goto out;
  1070. }
  1071. j = 0;
  1072. __argv[j++] = "/bin/sh";
  1073. __argv[j++] = rep_script_path;
  1074. for (i = 1; i < rep_args + 1; i++)
  1075. __argv[j++] = argv[i];
  1076. __argv[j++] = "-i";
  1077. __argv[j++] = "-";
  1078. __argv[j++] = NULL;
  1079. execvp("/bin/sh", (char **)__argv);
  1080. free(__argv);
  1081. exit(-1);
  1082. }
  1083. if (rec_script_path)
  1084. script_path = rec_script_path;
  1085. if (rep_script_path)
  1086. script_path = rep_script_path;
  1087. if (script_path) {
  1088. j = 0;
  1089. if (!rec_script_path)
  1090. system_wide = false;
  1091. else if (!system_wide) {
  1092. if (have_cmd(argc - 1, &argv[1]) != 0) {
  1093. err = -1;
  1094. goto out;
  1095. }
  1096. }
  1097. __argv = malloc((argc + 2) * sizeof(const char *));
  1098. if (!__argv) {
  1099. pr_err("malloc failed\n");
  1100. err = -ENOMEM;
  1101. goto out;
  1102. }
  1103. __argv[j++] = "/bin/sh";
  1104. __argv[j++] = script_path;
  1105. if (system_wide)
  1106. __argv[j++] = "-a";
  1107. for (i = 2; i < argc; i++)
  1108. __argv[j++] = argv[i];
  1109. __argv[j++] = NULL;
  1110. execvp("/bin/sh", (char **)__argv);
  1111. free(__argv);
  1112. exit(-1);
  1113. }
  1114. if (symbol__init() < 0)
  1115. return -1;
  1116. if (!script_name)
  1117. setup_pager();
  1118. session = perf_session__new(input_name, O_RDONLY, 0, false,
  1119. &perf_script);
  1120. if (session == NULL)
  1121. return -ENOMEM;
  1122. if (cpu_list) {
  1123. if (perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap))
  1124. return -1;
  1125. }
  1126. perf_session__fprintf_info(session, stdout, show_full_info);
  1127. if (!no_callchain)
  1128. symbol_conf.use_callchain = true;
  1129. else
  1130. symbol_conf.use_callchain = false;
  1131. if (generate_script_lang) {
  1132. struct stat perf_stat;
  1133. int input;
  1134. if (output_set_by_user()) {
  1135. fprintf(stderr,
  1136. "custom fields not supported for generated scripts");
  1137. return -1;
  1138. }
  1139. input = open(session->filename, O_RDONLY); /* input_name */
  1140. if (input < 0) {
  1141. perror("failed to open file");
  1142. return -1;
  1143. }
  1144. err = fstat(input, &perf_stat);
  1145. if (err < 0) {
  1146. perror("failed to stat file");
  1147. return -1;
  1148. }
  1149. if (!perf_stat.st_size) {
  1150. fprintf(stderr, "zero-sized file, nothing to do!\n");
  1151. return 0;
  1152. }
  1153. scripting_ops = script_spec__lookup(generate_script_lang);
  1154. if (!scripting_ops) {
  1155. fprintf(stderr, "invalid language specifier");
  1156. return -1;
  1157. }
  1158. err = scripting_ops->generate_script(session->pevent,
  1159. "perf-script");
  1160. goto out;
  1161. }
  1162. if (script_name) {
  1163. err = scripting_ops->start_script(script_name, argc, argv);
  1164. if (err)
  1165. goto out;
  1166. pr_debug("perf script started with script %s\n\n", script_name);
  1167. }
  1168. err = perf_session__check_output_opt(session);
  1169. if (err < 0)
  1170. goto out;
  1171. err = __cmd_script(session);
  1172. perf_session__delete(session);
  1173. cleanup_scripting();
  1174. out:
  1175. return err;
  1176. }