builtin-script.c 88 KB

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