builtin-script.c 44 KB

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