builtin-script.c 37 KB

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