builtin-script.c 78 KB

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