builtin-script.c 61 KB

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