builtin-script.c 85 KB

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