builtin-script.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "builtin.h"
  3. #include "perf.h"
  4. #include "util/cache.h"
  5. #include "util/debug.h"
  6. #include <subcmd/exec-cmd.h>
  7. #include "util/header.h"
  8. #include <subcmd/parse-options.h>
  9. #include "util/perf_regs.h"
  10. #include "util/session.h"
  11. #include "util/tool.h"
  12. #include "util/symbol.h"
  13. #include "util/thread.h"
  14. #include "util/trace-event.h"
  15. #include "util/util.h"
  16. #include "util/evlist.h"
  17. #include "util/evsel.h"
  18. #include "util/sort.h"
  19. #include "util/data.h"
  20. #include "util/auxtrace.h"
  21. #include "util/cpumap.h"
  22. #include "util/thread_map.h"
  23. #include "util/stat.h"
  24. #include "util/color.h"
  25. #include "util/string2.h"
  26. #include "util/thread-stack.h"
  27. #include "util/time-utils.h"
  28. #include "util/path.h"
  29. #include "print_binary.h"
  30. #include <linux/bitmap.h>
  31. #include <linux/kernel.h>
  32. #include <linux/stringify.h>
  33. #include <linux/time64.h>
  34. #include "asm/bug.h"
  35. #include "util/mem-events.h"
  36. #include "util/dump-insn.h"
  37. #include <dirent.h>
  38. #include <errno.h>
  39. #include <inttypes.h>
  40. #include <signal.h>
  41. #include <sys/param.h>
  42. #include <sys/types.h>
  43. #include <sys/stat.h>
  44. #include <fcntl.h>
  45. #include <unistd.h>
  46. #include <subcmd/pager.h>
  47. #include "sane_ctype.h"
  48. static char const *script_name;
  49. static char const *generate_script_lang;
  50. static bool debug_mode;
  51. static u64 last_timestamp;
  52. static u64 nr_unordered;
  53. static bool no_callchain;
  54. static bool latency_format;
  55. static bool system_wide;
  56. static bool print_flags;
  57. static bool nanosecs;
  58. static const char *cpu_list;
  59. static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
  60. static struct perf_stat_config stat_config;
  61. static int max_blocks;
  62. unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
  63. enum perf_output_field {
  64. PERF_OUTPUT_COMM = 1U << 0,
  65. PERF_OUTPUT_TID = 1U << 1,
  66. PERF_OUTPUT_PID = 1U << 2,
  67. PERF_OUTPUT_TIME = 1U << 3,
  68. PERF_OUTPUT_CPU = 1U << 4,
  69. PERF_OUTPUT_EVNAME = 1U << 5,
  70. PERF_OUTPUT_TRACE = 1U << 6,
  71. PERF_OUTPUT_IP = 1U << 7,
  72. PERF_OUTPUT_SYM = 1U << 8,
  73. PERF_OUTPUT_DSO = 1U << 9,
  74. PERF_OUTPUT_ADDR = 1U << 10,
  75. PERF_OUTPUT_SYMOFFSET = 1U << 11,
  76. PERF_OUTPUT_SRCLINE = 1U << 12,
  77. PERF_OUTPUT_PERIOD = 1U << 13,
  78. PERF_OUTPUT_IREGS = 1U << 14,
  79. PERF_OUTPUT_BRSTACK = 1U << 15,
  80. PERF_OUTPUT_BRSTACKSYM = 1U << 16,
  81. PERF_OUTPUT_DATA_SRC = 1U << 17,
  82. PERF_OUTPUT_WEIGHT = 1U << 18,
  83. PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
  84. PERF_OUTPUT_CALLINDENT = 1U << 20,
  85. PERF_OUTPUT_INSN = 1U << 21,
  86. PERF_OUTPUT_INSNLEN = 1U << 22,
  87. PERF_OUTPUT_BRSTACKINSN = 1U << 23,
  88. PERF_OUTPUT_BRSTACKOFF = 1U << 24,
  89. PERF_OUTPUT_SYNTH = 1U << 25,
  90. PERF_OUTPUT_PHYS_ADDR = 1U << 26,
  91. PERF_OUTPUT_UREGS = 1U << 27,
  92. PERF_OUTPUT_METRIC = 1U << 28,
  93. PERF_OUTPUT_MISC = 1U << 29,
  94. };
  95. struct output_option {
  96. const char *str;
  97. enum perf_output_field field;
  98. } all_output_options[] = {
  99. {.str = "comm", .field = PERF_OUTPUT_COMM},
  100. {.str = "tid", .field = PERF_OUTPUT_TID},
  101. {.str = "pid", .field = PERF_OUTPUT_PID},
  102. {.str = "time", .field = PERF_OUTPUT_TIME},
  103. {.str = "cpu", .field = PERF_OUTPUT_CPU},
  104. {.str = "event", .field = PERF_OUTPUT_EVNAME},
  105. {.str = "trace", .field = PERF_OUTPUT_TRACE},
  106. {.str = "ip", .field = PERF_OUTPUT_IP},
  107. {.str = "sym", .field = PERF_OUTPUT_SYM},
  108. {.str = "dso", .field = PERF_OUTPUT_DSO},
  109. {.str = "addr", .field = PERF_OUTPUT_ADDR},
  110. {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
  111. {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
  112. {.str = "period", .field = PERF_OUTPUT_PERIOD},
  113. {.str = "iregs", .field = PERF_OUTPUT_IREGS},
  114. {.str = "uregs", .field = PERF_OUTPUT_UREGS},
  115. {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
  116. {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
  117. {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
  118. {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
  119. {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
  120. {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
  121. {.str = "insn", .field = PERF_OUTPUT_INSN},
  122. {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
  123. {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
  124. {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
  125. {.str = "synth", .field = PERF_OUTPUT_SYNTH},
  126. {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
  127. {.str = "metric", .field = PERF_OUTPUT_METRIC},
  128. {.str = "misc", .field = PERF_OUTPUT_MISC},
  129. };
  130. enum {
  131. OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
  132. OUTPUT_TYPE_MAX
  133. };
  134. /* default set to maintain compatibility with current format */
  135. static struct {
  136. bool user_set;
  137. bool wildcard_set;
  138. unsigned int print_ip_opts;
  139. u64 fields;
  140. u64 invalid_fields;
  141. } output[OUTPUT_TYPE_MAX] = {
  142. [PERF_TYPE_HARDWARE] = {
  143. .user_set = false,
  144. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  145. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  146. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  147. PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
  148. PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
  149. .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
  150. },
  151. [PERF_TYPE_SOFTWARE] = {
  152. .user_set = false,
  153. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  154. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  155. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  156. PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
  157. PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
  158. PERF_OUTPUT_BPF_OUTPUT,
  159. .invalid_fields = PERF_OUTPUT_TRACE,
  160. },
  161. [PERF_TYPE_TRACEPOINT] = {
  162. .user_set = false,
  163. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  164. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  165. PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
  166. },
  167. [PERF_TYPE_HW_CACHE] = {
  168. .user_set = false,
  169. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  170. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  171. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  172. PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
  173. PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
  174. .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
  175. },
  176. [PERF_TYPE_RAW] = {
  177. .user_set = false,
  178. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  179. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  180. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  181. PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
  182. PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
  183. PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
  184. PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR,
  185. .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
  186. },
  187. [PERF_TYPE_BREAKPOINT] = {
  188. .user_set = false,
  189. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  190. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  191. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  192. PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
  193. PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
  194. .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
  195. },
  196. [OUTPUT_TYPE_SYNTH] = {
  197. .user_set = false,
  198. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  199. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  200. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  201. PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
  202. PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
  203. .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
  204. },
  205. };
  206. struct perf_evsel_script {
  207. char *filename;
  208. FILE *fp;
  209. u64 samples;
  210. /* For metric output */
  211. u64 val;
  212. int gnum;
  213. };
  214. static inline struct perf_evsel_script *evsel_script(struct perf_evsel *evsel)
  215. {
  216. return (struct perf_evsel_script *)evsel->priv;
  217. }
  218. static struct perf_evsel_script *perf_evsel_script__new(struct perf_evsel *evsel,
  219. struct perf_data *data)
  220. {
  221. struct perf_evsel_script *es = zalloc(sizeof(*es));
  222. if (es != NULL) {
  223. if (asprintf(&es->filename, "%s.%s.dump", data->file.path, perf_evsel__name(evsel)) < 0)
  224. goto out_free;
  225. es->fp = fopen(es->filename, "w");
  226. if (es->fp == NULL)
  227. goto out_free_filename;
  228. }
  229. return es;
  230. out_free_filename:
  231. zfree(&es->filename);
  232. out_free:
  233. free(es);
  234. return NULL;
  235. }
  236. static void perf_evsel_script__delete(struct perf_evsel_script *es)
  237. {
  238. zfree(&es->filename);
  239. fclose(es->fp);
  240. es->fp = NULL;
  241. free(es);
  242. }
  243. static int perf_evsel_script__fprintf(struct perf_evsel_script *es, FILE *fp)
  244. {
  245. struct stat st;
  246. fstat(fileno(es->fp), &st);
  247. return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
  248. st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
  249. }
  250. static inline int output_type(unsigned int type)
  251. {
  252. switch (type) {
  253. case PERF_TYPE_SYNTH:
  254. return OUTPUT_TYPE_SYNTH;
  255. default:
  256. return type;
  257. }
  258. }
  259. static inline unsigned int attr_type(unsigned int type)
  260. {
  261. switch (type) {
  262. case OUTPUT_TYPE_SYNTH:
  263. return PERF_TYPE_SYNTH;
  264. default:
  265. return type;
  266. }
  267. }
  268. static bool output_set_by_user(void)
  269. {
  270. int j;
  271. for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
  272. if (output[j].user_set)
  273. return true;
  274. }
  275. return false;
  276. }
  277. static const char *output_field2str(enum perf_output_field field)
  278. {
  279. int i, imax = ARRAY_SIZE(all_output_options);
  280. const char *str = "";
  281. for (i = 0; i < imax; ++i) {
  282. if (all_output_options[i].field == field) {
  283. str = all_output_options[i].str;
  284. break;
  285. }
  286. }
  287. return str;
  288. }
  289. #define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
  290. static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
  291. u64 sample_type, const char *sample_msg,
  292. enum perf_output_field field,
  293. bool allow_user_set)
  294. {
  295. struct perf_event_attr *attr = &evsel->attr;
  296. int type = output_type(attr->type);
  297. const char *evname;
  298. if (attr->sample_type & sample_type)
  299. return 0;
  300. if (output[type].user_set) {
  301. if (allow_user_set)
  302. return 0;
  303. evname = perf_evsel__name(evsel);
  304. pr_err("Samples for '%s' event do not have %s attribute set. "
  305. "Cannot print '%s' field.\n",
  306. evname, sample_msg, output_field2str(field));
  307. return -1;
  308. }
  309. /* user did not ask for it explicitly so remove from the default list */
  310. output[type].fields &= ~field;
  311. evname = perf_evsel__name(evsel);
  312. pr_debug("Samples for '%s' event do not have %s attribute set. "
  313. "Skipping '%s' field.\n",
  314. evname, sample_msg, output_field2str(field));
  315. return 0;
  316. }
  317. static int perf_evsel__check_stype(struct perf_evsel *evsel,
  318. u64 sample_type, const char *sample_msg,
  319. enum perf_output_field field)
  320. {
  321. return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
  322. false);
  323. }
  324. static int perf_evsel__check_attr(struct perf_evsel *evsel,
  325. struct perf_session *session)
  326. {
  327. struct perf_event_attr *attr = &evsel->attr;
  328. bool allow_user_set;
  329. if (perf_header__has_feat(&session->header, HEADER_STAT))
  330. return 0;
  331. allow_user_set = perf_header__has_feat(&session->header,
  332. HEADER_AUXTRACE);
  333. if (PRINT_FIELD(TRACE) &&
  334. !perf_session__has_traces(session, "record -R"))
  335. return -EINVAL;
  336. if (PRINT_FIELD(IP)) {
  337. if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
  338. PERF_OUTPUT_IP))
  339. return -EINVAL;
  340. }
  341. if (PRINT_FIELD(ADDR) &&
  342. perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
  343. PERF_OUTPUT_ADDR, allow_user_set))
  344. return -EINVAL;
  345. if (PRINT_FIELD(DATA_SRC) &&
  346. perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
  347. PERF_OUTPUT_DATA_SRC))
  348. return -EINVAL;
  349. if (PRINT_FIELD(WEIGHT) &&
  350. perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
  351. PERF_OUTPUT_WEIGHT))
  352. return -EINVAL;
  353. if (PRINT_FIELD(SYM) &&
  354. !(evsel->attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
  355. pr_err("Display of symbols requested but neither sample IP nor "
  356. "sample address\navailable. Hence, no addresses to convert "
  357. "to symbols.\n");
  358. return -EINVAL;
  359. }
  360. if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
  361. pr_err("Display of offsets requested but symbol is not"
  362. "selected.\n");
  363. return -EINVAL;
  364. }
  365. if (PRINT_FIELD(DSO) &&
  366. !(evsel->attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
  367. pr_err("Display of DSO requested but no address to convert.\n");
  368. return -EINVAL;
  369. }
  370. if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
  371. pr_err("Display of source line number requested but sample IP is not\n"
  372. "selected. Hence, no address to lookup the source line number.\n");
  373. return -EINVAL;
  374. }
  375. if (PRINT_FIELD(BRSTACKINSN) &&
  376. !(perf_evlist__combined_branch_type(session->evlist) &
  377. PERF_SAMPLE_BRANCH_ANY)) {
  378. pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
  379. "Hint: run 'perf record -b ...'\n");
  380. return -EINVAL;
  381. }
  382. if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
  383. perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
  384. PERF_OUTPUT_TID|PERF_OUTPUT_PID))
  385. return -EINVAL;
  386. if (PRINT_FIELD(TIME) &&
  387. perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
  388. PERF_OUTPUT_TIME))
  389. return -EINVAL;
  390. if (PRINT_FIELD(CPU) &&
  391. perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
  392. PERF_OUTPUT_CPU, allow_user_set))
  393. return -EINVAL;
  394. if (PRINT_FIELD(IREGS) &&
  395. perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
  396. PERF_OUTPUT_IREGS))
  397. return -EINVAL;
  398. if (PRINT_FIELD(UREGS) &&
  399. perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
  400. PERF_OUTPUT_UREGS))
  401. return -EINVAL;
  402. if (PRINT_FIELD(PHYS_ADDR) &&
  403. perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
  404. PERF_OUTPUT_PHYS_ADDR))
  405. return -EINVAL;
  406. return 0;
  407. }
  408. static void set_print_ip_opts(struct perf_event_attr *attr)
  409. {
  410. unsigned int type = output_type(attr->type);
  411. output[type].print_ip_opts = 0;
  412. if (PRINT_FIELD(IP))
  413. output[type].print_ip_opts |= EVSEL__PRINT_IP;
  414. if (PRINT_FIELD(SYM))
  415. output[type].print_ip_opts |= EVSEL__PRINT_SYM;
  416. if (PRINT_FIELD(DSO))
  417. output[type].print_ip_opts |= EVSEL__PRINT_DSO;
  418. if (PRINT_FIELD(SYMOFFSET))
  419. output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
  420. if (PRINT_FIELD(SRCLINE))
  421. output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
  422. }
  423. /*
  424. * verify all user requested events exist and the samples
  425. * have the expected data
  426. */
  427. static int perf_session__check_output_opt(struct perf_session *session)
  428. {
  429. unsigned int j;
  430. struct perf_evsel *evsel;
  431. for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
  432. evsel = perf_session__find_first_evtype(session, attr_type(j));
  433. /*
  434. * even if fields is set to 0 (ie., show nothing) event must
  435. * exist if user explicitly includes it on the command line
  436. */
  437. if (!evsel && output[j].user_set && !output[j].wildcard_set &&
  438. j != OUTPUT_TYPE_SYNTH) {
  439. pr_err("%s events do not exist. "
  440. "Remove corresponding -F option to proceed.\n",
  441. event_type(j));
  442. return -1;
  443. }
  444. if (evsel && output[j].fields &&
  445. perf_evsel__check_attr(evsel, session))
  446. return -1;
  447. if (evsel == NULL)
  448. continue;
  449. set_print_ip_opts(&evsel->attr);
  450. }
  451. if (!no_callchain) {
  452. bool use_callchain = false;
  453. bool not_pipe = false;
  454. evlist__for_each_entry(session->evlist, evsel) {
  455. not_pipe = true;
  456. if (evsel__has_callchain(evsel)) {
  457. use_callchain = true;
  458. break;
  459. }
  460. }
  461. if (not_pipe && !use_callchain)
  462. symbol_conf.use_callchain = false;
  463. }
  464. /*
  465. * set default for tracepoints to print symbols only
  466. * if callchains are present
  467. */
  468. if (symbol_conf.use_callchain &&
  469. !output[PERF_TYPE_TRACEPOINT].user_set) {
  470. j = PERF_TYPE_TRACEPOINT;
  471. evlist__for_each_entry(session->evlist, evsel) {
  472. if (evsel->attr.type != j)
  473. continue;
  474. if (evsel__has_callchain(evsel)) {
  475. output[j].fields |= PERF_OUTPUT_IP;
  476. output[j].fields |= PERF_OUTPUT_SYM;
  477. output[j].fields |= PERF_OUTPUT_SYMOFFSET;
  478. output[j].fields |= PERF_OUTPUT_DSO;
  479. set_print_ip_opts(&evsel->attr);
  480. goto out;
  481. }
  482. }
  483. }
  484. out:
  485. return 0;
  486. }
  487. static int perf_sample__fprintf_iregs(struct perf_sample *sample,
  488. struct perf_event_attr *attr, FILE *fp)
  489. {
  490. struct regs_dump *regs = &sample->intr_regs;
  491. uint64_t mask = attr->sample_regs_intr;
  492. unsigned i = 0, r;
  493. int printed = 0;
  494. if (!regs)
  495. return 0;
  496. for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
  497. u64 val = regs->regs[i++];
  498. printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
  499. }
  500. return printed;
  501. }
  502. static int perf_sample__fprintf_uregs(struct perf_sample *sample,
  503. struct perf_event_attr *attr, FILE *fp)
  504. {
  505. struct regs_dump *regs = &sample->user_regs;
  506. uint64_t mask = attr->sample_regs_user;
  507. unsigned i = 0, r;
  508. int printed = 0;
  509. if (!regs || !regs->regs)
  510. return 0;
  511. printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
  512. for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
  513. u64 val = regs->regs[i++];
  514. printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r), val);
  515. }
  516. return printed;
  517. }
  518. static int perf_sample__fprintf_start(struct perf_sample *sample,
  519. struct thread *thread,
  520. struct perf_evsel *evsel,
  521. u32 type, FILE *fp)
  522. {
  523. struct perf_event_attr *attr = &evsel->attr;
  524. unsigned long secs;
  525. unsigned long long nsecs;
  526. int printed = 0;
  527. if (PRINT_FIELD(COMM)) {
  528. if (latency_format)
  529. printed += fprintf(fp, "%8.8s ", thread__comm_str(thread));
  530. else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
  531. printed += fprintf(fp, "%s ", thread__comm_str(thread));
  532. else
  533. printed += fprintf(fp, "%16s ", thread__comm_str(thread));
  534. }
  535. if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
  536. printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
  537. else if (PRINT_FIELD(PID))
  538. printed += fprintf(fp, "%5d ", sample->pid);
  539. else if (PRINT_FIELD(TID))
  540. printed += fprintf(fp, "%5d ", sample->tid);
  541. if (PRINT_FIELD(CPU)) {
  542. if (latency_format)
  543. printed += fprintf(fp, "%3d ", sample->cpu);
  544. else
  545. printed += fprintf(fp, "[%03d] ", sample->cpu);
  546. }
  547. if (PRINT_FIELD(MISC)) {
  548. int ret = 0;
  549. #define has(m) \
  550. (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
  551. if (has(KERNEL))
  552. ret += fprintf(fp, "K");
  553. if (has(USER))
  554. ret += fprintf(fp, "U");
  555. if (has(HYPERVISOR))
  556. ret += fprintf(fp, "H");
  557. if (has(GUEST_KERNEL))
  558. ret += fprintf(fp, "G");
  559. if (has(GUEST_USER))
  560. ret += fprintf(fp, "g");
  561. switch (type) {
  562. case PERF_RECORD_MMAP:
  563. case PERF_RECORD_MMAP2:
  564. if (has(MMAP_DATA))
  565. ret += fprintf(fp, "M");
  566. break;
  567. case PERF_RECORD_COMM:
  568. if (has(COMM_EXEC))
  569. ret += fprintf(fp, "E");
  570. break;
  571. case PERF_RECORD_SWITCH:
  572. case PERF_RECORD_SWITCH_CPU_WIDE:
  573. if (has(SWITCH_OUT)) {
  574. ret += fprintf(fp, "S");
  575. if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
  576. ret += fprintf(fp, "p");
  577. }
  578. default:
  579. break;
  580. }
  581. #undef has
  582. ret += fprintf(fp, "%*s", 6 - ret, " ");
  583. printed += ret;
  584. }
  585. if (PRINT_FIELD(TIME)) {
  586. nsecs = sample->time;
  587. secs = nsecs / NSEC_PER_SEC;
  588. nsecs -= secs * NSEC_PER_SEC;
  589. if (nanosecs)
  590. printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
  591. else {
  592. char sample_time[32];
  593. timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
  594. printed += fprintf(fp, "%12s: ", sample_time);
  595. }
  596. }
  597. return printed;
  598. }
  599. static inline char
  600. mispred_str(struct branch_entry *br)
  601. {
  602. if (!(br->flags.mispred || br->flags.predicted))
  603. return '-';
  604. return br->flags.predicted ? 'P' : 'M';
  605. }
  606. static int perf_sample__fprintf_brstack(struct perf_sample *sample,
  607. struct thread *thread,
  608. struct perf_event_attr *attr, FILE *fp)
  609. {
  610. struct branch_stack *br = sample->branch_stack;
  611. struct addr_location alf, alt;
  612. u64 i, from, to;
  613. int printed = 0;
  614. if (!(br && br->nr))
  615. return 0;
  616. for (i = 0; i < br->nr; i++) {
  617. from = br->entries[i].from;
  618. to = br->entries[i].to;
  619. if (PRINT_FIELD(DSO)) {
  620. memset(&alf, 0, sizeof(alf));
  621. memset(&alt, 0, sizeof(alt));
  622. thread__find_map(thread, sample->cpumode, from, &alf);
  623. thread__find_map(thread, sample->cpumode, to, &alt);
  624. }
  625. printed += fprintf(fp, " 0x%"PRIx64, from);
  626. if (PRINT_FIELD(DSO)) {
  627. printed += fprintf(fp, "(");
  628. printed += map__fprintf_dsoname(alf.map, fp);
  629. printed += fprintf(fp, ")");
  630. }
  631. printed += fprintf(fp, "/0x%"PRIx64, to);
  632. if (PRINT_FIELD(DSO)) {
  633. printed += fprintf(fp, "(");
  634. printed += map__fprintf_dsoname(alt.map, fp);
  635. printed += fprintf(fp, ")");
  636. }
  637. printed += fprintf(fp, "/%c/%c/%c/%d ",
  638. mispred_str( br->entries + i),
  639. br->entries[i].flags.in_tx? 'X' : '-',
  640. br->entries[i].flags.abort? 'A' : '-',
  641. br->entries[i].flags.cycles);
  642. }
  643. return printed;
  644. }
  645. static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
  646. struct thread *thread,
  647. struct perf_event_attr *attr, FILE *fp)
  648. {
  649. struct branch_stack *br = sample->branch_stack;
  650. struct addr_location alf, alt;
  651. u64 i, from, to;
  652. int printed = 0;
  653. if (!(br && br->nr))
  654. return 0;
  655. for (i = 0; i < br->nr; i++) {
  656. memset(&alf, 0, sizeof(alf));
  657. memset(&alt, 0, sizeof(alt));
  658. from = br->entries[i].from;
  659. to = br->entries[i].to;
  660. thread__find_symbol(thread, sample->cpumode, from, &alf);
  661. thread__find_symbol(thread, sample->cpumode, to, &alt);
  662. printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
  663. if (PRINT_FIELD(DSO)) {
  664. printed += fprintf(fp, "(");
  665. printed += map__fprintf_dsoname(alf.map, fp);
  666. printed += fprintf(fp, ")");
  667. }
  668. printed += fprintf(fp, "%c", '/');
  669. printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
  670. if (PRINT_FIELD(DSO)) {
  671. printed += fprintf(fp, "(");
  672. printed += map__fprintf_dsoname(alt.map, fp);
  673. printed += fprintf(fp, ")");
  674. }
  675. printed += fprintf(fp, "/%c/%c/%c/%d ",
  676. mispred_str( br->entries + i),
  677. br->entries[i].flags.in_tx? 'X' : '-',
  678. br->entries[i].flags.abort? 'A' : '-',
  679. br->entries[i].flags.cycles);
  680. }
  681. return printed;
  682. }
  683. static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
  684. struct thread *thread,
  685. struct perf_event_attr *attr, FILE *fp)
  686. {
  687. struct branch_stack *br = sample->branch_stack;
  688. struct addr_location alf, alt;
  689. u64 i, from, to;
  690. int printed = 0;
  691. if (!(br && br->nr))
  692. return 0;
  693. for (i = 0; i < br->nr; i++) {
  694. memset(&alf, 0, sizeof(alf));
  695. memset(&alt, 0, sizeof(alt));
  696. from = br->entries[i].from;
  697. to = br->entries[i].to;
  698. if (thread__find_map(thread, sample->cpumode, from, &alf) &&
  699. !alf.map->dso->adjust_symbols)
  700. from = map__map_ip(alf.map, from);
  701. if (thread__find_map(thread, sample->cpumode, to, &alt) &&
  702. !alt.map->dso->adjust_symbols)
  703. to = map__map_ip(alt.map, to);
  704. printed += fprintf(fp, " 0x%"PRIx64, from);
  705. if (PRINT_FIELD(DSO)) {
  706. printed += fprintf(fp, "(");
  707. printed += map__fprintf_dsoname(alf.map, fp);
  708. printed += fprintf(fp, ")");
  709. }
  710. printed += fprintf(fp, "/0x%"PRIx64, to);
  711. if (PRINT_FIELD(DSO)) {
  712. printed += fprintf(fp, "(");
  713. printed += map__fprintf_dsoname(alt.map, fp);
  714. printed += fprintf(fp, ")");
  715. }
  716. printed += fprintf(fp, "/%c/%c/%c/%d ",
  717. mispred_str(br->entries + i),
  718. br->entries[i].flags.in_tx ? 'X' : '-',
  719. br->entries[i].flags.abort ? 'A' : '-',
  720. br->entries[i].flags.cycles);
  721. }
  722. return printed;
  723. }
  724. #define MAXBB 16384UL
  725. static int grab_bb(u8 *buffer, u64 start, u64 end,
  726. struct machine *machine, struct thread *thread,
  727. bool *is64bit, u8 *cpumode, bool last)
  728. {
  729. long offset, len;
  730. struct addr_location al;
  731. bool kernel;
  732. if (!start || !end)
  733. return 0;
  734. kernel = machine__kernel_ip(machine, start);
  735. if (kernel)
  736. *cpumode = PERF_RECORD_MISC_KERNEL;
  737. else
  738. *cpumode = PERF_RECORD_MISC_USER;
  739. /*
  740. * Block overlaps between kernel and user.
  741. * This can happen due to ring filtering
  742. * On Intel CPUs the entry into the kernel is filtered,
  743. * but the exit is not. Let the caller patch it up.
  744. */
  745. if (kernel != machine__kernel_ip(machine, end)) {
  746. pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
  747. return -ENXIO;
  748. }
  749. memset(&al, 0, sizeof(al));
  750. if (end - start > MAXBB - MAXINSN) {
  751. if (last)
  752. pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
  753. else
  754. pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
  755. return 0;
  756. }
  757. if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) {
  758. pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
  759. return 0;
  760. }
  761. if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
  762. pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
  763. return 0;
  764. }
  765. /* Load maps to ensure dso->is_64_bit has been updated */
  766. map__load(al.map);
  767. offset = al.map->map_ip(al.map, start);
  768. len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
  769. end - start + MAXINSN);
  770. *is64bit = al.map->dso->is_64_bit;
  771. if (len <= 0)
  772. pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
  773. start, end);
  774. return len;
  775. }
  776. static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
  777. struct perf_insn *x, u8 *inbuf, int len,
  778. int insn, FILE *fp)
  779. {
  780. int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t#%s%s%s%s", ip,
  781. dump_insn(x, ip, inbuf, len, NULL),
  782. en->flags.predicted ? " PRED" : "",
  783. en->flags.mispred ? " MISPRED" : "",
  784. en->flags.in_tx ? " INTX" : "",
  785. en->flags.abort ? " ABORT" : "");
  786. if (en->flags.cycles) {
  787. printed += fprintf(fp, " %d cycles", en->flags.cycles);
  788. if (insn)
  789. printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
  790. }
  791. return printed + fprintf(fp, "\n");
  792. }
  793. static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
  794. u8 cpumode, int cpu, struct symbol **lastsym,
  795. struct perf_event_attr *attr, FILE *fp)
  796. {
  797. struct addr_location al;
  798. int off, printed = 0;
  799. memset(&al, 0, sizeof(al));
  800. thread__find_map(thread, cpumode, addr, &al);
  801. if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
  802. return 0;
  803. al.cpu = cpu;
  804. al.sym = NULL;
  805. if (al.map)
  806. al.sym = map__find_symbol(al.map, al.addr);
  807. if (!al.sym)
  808. return 0;
  809. if (al.addr < al.sym->end)
  810. off = al.addr - al.sym->start;
  811. else
  812. off = al.addr - al.map->start - al.sym->start;
  813. printed += fprintf(fp, "\t%s", al.sym->name);
  814. if (off)
  815. printed += fprintf(fp, "%+d", off);
  816. printed += fprintf(fp, ":");
  817. if (PRINT_FIELD(SRCLINE))
  818. printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
  819. printed += fprintf(fp, "\n");
  820. *lastsym = al.sym;
  821. return printed;
  822. }
  823. static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
  824. struct thread *thread,
  825. struct perf_event_attr *attr,
  826. struct machine *machine, FILE *fp)
  827. {
  828. struct branch_stack *br = sample->branch_stack;
  829. u64 start, end;
  830. int i, insn, len, nr, ilen, printed = 0;
  831. struct perf_insn x;
  832. u8 buffer[MAXBB];
  833. unsigned off;
  834. struct symbol *lastsym = NULL;
  835. if (!(br && br->nr))
  836. return 0;
  837. nr = br->nr;
  838. if (max_blocks && nr > max_blocks + 1)
  839. nr = max_blocks + 1;
  840. x.thread = thread;
  841. x.cpu = sample->cpu;
  842. printed += fprintf(fp, "%c", '\n');
  843. /* Handle first from jump, of which we don't know the entry. */
  844. len = grab_bb(buffer, br->entries[nr-1].from,
  845. br->entries[nr-1].from,
  846. machine, thread, &x.is64bit, &x.cpumode, false);
  847. if (len > 0) {
  848. printed += ip__fprintf_sym(br->entries[nr - 1].from, thread,
  849. x.cpumode, x.cpu, &lastsym, attr, fp);
  850. printed += ip__fprintf_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
  851. &x, buffer, len, 0, fp);
  852. }
  853. /* Print all blocks */
  854. for (i = nr - 2; i >= 0; i--) {
  855. if (br->entries[i].from || br->entries[i].to)
  856. pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
  857. br->entries[i].from,
  858. br->entries[i].to);
  859. start = br->entries[i + 1].to;
  860. end = br->entries[i].from;
  861. len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
  862. /* Patch up missing kernel transfers due to ring filters */
  863. if (len == -ENXIO && i > 0) {
  864. end = br->entries[--i].from;
  865. pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
  866. len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
  867. }
  868. if (len <= 0)
  869. continue;
  870. insn = 0;
  871. for (off = 0;; off += ilen) {
  872. uint64_t ip = start + off;
  873. printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
  874. if (ip == end) {
  875. printed += ip__fprintf_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn, fp);
  876. break;
  877. } else {
  878. printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", ip,
  879. dump_insn(&x, ip, buffer + off, len - off, &ilen));
  880. if (ilen == 0)
  881. break;
  882. insn++;
  883. }
  884. }
  885. }
  886. /*
  887. * Hit the branch? In this case we are already done, and the target
  888. * has not been executed yet.
  889. */
  890. if (br->entries[0].from == sample->ip)
  891. goto out;
  892. if (br->entries[0].flags.abort)
  893. goto out;
  894. /*
  895. * Print final block upto sample
  896. */
  897. start = br->entries[0].to;
  898. end = sample->ip;
  899. len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
  900. printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
  901. if (len <= 0) {
  902. /* Print at least last IP if basic block did not work */
  903. len = grab_bb(buffer, sample->ip, sample->ip,
  904. machine, thread, &x.is64bit, &x.cpumode, false);
  905. if (len <= 0)
  906. goto out;
  907. printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", sample->ip,
  908. dump_insn(&x, sample->ip, buffer, len, NULL));
  909. goto out;
  910. }
  911. for (off = 0; off <= end - start; off += ilen) {
  912. printed += fprintf(fp, "\t%016" PRIx64 "\t%s\n", start + off,
  913. dump_insn(&x, start + off, buffer + off, len - off, &ilen));
  914. if (ilen == 0)
  915. break;
  916. }
  917. out:
  918. return printed;
  919. }
  920. static int perf_sample__fprintf_addr(struct perf_sample *sample,
  921. struct thread *thread,
  922. struct perf_event_attr *attr, FILE *fp)
  923. {
  924. struct addr_location al;
  925. int printed = fprintf(fp, "%16" PRIx64, sample->addr);
  926. if (!sample_addr_correlates_sym(attr))
  927. goto out;
  928. thread__resolve(thread, &al, sample);
  929. if (PRINT_FIELD(SYM)) {
  930. printed += fprintf(fp, " ");
  931. if (PRINT_FIELD(SYMOFFSET))
  932. printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
  933. else
  934. printed += symbol__fprintf_symname(al.sym, fp);
  935. }
  936. if (PRINT_FIELD(DSO)) {
  937. printed += fprintf(fp, " (");
  938. printed += map__fprintf_dsoname(al.map, fp);
  939. printed += fprintf(fp, ")");
  940. }
  941. out:
  942. return printed;
  943. }
  944. static int perf_sample__fprintf_callindent(struct perf_sample *sample,
  945. struct perf_evsel *evsel,
  946. struct thread *thread,
  947. struct addr_location *al, FILE *fp)
  948. {
  949. struct perf_event_attr *attr = &evsel->attr;
  950. size_t depth = thread_stack__depth(thread);
  951. struct addr_location addr_al;
  952. const char *name = NULL;
  953. static int spacing;
  954. int len = 0;
  955. int dlen = 0;
  956. u64 ip = 0;
  957. /*
  958. * The 'return' has already been popped off the stack so the depth has
  959. * to be adjusted to match the 'call'.
  960. */
  961. if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
  962. depth += 1;
  963. if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
  964. if (sample_addr_correlates_sym(attr)) {
  965. thread__resolve(thread, &addr_al, sample);
  966. if (addr_al.sym)
  967. name = addr_al.sym->name;
  968. else
  969. ip = sample->addr;
  970. } else {
  971. ip = sample->addr;
  972. }
  973. } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
  974. if (al->sym)
  975. name = al->sym->name;
  976. else
  977. ip = sample->ip;
  978. }
  979. if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
  980. dlen += fprintf(fp, "(");
  981. dlen += map__fprintf_dsoname(al->map, fp);
  982. dlen += fprintf(fp, ")\t");
  983. }
  984. if (name)
  985. len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
  986. else if (ip)
  987. len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
  988. if (len < 0)
  989. return len;
  990. /*
  991. * Try to keep the output length from changing frequently so that the
  992. * output lines up more nicely.
  993. */
  994. if (len > spacing || (len && len < spacing - 52))
  995. spacing = round_up(len + 4, 32);
  996. if (len < spacing)
  997. len += fprintf(fp, "%*s", spacing - len, "");
  998. return len + dlen;
  999. }
  1000. static int perf_sample__fprintf_insn(struct perf_sample *sample,
  1001. struct perf_event_attr *attr,
  1002. struct thread *thread,
  1003. struct machine *machine, FILE *fp)
  1004. {
  1005. int printed = 0;
  1006. if (PRINT_FIELD(INSNLEN))
  1007. printed += fprintf(fp, " ilen: %d", sample->insn_len);
  1008. if (PRINT_FIELD(INSN)) {
  1009. int i;
  1010. printed += fprintf(fp, " insn:");
  1011. for (i = 0; i < sample->insn_len; i++)
  1012. printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
  1013. }
  1014. if (PRINT_FIELD(BRSTACKINSN))
  1015. printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
  1016. return printed;
  1017. }
  1018. static int perf_sample__fprintf_bts(struct perf_sample *sample,
  1019. struct perf_evsel *evsel,
  1020. struct thread *thread,
  1021. struct addr_location *al,
  1022. struct machine *machine, FILE *fp)
  1023. {
  1024. struct perf_event_attr *attr = &evsel->attr;
  1025. unsigned int type = output_type(attr->type);
  1026. bool print_srcline_last = false;
  1027. int printed = 0;
  1028. if (PRINT_FIELD(CALLINDENT))
  1029. printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, fp);
  1030. /* print branch_from information */
  1031. if (PRINT_FIELD(IP)) {
  1032. unsigned int print_opts = output[type].print_ip_opts;
  1033. struct callchain_cursor *cursor = NULL;
  1034. if (symbol_conf.use_callchain && sample->callchain &&
  1035. thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
  1036. sample, NULL, NULL, scripting_max_stack) == 0)
  1037. cursor = &callchain_cursor;
  1038. if (cursor == NULL) {
  1039. printed += fprintf(fp, " ");
  1040. if (print_opts & EVSEL__PRINT_SRCLINE) {
  1041. print_srcline_last = true;
  1042. print_opts &= ~EVSEL__PRINT_SRCLINE;
  1043. }
  1044. } else
  1045. printed += fprintf(fp, "\n");
  1046. printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor, fp);
  1047. }
  1048. /* print branch_to information */
  1049. if (PRINT_FIELD(ADDR) ||
  1050. ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
  1051. !output[type].user_set)) {
  1052. printed += fprintf(fp, " => ");
  1053. printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
  1054. }
  1055. if (print_srcline_last)
  1056. printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
  1057. printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
  1058. return printed + fprintf(fp, "\n");
  1059. }
  1060. static struct {
  1061. u32 flags;
  1062. const char *name;
  1063. } sample_flags[] = {
  1064. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
  1065. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
  1066. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
  1067. {PERF_IP_FLAG_BRANCH, "jmp"},
  1068. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
  1069. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
  1070. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
  1071. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
  1072. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
  1073. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
  1074. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
  1075. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
  1076. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
  1077. {0, NULL}
  1078. };
  1079. static const char *sample_flags_to_name(u32 flags)
  1080. {
  1081. int i;
  1082. for (i = 0; sample_flags[i].name ; i++) {
  1083. if (sample_flags[i].flags == flags)
  1084. return sample_flags[i].name;
  1085. }
  1086. return NULL;
  1087. }
  1088. static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
  1089. {
  1090. const char *chars = PERF_IP_FLAG_CHARS;
  1091. const int n = strlen(PERF_IP_FLAG_CHARS);
  1092. bool in_tx = flags & PERF_IP_FLAG_IN_TX;
  1093. const char *name = NULL;
  1094. char str[33];
  1095. int i, pos = 0;
  1096. name = sample_flags_to_name(flags & ~PERF_IP_FLAG_IN_TX);
  1097. if (name)
  1098. return fprintf(fp, " %-15s%4s ", name, in_tx ? "(x)" : "");
  1099. if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
  1100. name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_BEGIN));
  1101. if (name)
  1102. return fprintf(fp, " tr strt %-7s%4s ", name, in_tx ? "(x)" : "");
  1103. }
  1104. if (flags & PERF_IP_FLAG_TRACE_END) {
  1105. name = sample_flags_to_name(flags & ~(PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_TRACE_END));
  1106. if (name)
  1107. return fprintf(fp, " tr end %-7s%4s ", name, in_tx ? "(x)" : "");
  1108. }
  1109. for (i = 0; i < n; i++, flags >>= 1) {
  1110. if (flags & 1)
  1111. str[pos++] = chars[i];
  1112. }
  1113. for (; i < 32; i++, flags >>= 1) {
  1114. if (flags & 1)
  1115. str[pos++] = '?';
  1116. }
  1117. str[pos] = 0;
  1118. return fprintf(fp, " %-19s ", str);
  1119. }
  1120. struct printer_data {
  1121. int line_no;
  1122. bool hit_nul;
  1123. bool is_printable;
  1124. };
  1125. static int sample__fprintf_bpf_output(enum binary_printer_ops op,
  1126. unsigned int val,
  1127. void *extra, FILE *fp)
  1128. {
  1129. unsigned char ch = (unsigned char)val;
  1130. struct printer_data *printer_data = extra;
  1131. int printed = 0;
  1132. switch (op) {
  1133. case BINARY_PRINT_DATA_BEGIN:
  1134. printed += fprintf(fp, "\n");
  1135. break;
  1136. case BINARY_PRINT_LINE_BEGIN:
  1137. printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
  1138. " ");
  1139. break;
  1140. case BINARY_PRINT_ADDR:
  1141. printed += fprintf(fp, " %04x:", val);
  1142. break;
  1143. case BINARY_PRINT_NUM_DATA:
  1144. printed += fprintf(fp, " %02x", val);
  1145. break;
  1146. case BINARY_PRINT_NUM_PAD:
  1147. printed += fprintf(fp, " ");
  1148. break;
  1149. case BINARY_PRINT_SEP:
  1150. printed += fprintf(fp, " ");
  1151. break;
  1152. case BINARY_PRINT_CHAR_DATA:
  1153. if (printer_data->hit_nul && ch)
  1154. printer_data->is_printable = false;
  1155. if (!isprint(ch)) {
  1156. printed += fprintf(fp, "%c", '.');
  1157. if (!printer_data->is_printable)
  1158. break;
  1159. if (ch == '\0')
  1160. printer_data->hit_nul = true;
  1161. else
  1162. printer_data->is_printable = false;
  1163. } else {
  1164. printed += fprintf(fp, "%c", ch);
  1165. }
  1166. break;
  1167. case BINARY_PRINT_CHAR_PAD:
  1168. printed += fprintf(fp, " ");
  1169. break;
  1170. case BINARY_PRINT_LINE_END:
  1171. printed += fprintf(fp, "\n");
  1172. printer_data->line_no++;
  1173. break;
  1174. case BINARY_PRINT_DATA_END:
  1175. default:
  1176. break;
  1177. }
  1178. return printed;
  1179. }
  1180. static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
  1181. {
  1182. unsigned int nr_bytes = sample->raw_size;
  1183. struct printer_data printer_data = {0, false, true};
  1184. int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
  1185. sample__fprintf_bpf_output, &printer_data, fp);
  1186. if (printer_data.is_printable && printer_data.hit_nul)
  1187. printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
  1188. return printed;
  1189. }
  1190. static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
  1191. {
  1192. if (len > 0 && len < spacing)
  1193. return fprintf(fp, "%*s", spacing - len, "");
  1194. return 0;
  1195. }
  1196. static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
  1197. {
  1198. return perf_sample__fprintf_spacing(len, 34, fp);
  1199. }
  1200. static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
  1201. {
  1202. struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
  1203. int len;
  1204. if (perf_sample__bad_synth_size(sample, *data))
  1205. return 0;
  1206. len = fprintf(fp, " IP: %u payload: %#" PRIx64 " ",
  1207. data->ip, le64_to_cpu(data->payload));
  1208. return len + perf_sample__fprintf_pt_spacing(len, fp);
  1209. }
  1210. static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
  1211. {
  1212. struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
  1213. int len;
  1214. if (perf_sample__bad_synth_size(sample, *data))
  1215. return 0;
  1216. len = fprintf(fp, " hints: %#x extensions: %#x ",
  1217. data->hints, data->extensions);
  1218. return len + perf_sample__fprintf_pt_spacing(len, fp);
  1219. }
  1220. static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
  1221. {
  1222. struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
  1223. int len;
  1224. if (perf_sample__bad_synth_size(sample, *data))
  1225. return 0;
  1226. len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
  1227. data->hw, data->cstate, data->subcstate);
  1228. return len + perf_sample__fprintf_pt_spacing(len, fp);
  1229. }
  1230. static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
  1231. {
  1232. struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
  1233. int len;
  1234. if (perf_sample__bad_synth_size(sample, *data))
  1235. return 0;
  1236. len = fprintf(fp, " IP: %u ", data->ip);
  1237. return len + perf_sample__fprintf_pt_spacing(len, fp);
  1238. }
  1239. static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
  1240. {
  1241. struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
  1242. int len;
  1243. if (perf_sample__bad_synth_size(sample, *data))
  1244. return 0;
  1245. len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
  1246. data->deepest_cstate, data->last_cstate,
  1247. data->wake_reason);
  1248. return len + perf_sample__fprintf_pt_spacing(len, fp);
  1249. }
  1250. static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
  1251. {
  1252. struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
  1253. unsigned int percent, freq;
  1254. int len;
  1255. if (perf_sample__bad_synth_size(sample, *data))
  1256. return 0;
  1257. freq = (le32_to_cpu(data->freq) + 500) / 1000;
  1258. len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
  1259. if (data->max_nonturbo) {
  1260. percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
  1261. len += fprintf(fp, "(%3u%%) ", percent);
  1262. }
  1263. return len + perf_sample__fprintf_pt_spacing(len, fp);
  1264. }
  1265. static int perf_sample__fprintf_synth(struct perf_sample *sample,
  1266. struct perf_evsel *evsel, FILE *fp)
  1267. {
  1268. switch (evsel->attr.config) {
  1269. case PERF_SYNTH_INTEL_PTWRITE:
  1270. return perf_sample__fprintf_synth_ptwrite(sample, fp);
  1271. case PERF_SYNTH_INTEL_MWAIT:
  1272. return perf_sample__fprintf_synth_mwait(sample, fp);
  1273. case PERF_SYNTH_INTEL_PWRE:
  1274. return perf_sample__fprintf_synth_pwre(sample, fp);
  1275. case PERF_SYNTH_INTEL_EXSTOP:
  1276. return perf_sample__fprintf_synth_exstop(sample, fp);
  1277. case PERF_SYNTH_INTEL_PWRX:
  1278. return perf_sample__fprintf_synth_pwrx(sample, fp);
  1279. case PERF_SYNTH_INTEL_CBR:
  1280. return perf_sample__fprintf_synth_cbr(sample, fp);
  1281. default:
  1282. break;
  1283. }
  1284. return 0;
  1285. }
  1286. struct perf_script {
  1287. struct perf_tool tool;
  1288. struct perf_session *session;
  1289. bool show_task_events;
  1290. bool show_mmap_events;
  1291. bool show_switch_events;
  1292. bool show_namespace_events;
  1293. bool show_lost_events;
  1294. bool show_round_events;
  1295. bool allocated;
  1296. bool per_event_dump;
  1297. struct cpu_map *cpus;
  1298. struct thread_map *threads;
  1299. int name_width;
  1300. const char *time_str;
  1301. struct perf_time_interval *ptime_range;
  1302. int range_size;
  1303. int range_num;
  1304. };
  1305. static int perf_evlist__max_name_len(struct perf_evlist *evlist)
  1306. {
  1307. struct perf_evsel *evsel;
  1308. int max = 0;
  1309. evlist__for_each_entry(evlist, evsel) {
  1310. int len = strlen(perf_evsel__name(evsel));
  1311. max = MAX(len, max);
  1312. }
  1313. return max;
  1314. }
  1315. static int data_src__fprintf(u64 data_src, FILE *fp)
  1316. {
  1317. struct mem_info mi = { .data_src.val = data_src };
  1318. char decode[100];
  1319. char out[100];
  1320. static int maxlen;
  1321. int len;
  1322. perf_script__meminfo_scnprintf(decode, 100, &mi);
  1323. len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
  1324. if (maxlen < len)
  1325. maxlen = len;
  1326. return fprintf(fp, "%-*s", maxlen, out);
  1327. }
  1328. struct metric_ctx {
  1329. struct perf_sample *sample;
  1330. struct thread *thread;
  1331. struct perf_evsel *evsel;
  1332. FILE *fp;
  1333. };
  1334. static void script_print_metric(struct perf_stat_config *config __maybe_unused,
  1335. void *ctx, const char *color,
  1336. const char *fmt,
  1337. const char *unit, double val)
  1338. {
  1339. struct metric_ctx *mctx = ctx;
  1340. if (!fmt)
  1341. return;
  1342. perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
  1343. PERF_RECORD_SAMPLE, mctx->fp);
  1344. fputs("\tmetric: ", mctx->fp);
  1345. if (color)
  1346. color_fprintf(mctx->fp, color, fmt, val);
  1347. else
  1348. printf(fmt, val);
  1349. fprintf(mctx->fp, " %s\n", unit);
  1350. }
  1351. static void script_new_line(struct perf_stat_config *config __maybe_unused,
  1352. void *ctx)
  1353. {
  1354. struct metric_ctx *mctx = ctx;
  1355. perf_sample__fprintf_start(mctx->sample, mctx->thread, mctx->evsel,
  1356. PERF_RECORD_SAMPLE, mctx->fp);
  1357. fputs("\tmetric: ", mctx->fp);
  1358. }
  1359. static void perf_sample__fprint_metric(struct perf_script *script,
  1360. struct thread *thread,
  1361. struct perf_evsel *evsel,
  1362. struct perf_sample *sample,
  1363. FILE *fp)
  1364. {
  1365. struct perf_stat_output_ctx ctx = {
  1366. .print_metric = script_print_metric,
  1367. .new_line = script_new_line,
  1368. .ctx = &(struct metric_ctx) {
  1369. .sample = sample,
  1370. .thread = thread,
  1371. .evsel = evsel,
  1372. .fp = fp,
  1373. },
  1374. .force_header = false,
  1375. };
  1376. struct perf_evsel *ev2;
  1377. static bool init;
  1378. u64 val;
  1379. if (!init) {
  1380. perf_stat__init_shadow_stats();
  1381. init = true;
  1382. }
  1383. if (!evsel->stats)
  1384. perf_evlist__alloc_stats(script->session->evlist, false);
  1385. if (evsel_script(evsel->leader)->gnum++ == 0)
  1386. perf_stat__reset_shadow_stats();
  1387. val = sample->period * evsel->scale;
  1388. perf_stat__update_shadow_stats(evsel,
  1389. val,
  1390. sample->cpu,
  1391. &rt_stat);
  1392. evsel_script(evsel)->val = val;
  1393. if (evsel_script(evsel->leader)->gnum == evsel->leader->nr_members) {
  1394. for_each_group_member (ev2, evsel->leader) {
  1395. perf_stat__print_shadow_stats(&stat_config, ev2,
  1396. evsel_script(ev2)->val,
  1397. sample->cpu,
  1398. &ctx,
  1399. NULL,
  1400. &rt_stat);
  1401. }
  1402. evsel_script(evsel->leader)->gnum = 0;
  1403. }
  1404. }
  1405. static void process_event(struct perf_script *script,
  1406. struct perf_sample *sample, struct perf_evsel *evsel,
  1407. struct addr_location *al,
  1408. struct machine *machine)
  1409. {
  1410. struct thread *thread = al->thread;
  1411. struct perf_event_attr *attr = &evsel->attr;
  1412. unsigned int type = output_type(attr->type);
  1413. struct perf_evsel_script *es = evsel->priv;
  1414. FILE *fp = es->fp;
  1415. if (output[type].fields == 0)
  1416. return;
  1417. ++es->samples;
  1418. perf_sample__fprintf_start(sample, thread, evsel,
  1419. PERF_RECORD_SAMPLE, fp);
  1420. if (PRINT_FIELD(PERIOD))
  1421. fprintf(fp, "%10" PRIu64 " ", sample->period);
  1422. if (PRINT_FIELD(EVNAME)) {
  1423. const char *evname = perf_evsel__name(evsel);
  1424. if (!script->name_width)
  1425. script->name_width = perf_evlist__max_name_len(script->session->evlist);
  1426. fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
  1427. }
  1428. if (print_flags)
  1429. perf_sample__fprintf_flags(sample->flags, fp);
  1430. if (is_bts_event(attr)) {
  1431. perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
  1432. return;
  1433. }
  1434. if (PRINT_FIELD(TRACE)) {
  1435. event_format__fprintf(evsel->tp_format, sample->cpu,
  1436. sample->raw_data, sample->raw_size, fp);
  1437. }
  1438. if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
  1439. perf_sample__fprintf_synth(sample, evsel, fp);
  1440. if (PRINT_FIELD(ADDR))
  1441. perf_sample__fprintf_addr(sample, thread, attr, fp);
  1442. if (PRINT_FIELD(DATA_SRC))
  1443. data_src__fprintf(sample->data_src, fp);
  1444. if (PRINT_FIELD(WEIGHT))
  1445. fprintf(fp, "%16" PRIu64, sample->weight);
  1446. if (PRINT_FIELD(IP)) {
  1447. struct callchain_cursor *cursor = NULL;
  1448. if (symbol_conf.use_callchain && sample->callchain &&
  1449. thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
  1450. sample, NULL, NULL, scripting_max_stack) == 0)
  1451. cursor = &callchain_cursor;
  1452. fputc(cursor ? '\n' : ' ', fp);
  1453. sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
  1454. }
  1455. if (PRINT_FIELD(IREGS))
  1456. perf_sample__fprintf_iregs(sample, attr, fp);
  1457. if (PRINT_FIELD(UREGS))
  1458. perf_sample__fprintf_uregs(sample, attr, fp);
  1459. if (PRINT_FIELD(BRSTACK))
  1460. perf_sample__fprintf_brstack(sample, thread, attr, fp);
  1461. else if (PRINT_FIELD(BRSTACKSYM))
  1462. perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
  1463. else if (PRINT_FIELD(BRSTACKOFF))
  1464. perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
  1465. if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
  1466. perf_sample__fprintf_bpf_output(sample, fp);
  1467. perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
  1468. if (PRINT_FIELD(PHYS_ADDR))
  1469. fprintf(fp, "%16" PRIx64, sample->phys_addr);
  1470. fprintf(fp, "\n");
  1471. if (PRINT_FIELD(METRIC))
  1472. perf_sample__fprint_metric(script, thread, evsel, sample, fp);
  1473. if (verbose)
  1474. fflush(fp);
  1475. }
  1476. static struct scripting_ops *scripting_ops;
  1477. static void __process_stat(struct perf_evsel *counter, u64 tstamp)
  1478. {
  1479. int nthreads = thread_map__nr(counter->threads);
  1480. int ncpus = perf_evsel__nr_cpus(counter);
  1481. int cpu, thread;
  1482. static int header_printed;
  1483. if (counter->system_wide)
  1484. nthreads = 1;
  1485. if (!header_printed) {
  1486. printf("%3s %8s %15s %15s %15s %15s %s\n",
  1487. "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
  1488. header_printed = 1;
  1489. }
  1490. for (thread = 0; thread < nthreads; thread++) {
  1491. for (cpu = 0; cpu < ncpus; cpu++) {
  1492. struct perf_counts_values *counts;
  1493. counts = perf_counts(counter->counts, cpu, thread);
  1494. printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
  1495. counter->cpus->map[cpu],
  1496. thread_map__pid(counter->threads, thread),
  1497. counts->val,
  1498. counts->ena,
  1499. counts->run,
  1500. tstamp,
  1501. perf_evsel__name(counter));
  1502. }
  1503. }
  1504. }
  1505. static void process_stat(struct perf_evsel *counter, u64 tstamp)
  1506. {
  1507. if (scripting_ops && scripting_ops->process_stat)
  1508. scripting_ops->process_stat(&stat_config, counter, tstamp);
  1509. else
  1510. __process_stat(counter, tstamp);
  1511. }
  1512. static void process_stat_interval(u64 tstamp)
  1513. {
  1514. if (scripting_ops && scripting_ops->process_stat_interval)
  1515. scripting_ops->process_stat_interval(tstamp);
  1516. }
  1517. static void setup_scripting(void)
  1518. {
  1519. setup_perl_scripting();
  1520. setup_python_scripting();
  1521. }
  1522. static int flush_scripting(void)
  1523. {
  1524. return scripting_ops ? scripting_ops->flush_script() : 0;
  1525. }
  1526. static int cleanup_scripting(void)
  1527. {
  1528. pr_debug("\nperf script stopped\n");
  1529. return scripting_ops ? scripting_ops->stop_script() : 0;
  1530. }
  1531. static int process_sample_event(struct perf_tool *tool,
  1532. union perf_event *event,
  1533. struct perf_sample *sample,
  1534. struct perf_evsel *evsel,
  1535. struct machine *machine)
  1536. {
  1537. struct perf_script *scr = container_of(tool, struct perf_script, tool);
  1538. struct addr_location al;
  1539. if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
  1540. sample->time)) {
  1541. return 0;
  1542. }
  1543. if (debug_mode) {
  1544. if (sample->time < last_timestamp) {
  1545. pr_err("Samples misordered, previous: %" PRIu64
  1546. " this: %" PRIu64 "\n", last_timestamp,
  1547. sample->time);
  1548. nr_unordered++;
  1549. }
  1550. last_timestamp = sample->time;
  1551. return 0;
  1552. }
  1553. if (machine__resolve(machine, &al, sample) < 0) {
  1554. pr_err("problem processing %d event, skipping it.\n",
  1555. event->header.type);
  1556. return -1;
  1557. }
  1558. if (al.filtered)
  1559. goto out_put;
  1560. if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
  1561. goto out_put;
  1562. if (scripting_ops)
  1563. scripting_ops->process_event(event, sample, evsel, &al);
  1564. else
  1565. process_event(scr, sample, evsel, &al, machine);
  1566. out_put:
  1567. addr_location__put(&al);
  1568. return 0;
  1569. }
  1570. static int process_attr(struct perf_tool *tool, union perf_event *event,
  1571. struct perf_evlist **pevlist)
  1572. {
  1573. struct perf_script *scr = container_of(tool, struct perf_script, tool);
  1574. struct perf_evlist *evlist;
  1575. struct perf_evsel *evsel, *pos;
  1576. int err;
  1577. static struct perf_evsel_script *es;
  1578. err = perf_event__process_attr(tool, event, pevlist);
  1579. if (err)
  1580. return err;
  1581. evlist = *pevlist;
  1582. evsel = perf_evlist__last(*pevlist);
  1583. if (!evsel->priv) {
  1584. if (scr->per_event_dump) {
  1585. evsel->priv = perf_evsel_script__new(evsel,
  1586. scr->session->data);
  1587. } else {
  1588. es = zalloc(sizeof(*es));
  1589. if (!es)
  1590. return -ENOMEM;
  1591. es->fp = stdout;
  1592. evsel->priv = es;
  1593. }
  1594. }
  1595. if (evsel->attr.type >= PERF_TYPE_MAX &&
  1596. evsel->attr.type != PERF_TYPE_SYNTH)
  1597. return 0;
  1598. evlist__for_each_entry(evlist, pos) {
  1599. if (pos->attr.type == evsel->attr.type && pos != evsel)
  1600. return 0;
  1601. }
  1602. set_print_ip_opts(&evsel->attr);
  1603. if (evsel->attr.sample_type)
  1604. err = perf_evsel__check_attr(evsel, scr->session);
  1605. return err;
  1606. }
  1607. static int process_comm_event(struct perf_tool *tool,
  1608. union perf_event *event,
  1609. struct perf_sample *sample,
  1610. struct machine *machine)
  1611. {
  1612. struct thread *thread;
  1613. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1614. struct perf_session *session = script->session;
  1615. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  1616. int ret = -1;
  1617. thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
  1618. if (thread == NULL) {
  1619. pr_debug("problem processing COMM event, skipping it.\n");
  1620. return -1;
  1621. }
  1622. if (perf_event__process_comm(tool, event, sample, machine) < 0)
  1623. goto out;
  1624. if (!evsel->attr.sample_id_all) {
  1625. sample->cpu = 0;
  1626. sample->time = 0;
  1627. sample->tid = event->comm.tid;
  1628. sample->pid = event->comm.pid;
  1629. }
  1630. perf_sample__fprintf_start(sample, thread, evsel,
  1631. PERF_RECORD_COMM, stdout);
  1632. perf_event__fprintf(event, stdout);
  1633. ret = 0;
  1634. out:
  1635. thread__put(thread);
  1636. return ret;
  1637. }
  1638. static int process_namespaces_event(struct perf_tool *tool,
  1639. union perf_event *event,
  1640. struct perf_sample *sample,
  1641. struct machine *machine)
  1642. {
  1643. struct thread *thread;
  1644. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1645. struct perf_session *session = script->session;
  1646. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  1647. int ret = -1;
  1648. thread = machine__findnew_thread(machine, event->namespaces.pid,
  1649. event->namespaces.tid);
  1650. if (thread == NULL) {
  1651. pr_debug("problem processing NAMESPACES event, skipping it.\n");
  1652. return -1;
  1653. }
  1654. if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
  1655. goto out;
  1656. if (!evsel->attr.sample_id_all) {
  1657. sample->cpu = 0;
  1658. sample->time = 0;
  1659. sample->tid = event->namespaces.tid;
  1660. sample->pid = event->namespaces.pid;
  1661. }
  1662. perf_sample__fprintf_start(sample, thread, evsel,
  1663. PERF_RECORD_NAMESPACES, stdout);
  1664. perf_event__fprintf(event, stdout);
  1665. ret = 0;
  1666. out:
  1667. thread__put(thread);
  1668. return ret;
  1669. }
  1670. static int process_fork_event(struct perf_tool *tool,
  1671. union perf_event *event,
  1672. struct perf_sample *sample,
  1673. struct machine *machine)
  1674. {
  1675. struct thread *thread;
  1676. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1677. struct perf_session *session = script->session;
  1678. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  1679. if (perf_event__process_fork(tool, event, sample, machine) < 0)
  1680. return -1;
  1681. thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
  1682. if (thread == NULL) {
  1683. pr_debug("problem processing FORK event, skipping it.\n");
  1684. return -1;
  1685. }
  1686. if (!evsel->attr.sample_id_all) {
  1687. sample->cpu = 0;
  1688. sample->time = event->fork.time;
  1689. sample->tid = event->fork.tid;
  1690. sample->pid = event->fork.pid;
  1691. }
  1692. perf_sample__fprintf_start(sample, thread, evsel,
  1693. PERF_RECORD_FORK, stdout);
  1694. perf_event__fprintf(event, stdout);
  1695. thread__put(thread);
  1696. return 0;
  1697. }
  1698. static int process_exit_event(struct perf_tool *tool,
  1699. union perf_event *event,
  1700. struct perf_sample *sample,
  1701. struct machine *machine)
  1702. {
  1703. int err = 0;
  1704. struct thread *thread;
  1705. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1706. struct perf_session *session = script->session;
  1707. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  1708. thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
  1709. if (thread == NULL) {
  1710. pr_debug("problem processing EXIT event, skipping it.\n");
  1711. return -1;
  1712. }
  1713. if (!evsel->attr.sample_id_all) {
  1714. sample->cpu = 0;
  1715. sample->time = 0;
  1716. sample->tid = event->fork.tid;
  1717. sample->pid = event->fork.pid;
  1718. }
  1719. perf_sample__fprintf_start(sample, thread, evsel,
  1720. PERF_RECORD_EXIT, stdout);
  1721. perf_event__fprintf(event, stdout);
  1722. if (perf_event__process_exit(tool, event, sample, machine) < 0)
  1723. err = -1;
  1724. thread__put(thread);
  1725. return err;
  1726. }
  1727. static int process_mmap_event(struct perf_tool *tool,
  1728. union perf_event *event,
  1729. struct perf_sample *sample,
  1730. struct machine *machine)
  1731. {
  1732. struct thread *thread;
  1733. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1734. struct perf_session *session = script->session;
  1735. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  1736. if (perf_event__process_mmap(tool, event, sample, machine) < 0)
  1737. return -1;
  1738. thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
  1739. if (thread == NULL) {
  1740. pr_debug("problem processing MMAP event, skipping it.\n");
  1741. return -1;
  1742. }
  1743. if (!evsel->attr.sample_id_all) {
  1744. sample->cpu = 0;
  1745. sample->time = 0;
  1746. sample->tid = event->mmap.tid;
  1747. sample->pid = event->mmap.pid;
  1748. }
  1749. perf_sample__fprintf_start(sample, thread, evsel,
  1750. PERF_RECORD_MMAP, stdout);
  1751. perf_event__fprintf(event, stdout);
  1752. thread__put(thread);
  1753. return 0;
  1754. }
  1755. static int process_mmap2_event(struct perf_tool *tool,
  1756. union perf_event *event,
  1757. struct perf_sample *sample,
  1758. struct machine *machine)
  1759. {
  1760. struct thread *thread;
  1761. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1762. struct perf_session *session = script->session;
  1763. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  1764. if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
  1765. return -1;
  1766. thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
  1767. if (thread == NULL) {
  1768. pr_debug("problem processing MMAP2 event, skipping it.\n");
  1769. return -1;
  1770. }
  1771. if (!evsel->attr.sample_id_all) {
  1772. sample->cpu = 0;
  1773. sample->time = 0;
  1774. sample->tid = event->mmap2.tid;
  1775. sample->pid = event->mmap2.pid;
  1776. }
  1777. perf_sample__fprintf_start(sample, thread, evsel,
  1778. PERF_RECORD_MMAP2, stdout);
  1779. perf_event__fprintf(event, stdout);
  1780. thread__put(thread);
  1781. return 0;
  1782. }
  1783. static int process_switch_event(struct perf_tool *tool,
  1784. union perf_event *event,
  1785. struct perf_sample *sample,
  1786. struct machine *machine)
  1787. {
  1788. struct thread *thread;
  1789. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1790. struct perf_session *session = script->session;
  1791. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  1792. if (perf_event__process_switch(tool, event, sample, machine) < 0)
  1793. return -1;
  1794. thread = machine__findnew_thread(machine, sample->pid,
  1795. sample->tid);
  1796. if (thread == NULL) {
  1797. pr_debug("problem processing SWITCH event, skipping it.\n");
  1798. return -1;
  1799. }
  1800. perf_sample__fprintf_start(sample, thread, evsel,
  1801. PERF_RECORD_SWITCH, stdout);
  1802. perf_event__fprintf(event, stdout);
  1803. thread__put(thread);
  1804. return 0;
  1805. }
  1806. static int
  1807. process_lost_event(struct perf_tool *tool,
  1808. union perf_event *event,
  1809. struct perf_sample *sample,
  1810. struct machine *machine)
  1811. {
  1812. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1813. struct perf_session *session = script->session;
  1814. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  1815. struct thread *thread;
  1816. thread = machine__findnew_thread(machine, sample->pid,
  1817. sample->tid);
  1818. if (thread == NULL)
  1819. return -1;
  1820. perf_sample__fprintf_start(sample, thread, evsel,
  1821. PERF_RECORD_LOST, stdout);
  1822. perf_event__fprintf(event, stdout);
  1823. thread__put(thread);
  1824. return 0;
  1825. }
  1826. static int
  1827. process_finished_round_event(struct perf_tool *tool __maybe_unused,
  1828. union perf_event *event,
  1829. struct ordered_events *oe __maybe_unused)
  1830. {
  1831. perf_event__fprintf(event, stdout);
  1832. return 0;
  1833. }
  1834. static void sig_handler(int sig __maybe_unused)
  1835. {
  1836. session_done = 1;
  1837. }
  1838. static void perf_script__fclose_per_event_dump(struct perf_script *script)
  1839. {
  1840. struct perf_evlist *evlist = script->session->evlist;
  1841. struct perf_evsel *evsel;
  1842. evlist__for_each_entry(evlist, evsel) {
  1843. if (!evsel->priv)
  1844. break;
  1845. perf_evsel_script__delete(evsel->priv);
  1846. evsel->priv = NULL;
  1847. }
  1848. }
  1849. static int perf_script__fopen_per_event_dump(struct perf_script *script)
  1850. {
  1851. struct perf_evsel *evsel;
  1852. evlist__for_each_entry(script->session->evlist, evsel) {
  1853. /*
  1854. * Already setup? I.e. we may be called twice in cases like
  1855. * Intel PT, one for the intel_pt// and dummy events, then
  1856. * for the evsels syntheized from the auxtrace info.
  1857. *
  1858. * Ses perf_script__process_auxtrace_info.
  1859. */
  1860. if (evsel->priv != NULL)
  1861. continue;
  1862. evsel->priv = perf_evsel_script__new(evsel, script->session->data);
  1863. if (evsel->priv == NULL)
  1864. goto out_err_fclose;
  1865. }
  1866. return 0;
  1867. out_err_fclose:
  1868. perf_script__fclose_per_event_dump(script);
  1869. return -1;
  1870. }
  1871. static int perf_script__setup_per_event_dump(struct perf_script *script)
  1872. {
  1873. struct perf_evsel *evsel;
  1874. static struct perf_evsel_script es_stdout;
  1875. if (script->per_event_dump)
  1876. return perf_script__fopen_per_event_dump(script);
  1877. es_stdout.fp = stdout;
  1878. evlist__for_each_entry(script->session->evlist, evsel)
  1879. evsel->priv = &es_stdout;
  1880. return 0;
  1881. }
  1882. static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
  1883. {
  1884. struct perf_evsel *evsel;
  1885. evlist__for_each_entry(script->session->evlist, evsel) {
  1886. struct perf_evsel_script *es = evsel->priv;
  1887. perf_evsel_script__fprintf(es, stdout);
  1888. perf_evsel_script__delete(es);
  1889. evsel->priv = NULL;
  1890. }
  1891. }
  1892. static int __cmd_script(struct perf_script *script)
  1893. {
  1894. int ret;
  1895. signal(SIGINT, sig_handler);
  1896. /* override event processing functions */
  1897. if (script->show_task_events) {
  1898. script->tool.comm = process_comm_event;
  1899. script->tool.fork = process_fork_event;
  1900. script->tool.exit = process_exit_event;
  1901. }
  1902. if (script->show_mmap_events) {
  1903. script->tool.mmap = process_mmap_event;
  1904. script->tool.mmap2 = process_mmap2_event;
  1905. }
  1906. if (script->show_switch_events)
  1907. script->tool.context_switch = process_switch_event;
  1908. if (script->show_namespace_events)
  1909. script->tool.namespaces = process_namespaces_event;
  1910. if (script->show_lost_events)
  1911. script->tool.lost = process_lost_event;
  1912. if (script->show_round_events) {
  1913. script->tool.ordered_events = false;
  1914. script->tool.finished_round = process_finished_round_event;
  1915. }
  1916. if (perf_script__setup_per_event_dump(script)) {
  1917. pr_err("Couldn't create the per event dump files\n");
  1918. return -1;
  1919. }
  1920. ret = perf_session__process_events(script->session);
  1921. if (script->per_event_dump)
  1922. perf_script__exit_per_event_dump_stats(script);
  1923. if (debug_mode)
  1924. pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
  1925. return ret;
  1926. }
  1927. struct script_spec {
  1928. struct list_head node;
  1929. struct scripting_ops *ops;
  1930. char spec[0];
  1931. };
  1932. static LIST_HEAD(script_specs);
  1933. static struct script_spec *script_spec__new(const char *spec,
  1934. struct scripting_ops *ops)
  1935. {
  1936. struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
  1937. if (s != NULL) {
  1938. strcpy(s->spec, spec);
  1939. s->ops = ops;
  1940. }
  1941. return s;
  1942. }
  1943. static void script_spec__add(struct script_spec *s)
  1944. {
  1945. list_add_tail(&s->node, &script_specs);
  1946. }
  1947. static struct script_spec *script_spec__find(const char *spec)
  1948. {
  1949. struct script_spec *s;
  1950. list_for_each_entry(s, &script_specs, node)
  1951. if (strcasecmp(s->spec, spec) == 0)
  1952. return s;
  1953. return NULL;
  1954. }
  1955. int script_spec_register(const char *spec, struct scripting_ops *ops)
  1956. {
  1957. struct script_spec *s;
  1958. s = script_spec__find(spec);
  1959. if (s)
  1960. return -1;
  1961. s = script_spec__new(spec, ops);
  1962. if (!s)
  1963. return -1;
  1964. else
  1965. script_spec__add(s);
  1966. return 0;
  1967. }
  1968. static struct scripting_ops *script_spec__lookup(const char *spec)
  1969. {
  1970. struct script_spec *s = script_spec__find(spec);
  1971. if (!s)
  1972. return NULL;
  1973. return s->ops;
  1974. }
  1975. static void list_available_languages(void)
  1976. {
  1977. struct script_spec *s;
  1978. fprintf(stderr, "\n");
  1979. fprintf(stderr, "Scripting language extensions (used in "
  1980. "perf script -s [spec:]script.[spec]):\n\n");
  1981. list_for_each_entry(s, &script_specs, node)
  1982. fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
  1983. fprintf(stderr, "\n");
  1984. }
  1985. static int parse_scriptname(const struct option *opt __maybe_unused,
  1986. const char *str, int unset __maybe_unused)
  1987. {
  1988. char spec[PATH_MAX];
  1989. const char *script, *ext;
  1990. int len;
  1991. if (strcmp(str, "lang") == 0) {
  1992. list_available_languages();
  1993. exit(0);
  1994. }
  1995. script = strchr(str, ':');
  1996. if (script) {
  1997. len = script - str;
  1998. if (len >= PATH_MAX) {
  1999. fprintf(stderr, "invalid language specifier");
  2000. return -1;
  2001. }
  2002. strncpy(spec, str, len);
  2003. spec[len] = '\0';
  2004. scripting_ops = script_spec__lookup(spec);
  2005. if (!scripting_ops) {
  2006. fprintf(stderr, "invalid language specifier");
  2007. return -1;
  2008. }
  2009. script++;
  2010. } else {
  2011. script = str;
  2012. ext = strrchr(script, '.');
  2013. if (!ext) {
  2014. fprintf(stderr, "invalid script extension");
  2015. return -1;
  2016. }
  2017. scripting_ops = script_spec__lookup(++ext);
  2018. if (!scripting_ops) {
  2019. fprintf(stderr, "invalid script extension");
  2020. return -1;
  2021. }
  2022. }
  2023. script_name = strdup(script);
  2024. return 0;
  2025. }
  2026. static int parse_output_fields(const struct option *opt __maybe_unused,
  2027. const char *arg, int unset __maybe_unused)
  2028. {
  2029. char *tok, *strtok_saveptr = NULL;
  2030. int i, imax = ARRAY_SIZE(all_output_options);
  2031. int j;
  2032. int rc = 0;
  2033. char *str = strdup(arg);
  2034. int type = -1;
  2035. enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
  2036. if (!str)
  2037. return -ENOMEM;
  2038. /* first word can state for which event type the user is specifying
  2039. * the fields. If no type exists, the specified fields apply to all
  2040. * event types found in the file minus the invalid fields for a type.
  2041. */
  2042. tok = strchr(str, ':');
  2043. if (tok) {
  2044. *tok = '\0';
  2045. tok++;
  2046. if (!strcmp(str, "hw"))
  2047. type = PERF_TYPE_HARDWARE;
  2048. else if (!strcmp(str, "sw"))
  2049. type = PERF_TYPE_SOFTWARE;
  2050. else if (!strcmp(str, "trace"))
  2051. type = PERF_TYPE_TRACEPOINT;
  2052. else if (!strcmp(str, "raw"))
  2053. type = PERF_TYPE_RAW;
  2054. else if (!strcmp(str, "break"))
  2055. type = PERF_TYPE_BREAKPOINT;
  2056. else if (!strcmp(str, "synth"))
  2057. type = OUTPUT_TYPE_SYNTH;
  2058. else {
  2059. fprintf(stderr, "Invalid event type in field string.\n");
  2060. rc = -EINVAL;
  2061. goto out;
  2062. }
  2063. if (output[type].user_set)
  2064. pr_warning("Overriding previous field request for %s events.\n",
  2065. event_type(type));
  2066. output[type].fields = 0;
  2067. output[type].user_set = true;
  2068. output[type].wildcard_set = false;
  2069. } else {
  2070. tok = str;
  2071. if (strlen(str) == 0) {
  2072. fprintf(stderr,
  2073. "Cannot set fields to 'none' for all event types.\n");
  2074. rc = -EINVAL;
  2075. goto out;
  2076. }
  2077. /* Don't override defaults for +- */
  2078. if (strchr(str, '+') || strchr(str, '-'))
  2079. goto parse;
  2080. if (output_set_by_user())
  2081. pr_warning("Overriding previous field request for all events.\n");
  2082. for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
  2083. output[j].fields = 0;
  2084. output[j].user_set = true;
  2085. output[j].wildcard_set = true;
  2086. }
  2087. }
  2088. parse:
  2089. for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
  2090. if (*tok == '+') {
  2091. if (change == SET)
  2092. goto out_badmix;
  2093. change = ADD;
  2094. tok++;
  2095. } else if (*tok == '-') {
  2096. if (change == SET)
  2097. goto out_badmix;
  2098. change = REMOVE;
  2099. tok++;
  2100. } else {
  2101. if (change != SET && change != DEFAULT)
  2102. goto out_badmix;
  2103. change = SET;
  2104. }
  2105. for (i = 0; i < imax; ++i) {
  2106. if (strcmp(tok, all_output_options[i].str) == 0)
  2107. break;
  2108. }
  2109. if (i == imax && strcmp(tok, "flags") == 0) {
  2110. print_flags = change == REMOVE ? false : true;
  2111. continue;
  2112. }
  2113. if (i == imax) {
  2114. fprintf(stderr, "Invalid field requested.\n");
  2115. rc = -EINVAL;
  2116. goto out;
  2117. }
  2118. if (type == -1) {
  2119. /* add user option to all events types for
  2120. * which it is valid
  2121. */
  2122. for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
  2123. if (output[j].invalid_fields & all_output_options[i].field) {
  2124. pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
  2125. all_output_options[i].str, event_type(j));
  2126. } else {
  2127. if (change == REMOVE)
  2128. output[j].fields &= ~all_output_options[i].field;
  2129. else
  2130. output[j].fields |= all_output_options[i].field;
  2131. output[j].user_set = true;
  2132. output[j].wildcard_set = true;
  2133. }
  2134. }
  2135. } else {
  2136. if (output[type].invalid_fields & all_output_options[i].field) {
  2137. fprintf(stderr, "\'%s\' not valid for %s events.\n",
  2138. all_output_options[i].str, event_type(type));
  2139. rc = -EINVAL;
  2140. goto out;
  2141. }
  2142. output[type].user_set = true;
  2143. output[type].wildcard_set = true;
  2144. }
  2145. }
  2146. if (type >= 0) {
  2147. if (output[type].fields == 0) {
  2148. pr_debug("No fields requested for %s type. "
  2149. "Events will not be displayed.\n", event_type(type));
  2150. }
  2151. }
  2152. goto out;
  2153. out_badmix:
  2154. fprintf(stderr, "Cannot mix +-field with overridden fields\n");
  2155. rc = -EINVAL;
  2156. out:
  2157. free(str);
  2158. return rc;
  2159. }
  2160. #define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
  2161. while ((lang_dirent = readdir(scripts_dir)) != NULL) \
  2162. if ((lang_dirent->d_type == DT_DIR || \
  2163. (lang_dirent->d_type == DT_UNKNOWN && \
  2164. is_directory(scripts_path, lang_dirent))) && \
  2165. (strcmp(lang_dirent->d_name, ".")) && \
  2166. (strcmp(lang_dirent->d_name, "..")))
  2167. #define for_each_script(lang_path, lang_dir, script_dirent) \
  2168. while ((script_dirent = readdir(lang_dir)) != NULL) \
  2169. if (script_dirent->d_type != DT_DIR && \
  2170. (script_dirent->d_type != DT_UNKNOWN || \
  2171. !is_directory(lang_path, script_dirent)))
  2172. #define RECORD_SUFFIX "-record"
  2173. #define REPORT_SUFFIX "-report"
  2174. struct script_desc {
  2175. struct list_head node;
  2176. char *name;
  2177. char *half_liner;
  2178. char *args;
  2179. };
  2180. static LIST_HEAD(script_descs);
  2181. static struct script_desc *script_desc__new(const char *name)
  2182. {
  2183. struct script_desc *s = zalloc(sizeof(*s));
  2184. if (s != NULL && name)
  2185. s->name = strdup(name);
  2186. return s;
  2187. }
  2188. static void script_desc__delete(struct script_desc *s)
  2189. {
  2190. zfree(&s->name);
  2191. zfree(&s->half_liner);
  2192. zfree(&s->args);
  2193. free(s);
  2194. }
  2195. static void script_desc__add(struct script_desc *s)
  2196. {
  2197. list_add_tail(&s->node, &script_descs);
  2198. }
  2199. static struct script_desc *script_desc__find(const char *name)
  2200. {
  2201. struct script_desc *s;
  2202. list_for_each_entry(s, &script_descs, node)
  2203. if (strcasecmp(s->name, name) == 0)
  2204. return s;
  2205. return NULL;
  2206. }
  2207. static struct script_desc *script_desc__findnew(const char *name)
  2208. {
  2209. struct script_desc *s = script_desc__find(name);
  2210. if (s)
  2211. return s;
  2212. s = script_desc__new(name);
  2213. if (!s)
  2214. return NULL;
  2215. script_desc__add(s);
  2216. return s;
  2217. }
  2218. static const char *ends_with(const char *str, const char *suffix)
  2219. {
  2220. size_t suffix_len = strlen(suffix);
  2221. const char *p = str;
  2222. if (strlen(str) > suffix_len) {
  2223. p = str + strlen(str) - suffix_len;
  2224. if (!strncmp(p, suffix, suffix_len))
  2225. return p;
  2226. }
  2227. return NULL;
  2228. }
  2229. static int read_script_info(struct script_desc *desc, const char *filename)
  2230. {
  2231. char line[BUFSIZ], *p;
  2232. FILE *fp;
  2233. fp = fopen(filename, "r");
  2234. if (!fp)
  2235. return -1;
  2236. while (fgets(line, sizeof(line), fp)) {
  2237. p = ltrim(line);
  2238. if (strlen(p) == 0)
  2239. continue;
  2240. if (*p != '#')
  2241. continue;
  2242. p++;
  2243. if (strlen(p) && *p == '!')
  2244. continue;
  2245. p = ltrim(p);
  2246. if (strlen(p) && p[strlen(p) - 1] == '\n')
  2247. p[strlen(p) - 1] = '\0';
  2248. if (!strncmp(p, "description:", strlen("description:"))) {
  2249. p += strlen("description:");
  2250. desc->half_liner = strdup(ltrim(p));
  2251. continue;
  2252. }
  2253. if (!strncmp(p, "args:", strlen("args:"))) {
  2254. p += strlen("args:");
  2255. desc->args = strdup(ltrim(p));
  2256. continue;
  2257. }
  2258. }
  2259. fclose(fp);
  2260. return 0;
  2261. }
  2262. static char *get_script_root(struct dirent *script_dirent, const char *suffix)
  2263. {
  2264. char *script_root, *str;
  2265. script_root = strdup(script_dirent->d_name);
  2266. if (!script_root)
  2267. return NULL;
  2268. str = (char *)ends_with(script_root, suffix);
  2269. if (!str) {
  2270. free(script_root);
  2271. return NULL;
  2272. }
  2273. *str = '\0';
  2274. return script_root;
  2275. }
  2276. static int list_available_scripts(const struct option *opt __maybe_unused,
  2277. const char *s __maybe_unused,
  2278. int unset __maybe_unused)
  2279. {
  2280. struct dirent *script_dirent, *lang_dirent;
  2281. char scripts_path[MAXPATHLEN];
  2282. DIR *scripts_dir, *lang_dir;
  2283. char script_path[MAXPATHLEN];
  2284. char lang_path[MAXPATHLEN];
  2285. struct script_desc *desc;
  2286. char first_half[BUFSIZ];
  2287. char *script_root;
  2288. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
  2289. scripts_dir = opendir(scripts_path);
  2290. if (!scripts_dir) {
  2291. fprintf(stdout,
  2292. "open(%s) failed.\n"
  2293. "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
  2294. scripts_path);
  2295. exit(-1);
  2296. }
  2297. for_each_lang(scripts_path, scripts_dir, lang_dirent) {
  2298. scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  2299. lang_dirent->d_name);
  2300. lang_dir = opendir(lang_path);
  2301. if (!lang_dir)
  2302. continue;
  2303. for_each_script(lang_path, lang_dir, script_dirent) {
  2304. script_root = get_script_root(script_dirent, REPORT_SUFFIX);
  2305. if (script_root) {
  2306. desc = script_desc__findnew(script_root);
  2307. scnprintf(script_path, MAXPATHLEN, "%s/%s",
  2308. lang_path, script_dirent->d_name);
  2309. read_script_info(desc, script_path);
  2310. free(script_root);
  2311. }
  2312. }
  2313. }
  2314. fprintf(stdout, "List of available trace scripts:\n");
  2315. list_for_each_entry(desc, &script_descs, node) {
  2316. sprintf(first_half, "%s %s", desc->name,
  2317. desc->args ? desc->args : "");
  2318. fprintf(stdout, " %-36s %s\n", first_half,
  2319. desc->half_liner ? desc->half_liner : "");
  2320. }
  2321. exit(0);
  2322. }
  2323. /*
  2324. * Some scripts specify the required events in their "xxx-record" file,
  2325. * this function will check if the events in perf.data match those
  2326. * mentioned in the "xxx-record".
  2327. *
  2328. * Fixme: All existing "xxx-record" are all in good formats "-e event ",
  2329. * which is covered well now. And new parsing code should be added to
  2330. * cover the future complexing formats like event groups etc.
  2331. */
  2332. static int check_ev_match(char *dir_name, char *scriptname,
  2333. struct perf_session *session)
  2334. {
  2335. char filename[MAXPATHLEN], evname[128];
  2336. char line[BUFSIZ], *p;
  2337. struct perf_evsel *pos;
  2338. int match, len;
  2339. FILE *fp;
  2340. scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
  2341. fp = fopen(filename, "r");
  2342. if (!fp)
  2343. return -1;
  2344. while (fgets(line, sizeof(line), fp)) {
  2345. p = ltrim(line);
  2346. if (*p == '#')
  2347. continue;
  2348. while (strlen(p)) {
  2349. p = strstr(p, "-e");
  2350. if (!p)
  2351. break;
  2352. p += 2;
  2353. p = ltrim(p);
  2354. len = strcspn(p, " \t");
  2355. if (!len)
  2356. break;
  2357. snprintf(evname, len + 1, "%s", p);
  2358. match = 0;
  2359. evlist__for_each_entry(session->evlist, pos) {
  2360. if (!strcmp(perf_evsel__name(pos), evname)) {
  2361. match = 1;
  2362. break;
  2363. }
  2364. }
  2365. if (!match) {
  2366. fclose(fp);
  2367. return -1;
  2368. }
  2369. }
  2370. }
  2371. fclose(fp);
  2372. return 0;
  2373. }
  2374. /*
  2375. * Return -1 if none is found, otherwise the actual scripts number.
  2376. *
  2377. * Currently the only user of this function is the script browser, which
  2378. * will list all statically runnable scripts, select one, execute it and
  2379. * show the output in a perf browser.
  2380. */
  2381. int find_scripts(char **scripts_array, char **scripts_path_array)
  2382. {
  2383. struct dirent *script_dirent, *lang_dirent;
  2384. char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
  2385. DIR *scripts_dir, *lang_dir;
  2386. struct perf_session *session;
  2387. struct perf_data data = {
  2388. .file = {
  2389. .path = input_name,
  2390. },
  2391. .mode = PERF_DATA_MODE_READ,
  2392. };
  2393. char *temp;
  2394. int i = 0;
  2395. session = perf_session__new(&data, false, NULL);
  2396. if (!session)
  2397. return -1;
  2398. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
  2399. scripts_dir = opendir(scripts_path);
  2400. if (!scripts_dir) {
  2401. perf_session__delete(session);
  2402. return -1;
  2403. }
  2404. for_each_lang(scripts_path, scripts_dir, lang_dirent) {
  2405. scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
  2406. lang_dirent->d_name);
  2407. #ifndef HAVE_LIBPERL_SUPPORT
  2408. if (strstr(lang_path, "perl"))
  2409. continue;
  2410. #endif
  2411. #ifndef HAVE_LIBPYTHON_SUPPORT
  2412. if (strstr(lang_path, "python"))
  2413. continue;
  2414. #endif
  2415. lang_dir = opendir(lang_path);
  2416. if (!lang_dir)
  2417. continue;
  2418. for_each_script(lang_path, lang_dir, script_dirent) {
  2419. /* Skip those real time scripts: xxxtop.p[yl] */
  2420. if (strstr(script_dirent->d_name, "top."))
  2421. continue;
  2422. sprintf(scripts_path_array[i], "%s/%s", lang_path,
  2423. script_dirent->d_name);
  2424. temp = strchr(script_dirent->d_name, '.');
  2425. snprintf(scripts_array[i],
  2426. (temp - script_dirent->d_name) + 1,
  2427. "%s", script_dirent->d_name);
  2428. if (check_ev_match(lang_path,
  2429. scripts_array[i], session))
  2430. continue;
  2431. i++;
  2432. }
  2433. closedir(lang_dir);
  2434. }
  2435. closedir(scripts_dir);
  2436. perf_session__delete(session);
  2437. return i;
  2438. }
  2439. static char *get_script_path(const char *script_root, const char *suffix)
  2440. {
  2441. struct dirent *script_dirent, *lang_dirent;
  2442. char scripts_path[MAXPATHLEN];
  2443. char script_path[MAXPATHLEN];
  2444. DIR *scripts_dir, *lang_dir;
  2445. char lang_path[MAXPATHLEN];
  2446. char *__script_root;
  2447. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
  2448. scripts_dir = opendir(scripts_path);
  2449. if (!scripts_dir)
  2450. return NULL;
  2451. for_each_lang(scripts_path, scripts_dir, lang_dirent) {
  2452. scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  2453. lang_dirent->d_name);
  2454. lang_dir = opendir(lang_path);
  2455. if (!lang_dir)
  2456. continue;
  2457. for_each_script(lang_path, lang_dir, script_dirent) {
  2458. __script_root = get_script_root(script_dirent, suffix);
  2459. if (__script_root && !strcmp(script_root, __script_root)) {
  2460. free(__script_root);
  2461. closedir(lang_dir);
  2462. closedir(scripts_dir);
  2463. scnprintf(script_path, MAXPATHLEN, "%s/%s",
  2464. lang_path, script_dirent->d_name);
  2465. return strdup(script_path);
  2466. }
  2467. free(__script_root);
  2468. }
  2469. closedir(lang_dir);
  2470. }
  2471. closedir(scripts_dir);
  2472. return NULL;
  2473. }
  2474. static bool is_top_script(const char *script_path)
  2475. {
  2476. return ends_with(script_path, "top") == NULL ? false : true;
  2477. }
  2478. static int has_required_arg(char *script_path)
  2479. {
  2480. struct script_desc *desc;
  2481. int n_args = 0;
  2482. char *p;
  2483. desc = script_desc__new(NULL);
  2484. if (read_script_info(desc, script_path))
  2485. goto out;
  2486. if (!desc->args)
  2487. goto out;
  2488. for (p = desc->args; *p; p++)
  2489. if (*p == '<')
  2490. n_args++;
  2491. out:
  2492. script_desc__delete(desc);
  2493. return n_args;
  2494. }
  2495. static int have_cmd(int argc, const char **argv)
  2496. {
  2497. char **__argv = malloc(sizeof(const char *) * argc);
  2498. if (!__argv) {
  2499. pr_err("malloc failed\n");
  2500. return -1;
  2501. }
  2502. memcpy(__argv, argv, sizeof(const char *) * argc);
  2503. argc = parse_options(argc, (const char **)__argv, record_options,
  2504. NULL, PARSE_OPT_STOP_AT_NON_OPTION);
  2505. free(__argv);
  2506. system_wide = (argc == 0);
  2507. return 0;
  2508. }
  2509. static void script__setup_sample_type(struct perf_script *script)
  2510. {
  2511. struct perf_session *session = script->session;
  2512. u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
  2513. if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
  2514. if ((sample_type & PERF_SAMPLE_REGS_USER) &&
  2515. (sample_type & PERF_SAMPLE_STACK_USER)) {
  2516. callchain_param.record_mode = CALLCHAIN_DWARF;
  2517. dwarf_callchain_users = true;
  2518. } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
  2519. callchain_param.record_mode = CALLCHAIN_LBR;
  2520. else
  2521. callchain_param.record_mode = CALLCHAIN_FP;
  2522. }
  2523. }
  2524. static int process_stat_round_event(struct perf_session *session,
  2525. union perf_event *event)
  2526. {
  2527. struct stat_round_event *round = &event->stat_round;
  2528. struct perf_evsel *counter;
  2529. evlist__for_each_entry(session->evlist, counter) {
  2530. perf_stat_process_counter(&stat_config, counter);
  2531. process_stat(counter, round->time);
  2532. }
  2533. process_stat_interval(round->time);
  2534. return 0;
  2535. }
  2536. static int process_stat_config_event(struct perf_session *session __maybe_unused,
  2537. union perf_event *event)
  2538. {
  2539. perf_event__read_stat_config(&stat_config, &event->stat_config);
  2540. return 0;
  2541. }
  2542. static int set_maps(struct perf_script *script)
  2543. {
  2544. struct perf_evlist *evlist = script->session->evlist;
  2545. if (!script->cpus || !script->threads)
  2546. return 0;
  2547. if (WARN_ONCE(script->allocated, "stats double allocation\n"))
  2548. return -EINVAL;
  2549. perf_evlist__set_maps(evlist, script->cpus, script->threads);
  2550. if (perf_evlist__alloc_stats(evlist, true))
  2551. return -ENOMEM;
  2552. script->allocated = true;
  2553. return 0;
  2554. }
  2555. static
  2556. int process_thread_map_event(struct perf_session *session,
  2557. union perf_event *event)
  2558. {
  2559. struct perf_tool *tool = session->tool;
  2560. struct perf_script *script = container_of(tool, struct perf_script, tool);
  2561. if (script->threads) {
  2562. pr_warning("Extra thread map event, ignoring.\n");
  2563. return 0;
  2564. }
  2565. script->threads = thread_map__new_event(&event->thread_map);
  2566. if (!script->threads)
  2567. return -ENOMEM;
  2568. return set_maps(script);
  2569. }
  2570. static
  2571. int process_cpu_map_event(struct perf_session *session,
  2572. union perf_event *event)
  2573. {
  2574. struct perf_tool *tool = session->tool;
  2575. struct perf_script *script = container_of(tool, struct perf_script, tool);
  2576. if (script->cpus) {
  2577. pr_warning("Extra cpu map event, ignoring.\n");
  2578. return 0;
  2579. }
  2580. script->cpus = cpu_map__new_data(&event->cpu_map.data);
  2581. if (!script->cpus)
  2582. return -ENOMEM;
  2583. return set_maps(script);
  2584. }
  2585. static int process_feature_event(struct perf_session *session,
  2586. union perf_event *event)
  2587. {
  2588. if (event->feat.feat_id < HEADER_LAST_FEATURE)
  2589. return perf_event__process_feature(session, event);
  2590. return 0;
  2591. }
  2592. #ifdef HAVE_AUXTRACE_SUPPORT
  2593. static int perf_script__process_auxtrace_info(struct perf_session *session,
  2594. union perf_event *event)
  2595. {
  2596. struct perf_tool *tool = session->tool;
  2597. int ret = perf_event__process_auxtrace_info(session, event);
  2598. if (ret == 0) {
  2599. struct perf_script *script = container_of(tool, struct perf_script, tool);
  2600. ret = perf_script__setup_per_event_dump(script);
  2601. }
  2602. return ret;
  2603. }
  2604. #else
  2605. #define perf_script__process_auxtrace_info 0
  2606. #endif
  2607. static int parse_insn_trace(const struct option *opt __maybe_unused,
  2608. const char *str __maybe_unused,
  2609. int unset __maybe_unused)
  2610. {
  2611. parse_output_fields(NULL, "+insn,-event,-period", 0);
  2612. itrace_parse_synth_opts(opt, "i0ns", 0);
  2613. nanosecs = true;
  2614. return 0;
  2615. }
  2616. static int parse_xed(const struct option *opt __maybe_unused,
  2617. const char *str __maybe_unused,
  2618. int unset __maybe_unused)
  2619. {
  2620. force_pager("xed -F insn: -A -64 | less");
  2621. return 0;
  2622. }
  2623. int cmd_script(int argc, const char **argv)
  2624. {
  2625. bool show_full_info = false;
  2626. bool header = false;
  2627. bool header_only = false;
  2628. bool script_started = false;
  2629. char *rec_script_path = NULL;
  2630. char *rep_script_path = NULL;
  2631. struct perf_session *session;
  2632. struct itrace_synth_opts itrace_synth_opts = { .set = false, };
  2633. char *script_path = NULL;
  2634. const char **__argv;
  2635. int i, j, err = 0;
  2636. struct perf_script script = {
  2637. .tool = {
  2638. .sample = process_sample_event,
  2639. .mmap = perf_event__process_mmap,
  2640. .mmap2 = perf_event__process_mmap2,
  2641. .comm = perf_event__process_comm,
  2642. .namespaces = perf_event__process_namespaces,
  2643. .exit = perf_event__process_exit,
  2644. .fork = perf_event__process_fork,
  2645. .attr = process_attr,
  2646. .event_update = perf_event__process_event_update,
  2647. .tracing_data = perf_event__process_tracing_data,
  2648. .feature = process_feature_event,
  2649. .build_id = perf_event__process_build_id,
  2650. .id_index = perf_event__process_id_index,
  2651. .auxtrace_info = perf_script__process_auxtrace_info,
  2652. .auxtrace = perf_event__process_auxtrace,
  2653. .auxtrace_error = perf_event__process_auxtrace_error,
  2654. .stat = perf_event__process_stat_event,
  2655. .stat_round = process_stat_round_event,
  2656. .stat_config = process_stat_config_event,
  2657. .thread_map = process_thread_map_event,
  2658. .cpu_map = process_cpu_map_event,
  2659. .ordered_events = true,
  2660. .ordering_requires_timestamps = true,
  2661. },
  2662. };
  2663. struct perf_data data = {
  2664. .mode = PERF_DATA_MODE_READ,
  2665. };
  2666. const struct option options[] = {
  2667. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  2668. "dump raw trace in ASCII"),
  2669. OPT_INCR('v', "verbose", &verbose,
  2670. "be more verbose (show symbol address, etc)"),
  2671. OPT_BOOLEAN('L', "Latency", &latency_format,
  2672. "show latency attributes (irqs/preemption disabled, etc)"),
  2673. OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
  2674. list_available_scripts),
  2675. OPT_CALLBACK('s', "script", NULL, "name",
  2676. "script file name (lang:script name, script name, or *)",
  2677. parse_scriptname),
  2678. OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
  2679. "generate perf-script.xx script in specified language"),
  2680. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  2681. OPT_BOOLEAN('d', "debug-mode", &debug_mode,
  2682. "do various checks like samples ordering and lost events"),
  2683. OPT_BOOLEAN(0, "header", &header, "Show data header."),
  2684. OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
  2685. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  2686. "file", "vmlinux pathname"),
  2687. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  2688. "file", "kallsyms pathname"),
  2689. OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
  2690. "When printing symbols do not display call chain"),
  2691. OPT_CALLBACK(0, "symfs", NULL, "directory",
  2692. "Look for files with symbols relative to this directory",
  2693. symbol__config_symfs),
  2694. OPT_CALLBACK('F', "fields", NULL, "str",
  2695. "comma separated output fields prepend with 'type:'. "
  2696. "+field to add and -field to remove."
  2697. "Valid types: hw,sw,trace,raw,synth. "
  2698. "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
  2699. "addr,symoff,srcline,period,iregs,uregs,brstack,"
  2700. "brstacksym,flags,bpf-output,brstackinsn,brstackoff,"
  2701. "callindent,insn,insnlen,synth,phys_addr,metric,misc",
  2702. parse_output_fields),
  2703. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  2704. "system-wide collection from all CPUs"),
  2705. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  2706. "only consider these symbols"),
  2707. OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
  2708. "Decode instructions from itrace", parse_insn_trace),
  2709. OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
  2710. "Run xed disassembler on output", parse_xed),
  2711. OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
  2712. "Stop display of callgraph at these symbols"),
  2713. OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
  2714. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  2715. "only display events for these comms"),
  2716. OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
  2717. "only consider symbols in these pids"),
  2718. OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
  2719. "only consider symbols in these tids"),
  2720. OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
  2721. "Set the maximum stack depth when parsing the callchain, "
  2722. "anything beyond the specified depth will be ignored. "
  2723. "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
  2724. OPT_BOOLEAN('I', "show-info", &show_full_info,
  2725. "display extended information from perf.data file"),
  2726. OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
  2727. "Show the path of [kernel.kallsyms]"),
  2728. OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
  2729. "Show the fork/comm/exit events"),
  2730. OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
  2731. "Show the mmap events"),
  2732. OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
  2733. "Show context switch events (if recorded)"),
  2734. OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
  2735. "Show namespace events (if recorded)"),
  2736. OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
  2737. "Show lost events (if recorded)"),
  2738. OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
  2739. "Show round events (if recorded)"),
  2740. OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
  2741. "Dump trace output to files named by the monitored events"),
  2742. OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
  2743. OPT_INTEGER(0, "max-blocks", &max_blocks,
  2744. "Maximum number of code blocks to dump with brstackinsn"),
  2745. OPT_BOOLEAN(0, "ns", &nanosecs,
  2746. "Use 9 decimal places when displaying time"),
  2747. OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
  2748. "Instruction Tracing options\n" ITRACE_HELP,
  2749. itrace_parse_synth_opts),
  2750. OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
  2751. "Show full source file name path for source lines"),
  2752. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  2753. "Enable symbol demangling"),
  2754. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  2755. "Enable kernel symbol demangling"),
  2756. OPT_STRING(0, "time", &script.time_str, "str",
  2757. "Time span of interest (start,stop)"),
  2758. OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
  2759. "Show inline function"),
  2760. OPT_END()
  2761. };
  2762. const char * const script_subcommands[] = { "record", "report", NULL };
  2763. const char *script_usage[] = {
  2764. "perf script [<options>]",
  2765. "perf script [<options>] record <script> [<record-options>] <command>",
  2766. "perf script [<options>] report <script> [script-args]",
  2767. "perf script [<options>] <script> [<record-options>] <command>",
  2768. "perf script [<options>] <top-script> [script-args]",
  2769. NULL
  2770. };
  2771. perf_set_singlethreaded();
  2772. setup_scripting();
  2773. argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
  2774. PARSE_OPT_STOP_AT_NON_OPTION);
  2775. data.file.path = input_name;
  2776. data.force = symbol_conf.force;
  2777. if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
  2778. rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
  2779. if (!rec_script_path)
  2780. return cmd_record(argc, argv);
  2781. }
  2782. if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
  2783. rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
  2784. if (!rep_script_path) {
  2785. fprintf(stderr,
  2786. "Please specify a valid report script"
  2787. "(see 'perf script -l' for listing)\n");
  2788. return -1;
  2789. }
  2790. }
  2791. if (itrace_synth_opts.callchain &&
  2792. itrace_synth_opts.callchain_sz > scripting_max_stack)
  2793. scripting_max_stack = itrace_synth_opts.callchain_sz;
  2794. /* make sure PERF_EXEC_PATH is set for scripts */
  2795. set_argv_exec_path(get_argv_exec_path());
  2796. if (argc && !script_name && !rec_script_path && !rep_script_path) {
  2797. int live_pipe[2];
  2798. int rep_args;
  2799. pid_t pid;
  2800. rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
  2801. rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
  2802. if (!rec_script_path && !rep_script_path) {
  2803. usage_with_options_msg(script_usage, options,
  2804. "Couldn't find script `%s'\n\n See perf"
  2805. " script -l for available scripts.\n", argv[0]);
  2806. }
  2807. if (is_top_script(argv[0])) {
  2808. rep_args = argc - 1;
  2809. } else {
  2810. int rec_args;
  2811. rep_args = has_required_arg(rep_script_path);
  2812. rec_args = (argc - 1) - rep_args;
  2813. if (rec_args < 0) {
  2814. usage_with_options_msg(script_usage, options,
  2815. "`%s' script requires options."
  2816. "\n\n See perf script -l for available "
  2817. "scripts and options.\n", argv[0]);
  2818. }
  2819. }
  2820. if (pipe(live_pipe) < 0) {
  2821. perror("failed to create pipe");
  2822. return -1;
  2823. }
  2824. pid = fork();
  2825. if (pid < 0) {
  2826. perror("failed to fork");
  2827. return -1;
  2828. }
  2829. if (!pid) {
  2830. j = 0;
  2831. dup2(live_pipe[1], 1);
  2832. close(live_pipe[0]);
  2833. if (is_top_script(argv[0])) {
  2834. system_wide = true;
  2835. } else if (!system_wide) {
  2836. if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
  2837. err = -1;
  2838. goto out;
  2839. }
  2840. }
  2841. __argv = malloc((argc + 6) * sizeof(const char *));
  2842. if (!__argv) {
  2843. pr_err("malloc failed\n");
  2844. err = -ENOMEM;
  2845. goto out;
  2846. }
  2847. __argv[j++] = "/bin/sh";
  2848. __argv[j++] = rec_script_path;
  2849. if (system_wide)
  2850. __argv[j++] = "-a";
  2851. __argv[j++] = "-q";
  2852. __argv[j++] = "-o";
  2853. __argv[j++] = "-";
  2854. for (i = rep_args + 1; i < argc; i++)
  2855. __argv[j++] = argv[i];
  2856. __argv[j++] = NULL;
  2857. execvp("/bin/sh", (char **)__argv);
  2858. free(__argv);
  2859. exit(-1);
  2860. }
  2861. dup2(live_pipe[0], 0);
  2862. close(live_pipe[1]);
  2863. __argv = malloc((argc + 4) * sizeof(const char *));
  2864. if (!__argv) {
  2865. pr_err("malloc failed\n");
  2866. err = -ENOMEM;
  2867. goto out;
  2868. }
  2869. j = 0;
  2870. __argv[j++] = "/bin/sh";
  2871. __argv[j++] = rep_script_path;
  2872. for (i = 1; i < rep_args + 1; i++)
  2873. __argv[j++] = argv[i];
  2874. __argv[j++] = "-i";
  2875. __argv[j++] = "-";
  2876. __argv[j++] = NULL;
  2877. execvp("/bin/sh", (char **)__argv);
  2878. free(__argv);
  2879. exit(-1);
  2880. }
  2881. if (rec_script_path)
  2882. script_path = rec_script_path;
  2883. if (rep_script_path)
  2884. script_path = rep_script_path;
  2885. if (script_path) {
  2886. j = 0;
  2887. if (!rec_script_path)
  2888. system_wide = false;
  2889. else if (!system_wide) {
  2890. if (have_cmd(argc - 1, &argv[1]) != 0) {
  2891. err = -1;
  2892. goto out;
  2893. }
  2894. }
  2895. __argv = malloc((argc + 2) * sizeof(const char *));
  2896. if (!__argv) {
  2897. pr_err("malloc failed\n");
  2898. err = -ENOMEM;
  2899. goto out;
  2900. }
  2901. __argv[j++] = "/bin/sh";
  2902. __argv[j++] = script_path;
  2903. if (system_wide)
  2904. __argv[j++] = "-a";
  2905. for (i = 2; i < argc; i++)
  2906. __argv[j++] = argv[i];
  2907. __argv[j++] = NULL;
  2908. execvp("/bin/sh", (char **)__argv);
  2909. free(__argv);
  2910. exit(-1);
  2911. }
  2912. if (!script_name) {
  2913. setup_pager();
  2914. use_browser = 0;
  2915. }
  2916. session = perf_session__new(&data, false, &script.tool);
  2917. if (session == NULL)
  2918. return -1;
  2919. if (header || header_only) {
  2920. script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
  2921. perf_session__fprintf_info(session, stdout, show_full_info);
  2922. if (header_only)
  2923. goto out_delete;
  2924. }
  2925. if (show_full_info)
  2926. script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
  2927. if (symbol__init(&session->header.env) < 0)
  2928. goto out_delete;
  2929. script.session = session;
  2930. script__setup_sample_type(&script);
  2931. if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
  2932. itrace_synth_opts.thread_stack = true;
  2933. session->itrace_synth_opts = &itrace_synth_opts;
  2934. if (cpu_list) {
  2935. err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
  2936. if (err < 0)
  2937. goto out_delete;
  2938. itrace_synth_opts.cpu_bitmap = cpu_bitmap;
  2939. }
  2940. if (!no_callchain)
  2941. symbol_conf.use_callchain = true;
  2942. else
  2943. symbol_conf.use_callchain = false;
  2944. if (session->tevent.pevent &&
  2945. tep_set_function_resolver(session->tevent.pevent,
  2946. machine__resolve_kernel_addr,
  2947. &session->machines.host) < 0) {
  2948. pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
  2949. err = -1;
  2950. goto out_delete;
  2951. }
  2952. if (generate_script_lang) {
  2953. struct stat perf_stat;
  2954. int input;
  2955. if (output_set_by_user()) {
  2956. fprintf(stderr,
  2957. "custom fields not supported for generated scripts");
  2958. err = -EINVAL;
  2959. goto out_delete;
  2960. }
  2961. input = open(data.file.path, O_RDONLY); /* input_name */
  2962. if (input < 0) {
  2963. err = -errno;
  2964. perror("failed to open file");
  2965. goto out_delete;
  2966. }
  2967. err = fstat(input, &perf_stat);
  2968. if (err < 0) {
  2969. perror("failed to stat file");
  2970. goto out_delete;
  2971. }
  2972. if (!perf_stat.st_size) {
  2973. fprintf(stderr, "zero-sized file, nothing to do!\n");
  2974. goto out_delete;
  2975. }
  2976. scripting_ops = script_spec__lookup(generate_script_lang);
  2977. if (!scripting_ops) {
  2978. fprintf(stderr, "invalid language specifier");
  2979. err = -ENOENT;
  2980. goto out_delete;
  2981. }
  2982. err = scripting_ops->generate_script(session->tevent.pevent,
  2983. "perf-script");
  2984. goto out_delete;
  2985. }
  2986. if (script_name) {
  2987. err = scripting_ops->start_script(script_name, argc, argv);
  2988. if (err)
  2989. goto out_delete;
  2990. pr_debug("perf script started with script %s\n\n", script_name);
  2991. script_started = true;
  2992. }
  2993. err = perf_session__check_output_opt(session);
  2994. if (err < 0)
  2995. goto out_delete;
  2996. script.ptime_range = perf_time__range_alloc(script.time_str,
  2997. &script.range_size);
  2998. if (!script.ptime_range) {
  2999. err = -ENOMEM;
  3000. goto out_delete;
  3001. }
  3002. /* needs to be parsed after looking up reference time */
  3003. if (perf_time__parse_str(script.ptime_range, script.time_str) != 0) {
  3004. if (session->evlist->first_sample_time == 0 &&
  3005. session->evlist->last_sample_time == 0) {
  3006. pr_err("HINT: no first/last sample time found in perf data.\n"
  3007. "Please use latest perf binary to execute 'perf record'\n"
  3008. "(if '--buildid-all' is enabled, please set '--timestamp-boundary').\n");
  3009. err = -EINVAL;
  3010. goto out_delete;
  3011. }
  3012. script.range_num = perf_time__percent_parse_str(
  3013. script.ptime_range, script.range_size,
  3014. script.time_str,
  3015. session->evlist->first_sample_time,
  3016. session->evlist->last_sample_time);
  3017. if (script.range_num < 0) {
  3018. pr_err("Invalid time string\n");
  3019. err = -EINVAL;
  3020. goto out_delete;
  3021. }
  3022. } else {
  3023. script.range_num = 1;
  3024. }
  3025. err = __cmd_script(&script);
  3026. flush_scripting();
  3027. out_delete:
  3028. zfree(&script.ptime_range);
  3029. perf_evlist__free_stats(session->evlist);
  3030. perf_session__delete(session);
  3031. if (script_started)
  3032. cleanup_scripting();
  3033. out:
  3034. return err;
  3035. }