builtin-script.c 120 KB

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