builtin-script.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121
  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/string2.h"
  24. #include "util/thread-stack.h"
  25. #include "util/time-utils.h"
  26. #include "print_binary.h"
  27. #include <linux/bitmap.h>
  28. #include <linux/kernel.h>
  29. #include <linux/stringify.h>
  30. #include <linux/time64.h>
  31. #include "asm/bug.h"
  32. #include "util/mem-events.h"
  33. #include "util/dump-insn.h"
  34. #include <dirent.h>
  35. #include <errno.h>
  36. #include <inttypes.h>
  37. #include <signal.h>
  38. #include <sys/param.h>
  39. #include <sys/types.h>
  40. #include <sys/stat.h>
  41. #include <unistd.h>
  42. #include "sane_ctype.h"
  43. static char const *script_name;
  44. static char const *generate_script_lang;
  45. static bool debug_mode;
  46. static u64 last_timestamp;
  47. static u64 nr_unordered;
  48. static bool no_callchain;
  49. static bool latency_format;
  50. static bool system_wide;
  51. static bool print_flags;
  52. static bool nanosecs;
  53. static const char *cpu_list;
  54. static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
  55. static struct perf_stat_config stat_config;
  56. static int max_blocks;
  57. unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
  58. enum perf_output_field {
  59. PERF_OUTPUT_COMM = 1U << 0,
  60. PERF_OUTPUT_TID = 1U << 1,
  61. PERF_OUTPUT_PID = 1U << 2,
  62. PERF_OUTPUT_TIME = 1U << 3,
  63. PERF_OUTPUT_CPU = 1U << 4,
  64. PERF_OUTPUT_EVNAME = 1U << 5,
  65. PERF_OUTPUT_TRACE = 1U << 6,
  66. PERF_OUTPUT_IP = 1U << 7,
  67. PERF_OUTPUT_SYM = 1U << 8,
  68. PERF_OUTPUT_DSO = 1U << 9,
  69. PERF_OUTPUT_ADDR = 1U << 10,
  70. PERF_OUTPUT_SYMOFFSET = 1U << 11,
  71. PERF_OUTPUT_SRCLINE = 1U << 12,
  72. PERF_OUTPUT_PERIOD = 1U << 13,
  73. PERF_OUTPUT_IREGS = 1U << 14,
  74. PERF_OUTPUT_BRSTACK = 1U << 15,
  75. PERF_OUTPUT_BRSTACKSYM = 1U << 16,
  76. PERF_OUTPUT_DATA_SRC = 1U << 17,
  77. PERF_OUTPUT_WEIGHT = 1U << 18,
  78. PERF_OUTPUT_BPF_OUTPUT = 1U << 19,
  79. PERF_OUTPUT_CALLINDENT = 1U << 20,
  80. PERF_OUTPUT_INSN = 1U << 21,
  81. PERF_OUTPUT_INSNLEN = 1U << 22,
  82. PERF_OUTPUT_BRSTACKINSN = 1U << 23,
  83. PERF_OUTPUT_BRSTACKOFF = 1U << 24,
  84. PERF_OUTPUT_SYNTH = 1U << 25,
  85. PERF_OUTPUT_PHYS_ADDR = 1U << 26,
  86. PERF_OUTPUT_UREGS = 1U << 27,
  87. };
  88. struct output_option {
  89. const char *str;
  90. enum perf_output_field field;
  91. } all_output_options[] = {
  92. {.str = "comm", .field = PERF_OUTPUT_COMM},
  93. {.str = "tid", .field = PERF_OUTPUT_TID},
  94. {.str = "pid", .field = PERF_OUTPUT_PID},
  95. {.str = "time", .field = PERF_OUTPUT_TIME},
  96. {.str = "cpu", .field = PERF_OUTPUT_CPU},
  97. {.str = "event", .field = PERF_OUTPUT_EVNAME},
  98. {.str = "trace", .field = PERF_OUTPUT_TRACE},
  99. {.str = "ip", .field = PERF_OUTPUT_IP},
  100. {.str = "sym", .field = PERF_OUTPUT_SYM},
  101. {.str = "dso", .field = PERF_OUTPUT_DSO},
  102. {.str = "addr", .field = PERF_OUTPUT_ADDR},
  103. {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
  104. {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
  105. {.str = "period", .field = PERF_OUTPUT_PERIOD},
  106. {.str = "iregs", .field = PERF_OUTPUT_IREGS},
  107. {.str = "uregs", .field = PERF_OUTPUT_UREGS},
  108. {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
  109. {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
  110. {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
  111. {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
  112. {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
  113. {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
  114. {.str = "insn", .field = PERF_OUTPUT_INSN},
  115. {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
  116. {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
  117. {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
  118. {.str = "synth", .field = PERF_OUTPUT_SYNTH},
  119. {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
  120. };
  121. enum {
  122. OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
  123. OUTPUT_TYPE_MAX
  124. };
  125. /* default set to maintain compatibility with current format */
  126. static struct {
  127. bool user_set;
  128. bool wildcard_set;
  129. unsigned int print_ip_opts;
  130. u64 fields;
  131. u64 invalid_fields;
  132. } output[OUTPUT_TYPE_MAX] = {
  133. [PERF_TYPE_HARDWARE] = {
  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. [PERF_TYPE_SOFTWARE] = {
  143. .user_set = false,
  144. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  145. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  146. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  147. PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
  148. PERF_OUTPUT_PERIOD | PERF_OUTPUT_BPF_OUTPUT,
  149. .invalid_fields = PERF_OUTPUT_TRACE,
  150. },
  151. [PERF_TYPE_TRACEPOINT] = {
  152. .user_set = false,
  153. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  154. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  155. PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
  156. },
  157. [PERF_TYPE_RAW] = {
  158. .user_set = false,
  159. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  160. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  161. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  162. PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
  163. PERF_OUTPUT_PERIOD | PERF_OUTPUT_ADDR |
  164. PERF_OUTPUT_DATA_SRC | PERF_OUTPUT_WEIGHT |
  165. PERF_OUTPUT_PHYS_ADDR,
  166. .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
  167. },
  168. [PERF_TYPE_BREAKPOINT] = {
  169. .user_set = false,
  170. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  171. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  172. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  173. PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
  174. PERF_OUTPUT_PERIOD,
  175. .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
  176. },
  177. [OUTPUT_TYPE_SYNTH] = {
  178. .user_set = false,
  179. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  180. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  181. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  182. PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
  183. PERF_OUTPUT_SYNTH,
  184. .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
  185. },
  186. };
  187. static inline int output_type(unsigned int type)
  188. {
  189. switch (type) {
  190. case PERF_TYPE_SYNTH:
  191. return OUTPUT_TYPE_SYNTH;
  192. default:
  193. return type;
  194. }
  195. }
  196. static inline unsigned int attr_type(unsigned int type)
  197. {
  198. switch (type) {
  199. case OUTPUT_TYPE_SYNTH:
  200. return PERF_TYPE_SYNTH;
  201. default:
  202. return type;
  203. }
  204. }
  205. static bool output_set_by_user(void)
  206. {
  207. int j;
  208. for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
  209. if (output[j].user_set)
  210. return true;
  211. }
  212. return false;
  213. }
  214. static const char *output_field2str(enum perf_output_field field)
  215. {
  216. int i, imax = ARRAY_SIZE(all_output_options);
  217. const char *str = "";
  218. for (i = 0; i < imax; ++i) {
  219. if (all_output_options[i].field == field) {
  220. str = all_output_options[i].str;
  221. break;
  222. }
  223. }
  224. return str;
  225. }
  226. #define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
  227. static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
  228. u64 sample_type, const char *sample_msg,
  229. enum perf_output_field field,
  230. bool allow_user_set)
  231. {
  232. struct perf_event_attr *attr = &evsel->attr;
  233. int type = output_type(attr->type);
  234. const char *evname;
  235. if (attr->sample_type & sample_type)
  236. return 0;
  237. if (output[type].user_set) {
  238. if (allow_user_set)
  239. return 0;
  240. evname = perf_evsel__name(evsel);
  241. pr_err("Samples for '%s' event do not have %s attribute set. "
  242. "Cannot print '%s' field.\n",
  243. evname, sample_msg, output_field2str(field));
  244. return -1;
  245. }
  246. /* user did not ask for it explicitly so remove from the default list */
  247. output[type].fields &= ~field;
  248. evname = perf_evsel__name(evsel);
  249. pr_debug("Samples for '%s' event do not have %s attribute set. "
  250. "Skipping '%s' field.\n",
  251. evname, sample_msg, output_field2str(field));
  252. return 0;
  253. }
  254. static int perf_evsel__check_stype(struct perf_evsel *evsel,
  255. u64 sample_type, const char *sample_msg,
  256. enum perf_output_field field)
  257. {
  258. return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
  259. false);
  260. }
  261. static int perf_evsel__check_attr(struct perf_evsel *evsel,
  262. struct perf_session *session)
  263. {
  264. struct perf_event_attr *attr = &evsel->attr;
  265. bool allow_user_set;
  266. if (perf_header__has_feat(&session->header, HEADER_STAT))
  267. return 0;
  268. allow_user_set = perf_header__has_feat(&session->header,
  269. HEADER_AUXTRACE);
  270. if (PRINT_FIELD(TRACE) &&
  271. !perf_session__has_traces(session, "record -R"))
  272. return -EINVAL;
  273. if (PRINT_FIELD(IP)) {
  274. if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
  275. PERF_OUTPUT_IP))
  276. return -EINVAL;
  277. }
  278. if (PRINT_FIELD(ADDR) &&
  279. perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
  280. PERF_OUTPUT_ADDR, allow_user_set))
  281. return -EINVAL;
  282. if (PRINT_FIELD(DATA_SRC) &&
  283. perf_evsel__check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC",
  284. PERF_OUTPUT_DATA_SRC))
  285. return -EINVAL;
  286. if (PRINT_FIELD(WEIGHT) &&
  287. perf_evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT, "WEIGHT",
  288. PERF_OUTPUT_WEIGHT))
  289. return -EINVAL;
  290. if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
  291. pr_err("Display of symbols requested but neither sample IP nor "
  292. "sample address\nis selected. Hence, no addresses to convert "
  293. "to symbols.\n");
  294. return -EINVAL;
  295. }
  296. if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
  297. pr_err("Display of offsets requested but symbol is not"
  298. "selected.\n");
  299. return -EINVAL;
  300. }
  301. if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR) &&
  302. !PRINT_FIELD(BRSTACK) && !PRINT_FIELD(BRSTACKSYM) && !PRINT_FIELD(BRSTACKOFF)) {
  303. pr_err("Display of DSO requested but no address to convert. Select\n"
  304. "sample IP, sample address, brstack, brstacksym, or brstackoff.\n");
  305. return -EINVAL;
  306. }
  307. if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
  308. pr_err("Display of source line number requested but sample IP is not\n"
  309. "selected. Hence, no address to lookup the source line number.\n");
  310. return -EINVAL;
  311. }
  312. if (PRINT_FIELD(BRSTACKINSN) &&
  313. !(perf_evlist__combined_branch_type(session->evlist) &
  314. PERF_SAMPLE_BRANCH_ANY)) {
  315. pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
  316. "Hint: run 'perf record -b ...'\n");
  317. return -EINVAL;
  318. }
  319. if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
  320. perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
  321. PERF_OUTPUT_TID|PERF_OUTPUT_PID))
  322. return -EINVAL;
  323. if (PRINT_FIELD(TIME) &&
  324. perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
  325. PERF_OUTPUT_TIME))
  326. return -EINVAL;
  327. if (PRINT_FIELD(CPU) &&
  328. perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
  329. PERF_OUTPUT_CPU, allow_user_set))
  330. return -EINVAL;
  331. if (PRINT_FIELD(PERIOD) &&
  332. perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
  333. PERF_OUTPUT_PERIOD))
  334. return -EINVAL;
  335. if (PRINT_FIELD(IREGS) &&
  336. perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
  337. PERF_OUTPUT_IREGS))
  338. return -EINVAL;
  339. if (PRINT_FIELD(UREGS) &&
  340. perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS",
  341. PERF_OUTPUT_UREGS))
  342. return -EINVAL;
  343. if (PRINT_FIELD(PHYS_ADDR) &&
  344. perf_evsel__check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR",
  345. PERF_OUTPUT_PHYS_ADDR))
  346. return -EINVAL;
  347. return 0;
  348. }
  349. static void set_print_ip_opts(struct perf_event_attr *attr)
  350. {
  351. unsigned int type = output_type(attr->type);
  352. output[type].print_ip_opts = 0;
  353. if (PRINT_FIELD(IP))
  354. output[type].print_ip_opts |= EVSEL__PRINT_IP;
  355. if (PRINT_FIELD(SYM))
  356. output[type].print_ip_opts |= EVSEL__PRINT_SYM;
  357. if (PRINT_FIELD(DSO))
  358. output[type].print_ip_opts |= EVSEL__PRINT_DSO;
  359. if (PRINT_FIELD(SYMOFFSET))
  360. output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
  361. if (PRINT_FIELD(SRCLINE))
  362. output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
  363. }
  364. /*
  365. * verify all user requested events exist and the samples
  366. * have the expected data
  367. */
  368. static int perf_session__check_output_opt(struct perf_session *session)
  369. {
  370. unsigned int j;
  371. struct perf_evsel *evsel;
  372. for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
  373. evsel = perf_session__find_first_evtype(session, attr_type(j));
  374. /*
  375. * even if fields is set to 0 (ie., show nothing) event must
  376. * exist if user explicitly includes it on the command line
  377. */
  378. if (!evsel && output[j].user_set && !output[j].wildcard_set &&
  379. j != OUTPUT_TYPE_SYNTH) {
  380. pr_err("%s events do not exist. "
  381. "Remove corresponding -F option to proceed.\n",
  382. event_type(j));
  383. return -1;
  384. }
  385. if (evsel && output[j].fields &&
  386. perf_evsel__check_attr(evsel, session))
  387. return -1;
  388. if (evsel == NULL)
  389. continue;
  390. set_print_ip_opts(&evsel->attr);
  391. }
  392. if (!no_callchain) {
  393. bool use_callchain = false;
  394. bool not_pipe = false;
  395. evlist__for_each_entry(session->evlist, evsel) {
  396. not_pipe = true;
  397. if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
  398. use_callchain = true;
  399. break;
  400. }
  401. }
  402. if (not_pipe && !use_callchain)
  403. symbol_conf.use_callchain = false;
  404. }
  405. /*
  406. * set default for tracepoints to print symbols only
  407. * if callchains are present
  408. */
  409. if (symbol_conf.use_callchain &&
  410. !output[PERF_TYPE_TRACEPOINT].user_set) {
  411. struct perf_event_attr *attr;
  412. j = PERF_TYPE_TRACEPOINT;
  413. evlist__for_each_entry(session->evlist, evsel) {
  414. if (evsel->attr.type != j)
  415. continue;
  416. attr = &evsel->attr;
  417. if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
  418. output[j].fields |= PERF_OUTPUT_IP;
  419. output[j].fields |= PERF_OUTPUT_SYM;
  420. output[j].fields |= PERF_OUTPUT_DSO;
  421. set_print_ip_opts(attr);
  422. goto out;
  423. }
  424. }
  425. }
  426. out:
  427. return 0;
  428. }
  429. static void print_sample_iregs(struct perf_sample *sample,
  430. struct perf_event_attr *attr)
  431. {
  432. struct regs_dump *regs = &sample->intr_regs;
  433. uint64_t mask = attr->sample_regs_intr;
  434. unsigned i = 0, r;
  435. if (!regs)
  436. return;
  437. for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
  438. u64 val = regs->regs[i++];
  439. printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
  440. }
  441. }
  442. static void print_sample_uregs(struct perf_sample *sample,
  443. struct perf_event_attr *attr)
  444. {
  445. struct regs_dump *regs = &sample->user_regs;
  446. uint64_t mask = attr->sample_regs_user;
  447. unsigned i = 0, r;
  448. if (!regs || !regs->regs)
  449. return;
  450. printf(" ABI:%" PRIu64 " ", regs->abi);
  451. for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
  452. u64 val = regs->regs[i++];
  453. printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
  454. }
  455. }
  456. static void print_sample_start(struct perf_sample *sample,
  457. struct thread *thread,
  458. struct perf_evsel *evsel)
  459. {
  460. struct perf_event_attr *attr = &evsel->attr;
  461. unsigned long secs;
  462. unsigned long long nsecs;
  463. if (PRINT_FIELD(COMM)) {
  464. if (latency_format)
  465. printf("%8.8s ", thread__comm_str(thread));
  466. else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
  467. printf("%s ", thread__comm_str(thread));
  468. else
  469. printf("%16s ", thread__comm_str(thread));
  470. }
  471. if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
  472. printf("%5d/%-5d ", sample->pid, sample->tid);
  473. else if (PRINT_FIELD(PID))
  474. printf("%5d ", sample->pid);
  475. else if (PRINT_FIELD(TID))
  476. printf("%5d ", sample->tid);
  477. if (PRINT_FIELD(CPU)) {
  478. if (latency_format)
  479. printf("%3d ", sample->cpu);
  480. else
  481. printf("[%03d] ", sample->cpu);
  482. }
  483. if (PRINT_FIELD(TIME)) {
  484. nsecs = sample->time;
  485. secs = nsecs / NSEC_PER_SEC;
  486. nsecs -= secs * NSEC_PER_SEC;
  487. if (nanosecs)
  488. printf("%5lu.%09llu: ", secs, nsecs);
  489. else {
  490. char sample_time[32];
  491. timestamp__scnprintf_usec(sample->time, sample_time, sizeof(sample_time));
  492. printf("%12s: ", sample_time);
  493. }
  494. }
  495. }
  496. static inline char
  497. mispred_str(struct branch_entry *br)
  498. {
  499. if (!(br->flags.mispred || br->flags.predicted))
  500. return '-';
  501. return br->flags.predicted ? 'P' : 'M';
  502. }
  503. static void print_sample_brstack(struct perf_sample *sample,
  504. struct thread *thread,
  505. struct perf_event_attr *attr)
  506. {
  507. struct branch_stack *br = sample->branch_stack;
  508. struct addr_location alf, alt;
  509. u64 i, from, to;
  510. if (!(br && br->nr))
  511. return;
  512. for (i = 0; i < br->nr; i++) {
  513. from = br->entries[i].from;
  514. to = br->entries[i].to;
  515. if (PRINT_FIELD(DSO)) {
  516. memset(&alf, 0, sizeof(alf));
  517. memset(&alt, 0, sizeof(alt));
  518. thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
  519. thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
  520. }
  521. printf("0x%"PRIx64, from);
  522. if (PRINT_FIELD(DSO)) {
  523. printf("(");
  524. map__fprintf_dsoname(alf.map, stdout);
  525. printf(")");
  526. }
  527. printf("/0x%"PRIx64, to);
  528. if (PRINT_FIELD(DSO)) {
  529. printf("(");
  530. map__fprintf_dsoname(alt.map, stdout);
  531. printf(")");
  532. }
  533. printf("/%c/%c/%c/%d ",
  534. mispred_str( br->entries + i),
  535. br->entries[i].flags.in_tx? 'X' : '-',
  536. br->entries[i].flags.abort? 'A' : '-',
  537. br->entries[i].flags.cycles);
  538. }
  539. }
  540. static void print_sample_brstacksym(struct perf_sample *sample,
  541. struct thread *thread,
  542. struct perf_event_attr *attr)
  543. {
  544. struct branch_stack *br = sample->branch_stack;
  545. struct addr_location alf, alt;
  546. u64 i, from, to;
  547. if (!(br && br->nr))
  548. return;
  549. for (i = 0; i < br->nr; i++) {
  550. memset(&alf, 0, sizeof(alf));
  551. memset(&alt, 0, sizeof(alt));
  552. from = br->entries[i].from;
  553. to = br->entries[i].to;
  554. thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
  555. if (alf.map)
  556. alf.sym = map__find_symbol(alf.map, alf.addr);
  557. thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
  558. if (alt.map)
  559. alt.sym = map__find_symbol(alt.map, alt.addr);
  560. symbol__fprintf_symname_offs(alf.sym, &alf, stdout);
  561. if (PRINT_FIELD(DSO)) {
  562. printf("(");
  563. map__fprintf_dsoname(alf.map, stdout);
  564. printf(")");
  565. }
  566. putchar('/');
  567. symbol__fprintf_symname_offs(alt.sym, &alt, stdout);
  568. if (PRINT_FIELD(DSO)) {
  569. printf("(");
  570. map__fprintf_dsoname(alt.map, stdout);
  571. printf(")");
  572. }
  573. printf("/%c/%c/%c/%d ",
  574. mispred_str( br->entries + i),
  575. br->entries[i].flags.in_tx? 'X' : '-',
  576. br->entries[i].flags.abort? 'A' : '-',
  577. br->entries[i].flags.cycles);
  578. }
  579. }
  580. static void print_sample_brstackoff(struct perf_sample *sample,
  581. struct thread *thread,
  582. struct perf_event_attr *attr)
  583. {
  584. struct branch_stack *br = sample->branch_stack;
  585. struct addr_location alf, alt;
  586. u64 i, from, to;
  587. if (!(br && br->nr))
  588. return;
  589. for (i = 0; i < br->nr; i++) {
  590. memset(&alf, 0, sizeof(alf));
  591. memset(&alt, 0, sizeof(alt));
  592. from = br->entries[i].from;
  593. to = br->entries[i].to;
  594. thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, from, &alf);
  595. if (alf.map && !alf.map->dso->adjust_symbols)
  596. from = map__map_ip(alf.map, from);
  597. thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, to, &alt);
  598. if (alt.map && !alt.map->dso->adjust_symbols)
  599. to = map__map_ip(alt.map, to);
  600. printf("0x%"PRIx64, from);
  601. if (PRINT_FIELD(DSO)) {
  602. printf("(");
  603. map__fprintf_dsoname(alf.map, stdout);
  604. printf(")");
  605. }
  606. printf("/0x%"PRIx64, to);
  607. if (PRINT_FIELD(DSO)) {
  608. printf("(");
  609. map__fprintf_dsoname(alt.map, stdout);
  610. printf(")");
  611. }
  612. printf("/%c/%c/%c/%d ",
  613. mispred_str(br->entries + i),
  614. br->entries[i].flags.in_tx ? 'X' : '-',
  615. br->entries[i].flags.abort ? 'A' : '-',
  616. br->entries[i].flags.cycles);
  617. }
  618. }
  619. #define MAXBB 16384UL
  620. static int grab_bb(u8 *buffer, u64 start, u64 end,
  621. struct machine *machine, struct thread *thread,
  622. bool *is64bit, u8 *cpumode, bool last)
  623. {
  624. long offset, len;
  625. struct addr_location al;
  626. bool kernel;
  627. if (!start || !end)
  628. return 0;
  629. kernel = machine__kernel_ip(machine, start);
  630. if (kernel)
  631. *cpumode = PERF_RECORD_MISC_KERNEL;
  632. else
  633. *cpumode = PERF_RECORD_MISC_USER;
  634. /*
  635. * Block overlaps between kernel and user.
  636. * This can happen due to ring filtering
  637. * On Intel CPUs the entry into the kernel is filtered,
  638. * but the exit is not. Let the caller patch it up.
  639. */
  640. if (kernel != machine__kernel_ip(machine, end)) {
  641. printf("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n",
  642. start, end);
  643. return -ENXIO;
  644. }
  645. memset(&al, 0, sizeof(al));
  646. if (end - start > MAXBB - MAXINSN) {
  647. if (last)
  648. printf("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
  649. else
  650. printf("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
  651. return 0;
  652. }
  653. thread__find_addr_map(thread, *cpumode, MAP__FUNCTION, start, &al);
  654. if (!al.map || !al.map->dso) {
  655. printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
  656. return 0;
  657. }
  658. if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
  659. printf("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
  660. return 0;
  661. }
  662. /* Load maps to ensure dso->is_64_bit has been updated */
  663. map__load(al.map);
  664. offset = al.map->map_ip(al.map, start);
  665. len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
  666. end - start + MAXINSN);
  667. *is64bit = al.map->dso->is_64_bit;
  668. if (len <= 0)
  669. printf("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
  670. start, end);
  671. return len;
  672. }
  673. static void print_jump(uint64_t ip, struct branch_entry *en,
  674. struct perf_insn *x, u8 *inbuf, int len,
  675. int insn)
  676. {
  677. printf("\t%016" PRIx64 "\t%-30s\t#%s%s%s%s",
  678. ip,
  679. dump_insn(x, ip, inbuf, len, NULL),
  680. en->flags.predicted ? " PRED" : "",
  681. en->flags.mispred ? " MISPRED" : "",
  682. en->flags.in_tx ? " INTX" : "",
  683. en->flags.abort ? " ABORT" : "");
  684. if (en->flags.cycles) {
  685. printf(" %d cycles", en->flags.cycles);
  686. if (insn)
  687. printf(" %.2f IPC", (float)insn / en->flags.cycles);
  688. }
  689. putchar('\n');
  690. }
  691. static void print_ip_sym(struct thread *thread, u8 cpumode, int cpu,
  692. uint64_t addr, struct symbol **lastsym,
  693. struct perf_event_attr *attr)
  694. {
  695. struct addr_location al;
  696. int off;
  697. memset(&al, 0, sizeof(al));
  698. thread__find_addr_map(thread, cpumode, MAP__FUNCTION, addr, &al);
  699. if (!al.map)
  700. thread__find_addr_map(thread, cpumode, MAP__VARIABLE,
  701. addr, &al);
  702. if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
  703. return;
  704. al.cpu = cpu;
  705. al.sym = NULL;
  706. if (al.map)
  707. al.sym = map__find_symbol(al.map, al.addr);
  708. if (!al.sym)
  709. return;
  710. if (al.addr < al.sym->end)
  711. off = al.addr - al.sym->start;
  712. else
  713. off = al.addr - al.map->start - al.sym->start;
  714. printf("\t%s", al.sym->name);
  715. if (off)
  716. printf("%+d", off);
  717. putchar(':');
  718. if (PRINT_FIELD(SRCLINE))
  719. map__fprintf_srcline(al.map, al.addr, "\t", stdout);
  720. putchar('\n');
  721. *lastsym = al.sym;
  722. }
  723. static void print_sample_brstackinsn(struct perf_sample *sample,
  724. struct thread *thread,
  725. struct perf_event_attr *attr,
  726. struct machine *machine)
  727. {
  728. struct branch_stack *br = sample->branch_stack;
  729. u64 start, end;
  730. int i, insn, len, nr, ilen;
  731. struct perf_insn x;
  732. u8 buffer[MAXBB];
  733. unsigned off;
  734. struct symbol *lastsym = NULL;
  735. if (!(br && br->nr))
  736. return;
  737. nr = br->nr;
  738. if (max_blocks && nr > max_blocks + 1)
  739. nr = max_blocks + 1;
  740. x.thread = thread;
  741. x.cpu = sample->cpu;
  742. putchar('\n');
  743. /* Handle first from jump, of which we don't know the entry. */
  744. len = grab_bb(buffer, br->entries[nr-1].from,
  745. br->entries[nr-1].from,
  746. machine, thread, &x.is64bit, &x.cpumode, false);
  747. if (len > 0) {
  748. print_ip_sym(thread, x.cpumode, x.cpu,
  749. br->entries[nr - 1].from, &lastsym, attr);
  750. print_jump(br->entries[nr - 1].from, &br->entries[nr - 1],
  751. &x, buffer, len, 0);
  752. }
  753. /* Print all blocks */
  754. for (i = nr - 2; i >= 0; i--) {
  755. if (br->entries[i].from || br->entries[i].to)
  756. pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
  757. br->entries[i].from,
  758. br->entries[i].to);
  759. start = br->entries[i + 1].to;
  760. end = br->entries[i].from;
  761. len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
  762. /* Patch up missing kernel transfers due to ring filters */
  763. if (len == -ENXIO && i > 0) {
  764. end = br->entries[--i].from;
  765. pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
  766. len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
  767. }
  768. if (len <= 0)
  769. continue;
  770. insn = 0;
  771. for (off = 0;; off += ilen) {
  772. uint64_t ip = start + off;
  773. print_ip_sym(thread, x.cpumode, x.cpu, ip, &lastsym, attr);
  774. if (ip == end) {
  775. print_jump(ip, &br->entries[i], &x, buffer + off, len - off, insn);
  776. break;
  777. } else {
  778. printf("\t%016" PRIx64 "\t%s\n", ip,
  779. dump_insn(&x, ip, buffer + off, len - off, &ilen));
  780. if (ilen == 0)
  781. break;
  782. insn++;
  783. }
  784. }
  785. }
  786. /*
  787. * Hit the branch? In this case we are already done, and the target
  788. * has not been executed yet.
  789. */
  790. if (br->entries[0].from == sample->ip)
  791. return;
  792. if (br->entries[0].flags.abort)
  793. return;
  794. /*
  795. * Print final block upto sample
  796. */
  797. start = br->entries[0].to;
  798. end = sample->ip;
  799. len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
  800. print_ip_sym(thread, x.cpumode, x.cpu, start, &lastsym, attr);
  801. if (len <= 0) {
  802. /* Print at least last IP if basic block did not work */
  803. len = grab_bb(buffer, sample->ip, sample->ip,
  804. machine, thread, &x.is64bit, &x.cpumode, false);
  805. if (len <= 0)
  806. return;
  807. printf("\t%016" PRIx64 "\t%s\n", sample->ip,
  808. dump_insn(&x, sample->ip, buffer, len, NULL));
  809. return;
  810. }
  811. for (off = 0; off <= end - start; off += ilen) {
  812. printf("\t%016" PRIx64 "\t%s\n", start + off,
  813. dump_insn(&x, start + off, buffer + off, len - off, &ilen));
  814. if (ilen == 0)
  815. break;
  816. }
  817. }
  818. static void print_sample_addr(struct perf_sample *sample,
  819. struct thread *thread,
  820. struct perf_event_attr *attr)
  821. {
  822. struct addr_location al;
  823. printf("%16" PRIx64, sample->addr);
  824. if (!sample_addr_correlates_sym(attr))
  825. return;
  826. thread__resolve(thread, &al, sample);
  827. if (PRINT_FIELD(SYM)) {
  828. printf(" ");
  829. if (PRINT_FIELD(SYMOFFSET))
  830. symbol__fprintf_symname_offs(al.sym, &al, stdout);
  831. else
  832. symbol__fprintf_symname(al.sym, stdout);
  833. }
  834. if (PRINT_FIELD(DSO)) {
  835. printf(" (");
  836. map__fprintf_dsoname(al.map, stdout);
  837. printf(")");
  838. }
  839. }
  840. static void print_sample_callindent(struct perf_sample *sample,
  841. struct perf_evsel *evsel,
  842. struct thread *thread,
  843. struct addr_location *al)
  844. {
  845. struct perf_event_attr *attr = &evsel->attr;
  846. size_t depth = thread_stack__depth(thread);
  847. struct addr_location addr_al;
  848. const char *name = NULL;
  849. static int spacing;
  850. int len = 0;
  851. u64 ip = 0;
  852. /*
  853. * The 'return' has already been popped off the stack so the depth has
  854. * to be adjusted to match the 'call'.
  855. */
  856. if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
  857. depth += 1;
  858. if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
  859. if (sample_addr_correlates_sym(attr)) {
  860. thread__resolve(thread, &addr_al, sample);
  861. if (addr_al.sym)
  862. name = addr_al.sym->name;
  863. else
  864. ip = sample->addr;
  865. } else {
  866. ip = sample->addr;
  867. }
  868. } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
  869. if (al->sym)
  870. name = al->sym->name;
  871. else
  872. ip = sample->ip;
  873. }
  874. if (name)
  875. len = printf("%*s%s", (int)depth * 4, "", name);
  876. else if (ip)
  877. len = printf("%*s%16" PRIx64, (int)depth * 4, "", ip);
  878. if (len < 0)
  879. return;
  880. /*
  881. * Try to keep the output length from changing frequently so that the
  882. * output lines up more nicely.
  883. */
  884. if (len > spacing || (len && len < spacing - 52))
  885. spacing = round_up(len + 4, 32);
  886. if (len < spacing)
  887. printf("%*s", spacing - len, "");
  888. }
  889. static void print_insn(struct perf_sample *sample,
  890. struct perf_event_attr *attr,
  891. struct thread *thread,
  892. struct machine *machine)
  893. {
  894. if (PRINT_FIELD(INSNLEN))
  895. printf(" ilen: %d", sample->insn_len);
  896. if (PRINT_FIELD(INSN)) {
  897. int i;
  898. printf(" insn:");
  899. for (i = 0; i < sample->insn_len; i++)
  900. printf(" %02x", (unsigned char)sample->insn[i]);
  901. }
  902. if (PRINT_FIELD(BRSTACKINSN))
  903. print_sample_brstackinsn(sample, thread, attr, machine);
  904. }
  905. static void print_sample_bts(struct perf_sample *sample,
  906. struct perf_evsel *evsel,
  907. struct thread *thread,
  908. struct addr_location *al,
  909. struct machine *machine)
  910. {
  911. struct perf_event_attr *attr = &evsel->attr;
  912. unsigned int type = output_type(attr->type);
  913. bool print_srcline_last = false;
  914. if (PRINT_FIELD(CALLINDENT))
  915. print_sample_callindent(sample, evsel, thread, al);
  916. /* print branch_from information */
  917. if (PRINT_FIELD(IP)) {
  918. unsigned int print_opts = output[type].print_ip_opts;
  919. struct callchain_cursor *cursor = NULL;
  920. if (symbol_conf.use_callchain && sample->callchain &&
  921. thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
  922. sample, NULL, NULL, scripting_max_stack) == 0)
  923. cursor = &callchain_cursor;
  924. if (cursor == NULL) {
  925. putchar(' ');
  926. if (print_opts & EVSEL__PRINT_SRCLINE) {
  927. print_srcline_last = true;
  928. print_opts &= ~EVSEL__PRINT_SRCLINE;
  929. }
  930. } else
  931. putchar('\n');
  932. sample__fprintf_sym(sample, al, 0, print_opts, cursor, stdout);
  933. }
  934. /* print branch_to information */
  935. if (PRINT_FIELD(ADDR) ||
  936. ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
  937. !output[type].user_set)) {
  938. printf(" => ");
  939. print_sample_addr(sample, thread, attr);
  940. }
  941. if (print_srcline_last)
  942. map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
  943. print_insn(sample, attr, thread, machine);
  944. printf("\n");
  945. }
  946. static struct {
  947. u32 flags;
  948. const char *name;
  949. } sample_flags[] = {
  950. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
  951. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
  952. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
  953. {PERF_IP_FLAG_BRANCH, "jmp"},
  954. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
  955. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
  956. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
  957. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
  958. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
  959. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
  960. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
  961. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
  962. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
  963. {0, NULL}
  964. };
  965. static void print_sample_flags(u32 flags)
  966. {
  967. const char *chars = PERF_IP_FLAG_CHARS;
  968. const int n = strlen(PERF_IP_FLAG_CHARS);
  969. bool in_tx = flags & PERF_IP_FLAG_IN_TX;
  970. const char *name = NULL;
  971. char str[33];
  972. int i, pos = 0;
  973. for (i = 0; sample_flags[i].name ; i++) {
  974. if (sample_flags[i].flags == (flags & ~PERF_IP_FLAG_IN_TX)) {
  975. name = sample_flags[i].name;
  976. break;
  977. }
  978. }
  979. for (i = 0; i < n; i++, flags >>= 1) {
  980. if (flags & 1)
  981. str[pos++] = chars[i];
  982. }
  983. for (; i < 32; i++, flags >>= 1) {
  984. if (flags & 1)
  985. str[pos++] = '?';
  986. }
  987. str[pos] = 0;
  988. if (name)
  989. printf(" %-7s%4s ", name, in_tx ? "(x)" : "");
  990. else
  991. printf(" %-11s ", str);
  992. }
  993. struct printer_data {
  994. int line_no;
  995. bool hit_nul;
  996. bool is_printable;
  997. };
  998. static void
  999. print_sample_bpf_output_printer(enum binary_printer_ops op,
  1000. unsigned int val,
  1001. void *extra)
  1002. {
  1003. unsigned char ch = (unsigned char)val;
  1004. struct printer_data *printer_data = extra;
  1005. switch (op) {
  1006. case BINARY_PRINT_DATA_BEGIN:
  1007. printf("\n");
  1008. break;
  1009. case BINARY_PRINT_LINE_BEGIN:
  1010. printf("%17s", !printer_data->line_no ? "BPF output:" :
  1011. " ");
  1012. break;
  1013. case BINARY_PRINT_ADDR:
  1014. printf(" %04x:", val);
  1015. break;
  1016. case BINARY_PRINT_NUM_DATA:
  1017. printf(" %02x", val);
  1018. break;
  1019. case BINARY_PRINT_NUM_PAD:
  1020. printf(" ");
  1021. break;
  1022. case BINARY_PRINT_SEP:
  1023. printf(" ");
  1024. break;
  1025. case BINARY_PRINT_CHAR_DATA:
  1026. if (printer_data->hit_nul && ch)
  1027. printer_data->is_printable = false;
  1028. if (!isprint(ch)) {
  1029. printf("%c", '.');
  1030. if (!printer_data->is_printable)
  1031. break;
  1032. if (ch == '\0')
  1033. printer_data->hit_nul = true;
  1034. else
  1035. printer_data->is_printable = false;
  1036. } else {
  1037. printf("%c", ch);
  1038. }
  1039. break;
  1040. case BINARY_PRINT_CHAR_PAD:
  1041. printf(" ");
  1042. break;
  1043. case BINARY_PRINT_LINE_END:
  1044. printf("\n");
  1045. printer_data->line_no++;
  1046. break;
  1047. case BINARY_PRINT_DATA_END:
  1048. default:
  1049. break;
  1050. }
  1051. }
  1052. static void print_sample_bpf_output(struct perf_sample *sample)
  1053. {
  1054. unsigned int nr_bytes = sample->raw_size;
  1055. struct printer_data printer_data = {0, false, true};
  1056. print_binary(sample->raw_data, nr_bytes, 8,
  1057. print_sample_bpf_output_printer, &printer_data);
  1058. if (printer_data.is_printable && printer_data.hit_nul)
  1059. printf("%17s \"%s\"\n", "BPF string:",
  1060. (char *)(sample->raw_data));
  1061. }
  1062. static void print_sample_spacing(int len, int spacing)
  1063. {
  1064. if (len > 0 && len < spacing)
  1065. printf("%*s", spacing - len, "");
  1066. }
  1067. static void print_sample_pt_spacing(int len)
  1068. {
  1069. print_sample_spacing(len, 34);
  1070. }
  1071. static void print_sample_synth_ptwrite(struct perf_sample *sample)
  1072. {
  1073. struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
  1074. int len;
  1075. if (perf_sample__bad_synth_size(sample, *data))
  1076. return;
  1077. len = printf(" IP: %u payload: %#" PRIx64 " ",
  1078. data->ip, le64_to_cpu(data->payload));
  1079. print_sample_pt_spacing(len);
  1080. }
  1081. static void print_sample_synth_mwait(struct perf_sample *sample)
  1082. {
  1083. struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
  1084. int len;
  1085. if (perf_sample__bad_synth_size(sample, *data))
  1086. return;
  1087. len = printf(" hints: %#x extensions: %#x ",
  1088. data->hints, data->extensions);
  1089. print_sample_pt_spacing(len);
  1090. }
  1091. static void print_sample_synth_pwre(struct perf_sample *sample)
  1092. {
  1093. struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
  1094. int len;
  1095. if (perf_sample__bad_synth_size(sample, *data))
  1096. return;
  1097. len = printf(" hw: %u cstate: %u sub-cstate: %u ",
  1098. data->hw, data->cstate, data->subcstate);
  1099. print_sample_pt_spacing(len);
  1100. }
  1101. static void print_sample_synth_exstop(struct perf_sample *sample)
  1102. {
  1103. struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
  1104. int len;
  1105. if (perf_sample__bad_synth_size(sample, *data))
  1106. return;
  1107. len = printf(" IP: %u ", data->ip);
  1108. print_sample_pt_spacing(len);
  1109. }
  1110. static void print_sample_synth_pwrx(struct perf_sample *sample)
  1111. {
  1112. struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
  1113. int len;
  1114. if (perf_sample__bad_synth_size(sample, *data))
  1115. return;
  1116. len = printf(" deepest cstate: %u last cstate: %u wake reason: %#x ",
  1117. data->deepest_cstate, data->last_cstate,
  1118. data->wake_reason);
  1119. print_sample_pt_spacing(len);
  1120. }
  1121. static void print_sample_synth_cbr(struct perf_sample *sample)
  1122. {
  1123. struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
  1124. unsigned int percent, freq;
  1125. int len;
  1126. if (perf_sample__bad_synth_size(sample, *data))
  1127. return;
  1128. freq = (le32_to_cpu(data->freq) + 500) / 1000;
  1129. len = printf(" cbr: %2u freq: %4u MHz ", data->cbr, freq);
  1130. if (data->max_nonturbo) {
  1131. percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
  1132. len += printf("(%3u%%) ", percent);
  1133. }
  1134. print_sample_pt_spacing(len);
  1135. }
  1136. static void print_sample_synth(struct perf_sample *sample,
  1137. struct perf_evsel *evsel)
  1138. {
  1139. switch (evsel->attr.config) {
  1140. case PERF_SYNTH_INTEL_PTWRITE:
  1141. print_sample_synth_ptwrite(sample);
  1142. break;
  1143. case PERF_SYNTH_INTEL_MWAIT:
  1144. print_sample_synth_mwait(sample);
  1145. break;
  1146. case PERF_SYNTH_INTEL_PWRE:
  1147. print_sample_synth_pwre(sample);
  1148. break;
  1149. case PERF_SYNTH_INTEL_EXSTOP:
  1150. print_sample_synth_exstop(sample);
  1151. break;
  1152. case PERF_SYNTH_INTEL_PWRX:
  1153. print_sample_synth_pwrx(sample);
  1154. break;
  1155. case PERF_SYNTH_INTEL_CBR:
  1156. print_sample_synth_cbr(sample);
  1157. break;
  1158. default:
  1159. break;
  1160. }
  1161. }
  1162. struct perf_script {
  1163. struct perf_tool tool;
  1164. struct perf_session *session;
  1165. bool show_task_events;
  1166. bool show_mmap_events;
  1167. bool show_switch_events;
  1168. bool show_namespace_events;
  1169. bool allocated;
  1170. struct cpu_map *cpus;
  1171. struct thread_map *threads;
  1172. int name_width;
  1173. const char *time_str;
  1174. struct perf_time_interval ptime;
  1175. };
  1176. static int perf_evlist__max_name_len(struct perf_evlist *evlist)
  1177. {
  1178. struct perf_evsel *evsel;
  1179. int max = 0;
  1180. evlist__for_each_entry(evlist, evsel) {
  1181. int len = strlen(perf_evsel__name(evsel));
  1182. max = MAX(len, max);
  1183. }
  1184. return max;
  1185. }
  1186. static size_t data_src__printf(u64 data_src)
  1187. {
  1188. struct mem_info mi = { .data_src.val = data_src };
  1189. char decode[100];
  1190. char out[100];
  1191. static int maxlen;
  1192. int len;
  1193. perf_script__meminfo_scnprintf(decode, 100, &mi);
  1194. len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
  1195. if (maxlen < len)
  1196. maxlen = len;
  1197. return printf("%-*s", maxlen, out);
  1198. }
  1199. static void process_event(struct perf_script *script,
  1200. struct perf_sample *sample, struct perf_evsel *evsel,
  1201. struct addr_location *al,
  1202. struct machine *machine)
  1203. {
  1204. struct thread *thread = al->thread;
  1205. struct perf_event_attr *attr = &evsel->attr;
  1206. unsigned int type = output_type(attr->type);
  1207. if (output[type].fields == 0)
  1208. return;
  1209. print_sample_start(sample, thread, evsel);
  1210. if (PRINT_FIELD(PERIOD))
  1211. printf("%10" PRIu64 " ", sample->period);
  1212. if (PRINT_FIELD(EVNAME)) {
  1213. const char *evname = perf_evsel__name(evsel);
  1214. if (!script->name_width)
  1215. script->name_width = perf_evlist__max_name_len(script->session->evlist);
  1216. printf("%*s: ", script->name_width,
  1217. evname ? evname : "[unknown]");
  1218. }
  1219. if (print_flags)
  1220. print_sample_flags(sample->flags);
  1221. if (is_bts_event(attr)) {
  1222. print_sample_bts(sample, evsel, thread, al, machine);
  1223. return;
  1224. }
  1225. if (PRINT_FIELD(TRACE))
  1226. event_format__print(evsel->tp_format, sample->cpu,
  1227. sample->raw_data, sample->raw_size);
  1228. if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
  1229. print_sample_synth(sample, evsel);
  1230. if (PRINT_FIELD(ADDR))
  1231. print_sample_addr(sample, thread, attr);
  1232. if (PRINT_FIELD(DATA_SRC))
  1233. data_src__printf(sample->data_src);
  1234. if (PRINT_FIELD(WEIGHT))
  1235. printf("%16" PRIu64, sample->weight);
  1236. if (PRINT_FIELD(IP)) {
  1237. struct callchain_cursor *cursor = NULL;
  1238. if (symbol_conf.use_callchain && sample->callchain &&
  1239. thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
  1240. sample, NULL, NULL, scripting_max_stack) == 0)
  1241. cursor = &callchain_cursor;
  1242. putchar(cursor ? '\n' : ' ');
  1243. sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, stdout);
  1244. }
  1245. if (PRINT_FIELD(IREGS))
  1246. print_sample_iregs(sample, attr);
  1247. if (PRINT_FIELD(UREGS))
  1248. print_sample_uregs(sample, attr);
  1249. if (PRINT_FIELD(BRSTACK))
  1250. print_sample_brstack(sample, thread, attr);
  1251. else if (PRINT_FIELD(BRSTACKSYM))
  1252. print_sample_brstacksym(sample, thread, attr);
  1253. else if (PRINT_FIELD(BRSTACKOFF))
  1254. print_sample_brstackoff(sample, thread, attr);
  1255. if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
  1256. print_sample_bpf_output(sample);
  1257. print_insn(sample, attr, thread, machine);
  1258. if (PRINT_FIELD(PHYS_ADDR))
  1259. printf("%16" PRIx64, sample->phys_addr);
  1260. printf("\n");
  1261. }
  1262. static struct scripting_ops *scripting_ops;
  1263. static void __process_stat(struct perf_evsel *counter, u64 tstamp)
  1264. {
  1265. int nthreads = thread_map__nr(counter->threads);
  1266. int ncpus = perf_evsel__nr_cpus(counter);
  1267. int cpu, thread;
  1268. static int header_printed;
  1269. if (counter->system_wide)
  1270. nthreads = 1;
  1271. if (!header_printed) {
  1272. printf("%3s %8s %15s %15s %15s %15s %s\n",
  1273. "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
  1274. header_printed = 1;
  1275. }
  1276. for (thread = 0; thread < nthreads; thread++) {
  1277. for (cpu = 0; cpu < ncpus; cpu++) {
  1278. struct perf_counts_values *counts;
  1279. counts = perf_counts(counter->counts, cpu, thread);
  1280. printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
  1281. counter->cpus->map[cpu],
  1282. thread_map__pid(counter->threads, thread),
  1283. counts->val,
  1284. counts->ena,
  1285. counts->run,
  1286. tstamp,
  1287. perf_evsel__name(counter));
  1288. }
  1289. }
  1290. }
  1291. static void process_stat(struct perf_evsel *counter, u64 tstamp)
  1292. {
  1293. if (scripting_ops && scripting_ops->process_stat)
  1294. scripting_ops->process_stat(&stat_config, counter, tstamp);
  1295. else
  1296. __process_stat(counter, tstamp);
  1297. }
  1298. static void process_stat_interval(u64 tstamp)
  1299. {
  1300. if (scripting_ops && scripting_ops->process_stat_interval)
  1301. scripting_ops->process_stat_interval(tstamp);
  1302. }
  1303. static void setup_scripting(void)
  1304. {
  1305. setup_perl_scripting();
  1306. setup_python_scripting();
  1307. }
  1308. static int flush_scripting(void)
  1309. {
  1310. return scripting_ops ? scripting_ops->flush_script() : 0;
  1311. }
  1312. static int cleanup_scripting(void)
  1313. {
  1314. pr_debug("\nperf script stopped\n");
  1315. return scripting_ops ? scripting_ops->stop_script() : 0;
  1316. }
  1317. static int process_sample_event(struct perf_tool *tool,
  1318. union perf_event *event,
  1319. struct perf_sample *sample,
  1320. struct perf_evsel *evsel,
  1321. struct machine *machine)
  1322. {
  1323. struct perf_script *scr = container_of(tool, struct perf_script, tool);
  1324. struct addr_location al;
  1325. if (perf_time__skip_sample(&scr->ptime, sample->time))
  1326. return 0;
  1327. if (debug_mode) {
  1328. if (sample->time < last_timestamp) {
  1329. pr_err("Samples misordered, previous: %" PRIu64
  1330. " this: %" PRIu64 "\n", last_timestamp,
  1331. sample->time);
  1332. nr_unordered++;
  1333. }
  1334. last_timestamp = sample->time;
  1335. return 0;
  1336. }
  1337. if (machine__resolve(machine, &al, sample) < 0) {
  1338. pr_err("problem processing %d event, skipping it.\n",
  1339. event->header.type);
  1340. return -1;
  1341. }
  1342. if (al.filtered)
  1343. goto out_put;
  1344. if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
  1345. goto out_put;
  1346. if (scripting_ops)
  1347. scripting_ops->process_event(event, sample, evsel, &al);
  1348. else
  1349. process_event(scr, sample, evsel, &al, machine);
  1350. out_put:
  1351. addr_location__put(&al);
  1352. return 0;
  1353. }
  1354. static int process_attr(struct perf_tool *tool, union perf_event *event,
  1355. struct perf_evlist **pevlist)
  1356. {
  1357. struct perf_script *scr = container_of(tool, struct perf_script, tool);
  1358. struct perf_evlist *evlist;
  1359. struct perf_evsel *evsel, *pos;
  1360. int err;
  1361. err = perf_event__process_attr(tool, event, pevlist);
  1362. if (err)
  1363. return err;
  1364. evlist = *pevlist;
  1365. evsel = perf_evlist__last(*pevlist);
  1366. if (evsel->attr.type >= PERF_TYPE_MAX &&
  1367. evsel->attr.type != PERF_TYPE_SYNTH)
  1368. return 0;
  1369. evlist__for_each_entry(evlist, pos) {
  1370. if (pos->attr.type == evsel->attr.type && pos != evsel)
  1371. return 0;
  1372. }
  1373. set_print_ip_opts(&evsel->attr);
  1374. if (evsel->attr.sample_type)
  1375. err = perf_evsel__check_attr(evsel, scr->session);
  1376. return err;
  1377. }
  1378. static int process_comm_event(struct perf_tool *tool,
  1379. union perf_event *event,
  1380. struct perf_sample *sample,
  1381. struct machine *machine)
  1382. {
  1383. struct thread *thread;
  1384. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1385. struct perf_session *session = script->session;
  1386. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  1387. int ret = -1;
  1388. thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
  1389. if (thread == NULL) {
  1390. pr_debug("problem processing COMM event, skipping it.\n");
  1391. return -1;
  1392. }
  1393. if (perf_event__process_comm(tool, event, sample, machine) < 0)
  1394. goto out;
  1395. if (!evsel->attr.sample_id_all) {
  1396. sample->cpu = 0;
  1397. sample->time = 0;
  1398. sample->tid = event->comm.tid;
  1399. sample->pid = event->comm.pid;
  1400. }
  1401. print_sample_start(sample, thread, evsel);
  1402. perf_event__fprintf(event, stdout);
  1403. ret = 0;
  1404. out:
  1405. thread__put(thread);
  1406. return ret;
  1407. }
  1408. static int process_namespaces_event(struct perf_tool *tool,
  1409. union perf_event *event,
  1410. struct perf_sample *sample,
  1411. struct machine *machine)
  1412. {
  1413. struct thread *thread;
  1414. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1415. struct perf_session *session = script->session;
  1416. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  1417. int ret = -1;
  1418. thread = machine__findnew_thread(machine, event->namespaces.pid,
  1419. event->namespaces.tid);
  1420. if (thread == NULL) {
  1421. pr_debug("problem processing NAMESPACES event, skipping it.\n");
  1422. return -1;
  1423. }
  1424. if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
  1425. goto out;
  1426. if (!evsel->attr.sample_id_all) {
  1427. sample->cpu = 0;
  1428. sample->time = 0;
  1429. sample->tid = event->namespaces.tid;
  1430. sample->pid = event->namespaces.pid;
  1431. }
  1432. print_sample_start(sample, thread, evsel);
  1433. perf_event__fprintf(event, stdout);
  1434. ret = 0;
  1435. out:
  1436. thread__put(thread);
  1437. return ret;
  1438. }
  1439. static int process_fork_event(struct perf_tool *tool,
  1440. union perf_event *event,
  1441. struct perf_sample *sample,
  1442. struct machine *machine)
  1443. {
  1444. struct thread *thread;
  1445. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1446. struct perf_session *session = script->session;
  1447. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  1448. if (perf_event__process_fork(tool, event, sample, machine) < 0)
  1449. return -1;
  1450. thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
  1451. if (thread == NULL) {
  1452. pr_debug("problem processing FORK event, skipping it.\n");
  1453. return -1;
  1454. }
  1455. if (!evsel->attr.sample_id_all) {
  1456. sample->cpu = 0;
  1457. sample->time = event->fork.time;
  1458. sample->tid = event->fork.tid;
  1459. sample->pid = event->fork.pid;
  1460. }
  1461. print_sample_start(sample, thread, evsel);
  1462. perf_event__fprintf(event, stdout);
  1463. thread__put(thread);
  1464. return 0;
  1465. }
  1466. static int process_exit_event(struct perf_tool *tool,
  1467. union perf_event *event,
  1468. struct perf_sample *sample,
  1469. struct machine *machine)
  1470. {
  1471. int err = 0;
  1472. struct thread *thread;
  1473. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1474. struct perf_session *session = script->session;
  1475. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  1476. thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
  1477. if (thread == NULL) {
  1478. pr_debug("problem processing EXIT event, skipping it.\n");
  1479. return -1;
  1480. }
  1481. if (!evsel->attr.sample_id_all) {
  1482. sample->cpu = 0;
  1483. sample->time = 0;
  1484. sample->tid = event->fork.tid;
  1485. sample->pid = event->fork.pid;
  1486. }
  1487. print_sample_start(sample, thread, evsel);
  1488. perf_event__fprintf(event, stdout);
  1489. if (perf_event__process_exit(tool, event, sample, machine) < 0)
  1490. err = -1;
  1491. thread__put(thread);
  1492. return err;
  1493. }
  1494. static int process_mmap_event(struct perf_tool *tool,
  1495. union perf_event *event,
  1496. struct perf_sample *sample,
  1497. struct machine *machine)
  1498. {
  1499. struct thread *thread;
  1500. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1501. struct perf_session *session = script->session;
  1502. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  1503. if (perf_event__process_mmap(tool, event, sample, machine) < 0)
  1504. return -1;
  1505. thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
  1506. if (thread == NULL) {
  1507. pr_debug("problem processing MMAP event, skipping it.\n");
  1508. return -1;
  1509. }
  1510. if (!evsel->attr.sample_id_all) {
  1511. sample->cpu = 0;
  1512. sample->time = 0;
  1513. sample->tid = event->mmap.tid;
  1514. sample->pid = event->mmap.pid;
  1515. }
  1516. print_sample_start(sample, thread, evsel);
  1517. perf_event__fprintf(event, stdout);
  1518. thread__put(thread);
  1519. return 0;
  1520. }
  1521. static int process_mmap2_event(struct perf_tool *tool,
  1522. union perf_event *event,
  1523. struct perf_sample *sample,
  1524. struct machine *machine)
  1525. {
  1526. struct thread *thread;
  1527. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1528. struct perf_session *session = script->session;
  1529. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  1530. if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
  1531. return -1;
  1532. thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
  1533. if (thread == NULL) {
  1534. pr_debug("problem processing MMAP2 event, skipping it.\n");
  1535. return -1;
  1536. }
  1537. if (!evsel->attr.sample_id_all) {
  1538. sample->cpu = 0;
  1539. sample->time = 0;
  1540. sample->tid = event->mmap2.tid;
  1541. sample->pid = event->mmap2.pid;
  1542. }
  1543. print_sample_start(sample, thread, evsel);
  1544. perf_event__fprintf(event, stdout);
  1545. thread__put(thread);
  1546. return 0;
  1547. }
  1548. static int process_switch_event(struct perf_tool *tool,
  1549. union perf_event *event,
  1550. struct perf_sample *sample,
  1551. struct machine *machine)
  1552. {
  1553. struct thread *thread;
  1554. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1555. struct perf_session *session = script->session;
  1556. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  1557. if (perf_event__process_switch(tool, event, sample, machine) < 0)
  1558. return -1;
  1559. thread = machine__findnew_thread(machine, sample->pid,
  1560. sample->tid);
  1561. if (thread == NULL) {
  1562. pr_debug("problem processing SWITCH event, skipping it.\n");
  1563. return -1;
  1564. }
  1565. print_sample_start(sample, thread, evsel);
  1566. perf_event__fprintf(event, stdout);
  1567. thread__put(thread);
  1568. return 0;
  1569. }
  1570. static void sig_handler(int sig __maybe_unused)
  1571. {
  1572. session_done = 1;
  1573. }
  1574. static int __cmd_script(struct perf_script *script)
  1575. {
  1576. int ret;
  1577. signal(SIGINT, sig_handler);
  1578. /* override event processing functions */
  1579. if (script->show_task_events) {
  1580. script->tool.comm = process_comm_event;
  1581. script->tool.fork = process_fork_event;
  1582. script->tool.exit = process_exit_event;
  1583. }
  1584. if (script->show_mmap_events) {
  1585. script->tool.mmap = process_mmap_event;
  1586. script->tool.mmap2 = process_mmap2_event;
  1587. }
  1588. if (script->show_switch_events)
  1589. script->tool.context_switch = process_switch_event;
  1590. if (script->show_namespace_events)
  1591. script->tool.namespaces = process_namespaces_event;
  1592. ret = perf_session__process_events(script->session);
  1593. if (debug_mode)
  1594. pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
  1595. return ret;
  1596. }
  1597. struct script_spec {
  1598. struct list_head node;
  1599. struct scripting_ops *ops;
  1600. char spec[0];
  1601. };
  1602. static LIST_HEAD(script_specs);
  1603. static struct script_spec *script_spec__new(const char *spec,
  1604. struct scripting_ops *ops)
  1605. {
  1606. struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
  1607. if (s != NULL) {
  1608. strcpy(s->spec, spec);
  1609. s->ops = ops;
  1610. }
  1611. return s;
  1612. }
  1613. static void script_spec__add(struct script_spec *s)
  1614. {
  1615. list_add_tail(&s->node, &script_specs);
  1616. }
  1617. static struct script_spec *script_spec__find(const char *spec)
  1618. {
  1619. struct script_spec *s;
  1620. list_for_each_entry(s, &script_specs, node)
  1621. if (strcasecmp(s->spec, spec) == 0)
  1622. return s;
  1623. return NULL;
  1624. }
  1625. int script_spec_register(const char *spec, struct scripting_ops *ops)
  1626. {
  1627. struct script_spec *s;
  1628. s = script_spec__find(spec);
  1629. if (s)
  1630. return -1;
  1631. s = script_spec__new(spec, ops);
  1632. if (!s)
  1633. return -1;
  1634. else
  1635. script_spec__add(s);
  1636. return 0;
  1637. }
  1638. static struct scripting_ops *script_spec__lookup(const char *spec)
  1639. {
  1640. struct script_spec *s = script_spec__find(spec);
  1641. if (!s)
  1642. return NULL;
  1643. return s->ops;
  1644. }
  1645. static void list_available_languages(void)
  1646. {
  1647. struct script_spec *s;
  1648. fprintf(stderr, "\n");
  1649. fprintf(stderr, "Scripting language extensions (used in "
  1650. "perf script -s [spec:]script.[spec]):\n\n");
  1651. list_for_each_entry(s, &script_specs, node)
  1652. fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
  1653. fprintf(stderr, "\n");
  1654. }
  1655. static int parse_scriptname(const struct option *opt __maybe_unused,
  1656. const char *str, int unset __maybe_unused)
  1657. {
  1658. char spec[PATH_MAX];
  1659. const char *script, *ext;
  1660. int len;
  1661. if (strcmp(str, "lang") == 0) {
  1662. list_available_languages();
  1663. exit(0);
  1664. }
  1665. script = strchr(str, ':');
  1666. if (script) {
  1667. len = script - str;
  1668. if (len >= PATH_MAX) {
  1669. fprintf(stderr, "invalid language specifier");
  1670. return -1;
  1671. }
  1672. strncpy(spec, str, len);
  1673. spec[len] = '\0';
  1674. scripting_ops = script_spec__lookup(spec);
  1675. if (!scripting_ops) {
  1676. fprintf(stderr, "invalid language specifier");
  1677. return -1;
  1678. }
  1679. script++;
  1680. } else {
  1681. script = str;
  1682. ext = strrchr(script, '.');
  1683. if (!ext) {
  1684. fprintf(stderr, "invalid script extension");
  1685. return -1;
  1686. }
  1687. scripting_ops = script_spec__lookup(++ext);
  1688. if (!scripting_ops) {
  1689. fprintf(stderr, "invalid script extension");
  1690. return -1;
  1691. }
  1692. }
  1693. script_name = strdup(script);
  1694. return 0;
  1695. }
  1696. static int parse_output_fields(const struct option *opt __maybe_unused,
  1697. const char *arg, int unset __maybe_unused)
  1698. {
  1699. char *tok, *strtok_saveptr = NULL;
  1700. int i, imax = ARRAY_SIZE(all_output_options);
  1701. int j;
  1702. int rc = 0;
  1703. char *str = strdup(arg);
  1704. int type = -1;
  1705. enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
  1706. if (!str)
  1707. return -ENOMEM;
  1708. /* first word can state for which event type the user is specifying
  1709. * the fields. If no type exists, the specified fields apply to all
  1710. * event types found in the file minus the invalid fields for a type.
  1711. */
  1712. tok = strchr(str, ':');
  1713. if (tok) {
  1714. *tok = '\0';
  1715. tok++;
  1716. if (!strcmp(str, "hw"))
  1717. type = PERF_TYPE_HARDWARE;
  1718. else if (!strcmp(str, "sw"))
  1719. type = PERF_TYPE_SOFTWARE;
  1720. else if (!strcmp(str, "trace"))
  1721. type = PERF_TYPE_TRACEPOINT;
  1722. else if (!strcmp(str, "raw"))
  1723. type = PERF_TYPE_RAW;
  1724. else if (!strcmp(str, "break"))
  1725. type = PERF_TYPE_BREAKPOINT;
  1726. else if (!strcmp(str, "synth"))
  1727. type = OUTPUT_TYPE_SYNTH;
  1728. else {
  1729. fprintf(stderr, "Invalid event type in field string.\n");
  1730. rc = -EINVAL;
  1731. goto out;
  1732. }
  1733. if (output[type].user_set)
  1734. pr_warning("Overriding previous field request for %s events.\n",
  1735. event_type(type));
  1736. output[type].fields = 0;
  1737. output[type].user_set = true;
  1738. output[type].wildcard_set = false;
  1739. } else {
  1740. tok = str;
  1741. if (strlen(str) == 0) {
  1742. fprintf(stderr,
  1743. "Cannot set fields to 'none' for all event types.\n");
  1744. rc = -EINVAL;
  1745. goto out;
  1746. }
  1747. /* Don't override defaults for +- */
  1748. if (strchr(str, '+') || strchr(str, '-'))
  1749. goto parse;
  1750. if (output_set_by_user())
  1751. pr_warning("Overriding previous field request for all events.\n");
  1752. for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
  1753. output[j].fields = 0;
  1754. output[j].user_set = true;
  1755. output[j].wildcard_set = true;
  1756. }
  1757. }
  1758. parse:
  1759. for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
  1760. if (*tok == '+') {
  1761. if (change == SET)
  1762. goto out_badmix;
  1763. change = ADD;
  1764. tok++;
  1765. } else if (*tok == '-') {
  1766. if (change == SET)
  1767. goto out_badmix;
  1768. change = REMOVE;
  1769. tok++;
  1770. } else {
  1771. if (change != SET && change != DEFAULT)
  1772. goto out_badmix;
  1773. change = SET;
  1774. }
  1775. for (i = 0; i < imax; ++i) {
  1776. if (strcmp(tok, all_output_options[i].str) == 0)
  1777. break;
  1778. }
  1779. if (i == imax && strcmp(tok, "flags") == 0) {
  1780. print_flags = change == REMOVE ? false : true;
  1781. continue;
  1782. }
  1783. if (i == imax) {
  1784. fprintf(stderr, "Invalid field requested.\n");
  1785. rc = -EINVAL;
  1786. goto out;
  1787. }
  1788. if (type == -1) {
  1789. /* add user option to all events types for
  1790. * which it is valid
  1791. */
  1792. for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
  1793. if (output[j].invalid_fields & all_output_options[i].field) {
  1794. pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
  1795. all_output_options[i].str, event_type(j));
  1796. } else {
  1797. if (change == REMOVE)
  1798. output[j].fields &= ~all_output_options[i].field;
  1799. else
  1800. output[j].fields |= all_output_options[i].field;
  1801. }
  1802. }
  1803. } else {
  1804. if (output[type].invalid_fields & all_output_options[i].field) {
  1805. fprintf(stderr, "\'%s\' not valid for %s events.\n",
  1806. all_output_options[i].str, event_type(type));
  1807. rc = -EINVAL;
  1808. goto out;
  1809. }
  1810. output[type].fields |= all_output_options[i].field;
  1811. }
  1812. }
  1813. if (type >= 0) {
  1814. if (output[type].fields == 0) {
  1815. pr_debug("No fields requested for %s type. "
  1816. "Events will not be displayed.\n", event_type(type));
  1817. }
  1818. }
  1819. goto out;
  1820. out_badmix:
  1821. fprintf(stderr, "Cannot mix +-field with overridden fields\n");
  1822. rc = -EINVAL;
  1823. out:
  1824. free(str);
  1825. return rc;
  1826. }
  1827. /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
  1828. static int is_directory(const char *base_path, const struct dirent *dent)
  1829. {
  1830. char path[PATH_MAX];
  1831. struct stat st;
  1832. sprintf(path, "%s/%s", base_path, dent->d_name);
  1833. if (stat(path, &st))
  1834. return 0;
  1835. return S_ISDIR(st.st_mode);
  1836. }
  1837. #define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
  1838. while ((lang_dirent = readdir(scripts_dir)) != NULL) \
  1839. if ((lang_dirent->d_type == DT_DIR || \
  1840. (lang_dirent->d_type == DT_UNKNOWN && \
  1841. is_directory(scripts_path, lang_dirent))) && \
  1842. (strcmp(lang_dirent->d_name, ".")) && \
  1843. (strcmp(lang_dirent->d_name, "..")))
  1844. #define for_each_script(lang_path, lang_dir, script_dirent) \
  1845. while ((script_dirent = readdir(lang_dir)) != NULL) \
  1846. if (script_dirent->d_type != DT_DIR && \
  1847. (script_dirent->d_type != DT_UNKNOWN || \
  1848. !is_directory(lang_path, script_dirent)))
  1849. #define RECORD_SUFFIX "-record"
  1850. #define REPORT_SUFFIX "-report"
  1851. struct script_desc {
  1852. struct list_head node;
  1853. char *name;
  1854. char *half_liner;
  1855. char *args;
  1856. };
  1857. static LIST_HEAD(script_descs);
  1858. static struct script_desc *script_desc__new(const char *name)
  1859. {
  1860. struct script_desc *s = zalloc(sizeof(*s));
  1861. if (s != NULL && name)
  1862. s->name = strdup(name);
  1863. return s;
  1864. }
  1865. static void script_desc__delete(struct script_desc *s)
  1866. {
  1867. zfree(&s->name);
  1868. zfree(&s->half_liner);
  1869. zfree(&s->args);
  1870. free(s);
  1871. }
  1872. static void script_desc__add(struct script_desc *s)
  1873. {
  1874. list_add_tail(&s->node, &script_descs);
  1875. }
  1876. static struct script_desc *script_desc__find(const char *name)
  1877. {
  1878. struct script_desc *s;
  1879. list_for_each_entry(s, &script_descs, node)
  1880. if (strcasecmp(s->name, name) == 0)
  1881. return s;
  1882. return NULL;
  1883. }
  1884. static struct script_desc *script_desc__findnew(const char *name)
  1885. {
  1886. struct script_desc *s = script_desc__find(name);
  1887. if (s)
  1888. return s;
  1889. s = script_desc__new(name);
  1890. if (!s)
  1891. return NULL;
  1892. script_desc__add(s);
  1893. return s;
  1894. }
  1895. static const char *ends_with(const char *str, const char *suffix)
  1896. {
  1897. size_t suffix_len = strlen(suffix);
  1898. const char *p = str;
  1899. if (strlen(str) > suffix_len) {
  1900. p = str + strlen(str) - suffix_len;
  1901. if (!strncmp(p, suffix, suffix_len))
  1902. return p;
  1903. }
  1904. return NULL;
  1905. }
  1906. static int read_script_info(struct script_desc *desc, const char *filename)
  1907. {
  1908. char line[BUFSIZ], *p;
  1909. FILE *fp;
  1910. fp = fopen(filename, "r");
  1911. if (!fp)
  1912. return -1;
  1913. while (fgets(line, sizeof(line), fp)) {
  1914. p = ltrim(line);
  1915. if (strlen(p) == 0)
  1916. continue;
  1917. if (*p != '#')
  1918. continue;
  1919. p++;
  1920. if (strlen(p) && *p == '!')
  1921. continue;
  1922. p = ltrim(p);
  1923. if (strlen(p) && p[strlen(p) - 1] == '\n')
  1924. p[strlen(p) - 1] = '\0';
  1925. if (!strncmp(p, "description:", strlen("description:"))) {
  1926. p += strlen("description:");
  1927. desc->half_liner = strdup(ltrim(p));
  1928. continue;
  1929. }
  1930. if (!strncmp(p, "args:", strlen("args:"))) {
  1931. p += strlen("args:");
  1932. desc->args = strdup(ltrim(p));
  1933. continue;
  1934. }
  1935. }
  1936. fclose(fp);
  1937. return 0;
  1938. }
  1939. static char *get_script_root(struct dirent *script_dirent, const char *suffix)
  1940. {
  1941. char *script_root, *str;
  1942. script_root = strdup(script_dirent->d_name);
  1943. if (!script_root)
  1944. return NULL;
  1945. str = (char *)ends_with(script_root, suffix);
  1946. if (!str) {
  1947. free(script_root);
  1948. return NULL;
  1949. }
  1950. *str = '\0';
  1951. return script_root;
  1952. }
  1953. static int list_available_scripts(const struct option *opt __maybe_unused,
  1954. const char *s __maybe_unused,
  1955. int unset __maybe_unused)
  1956. {
  1957. struct dirent *script_dirent, *lang_dirent;
  1958. char scripts_path[MAXPATHLEN];
  1959. DIR *scripts_dir, *lang_dir;
  1960. char script_path[MAXPATHLEN];
  1961. char lang_path[MAXPATHLEN];
  1962. struct script_desc *desc;
  1963. char first_half[BUFSIZ];
  1964. char *script_root;
  1965. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
  1966. scripts_dir = opendir(scripts_path);
  1967. if (!scripts_dir) {
  1968. fprintf(stdout,
  1969. "open(%s) failed.\n"
  1970. "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
  1971. scripts_path);
  1972. exit(-1);
  1973. }
  1974. for_each_lang(scripts_path, scripts_dir, lang_dirent) {
  1975. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  1976. lang_dirent->d_name);
  1977. lang_dir = opendir(lang_path);
  1978. if (!lang_dir)
  1979. continue;
  1980. for_each_script(lang_path, lang_dir, script_dirent) {
  1981. script_root = get_script_root(script_dirent, REPORT_SUFFIX);
  1982. if (script_root) {
  1983. desc = script_desc__findnew(script_root);
  1984. snprintf(script_path, MAXPATHLEN, "%s/%s",
  1985. lang_path, script_dirent->d_name);
  1986. read_script_info(desc, script_path);
  1987. free(script_root);
  1988. }
  1989. }
  1990. }
  1991. fprintf(stdout, "List of available trace scripts:\n");
  1992. list_for_each_entry(desc, &script_descs, node) {
  1993. sprintf(first_half, "%s %s", desc->name,
  1994. desc->args ? desc->args : "");
  1995. fprintf(stdout, " %-36s %s\n", first_half,
  1996. desc->half_liner ? desc->half_liner : "");
  1997. }
  1998. exit(0);
  1999. }
  2000. /*
  2001. * Some scripts specify the required events in their "xxx-record" file,
  2002. * this function will check if the events in perf.data match those
  2003. * mentioned in the "xxx-record".
  2004. *
  2005. * Fixme: All existing "xxx-record" are all in good formats "-e event ",
  2006. * which is covered well now. And new parsing code should be added to
  2007. * cover the future complexing formats like event groups etc.
  2008. */
  2009. static int check_ev_match(char *dir_name, char *scriptname,
  2010. struct perf_session *session)
  2011. {
  2012. char filename[MAXPATHLEN], evname[128];
  2013. char line[BUFSIZ], *p;
  2014. struct perf_evsel *pos;
  2015. int match, len;
  2016. FILE *fp;
  2017. sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
  2018. fp = fopen(filename, "r");
  2019. if (!fp)
  2020. return -1;
  2021. while (fgets(line, sizeof(line), fp)) {
  2022. p = ltrim(line);
  2023. if (*p == '#')
  2024. continue;
  2025. while (strlen(p)) {
  2026. p = strstr(p, "-e");
  2027. if (!p)
  2028. break;
  2029. p += 2;
  2030. p = ltrim(p);
  2031. len = strcspn(p, " \t");
  2032. if (!len)
  2033. break;
  2034. snprintf(evname, len + 1, "%s", p);
  2035. match = 0;
  2036. evlist__for_each_entry(session->evlist, pos) {
  2037. if (!strcmp(perf_evsel__name(pos), evname)) {
  2038. match = 1;
  2039. break;
  2040. }
  2041. }
  2042. if (!match) {
  2043. fclose(fp);
  2044. return -1;
  2045. }
  2046. }
  2047. }
  2048. fclose(fp);
  2049. return 0;
  2050. }
  2051. /*
  2052. * Return -1 if none is found, otherwise the actual scripts number.
  2053. *
  2054. * Currently the only user of this function is the script browser, which
  2055. * will list all statically runnable scripts, select one, execute it and
  2056. * show the output in a perf browser.
  2057. */
  2058. int find_scripts(char **scripts_array, char **scripts_path_array)
  2059. {
  2060. struct dirent *script_dirent, *lang_dirent;
  2061. char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
  2062. DIR *scripts_dir, *lang_dir;
  2063. struct perf_session *session;
  2064. struct perf_data_file file = {
  2065. .path = input_name,
  2066. .mode = PERF_DATA_MODE_READ,
  2067. };
  2068. char *temp;
  2069. int i = 0;
  2070. session = perf_session__new(&file, false, NULL);
  2071. if (!session)
  2072. return -1;
  2073. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
  2074. scripts_dir = opendir(scripts_path);
  2075. if (!scripts_dir) {
  2076. perf_session__delete(session);
  2077. return -1;
  2078. }
  2079. for_each_lang(scripts_path, scripts_dir, lang_dirent) {
  2080. snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
  2081. lang_dirent->d_name);
  2082. #ifdef NO_LIBPERL
  2083. if (strstr(lang_path, "perl"))
  2084. continue;
  2085. #endif
  2086. #ifdef NO_LIBPYTHON
  2087. if (strstr(lang_path, "python"))
  2088. continue;
  2089. #endif
  2090. lang_dir = opendir(lang_path);
  2091. if (!lang_dir)
  2092. continue;
  2093. for_each_script(lang_path, lang_dir, script_dirent) {
  2094. /* Skip those real time scripts: xxxtop.p[yl] */
  2095. if (strstr(script_dirent->d_name, "top."))
  2096. continue;
  2097. sprintf(scripts_path_array[i], "%s/%s", lang_path,
  2098. script_dirent->d_name);
  2099. temp = strchr(script_dirent->d_name, '.');
  2100. snprintf(scripts_array[i],
  2101. (temp - script_dirent->d_name) + 1,
  2102. "%s", script_dirent->d_name);
  2103. if (check_ev_match(lang_path,
  2104. scripts_array[i], session))
  2105. continue;
  2106. i++;
  2107. }
  2108. closedir(lang_dir);
  2109. }
  2110. closedir(scripts_dir);
  2111. perf_session__delete(session);
  2112. return i;
  2113. }
  2114. static char *get_script_path(const char *script_root, const char *suffix)
  2115. {
  2116. struct dirent *script_dirent, *lang_dirent;
  2117. char scripts_path[MAXPATHLEN];
  2118. char script_path[MAXPATHLEN];
  2119. DIR *scripts_dir, *lang_dir;
  2120. char lang_path[MAXPATHLEN];
  2121. char *__script_root;
  2122. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
  2123. scripts_dir = opendir(scripts_path);
  2124. if (!scripts_dir)
  2125. return NULL;
  2126. for_each_lang(scripts_path, scripts_dir, lang_dirent) {
  2127. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  2128. lang_dirent->d_name);
  2129. lang_dir = opendir(lang_path);
  2130. if (!lang_dir)
  2131. continue;
  2132. for_each_script(lang_path, lang_dir, script_dirent) {
  2133. __script_root = get_script_root(script_dirent, suffix);
  2134. if (__script_root && !strcmp(script_root, __script_root)) {
  2135. free(__script_root);
  2136. closedir(lang_dir);
  2137. closedir(scripts_dir);
  2138. snprintf(script_path, MAXPATHLEN, "%s/%s",
  2139. lang_path, script_dirent->d_name);
  2140. return strdup(script_path);
  2141. }
  2142. free(__script_root);
  2143. }
  2144. closedir(lang_dir);
  2145. }
  2146. closedir(scripts_dir);
  2147. return NULL;
  2148. }
  2149. static bool is_top_script(const char *script_path)
  2150. {
  2151. return ends_with(script_path, "top") == NULL ? false : true;
  2152. }
  2153. static int has_required_arg(char *script_path)
  2154. {
  2155. struct script_desc *desc;
  2156. int n_args = 0;
  2157. char *p;
  2158. desc = script_desc__new(NULL);
  2159. if (read_script_info(desc, script_path))
  2160. goto out;
  2161. if (!desc->args)
  2162. goto out;
  2163. for (p = desc->args; *p; p++)
  2164. if (*p == '<')
  2165. n_args++;
  2166. out:
  2167. script_desc__delete(desc);
  2168. return n_args;
  2169. }
  2170. static int have_cmd(int argc, const char **argv)
  2171. {
  2172. char **__argv = malloc(sizeof(const char *) * argc);
  2173. if (!__argv) {
  2174. pr_err("malloc failed\n");
  2175. return -1;
  2176. }
  2177. memcpy(__argv, argv, sizeof(const char *) * argc);
  2178. argc = parse_options(argc, (const char **)__argv, record_options,
  2179. NULL, PARSE_OPT_STOP_AT_NON_OPTION);
  2180. free(__argv);
  2181. system_wide = (argc == 0);
  2182. return 0;
  2183. }
  2184. static void script__setup_sample_type(struct perf_script *script)
  2185. {
  2186. struct perf_session *session = script->session;
  2187. u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
  2188. if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
  2189. if ((sample_type & PERF_SAMPLE_REGS_USER) &&
  2190. (sample_type & PERF_SAMPLE_STACK_USER))
  2191. callchain_param.record_mode = CALLCHAIN_DWARF;
  2192. else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
  2193. callchain_param.record_mode = CALLCHAIN_LBR;
  2194. else
  2195. callchain_param.record_mode = CALLCHAIN_FP;
  2196. }
  2197. }
  2198. static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
  2199. union perf_event *event,
  2200. struct perf_session *session)
  2201. {
  2202. struct stat_round_event *round = &event->stat_round;
  2203. struct perf_evsel *counter;
  2204. evlist__for_each_entry(session->evlist, counter) {
  2205. perf_stat_process_counter(&stat_config, counter);
  2206. process_stat(counter, round->time);
  2207. }
  2208. process_stat_interval(round->time);
  2209. return 0;
  2210. }
  2211. static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
  2212. union perf_event *event,
  2213. struct perf_session *session __maybe_unused)
  2214. {
  2215. perf_event__read_stat_config(&stat_config, &event->stat_config);
  2216. return 0;
  2217. }
  2218. static int set_maps(struct perf_script *script)
  2219. {
  2220. struct perf_evlist *evlist = script->session->evlist;
  2221. if (!script->cpus || !script->threads)
  2222. return 0;
  2223. if (WARN_ONCE(script->allocated, "stats double allocation\n"))
  2224. return -EINVAL;
  2225. perf_evlist__set_maps(evlist, script->cpus, script->threads);
  2226. if (perf_evlist__alloc_stats(evlist, true))
  2227. return -ENOMEM;
  2228. script->allocated = true;
  2229. return 0;
  2230. }
  2231. static
  2232. int process_thread_map_event(struct perf_tool *tool,
  2233. union perf_event *event,
  2234. struct perf_session *session __maybe_unused)
  2235. {
  2236. struct perf_script *script = container_of(tool, struct perf_script, tool);
  2237. if (script->threads) {
  2238. pr_warning("Extra thread map event, ignoring.\n");
  2239. return 0;
  2240. }
  2241. script->threads = thread_map__new_event(&event->thread_map);
  2242. if (!script->threads)
  2243. return -ENOMEM;
  2244. return set_maps(script);
  2245. }
  2246. static
  2247. int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
  2248. union perf_event *event,
  2249. struct perf_session *session __maybe_unused)
  2250. {
  2251. struct perf_script *script = container_of(tool, struct perf_script, tool);
  2252. if (script->cpus) {
  2253. pr_warning("Extra cpu map event, ignoring.\n");
  2254. return 0;
  2255. }
  2256. script->cpus = cpu_map__new_data(&event->cpu_map.data);
  2257. if (!script->cpus)
  2258. return -ENOMEM;
  2259. return set_maps(script);
  2260. }
  2261. int cmd_script(int argc, const char **argv)
  2262. {
  2263. bool show_full_info = false;
  2264. bool header = false;
  2265. bool header_only = false;
  2266. bool script_started = false;
  2267. char *rec_script_path = NULL;
  2268. char *rep_script_path = NULL;
  2269. struct perf_session *session;
  2270. struct itrace_synth_opts itrace_synth_opts = { .set = false, };
  2271. char *script_path = NULL;
  2272. const char **__argv;
  2273. int i, j, err = 0;
  2274. struct perf_script script = {
  2275. .tool = {
  2276. .sample = process_sample_event,
  2277. .mmap = perf_event__process_mmap,
  2278. .mmap2 = perf_event__process_mmap2,
  2279. .comm = perf_event__process_comm,
  2280. .namespaces = perf_event__process_namespaces,
  2281. .exit = perf_event__process_exit,
  2282. .fork = perf_event__process_fork,
  2283. .attr = process_attr,
  2284. .event_update = perf_event__process_event_update,
  2285. .tracing_data = perf_event__process_tracing_data,
  2286. .feature = perf_event__process_feature,
  2287. .build_id = perf_event__process_build_id,
  2288. .id_index = perf_event__process_id_index,
  2289. .auxtrace_info = perf_event__process_auxtrace_info,
  2290. .auxtrace = perf_event__process_auxtrace,
  2291. .auxtrace_error = perf_event__process_auxtrace_error,
  2292. .stat = perf_event__process_stat_event,
  2293. .stat_round = process_stat_round_event,
  2294. .stat_config = process_stat_config_event,
  2295. .thread_map = process_thread_map_event,
  2296. .cpu_map = process_cpu_map_event,
  2297. .ordered_events = true,
  2298. .ordering_requires_timestamps = true,
  2299. },
  2300. };
  2301. struct perf_data_file file = {
  2302. .mode = PERF_DATA_MODE_READ,
  2303. };
  2304. const struct option options[] = {
  2305. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  2306. "dump raw trace in ASCII"),
  2307. OPT_INCR('v', "verbose", &verbose,
  2308. "be more verbose (show symbol address, etc)"),
  2309. OPT_BOOLEAN('L', "Latency", &latency_format,
  2310. "show latency attributes (irqs/preemption disabled, etc)"),
  2311. OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
  2312. list_available_scripts),
  2313. OPT_CALLBACK('s', "script", NULL, "name",
  2314. "script file name (lang:script name, script name, or *)",
  2315. parse_scriptname),
  2316. OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
  2317. "generate perf-script.xx script in specified language"),
  2318. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  2319. OPT_BOOLEAN('d', "debug-mode", &debug_mode,
  2320. "do various checks like samples ordering and lost events"),
  2321. OPT_BOOLEAN(0, "header", &header, "Show data header."),
  2322. OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
  2323. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  2324. "file", "vmlinux pathname"),
  2325. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  2326. "file", "kallsyms pathname"),
  2327. OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
  2328. "When printing symbols do not display call chain"),
  2329. OPT_CALLBACK(0, "symfs", NULL, "directory",
  2330. "Look for files with symbols relative to this directory",
  2331. symbol__config_symfs),
  2332. OPT_CALLBACK('F', "fields", NULL, "str",
  2333. "comma separated output fields prepend with 'type:'. "
  2334. "+field to add and -field to remove."
  2335. "Valid types: hw,sw,trace,raw,synth. "
  2336. "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
  2337. "addr,symoff,period,iregs,uregs,brstack,brstacksym,flags,"
  2338. "bpf-output,callindent,insn,insnlen,brstackinsn,synth,phys_addr",
  2339. parse_output_fields),
  2340. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  2341. "system-wide collection from all CPUs"),
  2342. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  2343. "only consider these symbols"),
  2344. OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
  2345. "Stop display of callgraph at these symbols"),
  2346. OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
  2347. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  2348. "only display events for these comms"),
  2349. OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
  2350. "only consider symbols in these pids"),
  2351. OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
  2352. "only consider symbols in these tids"),
  2353. OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
  2354. "Set the maximum stack depth when parsing the callchain, "
  2355. "anything beyond the specified depth will be ignored. "
  2356. "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
  2357. OPT_BOOLEAN('I', "show-info", &show_full_info,
  2358. "display extended information from perf.data file"),
  2359. OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
  2360. "Show the path of [kernel.kallsyms]"),
  2361. OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
  2362. "Show the fork/comm/exit events"),
  2363. OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
  2364. "Show the mmap events"),
  2365. OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
  2366. "Show context switch events (if recorded)"),
  2367. OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
  2368. "Show namespace events (if recorded)"),
  2369. OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
  2370. OPT_INTEGER(0, "max-blocks", &max_blocks,
  2371. "Maximum number of code blocks to dump with brstackinsn"),
  2372. OPT_BOOLEAN(0, "ns", &nanosecs,
  2373. "Use 9 decimal places when displaying time"),
  2374. OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
  2375. "Instruction Tracing options",
  2376. itrace_parse_synth_opts),
  2377. OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
  2378. "Show full source file name path for source lines"),
  2379. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  2380. "Enable symbol demangling"),
  2381. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  2382. "Enable kernel symbol demangling"),
  2383. OPT_STRING(0, "time", &script.time_str, "str",
  2384. "Time span of interest (start,stop)"),
  2385. OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
  2386. "Show inline function"),
  2387. OPT_END()
  2388. };
  2389. const char * const script_subcommands[] = { "record", "report", NULL };
  2390. const char *script_usage[] = {
  2391. "perf script [<options>]",
  2392. "perf script [<options>] record <script> [<record-options>] <command>",
  2393. "perf script [<options>] report <script> [script-args]",
  2394. "perf script [<options>] <script> [<record-options>] <command>",
  2395. "perf script [<options>] <top-script> [script-args]",
  2396. NULL
  2397. };
  2398. setup_scripting();
  2399. argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
  2400. PARSE_OPT_STOP_AT_NON_OPTION);
  2401. file.path = input_name;
  2402. file.force = symbol_conf.force;
  2403. if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
  2404. rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
  2405. if (!rec_script_path)
  2406. return cmd_record(argc, argv);
  2407. }
  2408. if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
  2409. rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
  2410. if (!rep_script_path) {
  2411. fprintf(stderr,
  2412. "Please specify a valid report script"
  2413. "(see 'perf script -l' for listing)\n");
  2414. return -1;
  2415. }
  2416. }
  2417. if (itrace_synth_opts.callchain &&
  2418. itrace_synth_opts.callchain_sz > scripting_max_stack)
  2419. scripting_max_stack = itrace_synth_opts.callchain_sz;
  2420. /* make sure PERF_EXEC_PATH is set for scripts */
  2421. set_argv_exec_path(get_argv_exec_path());
  2422. if (argc && !script_name && !rec_script_path && !rep_script_path) {
  2423. int live_pipe[2];
  2424. int rep_args;
  2425. pid_t pid;
  2426. rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
  2427. rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
  2428. if (!rec_script_path && !rep_script_path) {
  2429. usage_with_options_msg(script_usage, options,
  2430. "Couldn't find script `%s'\n\n See perf"
  2431. " script -l for available scripts.\n", argv[0]);
  2432. }
  2433. if (is_top_script(argv[0])) {
  2434. rep_args = argc - 1;
  2435. } else {
  2436. int rec_args;
  2437. rep_args = has_required_arg(rep_script_path);
  2438. rec_args = (argc - 1) - rep_args;
  2439. if (rec_args < 0) {
  2440. usage_with_options_msg(script_usage, options,
  2441. "`%s' script requires options."
  2442. "\n\n See perf script -l for available "
  2443. "scripts and options.\n", argv[0]);
  2444. }
  2445. }
  2446. if (pipe(live_pipe) < 0) {
  2447. perror("failed to create pipe");
  2448. return -1;
  2449. }
  2450. pid = fork();
  2451. if (pid < 0) {
  2452. perror("failed to fork");
  2453. return -1;
  2454. }
  2455. if (!pid) {
  2456. j = 0;
  2457. dup2(live_pipe[1], 1);
  2458. close(live_pipe[0]);
  2459. if (is_top_script(argv[0])) {
  2460. system_wide = true;
  2461. } else if (!system_wide) {
  2462. if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
  2463. err = -1;
  2464. goto out;
  2465. }
  2466. }
  2467. __argv = malloc((argc + 6) * sizeof(const char *));
  2468. if (!__argv) {
  2469. pr_err("malloc failed\n");
  2470. err = -ENOMEM;
  2471. goto out;
  2472. }
  2473. __argv[j++] = "/bin/sh";
  2474. __argv[j++] = rec_script_path;
  2475. if (system_wide)
  2476. __argv[j++] = "-a";
  2477. __argv[j++] = "-q";
  2478. __argv[j++] = "-o";
  2479. __argv[j++] = "-";
  2480. for (i = rep_args + 1; i < argc; i++)
  2481. __argv[j++] = argv[i];
  2482. __argv[j++] = NULL;
  2483. execvp("/bin/sh", (char **)__argv);
  2484. free(__argv);
  2485. exit(-1);
  2486. }
  2487. dup2(live_pipe[0], 0);
  2488. close(live_pipe[1]);
  2489. __argv = malloc((argc + 4) * sizeof(const char *));
  2490. if (!__argv) {
  2491. pr_err("malloc failed\n");
  2492. err = -ENOMEM;
  2493. goto out;
  2494. }
  2495. j = 0;
  2496. __argv[j++] = "/bin/sh";
  2497. __argv[j++] = rep_script_path;
  2498. for (i = 1; i < rep_args + 1; i++)
  2499. __argv[j++] = argv[i];
  2500. __argv[j++] = "-i";
  2501. __argv[j++] = "-";
  2502. __argv[j++] = NULL;
  2503. execvp("/bin/sh", (char **)__argv);
  2504. free(__argv);
  2505. exit(-1);
  2506. }
  2507. if (rec_script_path)
  2508. script_path = rec_script_path;
  2509. if (rep_script_path)
  2510. script_path = rep_script_path;
  2511. if (script_path) {
  2512. j = 0;
  2513. if (!rec_script_path)
  2514. system_wide = false;
  2515. else if (!system_wide) {
  2516. if (have_cmd(argc - 1, &argv[1]) != 0) {
  2517. err = -1;
  2518. goto out;
  2519. }
  2520. }
  2521. __argv = malloc((argc + 2) * sizeof(const char *));
  2522. if (!__argv) {
  2523. pr_err("malloc failed\n");
  2524. err = -ENOMEM;
  2525. goto out;
  2526. }
  2527. __argv[j++] = "/bin/sh";
  2528. __argv[j++] = script_path;
  2529. if (system_wide)
  2530. __argv[j++] = "-a";
  2531. for (i = 2; i < argc; i++)
  2532. __argv[j++] = argv[i];
  2533. __argv[j++] = NULL;
  2534. execvp("/bin/sh", (char **)__argv);
  2535. free(__argv);
  2536. exit(-1);
  2537. }
  2538. if (!script_name)
  2539. setup_pager();
  2540. session = perf_session__new(&file, false, &script.tool);
  2541. if (session == NULL)
  2542. return -1;
  2543. if (header || header_only) {
  2544. script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
  2545. perf_session__fprintf_info(session, stdout, show_full_info);
  2546. if (header_only)
  2547. goto out_delete;
  2548. }
  2549. if (show_full_info)
  2550. script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
  2551. if (symbol__init(&session->header.env) < 0)
  2552. goto out_delete;
  2553. script.session = session;
  2554. script__setup_sample_type(&script);
  2555. if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
  2556. itrace_synth_opts.thread_stack = true;
  2557. session->itrace_synth_opts = &itrace_synth_opts;
  2558. if (cpu_list) {
  2559. err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
  2560. if (err < 0)
  2561. goto out_delete;
  2562. itrace_synth_opts.cpu_bitmap = cpu_bitmap;
  2563. }
  2564. if (!no_callchain)
  2565. symbol_conf.use_callchain = true;
  2566. else
  2567. symbol_conf.use_callchain = false;
  2568. if (session->tevent.pevent &&
  2569. pevent_set_function_resolver(session->tevent.pevent,
  2570. machine__resolve_kernel_addr,
  2571. &session->machines.host) < 0) {
  2572. pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
  2573. return -1;
  2574. }
  2575. if (generate_script_lang) {
  2576. struct stat perf_stat;
  2577. int input;
  2578. if (output_set_by_user()) {
  2579. fprintf(stderr,
  2580. "custom fields not supported for generated scripts");
  2581. err = -EINVAL;
  2582. goto out_delete;
  2583. }
  2584. input = open(file.path, O_RDONLY); /* input_name */
  2585. if (input < 0) {
  2586. err = -errno;
  2587. perror("failed to open file");
  2588. goto out_delete;
  2589. }
  2590. err = fstat(input, &perf_stat);
  2591. if (err < 0) {
  2592. perror("failed to stat file");
  2593. goto out_delete;
  2594. }
  2595. if (!perf_stat.st_size) {
  2596. fprintf(stderr, "zero-sized file, nothing to do!\n");
  2597. goto out_delete;
  2598. }
  2599. scripting_ops = script_spec__lookup(generate_script_lang);
  2600. if (!scripting_ops) {
  2601. fprintf(stderr, "invalid language specifier");
  2602. err = -ENOENT;
  2603. goto out_delete;
  2604. }
  2605. err = scripting_ops->generate_script(session->tevent.pevent,
  2606. "perf-script");
  2607. goto out_delete;
  2608. }
  2609. if (script_name) {
  2610. err = scripting_ops->start_script(script_name, argc, argv);
  2611. if (err)
  2612. goto out_delete;
  2613. pr_debug("perf script started with script %s\n\n", script_name);
  2614. script_started = true;
  2615. }
  2616. err = perf_session__check_output_opt(session);
  2617. if (err < 0)
  2618. goto out_delete;
  2619. /* needs to be parsed after looking up reference time */
  2620. if (perf_time__parse_str(&script.ptime, script.time_str) != 0) {
  2621. pr_err("Invalid time string\n");
  2622. return -EINVAL;
  2623. }
  2624. err = __cmd_script(&script);
  2625. flush_scripting();
  2626. out_delete:
  2627. perf_evlist__free_stats(session->evlist);
  2628. perf_session__delete(session);
  2629. if (script_started)
  2630. cleanup_scripting();
  2631. out:
  2632. return err;
  2633. }