builtin-script.c 54 KB

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