builtin-script.c 91 KB

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