builtin-script.c 92 KB

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