builtin-script.c 91 KB

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