builtin-script.c 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "builtin.h"
  3. #include "util/counts.h"
  4. #include "util/debug.h"
  5. #include "util/dso.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/map.h"
  13. #include "util/srcline.h"
  14. #include "util/symbol.h"
  15. #include "util/thread.h"
  16. #include "util/trace-event.h"
  17. #include "util/env.h"
  18. #include "util/evlist.h"
  19. #include "util/evsel.h"
  20. #include "util/evsel_fprintf.h"
  21. #include "util/evswitch.h"
  22. #include "util/sort.h"
  23. #include "util/data.h"
  24. #include "util/auxtrace.h"
  25. #include "util/cpumap.h"
  26. #include "util/thread_map.h"
  27. #include "util/stat.h"
  28. #include "util/color.h"
  29. #include "util/string2.h"
  30. #include "util/thread-stack.h"
  31. #include "util/time-utils.h"
  32. #include "util/path.h"
  33. #include "util/event.h"
  34. #include "ui/ui.h"
  35. #include "print_binary.h"
  36. #include "archinsn.h"
  37. #include <linux/bitmap.h>
  38. #include <linux/kernel.h>
  39. #include <linux/stringify.h>
  40. #include <linux/time64.h>
  41. #include <linux/zalloc.h>
  42. #include <sys/utsname.h>
  43. #include "asm/bug.h"
  44. #include "util/mem-events.h"
  45. #include "util/dump-insn.h"
  46. #include <dirent.h>
  47. #include <errno.h>
  48. #include <inttypes.h>
  49. #include <signal.h>
  50. #include <sys/param.h>
  51. #include <sys/types.h>
  52. #include <sys/stat.h>
  53. #include <fcntl.h>
  54. #include <unistd.h>
  55. #include <subcmd/pager.h>
  56. #include <perf/evlist.h>
  57. #include <linux/err.h>
  58. #include "util/dlfilter.h"
  59. #include "util/record.h"
  60. #include "util/util.h"
  61. #include "perf.h"
  62. #include <linux/ctype.h>
  63. static char const *script_name;
  64. static char const *generate_script_lang;
  65. static bool reltime;
  66. static bool deltatime;
  67. static u64 initial_time;
  68. static u64 previous_time;
  69. static bool debug_mode;
  70. static u64 last_timestamp;
  71. static u64 nr_unordered;
  72. static bool no_callchain;
  73. static bool latency_format;
  74. static bool system_wide;
  75. static bool print_flags;
  76. static const char *cpu_list;
  77. static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
  78. static struct perf_stat_config stat_config;
  79. static int max_blocks;
  80. static bool native_arch;
  81. static struct dlfilter *dlfilter;
  82. static int dlargc;
  83. static char **dlargv;
  84. unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
  85. enum perf_output_field {
  86. PERF_OUTPUT_COMM = 1ULL << 0,
  87. PERF_OUTPUT_TID = 1ULL << 1,
  88. PERF_OUTPUT_PID = 1ULL << 2,
  89. PERF_OUTPUT_TIME = 1ULL << 3,
  90. PERF_OUTPUT_CPU = 1ULL << 4,
  91. PERF_OUTPUT_EVNAME = 1ULL << 5,
  92. PERF_OUTPUT_TRACE = 1ULL << 6,
  93. PERF_OUTPUT_IP = 1ULL << 7,
  94. PERF_OUTPUT_SYM = 1ULL << 8,
  95. PERF_OUTPUT_DSO = 1ULL << 9,
  96. PERF_OUTPUT_ADDR = 1ULL << 10,
  97. PERF_OUTPUT_SYMOFFSET = 1ULL << 11,
  98. PERF_OUTPUT_SRCLINE = 1ULL << 12,
  99. PERF_OUTPUT_PERIOD = 1ULL << 13,
  100. PERF_OUTPUT_IREGS = 1ULL << 14,
  101. PERF_OUTPUT_BRSTACK = 1ULL << 15,
  102. PERF_OUTPUT_BRSTACKSYM = 1ULL << 16,
  103. PERF_OUTPUT_DATA_SRC = 1ULL << 17,
  104. PERF_OUTPUT_WEIGHT = 1ULL << 18,
  105. PERF_OUTPUT_BPF_OUTPUT = 1ULL << 19,
  106. PERF_OUTPUT_CALLINDENT = 1ULL << 20,
  107. PERF_OUTPUT_INSN = 1ULL << 21,
  108. PERF_OUTPUT_INSNLEN = 1ULL << 22,
  109. PERF_OUTPUT_BRSTACKINSN = 1ULL << 23,
  110. PERF_OUTPUT_BRSTACKOFF = 1ULL << 24,
  111. PERF_OUTPUT_SYNTH = 1ULL << 25,
  112. PERF_OUTPUT_PHYS_ADDR = 1ULL << 26,
  113. PERF_OUTPUT_UREGS = 1ULL << 27,
  114. PERF_OUTPUT_METRIC = 1ULL << 28,
  115. PERF_OUTPUT_MISC = 1ULL << 29,
  116. PERF_OUTPUT_SRCCODE = 1ULL << 30,
  117. PERF_OUTPUT_IPC = 1ULL << 31,
  118. PERF_OUTPUT_TOD = 1ULL << 32,
  119. PERF_OUTPUT_DATA_PAGE_SIZE = 1ULL << 33,
  120. PERF_OUTPUT_CODE_PAGE_SIZE = 1ULL << 34,
  121. PERF_OUTPUT_INS_LAT = 1ULL << 35,
  122. PERF_OUTPUT_BRSTACKINSNLEN = 1ULL << 36,
  123. PERF_OUTPUT_MACHINE_PID = 1ULL << 37,
  124. PERF_OUTPUT_VCPU = 1ULL << 38,
  125. };
  126. struct perf_script {
  127. struct perf_tool tool;
  128. struct perf_session *session;
  129. bool show_task_events;
  130. bool show_mmap_events;
  131. bool show_switch_events;
  132. bool show_namespace_events;
  133. bool show_lost_events;
  134. bool show_round_events;
  135. bool show_bpf_events;
  136. bool show_cgroup_events;
  137. bool show_text_poke_events;
  138. bool allocated;
  139. bool per_event_dump;
  140. bool stitch_lbr;
  141. struct evswitch evswitch;
  142. struct perf_cpu_map *cpus;
  143. struct perf_thread_map *threads;
  144. int name_width;
  145. const char *time_str;
  146. struct perf_time_interval *ptime_range;
  147. int range_size;
  148. int range_num;
  149. };
  150. struct output_option {
  151. const char *str;
  152. enum perf_output_field field;
  153. } all_output_options[] = {
  154. {.str = "comm", .field = PERF_OUTPUT_COMM},
  155. {.str = "tid", .field = PERF_OUTPUT_TID},
  156. {.str = "pid", .field = PERF_OUTPUT_PID},
  157. {.str = "time", .field = PERF_OUTPUT_TIME},
  158. {.str = "cpu", .field = PERF_OUTPUT_CPU},
  159. {.str = "event", .field = PERF_OUTPUT_EVNAME},
  160. {.str = "trace", .field = PERF_OUTPUT_TRACE},
  161. {.str = "ip", .field = PERF_OUTPUT_IP},
  162. {.str = "sym", .field = PERF_OUTPUT_SYM},
  163. {.str = "dso", .field = PERF_OUTPUT_DSO},
  164. {.str = "addr", .field = PERF_OUTPUT_ADDR},
  165. {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
  166. {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
  167. {.str = "period", .field = PERF_OUTPUT_PERIOD},
  168. {.str = "iregs", .field = PERF_OUTPUT_IREGS},
  169. {.str = "uregs", .field = PERF_OUTPUT_UREGS},
  170. {.str = "brstack", .field = PERF_OUTPUT_BRSTACK},
  171. {.str = "brstacksym", .field = PERF_OUTPUT_BRSTACKSYM},
  172. {.str = "data_src", .field = PERF_OUTPUT_DATA_SRC},
  173. {.str = "weight", .field = PERF_OUTPUT_WEIGHT},
  174. {.str = "bpf-output", .field = PERF_OUTPUT_BPF_OUTPUT},
  175. {.str = "callindent", .field = PERF_OUTPUT_CALLINDENT},
  176. {.str = "insn", .field = PERF_OUTPUT_INSN},
  177. {.str = "insnlen", .field = PERF_OUTPUT_INSNLEN},
  178. {.str = "brstackinsn", .field = PERF_OUTPUT_BRSTACKINSN},
  179. {.str = "brstackoff", .field = PERF_OUTPUT_BRSTACKOFF},
  180. {.str = "synth", .field = PERF_OUTPUT_SYNTH},
  181. {.str = "phys_addr", .field = PERF_OUTPUT_PHYS_ADDR},
  182. {.str = "metric", .field = PERF_OUTPUT_METRIC},
  183. {.str = "misc", .field = PERF_OUTPUT_MISC},
  184. {.str = "srccode", .field = PERF_OUTPUT_SRCCODE},
  185. {.str = "ipc", .field = PERF_OUTPUT_IPC},
  186. {.str = "tod", .field = PERF_OUTPUT_TOD},
  187. {.str = "data_page_size", .field = PERF_OUTPUT_DATA_PAGE_SIZE},
  188. {.str = "code_page_size", .field = PERF_OUTPUT_CODE_PAGE_SIZE},
  189. {.str = "ins_lat", .field = PERF_OUTPUT_INS_LAT},
  190. {.str = "brstackinsnlen", .field = PERF_OUTPUT_BRSTACKINSNLEN},
  191. {.str = "machine_pid", .field = PERF_OUTPUT_MACHINE_PID},
  192. {.str = "vcpu", .field = PERF_OUTPUT_VCPU},
  193. };
  194. enum {
  195. OUTPUT_TYPE_SYNTH = PERF_TYPE_MAX,
  196. OUTPUT_TYPE_OTHER,
  197. OUTPUT_TYPE_MAX
  198. };
  199. /* default set to maintain compatibility with current format */
  200. static struct {
  201. bool user_set;
  202. bool wildcard_set;
  203. unsigned int print_ip_opts;
  204. u64 fields;
  205. u64 invalid_fields;
  206. u64 user_set_fields;
  207. u64 user_unset_fields;
  208. } output[OUTPUT_TYPE_MAX] = {
  209. [PERF_TYPE_HARDWARE] = {
  210. .user_set = false,
  211. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  212. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  213. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  214. PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
  215. PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
  216. .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
  217. },
  218. [PERF_TYPE_SOFTWARE] = {
  219. .user_set = false,
  220. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  221. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  222. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  223. PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
  224. PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
  225. PERF_OUTPUT_BPF_OUTPUT,
  226. .invalid_fields = PERF_OUTPUT_TRACE,
  227. },
  228. [PERF_TYPE_TRACEPOINT] = {
  229. .user_set = false,
  230. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  231. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  232. PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE
  233. },
  234. [PERF_TYPE_HW_CACHE] = {
  235. .user_set = false,
  236. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  237. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  238. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  239. PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
  240. PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
  241. .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
  242. },
  243. [PERF_TYPE_RAW] = {
  244. .user_set = false,
  245. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  246. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  247. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  248. PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
  249. PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD |
  250. PERF_OUTPUT_ADDR | PERF_OUTPUT_DATA_SRC |
  251. PERF_OUTPUT_WEIGHT | PERF_OUTPUT_PHYS_ADDR |
  252. PERF_OUTPUT_DATA_PAGE_SIZE | PERF_OUTPUT_CODE_PAGE_SIZE |
  253. PERF_OUTPUT_INS_LAT,
  254. .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
  255. },
  256. [PERF_TYPE_BREAKPOINT] = {
  257. .user_set = false,
  258. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  259. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  260. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  261. PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
  262. PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
  263. .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
  264. },
  265. [OUTPUT_TYPE_SYNTH] = {
  266. .user_set = false,
  267. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  268. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  269. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  270. PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
  271. PERF_OUTPUT_DSO | PERF_OUTPUT_SYNTH,
  272. .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
  273. },
  274. [OUTPUT_TYPE_OTHER] = {
  275. .user_set = false,
  276. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  277. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  278. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  279. PERF_OUTPUT_SYM | PERF_OUTPUT_SYMOFFSET |
  280. PERF_OUTPUT_DSO | PERF_OUTPUT_PERIOD,
  281. .invalid_fields = PERF_OUTPUT_TRACE | PERF_OUTPUT_BPF_OUTPUT,
  282. },
  283. };
  284. struct evsel_script {
  285. char *filename;
  286. FILE *fp;
  287. u64 samples;
  288. /* For metric output */
  289. u64 val;
  290. int gnum;
  291. };
  292. static inline struct evsel_script *evsel_script(struct evsel *evsel)
  293. {
  294. return (struct evsel_script *)evsel->priv;
  295. }
  296. static struct evsel_script *evsel_script__new(struct evsel *evsel, struct perf_data *data)
  297. {
  298. struct evsel_script *es = zalloc(sizeof(*es));
  299. if (es != NULL) {
  300. if (asprintf(&es->filename, "%s.%s.dump", data->file.path, evsel__name(evsel)) < 0)
  301. goto out_free;
  302. es->fp = fopen(es->filename, "w");
  303. if (es->fp == NULL)
  304. goto out_free_filename;
  305. }
  306. return es;
  307. out_free_filename:
  308. zfree(&es->filename);
  309. out_free:
  310. free(es);
  311. return NULL;
  312. }
  313. static void evsel_script__delete(struct evsel_script *es)
  314. {
  315. zfree(&es->filename);
  316. fclose(es->fp);
  317. es->fp = NULL;
  318. free(es);
  319. }
  320. static int evsel_script__fprintf(struct evsel_script *es, FILE *fp)
  321. {
  322. struct stat st;
  323. fstat(fileno(es->fp), &st);
  324. return fprintf(fp, "[ perf script: Wrote %.3f MB %s (%" PRIu64 " samples) ]\n",
  325. st.st_size / 1024.0 / 1024.0, es->filename, es->samples);
  326. }
  327. static inline int output_type(unsigned int type)
  328. {
  329. switch (type) {
  330. case PERF_TYPE_SYNTH:
  331. return OUTPUT_TYPE_SYNTH;
  332. default:
  333. if (type < PERF_TYPE_MAX)
  334. return type;
  335. }
  336. return OUTPUT_TYPE_OTHER;
  337. }
  338. static bool output_set_by_user(void)
  339. {
  340. int j;
  341. for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
  342. if (output[j].user_set)
  343. return true;
  344. }
  345. return false;
  346. }
  347. static const char *output_field2str(enum perf_output_field field)
  348. {
  349. int i, imax = ARRAY_SIZE(all_output_options);
  350. const char *str = "";
  351. for (i = 0; i < imax; ++i) {
  352. if (all_output_options[i].field == field) {
  353. str = all_output_options[i].str;
  354. break;
  355. }
  356. }
  357. return str;
  358. }
  359. #define PRINT_FIELD(x) (output[output_type(attr->type)].fields & PERF_OUTPUT_##x)
  360. static int evsel__do_check_stype(struct evsel *evsel, u64 sample_type, const char *sample_msg,
  361. enum perf_output_field field, bool allow_user_set)
  362. {
  363. struct perf_event_attr *attr = &evsel->core.attr;
  364. int type = output_type(attr->type);
  365. const char *evname;
  366. if (attr->sample_type & sample_type)
  367. return 0;
  368. if (output[type].user_set_fields & field) {
  369. if (allow_user_set)
  370. return 0;
  371. evname = evsel__name(evsel);
  372. pr_err("Samples for '%s' event do not have %s attribute set. "
  373. "Cannot print '%s' field.\n",
  374. evname, sample_msg, output_field2str(field));
  375. return -1;
  376. }
  377. /* user did not ask for it explicitly so remove from the default list */
  378. output[type].fields &= ~field;
  379. evname = evsel__name(evsel);
  380. pr_debug("Samples for '%s' event do not have %s attribute set. "
  381. "Skipping '%s' field.\n",
  382. evname, sample_msg, output_field2str(field));
  383. return 0;
  384. }
  385. static int evsel__check_stype(struct evsel *evsel, u64 sample_type, const char *sample_msg,
  386. enum perf_output_field field)
  387. {
  388. return evsel__do_check_stype(evsel, sample_type, sample_msg, field, false);
  389. }
  390. static int evsel__check_attr(struct evsel *evsel, struct perf_session *session)
  391. {
  392. struct perf_event_attr *attr = &evsel->core.attr;
  393. bool allow_user_set;
  394. if (evsel__is_dummy_event(evsel))
  395. return 0;
  396. if (perf_header__has_feat(&session->header, HEADER_STAT))
  397. return 0;
  398. allow_user_set = perf_header__has_feat(&session->header,
  399. HEADER_AUXTRACE);
  400. if (PRINT_FIELD(TRACE) &&
  401. !perf_session__has_traces(session, "record -R"))
  402. return -EINVAL;
  403. if (PRINT_FIELD(IP)) {
  404. if (evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP", PERF_OUTPUT_IP))
  405. return -EINVAL;
  406. }
  407. if (PRINT_FIELD(ADDR) &&
  408. evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR", PERF_OUTPUT_ADDR, allow_user_set))
  409. return -EINVAL;
  410. if (PRINT_FIELD(DATA_SRC) &&
  411. evsel__do_check_stype(evsel, PERF_SAMPLE_DATA_SRC, "DATA_SRC", PERF_OUTPUT_DATA_SRC, allow_user_set))
  412. return -EINVAL;
  413. if (PRINT_FIELD(WEIGHT) &&
  414. evsel__do_check_stype(evsel, PERF_SAMPLE_WEIGHT_TYPE, "WEIGHT", PERF_OUTPUT_WEIGHT, allow_user_set))
  415. return -EINVAL;
  416. if (PRINT_FIELD(SYM) &&
  417. !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
  418. pr_err("Display of symbols requested but neither sample IP nor "
  419. "sample address\navailable. Hence, no addresses to convert "
  420. "to symbols.\n");
  421. return -EINVAL;
  422. }
  423. if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
  424. pr_err("Display of offsets requested but symbol is not"
  425. "selected.\n");
  426. return -EINVAL;
  427. }
  428. if (PRINT_FIELD(DSO) &&
  429. !(evsel->core.attr.sample_type & (PERF_SAMPLE_IP|PERF_SAMPLE_ADDR))) {
  430. pr_err("Display of DSO requested but no address to convert.\n");
  431. return -EINVAL;
  432. }
  433. if ((PRINT_FIELD(SRCLINE) || PRINT_FIELD(SRCCODE)) && !PRINT_FIELD(IP)) {
  434. pr_err("Display of source line number requested but sample IP is not\n"
  435. "selected. Hence, no address to lookup the source line number.\n");
  436. return -EINVAL;
  437. }
  438. if ((PRINT_FIELD(BRSTACKINSN) || PRINT_FIELD(BRSTACKINSNLEN)) && !allow_user_set &&
  439. !(evlist__combined_branch_type(session->evlist) & PERF_SAMPLE_BRANCH_ANY)) {
  440. pr_err("Display of branch stack assembler requested, but non all-branch filter set\n"
  441. "Hint: run 'perf record -b ...'\n");
  442. return -EINVAL;
  443. }
  444. if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
  445. evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID", PERF_OUTPUT_TID|PERF_OUTPUT_PID))
  446. return -EINVAL;
  447. if (PRINT_FIELD(TIME) &&
  448. evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME", PERF_OUTPUT_TIME))
  449. return -EINVAL;
  450. if (PRINT_FIELD(CPU) &&
  451. evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU", PERF_OUTPUT_CPU, allow_user_set))
  452. return -EINVAL;
  453. if (PRINT_FIELD(IREGS) &&
  454. evsel__do_check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS", PERF_OUTPUT_IREGS, allow_user_set))
  455. return -EINVAL;
  456. if (PRINT_FIELD(UREGS) &&
  457. evsel__check_stype(evsel, PERF_SAMPLE_REGS_USER, "UREGS", PERF_OUTPUT_UREGS))
  458. return -EINVAL;
  459. if (PRINT_FIELD(PHYS_ADDR) &&
  460. evsel__do_check_stype(evsel, PERF_SAMPLE_PHYS_ADDR, "PHYS_ADDR", PERF_OUTPUT_PHYS_ADDR, allow_user_set))
  461. return -EINVAL;
  462. if (PRINT_FIELD(DATA_PAGE_SIZE) &&
  463. evsel__check_stype(evsel, PERF_SAMPLE_DATA_PAGE_SIZE, "DATA_PAGE_SIZE", PERF_OUTPUT_DATA_PAGE_SIZE))
  464. return -EINVAL;
  465. if (PRINT_FIELD(CODE_PAGE_SIZE) &&
  466. evsel__check_stype(evsel, PERF_SAMPLE_CODE_PAGE_SIZE, "CODE_PAGE_SIZE", PERF_OUTPUT_CODE_PAGE_SIZE))
  467. return -EINVAL;
  468. if (PRINT_FIELD(INS_LAT) &&
  469. evsel__check_stype(evsel, PERF_SAMPLE_WEIGHT_STRUCT, "WEIGHT_STRUCT", PERF_OUTPUT_INS_LAT))
  470. return -EINVAL;
  471. return 0;
  472. }
  473. static void set_print_ip_opts(struct perf_event_attr *attr)
  474. {
  475. unsigned int type = output_type(attr->type);
  476. output[type].print_ip_opts = 0;
  477. if (PRINT_FIELD(IP))
  478. output[type].print_ip_opts |= EVSEL__PRINT_IP;
  479. if (PRINT_FIELD(SYM))
  480. output[type].print_ip_opts |= EVSEL__PRINT_SYM;
  481. if (PRINT_FIELD(DSO))
  482. output[type].print_ip_opts |= EVSEL__PRINT_DSO;
  483. if (PRINT_FIELD(SYMOFFSET))
  484. output[type].print_ip_opts |= EVSEL__PRINT_SYMOFFSET;
  485. if (PRINT_FIELD(SRCLINE))
  486. output[type].print_ip_opts |= EVSEL__PRINT_SRCLINE;
  487. }
  488. static struct evsel *find_first_output_type(struct evlist *evlist,
  489. unsigned int type)
  490. {
  491. struct evsel *evsel;
  492. evlist__for_each_entry(evlist, evsel) {
  493. if (evsel__is_dummy_event(evsel))
  494. continue;
  495. if (output_type(evsel->core.attr.type) == (int)type)
  496. return evsel;
  497. }
  498. return NULL;
  499. }
  500. /*
  501. * verify all user requested events exist and the samples
  502. * have the expected data
  503. */
  504. static int perf_session__check_output_opt(struct perf_session *session)
  505. {
  506. bool tod = false;
  507. unsigned int j;
  508. struct evsel *evsel;
  509. for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
  510. evsel = find_first_output_type(session->evlist, j);
  511. /*
  512. * even if fields is set to 0 (ie., show nothing) event must
  513. * exist if user explicitly includes it on the command line
  514. */
  515. if (!evsel && output[j].user_set && !output[j].wildcard_set &&
  516. j != OUTPUT_TYPE_SYNTH) {
  517. pr_err("%s events do not exist. "
  518. "Remove corresponding -F option to proceed.\n",
  519. event_type(j));
  520. return -1;
  521. }
  522. if (evsel && output[j].fields &&
  523. evsel__check_attr(evsel, session))
  524. return -1;
  525. if (evsel == NULL)
  526. continue;
  527. set_print_ip_opts(&evsel->core.attr);
  528. tod |= output[j].fields & PERF_OUTPUT_TOD;
  529. }
  530. if (!no_callchain) {
  531. bool use_callchain = false;
  532. bool not_pipe = false;
  533. evlist__for_each_entry(session->evlist, evsel) {
  534. not_pipe = true;
  535. if (evsel__has_callchain(evsel)) {
  536. use_callchain = true;
  537. break;
  538. }
  539. }
  540. if (not_pipe && !use_callchain)
  541. symbol_conf.use_callchain = false;
  542. }
  543. /*
  544. * set default for tracepoints to print symbols only
  545. * if callchains are present
  546. */
  547. if (symbol_conf.use_callchain &&
  548. !output[PERF_TYPE_TRACEPOINT].user_set) {
  549. j = PERF_TYPE_TRACEPOINT;
  550. evlist__for_each_entry(session->evlist, evsel) {
  551. if (evsel->core.attr.type != j)
  552. continue;
  553. if (evsel__has_callchain(evsel)) {
  554. output[j].fields |= PERF_OUTPUT_IP;
  555. output[j].fields |= PERF_OUTPUT_SYM;
  556. output[j].fields |= PERF_OUTPUT_SYMOFFSET;
  557. output[j].fields |= PERF_OUTPUT_DSO;
  558. set_print_ip_opts(&evsel->core.attr);
  559. goto out;
  560. }
  561. }
  562. }
  563. if (tod && !session->header.env.clock.enabled) {
  564. pr_err("Can't provide 'tod' time, missing clock data. "
  565. "Please record with -k/--clockid option.\n");
  566. return -1;
  567. }
  568. out:
  569. return 0;
  570. }
  571. static int perf_sample__fprintf_regs(struct regs_dump *regs, uint64_t mask, const char *arch,
  572. FILE *fp)
  573. {
  574. unsigned i = 0, r;
  575. int printed = 0;
  576. if (!regs || !regs->regs)
  577. return 0;
  578. printed += fprintf(fp, " ABI:%" PRIu64 " ", regs->abi);
  579. for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
  580. u64 val = regs->regs[i++];
  581. printed += fprintf(fp, "%5s:0x%"PRIx64" ", perf_reg_name(r, arch), val);
  582. }
  583. return printed;
  584. }
  585. #define DEFAULT_TOD_FMT "%F %H:%M:%S"
  586. static char*
  587. tod_scnprintf(struct perf_script *script, char *buf, int buflen,
  588. u64 timestamp)
  589. {
  590. u64 tod_ns, clockid_ns;
  591. struct perf_env *env;
  592. unsigned long nsec;
  593. struct tm ltime;
  594. char date[64];
  595. time_t sec;
  596. buf[0] = '\0';
  597. if (buflen < 64 || !script)
  598. return buf;
  599. env = &script->session->header.env;
  600. if (!env->clock.enabled) {
  601. scnprintf(buf, buflen, "disabled");
  602. return buf;
  603. }
  604. clockid_ns = env->clock.clockid_ns;
  605. tod_ns = env->clock.tod_ns;
  606. if (timestamp > clockid_ns)
  607. tod_ns += timestamp - clockid_ns;
  608. else
  609. tod_ns -= clockid_ns - timestamp;
  610. sec = (time_t) (tod_ns / NSEC_PER_SEC);
  611. nsec = tod_ns - sec * NSEC_PER_SEC;
  612. if (localtime_r(&sec, &ltime) == NULL) {
  613. scnprintf(buf, buflen, "failed");
  614. } else {
  615. strftime(date, sizeof(date), DEFAULT_TOD_FMT, &ltime);
  616. if (symbol_conf.nanosecs) {
  617. snprintf(buf, buflen, "%s.%09lu", date, nsec);
  618. } else {
  619. snprintf(buf, buflen, "%s.%06lu",
  620. date, nsec / NSEC_PER_USEC);
  621. }
  622. }
  623. return buf;
  624. }
  625. static int perf_sample__fprintf_iregs(struct perf_sample *sample,
  626. struct perf_event_attr *attr, const char *arch, FILE *fp)
  627. {
  628. return perf_sample__fprintf_regs(&sample->intr_regs,
  629. attr->sample_regs_intr, arch, fp);
  630. }
  631. static int perf_sample__fprintf_uregs(struct perf_sample *sample,
  632. struct perf_event_attr *attr, const char *arch, FILE *fp)
  633. {
  634. return perf_sample__fprintf_regs(&sample->user_regs,
  635. attr->sample_regs_user, arch, fp);
  636. }
  637. static int perf_sample__fprintf_start(struct perf_script *script,
  638. struct perf_sample *sample,
  639. struct thread *thread,
  640. struct evsel *evsel,
  641. u32 type, FILE *fp)
  642. {
  643. struct perf_event_attr *attr = &evsel->core.attr;
  644. unsigned long secs;
  645. unsigned long long nsecs;
  646. int printed = 0;
  647. char tstr[128];
  648. if (PRINT_FIELD(MACHINE_PID) && sample->machine_pid)
  649. printed += fprintf(fp, "VM:%5d ", sample->machine_pid);
  650. /* Print VCPU only for guest events i.e. with machine_pid */
  651. if (PRINT_FIELD(VCPU) && sample->machine_pid)
  652. printed += fprintf(fp, "VCPU:%03d ", sample->vcpu);
  653. if (PRINT_FIELD(COMM)) {
  654. const char *comm = thread ? thread__comm_str(thread) : ":-1";
  655. if (latency_format)
  656. printed += fprintf(fp, "%8.8s ", comm);
  657. else if (PRINT_FIELD(IP) && evsel__has_callchain(evsel) && symbol_conf.use_callchain)
  658. printed += fprintf(fp, "%s ", comm);
  659. else
  660. printed += fprintf(fp, "%16s ", comm);
  661. }
  662. if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
  663. printed += fprintf(fp, "%5d/%-5d ", sample->pid, sample->tid);
  664. else if (PRINT_FIELD(PID))
  665. printed += fprintf(fp, "%5d ", sample->pid);
  666. else if (PRINT_FIELD(TID))
  667. printed += fprintf(fp, "%5d ", sample->tid);
  668. if (PRINT_FIELD(CPU)) {
  669. if (latency_format)
  670. printed += fprintf(fp, "%3d ", sample->cpu);
  671. else
  672. printed += fprintf(fp, "[%03d] ", sample->cpu);
  673. }
  674. if (PRINT_FIELD(MISC)) {
  675. int ret = 0;
  676. #define has(m) \
  677. (sample->misc & PERF_RECORD_MISC_##m) == PERF_RECORD_MISC_##m
  678. if (has(KERNEL))
  679. ret += fprintf(fp, "K");
  680. if (has(USER))
  681. ret += fprintf(fp, "U");
  682. if (has(HYPERVISOR))
  683. ret += fprintf(fp, "H");
  684. if (has(GUEST_KERNEL))
  685. ret += fprintf(fp, "G");
  686. if (has(GUEST_USER))
  687. ret += fprintf(fp, "g");
  688. switch (type) {
  689. case PERF_RECORD_MMAP:
  690. case PERF_RECORD_MMAP2:
  691. if (has(MMAP_DATA))
  692. ret += fprintf(fp, "M");
  693. break;
  694. case PERF_RECORD_COMM:
  695. if (has(COMM_EXEC))
  696. ret += fprintf(fp, "E");
  697. break;
  698. case PERF_RECORD_SWITCH:
  699. case PERF_RECORD_SWITCH_CPU_WIDE:
  700. if (has(SWITCH_OUT)) {
  701. ret += fprintf(fp, "S");
  702. if (sample->misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT)
  703. ret += fprintf(fp, "p");
  704. }
  705. default:
  706. break;
  707. }
  708. #undef has
  709. ret += fprintf(fp, "%*s", 6 - ret, " ");
  710. printed += ret;
  711. }
  712. if (PRINT_FIELD(TOD)) {
  713. tod_scnprintf(script, tstr, sizeof(tstr), sample->time);
  714. printed += fprintf(fp, "%s ", tstr);
  715. }
  716. if (PRINT_FIELD(TIME)) {
  717. u64 t = sample->time;
  718. if (reltime) {
  719. if (!initial_time)
  720. initial_time = sample->time;
  721. t = sample->time - initial_time;
  722. } else if (deltatime) {
  723. if (previous_time)
  724. t = sample->time - previous_time;
  725. else {
  726. t = 0;
  727. }
  728. previous_time = sample->time;
  729. }
  730. nsecs = t;
  731. secs = nsecs / NSEC_PER_SEC;
  732. nsecs -= secs * NSEC_PER_SEC;
  733. if (symbol_conf.nanosecs)
  734. printed += fprintf(fp, "%5lu.%09llu: ", secs, nsecs);
  735. else {
  736. char sample_time[32];
  737. timestamp__scnprintf_usec(t, sample_time, sizeof(sample_time));
  738. printed += fprintf(fp, "%12s: ", sample_time);
  739. }
  740. }
  741. return printed;
  742. }
  743. static inline char
  744. mispred_str(struct branch_entry *br)
  745. {
  746. if (!(br->flags.mispred || br->flags.predicted))
  747. return '-';
  748. return br->flags.predicted ? 'P' : 'M';
  749. }
  750. static int print_bstack_flags(FILE *fp, struct branch_entry *br)
  751. {
  752. return fprintf(fp, "/%c/%c/%c/%d/%s ",
  753. mispred_str(br),
  754. br->flags.in_tx ? 'X' : '-',
  755. br->flags.abort ? 'A' : '-',
  756. br->flags.cycles,
  757. get_branch_type(br));
  758. }
  759. static int perf_sample__fprintf_brstack(struct perf_sample *sample,
  760. struct thread *thread,
  761. struct perf_event_attr *attr, FILE *fp)
  762. {
  763. struct branch_stack *br = sample->branch_stack;
  764. struct branch_entry *entries = perf_sample__branch_entries(sample);
  765. struct addr_location alf, alt;
  766. u64 i, from, to;
  767. int printed = 0;
  768. if (!(br && br->nr))
  769. return 0;
  770. for (i = 0; i < br->nr; i++) {
  771. from = entries[i].from;
  772. to = entries[i].to;
  773. if (PRINT_FIELD(DSO)) {
  774. memset(&alf, 0, sizeof(alf));
  775. memset(&alt, 0, sizeof(alt));
  776. thread__find_map_fb(thread, sample->cpumode, from, &alf);
  777. thread__find_map_fb(thread, sample->cpumode, to, &alt);
  778. }
  779. printed += fprintf(fp, " 0x%"PRIx64, from);
  780. if (PRINT_FIELD(DSO)) {
  781. printed += fprintf(fp, "(");
  782. printed += map__fprintf_dsoname(alf.map, fp);
  783. printed += fprintf(fp, ")");
  784. }
  785. printed += fprintf(fp, "/0x%"PRIx64, to);
  786. if (PRINT_FIELD(DSO)) {
  787. printed += fprintf(fp, "(");
  788. printed += map__fprintf_dsoname(alt.map, fp);
  789. printed += fprintf(fp, ")");
  790. }
  791. printed += print_bstack_flags(fp, entries + i);
  792. }
  793. return printed;
  794. }
  795. static int perf_sample__fprintf_brstacksym(struct perf_sample *sample,
  796. struct thread *thread,
  797. struct perf_event_attr *attr, FILE *fp)
  798. {
  799. struct branch_stack *br = sample->branch_stack;
  800. struct branch_entry *entries = perf_sample__branch_entries(sample);
  801. struct addr_location alf, alt;
  802. u64 i, from, to;
  803. int printed = 0;
  804. if (!(br && br->nr))
  805. return 0;
  806. for (i = 0; i < br->nr; i++) {
  807. memset(&alf, 0, sizeof(alf));
  808. memset(&alt, 0, sizeof(alt));
  809. from = entries[i].from;
  810. to = entries[i].to;
  811. thread__find_symbol_fb(thread, sample->cpumode, from, &alf);
  812. thread__find_symbol_fb(thread, sample->cpumode, to, &alt);
  813. printed += symbol__fprintf_symname_offs(alf.sym, &alf, fp);
  814. if (PRINT_FIELD(DSO)) {
  815. printed += fprintf(fp, "(");
  816. printed += map__fprintf_dsoname(alf.map, fp);
  817. printed += fprintf(fp, ")");
  818. }
  819. printed += fprintf(fp, "%c", '/');
  820. printed += symbol__fprintf_symname_offs(alt.sym, &alt, fp);
  821. if (PRINT_FIELD(DSO)) {
  822. printed += fprintf(fp, "(");
  823. printed += map__fprintf_dsoname(alt.map, fp);
  824. printed += fprintf(fp, ")");
  825. }
  826. printed += print_bstack_flags(fp, entries + i);
  827. }
  828. return printed;
  829. }
  830. static int perf_sample__fprintf_brstackoff(struct perf_sample *sample,
  831. struct thread *thread,
  832. struct perf_event_attr *attr, FILE *fp)
  833. {
  834. struct branch_stack *br = sample->branch_stack;
  835. struct branch_entry *entries = perf_sample__branch_entries(sample);
  836. struct addr_location alf, alt;
  837. u64 i, from, to;
  838. int printed = 0;
  839. if (!(br && br->nr))
  840. return 0;
  841. for (i = 0; i < br->nr; i++) {
  842. memset(&alf, 0, sizeof(alf));
  843. memset(&alt, 0, sizeof(alt));
  844. from = entries[i].from;
  845. to = entries[i].to;
  846. if (thread__find_map_fb(thread, sample->cpumode, from, &alf) &&
  847. !alf.map->dso->adjust_symbols)
  848. from = map__map_ip(alf.map, from);
  849. if (thread__find_map_fb(thread, sample->cpumode, to, &alt) &&
  850. !alt.map->dso->adjust_symbols)
  851. to = map__map_ip(alt.map, to);
  852. printed += fprintf(fp, " 0x%"PRIx64, from);
  853. if (PRINT_FIELD(DSO)) {
  854. printed += fprintf(fp, "(");
  855. printed += map__fprintf_dsoname(alf.map, fp);
  856. printed += fprintf(fp, ")");
  857. }
  858. printed += fprintf(fp, "/0x%"PRIx64, to);
  859. if (PRINT_FIELD(DSO)) {
  860. printed += fprintf(fp, "(");
  861. printed += map__fprintf_dsoname(alt.map, fp);
  862. printed += fprintf(fp, ")");
  863. }
  864. printed += print_bstack_flags(fp, entries + i);
  865. }
  866. return printed;
  867. }
  868. #define MAXBB 16384UL
  869. static int grab_bb(u8 *buffer, u64 start, u64 end,
  870. struct machine *machine, struct thread *thread,
  871. bool *is64bit, u8 *cpumode, bool last)
  872. {
  873. long offset, len;
  874. struct addr_location al;
  875. bool kernel;
  876. if (!start || !end)
  877. return 0;
  878. kernel = machine__kernel_ip(machine, start);
  879. if (kernel)
  880. *cpumode = PERF_RECORD_MISC_KERNEL;
  881. else
  882. *cpumode = PERF_RECORD_MISC_USER;
  883. /*
  884. * Block overlaps between kernel and user.
  885. * This can happen due to ring filtering
  886. * On Intel CPUs the entry into the kernel is filtered,
  887. * but the exit is not. Let the caller patch it up.
  888. */
  889. if (kernel != machine__kernel_ip(machine, end)) {
  890. pr_debug("\tblock %" PRIx64 "-%" PRIx64 " transfers between kernel and user\n", start, end);
  891. return -ENXIO;
  892. }
  893. memset(&al, 0, sizeof(al));
  894. if (end - start > MAXBB - MAXINSN) {
  895. if (last)
  896. pr_debug("\tbrstack does not reach to final jump (%" PRIx64 "-%" PRIx64 ")\n", start, end);
  897. else
  898. pr_debug("\tblock %" PRIx64 "-%" PRIx64 " (%" PRIu64 ") too long to dump\n", start, end, end - start);
  899. return 0;
  900. }
  901. if (!thread__find_map(thread, *cpumode, start, &al) || !al.map->dso) {
  902. pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
  903. return 0;
  904. }
  905. if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR) {
  906. pr_debug("\tcannot resolve %" PRIx64 "-%" PRIx64 "\n", start, end);
  907. return 0;
  908. }
  909. /* Load maps to ensure dso->is_64_bit has been updated */
  910. map__load(al.map);
  911. offset = al.map->map_ip(al.map, start);
  912. len = dso__data_read_offset(al.map->dso, machine, offset, (u8 *)buffer,
  913. end - start + MAXINSN);
  914. *is64bit = al.map->dso->is_64_bit;
  915. if (len <= 0)
  916. pr_debug("\tcannot fetch code for block at %" PRIx64 "-%" PRIx64 "\n",
  917. start, end);
  918. return len;
  919. }
  920. static int map__fprintf_srccode(struct map *map, u64 addr, FILE *fp, struct srccode_state *state)
  921. {
  922. char *srcfile;
  923. int ret = 0;
  924. unsigned line;
  925. int len;
  926. char *srccode;
  927. if (!map || !map->dso)
  928. return 0;
  929. srcfile = get_srcline_split(map->dso,
  930. map__rip_2objdump(map, addr),
  931. &line);
  932. if (!srcfile)
  933. return 0;
  934. /* Avoid redundant printing */
  935. if (state &&
  936. state->srcfile &&
  937. !strcmp(state->srcfile, srcfile) &&
  938. state->line == line) {
  939. free(srcfile);
  940. return 0;
  941. }
  942. srccode = find_sourceline(srcfile, line, &len);
  943. if (!srccode)
  944. goto out_free_line;
  945. ret = fprintf(fp, "|%-8d %.*s", line, len, srccode);
  946. if (state) {
  947. state->srcfile = srcfile;
  948. state->line = line;
  949. }
  950. return ret;
  951. out_free_line:
  952. free(srcfile);
  953. return ret;
  954. }
  955. static int print_srccode(struct thread *thread, u8 cpumode, uint64_t addr)
  956. {
  957. struct addr_location al;
  958. int ret = 0;
  959. memset(&al, 0, sizeof(al));
  960. thread__find_map(thread, cpumode, addr, &al);
  961. if (!al.map)
  962. return 0;
  963. ret = map__fprintf_srccode(al.map, al.addr, stdout,
  964. &thread->srccode_state);
  965. if (ret)
  966. ret += printf("\n");
  967. return ret;
  968. }
  969. static int ip__fprintf_jump(uint64_t ip, struct branch_entry *en,
  970. struct perf_insn *x, u8 *inbuf, int len,
  971. int insn, FILE *fp, int *total_cycles,
  972. struct perf_event_attr *attr)
  973. {
  974. int ilen = 0;
  975. int printed = fprintf(fp, "\t%016" PRIx64 "\t%-30s\t", ip,
  976. dump_insn(x, ip, inbuf, len, &ilen));
  977. if (PRINT_FIELD(BRSTACKINSNLEN))
  978. printed += fprintf(fp, "ilen: %d\t", ilen);
  979. printed += fprintf(fp, "#%s%s%s%s",
  980. en->flags.predicted ? " PRED" : "",
  981. en->flags.mispred ? " MISPRED" : "",
  982. en->flags.in_tx ? " INTX" : "",
  983. en->flags.abort ? " ABORT" : "");
  984. if (en->flags.cycles) {
  985. *total_cycles += en->flags.cycles;
  986. printed += fprintf(fp, " %d cycles [%d]", en->flags.cycles, *total_cycles);
  987. if (insn)
  988. printed += fprintf(fp, " %.2f IPC", (float)insn / en->flags.cycles);
  989. }
  990. return printed + fprintf(fp, "\n");
  991. }
  992. static int ip__fprintf_sym(uint64_t addr, struct thread *thread,
  993. u8 cpumode, int cpu, struct symbol **lastsym,
  994. struct perf_event_attr *attr, FILE *fp)
  995. {
  996. struct addr_location al;
  997. int off, printed = 0;
  998. memset(&al, 0, sizeof(al));
  999. thread__find_map(thread, cpumode, addr, &al);
  1000. if ((*lastsym) && al.addr >= (*lastsym)->start && al.addr < (*lastsym)->end)
  1001. return 0;
  1002. al.cpu = cpu;
  1003. al.sym = NULL;
  1004. if (al.map)
  1005. al.sym = map__find_symbol(al.map, al.addr);
  1006. if (!al.sym)
  1007. return 0;
  1008. if (al.addr < al.sym->end)
  1009. off = al.addr - al.sym->start;
  1010. else
  1011. off = al.addr - al.map->start - al.sym->start;
  1012. printed += fprintf(fp, "\t%s", al.sym->name);
  1013. if (off)
  1014. printed += fprintf(fp, "%+d", off);
  1015. printed += fprintf(fp, ":");
  1016. if (PRINT_FIELD(SRCLINE))
  1017. printed += map__fprintf_srcline(al.map, al.addr, "\t", fp);
  1018. printed += fprintf(fp, "\n");
  1019. *lastsym = al.sym;
  1020. return printed;
  1021. }
  1022. static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample,
  1023. struct thread *thread,
  1024. struct perf_event_attr *attr,
  1025. struct machine *machine, FILE *fp)
  1026. {
  1027. struct branch_stack *br = sample->branch_stack;
  1028. struct branch_entry *entries = perf_sample__branch_entries(sample);
  1029. u64 start, end;
  1030. int i, insn, len, nr, ilen, printed = 0;
  1031. struct perf_insn x;
  1032. u8 buffer[MAXBB];
  1033. unsigned off;
  1034. struct symbol *lastsym = NULL;
  1035. int total_cycles = 0;
  1036. if (!(br && br->nr))
  1037. return 0;
  1038. nr = br->nr;
  1039. if (max_blocks && nr > max_blocks + 1)
  1040. nr = max_blocks + 1;
  1041. x.thread = thread;
  1042. x.cpu = sample->cpu;
  1043. printed += fprintf(fp, "%c", '\n');
  1044. /* Handle first from jump, of which we don't know the entry. */
  1045. len = grab_bb(buffer, entries[nr-1].from,
  1046. entries[nr-1].from,
  1047. machine, thread, &x.is64bit, &x.cpumode, false);
  1048. if (len > 0) {
  1049. printed += ip__fprintf_sym(entries[nr - 1].from, thread,
  1050. x.cpumode, x.cpu, &lastsym, attr, fp);
  1051. printed += ip__fprintf_jump(entries[nr - 1].from, &entries[nr - 1],
  1052. &x, buffer, len, 0, fp, &total_cycles,
  1053. attr);
  1054. if (PRINT_FIELD(SRCCODE))
  1055. printed += print_srccode(thread, x.cpumode, entries[nr - 1].from);
  1056. }
  1057. /* Print all blocks */
  1058. for (i = nr - 2; i >= 0; i--) {
  1059. if (entries[i].from || entries[i].to)
  1060. pr_debug("%d: %" PRIx64 "-%" PRIx64 "\n", i,
  1061. entries[i].from,
  1062. entries[i].to);
  1063. start = entries[i + 1].to;
  1064. end = entries[i].from;
  1065. len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
  1066. /* Patch up missing kernel transfers due to ring filters */
  1067. if (len == -ENXIO && i > 0) {
  1068. end = entries[--i].from;
  1069. pr_debug("\tpatching up to %" PRIx64 "-%" PRIx64 "\n", start, end);
  1070. len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, false);
  1071. }
  1072. if (len <= 0)
  1073. continue;
  1074. insn = 0;
  1075. for (off = 0; off < (unsigned)len; off += ilen) {
  1076. uint64_t ip = start + off;
  1077. printed += ip__fprintf_sym(ip, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
  1078. if (ip == end) {
  1079. printed += ip__fprintf_jump(ip, &entries[i], &x, buffer + off, len - off, ++insn, fp,
  1080. &total_cycles, attr);
  1081. if (PRINT_FIELD(SRCCODE))
  1082. printed += print_srccode(thread, x.cpumode, ip);
  1083. break;
  1084. } else {
  1085. ilen = 0;
  1086. printed += fprintf(fp, "\t%016" PRIx64 "\t%s", ip,
  1087. dump_insn(&x, ip, buffer + off, len - off, &ilen));
  1088. if (PRINT_FIELD(BRSTACKINSNLEN))
  1089. printed += fprintf(fp, "\tilen: %d", ilen);
  1090. printed += fprintf(fp, "\n");
  1091. if (ilen == 0)
  1092. break;
  1093. if (PRINT_FIELD(SRCCODE))
  1094. print_srccode(thread, x.cpumode, ip);
  1095. insn++;
  1096. }
  1097. }
  1098. if (off != end - start)
  1099. printed += fprintf(fp, "\tmismatch of LBR data and executable\n");
  1100. }
  1101. /*
  1102. * Hit the branch? In this case we are already done, and the target
  1103. * has not been executed yet.
  1104. */
  1105. if (entries[0].from == sample->ip)
  1106. goto out;
  1107. if (entries[0].flags.abort)
  1108. goto out;
  1109. /*
  1110. * Print final block upto sample
  1111. *
  1112. * Due to pipeline delays the LBRs might be missing a branch
  1113. * or two, which can result in very large or negative blocks
  1114. * between final branch and sample. When this happens just
  1115. * continue walking after the last TO until we hit a branch.
  1116. */
  1117. start = entries[0].to;
  1118. end = sample->ip;
  1119. if (end < start) {
  1120. /* Missing jump. Scan 128 bytes for the next branch */
  1121. end = start + 128;
  1122. }
  1123. len = grab_bb(buffer, start, end, machine, thread, &x.is64bit, &x.cpumode, true);
  1124. printed += ip__fprintf_sym(start, thread, x.cpumode, x.cpu, &lastsym, attr, fp);
  1125. if (len <= 0) {
  1126. /* Print at least last IP if basic block did not work */
  1127. len = grab_bb(buffer, sample->ip, sample->ip,
  1128. machine, thread, &x.is64bit, &x.cpumode, false);
  1129. if (len <= 0)
  1130. goto out;
  1131. ilen = 0;
  1132. printed += fprintf(fp, "\t%016" PRIx64 "\t%s", sample->ip,
  1133. dump_insn(&x, sample->ip, buffer, len, &ilen));
  1134. if (PRINT_FIELD(BRSTACKINSNLEN))
  1135. printed += fprintf(fp, "\tilen: %d", ilen);
  1136. printed += fprintf(fp, "\n");
  1137. if (PRINT_FIELD(SRCCODE))
  1138. print_srccode(thread, x.cpumode, sample->ip);
  1139. goto out;
  1140. }
  1141. for (off = 0; off <= end - start; off += ilen) {
  1142. ilen = 0;
  1143. printed += fprintf(fp, "\t%016" PRIx64 "\t%s", start + off,
  1144. dump_insn(&x, start + off, buffer + off, len - off, &ilen));
  1145. if (PRINT_FIELD(BRSTACKINSNLEN))
  1146. printed += fprintf(fp, "\tilen: %d", ilen);
  1147. printed += fprintf(fp, "\n");
  1148. if (ilen == 0)
  1149. break;
  1150. if (arch_is_branch(buffer + off, len - off, x.is64bit) && start + off != sample->ip) {
  1151. /*
  1152. * Hit a missing branch. Just stop.
  1153. */
  1154. printed += fprintf(fp, "\t... not reaching sample ...\n");
  1155. break;
  1156. }
  1157. if (PRINT_FIELD(SRCCODE))
  1158. print_srccode(thread, x.cpumode, start + off);
  1159. }
  1160. out:
  1161. return printed;
  1162. }
  1163. static int perf_sample__fprintf_addr(struct perf_sample *sample,
  1164. struct thread *thread,
  1165. struct perf_event_attr *attr, FILE *fp)
  1166. {
  1167. struct addr_location al;
  1168. int printed = fprintf(fp, "%16" PRIx64, sample->addr);
  1169. if (!sample_addr_correlates_sym(attr))
  1170. goto out;
  1171. thread__resolve(thread, &al, sample);
  1172. if (PRINT_FIELD(SYM)) {
  1173. printed += fprintf(fp, " ");
  1174. if (PRINT_FIELD(SYMOFFSET))
  1175. printed += symbol__fprintf_symname_offs(al.sym, &al, fp);
  1176. else
  1177. printed += symbol__fprintf_symname(al.sym, fp);
  1178. }
  1179. if (PRINT_FIELD(DSO)) {
  1180. printed += fprintf(fp, " (");
  1181. printed += map__fprintf_dsoname(al.map, fp);
  1182. printed += fprintf(fp, ")");
  1183. }
  1184. out:
  1185. return printed;
  1186. }
  1187. static const char *resolve_branch_sym(struct perf_sample *sample,
  1188. struct evsel *evsel,
  1189. struct thread *thread,
  1190. struct addr_location *al,
  1191. struct addr_location *addr_al,
  1192. u64 *ip)
  1193. {
  1194. struct perf_event_attr *attr = &evsel->core.attr;
  1195. const char *name = NULL;
  1196. if (sample->flags & (PERF_IP_FLAG_CALL | PERF_IP_FLAG_TRACE_BEGIN)) {
  1197. if (sample_addr_correlates_sym(attr)) {
  1198. if (!addr_al->thread)
  1199. thread__resolve(thread, addr_al, sample);
  1200. if (addr_al->sym)
  1201. name = addr_al->sym->name;
  1202. else
  1203. *ip = sample->addr;
  1204. } else {
  1205. *ip = sample->addr;
  1206. }
  1207. } else if (sample->flags & (PERF_IP_FLAG_RETURN | PERF_IP_FLAG_TRACE_END)) {
  1208. if (al->sym)
  1209. name = al->sym->name;
  1210. else
  1211. *ip = sample->ip;
  1212. }
  1213. return name;
  1214. }
  1215. static int perf_sample__fprintf_callindent(struct perf_sample *sample,
  1216. struct evsel *evsel,
  1217. struct thread *thread,
  1218. struct addr_location *al,
  1219. struct addr_location *addr_al,
  1220. FILE *fp)
  1221. {
  1222. struct perf_event_attr *attr = &evsel->core.attr;
  1223. size_t depth = thread_stack__depth(thread, sample->cpu);
  1224. const char *name = NULL;
  1225. static int spacing;
  1226. int len = 0;
  1227. int dlen = 0;
  1228. u64 ip = 0;
  1229. /*
  1230. * The 'return' has already been popped off the stack so the depth has
  1231. * to be adjusted to match the 'call'.
  1232. */
  1233. if (thread->ts && sample->flags & PERF_IP_FLAG_RETURN)
  1234. depth += 1;
  1235. name = resolve_branch_sym(sample, evsel, thread, al, addr_al, &ip);
  1236. if (PRINT_FIELD(DSO) && !(PRINT_FIELD(IP) || PRINT_FIELD(ADDR))) {
  1237. dlen += fprintf(fp, "(");
  1238. dlen += map__fprintf_dsoname(al->map, fp);
  1239. dlen += fprintf(fp, ")\t");
  1240. }
  1241. if (name)
  1242. len = fprintf(fp, "%*s%s", (int)depth * 4, "", name);
  1243. else if (ip)
  1244. len = fprintf(fp, "%*s%16" PRIx64, (int)depth * 4, "", ip);
  1245. if (len < 0)
  1246. return len;
  1247. /*
  1248. * Try to keep the output length from changing frequently so that the
  1249. * output lines up more nicely.
  1250. */
  1251. if (len > spacing || (len && len < spacing - 52))
  1252. spacing = round_up(len + 4, 32);
  1253. if (len < spacing)
  1254. len += fprintf(fp, "%*s", spacing - len, "");
  1255. return len + dlen;
  1256. }
  1257. __weak void arch_fetch_insn(struct perf_sample *sample __maybe_unused,
  1258. struct thread *thread __maybe_unused,
  1259. struct machine *machine __maybe_unused)
  1260. {
  1261. }
  1262. void script_fetch_insn(struct perf_sample *sample, struct thread *thread,
  1263. struct machine *machine)
  1264. {
  1265. if (sample->insn_len == 0 && native_arch)
  1266. arch_fetch_insn(sample, thread, machine);
  1267. }
  1268. static int perf_sample__fprintf_insn(struct perf_sample *sample,
  1269. struct perf_event_attr *attr,
  1270. struct thread *thread,
  1271. struct machine *machine, FILE *fp)
  1272. {
  1273. int printed = 0;
  1274. script_fetch_insn(sample, thread, machine);
  1275. if (PRINT_FIELD(INSNLEN))
  1276. printed += fprintf(fp, " ilen: %d", sample->insn_len);
  1277. if (PRINT_FIELD(INSN) && sample->insn_len) {
  1278. int i;
  1279. printed += fprintf(fp, " insn:");
  1280. for (i = 0; i < sample->insn_len; i++)
  1281. printed += fprintf(fp, " %02x", (unsigned char)sample->insn[i]);
  1282. }
  1283. if (PRINT_FIELD(BRSTACKINSN) || PRINT_FIELD(BRSTACKINSNLEN))
  1284. printed += perf_sample__fprintf_brstackinsn(sample, thread, attr, machine, fp);
  1285. return printed;
  1286. }
  1287. static int perf_sample__fprintf_ipc(struct perf_sample *sample,
  1288. struct perf_event_attr *attr, FILE *fp)
  1289. {
  1290. unsigned int ipc;
  1291. if (!PRINT_FIELD(IPC) || !sample->cyc_cnt || !sample->insn_cnt)
  1292. return 0;
  1293. ipc = (sample->insn_cnt * 100) / sample->cyc_cnt;
  1294. return fprintf(fp, " \t IPC: %u.%02u (%" PRIu64 "/%" PRIu64 ") ",
  1295. ipc / 100, ipc % 100, sample->insn_cnt, sample->cyc_cnt);
  1296. }
  1297. static int perf_sample__fprintf_bts(struct perf_sample *sample,
  1298. struct evsel *evsel,
  1299. struct thread *thread,
  1300. struct addr_location *al,
  1301. struct addr_location *addr_al,
  1302. struct machine *machine, FILE *fp)
  1303. {
  1304. struct perf_event_attr *attr = &evsel->core.attr;
  1305. unsigned int type = output_type(attr->type);
  1306. bool print_srcline_last = false;
  1307. int printed = 0;
  1308. if (PRINT_FIELD(CALLINDENT))
  1309. printed += perf_sample__fprintf_callindent(sample, evsel, thread, al, addr_al, fp);
  1310. /* print branch_from information */
  1311. if (PRINT_FIELD(IP)) {
  1312. unsigned int print_opts = output[type].print_ip_opts;
  1313. struct callchain_cursor *cursor = NULL;
  1314. if (symbol_conf.use_callchain && sample->callchain &&
  1315. thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
  1316. sample, NULL, NULL, scripting_max_stack) == 0)
  1317. cursor = &callchain_cursor;
  1318. if (cursor == NULL) {
  1319. printed += fprintf(fp, " ");
  1320. if (print_opts & EVSEL__PRINT_SRCLINE) {
  1321. print_srcline_last = true;
  1322. print_opts &= ~EVSEL__PRINT_SRCLINE;
  1323. }
  1324. } else
  1325. printed += fprintf(fp, "\n");
  1326. printed += sample__fprintf_sym(sample, al, 0, print_opts, cursor,
  1327. symbol_conf.bt_stop_list, fp);
  1328. }
  1329. /* print branch_to information */
  1330. if (PRINT_FIELD(ADDR) ||
  1331. ((evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
  1332. !output[type].user_set)) {
  1333. printed += fprintf(fp, " => ");
  1334. printed += perf_sample__fprintf_addr(sample, thread, attr, fp);
  1335. }
  1336. printed += perf_sample__fprintf_ipc(sample, attr, fp);
  1337. if (print_srcline_last)
  1338. printed += map__fprintf_srcline(al->map, al->addr, "\n ", fp);
  1339. printed += perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
  1340. printed += fprintf(fp, "\n");
  1341. if (PRINT_FIELD(SRCCODE)) {
  1342. int ret = map__fprintf_srccode(al->map, al->addr, stdout,
  1343. &thread->srccode_state);
  1344. if (ret) {
  1345. printed += ret;
  1346. printed += printf("\n");
  1347. }
  1348. }
  1349. return printed;
  1350. }
  1351. static struct {
  1352. u32 flags;
  1353. const char *name;
  1354. } sample_flags[] = {
  1355. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL, "call"},
  1356. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN, "return"},
  1357. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CONDITIONAL, "jcc"},
  1358. {PERF_IP_FLAG_BRANCH, "jmp"},
  1359. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_INTERRUPT, "int"},
  1360. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_INTERRUPT, "iret"},
  1361. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_SYSCALLRET, "syscall"},
  1362. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_RETURN | PERF_IP_FLAG_SYSCALLRET, "sysret"},
  1363. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_ASYNC, "async"},
  1364. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | PERF_IP_FLAG_INTERRUPT, "hw int"},
  1365. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT, "tx abrt"},
  1366. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_BEGIN, "tr strt"},
  1367. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TRACE_END, "tr end"},
  1368. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_VMENTRY, "vmentry"},
  1369. {PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL | PERF_IP_FLAG_VMEXIT, "vmexit"},
  1370. {0, NULL}
  1371. };
  1372. static const char *sample_flags_to_name(u32 flags)
  1373. {
  1374. int i;
  1375. for (i = 0; sample_flags[i].name ; i++) {
  1376. if (sample_flags[i].flags == flags)
  1377. return sample_flags[i].name;
  1378. }
  1379. return NULL;
  1380. }
  1381. int perf_sample__sprintf_flags(u32 flags, char *str, size_t sz)
  1382. {
  1383. u32 xf = PERF_IP_FLAG_IN_TX | PERF_IP_FLAG_INTR_DISABLE |
  1384. PERF_IP_FLAG_INTR_TOGGLE;
  1385. const char *chars = PERF_IP_FLAG_CHARS;
  1386. const size_t n = strlen(PERF_IP_FLAG_CHARS);
  1387. const char *name = NULL;
  1388. size_t i, pos = 0;
  1389. char xs[16] = {0};
  1390. if (flags & xf)
  1391. snprintf(xs, sizeof(xs), "(%s%s%s)",
  1392. flags & PERF_IP_FLAG_IN_TX ? "x" : "",
  1393. flags & PERF_IP_FLAG_INTR_DISABLE ? "D" : "",
  1394. flags & PERF_IP_FLAG_INTR_TOGGLE ? "t" : "");
  1395. name = sample_flags_to_name(flags & ~xf);
  1396. if (name)
  1397. return snprintf(str, sz, "%-15s%6s", name, xs);
  1398. if (flags & PERF_IP_FLAG_TRACE_BEGIN) {
  1399. name = sample_flags_to_name(flags & ~(xf | PERF_IP_FLAG_TRACE_BEGIN));
  1400. if (name)
  1401. return snprintf(str, sz, "tr strt %-7s%6s", name, xs);
  1402. }
  1403. if (flags & PERF_IP_FLAG_TRACE_END) {
  1404. name = sample_flags_to_name(flags & ~(xf | PERF_IP_FLAG_TRACE_END));
  1405. if (name)
  1406. return snprintf(str, sz, "tr end %-7s%6s", name, xs);
  1407. }
  1408. for (i = 0; i < n; i++, flags >>= 1) {
  1409. if ((flags & 1) && pos < sz)
  1410. str[pos++] = chars[i];
  1411. }
  1412. for (; i < 32; i++, flags >>= 1) {
  1413. if ((flags & 1) && pos < sz)
  1414. str[pos++] = '?';
  1415. }
  1416. if (pos < sz)
  1417. str[pos] = 0;
  1418. return pos;
  1419. }
  1420. static int perf_sample__fprintf_flags(u32 flags, FILE *fp)
  1421. {
  1422. char str[SAMPLE_FLAGS_BUF_SIZE];
  1423. perf_sample__sprintf_flags(flags, str, sizeof(str));
  1424. return fprintf(fp, " %-21s ", str);
  1425. }
  1426. struct printer_data {
  1427. int line_no;
  1428. bool hit_nul;
  1429. bool is_printable;
  1430. };
  1431. static int sample__fprintf_bpf_output(enum binary_printer_ops op,
  1432. unsigned int val,
  1433. void *extra, FILE *fp)
  1434. {
  1435. unsigned char ch = (unsigned char)val;
  1436. struct printer_data *printer_data = extra;
  1437. int printed = 0;
  1438. switch (op) {
  1439. case BINARY_PRINT_DATA_BEGIN:
  1440. printed += fprintf(fp, "\n");
  1441. break;
  1442. case BINARY_PRINT_LINE_BEGIN:
  1443. printed += fprintf(fp, "%17s", !printer_data->line_no ? "BPF output:" :
  1444. " ");
  1445. break;
  1446. case BINARY_PRINT_ADDR:
  1447. printed += fprintf(fp, " %04x:", val);
  1448. break;
  1449. case BINARY_PRINT_NUM_DATA:
  1450. printed += fprintf(fp, " %02x", val);
  1451. break;
  1452. case BINARY_PRINT_NUM_PAD:
  1453. printed += fprintf(fp, " ");
  1454. break;
  1455. case BINARY_PRINT_SEP:
  1456. printed += fprintf(fp, " ");
  1457. break;
  1458. case BINARY_PRINT_CHAR_DATA:
  1459. if (printer_data->hit_nul && ch)
  1460. printer_data->is_printable = false;
  1461. if (!isprint(ch)) {
  1462. printed += fprintf(fp, "%c", '.');
  1463. if (!printer_data->is_printable)
  1464. break;
  1465. if (ch == '\0')
  1466. printer_data->hit_nul = true;
  1467. else
  1468. printer_data->is_printable = false;
  1469. } else {
  1470. printed += fprintf(fp, "%c", ch);
  1471. }
  1472. break;
  1473. case BINARY_PRINT_CHAR_PAD:
  1474. printed += fprintf(fp, " ");
  1475. break;
  1476. case BINARY_PRINT_LINE_END:
  1477. printed += fprintf(fp, "\n");
  1478. printer_data->line_no++;
  1479. break;
  1480. case BINARY_PRINT_DATA_END:
  1481. default:
  1482. break;
  1483. }
  1484. return printed;
  1485. }
  1486. static int perf_sample__fprintf_bpf_output(struct perf_sample *sample, FILE *fp)
  1487. {
  1488. unsigned int nr_bytes = sample->raw_size;
  1489. struct printer_data printer_data = {0, false, true};
  1490. int printed = binary__fprintf(sample->raw_data, nr_bytes, 8,
  1491. sample__fprintf_bpf_output, &printer_data, fp);
  1492. if (printer_data.is_printable && printer_data.hit_nul)
  1493. printed += fprintf(fp, "%17s \"%s\"\n", "BPF string:", (char *)(sample->raw_data));
  1494. return printed;
  1495. }
  1496. static int perf_sample__fprintf_spacing(int len, int spacing, FILE *fp)
  1497. {
  1498. if (len > 0 && len < spacing)
  1499. return fprintf(fp, "%*s", spacing - len, "");
  1500. return 0;
  1501. }
  1502. static int perf_sample__fprintf_pt_spacing(int len, FILE *fp)
  1503. {
  1504. return perf_sample__fprintf_spacing(len, 34, fp);
  1505. }
  1506. /* If a value contains only printable ASCII characters padded with NULLs */
  1507. static bool ptw_is_prt(u64 val)
  1508. {
  1509. char c;
  1510. u32 i;
  1511. for (i = 0; i < sizeof(val); i++) {
  1512. c = ((char *)&val)[i];
  1513. if (!c)
  1514. break;
  1515. if (!isprint(c) || !isascii(c))
  1516. return false;
  1517. }
  1518. for (; i < sizeof(val); i++) {
  1519. c = ((char *)&val)[i];
  1520. if (c)
  1521. return false;
  1522. }
  1523. return true;
  1524. }
  1525. static int perf_sample__fprintf_synth_ptwrite(struct perf_sample *sample, FILE *fp)
  1526. {
  1527. struct perf_synth_intel_ptwrite *data = perf_sample__synth_ptr(sample);
  1528. char str[sizeof(u64) + 1] = "";
  1529. int len;
  1530. u64 val;
  1531. if (perf_sample__bad_synth_size(sample, *data))
  1532. return 0;
  1533. val = le64_to_cpu(data->payload);
  1534. if (ptw_is_prt(val)) {
  1535. memcpy(str, &val, sizeof(val));
  1536. str[sizeof(val)] = 0;
  1537. }
  1538. len = fprintf(fp, " IP: %u payload: %#" PRIx64 " %s ",
  1539. data->ip, val, str);
  1540. return len + perf_sample__fprintf_pt_spacing(len, fp);
  1541. }
  1542. static int perf_sample__fprintf_synth_mwait(struct perf_sample *sample, FILE *fp)
  1543. {
  1544. struct perf_synth_intel_mwait *data = perf_sample__synth_ptr(sample);
  1545. int len;
  1546. if (perf_sample__bad_synth_size(sample, *data))
  1547. return 0;
  1548. len = fprintf(fp, " hints: %#x extensions: %#x ",
  1549. data->hints, data->extensions);
  1550. return len + perf_sample__fprintf_pt_spacing(len, fp);
  1551. }
  1552. static int perf_sample__fprintf_synth_pwre(struct perf_sample *sample, FILE *fp)
  1553. {
  1554. struct perf_synth_intel_pwre *data = perf_sample__synth_ptr(sample);
  1555. int len;
  1556. if (perf_sample__bad_synth_size(sample, *data))
  1557. return 0;
  1558. len = fprintf(fp, " hw: %u cstate: %u sub-cstate: %u ",
  1559. data->hw, data->cstate, data->subcstate);
  1560. return len + perf_sample__fprintf_pt_spacing(len, fp);
  1561. }
  1562. static int perf_sample__fprintf_synth_exstop(struct perf_sample *sample, FILE *fp)
  1563. {
  1564. struct perf_synth_intel_exstop *data = perf_sample__synth_ptr(sample);
  1565. int len;
  1566. if (perf_sample__bad_synth_size(sample, *data))
  1567. return 0;
  1568. len = fprintf(fp, " IP: %u ", data->ip);
  1569. return len + perf_sample__fprintf_pt_spacing(len, fp);
  1570. }
  1571. static int perf_sample__fprintf_synth_pwrx(struct perf_sample *sample, FILE *fp)
  1572. {
  1573. struct perf_synth_intel_pwrx *data = perf_sample__synth_ptr(sample);
  1574. int len;
  1575. if (perf_sample__bad_synth_size(sample, *data))
  1576. return 0;
  1577. len = fprintf(fp, " deepest cstate: %u last cstate: %u wake reason: %#x ",
  1578. data->deepest_cstate, data->last_cstate,
  1579. data->wake_reason);
  1580. return len + perf_sample__fprintf_pt_spacing(len, fp);
  1581. }
  1582. static int perf_sample__fprintf_synth_cbr(struct perf_sample *sample, FILE *fp)
  1583. {
  1584. struct perf_synth_intel_cbr *data = perf_sample__synth_ptr(sample);
  1585. unsigned int percent, freq;
  1586. int len;
  1587. if (perf_sample__bad_synth_size(sample, *data))
  1588. return 0;
  1589. freq = (le32_to_cpu(data->freq) + 500) / 1000;
  1590. len = fprintf(fp, " cbr: %2u freq: %4u MHz ", data->cbr, freq);
  1591. if (data->max_nonturbo) {
  1592. percent = (5 + (1000 * data->cbr) / data->max_nonturbo) / 10;
  1593. len += fprintf(fp, "(%3u%%) ", percent);
  1594. }
  1595. return len + perf_sample__fprintf_pt_spacing(len, fp);
  1596. }
  1597. static int perf_sample__fprintf_synth_psb(struct perf_sample *sample, FILE *fp)
  1598. {
  1599. struct perf_synth_intel_psb *data = perf_sample__synth_ptr(sample);
  1600. int len;
  1601. if (perf_sample__bad_synth_size(sample, *data))
  1602. return 0;
  1603. len = fprintf(fp, " psb offs: %#" PRIx64, data->offset);
  1604. return len + perf_sample__fprintf_pt_spacing(len, fp);
  1605. }
  1606. /* Intel PT Event Trace */
  1607. static int perf_sample__fprintf_synth_evt(struct perf_sample *sample, FILE *fp)
  1608. {
  1609. struct perf_synth_intel_evt *data = perf_sample__synth_ptr(sample);
  1610. const char *cfe[32] = {NULL, "INTR", "IRET", "SMI", "RSM", "SIPI",
  1611. "INIT", "VMENTRY", "VMEXIT", "VMEXIT_INTR",
  1612. "SHUTDOWN"};
  1613. const char *evd[64] = {"PFA", "VMXQ", "VMXR"};
  1614. const char *s;
  1615. int len, i;
  1616. if (perf_sample__bad_synth_size(sample, *data))
  1617. return 0;
  1618. s = cfe[data->type];
  1619. if (s) {
  1620. len = fprintf(fp, " cfe: %s IP: %d vector: %u",
  1621. s, data->ip, data->vector);
  1622. } else {
  1623. len = fprintf(fp, " cfe: %u IP: %d vector: %u",
  1624. data->type, data->ip, data->vector);
  1625. }
  1626. for (i = 0; i < data->evd_cnt; i++) {
  1627. unsigned int et = data->evd[i].evd_type & 0x3f;
  1628. s = evd[et];
  1629. if (s) {
  1630. len += fprintf(fp, " %s: %#" PRIx64,
  1631. s, data->evd[i].payload);
  1632. } else {
  1633. len += fprintf(fp, " EVD_%u: %#" PRIx64,
  1634. et, data->evd[i].payload);
  1635. }
  1636. }
  1637. return len + perf_sample__fprintf_pt_spacing(len, fp);
  1638. }
  1639. static int perf_sample__fprintf_synth_iflag_chg(struct perf_sample *sample, FILE *fp)
  1640. {
  1641. struct perf_synth_intel_iflag_chg *data = perf_sample__synth_ptr(sample);
  1642. int len;
  1643. if (perf_sample__bad_synth_size(sample, *data))
  1644. return 0;
  1645. len = fprintf(fp, " IFLAG: %d->%d %s branch", !data->iflag, data->iflag,
  1646. data->via_branch ? "via" : "non");
  1647. return len + perf_sample__fprintf_pt_spacing(len, fp);
  1648. }
  1649. static int perf_sample__fprintf_synth(struct perf_sample *sample,
  1650. struct evsel *evsel, FILE *fp)
  1651. {
  1652. switch (evsel->core.attr.config) {
  1653. case PERF_SYNTH_INTEL_PTWRITE:
  1654. return perf_sample__fprintf_synth_ptwrite(sample, fp);
  1655. case PERF_SYNTH_INTEL_MWAIT:
  1656. return perf_sample__fprintf_synth_mwait(sample, fp);
  1657. case PERF_SYNTH_INTEL_PWRE:
  1658. return perf_sample__fprintf_synth_pwre(sample, fp);
  1659. case PERF_SYNTH_INTEL_EXSTOP:
  1660. return perf_sample__fprintf_synth_exstop(sample, fp);
  1661. case PERF_SYNTH_INTEL_PWRX:
  1662. return perf_sample__fprintf_synth_pwrx(sample, fp);
  1663. case PERF_SYNTH_INTEL_CBR:
  1664. return perf_sample__fprintf_synth_cbr(sample, fp);
  1665. case PERF_SYNTH_INTEL_PSB:
  1666. return perf_sample__fprintf_synth_psb(sample, fp);
  1667. case PERF_SYNTH_INTEL_EVT:
  1668. return perf_sample__fprintf_synth_evt(sample, fp);
  1669. case PERF_SYNTH_INTEL_IFLAG_CHG:
  1670. return perf_sample__fprintf_synth_iflag_chg(sample, fp);
  1671. default:
  1672. break;
  1673. }
  1674. return 0;
  1675. }
  1676. static int evlist__max_name_len(struct evlist *evlist)
  1677. {
  1678. struct evsel *evsel;
  1679. int max = 0;
  1680. evlist__for_each_entry(evlist, evsel) {
  1681. int len = strlen(evsel__name(evsel));
  1682. max = MAX(len, max);
  1683. }
  1684. return max;
  1685. }
  1686. static int data_src__fprintf(u64 data_src, FILE *fp)
  1687. {
  1688. struct mem_info mi = { .data_src.val = data_src };
  1689. char decode[100];
  1690. char out[100];
  1691. static int maxlen;
  1692. int len;
  1693. perf_script__meminfo_scnprintf(decode, 100, &mi);
  1694. len = scnprintf(out, 100, "%16" PRIx64 " %s", data_src, decode);
  1695. if (maxlen < len)
  1696. maxlen = len;
  1697. return fprintf(fp, "%-*s", maxlen, out);
  1698. }
  1699. struct metric_ctx {
  1700. struct perf_sample *sample;
  1701. struct thread *thread;
  1702. struct evsel *evsel;
  1703. FILE *fp;
  1704. };
  1705. static void script_print_metric(struct perf_stat_config *config __maybe_unused,
  1706. void *ctx, const char *color,
  1707. const char *fmt,
  1708. const char *unit, double val)
  1709. {
  1710. struct metric_ctx *mctx = ctx;
  1711. if (!fmt)
  1712. return;
  1713. perf_sample__fprintf_start(NULL, mctx->sample, mctx->thread, mctx->evsel,
  1714. PERF_RECORD_SAMPLE, mctx->fp);
  1715. fputs("\tmetric: ", mctx->fp);
  1716. if (color)
  1717. color_fprintf(mctx->fp, color, fmt, val);
  1718. else
  1719. printf(fmt, val);
  1720. fprintf(mctx->fp, " %s\n", unit);
  1721. }
  1722. static void script_new_line(struct perf_stat_config *config __maybe_unused,
  1723. void *ctx)
  1724. {
  1725. struct metric_ctx *mctx = ctx;
  1726. perf_sample__fprintf_start(NULL, mctx->sample, mctx->thread, mctx->evsel,
  1727. PERF_RECORD_SAMPLE, mctx->fp);
  1728. fputs("\tmetric: ", mctx->fp);
  1729. }
  1730. static void perf_sample__fprint_metric(struct perf_script *script,
  1731. struct thread *thread,
  1732. struct evsel *evsel,
  1733. struct perf_sample *sample,
  1734. FILE *fp)
  1735. {
  1736. struct evsel *leader = evsel__leader(evsel);
  1737. struct perf_stat_output_ctx ctx = {
  1738. .print_metric = script_print_metric,
  1739. .new_line = script_new_line,
  1740. .ctx = &(struct metric_ctx) {
  1741. .sample = sample,
  1742. .thread = thread,
  1743. .evsel = evsel,
  1744. .fp = fp,
  1745. },
  1746. .force_header = false,
  1747. };
  1748. struct evsel *ev2;
  1749. u64 val;
  1750. if (!evsel->stats)
  1751. evlist__alloc_stats(script->session->evlist, false);
  1752. if (evsel_script(leader)->gnum++ == 0)
  1753. perf_stat__reset_shadow_stats();
  1754. val = sample->period * evsel->scale;
  1755. perf_stat__update_shadow_stats(evsel,
  1756. val,
  1757. sample->cpu,
  1758. &rt_stat);
  1759. evsel_script(evsel)->val = val;
  1760. if (evsel_script(leader)->gnum == leader->core.nr_members) {
  1761. for_each_group_member (ev2, leader) {
  1762. perf_stat__print_shadow_stats(&stat_config, ev2,
  1763. evsel_script(ev2)->val,
  1764. sample->cpu,
  1765. &ctx,
  1766. NULL,
  1767. &rt_stat);
  1768. }
  1769. evsel_script(leader)->gnum = 0;
  1770. }
  1771. }
  1772. static bool show_event(struct perf_sample *sample,
  1773. struct evsel *evsel,
  1774. struct thread *thread,
  1775. struct addr_location *al,
  1776. struct addr_location *addr_al)
  1777. {
  1778. int depth = thread_stack__depth(thread, sample->cpu);
  1779. if (!symbol_conf.graph_function)
  1780. return true;
  1781. if (thread->filter) {
  1782. if (depth <= thread->filter_entry_depth) {
  1783. thread->filter = false;
  1784. return false;
  1785. }
  1786. return true;
  1787. } else {
  1788. const char *s = symbol_conf.graph_function;
  1789. u64 ip;
  1790. const char *name = resolve_branch_sym(sample, evsel, thread, al, addr_al,
  1791. &ip);
  1792. unsigned nlen;
  1793. if (!name)
  1794. return false;
  1795. nlen = strlen(name);
  1796. while (*s) {
  1797. unsigned len = strcspn(s, ",");
  1798. if (nlen == len && !strncmp(name, s, len)) {
  1799. thread->filter = true;
  1800. thread->filter_entry_depth = depth;
  1801. return true;
  1802. }
  1803. s += len;
  1804. if (*s == ',')
  1805. s++;
  1806. }
  1807. return false;
  1808. }
  1809. }
  1810. static void process_event(struct perf_script *script,
  1811. struct perf_sample *sample, struct evsel *evsel,
  1812. struct addr_location *al,
  1813. struct addr_location *addr_al,
  1814. struct machine *machine)
  1815. {
  1816. struct thread *thread = al->thread;
  1817. struct perf_event_attr *attr = &evsel->core.attr;
  1818. unsigned int type = output_type(attr->type);
  1819. struct evsel_script *es = evsel->priv;
  1820. FILE *fp = es->fp;
  1821. char str[PAGE_SIZE_NAME_LEN];
  1822. const char *arch = perf_env__arch(machine->env);
  1823. if (output[type].fields == 0)
  1824. return;
  1825. ++es->samples;
  1826. perf_sample__fprintf_start(script, sample, thread, evsel,
  1827. PERF_RECORD_SAMPLE, fp);
  1828. if (PRINT_FIELD(PERIOD))
  1829. fprintf(fp, "%10" PRIu64 " ", sample->period);
  1830. if (PRINT_FIELD(EVNAME)) {
  1831. const char *evname = evsel__name(evsel);
  1832. if (!script->name_width)
  1833. script->name_width = evlist__max_name_len(script->session->evlist);
  1834. fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
  1835. }
  1836. if (print_flags)
  1837. perf_sample__fprintf_flags(sample->flags, fp);
  1838. if (is_bts_event(attr)) {
  1839. perf_sample__fprintf_bts(sample, evsel, thread, al, addr_al, machine, fp);
  1840. return;
  1841. }
  1842. if (PRINT_FIELD(TRACE) && sample->raw_data) {
  1843. event_format__fprintf(evsel->tp_format, sample->cpu,
  1844. sample->raw_data, sample->raw_size, fp);
  1845. }
  1846. if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
  1847. perf_sample__fprintf_synth(sample, evsel, fp);
  1848. if (PRINT_FIELD(ADDR))
  1849. perf_sample__fprintf_addr(sample, thread, attr, fp);
  1850. if (PRINT_FIELD(DATA_SRC))
  1851. data_src__fprintf(sample->data_src, fp);
  1852. if (PRINT_FIELD(WEIGHT))
  1853. fprintf(fp, "%16" PRIu64, sample->weight);
  1854. if (PRINT_FIELD(INS_LAT))
  1855. fprintf(fp, "%16" PRIu16, sample->ins_lat);
  1856. if (PRINT_FIELD(IP)) {
  1857. struct callchain_cursor *cursor = NULL;
  1858. if (script->stitch_lbr)
  1859. al->thread->lbr_stitch_enable = true;
  1860. if (symbol_conf.use_callchain && sample->callchain &&
  1861. thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
  1862. sample, NULL, NULL, scripting_max_stack) == 0)
  1863. cursor = &callchain_cursor;
  1864. fputc(cursor ? '\n' : ' ', fp);
  1865. sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor,
  1866. symbol_conf.bt_stop_list, fp);
  1867. }
  1868. if (PRINT_FIELD(IREGS))
  1869. perf_sample__fprintf_iregs(sample, attr, arch, fp);
  1870. if (PRINT_FIELD(UREGS))
  1871. perf_sample__fprintf_uregs(sample, attr, arch, fp);
  1872. if (PRINT_FIELD(BRSTACK))
  1873. perf_sample__fprintf_brstack(sample, thread, attr, fp);
  1874. else if (PRINT_FIELD(BRSTACKSYM))
  1875. perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
  1876. else if (PRINT_FIELD(BRSTACKOFF))
  1877. perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
  1878. if (evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
  1879. perf_sample__fprintf_bpf_output(sample, fp);
  1880. perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
  1881. if (PRINT_FIELD(PHYS_ADDR))
  1882. fprintf(fp, "%16" PRIx64, sample->phys_addr);
  1883. if (PRINT_FIELD(DATA_PAGE_SIZE))
  1884. fprintf(fp, " %s", get_page_size_name(sample->data_page_size, str));
  1885. if (PRINT_FIELD(CODE_PAGE_SIZE))
  1886. fprintf(fp, " %s", get_page_size_name(sample->code_page_size, str));
  1887. perf_sample__fprintf_ipc(sample, attr, fp);
  1888. fprintf(fp, "\n");
  1889. if (PRINT_FIELD(SRCCODE)) {
  1890. if (map__fprintf_srccode(al->map, al->addr, stdout,
  1891. &thread->srccode_state))
  1892. printf("\n");
  1893. }
  1894. if (PRINT_FIELD(METRIC))
  1895. perf_sample__fprint_metric(script, thread, evsel, sample, fp);
  1896. if (verbose)
  1897. fflush(fp);
  1898. }
  1899. static struct scripting_ops *scripting_ops;
  1900. static void __process_stat(struct evsel *counter, u64 tstamp)
  1901. {
  1902. int nthreads = perf_thread_map__nr(counter->core.threads);
  1903. int idx, thread;
  1904. struct perf_cpu cpu;
  1905. static int header_printed;
  1906. if (!header_printed) {
  1907. printf("%3s %8s %15s %15s %15s %15s %s\n",
  1908. "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
  1909. header_printed = 1;
  1910. }
  1911. for (thread = 0; thread < nthreads; thread++) {
  1912. perf_cpu_map__for_each_cpu(cpu, idx, evsel__cpus(counter)) {
  1913. struct perf_counts_values *counts;
  1914. counts = perf_counts(counter->counts, idx, thread);
  1915. printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
  1916. cpu.cpu,
  1917. perf_thread_map__pid(counter->core.threads, thread),
  1918. counts->val,
  1919. counts->ena,
  1920. counts->run,
  1921. tstamp,
  1922. evsel__name(counter));
  1923. }
  1924. }
  1925. }
  1926. static void process_stat(struct evsel *counter, u64 tstamp)
  1927. {
  1928. if (scripting_ops && scripting_ops->process_stat)
  1929. scripting_ops->process_stat(&stat_config, counter, tstamp);
  1930. else
  1931. __process_stat(counter, tstamp);
  1932. }
  1933. static void process_stat_interval(u64 tstamp)
  1934. {
  1935. if (scripting_ops && scripting_ops->process_stat_interval)
  1936. scripting_ops->process_stat_interval(tstamp);
  1937. }
  1938. static void setup_scripting(void)
  1939. {
  1940. setup_perl_scripting();
  1941. setup_python_scripting();
  1942. }
  1943. static int flush_scripting(void)
  1944. {
  1945. return scripting_ops ? scripting_ops->flush_script() : 0;
  1946. }
  1947. static int cleanup_scripting(void)
  1948. {
  1949. pr_debug("\nperf script stopped\n");
  1950. return scripting_ops ? scripting_ops->stop_script() : 0;
  1951. }
  1952. static bool filter_cpu(struct perf_sample *sample)
  1953. {
  1954. if (cpu_list && sample->cpu != (u32)-1)
  1955. return !test_bit(sample->cpu, cpu_bitmap);
  1956. return false;
  1957. }
  1958. static int process_sample_event(struct perf_tool *tool,
  1959. union perf_event *event,
  1960. struct perf_sample *sample,
  1961. struct evsel *evsel,
  1962. struct machine *machine)
  1963. {
  1964. struct perf_script *scr = container_of(tool, struct perf_script, tool);
  1965. struct addr_location al;
  1966. struct addr_location addr_al;
  1967. int ret = 0;
  1968. /* Set thread to NULL to indicate addr_al and al are not initialized */
  1969. addr_al.thread = NULL;
  1970. al.thread = NULL;
  1971. ret = dlfilter__filter_event_early(dlfilter, event, sample, evsel, machine, &al, &addr_al);
  1972. if (ret) {
  1973. if (ret > 0)
  1974. ret = 0;
  1975. goto out_put;
  1976. }
  1977. if (perf_time__ranges_skip_sample(scr->ptime_range, scr->range_num,
  1978. sample->time)) {
  1979. goto out_put;
  1980. }
  1981. if (debug_mode) {
  1982. if (sample->time < last_timestamp) {
  1983. pr_err("Samples misordered, previous: %" PRIu64
  1984. " this: %" PRIu64 "\n", last_timestamp,
  1985. sample->time);
  1986. nr_unordered++;
  1987. }
  1988. last_timestamp = sample->time;
  1989. goto out_put;
  1990. }
  1991. if (filter_cpu(sample))
  1992. goto out_put;
  1993. if (!al.thread && machine__resolve(machine, &al, sample) < 0) {
  1994. pr_err("problem processing %d event, skipping it.\n",
  1995. event->header.type);
  1996. ret = -1;
  1997. goto out_put;
  1998. }
  1999. if (al.filtered)
  2000. goto out_put;
  2001. if (!show_event(sample, evsel, al.thread, &al, &addr_al))
  2002. goto out_put;
  2003. if (evswitch__discard(&scr->evswitch, evsel))
  2004. goto out_put;
  2005. ret = dlfilter__filter_event(dlfilter, event, sample, evsel, machine, &al, &addr_al);
  2006. if (ret) {
  2007. if (ret > 0)
  2008. ret = 0;
  2009. goto out_put;
  2010. }
  2011. if (scripting_ops) {
  2012. struct addr_location *addr_al_ptr = NULL;
  2013. if ((evsel->core.attr.sample_type & PERF_SAMPLE_ADDR) &&
  2014. sample_addr_correlates_sym(&evsel->core.attr)) {
  2015. if (!addr_al.thread)
  2016. thread__resolve(al.thread, &addr_al, sample);
  2017. addr_al_ptr = &addr_al;
  2018. }
  2019. scripting_ops->process_event(event, sample, evsel, &al, addr_al_ptr);
  2020. } else {
  2021. process_event(scr, sample, evsel, &al, &addr_al, machine);
  2022. }
  2023. out_put:
  2024. if (al.thread)
  2025. addr_location__put(&al);
  2026. return ret;
  2027. }
  2028. // Used when scr->per_event_dump is not set
  2029. static struct evsel_script es_stdout;
  2030. static int process_attr(struct perf_tool *tool, union perf_event *event,
  2031. struct evlist **pevlist)
  2032. {
  2033. struct perf_script *scr = container_of(tool, struct perf_script, tool);
  2034. struct evlist *evlist;
  2035. struct evsel *evsel, *pos;
  2036. u64 sample_type;
  2037. int err;
  2038. err = perf_event__process_attr(tool, event, pevlist);
  2039. if (err)
  2040. return err;
  2041. evlist = *pevlist;
  2042. evsel = evlist__last(*pevlist);
  2043. if (!evsel->priv) {
  2044. if (scr->per_event_dump) {
  2045. evsel->priv = evsel_script__new(evsel, scr->session->data);
  2046. if (!evsel->priv)
  2047. return -ENOMEM;
  2048. } else { // Replicate what is done in perf_script__setup_per_event_dump()
  2049. es_stdout.fp = stdout;
  2050. evsel->priv = &es_stdout;
  2051. }
  2052. }
  2053. if (evsel->core.attr.type >= PERF_TYPE_MAX &&
  2054. evsel->core.attr.type != PERF_TYPE_SYNTH)
  2055. return 0;
  2056. evlist__for_each_entry(evlist, pos) {
  2057. if (pos->core.attr.type == evsel->core.attr.type && pos != evsel)
  2058. return 0;
  2059. }
  2060. if (evsel->core.attr.sample_type) {
  2061. err = evsel__check_attr(evsel, scr->session);
  2062. if (err)
  2063. return err;
  2064. }
  2065. /*
  2066. * Check if we need to enable callchains based
  2067. * on events sample_type.
  2068. */
  2069. sample_type = evlist__combined_sample_type(evlist);
  2070. callchain_param_setup(sample_type, perf_env__arch((*pevlist)->env));
  2071. /* Enable fields for callchain entries */
  2072. if (symbol_conf.use_callchain &&
  2073. (sample_type & PERF_SAMPLE_CALLCHAIN ||
  2074. sample_type & PERF_SAMPLE_BRANCH_STACK ||
  2075. (sample_type & PERF_SAMPLE_REGS_USER &&
  2076. sample_type & PERF_SAMPLE_STACK_USER))) {
  2077. int type = output_type(evsel->core.attr.type);
  2078. if (!(output[type].user_unset_fields & PERF_OUTPUT_IP))
  2079. output[type].fields |= PERF_OUTPUT_IP;
  2080. if (!(output[type].user_unset_fields & PERF_OUTPUT_SYM))
  2081. output[type].fields |= PERF_OUTPUT_SYM;
  2082. }
  2083. set_print_ip_opts(&evsel->core.attr);
  2084. return 0;
  2085. }
  2086. static int print_event_with_time(struct perf_tool *tool,
  2087. union perf_event *event,
  2088. struct perf_sample *sample,
  2089. struct machine *machine,
  2090. pid_t pid, pid_t tid, u64 timestamp)
  2091. {
  2092. struct perf_script *script = container_of(tool, struct perf_script, tool);
  2093. struct perf_session *session = script->session;
  2094. struct evsel *evsel = evlist__id2evsel(session->evlist, sample->id);
  2095. struct thread *thread = NULL;
  2096. if (evsel && !evsel->core.attr.sample_id_all) {
  2097. sample->cpu = 0;
  2098. sample->time = timestamp;
  2099. sample->pid = pid;
  2100. sample->tid = tid;
  2101. }
  2102. if (filter_cpu(sample))
  2103. return 0;
  2104. if (tid != -1)
  2105. thread = machine__findnew_thread(machine, pid, tid);
  2106. if (evsel) {
  2107. perf_sample__fprintf_start(script, sample, thread, evsel,
  2108. event->header.type, stdout);
  2109. }
  2110. perf_event__fprintf(event, machine, stdout);
  2111. thread__put(thread);
  2112. return 0;
  2113. }
  2114. static int print_event(struct perf_tool *tool, union perf_event *event,
  2115. struct perf_sample *sample, struct machine *machine,
  2116. pid_t pid, pid_t tid)
  2117. {
  2118. return print_event_with_time(tool, event, sample, machine, pid, tid, 0);
  2119. }
  2120. static int process_comm_event(struct perf_tool *tool,
  2121. union perf_event *event,
  2122. struct perf_sample *sample,
  2123. struct machine *machine)
  2124. {
  2125. if (perf_event__process_comm(tool, event, sample, machine) < 0)
  2126. return -1;
  2127. return print_event(tool, event, sample, machine, event->comm.pid,
  2128. event->comm.tid);
  2129. }
  2130. static int process_namespaces_event(struct perf_tool *tool,
  2131. union perf_event *event,
  2132. struct perf_sample *sample,
  2133. struct machine *machine)
  2134. {
  2135. if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
  2136. return -1;
  2137. return print_event(tool, event, sample, machine, event->namespaces.pid,
  2138. event->namespaces.tid);
  2139. }
  2140. static int process_cgroup_event(struct perf_tool *tool,
  2141. union perf_event *event,
  2142. struct perf_sample *sample,
  2143. struct machine *machine)
  2144. {
  2145. if (perf_event__process_cgroup(tool, event, sample, machine) < 0)
  2146. return -1;
  2147. return print_event(tool, event, sample, machine, sample->pid,
  2148. sample->tid);
  2149. }
  2150. static int process_fork_event(struct perf_tool *tool,
  2151. union perf_event *event,
  2152. struct perf_sample *sample,
  2153. struct machine *machine)
  2154. {
  2155. if (perf_event__process_fork(tool, event, sample, machine) < 0)
  2156. return -1;
  2157. return print_event_with_time(tool, event, sample, machine,
  2158. event->fork.pid, event->fork.tid,
  2159. event->fork.time);
  2160. }
  2161. static int process_exit_event(struct perf_tool *tool,
  2162. union perf_event *event,
  2163. struct perf_sample *sample,
  2164. struct machine *machine)
  2165. {
  2166. /* Print before 'exit' deletes anything */
  2167. if (print_event_with_time(tool, event, sample, machine, event->fork.pid,
  2168. event->fork.tid, event->fork.time))
  2169. return -1;
  2170. return perf_event__process_exit(tool, event, sample, machine);
  2171. }
  2172. static int process_mmap_event(struct perf_tool *tool,
  2173. union perf_event *event,
  2174. struct perf_sample *sample,
  2175. struct machine *machine)
  2176. {
  2177. if (perf_event__process_mmap(tool, event, sample, machine) < 0)
  2178. return -1;
  2179. return print_event(tool, event, sample, machine, event->mmap.pid,
  2180. event->mmap.tid);
  2181. }
  2182. static int process_mmap2_event(struct perf_tool *tool,
  2183. union perf_event *event,
  2184. struct perf_sample *sample,
  2185. struct machine *machine)
  2186. {
  2187. if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
  2188. return -1;
  2189. return print_event(tool, event, sample, machine, event->mmap2.pid,
  2190. event->mmap2.tid);
  2191. }
  2192. static int process_switch_event(struct perf_tool *tool,
  2193. union perf_event *event,
  2194. struct perf_sample *sample,
  2195. struct machine *machine)
  2196. {
  2197. struct perf_script *script = container_of(tool, struct perf_script, tool);
  2198. if (perf_event__process_switch(tool, event, sample, machine) < 0)
  2199. return -1;
  2200. if (scripting_ops && scripting_ops->process_switch && !filter_cpu(sample))
  2201. scripting_ops->process_switch(event, sample, machine);
  2202. if (!script->show_switch_events)
  2203. return 0;
  2204. return print_event(tool, event, sample, machine, sample->pid,
  2205. sample->tid);
  2206. }
  2207. static int process_auxtrace_error(struct perf_session *session,
  2208. union perf_event *event)
  2209. {
  2210. if (scripting_ops && scripting_ops->process_auxtrace_error) {
  2211. scripting_ops->process_auxtrace_error(session, event);
  2212. return 0;
  2213. }
  2214. return perf_event__process_auxtrace_error(session, event);
  2215. }
  2216. static int
  2217. process_lost_event(struct perf_tool *tool,
  2218. union perf_event *event,
  2219. struct perf_sample *sample,
  2220. struct machine *machine)
  2221. {
  2222. return print_event(tool, event, sample, machine, sample->pid,
  2223. sample->tid);
  2224. }
  2225. static int
  2226. process_throttle_event(struct perf_tool *tool __maybe_unused,
  2227. union perf_event *event,
  2228. struct perf_sample *sample,
  2229. struct machine *machine)
  2230. {
  2231. if (scripting_ops && scripting_ops->process_throttle)
  2232. scripting_ops->process_throttle(event, sample, machine);
  2233. return 0;
  2234. }
  2235. static int
  2236. process_finished_round_event(struct perf_tool *tool __maybe_unused,
  2237. union perf_event *event,
  2238. struct ordered_events *oe __maybe_unused)
  2239. {
  2240. perf_event__fprintf(event, NULL, stdout);
  2241. return 0;
  2242. }
  2243. static int
  2244. process_bpf_events(struct perf_tool *tool __maybe_unused,
  2245. union perf_event *event,
  2246. struct perf_sample *sample,
  2247. struct machine *machine)
  2248. {
  2249. if (machine__process_ksymbol(machine, event, sample) < 0)
  2250. return -1;
  2251. return print_event(tool, event, sample, machine, sample->pid,
  2252. sample->tid);
  2253. }
  2254. static int process_text_poke_events(struct perf_tool *tool,
  2255. union perf_event *event,
  2256. struct perf_sample *sample,
  2257. struct machine *machine)
  2258. {
  2259. if (perf_event__process_text_poke(tool, event, sample, machine) < 0)
  2260. return -1;
  2261. return print_event(tool, event, sample, machine, sample->pid,
  2262. sample->tid);
  2263. }
  2264. static void sig_handler(int sig __maybe_unused)
  2265. {
  2266. session_done = 1;
  2267. }
  2268. static void perf_script__fclose_per_event_dump(struct perf_script *script)
  2269. {
  2270. struct evlist *evlist = script->session->evlist;
  2271. struct evsel *evsel;
  2272. evlist__for_each_entry(evlist, evsel) {
  2273. if (!evsel->priv)
  2274. break;
  2275. evsel_script__delete(evsel->priv);
  2276. evsel->priv = NULL;
  2277. }
  2278. }
  2279. static int perf_script__fopen_per_event_dump(struct perf_script *script)
  2280. {
  2281. struct evsel *evsel;
  2282. evlist__for_each_entry(script->session->evlist, evsel) {
  2283. /*
  2284. * Already setup? I.e. we may be called twice in cases like
  2285. * Intel PT, one for the intel_pt// and dummy events, then
  2286. * for the evsels synthesized from the auxtrace info.
  2287. *
  2288. * Ses perf_script__process_auxtrace_info.
  2289. */
  2290. if (evsel->priv != NULL)
  2291. continue;
  2292. evsel->priv = evsel_script__new(evsel, script->session->data);
  2293. if (evsel->priv == NULL)
  2294. goto out_err_fclose;
  2295. }
  2296. return 0;
  2297. out_err_fclose:
  2298. perf_script__fclose_per_event_dump(script);
  2299. return -1;
  2300. }
  2301. static int perf_script__setup_per_event_dump(struct perf_script *script)
  2302. {
  2303. struct evsel *evsel;
  2304. if (script->per_event_dump)
  2305. return perf_script__fopen_per_event_dump(script);
  2306. es_stdout.fp = stdout;
  2307. evlist__for_each_entry(script->session->evlist, evsel)
  2308. evsel->priv = &es_stdout;
  2309. return 0;
  2310. }
  2311. static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
  2312. {
  2313. struct evsel *evsel;
  2314. evlist__for_each_entry(script->session->evlist, evsel) {
  2315. struct evsel_script *es = evsel->priv;
  2316. evsel_script__fprintf(es, stdout);
  2317. evsel_script__delete(es);
  2318. evsel->priv = NULL;
  2319. }
  2320. }
  2321. static void perf_script__exit(struct perf_script *script)
  2322. {
  2323. perf_thread_map__put(script->threads);
  2324. perf_cpu_map__put(script->cpus);
  2325. }
  2326. static int __cmd_script(struct perf_script *script)
  2327. {
  2328. int ret;
  2329. signal(SIGINT, sig_handler);
  2330. perf_stat__init_shadow_stats();
  2331. /* override event processing functions */
  2332. if (script->show_task_events) {
  2333. script->tool.comm = process_comm_event;
  2334. script->tool.fork = process_fork_event;
  2335. script->tool.exit = process_exit_event;
  2336. }
  2337. if (script->show_mmap_events) {
  2338. script->tool.mmap = process_mmap_event;
  2339. script->tool.mmap2 = process_mmap2_event;
  2340. }
  2341. if (script->show_switch_events || (scripting_ops && scripting_ops->process_switch))
  2342. script->tool.context_switch = process_switch_event;
  2343. if (scripting_ops && scripting_ops->process_auxtrace_error)
  2344. script->tool.auxtrace_error = process_auxtrace_error;
  2345. if (script->show_namespace_events)
  2346. script->tool.namespaces = process_namespaces_event;
  2347. if (script->show_cgroup_events)
  2348. script->tool.cgroup = process_cgroup_event;
  2349. if (script->show_lost_events)
  2350. script->tool.lost = process_lost_event;
  2351. if (script->show_round_events) {
  2352. script->tool.ordered_events = false;
  2353. script->tool.finished_round = process_finished_round_event;
  2354. }
  2355. if (script->show_bpf_events) {
  2356. script->tool.ksymbol = process_bpf_events;
  2357. script->tool.bpf = process_bpf_events;
  2358. }
  2359. if (script->show_text_poke_events) {
  2360. script->tool.ksymbol = process_bpf_events;
  2361. script->tool.text_poke = process_text_poke_events;
  2362. }
  2363. if (perf_script__setup_per_event_dump(script)) {
  2364. pr_err("Couldn't create the per event dump files\n");
  2365. return -1;
  2366. }
  2367. ret = perf_session__process_events(script->session);
  2368. if (script->per_event_dump)
  2369. perf_script__exit_per_event_dump_stats(script);
  2370. if (debug_mode)
  2371. pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
  2372. return ret;
  2373. }
  2374. struct script_spec {
  2375. struct list_head node;
  2376. struct scripting_ops *ops;
  2377. char spec[];
  2378. };
  2379. static LIST_HEAD(script_specs);
  2380. static struct script_spec *script_spec__new(const char *spec,
  2381. struct scripting_ops *ops)
  2382. {
  2383. struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
  2384. if (s != NULL) {
  2385. strcpy(s->spec, spec);
  2386. s->ops = ops;
  2387. }
  2388. return s;
  2389. }
  2390. static void script_spec__add(struct script_spec *s)
  2391. {
  2392. list_add_tail(&s->node, &script_specs);
  2393. }
  2394. static struct script_spec *script_spec__find(const char *spec)
  2395. {
  2396. struct script_spec *s;
  2397. list_for_each_entry(s, &script_specs, node)
  2398. if (strcasecmp(s->spec, spec) == 0)
  2399. return s;
  2400. return NULL;
  2401. }
  2402. int script_spec_register(const char *spec, struct scripting_ops *ops)
  2403. {
  2404. struct script_spec *s;
  2405. s = script_spec__find(spec);
  2406. if (s)
  2407. return -1;
  2408. s = script_spec__new(spec, ops);
  2409. if (!s)
  2410. return -1;
  2411. else
  2412. script_spec__add(s);
  2413. return 0;
  2414. }
  2415. static struct scripting_ops *script_spec__lookup(const char *spec)
  2416. {
  2417. struct script_spec *s = script_spec__find(spec);
  2418. if (!s)
  2419. return NULL;
  2420. return s->ops;
  2421. }
  2422. static void list_available_languages(void)
  2423. {
  2424. struct script_spec *s;
  2425. fprintf(stderr, "\n");
  2426. fprintf(stderr, "Scripting language extensions (used in "
  2427. "perf script -s [spec:]script.[spec]):\n\n");
  2428. list_for_each_entry(s, &script_specs, node)
  2429. fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
  2430. fprintf(stderr, "\n");
  2431. }
  2432. /* Find script file relative to current directory or exec path */
  2433. static char *find_script(const char *script)
  2434. {
  2435. char path[PATH_MAX];
  2436. if (!scripting_ops) {
  2437. const char *ext = strrchr(script, '.');
  2438. if (!ext)
  2439. return NULL;
  2440. scripting_ops = script_spec__lookup(++ext);
  2441. if (!scripting_ops)
  2442. return NULL;
  2443. }
  2444. if (access(script, R_OK)) {
  2445. char *exec_path = get_argv_exec_path();
  2446. if (!exec_path)
  2447. return NULL;
  2448. snprintf(path, sizeof(path), "%s/scripts/%s/%s",
  2449. exec_path, scripting_ops->dirname, script);
  2450. free(exec_path);
  2451. script = path;
  2452. if (access(script, R_OK))
  2453. return NULL;
  2454. }
  2455. return strdup(script);
  2456. }
  2457. static int parse_scriptname(const struct option *opt __maybe_unused,
  2458. const char *str, int unset __maybe_unused)
  2459. {
  2460. char spec[PATH_MAX];
  2461. const char *script, *ext;
  2462. int len;
  2463. if (strcmp(str, "lang") == 0) {
  2464. list_available_languages();
  2465. exit(0);
  2466. }
  2467. script = strchr(str, ':');
  2468. if (script) {
  2469. len = script - str;
  2470. if (len >= PATH_MAX) {
  2471. fprintf(stderr, "invalid language specifier");
  2472. return -1;
  2473. }
  2474. strncpy(spec, str, len);
  2475. spec[len] = '\0';
  2476. scripting_ops = script_spec__lookup(spec);
  2477. if (!scripting_ops) {
  2478. fprintf(stderr, "invalid language specifier");
  2479. return -1;
  2480. }
  2481. script++;
  2482. } else {
  2483. script = str;
  2484. ext = strrchr(script, '.');
  2485. if (!ext) {
  2486. fprintf(stderr, "invalid script extension");
  2487. return -1;
  2488. }
  2489. scripting_ops = script_spec__lookup(++ext);
  2490. if (!scripting_ops) {
  2491. fprintf(stderr, "invalid script extension");
  2492. return -1;
  2493. }
  2494. }
  2495. script_name = find_script(script);
  2496. if (!script_name)
  2497. script_name = strdup(script);
  2498. return 0;
  2499. }
  2500. static int parse_output_fields(const struct option *opt __maybe_unused,
  2501. const char *arg, int unset __maybe_unused)
  2502. {
  2503. char *tok, *strtok_saveptr = NULL;
  2504. int i, imax = ARRAY_SIZE(all_output_options);
  2505. int j;
  2506. int rc = 0;
  2507. char *str = strdup(arg);
  2508. int type = -1;
  2509. enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
  2510. if (!str)
  2511. return -ENOMEM;
  2512. /* first word can state for which event type the user is specifying
  2513. * the fields. If no type exists, the specified fields apply to all
  2514. * event types found in the file minus the invalid fields for a type.
  2515. */
  2516. tok = strchr(str, ':');
  2517. if (tok) {
  2518. *tok = '\0';
  2519. tok++;
  2520. if (!strcmp(str, "hw"))
  2521. type = PERF_TYPE_HARDWARE;
  2522. else if (!strcmp(str, "sw"))
  2523. type = PERF_TYPE_SOFTWARE;
  2524. else if (!strcmp(str, "trace"))
  2525. type = PERF_TYPE_TRACEPOINT;
  2526. else if (!strcmp(str, "raw"))
  2527. type = PERF_TYPE_RAW;
  2528. else if (!strcmp(str, "break"))
  2529. type = PERF_TYPE_BREAKPOINT;
  2530. else if (!strcmp(str, "synth"))
  2531. type = OUTPUT_TYPE_SYNTH;
  2532. else {
  2533. fprintf(stderr, "Invalid event type in field string.\n");
  2534. rc = -EINVAL;
  2535. goto out;
  2536. }
  2537. if (output[type].user_set)
  2538. pr_warning("Overriding previous field request for %s events.\n",
  2539. event_type(type));
  2540. /* Don't override defaults for +- */
  2541. if (strchr(tok, '+') || strchr(tok, '-'))
  2542. goto parse;
  2543. output[type].fields = 0;
  2544. output[type].user_set = true;
  2545. output[type].wildcard_set = false;
  2546. } else {
  2547. tok = str;
  2548. if (strlen(str) == 0) {
  2549. fprintf(stderr,
  2550. "Cannot set fields to 'none' for all event types.\n");
  2551. rc = -EINVAL;
  2552. goto out;
  2553. }
  2554. /* Don't override defaults for +- */
  2555. if (strchr(str, '+') || strchr(str, '-'))
  2556. goto parse;
  2557. if (output_set_by_user())
  2558. pr_warning("Overriding previous field request for all events.\n");
  2559. for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
  2560. output[j].fields = 0;
  2561. output[j].user_set = true;
  2562. output[j].wildcard_set = true;
  2563. }
  2564. }
  2565. parse:
  2566. for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
  2567. if (*tok == '+') {
  2568. if (change == SET)
  2569. goto out_badmix;
  2570. change = ADD;
  2571. tok++;
  2572. } else if (*tok == '-') {
  2573. if (change == SET)
  2574. goto out_badmix;
  2575. change = REMOVE;
  2576. tok++;
  2577. } else {
  2578. if (change != SET && change != DEFAULT)
  2579. goto out_badmix;
  2580. change = SET;
  2581. }
  2582. for (i = 0; i < imax; ++i) {
  2583. if (strcmp(tok, all_output_options[i].str) == 0)
  2584. break;
  2585. }
  2586. if (i == imax && strcmp(tok, "flags") == 0) {
  2587. print_flags = change != REMOVE;
  2588. continue;
  2589. }
  2590. if (i == imax) {
  2591. fprintf(stderr, "Invalid field requested.\n");
  2592. rc = -EINVAL;
  2593. goto out;
  2594. }
  2595. if (type == -1) {
  2596. /* add user option to all events types for
  2597. * which it is valid
  2598. */
  2599. for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
  2600. if (output[j].invalid_fields & all_output_options[i].field) {
  2601. pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
  2602. all_output_options[i].str, event_type(j));
  2603. } else {
  2604. if (change == REMOVE) {
  2605. output[j].fields &= ~all_output_options[i].field;
  2606. output[j].user_set_fields &= ~all_output_options[i].field;
  2607. output[j].user_unset_fields |= all_output_options[i].field;
  2608. } else {
  2609. output[j].fields |= all_output_options[i].field;
  2610. output[j].user_set_fields |= all_output_options[i].field;
  2611. output[j].user_unset_fields &= ~all_output_options[i].field;
  2612. }
  2613. output[j].user_set = true;
  2614. output[j].wildcard_set = true;
  2615. }
  2616. }
  2617. } else {
  2618. if (output[type].invalid_fields & all_output_options[i].field) {
  2619. fprintf(stderr, "\'%s\' not valid for %s events.\n",
  2620. all_output_options[i].str, event_type(type));
  2621. rc = -EINVAL;
  2622. goto out;
  2623. }
  2624. if (change == REMOVE)
  2625. output[type].fields &= ~all_output_options[i].field;
  2626. else
  2627. output[type].fields |= all_output_options[i].field;
  2628. output[type].user_set = true;
  2629. output[type].wildcard_set = true;
  2630. }
  2631. }
  2632. if (type >= 0) {
  2633. if (output[type].fields == 0) {
  2634. pr_debug("No fields requested for %s type. "
  2635. "Events will not be displayed.\n", event_type(type));
  2636. }
  2637. }
  2638. goto out;
  2639. out_badmix:
  2640. fprintf(stderr, "Cannot mix +-field with overridden fields\n");
  2641. rc = -EINVAL;
  2642. out:
  2643. free(str);
  2644. return rc;
  2645. }
  2646. #define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
  2647. while ((lang_dirent = readdir(scripts_dir)) != NULL) \
  2648. if ((lang_dirent->d_type == DT_DIR || \
  2649. (lang_dirent->d_type == DT_UNKNOWN && \
  2650. is_directory(scripts_path, lang_dirent))) && \
  2651. (strcmp(lang_dirent->d_name, ".")) && \
  2652. (strcmp(lang_dirent->d_name, "..")))
  2653. #define for_each_script(lang_path, lang_dir, script_dirent) \
  2654. while ((script_dirent = readdir(lang_dir)) != NULL) \
  2655. if (script_dirent->d_type != DT_DIR && \
  2656. (script_dirent->d_type != DT_UNKNOWN || \
  2657. !is_directory(lang_path, script_dirent)))
  2658. #define RECORD_SUFFIX "-record"
  2659. #define REPORT_SUFFIX "-report"
  2660. struct script_desc {
  2661. struct list_head node;
  2662. char *name;
  2663. char *half_liner;
  2664. char *args;
  2665. };
  2666. static LIST_HEAD(script_descs);
  2667. static struct script_desc *script_desc__new(const char *name)
  2668. {
  2669. struct script_desc *s = zalloc(sizeof(*s));
  2670. if (s != NULL && name)
  2671. s->name = strdup(name);
  2672. return s;
  2673. }
  2674. static void script_desc__delete(struct script_desc *s)
  2675. {
  2676. zfree(&s->name);
  2677. zfree(&s->half_liner);
  2678. zfree(&s->args);
  2679. free(s);
  2680. }
  2681. static void script_desc__add(struct script_desc *s)
  2682. {
  2683. list_add_tail(&s->node, &script_descs);
  2684. }
  2685. static struct script_desc *script_desc__find(const char *name)
  2686. {
  2687. struct script_desc *s;
  2688. list_for_each_entry(s, &script_descs, node)
  2689. if (strcasecmp(s->name, name) == 0)
  2690. return s;
  2691. return NULL;
  2692. }
  2693. static struct script_desc *script_desc__findnew(const char *name)
  2694. {
  2695. struct script_desc *s = script_desc__find(name);
  2696. if (s)
  2697. return s;
  2698. s = script_desc__new(name);
  2699. if (!s)
  2700. return NULL;
  2701. script_desc__add(s);
  2702. return s;
  2703. }
  2704. static const char *ends_with(const char *str, const char *suffix)
  2705. {
  2706. size_t suffix_len = strlen(suffix);
  2707. const char *p = str;
  2708. if (strlen(str) > suffix_len) {
  2709. p = str + strlen(str) - suffix_len;
  2710. if (!strncmp(p, suffix, suffix_len))
  2711. return p;
  2712. }
  2713. return NULL;
  2714. }
  2715. static int read_script_info(struct script_desc *desc, const char *filename)
  2716. {
  2717. char line[BUFSIZ], *p;
  2718. FILE *fp;
  2719. fp = fopen(filename, "r");
  2720. if (!fp)
  2721. return -1;
  2722. while (fgets(line, sizeof(line), fp)) {
  2723. p = skip_spaces(line);
  2724. if (strlen(p) == 0)
  2725. continue;
  2726. if (*p != '#')
  2727. continue;
  2728. p++;
  2729. if (strlen(p) && *p == '!')
  2730. continue;
  2731. p = skip_spaces(p);
  2732. if (strlen(p) && p[strlen(p) - 1] == '\n')
  2733. p[strlen(p) - 1] = '\0';
  2734. if (!strncmp(p, "description:", strlen("description:"))) {
  2735. p += strlen("description:");
  2736. desc->half_liner = strdup(skip_spaces(p));
  2737. continue;
  2738. }
  2739. if (!strncmp(p, "args:", strlen("args:"))) {
  2740. p += strlen("args:");
  2741. desc->args = strdup(skip_spaces(p));
  2742. continue;
  2743. }
  2744. }
  2745. fclose(fp);
  2746. return 0;
  2747. }
  2748. static char *get_script_root(struct dirent *script_dirent, const char *suffix)
  2749. {
  2750. char *script_root, *str;
  2751. script_root = strdup(script_dirent->d_name);
  2752. if (!script_root)
  2753. return NULL;
  2754. str = (char *)ends_with(script_root, suffix);
  2755. if (!str) {
  2756. free(script_root);
  2757. return NULL;
  2758. }
  2759. *str = '\0';
  2760. return script_root;
  2761. }
  2762. static int list_available_scripts(const struct option *opt __maybe_unused,
  2763. const char *s __maybe_unused,
  2764. int unset __maybe_unused)
  2765. {
  2766. struct dirent *script_dirent, *lang_dirent;
  2767. char scripts_path[MAXPATHLEN];
  2768. DIR *scripts_dir, *lang_dir;
  2769. char script_path[MAXPATHLEN];
  2770. char lang_path[MAXPATHLEN];
  2771. struct script_desc *desc;
  2772. char first_half[BUFSIZ];
  2773. char *script_root;
  2774. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
  2775. scripts_dir = opendir(scripts_path);
  2776. if (!scripts_dir) {
  2777. fprintf(stdout,
  2778. "open(%s) failed.\n"
  2779. "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
  2780. scripts_path);
  2781. exit(-1);
  2782. }
  2783. for_each_lang(scripts_path, scripts_dir, lang_dirent) {
  2784. scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  2785. lang_dirent->d_name);
  2786. lang_dir = opendir(lang_path);
  2787. if (!lang_dir)
  2788. continue;
  2789. for_each_script(lang_path, lang_dir, script_dirent) {
  2790. script_root = get_script_root(script_dirent, REPORT_SUFFIX);
  2791. if (script_root) {
  2792. desc = script_desc__findnew(script_root);
  2793. scnprintf(script_path, MAXPATHLEN, "%s/%s",
  2794. lang_path, script_dirent->d_name);
  2795. read_script_info(desc, script_path);
  2796. free(script_root);
  2797. }
  2798. }
  2799. }
  2800. fprintf(stdout, "List of available trace scripts:\n");
  2801. list_for_each_entry(desc, &script_descs, node) {
  2802. sprintf(first_half, "%s %s", desc->name,
  2803. desc->args ? desc->args : "");
  2804. fprintf(stdout, " %-36s %s\n", first_half,
  2805. desc->half_liner ? desc->half_liner : "");
  2806. }
  2807. exit(0);
  2808. }
  2809. static int add_dlarg(const struct option *opt __maybe_unused,
  2810. const char *s, int unset __maybe_unused)
  2811. {
  2812. char *arg = strdup(s);
  2813. void *a;
  2814. if (!arg)
  2815. return -1;
  2816. a = realloc(dlargv, sizeof(dlargv[0]) * (dlargc + 1));
  2817. if (!a) {
  2818. free(arg);
  2819. return -1;
  2820. }
  2821. dlargv = a;
  2822. dlargv[dlargc++] = arg;
  2823. return 0;
  2824. }
  2825. static void free_dlarg(void)
  2826. {
  2827. while (dlargc--)
  2828. free(dlargv[dlargc]);
  2829. free(dlargv);
  2830. }
  2831. /*
  2832. * Some scripts specify the required events in their "xxx-record" file,
  2833. * this function will check if the events in perf.data match those
  2834. * mentioned in the "xxx-record".
  2835. *
  2836. * Fixme: All existing "xxx-record" are all in good formats "-e event ",
  2837. * which is covered well now. And new parsing code should be added to
  2838. * cover the future complex formats like event groups etc.
  2839. */
  2840. static int check_ev_match(char *dir_name, char *scriptname,
  2841. struct perf_session *session)
  2842. {
  2843. char filename[MAXPATHLEN], evname[128];
  2844. char line[BUFSIZ], *p;
  2845. struct evsel *pos;
  2846. int match, len;
  2847. FILE *fp;
  2848. scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
  2849. fp = fopen(filename, "r");
  2850. if (!fp)
  2851. return -1;
  2852. while (fgets(line, sizeof(line), fp)) {
  2853. p = skip_spaces(line);
  2854. if (*p == '#')
  2855. continue;
  2856. while (strlen(p)) {
  2857. p = strstr(p, "-e");
  2858. if (!p)
  2859. break;
  2860. p += 2;
  2861. p = skip_spaces(p);
  2862. len = strcspn(p, " \t");
  2863. if (!len)
  2864. break;
  2865. snprintf(evname, len + 1, "%s", p);
  2866. match = 0;
  2867. evlist__for_each_entry(session->evlist, pos) {
  2868. if (!strcmp(evsel__name(pos), evname)) {
  2869. match = 1;
  2870. break;
  2871. }
  2872. }
  2873. if (!match) {
  2874. fclose(fp);
  2875. return -1;
  2876. }
  2877. }
  2878. }
  2879. fclose(fp);
  2880. return 0;
  2881. }
  2882. /*
  2883. * Return -1 if none is found, otherwise the actual scripts number.
  2884. *
  2885. * Currently the only user of this function is the script browser, which
  2886. * will list all statically runnable scripts, select one, execute it and
  2887. * show the output in a perf browser.
  2888. */
  2889. int find_scripts(char **scripts_array, char **scripts_path_array, int num,
  2890. int pathlen)
  2891. {
  2892. struct dirent *script_dirent, *lang_dirent;
  2893. char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
  2894. DIR *scripts_dir, *lang_dir;
  2895. struct perf_session *session;
  2896. struct perf_data data = {
  2897. .path = input_name,
  2898. .mode = PERF_DATA_MODE_READ,
  2899. };
  2900. char *temp;
  2901. int i = 0;
  2902. session = perf_session__new(&data, NULL);
  2903. if (IS_ERR(session))
  2904. return PTR_ERR(session);
  2905. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
  2906. scripts_dir = opendir(scripts_path);
  2907. if (!scripts_dir) {
  2908. perf_session__delete(session);
  2909. return -1;
  2910. }
  2911. for_each_lang(scripts_path, scripts_dir, lang_dirent) {
  2912. scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
  2913. lang_dirent->d_name);
  2914. #ifndef HAVE_LIBPERL_SUPPORT
  2915. if (strstr(lang_path, "perl"))
  2916. continue;
  2917. #endif
  2918. #ifndef HAVE_LIBPYTHON_SUPPORT
  2919. if (strstr(lang_path, "python"))
  2920. continue;
  2921. #endif
  2922. lang_dir = opendir(lang_path);
  2923. if (!lang_dir)
  2924. continue;
  2925. for_each_script(lang_path, lang_dir, script_dirent) {
  2926. /* Skip those real time scripts: xxxtop.p[yl] */
  2927. if (strstr(script_dirent->d_name, "top."))
  2928. continue;
  2929. if (i >= num)
  2930. break;
  2931. snprintf(scripts_path_array[i], pathlen, "%s/%s",
  2932. lang_path,
  2933. script_dirent->d_name);
  2934. temp = strchr(script_dirent->d_name, '.');
  2935. snprintf(scripts_array[i],
  2936. (temp - script_dirent->d_name) + 1,
  2937. "%s", script_dirent->d_name);
  2938. if (check_ev_match(lang_path,
  2939. scripts_array[i], session))
  2940. continue;
  2941. i++;
  2942. }
  2943. closedir(lang_dir);
  2944. }
  2945. closedir(scripts_dir);
  2946. perf_session__delete(session);
  2947. return i;
  2948. }
  2949. static char *get_script_path(const char *script_root, const char *suffix)
  2950. {
  2951. struct dirent *script_dirent, *lang_dirent;
  2952. char scripts_path[MAXPATHLEN];
  2953. char script_path[MAXPATHLEN];
  2954. DIR *scripts_dir, *lang_dir;
  2955. char lang_path[MAXPATHLEN];
  2956. char *__script_root;
  2957. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
  2958. scripts_dir = opendir(scripts_path);
  2959. if (!scripts_dir)
  2960. return NULL;
  2961. for_each_lang(scripts_path, scripts_dir, lang_dirent) {
  2962. scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  2963. lang_dirent->d_name);
  2964. lang_dir = opendir(lang_path);
  2965. if (!lang_dir)
  2966. continue;
  2967. for_each_script(lang_path, lang_dir, script_dirent) {
  2968. __script_root = get_script_root(script_dirent, suffix);
  2969. if (__script_root && !strcmp(script_root, __script_root)) {
  2970. free(__script_root);
  2971. closedir(scripts_dir);
  2972. scnprintf(script_path, MAXPATHLEN, "%s/%s",
  2973. lang_path, script_dirent->d_name);
  2974. closedir(lang_dir);
  2975. return strdup(script_path);
  2976. }
  2977. free(__script_root);
  2978. }
  2979. closedir(lang_dir);
  2980. }
  2981. closedir(scripts_dir);
  2982. return NULL;
  2983. }
  2984. static bool is_top_script(const char *script_path)
  2985. {
  2986. return ends_with(script_path, "top") != NULL;
  2987. }
  2988. static int has_required_arg(char *script_path)
  2989. {
  2990. struct script_desc *desc;
  2991. int n_args = 0;
  2992. char *p;
  2993. desc = script_desc__new(NULL);
  2994. if (read_script_info(desc, script_path))
  2995. goto out;
  2996. if (!desc->args)
  2997. goto out;
  2998. for (p = desc->args; *p; p++)
  2999. if (*p == '<')
  3000. n_args++;
  3001. out:
  3002. script_desc__delete(desc);
  3003. return n_args;
  3004. }
  3005. static int have_cmd(int argc, const char **argv)
  3006. {
  3007. char **__argv = malloc(sizeof(const char *) * argc);
  3008. if (!__argv) {
  3009. pr_err("malloc failed\n");
  3010. return -1;
  3011. }
  3012. memcpy(__argv, argv, sizeof(const char *) * argc);
  3013. argc = parse_options(argc, (const char **)__argv, record_options,
  3014. NULL, PARSE_OPT_STOP_AT_NON_OPTION);
  3015. free(__argv);
  3016. system_wide = (argc == 0);
  3017. return 0;
  3018. }
  3019. static void script__setup_sample_type(struct perf_script *script)
  3020. {
  3021. struct perf_session *session = script->session;
  3022. u64 sample_type = evlist__combined_sample_type(session->evlist);
  3023. callchain_param_setup(sample_type, perf_env__arch(session->machines.host.env));
  3024. if (script->stitch_lbr && (callchain_param.record_mode != CALLCHAIN_LBR)) {
  3025. pr_warning("Can't find LBR callchain. Switch off --stitch-lbr.\n"
  3026. "Please apply --call-graph lbr when recording.\n");
  3027. script->stitch_lbr = false;
  3028. }
  3029. }
  3030. static int process_stat_round_event(struct perf_session *session,
  3031. union perf_event *event)
  3032. {
  3033. struct perf_record_stat_round *round = &event->stat_round;
  3034. struct evsel *counter;
  3035. evlist__for_each_entry(session->evlist, counter) {
  3036. perf_stat_process_counter(&stat_config, counter);
  3037. process_stat(counter, round->time);
  3038. }
  3039. process_stat_interval(round->time);
  3040. return 0;
  3041. }
  3042. static int process_stat_config_event(struct perf_session *session __maybe_unused,
  3043. union perf_event *event)
  3044. {
  3045. perf_event__read_stat_config(&stat_config, &event->stat_config);
  3046. return 0;
  3047. }
  3048. static int set_maps(struct perf_script *script)
  3049. {
  3050. struct evlist *evlist = script->session->evlist;
  3051. if (!script->cpus || !script->threads)
  3052. return 0;
  3053. if (WARN_ONCE(script->allocated, "stats double allocation\n"))
  3054. return -EINVAL;
  3055. perf_evlist__set_maps(&evlist->core, script->cpus, script->threads);
  3056. if (evlist__alloc_stats(evlist, true))
  3057. return -ENOMEM;
  3058. script->allocated = true;
  3059. return 0;
  3060. }
  3061. static
  3062. int process_thread_map_event(struct perf_session *session,
  3063. union perf_event *event)
  3064. {
  3065. struct perf_tool *tool = session->tool;
  3066. struct perf_script *script = container_of(tool, struct perf_script, tool);
  3067. if (dump_trace)
  3068. perf_event__fprintf_thread_map(event, stdout);
  3069. if (script->threads) {
  3070. pr_warning("Extra thread map event, ignoring.\n");
  3071. return 0;
  3072. }
  3073. script->threads = thread_map__new_event(&event->thread_map);
  3074. if (!script->threads)
  3075. return -ENOMEM;
  3076. return set_maps(script);
  3077. }
  3078. static
  3079. int process_cpu_map_event(struct perf_session *session,
  3080. union perf_event *event)
  3081. {
  3082. struct perf_tool *tool = session->tool;
  3083. struct perf_script *script = container_of(tool, struct perf_script, tool);
  3084. if (dump_trace)
  3085. perf_event__fprintf_cpu_map(event, stdout);
  3086. if (script->cpus) {
  3087. pr_warning("Extra cpu map event, ignoring.\n");
  3088. return 0;
  3089. }
  3090. script->cpus = cpu_map__new_data(&event->cpu_map.data);
  3091. if (!script->cpus)
  3092. return -ENOMEM;
  3093. return set_maps(script);
  3094. }
  3095. static int process_feature_event(struct perf_session *session,
  3096. union perf_event *event)
  3097. {
  3098. if (event->feat.feat_id < HEADER_LAST_FEATURE)
  3099. return perf_event__process_feature(session, event);
  3100. return 0;
  3101. }
  3102. #ifdef HAVE_AUXTRACE_SUPPORT
  3103. static int perf_script__process_auxtrace_info(struct perf_session *session,
  3104. union perf_event *event)
  3105. {
  3106. struct perf_tool *tool = session->tool;
  3107. int ret = perf_event__process_auxtrace_info(session, event);
  3108. if (ret == 0) {
  3109. struct perf_script *script = container_of(tool, struct perf_script, tool);
  3110. ret = perf_script__setup_per_event_dump(script);
  3111. }
  3112. return ret;
  3113. }
  3114. #else
  3115. #define perf_script__process_auxtrace_info 0
  3116. #endif
  3117. static int parse_insn_trace(const struct option *opt __maybe_unused,
  3118. const char *str __maybe_unused,
  3119. int unset __maybe_unused)
  3120. {
  3121. parse_output_fields(NULL, "+insn,-event,-period", 0);
  3122. itrace_parse_synth_opts(opt, "i0ns", 0);
  3123. symbol_conf.nanosecs = true;
  3124. return 0;
  3125. }
  3126. static int parse_xed(const struct option *opt __maybe_unused,
  3127. const char *str __maybe_unused,
  3128. int unset __maybe_unused)
  3129. {
  3130. if (isatty(1))
  3131. force_pager("xed -F insn: -A -64 | less");
  3132. else
  3133. force_pager("xed -F insn: -A -64");
  3134. return 0;
  3135. }
  3136. static int parse_call_trace(const struct option *opt __maybe_unused,
  3137. const char *str __maybe_unused,
  3138. int unset __maybe_unused)
  3139. {
  3140. parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent", 0);
  3141. itrace_parse_synth_opts(opt, "cewp", 0);
  3142. symbol_conf.nanosecs = true;
  3143. symbol_conf.pad_output_len_dso = 50;
  3144. return 0;
  3145. }
  3146. static int parse_callret_trace(const struct option *opt __maybe_unused,
  3147. const char *str __maybe_unused,
  3148. int unset __maybe_unused)
  3149. {
  3150. parse_output_fields(NULL, "-ip,-addr,-event,-period,+callindent,+flags", 0);
  3151. itrace_parse_synth_opts(opt, "crewp", 0);
  3152. symbol_conf.nanosecs = true;
  3153. return 0;
  3154. }
  3155. int cmd_script(int argc, const char **argv)
  3156. {
  3157. bool show_full_info = false;
  3158. bool header = false;
  3159. bool header_only = false;
  3160. bool script_started = false;
  3161. bool unsorted_dump = false;
  3162. char *rec_script_path = NULL;
  3163. char *rep_script_path = NULL;
  3164. struct perf_session *session;
  3165. struct itrace_synth_opts itrace_synth_opts = {
  3166. .set = false,
  3167. .default_no_sample = true,
  3168. };
  3169. struct utsname uts;
  3170. char *script_path = NULL;
  3171. const char *dlfilter_file = NULL;
  3172. const char **__argv;
  3173. int i, j, err = 0;
  3174. struct perf_script script = {
  3175. .tool = {
  3176. .sample = process_sample_event,
  3177. .mmap = perf_event__process_mmap,
  3178. .mmap2 = perf_event__process_mmap2,
  3179. .comm = perf_event__process_comm,
  3180. .namespaces = perf_event__process_namespaces,
  3181. .cgroup = perf_event__process_cgroup,
  3182. .exit = perf_event__process_exit,
  3183. .fork = perf_event__process_fork,
  3184. .attr = process_attr,
  3185. .event_update = perf_event__process_event_update,
  3186. .tracing_data = perf_event__process_tracing_data,
  3187. .feature = process_feature_event,
  3188. .build_id = perf_event__process_build_id,
  3189. .id_index = perf_event__process_id_index,
  3190. .auxtrace_info = perf_script__process_auxtrace_info,
  3191. .auxtrace = perf_event__process_auxtrace,
  3192. .auxtrace_error = perf_event__process_auxtrace_error,
  3193. .stat = perf_event__process_stat_event,
  3194. .stat_round = process_stat_round_event,
  3195. .stat_config = process_stat_config_event,
  3196. .thread_map = process_thread_map_event,
  3197. .cpu_map = process_cpu_map_event,
  3198. .throttle = process_throttle_event,
  3199. .unthrottle = process_throttle_event,
  3200. .ordered_events = true,
  3201. .ordering_requires_timestamps = true,
  3202. },
  3203. };
  3204. struct perf_data data = {
  3205. .mode = PERF_DATA_MODE_READ,
  3206. };
  3207. const struct option options[] = {
  3208. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  3209. "dump raw trace in ASCII"),
  3210. OPT_BOOLEAN(0, "dump-unsorted-raw-trace", &unsorted_dump,
  3211. "dump unsorted raw trace in ASCII"),
  3212. OPT_INCR('v', "verbose", &verbose,
  3213. "be more verbose (show symbol address, etc)"),
  3214. OPT_BOOLEAN('L', "Latency", &latency_format,
  3215. "show latency attributes (irqs/preemption disabled, etc)"),
  3216. OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
  3217. list_available_scripts),
  3218. OPT_CALLBACK_NOOPT(0, "list-dlfilters", NULL, NULL, "list available dlfilters",
  3219. list_available_dlfilters),
  3220. OPT_CALLBACK('s', "script", NULL, "name",
  3221. "script file name (lang:script name, script name, or *)",
  3222. parse_scriptname),
  3223. OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
  3224. "generate perf-script.xx script in specified language"),
  3225. OPT_STRING(0, "dlfilter", &dlfilter_file, "file", "filter .so file name"),
  3226. OPT_CALLBACK(0, "dlarg", NULL, "argument", "filter argument",
  3227. add_dlarg),
  3228. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  3229. OPT_BOOLEAN('d', "debug-mode", &debug_mode,
  3230. "do various checks like samples ordering and lost events"),
  3231. OPT_BOOLEAN(0, "header", &header, "Show data header."),
  3232. OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
  3233. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  3234. "file", "vmlinux pathname"),
  3235. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  3236. "file", "kallsyms pathname"),
  3237. OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
  3238. "When printing symbols do not display call chain"),
  3239. OPT_CALLBACK(0, "symfs", NULL, "directory",
  3240. "Look for files with symbols relative to this directory",
  3241. symbol__config_symfs),
  3242. OPT_CALLBACK('F', "fields", NULL, "str",
  3243. "comma separated output fields prepend with 'type:'. "
  3244. "+field to add and -field to remove."
  3245. "Valid types: hw,sw,trace,raw,synth. "
  3246. "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
  3247. "addr,symoff,srcline,period,iregs,uregs,brstack,"
  3248. "brstacksym,flags,data_src,weight,bpf-output,brstackinsn,"
  3249. "brstackinsnlen,brstackoff,callindent,insn,insnlen,synth,"
  3250. "phys_addr,metric,misc,srccode,ipc,tod,data_page_size,"
  3251. "code_page_size,ins_lat",
  3252. parse_output_fields),
  3253. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  3254. "system-wide collection from all CPUs"),
  3255. OPT_STRING(0, "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  3256. "only consider symbols in these DSOs"),
  3257. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  3258. "only consider these symbols"),
  3259. OPT_INTEGER(0, "addr-range", &symbol_conf.addr_range,
  3260. "Use with -S to list traced records within address range"),
  3261. OPT_CALLBACK_OPTARG(0, "insn-trace", &itrace_synth_opts, NULL, NULL,
  3262. "Decode instructions from itrace", parse_insn_trace),
  3263. OPT_CALLBACK_OPTARG(0, "xed", NULL, NULL, NULL,
  3264. "Run xed disassembler on output", parse_xed),
  3265. OPT_CALLBACK_OPTARG(0, "call-trace", &itrace_synth_opts, NULL, NULL,
  3266. "Decode calls from itrace", parse_call_trace),
  3267. OPT_CALLBACK_OPTARG(0, "call-ret-trace", &itrace_synth_opts, NULL, NULL,
  3268. "Decode calls and returns from itrace", parse_callret_trace),
  3269. OPT_STRING(0, "graph-function", &symbol_conf.graph_function, "symbol[,symbol...]",
  3270. "Only print symbols and callees with --call-trace/--call-ret-trace"),
  3271. OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
  3272. "Stop display of callgraph at these symbols"),
  3273. OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
  3274. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  3275. "only display events for these comms"),
  3276. OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
  3277. "only consider symbols in these pids"),
  3278. OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
  3279. "only consider symbols in these tids"),
  3280. OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
  3281. "Set the maximum stack depth when parsing the callchain, "
  3282. "anything beyond the specified depth will be ignored. "
  3283. "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
  3284. OPT_BOOLEAN(0, "reltime", &reltime, "Show time stamps relative to start"),
  3285. OPT_BOOLEAN(0, "deltatime", &deltatime, "Show time stamps relative to previous event"),
  3286. OPT_BOOLEAN('I', "show-info", &show_full_info,
  3287. "display extended information from perf.data file"),
  3288. OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
  3289. "Show the path of [kernel.kallsyms]"),
  3290. OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
  3291. "Show the fork/comm/exit events"),
  3292. OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
  3293. "Show the mmap events"),
  3294. OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
  3295. "Show context switch events (if recorded)"),
  3296. OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
  3297. "Show namespace events (if recorded)"),
  3298. OPT_BOOLEAN('\0', "show-cgroup-events", &script.show_cgroup_events,
  3299. "Show cgroup events (if recorded)"),
  3300. OPT_BOOLEAN('\0', "show-lost-events", &script.show_lost_events,
  3301. "Show lost events (if recorded)"),
  3302. OPT_BOOLEAN('\0', "show-round-events", &script.show_round_events,
  3303. "Show round events (if recorded)"),
  3304. OPT_BOOLEAN('\0', "show-bpf-events", &script.show_bpf_events,
  3305. "Show bpf related events (if recorded)"),
  3306. OPT_BOOLEAN('\0', "show-text-poke-events", &script.show_text_poke_events,
  3307. "Show text poke related events (if recorded)"),
  3308. OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
  3309. "Dump trace output to files named by the monitored events"),
  3310. OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
  3311. OPT_INTEGER(0, "max-blocks", &max_blocks,
  3312. "Maximum number of code blocks to dump with brstackinsn"),
  3313. OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs,
  3314. "Use 9 decimal places when displaying time"),
  3315. OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
  3316. "Instruction Tracing options\n" ITRACE_HELP,
  3317. itrace_parse_synth_opts),
  3318. OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
  3319. "Show full source file name path for source lines"),
  3320. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  3321. "Enable symbol demangling"),
  3322. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  3323. "Enable kernel symbol demangling"),
  3324. OPT_STRING(0, "time", &script.time_str, "str",
  3325. "Time span of interest (start,stop)"),
  3326. OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
  3327. "Show inline function"),
  3328. OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
  3329. "guest mount directory under which every guest os"
  3330. " instance has a subdir"),
  3331. OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
  3332. "file", "file saving guest os vmlinux"),
  3333. OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
  3334. "file", "file saving guest os /proc/kallsyms"),
  3335. OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
  3336. "file", "file saving guest os /proc/modules"),
  3337. OPT_BOOLEAN(0, "guest-code", &symbol_conf.guest_code,
  3338. "Guest code can be found in hypervisor process"),
  3339. OPT_BOOLEAN('\0', "stitch-lbr", &script.stitch_lbr,
  3340. "Enable LBR callgraph stitching approach"),
  3341. OPTS_EVSWITCH(&script.evswitch),
  3342. OPT_END()
  3343. };
  3344. const char * const script_subcommands[] = { "record", "report", NULL };
  3345. const char *script_usage[] = {
  3346. "perf script [<options>]",
  3347. "perf script [<options>] record <script> [<record-options>] <command>",
  3348. "perf script [<options>] report <script> [script-args]",
  3349. "perf script [<options>] <script> [<record-options>] <command>",
  3350. "perf script [<options>] <top-script> [script-args]",
  3351. NULL
  3352. };
  3353. perf_set_singlethreaded();
  3354. setup_scripting();
  3355. argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
  3356. PARSE_OPT_STOP_AT_NON_OPTION);
  3357. if (symbol_conf.guestmount ||
  3358. symbol_conf.default_guest_vmlinux_name ||
  3359. symbol_conf.default_guest_kallsyms ||
  3360. symbol_conf.default_guest_modules ||
  3361. symbol_conf.guest_code) {
  3362. /*
  3363. * Enable guest sample processing.
  3364. */
  3365. perf_guest = true;
  3366. }
  3367. data.path = input_name;
  3368. data.force = symbol_conf.force;
  3369. if (unsorted_dump) {
  3370. dump_trace = true;
  3371. script.tool.ordered_events = false;
  3372. }
  3373. if (symbol__validate_sym_arguments())
  3374. return -1;
  3375. if (argc > 1 && strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
  3376. rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
  3377. if (!rec_script_path)
  3378. return cmd_record(argc, argv);
  3379. }
  3380. if (argc > 1 && strlen(argv[0]) > 2 && strstarts("report", argv[0])) {
  3381. rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
  3382. if (!rep_script_path) {
  3383. fprintf(stderr,
  3384. "Please specify a valid report script"
  3385. "(see 'perf script -l' for listing)\n");
  3386. return -1;
  3387. }
  3388. }
  3389. if (reltime && deltatime) {
  3390. fprintf(stderr,
  3391. "reltime and deltatime - the two don't get along well. "
  3392. "Please limit to --reltime or --deltatime.\n");
  3393. return -1;
  3394. }
  3395. if ((itrace_synth_opts.callchain || itrace_synth_opts.add_callchain) &&
  3396. itrace_synth_opts.callchain_sz > scripting_max_stack)
  3397. scripting_max_stack = itrace_synth_opts.callchain_sz;
  3398. /* make sure PERF_EXEC_PATH is set for scripts */
  3399. set_argv_exec_path(get_argv_exec_path());
  3400. if (argc && !script_name && !rec_script_path && !rep_script_path) {
  3401. int live_pipe[2];
  3402. int rep_args;
  3403. pid_t pid;
  3404. rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
  3405. rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
  3406. if (!rec_script_path && !rep_script_path) {
  3407. script_name = find_script(argv[0]);
  3408. if (script_name) {
  3409. argc -= 1;
  3410. argv += 1;
  3411. goto script_found;
  3412. }
  3413. usage_with_options_msg(script_usage, options,
  3414. "Couldn't find script `%s'\n\n See perf"
  3415. " script -l for available scripts.\n", argv[0]);
  3416. }
  3417. if (is_top_script(argv[0])) {
  3418. rep_args = argc - 1;
  3419. } else {
  3420. int rec_args;
  3421. rep_args = has_required_arg(rep_script_path);
  3422. rec_args = (argc - 1) - rep_args;
  3423. if (rec_args < 0) {
  3424. usage_with_options_msg(script_usage, options,
  3425. "`%s' script requires options."
  3426. "\n\n See perf script -l for available "
  3427. "scripts and options.\n", argv[0]);
  3428. }
  3429. }
  3430. if (pipe(live_pipe) < 0) {
  3431. perror("failed to create pipe");
  3432. return -1;
  3433. }
  3434. pid = fork();
  3435. if (pid < 0) {
  3436. perror("failed to fork");
  3437. return -1;
  3438. }
  3439. if (!pid) {
  3440. j = 0;
  3441. dup2(live_pipe[1], 1);
  3442. close(live_pipe[0]);
  3443. if (is_top_script(argv[0])) {
  3444. system_wide = true;
  3445. } else if (!system_wide) {
  3446. if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
  3447. err = -1;
  3448. goto out;
  3449. }
  3450. }
  3451. __argv = malloc((argc + 6) * sizeof(const char *));
  3452. if (!__argv) {
  3453. pr_err("malloc failed\n");
  3454. err = -ENOMEM;
  3455. goto out;
  3456. }
  3457. __argv[j++] = "/bin/sh";
  3458. __argv[j++] = rec_script_path;
  3459. if (system_wide)
  3460. __argv[j++] = "-a";
  3461. __argv[j++] = "-q";
  3462. __argv[j++] = "-o";
  3463. __argv[j++] = "-";
  3464. for (i = rep_args + 1; i < argc; i++)
  3465. __argv[j++] = argv[i];
  3466. __argv[j++] = NULL;
  3467. execvp("/bin/sh", (char **)__argv);
  3468. free(__argv);
  3469. exit(-1);
  3470. }
  3471. dup2(live_pipe[0], 0);
  3472. close(live_pipe[1]);
  3473. __argv = malloc((argc + 4) * sizeof(const char *));
  3474. if (!__argv) {
  3475. pr_err("malloc failed\n");
  3476. err = -ENOMEM;
  3477. goto out;
  3478. }
  3479. j = 0;
  3480. __argv[j++] = "/bin/sh";
  3481. __argv[j++] = rep_script_path;
  3482. for (i = 1; i < rep_args + 1; i++)
  3483. __argv[j++] = argv[i];
  3484. __argv[j++] = "-i";
  3485. __argv[j++] = "-";
  3486. __argv[j++] = NULL;
  3487. execvp("/bin/sh", (char **)__argv);
  3488. free(__argv);
  3489. exit(-1);
  3490. }
  3491. script_found:
  3492. if (rec_script_path)
  3493. script_path = rec_script_path;
  3494. if (rep_script_path)
  3495. script_path = rep_script_path;
  3496. if (script_path) {
  3497. j = 0;
  3498. if (!rec_script_path)
  3499. system_wide = false;
  3500. else if (!system_wide) {
  3501. if (have_cmd(argc - 1, &argv[1]) != 0) {
  3502. err = -1;
  3503. goto out;
  3504. }
  3505. }
  3506. __argv = malloc((argc + 2) * sizeof(const char *));
  3507. if (!__argv) {
  3508. pr_err("malloc failed\n");
  3509. err = -ENOMEM;
  3510. goto out;
  3511. }
  3512. __argv[j++] = "/bin/sh";
  3513. __argv[j++] = script_path;
  3514. if (system_wide)
  3515. __argv[j++] = "-a";
  3516. for (i = 2; i < argc; i++)
  3517. __argv[j++] = argv[i];
  3518. __argv[j++] = NULL;
  3519. execvp("/bin/sh", (char **)__argv);
  3520. free(__argv);
  3521. exit(-1);
  3522. }
  3523. if (dlfilter_file) {
  3524. dlfilter = dlfilter__new(dlfilter_file, dlargc, dlargv);
  3525. if (!dlfilter)
  3526. return -1;
  3527. }
  3528. if (!script_name) {
  3529. setup_pager();
  3530. use_browser = 0;
  3531. }
  3532. session = perf_session__new(&data, &script.tool);
  3533. if (IS_ERR(session))
  3534. return PTR_ERR(session);
  3535. if (header || header_only) {
  3536. script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
  3537. perf_session__fprintf_info(session, stdout, show_full_info);
  3538. if (header_only)
  3539. goto out_delete;
  3540. }
  3541. if (show_full_info)
  3542. script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
  3543. if (symbol__init(&session->header.env) < 0)
  3544. goto out_delete;
  3545. uname(&uts);
  3546. if (data.is_pipe) { /* Assume pipe_mode indicates native_arch */
  3547. native_arch = true;
  3548. } else if (session->header.env.arch) {
  3549. if (!strcmp(uts.machine, session->header.env.arch))
  3550. native_arch = true;
  3551. else if (!strcmp(uts.machine, "x86_64") &&
  3552. !strcmp(session->header.env.arch, "i386"))
  3553. native_arch = true;
  3554. }
  3555. script.session = session;
  3556. script__setup_sample_type(&script);
  3557. if ((output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT) ||
  3558. symbol_conf.graph_function)
  3559. itrace_synth_opts.thread_stack = true;
  3560. session->itrace_synth_opts = &itrace_synth_opts;
  3561. if (cpu_list) {
  3562. err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
  3563. if (err < 0)
  3564. goto out_delete;
  3565. itrace_synth_opts.cpu_bitmap = cpu_bitmap;
  3566. }
  3567. if (!no_callchain)
  3568. symbol_conf.use_callchain = true;
  3569. else
  3570. symbol_conf.use_callchain = false;
  3571. if (session->tevent.pevent &&
  3572. tep_set_function_resolver(session->tevent.pevent,
  3573. machine__resolve_kernel_addr,
  3574. &session->machines.host) < 0) {
  3575. pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
  3576. err = -1;
  3577. goto out_delete;
  3578. }
  3579. if (generate_script_lang) {
  3580. struct stat perf_stat;
  3581. int input;
  3582. if (output_set_by_user()) {
  3583. fprintf(stderr,
  3584. "custom fields not supported for generated scripts");
  3585. err = -EINVAL;
  3586. goto out_delete;
  3587. }
  3588. input = open(data.path, O_RDONLY); /* input_name */
  3589. if (input < 0) {
  3590. err = -errno;
  3591. perror("failed to open file");
  3592. goto out_delete;
  3593. }
  3594. err = fstat(input, &perf_stat);
  3595. if (err < 0) {
  3596. perror("failed to stat file");
  3597. goto out_delete;
  3598. }
  3599. if (!perf_stat.st_size) {
  3600. fprintf(stderr, "zero-sized file, nothing to do!\n");
  3601. goto out_delete;
  3602. }
  3603. scripting_ops = script_spec__lookup(generate_script_lang);
  3604. if (!scripting_ops) {
  3605. fprintf(stderr, "invalid language specifier");
  3606. err = -ENOENT;
  3607. goto out_delete;
  3608. }
  3609. err = scripting_ops->generate_script(session->tevent.pevent,
  3610. "perf-script");
  3611. goto out_delete;
  3612. }
  3613. err = dlfilter__start(dlfilter, session);
  3614. if (err)
  3615. goto out_delete;
  3616. if (script_name) {
  3617. err = scripting_ops->start_script(script_name, argc, argv, session);
  3618. if (err)
  3619. goto out_delete;
  3620. pr_debug("perf script started with script %s\n\n", script_name);
  3621. script_started = true;
  3622. }
  3623. err = perf_session__check_output_opt(session);
  3624. if (err < 0)
  3625. goto out_delete;
  3626. if (script.time_str) {
  3627. err = perf_time__parse_for_ranges_reltime(script.time_str, session,
  3628. &script.ptime_range,
  3629. &script.range_size,
  3630. &script.range_num,
  3631. reltime);
  3632. if (err < 0)
  3633. goto out_delete;
  3634. itrace_synth_opts__set_time_range(&itrace_synth_opts,
  3635. script.ptime_range,
  3636. script.range_num);
  3637. }
  3638. err = evswitch__init(&script.evswitch, session->evlist, stderr);
  3639. if (err)
  3640. goto out_delete;
  3641. if (zstd_init(&(session->zstd_data), 0) < 0)
  3642. pr_warning("Decompression initialization failed. Reported data may be incomplete.\n");
  3643. err = __cmd_script(&script);
  3644. flush_scripting();
  3645. out_delete:
  3646. if (script.ptime_range) {
  3647. itrace_synth_opts__clear_time_range(&itrace_synth_opts);
  3648. zfree(&script.ptime_range);
  3649. }
  3650. zstd_fini(&(session->zstd_data));
  3651. evlist__free_stats(session->evlist);
  3652. perf_session__delete(session);
  3653. perf_script__exit(&script);
  3654. if (script_started)
  3655. cleanup_scripting();
  3656. dlfilter__cleanup(dlfilter);
  3657. free_dlarg();
  3658. out:
  3659. return err;
  3660. }