builtin-script.c 53 KB

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