builtin-script.c 95 KB

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