builtin-script.c 44 KB

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