builtin-script.c 91 KB

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