builtin-script.c 35 KB

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