builtin-script.c 44 KB

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