builtin-script.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  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. /*
  849. * Return -1 if none is found, otherwise the actual scripts number.
  850. *
  851. * Currently the only user of this function is the script browser, which
  852. * will list all statically runnable scripts, select one, execute it and
  853. * show the output in a perf browser.
  854. */
  855. int find_scripts(char **scripts_array, char **scripts_path_array)
  856. {
  857. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  858. char scripts_path[MAXPATHLEN];
  859. DIR *scripts_dir, *lang_dir;
  860. char lang_path[MAXPATHLEN];
  861. char *temp;
  862. int i = 0;
  863. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  864. scripts_dir = opendir(scripts_path);
  865. if (!scripts_dir)
  866. return -1;
  867. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  868. snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
  869. lang_dirent.d_name);
  870. #ifdef NO_LIBPERL
  871. if (strstr(lang_path, "perl"))
  872. continue;
  873. #endif
  874. #ifdef NO_LIBPYTHON
  875. if (strstr(lang_path, "python"))
  876. continue;
  877. #endif
  878. lang_dir = opendir(lang_path);
  879. if (!lang_dir)
  880. continue;
  881. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  882. /* Skip those real time scripts: xxxtop.p[yl] */
  883. if (strstr(script_dirent.d_name, "top."))
  884. continue;
  885. sprintf(scripts_path_array[i], "%s/%s", lang_path,
  886. script_dirent.d_name);
  887. temp = strchr(script_dirent.d_name, '.');
  888. snprintf(scripts_array[i],
  889. (temp - script_dirent.d_name) + 1,
  890. "%s", script_dirent.d_name);
  891. i++;
  892. }
  893. }
  894. return i;
  895. }
  896. static char *get_script_path(const char *script_root, const char *suffix)
  897. {
  898. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  899. char scripts_path[MAXPATHLEN];
  900. char script_path[MAXPATHLEN];
  901. DIR *scripts_dir, *lang_dir;
  902. char lang_path[MAXPATHLEN];
  903. char *__script_root;
  904. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  905. scripts_dir = opendir(scripts_path);
  906. if (!scripts_dir)
  907. return NULL;
  908. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  909. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  910. lang_dirent.d_name);
  911. lang_dir = opendir(lang_path);
  912. if (!lang_dir)
  913. continue;
  914. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  915. __script_root = get_script_root(&script_dirent, suffix);
  916. if (__script_root && !strcmp(script_root, __script_root)) {
  917. free(__script_root);
  918. closedir(lang_dir);
  919. closedir(scripts_dir);
  920. snprintf(script_path, MAXPATHLEN, "%s/%s",
  921. lang_path, script_dirent.d_name);
  922. return strdup(script_path);
  923. }
  924. free(__script_root);
  925. }
  926. closedir(lang_dir);
  927. }
  928. closedir(scripts_dir);
  929. return NULL;
  930. }
  931. static bool is_top_script(const char *script_path)
  932. {
  933. return ends_with(script_path, "top") == NULL ? false : true;
  934. }
  935. static int has_required_arg(char *script_path)
  936. {
  937. struct script_desc *desc;
  938. int n_args = 0;
  939. char *p;
  940. desc = script_desc__new(NULL);
  941. if (read_script_info(desc, script_path))
  942. goto out;
  943. if (!desc->args)
  944. goto out;
  945. for (p = desc->args; *p; p++)
  946. if (*p == '<')
  947. n_args++;
  948. out:
  949. script_desc__delete(desc);
  950. return n_args;
  951. }
  952. static const char * const script_usage[] = {
  953. "perf script [<options>]",
  954. "perf script [<options>] record <script> [<record-options>] <command>",
  955. "perf script [<options>] report <script> [script-args]",
  956. "perf script [<options>] <script> [<record-options>] <command>",
  957. "perf script [<options>] <top-script> [script-args]",
  958. NULL
  959. };
  960. static const struct option options[] = {
  961. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  962. "dump raw trace in ASCII"),
  963. OPT_INCR('v', "verbose", &verbose,
  964. "be more verbose (show symbol address, etc)"),
  965. OPT_BOOLEAN('L', "Latency", &latency_format,
  966. "show latency attributes (irqs/preemption disabled, etc)"),
  967. OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
  968. list_available_scripts),
  969. OPT_CALLBACK('s', "script", NULL, "name",
  970. "script file name (lang:script name, script name, or *)",
  971. parse_scriptname),
  972. OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
  973. "generate perf-script.xx script in specified language"),
  974. OPT_STRING('i', "input", &input_name, "file",
  975. "input file name"),
  976. OPT_BOOLEAN('d', "debug-mode", &debug_mode,
  977. "do various checks like samples ordering and lost events"),
  978. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  979. "file", "vmlinux pathname"),
  980. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  981. "file", "kallsyms pathname"),
  982. OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
  983. "When printing symbols do not display call chain"),
  984. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  985. "Look for files with symbols relative to this directory"),
  986. OPT_CALLBACK('f', "fields", NULL, "str",
  987. "comma separated output fields prepend with 'type:'. "
  988. "Valid types: hw,sw,trace,raw. "
  989. "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
  990. "addr,symoff",
  991. parse_output_fields),
  992. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  993. "system-wide collection from all CPUs"),
  994. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  995. "only consider these symbols"),
  996. OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
  997. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  998. "only display events for these comms"),
  999. OPT_BOOLEAN('I', "show-info", &show_full_info,
  1000. "display extended information from perf.data file"),
  1001. OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
  1002. "Show the path of [kernel.kallsyms]"),
  1003. OPT_END()
  1004. };
  1005. static int have_cmd(int argc, const char **argv)
  1006. {
  1007. char **__argv = malloc(sizeof(const char *) * argc);
  1008. if (!__argv) {
  1009. pr_err("malloc failed\n");
  1010. return -1;
  1011. }
  1012. memcpy(__argv, argv, sizeof(const char *) * argc);
  1013. argc = parse_options(argc, (const char **)__argv, record_options,
  1014. NULL, PARSE_OPT_STOP_AT_NON_OPTION);
  1015. free(__argv);
  1016. system_wide = (argc == 0);
  1017. return 0;
  1018. }
  1019. int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
  1020. {
  1021. char *rec_script_path = NULL;
  1022. char *rep_script_path = NULL;
  1023. struct perf_session *session;
  1024. char *script_path = NULL;
  1025. const char **__argv;
  1026. int i, j, err;
  1027. setup_scripting();
  1028. argc = parse_options(argc, argv, options, script_usage,
  1029. PARSE_OPT_STOP_AT_NON_OPTION);
  1030. if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
  1031. rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
  1032. if (!rec_script_path)
  1033. return cmd_record(argc, argv, NULL);
  1034. }
  1035. if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
  1036. rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
  1037. if (!rep_script_path) {
  1038. fprintf(stderr,
  1039. "Please specify a valid report script"
  1040. "(see 'perf script -l' for listing)\n");
  1041. return -1;
  1042. }
  1043. }
  1044. /* make sure PERF_EXEC_PATH is set for scripts */
  1045. perf_set_argv_exec_path(perf_exec_path());
  1046. if (argc && !script_name && !rec_script_path && !rep_script_path) {
  1047. int live_pipe[2];
  1048. int rep_args;
  1049. pid_t pid;
  1050. rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
  1051. rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
  1052. if (!rec_script_path && !rep_script_path) {
  1053. fprintf(stderr, " Couldn't find script %s\n\n See perf"
  1054. " script -l for available scripts.\n", argv[0]);
  1055. usage_with_options(script_usage, options);
  1056. }
  1057. if (is_top_script(argv[0])) {
  1058. rep_args = argc - 1;
  1059. } else {
  1060. int rec_args;
  1061. rep_args = has_required_arg(rep_script_path);
  1062. rec_args = (argc - 1) - rep_args;
  1063. if (rec_args < 0) {
  1064. fprintf(stderr, " %s script requires options."
  1065. "\n\n See perf script -l for available "
  1066. "scripts and options.\n", argv[0]);
  1067. usage_with_options(script_usage, options);
  1068. }
  1069. }
  1070. if (pipe(live_pipe) < 0) {
  1071. perror("failed to create pipe");
  1072. return -1;
  1073. }
  1074. pid = fork();
  1075. if (pid < 0) {
  1076. perror("failed to fork");
  1077. return -1;
  1078. }
  1079. if (!pid) {
  1080. j = 0;
  1081. dup2(live_pipe[1], 1);
  1082. close(live_pipe[0]);
  1083. if (is_top_script(argv[0])) {
  1084. system_wide = true;
  1085. } else if (!system_wide) {
  1086. if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
  1087. err = -1;
  1088. goto out;
  1089. }
  1090. }
  1091. __argv = malloc((argc + 6) * sizeof(const char *));
  1092. if (!__argv) {
  1093. pr_err("malloc failed\n");
  1094. err = -ENOMEM;
  1095. goto out;
  1096. }
  1097. __argv[j++] = "/bin/sh";
  1098. __argv[j++] = rec_script_path;
  1099. if (system_wide)
  1100. __argv[j++] = "-a";
  1101. __argv[j++] = "-q";
  1102. __argv[j++] = "-o";
  1103. __argv[j++] = "-";
  1104. for (i = rep_args + 1; i < argc; i++)
  1105. __argv[j++] = argv[i];
  1106. __argv[j++] = NULL;
  1107. execvp("/bin/sh", (char **)__argv);
  1108. free(__argv);
  1109. exit(-1);
  1110. }
  1111. dup2(live_pipe[0], 0);
  1112. close(live_pipe[1]);
  1113. __argv = malloc((argc + 4) * sizeof(const char *));
  1114. if (!__argv) {
  1115. pr_err("malloc failed\n");
  1116. err = -ENOMEM;
  1117. goto out;
  1118. }
  1119. j = 0;
  1120. __argv[j++] = "/bin/sh";
  1121. __argv[j++] = rep_script_path;
  1122. for (i = 1; i < rep_args + 1; i++)
  1123. __argv[j++] = argv[i];
  1124. __argv[j++] = "-i";
  1125. __argv[j++] = "-";
  1126. __argv[j++] = NULL;
  1127. execvp("/bin/sh", (char **)__argv);
  1128. free(__argv);
  1129. exit(-1);
  1130. }
  1131. if (rec_script_path)
  1132. script_path = rec_script_path;
  1133. if (rep_script_path)
  1134. script_path = rep_script_path;
  1135. if (script_path) {
  1136. j = 0;
  1137. if (!rec_script_path)
  1138. system_wide = false;
  1139. else if (!system_wide) {
  1140. if (have_cmd(argc - 1, &argv[1]) != 0) {
  1141. err = -1;
  1142. goto out;
  1143. }
  1144. }
  1145. __argv = malloc((argc + 2) * sizeof(const char *));
  1146. if (!__argv) {
  1147. pr_err("malloc failed\n");
  1148. err = -ENOMEM;
  1149. goto out;
  1150. }
  1151. __argv[j++] = "/bin/sh";
  1152. __argv[j++] = script_path;
  1153. if (system_wide)
  1154. __argv[j++] = "-a";
  1155. for (i = 2; i < argc; i++)
  1156. __argv[j++] = argv[i];
  1157. __argv[j++] = NULL;
  1158. execvp("/bin/sh", (char **)__argv);
  1159. free(__argv);
  1160. exit(-1);
  1161. }
  1162. if (symbol__init() < 0)
  1163. return -1;
  1164. if (!script_name)
  1165. setup_pager();
  1166. session = perf_session__new(input_name, O_RDONLY, 0, false,
  1167. &perf_script);
  1168. if (session == NULL)
  1169. return -ENOMEM;
  1170. if (cpu_list) {
  1171. if (perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap))
  1172. return -1;
  1173. }
  1174. perf_session__fprintf_info(session, stdout, show_full_info);
  1175. if (!no_callchain)
  1176. symbol_conf.use_callchain = true;
  1177. else
  1178. symbol_conf.use_callchain = false;
  1179. if (generate_script_lang) {
  1180. struct stat perf_stat;
  1181. int input;
  1182. if (output_set_by_user()) {
  1183. fprintf(stderr,
  1184. "custom fields not supported for generated scripts");
  1185. return -1;
  1186. }
  1187. input = open(session->filename, O_RDONLY); /* input_name */
  1188. if (input < 0) {
  1189. perror("failed to open file");
  1190. return -1;
  1191. }
  1192. err = fstat(input, &perf_stat);
  1193. if (err < 0) {
  1194. perror("failed to stat file");
  1195. return -1;
  1196. }
  1197. if (!perf_stat.st_size) {
  1198. fprintf(stderr, "zero-sized file, nothing to do!\n");
  1199. return 0;
  1200. }
  1201. scripting_ops = script_spec__lookup(generate_script_lang);
  1202. if (!scripting_ops) {
  1203. fprintf(stderr, "invalid language specifier");
  1204. return -1;
  1205. }
  1206. err = scripting_ops->generate_script(session->pevent,
  1207. "perf-script");
  1208. goto out;
  1209. }
  1210. if (script_name) {
  1211. err = scripting_ops->start_script(script_name, argc, argv);
  1212. if (err)
  1213. goto out;
  1214. pr_debug("perf script started with script %s\n\n", script_name);
  1215. }
  1216. err = perf_session__check_output_opt(session);
  1217. if (err < 0)
  1218. goto out;
  1219. err = __cmd_script(session);
  1220. perf_session__delete(session);
  1221. cleanup_scripting();
  1222. out:
  1223. return err;
  1224. }