builtin-script.c 33 KB

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