builtin-script.c 92 KB

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