builtin-script.c 78 KB

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