builtin-script.c 57 KB

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