builtin-script.c 91 KB

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