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