builtin-script.c 59 KB

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