builtin-script.c 46 KB

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