builtin-script.c 62 KB

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