builtin-script.c 61 KB

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