builtin-script.c 85 KB

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