builtin-script.c 49 KB

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