builtin-script.c 91 KB

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