builtin-script.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961
  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. bool show_switch_events;
  523. };
  524. static int process_attr(struct perf_tool *tool, union perf_event *event,
  525. struct perf_evlist **pevlist)
  526. {
  527. struct perf_script *scr = container_of(tool, struct perf_script, tool);
  528. struct perf_evlist *evlist;
  529. struct perf_evsel *evsel, *pos;
  530. int err;
  531. err = perf_event__process_attr(tool, event, pevlist);
  532. if (err)
  533. return err;
  534. evlist = *pevlist;
  535. evsel = perf_evlist__last(*pevlist);
  536. if (evsel->attr.type >= PERF_TYPE_MAX)
  537. return 0;
  538. evlist__for_each(evlist, pos) {
  539. if (pos->attr.type == evsel->attr.type && pos != evsel)
  540. return 0;
  541. }
  542. set_print_ip_opts(&evsel->attr);
  543. return perf_evsel__check_attr(evsel, scr->session);
  544. }
  545. static int process_comm_event(struct perf_tool *tool,
  546. union perf_event *event,
  547. struct perf_sample *sample,
  548. struct machine *machine)
  549. {
  550. struct thread *thread;
  551. struct perf_script *script = container_of(tool, struct perf_script, tool);
  552. struct perf_session *session = script->session;
  553. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  554. int ret = -1;
  555. thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
  556. if (thread == NULL) {
  557. pr_debug("problem processing COMM event, skipping it.\n");
  558. return -1;
  559. }
  560. if (perf_event__process_comm(tool, event, sample, machine) < 0)
  561. goto out;
  562. if (!evsel->attr.sample_id_all) {
  563. sample->cpu = 0;
  564. sample->time = 0;
  565. sample->tid = event->comm.tid;
  566. sample->pid = event->comm.pid;
  567. }
  568. print_sample_start(sample, thread, evsel);
  569. perf_event__fprintf(event, stdout);
  570. ret = 0;
  571. out:
  572. thread__put(thread);
  573. return ret;
  574. }
  575. static int process_fork_event(struct perf_tool *tool,
  576. union perf_event *event,
  577. struct perf_sample *sample,
  578. struct machine *machine)
  579. {
  580. struct thread *thread;
  581. struct perf_script *script = container_of(tool, struct perf_script, tool);
  582. struct perf_session *session = script->session;
  583. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  584. if (perf_event__process_fork(tool, event, sample, machine) < 0)
  585. return -1;
  586. thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
  587. if (thread == NULL) {
  588. pr_debug("problem processing FORK event, skipping it.\n");
  589. return -1;
  590. }
  591. if (!evsel->attr.sample_id_all) {
  592. sample->cpu = 0;
  593. sample->time = event->fork.time;
  594. sample->tid = event->fork.tid;
  595. sample->pid = event->fork.pid;
  596. }
  597. print_sample_start(sample, thread, evsel);
  598. perf_event__fprintf(event, stdout);
  599. thread__put(thread);
  600. return 0;
  601. }
  602. static int process_exit_event(struct perf_tool *tool,
  603. union perf_event *event,
  604. struct perf_sample *sample,
  605. struct machine *machine)
  606. {
  607. int err = 0;
  608. struct thread *thread;
  609. struct perf_script *script = container_of(tool, struct perf_script, tool);
  610. struct perf_session *session = script->session;
  611. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  612. thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
  613. if (thread == NULL) {
  614. pr_debug("problem processing EXIT event, skipping it.\n");
  615. return -1;
  616. }
  617. if (!evsel->attr.sample_id_all) {
  618. sample->cpu = 0;
  619. sample->time = 0;
  620. sample->tid = event->comm.tid;
  621. sample->pid = event->comm.pid;
  622. }
  623. print_sample_start(sample, thread, evsel);
  624. perf_event__fprintf(event, stdout);
  625. if (perf_event__process_exit(tool, event, sample, machine) < 0)
  626. err = -1;
  627. thread__put(thread);
  628. return err;
  629. }
  630. static int process_mmap_event(struct perf_tool *tool,
  631. union perf_event *event,
  632. struct perf_sample *sample,
  633. struct machine *machine)
  634. {
  635. struct thread *thread;
  636. struct perf_script *script = container_of(tool, struct perf_script, tool);
  637. struct perf_session *session = script->session;
  638. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  639. if (perf_event__process_mmap(tool, event, sample, machine) < 0)
  640. return -1;
  641. thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
  642. if (thread == NULL) {
  643. pr_debug("problem processing MMAP event, skipping it.\n");
  644. return -1;
  645. }
  646. if (!evsel->attr.sample_id_all) {
  647. sample->cpu = 0;
  648. sample->time = 0;
  649. sample->tid = event->mmap.tid;
  650. sample->pid = event->mmap.pid;
  651. }
  652. print_sample_start(sample, thread, evsel);
  653. perf_event__fprintf(event, stdout);
  654. thread__put(thread);
  655. return 0;
  656. }
  657. static int process_mmap2_event(struct perf_tool *tool,
  658. union perf_event *event,
  659. struct perf_sample *sample,
  660. struct machine *machine)
  661. {
  662. struct thread *thread;
  663. struct perf_script *script = container_of(tool, struct perf_script, tool);
  664. struct perf_session *session = script->session;
  665. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  666. if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
  667. return -1;
  668. thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
  669. if (thread == NULL) {
  670. pr_debug("problem processing MMAP2 event, skipping it.\n");
  671. return -1;
  672. }
  673. if (!evsel->attr.sample_id_all) {
  674. sample->cpu = 0;
  675. sample->time = 0;
  676. sample->tid = event->mmap2.tid;
  677. sample->pid = event->mmap2.pid;
  678. }
  679. print_sample_start(sample, thread, evsel);
  680. perf_event__fprintf(event, stdout);
  681. thread__put(thread);
  682. return 0;
  683. }
  684. static int process_switch_event(struct perf_tool *tool,
  685. union perf_event *event,
  686. struct perf_sample *sample,
  687. struct machine *machine)
  688. {
  689. struct thread *thread;
  690. struct perf_script *script = container_of(tool, struct perf_script, tool);
  691. struct perf_session *session = script->session;
  692. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  693. if (perf_event__process_switch(tool, event, sample, machine) < 0)
  694. return -1;
  695. thread = machine__findnew_thread(machine, sample->pid,
  696. sample->tid);
  697. if (thread == NULL) {
  698. pr_debug("problem processing SWITCH event, skipping it.\n");
  699. return -1;
  700. }
  701. print_sample_start(sample, thread, evsel);
  702. perf_event__fprintf(event, stdout);
  703. thread__put(thread);
  704. return 0;
  705. }
  706. static void sig_handler(int sig __maybe_unused)
  707. {
  708. session_done = 1;
  709. }
  710. static int __cmd_script(struct perf_script *script)
  711. {
  712. int ret;
  713. signal(SIGINT, sig_handler);
  714. /* override event processing functions */
  715. if (script->show_task_events) {
  716. script->tool.comm = process_comm_event;
  717. script->tool.fork = process_fork_event;
  718. script->tool.exit = process_exit_event;
  719. }
  720. if (script->show_mmap_events) {
  721. script->tool.mmap = process_mmap_event;
  722. script->tool.mmap2 = process_mmap2_event;
  723. }
  724. if (script->show_switch_events)
  725. script->tool.context_switch = process_switch_event;
  726. ret = perf_session__process_events(script->session);
  727. if (debug_mode)
  728. pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
  729. return ret;
  730. }
  731. struct script_spec {
  732. struct list_head node;
  733. struct scripting_ops *ops;
  734. char spec[0];
  735. };
  736. static LIST_HEAD(script_specs);
  737. static struct script_spec *script_spec__new(const char *spec,
  738. struct scripting_ops *ops)
  739. {
  740. struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
  741. if (s != NULL) {
  742. strcpy(s->spec, spec);
  743. s->ops = ops;
  744. }
  745. return s;
  746. }
  747. static void script_spec__add(struct script_spec *s)
  748. {
  749. list_add_tail(&s->node, &script_specs);
  750. }
  751. static struct script_spec *script_spec__find(const char *spec)
  752. {
  753. struct script_spec *s;
  754. list_for_each_entry(s, &script_specs, node)
  755. if (strcasecmp(s->spec, spec) == 0)
  756. return s;
  757. return NULL;
  758. }
  759. static struct script_spec *script_spec__findnew(const char *spec,
  760. struct scripting_ops *ops)
  761. {
  762. struct script_spec *s = script_spec__find(spec);
  763. if (s)
  764. return s;
  765. s = script_spec__new(spec, ops);
  766. if (!s)
  767. return NULL;
  768. script_spec__add(s);
  769. return s;
  770. }
  771. int script_spec_register(const char *spec, struct scripting_ops *ops)
  772. {
  773. struct script_spec *s;
  774. s = script_spec__find(spec);
  775. if (s)
  776. return -1;
  777. s = script_spec__findnew(spec, ops);
  778. if (!s)
  779. return -1;
  780. return 0;
  781. }
  782. static struct scripting_ops *script_spec__lookup(const char *spec)
  783. {
  784. struct script_spec *s = script_spec__find(spec);
  785. if (!s)
  786. return NULL;
  787. return s->ops;
  788. }
  789. static void list_available_languages(void)
  790. {
  791. struct script_spec *s;
  792. fprintf(stderr, "\n");
  793. fprintf(stderr, "Scripting language extensions (used in "
  794. "perf script -s [spec:]script.[spec]):\n\n");
  795. list_for_each_entry(s, &script_specs, node)
  796. fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
  797. fprintf(stderr, "\n");
  798. }
  799. static int parse_scriptname(const struct option *opt __maybe_unused,
  800. const char *str, int unset __maybe_unused)
  801. {
  802. char spec[PATH_MAX];
  803. const char *script, *ext;
  804. int len;
  805. if (strcmp(str, "lang") == 0) {
  806. list_available_languages();
  807. exit(0);
  808. }
  809. script = strchr(str, ':');
  810. if (script) {
  811. len = script - str;
  812. if (len >= PATH_MAX) {
  813. fprintf(stderr, "invalid language specifier");
  814. return -1;
  815. }
  816. strncpy(spec, str, len);
  817. spec[len] = '\0';
  818. scripting_ops = script_spec__lookup(spec);
  819. if (!scripting_ops) {
  820. fprintf(stderr, "invalid language specifier");
  821. return -1;
  822. }
  823. script++;
  824. } else {
  825. script = str;
  826. ext = strrchr(script, '.');
  827. if (!ext) {
  828. fprintf(stderr, "invalid script extension");
  829. return -1;
  830. }
  831. scripting_ops = script_spec__lookup(++ext);
  832. if (!scripting_ops) {
  833. fprintf(stderr, "invalid script extension");
  834. return -1;
  835. }
  836. }
  837. script_name = strdup(script);
  838. return 0;
  839. }
  840. static int parse_output_fields(const struct option *opt __maybe_unused,
  841. const char *arg, int unset __maybe_unused)
  842. {
  843. char *tok;
  844. int i, imax = ARRAY_SIZE(all_output_options);
  845. int j;
  846. int rc = 0;
  847. char *str = strdup(arg);
  848. int type = -1;
  849. if (!str)
  850. return -ENOMEM;
  851. /* first word can state for which event type the user is specifying
  852. * the fields. If no type exists, the specified fields apply to all
  853. * event types found in the file minus the invalid fields for a type.
  854. */
  855. tok = strchr(str, ':');
  856. if (tok) {
  857. *tok = '\0';
  858. tok++;
  859. if (!strcmp(str, "hw"))
  860. type = PERF_TYPE_HARDWARE;
  861. else if (!strcmp(str, "sw"))
  862. type = PERF_TYPE_SOFTWARE;
  863. else if (!strcmp(str, "trace"))
  864. type = PERF_TYPE_TRACEPOINT;
  865. else if (!strcmp(str, "raw"))
  866. type = PERF_TYPE_RAW;
  867. else {
  868. fprintf(stderr, "Invalid event type in field string.\n");
  869. rc = -EINVAL;
  870. goto out;
  871. }
  872. if (output[type].user_set)
  873. pr_warning("Overriding previous field request for %s events.\n",
  874. event_type(type));
  875. output[type].fields = 0;
  876. output[type].user_set = true;
  877. output[type].wildcard_set = false;
  878. } else {
  879. tok = str;
  880. if (strlen(str) == 0) {
  881. fprintf(stderr,
  882. "Cannot set fields to 'none' for all event types.\n");
  883. rc = -EINVAL;
  884. goto out;
  885. }
  886. if (output_set_by_user())
  887. pr_warning("Overriding previous field request for all events.\n");
  888. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  889. output[j].fields = 0;
  890. output[j].user_set = true;
  891. output[j].wildcard_set = true;
  892. }
  893. }
  894. for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
  895. for (i = 0; i < imax; ++i) {
  896. if (strcmp(tok, all_output_options[i].str) == 0)
  897. break;
  898. }
  899. if (i == imax && strcmp(tok, "flags") == 0) {
  900. print_flags = true;
  901. continue;
  902. }
  903. if (i == imax) {
  904. fprintf(stderr, "Invalid field requested.\n");
  905. rc = -EINVAL;
  906. goto out;
  907. }
  908. if (type == -1) {
  909. /* add user option to all events types for
  910. * which it is valid
  911. */
  912. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  913. if (output[j].invalid_fields & all_output_options[i].field) {
  914. pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
  915. all_output_options[i].str, event_type(j));
  916. } else
  917. output[j].fields |= all_output_options[i].field;
  918. }
  919. } else {
  920. if (output[type].invalid_fields & all_output_options[i].field) {
  921. fprintf(stderr, "\'%s\' not valid for %s events.\n",
  922. all_output_options[i].str, event_type(type));
  923. rc = -EINVAL;
  924. goto out;
  925. }
  926. output[type].fields |= all_output_options[i].field;
  927. }
  928. }
  929. if (type >= 0) {
  930. if (output[type].fields == 0) {
  931. pr_debug("No fields requested for %s type. "
  932. "Events will not be displayed.\n", event_type(type));
  933. }
  934. }
  935. out:
  936. free(str);
  937. return rc;
  938. }
  939. /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
  940. static int is_directory(const char *base_path, const struct dirent *dent)
  941. {
  942. char path[PATH_MAX];
  943. struct stat st;
  944. sprintf(path, "%s/%s", base_path, dent->d_name);
  945. if (stat(path, &st))
  946. return 0;
  947. return S_ISDIR(st.st_mode);
  948. }
  949. #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
  950. while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
  951. lang_next) \
  952. if ((lang_dirent.d_type == DT_DIR || \
  953. (lang_dirent.d_type == DT_UNKNOWN && \
  954. is_directory(scripts_path, &lang_dirent))) && \
  955. (strcmp(lang_dirent.d_name, ".")) && \
  956. (strcmp(lang_dirent.d_name, "..")))
  957. #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
  958. while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
  959. script_next) \
  960. if (script_dirent.d_type != DT_DIR && \
  961. (script_dirent.d_type != DT_UNKNOWN || \
  962. !is_directory(lang_path, &script_dirent)))
  963. #define RECORD_SUFFIX "-record"
  964. #define REPORT_SUFFIX "-report"
  965. struct script_desc {
  966. struct list_head node;
  967. char *name;
  968. char *half_liner;
  969. char *args;
  970. };
  971. static LIST_HEAD(script_descs);
  972. static struct script_desc *script_desc__new(const char *name)
  973. {
  974. struct script_desc *s = zalloc(sizeof(*s));
  975. if (s != NULL && name)
  976. s->name = strdup(name);
  977. return s;
  978. }
  979. static void script_desc__delete(struct script_desc *s)
  980. {
  981. zfree(&s->name);
  982. zfree(&s->half_liner);
  983. zfree(&s->args);
  984. free(s);
  985. }
  986. static void script_desc__add(struct script_desc *s)
  987. {
  988. list_add_tail(&s->node, &script_descs);
  989. }
  990. static struct script_desc *script_desc__find(const char *name)
  991. {
  992. struct script_desc *s;
  993. list_for_each_entry(s, &script_descs, node)
  994. if (strcasecmp(s->name, name) == 0)
  995. return s;
  996. return NULL;
  997. }
  998. static struct script_desc *script_desc__findnew(const char *name)
  999. {
  1000. struct script_desc *s = script_desc__find(name);
  1001. if (s)
  1002. return s;
  1003. s = script_desc__new(name);
  1004. if (!s)
  1005. goto out_delete_desc;
  1006. script_desc__add(s);
  1007. return s;
  1008. out_delete_desc:
  1009. script_desc__delete(s);
  1010. return NULL;
  1011. }
  1012. static const char *ends_with(const char *str, const char *suffix)
  1013. {
  1014. size_t suffix_len = strlen(suffix);
  1015. const char *p = str;
  1016. if (strlen(str) > suffix_len) {
  1017. p = str + strlen(str) - suffix_len;
  1018. if (!strncmp(p, suffix, suffix_len))
  1019. return p;
  1020. }
  1021. return NULL;
  1022. }
  1023. static int read_script_info(struct script_desc *desc, const char *filename)
  1024. {
  1025. char line[BUFSIZ], *p;
  1026. FILE *fp;
  1027. fp = fopen(filename, "r");
  1028. if (!fp)
  1029. return -1;
  1030. while (fgets(line, sizeof(line), fp)) {
  1031. p = ltrim(line);
  1032. if (strlen(p) == 0)
  1033. continue;
  1034. if (*p != '#')
  1035. continue;
  1036. p++;
  1037. if (strlen(p) && *p == '!')
  1038. continue;
  1039. p = ltrim(p);
  1040. if (strlen(p) && p[strlen(p) - 1] == '\n')
  1041. p[strlen(p) - 1] = '\0';
  1042. if (!strncmp(p, "description:", strlen("description:"))) {
  1043. p += strlen("description:");
  1044. desc->half_liner = strdup(ltrim(p));
  1045. continue;
  1046. }
  1047. if (!strncmp(p, "args:", strlen("args:"))) {
  1048. p += strlen("args:");
  1049. desc->args = strdup(ltrim(p));
  1050. continue;
  1051. }
  1052. }
  1053. fclose(fp);
  1054. return 0;
  1055. }
  1056. static char *get_script_root(struct dirent *script_dirent, const char *suffix)
  1057. {
  1058. char *script_root, *str;
  1059. script_root = strdup(script_dirent->d_name);
  1060. if (!script_root)
  1061. return NULL;
  1062. str = (char *)ends_with(script_root, suffix);
  1063. if (!str) {
  1064. free(script_root);
  1065. return NULL;
  1066. }
  1067. *str = '\0';
  1068. return script_root;
  1069. }
  1070. static int list_available_scripts(const struct option *opt __maybe_unused,
  1071. const char *s __maybe_unused,
  1072. int unset __maybe_unused)
  1073. {
  1074. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  1075. char scripts_path[MAXPATHLEN];
  1076. DIR *scripts_dir, *lang_dir;
  1077. char script_path[MAXPATHLEN];
  1078. char lang_path[MAXPATHLEN];
  1079. struct script_desc *desc;
  1080. char first_half[BUFSIZ];
  1081. char *script_root;
  1082. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  1083. scripts_dir = opendir(scripts_path);
  1084. if (!scripts_dir)
  1085. return -1;
  1086. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  1087. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  1088. lang_dirent.d_name);
  1089. lang_dir = opendir(lang_path);
  1090. if (!lang_dir)
  1091. continue;
  1092. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  1093. script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
  1094. if (script_root) {
  1095. desc = script_desc__findnew(script_root);
  1096. snprintf(script_path, MAXPATHLEN, "%s/%s",
  1097. lang_path, script_dirent.d_name);
  1098. read_script_info(desc, script_path);
  1099. free(script_root);
  1100. }
  1101. }
  1102. }
  1103. fprintf(stdout, "List of available trace scripts:\n");
  1104. list_for_each_entry(desc, &script_descs, node) {
  1105. sprintf(first_half, "%s %s", desc->name,
  1106. desc->args ? desc->args : "");
  1107. fprintf(stdout, " %-36s %s\n", first_half,
  1108. desc->half_liner ? desc->half_liner : "");
  1109. }
  1110. exit(0);
  1111. }
  1112. /*
  1113. * Some scripts specify the required events in their "xxx-record" file,
  1114. * this function will check if the events in perf.data match those
  1115. * mentioned in the "xxx-record".
  1116. *
  1117. * Fixme: All existing "xxx-record" are all in good formats "-e event ",
  1118. * which is covered well now. And new parsing code should be added to
  1119. * cover the future complexing formats like event groups etc.
  1120. */
  1121. static int check_ev_match(char *dir_name, char *scriptname,
  1122. struct perf_session *session)
  1123. {
  1124. char filename[MAXPATHLEN], evname[128];
  1125. char line[BUFSIZ], *p;
  1126. struct perf_evsel *pos;
  1127. int match, len;
  1128. FILE *fp;
  1129. sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
  1130. fp = fopen(filename, "r");
  1131. if (!fp)
  1132. return -1;
  1133. while (fgets(line, sizeof(line), fp)) {
  1134. p = ltrim(line);
  1135. if (*p == '#')
  1136. continue;
  1137. while (strlen(p)) {
  1138. p = strstr(p, "-e");
  1139. if (!p)
  1140. break;
  1141. p += 2;
  1142. p = ltrim(p);
  1143. len = strcspn(p, " \t");
  1144. if (!len)
  1145. break;
  1146. snprintf(evname, len + 1, "%s", p);
  1147. match = 0;
  1148. evlist__for_each(session->evlist, pos) {
  1149. if (!strcmp(perf_evsel__name(pos), evname)) {
  1150. match = 1;
  1151. break;
  1152. }
  1153. }
  1154. if (!match) {
  1155. fclose(fp);
  1156. return -1;
  1157. }
  1158. }
  1159. }
  1160. fclose(fp);
  1161. return 0;
  1162. }
  1163. /*
  1164. * Return -1 if none is found, otherwise the actual scripts number.
  1165. *
  1166. * Currently the only user of this function is the script browser, which
  1167. * will list all statically runnable scripts, select one, execute it and
  1168. * show the output in a perf browser.
  1169. */
  1170. int find_scripts(char **scripts_array, char **scripts_path_array)
  1171. {
  1172. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  1173. char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
  1174. DIR *scripts_dir, *lang_dir;
  1175. struct perf_session *session;
  1176. struct perf_data_file file = {
  1177. .path = input_name,
  1178. .mode = PERF_DATA_MODE_READ,
  1179. };
  1180. char *temp;
  1181. int i = 0;
  1182. session = perf_session__new(&file, false, NULL);
  1183. if (!session)
  1184. return -1;
  1185. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  1186. scripts_dir = opendir(scripts_path);
  1187. if (!scripts_dir) {
  1188. perf_session__delete(session);
  1189. return -1;
  1190. }
  1191. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  1192. snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
  1193. lang_dirent.d_name);
  1194. #ifdef NO_LIBPERL
  1195. if (strstr(lang_path, "perl"))
  1196. continue;
  1197. #endif
  1198. #ifdef NO_LIBPYTHON
  1199. if (strstr(lang_path, "python"))
  1200. continue;
  1201. #endif
  1202. lang_dir = opendir(lang_path);
  1203. if (!lang_dir)
  1204. continue;
  1205. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  1206. /* Skip those real time scripts: xxxtop.p[yl] */
  1207. if (strstr(script_dirent.d_name, "top."))
  1208. continue;
  1209. sprintf(scripts_path_array[i], "%s/%s", lang_path,
  1210. script_dirent.d_name);
  1211. temp = strchr(script_dirent.d_name, '.');
  1212. snprintf(scripts_array[i],
  1213. (temp - script_dirent.d_name) + 1,
  1214. "%s", script_dirent.d_name);
  1215. if (check_ev_match(lang_path,
  1216. scripts_array[i], session))
  1217. continue;
  1218. i++;
  1219. }
  1220. closedir(lang_dir);
  1221. }
  1222. closedir(scripts_dir);
  1223. perf_session__delete(session);
  1224. return i;
  1225. }
  1226. static char *get_script_path(const char *script_root, const char *suffix)
  1227. {
  1228. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  1229. char scripts_path[MAXPATHLEN];
  1230. char script_path[MAXPATHLEN];
  1231. DIR *scripts_dir, *lang_dir;
  1232. char lang_path[MAXPATHLEN];
  1233. char *__script_root;
  1234. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  1235. scripts_dir = opendir(scripts_path);
  1236. if (!scripts_dir)
  1237. return NULL;
  1238. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  1239. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  1240. lang_dirent.d_name);
  1241. lang_dir = opendir(lang_path);
  1242. if (!lang_dir)
  1243. continue;
  1244. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  1245. __script_root = get_script_root(&script_dirent, suffix);
  1246. if (__script_root && !strcmp(script_root, __script_root)) {
  1247. free(__script_root);
  1248. closedir(lang_dir);
  1249. closedir(scripts_dir);
  1250. snprintf(script_path, MAXPATHLEN, "%s/%s",
  1251. lang_path, script_dirent.d_name);
  1252. return strdup(script_path);
  1253. }
  1254. free(__script_root);
  1255. }
  1256. closedir(lang_dir);
  1257. }
  1258. closedir(scripts_dir);
  1259. return NULL;
  1260. }
  1261. static bool is_top_script(const char *script_path)
  1262. {
  1263. return ends_with(script_path, "top") == NULL ? false : true;
  1264. }
  1265. static int has_required_arg(char *script_path)
  1266. {
  1267. struct script_desc *desc;
  1268. int n_args = 0;
  1269. char *p;
  1270. desc = script_desc__new(NULL);
  1271. if (read_script_info(desc, script_path))
  1272. goto out;
  1273. if (!desc->args)
  1274. goto out;
  1275. for (p = desc->args; *p; p++)
  1276. if (*p == '<')
  1277. n_args++;
  1278. out:
  1279. script_desc__delete(desc);
  1280. return n_args;
  1281. }
  1282. static int have_cmd(int argc, const char **argv)
  1283. {
  1284. char **__argv = malloc(sizeof(const char *) * argc);
  1285. if (!__argv) {
  1286. pr_err("malloc failed\n");
  1287. return -1;
  1288. }
  1289. memcpy(__argv, argv, sizeof(const char *) * argc);
  1290. argc = parse_options(argc, (const char **)__argv, record_options,
  1291. NULL, PARSE_OPT_STOP_AT_NON_OPTION);
  1292. free(__argv);
  1293. system_wide = (argc == 0);
  1294. return 0;
  1295. }
  1296. static void script__setup_sample_type(struct perf_script *script)
  1297. {
  1298. struct perf_session *session = script->session;
  1299. u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
  1300. if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
  1301. if ((sample_type & PERF_SAMPLE_REGS_USER) &&
  1302. (sample_type & PERF_SAMPLE_STACK_USER))
  1303. callchain_param.record_mode = CALLCHAIN_DWARF;
  1304. else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
  1305. callchain_param.record_mode = CALLCHAIN_LBR;
  1306. else
  1307. callchain_param.record_mode = CALLCHAIN_FP;
  1308. }
  1309. }
  1310. int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
  1311. {
  1312. bool show_full_info = false;
  1313. bool header = false;
  1314. bool header_only = false;
  1315. bool script_started = false;
  1316. char *rec_script_path = NULL;
  1317. char *rep_script_path = NULL;
  1318. struct perf_session *session;
  1319. struct itrace_synth_opts itrace_synth_opts = { .set = false, };
  1320. char *script_path = NULL;
  1321. const char **__argv;
  1322. int i, j, err = 0;
  1323. struct perf_script script = {
  1324. .tool = {
  1325. .sample = process_sample_event,
  1326. .mmap = perf_event__process_mmap,
  1327. .mmap2 = perf_event__process_mmap2,
  1328. .comm = perf_event__process_comm,
  1329. .exit = perf_event__process_exit,
  1330. .fork = perf_event__process_fork,
  1331. .attr = process_attr,
  1332. .tracing_data = perf_event__process_tracing_data,
  1333. .build_id = perf_event__process_build_id,
  1334. .id_index = perf_event__process_id_index,
  1335. .auxtrace_info = perf_event__process_auxtrace_info,
  1336. .auxtrace = perf_event__process_auxtrace,
  1337. .auxtrace_error = perf_event__process_auxtrace_error,
  1338. .ordered_events = true,
  1339. .ordering_requires_timestamps = true,
  1340. },
  1341. };
  1342. struct perf_data_file file = {
  1343. .mode = PERF_DATA_MODE_READ,
  1344. };
  1345. const struct option options[] = {
  1346. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1347. "dump raw trace in ASCII"),
  1348. OPT_INCR('v', "verbose", &verbose,
  1349. "be more verbose (show symbol address, etc)"),
  1350. OPT_BOOLEAN('L', "Latency", &latency_format,
  1351. "show latency attributes (irqs/preemption disabled, etc)"),
  1352. OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
  1353. list_available_scripts),
  1354. OPT_CALLBACK('s', "script", NULL, "name",
  1355. "script file name (lang:script name, script name, or *)",
  1356. parse_scriptname),
  1357. OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
  1358. "generate perf-script.xx script in specified language"),
  1359. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  1360. OPT_BOOLEAN('d', "debug-mode", &debug_mode,
  1361. "do various checks like samples ordering and lost events"),
  1362. OPT_BOOLEAN(0, "header", &header, "Show data header."),
  1363. OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
  1364. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  1365. "file", "vmlinux pathname"),
  1366. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  1367. "file", "kallsyms pathname"),
  1368. OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
  1369. "When printing symbols do not display call chain"),
  1370. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  1371. "Look for files with symbols relative to this directory"),
  1372. OPT_CALLBACK('F', "fields", NULL, "str",
  1373. "comma separated output fields prepend with 'type:'. "
  1374. "Valid types: hw,sw,trace,raw. "
  1375. "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
  1376. "addr,symoff,period,flags", parse_output_fields),
  1377. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  1378. "system-wide collection from all CPUs"),
  1379. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  1380. "only consider these symbols"),
  1381. OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
  1382. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  1383. "only display events for these comms"),
  1384. OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
  1385. "only consider symbols in these pids"),
  1386. OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
  1387. "only consider symbols in these tids"),
  1388. OPT_BOOLEAN('I', "show-info", &show_full_info,
  1389. "display extended information from perf.data file"),
  1390. OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
  1391. "Show the path of [kernel.kallsyms]"),
  1392. OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
  1393. "Show the fork/comm/exit events"),
  1394. OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
  1395. "Show the mmap events"),
  1396. OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
  1397. "Show context switch events (if recorded)"),
  1398. OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
  1399. OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
  1400. "Instruction Tracing options",
  1401. itrace_parse_synth_opts),
  1402. OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
  1403. "Show full source file name path for source lines"),
  1404. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  1405. "Enable symbol demangling"),
  1406. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  1407. "Enable kernel symbol demangling"),
  1408. OPT_END()
  1409. };
  1410. const char * const script_subcommands[] = { "record", "report", NULL };
  1411. const char *script_usage[] = {
  1412. "perf script [<options>]",
  1413. "perf script [<options>] record <script> [<record-options>] <command>",
  1414. "perf script [<options>] report <script> [script-args]",
  1415. "perf script [<options>] <script> [<record-options>] <command>",
  1416. "perf script [<options>] <top-script> [script-args]",
  1417. NULL
  1418. };
  1419. setup_scripting();
  1420. argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
  1421. PARSE_OPT_STOP_AT_NON_OPTION);
  1422. file.path = input_name;
  1423. if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
  1424. rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
  1425. if (!rec_script_path)
  1426. return cmd_record(argc, argv, NULL);
  1427. }
  1428. if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
  1429. rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
  1430. if (!rep_script_path) {
  1431. fprintf(stderr,
  1432. "Please specify a valid report script"
  1433. "(see 'perf script -l' for listing)\n");
  1434. return -1;
  1435. }
  1436. }
  1437. /* make sure PERF_EXEC_PATH is set for scripts */
  1438. perf_set_argv_exec_path(perf_exec_path());
  1439. if (argc && !script_name && !rec_script_path && !rep_script_path) {
  1440. int live_pipe[2];
  1441. int rep_args;
  1442. pid_t pid;
  1443. rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
  1444. rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
  1445. if (!rec_script_path && !rep_script_path) {
  1446. fprintf(stderr, " Couldn't find script %s\n\n See perf"
  1447. " script -l for available scripts.\n", argv[0]);
  1448. usage_with_options(script_usage, options);
  1449. }
  1450. if (is_top_script(argv[0])) {
  1451. rep_args = argc - 1;
  1452. } else {
  1453. int rec_args;
  1454. rep_args = has_required_arg(rep_script_path);
  1455. rec_args = (argc - 1) - rep_args;
  1456. if (rec_args < 0) {
  1457. fprintf(stderr, " %s script requires options."
  1458. "\n\n See perf script -l for available "
  1459. "scripts and options.\n", argv[0]);
  1460. usage_with_options(script_usage, options);
  1461. }
  1462. }
  1463. if (pipe(live_pipe) < 0) {
  1464. perror("failed to create pipe");
  1465. return -1;
  1466. }
  1467. pid = fork();
  1468. if (pid < 0) {
  1469. perror("failed to fork");
  1470. return -1;
  1471. }
  1472. if (!pid) {
  1473. j = 0;
  1474. dup2(live_pipe[1], 1);
  1475. close(live_pipe[0]);
  1476. if (is_top_script(argv[0])) {
  1477. system_wide = true;
  1478. } else if (!system_wide) {
  1479. if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
  1480. err = -1;
  1481. goto out;
  1482. }
  1483. }
  1484. __argv = malloc((argc + 6) * sizeof(const char *));
  1485. if (!__argv) {
  1486. pr_err("malloc failed\n");
  1487. err = -ENOMEM;
  1488. goto out;
  1489. }
  1490. __argv[j++] = "/bin/sh";
  1491. __argv[j++] = rec_script_path;
  1492. if (system_wide)
  1493. __argv[j++] = "-a";
  1494. __argv[j++] = "-q";
  1495. __argv[j++] = "-o";
  1496. __argv[j++] = "-";
  1497. for (i = rep_args + 1; i < argc; i++)
  1498. __argv[j++] = argv[i];
  1499. __argv[j++] = NULL;
  1500. execvp("/bin/sh", (char **)__argv);
  1501. free(__argv);
  1502. exit(-1);
  1503. }
  1504. dup2(live_pipe[0], 0);
  1505. close(live_pipe[1]);
  1506. __argv = malloc((argc + 4) * sizeof(const char *));
  1507. if (!__argv) {
  1508. pr_err("malloc failed\n");
  1509. err = -ENOMEM;
  1510. goto out;
  1511. }
  1512. j = 0;
  1513. __argv[j++] = "/bin/sh";
  1514. __argv[j++] = rep_script_path;
  1515. for (i = 1; i < rep_args + 1; i++)
  1516. __argv[j++] = argv[i];
  1517. __argv[j++] = "-i";
  1518. __argv[j++] = "-";
  1519. __argv[j++] = NULL;
  1520. execvp("/bin/sh", (char **)__argv);
  1521. free(__argv);
  1522. exit(-1);
  1523. }
  1524. if (rec_script_path)
  1525. script_path = rec_script_path;
  1526. if (rep_script_path)
  1527. script_path = rep_script_path;
  1528. if (script_path) {
  1529. j = 0;
  1530. if (!rec_script_path)
  1531. system_wide = false;
  1532. else if (!system_wide) {
  1533. if (have_cmd(argc - 1, &argv[1]) != 0) {
  1534. err = -1;
  1535. goto out;
  1536. }
  1537. }
  1538. __argv = malloc((argc + 2) * sizeof(const char *));
  1539. if (!__argv) {
  1540. pr_err("malloc failed\n");
  1541. err = -ENOMEM;
  1542. goto out;
  1543. }
  1544. __argv[j++] = "/bin/sh";
  1545. __argv[j++] = script_path;
  1546. if (system_wide)
  1547. __argv[j++] = "-a";
  1548. for (i = 2; i < argc; i++)
  1549. __argv[j++] = argv[i];
  1550. __argv[j++] = NULL;
  1551. execvp("/bin/sh", (char **)__argv);
  1552. free(__argv);
  1553. exit(-1);
  1554. }
  1555. if (!script_name)
  1556. setup_pager();
  1557. session = perf_session__new(&file, false, &script.tool);
  1558. if (session == NULL)
  1559. return -1;
  1560. if (header || header_only) {
  1561. perf_session__fprintf_info(session, stdout, show_full_info);
  1562. if (header_only)
  1563. goto out_delete;
  1564. }
  1565. if (symbol__init(&session->header.env) < 0)
  1566. goto out_delete;
  1567. script.session = session;
  1568. script__setup_sample_type(&script);
  1569. session->itrace_synth_opts = &itrace_synth_opts;
  1570. if (cpu_list) {
  1571. err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
  1572. if (err < 0)
  1573. goto out_delete;
  1574. }
  1575. if (!no_callchain)
  1576. symbol_conf.use_callchain = true;
  1577. else
  1578. symbol_conf.use_callchain = false;
  1579. if (session->tevent.pevent &&
  1580. pevent_set_function_resolver(session->tevent.pevent,
  1581. machine__resolve_kernel_addr,
  1582. &session->machines.host) < 0) {
  1583. pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
  1584. return -1;
  1585. }
  1586. if (generate_script_lang) {
  1587. struct stat perf_stat;
  1588. int input;
  1589. if (output_set_by_user()) {
  1590. fprintf(stderr,
  1591. "custom fields not supported for generated scripts");
  1592. err = -EINVAL;
  1593. goto out_delete;
  1594. }
  1595. input = open(file.path, O_RDONLY); /* input_name */
  1596. if (input < 0) {
  1597. err = -errno;
  1598. perror("failed to open file");
  1599. goto out_delete;
  1600. }
  1601. err = fstat(input, &perf_stat);
  1602. if (err < 0) {
  1603. perror("failed to stat file");
  1604. goto out_delete;
  1605. }
  1606. if (!perf_stat.st_size) {
  1607. fprintf(stderr, "zero-sized file, nothing to do!\n");
  1608. goto out_delete;
  1609. }
  1610. scripting_ops = script_spec__lookup(generate_script_lang);
  1611. if (!scripting_ops) {
  1612. fprintf(stderr, "invalid language specifier");
  1613. err = -ENOENT;
  1614. goto out_delete;
  1615. }
  1616. err = scripting_ops->generate_script(session->tevent.pevent,
  1617. "perf-script");
  1618. goto out_delete;
  1619. }
  1620. if (script_name) {
  1621. err = scripting_ops->start_script(script_name, argc, argv);
  1622. if (err)
  1623. goto out_delete;
  1624. pr_debug("perf script started with script %s\n\n", script_name);
  1625. script_started = true;
  1626. }
  1627. err = perf_session__check_output_opt(session);
  1628. if (err < 0)
  1629. goto out_delete;
  1630. err = __cmd_script(&script);
  1631. flush_scripting();
  1632. out_delete:
  1633. perf_session__delete(session);
  1634. if (script_started)
  1635. cleanup_scripting();
  1636. out:
  1637. return err;
  1638. }