builtin-script.c 91 KB

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