builtin-script.c 37 KB

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