builtin-script.c 58 KB

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