builtin-script.c 58 KB

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