builtin-script.c 87 KB

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