builtin-script.c 51 KB

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