builtin-script.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349
  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(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(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 void print_sample_flags(u32 flags)
  509. {
  510. const char *chars = PERF_IP_FLAG_CHARS;
  511. const int n = strlen(PERF_IP_FLAG_CHARS);
  512. char str[33];
  513. int i, pos = 0;
  514. for (i = 0; i < n; i++, flags >>= 1) {
  515. if (flags & 1)
  516. str[pos++] = chars[i];
  517. }
  518. for (; i < 32; i++, flags >>= 1) {
  519. if (flags & 1)
  520. str[pos++] = '?';
  521. }
  522. str[pos] = 0;
  523. printf(" %-4s ", str);
  524. }
  525. struct printer_data {
  526. int line_no;
  527. bool hit_nul;
  528. bool is_printable;
  529. };
  530. static void
  531. print_sample_bpf_output_printer(enum binary_printer_ops op,
  532. unsigned int val,
  533. void *extra)
  534. {
  535. unsigned char ch = (unsigned char)val;
  536. struct printer_data *printer_data = extra;
  537. switch (op) {
  538. case BINARY_PRINT_DATA_BEGIN:
  539. printf("\n");
  540. break;
  541. case BINARY_PRINT_LINE_BEGIN:
  542. printf("%17s", !printer_data->line_no ? "BPF output:" :
  543. " ");
  544. break;
  545. case BINARY_PRINT_ADDR:
  546. printf(" %04x:", val);
  547. break;
  548. case BINARY_PRINT_NUM_DATA:
  549. printf(" %02x", val);
  550. break;
  551. case BINARY_PRINT_NUM_PAD:
  552. printf(" ");
  553. break;
  554. case BINARY_PRINT_SEP:
  555. printf(" ");
  556. break;
  557. case BINARY_PRINT_CHAR_DATA:
  558. if (printer_data->hit_nul && ch)
  559. printer_data->is_printable = false;
  560. if (!isprint(ch)) {
  561. printf("%c", '.');
  562. if (!printer_data->is_printable)
  563. break;
  564. if (ch == '\0')
  565. printer_data->hit_nul = true;
  566. else
  567. printer_data->is_printable = false;
  568. } else {
  569. printf("%c", ch);
  570. }
  571. break;
  572. case BINARY_PRINT_CHAR_PAD:
  573. printf(" ");
  574. break;
  575. case BINARY_PRINT_LINE_END:
  576. printf("\n");
  577. printer_data->line_no++;
  578. break;
  579. case BINARY_PRINT_DATA_END:
  580. default:
  581. break;
  582. }
  583. }
  584. static void print_sample_bpf_output(struct perf_sample *sample)
  585. {
  586. unsigned int nr_bytes = sample->raw_size;
  587. struct printer_data printer_data = {0, false, true};
  588. print_binary(sample->raw_data, nr_bytes, 8,
  589. print_sample_bpf_output_printer, &printer_data);
  590. if (printer_data.is_printable && printer_data.hit_nul)
  591. printf("%17s \"%s\"\n", "BPF string:",
  592. (char *)(sample->raw_data));
  593. }
  594. struct perf_script {
  595. struct perf_tool tool;
  596. struct perf_session *session;
  597. bool show_task_events;
  598. bool show_mmap_events;
  599. bool show_switch_events;
  600. bool allocated;
  601. struct cpu_map *cpus;
  602. struct thread_map *threads;
  603. int name_width;
  604. };
  605. static int perf_evlist__max_name_len(struct perf_evlist *evlist)
  606. {
  607. struct perf_evsel *evsel;
  608. int max = 0;
  609. evlist__for_each(evlist, evsel) {
  610. int len = strlen(perf_evsel__name(evsel));
  611. max = MAX(len, max);
  612. }
  613. return max;
  614. }
  615. static size_t data_src__printf(u64 data_src)
  616. {
  617. struct mem_info mi = { .data_src.val = data_src };
  618. char decode[100];
  619. char out[100];
  620. static int maxlen;
  621. int len;
  622. perf_script__meminfo_scnprintf(decode, 100, &mi);
  623. len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
  624. if (maxlen < len)
  625. maxlen = len;
  626. return printf("%-*s", maxlen, out);
  627. }
  628. static void process_event(struct perf_script *script,
  629. struct perf_sample *sample, struct perf_evsel *evsel,
  630. struct addr_location *al)
  631. {
  632. struct thread *thread = al->thread;
  633. struct perf_event_attr *attr = &evsel->attr;
  634. if (output[attr->type].fields == 0)
  635. return;
  636. print_sample_start(sample, thread, evsel);
  637. if (PRINT_FIELD(PERIOD))
  638. printf("%10" PRIu64 " ", sample->period);
  639. if (PRINT_FIELD(EVNAME)) {
  640. const char *evname = perf_evsel__name(evsel);
  641. if (!script->name_width)
  642. script->name_width = perf_evlist__max_name_len(script->session->evlist);
  643. printf("%*s: ", script->name_width,
  644. evname ? evname : "[unknown]");
  645. }
  646. if (print_flags)
  647. print_sample_flags(sample->flags);
  648. if (is_bts_event(attr)) {
  649. print_sample_bts(sample, evsel, thread, al);
  650. return;
  651. }
  652. if (PRINT_FIELD(TRACE))
  653. event_format__print(evsel->tp_format, sample->cpu,
  654. sample->raw_data, sample->raw_size);
  655. if (PRINT_FIELD(ADDR))
  656. print_sample_addr(sample, thread, attr);
  657. if (PRINT_FIELD(DATA_SRC))
  658. data_src__printf(sample->data_src);
  659. if (PRINT_FIELD(WEIGHT))
  660. printf("%16" PRIu64, sample->weight);
  661. if (PRINT_FIELD(IP)) {
  662. struct callchain_cursor *cursor = NULL;
  663. if (symbol_conf.use_callchain && sample->callchain &&
  664. thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
  665. sample, NULL, NULL, scripting_max_stack) == 0)
  666. cursor = &callchain_cursor;
  667. putchar(cursor ? '\n' : ' ');
  668. sample__fprintf_sym(sample, al, 0, output[attr->type].print_ip_opts, cursor, stdout);
  669. }
  670. if (PRINT_FIELD(IREGS))
  671. print_sample_iregs(sample, attr);
  672. if (PRINT_FIELD(BRSTACK))
  673. print_sample_brstack(sample);
  674. else if (PRINT_FIELD(BRSTACKSYM))
  675. print_sample_brstacksym(sample, thread);
  676. if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
  677. print_sample_bpf_output(sample);
  678. printf("\n");
  679. }
  680. static struct scripting_ops *scripting_ops;
  681. static void __process_stat(struct perf_evsel *counter, u64 tstamp)
  682. {
  683. int nthreads = thread_map__nr(counter->threads);
  684. int ncpus = perf_evsel__nr_cpus(counter);
  685. int cpu, thread;
  686. static int header_printed;
  687. if (counter->system_wide)
  688. nthreads = 1;
  689. if (!header_printed) {
  690. printf("%3s %8s %15s %15s %15s %15s %s\n",
  691. "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
  692. header_printed = 1;
  693. }
  694. for (thread = 0; thread < nthreads; thread++) {
  695. for (cpu = 0; cpu < ncpus; cpu++) {
  696. struct perf_counts_values *counts;
  697. counts = perf_counts(counter->counts, cpu, thread);
  698. printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
  699. counter->cpus->map[cpu],
  700. thread_map__pid(counter->threads, thread),
  701. counts->val,
  702. counts->ena,
  703. counts->run,
  704. tstamp,
  705. perf_evsel__name(counter));
  706. }
  707. }
  708. }
  709. static void process_stat(struct perf_evsel *counter, u64 tstamp)
  710. {
  711. if (scripting_ops && scripting_ops->process_stat)
  712. scripting_ops->process_stat(&stat_config, counter, tstamp);
  713. else
  714. __process_stat(counter, tstamp);
  715. }
  716. static void process_stat_interval(u64 tstamp)
  717. {
  718. if (scripting_ops && scripting_ops->process_stat_interval)
  719. scripting_ops->process_stat_interval(tstamp);
  720. }
  721. static void setup_scripting(void)
  722. {
  723. setup_perl_scripting();
  724. setup_python_scripting();
  725. }
  726. static int flush_scripting(void)
  727. {
  728. return scripting_ops ? scripting_ops->flush_script() : 0;
  729. }
  730. static int cleanup_scripting(void)
  731. {
  732. pr_debug("\nperf script stopped\n");
  733. return scripting_ops ? scripting_ops->stop_script() : 0;
  734. }
  735. static int process_sample_event(struct perf_tool *tool,
  736. union perf_event *event,
  737. struct perf_sample *sample,
  738. struct perf_evsel *evsel,
  739. struct machine *machine)
  740. {
  741. struct perf_script *scr = container_of(tool, struct perf_script, tool);
  742. struct addr_location al;
  743. if (debug_mode) {
  744. if (sample->time < last_timestamp) {
  745. pr_err("Samples misordered, previous: %" PRIu64
  746. " this: %" PRIu64 "\n", last_timestamp,
  747. sample->time);
  748. nr_unordered++;
  749. }
  750. last_timestamp = sample->time;
  751. return 0;
  752. }
  753. if (machine__resolve(machine, &al, sample) < 0) {
  754. pr_err("problem processing %d event, skipping it.\n",
  755. event->header.type);
  756. return -1;
  757. }
  758. if (al.filtered)
  759. goto out_put;
  760. if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
  761. goto out_put;
  762. if (scripting_ops)
  763. scripting_ops->process_event(event, sample, evsel, &al);
  764. else
  765. process_event(scr, sample, evsel, &al);
  766. out_put:
  767. addr_location__put(&al);
  768. return 0;
  769. }
  770. static int process_attr(struct perf_tool *tool, union perf_event *event,
  771. struct perf_evlist **pevlist)
  772. {
  773. struct perf_script *scr = container_of(tool, struct perf_script, tool);
  774. struct perf_evlist *evlist;
  775. struct perf_evsel *evsel, *pos;
  776. int err;
  777. err = perf_event__process_attr(tool, event, pevlist);
  778. if (err)
  779. return err;
  780. evlist = *pevlist;
  781. evsel = perf_evlist__last(*pevlist);
  782. if (evsel->attr.type >= PERF_TYPE_MAX)
  783. return 0;
  784. evlist__for_each(evlist, pos) {
  785. if (pos->attr.type == evsel->attr.type && pos != evsel)
  786. return 0;
  787. }
  788. set_print_ip_opts(&evsel->attr);
  789. if (evsel->attr.sample_type)
  790. err = perf_evsel__check_attr(evsel, scr->session);
  791. return err;
  792. }
  793. static int process_comm_event(struct perf_tool *tool,
  794. union perf_event *event,
  795. struct perf_sample *sample,
  796. struct machine *machine)
  797. {
  798. struct thread *thread;
  799. struct perf_script *script = container_of(tool, struct perf_script, tool);
  800. struct perf_session *session = script->session;
  801. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  802. int ret = -1;
  803. thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
  804. if (thread == NULL) {
  805. pr_debug("problem processing COMM event, skipping it.\n");
  806. return -1;
  807. }
  808. if (perf_event__process_comm(tool, event, sample, machine) < 0)
  809. goto out;
  810. if (!evsel->attr.sample_id_all) {
  811. sample->cpu = 0;
  812. sample->time = 0;
  813. sample->tid = event->comm.tid;
  814. sample->pid = event->comm.pid;
  815. }
  816. print_sample_start(sample, thread, evsel);
  817. perf_event__fprintf(event, stdout);
  818. ret = 0;
  819. out:
  820. thread__put(thread);
  821. return ret;
  822. }
  823. static int process_fork_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. if (perf_event__process_fork(tool, event, sample, machine) < 0)
  833. return -1;
  834. thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
  835. if (thread == NULL) {
  836. pr_debug("problem processing FORK event, skipping it.\n");
  837. return -1;
  838. }
  839. if (!evsel->attr.sample_id_all) {
  840. sample->cpu = 0;
  841. sample->time = event->fork.time;
  842. sample->tid = event->fork.tid;
  843. sample->pid = event->fork.pid;
  844. }
  845. print_sample_start(sample, thread, evsel);
  846. perf_event__fprintf(event, stdout);
  847. thread__put(thread);
  848. return 0;
  849. }
  850. static int process_exit_event(struct perf_tool *tool,
  851. union perf_event *event,
  852. struct perf_sample *sample,
  853. struct machine *machine)
  854. {
  855. int err = 0;
  856. struct thread *thread;
  857. struct perf_script *script = container_of(tool, struct perf_script, tool);
  858. struct perf_session *session = script->session;
  859. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  860. thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
  861. if (thread == NULL) {
  862. pr_debug("problem processing EXIT event, skipping it.\n");
  863. return -1;
  864. }
  865. if (!evsel->attr.sample_id_all) {
  866. sample->cpu = 0;
  867. sample->time = 0;
  868. sample->tid = event->fork.tid;
  869. sample->pid = event->fork.pid;
  870. }
  871. print_sample_start(sample, thread, evsel);
  872. perf_event__fprintf(event, stdout);
  873. if (perf_event__process_exit(tool, event, sample, machine) < 0)
  874. err = -1;
  875. thread__put(thread);
  876. return err;
  877. }
  878. static int process_mmap_event(struct perf_tool *tool,
  879. union perf_event *event,
  880. struct perf_sample *sample,
  881. struct machine *machine)
  882. {
  883. struct thread *thread;
  884. struct perf_script *script = container_of(tool, struct perf_script, tool);
  885. struct perf_session *session = script->session;
  886. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  887. if (perf_event__process_mmap(tool, event, sample, machine) < 0)
  888. return -1;
  889. thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
  890. if (thread == NULL) {
  891. pr_debug("problem processing MMAP event, skipping it.\n");
  892. return -1;
  893. }
  894. if (!evsel->attr.sample_id_all) {
  895. sample->cpu = 0;
  896. sample->time = 0;
  897. sample->tid = event->mmap.tid;
  898. sample->pid = event->mmap.pid;
  899. }
  900. print_sample_start(sample, thread, evsel);
  901. perf_event__fprintf(event, stdout);
  902. thread__put(thread);
  903. return 0;
  904. }
  905. static int process_mmap2_event(struct perf_tool *tool,
  906. union perf_event *event,
  907. struct perf_sample *sample,
  908. struct machine *machine)
  909. {
  910. struct thread *thread;
  911. struct perf_script *script = container_of(tool, struct perf_script, tool);
  912. struct perf_session *session = script->session;
  913. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  914. if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
  915. return -1;
  916. thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
  917. if (thread == NULL) {
  918. pr_debug("problem processing MMAP2 event, skipping it.\n");
  919. return -1;
  920. }
  921. if (!evsel->attr.sample_id_all) {
  922. sample->cpu = 0;
  923. sample->time = 0;
  924. sample->tid = event->mmap2.tid;
  925. sample->pid = event->mmap2.pid;
  926. }
  927. print_sample_start(sample, thread, evsel);
  928. perf_event__fprintf(event, stdout);
  929. thread__put(thread);
  930. return 0;
  931. }
  932. static int process_switch_event(struct perf_tool *tool,
  933. union perf_event *event,
  934. struct perf_sample *sample,
  935. struct machine *machine)
  936. {
  937. struct thread *thread;
  938. struct perf_script *script = container_of(tool, struct perf_script, tool);
  939. struct perf_session *session = script->session;
  940. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  941. if (perf_event__process_switch(tool, event, sample, machine) < 0)
  942. return -1;
  943. thread = machine__findnew_thread(machine, sample->pid,
  944. sample->tid);
  945. if (thread == NULL) {
  946. pr_debug("problem processing SWITCH event, skipping it.\n");
  947. return -1;
  948. }
  949. print_sample_start(sample, thread, evsel);
  950. perf_event__fprintf(event, stdout);
  951. thread__put(thread);
  952. return 0;
  953. }
  954. static void sig_handler(int sig __maybe_unused)
  955. {
  956. session_done = 1;
  957. }
  958. static int __cmd_script(struct perf_script *script)
  959. {
  960. int ret;
  961. signal(SIGINT, sig_handler);
  962. /* override event processing functions */
  963. if (script->show_task_events) {
  964. script->tool.comm = process_comm_event;
  965. script->tool.fork = process_fork_event;
  966. script->tool.exit = process_exit_event;
  967. }
  968. if (script->show_mmap_events) {
  969. script->tool.mmap = process_mmap_event;
  970. script->tool.mmap2 = process_mmap2_event;
  971. }
  972. if (script->show_switch_events)
  973. script->tool.context_switch = process_switch_event;
  974. ret = perf_session__process_events(script->session);
  975. if (debug_mode)
  976. pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
  977. return ret;
  978. }
  979. struct script_spec {
  980. struct list_head node;
  981. struct scripting_ops *ops;
  982. char spec[0];
  983. };
  984. static LIST_HEAD(script_specs);
  985. static struct script_spec *script_spec__new(const char *spec,
  986. struct scripting_ops *ops)
  987. {
  988. struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
  989. if (s != NULL) {
  990. strcpy(s->spec, spec);
  991. s->ops = ops;
  992. }
  993. return s;
  994. }
  995. static void script_spec__add(struct script_spec *s)
  996. {
  997. list_add_tail(&s->node, &script_specs);
  998. }
  999. static struct script_spec *script_spec__find(const char *spec)
  1000. {
  1001. struct script_spec *s;
  1002. list_for_each_entry(s, &script_specs, node)
  1003. if (strcasecmp(s->spec, spec) == 0)
  1004. return s;
  1005. return NULL;
  1006. }
  1007. int script_spec_register(const char *spec, struct scripting_ops *ops)
  1008. {
  1009. struct script_spec *s;
  1010. s = script_spec__find(spec);
  1011. if (s)
  1012. return -1;
  1013. s = script_spec__new(spec, ops);
  1014. if (!s)
  1015. return -1;
  1016. else
  1017. script_spec__add(s);
  1018. return 0;
  1019. }
  1020. static struct scripting_ops *script_spec__lookup(const char *spec)
  1021. {
  1022. struct script_spec *s = script_spec__find(spec);
  1023. if (!s)
  1024. return NULL;
  1025. return s->ops;
  1026. }
  1027. static void list_available_languages(void)
  1028. {
  1029. struct script_spec *s;
  1030. fprintf(stderr, "\n");
  1031. fprintf(stderr, "Scripting language extensions (used in "
  1032. "perf script -s [spec:]script.[spec]):\n\n");
  1033. list_for_each_entry(s, &script_specs, node)
  1034. fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
  1035. fprintf(stderr, "\n");
  1036. }
  1037. static int parse_scriptname(const struct option *opt __maybe_unused,
  1038. const char *str, int unset __maybe_unused)
  1039. {
  1040. char spec[PATH_MAX];
  1041. const char *script, *ext;
  1042. int len;
  1043. if (strcmp(str, "lang") == 0) {
  1044. list_available_languages();
  1045. exit(0);
  1046. }
  1047. script = strchr(str, ':');
  1048. if (script) {
  1049. len = script - str;
  1050. if (len >= PATH_MAX) {
  1051. fprintf(stderr, "invalid language specifier");
  1052. return -1;
  1053. }
  1054. strncpy(spec, str, len);
  1055. spec[len] = '\0';
  1056. scripting_ops = script_spec__lookup(spec);
  1057. if (!scripting_ops) {
  1058. fprintf(stderr, "invalid language specifier");
  1059. return -1;
  1060. }
  1061. script++;
  1062. } else {
  1063. script = str;
  1064. ext = strrchr(script, '.');
  1065. if (!ext) {
  1066. fprintf(stderr, "invalid script extension");
  1067. return -1;
  1068. }
  1069. scripting_ops = script_spec__lookup(++ext);
  1070. if (!scripting_ops) {
  1071. fprintf(stderr, "invalid script extension");
  1072. return -1;
  1073. }
  1074. }
  1075. script_name = strdup(script);
  1076. return 0;
  1077. }
  1078. static int parse_output_fields(const struct option *opt __maybe_unused,
  1079. const char *arg, int unset __maybe_unused)
  1080. {
  1081. char *tok;
  1082. int i, imax = ARRAY_SIZE(all_output_options);
  1083. int j;
  1084. int rc = 0;
  1085. char *str = strdup(arg);
  1086. int type = -1;
  1087. if (!str)
  1088. return -ENOMEM;
  1089. /* first word can state for which event type the user is specifying
  1090. * the fields. If no type exists, the specified fields apply to all
  1091. * event types found in the file minus the invalid fields for a type.
  1092. */
  1093. tok = strchr(str, ':');
  1094. if (tok) {
  1095. *tok = '\0';
  1096. tok++;
  1097. if (!strcmp(str, "hw"))
  1098. type = PERF_TYPE_HARDWARE;
  1099. else if (!strcmp(str, "sw"))
  1100. type = PERF_TYPE_SOFTWARE;
  1101. else if (!strcmp(str, "trace"))
  1102. type = PERF_TYPE_TRACEPOINT;
  1103. else if (!strcmp(str, "raw"))
  1104. type = PERF_TYPE_RAW;
  1105. else if (!strcmp(str, "break"))
  1106. type = PERF_TYPE_BREAKPOINT;
  1107. else {
  1108. fprintf(stderr, "Invalid event type in field string.\n");
  1109. rc = -EINVAL;
  1110. goto out;
  1111. }
  1112. if (output[type].user_set)
  1113. pr_warning("Overriding previous field request for %s events.\n",
  1114. event_type(type));
  1115. output[type].fields = 0;
  1116. output[type].user_set = true;
  1117. output[type].wildcard_set = false;
  1118. } else {
  1119. tok = str;
  1120. if (strlen(str) == 0) {
  1121. fprintf(stderr,
  1122. "Cannot set fields to 'none' for all event types.\n");
  1123. rc = -EINVAL;
  1124. goto out;
  1125. }
  1126. if (output_set_by_user())
  1127. pr_warning("Overriding previous field request for all events.\n");
  1128. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  1129. output[j].fields = 0;
  1130. output[j].user_set = true;
  1131. output[j].wildcard_set = true;
  1132. }
  1133. }
  1134. for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
  1135. for (i = 0; i < imax; ++i) {
  1136. if (strcmp(tok, all_output_options[i].str) == 0)
  1137. break;
  1138. }
  1139. if (i == imax && strcmp(tok, "flags") == 0) {
  1140. print_flags = true;
  1141. continue;
  1142. }
  1143. if (i == imax) {
  1144. fprintf(stderr, "Invalid field requested.\n");
  1145. rc = -EINVAL;
  1146. goto out;
  1147. }
  1148. if (type == -1) {
  1149. /* add user option to all events types for
  1150. * which it is valid
  1151. */
  1152. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  1153. if (output[j].invalid_fields & all_output_options[i].field) {
  1154. pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
  1155. all_output_options[i].str, event_type(j));
  1156. } else
  1157. output[j].fields |= all_output_options[i].field;
  1158. }
  1159. } else {
  1160. if (output[type].invalid_fields & all_output_options[i].field) {
  1161. fprintf(stderr, "\'%s\' not valid for %s events.\n",
  1162. all_output_options[i].str, event_type(type));
  1163. rc = -EINVAL;
  1164. goto out;
  1165. }
  1166. output[type].fields |= all_output_options[i].field;
  1167. }
  1168. }
  1169. if (type >= 0) {
  1170. if (output[type].fields == 0) {
  1171. pr_debug("No fields requested for %s type. "
  1172. "Events will not be displayed.\n", event_type(type));
  1173. }
  1174. }
  1175. out:
  1176. free(str);
  1177. return rc;
  1178. }
  1179. /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
  1180. static int is_directory(const char *base_path, const struct dirent *dent)
  1181. {
  1182. char path[PATH_MAX];
  1183. struct stat st;
  1184. sprintf(path, "%s/%s", base_path, dent->d_name);
  1185. if (stat(path, &st))
  1186. return 0;
  1187. return S_ISDIR(st.st_mode);
  1188. }
  1189. #define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
  1190. while ((lang_dirent = readdir(scripts_dir)) != NULL) \
  1191. if ((lang_dirent->d_type == DT_DIR || \
  1192. (lang_dirent->d_type == DT_UNKNOWN && \
  1193. is_directory(scripts_path, lang_dirent))) && \
  1194. (strcmp(lang_dirent->d_name, ".")) && \
  1195. (strcmp(lang_dirent->d_name, "..")))
  1196. #define for_each_script(lang_path, lang_dir, script_dirent) \
  1197. while ((script_dirent = readdir(lang_dir)) != NULL) \
  1198. if (script_dirent->d_type != DT_DIR && \
  1199. (script_dirent->d_type != DT_UNKNOWN || \
  1200. !is_directory(lang_path, script_dirent)))
  1201. #define RECORD_SUFFIX "-record"
  1202. #define REPORT_SUFFIX "-report"
  1203. struct script_desc {
  1204. struct list_head node;
  1205. char *name;
  1206. char *half_liner;
  1207. char *args;
  1208. };
  1209. static LIST_HEAD(script_descs);
  1210. static struct script_desc *script_desc__new(const char *name)
  1211. {
  1212. struct script_desc *s = zalloc(sizeof(*s));
  1213. if (s != NULL && name)
  1214. s->name = strdup(name);
  1215. return s;
  1216. }
  1217. static void script_desc__delete(struct script_desc *s)
  1218. {
  1219. zfree(&s->name);
  1220. zfree(&s->half_liner);
  1221. zfree(&s->args);
  1222. free(s);
  1223. }
  1224. static void script_desc__add(struct script_desc *s)
  1225. {
  1226. list_add_tail(&s->node, &script_descs);
  1227. }
  1228. static struct script_desc *script_desc__find(const char *name)
  1229. {
  1230. struct script_desc *s;
  1231. list_for_each_entry(s, &script_descs, node)
  1232. if (strcasecmp(s->name, name) == 0)
  1233. return s;
  1234. return NULL;
  1235. }
  1236. static struct script_desc *script_desc__findnew(const char *name)
  1237. {
  1238. struct script_desc *s = script_desc__find(name);
  1239. if (s)
  1240. return s;
  1241. s = script_desc__new(name);
  1242. if (!s)
  1243. goto out_delete_desc;
  1244. script_desc__add(s);
  1245. return s;
  1246. out_delete_desc:
  1247. script_desc__delete(s);
  1248. return NULL;
  1249. }
  1250. static const char *ends_with(const char *str, const char *suffix)
  1251. {
  1252. size_t suffix_len = strlen(suffix);
  1253. const char *p = str;
  1254. if (strlen(str) > suffix_len) {
  1255. p = str + strlen(str) - suffix_len;
  1256. if (!strncmp(p, suffix, suffix_len))
  1257. return p;
  1258. }
  1259. return NULL;
  1260. }
  1261. static int read_script_info(struct script_desc *desc, const char *filename)
  1262. {
  1263. char line[BUFSIZ], *p;
  1264. FILE *fp;
  1265. fp = fopen(filename, "r");
  1266. if (!fp)
  1267. return -1;
  1268. while (fgets(line, sizeof(line), fp)) {
  1269. p = ltrim(line);
  1270. if (strlen(p) == 0)
  1271. continue;
  1272. if (*p != '#')
  1273. continue;
  1274. p++;
  1275. if (strlen(p) && *p == '!')
  1276. continue;
  1277. p = ltrim(p);
  1278. if (strlen(p) && p[strlen(p) - 1] == '\n')
  1279. p[strlen(p) - 1] = '\0';
  1280. if (!strncmp(p, "description:", strlen("description:"))) {
  1281. p += strlen("description:");
  1282. desc->half_liner = strdup(ltrim(p));
  1283. continue;
  1284. }
  1285. if (!strncmp(p, "args:", strlen("args:"))) {
  1286. p += strlen("args:");
  1287. desc->args = strdup(ltrim(p));
  1288. continue;
  1289. }
  1290. }
  1291. fclose(fp);
  1292. return 0;
  1293. }
  1294. static char *get_script_root(struct dirent *script_dirent, const char *suffix)
  1295. {
  1296. char *script_root, *str;
  1297. script_root = strdup(script_dirent->d_name);
  1298. if (!script_root)
  1299. return NULL;
  1300. str = (char *)ends_with(script_root, suffix);
  1301. if (!str) {
  1302. free(script_root);
  1303. return NULL;
  1304. }
  1305. *str = '\0';
  1306. return script_root;
  1307. }
  1308. static int list_available_scripts(const struct option *opt __maybe_unused,
  1309. const char *s __maybe_unused,
  1310. int unset __maybe_unused)
  1311. {
  1312. struct dirent *script_dirent, *lang_dirent;
  1313. char scripts_path[MAXPATHLEN];
  1314. DIR *scripts_dir, *lang_dir;
  1315. char script_path[MAXPATHLEN];
  1316. char lang_path[MAXPATHLEN];
  1317. struct script_desc *desc;
  1318. char first_half[BUFSIZ];
  1319. char *script_root;
  1320. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
  1321. scripts_dir = opendir(scripts_path);
  1322. if (!scripts_dir)
  1323. return -1;
  1324. for_each_lang(scripts_path, scripts_dir, lang_dirent) {
  1325. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  1326. lang_dirent->d_name);
  1327. lang_dir = opendir(lang_path);
  1328. if (!lang_dir)
  1329. continue;
  1330. for_each_script(lang_path, lang_dir, script_dirent) {
  1331. script_root = get_script_root(script_dirent, REPORT_SUFFIX);
  1332. if (script_root) {
  1333. desc = script_desc__findnew(script_root);
  1334. snprintf(script_path, MAXPATHLEN, "%s/%s",
  1335. lang_path, script_dirent->d_name);
  1336. read_script_info(desc, script_path);
  1337. free(script_root);
  1338. }
  1339. }
  1340. }
  1341. fprintf(stdout, "List of available trace scripts:\n");
  1342. list_for_each_entry(desc, &script_descs, node) {
  1343. sprintf(first_half, "%s %s", desc->name,
  1344. desc->args ? desc->args : "");
  1345. fprintf(stdout, " %-36s %s\n", first_half,
  1346. desc->half_liner ? desc->half_liner : "");
  1347. }
  1348. exit(0);
  1349. }
  1350. /*
  1351. * Some scripts specify the required events in their "xxx-record" file,
  1352. * this function will check if the events in perf.data match those
  1353. * mentioned in the "xxx-record".
  1354. *
  1355. * Fixme: All existing "xxx-record" are all in good formats "-e event ",
  1356. * which is covered well now. And new parsing code should be added to
  1357. * cover the future complexing formats like event groups etc.
  1358. */
  1359. static int check_ev_match(char *dir_name, char *scriptname,
  1360. struct perf_session *session)
  1361. {
  1362. char filename[MAXPATHLEN], evname[128];
  1363. char line[BUFSIZ], *p;
  1364. struct perf_evsel *pos;
  1365. int match, len;
  1366. FILE *fp;
  1367. sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
  1368. fp = fopen(filename, "r");
  1369. if (!fp)
  1370. return -1;
  1371. while (fgets(line, sizeof(line), fp)) {
  1372. p = ltrim(line);
  1373. if (*p == '#')
  1374. continue;
  1375. while (strlen(p)) {
  1376. p = strstr(p, "-e");
  1377. if (!p)
  1378. break;
  1379. p += 2;
  1380. p = ltrim(p);
  1381. len = strcspn(p, " \t");
  1382. if (!len)
  1383. break;
  1384. snprintf(evname, len + 1, "%s", p);
  1385. match = 0;
  1386. evlist__for_each(session->evlist, pos) {
  1387. if (!strcmp(perf_evsel__name(pos), evname)) {
  1388. match = 1;
  1389. break;
  1390. }
  1391. }
  1392. if (!match) {
  1393. fclose(fp);
  1394. return -1;
  1395. }
  1396. }
  1397. }
  1398. fclose(fp);
  1399. return 0;
  1400. }
  1401. /*
  1402. * Return -1 if none is found, otherwise the actual scripts number.
  1403. *
  1404. * Currently the only user of this function is the script browser, which
  1405. * will list all statically runnable scripts, select one, execute it and
  1406. * show the output in a perf browser.
  1407. */
  1408. int find_scripts(char **scripts_array, char **scripts_path_array)
  1409. {
  1410. struct dirent *script_dirent, *lang_dirent;
  1411. char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
  1412. DIR *scripts_dir, *lang_dir;
  1413. struct perf_session *session;
  1414. struct perf_data_file file = {
  1415. .path = input_name,
  1416. .mode = PERF_DATA_MODE_READ,
  1417. };
  1418. char *temp;
  1419. int i = 0;
  1420. session = perf_session__new(&file, false, NULL);
  1421. if (!session)
  1422. return -1;
  1423. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
  1424. scripts_dir = opendir(scripts_path);
  1425. if (!scripts_dir) {
  1426. perf_session__delete(session);
  1427. return -1;
  1428. }
  1429. for_each_lang(scripts_path, scripts_dir, lang_dirent) {
  1430. snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
  1431. lang_dirent->d_name);
  1432. #ifdef NO_LIBPERL
  1433. if (strstr(lang_path, "perl"))
  1434. continue;
  1435. #endif
  1436. #ifdef NO_LIBPYTHON
  1437. if (strstr(lang_path, "python"))
  1438. continue;
  1439. #endif
  1440. lang_dir = opendir(lang_path);
  1441. if (!lang_dir)
  1442. continue;
  1443. for_each_script(lang_path, lang_dir, script_dirent) {
  1444. /* Skip those real time scripts: xxxtop.p[yl] */
  1445. if (strstr(script_dirent->d_name, "top."))
  1446. continue;
  1447. sprintf(scripts_path_array[i], "%s/%s", lang_path,
  1448. script_dirent->d_name);
  1449. temp = strchr(script_dirent->d_name, '.');
  1450. snprintf(scripts_array[i],
  1451. (temp - script_dirent->d_name) + 1,
  1452. "%s", script_dirent->d_name);
  1453. if (check_ev_match(lang_path,
  1454. scripts_array[i], session))
  1455. continue;
  1456. i++;
  1457. }
  1458. closedir(lang_dir);
  1459. }
  1460. closedir(scripts_dir);
  1461. perf_session__delete(session);
  1462. return i;
  1463. }
  1464. static char *get_script_path(const char *script_root, const char *suffix)
  1465. {
  1466. struct dirent *script_dirent, *lang_dirent;
  1467. char scripts_path[MAXPATHLEN];
  1468. char script_path[MAXPATHLEN];
  1469. DIR *scripts_dir, *lang_dir;
  1470. char lang_path[MAXPATHLEN];
  1471. char *__script_root;
  1472. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
  1473. scripts_dir = opendir(scripts_path);
  1474. if (!scripts_dir)
  1475. return NULL;
  1476. for_each_lang(scripts_path, scripts_dir, lang_dirent) {
  1477. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  1478. lang_dirent->d_name);
  1479. lang_dir = opendir(lang_path);
  1480. if (!lang_dir)
  1481. continue;
  1482. for_each_script(lang_path, lang_dir, script_dirent) {
  1483. __script_root = get_script_root(script_dirent, suffix);
  1484. if (__script_root && !strcmp(script_root, __script_root)) {
  1485. free(__script_root);
  1486. closedir(lang_dir);
  1487. closedir(scripts_dir);
  1488. snprintf(script_path, MAXPATHLEN, "%s/%s",
  1489. lang_path, script_dirent->d_name);
  1490. return strdup(script_path);
  1491. }
  1492. free(__script_root);
  1493. }
  1494. closedir(lang_dir);
  1495. }
  1496. closedir(scripts_dir);
  1497. return NULL;
  1498. }
  1499. static bool is_top_script(const char *script_path)
  1500. {
  1501. return ends_with(script_path, "top") == NULL ? false : true;
  1502. }
  1503. static int has_required_arg(char *script_path)
  1504. {
  1505. struct script_desc *desc;
  1506. int n_args = 0;
  1507. char *p;
  1508. desc = script_desc__new(NULL);
  1509. if (read_script_info(desc, script_path))
  1510. goto out;
  1511. if (!desc->args)
  1512. goto out;
  1513. for (p = desc->args; *p; p++)
  1514. if (*p == '<')
  1515. n_args++;
  1516. out:
  1517. script_desc__delete(desc);
  1518. return n_args;
  1519. }
  1520. static int have_cmd(int argc, const char **argv)
  1521. {
  1522. char **__argv = malloc(sizeof(const char *) * argc);
  1523. if (!__argv) {
  1524. pr_err("malloc failed\n");
  1525. return -1;
  1526. }
  1527. memcpy(__argv, argv, sizeof(const char *) * argc);
  1528. argc = parse_options(argc, (const char **)__argv, record_options,
  1529. NULL, PARSE_OPT_STOP_AT_NON_OPTION);
  1530. free(__argv);
  1531. system_wide = (argc == 0);
  1532. return 0;
  1533. }
  1534. static void script__setup_sample_type(struct perf_script *script)
  1535. {
  1536. struct perf_session *session = script->session;
  1537. u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
  1538. if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
  1539. if ((sample_type & PERF_SAMPLE_REGS_USER) &&
  1540. (sample_type & PERF_SAMPLE_STACK_USER))
  1541. callchain_param.record_mode = CALLCHAIN_DWARF;
  1542. else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
  1543. callchain_param.record_mode = CALLCHAIN_LBR;
  1544. else
  1545. callchain_param.record_mode = CALLCHAIN_FP;
  1546. }
  1547. }
  1548. static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
  1549. union perf_event *event,
  1550. struct perf_session *session)
  1551. {
  1552. struct stat_round_event *round = &event->stat_round;
  1553. struct perf_evsel *counter;
  1554. evlist__for_each(session->evlist, counter) {
  1555. perf_stat_process_counter(&stat_config, counter);
  1556. process_stat(counter, round->time);
  1557. }
  1558. process_stat_interval(round->time);
  1559. return 0;
  1560. }
  1561. static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
  1562. union perf_event *event,
  1563. struct perf_session *session __maybe_unused)
  1564. {
  1565. perf_event__read_stat_config(&stat_config, &event->stat_config);
  1566. return 0;
  1567. }
  1568. static int set_maps(struct perf_script *script)
  1569. {
  1570. struct perf_evlist *evlist = script->session->evlist;
  1571. if (!script->cpus || !script->threads)
  1572. return 0;
  1573. if (WARN_ONCE(script->allocated, "stats double allocation\n"))
  1574. return -EINVAL;
  1575. perf_evlist__set_maps(evlist, script->cpus, script->threads);
  1576. if (perf_evlist__alloc_stats(evlist, true))
  1577. return -ENOMEM;
  1578. script->allocated = true;
  1579. return 0;
  1580. }
  1581. static
  1582. int process_thread_map_event(struct perf_tool *tool,
  1583. union perf_event *event,
  1584. struct perf_session *session __maybe_unused)
  1585. {
  1586. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1587. if (script->threads) {
  1588. pr_warning("Extra thread map event, ignoring.\n");
  1589. return 0;
  1590. }
  1591. script->threads = thread_map__new_event(&event->thread_map);
  1592. if (!script->threads)
  1593. return -ENOMEM;
  1594. return set_maps(script);
  1595. }
  1596. static
  1597. int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
  1598. union perf_event *event,
  1599. struct perf_session *session __maybe_unused)
  1600. {
  1601. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1602. if (script->cpus) {
  1603. pr_warning("Extra cpu map event, ignoring.\n");
  1604. return 0;
  1605. }
  1606. script->cpus = cpu_map__new_data(&event->cpu_map.data);
  1607. if (!script->cpus)
  1608. return -ENOMEM;
  1609. return set_maps(script);
  1610. }
  1611. int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
  1612. {
  1613. bool show_full_info = false;
  1614. bool header = false;
  1615. bool header_only = false;
  1616. bool script_started = false;
  1617. char *rec_script_path = NULL;
  1618. char *rep_script_path = NULL;
  1619. struct perf_session *session;
  1620. struct itrace_synth_opts itrace_synth_opts = { .set = false, };
  1621. char *script_path = NULL;
  1622. const char **__argv;
  1623. int i, j, err = 0;
  1624. struct perf_script script = {
  1625. .tool = {
  1626. .sample = process_sample_event,
  1627. .mmap = perf_event__process_mmap,
  1628. .mmap2 = perf_event__process_mmap2,
  1629. .comm = perf_event__process_comm,
  1630. .exit = perf_event__process_exit,
  1631. .fork = perf_event__process_fork,
  1632. .attr = process_attr,
  1633. .event_update = perf_event__process_event_update,
  1634. .tracing_data = perf_event__process_tracing_data,
  1635. .build_id = perf_event__process_build_id,
  1636. .id_index = perf_event__process_id_index,
  1637. .auxtrace_info = perf_event__process_auxtrace_info,
  1638. .auxtrace = perf_event__process_auxtrace,
  1639. .auxtrace_error = perf_event__process_auxtrace_error,
  1640. .stat = perf_event__process_stat_event,
  1641. .stat_round = process_stat_round_event,
  1642. .stat_config = process_stat_config_event,
  1643. .thread_map = process_thread_map_event,
  1644. .cpu_map = process_cpu_map_event,
  1645. .ordered_events = true,
  1646. .ordering_requires_timestamps = true,
  1647. },
  1648. };
  1649. struct perf_data_file file = {
  1650. .mode = PERF_DATA_MODE_READ,
  1651. };
  1652. const struct option options[] = {
  1653. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1654. "dump raw trace in ASCII"),
  1655. OPT_INCR('v', "verbose", &verbose,
  1656. "be more verbose (show symbol address, etc)"),
  1657. OPT_BOOLEAN('L', "Latency", &latency_format,
  1658. "show latency attributes (irqs/preemption disabled, etc)"),
  1659. OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
  1660. list_available_scripts),
  1661. OPT_CALLBACK('s', "script", NULL, "name",
  1662. "script file name (lang:script name, script name, or *)",
  1663. parse_scriptname),
  1664. OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
  1665. "generate perf-script.xx script in specified language"),
  1666. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  1667. OPT_BOOLEAN('d', "debug-mode", &debug_mode,
  1668. "do various checks like samples ordering and lost events"),
  1669. OPT_BOOLEAN(0, "header", &header, "Show data header."),
  1670. OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
  1671. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  1672. "file", "vmlinux pathname"),
  1673. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  1674. "file", "kallsyms pathname"),
  1675. OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
  1676. "When printing symbols do not display call chain"),
  1677. OPT_CALLBACK(0, "symfs", NULL, "directory",
  1678. "Look for files with symbols relative to this directory",
  1679. symbol__config_symfs),
  1680. OPT_CALLBACK('F', "fields", NULL, "str",
  1681. "comma separated output fields prepend with 'type:'. "
  1682. "Valid types: hw,sw,trace,raw. "
  1683. "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
  1684. "addr,symoff,period,iregs,brstack,brstacksym,flags", parse_output_fields),
  1685. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  1686. "system-wide collection from all CPUs"),
  1687. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  1688. "only consider these symbols"),
  1689. OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
  1690. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  1691. "only display events for these comms"),
  1692. OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
  1693. "only consider symbols in these pids"),
  1694. OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
  1695. "only consider symbols in these tids"),
  1696. OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
  1697. "Set the maximum stack depth when parsing the callchain, "
  1698. "anything beyond the specified depth will be ignored. "
  1699. "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
  1700. OPT_BOOLEAN('I', "show-info", &show_full_info,
  1701. "display extended information from perf.data file"),
  1702. OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
  1703. "Show the path of [kernel.kallsyms]"),
  1704. OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
  1705. "Show the fork/comm/exit events"),
  1706. OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
  1707. "Show the mmap events"),
  1708. OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
  1709. "Show context switch events (if recorded)"),
  1710. OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
  1711. OPT_BOOLEAN(0, "ns", &nanosecs,
  1712. "Use 9 decimal places when displaying time"),
  1713. OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
  1714. "Instruction Tracing options",
  1715. itrace_parse_synth_opts),
  1716. OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
  1717. "Show full source file name path for source lines"),
  1718. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  1719. "Enable symbol demangling"),
  1720. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  1721. "Enable kernel symbol demangling"),
  1722. OPT_END()
  1723. };
  1724. const char * const script_subcommands[] = { "record", "report", NULL };
  1725. const char *script_usage[] = {
  1726. "perf script [<options>]",
  1727. "perf script [<options>] record <script> [<record-options>] <command>",
  1728. "perf script [<options>] report <script> [script-args]",
  1729. "perf script [<options>] <script> [<record-options>] <command>",
  1730. "perf script [<options>] <top-script> [script-args]",
  1731. NULL
  1732. };
  1733. setup_scripting();
  1734. argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
  1735. PARSE_OPT_STOP_AT_NON_OPTION);
  1736. file.path = input_name;
  1737. if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
  1738. rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
  1739. if (!rec_script_path)
  1740. return cmd_record(argc, argv, NULL);
  1741. }
  1742. if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
  1743. rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
  1744. if (!rep_script_path) {
  1745. fprintf(stderr,
  1746. "Please specify a valid report script"
  1747. "(see 'perf script -l' for listing)\n");
  1748. return -1;
  1749. }
  1750. }
  1751. if (itrace_synth_opts.callchain &&
  1752. itrace_synth_opts.callchain_sz > scripting_max_stack)
  1753. scripting_max_stack = itrace_synth_opts.callchain_sz;
  1754. /* make sure PERF_EXEC_PATH is set for scripts */
  1755. set_argv_exec_path(get_argv_exec_path());
  1756. if (argc && !script_name && !rec_script_path && !rep_script_path) {
  1757. int live_pipe[2];
  1758. int rep_args;
  1759. pid_t pid;
  1760. rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
  1761. rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
  1762. if (!rec_script_path && !rep_script_path) {
  1763. usage_with_options_msg(script_usage, options,
  1764. "Couldn't find script `%s'\n\n See perf"
  1765. " script -l for available scripts.\n", argv[0]);
  1766. }
  1767. if (is_top_script(argv[0])) {
  1768. rep_args = argc - 1;
  1769. } else {
  1770. int rec_args;
  1771. rep_args = has_required_arg(rep_script_path);
  1772. rec_args = (argc - 1) - rep_args;
  1773. if (rec_args < 0) {
  1774. usage_with_options_msg(script_usage, options,
  1775. "`%s' script requires options."
  1776. "\n\n See perf script -l for available "
  1777. "scripts and options.\n", argv[0]);
  1778. }
  1779. }
  1780. if (pipe(live_pipe) < 0) {
  1781. perror("failed to create pipe");
  1782. return -1;
  1783. }
  1784. pid = fork();
  1785. if (pid < 0) {
  1786. perror("failed to fork");
  1787. return -1;
  1788. }
  1789. if (!pid) {
  1790. j = 0;
  1791. dup2(live_pipe[1], 1);
  1792. close(live_pipe[0]);
  1793. if (is_top_script(argv[0])) {
  1794. system_wide = true;
  1795. } else if (!system_wide) {
  1796. if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
  1797. err = -1;
  1798. goto out;
  1799. }
  1800. }
  1801. __argv = malloc((argc + 6) * sizeof(const char *));
  1802. if (!__argv) {
  1803. pr_err("malloc failed\n");
  1804. err = -ENOMEM;
  1805. goto out;
  1806. }
  1807. __argv[j++] = "/bin/sh";
  1808. __argv[j++] = rec_script_path;
  1809. if (system_wide)
  1810. __argv[j++] = "-a";
  1811. __argv[j++] = "-q";
  1812. __argv[j++] = "-o";
  1813. __argv[j++] = "-";
  1814. for (i = rep_args + 1; i < argc; i++)
  1815. __argv[j++] = argv[i];
  1816. __argv[j++] = NULL;
  1817. execvp("/bin/sh", (char **)__argv);
  1818. free(__argv);
  1819. exit(-1);
  1820. }
  1821. dup2(live_pipe[0], 0);
  1822. close(live_pipe[1]);
  1823. __argv = malloc((argc + 4) * sizeof(const char *));
  1824. if (!__argv) {
  1825. pr_err("malloc failed\n");
  1826. err = -ENOMEM;
  1827. goto out;
  1828. }
  1829. j = 0;
  1830. __argv[j++] = "/bin/sh";
  1831. __argv[j++] = rep_script_path;
  1832. for (i = 1; i < rep_args + 1; i++)
  1833. __argv[j++] = argv[i];
  1834. __argv[j++] = "-i";
  1835. __argv[j++] = "-";
  1836. __argv[j++] = NULL;
  1837. execvp("/bin/sh", (char **)__argv);
  1838. free(__argv);
  1839. exit(-1);
  1840. }
  1841. if (rec_script_path)
  1842. script_path = rec_script_path;
  1843. if (rep_script_path)
  1844. script_path = rep_script_path;
  1845. if (script_path) {
  1846. j = 0;
  1847. if (!rec_script_path)
  1848. system_wide = false;
  1849. else if (!system_wide) {
  1850. if (have_cmd(argc - 1, &argv[1]) != 0) {
  1851. err = -1;
  1852. goto out;
  1853. }
  1854. }
  1855. __argv = malloc((argc + 2) * sizeof(const char *));
  1856. if (!__argv) {
  1857. pr_err("malloc failed\n");
  1858. err = -ENOMEM;
  1859. goto out;
  1860. }
  1861. __argv[j++] = "/bin/sh";
  1862. __argv[j++] = script_path;
  1863. if (system_wide)
  1864. __argv[j++] = "-a";
  1865. for (i = 2; i < argc; i++)
  1866. __argv[j++] = argv[i];
  1867. __argv[j++] = NULL;
  1868. execvp("/bin/sh", (char **)__argv);
  1869. free(__argv);
  1870. exit(-1);
  1871. }
  1872. if (!script_name)
  1873. setup_pager();
  1874. session = perf_session__new(&file, false, &script.tool);
  1875. if (session == NULL)
  1876. return -1;
  1877. if (header || header_only) {
  1878. perf_session__fprintf_info(session, stdout, show_full_info);
  1879. if (header_only)
  1880. goto out_delete;
  1881. }
  1882. if (symbol__init(&session->header.env) < 0)
  1883. goto out_delete;
  1884. script.session = session;
  1885. script__setup_sample_type(&script);
  1886. session->itrace_synth_opts = &itrace_synth_opts;
  1887. if (cpu_list) {
  1888. err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
  1889. if (err < 0)
  1890. goto out_delete;
  1891. }
  1892. if (!no_callchain)
  1893. symbol_conf.use_callchain = true;
  1894. else
  1895. symbol_conf.use_callchain = false;
  1896. if (session->tevent.pevent &&
  1897. pevent_set_function_resolver(session->tevent.pevent,
  1898. machine__resolve_kernel_addr,
  1899. &session->machines.host) < 0) {
  1900. pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
  1901. return -1;
  1902. }
  1903. if (generate_script_lang) {
  1904. struct stat perf_stat;
  1905. int input;
  1906. if (output_set_by_user()) {
  1907. fprintf(stderr,
  1908. "custom fields not supported for generated scripts");
  1909. err = -EINVAL;
  1910. goto out_delete;
  1911. }
  1912. input = open(file.path, O_RDONLY); /* input_name */
  1913. if (input < 0) {
  1914. err = -errno;
  1915. perror("failed to open file");
  1916. goto out_delete;
  1917. }
  1918. err = fstat(input, &perf_stat);
  1919. if (err < 0) {
  1920. perror("failed to stat file");
  1921. goto out_delete;
  1922. }
  1923. if (!perf_stat.st_size) {
  1924. fprintf(stderr, "zero-sized file, nothing to do!\n");
  1925. goto out_delete;
  1926. }
  1927. scripting_ops = script_spec__lookup(generate_script_lang);
  1928. if (!scripting_ops) {
  1929. fprintf(stderr, "invalid language specifier");
  1930. err = -ENOENT;
  1931. goto out_delete;
  1932. }
  1933. err = scripting_ops->generate_script(session->tevent.pevent,
  1934. "perf-script");
  1935. goto out_delete;
  1936. }
  1937. if (script_name) {
  1938. err = scripting_ops->start_script(script_name, argc, argv);
  1939. if (err)
  1940. goto out_delete;
  1941. pr_debug("perf script started with script %s\n\n", script_name);
  1942. script_started = true;
  1943. }
  1944. err = perf_session__check_output_opt(session);
  1945. if (err < 0)
  1946. goto out_delete;
  1947. err = __cmd_script(&script);
  1948. flush_scripting();
  1949. out_delete:
  1950. perf_evlist__free_stats(session->evlist);
  1951. perf_session__delete(session);
  1952. if (script_started)
  1953. cleanup_scripting();
  1954. out:
  1955. return err;
  1956. }