builtin-script.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345
  1. #include "builtin.h"
  2. #include "perf.h"
  3. #include "util/cache.h"
  4. #include "util/debug.h"
  5. #include <subcmd/exec-cmd.h>
  6. #include "util/header.h"
  7. #include <subcmd/parse-options.h>
  8. #include "util/perf_regs.h"
  9. #include "util/session.h"
  10. #include "util/tool.h"
  11. #include "util/symbol.h"
  12. #include "util/thread.h"
  13. #include "util/trace-event.h"
  14. #include "util/util.h"
  15. #include "util/evlist.h"
  16. #include "util/evsel.h"
  17. #include "util/sort.h"
  18. #include "util/data.h"
  19. #include "util/auxtrace.h"
  20. #include "util/cpumap.h"
  21. #include "util/thread_map.h"
  22. #include "util/stat.h"
  23. #include <linux/bitmap.h>
  24. #include "asm/bug.h"
  25. #include "util/mem-events.h"
  26. static char const *script_name;
  27. static char const *generate_script_lang;
  28. static bool debug_mode;
  29. static u64 last_timestamp;
  30. static u64 nr_unordered;
  31. static bool no_callchain;
  32. static bool latency_format;
  33. static bool system_wide;
  34. static bool print_flags;
  35. static bool nanosecs;
  36. static const char *cpu_list;
  37. static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
  38. static struct perf_stat_config stat_config;
  39. unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
  40. enum perf_output_field {
  41. PERF_OUTPUT_COMM = 1U << 0,
  42. PERF_OUTPUT_TID = 1U << 1,
  43. PERF_OUTPUT_PID = 1U << 2,
  44. PERF_OUTPUT_TIME = 1U << 3,
  45. PERF_OUTPUT_CPU = 1U << 4,
  46. PERF_OUTPUT_EVNAME = 1U << 5,
  47. PERF_OUTPUT_TRACE = 1U << 6,
  48. PERF_OUTPUT_IP = 1U << 7,
  49. PERF_OUTPUT_SYM = 1U << 8,
  50. PERF_OUTPUT_DSO = 1U << 9,
  51. PERF_OUTPUT_ADDR = 1U << 10,
  52. PERF_OUTPUT_SYMOFFSET = 1U << 11,
  53. PERF_OUTPUT_SRCLINE = 1U << 12,
  54. PERF_OUTPUT_PERIOD = 1U << 13,
  55. PERF_OUTPUT_IREGS = 1U << 14,
  56. PERF_OUTPUT_BRSTACK = 1U << 15,
  57. PERF_OUTPUT_BRSTACKSYM = 1U << 16,
  58. PERF_OUTPUT_DATA_SRC = 1U << 17,
  59. PERF_OUTPUT_WEIGHT = 1U << 18,
  60. PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
  61. };
  62. struct output_option {
  63. const char *str;
  64. enum perf_output_field field;
  65. } all_output_options[] = {
  66. {.str = "comm", .field = PERF_OUTPUT_COMM},
  67. {.str = "tid", .field = PERF_OUTPUT_TID},
  68. {.str = "pid", .field = PERF_OUTPUT_PID},
  69. {.str = "time", .field = PERF_OUTPUT_TIME},
  70. {.str = "cpu", .field = PERF_OUTPUT_CPU},
  71. {.str = "event", .field = PERF_OUTPUT_EVNAME},
  72. {.str = "trace", .field = PERF_OUTPUT_TRACE},
  73. {.str = "ip", .field = PERF_OUTPUT_IP},
  74. {.str = "sym", .field = PERF_OUTPUT_SYM},
  75. {.str = "dso", .field = PERF_OUTPUT_DSO},
  76. {.str = "addr", .field = PERF_OUTPUT_ADDR},
  77. {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
  78. {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
  79. {.str = "period", .field = PERF_OUTPUT_PERIOD},
  80. {.str = "iregs", .field = PERF_OUTPUT_IREGS},
  81. {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
  82. {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
  83. {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
  84. {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
  85. {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
  86. };
  87. /* default set to maintain compatibility with current format */
  88. static struct {
  89. bool user_set;
  90. bool wildcard_set;
  91. unsigned int print_ip_opts;
  92. u64 fields;
  93. u64 invalid_fields;
  94. } output[PERF_TYPE_MAX] = {
  95. [PERF_TYPE_HARDWARE] = {
  96. .user_set = false,
  97. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  98. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  99. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  100. PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
  101. PERF_OUTPUT_PERIOD,
  102. .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
  103. },
  104. [PERF_TYPE_SOFTWARE] = {
  105. .user_set = false,
  106. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  107. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  108. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  109. PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
  110. PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
  111. .invalid_fields = PERF_OUTPUT_TRACE,
  112. },
  113. [PERF_TYPE_TRACEPOINT] = {
  114. .user_set = false,
  115. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  116. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  117. PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
  118. },
  119. [PERF_TYPE_RAW] = {
  120. .user_set = false,
  121. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  122. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  123. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  124. PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
  125. PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
  126. PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT,
  127. .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
  128. },
  129. [PERF_TYPE_BREAKPOINT] = {
  130. .user_set = false,
  131. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  132. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  133. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  134. PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
  135. PERF_OUTPUT_PERIOD,
  136. .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
  137. },
  138. };
  139. static bool output_set_by_user(void)
  140. {
  141. int j;
  142. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  143. if (output[j].user_set)
  144. return true;
  145. }
  146. return false;
  147. }
  148. static const char *output_field2str(enum perf_output_field field)
  149. {
  150. int i, imax = ARRAY_SIZE(all_output_options);
  151. const char *str = "";
  152. for (i = 0; i < imax; ++i) {
  153. if (all_output_options[i].field == field) {
  154. str = all_output_options[i].str;
  155. break;
  156. }
  157. }
  158. return str;
  159. }
  160. #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
  161. static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
  162. u64 sample_type, const char *sample_msg,
  163. enum perf_output_field field,
  164. bool allow_user_set)
  165. {
  166. struct perf_event_attr *attr = &evsel->attr;
  167. int type = attr->type;
  168. const char *evname;
  169. if (attr->sample_type & sample_type)
  170. return 0;
  171. if (output[type].user_set) {
  172. if (allow_user_set)
  173. return 0;
  174. evname = perf_evsel__name(evsel);
  175. pr_err("Samples for '%s' event do not have %s attribute set. "
  176. "Cannot print '%s' field.\n",
  177. evname, sample_msg, output_field2str(field));
  178. return -1;
  179. }
  180. /* user did not ask for it explicitly so remove from the default list */
  181. output[type].fields &= ~field;
  182. evname = perf_evsel__name(evsel);
  183. pr_debug("Samples for '%s' event do not have %s attribute set. "
  184. "Skipping '%s' field.\n",
  185. evname, sample_msg, output_field2str(field));
  186. return 0;
  187. }
  188. static int perf_evsel__check_stype(struct perf_evsel *evsel,
  189. u64 sample_type, const char *sample_msg,
  190. enum perf_output_field field)
  191. {
  192. return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
  193. false);
  194. }
  195. static int perf_evsel__check_attr(struct perf_evsel *evsel,
  196. struct perf_session *session)
  197. {
  198. struct perf_event_attr *attr = &evsel->attr;
  199. bool allow_user_set;
  200. if (perf_header__has_feat(&session->header, HEADER_STAT))
  201. return 0;
  202. allow_user_set = perf_header__has_feat(&session->header,
  203. HEADER_AUXTRACE);
  204. if (PRINT_FIELD(TRACE) &&
  205. !perf_session__has_traces(session, "record -R"))
  206. return -EINVAL;
  207. if (PRINT_FIELD(IP)) {
  208. if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
  209. PERF_OUTPUT_IP))
  210. return -EINVAL;
  211. }
  212. if (PRINT_FIELD(ADDR) &&
  213. perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
  214. PERF_OUTPUT_ADDR, allow_user_set))
  215. return -EINVAL;
  216. if (PRINT_FIELD(DATA_SRC) &&
  217. perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
  218. PERF_OUTPUT_DATA_SRC))
  219. return -EINVAL;
  220. if (PRINT_FIELD(WEIGHT) &&
  221. perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
  222. PERF_OUTPUT_WEIGHT))
  223. return -EINVAL;
  224. if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
  225. pr_err("Display of symbols requested but neither sample IP nor "
  226. "sample address\nis selected. Hence, no addresses to convert "
  227. "to symbols.\n");
  228. return -EINVAL;
  229. }
  230. if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
  231. pr_err("Display of offsets requested but symbol is not"
  232. "selected.\n");
  233. return -EINVAL;
  234. }
  235. if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
  236. pr_err("Display of DSO requested but neither sample IP nor "
  237. "sample address\nis selected. Hence, no addresses to convert "
  238. "to DSO.\n");
  239. return -EINVAL;
  240. }
  241. if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
  242. pr_err("Display of source line number requested but sample IP is not\n"
  243. "selected. Hence, no address to lookup the source line number.\n");
  244. return -EINVAL;
  245. }
  246. if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
  247. perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
  248. PERF_OUTPUT_TID|PERF_OUTPUT_PID))
  249. return -EINVAL;
  250. if (PRINT_FIELD(TIME) &&
  251. perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
  252. PERF_OUTPUT_TIME))
  253. return -EINVAL;
  254. if (PRINT_FIELD(CPU) &&
  255. perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
  256. PERF_OUTPUT_CPU, allow_user_set))
  257. return -EINVAL;
  258. if (PRINT_FIELD(PERIOD) &&
  259. perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
  260. PERF_OUTPUT_PERIOD))
  261. return -EINVAL;
  262. if (PRINT_FIELD(IREGS) &&
  263. perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
  264. PERF_OUTPUT_IREGS))
  265. return -EINVAL;
  266. return 0;
  267. }
  268. static void set_print_ip_opts(struct perf_event_attr *attr)
  269. {
  270. unsigned int type = attr->type;
  271. output[type].print_ip_opts = 0;
  272. if (PRINT_FIELD(IP))
  273. output[type].print_ip_opts |= PRINT_IP_OPT_IP;
  274. if (PRINT_FIELD(SYM))
  275. output[type].print_ip_opts |= PRINT_IP_OPT_SYM;
  276. if (PRINT_FIELD(DSO))
  277. output[type].print_ip_opts |= PRINT_IP_OPT_DSO;
  278. if (PRINT_FIELD(SYMOFFSET))
  279. output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET;
  280. if (PRINT_FIELD(SRCLINE))
  281. output[type].print_ip_opts |= PRINT_IP_OPT_SRCLINE;
  282. }
  283. /*
  284. * verify all user requested events exist and the samples
  285. * have the expected data
  286. */
  287. static int perf_session__check_output_opt(struct perf_session *session)
  288. {
  289. int j;
  290. struct perf_evsel *evsel;
  291. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  292. evsel = perf_session__find_first_evtype(session, j);
  293. /*
  294. * even if fields is set to 0 (ie., show nothing) event must
  295. * exist if user explicitly includes it on the command line
  296. */
  297. if (!evsel && output[j].user_set && !output[j].wildcard_set) {
  298. pr_err("%s events do not exist. "
  299. "Remove corresponding -f option to proceed.\n",
  300. event_type(j));
  301. return -1;
  302. }
  303. if (evsel && output[j].fields &&
  304. perf_evsel__check_attr(evsel, session))
  305. return -1;
  306. if (evsel == NULL)
  307. continue;
  308. set_print_ip_opts(&evsel->attr);
  309. }
  310. if (!no_callchain) {
  311. bool use_callchain = false;
  312. evlist__for_each(session->evlist, evsel) {
  313. if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
  314. use_callchain = true;
  315. break;
  316. }
  317. }
  318. if (!use_callchain)
  319. symbol_conf.use_callchain = false;
  320. }
  321. /*
  322. * set default for tracepoints to print symbols only
  323. * if callchains are present
  324. */
  325. if (symbol_conf.use_callchain &&
  326. !output[PERF_TYPE_TRACEPOINT].user_set) {
  327. struct perf_event_attr *attr;
  328. j = PERF_TYPE_TRACEPOINT;
  329. evsel = perf_session__find_first_evtype(session, j);
  330. if (evsel == NULL)
  331. goto out;
  332. attr = &evsel->attr;
  333. if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
  334. output[j].fields |= PERF_OUTPUT_IP;
  335. output[j].fields |= PERF_OUTPUT_SYM;
  336. output[j].fields |= PERF_OUTPUT_DSO;
  337. set_print_ip_opts(attr);
  338. }
  339. }
  340. out:
  341. return 0;
  342. }
  343. static void print_sample_iregs(union perf_event *event __maybe_unused,
  344. struct perf_sample *sample,
  345. struct thread *thread __maybe_unused,
  346. struct perf_event_attr *attr)
  347. {
  348. struct regs_dump *regs = &sample->intr_regs;
  349. uint64_t mask = attr->sample_regs_intr;
  350. unsigned i = 0, r;
  351. if (!regs)
  352. return;
  353. for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
  354. u64 val = regs->regs[i++];
  355. printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
  356. }
  357. }
  358. static void print_sample_start(struct perf_sample *sample,
  359. struct thread *thread,
  360. struct perf_evsel *evsel)
  361. {
  362. struct perf_event_attr *attr = &evsel->attr;
  363. unsigned long secs;
  364. unsigned long usecs;
  365. unsigned long long nsecs;
  366. if (PRINT_FIELD(COMM)) {
  367. if (latency_format)
  368. printf("%8.8s ", thread__comm_str(thread));
  369. else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
  370. printf("%s ", thread__comm_str(thread));
  371. else
  372. printf("%16s ", thread__comm_str(thread));
  373. }
  374. if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
  375. printf("%5d/%-5d ", sample->pid, sample->tid);
  376. else if (PRINT_FIELD(PID))
  377. printf("%5d ", sample->pid);
  378. else if (PRINT_FIELD(TID))
  379. printf("%5d ", sample->tid);
  380. if (PRINT_FIELD(CPU)) {
  381. if (latency_format)
  382. printf("%3d ", sample->cpu);
  383. else
  384. printf("[%03d] ", sample->cpu);
  385. }
  386. if (PRINT_FIELD(TIME)) {
  387. nsecs = sample->time;
  388. secs = nsecs / NSECS_PER_SEC;
  389. nsecs -= secs * NSECS_PER_SEC;
  390. usecs = nsecs / NSECS_PER_USEC;
  391. if (nanosecs)
  392. printf("%5lu.%09llu: ", secs, nsecs);
  393. else
  394. printf("%5lu.%06lu: ", secs, usecs);
  395. }
  396. }
  397. static inline char
  398. mispred_str(struct branch_entry *br)
  399. {
  400. if (!(br->flags.mispred || br->flags.predicted))
  401. return '-';
  402. return br->flags.predicted ? 'P' : 'M';
  403. }
  404. static void print_sample_brstack(union perf_event *event __maybe_unused,
  405. struct perf_sample *sample,
  406. struct thread *thread __maybe_unused,
  407. struct perf_event_attr *attr __maybe_unused)
  408. {
  409. struct branch_stack *br = sample->branch_stack;
  410. u64 i;
  411. if (!(br && br->nr))
  412. return;
  413. for (i = 0; i < br->nr; i++) {
  414. printf(" 0x%"PRIx64"/0x%"PRIx64"/%c/%c/%c/%d ",
  415. br->entries[i].from,
  416. br->entries[i].to,
  417. mispred_str( br->entries + i),
  418. br->entries[i].flags.in_tx? 'X' : '-',
  419. br->entries[i].flags.abort? 'A' : '-',
  420. br->entries[i].flags.cycles);
  421. }
  422. }
  423. static void print_sample_brstacksym(union perf_event *event,
  424. struct perf_sample *sample,
  425. struct thread *thread __maybe_unused,
  426. struct perf_event_attr *attr __maybe_unused)
  427. {
  428. struct branch_stack *br = sample->branch_stack;
  429. struct addr_location alf, alt;
  430. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  431. u64 i, from, to;
  432. if (!(br && br->nr))
  433. return;
  434. for (i = 0; i < br->nr; i++) {
  435. memset(&alf, 0, sizeof(alf));
  436. memset(&alt, 0, sizeof(alt));
  437. from = br->entries[i].from;
  438. to = br->entries[i].to;
  439. thread__find_addr_map(thread, cpumode, MAP__FUNCTION, from, &alf);
  440. if (alf.map)
  441. alf.sym = map__find_symbol(alf.map, alf.addr, NULL);
  442. thread__find_addr_map(thread, cpumode, MAP__FUNCTION, to, &alt);
  443. if (alt.map)
  444. alt.sym = map__find_symbol(alt.map, alt.addr, NULL);
  445. symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
  446. putchar('/');
  447. symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
  448. printf("/%c/%c/%c/%d ",
  449. mispred_str( br->entries + i),
  450. br->entries[i].flags.in_tx? 'X' : '-',
  451. br->entries[i].flags.abort? 'A' : '-',
  452. br->entries[i].flags.cycles);
  453. }
  454. }
  455. static void print_sample_addr(union perf_event *event,
  456. struct perf_sample *sample,
  457. struct thread *thread,
  458. struct perf_event_attr *attr)
  459. {
  460. struct addr_location al;
  461. printf("%16" PRIx64, sample->addr);
  462. if (!sample_addr_correlates_sym(attr))
  463. return;
  464. perf_event__preprocess_sample_addr(event, sample, thread, &al);
  465. if (PRINT_FIELD(SYM)) {
  466. printf(" ");
  467. if (PRINT_FIELD(SYMOFFSET))
  468. symbol__fprintf_symname_offs(al.sym, &al, stdout);
  469. else
  470. symbol__fprintf_symname(al.sym, stdout);
  471. }
  472. if (PRINT_FIELD(DSO)) {
  473. printf(" (");
  474. map__fprintf_dsoname(al.map, stdout);
  475. printf(")");
  476. }
  477. }
  478. static void print_sample_bts(union perf_event *event,
  479. struct perf_sample *sample,
  480. struct perf_evsel *evsel,
  481. struct thread *thread,
  482. struct addr_location *al)
  483. {
  484. struct perf_event_attr *attr = &evsel->attr;
  485. bool print_srcline_last = false;
  486. /* print branch_from information */
  487. if (PRINT_FIELD(IP)) {
  488. unsigned int print_opts = output[attr->type].print_ip_opts;
  489. if (symbol_conf.use_callchain && sample->callchain) {
  490. printf("\n");
  491. } else {
  492. printf(" ");
  493. if (print_opts & PRINT_IP_OPT_SRCLINE) {
  494. print_srcline_last = true;
  495. print_opts &= ~PRINT_IP_OPT_SRCLINE;
  496. }
  497. }
  498. perf_evsel__print_ip(evsel, sample, al, print_opts,
  499. scripting_max_stack);
  500. }
  501. /* print branch_to information */
  502. if (PRINT_FIELD(ADDR) ||
  503. ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
  504. !output[attr->type].user_set)) {
  505. printf(" => ");
  506. print_sample_addr(event, sample, thread, attr);
  507. }
  508. if (print_srcline_last)
  509. map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
  510. printf("\n");
  511. }
  512. static void print_sample_flags(u32 flags)
  513. {
  514. const char *chars = PERF_IP_FLAG_CHARS;
  515. const int n = strlen(PERF_IP_FLAG_CHARS);
  516. char str[33];
  517. int i, pos = 0;
  518. for (i = 0; i < n; i++, flags >>= 1) {
  519. if (flags & 1)
  520. str[pos++] = chars[i];
  521. }
  522. for (; i < 32; i++, flags >>= 1) {
  523. if (flags & 1)
  524. str[pos++] = '?';
  525. }
  526. str[pos] = 0;
  527. printf(" %-4s ", str);
  528. }
  529. struct printer_data {
  530. int line_no;
  531. bool hit_nul;
  532. bool is_printable;
  533. };
  534. static void
  535. print_sample_bpf_output_printer(enum binary_printer_ops op,
  536. unsigned int val,
  537. void *extra)
  538. {
  539. unsigned char ch = (unsigned char)val;
  540. struct printer_data *printer_data = extra;
  541. switch (op) {
  542. case BINARY_PRINT_DATA_BEGIN:
  543. printf("\n");
  544. break;
  545. case BINARY_PRINT_LINE_BEGIN:
  546. printf("%17s", !printer_data->line_no ? "BPF output:" :
  547. " ");
  548. break;
  549. case BINARY_PRINT_ADDR:
  550. printf(" %04x:", val);
  551. break;
  552. case BINARY_PRINT_NUM_DATA:
  553. printf(" %02x", val);
  554. break;
  555. case BINARY_PRINT_NUM_PAD:
  556. printf(" ");
  557. break;
  558. case BINARY_PRINT_SEP:
  559. printf(" ");
  560. break;
  561. case BINARY_PRINT_CHAR_DATA:
  562. if (printer_data->hit_nul && ch)
  563. printer_data->is_printable = false;
  564. if (!isprint(ch)) {
  565. printf("%c", '.');
  566. if (!printer_data->is_printable)
  567. break;
  568. if (ch == '\0')
  569. printer_data->hit_nul = true;
  570. else
  571. printer_data->is_printable = false;
  572. } else {
  573. printf("%c", ch);
  574. }
  575. break;
  576. case BINARY_PRINT_CHAR_PAD:
  577. printf(" ");
  578. break;
  579. case BINARY_PRINT_LINE_END:
  580. printf("\n");
  581. printer_data->line_no++;
  582. break;
  583. case BINARY_PRINT_DATA_END:
  584. default:
  585. break;
  586. }
  587. }
  588. static void print_sample_bpf_output(struct perf_sample *sample)
  589. {
  590. unsigned int nr_bytes = sample->raw_size;
  591. struct printer_data printer_data = {0, false, true};
  592. print_binary(sample->raw_data, nr_bytes, 8,
  593. print_sample_bpf_output_printer, &printer_data);
  594. if (printer_data.is_printable && printer_data.hit_nul)
  595. printf("%17s \"%s\"\n", "BPF string:",
  596. (char *)(sample->raw_data));
  597. }
  598. struct perf_script {
  599. struct perf_tool tool;
  600. struct perf_session *session;
  601. bool show_task_events;
  602. bool show_mmap_events;
  603. bool show_switch_events;
  604. bool allocated;
  605. struct cpu_map *cpus;
  606. struct thread_map *threads;
  607. int name_width;
  608. };
  609. static int perf_evlist__max_name_len(struct perf_evlist *evlist)
  610. {
  611. struct perf_evsel *evsel;
  612. int max = 0;
  613. evlist__for_each(evlist, evsel) {
  614. int len = strlen(perf_evsel__name(evsel));
  615. max = MAX(len, max);
  616. }
  617. return max;
  618. }
  619. static size_t data_src__printf(u64 data_src)
  620. {
  621. struct mem_info mi = { .data_src.val = data_src };
  622. char decode[100];
  623. char out[100];
  624. static int maxlen;
  625. int len;
  626. perf_script__meminfo_scnprintf(decode, 100, &mi);
  627. len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
  628. if (maxlen < len)
  629. maxlen = len;
  630. return printf("%-*s", maxlen, out);
  631. }
  632. static void process_event(struct perf_script *script, union perf_event *event,
  633. struct perf_sample *sample, struct perf_evsel *evsel,
  634. struct addr_location *al)
  635. {
  636. struct thread *thread = al->thread;
  637. struct perf_event_attr *attr = &evsel->attr;
  638. if (output[attr->type].fields == 0)
  639. return;
  640. print_sample_start(sample, thread, evsel);
  641. if (PRINT_FIELD(PERIOD))
  642. printf("%10" PRIu64 " ", sample->period);
  643. if (PRINT_FIELD(EVNAME)) {
  644. const char *evname = perf_evsel__name(evsel);
  645. if (!script->name_width)
  646. script->name_width = perf_evlist__max_name_len(script->session->evlist);
  647. printf("%*s: ", script->name_width,
  648. evname ? evname : "[unknown]");
  649. }
  650. if (print_flags)
  651. print_sample_flags(sample->flags);
  652. if (is_bts_event(attr)) {
  653. print_sample_bts(event, sample, evsel, thread, al);
  654. return;
  655. }
  656. if (PRINT_FIELD(TRACE))
  657. event_format__print(evsel->tp_format, sample->cpu,
  658. sample->raw_data, sample->raw_size);
  659. if (PRINT_FIELD(ADDR))
  660. print_sample_addr(event, sample, thread, attr);
  661. if (PRINT_FIELD(DATA_SRC))
  662. data_src__printf(sample->data_src);
  663. if (PRINT_FIELD(WEIGHT))
  664. printf("%16" PRIu64, sample->weight);
  665. if (PRINT_FIELD(IP)) {
  666. if (!symbol_conf.use_callchain)
  667. printf(" ");
  668. else
  669. printf("\n");
  670. perf_evsel__print_ip(evsel, sample, al,
  671. output[attr->type].print_ip_opts,
  672. scripting_max_stack);
  673. }
  674. if (PRINT_FIELD(IREGS))
  675. print_sample_iregs(event, sample, thread, attr);
  676. if (PRINT_FIELD(BRSTACK))
  677. print_sample_brstack(event, sample, thread, attr);
  678. else if (PRINT_FIELD(BRSTACKSYM))
  679. print_sample_brstacksym(event, sample, thread, attr);
  680. if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
  681. print_sample_bpf_output(sample);
  682. printf("\n");
  683. }
  684. static struct scripting_ops *scripting_ops;
  685. static void __process_stat(struct perf_evsel *counter, u64 tstamp)
  686. {
  687. int nthreads = thread_map__nr(counter->threads);
  688. int ncpus = perf_evsel__nr_cpus(counter);
  689. int cpu, thread;
  690. static int header_printed;
  691. if (counter->system_wide)
  692. nthreads = 1;
  693. if (!header_printed) {
  694. printf("%3s %8s %15s %15s %15s %15s %s\n",
  695. "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
  696. header_printed = 1;
  697. }
  698. for (thread = 0; thread < nthreads; thread++) {
  699. for (cpu = 0; cpu < ncpus; cpu++) {
  700. struct perf_counts_values *counts;
  701. counts = perf_counts(counter->counts, cpu, thread);
  702. printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
  703. counter->cpus->map[cpu],
  704. thread_map__pid(counter->threads, thread),
  705. counts->val,
  706. counts->ena,
  707. counts->run,
  708. tstamp,
  709. perf_evsel__name(counter));
  710. }
  711. }
  712. }
  713. static void process_stat(struct perf_evsel *counter, u64 tstamp)
  714. {
  715. if (scripting_ops && scripting_ops->process_stat)
  716. scripting_ops->process_stat(&stat_config, counter, tstamp);
  717. else
  718. __process_stat(counter, tstamp);
  719. }
  720. static void process_stat_interval(u64 tstamp)
  721. {
  722. if (scripting_ops && scripting_ops->process_stat_interval)
  723. scripting_ops->process_stat_interval(tstamp);
  724. }
  725. static void setup_scripting(void)
  726. {
  727. setup_perl_scripting();
  728. setup_python_scripting();
  729. }
  730. static int flush_scripting(void)
  731. {
  732. return scripting_ops ? scripting_ops->flush_script() : 0;
  733. }
  734. static int cleanup_scripting(void)
  735. {
  736. pr_debug("\nperf script stopped\n");
  737. return scripting_ops ? scripting_ops->stop_script() : 0;
  738. }
  739. static int process_sample_event(struct perf_tool *tool,
  740. union perf_event *event,
  741. struct perf_sample *sample,
  742. struct perf_evsel *evsel,
  743. struct machine *machine)
  744. {
  745. struct perf_script *scr = container_of(tool, struct perf_script, tool);
  746. struct addr_location al;
  747. if (debug_mode) {
  748. if (sample->time < last_timestamp) {
  749. pr_err("Samples misordered, previous: %" PRIu64
  750. " this: %" PRIu64 "\n", last_timestamp,
  751. sample->time);
  752. nr_unordered++;
  753. }
  754. last_timestamp = sample->time;
  755. return 0;
  756. }
  757. if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
  758. pr_err("problem processing %d event, skipping it.\n",
  759. event->header.type);
  760. return -1;
  761. }
  762. if (al.filtered)
  763. goto out_put;
  764. if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
  765. goto out_put;
  766. if (scripting_ops)
  767. scripting_ops->process_event(event, sample, evsel, &al);
  768. else
  769. process_event(scr, event, sample, evsel, &al);
  770. out_put:
  771. addr_location__put(&al);
  772. return 0;
  773. }
  774. static int process_attr(struct perf_tool *tool, union perf_event *event,
  775. struct perf_evlist **pevlist)
  776. {
  777. struct perf_script *scr = container_of(tool, struct perf_script, tool);
  778. struct perf_evlist *evlist;
  779. struct perf_evsel *evsel, *pos;
  780. int err;
  781. err = perf_event__process_attr(tool, event, pevlist);
  782. if (err)
  783. return err;
  784. evlist = *pevlist;
  785. evsel = perf_evlist__last(*pevlist);
  786. if (evsel->attr.type >= PERF_TYPE_MAX)
  787. return 0;
  788. evlist__for_each(evlist, pos) {
  789. if (pos->attr.type == evsel->attr.type && pos != evsel)
  790. return 0;
  791. }
  792. set_print_ip_opts(&evsel->attr);
  793. if (evsel->attr.sample_type)
  794. err = perf_evsel__check_attr(evsel, scr->session);
  795. return err;
  796. }
  797. static int process_comm_event(struct perf_tool *tool,
  798. union perf_event *event,
  799. struct perf_sample *sample,
  800. struct machine *machine)
  801. {
  802. struct thread *thread;
  803. struct perf_script *script = container_of(tool, struct perf_script, tool);
  804. struct perf_session *session = script->session;
  805. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  806. int ret = -1;
  807. thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
  808. if (thread == NULL) {
  809. pr_debug("problem processing COMM event, skipping it.\n");
  810. return -1;
  811. }
  812. if (perf_event__process_comm(tool, event, sample, machine) < 0)
  813. goto out;
  814. if (!evsel->attr.sample_id_all) {
  815. sample->cpu = 0;
  816. sample->time = 0;
  817. sample->tid = event->comm.tid;
  818. sample->pid = event->comm.pid;
  819. }
  820. print_sample_start(sample, thread, evsel);
  821. perf_event__fprintf(event, stdout);
  822. ret = 0;
  823. out:
  824. thread__put(thread);
  825. return ret;
  826. }
  827. static int process_fork_event(struct perf_tool *tool,
  828. union perf_event *event,
  829. struct perf_sample *sample,
  830. struct machine *machine)
  831. {
  832. struct thread *thread;
  833. struct perf_script *script = container_of(tool, struct perf_script, tool);
  834. struct perf_session *session = script->session;
  835. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  836. if (perf_event__process_fork(tool, event, sample, machine) < 0)
  837. return -1;
  838. thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
  839. if (thread == NULL) {
  840. pr_debug("problem processing FORK event, skipping it.\n");
  841. return -1;
  842. }
  843. if (!evsel->attr.sample_id_all) {
  844. sample->cpu = 0;
  845. sample->time = event->fork.time;
  846. sample->tid = event->fork.tid;
  847. sample->pid = event->fork.pid;
  848. }
  849. print_sample_start(sample, thread, evsel);
  850. perf_event__fprintf(event, stdout);
  851. thread__put(thread);
  852. return 0;
  853. }
  854. static int process_exit_event(struct perf_tool *tool,
  855. union perf_event *event,
  856. struct perf_sample *sample,
  857. struct machine *machine)
  858. {
  859. int err = 0;
  860. struct thread *thread;
  861. struct perf_script *script = container_of(tool, struct perf_script, tool);
  862. struct perf_session *session = script->session;
  863. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  864. thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
  865. if (thread == NULL) {
  866. pr_debug("problem processing EXIT event, skipping it.\n");
  867. return -1;
  868. }
  869. if (!evsel->attr.sample_id_all) {
  870. sample->cpu = 0;
  871. sample->time = 0;
  872. sample->tid = event->fork.tid;
  873. sample->pid = event->fork.pid;
  874. }
  875. print_sample_start(sample, thread, evsel);
  876. perf_event__fprintf(event, stdout);
  877. if (perf_event__process_exit(tool, event, sample, machine) < 0)
  878. err = -1;
  879. thread__put(thread);
  880. return err;
  881. }
  882. static int process_mmap_event(struct perf_tool *tool,
  883. union perf_event *event,
  884. struct perf_sample *sample,
  885. struct machine *machine)
  886. {
  887. struct thread *thread;
  888. struct perf_script *script = container_of(tool, struct perf_script, tool);
  889. struct perf_session *session = script->session;
  890. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  891. if (perf_event__process_mmap(tool, event, sample, machine) < 0)
  892. return -1;
  893. thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
  894. if (thread == NULL) {
  895. pr_debug("problem processing MMAP event, skipping it.\n");
  896. return -1;
  897. }
  898. if (!evsel->attr.sample_id_all) {
  899. sample->cpu = 0;
  900. sample->time = 0;
  901. sample->tid = event->mmap.tid;
  902. sample->pid = event->mmap.pid;
  903. }
  904. print_sample_start(sample, thread, evsel);
  905. perf_event__fprintf(event, stdout);
  906. thread__put(thread);
  907. return 0;
  908. }
  909. static int process_mmap2_event(struct perf_tool *tool,
  910. union perf_event *event,
  911. struct perf_sample *sample,
  912. struct machine *machine)
  913. {
  914. struct thread *thread;
  915. struct perf_script *script = container_of(tool, struct perf_script, tool);
  916. struct perf_session *session = script->session;
  917. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  918. if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
  919. return -1;
  920. thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
  921. if (thread == NULL) {
  922. pr_debug("problem processing MMAP2 event, skipping it.\n");
  923. return -1;
  924. }
  925. if (!evsel->attr.sample_id_all) {
  926. sample->cpu = 0;
  927. sample->time = 0;
  928. sample->tid = event->mmap2.tid;
  929. sample->pid = event->mmap2.pid;
  930. }
  931. print_sample_start(sample, thread, evsel);
  932. perf_event__fprintf(event, stdout);
  933. thread__put(thread);
  934. return 0;
  935. }
  936. static int process_switch_event(struct perf_tool *tool,
  937. union perf_event *event,
  938. struct perf_sample *sample,
  939. struct machine *machine)
  940. {
  941. struct thread *thread;
  942. struct perf_script *script = container_of(tool, struct perf_script, tool);
  943. struct perf_session *session = script->session;
  944. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  945. if (perf_event__process_switch(tool, event, sample, machine) < 0)
  946. return -1;
  947. thread = machine__findnew_thread(machine, sample->pid,
  948. sample->tid);
  949. if (thread == NULL) {
  950. pr_debug("problem processing SWITCH event, skipping it.\n");
  951. return -1;
  952. }
  953. print_sample_start(sample, thread, evsel);
  954. perf_event__fprintf(event, stdout);
  955. thread__put(thread);
  956. return 0;
  957. }
  958. static void sig_handler(int sig __maybe_unused)
  959. {
  960. session_done = 1;
  961. }
  962. static int __cmd_script(struct perf_script *script)
  963. {
  964. int ret;
  965. signal(SIGINT, sig_handler);
  966. /* override event processing functions */
  967. if (script->show_task_events) {
  968. script->tool.comm = process_comm_event;
  969. script->tool.fork = process_fork_event;
  970. script->tool.exit = process_exit_event;
  971. }
  972. if (script->show_mmap_events) {
  973. script->tool.mmap = process_mmap_event;
  974. script->tool.mmap2 = process_mmap2_event;
  975. }
  976. if (script->show_switch_events)
  977. script->tool.context_switch = process_switch_event;
  978. ret = perf_session__process_events(script->session);
  979. if (debug_mode)
  980. pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
  981. return ret;
  982. }
  983. struct script_spec {
  984. struct list_head node;
  985. struct scripting_ops *ops;
  986. char spec[0];
  987. };
  988. static LIST_HEAD(script_specs);
  989. static struct script_spec *script_spec__new(const char *spec,
  990. struct scripting_ops *ops)
  991. {
  992. struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
  993. if (s != NULL) {
  994. strcpy(s->spec, spec);
  995. s->ops = ops;
  996. }
  997. return s;
  998. }
  999. static void script_spec__add(struct script_spec *s)
  1000. {
  1001. list_add_tail(&s->node, &script_specs);
  1002. }
  1003. static struct script_spec *script_spec__find(const char *spec)
  1004. {
  1005. struct script_spec *s;
  1006. list_for_each_entry(s, &script_specs, node)
  1007. if (strcasecmp(s->spec, spec) == 0)
  1008. return s;
  1009. return NULL;
  1010. }
  1011. int script_spec_register(const char *spec, struct scripting_ops *ops)
  1012. {
  1013. struct script_spec *s;
  1014. s = script_spec__find(spec);
  1015. if (s)
  1016. return -1;
  1017. s = script_spec__new(spec, ops);
  1018. if (!s)
  1019. return -1;
  1020. else
  1021. script_spec__add(s);
  1022. return 0;
  1023. }
  1024. static struct scripting_ops *script_spec__lookup(const char *spec)
  1025. {
  1026. struct script_spec *s = script_spec__find(spec);
  1027. if (!s)
  1028. return NULL;
  1029. return s->ops;
  1030. }
  1031. static void list_available_languages(void)
  1032. {
  1033. struct script_spec *s;
  1034. fprintf(stderr, "\n");
  1035. fprintf(stderr, "Scripting language extensions (used in "
  1036. "perf script -s [spec:]script.[spec]):\n\n");
  1037. list_for_each_entry(s, &script_specs, node)
  1038. fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
  1039. fprintf(stderr, "\n");
  1040. }
  1041. static int parse_scriptname(const struct option *opt __maybe_unused,
  1042. const char *str, int unset __maybe_unused)
  1043. {
  1044. char spec[PATH_MAX];
  1045. const char *script, *ext;
  1046. int len;
  1047. if (strcmp(str, "lang") == 0) {
  1048. list_available_languages();
  1049. exit(0);
  1050. }
  1051. script = strchr(str, ':');
  1052. if (script) {
  1053. len = script - str;
  1054. if (len >= PATH_MAX) {
  1055. fprintf(stderr, "invalid language specifier");
  1056. return -1;
  1057. }
  1058. strncpy(spec, str, len);
  1059. spec[len] = '\0';
  1060. scripting_ops = script_spec__lookup(spec);
  1061. if (!scripting_ops) {
  1062. fprintf(stderr, "invalid language specifier");
  1063. return -1;
  1064. }
  1065. script++;
  1066. } else {
  1067. script = str;
  1068. ext = strrchr(script, '.');
  1069. if (!ext) {
  1070. fprintf(stderr, "invalid script extension");
  1071. return -1;
  1072. }
  1073. scripting_ops = script_spec__lookup(++ext);
  1074. if (!scripting_ops) {
  1075. fprintf(stderr, "invalid script extension");
  1076. return -1;
  1077. }
  1078. }
  1079. script_name = strdup(script);
  1080. return 0;
  1081. }
  1082. static int parse_output_fields(const struct option *opt __maybe_unused,
  1083. const char *arg, int unset __maybe_unused)
  1084. {
  1085. char *tok;
  1086. int i, imax = ARRAY_SIZE(all_output_options);
  1087. int j;
  1088. int rc = 0;
  1089. char *str = strdup(arg);
  1090. int type = -1;
  1091. if (!str)
  1092. return -ENOMEM;
  1093. /* first word can state for which event type the user is specifying
  1094. * the fields. If no type exists, the specified fields apply to all
  1095. * event types found in the file minus the invalid fields for a type.
  1096. */
  1097. tok = strchr(str, ':');
  1098. if (tok) {
  1099. *tok = '\0';
  1100. tok++;
  1101. if (!strcmp(str, "hw"))
  1102. type = PERF_TYPE_HARDWARE;
  1103. else if (!strcmp(str, "sw"))
  1104. type = PERF_TYPE_SOFTWARE;
  1105. else if (!strcmp(str, "trace"))
  1106. type = PERF_TYPE_TRACEPOINT;
  1107. else if (!strcmp(str, "raw"))
  1108. type = PERF_TYPE_RAW;
  1109. else if (!strcmp(str, "break"))
  1110. type = PERF_TYPE_BREAKPOINT;
  1111. else {
  1112. fprintf(stderr, "Invalid event type in field string.\n");
  1113. rc = -EINVAL;
  1114. goto out;
  1115. }
  1116. if (output[type].user_set)
  1117. pr_warning("Overriding previous field request for %s events.\n",
  1118. event_type(type));
  1119. output[type].fields = 0;
  1120. output[type].user_set = true;
  1121. output[type].wildcard_set = false;
  1122. } else {
  1123. tok = str;
  1124. if (strlen(str) == 0) {
  1125. fprintf(stderr,
  1126. "Cannot set fields to 'none' for all event types.\n");
  1127. rc = -EINVAL;
  1128. goto out;
  1129. }
  1130. if (output_set_by_user())
  1131. pr_warning("Overriding previous field request for all events.\n");
  1132. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  1133. output[j].fields = 0;
  1134. output[j].user_set = true;
  1135. output[j].wildcard_set = true;
  1136. }
  1137. }
  1138. for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
  1139. for (i = 0; i < imax; ++i) {
  1140. if (strcmp(tok, all_output_options[i].str) == 0)
  1141. break;
  1142. }
  1143. if (i == imax && strcmp(tok, "flags") == 0) {
  1144. print_flags = true;
  1145. continue;
  1146. }
  1147. if (i == imax) {
  1148. fprintf(stderr, "Invalid field requested.\n");
  1149. rc = -EINVAL;
  1150. goto out;
  1151. }
  1152. if (type == -1) {
  1153. /* add user option to all events types for
  1154. * which it is valid
  1155. */
  1156. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  1157. if (output[j].invalid_fields & all_output_options[i].field) {
  1158. pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
  1159. all_output_options[i].str, event_type(j));
  1160. } else
  1161. output[j].fields |= all_output_options[i].field;
  1162. }
  1163. } else {
  1164. if (output[type].invalid_fields & all_output_options[i].field) {
  1165. fprintf(stderr, "\'%s\' not valid for %s events.\n",
  1166. all_output_options[i].str, event_type(type));
  1167. rc = -EINVAL;
  1168. goto out;
  1169. }
  1170. output[type].fields |= all_output_options[i].field;
  1171. }
  1172. }
  1173. if (type >= 0) {
  1174. if (output[type].fields == 0) {
  1175. pr_debug("No fields requested for %s type. "
  1176. "Events will not be displayed.\n", event_type(type));
  1177. }
  1178. }
  1179. out:
  1180. free(str);
  1181. return rc;
  1182. }
  1183. /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
  1184. static int is_directory(const char *base_path, const struct dirent *dent)
  1185. {
  1186. char path[PATH_MAX];
  1187. struct stat st;
  1188. sprintf(path, "%s/%s", base_path, dent->d_name);
  1189. if (stat(path, &st))
  1190. return 0;
  1191. return S_ISDIR(st.st_mode);
  1192. }
  1193. #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
  1194. while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
  1195. lang_next) \
  1196. if ((lang_dirent.d_type == DT_DIR || \
  1197. (lang_dirent.d_type == DT_UNKNOWN && \
  1198. is_directory(scripts_path, &lang_dirent))) && \
  1199. (strcmp(lang_dirent.d_name, ".")) && \
  1200. (strcmp(lang_dirent.d_name, "..")))
  1201. #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
  1202. while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
  1203. script_next) \
  1204. if (script_dirent.d_type != DT_DIR && \
  1205. (script_dirent.d_type != DT_UNKNOWN || \
  1206. !is_directory(lang_path, &script_dirent)))
  1207. #define RECORD_SUFFIX "-record"
  1208. #define REPORT_SUFFIX "-report"
  1209. struct script_desc {
  1210. struct list_head node;
  1211. char *name;
  1212. char *half_liner;
  1213. char *args;
  1214. };
  1215. static LIST_HEAD(script_descs);
  1216. static struct script_desc *script_desc__new(const char *name)
  1217. {
  1218. struct script_desc *s = zalloc(sizeof(*s));
  1219. if (s != NULL && name)
  1220. s->name = strdup(name);
  1221. return s;
  1222. }
  1223. static void script_desc__delete(struct script_desc *s)
  1224. {
  1225. zfree(&s->name);
  1226. zfree(&s->half_liner);
  1227. zfree(&s->args);
  1228. free(s);
  1229. }
  1230. static void script_desc__add(struct script_desc *s)
  1231. {
  1232. list_add_tail(&s->node, &script_descs);
  1233. }
  1234. static struct script_desc *script_desc__find(const char *name)
  1235. {
  1236. struct script_desc *s;
  1237. list_for_each_entry(s, &script_descs, node)
  1238. if (strcasecmp(s->name, name) == 0)
  1239. return s;
  1240. return NULL;
  1241. }
  1242. static struct script_desc *script_desc__findnew(const char *name)
  1243. {
  1244. struct script_desc *s = script_desc__find(name);
  1245. if (s)
  1246. return s;
  1247. s = script_desc__new(name);
  1248. if (!s)
  1249. goto out_delete_desc;
  1250. script_desc__add(s);
  1251. return s;
  1252. out_delete_desc:
  1253. script_desc__delete(s);
  1254. return NULL;
  1255. }
  1256. static const char *ends_with(const char *str, const char *suffix)
  1257. {
  1258. size_t suffix_len = strlen(suffix);
  1259. const char *p = str;
  1260. if (strlen(str) > suffix_len) {
  1261. p = str + strlen(str) - suffix_len;
  1262. if (!strncmp(p, suffix, suffix_len))
  1263. return p;
  1264. }
  1265. return NULL;
  1266. }
  1267. static int read_script_info(struct script_desc *desc, const char *filename)
  1268. {
  1269. char line[BUFSIZ], *p;
  1270. FILE *fp;
  1271. fp = fopen(filename, "r");
  1272. if (!fp)
  1273. return -1;
  1274. while (fgets(line, sizeof(line), fp)) {
  1275. p = ltrim(line);
  1276. if (strlen(p) == 0)
  1277. continue;
  1278. if (*p != '#')
  1279. continue;
  1280. p++;
  1281. if (strlen(p) && *p == '!')
  1282. continue;
  1283. p = ltrim(p);
  1284. if (strlen(p) && p[strlen(p) - 1] == '\n')
  1285. p[strlen(p) - 1] = '\0';
  1286. if (!strncmp(p, "description:", strlen("description:"))) {
  1287. p += strlen("description:");
  1288. desc->half_liner = strdup(ltrim(p));
  1289. continue;
  1290. }
  1291. if (!strncmp(p, "args:", strlen("args:"))) {
  1292. p += strlen("args:");
  1293. desc->args = strdup(ltrim(p));
  1294. continue;
  1295. }
  1296. }
  1297. fclose(fp);
  1298. return 0;
  1299. }
  1300. static char *get_script_root(struct dirent *script_dirent, const char *suffix)
  1301. {
  1302. char *script_root, *str;
  1303. script_root = strdup(script_dirent->d_name);
  1304. if (!script_root)
  1305. return NULL;
  1306. str = (char *)ends_with(script_root, suffix);
  1307. if (!str) {
  1308. free(script_root);
  1309. return NULL;
  1310. }
  1311. *str = '\0';
  1312. return script_root;
  1313. }
  1314. static int list_available_scripts(const struct option *opt __maybe_unused,
  1315. const char *s __maybe_unused,
  1316. int unset __maybe_unused)
  1317. {
  1318. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  1319. char scripts_path[MAXPATHLEN];
  1320. DIR *scripts_dir, *lang_dir;
  1321. char script_path[MAXPATHLEN];
  1322. char lang_path[MAXPATHLEN];
  1323. struct script_desc *desc;
  1324. char first_half[BUFSIZ];
  1325. char *script_root;
  1326. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
  1327. scripts_dir = opendir(scripts_path);
  1328. if (!scripts_dir)
  1329. return -1;
  1330. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  1331. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  1332. lang_dirent.d_name);
  1333. lang_dir = opendir(lang_path);
  1334. if (!lang_dir)
  1335. continue;
  1336. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  1337. script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
  1338. if (script_root) {
  1339. desc = script_desc__findnew(script_root);
  1340. snprintf(script_path, MAXPATHLEN, "%s/%s",
  1341. lang_path, script_dirent.d_name);
  1342. read_script_info(desc, script_path);
  1343. free(script_root);
  1344. }
  1345. }
  1346. }
  1347. fprintf(stdout, "List of available trace scripts:\n");
  1348. list_for_each_entry(desc, &script_descs, node) {
  1349. sprintf(first_half, "%s %s", desc->name,
  1350. desc->args ? desc->args : "");
  1351. fprintf(stdout, " %-36s %s\n", first_half,
  1352. desc->half_liner ? desc->half_liner : "");
  1353. }
  1354. exit(0);
  1355. }
  1356. /*
  1357. * Some scripts specify the required events in their "xxx-record" file,
  1358. * this function will check if the events in perf.data match those
  1359. * mentioned in the "xxx-record".
  1360. *
  1361. * Fixme: All existing "xxx-record" are all in good formats "-e event ",
  1362. * which is covered well now. And new parsing code should be added to
  1363. * cover the future complexing formats like event groups etc.
  1364. */
  1365. static int check_ev_match(char *dir_name, char *scriptname,
  1366. struct perf_session *session)
  1367. {
  1368. char filename[MAXPATHLEN], evname[128];
  1369. char line[BUFSIZ], *p;
  1370. struct perf_evsel *pos;
  1371. int match, len;
  1372. FILE *fp;
  1373. sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
  1374. fp = fopen(filename, "r");
  1375. if (!fp)
  1376. return -1;
  1377. while (fgets(line, sizeof(line), fp)) {
  1378. p = ltrim(line);
  1379. if (*p == '#')
  1380. continue;
  1381. while (strlen(p)) {
  1382. p = strstr(p, "-e");
  1383. if (!p)
  1384. break;
  1385. p += 2;
  1386. p = ltrim(p);
  1387. len = strcspn(p, " \t");
  1388. if (!len)
  1389. break;
  1390. snprintf(evname, len + 1, "%s", p);
  1391. match = 0;
  1392. evlist__for_each(session->evlist, pos) {
  1393. if (!strcmp(perf_evsel__name(pos), evname)) {
  1394. match = 1;
  1395. break;
  1396. }
  1397. }
  1398. if (!match) {
  1399. fclose(fp);
  1400. return -1;
  1401. }
  1402. }
  1403. }
  1404. fclose(fp);
  1405. return 0;
  1406. }
  1407. /*
  1408. * Return -1 if none is found, otherwise the actual scripts number.
  1409. *
  1410. * Currently the only user of this function is the script browser, which
  1411. * will list all statically runnable scripts, select one, execute it and
  1412. * show the output in a perf browser.
  1413. */
  1414. int find_scripts(char **scripts_array, char **scripts_path_array)
  1415. {
  1416. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  1417. char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
  1418. DIR *scripts_dir, *lang_dir;
  1419. struct perf_session *session;
  1420. struct perf_data_file file = {
  1421. .path = input_name,
  1422. .mode = PERF_DATA_MODE_READ,
  1423. };
  1424. char *temp;
  1425. int i = 0;
  1426. session = perf_session__new(&file, false, NULL);
  1427. if (!session)
  1428. return -1;
  1429. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
  1430. scripts_dir = opendir(scripts_path);
  1431. if (!scripts_dir) {
  1432. perf_session__delete(session);
  1433. return -1;
  1434. }
  1435. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  1436. snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
  1437. lang_dirent.d_name);
  1438. #ifdef NO_LIBPERL
  1439. if (strstr(lang_path, "perl"))
  1440. continue;
  1441. #endif
  1442. #ifdef NO_LIBPYTHON
  1443. if (strstr(lang_path, "python"))
  1444. continue;
  1445. #endif
  1446. lang_dir = opendir(lang_path);
  1447. if (!lang_dir)
  1448. continue;
  1449. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  1450. /* Skip those real time scripts: xxxtop.p[yl] */
  1451. if (strstr(script_dirent.d_name, "top."))
  1452. continue;
  1453. sprintf(scripts_path_array[i], "%s/%s", lang_path,
  1454. script_dirent.d_name);
  1455. temp = strchr(script_dirent.d_name, '.');
  1456. snprintf(scripts_array[i],
  1457. (temp - script_dirent.d_name) + 1,
  1458. "%s", script_dirent.d_name);
  1459. if (check_ev_match(lang_path,
  1460. scripts_array[i], session))
  1461. continue;
  1462. i++;
  1463. }
  1464. closedir(lang_dir);
  1465. }
  1466. closedir(scripts_dir);
  1467. perf_session__delete(session);
  1468. return i;
  1469. }
  1470. static char *get_script_path(const char *script_root, const char *suffix)
  1471. {
  1472. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  1473. char scripts_path[MAXPATHLEN];
  1474. char script_path[MAXPATHLEN];
  1475. DIR *scripts_dir, *lang_dir;
  1476. char lang_path[MAXPATHLEN];
  1477. char *__script_root;
  1478. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
  1479. scripts_dir = opendir(scripts_path);
  1480. if (!scripts_dir)
  1481. return NULL;
  1482. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  1483. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  1484. lang_dirent.d_name);
  1485. lang_dir = opendir(lang_path);
  1486. if (!lang_dir)
  1487. continue;
  1488. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  1489. __script_root = get_script_root(&script_dirent, suffix);
  1490. if (__script_root && !strcmp(script_root, __script_root)) {
  1491. free(__script_root);
  1492. closedir(lang_dir);
  1493. closedir(scripts_dir);
  1494. snprintf(script_path, MAXPATHLEN, "%s/%s",
  1495. lang_path, script_dirent.d_name);
  1496. return strdup(script_path);
  1497. }
  1498. free(__script_root);
  1499. }
  1500. closedir(lang_dir);
  1501. }
  1502. closedir(scripts_dir);
  1503. return NULL;
  1504. }
  1505. static bool is_top_script(const char *script_path)
  1506. {
  1507. return ends_with(script_path, "top") == NULL ? false : true;
  1508. }
  1509. static int has_required_arg(char *script_path)
  1510. {
  1511. struct script_desc *desc;
  1512. int n_args = 0;
  1513. char *p;
  1514. desc = script_desc__new(NULL);
  1515. if (read_script_info(desc, script_path))
  1516. goto out;
  1517. if (!desc->args)
  1518. goto out;
  1519. for (p = desc->args; *p; p++)
  1520. if (*p == '<')
  1521. n_args++;
  1522. out:
  1523. script_desc__delete(desc);
  1524. return n_args;
  1525. }
  1526. static int have_cmd(int argc, const char **argv)
  1527. {
  1528. char **__argv = malloc(sizeof(const char *) * argc);
  1529. if (!__argv) {
  1530. pr_err("malloc failed\n");
  1531. return -1;
  1532. }
  1533. memcpy(__argv, argv, sizeof(const char *) * argc);
  1534. argc = parse_options(argc, (const char **)__argv, record_options,
  1535. NULL, PARSE_OPT_STOP_AT_NON_OPTION);
  1536. free(__argv);
  1537. system_wide = (argc == 0);
  1538. return 0;
  1539. }
  1540. static void script__setup_sample_type(struct perf_script *script)
  1541. {
  1542. struct perf_session *session = script->session;
  1543. u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
  1544. if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
  1545. if ((sample_type & PERF_SAMPLE_REGS_USER) &&
  1546. (sample_type & PERF_SAMPLE_STACK_USER))
  1547. callchain_param.record_mode = CALLCHAIN_DWARF;
  1548. else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
  1549. callchain_param.record_mode = CALLCHAIN_LBR;
  1550. else
  1551. callchain_param.record_mode = CALLCHAIN_FP;
  1552. }
  1553. }
  1554. static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
  1555. union perf_event *event,
  1556. struct perf_session *session)
  1557. {
  1558. struct stat_round_event *round = &event->stat_round;
  1559. struct perf_evsel *counter;
  1560. evlist__for_each(session->evlist, counter) {
  1561. perf_stat_process_counter(&stat_config, counter);
  1562. process_stat(counter, round->time);
  1563. }
  1564. process_stat_interval(round->time);
  1565. return 0;
  1566. }
  1567. static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
  1568. union perf_event *event,
  1569. struct perf_session *session __maybe_unused)
  1570. {
  1571. perf_event__read_stat_config(&stat_config, &event->stat_config);
  1572. return 0;
  1573. }
  1574. static int set_maps(struct perf_script *script)
  1575. {
  1576. struct perf_evlist *evlist = script->session->evlist;
  1577. if (!script->cpus || !script->threads)
  1578. return 0;
  1579. if (WARN_ONCE(script->allocated, "stats double allocation\n"))
  1580. return -EINVAL;
  1581. perf_evlist__set_maps(evlist, script->cpus, script->threads);
  1582. if (perf_evlist__alloc_stats(evlist, true))
  1583. return -ENOMEM;
  1584. script->allocated = true;
  1585. return 0;
  1586. }
  1587. static
  1588. int process_thread_map_event(struct perf_tool *tool,
  1589. union perf_event *event,
  1590. struct perf_session *session __maybe_unused)
  1591. {
  1592. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1593. if (script->threads) {
  1594. pr_warning("Extra thread map event, ignoring.\n");
  1595. return 0;
  1596. }
  1597. script->threads = thread_map__new_event(&event->thread_map);
  1598. if (!script->threads)
  1599. return -ENOMEM;
  1600. return set_maps(script);
  1601. }
  1602. static
  1603. int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
  1604. union perf_event *event,
  1605. struct perf_session *session __maybe_unused)
  1606. {
  1607. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1608. if (script->cpus) {
  1609. pr_warning("Extra cpu map event, ignoring.\n");
  1610. return 0;
  1611. }
  1612. script->cpus = cpu_map__new_data(&event->cpu_map.data);
  1613. if (!script->cpus)
  1614. return -ENOMEM;
  1615. return set_maps(script);
  1616. }
  1617. int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
  1618. {
  1619. bool show_full_info = false;
  1620. bool header = false;
  1621. bool header_only = false;
  1622. bool script_started = false;
  1623. char *rec_script_path = NULL;
  1624. char *rep_script_path = NULL;
  1625. struct perf_session *session;
  1626. struct itrace_synth_opts itrace_synth_opts = { .set = false, };
  1627. char *script_path = NULL;
  1628. const char **__argv;
  1629. int i, j, err = 0;
  1630. struct perf_script script = {
  1631. .tool = {
  1632. .sample = process_sample_event,
  1633. .mmap = perf_event__process_mmap,
  1634. .mmap2 = perf_event__process_mmap2,
  1635. .comm = perf_event__process_comm,
  1636. .exit = perf_event__process_exit,
  1637. .fork = perf_event__process_fork,
  1638. .attr = process_attr,
  1639. .tracing_data = perf_event__process_tracing_data,
  1640. .build_id = perf_event__process_build_id,
  1641. .id_index = perf_event__process_id_index,
  1642. .auxtrace_info = perf_event__process_auxtrace_info,
  1643. .auxtrace = perf_event__process_auxtrace,
  1644. .auxtrace_error = perf_event__process_auxtrace_error,
  1645. .stat = perf_event__process_stat_event,
  1646. .stat_round = process_stat_round_event,
  1647. .stat_config = process_stat_config_event,
  1648. .thread_map = process_thread_map_event,
  1649. .cpu_map = process_cpu_map_event,
  1650. .ordered_events = true,
  1651. .ordering_requires_timestamps = true,
  1652. },
  1653. };
  1654. struct perf_data_file file = {
  1655. .mode = PERF_DATA_MODE_READ,
  1656. };
  1657. const struct option options[] = {
  1658. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1659. "dump raw trace in ASCII"),
  1660. OPT_INCR('v', "verbose", &verbose,
  1661. "be more verbose (show symbol address, etc)"),
  1662. OPT_BOOLEAN('L', "Latency", &latency_format,
  1663. "show latency attributes (irqs/preemption disabled, etc)"),
  1664. OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
  1665. list_available_scripts),
  1666. OPT_CALLBACK('s', "script", NULL, "name",
  1667. "script file name (lang:script name, script name, or *)",
  1668. parse_scriptname),
  1669. OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
  1670. "generate perf-script.xx script in specified language"),
  1671. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  1672. OPT_BOOLEAN('d', "debug-mode", &debug_mode,
  1673. "do various checks like samples ordering and lost events"),
  1674. OPT_BOOLEAN(0, "header", &header, "Show data header."),
  1675. OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
  1676. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  1677. "file", "vmlinux pathname"),
  1678. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  1679. "file", "kallsyms pathname"),
  1680. OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
  1681. "When printing symbols do not display call chain"),
  1682. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  1683. "Look for files with symbols relative to this directory"),
  1684. OPT_CALLBACK('F', "fields", NULL, "str",
  1685. "comma separated output fields prepend with 'type:'. "
  1686. "Valid types: hw,sw,trace,raw. "
  1687. "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
  1688. "addr,symoff,period,iregs,brstack,brstacksym,flags", parse_output_fields),
  1689. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  1690. "system-wide collection from all CPUs"),
  1691. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  1692. "only consider these symbols"),
  1693. OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
  1694. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  1695. "only display events for these comms"),
  1696. OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
  1697. "only consider symbols in these pids"),
  1698. OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
  1699. "only consider symbols in these tids"),
  1700. OPT_BOOLEAN('I', "show-info", &show_full_info,
  1701. "display extended information from perf.data file"),
  1702. OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
  1703. "Show the path of [kernel.kallsyms]"),
  1704. OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
  1705. "Show the fork/comm/exit events"),
  1706. OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
  1707. "Show the mmap events"),
  1708. OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
  1709. "Show context switch events (if recorded)"),
  1710. OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
  1711. OPT_BOOLEAN(0, "ns", &nanosecs,
  1712. "Use 9 decimal places when displaying time"),
  1713. OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
  1714. "Instruction Tracing options",
  1715. itrace_parse_synth_opts),
  1716. OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
  1717. "Show full source file name path for source lines"),
  1718. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  1719. "Enable symbol demangling"),
  1720. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  1721. "Enable kernel symbol demangling"),
  1722. OPT_END()
  1723. };
  1724. const char * const script_subcommands[] = { "record", "report", NULL };
  1725. const char *script_usage[] = {
  1726. "perf script [<options>]",
  1727. "perf script [<options>] record <script> [<record-options>] <command>",
  1728. "perf script [<options>] report <script> [script-args]",
  1729. "perf script [<options>] <script> [<record-options>] <command>",
  1730. "perf script [<options>] <top-script> [script-args]",
  1731. NULL
  1732. };
  1733. setup_scripting();
  1734. argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
  1735. PARSE_OPT_STOP_AT_NON_OPTION);
  1736. file.path = input_name;
  1737. if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
  1738. rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
  1739. if (!rec_script_path)
  1740. return cmd_record(argc, argv, NULL);
  1741. }
  1742. if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
  1743. rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
  1744. if (!rep_script_path) {
  1745. fprintf(stderr,
  1746. "Please specify a valid report script"
  1747. "(see 'perf script -l' for listing)\n");
  1748. return -1;
  1749. }
  1750. }
  1751. if (itrace_synth_opts.callchain &&
  1752. itrace_synth_opts.callchain_sz > scripting_max_stack)
  1753. scripting_max_stack = itrace_synth_opts.callchain_sz;
  1754. /* make sure PERF_EXEC_PATH is set for scripts */
  1755. set_argv_exec_path(get_argv_exec_path());
  1756. if (argc && !script_name && !rec_script_path && !rep_script_path) {
  1757. int live_pipe[2];
  1758. int rep_args;
  1759. pid_t pid;
  1760. rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
  1761. rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
  1762. if (!rec_script_path && !rep_script_path) {
  1763. usage_with_options_msg(script_usage, options,
  1764. "Couldn't find script `%s'\n\n See perf"
  1765. " script -l for available scripts.\n", argv[0]);
  1766. }
  1767. if (is_top_script(argv[0])) {
  1768. rep_args = argc - 1;
  1769. } else {
  1770. int rec_args;
  1771. rep_args = has_required_arg(rep_script_path);
  1772. rec_args = (argc - 1) - rep_args;
  1773. if (rec_args < 0) {
  1774. usage_with_options_msg(script_usage, options,
  1775. "`%s' script requires options."
  1776. "\n\n See perf script -l for available "
  1777. "scripts and options.\n", argv[0]);
  1778. }
  1779. }
  1780. if (pipe(live_pipe) < 0) {
  1781. perror("failed to create pipe");
  1782. return -1;
  1783. }
  1784. pid = fork();
  1785. if (pid < 0) {
  1786. perror("failed to fork");
  1787. return -1;
  1788. }
  1789. if (!pid) {
  1790. j = 0;
  1791. dup2(live_pipe[1], 1);
  1792. close(live_pipe[0]);
  1793. if (is_top_script(argv[0])) {
  1794. system_wide = true;
  1795. } else if (!system_wide) {
  1796. if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
  1797. err = -1;
  1798. goto out;
  1799. }
  1800. }
  1801. __argv = malloc((argc + 6) * sizeof(const char *));
  1802. if (!__argv) {
  1803. pr_err("malloc failed\n");
  1804. err = -ENOMEM;
  1805. goto out;
  1806. }
  1807. __argv[j++] = "/bin/sh";
  1808. __argv[j++] = rec_script_path;
  1809. if (system_wide)
  1810. __argv[j++] = "-a";
  1811. __argv[j++] = "-q";
  1812. __argv[j++] = "-o";
  1813. __argv[j++] = "-";
  1814. for (i = rep_args + 1; i < argc; i++)
  1815. __argv[j++] = argv[i];
  1816. __argv[j++] = NULL;
  1817. execvp("/bin/sh", (char **)__argv);
  1818. free(__argv);
  1819. exit(-1);
  1820. }
  1821. dup2(live_pipe[0], 0);
  1822. close(live_pipe[1]);
  1823. __argv = malloc((argc + 4) * sizeof(const char *));
  1824. if (!__argv) {
  1825. pr_err("malloc failed\n");
  1826. err = -ENOMEM;
  1827. goto out;
  1828. }
  1829. j = 0;
  1830. __argv[j++] = "/bin/sh";
  1831. __argv[j++] = rep_script_path;
  1832. for (i = 1; i < rep_args + 1; i++)
  1833. __argv[j++] = argv[i];
  1834. __argv[j++] = "-i";
  1835. __argv[j++] = "-";
  1836. __argv[j++] = NULL;
  1837. execvp("/bin/sh", (char **)__argv);
  1838. free(__argv);
  1839. exit(-1);
  1840. }
  1841. if (rec_script_path)
  1842. script_path = rec_script_path;
  1843. if (rep_script_path)
  1844. script_path = rep_script_path;
  1845. if (script_path) {
  1846. j = 0;
  1847. if (!rec_script_path)
  1848. system_wide = false;
  1849. else if (!system_wide) {
  1850. if (have_cmd(argc - 1, &argv[1]) != 0) {
  1851. err = -1;
  1852. goto out;
  1853. }
  1854. }
  1855. __argv = malloc((argc + 2) * sizeof(const char *));
  1856. if (!__argv) {
  1857. pr_err("malloc failed\n");
  1858. err = -ENOMEM;
  1859. goto out;
  1860. }
  1861. __argv[j++] = "/bin/sh";
  1862. __argv[j++] = script_path;
  1863. if (system_wide)
  1864. __argv[j++] = "-a";
  1865. for (i = 2; i < argc; i++)
  1866. __argv[j++] = argv[i];
  1867. __argv[j++] = NULL;
  1868. execvp("/bin/sh", (char **)__argv);
  1869. free(__argv);
  1870. exit(-1);
  1871. }
  1872. if (!script_name)
  1873. setup_pager();
  1874. session = perf_session__new(&file, false, &script.tool);
  1875. if (session == NULL)
  1876. return -1;
  1877. if (header || header_only) {
  1878. perf_session__fprintf_info(session, stdout, show_full_info);
  1879. if (header_only)
  1880. goto out_delete;
  1881. }
  1882. if (symbol__init(&session->header.env) < 0)
  1883. goto out_delete;
  1884. script.session = session;
  1885. script__setup_sample_type(&script);
  1886. session->itrace_synth_opts = &itrace_synth_opts;
  1887. if (cpu_list) {
  1888. err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
  1889. if (err < 0)
  1890. goto out_delete;
  1891. }
  1892. if (!no_callchain)
  1893. symbol_conf.use_callchain = true;
  1894. else
  1895. symbol_conf.use_callchain = false;
  1896. if (session->tevent.pevent &&
  1897. pevent_set_function_resolver(session->tevent.pevent,
  1898. machine__resolve_kernel_addr,
  1899. &session->machines.host) < 0) {
  1900. pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
  1901. return -1;
  1902. }
  1903. if (generate_script_lang) {
  1904. struct stat perf_stat;
  1905. int input;
  1906. if (output_set_by_user()) {
  1907. fprintf(stderr,
  1908. "custom fields not supported for generated scripts");
  1909. err = -EINVAL;
  1910. goto out_delete;
  1911. }
  1912. input = open(file.path, O_RDONLY); /* input_name */
  1913. if (input < 0) {
  1914. err = -errno;
  1915. perror("failed to open file");
  1916. goto out_delete;
  1917. }
  1918. err = fstat(input, &perf_stat);
  1919. if (err < 0) {
  1920. perror("failed to stat file");
  1921. goto out_delete;
  1922. }
  1923. if (!perf_stat.st_size) {
  1924. fprintf(stderr, "zero-sized file, nothing to do!\n");
  1925. goto out_delete;
  1926. }
  1927. scripting_ops = script_spec__lookup(generate_script_lang);
  1928. if (!scripting_ops) {
  1929. fprintf(stderr, "invalid language specifier");
  1930. err = -ENOENT;
  1931. goto out_delete;
  1932. }
  1933. err = scripting_ops->generate_script(session->tevent.pevent,
  1934. "perf-script");
  1935. goto out_delete;
  1936. }
  1937. if (script_name) {
  1938. err = scripting_ops->start_script(script_name, argc, argv);
  1939. if (err)
  1940. goto out_delete;
  1941. pr_debug("perf script started with script %s\n\n", script_name);
  1942. script_started = true;
  1943. }
  1944. err = perf_session__check_output_opt(session);
  1945. if (err < 0)
  1946. goto out_delete;
  1947. err = __cmd_script(&script);
  1948. flush_scripting();
  1949. out_delete:
  1950. perf_evlist__free_stats(session->evlist);
  1951. perf_session__delete(session);
  1952. if (script_started)
  1953. cleanup_scripting();
  1954. out:
  1955. return err;
  1956. }