builtin-script.c 48 KB

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