builtin-script.c 75 KB

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