builtin-script.c 51 KB

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