builtin-script.c 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "builtin.h"
  3. #include "util/counts.h"
  4. #include "util/debug.h"
  5. #include "util/dso.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/map.h"
  13. #include "util/srcline.h"
  14. #include "util/symbol.h"
  15. #include "util/thread.h"
  16. #include "util/trace-event.h"
  17. #include "util/env.h"
  18. #include "util/evlist.h"
  19. #include "util/evsel.h"
  20. #include "util/evsel_fprintf.h"
  21. #include "util/evswitch.h"
  22. #include "util/sort.h"
  23. #include "util/data.h"
  24. #include "util/auxtrace.h"
  25. #include "util/cpumap.h"
  26. #include "util/thread_map.h"
  27. #include "util/stat.h"
  28. #include "util/color.h"
  29. #include "util/string2.h"
  30. #include "util/thread-stack.h"
  31. #include "util/time-utils.h"
  32. #include "util/path.h"
  33. #include "util/event.h"
  34. #include "util/mem-info.h"
  35. #include "util/metricgroup.h"
  36. #include "ui/ui.h"
  37. #include "print_binary.h"
  38. #include "print_insn.h"
  39. #include <linux/bitmap.h>
  40. #include <linux/compiler.h>
  41. #include <linux/kernel.h>
  42. #include <linux/stringify.h>
  43. #include <linux/time64.h>
  44. #include <linux/zalloc.h>
  45. #include <linux/unaligned.h>
  46. #include <sys/utsname.h>
  47. #include "asm/bug.h"
  48. #include "util/mem-events.h"
  49. #include "util/dump-insn.h"
  50. #include <dirent.h>
  51. #include <errno.h>
  52. #include <inttypes.h>
  53. #include <signal.h>
  54. #include <stdio.h>
  55. #include <sys/param.h>
  56. #include <sys/types.h>
  57. #include <sys/stat.h>
  58. #include <fcntl.h>
  59. #include <unistd.h>
  60. #include <subcmd/pager.h>
  61. #include <perf/evlist.h>
  62. #include <linux/err.h>
  63. #include "util/dlfilter.h"
  64. #include "util/record.h"
  65. #include "util/util.h"
  66. #include "util/cgroup.h"
  67. #include "util/annotate.h"
  68. #include "perf.h"
  69. #include <linux/ctype.h>
  70. #ifdef HAVE_LIBTRACEEVENT
  71. #include <event-parse.h>
  72. #endif
  73. static char const *script_name;
  74. static char const *generate_script_lang;
  75. static bool reltime;
  76. static bool deltatime;
  77. static u64 initial_time;
  78. static u64 previous_time;
  79. static bool debug_mode;
  80. static u64 last_timestamp;
  81. static u64 nr_unordered;
  82. static bool no_callchain;
  83. static bool latency_format;
  84. static bool system_wide;
  85. static bool print_flags;
  86. static const char *cpu_list;
  87. static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
  88. static int max_blocks;
  89. static struct dlfilter *dlfilter;
  90. static int dlargc;
  91. static char **dlargv;
  92. enum perf_output_field {
  93. PERF_OUTPUT_COMM = 1ULL << 0,
  94. PERF_OUTPUT_TID = 1ULL << 1,
  95. PERF_OUTPUT_PID = 1ULL << 2,
  96. PERF_OUTPUT_TIME = 1ULL << 3,
  97. PERF_OUTPUT_CPU = 1ULL << 4,
  98. PERF_OUTPUT_EVNAME = 1ULL << 5,
  99. PERF_OUTPUT_TRACE = 1ULL << 6,
  100. PERF_OUTPUT_IP = 1ULL << 7,
  101. PERF_OUTPUT_SYM = 1ULL << 8,
  102. PERF_OUTPUT_DSO = 1ULL << 9,
  103. PERF_OUTPUT_ADDR = 1ULL << 10,
  104. PERF_OUTPUT_SYMOFFSET = 1ULL << 11,
  105. PERF_OUTPUT_SRCLINE = 1ULL << 12,
  106. PERF_OUTPUT_PERIOD = 1ULL << 13,
  107. PERF_OUTPUT_IREGS = 1ULL << 14,
  108. PERF_OUTPUT_BRSTACK = 1ULL << 15,
  109. PERF_OUTPUT_BRSTACKSYM = 1ULL << 16,
  110. PERF_OUTPUT_DATA_SRC = 1ULL << 17,
  111. PERF_OUTPUT_WEIGHT = 1ULL << 18,
  112. PERF_OUTPUT_BPF_OUTPUT = 1ULL << 19,
  113. PERF_OUTPUT_CALLINDENT = 1ULL << 20,
  114. PERF_OUTPUT_INSN = 1ULL << 21,
  115. PERF_OUTPUT_INSNLEN = 1ULL << 22,
  116. PERF_OUTPUT_BRSTACKINSN = 1ULL << 23,
  117. PERF_OUTPUT_BRSTACKOFF = 1ULL << 24,
  118. PERF_OUTPUT_SYNTH = 1ULL << 25,
  119. PERF_OUTPUT_PHYS_ADDR = 1ULL << 26,
  120. PERF_OUTPUT_UREGS = 1ULL << 27,
  121. PERF_OUTPUT_METRIC = 1ULL << 28,
  122. PERF_OUTPUT_MISC = 1ULL << 29,
  123. PERF_OUTPUT_SRCCODE = 1ULL << 30,
  124. PERF_OUTPUT_IPC = 1ULL << 31,
  125. PERF_OUTPUT_TOD = 1ULL << 32,
  126. PERF_OUTPUT_DATA_PAGE_SIZE = 1ULL << 33,
  127. PERF_OUTPUT_CODE_PAGE_SIZE = 1ULL << 34,
  128. PERF_OUTPUT_INS_LAT = 1ULL << 35,
  129. PERF_OUTPUT_BRSTACKINSNLEN = 1ULL << 36,
  130. PERF_OUTPUT_MACHINE_PID = 1ULL << 37,
  131. PERF_OUTPUT_VCPU = 1ULL << 38,
  132. PERF_OUTPUT_CGROUP = 1ULL << 39,
  133. PERF_OUTPUT_RETIRE_LAT = 1ULL << 40,
  134. PERF_OUTPUT_DSOFF = 1ULL << 41,
  135. PERF_OUTPUT_DISASM = 1ULL << 42,
  136. PERF_OUTPUT_BRSTACKDISASM = 1ULL << 43,
  137. PERF_OUTPUT_BRCNTR = 1ULL << 44,
  138. };
  139. struct perf_script {
  140. struct perf_tool tool;
  141. struct perf_session *session;
  142. bool show_task_events;
  143. bool show_mmap_events;
  144. bool show_switch_events;
  145. bool show_namespace_events;
  146. bool show_lost_events;
  147. bool show_round_events;
  148. bool show_bpf_events;
  149. bool show_cgroup_events;
  150. bool show_text_poke_events;
  151. bool allocated;
  152. bool per_event_dump;
  153. bool stitch_lbr;
  154. struct evswitch evswitch;
  155. struct perf_cpu_map *cpus;
  156. struct perf_thread_map *threads;
  157. int name_width;
  158. const char *time_str;
  159. struct perf_time_interval *ptime_range;
  160. int range_size;
  161. int range_num;
  162. };
  163. struct output_option {
  164. const char *str;
  165. enum perf_output_field field;
  166. } all_output_options[] = {
  167. {.str = "comm", .field = PERF_OUTPUT_COMM},
  168. {.str = "tid", .field = PERF_OUTPUT_TID},
  169. {.str = "pid", .field = PERF_OUTPUT_PID},
  170. {.str = "time", .field = PERF_OUTPUT_TIME},
  171. {.str = "cpu", .field = PERF_OUTPUT_CPU},
  172. {.str = "event", .field = PERF_OUTPUT_EVNAME},
  173. {.str = "trace", .field = PERF_OUTPUT_TRACE},
  174. {.str = "ip", .field = PERF_OUTPUT_IP},
  175. {.str = "sym", .field = PERF_OUTPUT_SYM},
  176. {.str = "dso", .field = PERF_OUTPUT_DSO},
  177. {.str = "dsoff", .field = PERF_OUTPUT_DSOFF},
  178. {.str = "addr", .field = PERF_OUTPUT_ADDR},
  179. {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
  180. {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
  181. {.str = "period", .field = PERF_OUTPUT_PERIOD},
  182. {.str = "iregs", .field = PERF_OUTPUT_IREGS},
  183. {.str = "uregs", .field = PERF_OUTPUT_UREGS},
  184. {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
  185. {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
  186. {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
  187. {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
  188. {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
  189. {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
  190. {.str = "insn", .field = PERF_OUTPUT_INSN},
  191. {.str = "disasm", .field = PERF_OUTPUT_DISASM},
  192. {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
  193. {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
  194. {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
  195. {.str = "synth", .field = PERF_OUTPUT_SYNTH},
  196. {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
  197. {.str = "metric", .field = PERF_OUTPUT_METRIC},
  198. {.str = "misc", .field = PERF_OUTPUT_MISC},
  199. {.str = "srccode", .field = PERF_OUTPUT_SRCCODE},
  200. {.str = "ipc", .field = PERF_OUTPUT_IPC},
  201. {.str = "tod", .field = PERF_OUTPUT_TOD},
  202. {.str = "data_page_size", .field = PERF_OUTPUT_DATA_PAGE_SIZE},
  203. {.str = "code_page_size", .field = PERF_OUTPUT_CODE_PAGE_SIZE},
  204. {.str = "ins_lat", .field = PERF_OUTPUT_INS_LAT},
  205. {.str = "brstackinsnlen", .field = PERF_OUTPUT_BRSTACKINSNLEN},
  206. {.str = "machine_pid", .field = PERF_OUTPUT_MACHINE_PID},
  207. {.str = "vcpu", .field = PERF_OUTPUT_VCPU},
  208. {.str = "cgroup", .field = PERF_OUTPUT_CGROUP},
  209. {.str = "retire_lat", .field = PERF_OUTPUT_RETIRE_LAT},
  210. {.str = "brstackdisasm", .field = PERF_OUTPUT_BRSTACKDISASM},
  211. {.str = "brcntr", .field = PERF_OUTPUT_BRCNTR},
  212. };
  213. enum {
  214. OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
  215. OUTPUT_TYPE_OTHER,
  216. OUTPUT_TYPE_MAX
  217. };
  218. // We need to refactor the evsel->priv use in 'perf script' to allow for
  219. // using that area, that is being used only in some cases.
  220. #define OUTPUT_TYPE_UNSET -1
  221. /* default set to maintain compatibility with current format */
  222. static struct {
  223. bool user_set;
  224. bool wildcard_set;
  225. unsigned int print_ip_opts;
  226. u64 fields;
  227. u64 invalid_fields;
  228. u64 user_set_fields;
  229. u64 user_unset_fields;
  230. } output[OUTPUT_TYPE_MAX] = {
  231. [PERF_TYPE_HARDWARE] = {
  232. .user_set = false,
  233. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  234. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  235. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  236. PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
  237. PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
  238. .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
  239. },
  240. [PERF_TYPE_SOFTWARE] = {
  241. .user_set = false,
  242. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  243. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  244. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  245. PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
  246. PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
  247. PERF_OUTPUT_BPF_OUTPUT,
  248. .invalid_fields = PERF_OUTPUT_TRACE,
  249. },
  250. [PERF_TYPE_TRACEPOINT] = {
  251. .user_set = false,
  252. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  253. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  254. PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
  255. },
  256. [PERF_TYPE_HW_CACHE] = {
  257. .user_set = false,
  258. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  259. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  260. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  261. PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
  262. PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
  263. .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
  264. },
  265. [PERF_TYPE_RAW] = {
  266. .user_set = false,
  267. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  268. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  269. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  270. PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
  271. PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
  272. PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
  273. PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR |
  274. PERF_OUTPUT_DATA_PAGE_SIZE | PERF_OUTPUT_CODE_PAGE_SIZE |
  275. PERF_OUTPUT_INS_LAT | PERF_OUTPUT_RETIRE_LAT,
  276. .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
  277. },
  278. [PERF_TYPE_BREAKPOINT] = {
  279. .user_set = false,
  280. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  281. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  282. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  283. PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
  284. PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
  285. .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
  286. },
  287. [OUTPUT_TYPE_SYNTH] = {
  288. .user_set = false,
  289. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  290. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  291. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  292. PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
  293. PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
  294. .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
  295. },
  296. [OUTPUT_TYPE_OTHER] = {
  297. .user_set = false,
  298. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  299. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  300. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  301. PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
  302. PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
  303. .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
  304. },
  305. };
  306. struct evsel_script {
  307. char *filename;
  308. FILE *fp;
  309. u64 samples;
  310. };
  311. static struct evsel_script *evsel_script__new(struct evsel *evsel, struct perf_data *data)
  312. {
  313. struct evsel_script *es = zalloc(sizeof(*es));
  314. if (es != NULL) {
  315. if (asprintf(&es->filename, "%s.%s.dump", data->file.path, evsel__name(evsel)) < 0)
  316. goto out_free;
  317. es->fp = fopen(es->filename, "w");
  318. if (es->fp == NULL)
  319. goto out_free_filename;
  320. }
  321. return es;
  322. out_free_filename:
  323. zfree(&es->filename);
  324. out_free:
  325. free(es);
  326. return NULL;
  327. }
  328. static void evsel_script__delete(struct evsel_script *es)
  329. {
  330. zfree(&es->filename);
  331. fclose(es->fp);
  332. es->fp = NULL;
  333. free(es);
  334. }
  335. static int evsel_script__fprintf(struct evsel_script *es, FILE *fp)
  336. {
  337. struct stat st;
  338. fstat(fileno(es->fp), &st);
  339. return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
  340. st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
  341. }
  342. static inline int output_type(unsigned int type)
  343. {
  344. switch (type) {
  345. case PERF_TYPE_SYNTH:
  346. return OUTPUT_TYPE_SYNTH;
  347. default:
  348. if (type < PERF_TYPE_MAX)
  349. return type;
  350. }
  351. return OUTPUT_TYPE_OTHER;
  352. }
  353. static inline int evsel__output_type(struct evsel *evsel)
  354. {
  355. int type = evsel->script_output_type;
  356. if (type == OUTPUT_TYPE_UNSET) {
  357. type = output_type(evsel->core.attr.type);
  358. if (type == OUTPUT_TYPE_OTHER) {
  359. struct perf_pmu *pmu = evsel__find_pmu(evsel);
  360. if (pmu && pmu->is_core)
  361. type = PERF_TYPE_RAW;
  362. }
  363. evsel->script_output_type = type;
  364. }
  365. return type;
  366. }
  367. static bool output_set_by_user(void)
  368. {
  369. int j;
  370. for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
  371. if (output[j].user_set)
  372. return true;
  373. }
  374. return false;
  375. }
  376. static const char *output_field2str(enum perf_output_field field)
  377. {
  378. int i, imax = ARRAY_SIZE(all_output_options);
  379. const char *str = "";
  380. for (i = 0; i < imax; ++i) {
  381. if (all_output_options[i].field == field) {
  382. str = all_output_options[i].str;
  383. break;
  384. }
  385. }
  386. return str;
  387. }
  388. #define PRINT_FIELD(x) (output[evsel__output_type(evsel)].fields & PERF_OUTPUT_##x)
  389. static int evsel__do_check_stype(struct evsel *evsel, u64 sample_type, const char *sample_msg,
  390. enum perf_output_field field, bool allow_user_set)
  391. {
  392. struct perf_event_attr *attr = &evsel->core.attr;
  393. int type = evsel__output_type(evsel);
  394. const char *evname;
  395. if (attr->sample_type & sample_type)
  396. return 0;
  397. if (output[type].user_set_fields & field) {
  398. if (allow_user_set)
  399. return 0;
  400. evname = evsel__name(evsel);
  401. pr_err("Samples for '%s' event do not have %s attribute set. "
  402. "Cannot print '%s' field.\n",
  403. evname, sample_msg, output_field2str(field));
  404. return -1;
  405. }
  406. /* user did not ask for it explicitly so remove from the default list */
  407. output[type].fields &= ~field;
  408. evname = evsel__name(evsel);
  409. pr_debug("Samples for '%s' event do not have %s attribute set. "
  410. "Skipping '%s' field.\n",
  411. evname, sample_msg, output_field2str(field));
  412. return 0;
  413. }
  414. static int evsel__check_stype(struct evsel *evsel, u64 sample_type, const char *sample_msg,
  415. enum perf_output_field field)
  416. {
  417. return evsel__do_check_stype(evsel, sample_type, sample_msg, field, false);
  418. }
  419. static int evsel__check_attr(struct evsel *evsel, struct perf_session *session)
  420. {
  421. bool allow_user_set;
  422. if (evsel__is_dummy_event(evsel))
  423. return 0;
  424. if (perf_header__has_feat(&session->header, HEADER_STAT))
  425. return 0;
  426. allow_user_set = perf_header__has_feat(&session->header,
  427. HEADER_AUXTRACE);
  428. if (PRINT_FIELD(TRACE) &&
  429. !perf_session__has_traces(session, "record -R"))
  430. return -EINVAL;
  431. if (PRINT_FIELD(IP)) {
  432. if (evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP", PERF_OUTPUT_IP))
  433. return -EINVAL;
  434. }
  435. if (PRINT_FIELD(ADDR) &&
  436. evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR", PERF_OUTPUT_ADDR, allow_user_set))
  437. return -EINVAL;
  438. if (PRINT_FIELD(DATA_SRC) &&
  439. evsel__do_check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC", PERF_OUTPUT_DATA_SRC, allow_user_set))
  440. return -EINVAL;
  441. if (PRINT_FIELD(WEIGHT) &&
  442. evsel__do_check_stype(evsel, PERF_SAMPLE_WEIGHT_TYPE, "WEIGHT", PERF_OUTPUT_WEIGHT, allow_user_set))
  443. return -EINVAL;
  444. if (PRINT_FIELD(SYM) &&
  445. !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
  446. pr_err("Display of symbols requested but neither sample IP nor "
  447. "sample address\navailable. Hence, no addresses to convert "
  448. "to symbols.\n");
  449. return -EINVAL;
  450. }
  451. if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
  452. pr_err("Display of offsets requested but symbol is not"
  453. "selected.\n");
  454. return -EINVAL;
  455. }
  456. if (PRINT_FIELD(DSO) &&
  457. !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
  458. pr_err("Display of DSO requested but no address to convert.\n");
  459. return -EINVAL;
  460. }
  461. if ((PRINT_FIELD(SRCLINE) || PRINT_FIELD(SRCCODE)) && !PRINT_FIELD(IP)) {
  462. pr_err("Display of source line number requested but sample IP is not\n"
  463. "selected. Hence, no address to lookup the source line number.\n");
  464. return -EINVAL;
  465. }
  466. if ((PRINT_FIELD(BRSTACKINSN) || PRINT_FIELD(BRSTACKINSNLEN) || PRINT_FIELD(BRSTACKDISASM))
  467. && !allow_user_set &&
  468. !(evlist__combined_branch_type(session->evlist) & PERF_SAMPLE_BRANCH_ANY)) {
  469. pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
  470. "Hint: run 'perf record -b ...'\n");
  471. return -EINVAL;
  472. }
  473. if (PRINT_FIELD(BRCNTR) &&
  474. !(evlist__combined_branch_type(session->evlist) & PERF_SAMPLE_BRANCH_COUNTERS)) {
  475. pr_err("Display of branch counter requested but it's not enabled\n"
  476. "Hint: run 'perf record -j any,counter ...'\n");
  477. return -EINVAL;
  478. }
  479. if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
  480. evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID", PERF_OUTPUT_TID|PERF_OUTPUT_PID))
  481. return -EINVAL;
  482. if (PRINT_FIELD(TIME) &&
  483. evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME", PERF_OUTPUT_TIME))
  484. return -EINVAL;
  485. if (PRINT_FIELD(CPU) &&
  486. evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU", PERF_OUTPUT_CPU, allow_user_set))
  487. return -EINVAL;
  488. if (PRINT_FIELD(IREGS) &&
  489. evsel__do_check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS", PERF_OUTPUT_IREGS, allow_user_set))
  490. return -EINVAL;
  491. if (PRINT_FIELD(UREGS) &&
  492. evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS", PERF_OUTPUT_UREGS))
  493. return -EINVAL;
  494. if (PRINT_FIELD(PHYS_ADDR) &&
  495. evsel__do_check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR", PERF_OUTPUT_PHYS_ADDR, allow_user_set))
  496. return -EINVAL;
  497. if (PRINT_FIELD(DATA_PAGE_SIZE) &&
  498. evsel__check_stype(evsel, PERF_SAMPLE_DATA_PAGE_SIZE, "DATA_PAGE_SIZE", PERF_OUTPUT_DATA_PAGE_SIZE))
  499. return -EINVAL;
  500. if (PRINT_FIELD(CODE_PAGE_SIZE) &&
  501. evsel__check_stype(evsel, PERF_SAMPLE_CODE_PAGE_SIZE, "CODE_PAGE_SIZE", PERF_OUTPUT_CODE_PAGE_SIZE))
  502. return -EINVAL;
  503. if (PRINT_FIELD(INS_LAT) &&
  504. evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT_STRUCT, "WEIGHT_STRUCT", PERF_OUTPUT_INS_LAT))
  505. return -EINVAL;
  506. if (PRINT_FIELD(CGROUP) &&
  507. evsel__check_stype(evsel, PERF_SAMPLE_CGROUP, "CGROUP", PERF_OUTPUT_CGROUP)) {
  508. pr_err("Hint: run 'perf record --all-cgroups ...'\n");
  509. return -EINVAL;
  510. }
  511. if (PRINT_FIELD(RETIRE_LAT) &&
  512. evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT_STRUCT, "WEIGHT_STRUCT", PERF_OUTPUT_RETIRE_LAT))
  513. return -EINVAL;
  514. return 0;
  515. }
  516. static void evsel__set_print_ip_opts(struct evsel *evsel)
  517. {
  518. unsigned int type = evsel__output_type(evsel);
  519. output[type].print_ip_opts = 0;
  520. if (PRINT_FIELD(IP))
  521. output[type].print_ip_opts |= EVSEL__PRINT_IP;
  522. if (PRINT_FIELD(SYM))
  523. output[type].print_ip_opts |= EVSEL__PRINT_SYM;
  524. if (PRINT_FIELD(DSO))
  525. output[type].print_ip_opts |= EVSEL__PRINT_DSO;
  526. if (PRINT_FIELD(DSOFF))
  527. output[type].print_ip_opts |= EVSEL__PRINT_DSOFF;
  528. if (PRINT_FIELD(SYMOFFSET))
  529. output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
  530. if (PRINT_FIELD(SRCLINE))
  531. output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
  532. }
  533. static struct evsel *find_first_output_type(struct evlist *evlist,
  534. unsigned int type)
  535. {
  536. struct evsel *evsel;
  537. evlist__for_each_entry(evlist, evsel) {
  538. if (evsel__is_dummy_event(evsel))
  539. continue;
  540. if (evsel__output_type(evsel) == (int)type)
  541. return evsel;
  542. }
  543. return NULL;
  544. }
  545. /*
  546. * verify all user requested events exist and the samples
  547. * have the expected data
  548. */
  549. static int perf_session__check_output_opt(struct perf_session *session)
  550. {
  551. bool tod = false;
  552. unsigned int j;
  553. struct evsel *evsel;
  554. for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
  555. evsel = find_first_output_type(session->evlist, j);
  556. /*
  557. * even if fields is set to 0 (ie., show nothing) event must
  558. * exist if user explicitly includes it on the command line
  559. */
  560. if (!evsel && output[j].user_set && !output[j].wildcard_set &&
  561. j != OUTPUT_TYPE_SYNTH) {
  562. pr_err("%s events do not exist. "
  563. "Remove corresponding -F option to proceed.\n",
  564. event_type(j));
  565. return -1;
  566. }
  567. if (evsel && output[j].fields &&
  568. evsel__check_attr(evsel, session))
  569. return -1;
  570. if (evsel == NULL)
  571. continue;
  572. /* 'dsoff' implys 'dso' field */
  573. if (output[j].fields & PERF_OUTPUT_DSOFF)
  574. output[j].fields |= PERF_OUTPUT_DSO;
  575. evsel__set_print_ip_opts(evsel);
  576. tod |= output[j].fields & PERF_OUTPUT_TOD;
  577. }
  578. if (!no_callchain) {
  579. bool use_callchain = false;
  580. bool not_pipe = false;
  581. evlist__for_each_entry(session->evlist, evsel) {
  582. not_pipe = true;
  583. if (evsel__has_callchain(evsel) || evsel__is_offcpu_event(evsel)) {
  584. use_callchain = true;
  585. break;
  586. }
  587. }
  588. if (not_pipe && !use_callchain)
  589. symbol_conf.use_callchain = false;
  590. }
  591. /*
  592. * set default for tracepoints to print symbols only
  593. * if callchains are present
  594. */
  595. if (symbol_conf.use_callchain &&
  596. !output[PERF_TYPE_TRACEPOINT].user_set) {
  597. j = PERF_TYPE_TRACEPOINT;
  598. evlist__for_each_entry(session->evlist, evsel) {
  599. if (evsel->core.attr.type != j)
  600. continue;
  601. if (evsel__has_callchain(evsel)) {
  602. output[j].fields |= PERF_OUTPUT_IP;
  603. output[j].fields |= PERF_OUTPUT_SYM;
  604. output[j].fields |= PERF_OUTPUT_SYMOFFSET;
  605. output[j].fields |= PERF_OUTPUT_DSO;
  606. evsel__set_print_ip_opts(evsel);
  607. goto out;
  608. }
  609. }
  610. }
  611. if (tod && !perf_session__env(session)->clock.enabled) {
  612. pr_err("Can't provide 'tod' time, missing clock data. "
  613. "Please record with -k/--clockid option.\n");
  614. return -1;
  615. }
  616. out:
  617. return 0;
  618. }
  619. static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask,
  620. uint16_t e_machine, uint32_t e_flags,
  621. FILE *fp)
  622. {
  623. unsigned i = 0, r;
  624. int printed = 0;
  625. if (!regs || !regs->regs)
  626. return 0;
  627. printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
  628. for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
  629. u64 val = regs->regs[i++];
  630. printed += fprintf(fp, "%5s:0x%"PRIx64" ",
  631. perf_reg_name(r, e_machine, e_flags),
  632. val);
  633. }
  634. return printed;
  635. }
  636. #define DEFAULT_TOD_FMT "%F %H:%M:%S"
  637. static char*
  638. tod_scnprintf(struct perf_script *script, char *buf, int buflen,
  639. u64 timestamp)
  640. {
  641. u64 tod_ns, clockid_ns;
  642. struct perf_env *env;
  643. unsigned long nsec;
  644. struct tm ltime;
  645. char date[64];
  646. time_t sec;
  647. buf[0] = '\0';
  648. if (buflen < 64 || !script)
  649. return buf;
  650. env = perf_session__env(script->session);
  651. if (!env->clock.enabled) {
  652. scnprintf(buf, buflen, "disabled");
  653. return buf;
  654. }
  655. clockid_ns = env->clock.clockid_ns;
  656. tod_ns = env->clock.tod_ns;
  657. if (timestamp > clockid_ns)
  658. tod_ns += timestamp - clockid_ns;
  659. else
  660. tod_ns -= clockid_ns - timestamp;
  661. sec = (time_t) (tod_ns / NSEC_PER_SEC);
  662. nsec = tod_ns - sec * NSEC_PER_SEC;
  663. if (localtime_r(&sec, &ltime) == NULL) {
  664. scnprintf(buf, buflen, "failed");
  665. } else {
  666. strftime(date, sizeof(date), DEFAULT_TOD_FMT, &ltime);
  667. if (symbol_conf.nanosecs) {
  668. snprintf(buf, buflen, "%s.%09lu", date, nsec);
  669. } else {
  670. snprintf(buf, buflen, "%s.%06lu",
  671. date, nsec / NSEC_PER_USEC);
  672. }
  673. }
  674. return buf;
  675. }
  676. static int perf_sample__fprintf_iregs(struct perf_sample *sample,
  677. struct perf_event_attr *attr,
  678. uint16_t e_machine,
  679. uint32_t e_flags,
  680. FILE *fp)
  681. {
  682. if (!sample->intr_regs)
  683. return 0;
  684. return perf_sample__fprintf_regs(perf_sample__intr_regs(sample),
  685. attr->sample_regs_intr, e_machine, e_flags, fp);
  686. }
  687. static int perf_sample__fprintf_uregs(struct perf_sample *sample,
  688. struct perf_event_attr *attr,
  689. uint16_t e_machine,
  690. uint32_t e_flags,
  691. FILE *fp)
  692. {
  693. if (!sample->user_regs)
  694. return 0;
  695. return perf_sample__fprintf_regs(perf_sample__user_regs(sample),
  696. attr->sample_regs_user, e_machine, e_flags, fp);
  697. }
  698. static int perf_sample__fprintf_start(struct perf_script *script,
  699. struct perf_sample *sample,
  700. struct thread *thread,
  701. struct evsel *evsel,
  702. u32 type, FILE *fp)
  703. {
  704. unsigned long secs;
  705. unsigned long long nsecs;
  706. int printed = 0;
  707. char tstr[128];
  708. /*
  709. * Print the branch counter's abbreviation list,
  710. * if the branch counter is available.
  711. */
  712. if (PRINT_FIELD(BRCNTR) && !verbose) {
  713. char *buf;
  714. if (!annotation_br_cntr_abbr_list(&buf, evsel, true)) {
  715. printed += fprintf(stdout, "%s", buf);
  716. free(buf);
  717. }
  718. }
  719. if (PRINT_FIELD(MACHINE_PID) && sample->machine_pid)
  720. printed += fprintf(fp, "VM:%5d ", sample->machine_pid);
  721. /* Print VCPU only for guest events i.e. with machine_pid */
  722. if (PRINT_FIELD(VCPU) && sample->machine_pid)
  723. printed += fprintf(fp, "VCPU:%03d ", sample->vcpu);
  724. if (PRINT_FIELD(COMM)) {
  725. const char *comm = thread ? thread__comm_str(thread) : ":-1";
  726. if (latency_format)
  727. printed += fprintf(fp, "%8.8s ", comm);
  728. else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
  729. printed += fprintf(fp, "%s ", comm);
  730. else
  731. printed += fprintf(fp, "%16s ", comm);
  732. }
  733. if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
  734. printed += fprintf(fp, "%7d/%-7d ", sample->pid, sample->tid);
  735. else if (PRINT_FIELD(PID))
  736. printed += fprintf(fp, "%7d ", sample->pid);
  737. else if (PRINT_FIELD(TID))
  738. printed += fprintf(fp, "%7d ", sample->tid);
  739. if (PRINT_FIELD(CPU)) {
  740. if (latency_format)
  741. printed += fprintf(fp, "%3d ", sample->cpu);
  742. else
  743. printed += fprintf(fp, "[%03d] ", sample->cpu);
  744. }
  745. if (PRINT_FIELD(MISC)) {
  746. int ret = 0;
  747. #define has(m) \
  748. (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
  749. if (has(KERNEL))
  750. ret += fprintf(fp, "K");
  751. if (has(USER))
  752. ret += fprintf(fp, "U");
  753. if (has(HYPERVISOR))
  754. ret += fprintf(fp, "H");
  755. if (has(GUEST_KERNEL))
  756. ret += fprintf(fp, "G");
  757. if (has(GUEST_USER))
  758. ret += fprintf(fp, "g");
  759. switch (type) {
  760. case PERF_RECORD_MMAP:
  761. case PERF_RECORD_MMAP2:
  762. if (has(MMAP_DATA))
  763. ret += fprintf(fp, "M");
  764. break;
  765. case PERF_RECORD_COMM:
  766. if (has(COMM_EXEC))
  767. ret += fprintf(fp, "E");
  768. break;
  769. case PERF_RECORD_SWITCH:
  770. case PERF_RECORD_SWITCH_CPU_WIDE:
  771. if (has(SWITCH_OUT)) {
  772. ret += fprintf(fp, "S");
  773. if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
  774. ret += fprintf(fp, "p");
  775. }
  776. default:
  777. break;
  778. }
  779. #undef has
  780. ret += fprintf(fp, "%*s", 6 - ret, " ");
  781. printed += ret;
  782. }
  783. if (PRINT_FIELD(TOD)) {
  784. tod_scnprintf(script, tstr, sizeof(tstr), sample->time);
  785. printed += fprintf(fp, "%s ", tstr);
  786. }
  787. if (PRINT_FIELD(TIME)) {
  788. u64 t = sample->time;
  789. if (reltime) {
  790. if (!initial_time)
  791. initial_time = sample->time;
  792. t = sample->time - initial_time;
  793. } else if (deltatime) {
  794. if (previous_time)
  795. t = sample->time - previous_time;
  796. else {
  797. t = 0;
  798. }
  799. previous_time = sample->time;
  800. }
  801. nsecs = t;
  802. secs = nsecs / NSEC_PER_SEC;
  803. nsecs -= secs * NSEC_PER_SEC;
  804. if (symbol_conf.nanosecs)
  805. printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
  806. else {
  807. char sample_time[32];
  808. timestamp__scnprintf_usec(t, sample_time, sizeof(sample_time));
  809. printed += fprintf(fp, "%12s: ", sample_time);
  810. }
  811. }
  812. return printed;
  813. }
  814. static inline size_t
  815. bstack_event_str(struct branch_entry *br, char *buf, size_t sz)
  816. {
  817. if (!(br->flags.mispred || br->flags.predicted || br->flags.not_taken))
  818. return snprintf(buf, sz, "-");
  819. return snprintf(buf, sz, "%s%s",
  820. br->flags.predicted ? "P" : "M",
  821. br->flags.not_taken ? "N" : "");
  822. }
  823. static int print_bstack_flags(FILE *fp, struct branch_entry *br)
  824. {
  825. char events[16] = { 0 };
  826. size_t pos;
  827. pos = bstack_event_str(br, events, sizeof(events));
  828. return fprintf(fp, "/%s/%c/%c/%d/%s/%s ",
  829. pos < 0 ? "-" : events,
  830. br->flags.in_tx ? 'X' : '-',
  831. br->flags.abort ? 'A' : '-',
  832. br->flags.cycles,
  833. get_branch_type(br),
  834. br->flags.spec ? branch_spec_desc(br->flags.spec) : "-");
  835. }
  836. static int perf_sample__fprintf_brstack(struct perf_sample *sample,
  837. struct thread *thread,
  838. struct evsel *evsel, FILE *fp)
  839. {
  840. struct branch_stack *br = sample->branch_stack;
  841. struct branch_entry *entries = perf_sample__branch_entries(sample);
  842. u64 i, from, to;
  843. int printed = 0;
  844. if (!(br && br->nr))
  845. return 0;
  846. for (i = 0; i < br->nr; i++) {
  847. from = entries[i].from;
  848. to = entries[i].to;
  849. printed += fprintf(fp, " 0x%"PRIx64, from);
  850. if (PRINT_FIELD(DSO)) {
  851. struct addr_location alf, alt;
  852. addr_location__init(&alf);
  853. addr_location__init(&alt);
  854. thread__find_map_fb(thread, sample->cpumode, from, &alf);
  855. thread__find_map_fb(thread, sample->cpumode, to, &alt);
  856. printed += map__fprintf_dsoname_dsoff(alf.map, PRINT_FIELD(DSOFF), alf.addr, fp);
  857. printed += fprintf(fp, "/0x%"PRIx64, to);
  858. printed += map__fprintf_dsoname_dsoff(alt.map, PRINT_FIELD(DSOFF), alt.addr, fp);
  859. addr_location__exit(&alt);
  860. addr_location__exit(&alf);
  861. } else
  862. printed += fprintf(fp, "/0x%"PRIx64, to);
  863. printed += print_bstack_flags(fp, entries + i);
  864. }
  865. return printed;
  866. }
  867. static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
  868. struct thread *thread,
  869. struct evsel *evsel, FILE *fp)
  870. {
  871. struct branch_stack *br = sample->branch_stack;
  872. struct branch_entry *entries = perf_sample__branch_entries(sample);
  873. u64 i, from, to;
  874. int printed = 0;
  875. if (!(br && br->nr))
  876. return 0;
  877. for (i = 0; i < br->nr; i++) {
  878. struct addr_location alf, alt;
  879. addr_location__init(&alf);
  880. addr_location__init(&alt);
  881. from = entries[i].from;
  882. to = entries[i].to;
  883. thread__find_symbol_fb(thread, sample->cpumode, from, &alf);
  884. thread__find_symbol_fb(thread, sample->cpumode, to, &alt);
  885. printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
  886. if (PRINT_FIELD(DSO))
  887. printed += map__fprintf_dsoname_dsoff(alf.map, PRINT_FIELD(DSOFF), alf.addr, fp);
  888. printed += fprintf(fp, "%c", '/');
  889. printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
  890. if (PRINT_FIELD(DSO))
  891. printed += map__fprintf_dsoname_dsoff(alt.map, PRINT_FIELD(DSOFF), alt.addr, fp);
  892. printed += print_bstack_flags(fp, entries + i);
  893. addr_location__exit(&alt);
  894. addr_location__exit(&alf);
  895. }
  896. return printed;
  897. }
  898. static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
  899. struct thread *thread,
  900. struct evsel *evsel, FILE *fp)
  901. {
  902. struct branch_stack *br = sample->branch_stack;
  903. struct branch_entry *entries = perf_sample__branch_entries(sample);
  904. u64 i, from, to;
  905. int printed = 0;
  906. if (!(br && br->nr))
  907. return 0;
  908. for (i = 0; i < br->nr; i++) {
  909. struct addr_location alf, alt;
  910. addr_location__init(&alf);
  911. addr_location__init(&alt);
  912. from = entries[i].from;
  913. to = entries[i].to;
  914. if (thread__find_map_fb(thread, sample->cpumode, from, &alf) &&
  915. !dso__adjust_symbols(map__dso(alf.map)))
  916. from = map__dso_map_ip(alf.map, from);
  917. if (thread__find_map_fb(thread, sample->cpumode, to, &alt) &&
  918. !dso__adjust_symbols(map__dso(alt.map)))
  919. to = map__dso_map_ip(alt.map, to);
  920. printed += fprintf(fp, " 0x%"PRIx64, from);
  921. if (PRINT_FIELD(DSO))
  922. printed += map__fprintf_dsoname_dsoff(alf.map, PRINT_FIELD(DSOFF), alf.addr, fp);
  923. printed += fprintf(fp, "/0x%"PRIx64, to);
  924. if (PRINT_FIELD(DSO))
  925. printed += map__fprintf_dsoname_dsoff(alt.map, PRINT_FIELD(DSOFF), alt.addr, fp);
  926. printed += print_bstack_flags(fp, entries + i);
  927. addr_location__exit(&alt);
  928. addr_location__exit(&alf);
  929. }
  930. return printed;
  931. }
  932. #define MAXBB 16384UL
  933. static int grab_bb(u8 *buffer, u64 start, u64 end,
  934. struct machine *machine, struct thread *thread,
  935. bool *is64bit, u8 *cpumode, bool last)
  936. {
  937. long offset, len;
  938. struct addr_location al;
  939. bool kernel;
  940. struct dso *dso;
  941. int ret = 0;
  942. if (!start || !end)
  943. return 0;
  944. kernel = machine__kernel_ip(machine, start);
  945. if (kernel)
  946. *cpumode = PERF_RECORD_MISC_KERNEL;
  947. else
  948. *cpumode = PERF_RECORD_MISC_USER;
  949. /*
  950. * Block overlaps between kernel and user.
  951. * This can happen due to ring filtering
  952. * On Intel CPUs the entry into the kernel is filtered,
  953. * but the exit is not. Let the caller patch it up.
  954. */
  955. if (kernel != machine__kernel_ip(machine, end)) {
  956. pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
  957. return -ENXIO;
  958. }
  959. if (end - start > MAXBB - MAXINSN) {
  960. if (last)
  961. pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
  962. else
  963. pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
  964. return 0;
  965. }
  966. addr_location__init(&al);
  967. if (!thread__find_map(thread, *cpumode, start, &al) || (dso = map__dso(al.map)) == NULL) {
  968. pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
  969. goto out;
  970. }
  971. if (dso__data(dso)->status == DSO_DATA_STATUS_ERROR) {
  972. pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
  973. goto out;
  974. }
  975. /* Load maps to ensure dso->is_64_bit has been updated */
  976. map__load(al.map);
  977. offset = map__map_ip(al.map, start);
  978. len = dso__data_read_offset(dso, machine, offset, (u8 *)buffer,
  979. end - start + MAXINSN);
  980. *is64bit = dso__is_64_bit(dso);
  981. if (len <= 0)
  982. pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
  983. start, end);
  984. ret = len;
  985. out:
  986. addr_location__exit(&al);
  987. return ret;
  988. }
  989. static int map__fprintf_srccode(struct map *map, u64 addr, FILE *fp, struct srccode_state *state)
  990. {
  991. char *srcfile;
  992. int ret = 0;
  993. unsigned line;
  994. int len;
  995. char *srccode;
  996. struct dso *dso;
  997. if (!map || (dso = map__dso(map)) == NULL)
  998. return 0;
  999. srcfile = get_srcline_split(dso,
  1000. map__rip_2objdump(map, addr),
  1001. &line);
  1002. if (!srcfile)
  1003. return 0;
  1004. /* Avoid redundant printing */
  1005. if (state &&
  1006. state->srcfile &&
  1007. !strcmp(state->srcfile, srcfile) &&
  1008. state->line == line) {
  1009. free(srcfile);
  1010. return 0;
  1011. }
  1012. srccode = find_sourceline(srcfile, line, &len);
  1013. if (!srccode)
  1014. goto out_free_line;
  1015. ret = fprintf(fp, "|%-8d %.*s", line, len, srccode);
  1016. if (state) {
  1017. state->srcfile = srcfile;
  1018. state->line = line;
  1019. }
  1020. return ret;
  1021. out_free_line:
  1022. free(srcfile);
  1023. return ret;
  1024. }
  1025. static int print_srccode(struct thread *thread, u8 cpumode, uint64_t addr)
  1026. {
  1027. struct addr_location al;
  1028. int ret = 0;
  1029. addr_location__init(&al);
  1030. thread__find_map(thread, cpumode, addr, &al);
  1031. if (!al.map)
  1032. goto out;
  1033. ret = map__fprintf_srccode(al.map, al.addr, stdout,
  1034. thread__srccode_state(thread));
  1035. if (ret)
  1036. ret += printf("\n");
  1037. out:
  1038. addr_location__exit(&al);
  1039. return ret;
  1040. }
  1041. static int any_dump_insn(struct evsel *evsel __maybe_unused,
  1042. struct perf_insn *x, uint64_t ip,
  1043. u8 *inbuf, int inlen, int *lenp,
  1044. FILE *fp)
  1045. {
  1046. if (PRINT_FIELD(BRSTACKDISASM)) {
  1047. int printed = fprintf_insn_asm(x->machine, x->thread, x->cpumode, x->is64bit,
  1048. (uint8_t *)inbuf, inlen, ip, lenp,
  1049. PRINT_INSN_IMM_HEX, fp);
  1050. if (printed > 0)
  1051. return printed;
  1052. }
  1053. return fprintf(fp, "%s", dump_insn(x, ip, inbuf, inlen, lenp));
  1054. }
  1055. static int add_padding(FILE *fp, int printed, int padding)
  1056. {
  1057. if (printed >= 0 && printed < padding)
  1058. printed += fprintf(fp, "%*s", padding - printed, "");
  1059. return printed;
  1060. }
  1061. static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
  1062. struct perf_insn *x, u8 *inbuf, int len,
  1063. int insn, FILE *fp, int *total_cycles,
  1064. struct evsel *evsel,
  1065. struct thread *thread,
  1066. u64 br_cntr)
  1067. {
  1068. int ilen = 0;
  1069. int printed = fprintf(fp, "\t%016" PRIx64 "\t", ip);
  1070. printed += add_padding(fp, any_dump_insn(evsel, x, ip, inbuf, len, &ilen, fp), 30);
  1071. printed += fprintf(fp, "\t");
  1072. if (PRINT_FIELD(BRSTACKINSNLEN))
  1073. printed += fprintf(fp, "ilen: %d\t", ilen);
  1074. if (PRINT_FIELD(SRCLINE)) {
  1075. struct addr_location al;
  1076. addr_location__init(&al);
  1077. thread__find_map(thread, x->cpumode, ip, &al);
  1078. printed += map__fprintf_srcline(al.map, al.addr, " srcline: ", fp);
  1079. printed += fprintf(fp, "\t");
  1080. addr_location__exit(&al);
  1081. }
  1082. if (PRINT_FIELD(BRCNTR)) {
  1083. struct evsel *pos = evsel__leader(evsel);
  1084. unsigned int i = 0, j, num, mask, width;
  1085. perf_env__find_br_cntr_info(evsel__env(evsel), NULL, &width);
  1086. mask = (1L << width) - 1;
  1087. printed += fprintf(fp, "br_cntr: ");
  1088. evlist__for_each_entry_from(evsel->evlist, pos) {
  1089. if (!(pos->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_COUNTERS))
  1090. continue;
  1091. if (evsel__leader(pos) != evsel__leader(evsel))
  1092. break;
  1093. num = (br_cntr >> (i++ * width)) & mask;
  1094. if (!verbose) {
  1095. for (j = 0; j < num; j++)
  1096. printed += fprintf(fp, "%s", pos->abbr_name);
  1097. } else
  1098. printed += fprintf(fp, "%s %d ", pos->name, num);
  1099. }
  1100. printed += fprintf(fp, "\t");
  1101. }
  1102. printed += fprintf(fp, "#%s%s%s%s",
  1103. en->flags.predicted ? " PRED" : "",
  1104. en->flags.mispred ? " MISPRED" : "",
  1105. en->flags.in_tx ? " INTX" : "",
  1106. en->flags.abort ? " ABORT" : "");
  1107. if (en->flags.cycles) {
  1108. *total_cycles += en->flags.cycles;
  1109. printed += fprintf(fp, " %d cycles [%d]", en->flags.cycles, *total_cycles);
  1110. if (insn)
  1111. printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
  1112. }
  1113. return printed + fprintf(fp, "\n");
  1114. }
  1115. static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
  1116. u8 cpumode, int cpu, struct symbol **lastsym,
  1117. struct evsel *evsel, FILE *fp)
  1118. {
  1119. struct addr_location al;
  1120. int off, printed = 0, ret = 0;
  1121. addr_location__init(&al);
  1122. thread__find_map(thread, cpumode, addr, &al);
  1123. if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
  1124. goto out;
  1125. al.cpu = cpu;
  1126. al.sym = NULL;
  1127. if (al.map)
  1128. al.sym = map__find_symbol(al.map, al.addr);
  1129. if (!al.sym)
  1130. goto out;
  1131. if (al.addr < al.sym->end)
  1132. off = al.addr - al.sym->start;
  1133. else
  1134. off = al.addr - map__start(al.map) - al.sym->start;
  1135. printed += fprintf(fp, "\t%s", al.sym->name);
  1136. if (off)
  1137. printed += fprintf(fp, "%+d", off);
  1138. printed += fprintf(fp, ":");
  1139. if (PRINT_FIELD(SRCLINE))
  1140. printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
  1141. printed += fprintf(fp, "\n");
  1142. *lastsym = al.sym;
  1143. ret = printed;
  1144. out:
  1145. addr_location__exit(&al);
  1146. return ret;
  1147. }
  1148. static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
  1149. struct evsel *evsel,
  1150. struct thread *thread,
  1151. struct perf_event_attr *attr,
  1152. struct machine *machine, FILE *fp)
  1153. {
  1154. struct branch_stack *br = sample->branch_stack;
  1155. struct branch_entry *entries = perf_sample__branch_entries(sample);
  1156. u64 start, end;
  1157. int i, insn, len, nr, ilen, printed = 0;
  1158. struct perf_insn x;
  1159. u8 buffer[MAXBB];
  1160. unsigned off;
  1161. struct symbol *lastsym = NULL;
  1162. int total_cycles = 0;
  1163. u64 br_cntr = 0;
  1164. if (!(br && br->nr))
  1165. return 0;
  1166. nr = br->nr;
  1167. if (max_blocks && nr > max_blocks + 1)
  1168. nr = max_blocks + 1;
  1169. x.thread = thread;
  1170. x.machine = machine;
  1171. x.cpu = sample->cpu;
  1172. if (PRINT_FIELD(BRCNTR) && sample->branch_stack_cntr)
  1173. br_cntr = sample->branch_stack_cntr[nr - 1];
  1174. printed += fprintf(fp, "%c", '\n');
  1175. /* Handle first from jump, of which we don't know the entry. */
  1176. len = grab_bb(buffer, entries[nr-1].from,
  1177. entries[nr-1].from,
  1178. machine, thread, &x.is64bit, &x.cpumode, false);
  1179. if (len > 0) {
  1180. printed += ip__fprintf_sym(entries[nr - 1].from, thread,
  1181. x.cpumode, x.cpu, &lastsym, evsel, fp);
  1182. printed += ip__fprintf_jump(entries[nr - 1].from, &entries[nr - 1],
  1183. &x, buffer, len, 0, fp, &total_cycles,
  1184. evsel, thread, br_cntr);
  1185. if (PRINT_FIELD(SRCCODE))
  1186. printed += print_srccode(thread, x.cpumode, entries[nr - 1].from);
  1187. }
  1188. /* Print all blocks */
  1189. for (i = nr - 2; i >= 0; i--) {
  1190. if (entries[i].from || entries[i].to)
  1191. pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
  1192. entries[i].from,
  1193. entries[i].to);
  1194. start = entries[i + 1].to;
  1195. end = entries[i].from;
  1196. len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
  1197. /* Patch up missing kernel transfers due to ring filters */
  1198. if (len == -ENXIO && i > 0) {
  1199. end = entries[--i].from;
  1200. pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
  1201. len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
  1202. }
  1203. if (len <= 0)
  1204. continue;
  1205. insn = 0;
  1206. for (off = 0; off < (unsigned)len; off += ilen) {
  1207. uint64_t ip = start + off;
  1208. printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, evsel, fp);
  1209. if (ip == end) {
  1210. if (PRINT_FIELD(BRCNTR) && sample->branch_stack_cntr)
  1211. br_cntr = sample->branch_stack_cntr[i];
  1212. printed += ip__fprintf_jump(ip, &entries[i], &x, buffer + off, len - off, ++insn, fp,
  1213. &total_cycles, evsel, thread, br_cntr);
  1214. if (PRINT_FIELD(SRCCODE))
  1215. printed += print_srccode(thread, x.cpumode, ip);
  1216. break;
  1217. } else {
  1218. ilen = 0;
  1219. printed += fprintf(fp, "\t%016" PRIx64 "\t", ip);
  1220. printed += any_dump_insn(evsel, &x, ip, buffer + off, len - off, &ilen, fp);
  1221. if (PRINT_FIELD(BRSTACKINSNLEN))
  1222. printed += fprintf(fp, "\tilen: %d", ilen);
  1223. printed += fprintf(fp, "\n");
  1224. if (ilen == 0)
  1225. break;
  1226. if (PRINT_FIELD(SRCCODE))
  1227. print_srccode(thread, x.cpumode, ip);
  1228. insn++;
  1229. }
  1230. }
  1231. if (off != end - start)
  1232. printed += fprintf(fp, "\tmismatch of LBR data and executable\n");
  1233. }
  1234. /*
  1235. * Hit the branch? In this case we are already done, and the target
  1236. * has not been executed yet.
  1237. */
  1238. if (entries[0].from == sample->ip)
  1239. goto out;
  1240. if (entries[0].flags.abort)
  1241. goto out;
  1242. /*
  1243. * Print final block up to sample
  1244. *
  1245. * Due to pipeline delays the LBRs might be missing a branch
  1246. * or two, which can result in very large or negative blocks
  1247. * between final branch and sample. When this happens just
  1248. * continue walking after the last TO.
  1249. */
  1250. start = entries[0].to;
  1251. end = sample->ip;
  1252. if (end < start) {
  1253. /* Missing jump. Scan 128 bytes for the next branch */
  1254. end = start + 128;
  1255. }
  1256. len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
  1257. printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, evsel, fp);
  1258. if (len <= 0) {
  1259. /* Print at least last IP if basic block did not work */
  1260. len = grab_bb(buffer, sample->ip, sample->ip,
  1261. machine, thread, &x.is64bit, &x.cpumode, false);
  1262. if (len <= 0)
  1263. goto out;
  1264. ilen = 0;
  1265. printed += fprintf(fp, "\t%016" PRIx64 "\t", sample->ip);
  1266. printed += any_dump_insn(evsel, &x, sample->ip, buffer, len, &ilen, fp);
  1267. if (PRINT_FIELD(BRSTACKINSNLEN))
  1268. printed += fprintf(fp, "\tilen: %d", ilen);
  1269. printed += fprintf(fp, "\n");
  1270. if (PRINT_FIELD(SRCCODE))
  1271. print_srccode(thread, x.cpumode, sample->ip);
  1272. goto out;
  1273. }
  1274. for (off = 0; off <= end - start; off += ilen) {
  1275. ilen = 0;
  1276. printed += fprintf(fp, "\t%016" PRIx64 "\t", start + off);
  1277. printed += any_dump_insn(evsel, &x, start + off, buffer + off, len - off, &ilen, fp);
  1278. if (PRINT_FIELD(BRSTACKINSNLEN))
  1279. printed += fprintf(fp, "\tilen: %d", ilen);
  1280. printed += fprintf(fp, "\n");
  1281. if (ilen == 0)
  1282. break;
  1283. if ((attr->branch_sample_type == 0 || attr->branch_sample_type & PERF_SAMPLE_BRANCH_ANY)
  1284. && arch_is_uncond_branch(buffer + off, len - off, x.is64bit)
  1285. && start + off != sample->ip) {
  1286. /*
  1287. * Hit a missing branch. Just stop.
  1288. */
  1289. printed += fprintf(fp, "\t... not reaching sample ...\n");
  1290. break;
  1291. }
  1292. if (PRINT_FIELD(SRCCODE))
  1293. print_srccode(thread, x.cpumode, start + off);
  1294. }
  1295. out:
  1296. return printed;
  1297. }
  1298. static int perf_sample__fprintf_addr(struct perf_sample *sample,
  1299. struct thread *thread,
  1300. struct evsel *evsel, FILE *fp)
  1301. {
  1302. struct addr_location al;
  1303. int printed = fprintf(fp, "%16" PRIx64, sample->addr);
  1304. addr_location__init(&al);
  1305. if (!sample_addr_correlates_sym(&evsel->core.attr))
  1306. goto out;
  1307. thread__resolve(thread, &al, sample);
  1308. if (PRINT_FIELD(SYM)) {
  1309. printed += fprintf(fp, " ");
  1310. if (PRINT_FIELD(SYMOFFSET))
  1311. printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
  1312. else
  1313. printed += symbol__fprintf_symname(al.sym, fp);
  1314. }
  1315. if (PRINT_FIELD(DSO))
  1316. printed += map__fprintf_dsoname_dsoff(al.map, PRINT_FIELD(DSOFF), al.addr, fp);
  1317. out:
  1318. addr_location__exit(&al);
  1319. return printed;
  1320. }
  1321. static const char *resolve_branch_sym(struct perf_sample *sample,
  1322. struct evsel *evsel,
  1323. struct thread *thread,
  1324. struct addr_location *al,
  1325. struct addr_location *addr_al,
  1326. u64 *ip)
  1327. {
  1328. const char *name = NULL;
  1329. if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
  1330. if (sample_addr_correlates_sym(&evsel->core.attr)) {
  1331. if (!addr_al->thread)
  1332. thread__resolve(thread, addr_al, sample);
  1333. if (addr_al->sym)
  1334. name = addr_al->sym->name;
  1335. else
  1336. *ip = sample->addr;
  1337. } else {
  1338. *ip = sample->addr;
  1339. }
  1340. } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
  1341. if (al->sym)
  1342. name = al->sym->name;
  1343. else
  1344. *ip = sample->ip;
  1345. }
  1346. return name;
  1347. }
  1348. static int perf_sample__fprintf_callindent(struct perf_sample *sample,
  1349. struct evsel *evsel,
  1350. struct thread *thread,
  1351. struct addr_location *al,
  1352. struct addr_location *addr_al,
  1353. FILE *fp)
  1354. {
  1355. size_t depth = thread_stack__depth(thread, sample->cpu);
  1356. const char *name = NULL;
  1357. static int spacing;
  1358. int len = 0;
  1359. int dlen = 0;
  1360. u64 ip = 0;
  1361. /*
  1362. * The 'return' has already been popped off the stack so the depth has
  1363. * to be adjusted to match the 'call'.
  1364. */
  1365. if (thread__ts(thread) && sample->flags & PERF_IP_FLAG_RETURN)
  1366. depth += 1;
  1367. name = resolve_branch_sym(sample, evsel, thread, al, addr_al, &ip);
  1368. if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
  1369. dlen += fprintf(fp, "(");
  1370. dlen += map__fprintf_dsoname(al->map, fp);
  1371. dlen += fprintf(fp, ")\t");
  1372. }
  1373. if (name)
  1374. len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
  1375. else if (ip)
  1376. len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
  1377. if (len < 0)
  1378. return len;
  1379. /*
  1380. * Try to keep the output length from changing frequently so that the
  1381. * output lines up more nicely.
  1382. */
  1383. if (len > spacing || (len && len < spacing - 52))
  1384. spacing = round_up(len + 4, 32);
  1385. if (len < spacing)
  1386. len += fprintf(fp, "%*s", spacing - len, "");
  1387. return len + dlen;
  1388. }
  1389. static int perf_sample__fprintf_insn(struct perf_sample *sample,
  1390. struct evsel *evsel,
  1391. struct perf_event_attr *attr,
  1392. struct thread *thread,
  1393. struct machine *machine, FILE *fp,
  1394. struct addr_location *al)
  1395. {
  1396. int printed = 0;
  1397. perf_sample__fetch_insn(sample, thread, machine);
  1398. if (PRINT_FIELD(INSNLEN))
  1399. printed += fprintf(fp, " ilen: %d", sample->insn_len);
  1400. if (PRINT_FIELD(INSN) && sample->insn_len) {
  1401. printed += fprintf(fp, " insn: ");
  1402. printed += sample__fprintf_insn_raw(sample, fp);
  1403. }
  1404. if (PRINT_FIELD(DISASM) && sample->insn_len) {
  1405. printed += fprintf(fp, "\t\t");
  1406. printed += sample__fprintf_insn_asm(sample, thread, machine, fp, al);
  1407. }
  1408. if (PRINT_FIELD(BRSTACKINSN) || PRINT_FIELD(BRSTACKINSNLEN) || PRINT_FIELD(BRSTACKDISASM))
  1409. printed += perf_sample__fprintf_brstackinsn(sample, evsel, thread, attr, machine, fp);
  1410. return printed;
  1411. }
  1412. static int perf_sample__fprintf_ipc(struct perf_sample *sample,
  1413. struct evsel *evsel, FILE *fp)
  1414. {
  1415. unsigned int ipc;
  1416. if (!PRINT_FIELD(IPC) || !sample->cyc_cnt || !sample->insn_cnt)
  1417. return 0;
  1418. ipc = (sample->insn_cnt * 100) / sample->cyc_cnt;
  1419. return fprintf(fp, " \t IPC: %u.%02u (%" PRIu64 "/%" PRIu64 ") ",
  1420. ipc / 100, ipc % 100, sample->insn_cnt, sample->cyc_cnt);
  1421. }
  1422. static int perf_sample__fprintf_bts(struct perf_sample *sample,
  1423. struct evsel *evsel,
  1424. struct thread *thread,
  1425. struct addr_location *al,
  1426. struct addr_location *addr_al,
  1427. struct machine *machine, FILE *fp)
  1428. {
  1429. struct perf_event_attr *attr = &evsel->core.attr;
  1430. unsigned int type = evsel__output_type(evsel);
  1431. bool print_srcline_last = false;
  1432. int printed = 0;
  1433. if (PRINT_FIELD(CALLINDENT))
  1434. printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, addr_al, fp);
  1435. /* print branch_from information */
  1436. if (PRINT_FIELD(IP)) {
  1437. unsigned int print_opts = output[type].print_ip_opts;
  1438. struct callchain_cursor *cursor = NULL;
  1439. if (symbol_conf.use_callchain && sample->callchain) {
  1440. cursor = get_tls_callchain_cursor();
  1441. if (thread__resolve_callchain(al->thread, cursor, evsel,
  1442. sample, NULL, NULL,
  1443. scripting_max_stack))
  1444. cursor = NULL;
  1445. }
  1446. if (cursor == NULL) {
  1447. printed += fprintf(fp, " ");
  1448. if (print_opts & EVSEL__PRINT_SRCLINE) {
  1449. print_srcline_last = true;
  1450. print_opts &= ~EVSEL__PRINT_SRCLINE;
  1451. }
  1452. } else
  1453. printed += fprintf(fp, "\n");
  1454. printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor,
  1455. symbol_conf.bt_stop_list, fp);
  1456. }
  1457. /* print branch_to information */
  1458. if (PRINT_FIELD(ADDR) ||
  1459. ((evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
  1460. !output[type].user_set)) {
  1461. printed += fprintf(fp, " => ");
  1462. printed += perf_sample__fprintf_addr(sample, thread, evsel, fp);
  1463. }
  1464. printed += perf_sample__fprintf_ipc(sample, evsel, fp);
  1465. if (print_srcline_last)
  1466. printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
  1467. printed += perf_sample__fprintf_insn(sample, evsel, attr, thread, machine, fp, al);
  1468. printed += fprintf(fp, "\n");
  1469. if (PRINT_FIELD(SRCCODE)) {
  1470. int ret = map__fprintf_srccode(al->map, al->addr, stdout,
  1471. thread__srccode_state(thread));
  1472. if (ret) {
  1473. printed += ret;
  1474. printed += printf("\n");
  1475. }
  1476. }
  1477. return printed;
  1478. }
  1479. static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
  1480. {
  1481. char str[SAMPLE_FLAGS_BUF_SIZE];
  1482. int ret;
  1483. ret = perf_sample__sprintf_flags(flags, str, sizeof(str));
  1484. if (ret < 0)
  1485. return fprintf(fp, " raw flags:0x%-*x ",
  1486. SAMPLE_FLAGS_STR_ALIGNED_SIZE - 12, flags);
  1487. return fprintf(fp, " %-*s ", SAMPLE_FLAGS_STR_ALIGNED_SIZE, str);
  1488. }
  1489. struct printer_data {
  1490. int line_no;
  1491. bool hit_nul;
  1492. bool is_printable;
  1493. };
  1494. static int sample__fprintf_bpf_output(enum binary_printer_ops op,
  1495. unsigned int val,
  1496. void *extra, FILE *fp)
  1497. {
  1498. unsigned char ch = (unsigned char)val;
  1499. struct printer_data *printer_data = extra;
  1500. int printed = 0;
  1501. switch (op) {
  1502. case BINARY_PRINT_DATA_BEGIN:
  1503. printed += fprintf(fp, "\n");
  1504. break;
  1505. case BINARY_PRINT_LINE_BEGIN:
  1506. printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
  1507. " ");
  1508. break;
  1509. case BINARY_PRINT_ADDR:
  1510. printed += fprintf(fp, " %04x:", val);
  1511. break;
  1512. case BINARY_PRINT_NUM_DATA:
  1513. printed += fprintf(fp, " %02x", val);
  1514. break;
  1515. case BINARY_PRINT_NUM_PAD:
  1516. printed += fprintf(fp, " ");
  1517. break;
  1518. case BINARY_PRINT_SEP:
  1519. printed += fprintf(fp, " ");
  1520. break;
  1521. case BINARY_PRINT_CHAR_DATA:
  1522. if (printer_data->hit_nul && ch)
  1523. printer_data->is_printable = false;
  1524. if (!isprint(ch)) {
  1525. printed += fprintf(fp, "%c", '.');
  1526. if (!printer_data->is_printable)
  1527. break;
  1528. if (ch == '\0')
  1529. printer_data->hit_nul = true;
  1530. else
  1531. printer_data->is_printable = false;
  1532. } else {
  1533. printed += fprintf(fp, "%c", ch);
  1534. }
  1535. break;
  1536. case BINARY_PRINT_CHAR_PAD:
  1537. printed += fprintf(fp, " ");
  1538. break;
  1539. case BINARY_PRINT_LINE_END:
  1540. printed += fprintf(fp, "\n");
  1541. printer_data->line_no++;
  1542. break;
  1543. case BINARY_PRINT_DATA_END:
  1544. default:
  1545. break;
  1546. }
  1547. return printed;
  1548. }
  1549. static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
  1550. {
  1551. unsigned int nr_bytes = sample->raw_size;
  1552. struct printer_data printer_data = {0, false, true};
  1553. int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
  1554. sample__fprintf_bpf_output, &printer_data, fp);
  1555. if (printer_data.is_printable && printer_data.hit_nul)
  1556. printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
  1557. return printed;
  1558. }
  1559. static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
  1560. {
  1561. if (len > 0 && len < spacing)
  1562. return fprintf(fp, "%*s", spacing - len, "");
  1563. return 0;
  1564. }
  1565. static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
  1566. {
  1567. return perf_sample__fprintf_spacing(len, 34, fp);
  1568. }
  1569. /* If a value contains only printable ASCII characters padded with NULLs */
  1570. static bool ptw_is_prt(u64 val)
  1571. {
  1572. char c;
  1573. u32 i;
  1574. for (i = 0; i < sizeof(val); i++) {
  1575. c = ((char *)&val)[i];
  1576. if (!c)
  1577. break;
  1578. if (!isprint(c) || !isascii(c))
  1579. return false;
  1580. }
  1581. for (; i < sizeof(val); i++) {
  1582. c = ((char *)&val)[i];
  1583. if (c)
  1584. return false;
  1585. }
  1586. return true;
  1587. }
  1588. static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
  1589. {
  1590. struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
  1591. char str[sizeof(u64) + 1] = "";
  1592. int len;
  1593. u64 val;
  1594. if (perf_sample__bad_synth_size(sample, *data))
  1595. return 0;
  1596. val = le64_to_cpu(data->payload);
  1597. if (ptw_is_prt(val)) {
  1598. memcpy(str, &val, sizeof(val));
  1599. str[sizeof(val)] = 0;
  1600. }
  1601. len = fprintf(fp, " IP: %u payload: %#" PRIx64 " %s ",
  1602. data->ip, val, str);
  1603. return len + perf_sample__fprintf_pt_spacing(len, fp);
  1604. }
  1605. static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
  1606. {
  1607. struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
  1608. int len;
  1609. if (perf_sample__bad_synth_size(sample, *data))
  1610. return 0;
  1611. len = fprintf(fp, " hints: %#x extensions: %#x ",
  1612. data->hints, data->extensions);
  1613. return len + perf_sample__fprintf_pt_spacing(len, fp);
  1614. }
  1615. static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
  1616. {
  1617. struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
  1618. int len;
  1619. if (perf_sample__bad_synth_size(sample, *data))
  1620. return 0;
  1621. len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
  1622. data->hw, data->cstate, data->subcstate);
  1623. return len + perf_sample__fprintf_pt_spacing(len, fp);
  1624. }
  1625. static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
  1626. {
  1627. struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
  1628. int len;
  1629. if (perf_sample__bad_synth_size(sample, *data))
  1630. return 0;
  1631. len = fprintf(fp, " IP: %u ", data->ip);
  1632. return len + perf_sample__fprintf_pt_spacing(len, fp);
  1633. }
  1634. static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
  1635. {
  1636. struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
  1637. int len;
  1638. if (perf_sample__bad_synth_size(sample, *data))
  1639. return 0;
  1640. len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
  1641. data->deepest_cstate, data->last_cstate,
  1642. data->wake_reason);
  1643. return len + perf_sample__fprintf_pt_spacing(len, fp);
  1644. }
  1645. static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
  1646. {
  1647. struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
  1648. unsigned int percent, freq;
  1649. int len;
  1650. if (perf_sample__bad_synth_size(sample, *data))
  1651. return 0;
  1652. freq = (le32_to_cpu(data->freq) + 500) / 1000;
  1653. len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
  1654. if (data->max_nonturbo) {
  1655. percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
  1656. len += fprintf(fp, "(%3u%%) ", percent);
  1657. }
  1658. return len + perf_sample__fprintf_pt_spacing(len, fp);
  1659. }
  1660. static int perf_sample__fprintf_synth_psb(struct perf_sample *sample, FILE *fp)
  1661. {
  1662. struct perf_synth_intel_psb *data = perf_sample__synth_ptr(sample);
  1663. int len;
  1664. if (perf_sample__bad_synth_size(sample, *data))
  1665. return 0;
  1666. len = fprintf(fp, " psb offs: %#" PRIx64, data->offset);
  1667. return len + perf_sample__fprintf_pt_spacing(len, fp);
  1668. }
  1669. /* Intel PT Event Trace */
  1670. static int perf_sample__fprintf_synth_evt(struct perf_sample *sample, FILE *fp)
  1671. {
  1672. struct perf_synth_intel_evt *data = perf_sample__synth_ptr(sample);
  1673. const char *cfe[32] = {NULL, "INTR", "IRET", "SMI", "RSM", "SIPI",
  1674. "INIT", "VMENTRY", "VMEXIT", "VMEXIT_INTR",
  1675. "SHUTDOWN", NULL, "UINTR", "UIRET"};
  1676. const char *evd[64] = {"PFA", "VMXQ", "VMXR"};
  1677. const char *s;
  1678. int len, i;
  1679. if (perf_sample__bad_synth_size(sample, *data))
  1680. return 0;
  1681. s = cfe[data->type];
  1682. if (s) {
  1683. len = fprintf(fp, " cfe: %s IP: %d vector: %u",
  1684. s, data->ip, data->vector);
  1685. } else {
  1686. len = fprintf(fp, " cfe: %u IP: %d vector: %u",
  1687. data->type, data->ip, data->vector);
  1688. }
  1689. for (i = 0; i < data->evd_cnt; i++) {
  1690. unsigned int et = data->evd[i].evd_type & 0x3f;
  1691. s = evd[et];
  1692. if (s) {
  1693. len += fprintf(fp, " %s: %#" PRIx64,
  1694. s, data->evd[i].payload);
  1695. } else {
  1696. len += fprintf(fp, " EVD_%u: %#" PRIx64,
  1697. et, data->evd[i].payload);
  1698. }
  1699. }
  1700. return len + perf_sample__fprintf_pt_spacing(len, fp);
  1701. }
  1702. static int perf_sample__fprintf_synth_iflag_chg(struct perf_sample *sample, FILE *fp)
  1703. {
  1704. struct perf_synth_intel_iflag_chg *data = perf_sample__synth_ptr(sample);
  1705. int len;
  1706. if (perf_sample__bad_synth_size(sample, *data))
  1707. return 0;
  1708. len = fprintf(fp, " IFLAG: %d->%d %s branch", !data->iflag, data->iflag,
  1709. data->via_branch ? "via" : "non");
  1710. return len + perf_sample__fprintf_pt_spacing(len, fp);
  1711. }
  1712. static int perf_sample__fprintf_synth_vpadtl(struct perf_sample *data, FILE *fp)
  1713. {
  1714. struct powerpc_vpadtl_entry *dtl = (struct powerpc_vpadtl_entry *)data->raw_data;
  1715. int len;
  1716. len = fprintf(fp, "timebase: %" PRIu64 " dispatch_reason:%s, preempt_reason:%s,\n"
  1717. "enqueue_to_dispatch_time:%d, ready_to_enqueue_time:%d,"
  1718. "waiting_to_ready_time:%d, processor_id: %d",
  1719. get_unaligned_be64(&dtl->timebase),
  1720. dispatch_reasons[dtl->dispatch_reason],
  1721. preempt_reasons[dtl->preempt_reason],
  1722. be32_to_cpu(dtl->enqueue_to_dispatch_time),
  1723. be32_to_cpu(dtl->ready_to_enqueue_time),
  1724. be32_to_cpu(dtl->waiting_to_ready_time),
  1725. be16_to_cpu(dtl->processor_id));
  1726. return len;
  1727. }
  1728. static int perf_sample__fprintf_synth(struct perf_sample *sample,
  1729. struct evsel *evsel, FILE *fp)
  1730. {
  1731. switch (evsel->core.attr.config) {
  1732. case PERF_SYNTH_INTEL_PTWRITE:
  1733. return perf_sample__fprintf_synth_ptwrite(sample, fp);
  1734. case PERF_SYNTH_INTEL_MWAIT:
  1735. return perf_sample__fprintf_synth_mwait(sample, fp);
  1736. case PERF_SYNTH_INTEL_PWRE:
  1737. return perf_sample__fprintf_synth_pwre(sample, fp);
  1738. case PERF_SYNTH_INTEL_EXSTOP:
  1739. return perf_sample__fprintf_synth_exstop(sample, fp);
  1740. case PERF_SYNTH_INTEL_PWRX:
  1741. return perf_sample__fprintf_synth_pwrx(sample, fp);
  1742. case PERF_SYNTH_INTEL_CBR:
  1743. return perf_sample__fprintf_synth_cbr(sample, fp);
  1744. case PERF_SYNTH_INTEL_PSB:
  1745. return perf_sample__fprintf_synth_psb(sample, fp);
  1746. case PERF_SYNTH_INTEL_EVT:
  1747. return perf_sample__fprintf_synth_evt(sample, fp);
  1748. case PERF_SYNTH_INTEL_IFLAG_CHG:
  1749. return perf_sample__fprintf_synth_iflag_chg(sample, fp);
  1750. case PERF_SYNTH_POWERPC_VPA_DTL:
  1751. return perf_sample__fprintf_synth_vpadtl(sample, fp);
  1752. default:
  1753. break;
  1754. }
  1755. return 0;
  1756. }
  1757. static int evlist__max_name_len(struct evlist *evlist)
  1758. {
  1759. struct evsel *evsel;
  1760. int max = 0;
  1761. evlist__for_each_entry(evlist, evsel) {
  1762. int len = strlen(evsel__name(evsel));
  1763. max = MAX(len, max);
  1764. }
  1765. return max;
  1766. }
  1767. static int data_src__fprintf(u64 data_src, FILE *fp)
  1768. {
  1769. struct mem_info *mi = mem_info__new();
  1770. char decode[100];
  1771. char out[100];
  1772. static int maxlen;
  1773. int len;
  1774. if (!mi)
  1775. return -ENOMEM;
  1776. mem_info__data_src(mi)->val = data_src;
  1777. perf_script__meminfo_scnprintf(decode, 100, mi);
  1778. mem_info__put(mi);
  1779. len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
  1780. if (maxlen < len)
  1781. maxlen = len;
  1782. return fprintf(fp, "%-*s", maxlen, out);
  1783. }
  1784. struct metric_ctx {
  1785. struct perf_sample *sample;
  1786. struct thread *thread;
  1787. struct evsel *evsel;
  1788. FILE *fp;
  1789. };
  1790. static void script_print_metric(struct perf_stat_config *config __maybe_unused,
  1791. void *ctx, enum metric_threshold_classify thresh,
  1792. const char *fmt, const char *unit, double val)
  1793. {
  1794. struct metric_ctx *mctx = ctx;
  1795. const char *color = metric_threshold_classify__color(thresh);
  1796. if (!fmt)
  1797. return;
  1798. perf_sample__fprintf_start(NULL, mctx->sample, mctx->thread, mctx->evsel,
  1799. PERF_RECORD_SAMPLE, mctx->fp);
  1800. fputs("\tmetric: ", mctx->fp);
  1801. if (color)
  1802. color_fprintf(mctx->fp, color, fmt, val);
  1803. else
  1804. printf(fmt, val);
  1805. fprintf(mctx->fp, " %s\n", unit);
  1806. }
  1807. static void script_new_line(struct perf_stat_config *config __maybe_unused,
  1808. void *ctx)
  1809. {
  1810. struct metric_ctx *mctx = ctx;
  1811. perf_sample__fprintf_start(NULL, mctx->sample, mctx->thread, mctx->evsel,
  1812. PERF_RECORD_SAMPLE, mctx->fp);
  1813. fputs("\tmetric: ", mctx->fp);
  1814. }
  1815. struct script_find_metrics_args {
  1816. struct evlist *evlist;
  1817. bool system_wide;
  1818. };
  1819. static struct evsel *map_metric_evsel_to_script_evsel(struct evlist *script_evlist,
  1820. struct evsel *metric_evsel)
  1821. {
  1822. struct evsel *script_evsel;
  1823. evlist__for_each_entry(script_evlist, script_evsel) {
  1824. /* Skip if perf_event_attr differ. */
  1825. if (metric_evsel->core.attr.type != script_evsel->core.attr.type)
  1826. continue;
  1827. if (metric_evsel->core.attr.config != script_evsel->core.attr.config)
  1828. continue;
  1829. /* Skip if the script event has a metric_id that doesn't match. */
  1830. if (script_evsel->metric_id &&
  1831. strcmp(evsel__metric_id(metric_evsel), evsel__metric_id(script_evsel))) {
  1832. pr_debug("Skipping matching evsel due to differing metric ids '%s' vs '%s'\n",
  1833. evsel__metric_id(metric_evsel), evsel__metric_id(script_evsel));
  1834. continue;
  1835. }
  1836. return script_evsel;
  1837. }
  1838. return NULL;
  1839. }
  1840. static int script_find_metrics(const struct pmu_metric *pm,
  1841. const struct pmu_metrics_table *table __maybe_unused,
  1842. void *data)
  1843. {
  1844. struct script_find_metrics_args *args = data;
  1845. struct evlist *script_evlist = args->evlist;
  1846. struct evlist *metric_evlist = evlist__new();
  1847. struct evsel *metric_evsel;
  1848. int ret = metricgroup__parse_groups(metric_evlist,
  1849. /*pmu=*/"all",
  1850. pm->metric_name,
  1851. /*metric_no_group=*/false,
  1852. /*metric_no_merge=*/false,
  1853. /*metric_no_threshold=*/true,
  1854. /*user_requested_cpu_list=*/NULL,
  1855. args->system_wide,
  1856. /*hardware_aware_grouping=*/false);
  1857. if (ret) {
  1858. /* Metric parsing failed but continue the search. */
  1859. goto out;
  1860. }
  1861. /*
  1862. * Check the script_evlist has an entry for each metric_evlist entry. If
  1863. * the script evsel was already set up avoid changing data that may
  1864. * break it.
  1865. */
  1866. evlist__for_each_entry(metric_evlist, metric_evsel) {
  1867. struct evsel *script_evsel =
  1868. map_metric_evsel_to_script_evsel(script_evlist, metric_evsel);
  1869. struct evsel *new_metric_leader;
  1870. if (!script_evsel) {
  1871. pr_debug("Skipping metric '%s' as evsel '%s' / '%s' is missing\n",
  1872. pm->metric_name, evsel__name(metric_evsel),
  1873. evsel__metric_id(metric_evsel));
  1874. goto out;
  1875. }
  1876. if (script_evsel->metric_leader == NULL)
  1877. continue;
  1878. if (metric_evsel->metric_leader == metric_evsel) {
  1879. new_metric_leader = script_evsel;
  1880. } else {
  1881. new_metric_leader =
  1882. map_metric_evsel_to_script_evsel(script_evlist,
  1883. metric_evsel->metric_leader);
  1884. }
  1885. /* Mismatching evsel leaders. */
  1886. if (script_evsel->metric_leader != new_metric_leader) {
  1887. pr_debug("Skipping metric '%s' due to mismatching evsel metric leaders '%s' vs '%s'\n",
  1888. pm->metric_name, evsel__metric_id(metric_evsel),
  1889. evsel__metric_id(script_evsel));
  1890. goto out;
  1891. }
  1892. }
  1893. /*
  1894. * Metric events match those in the script evlist, copy metric evsel
  1895. * data into the script evlist.
  1896. */
  1897. evlist__for_each_entry(metric_evlist, metric_evsel) {
  1898. struct evsel *script_evsel =
  1899. map_metric_evsel_to_script_evsel(script_evlist, metric_evsel);
  1900. struct metric_event *metric_me = metricgroup__lookup(&metric_evlist->metric_events,
  1901. metric_evsel,
  1902. /*create=*/false);
  1903. if (script_evsel->metric_id == NULL) {
  1904. script_evsel->metric_id = metric_evsel->metric_id;
  1905. metric_evsel->metric_id = NULL;
  1906. }
  1907. if (script_evsel->metric_leader == NULL) {
  1908. if (metric_evsel->metric_leader == metric_evsel) {
  1909. script_evsel->metric_leader = script_evsel;
  1910. } else {
  1911. script_evsel->metric_leader =
  1912. map_metric_evsel_to_script_evsel(script_evlist,
  1913. metric_evsel->metric_leader);
  1914. }
  1915. }
  1916. if (metric_me) {
  1917. struct metric_expr *expr;
  1918. struct metric_event *script_me =
  1919. metricgroup__lookup(&script_evlist->metric_events,
  1920. script_evsel,
  1921. /*create=*/true);
  1922. if (!script_me) {
  1923. /*
  1924. * As the metric_expr is created, the only
  1925. * failure is a lack of memory.
  1926. */
  1927. goto out;
  1928. }
  1929. list_splice_init(&metric_me->head, &script_me->head);
  1930. list_for_each_entry(expr, &script_me->head, nd) {
  1931. for (int i = 0; expr->metric_events[i]; i++) {
  1932. expr->metric_events[i] =
  1933. map_metric_evsel_to_script_evsel(script_evlist,
  1934. expr->metric_events[i]);
  1935. }
  1936. }
  1937. }
  1938. }
  1939. pr_debug("Found metric '%s' whose evsels match those of in the perf data\n",
  1940. pm->metric_name);
  1941. evlist__delete(metric_evlist);
  1942. out:
  1943. return 0;
  1944. }
  1945. static struct aggr_cpu_id script_aggr_cpu_id_get(struct perf_stat_config *config __maybe_unused,
  1946. struct perf_cpu cpu)
  1947. {
  1948. return aggr_cpu_id__global(cpu, /*data=*/NULL);
  1949. }
  1950. static void perf_sample__fprint_metric(struct thread *thread,
  1951. struct evsel *evsel,
  1952. struct perf_sample *sample,
  1953. FILE *fp)
  1954. {
  1955. static bool init_metrics;
  1956. struct perf_stat_output_ctx ctx = {
  1957. .print_metric = script_print_metric,
  1958. .new_line = script_new_line,
  1959. .ctx = &(struct metric_ctx) {
  1960. .sample = sample,
  1961. .thread = thread,
  1962. .evsel = evsel,
  1963. .fp = fp,
  1964. },
  1965. .force_header = false,
  1966. };
  1967. struct perf_counts_values *count, *old_count;
  1968. int cpu_map_idx, thread_map_idx, aggr_idx;
  1969. struct evsel *pos;
  1970. if (!init_metrics) {
  1971. /* One time initialization of stat_config and metric data. */
  1972. struct script_find_metrics_args args = {
  1973. .evlist = evsel->evlist,
  1974. .system_wide = perf_thread_map__pid(evsel->core.threads, /*idx=*/0) == -1,
  1975. };
  1976. if (!stat_config.output)
  1977. stat_config.output = stdout;
  1978. if (!stat_config.aggr_map) {
  1979. /* TODO: currently only global aggregation is supported. */
  1980. assert(stat_config.aggr_mode == AGGR_GLOBAL);
  1981. stat_config.aggr_get_id = script_aggr_cpu_id_get;
  1982. stat_config.aggr_map =
  1983. cpu_aggr_map__new(evsel->evlist->core.user_requested_cpus,
  1984. aggr_cpu_id__global, /*data=*/NULL,
  1985. /*needs_sort=*/false);
  1986. }
  1987. metricgroup__for_each_metric(pmu_metrics_table__find(), script_find_metrics, &args);
  1988. init_metrics = true;
  1989. }
  1990. if (!evsel->stats) {
  1991. if (evlist__alloc_stats(&stat_config, evsel->evlist, /*alloc_raw=*/true) < 0)
  1992. return;
  1993. }
  1994. if (!evsel->stats->aggr) {
  1995. if (evlist__alloc_aggr_stats(evsel->evlist, stat_config.aggr_map->nr) < 0)
  1996. return;
  1997. }
  1998. /* Update the evsel's count using the sample's data. */
  1999. cpu_map_idx = perf_cpu_map__idx(evsel->core.cpus, (struct perf_cpu){sample->cpu});
  2000. if (cpu_map_idx < 0) {
  2001. /* Missing CPU, check for any CPU. */
  2002. if (perf_cpu_map__cpu(evsel->core.cpus, /*idx=*/0).cpu == -1 ||
  2003. sample->cpu == (u32)-1) {
  2004. /* Place the counts in the which ever CPU is first in the map. */
  2005. cpu_map_idx = 0;
  2006. } else {
  2007. pr_info("Missing CPU map entry for CPU %d\n", sample->cpu);
  2008. return;
  2009. }
  2010. }
  2011. thread_map_idx = perf_thread_map__idx(evsel->core.threads, sample->tid);
  2012. if (thread_map_idx < 0) {
  2013. /* Missing thread, check for any thread. */
  2014. if (perf_thread_map__pid(evsel->core.threads, /*idx=*/0) == -1 ||
  2015. sample->tid == (u32)-1) {
  2016. /* Place the counts in the which ever thread is first in the map. */
  2017. thread_map_idx = 0;
  2018. } else {
  2019. pr_info("Missing thread map entry for thread %d\n", sample->tid);
  2020. return;
  2021. }
  2022. }
  2023. count = perf_counts(evsel->counts, cpu_map_idx, thread_map_idx);
  2024. old_count = perf_counts(evsel->prev_raw_counts, cpu_map_idx, thread_map_idx);
  2025. count->val = old_count->val + sample->period;
  2026. count->run = old_count->run + 1;
  2027. count->ena = old_count->ena + 1;
  2028. /* Update the aggregated stats. */
  2029. perf_stat_process_counter(&stat_config, evsel);
  2030. /* Display all metrics. */
  2031. evlist__for_each_entry(evsel->evlist, pos) {
  2032. cpu_aggr_map__for_each_idx(aggr_idx, stat_config.aggr_map) {
  2033. perf_stat__print_shadow_stats(&stat_config, pos,
  2034. aggr_idx,
  2035. &ctx);
  2036. }
  2037. }
  2038. }
  2039. static bool show_event(struct perf_sample *sample,
  2040. struct evsel *evsel,
  2041. struct thread *thread,
  2042. struct addr_location *al,
  2043. struct addr_location *addr_al)
  2044. {
  2045. int depth = thread_stack__depth(thread, sample->cpu);
  2046. if (!symbol_conf.graph_function)
  2047. return true;
  2048. if (thread__filter(thread)) {
  2049. if (depth <= thread__filter_entry_depth(thread)) {
  2050. thread__set_filter(thread, false);
  2051. return false;
  2052. }
  2053. return true;
  2054. } else {
  2055. const char *s = symbol_conf.graph_function;
  2056. u64 ip;
  2057. const char *name = resolve_branch_sym(sample, evsel, thread, al, addr_al,
  2058. &ip);
  2059. unsigned nlen;
  2060. if (!name)
  2061. return false;
  2062. nlen = strlen(name);
  2063. while (*s) {
  2064. unsigned len = strcspn(s, ",");
  2065. if (nlen == len && !strncmp(name, s, len)) {
  2066. thread__set_filter(thread, true);
  2067. thread__set_filter_entry_depth(thread, depth);
  2068. return true;
  2069. }
  2070. s += len;
  2071. if (*s == ',')
  2072. s++;
  2073. }
  2074. return false;
  2075. }
  2076. }
  2077. static void process_event(struct perf_script *script,
  2078. struct perf_sample *sample, struct evsel *evsel,
  2079. struct addr_location *al,
  2080. struct addr_location *addr_al,
  2081. struct machine *machine)
  2082. {
  2083. struct thread *thread = al->thread;
  2084. struct perf_event_attr *attr = &evsel->core.attr;
  2085. unsigned int type = evsel__output_type(evsel);
  2086. struct evsel_script *es = evsel->priv;
  2087. FILE *fp = es->fp;
  2088. char str[PAGE_SIZE_NAME_LEN];
  2089. uint32_t e_flags;
  2090. if (output[type].fields == 0)
  2091. return;
  2092. ++es->samples;
  2093. perf_sample__fprintf_start(script, sample, thread, evsel,
  2094. PERF_RECORD_SAMPLE, fp);
  2095. if (PRINT_FIELD(PERIOD))
  2096. fprintf(fp, "%10" PRIu64 " ", sample->period);
  2097. if (PRINT_FIELD(EVNAME)) {
  2098. const char *evname = evsel__name(evsel);
  2099. if (!script->name_width)
  2100. script->name_width = evlist__max_name_len(script->session->evlist);
  2101. fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
  2102. }
  2103. if (print_flags)
  2104. perf_sample__fprintf_flags(sample->flags, fp);
  2105. if (is_bts_event(attr)) {
  2106. perf_sample__fprintf_bts(sample, evsel, thread, al, addr_al, machine, fp);
  2107. return;
  2108. }
  2109. #ifdef HAVE_LIBTRACEEVENT
  2110. if (PRINT_FIELD(TRACE) && sample->raw_data) {
  2111. const struct tep_event *tp_format = evsel__tp_format(evsel);
  2112. if (tp_format) {
  2113. event_format__fprintf(tp_format, sample->cpu,
  2114. sample->raw_data, sample->raw_size,
  2115. fp);
  2116. }
  2117. }
  2118. #endif
  2119. if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
  2120. perf_sample__fprintf_synth(sample, evsel, fp);
  2121. if (PRINT_FIELD(ADDR))
  2122. perf_sample__fprintf_addr(sample, thread, evsel, fp);
  2123. if (PRINT_FIELD(DATA_SRC))
  2124. data_src__fprintf(sample->data_src, fp);
  2125. if (PRINT_FIELD(WEIGHT))
  2126. fprintf(fp, "%16" PRIu64, sample->weight);
  2127. if (PRINT_FIELD(INS_LAT))
  2128. fprintf(fp, "%16" PRIu16, sample->ins_lat);
  2129. if (PRINT_FIELD(RETIRE_LAT))
  2130. fprintf(fp, "%16" PRIu16, sample->weight3);
  2131. if (PRINT_FIELD(CGROUP)) {
  2132. const char *cgrp_name;
  2133. struct cgroup *cgrp = cgroup__find(machine->env,
  2134. sample->cgroup);
  2135. if (cgrp != NULL)
  2136. cgrp_name = cgrp->name;
  2137. else
  2138. cgrp_name = "unknown";
  2139. fprintf(fp, " %s", cgrp_name);
  2140. }
  2141. if (PRINT_FIELD(IP)) {
  2142. struct callchain_cursor *cursor = NULL;
  2143. if (script->stitch_lbr)
  2144. thread__set_lbr_stitch_enable(al->thread, true);
  2145. if (symbol_conf.use_callchain && sample->callchain) {
  2146. cursor = get_tls_callchain_cursor();
  2147. if (thread__resolve_callchain(al->thread, cursor, evsel,
  2148. sample, NULL, NULL,
  2149. scripting_max_stack))
  2150. cursor = NULL;
  2151. }
  2152. fputc(cursor ? '\n' : ' ', fp);
  2153. sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor,
  2154. symbol_conf.bt_stop_list, fp);
  2155. }
  2156. if (PRINT_FIELD(IREGS)) {
  2157. perf_sample__fprintf_iregs(sample, attr,
  2158. thread__e_machine(thread, machine, &e_flags),
  2159. e_flags,
  2160. fp);
  2161. }
  2162. if (PRINT_FIELD(UREGS)) {
  2163. perf_sample__fprintf_uregs(sample, attr,
  2164. thread__e_machine(thread, machine, &e_flags),
  2165. e_flags,
  2166. fp);
  2167. }
  2168. if (PRINT_FIELD(BRSTACK))
  2169. perf_sample__fprintf_brstack(sample, thread, evsel, fp);
  2170. else if (PRINT_FIELD(BRSTACKSYM))
  2171. perf_sample__fprintf_brstacksym(sample, thread, evsel, fp);
  2172. else if (PRINT_FIELD(BRSTACKOFF))
  2173. perf_sample__fprintf_brstackoff(sample, thread, evsel, fp);
  2174. if (evsel__is_bpf_output(evsel) && !evsel__is_offcpu_event(evsel) && PRINT_FIELD(BPF_OUTPUT))
  2175. perf_sample__fprintf_bpf_output(sample, fp);
  2176. perf_sample__fprintf_insn(sample, evsel, attr, thread, machine, fp, al);
  2177. if (PRINT_FIELD(PHYS_ADDR))
  2178. fprintf(fp, "%16" PRIx64, sample->phys_addr);
  2179. if (PRINT_FIELD(DATA_PAGE_SIZE))
  2180. fprintf(fp, " %s", get_page_size_name(sample->data_page_size, str));
  2181. if (PRINT_FIELD(CODE_PAGE_SIZE))
  2182. fprintf(fp, " %s", get_page_size_name(sample->code_page_size, str));
  2183. perf_sample__fprintf_ipc(sample, evsel, fp);
  2184. fprintf(fp, "\n");
  2185. if (PRINT_FIELD(SRCCODE)) {
  2186. if (map__fprintf_srccode(al->map, al->addr, stdout,
  2187. thread__srccode_state(thread)))
  2188. printf("\n");
  2189. }
  2190. if (PRINT_FIELD(METRIC))
  2191. perf_sample__fprint_metric(thread, evsel, sample, fp);
  2192. if (verbose > 0)
  2193. fflush(fp);
  2194. }
  2195. static struct scripting_ops *scripting_ops;
  2196. static void __process_stat(struct evsel *counter, u64 tstamp)
  2197. {
  2198. int nthreads = perf_thread_map__nr(counter->core.threads);
  2199. int idx, thread;
  2200. struct perf_cpu cpu;
  2201. static int header_printed;
  2202. if (!header_printed) {
  2203. printf("%3s %8s %15s %15s %15s %15s %s\n",
  2204. "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
  2205. header_printed = 1;
  2206. }
  2207. for (thread = 0; thread < nthreads; thread++) {
  2208. perf_cpu_map__for_each_cpu(cpu, idx, evsel__cpus(counter)) {
  2209. struct perf_counts_values *counts;
  2210. counts = perf_counts(counter->counts, idx, thread);
  2211. printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
  2212. cpu.cpu,
  2213. perf_thread_map__pid(counter->core.threads, thread),
  2214. counts->val,
  2215. counts->ena,
  2216. counts->run,
  2217. tstamp,
  2218. evsel__name(counter));
  2219. }
  2220. }
  2221. }
  2222. static void process_stat(struct evsel *counter, u64 tstamp)
  2223. {
  2224. if (scripting_ops && scripting_ops->process_stat)
  2225. scripting_ops->process_stat(&stat_config, counter, tstamp);
  2226. else
  2227. __process_stat(counter, tstamp);
  2228. }
  2229. static void process_stat_interval(u64 tstamp)
  2230. {
  2231. if (scripting_ops && scripting_ops->process_stat_interval)
  2232. scripting_ops->process_stat_interval(tstamp);
  2233. }
  2234. static void setup_scripting(void)
  2235. {
  2236. #ifdef HAVE_LIBTRACEEVENT
  2237. setup_perl_scripting();
  2238. #endif
  2239. setup_python_scripting();
  2240. }
  2241. static int flush_scripting(void)
  2242. {
  2243. return scripting_ops ? scripting_ops->flush_script() : 0;
  2244. }
  2245. static int cleanup_scripting(void)
  2246. {
  2247. pr_debug("\nperf script stopped\n");
  2248. return scripting_ops ? scripting_ops->stop_script() : 0;
  2249. }
  2250. static bool filter_cpu(struct perf_sample *sample)
  2251. {
  2252. if (cpu_list && sample->cpu != (u32)-1)
  2253. return !test_bit(sample->cpu, cpu_bitmap);
  2254. return false;
  2255. }
  2256. static int process_sample_event(const struct perf_tool *tool,
  2257. union perf_event *event,
  2258. struct perf_sample *sample,
  2259. struct evsel *evsel,
  2260. struct machine *machine)
  2261. {
  2262. struct perf_script *scr = container_of(tool, struct perf_script, tool);
  2263. struct addr_location al;
  2264. struct addr_location addr_al;
  2265. int ret = 0;
  2266. /* Set thread to NULL to indicate addr_al and al are not initialized */
  2267. addr_location__init(&al);
  2268. addr_location__init(&addr_al);
  2269. ret = dlfilter__filter_event_early(dlfilter, event, sample, evsel, machine, &al, &addr_al);
  2270. if (ret) {
  2271. if (ret > 0)
  2272. ret = 0;
  2273. goto out_put;
  2274. }
  2275. if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
  2276. sample->time)) {
  2277. goto out_put;
  2278. }
  2279. if (debug_mode) {
  2280. if (sample->time < last_timestamp) {
  2281. pr_err("Samples misordered, previous: %" PRIu64
  2282. " this: %" PRIu64 "\n", last_timestamp,
  2283. sample->time);
  2284. nr_unordered++;
  2285. }
  2286. last_timestamp = sample->time;
  2287. goto out_put;
  2288. }
  2289. if (filter_cpu(sample))
  2290. goto out_put;
  2291. if (!al.thread && machine__resolve(machine, &al, sample) < 0) {
  2292. pr_err("problem processing %d event, skipping it.\n",
  2293. event->header.type);
  2294. ret = -1;
  2295. goto out_put;
  2296. }
  2297. if (al.filtered)
  2298. goto out_put;
  2299. if (!show_event(sample, evsel, al.thread, &al, &addr_al))
  2300. goto out_put;
  2301. if (evswitch__discard(&scr->evswitch, evsel))
  2302. goto out_put;
  2303. ret = dlfilter__filter_event(dlfilter, event, sample, evsel, machine, &al, &addr_al);
  2304. if (ret) {
  2305. if (ret > 0)
  2306. ret = 0;
  2307. goto out_put;
  2308. }
  2309. if (scripting_ops) {
  2310. struct addr_location *addr_al_ptr = NULL;
  2311. if ((evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
  2312. sample_addr_correlates_sym(&evsel->core.attr)) {
  2313. if (!addr_al.thread)
  2314. thread__resolve(al.thread, &addr_al, sample);
  2315. addr_al_ptr = &addr_al;
  2316. }
  2317. scripting_ops->process_event(event, sample, evsel, &al, addr_al_ptr);
  2318. } else {
  2319. process_event(scr, sample, evsel, &al, &addr_al, machine);
  2320. }
  2321. out_put:
  2322. addr_location__exit(&addr_al);
  2323. addr_location__exit(&al);
  2324. return ret;
  2325. }
  2326. static int process_deferred_sample_event(const struct perf_tool *tool,
  2327. union perf_event *event,
  2328. struct perf_sample *sample,
  2329. struct evsel *evsel,
  2330. struct machine *machine)
  2331. {
  2332. struct perf_script *scr = container_of(tool, struct perf_script, tool);
  2333. struct perf_event_attr *attr = &evsel->core.attr;
  2334. struct evsel_script *es = evsel->priv;
  2335. unsigned int type = output_type(attr->type);
  2336. struct addr_location al;
  2337. FILE *fp = es->fp;
  2338. int ret = 0;
  2339. if (output[type].fields == 0)
  2340. return 0;
  2341. /* Set thread to NULL to indicate addr_al and al are not initialized */
  2342. addr_location__init(&al);
  2343. if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
  2344. sample->time)) {
  2345. goto out_put;
  2346. }
  2347. if (debug_mode) {
  2348. if (sample->time < last_timestamp) {
  2349. pr_err("Samples misordered, previous: %" PRIu64
  2350. " this: %" PRIu64 "\n", last_timestamp,
  2351. sample->time);
  2352. nr_unordered++;
  2353. }
  2354. last_timestamp = sample->time;
  2355. goto out_put;
  2356. }
  2357. if (filter_cpu(sample))
  2358. goto out_put;
  2359. if (machine__resolve(machine, &al, sample) < 0) {
  2360. pr_err("problem processing %d event, skipping it.\n",
  2361. event->header.type);
  2362. ret = -1;
  2363. goto out_put;
  2364. }
  2365. if (al.filtered)
  2366. goto out_put;
  2367. if (!show_event(sample, evsel, al.thread, &al, NULL))
  2368. goto out_put;
  2369. if (evswitch__discard(&scr->evswitch, evsel))
  2370. goto out_put;
  2371. perf_sample__fprintf_start(scr, sample, al.thread, evsel,
  2372. PERF_RECORD_CALLCHAIN_DEFERRED, fp);
  2373. fprintf(fp, "DEFERRED CALLCHAIN [cookie: %llx]",
  2374. (unsigned long long)event->callchain_deferred.cookie);
  2375. if (PRINT_FIELD(IP)) {
  2376. struct callchain_cursor *cursor = NULL;
  2377. if (symbol_conf.use_callchain && sample->callchain) {
  2378. cursor = get_tls_callchain_cursor();
  2379. if (thread__resolve_callchain(al.thread, cursor, evsel,
  2380. sample, NULL, NULL,
  2381. scripting_max_stack)) {
  2382. pr_info("cannot resolve deferred callchains\n");
  2383. cursor = NULL;
  2384. }
  2385. }
  2386. fputc(cursor ? '\n' : ' ', fp);
  2387. sample__fprintf_sym(sample, &al, 0, output[type].print_ip_opts,
  2388. cursor, symbol_conf.bt_stop_list, fp);
  2389. }
  2390. fprintf(fp, "\n");
  2391. if (verbose > 0)
  2392. fflush(fp);
  2393. out_put:
  2394. addr_location__exit(&al);
  2395. return ret;
  2396. }
  2397. // Used when scr->per_event_dump is not set
  2398. static struct evsel_script es_stdout;
  2399. static int process_attr(const struct perf_tool *tool, union perf_event *event,
  2400. struct evlist **pevlist)
  2401. {
  2402. struct perf_script *scr = container_of(tool, struct perf_script, tool);
  2403. struct evlist *evlist;
  2404. struct evsel *evsel, *pos;
  2405. uint16_t e_machine;
  2406. u64 sample_type;
  2407. int err;
  2408. err = perf_event__process_attr(tool, event, pevlist);
  2409. if (err)
  2410. return err;
  2411. evlist = *pevlist;
  2412. evsel = evlist__last(*pevlist);
  2413. if (!evsel->priv) {
  2414. if (scr->per_event_dump) {
  2415. evsel->priv = evsel_script__new(evsel, scr->session->data);
  2416. if (!evsel->priv)
  2417. return -ENOMEM;
  2418. } else { // Replicate what is done in perf_script__setup_per_event_dump()
  2419. es_stdout.fp = stdout;
  2420. evsel->priv = &es_stdout;
  2421. }
  2422. }
  2423. if (evsel->core.attr.type >= PERF_TYPE_MAX &&
  2424. evsel->core.attr.type != PERF_TYPE_SYNTH)
  2425. return 0;
  2426. evlist__for_each_entry(evlist, pos) {
  2427. if (pos->core.attr.type == evsel->core.attr.type && pos != evsel)
  2428. return 0;
  2429. }
  2430. if (evsel->core.attr.sample_type) {
  2431. err = evsel__check_attr(evsel, scr->session);
  2432. if (err)
  2433. return err;
  2434. }
  2435. /*
  2436. * Check if we need to enable callchains based
  2437. * on events sample_type.
  2438. */
  2439. sample_type = evlist__combined_sample_type(evlist);
  2440. e_machine = perf_session__e_machine(evsel__session(evsel), /*e_flags=*/NULL);
  2441. callchain_param_setup(sample_type, e_machine);
  2442. /* Enable fields for callchain entries */
  2443. if (symbol_conf.use_callchain &&
  2444. (sample_type & PERF_SAMPLE_CALLCHAIN ||
  2445. sample_type & PERF_SAMPLE_BRANCH_STACK ||
  2446. (sample_type & PERF_SAMPLE_REGS_USER &&
  2447. sample_type & PERF_SAMPLE_STACK_USER))) {
  2448. int type = evsel__output_type(evsel);
  2449. if (!(output[type].user_unset_fields & PERF_OUTPUT_IP))
  2450. output[type].fields |= PERF_OUTPUT_IP;
  2451. if (!(output[type].user_unset_fields & PERF_OUTPUT_SYM))
  2452. output[type].fields |= PERF_OUTPUT_SYM;
  2453. }
  2454. evsel__set_print_ip_opts(evsel);
  2455. return 0;
  2456. }
  2457. static int print_event_with_time(const struct perf_tool *tool,
  2458. union perf_event *event,
  2459. struct perf_sample *sample,
  2460. struct machine *machine,
  2461. pid_t pid, pid_t tid, u64 timestamp)
  2462. {
  2463. struct perf_script *script = container_of(tool, struct perf_script, tool);
  2464. struct perf_session *session = script->session;
  2465. struct evsel *evsel = evlist__id2evsel(session->evlist, sample->id);
  2466. struct thread *thread = NULL;
  2467. if (evsel && !evsel->core.attr.sample_id_all) {
  2468. sample->cpu = 0;
  2469. sample->time = timestamp;
  2470. sample->pid = pid;
  2471. sample->tid = tid;
  2472. }
  2473. if (filter_cpu(sample))
  2474. return 0;
  2475. if (tid != -1)
  2476. thread = machine__findnew_thread(machine, pid, tid);
  2477. if (evsel) {
  2478. perf_sample__fprintf_start(script, sample, thread, evsel,
  2479. event->header.type, stdout);
  2480. }
  2481. perf_event__fprintf(event, machine, stdout);
  2482. thread__put(thread);
  2483. return 0;
  2484. }
  2485. static int print_event(const struct perf_tool *tool, union perf_event *event,
  2486. struct perf_sample *sample, struct machine *machine,
  2487. pid_t pid, pid_t tid)
  2488. {
  2489. return print_event_with_time(tool, event, sample, machine, pid, tid, 0);
  2490. }
  2491. static int process_comm_event(const struct perf_tool *tool,
  2492. union perf_event *event,
  2493. struct perf_sample *sample,
  2494. struct machine *machine)
  2495. {
  2496. if (perf_event__process_comm(tool, event, sample, machine) < 0)
  2497. return -1;
  2498. return print_event(tool, event, sample, machine, event->comm.pid,
  2499. event->comm.tid);
  2500. }
  2501. static int process_namespaces_event(const struct perf_tool *tool,
  2502. union perf_event *event,
  2503. struct perf_sample *sample,
  2504. struct machine *machine)
  2505. {
  2506. if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
  2507. return -1;
  2508. return print_event(tool, event, sample, machine, event->namespaces.pid,
  2509. event->namespaces.tid);
  2510. }
  2511. static int process_cgroup_event(const struct perf_tool *tool,
  2512. union perf_event *event,
  2513. struct perf_sample *sample,
  2514. struct machine *machine)
  2515. {
  2516. if (perf_event__process_cgroup(tool, event, sample, machine) < 0)
  2517. return -1;
  2518. return print_event(tool, event, sample, machine, sample->pid,
  2519. sample->tid);
  2520. }
  2521. static int process_fork_event(const struct perf_tool *tool,
  2522. union perf_event *event,
  2523. struct perf_sample *sample,
  2524. struct machine *machine)
  2525. {
  2526. if (perf_event__process_fork(tool, event, sample, machine) < 0)
  2527. return -1;
  2528. return print_event_with_time(tool, event, sample, machine,
  2529. event->fork.pid, event->fork.tid,
  2530. event->fork.time);
  2531. }
  2532. static int process_exit_event(const struct perf_tool *tool,
  2533. union perf_event *event,
  2534. struct perf_sample *sample,
  2535. struct machine *machine)
  2536. {
  2537. /* Print before 'exit' deletes anything */
  2538. if (print_event_with_time(tool, event, sample, machine, event->fork.pid,
  2539. event->fork.tid, event->fork.time))
  2540. return -1;
  2541. return perf_event__process_exit(tool, event, sample, machine);
  2542. }
  2543. static int process_mmap_event(const struct perf_tool *tool,
  2544. union perf_event *event,
  2545. struct perf_sample *sample,
  2546. struct machine *machine)
  2547. {
  2548. if (perf_event__process_mmap(tool, event, sample, machine) < 0)
  2549. return -1;
  2550. return print_event(tool, event, sample, machine, event->mmap.pid,
  2551. event->mmap.tid);
  2552. }
  2553. static int process_mmap2_event(const struct perf_tool *tool,
  2554. union perf_event *event,
  2555. struct perf_sample *sample,
  2556. struct machine *machine)
  2557. {
  2558. if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
  2559. return -1;
  2560. return print_event(tool, event, sample, machine, event->mmap2.pid,
  2561. event->mmap2.tid);
  2562. }
  2563. static int process_switch_event(const struct perf_tool *tool,
  2564. union perf_event *event,
  2565. struct perf_sample *sample,
  2566. struct machine *machine)
  2567. {
  2568. struct perf_script *script = container_of(tool, struct perf_script, tool);
  2569. if (perf_event__process_switch(tool, event, sample, machine) < 0)
  2570. return -1;
  2571. if (scripting_ops && scripting_ops->process_switch && !filter_cpu(sample))
  2572. scripting_ops->process_switch(event, sample, machine);
  2573. if (!script->show_switch_events)
  2574. return 0;
  2575. return print_event(tool, event, sample, machine, sample->pid,
  2576. sample->tid);
  2577. }
  2578. static int process_auxtrace_error(const struct perf_tool *tool,
  2579. struct perf_session *session,
  2580. union perf_event *event)
  2581. {
  2582. if (scripting_ops && scripting_ops->process_auxtrace_error) {
  2583. scripting_ops->process_auxtrace_error(session, event);
  2584. return 0;
  2585. }
  2586. return perf_event__process_auxtrace_error(tool, session, event);
  2587. }
  2588. static int
  2589. process_lost_event(const struct perf_tool *tool,
  2590. union perf_event *event,
  2591. struct perf_sample *sample,
  2592. struct machine *machine)
  2593. {
  2594. return print_event(tool, event, sample, machine, sample->pid,
  2595. sample->tid);
  2596. }
  2597. static int
  2598. process_throttle_event(const struct perf_tool *tool __maybe_unused,
  2599. union perf_event *event,
  2600. struct perf_sample *sample,
  2601. struct machine *machine)
  2602. {
  2603. if (scripting_ops && scripting_ops->process_throttle)
  2604. scripting_ops->process_throttle(event, sample, machine);
  2605. return 0;
  2606. }
  2607. static int
  2608. process_finished_round_event(const struct perf_tool *tool __maybe_unused,
  2609. union perf_event *event,
  2610. struct ordered_events *oe __maybe_unused)
  2611. {
  2612. perf_event__fprintf(event, NULL, stdout);
  2613. return 0;
  2614. }
  2615. static int
  2616. process_bpf_events(const struct perf_tool *tool __maybe_unused,
  2617. union perf_event *event,
  2618. struct perf_sample *sample,
  2619. struct machine *machine)
  2620. {
  2621. if (machine__process_ksymbol(machine, event, sample) < 0)
  2622. return -1;
  2623. return print_event(tool, event, sample, machine, sample->pid,
  2624. sample->tid);
  2625. }
  2626. static int
  2627. process_bpf_metadata_event(const struct perf_tool *tool __maybe_unused,
  2628. struct perf_session *session __maybe_unused,
  2629. union perf_event *event)
  2630. {
  2631. perf_event__fprintf(event, NULL, stdout);
  2632. return 0;
  2633. }
  2634. static int process_text_poke_events(const struct perf_tool *tool,
  2635. union perf_event *event,
  2636. struct perf_sample *sample,
  2637. struct machine *machine)
  2638. {
  2639. if (perf_event__process_text_poke(tool, event, sample, machine) < 0)
  2640. return -1;
  2641. return print_event(tool, event, sample, machine, sample->pid,
  2642. sample->tid);
  2643. }
  2644. static void sig_handler(int sig __maybe_unused)
  2645. {
  2646. session_done = 1;
  2647. }
  2648. static void perf_script__fclose_per_event_dump(struct perf_script *script)
  2649. {
  2650. struct evlist *evlist = script->session->evlist;
  2651. struct evsel *evsel;
  2652. evlist__for_each_entry(evlist, evsel) {
  2653. if (!evsel->priv)
  2654. break;
  2655. evsel_script__delete(evsel->priv);
  2656. evsel->priv = NULL;
  2657. }
  2658. }
  2659. static int perf_script__fopen_per_event_dump(struct perf_script *script)
  2660. {
  2661. struct evsel *evsel;
  2662. evlist__for_each_entry(script->session->evlist, evsel) {
  2663. /*
  2664. * Already setup? I.e. we may be called twice in cases like
  2665. * Intel PT, one for the intel_pt// and dummy events, then
  2666. * for the evsels synthesized from the auxtrace info.
  2667. *
  2668. * Ses perf_script__process_auxtrace_info.
  2669. */
  2670. if (evsel->priv != NULL)
  2671. continue;
  2672. evsel->priv = evsel_script__new(evsel, script->session->data);
  2673. if (evsel->priv == NULL)
  2674. goto out_err_fclose;
  2675. }
  2676. return 0;
  2677. out_err_fclose:
  2678. perf_script__fclose_per_event_dump(script);
  2679. return -1;
  2680. }
  2681. static int perf_script__setup_per_event_dump(struct perf_script *script)
  2682. {
  2683. struct evsel *evsel;
  2684. if (script->per_event_dump)
  2685. return perf_script__fopen_per_event_dump(script);
  2686. es_stdout.fp = stdout;
  2687. evlist__for_each_entry(script->session->evlist, evsel)
  2688. evsel->priv = &es_stdout;
  2689. return 0;
  2690. }
  2691. static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
  2692. {
  2693. struct evsel *evsel;
  2694. evlist__for_each_entry(script->session->evlist, evsel) {
  2695. struct evsel_script *es = evsel->priv;
  2696. evsel_script__fprintf(es, stdout);
  2697. evsel_script__delete(es);
  2698. evsel->priv = NULL;
  2699. }
  2700. }
  2701. static void perf_script__exit(struct perf_script *script)
  2702. {
  2703. perf_thread_map__put(script->threads);
  2704. perf_cpu_map__put(script->cpus);
  2705. }
  2706. static int __cmd_script(struct perf_script *script)
  2707. {
  2708. int ret;
  2709. signal(SIGINT, sig_handler);
  2710. /* override event processing functions */
  2711. if (script->show_task_events) {
  2712. script->tool.comm = process_comm_event;
  2713. script->tool.fork = process_fork_event;
  2714. script->tool.exit = process_exit_event;
  2715. }
  2716. if (script->show_mmap_events) {
  2717. script->tool.mmap = process_mmap_event;
  2718. script->tool.mmap2 = process_mmap2_event;
  2719. }
  2720. if (script->show_switch_events || (scripting_ops && scripting_ops->process_switch))
  2721. script->tool.context_switch = process_switch_event;
  2722. if (scripting_ops && scripting_ops->process_auxtrace_error)
  2723. script->tool.auxtrace_error = process_auxtrace_error;
  2724. if (script->show_namespace_events)
  2725. script->tool.namespaces = process_namespaces_event;
  2726. if (script->show_cgroup_events)
  2727. script->tool.cgroup = process_cgroup_event;
  2728. if (script->show_lost_events)
  2729. script->tool.lost = process_lost_event;
  2730. if (script->show_round_events) {
  2731. script->tool.ordered_events = false;
  2732. script->tool.finished_round = process_finished_round_event;
  2733. }
  2734. if (script->show_bpf_events) {
  2735. script->tool.ksymbol = process_bpf_events;
  2736. script->tool.bpf = process_bpf_events;
  2737. script->tool.bpf_metadata = process_bpf_metadata_event;
  2738. }
  2739. if (script->show_text_poke_events) {
  2740. script->tool.ksymbol = process_bpf_events;
  2741. script->tool.text_poke = process_text_poke_events;
  2742. }
  2743. if (perf_script__setup_per_event_dump(script)) {
  2744. pr_err("Couldn't create the per event dump files\n");
  2745. return -1;
  2746. }
  2747. ret = perf_session__process_events(script->session);
  2748. if (script->per_event_dump)
  2749. perf_script__exit_per_event_dump_stats(script);
  2750. if (debug_mode)
  2751. pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
  2752. return ret;
  2753. }
  2754. static int list_available_languages_cb(struct scripting_ops *ops, const char *spec)
  2755. {
  2756. fprintf(stderr, " %-42s [%s]\n", spec, ops->name);
  2757. return 0;
  2758. }
  2759. static void list_available_languages(void)
  2760. {
  2761. fprintf(stderr, "\n");
  2762. fprintf(stderr, "Scripting language extensions (used in "
  2763. "perf script -s [spec:]script.[spec]):\n\n");
  2764. script_spec__for_each(&list_available_languages_cb);
  2765. fprintf(stderr, "\n");
  2766. }
  2767. /* Find script file relative to current directory or exec path */
  2768. static char *find_script(const char *script)
  2769. {
  2770. char path[PATH_MAX];
  2771. if (!scripting_ops) {
  2772. const char *ext = strrchr(script, '.');
  2773. if (!ext)
  2774. return NULL;
  2775. scripting_ops = script_spec__lookup(++ext);
  2776. if (!scripting_ops)
  2777. return NULL;
  2778. }
  2779. if (access(script, R_OK)) {
  2780. char *exec_path = get_argv_exec_path();
  2781. if (!exec_path)
  2782. return NULL;
  2783. snprintf(path, sizeof(path), "%s/scripts/%s/%s",
  2784. exec_path, scripting_ops->dirname, script);
  2785. free(exec_path);
  2786. script = path;
  2787. if (access(script, R_OK))
  2788. return NULL;
  2789. }
  2790. return strdup(script);
  2791. }
  2792. static int parse_scriptname(const struct option *opt __maybe_unused,
  2793. const char *str, int unset __maybe_unused)
  2794. {
  2795. char spec[PATH_MAX];
  2796. const char *script, *ext;
  2797. int len;
  2798. if (strcmp(str, "lang") == 0) {
  2799. list_available_languages();
  2800. exit(0);
  2801. }
  2802. script = strchr(str, ':');
  2803. if (script) {
  2804. len = script - str;
  2805. if (len >= PATH_MAX) {
  2806. fprintf(stderr, "invalid language specifier");
  2807. return -1;
  2808. }
  2809. strncpy(spec, str, len);
  2810. spec[len] = '\0';
  2811. scripting_ops = script_spec__lookup(spec);
  2812. if (!scripting_ops) {
  2813. fprintf(stderr, "invalid language specifier");
  2814. return -1;
  2815. }
  2816. script++;
  2817. } else {
  2818. script = str;
  2819. ext = strrchr(script, '.');
  2820. if (!ext) {
  2821. fprintf(stderr, "invalid script extension");
  2822. return -1;
  2823. }
  2824. scripting_ops = script_spec__lookup(++ext);
  2825. if (!scripting_ops) {
  2826. fprintf(stderr, "invalid script extension");
  2827. return -1;
  2828. }
  2829. }
  2830. script_name = find_script(script);
  2831. if (!script_name)
  2832. script_name = strdup(script);
  2833. return 0;
  2834. }
  2835. static int parse_output_fields(const struct option *opt __maybe_unused,
  2836. const char *arg, int unset __maybe_unused)
  2837. {
  2838. char *tok, *strtok_saveptr = NULL;
  2839. int i, imax = ARRAY_SIZE(all_output_options);
  2840. int j;
  2841. int rc = 0;
  2842. char *str = strdup(arg);
  2843. int type = -1;
  2844. enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
  2845. if (!str)
  2846. return -ENOMEM;
  2847. /* first word can state for which event type the user is specifying
  2848. * the fields. If no type exists, the specified fields apply to all
  2849. * event types found in the file minus the invalid fields for a type.
  2850. */
  2851. tok = strchr(str, ':');
  2852. if (tok) {
  2853. *tok = '\0';
  2854. tok++;
  2855. if (!strcmp(str, "hw"))
  2856. type = PERF_TYPE_HARDWARE;
  2857. else if (!strcmp(str, "sw"))
  2858. type = PERF_TYPE_SOFTWARE;
  2859. else if (!strcmp(str, "trace"))
  2860. type = PERF_TYPE_TRACEPOINT;
  2861. else if (!strcmp(str, "raw"))
  2862. type = PERF_TYPE_RAW;
  2863. else if (!strcmp(str, "break"))
  2864. type = PERF_TYPE_BREAKPOINT;
  2865. else if (!strcmp(str, "synth"))
  2866. type = OUTPUT_TYPE_SYNTH;
  2867. else {
  2868. fprintf(stderr, "Invalid event type in field string.\n");
  2869. rc = -EINVAL;
  2870. goto out;
  2871. }
  2872. if (output[type].user_set)
  2873. pr_warning("Overriding previous field request for %s events.\n",
  2874. event_type(type));
  2875. /* Don't override defaults for +- */
  2876. if (strchr(tok, '+') || strchr(tok, '-'))
  2877. goto parse;
  2878. output[type].fields = 0;
  2879. output[type].user_set = true;
  2880. output[type].wildcard_set = false;
  2881. } else {
  2882. tok = str;
  2883. if (strlen(str) == 0) {
  2884. fprintf(stderr,
  2885. "Cannot set fields to 'none' for all event types.\n");
  2886. rc = -EINVAL;
  2887. goto out;
  2888. }
  2889. /* Don't override defaults for +- */
  2890. if (strchr(str, '+') || strchr(str, '-'))
  2891. goto parse;
  2892. if (output_set_by_user())
  2893. pr_warning("Overriding previous field request for all events.\n");
  2894. for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
  2895. output[j].fields = 0;
  2896. output[j].user_set = true;
  2897. output[j].wildcard_set = true;
  2898. }
  2899. }
  2900. parse:
  2901. for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
  2902. if (*tok == '+') {
  2903. if (change == SET)
  2904. goto out_badmix;
  2905. change = ADD;
  2906. tok++;
  2907. } else if (*tok == '-') {
  2908. if (change == SET)
  2909. goto out_badmix;
  2910. change = REMOVE;
  2911. tok++;
  2912. } else {
  2913. if (change != SET && change != DEFAULT)
  2914. goto out_badmix;
  2915. change = SET;
  2916. }
  2917. for (i = 0; i < imax; ++i) {
  2918. if (strcmp(tok, all_output_options[i].str) == 0)
  2919. break;
  2920. }
  2921. if (i == imax && strcmp(tok, "flags") == 0) {
  2922. print_flags = change != REMOVE;
  2923. continue;
  2924. }
  2925. if (i == imax) {
  2926. fprintf(stderr, "Invalid field requested.\n");
  2927. rc = -EINVAL;
  2928. goto out;
  2929. }
  2930. #ifndef HAVE_LIBCAPSTONE_SUPPORT
  2931. if (change != REMOVE && strcmp(tok, "disasm") == 0) {
  2932. fprintf(stderr, "Field \"disasm\" requires perf to be built with libcapstone support.\n");
  2933. rc = -EINVAL;
  2934. goto out;
  2935. }
  2936. #endif
  2937. if (type == -1) {
  2938. /* add user option to all events types for
  2939. * which it is valid
  2940. */
  2941. for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
  2942. if (output[j].invalid_fields & all_output_options[i].field) {
  2943. pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
  2944. all_output_options[i].str, event_type(j));
  2945. } else {
  2946. if (change == REMOVE) {
  2947. output[j].fields &= ~all_output_options[i].field;
  2948. output[j].user_set_fields &= ~all_output_options[i].field;
  2949. output[j].user_unset_fields |= all_output_options[i].field;
  2950. } else {
  2951. output[j].fields |= all_output_options[i].field;
  2952. output[j].user_set_fields |= all_output_options[i].field;
  2953. output[j].user_unset_fields &= ~all_output_options[i].field;
  2954. }
  2955. output[j].user_set = true;
  2956. output[j].wildcard_set = true;
  2957. }
  2958. }
  2959. } else {
  2960. if (output[type].invalid_fields & all_output_options[i].field) {
  2961. fprintf(stderr, "\'%s\' not valid for %s events.\n",
  2962. all_output_options[i].str, event_type(type));
  2963. rc = -EINVAL;
  2964. goto out;
  2965. }
  2966. if (change == REMOVE)
  2967. output[type].fields &= ~all_output_options[i].field;
  2968. else
  2969. output[type].fields |= all_output_options[i].field;
  2970. output[type].user_set = true;
  2971. output[type].wildcard_set = true;
  2972. }
  2973. }
  2974. if (type >= 0) {
  2975. if (output[type].fields == 0) {
  2976. pr_debug("No fields requested for %s type. "
  2977. "Events will not be displayed.\n", event_type(type));
  2978. }
  2979. }
  2980. goto out;
  2981. out_badmix:
  2982. fprintf(stderr, "Cannot mix +-field with overridden fields\n");
  2983. rc = -EINVAL;
  2984. out:
  2985. free(str);
  2986. return rc;
  2987. }
  2988. #define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
  2989. while ((lang_dirent = readdir(scripts_dir)) != NULL) \
  2990. if ((lang_dirent->d_type == DT_DIR || \
  2991. (lang_dirent->d_type == DT_UNKNOWN && \
  2992. is_directory(scripts_path, lang_dirent))) && \
  2993. (strcmp(lang_dirent->d_name, ".")) && \
  2994. (strcmp(lang_dirent->d_name, "..")))
  2995. #define for_each_script(lang_path, lang_dir, script_dirent) \
  2996. while ((script_dirent = readdir(lang_dir)) != NULL) \
  2997. if (script_dirent->d_type != DT_DIR && \
  2998. (script_dirent->d_type != DT_UNKNOWN || \
  2999. !is_directory(lang_path, script_dirent)))
  3000. #define RECORD_SUFFIX "-record"
  3001. #define REPORT_SUFFIX "-report"
  3002. struct script_desc {
  3003. struct list_head node;
  3004. char *name;
  3005. char *half_liner;
  3006. char *args;
  3007. };
  3008. static LIST_HEAD(script_descs);
  3009. static struct script_desc *script_desc__new(const char *name)
  3010. {
  3011. struct script_desc *s = zalloc(sizeof(*s));
  3012. if (s != NULL && name)
  3013. s->name = strdup(name);
  3014. return s;
  3015. }
  3016. static void script_desc__delete(struct script_desc *s)
  3017. {
  3018. zfree(&s->name);
  3019. zfree(&s->half_liner);
  3020. zfree(&s->args);
  3021. free(s);
  3022. }
  3023. static void script_desc__add(struct script_desc *s)
  3024. {
  3025. list_add_tail(&s->node, &script_descs);
  3026. }
  3027. static struct script_desc *script_desc__find(const char *name)
  3028. {
  3029. struct script_desc *s;
  3030. list_for_each_entry(s, &script_descs, node)
  3031. if (strcasecmp(s->name, name) == 0)
  3032. return s;
  3033. return NULL;
  3034. }
  3035. static struct script_desc *script_desc__findnew(const char *name)
  3036. {
  3037. struct script_desc *s = script_desc__find(name);
  3038. if (s)
  3039. return s;
  3040. s = script_desc__new(name);
  3041. if (!s)
  3042. return NULL;
  3043. script_desc__add(s);
  3044. return s;
  3045. }
  3046. static const char *ends_with(const char *str, const char *suffix)
  3047. {
  3048. size_t suffix_len = strlen(suffix);
  3049. const char *p = str;
  3050. if (strlen(str) > suffix_len) {
  3051. p = str + strlen(str) - suffix_len;
  3052. if (!strncmp(p, suffix, suffix_len))
  3053. return p;
  3054. }
  3055. return NULL;
  3056. }
  3057. static int read_script_info(struct script_desc *desc, const char *filename)
  3058. {
  3059. char line[BUFSIZ], *p;
  3060. FILE *fp;
  3061. fp = fopen(filename, "r");
  3062. if (!fp)
  3063. return -1;
  3064. while (fgets(line, sizeof(line), fp)) {
  3065. p = skip_spaces(line);
  3066. if (strlen(p) == 0)
  3067. continue;
  3068. if (*p != '#')
  3069. continue;
  3070. p++;
  3071. if (strlen(p) && *p == '!')
  3072. continue;
  3073. p = skip_spaces(p);
  3074. if (strlen(p) && p[strlen(p) - 1] == '\n')
  3075. p[strlen(p) - 1] = '\0';
  3076. if (!strncmp(p, "description:", strlen("description:"))) {
  3077. p += strlen("description:");
  3078. desc->half_liner = strdup(skip_spaces(p));
  3079. continue;
  3080. }
  3081. if (!strncmp(p, "args:", strlen("args:"))) {
  3082. p += strlen("args:");
  3083. desc->args = strdup(skip_spaces(p));
  3084. continue;
  3085. }
  3086. }
  3087. fclose(fp);
  3088. return 0;
  3089. }
  3090. static char *get_script_root(struct dirent *script_dirent, const char *suffix)
  3091. {
  3092. char *script_root, *str;
  3093. script_root = strdup(script_dirent->d_name);
  3094. if (!script_root)
  3095. return NULL;
  3096. str = (char *)ends_with(script_root, suffix);
  3097. if (!str) {
  3098. free(script_root);
  3099. return NULL;
  3100. }
  3101. *str = '\0';
  3102. return script_root;
  3103. }
  3104. static int list_available_scripts(const struct option *opt __maybe_unused,
  3105. const char *s __maybe_unused,
  3106. int unset __maybe_unused)
  3107. {
  3108. struct dirent *script_dirent, *lang_dirent;
  3109. char *buf, *scripts_path, *script_path, *lang_path, *first_half;
  3110. DIR *scripts_dir, *lang_dir;
  3111. struct script_desc *desc;
  3112. char *script_root;
  3113. buf = malloc(3 * MAXPATHLEN + BUFSIZ);
  3114. if (!buf) {
  3115. pr_err("malloc failed\n");
  3116. exit(-1);
  3117. }
  3118. scripts_path = buf;
  3119. script_path = buf + MAXPATHLEN;
  3120. lang_path = buf + 2 * MAXPATHLEN;
  3121. first_half = buf + 3 * MAXPATHLEN;
  3122. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
  3123. scripts_dir = opendir(scripts_path);
  3124. if (!scripts_dir) {
  3125. fprintf(stdout,
  3126. "open(%s) failed.\n"
  3127. "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
  3128. scripts_path);
  3129. free(buf);
  3130. exit(-1);
  3131. }
  3132. for_each_lang(scripts_path, scripts_dir, lang_dirent) {
  3133. scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  3134. lang_dirent->d_name);
  3135. lang_dir = opendir(lang_path);
  3136. if (!lang_dir)
  3137. continue;
  3138. for_each_script(lang_path, lang_dir, script_dirent) {
  3139. script_root = get_script_root(script_dirent, REPORT_SUFFIX);
  3140. if (script_root) {
  3141. desc = script_desc__findnew(script_root);
  3142. scnprintf(script_path, MAXPATHLEN, "%s/%s",
  3143. lang_path, script_dirent->d_name);
  3144. read_script_info(desc, script_path);
  3145. free(script_root);
  3146. }
  3147. }
  3148. }
  3149. fprintf(stdout, "List of available trace scripts:\n");
  3150. list_for_each_entry(desc, &script_descs, node) {
  3151. sprintf(first_half, "%s %s", desc->name,
  3152. desc->args ? desc->args : "");
  3153. fprintf(stdout, " %-36s %s\n", first_half,
  3154. desc->half_liner ? desc->half_liner : "");
  3155. }
  3156. free(buf);
  3157. exit(0);
  3158. }
  3159. static int add_dlarg(const struct option *opt __maybe_unused,
  3160. const char *s, int unset __maybe_unused)
  3161. {
  3162. char *arg = strdup(s);
  3163. void *a;
  3164. if (!arg)
  3165. return -1;
  3166. a = realloc(dlargv, sizeof(dlargv[0]) * (dlargc + 1));
  3167. if (!a) {
  3168. free(arg);
  3169. return -1;
  3170. }
  3171. dlargv = a;
  3172. dlargv[dlargc++] = arg;
  3173. return 0;
  3174. }
  3175. static void free_dlarg(void)
  3176. {
  3177. while (dlargc--)
  3178. free(dlargv[dlargc]);
  3179. free(dlargv);
  3180. }
  3181. static char *get_script_path(const char *script_root, const char *suffix)
  3182. {
  3183. struct dirent *script_dirent, *lang_dirent;
  3184. char scripts_path[MAXPATHLEN];
  3185. char script_path[MAXPATHLEN];
  3186. DIR *scripts_dir, *lang_dir;
  3187. char lang_path[MAXPATHLEN];
  3188. char *__script_root;
  3189. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
  3190. scripts_dir = opendir(scripts_path);
  3191. if (!scripts_dir)
  3192. return NULL;
  3193. for_each_lang(scripts_path, scripts_dir, lang_dirent) {
  3194. scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  3195. lang_dirent->d_name);
  3196. lang_dir = opendir(lang_path);
  3197. if (!lang_dir)
  3198. continue;
  3199. for_each_script(lang_path, lang_dir, script_dirent) {
  3200. __script_root = get_script_root(script_dirent, suffix);
  3201. if (__script_root && !strcmp(script_root, __script_root)) {
  3202. free(__script_root);
  3203. closedir(scripts_dir);
  3204. scnprintf(script_path, MAXPATHLEN, "%s/%s",
  3205. lang_path, script_dirent->d_name);
  3206. closedir(lang_dir);
  3207. return strdup(script_path);
  3208. }
  3209. free(__script_root);
  3210. }
  3211. closedir(lang_dir);
  3212. }
  3213. closedir(scripts_dir);
  3214. return NULL;
  3215. }
  3216. static bool is_top_script(const char *script_path)
  3217. {
  3218. return ends_with(script_path, "top") != NULL;
  3219. }
  3220. static int has_required_arg(char *script_path)
  3221. {
  3222. struct script_desc *desc;
  3223. int n_args = 0;
  3224. char *p;
  3225. desc = script_desc__new(NULL);
  3226. if (read_script_info(desc, script_path))
  3227. goto out;
  3228. if (!desc->args)
  3229. goto out;
  3230. for (p = desc->args; *p; p++)
  3231. if (*p == '<')
  3232. n_args++;
  3233. out:
  3234. script_desc__delete(desc);
  3235. return n_args;
  3236. }
  3237. static int have_cmd(int argc, const char **argv)
  3238. {
  3239. char **__argv = malloc(sizeof(const char *) * argc);
  3240. if (!__argv) {
  3241. pr_err("malloc failed\n");
  3242. return -1;
  3243. }
  3244. memcpy(__argv, argv, sizeof(const char *) * argc);
  3245. argc = parse_options(argc, (const char **)__argv, record_options,
  3246. NULL, PARSE_OPT_STOP_AT_NON_OPTION);
  3247. free(__argv);
  3248. system_wide = (argc == 0);
  3249. return 0;
  3250. }
  3251. static void script__setup_sample_type(struct perf_script *script)
  3252. {
  3253. struct perf_session *session = script->session;
  3254. u64 sample_type = evlist__combined_sample_type(session->evlist);
  3255. callchain_param_setup(sample_type, perf_session__e_machine(session, /*e_flags=*/NULL));
  3256. if (script->stitch_lbr && (callchain_param.record_mode != CALLCHAIN_LBR)) {
  3257. pr_warning("Can't find LBR callchain. Switch off --stitch-lbr.\n"
  3258. "Please apply --call-graph lbr when recording.\n");
  3259. script->stitch_lbr = false;
  3260. }
  3261. }
  3262. static int process_stat_round_event(const struct perf_tool *tool __maybe_unused,
  3263. struct perf_session *session,
  3264. union perf_event *event)
  3265. {
  3266. struct perf_record_stat_round *round = &event->stat_round;
  3267. struct evsel *counter;
  3268. evlist__for_each_entry(session->evlist, counter) {
  3269. perf_stat_process_counter(&stat_config, counter);
  3270. process_stat(counter, round->time);
  3271. }
  3272. process_stat_interval(round->time);
  3273. return 0;
  3274. }
  3275. static int process_stat_config_event(const struct perf_tool *tool __maybe_unused,
  3276. struct perf_session *session __maybe_unused,
  3277. union perf_event *event)
  3278. {
  3279. perf_event__read_stat_config(&stat_config, &event->stat_config);
  3280. /*
  3281. * Aggregation modes are not used since post-processing scripts are
  3282. * supposed to take care of such requirements
  3283. */
  3284. stat_config.aggr_mode = AGGR_NONE;
  3285. return 0;
  3286. }
  3287. static int set_maps(struct perf_script *script)
  3288. {
  3289. struct evlist *evlist = script->session->evlist;
  3290. if (!script->cpus || !script->threads)
  3291. return 0;
  3292. if (WARN_ONCE(script->allocated, "stats double allocation\n"))
  3293. return -EINVAL;
  3294. perf_evlist__set_maps(&evlist->core, script->cpus, script->threads);
  3295. if (evlist__alloc_stats(&stat_config, evlist, /*alloc_raw=*/true))
  3296. return -ENOMEM;
  3297. script->allocated = true;
  3298. return 0;
  3299. }
  3300. static
  3301. int process_thread_map_event(const struct perf_tool *tool,
  3302. struct perf_session *session __maybe_unused,
  3303. union perf_event *event)
  3304. {
  3305. struct perf_script *script = container_of(tool, struct perf_script, tool);
  3306. if (dump_trace)
  3307. perf_event__fprintf_thread_map(event, stdout);
  3308. if (script->threads) {
  3309. pr_warning("Extra thread map event, ignoring.\n");
  3310. return 0;
  3311. }
  3312. script->threads = thread_map__new_event(&event->thread_map);
  3313. if (!script->threads)
  3314. return -ENOMEM;
  3315. return set_maps(script);
  3316. }
  3317. static
  3318. int process_cpu_map_event(const struct perf_tool *tool,
  3319. struct perf_session *session __maybe_unused,
  3320. union perf_event *event)
  3321. {
  3322. struct perf_script *script = container_of(tool, struct perf_script, tool);
  3323. if (dump_trace)
  3324. perf_event__fprintf_cpu_map(event, stdout);
  3325. if (script->cpus) {
  3326. pr_warning("Extra cpu map event, ignoring.\n");
  3327. return 0;
  3328. }
  3329. script->cpus = cpu_map__new_data(&event->cpu_map.data);
  3330. if (!script->cpus)
  3331. return -ENOMEM;
  3332. return set_maps(script);
  3333. }
  3334. static int process_feature_event(const struct perf_tool *tool __maybe_unused,
  3335. struct perf_session *session,
  3336. union perf_event *event)
  3337. {
  3338. if (event->feat.feat_id < HEADER_LAST_FEATURE)
  3339. return perf_event__process_feature(session, event);
  3340. return 0;
  3341. }
  3342. static int perf_script__process_auxtrace_info(const struct perf_tool *tool,
  3343. struct perf_session *session,
  3344. union perf_event *event)
  3345. {
  3346. int ret = perf_event__process_auxtrace_info(tool, session, event);
  3347. if (ret == 0) {
  3348. struct perf_script *script = container_of(tool, struct perf_script, tool);
  3349. ret = perf_script__setup_per_event_dump(script);
  3350. }
  3351. return ret;
  3352. }
  3353. static int parse_insn_trace(const struct option *opt __maybe_unused,
  3354. const char *str, int unset __maybe_unused)
  3355. {
  3356. const char *fields = "+insn,-event,-period";
  3357. int ret;
  3358. if (str) {
  3359. if (strcmp(str, "disasm") == 0)
  3360. fields = "+disasm,-event,-period";
  3361. else if (strlen(str) != 0 && strcmp(str, "raw") != 0) {
  3362. fprintf(stderr, "Only accept raw|disasm\n");
  3363. return -EINVAL;
  3364. }
  3365. }
  3366. ret = parse_output_fields(NULL, fields, 0);
  3367. if (ret < 0)
  3368. return ret;
  3369. itrace_parse_synth_opts(opt, "i0nse", 0);
  3370. symbol_conf.nanosecs = true;
  3371. return 0;
  3372. }
  3373. static int parse_xed(const struct option *opt __maybe_unused,
  3374. const char *str __maybe_unused,
  3375. int unset __maybe_unused)
  3376. {
  3377. if (isatty(1))
  3378. force_pager("xed -F insn: -A -64 | less");
  3379. else
  3380. force_pager("xed -F insn: -A -64");
  3381. return 0;
  3382. }
  3383. static int parse_call_trace(const struct option *opt __maybe_unused,
  3384. const char *str __maybe_unused,
  3385. int unset __maybe_unused)
  3386. {
  3387. parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
  3388. itrace_parse_synth_opts(opt, "cewp", 0);
  3389. symbol_conf.nanosecs = true;
  3390. symbol_conf.pad_output_len_dso = 50;
  3391. return 0;
  3392. }
  3393. static int parse_callret_trace(const struct option *opt __maybe_unused,
  3394. const char *str __maybe_unused,
  3395. int unset __maybe_unused)
  3396. {
  3397. parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
  3398. itrace_parse_synth_opts(opt, "crewp", 0);
  3399. symbol_conf.nanosecs = true;
  3400. return 0;
  3401. }
  3402. int cmd_script(int argc, const char **argv)
  3403. {
  3404. bool show_full_info = false;
  3405. bool header = false;
  3406. bool header_only = false;
  3407. bool script_started = false;
  3408. bool unsorted_dump = false;
  3409. bool merge_deferred_callchains = true;
  3410. char *rec_script_path = NULL;
  3411. char *rep_script_path = NULL;
  3412. struct perf_session *session;
  3413. struct itrace_synth_opts itrace_synth_opts = {
  3414. .set = false,
  3415. .default_no_sample = true,
  3416. };
  3417. char *script_path = NULL;
  3418. const char *dlfilter_file = NULL;
  3419. const char **__argv;
  3420. int i, j, err = 0;
  3421. struct perf_script script = {};
  3422. struct perf_data data = {
  3423. .mode = PERF_DATA_MODE_READ,
  3424. };
  3425. const struct option options[] = {
  3426. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  3427. "dump raw trace in ASCII"),
  3428. OPT_BOOLEAN(0, "dump-unsorted-raw-trace", &unsorted_dump,
  3429. "dump unsorted raw trace in ASCII"),
  3430. OPT_INCR('v', "verbose", &verbose,
  3431. "be more verbose (show symbol address, etc)"),
  3432. OPT_BOOLEAN('L', "Latency", &latency_format,
  3433. "show latency attributes (irqs/preemption disabled, etc)"),
  3434. OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
  3435. list_available_scripts),
  3436. OPT_CALLBACK_NOOPT(0, "list-dlfilters", NULL, NULL, "list available dlfilters",
  3437. list_available_dlfilters),
  3438. OPT_CALLBACK('s', "script", NULL, "name",
  3439. "script file name (lang:script name, script name, or *)",
  3440. parse_scriptname),
  3441. OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
  3442. "generate perf-script.xx script in specified language"),
  3443. OPT_STRING(0, "dlfilter", &dlfilter_file, "file", "filter .so file name"),
  3444. OPT_CALLBACK(0, "dlarg", NULL, "argument", "filter argument",
  3445. add_dlarg),
  3446. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  3447. OPT_BOOLEAN('d', "debug-mode", &debug_mode,
  3448. "do various checks like samples ordering and lost events"),
  3449. OPT_BOOLEAN(0, "header", &header, "Show data header."),
  3450. OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
  3451. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  3452. "file", "vmlinux pathname"),
  3453. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  3454. "file", "kallsyms pathname"),
  3455. OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
  3456. "When printing symbols do not display call chain"),
  3457. OPT_CALLBACK(0, "symfs", NULL, "directory",
  3458. "Look for files with symbols relative to this directory",
  3459. symbol__config_symfs),
  3460. OPT_CALLBACK('F', "fields", NULL, "str",
  3461. "comma separated output fields prepend with 'type:'. "
  3462. "+field to add and -field to remove."
  3463. "Valid types: hw,sw,trace,raw,synth. "
  3464. "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,dsoff,"
  3465. "addr,symoff,srcline,period,iregs,uregs,brstack,"
  3466. "brstacksym,flags,data_src,weight,bpf-output,brstackinsn,"
  3467. "brstackinsnlen,brstackdisasm,brstackoff,callindent,insn,disasm,insnlen,synth,"
  3468. "phys_addr,metric,misc,srccode,ipc,tod,data_page_size,"
  3469. "code_page_size,ins_lat,machine_pid,vcpu,cgroup,retire_lat,"
  3470. "brcntr",
  3471. parse_output_fields),
  3472. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  3473. "system-wide collection from all CPUs"),
  3474. OPT_STRING(0, "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  3475. "only consider symbols in these DSOs"),
  3476. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  3477. "only consider these symbols"),
  3478. OPT_INTEGER(0, "addr-range", &symbol_conf.addr_range,
  3479. "Use with -S to list traced records within address range"),
  3480. OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, "raw|disasm",
  3481. "Decode instructions from itrace", parse_insn_trace),
  3482. OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
  3483. "Run xed disassembler on output", parse_xed),
  3484. OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
  3485. "Decode calls from itrace", parse_call_trace),
  3486. OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
  3487. "Decode calls and returns from itrace", parse_callret_trace),
  3488. OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
  3489. "Only print symbols and callees with --call-trace/--call-ret-trace"),
  3490. OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
  3491. "Stop display of callgraph at these symbols"),
  3492. OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
  3493. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  3494. "only display events for these comms"),
  3495. OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
  3496. "only consider symbols in these pids"),
  3497. OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
  3498. "only consider symbols in these tids"),
  3499. OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
  3500. "Set the maximum stack depth when parsing the callchain, "
  3501. "anything beyond the specified depth will be ignored. "
  3502. "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
  3503. OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
  3504. OPT_BOOLEAN(0, "deltatime", &deltatime, "Show time stamps relative to previous event"),
  3505. OPT_BOOLEAN('I', "show-info", &show_full_info,
  3506. "display extended information from perf.data file"),
  3507. OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
  3508. "Show the path of [kernel.kallsyms]"),
  3509. OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
  3510. "Show the fork/comm/exit events"),
  3511. OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
  3512. "Show the mmap events"),
  3513. OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
  3514. "Show context switch events (if recorded)"),
  3515. OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
  3516. "Show namespace events (if recorded)"),
  3517. OPT_BOOLEAN('\0', "show-cgroup-events", &script.show_cgroup_events,
  3518. "Show cgroup events (if recorded)"),
  3519. OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
  3520. "Show lost events (if recorded)"),
  3521. OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
  3522. "Show round events (if recorded)"),
  3523. OPT_BOOLEAN('\0', "show-bpf-events", &script.show_bpf_events,
  3524. "Show bpf related events (if recorded)"),
  3525. OPT_BOOLEAN('\0', "show-text-poke-events", &script.show_text_poke_events,
  3526. "Show text poke related events (if recorded)"),
  3527. OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
  3528. "Dump trace output to files named by the monitored events"),
  3529. OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
  3530. OPT_INTEGER(0, "max-blocks", &max_blocks,
  3531. "Maximum number of code blocks to dump with brstackinsn"),
  3532. OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs,
  3533. "Use 9 decimal places when displaying time"),
  3534. OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
  3535. "Instruction Tracing options\n" ITRACE_HELP,
  3536. itrace_parse_synth_opts),
  3537. OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
  3538. "Show full source file name path for source lines"),
  3539. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  3540. "Enable symbol demangling"),
  3541. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  3542. "Enable kernel symbol demangling"),
  3543. OPT_STRING(0, "addr2line", &symbol_conf.addr2line_path, "path",
  3544. "addr2line binary to use for line numbers"),
  3545. OPT_STRING(0, "time", &script.time_str, "str",
  3546. "Time span of interest (start,stop)"),
  3547. OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
  3548. "Show inline function"),
  3549. OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
  3550. "guest mount directory under which every guest os"
  3551. " instance has a subdir"),
  3552. OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
  3553. "file", "file saving guest os vmlinux"),
  3554. OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
  3555. "file", "file saving guest os /proc/kallsyms"),
  3556. OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
  3557. "file", "file saving guest os /proc/modules"),
  3558. OPT_BOOLEAN(0, "guest-code", &symbol_conf.guest_code,
  3559. "Guest code can be found in hypervisor process"),
  3560. OPT_BOOLEAN('\0', "stitch-lbr", &script.stitch_lbr,
  3561. "Enable LBR callgraph stitching approach"),
  3562. OPT_BOOLEAN('\0', "merge-callchains", &merge_deferred_callchains,
  3563. "Enable merge deferred user callchains"),
  3564. OPTS_EVSWITCH(&script.evswitch),
  3565. OPT_END()
  3566. };
  3567. const char * const script_subcommands[] = { "record", "report", NULL };
  3568. const char *script_usage[] = {
  3569. "perf script [<options>]",
  3570. "perf script [<options>] record <script> [<record-options>] <command>",
  3571. "perf script [<options>] report <script> [script-args]",
  3572. "perf script [<options>] <script> [<record-options>] <command>",
  3573. "perf script [<options>] <top-script> [script-args]",
  3574. NULL
  3575. };
  3576. struct perf_env *env;
  3577. perf_set_singlethreaded();
  3578. setup_scripting();
  3579. argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
  3580. PARSE_OPT_STOP_AT_NON_OPTION);
  3581. if (symbol_conf.guestmount ||
  3582. symbol_conf.default_guest_vmlinux_name ||
  3583. symbol_conf.default_guest_kallsyms ||
  3584. symbol_conf.default_guest_modules ||
  3585. symbol_conf.guest_code) {
  3586. /*
  3587. * Enable guest sample processing.
  3588. */
  3589. perf_guest = true;
  3590. }
  3591. data.path = input_name;
  3592. data.force = symbol_conf.force;
  3593. if (unsorted_dump)
  3594. dump_trace = true;
  3595. if (symbol__validate_sym_arguments())
  3596. return -1;
  3597. if (argc > 1 && strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
  3598. rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
  3599. if (!rec_script_path)
  3600. return cmd_record(argc, argv);
  3601. }
  3602. if (argc > 1 && strlen(argv[0]) > 2 && strstarts("report", argv[0])) {
  3603. rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
  3604. if (!rep_script_path) {
  3605. fprintf(stderr,
  3606. "Please specify a valid report script"
  3607. "(see 'perf script -l' for listing)\n");
  3608. return -1;
  3609. }
  3610. }
  3611. if (reltime && deltatime) {
  3612. fprintf(stderr,
  3613. "reltime and deltatime - the two don't get along well. "
  3614. "Please limit to --reltime or --deltatime.\n");
  3615. return -1;
  3616. }
  3617. if ((itrace_synth_opts.callchain || itrace_synth_opts.add_callchain) &&
  3618. itrace_synth_opts.callchain_sz > scripting_max_stack)
  3619. scripting_max_stack = itrace_synth_opts.callchain_sz;
  3620. /* make sure PERF_EXEC_PATH is set for scripts */
  3621. set_argv_exec_path(get_argv_exec_path());
  3622. if (argc && !script_name && !rec_script_path && !rep_script_path) {
  3623. int live_pipe[2];
  3624. int rep_args;
  3625. pid_t pid;
  3626. rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
  3627. rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
  3628. if (!rec_script_path && !rep_script_path) {
  3629. script_name = find_script(argv[0]);
  3630. if (script_name) {
  3631. argc -= 1;
  3632. argv += 1;
  3633. goto script_found;
  3634. }
  3635. usage_with_options_msg(script_usage, options,
  3636. "Couldn't find script `%s'\n\n See perf"
  3637. " script -l for available scripts.\n", argv[0]);
  3638. }
  3639. if (is_top_script(argv[0])) {
  3640. rep_args = argc - 1;
  3641. } else {
  3642. int rec_args;
  3643. rep_args = has_required_arg(rep_script_path);
  3644. rec_args = (argc - 1) - rep_args;
  3645. if (rec_args < 0) {
  3646. usage_with_options_msg(script_usage, options,
  3647. "`%s' script requires options."
  3648. "\n\n See perf script -l for available "
  3649. "scripts and options.\n", argv[0]);
  3650. }
  3651. }
  3652. if (pipe(live_pipe) < 0) {
  3653. perror("failed to create pipe");
  3654. return -1;
  3655. }
  3656. pid = fork();
  3657. if (pid < 0) {
  3658. perror("failed to fork");
  3659. return -1;
  3660. }
  3661. if (!pid) {
  3662. j = 0;
  3663. dup2(live_pipe[1], 1);
  3664. close(live_pipe[0]);
  3665. if (is_top_script(argv[0])) {
  3666. system_wide = true;
  3667. } else if (!system_wide) {
  3668. if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
  3669. err = -1;
  3670. goto out;
  3671. }
  3672. }
  3673. __argv = malloc((argc + 6) * sizeof(const char *));
  3674. if (!__argv) {
  3675. pr_err("malloc failed\n");
  3676. err = -ENOMEM;
  3677. goto out;
  3678. }
  3679. __argv[j++] = "/bin/sh";
  3680. __argv[j++] = rec_script_path;
  3681. if (system_wide)
  3682. __argv[j++] = "-a";
  3683. __argv[j++] = "-q";
  3684. __argv[j++] = "-o";
  3685. __argv[j++] = "-";
  3686. for (i = rep_args + 1; i < argc; i++)
  3687. __argv[j++] = argv[i];
  3688. __argv[j++] = NULL;
  3689. execvp("/bin/sh", (char **)__argv);
  3690. free(__argv);
  3691. exit(-1);
  3692. }
  3693. dup2(live_pipe[0], 0);
  3694. close(live_pipe[1]);
  3695. __argv = malloc((argc + 4) * sizeof(const char *));
  3696. if (!__argv) {
  3697. pr_err("malloc failed\n");
  3698. err = -ENOMEM;
  3699. goto out;
  3700. }
  3701. j = 0;
  3702. __argv[j++] = "/bin/sh";
  3703. __argv[j++] = rep_script_path;
  3704. for (i = 1; i < rep_args + 1; i++)
  3705. __argv[j++] = argv[i];
  3706. __argv[j++] = "-i";
  3707. __argv[j++] = "-";
  3708. __argv[j++] = NULL;
  3709. execvp("/bin/sh", (char **)__argv);
  3710. free(__argv);
  3711. exit(-1);
  3712. }
  3713. script_found:
  3714. if (rec_script_path)
  3715. script_path = rec_script_path;
  3716. if (rep_script_path)
  3717. script_path = rep_script_path;
  3718. if (script_path) {
  3719. j = 0;
  3720. if (!rec_script_path)
  3721. system_wide = false;
  3722. else if (!system_wide) {
  3723. if (have_cmd(argc - 1, &argv[1]) != 0) {
  3724. err = -1;
  3725. goto out;
  3726. }
  3727. }
  3728. __argv = malloc((argc + 2) * sizeof(const char *));
  3729. if (!__argv) {
  3730. pr_err("malloc failed\n");
  3731. err = -ENOMEM;
  3732. goto out;
  3733. }
  3734. __argv[j++] = "/bin/sh";
  3735. __argv[j++] = script_path;
  3736. if (system_wide)
  3737. __argv[j++] = "-a";
  3738. for (i = 2; i < argc; i++)
  3739. __argv[j++] = argv[i];
  3740. __argv[j++] = NULL;
  3741. execvp("/bin/sh", (char **)__argv);
  3742. free(__argv);
  3743. exit(-1);
  3744. }
  3745. if (dlfilter_file) {
  3746. dlfilter = dlfilter__new(dlfilter_file, dlargc, dlargv);
  3747. if (!dlfilter)
  3748. return -1;
  3749. }
  3750. if (!script_name) {
  3751. setup_pager();
  3752. use_browser = 0;
  3753. }
  3754. perf_tool__init(&script.tool, !unsorted_dump);
  3755. script.tool.sample = process_sample_event;
  3756. script.tool.callchain_deferred = process_deferred_sample_event;
  3757. script.tool.mmap = perf_event__process_mmap;
  3758. script.tool.mmap2 = perf_event__process_mmap2;
  3759. script.tool.comm = perf_event__process_comm;
  3760. script.tool.namespaces = perf_event__process_namespaces;
  3761. script.tool.cgroup = perf_event__process_cgroup;
  3762. script.tool.exit = perf_event__process_exit;
  3763. script.tool.fork = perf_event__process_fork;
  3764. script.tool.attr = process_attr;
  3765. script.tool.event_update = perf_event__process_event_update;
  3766. #ifdef HAVE_LIBTRACEEVENT
  3767. script.tool.tracing_data = perf_event__process_tracing_data;
  3768. #endif
  3769. script.tool.feature = process_feature_event;
  3770. script.tool.build_id = perf_event__process_build_id;
  3771. script.tool.id_index = perf_event__process_id_index;
  3772. script.tool.auxtrace_info = perf_script__process_auxtrace_info;
  3773. script.tool.auxtrace = perf_event__process_auxtrace;
  3774. script.tool.auxtrace_error = perf_event__process_auxtrace_error;
  3775. script.tool.stat = perf_event__process_stat_event;
  3776. script.tool.stat_round = process_stat_round_event;
  3777. script.tool.stat_config = process_stat_config_event;
  3778. script.tool.thread_map = process_thread_map_event;
  3779. script.tool.cpu_map = process_cpu_map_event;
  3780. script.tool.throttle = process_throttle_event;
  3781. script.tool.unthrottle = process_throttle_event;
  3782. script.tool.ordering_requires_timestamps = true;
  3783. script.tool.merge_deferred_callchains = merge_deferred_callchains;
  3784. session = perf_session__new(&data, &script.tool);
  3785. if (IS_ERR(session))
  3786. return PTR_ERR(session);
  3787. env = perf_session__env(session);
  3788. if (header || header_only) {
  3789. script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
  3790. perf_session__fprintf_info(session, stdout, show_full_info);
  3791. if (header_only)
  3792. goto out_delete;
  3793. }
  3794. if (show_full_info)
  3795. script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
  3796. if (symbol__init(env) < 0)
  3797. goto out_delete;
  3798. script.session = session;
  3799. script__setup_sample_type(&script);
  3800. if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
  3801. symbol_conf.graph_function)
  3802. itrace_synth_opts.thread_stack = true;
  3803. session->itrace_synth_opts = &itrace_synth_opts;
  3804. if (cpu_list) {
  3805. err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
  3806. if (err < 0)
  3807. goto out_delete;
  3808. itrace_synth_opts.cpu_bitmap = cpu_bitmap;
  3809. }
  3810. if (!no_callchain)
  3811. symbol_conf.use_callchain = true;
  3812. else
  3813. symbol_conf.use_callchain = false;
  3814. #ifdef HAVE_LIBTRACEEVENT
  3815. if (session->tevent.pevent &&
  3816. tep_set_function_resolver(session->tevent.pevent,
  3817. machine__resolve_kernel_addr,
  3818. &session->machines.host) < 0) {
  3819. pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
  3820. err = -1;
  3821. goto out_delete;
  3822. }
  3823. #endif
  3824. if (generate_script_lang) {
  3825. struct stat perf_stat;
  3826. int input;
  3827. char *filename = strdup("perf-script");
  3828. if (output_set_by_user()) {
  3829. fprintf(stderr,
  3830. "custom fields not supported for generated scripts");
  3831. err = -EINVAL;
  3832. goto out_delete;
  3833. }
  3834. input = open(data.path, O_RDONLY); /* input_name */
  3835. if (input < 0) {
  3836. err = -errno;
  3837. perror("failed to open file");
  3838. goto out_delete;
  3839. }
  3840. err = fstat(input, &perf_stat);
  3841. if (err < 0) {
  3842. perror("failed to stat file");
  3843. goto out_delete;
  3844. }
  3845. if (!perf_stat.st_size) {
  3846. fprintf(stderr, "zero-sized file, nothing to do!\n");
  3847. goto out_delete;
  3848. }
  3849. scripting_ops = script_spec__lookup(generate_script_lang);
  3850. if (!scripting_ops && ends_with(generate_script_lang, ".py")) {
  3851. scripting_ops = script_spec__lookup("python");
  3852. free(filename);
  3853. filename = strdup(generate_script_lang);
  3854. filename[strlen(filename) - 3] = '\0';
  3855. } else if (!scripting_ops && ends_with(generate_script_lang, ".pl")) {
  3856. scripting_ops = script_spec__lookup("perl");
  3857. free(filename);
  3858. filename = strdup(generate_script_lang);
  3859. filename[strlen(filename) - 3] = '\0';
  3860. }
  3861. if (!scripting_ops) {
  3862. fprintf(stderr, "invalid language specifier '%s'\n", generate_script_lang);
  3863. err = -ENOENT;
  3864. goto out_delete;
  3865. }
  3866. if (!filename) {
  3867. err = -ENOMEM;
  3868. goto out_delete;
  3869. }
  3870. #ifdef HAVE_LIBTRACEEVENT
  3871. err = scripting_ops->generate_script(session->tevent.pevent, filename);
  3872. #else
  3873. err = scripting_ops->generate_script(NULL, filename);
  3874. #endif
  3875. free(filename);
  3876. goto out_delete;
  3877. }
  3878. err = dlfilter__start(dlfilter, session);
  3879. if (err)
  3880. goto out_delete;
  3881. if (script_name) {
  3882. err = scripting_ops->start_script(script_name, argc, argv, session);
  3883. if (err)
  3884. goto out_delete;
  3885. pr_debug("perf script started with script %s\n\n", script_name);
  3886. script_started = true;
  3887. }
  3888. err = perf_session__check_output_opt(session);
  3889. if (err < 0)
  3890. goto out_delete;
  3891. if (script.time_str) {
  3892. err = perf_time__parse_for_ranges_reltime(script.time_str, session,
  3893. &script.ptime_range,
  3894. &script.range_size,
  3895. &script.range_num,
  3896. reltime);
  3897. if (err < 0)
  3898. goto out_delete;
  3899. itrace_synth_opts__set_time_range(&itrace_synth_opts,
  3900. script.ptime_range,
  3901. script.range_num);
  3902. }
  3903. err = evswitch__init(&script.evswitch, session->evlist, stderr);
  3904. if (err)
  3905. goto out_delete;
  3906. if (zstd_init(&(session->zstd_data), 0) < 0)
  3907. pr_warning("Decompression initialization failed. Reported data may be incomplete.\n");
  3908. err = __cmd_script(&script);
  3909. flush_scripting();
  3910. if (verbose > 2 || debug_kmaps)
  3911. perf_session__dump_kmaps(session);
  3912. out_delete:
  3913. if (script.ptime_range) {
  3914. itrace_synth_opts__clear_time_range(&itrace_synth_opts);
  3915. zfree(&script.ptime_range);
  3916. }
  3917. zstd_fini(&(session->zstd_data));
  3918. evlist__free_stats(session->evlist);
  3919. perf_session__delete(session);
  3920. perf_script__exit(&script);
  3921. if (script_started)
  3922. cleanup_scripting();
  3923. dlfilter__cleanup(dlfilter);
  3924. free_dlarg();
  3925. out:
  3926. return err;
  3927. }