builtin-script.c 61 KB

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