builtin-script.c 90 KB

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