builtin-script.c 44 KB

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