builtin-script.c 44 KB

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