builtin-script.c 78 KB

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