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