builtin-script.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002
  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. return perf_evsel__check_attr(evsel, scr->session);
  573. }
  574. static int process_comm_event(struct perf_tool *tool,
  575. union perf_event *event,
  576. struct perf_sample *sample,
  577. struct machine *machine)
  578. {
  579. struct thread *thread;
  580. struct perf_script *script = container_of(tool, struct perf_script, tool);
  581. struct perf_session *session = script->session;
  582. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  583. int ret = -1;
  584. thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
  585. if (thread == NULL) {
  586. pr_debug("problem processing COMM event, skipping it.\n");
  587. return -1;
  588. }
  589. if (perf_event__process_comm(tool, event, sample, machine) < 0)
  590. goto out;
  591. if (!evsel->attr.sample_id_all) {
  592. sample->cpu = 0;
  593. sample->time = 0;
  594. sample->tid = event->comm.tid;
  595. sample->pid = event->comm.pid;
  596. }
  597. print_sample_start(sample, thread, evsel);
  598. perf_event__fprintf(event, stdout);
  599. ret = 0;
  600. out:
  601. thread__put(thread);
  602. return ret;
  603. }
  604. static int process_fork_event(struct perf_tool *tool,
  605. union perf_event *event,
  606. struct perf_sample *sample,
  607. struct machine *machine)
  608. {
  609. struct thread *thread;
  610. struct perf_script *script = container_of(tool, struct perf_script, tool);
  611. struct perf_session *session = script->session;
  612. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  613. if (perf_event__process_fork(tool, event, sample, machine) < 0)
  614. return -1;
  615. thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
  616. if (thread == NULL) {
  617. pr_debug("problem processing FORK event, skipping it.\n");
  618. return -1;
  619. }
  620. if (!evsel->attr.sample_id_all) {
  621. sample->cpu = 0;
  622. sample->time = event->fork.time;
  623. sample->tid = event->fork.tid;
  624. sample->pid = event->fork.pid;
  625. }
  626. print_sample_start(sample, thread, evsel);
  627. perf_event__fprintf(event, stdout);
  628. thread__put(thread);
  629. return 0;
  630. }
  631. static int process_exit_event(struct perf_tool *tool,
  632. union perf_event *event,
  633. struct perf_sample *sample,
  634. struct machine *machine)
  635. {
  636. int err = 0;
  637. struct thread *thread;
  638. struct perf_script *script = container_of(tool, struct perf_script, tool);
  639. struct perf_session *session = script->session;
  640. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  641. thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
  642. if (thread == NULL) {
  643. pr_debug("problem processing EXIT event, skipping it.\n");
  644. return -1;
  645. }
  646. if (!evsel->attr.sample_id_all) {
  647. sample->cpu = 0;
  648. sample->time = 0;
  649. sample->tid = event->fork.tid;
  650. sample->pid = event->fork.pid;
  651. }
  652. print_sample_start(sample, thread, evsel);
  653. perf_event__fprintf(event, stdout);
  654. if (perf_event__process_exit(tool, event, sample, machine) < 0)
  655. err = -1;
  656. thread__put(thread);
  657. return err;
  658. }
  659. static int process_mmap_event(struct perf_tool *tool,
  660. union perf_event *event,
  661. struct perf_sample *sample,
  662. struct machine *machine)
  663. {
  664. struct thread *thread;
  665. struct perf_script *script = container_of(tool, struct perf_script, tool);
  666. struct perf_session *session = script->session;
  667. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  668. if (perf_event__process_mmap(tool, event, sample, machine) < 0)
  669. return -1;
  670. thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
  671. if (thread == NULL) {
  672. pr_debug("problem processing MMAP event, skipping it.\n");
  673. return -1;
  674. }
  675. if (!evsel->attr.sample_id_all) {
  676. sample->cpu = 0;
  677. sample->time = 0;
  678. sample->tid = event->mmap.tid;
  679. sample->pid = event->mmap.pid;
  680. }
  681. print_sample_start(sample, thread, evsel);
  682. perf_event__fprintf(event, stdout);
  683. thread__put(thread);
  684. return 0;
  685. }
  686. static int process_mmap2_event(struct perf_tool *tool,
  687. union perf_event *event,
  688. struct perf_sample *sample,
  689. struct machine *machine)
  690. {
  691. struct thread *thread;
  692. struct perf_script *script = container_of(tool, struct perf_script, tool);
  693. struct perf_session *session = script->session;
  694. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  695. if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
  696. return -1;
  697. thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
  698. if (thread == NULL) {
  699. pr_debug("problem processing MMAP2 event, skipping it.\n");
  700. return -1;
  701. }
  702. if (!evsel->attr.sample_id_all) {
  703. sample->cpu = 0;
  704. sample->time = 0;
  705. sample->tid = event->mmap2.tid;
  706. sample->pid = event->mmap2.pid;
  707. }
  708. print_sample_start(sample, thread, evsel);
  709. perf_event__fprintf(event, stdout);
  710. thread__put(thread);
  711. return 0;
  712. }
  713. static int process_switch_event(struct perf_tool *tool,
  714. union perf_event *event,
  715. struct perf_sample *sample,
  716. struct machine *machine)
  717. {
  718. struct thread *thread;
  719. struct perf_script *script = container_of(tool, struct perf_script, tool);
  720. struct perf_session *session = script->session;
  721. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  722. if (perf_event__process_switch(tool, event, sample, machine) < 0)
  723. return -1;
  724. thread = machine__findnew_thread(machine, sample->pid,
  725. sample->tid);
  726. if (thread == NULL) {
  727. pr_debug("problem processing SWITCH event, skipping it.\n");
  728. return -1;
  729. }
  730. print_sample_start(sample, thread, evsel);
  731. perf_event__fprintf(event, stdout);
  732. thread__put(thread);
  733. return 0;
  734. }
  735. static void sig_handler(int sig __maybe_unused)
  736. {
  737. session_done = 1;
  738. }
  739. static int __cmd_script(struct perf_script *script)
  740. {
  741. int ret;
  742. signal(SIGINT, sig_handler);
  743. /* override event processing functions */
  744. if (script->show_task_events) {
  745. script->tool.comm = process_comm_event;
  746. script->tool.fork = process_fork_event;
  747. script->tool.exit = process_exit_event;
  748. }
  749. if (script->show_mmap_events) {
  750. script->tool.mmap = process_mmap_event;
  751. script->tool.mmap2 = process_mmap2_event;
  752. }
  753. if (script->show_switch_events)
  754. script->tool.context_switch = process_switch_event;
  755. ret = perf_session__process_events(script->session);
  756. if (debug_mode)
  757. pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
  758. return ret;
  759. }
  760. struct script_spec {
  761. struct list_head node;
  762. struct scripting_ops *ops;
  763. char spec[0];
  764. };
  765. static LIST_HEAD(script_specs);
  766. static struct script_spec *script_spec__new(const char *spec,
  767. struct scripting_ops *ops)
  768. {
  769. struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
  770. if (s != NULL) {
  771. strcpy(s->spec, spec);
  772. s->ops = ops;
  773. }
  774. return s;
  775. }
  776. static void script_spec__add(struct script_spec *s)
  777. {
  778. list_add_tail(&s->node, &script_specs);
  779. }
  780. static struct script_spec *script_spec__find(const char *spec)
  781. {
  782. struct script_spec *s;
  783. list_for_each_entry(s, &script_specs, node)
  784. if (strcasecmp(s->spec, spec) == 0)
  785. return s;
  786. return NULL;
  787. }
  788. static struct script_spec *script_spec__findnew(const char *spec,
  789. struct scripting_ops *ops)
  790. {
  791. struct script_spec *s = script_spec__find(spec);
  792. if (s)
  793. return s;
  794. s = script_spec__new(spec, ops);
  795. if (!s)
  796. return NULL;
  797. script_spec__add(s);
  798. return s;
  799. }
  800. int script_spec_register(const char *spec, struct scripting_ops *ops)
  801. {
  802. struct script_spec *s;
  803. s = script_spec__find(spec);
  804. if (s)
  805. return -1;
  806. s = script_spec__findnew(spec, ops);
  807. if (!s)
  808. return -1;
  809. return 0;
  810. }
  811. static struct scripting_ops *script_spec__lookup(const char *spec)
  812. {
  813. struct script_spec *s = script_spec__find(spec);
  814. if (!s)
  815. return NULL;
  816. return s->ops;
  817. }
  818. static void list_available_languages(void)
  819. {
  820. struct script_spec *s;
  821. fprintf(stderr, "\n");
  822. fprintf(stderr, "Scripting language extensions (used in "
  823. "perf script -s [spec:]script.[spec]):\n\n");
  824. list_for_each_entry(s, &script_specs, node)
  825. fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
  826. fprintf(stderr, "\n");
  827. }
  828. static int parse_scriptname(const struct option *opt __maybe_unused,
  829. const char *str, int unset __maybe_unused)
  830. {
  831. char spec[PATH_MAX];
  832. const char *script, *ext;
  833. int len;
  834. if (strcmp(str, "lang") == 0) {
  835. list_available_languages();
  836. exit(0);
  837. }
  838. script = strchr(str, ':');
  839. if (script) {
  840. len = script - str;
  841. if (len >= PATH_MAX) {
  842. fprintf(stderr, "invalid language specifier");
  843. return -1;
  844. }
  845. strncpy(spec, str, len);
  846. spec[len] = '\0';
  847. scripting_ops = script_spec__lookup(spec);
  848. if (!scripting_ops) {
  849. fprintf(stderr, "invalid language specifier");
  850. return -1;
  851. }
  852. script++;
  853. } else {
  854. script = str;
  855. ext = strrchr(script, '.');
  856. if (!ext) {
  857. fprintf(stderr, "invalid script extension");
  858. return -1;
  859. }
  860. scripting_ops = script_spec__lookup(++ext);
  861. if (!scripting_ops) {
  862. fprintf(stderr, "invalid script extension");
  863. return -1;
  864. }
  865. }
  866. script_name = strdup(script);
  867. return 0;
  868. }
  869. static int parse_output_fields(const struct option *opt __maybe_unused,
  870. const char *arg, int unset __maybe_unused)
  871. {
  872. char *tok;
  873. int i, imax = ARRAY_SIZE(all_output_options);
  874. int j;
  875. int rc = 0;
  876. char *str = strdup(arg);
  877. int type = -1;
  878. if (!str)
  879. return -ENOMEM;
  880. /* first word can state for which event type the user is specifying
  881. * the fields. If no type exists, the specified fields apply to all
  882. * event types found in the file minus the invalid fields for a type.
  883. */
  884. tok = strchr(str, ':');
  885. if (tok) {
  886. *tok = '\0';
  887. tok++;
  888. if (!strcmp(str, "hw"))
  889. type = PERF_TYPE_HARDWARE;
  890. else if (!strcmp(str, "sw"))
  891. type = PERF_TYPE_SOFTWARE;
  892. else if (!strcmp(str, "trace"))
  893. type = PERF_TYPE_TRACEPOINT;
  894. else if (!strcmp(str, "raw"))
  895. type = PERF_TYPE_RAW;
  896. else {
  897. fprintf(stderr, "Invalid event type in field string.\n");
  898. rc = -EINVAL;
  899. goto out;
  900. }
  901. if (output[type].user_set)
  902. pr_warning("Overriding previous field request for %s events.\n",
  903. event_type(type));
  904. output[type].fields = 0;
  905. output[type].user_set = true;
  906. output[type].wildcard_set = false;
  907. } else {
  908. tok = str;
  909. if (strlen(str) == 0) {
  910. fprintf(stderr,
  911. "Cannot set fields to 'none' for all event types.\n");
  912. rc = -EINVAL;
  913. goto out;
  914. }
  915. if (output_set_by_user())
  916. pr_warning("Overriding previous field request for all events.\n");
  917. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  918. output[j].fields = 0;
  919. output[j].user_set = true;
  920. output[j].wildcard_set = true;
  921. }
  922. }
  923. for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
  924. for (i = 0; i < imax; ++i) {
  925. if (strcmp(tok, all_output_options[i].str) == 0)
  926. break;
  927. }
  928. if (i == imax && strcmp(tok, "flags") == 0) {
  929. print_flags = true;
  930. continue;
  931. }
  932. if (i == imax) {
  933. fprintf(stderr, "Invalid field requested.\n");
  934. rc = -EINVAL;
  935. goto out;
  936. }
  937. if (type == -1) {
  938. /* add user option to all events types for
  939. * which it is valid
  940. */
  941. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  942. if (output[j].invalid_fields & all_output_options[i].field) {
  943. pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
  944. all_output_options[i].str, event_type(j));
  945. } else
  946. output[j].fields |= all_output_options[i].field;
  947. }
  948. } else {
  949. if (output[type].invalid_fields & all_output_options[i].field) {
  950. fprintf(stderr, "\'%s\' not valid for %s events.\n",
  951. all_output_options[i].str, event_type(type));
  952. rc = -EINVAL;
  953. goto out;
  954. }
  955. output[type].fields |= all_output_options[i].field;
  956. }
  957. }
  958. if (type >= 0) {
  959. if (output[type].fields == 0) {
  960. pr_debug("No fields requested for %s type. "
  961. "Events will not be displayed.\n", event_type(type));
  962. }
  963. }
  964. out:
  965. free(str);
  966. return rc;
  967. }
  968. /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
  969. static int is_directory(const char *base_path, const struct dirent *dent)
  970. {
  971. char path[PATH_MAX];
  972. struct stat st;
  973. sprintf(path, "%s/%s", base_path, dent->d_name);
  974. if (stat(path, &st))
  975. return 0;
  976. return S_ISDIR(st.st_mode);
  977. }
  978. #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
  979. while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
  980. lang_next) \
  981. if ((lang_dirent.d_type == DT_DIR || \
  982. (lang_dirent.d_type == DT_UNKNOWN && \
  983. is_directory(scripts_path, &lang_dirent))) && \
  984. (strcmp(lang_dirent.d_name, ".")) && \
  985. (strcmp(lang_dirent.d_name, "..")))
  986. #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
  987. while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
  988. script_next) \
  989. if (script_dirent.d_type != DT_DIR && \
  990. (script_dirent.d_type != DT_UNKNOWN || \
  991. !is_directory(lang_path, &script_dirent)))
  992. #define RECORD_SUFFIX "-record"
  993. #define REPORT_SUFFIX "-report"
  994. struct script_desc {
  995. struct list_head node;
  996. char *name;
  997. char *half_liner;
  998. char *args;
  999. };
  1000. static LIST_HEAD(script_descs);
  1001. static struct script_desc *script_desc__new(const char *name)
  1002. {
  1003. struct script_desc *s = zalloc(sizeof(*s));
  1004. if (s != NULL && name)
  1005. s->name = strdup(name);
  1006. return s;
  1007. }
  1008. static void script_desc__delete(struct script_desc *s)
  1009. {
  1010. zfree(&s->name);
  1011. zfree(&s->half_liner);
  1012. zfree(&s->args);
  1013. free(s);
  1014. }
  1015. static void script_desc__add(struct script_desc *s)
  1016. {
  1017. list_add_tail(&s->node, &script_descs);
  1018. }
  1019. static struct script_desc *script_desc__find(const char *name)
  1020. {
  1021. struct script_desc *s;
  1022. list_for_each_entry(s, &script_descs, node)
  1023. if (strcasecmp(s->name, name) == 0)
  1024. return s;
  1025. return NULL;
  1026. }
  1027. static struct script_desc *script_desc__findnew(const char *name)
  1028. {
  1029. struct script_desc *s = script_desc__find(name);
  1030. if (s)
  1031. return s;
  1032. s = script_desc__new(name);
  1033. if (!s)
  1034. goto out_delete_desc;
  1035. script_desc__add(s);
  1036. return s;
  1037. out_delete_desc:
  1038. script_desc__delete(s);
  1039. return NULL;
  1040. }
  1041. static const char *ends_with(const char *str, const char *suffix)
  1042. {
  1043. size_t suffix_len = strlen(suffix);
  1044. const char *p = str;
  1045. if (strlen(str) > suffix_len) {
  1046. p = str + strlen(str) - suffix_len;
  1047. if (!strncmp(p, suffix, suffix_len))
  1048. return p;
  1049. }
  1050. return NULL;
  1051. }
  1052. static int read_script_info(struct script_desc *desc, const char *filename)
  1053. {
  1054. char line[BUFSIZ], *p;
  1055. FILE *fp;
  1056. fp = fopen(filename, "r");
  1057. if (!fp)
  1058. return -1;
  1059. while (fgets(line, sizeof(line), fp)) {
  1060. p = ltrim(line);
  1061. if (strlen(p) == 0)
  1062. continue;
  1063. if (*p != '#')
  1064. continue;
  1065. p++;
  1066. if (strlen(p) && *p == '!')
  1067. continue;
  1068. p = ltrim(p);
  1069. if (strlen(p) && p[strlen(p) - 1] == '\n')
  1070. p[strlen(p) - 1] = '\0';
  1071. if (!strncmp(p, "description:", strlen("description:"))) {
  1072. p += strlen("description:");
  1073. desc->half_liner = strdup(ltrim(p));
  1074. continue;
  1075. }
  1076. if (!strncmp(p, "args:", strlen("args:"))) {
  1077. p += strlen("args:");
  1078. desc->args = strdup(ltrim(p));
  1079. continue;
  1080. }
  1081. }
  1082. fclose(fp);
  1083. return 0;
  1084. }
  1085. static char *get_script_root(struct dirent *script_dirent, const char *suffix)
  1086. {
  1087. char *script_root, *str;
  1088. script_root = strdup(script_dirent->d_name);
  1089. if (!script_root)
  1090. return NULL;
  1091. str = (char *)ends_with(script_root, suffix);
  1092. if (!str) {
  1093. free(script_root);
  1094. return NULL;
  1095. }
  1096. *str = '\0';
  1097. return script_root;
  1098. }
  1099. static int list_available_scripts(const struct option *opt __maybe_unused,
  1100. const char *s __maybe_unused,
  1101. int unset __maybe_unused)
  1102. {
  1103. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  1104. char scripts_path[MAXPATHLEN];
  1105. DIR *scripts_dir, *lang_dir;
  1106. char script_path[MAXPATHLEN];
  1107. char lang_path[MAXPATHLEN];
  1108. struct script_desc *desc;
  1109. char first_half[BUFSIZ];
  1110. char *script_root;
  1111. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  1112. scripts_dir = opendir(scripts_path);
  1113. if (!scripts_dir)
  1114. return -1;
  1115. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  1116. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  1117. lang_dirent.d_name);
  1118. lang_dir = opendir(lang_path);
  1119. if (!lang_dir)
  1120. continue;
  1121. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  1122. script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
  1123. if (script_root) {
  1124. desc = script_desc__findnew(script_root);
  1125. snprintf(script_path, MAXPATHLEN, "%s/%s",
  1126. lang_path, script_dirent.d_name);
  1127. read_script_info(desc, script_path);
  1128. free(script_root);
  1129. }
  1130. }
  1131. }
  1132. fprintf(stdout, "List of available trace scripts:\n");
  1133. list_for_each_entry(desc, &script_descs, node) {
  1134. sprintf(first_half, "%s %s", desc->name,
  1135. desc->args ? desc->args : "");
  1136. fprintf(stdout, " %-36s %s\n", first_half,
  1137. desc->half_liner ? desc->half_liner : "");
  1138. }
  1139. exit(0);
  1140. }
  1141. /*
  1142. * Some scripts specify the required events in their "xxx-record" file,
  1143. * this function will check if the events in perf.data match those
  1144. * mentioned in the "xxx-record".
  1145. *
  1146. * Fixme: All existing "xxx-record" are all in good formats "-e event ",
  1147. * which is covered well now. And new parsing code should be added to
  1148. * cover the future complexing formats like event groups etc.
  1149. */
  1150. static int check_ev_match(char *dir_name, char *scriptname,
  1151. struct perf_session *session)
  1152. {
  1153. char filename[MAXPATHLEN], evname[128];
  1154. char line[BUFSIZ], *p;
  1155. struct perf_evsel *pos;
  1156. int match, len;
  1157. FILE *fp;
  1158. sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
  1159. fp = fopen(filename, "r");
  1160. if (!fp)
  1161. return -1;
  1162. while (fgets(line, sizeof(line), fp)) {
  1163. p = ltrim(line);
  1164. if (*p == '#')
  1165. continue;
  1166. while (strlen(p)) {
  1167. p = strstr(p, "-e");
  1168. if (!p)
  1169. break;
  1170. p += 2;
  1171. p = ltrim(p);
  1172. len = strcspn(p, " \t");
  1173. if (!len)
  1174. break;
  1175. snprintf(evname, len + 1, "%s", p);
  1176. match = 0;
  1177. evlist__for_each(session->evlist, pos) {
  1178. if (!strcmp(perf_evsel__name(pos), evname)) {
  1179. match = 1;
  1180. break;
  1181. }
  1182. }
  1183. if (!match) {
  1184. fclose(fp);
  1185. return -1;
  1186. }
  1187. }
  1188. }
  1189. fclose(fp);
  1190. return 0;
  1191. }
  1192. /*
  1193. * Return -1 if none is found, otherwise the actual scripts number.
  1194. *
  1195. * Currently the only user of this function is the script browser, which
  1196. * will list all statically runnable scripts, select one, execute it and
  1197. * show the output in a perf browser.
  1198. */
  1199. int find_scripts(char **scripts_array, char **scripts_path_array)
  1200. {
  1201. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  1202. char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
  1203. DIR *scripts_dir, *lang_dir;
  1204. struct perf_session *session;
  1205. struct perf_data_file file = {
  1206. .path = input_name,
  1207. .mode = PERF_DATA_MODE_READ,
  1208. };
  1209. char *temp;
  1210. int i = 0;
  1211. session = perf_session__new(&file, false, NULL);
  1212. if (!session)
  1213. return -1;
  1214. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  1215. scripts_dir = opendir(scripts_path);
  1216. if (!scripts_dir) {
  1217. perf_session__delete(session);
  1218. return -1;
  1219. }
  1220. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  1221. snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
  1222. lang_dirent.d_name);
  1223. #ifdef NO_LIBPERL
  1224. if (strstr(lang_path, "perl"))
  1225. continue;
  1226. #endif
  1227. #ifdef NO_LIBPYTHON
  1228. if (strstr(lang_path, "python"))
  1229. continue;
  1230. #endif
  1231. lang_dir = opendir(lang_path);
  1232. if (!lang_dir)
  1233. continue;
  1234. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  1235. /* Skip those real time scripts: xxxtop.p[yl] */
  1236. if (strstr(script_dirent.d_name, "top."))
  1237. continue;
  1238. sprintf(scripts_path_array[i], "%s/%s", lang_path,
  1239. script_dirent.d_name);
  1240. temp = strchr(script_dirent.d_name, '.');
  1241. snprintf(scripts_array[i],
  1242. (temp - script_dirent.d_name) + 1,
  1243. "%s", script_dirent.d_name);
  1244. if (check_ev_match(lang_path,
  1245. scripts_array[i], session))
  1246. continue;
  1247. i++;
  1248. }
  1249. closedir(lang_dir);
  1250. }
  1251. closedir(scripts_dir);
  1252. perf_session__delete(session);
  1253. return i;
  1254. }
  1255. static char *get_script_path(const char *script_root, const char *suffix)
  1256. {
  1257. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  1258. char scripts_path[MAXPATHLEN];
  1259. char script_path[MAXPATHLEN];
  1260. DIR *scripts_dir, *lang_dir;
  1261. char lang_path[MAXPATHLEN];
  1262. char *__script_root;
  1263. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  1264. scripts_dir = opendir(scripts_path);
  1265. if (!scripts_dir)
  1266. return NULL;
  1267. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  1268. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  1269. lang_dirent.d_name);
  1270. lang_dir = opendir(lang_path);
  1271. if (!lang_dir)
  1272. continue;
  1273. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  1274. __script_root = get_script_root(&script_dirent, suffix);
  1275. if (__script_root && !strcmp(script_root, __script_root)) {
  1276. free(__script_root);
  1277. closedir(lang_dir);
  1278. closedir(scripts_dir);
  1279. snprintf(script_path, MAXPATHLEN, "%s/%s",
  1280. lang_path, script_dirent.d_name);
  1281. return strdup(script_path);
  1282. }
  1283. free(__script_root);
  1284. }
  1285. closedir(lang_dir);
  1286. }
  1287. closedir(scripts_dir);
  1288. return NULL;
  1289. }
  1290. static bool is_top_script(const char *script_path)
  1291. {
  1292. return ends_with(script_path, "top") == NULL ? false : true;
  1293. }
  1294. static int has_required_arg(char *script_path)
  1295. {
  1296. struct script_desc *desc;
  1297. int n_args = 0;
  1298. char *p;
  1299. desc = script_desc__new(NULL);
  1300. if (read_script_info(desc, script_path))
  1301. goto out;
  1302. if (!desc->args)
  1303. goto out;
  1304. for (p = desc->args; *p; p++)
  1305. if (*p == '<')
  1306. n_args++;
  1307. out:
  1308. script_desc__delete(desc);
  1309. return n_args;
  1310. }
  1311. static int have_cmd(int argc, const char **argv)
  1312. {
  1313. char **__argv = malloc(sizeof(const char *) * argc);
  1314. if (!__argv) {
  1315. pr_err("malloc failed\n");
  1316. return -1;
  1317. }
  1318. memcpy(__argv, argv, sizeof(const char *) * argc);
  1319. argc = parse_options(argc, (const char **)__argv, record_options,
  1320. NULL, PARSE_OPT_STOP_AT_NON_OPTION);
  1321. free(__argv);
  1322. system_wide = (argc == 0);
  1323. return 0;
  1324. }
  1325. static void script__setup_sample_type(struct perf_script *script)
  1326. {
  1327. struct perf_session *session = script->session;
  1328. u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
  1329. if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
  1330. if ((sample_type & PERF_SAMPLE_REGS_USER) &&
  1331. (sample_type & PERF_SAMPLE_STACK_USER))
  1332. callchain_param.record_mode = CALLCHAIN_DWARF;
  1333. else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
  1334. callchain_param.record_mode = CALLCHAIN_LBR;
  1335. else
  1336. callchain_param.record_mode = CALLCHAIN_FP;
  1337. }
  1338. }
  1339. int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
  1340. {
  1341. bool show_full_info = false;
  1342. bool header = false;
  1343. bool header_only = false;
  1344. bool script_started = false;
  1345. char *rec_script_path = NULL;
  1346. char *rep_script_path = NULL;
  1347. struct perf_session *session;
  1348. struct itrace_synth_opts itrace_synth_opts = { .set = false, };
  1349. char *script_path = NULL;
  1350. const char **__argv;
  1351. int i, j, err = 0;
  1352. struct perf_script script = {
  1353. .tool = {
  1354. .sample = process_sample_event,
  1355. .mmap = perf_event__process_mmap,
  1356. .mmap2 = perf_event__process_mmap2,
  1357. .comm = perf_event__process_comm,
  1358. .exit = perf_event__process_exit,
  1359. .fork = perf_event__process_fork,
  1360. .attr = process_attr,
  1361. .tracing_data = perf_event__process_tracing_data,
  1362. .build_id = perf_event__process_build_id,
  1363. .id_index = perf_event__process_id_index,
  1364. .auxtrace_info = perf_event__process_auxtrace_info,
  1365. .auxtrace = perf_event__process_auxtrace,
  1366. .auxtrace_error = perf_event__process_auxtrace_error,
  1367. .ordered_events = true,
  1368. .ordering_requires_timestamps = true,
  1369. },
  1370. };
  1371. struct perf_data_file file = {
  1372. .mode = PERF_DATA_MODE_READ,
  1373. };
  1374. const struct option options[] = {
  1375. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1376. "dump raw trace in ASCII"),
  1377. OPT_INCR('v', "verbose", &verbose,
  1378. "be more verbose (show symbol address, etc)"),
  1379. OPT_BOOLEAN('L', "Latency", &latency_format,
  1380. "show latency attributes (irqs/preemption disabled, etc)"),
  1381. OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
  1382. list_available_scripts),
  1383. OPT_CALLBACK('s', "script", NULL, "name",
  1384. "script file name (lang:script name, script name, or *)",
  1385. parse_scriptname),
  1386. OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
  1387. "generate perf-script.xx script in specified language"),
  1388. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  1389. OPT_BOOLEAN('d', "debug-mode", &debug_mode,
  1390. "do various checks like samples ordering and lost events"),
  1391. OPT_BOOLEAN(0, "header", &header, "Show data header."),
  1392. OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
  1393. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  1394. "file", "vmlinux pathname"),
  1395. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  1396. "file", "kallsyms pathname"),
  1397. OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
  1398. "When printing symbols do not display call chain"),
  1399. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  1400. "Look for files with symbols relative to this directory"),
  1401. OPT_CALLBACK('F', "fields", NULL, "str",
  1402. "comma separated output fields prepend with 'type:'. "
  1403. "Valid types: hw,sw,trace,raw. "
  1404. "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
  1405. "addr,symoff,period,iregs,flags", parse_output_fields),
  1406. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  1407. "system-wide collection from all CPUs"),
  1408. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  1409. "only consider these symbols"),
  1410. OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
  1411. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  1412. "only display events for these comms"),
  1413. OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
  1414. "only consider symbols in these pids"),
  1415. OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
  1416. "only consider symbols in these tids"),
  1417. OPT_BOOLEAN('I', "show-info", &show_full_info,
  1418. "display extended information from perf.data file"),
  1419. OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
  1420. "Show the path of [kernel.kallsyms]"),
  1421. OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
  1422. "Show the fork/comm/exit events"),
  1423. OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
  1424. "Show the mmap events"),
  1425. OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
  1426. "Show context switch events (if recorded)"),
  1427. OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
  1428. OPT_BOOLEAN(0, "ns", &nanosecs,
  1429. "Use 9 decimal places when displaying time"),
  1430. OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
  1431. "Instruction Tracing options",
  1432. itrace_parse_synth_opts),
  1433. OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
  1434. "Show full source file name path for source lines"),
  1435. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  1436. "Enable symbol demangling"),
  1437. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  1438. "Enable kernel symbol demangling"),
  1439. OPT_END()
  1440. };
  1441. const char * const script_subcommands[] = { "record", "report", NULL };
  1442. const char *script_usage[] = {
  1443. "perf script [<options>]",
  1444. "perf script [<options>] record <script> [<record-options>] <command>",
  1445. "perf script [<options>] report <script> [script-args]",
  1446. "perf script [<options>] <script> [<record-options>] <command>",
  1447. "perf script [<options>] <top-script> [script-args]",
  1448. NULL
  1449. };
  1450. setup_scripting();
  1451. argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
  1452. PARSE_OPT_STOP_AT_NON_OPTION);
  1453. file.path = input_name;
  1454. if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
  1455. rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
  1456. if (!rec_script_path)
  1457. return cmd_record(argc, argv, NULL);
  1458. }
  1459. if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
  1460. rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
  1461. if (!rep_script_path) {
  1462. fprintf(stderr,
  1463. "Please specify a valid report script"
  1464. "(see 'perf script -l' for listing)\n");
  1465. return -1;
  1466. }
  1467. }
  1468. if (itrace_synth_opts.callchain &&
  1469. itrace_synth_opts.callchain_sz > scripting_max_stack)
  1470. scripting_max_stack = itrace_synth_opts.callchain_sz;
  1471. /* make sure PERF_EXEC_PATH is set for scripts */
  1472. perf_set_argv_exec_path(perf_exec_path());
  1473. if (argc && !script_name && !rec_script_path && !rep_script_path) {
  1474. int live_pipe[2];
  1475. int rep_args;
  1476. pid_t pid;
  1477. rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
  1478. rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
  1479. if (!rec_script_path && !rep_script_path) {
  1480. fprintf(stderr, " Couldn't find script %s\n\n See perf"
  1481. " script -l for available scripts.\n", argv[0]);
  1482. usage_with_options(script_usage, options);
  1483. }
  1484. if (is_top_script(argv[0])) {
  1485. rep_args = argc - 1;
  1486. } else {
  1487. int rec_args;
  1488. rep_args = has_required_arg(rep_script_path);
  1489. rec_args = (argc - 1) - rep_args;
  1490. if (rec_args < 0) {
  1491. fprintf(stderr, " %s script requires options."
  1492. "\n\n See perf script -l for available "
  1493. "scripts and options.\n", argv[0]);
  1494. usage_with_options(script_usage, options);
  1495. }
  1496. }
  1497. if (pipe(live_pipe) < 0) {
  1498. perror("failed to create pipe");
  1499. return -1;
  1500. }
  1501. pid = fork();
  1502. if (pid < 0) {
  1503. perror("failed to fork");
  1504. return -1;
  1505. }
  1506. if (!pid) {
  1507. j = 0;
  1508. dup2(live_pipe[1], 1);
  1509. close(live_pipe[0]);
  1510. if (is_top_script(argv[0])) {
  1511. system_wide = true;
  1512. } else if (!system_wide) {
  1513. if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
  1514. err = -1;
  1515. goto out;
  1516. }
  1517. }
  1518. __argv = malloc((argc + 6) * sizeof(const char *));
  1519. if (!__argv) {
  1520. pr_err("malloc failed\n");
  1521. err = -ENOMEM;
  1522. goto out;
  1523. }
  1524. __argv[j++] = "/bin/sh";
  1525. __argv[j++] = rec_script_path;
  1526. if (system_wide)
  1527. __argv[j++] = "-a";
  1528. __argv[j++] = "-q";
  1529. __argv[j++] = "-o";
  1530. __argv[j++] = "-";
  1531. for (i = rep_args + 1; i < argc; i++)
  1532. __argv[j++] = argv[i];
  1533. __argv[j++] = NULL;
  1534. execvp("/bin/sh", (char **)__argv);
  1535. free(__argv);
  1536. exit(-1);
  1537. }
  1538. dup2(live_pipe[0], 0);
  1539. close(live_pipe[1]);
  1540. __argv = malloc((argc + 4) * sizeof(const char *));
  1541. if (!__argv) {
  1542. pr_err("malloc failed\n");
  1543. err = -ENOMEM;
  1544. goto out;
  1545. }
  1546. j = 0;
  1547. __argv[j++] = "/bin/sh";
  1548. __argv[j++] = rep_script_path;
  1549. for (i = 1; i < rep_args + 1; i++)
  1550. __argv[j++] = argv[i];
  1551. __argv[j++] = "-i";
  1552. __argv[j++] = "-";
  1553. __argv[j++] = NULL;
  1554. execvp("/bin/sh", (char **)__argv);
  1555. free(__argv);
  1556. exit(-1);
  1557. }
  1558. if (rec_script_path)
  1559. script_path = rec_script_path;
  1560. if (rep_script_path)
  1561. script_path = rep_script_path;
  1562. if (script_path) {
  1563. j = 0;
  1564. if (!rec_script_path)
  1565. system_wide = false;
  1566. else if (!system_wide) {
  1567. if (have_cmd(argc - 1, &argv[1]) != 0) {
  1568. err = -1;
  1569. goto out;
  1570. }
  1571. }
  1572. __argv = malloc((argc + 2) * sizeof(const char *));
  1573. if (!__argv) {
  1574. pr_err("malloc failed\n");
  1575. err = -ENOMEM;
  1576. goto out;
  1577. }
  1578. __argv[j++] = "/bin/sh";
  1579. __argv[j++] = script_path;
  1580. if (system_wide)
  1581. __argv[j++] = "-a";
  1582. for (i = 2; i < argc; i++)
  1583. __argv[j++] = argv[i];
  1584. __argv[j++] = NULL;
  1585. execvp("/bin/sh", (char **)__argv);
  1586. free(__argv);
  1587. exit(-1);
  1588. }
  1589. if (!script_name)
  1590. setup_pager();
  1591. session = perf_session__new(&file, false, &script.tool);
  1592. if (session == NULL)
  1593. return -1;
  1594. if (header || header_only) {
  1595. perf_session__fprintf_info(session, stdout, show_full_info);
  1596. if (header_only)
  1597. goto out_delete;
  1598. }
  1599. if (symbol__init(&session->header.env) < 0)
  1600. goto out_delete;
  1601. script.session = session;
  1602. script__setup_sample_type(&script);
  1603. session->itrace_synth_opts = &itrace_synth_opts;
  1604. if (cpu_list) {
  1605. err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
  1606. if (err < 0)
  1607. goto out_delete;
  1608. }
  1609. if (!no_callchain)
  1610. symbol_conf.use_callchain = true;
  1611. else
  1612. symbol_conf.use_callchain = false;
  1613. if (session->tevent.pevent &&
  1614. pevent_set_function_resolver(session->tevent.pevent,
  1615. machine__resolve_kernel_addr,
  1616. &session->machines.host) < 0) {
  1617. pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
  1618. return -1;
  1619. }
  1620. if (generate_script_lang) {
  1621. struct stat perf_stat;
  1622. int input;
  1623. if (output_set_by_user()) {
  1624. fprintf(stderr,
  1625. "custom fields not supported for generated scripts");
  1626. err = -EINVAL;
  1627. goto out_delete;
  1628. }
  1629. input = open(file.path, O_RDONLY); /* input_name */
  1630. if (input < 0) {
  1631. err = -errno;
  1632. perror("failed to open file");
  1633. goto out_delete;
  1634. }
  1635. err = fstat(input, &perf_stat);
  1636. if (err < 0) {
  1637. perror("failed to stat file");
  1638. goto out_delete;
  1639. }
  1640. if (!perf_stat.st_size) {
  1641. fprintf(stderr, "zero-sized file, nothing to do!\n");
  1642. goto out_delete;
  1643. }
  1644. scripting_ops = script_spec__lookup(generate_script_lang);
  1645. if (!scripting_ops) {
  1646. fprintf(stderr, "invalid language specifier");
  1647. err = -ENOENT;
  1648. goto out_delete;
  1649. }
  1650. err = scripting_ops->generate_script(session->tevent.pevent,
  1651. "perf-script");
  1652. goto out_delete;
  1653. }
  1654. if (script_name) {
  1655. err = scripting_ops->start_script(script_name, argc, argv);
  1656. if (err)
  1657. goto out_delete;
  1658. pr_debug("perf script started with script %s\n\n", script_name);
  1659. script_started = true;
  1660. }
  1661. err = perf_session__check_output_opt(session);
  1662. if (err < 0)
  1663. goto out_delete;
  1664. err = __cmd_script(&script);
  1665. flush_scripting();
  1666. out_delete:
  1667. perf_session__delete(session);
  1668. if (script_started)
  1669. cleanup_scripting();
  1670. out:
  1671. return err;
  1672. }