builtin-script.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391
  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. evsel_script(evsel)->val = val;
  1321. if (evsel_script(evsel->leader)->gnum == evsel->leader->nr_members) {
  1322. for_each_group_member (ev2, evsel->leader) {
  1323. perf_stat__print_shadow_stats(ev2,
  1324. evsel_script(ev2)->val,
  1325. sample->cpu,
  1326. &ctx,
  1327. NULL);
  1328. }
  1329. evsel_script(evsel->leader)->gnum = 0;
  1330. }
  1331. }
  1332. static void process_event(struct perf_script *script,
  1333. struct perf_sample *sample, struct perf_evsel *evsel,
  1334. struct addr_location *al,
  1335. struct machine *machine)
  1336. {
  1337. struct thread *thread = al->thread;
  1338. struct perf_event_attr *attr = &evsel->attr;
  1339. unsigned int type = output_type(attr->type);
  1340. struct perf_evsel_script *es = evsel->priv;
  1341. FILE *fp = es->fp;
  1342. if (output[type].fields == 0)
  1343. return;
  1344. ++es->samples;
  1345. perf_sample__fprintf_start(sample, thread, evsel, fp);
  1346. if (PRINT_FIELD(PERIOD))
  1347. fprintf(fp, "%10" PRIu64 " ", sample->period);
  1348. if (PRINT_FIELD(EVNAME)) {
  1349. const char *evname = perf_evsel__name(evsel);
  1350. if (!script->name_width)
  1351. script->name_width = perf_evlist__max_name_len(script->session->evlist);
  1352. fprintf(fp, "%*s: ", script->name_width, evname ?: "[unknown]");
  1353. }
  1354. if (print_flags)
  1355. perf_sample__fprintf_flags(sample->flags, fp);
  1356. if (is_bts_event(attr)) {
  1357. perf_sample__fprintf_bts(sample, evsel, thread, al, machine, fp);
  1358. return;
  1359. }
  1360. if (PRINT_FIELD(TRACE)) {
  1361. event_format__fprintf(evsel->tp_format, sample->cpu,
  1362. sample->raw_data, sample->raw_size, fp);
  1363. }
  1364. if (attr->type == PERF_TYPE_SYNTH && PRINT_FIELD(SYNTH))
  1365. perf_sample__fprintf_synth(sample, evsel, fp);
  1366. if (PRINT_FIELD(ADDR))
  1367. perf_sample__fprintf_addr(sample, thread, attr, fp);
  1368. if (PRINT_FIELD(DATA_SRC))
  1369. data_src__fprintf(sample->data_src, fp);
  1370. if (PRINT_FIELD(WEIGHT))
  1371. fprintf(fp, "%16" PRIu64, sample->weight);
  1372. if (PRINT_FIELD(IP)) {
  1373. struct callchain_cursor *cursor = NULL;
  1374. if (symbol_conf.use_callchain && sample->callchain &&
  1375. thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
  1376. sample, NULL, NULL, scripting_max_stack) == 0)
  1377. cursor = &callchain_cursor;
  1378. fputc(cursor ? '\n' : ' ', fp);
  1379. sample__fprintf_sym(sample, al, 0, output[type].print_ip_opts, cursor, fp);
  1380. }
  1381. if (PRINT_FIELD(IREGS))
  1382. perf_sample__fprintf_iregs(sample, attr, fp);
  1383. if (PRINT_FIELD(UREGS))
  1384. perf_sample__fprintf_uregs(sample, attr, fp);
  1385. if (PRINT_FIELD(BRSTACK))
  1386. perf_sample__fprintf_brstack(sample, thread, attr, fp);
  1387. else if (PRINT_FIELD(BRSTACKSYM))
  1388. perf_sample__fprintf_brstacksym(sample, thread, attr, fp);
  1389. else if (PRINT_FIELD(BRSTACKOFF))
  1390. perf_sample__fprintf_brstackoff(sample, thread, attr, fp);
  1391. if (perf_evsel__is_bpf_output(evsel) && PRINT_FIELD(BPF_OUTPUT))
  1392. perf_sample__fprintf_bpf_output(sample, fp);
  1393. perf_sample__fprintf_insn(sample, attr, thread, machine, fp);
  1394. if (PRINT_FIELD(PHYS_ADDR))
  1395. fprintf(fp, "%16" PRIx64, sample->phys_addr);
  1396. fprintf(fp, "\n");
  1397. if (PRINT_FIELD(METRIC))
  1398. perf_sample__fprint_metric(script, thread, evsel, sample, fp);
  1399. }
  1400. static struct scripting_ops *scripting_ops;
  1401. static void __process_stat(struct perf_evsel *counter, u64 tstamp)
  1402. {
  1403. int nthreads = thread_map__nr(counter->threads);
  1404. int ncpus = perf_evsel__nr_cpus(counter);
  1405. int cpu, thread;
  1406. static int header_printed;
  1407. if (counter->system_wide)
  1408. nthreads = 1;
  1409. if (!header_printed) {
  1410. printf("%3s %8s %15s %15s %15s %15s %s\n",
  1411. "CPU", "THREAD", "VAL", "ENA", "RUN", "TIME", "EVENT");
  1412. header_printed = 1;
  1413. }
  1414. for (thread = 0; thread < nthreads; thread++) {
  1415. for (cpu = 0; cpu < ncpus; cpu++) {
  1416. struct perf_counts_values *counts;
  1417. counts = perf_counts(counter->counts, cpu, thread);
  1418. printf("%3d %8d %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %15" PRIu64 " %s\n",
  1419. counter->cpus->map[cpu],
  1420. thread_map__pid(counter->threads, thread),
  1421. counts->val,
  1422. counts->ena,
  1423. counts->run,
  1424. tstamp,
  1425. perf_evsel__name(counter));
  1426. }
  1427. }
  1428. }
  1429. static void process_stat(struct perf_evsel *counter, u64 tstamp)
  1430. {
  1431. if (scripting_ops && scripting_ops->process_stat)
  1432. scripting_ops->process_stat(&stat_config, counter, tstamp);
  1433. else
  1434. __process_stat(counter, tstamp);
  1435. }
  1436. static void process_stat_interval(u64 tstamp)
  1437. {
  1438. if (scripting_ops && scripting_ops->process_stat_interval)
  1439. scripting_ops->process_stat_interval(tstamp);
  1440. }
  1441. static void setup_scripting(void)
  1442. {
  1443. setup_perl_scripting();
  1444. setup_python_scripting();
  1445. }
  1446. static int flush_scripting(void)
  1447. {
  1448. return scripting_ops ? scripting_ops->flush_script() : 0;
  1449. }
  1450. static int cleanup_scripting(void)
  1451. {
  1452. pr_debug("\nperf script stopped\n");
  1453. return scripting_ops ? scripting_ops->stop_script() : 0;
  1454. }
  1455. static int process_sample_event(struct perf_tool *tool,
  1456. union perf_event *event,
  1457. struct perf_sample *sample,
  1458. struct perf_evsel *evsel,
  1459. struct machine *machine)
  1460. {
  1461. struct perf_script *scr = container_of(tool, struct perf_script, tool);
  1462. struct addr_location al;
  1463. if (perf_time__skip_sample(&scr->ptime, sample->time))
  1464. return 0;
  1465. if (debug_mode) {
  1466. if (sample->time < last_timestamp) {
  1467. pr_err("Samples misordered, previous: %" PRIu64
  1468. " this: %" PRIu64 "\n", last_timestamp,
  1469. sample->time);
  1470. nr_unordered++;
  1471. }
  1472. last_timestamp = sample->time;
  1473. return 0;
  1474. }
  1475. if (machine__resolve(machine, &al, sample) < 0) {
  1476. pr_err("problem processing %d event, skipping it.\n",
  1477. event->header.type);
  1478. return -1;
  1479. }
  1480. if (al.filtered)
  1481. goto out_put;
  1482. if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
  1483. goto out_put;
  1484. if (scripting_ops)
  1485. scripting_ops->process_event(event, sample, evsel, &al);
  1486. else
  1487. process_event(scr, sample, evsel, &al, machine);
  1488. out_put:
  1489. addr_location__put(&al);
  1490. return 0;
  1491. }
  1492. static int process_attr(struct perf_tool *tool, union perf_event *event,
  1493. struct perf_evlist **pevlist)
  1494. {
  1495. struct perf_script *scr = container_of(tool, struct perf_script, tool);
  1496. struct perf_evlist *evlist;
  1497. struct perf_evsel *evsel, *pos;
  1498. int err;
  1499. err = perf_event__process_attr(tool, event, pevlist);
  1500. if (err)
  1501. return err;
  1502. evlist = *pevlist;
  1503. evsel = perf_evlist__last(*pevlist);
  1504. if (evsel->attr.type >= PERF_TYPE_MAX &&
  1505. evsel->attr.type != PERF_TYPE_SYNTH)
  1506. return 0;
  1507. evlist__for_each_entry(evlist, pos) {
  1508. if (pos->attr.type == evsel->attr.type && pos != evsel)
  1509. return 0;
  1510. }
  1511. set_print_ip_opts(&evsel->attr);
  1512. if (evsel->attr.sample_type)
  1513. err = perf_evsel__check_attr(evsel, scr->session);
  1514. return err;
  1515. }
  1516. static int process_comm_event(struct perf_tool *tool,
  1517. union perf_event *event,
  1518. struct perf_sample *sample,
  1519. struct machine *machine)
  1520. {
  1521. struct thread *thread;
  1522. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1523. struct perf_session *session = script->session;
  1524. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  1525. int ret = -1;
  1526. thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
  1527. if (thread == NULL) {
  1528. pr_debug("problem processing COMM event, skipping it.\n");
  1529. return -1;
  1530. }
  1531. if (perf_event__process_comm(tool, event, sample, machine) < 0)
  1532. goto out;
  1533. if (!evsel->attr.sample_id_all) {
  1534. sample->cpu = 0;
  1535. sample->time = 0;
  1536. sample->tid = event->comm.tid;
  1537. sample->pid = event->comm.pid;
  1538. }
  1539. perf_sample__fprintf_start(sample, thread, evsel, stdout);
  1540. perf_event__fprintf(event, stdout);
  1541. ret = 0;
  1542. out:
  1543. thread__put(thread);
  1544. return ret;
  1545. }
  1546. static int process_namespaces_event(struct perf_tool *tool,
  1547. union perf_event *event,
  1548. struct perf_sample *sample,
  1549. struct machine *machine)
  1550. {
  1551. struct thread *thread;
  1552. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1553. struct perf_session *session = script->session;
  1554. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  1555. int ret = -1;
  1556. thread = machine__findnew_thread(machine, event->namespaces.pid,
  1557. event->namespaces.tid);
  1558. if (thread == NULL) {
  1559. pr_debug("problem processing NAMESPACES event, skipping it.\n");
  1560. return -1;
  1561. }
  1562. if (perf_event__process_namespaces(tool, event, sample, machine) < 0)
  1563. goto out;
  1564. if (!evsel->attr.sample_id_all) {
  1565. sample->cpu = 0;
  1566. sample->time = 0;
  1567. sample->tid = event->namespaces.tid;
  1568. sample->pid = event->namespaces.pid;
  1569. }
  1570. perf_sample__fprintf_start(sample, thread, evsel, stdout);
  1571. perf_event__fprintf(event, stdout);
  1572. ret = 0;
  1573. out:
  1574. thread__put(thread);
  1575. return ret;
  1576. }
  1577. static int process_fork_event(struct perf_tool *tool,
  1578. union perf_event *event,
  1579. struct perf_sample *sample,
  1580. struct machine *machine)
  1581. {
  1582. struct thread *thread;
  1583. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1584. struct perf_session *session = script->session;
  1585. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  1586. if (perf_event__process_fork(tool, event, sample, machine) < 0)
  1587. return -1;
  1588. thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
  1589. if (thread == NULL) {
  1590. pr_debug("problem processing FORK event, skipping it.\n");
  1591. return -1;
  1592. }
  1593. if (!evsel->attr.sample_id_all) {
  1594. sample->cpu = 0;
  1595. sample->time = event->fork.time;
  1596. sample->tid = event->fork.tid;
  1597. sample->pid = event->fork.pid;
  1598. }
  1599. perf_sample__fprintf_start(sample, thread, evsel, stdout);
  1600. perf_event__fprintf(event, stdout);
  1601. thread__put(thread);
  1602. return 0;
  1603. }
  1604. static int process_exit_event(struct perf_tool *tool,
  1605. union perf_event *event,
  1606. struct perf_sample *sample,
  1607. struct machine *machine)
  1608. {
  1609. int err = 0;
  1610. struct thread *thread;
  1611. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1612. struct perf_session *session = script->session;
  1613. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  1614. thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
  1615. if (thread == NULL) {
  1616. pr_debug("problem processing EXIT event, skipping it.\n");
  1617. return -1;
  1618. }
  1619. if (!evsel->attr.sample_id_all) {
  1620. sample->cpu = 0;
  1621. sample->time = 0;
  1622. sample->tid = event->fork.tid;
  1623. sample->pid = event->fork.pid;
  1624. }
  1625. perf_sample__fprintf_start(sample, thread, evsel, stdout);
  1626. perf_event__fprintf(event, stdout);
  1627. if (perf_event__process_exit(tool, event, sample, machine) < 0)
  1628. err = -1;
  1629. thread__put(thread);
  1630. return err;
  1631. }
  1632. static int process_mmap_event(struct perf_tool *tool,
  1633. union perf_event *event,
  1634. struct perf_sample *sample,
  1635. struct machine *machine)
  1636. {
  1637. struct thread *thread;
  1638. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1639. struct perf_session *session = script->session;
  1640. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  1641. if (perf_event__process_mmap(tool, event, sample, machine) < 0)
  1642. return -1;
  1643. thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
  1644. if (thread == NULL) {
  1645. pr_debug("problem processing MMAP event, skipping it.\n");
  1646. return -1;
  1647. }
  1648. if (!evsel->attr.sample_id_all) {
  1649. sample->cpu = 0;
  1650. sample->time = 0;
  1651. sample->tid = event->mmap.tid;
  1652. sample->pid = event->mmap.pid;
  1653. }
  1654. perf_sample__fprintf_start(sample, thread, evsel, stdout);
  1655. perf_event__fprintf(event, stdout);
  1656. thread__put(thread);
  1657. return 0;
  1658. }
  1659. static int process_mmap2_event(struct perf_tool *tool,
  1660. union perf_event *event,
  1661. struct perf_sample *sample,
  1662. struct machine *machine)
  1663. {
  1664. struct thread *thread;
  1665. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1666. struct perf_session *session = script->session;
  1667. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  1668. if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
  1669. return -1;
  1670. thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
  1671. if (thread == NULL) {
  1672. pr_debug("problem processing MMAP2 event, skipping it.\n");
  1673. return -1;
  1674. }
  1675. if (!evsel->attr.sample_id_all) {
  1676. sample->cpu = 0;
  1677. sample->time = 0;
  1678. sample->tid = event->mmap2.tid;
  1679. sample->pid = event->mmap2.pid;
  1680. }
  1681. perf_sample__fprintf_start(sample, thread, evsel, stdout);
  1682. perf_event__fprintf(event, stdout);
  1683. thread__put(thread);
  1684. return 0;
  1685. }
  1686. static int process_switch_event(struct perf_tool *tool,
  1687. union perf_event *event,
  1688. struct perf_sample *sample,
  1689. struct machine *machine)
  1690. {
  1691. struct thread *thread;
  1692. struct perf_script *script = container_of(tool, struct perf_script, tool);
  1693. struct perf_session *session = script->session;
  1694. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  1695. if (perf_event__process_switch(tool, event, sample, machine) < 0)
  1696. return -1;
  1697. thread = machine__findnew_thread(machine, sample->pid,
  1698. sample->tid);
  1699. if (thread == NULL) {
  1700. pr_debug("problem processing SWITCH event, skipping it.\n");
  1701. return -1;
  1702. }
  1703. perf_sample__fprintf_start(sample, thread, evsel, stdout);
  1704. perf_event__fprintf(event, stdout);
  1705. thread__put(thread);
  1706. return 0;
  1707. }
  1708. static void sig_handler(int sig __maybe_unused)
  1709. {
  1710. session_done = 1;
  1711. }
  1712. static void perf_script__fclose_per_event_dump(struct perf_script *script)
  1713. {
  1714. struct perf_evlist *evlist = script->session->evlist;
  1715. struct perf_evsel *evsel;
  1716. evlist__for_each_entry(evlist, evsel) {
  1717. if (!evsel->priv)
  1718. break;
  1719. perf_evsel_script__delete(evsel->priv);
  1720. evsel->priv = NULL;
  1721. }
  1722. }
  1723. static int perf_script__fopen_per_event_dump(struct perf_script *script)
  1724. {
  1725. struct perf_evsel *evsel;
  1726. evlist__for_each_entry(script->session->evlist, evsel) {
  1727. /*
  1728. * Already setup? I.e. we may be called twice in cases like
  1729. * Intel PT, one for the intel_pt// and dummy events, then
  1730. * for the evsels syntheized from the auxtrace info.
  1731. *
  1732. * Ses perf_script__process_auxtrace_info.
  1733. */
  1734. if (evsel->priv != NULL)
  1735. continue;
  1736. evsel->priv = perf_evsel_script__new(evsel, script->session->data);
  1737. if (evsel->priv == NULL)
  1738. goto out_err_fclose;
  1739. }
  1740. return 0;
  1741. out_err_fclose:
  1742. perf_script__fclose_per_event_dump(script);
  1743. return -1;
  1744. }
  1745. static int perf_script__setup_per_event_dump(struct perf_script *script)
  1746. {
  1747. struct perf_evsel *evsel;
  1748. static struct perf_evsel_script es_stdout;
  1749. if (script->per_event_dump)
  1750. return perf_script__fopen_per_event_dump(script);
  1751. es_stdout.fp = stdout;
  1752. evlist__for_each_entry(script->session->evlist, evsel)
  1753. evsel->priv = &es_stdout;
  1754. return 0;
  1755. }
  1756. static void perf_script__exit_per_event_dump_stats(struct perf_script *script)
  1757. {
  1758. struct perf_evsel *evsel;
  1759. evlist__for_each_entry(script->session->evlist, evsel) {
  1760. struct perf_evsel_script *es = evsel->priv;
  1761. perf_evsel_script__fprintf(es, stdout);
  1762. perf_evsel_script__delete(es);
  1763. evsel->priv = NULL;
  1764. }
  1765. }
  1766. static int __cmd_script(struct perf_script *script)
  1767. {
  1768. int ret;
  1769. signal(SIGINT, sig_handler);
  1770. /* override event processing functions */
  1771. if (script->show_task_events) {
  1772. script->tool.comm = process_comm_event;
  1773. script->tool.fork = process_fork_event;
  1774. script->tool.exit = process_exit_event;
  1775. }
  1776. if (script->show_mmap_events) {
  1777. script->tool.mmap = process_mmap_event;
  1778. script->tool.mmap2 = process_mmap2_event;
  1779. }
  1780. if (script->show_switch_events)
  1781. script->tool.context_switch = process_switch_event;
  1782. if (script->show_namespace_events)
  1783. script->tool.namespaces = process_namespaces_event;
  1784. if (perf_script__setup_per_event_dump(script)) {
  1785. pr_err("Couldn't create the per event dump files\n");
  1786. return -1;
  1787. }
  1788. ret = perf_session__process_events(script->session);
  1789. if (script->per_event_dump)
  1790. perf_script__exit_per_event_dump_stats(script);
  1791. if (debug_mode)
  1792. pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
  1793. return ret;
  1794. }
  1795. struct script_spec {
  1796. struct list_head node;
  1797. struct scripting_ops *ops;
  1798. char spec[0];
  1799. };
  1800. static LIST_HEAD(script_specs);
  1801. static struct script_spec *script_spec__new(const char *spec,
  1802. struct scripting_ops *ops)
  1803. {
  1804. struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
  1805. if (s != NULL) {
  1806. strcpy(s->spec, spec);
  1807. s->ops = ops;
  1808. }
  1809. return s;
  1810. }
  1811. static void script_spec__add(struct script_spec *s)
  1812. {
  1813. list_add_tail(&s->node, &script_specs);
  1814. }
  1815. static struct script_spec *script_spec__find(const char *spec)
  1816. {
  1817. struct script_spec *s;
  1818. list_for_each_entry(s, &script_specs, node)
  1819. if (strcasecmp(s->spec, spec) == 0)
  1820. return s;
  1821. return NULL;
  1822. }
  1823. int script_spec_register(const char *spec, struct scripting_ops *ops)
  1824. {
  1825. struct script_spec *s;
  1826. s = script_spec__find(spec);
  1827. if (s)
  1828. return -1;
  1829. s = script_spec__new(spec, ops);
  1830. if (!s)
  1831. return -1;
  1832. else
  1833. script_spec__add(s);
  1834. return 0;
  1835. }
  1836. static struct scripting_ops *script_spec__lookup(const char *spec)
  1837. {
  1838. struct script_spec *s = script_spec__find(spec);
  1839. if (!s)
  1840. return NULL;
  1841. return s->ops;
  1842. }
  1843. static void list_available_languages(void)
  1844. {
  1845. struct script_spec *s;
  1846. fprintf(stderr, "\n");
  1847. fprintf(stderr, "Scripting language extensions (used in "
  1848. "perf script -s [spec:]script.[spec]):\n\n");
  1849. list_for_each_entry(s, &script_specs, node)
  1850. fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
  1851. fprintf(stderr, "\n");
  1852. }
  1853. static int parse_scriptname(const struct option *opt __maybe_unused,
  1854. const char *str, int unset __maybe_unused)
  1855. {
  1856. char spec[PATH_MAX];
  1857. const char *script, *ext;
  1858. int len;
  1859. if (strcmp(str, "lang") == 0) {
  1860. list_available_languages();
  1861. exit(0);
  1862. }
  1863. script = strchr(str, ':');
  1864. if (script) {
  1865. len = script - str;
  1866. if (len >= PATH_MAX) {
  1867. fprintf(stderr, "invalid language specifier");
  1868. return -1;
  1869. }
  1870. strncpy(spec, str, len);
  1871. spec[len] = '\0';
  1872. scripting_ops = script_spec__lookup(spec);
  1873. if (!scripting_ops) {
  1874. fprintf(stderr, "invalid language specifier");
  1875. return -1;
  1876. }
  1877. script++;
  1878. } else {
  1879. script = str;
  1880. ext = strrchr(script, '.');
  1881. if (!ext) {
  1882. fprintf(stderr, "invalid script extension");
  1883. return -1;
  1884. }
  1885. scripting_ops = script_spec__lookup(++ext);
  1886. if (!scripting_ops) {
  1887. fprintf(stderr, "invalid script extension");
  1888. return -1;
  1889. }
  1890. }
  1891. script_name = strdup(script);
  1892. return 0;
  1893. }
  1894. static int parse_output_fields(const struct option *opt __maybe_unused,
  1895. const char *arg, int unset __maybe_unused)
  1896. {
  1897. char *tok, *strtok_saveptr = NULL;
  1898. int i, imax = ARRAY_SIZE(all_output_options);
  1899. int j;
  1900. int rc = 0;
  1901. char *str = strdup(arg);
  1902. int type = -1;
  1903. enum { DEFAULT, SET, ADD, REMOVE } change = DEFAULT;
  1904. if (!str)
  1905. return -ENOMEM;
  1906. /* first word can state for which event type the user is specifying
  1907. * the fields. If no type exists, the specified fields apply to all
  1908. * event types found in the file minus the invalid fields for a type.
  1909. */
  1910. tok = strchr(str, ':');
  1911. if (tok) {
  1912. *tok = '\0';
  1913. tok++;
  1914. if (!strcmp(str, "hw"))
  1915. type = PERF_TYPE_HARDWARE;
  1916. else if (!strcmp(str, "sw"))
  1917. type = PERF_TYPE_SOFTWARE;
  1918. else if (!strcmp(str, "trace"))
  1919. type = PERF_TYPE_TRACEPOINT;
  1920. else if (!strcmp(str, "raw"))
  1921. type = PERF_TYPE_RAW;
  1922. else if (!strcmp(str, "break"))
  1923. type = PERF_TYPE_BREAKPOINT;
  1924. else if (!strcmp(str, "synth"))
  1925. type = OUTPUT_TYPE_SYNTH;
  1926. else {
  1927. fprintf(stderr, "Invalid event type in field string.\n");
  1928. rc = -EINVAL;
  1929. goto out;
  1930. }
  1931. if (output[type].user_set)
  1932. pr_warning("Overriding previous field request for %s events.\n",
  1933. event_type(type));
  1934. output[type].fields = 0;
  1935. output[type].user_set = true;
  1936. output[type].wildcard_set = false;
  1937. } else {
  1938. tok = str;
  1939. if (strlen(str) == 0) {
  1940. fprintf(stderr,
  1941. "Cannot set fields to 'none' for all event types.\n");
  1942. rc = -EINVAL;
  1943. goto out;
  1944. }
  1945. /* Don't override defaults for +- */
  1946. if (strchr(str, '+') || strchr(str, '-'))
  1947. goto parse;
  1948. if (output_set_by_user())
  1949. pr_warning("Overriding previous field request for all events.\n");
  1950. for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
  1951. output[j].fields = 0;
  1952. output[j].user_set = true;
  1953. output[j].wildcard_set = true;
  1954. }
  1955. }
  1956. parse:
  1957. for (tok = strtok_r(tok, ",", &strtok_saveptr); tok; tok = strtok_r(NULL, ",", &strtok_saveptr)) {
  1958. if (*tok == '+') {
  1959. if (change == SET)
  1960. goto out_badmix;
  1961. change = ADD;
  1962. tok++;
  1963. } else if (*tok == '-') {
  1964. if (change == SET)
  1965. goto out_badmix;
  1966. change = REMOVE;
  1967. tok++;
  1968. } else {
  1969. if (change != SET && change != DEFAULT)
  1970. goto out_badmix;
  1971. change = SET;
  1972. }
  1973. for (i = 0; i < imax; ++i) {
  1974. if (strcmp(tok, all_output_options[i].str) == 0)
  1975. break;
  1976. }
  1977. if (i == imax && strcmp(tok, "flags") == 0) {
  1978. print_flags = change == REMOVE ? false : true;
  1979. continue;
  1980. }
  1981. if (i == imax) {
  1982. fprintf(stderr, "Invalid field requested.\n");
  1983. rc = -EINVAL;
  1984. goto out;
  1985. }
  1986. if (type == -1) {
  1987. /* add user option to all events types for
  1988. * which it is valid
  1989. */
  1990. for (j = 0; j < OUTPUT_TYPE_MAX; ++j) {
  1991. if (output[j].invalid_fields & all_output_options[i].field) {
  1992. pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
  1993. all_output_options[i].str, event_type(j));
  1994. } else {
  1995. if (change == REMOVE)
  1996. output[j].fields &= ~all_output_options[i].field;
  1997. else
  1998. output[j].fields |= all_output_options[i].field;
  1999. }
  2000. }
  2001. } else {
  2002. if (output[type].invalid_fields & all_output_options[i].field) {
  2003. fprintf(stderr, "\'%s\' not valid for %s events.\n",
  2004. all_output_options[i].str, event_type(type));
  2005. rc = -EINVAL;
  2006. goto out;
  2007. }
  2008. output[type].fields |= all_output_options[i].field;
  2009. }
  2010. }
  2011. if (type >= 0) {
  2012. if (output[type].fields == 0) {
  2013. pr_debug("No fields requested for %s type. "
  2014. "Events will not be displayed.\n", event_type(type));
  2015. }
  2016. }
  2017. goto out;
  2018. out_badmix:
  2019. fprintf(stderr, "Cannot mix +-field with overridden fields\n");
  2020. rc = -EINVAL;
  2021. out:
  2022. free(str);
  2023. return rc;
  2024. }
  2025. /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
  2026. static int is_directory(const char *base_path, const struct dirent *dent)
  2027. {
  2028. char path[PATH_MAX];
  2029. struct stat st;
  2030. sprintf(path, "%s/%s", base_path, dent->d_name);
  2031. if (stat(path, &st))
  2032. return 0;
  2033. return S_ISDIR(st.st_mode);
  2034. }
  2035. #define for_each_lang(scripts_path, scripts_dir, lang_dirent) \
  2036. while ((lang_dirent = readdir(scripts_dir)) != NULL) \
  2037. if ((lang_dirent->d_type == DT_DIR || \
  2038. (lang_dirent->d_type == DT_UNKNOWN && \
  2039. is_directory(scripts_path, lang_dirent))) && \
  2040. (strcmp(lang_dirent->d_name, ".")) && \
  2041. (strcmp(lang_dirent->d_name, "..")))
  2042. #define for_each_script(lang_path, lang_dir, script_dirent) \
  2043. while ((script_dirent = readdir(lang_dir)) != NULL) \
  2044. if (script_dirent->d_type != DT_DIR && \
  2045. (script_dirent->d_type != DT_UNKNOWN || \
  2046. !is_directory(lang_path, script_dirent)))
  2047. #define RECORD_SUFFIX "-record"
  2048. #define REPORT_SUFFIX "-report"
  2049. struct script_desc {
  2050. struct list_head node;
  2051. char *name;
  2052. char *half_liner;
  2053. char *args;
  2054. };
  2055. static LIST_HEAD(script_descs);
  2056. static struct script_desc *script_desc__new(const char *name)
  2057. {
  2058. struct script_desc *s = zalloc(sizeof(*s));
  2059. if (s != NULL && name)
  2060. s->name = strdup(name);
  2061. return s;
  2062. }
  2063. static void script_desc__delete(struct script_desc *s)
  2064. {
  2065. zfree(&s->name);
  2066. zfree(&s->half_liner);
  2067. zfree(&s->args);
  2068. free(s);
  2069. }
  2070. static void script_desc__add(struct script_desc *s)
  2071. {
  2072. list_add_tail(&s->node, &script_descs);
  2073. }
  2074. static struct script_desc *script_desc__find(const char *name)
  2075. {
  2076. struct script_desc *s;
  2077. list_for_each_entry(s, &script_descs, node)
  2078. if (strcasecmp(s->name, name) == 0)
  2079. return s;
  2080. return NULL;
  2081. }
  2082. static struct script_desc *script_desc__findnew(const char *name)
  2083. {
  2084. struct script_desc *s = script_desc__find(name);
  2085. if (s)
  2086. return s;
  2087. s = script_desc__new(name);
  2088. if (!s)
  2089. return NULL;
  2090. script_desc__add(s);
  2091. return s;
  2092. }
  2093. static const char *ends_with(const char *str, const char *suffix)
  2094. {
  2095. size_t suffix_len = strlen(suffix);
  2096. const char *p = str;
  2097. if (strlen(str) > suffix_len) {
  2098. p = str + strlen(str) - suffix_len;
  2099. if (!strncmp(p, suffix, suffix_len))
  2100. return p;
  2101. }
  2102. return NULL;
  2103. }
  2104. static int read_script_info(struct script_desc *desc, const char *filename)
  2105. {
  2106. char line[BUFSIZ], *p;
  2107. FILE *fp;
  2108. fp = fopen(filename, "r");
  2109. if (!fp)
  2110. return -1;
  2111. while (fgets(line, sizeof(line), fp)) {
  2112. p = ltrim(line);
  2113. if (strlen(p) == 0)
  2114. continue;
  2115. if (*p != '#')
  2116. continue;
  2117. p++;
  2118. if (strlen(p) && *p == '!')
  2119. continue;
  2120. p = ltrim(p);
  2121. if (strlen(p) && p[strlen(p) - 1] == '\n')
  2122. p[strlen(p) - 1] = '\0';
  2123. if (!strncmp(p, "description:", strlen("description:"))) {
  2124. p += strlen("description:");
  2125. desc->half_liner = strdup(ltrim(p));
  2126. continue;
  2127. }
  2128. if (!strncmp(p, "args:", strlen("args:"))) {
  2129. p += strlen("args:");
  2130. desc->args = strdup(ltrim(p));
  2131. continue;
  2132. }
  2133. }
  2134. fclose(fp);
  2135. return 0;
  2136. }
  2137. static char *get_script_root(struct dirent *script_dirent, const char *suffix)
  2138. {
  2139. char *script_root, *str;
  2140. script_root = strdup(script_dirent->d_name);
  2141. if (!script_root)
  2142. return NULL;
  2143. str = (char *)ends_with(script_root, suffix);
  2144. if (!str) {
  2145. free(script_root);
  2146. return NULL;
  2147. }
  2148. *str = '\0';
  2149. return script_root;
  2150. }
  2151. static int list_available_scripts(const struct option *opt __maybe_unused,
  2152. const char *s __maybe_unused,
  2153. int unset __maybe_unused)
  2154. {
  2155. struct dirent *script_dirent, *lang_dirent;
  2156. char scripts_path[MAXPATHLEN];
  2157. DIR *scripts_dir, *lang_dir;
  2158. char script_path[MAXPATHLEN];
  2159. char lang_path[MAXPATHLEN];
  2160. struct script_desc *desc;
  2161. char first_half[BUFSIZ];
  2162. char *script_root;
  2163. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
  2164. scripts_dir = opendir(scripts_path);
  2165. if (!scripts_dir) {
  2166. fprintf(stdout,
  2167. "open(%s) failed.\n"
  2168. "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n",
  2169. scripts_path);
  2170. exit(-1);
  2171. }
  2172. for_each_lang(scripts_path, scripts_dir, lang_dirent) {
  2173. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  2174. lang_dirent->d_name);
  2175. lang_dir = opendir(lang_path);
  2176. if (!lang_dir)
  2177. continue;
  2178. for_each_script(lang_path, lang_dir, script_dirent) {
  2179. script_root = get_script_root(script_dirent, REPORT_SUFFIX);
  2180. if (script_root) {
  2181. desc = script_desc__findnew(script_root);
  2182. snprintf(script_path, MAXPATHLEN, "%s/%s",
  2183. lang_path, script_dirent->d_name);
  2184. read_script_info(desc, script_path);
  2185. free(script_root);
  2186. }
  2187. }
  2188. }
  2189. fprintf(stdout, "List of available trace scripts:\n");
  2190. list_for_each_entry(desc, &script_descs, node) {
  2191. sprintf(first_half, "%s %s", desc->name,
  2192. desc->args ? desc->args : "");
  2193. fprintf(stdout, " %-36s %s\n", first_half,
  2194. desc->half_liner ? desc->half_liner : "");
  2195. }
  2196. exit(0);
  2197. }
  2198. /*
  2199. * Some scripts specify the required events in their "xxx-record" file,
  2200. * this function will check if the events in perf.data match those
  2201. * mentioned in the "xxx-record".
  2202. *
  2203. * Fixme: All existing "xxx-record" are all in good formats "-e event ",
  2204. * which is covered well now. And new parsing code should be added to
  2205. * cover the future complexing formats like event groups etc.
  2206. */
  2207. static int check_ev_match(char *dir_name, char *scriptname,
  2208. struct perf_session *session)
  2209. {
  2210. char filename[MAXPATHLEN], evname[128];
  2211. char line[BUFSIZ], *p;
  2212. struct perf_evsel *pos;
  2213. int match, len;
  2214. FILE *fp;
  2215. sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
  2216. fp = fopen(filename, "r");
  2217. if (!fp)
  2218. return -1;
  2219. while (fgets(line, sizeof(line), fp)) {
  2220. p = ltrim(line);
  2221. if (*p == '#')
  2222. continue;
  2223. while (strlen(p)) {
  2224. p = strstr(p, "-e");
  2225. if (!p)
  2226. break;
  2227. p += 2;
  2228. p = ltrim(p);
  2229. len = strcspn(p, " \t");
  2230. if (!len)
  2231. break;
  2232. snprintf(evname, len + 1, "%s", p);
  2233. match = 0;
  2234. evlist__for_each_entry(session->evlist, pos) {
  2235. if (!strcmp(perf_evsel__name(pos), evname)) {
  2236. match = 1;
  2237. break;
  2238. }
  2239. }
  2240. if (!match) {
  2241. fclose(fp);
  2242. return -1;
  2243. }
  2244. }
  2245. }
  2246. fclose(fp);
  2247. return 0;
  2248. }
  2249. /*
  2250. * Return -1 if none is found, otherwise the actual scripts number.
  2251. *
  2252. * Currently the only user of this function is the script browser, which
  2253. * will list all statically runnable scripts, select one, execute it and
  2254. * show the output in a perf browser.
  2255. */
  2256. int find_scripts(char **scripts_array, char **scripts_path_array)
  2257. {
  2258. struct dirent *script_dirent, *lang_dirent;
  2259. char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
  2260. DIR *scripts_dir, *lang_dir;
  2261. struct perf_session *session;
  2262. struct perf_data data = {
  2263. .file = {
  2264. .path = input_name,
  2265. },
  2266. .mode = PERF_DATA_MODE_READ,
  2267. };
  2268. char *temp;
  2269. int i = 0;
  2270. session = perf_session__new(&data, false, NULL);
  2271. if (!session)
  2272. return -1;
  2273. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
  2274. scripts_dir = opendir(scripts_path);
  2275. if (!scripts_dir) {
  2276. perf_session__delete(session);
  2277. return -1;
  2278. }
  2279. for_each_lang(scripts_path, scripts_dir, lang_dirent) {
  2280. snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
  2281. lang_dirent->d_name);
  2282. #ifdef NO_LIBPERL
  2283. if (strstr(lang_path, "perl"))
  2284. continue;
  2285. #endif
  2286. #ifdef NO_LIBPYTHON
  2287. if (strstr(lang_path, "python"))
  2288. continue;
  2289. #endif
  2290. lang_dir = opendir(lang_path);
  2291. if (!lang_dir)
  2292. continue;
  2293. for_each_script(lang_path, lang_dir, script_dirent) {
  2294. /* Skip those real time scripts: xxxtop.p[yl] */
  2295. if (strstr(script_dirent->d_name, "top."))
  2296. continue;
  2297. sprintf(scripts_path_array[i], "%s/%s", lang_path,
  2298. script_dirent->d_name);
  2299. temp = strchr(script_dirent->d_name, '.');
  2300. snprintf(scripts_array[i],
  2301. (temp - script_dirent->d_name) + 1,
  2302. "%s", script_dirent->d_name);
  2303. if (check_ev_match(lang_path,
  2304. scripts_array[i], session))
  2305. continue;
  2306. i++;
  2307. }
  2308. closedir(lang_dir);
  2309. }
  2310. closedir(scripts_dir);
  2311. perf_session__delete(session);
  2312. return i;
  2313. }
  2314. static char *get_script_path(const char *script_root, const char *suffix)
  2315. {
  2316. struct dirent *script_dirent, *lang_dirent;
  2317. char scripts_path[MAXPATHLEN];
  2318. char script_path[MAXPATHLEN];
  2319. DIR *scripts_dir, *lang_dir;
  2320. char lang_path[MAXPATHLEN];
  2321. char *__script_root;
  2322. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path());
  2323. scripts_dir = opendir(scripts_path);
  2324. if (!scripts_dir)
  2325. return NULL;
  2326. for_each_lang(scripts_path, scripts_dir, lang_dirent) {
  2327. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  2328. lang_dirent->d_name);
  2329. lang_dir = opendir(lang_path);
  2330. if (!lang_dir)
  2331. continue;
  2332. for_each_script(lang_path, lang_dir, script_dirent) {
  2333. __script_root = get_script_root(script_dirent, suffix);
  2334. if (__script_root && !strcmp(script_root, __script_root)) {
  2335. free(__script_root);
  2336. closedir(lang_dir);
  2337. closedir(scripts_dir);
  2338. snprintf(script_path, MAXPATHLEN, "%s/%s",
  2339. lang_path, script_dirent->d_name);
  2340. return strdup(script_path);
  2341. }
  2342. free(__script_root);
  2343. }
  2344. closedir(lang_dir);
  2345. }
  2346. closedir(scripts_dir);
  2347. return NULL;
  2348. }
  2349. static bool is_top_script(const char *script_path)
  2350. {
  2351. return ends_with(script_path, "top") == NULL ? false : true;
  2352. }
  2353. static int has_required_arg(char *script_path)
  2354. {
  2355. struct script_desc *desc;
  2356. int n_args = 0;
  2357. char *p;
  2358. desc = script_desc__new(NULL);
  2359. if (read_script_info(desc, script_path))
  2360. goto out;
  2361. if (!desc->args)
  2362. goto out;
  2363. for (p = desc->args; *p; p++)
  2364. if (*p == '<')
  2365. n_args++;
  2366. out:
  2367. script_desc__delete(desc);
  2368. return n_args;
  2369. }
  2370. static int have_cmd(int argc, const char **argv)
  2371. {
  2372. char **__argv = malloc(sizeof(const char *) * argc);
  2373. if (!__argv) {
  2374. pr_err("malloc failed\n");
  2375. return -1;
  2376. }
  2377. memcpy(__argv, argv, sizeof(const char *) * argc);
  2378. argc = parse_options(argc, (const char **)__argv, record_options,
  2379. NULL, PARSE_OPT_STOP_AT_NON_OPTION);
  2380. free(__argv);
  2381. system_wide = (argc == 0);
  2382. return 0;
  2383. }
  2384. static void script__setup_sample_type(struct perf_script *script)
  2385. {
  2386. struct perf_session *session = script->session;
  2387. u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
  2388. if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
  2389. if ((sample_type & PERF_SAMPLE_REGS_USER) &&
  2390. (sample_type & PERF_SAMPLE_STACK_USER))
  2391. callchain_param.record_mode = CALLCHAIN_DWARF;
  2392. else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
  2393. callchain_param.record_mode = CALLCHAIN_LBR;
  2394. else
  2395. callchain_param.record_mode = CALLCHAIN_FP;
  2396. }
  2397. }
  2398. static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
  2399. union perf_event *event,
  2400. struct perf_session *session)
  2401. {
  2402. struct stat_round_event *round = &event->stat_round;
  2403. struct perf_evsel *counter;
  2404. evlist__for_each_entry(session->evlist, counter) {
  2405. perf_stat_process_counter(&stat_config, counter);
  2406. process_stat(counter, round->time);
  2407. }
  2408. process_stat_interval(round->time);
  2409. return 0;
  2410. }
  2411. static int process_stat_config_event(struct perf_tool *tool __maybe_unused,
  2412. union perf_event *event,
  2413. struct perf_session *session __maybe_unused)
  2414. {
  2415. perf_event__read_stat_config(&stat_config, &event->stat_config);
  2416. return 0;
  2417. }
  2418. static int set_maps(struct perf_script *script)
  2419. {
  2420. struct perf_evlist *evlist = script->session->evlist;
  2421. if (!script->cpus || !script->threads)
  2422. return 0;
  2423. if (WARN_ONCE(script->allocated, "stats double allocation\n"))
  2424. return -EINVAL;
  2425. perf_evlist__set_maps(evlist, script->cpus, script->threads);
  2426. if (perf_evlist__alloc_stats(evlist, true))
  2427. return -ENOMEM;
  2428. script->allocated = true;
  2429. return 0;
  2430. }
  2431. static
  2432. int process_thread_map_event(struct perf_tool *tool,
  2433. union perf_event *event,
  2434. struct perf_session *session __maybe_unused)
  2435. {
  2436. struct perf_script *script = container_of(tool, struct perf_script, tool);
  2437. if (script->threads) {
  2438. pr_warning("Extra thread map event, ignoring.\n");
  2439. return 0;
  2440. }
  2441. script->threads = thread_map__new_event(&event->thread_map);
  2442. if (!script->threads)
  2443. return -ENOMEM;
  2444. return set_maps(script);
  2445. }
  2446. static
  2447. int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
  2448. union perf_event *event,
  2449. struct perf_session *session __maybe_unused)
  2450. {
  2451. struct perf_script *script = container_of(tool, struct perf_script, tool);
  2452. if (script->cpus) {
  2453. pr_warning("Extra cpu map event, ignoring.\n");
  2454. return 0;
  2455. }
  2456. script->cpus = cpu_map__new_data(&event->cpu_map.data);
  2457. if (!script->cpus)
  2458. return -ENOMEM;
  2459. return set_maps(script);
  2460. }
  2461. #ifdef HAVE_AUXTRACE_SUPPORT
  2462. static int perf_script__process_auxtrace_info(struct perf_tool *tool,
  2463. union perf_event *event,
  2464. struct perf_session *session)
  2465. {
  2466. int ret = perf_event__process_auxtrace_info(tool, event, session);
  2467. if (ret == 0) {
  2468. struct perf_script *script = container_of(tool, struct perf_script, tool);
  2469. ret = perf_script__setup_per_event_dump(script);
  2470. }
  2471. return ret;
  2472. }
  2473. #else
  2474. #define perf_script__process_auxtrace_info 0
  2475. #endif
  2476. int cmd_script(int argc, const char **argv)
  2477. {
  2478. bool show_full_info = false;
  2479. bool header = false;
  2480. bool header_only = false;
  2481. bool script_started = false;
  2482. char *rec_script_path = NULL;
  2483. char *rep_script_path = NULL;
  2484. struct perf_session *session;
  2485. struct itrace_synth_opts itrace_synth_opts = { .set = false, };
  2486. char *script_path = NULL;
  2487. const char **__argv;
  2488. int i, j, err = 0;
  2489. struct perf_script script = {
  2490. .tool = {
  2491. .sample = process_sample_event,
  2492. .mmap = perf_event__process_mmap,
  2493. .mmap2 = perf_event__process_mmap2,
  2494. .comm = perf_event__process_comm,
  2495. .namespaces = perf_event__process_namespaces,
  2496. .exit = perf_event__process_exit,
  2497. .fork = perf_event__process_fork,
  2498. .attr = process_attr,
  2499. .event_update = perf_event__process_event_update,
  2500. .tracing_data = perf_event__process_tracing_data,
  2501. .feature = perf_event__process_feature,
  2502. .build_id = perf_event__process_build_id,
  2503. .id_index = perf_event__process_id_index,
  2504. .auxtrace_info = perf_script__process_auxtrace_info,
  2505. .auxtrace = perf_event__process_auxtrace,
  2506. .auxtrace_error = perf_event__process_auxtrace_error,
  2507. .stat = perf_event__process_stat_event,
  2508. .stat_round = process_stat_round_event,
  2509. .stat_config = process_stat_config_event,
  2510. .thread_map = process_thread_map_event,
  2511. .cpu_map = process_cpu_map_event,
  2512. .ordered_events = true,
  2513. .ordering_requires_timestamps = true,
  2514. },
  2515. };
  2516. struct perf_data data = {
  2517. .mode = PERF_DATA_MODE_READ,
  2518. };
  2519. const struct option options[] = {
  2520. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  2521. "dump raw trace in ASCII"),
  2522. OPT_INCR('v', "verbose", &verbose,
  2523. "be more verbose (show symbol address, etc)"),
  2524. OPT_BOOLEAN('L', "Latency", &latency_format,
  2525. "show latency attributes (irqs/preemption disabled, etc)"),
  2526. OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
  2527. list_available_scripts),
  2528. OPT_CALLBACK('s', "script", NULL, "name",
  2529. "script file name (lang:script name, script name, or *)",
  2530. parse_scriptname),
  2531. OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
  2532. "generate perf-script.xx script in specified language"),
  2533. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  2534. OPT_BOOLEAN('d', "debug-mode", &debug_mode,
  2535. "do various checks like samples ordering and lost events"),
  2536. OPT_BOOLEAN(0, "header", &header, "Show data header."),
  2537. OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
  2538. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  2539. "file", "vmlinux pathname"),
  2540. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  2541. "file", "kallsyms pathname"),
  2542. OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
  2543. "When printing symbols do not display call chain"),
  2544. OPT_CALLBACK(0, "symfs", NULL, "directory",
  2545. "Look for files with symbols relative to this directory",
  2546. symbol__config_symfs),
  2547. OPT_CALLBACK('F', "fields", NULL, "str",
  2548. "comma separated output fields prepend with 'type:'. "
  2549. "+field to add and -field to remove."
  2550. "Valid types: hw,sw,trace,raw,synth. "
  2551. "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
  2552. "addr,symoff,period,iregs,uregs,brstack,brstacksym,flags,"
  2553. "bpf-output,callindent,insn,insnlen,brstackinsn,synth,phys_addr",
  2554. parse_output_fields),
  2555. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  2556. "system-wide collection from all CPUs"),
  2557. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  2558. "only consider these symbols"),
  2559. OPT_STRING(0, "stop-bt", &symbol_conf.bt_stop_list_str, "symbol[,symbol...]",
  2560. "Stop display of callgraph at these symbols"),
  2561. OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
  2562. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  2563. "only display events for these comms"),
  2564. OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
  2565. "only consider symbols in these pids"),
  2566. OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
  2567. "only consider symbols in these tids"),
  2568. OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
  2569. "Set the maximum stack depth when parsing the callchain, "
  2570. "anything beyond the specified depth will be ignored. "
  2571. "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
  2572. OPT_BOOLEAN('I', "show-info", &show_full_info,
  2573. "display extended information from perf.data file"),
  2574. OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
  2575. "Show the path of [kernel.kallsyms]"),
  2576. OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
  2577. "Show the fork/comm/exit events"),
  2578. OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
  2579. "Show the mmap events"),
  2580. OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
  2581. "Show context switch events (if recorded)"),
  2582. OPT_BOOLEAN('\0', "show-namespace-events", &script.show_namespace_events,
  2583. "Show namespace events (if recorded)"),
  2584. OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
  2585. "Dump trace output to files named by the monitored events"),
  2586. OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
  2587. OPT_INTEGER(0, "max-blocks", &max_blocks,
  2588. "Maximum number of code blocks to dump with brstackinsn"),
  2589. OPT_BOOLEAN(0, "ns", &nanosecs,
  2590. "Use 9 decimal places when displaying time"),
  2591. OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
  2592. "Instruction Tracing options",
  2593. itrace_parse_synth_opts),
  2594. OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
  2595. "Show full source file name path for source lines"),
  2596. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  2597. "Enable symbol demangling"),
  2598. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  2599. "Enable kernel symbol demangling"),
  2600. OPT_STRING(0, "time", &script.time_str, "str",
  2601. "Time span of interest (start,stop)"),
  2602. OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
  2603. "Show inline function"),
  2604. OPT_END()
  2605. };
  2606. const char * const script_subcommands[] = { "record", "report", NULL };
  2607. const char *script_usage[] = {
  2608. "perf script [<options>]",
  2609. "perf script [<options>] record <script> [<record-options>] <command>",
  2610. "perf script [<options>] report <script> [script-args]",
  2611. "perf script [<options>] <script> [<record-options>] <command>",
  2612. "perf script [<options>] <top-script> [script-args]",
  2613. NULL
  2614. };
  2615. perf_set_singlethreaded();
  2616. setup_scripting();
  2617. argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
  2618. PARSE_OPT_STOP_AT_NON_OPTION);
  2619. data.file.path = input_name;
  2620. data.force = symbol_conf.force;
  2621. if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
  2622. rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
  2623. if (!rec_script_path)
  2624. return cmd_record(argc, argv);
  2625. }
  2626. if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
  2627. rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
  2628. if (!rep_script_path) {
  2629. fprintf(stderr,
  2630. "Please specify a valid report script"
  2631. "(see 'perf script -l' for listing)\n");
  2632. return -1;
  2633. }
  2634. }
  2635. if (itrace_synth_opts.callchain &&
  2636. itrace_synth_opts.callchain_sz > scripting_max_stack)
  2637. scripting_max_stack = itrace_synth_opts.callchain_sz;
  2638. /* make sure PERF_EXEC_PATH is set for scripts */
  2639. set_argv_exec_path(get_argv_exec_path());
  2640. if (argc && !script_name && !rec_script_path && !rep_script_path) {
  2641. int live_pipe[2];
  2642. int rep_args;
  2643. pid_t pid;
  2644. rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
  2645. rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
  2646. if (!rec_script_path && !rep_script_path) {
  2647. usage_with_options_msg(script_usage, options,
  2648. "Couldn't find script `%s'\n\n See perf"
  2649. " script -l for available scripts.\n", argv[0]);
  2650. }
  2651. if (is_top_script(argv[0])) {
  2652. rep_args = argc - 1;
  2653. } else {
  2654. int rec_args;
  2655. rep_args = has_required_arg(rep_script_path);
  2656. rec_args = (argc - 1) - rep_args;
  2657. if (rec_args < 0) {
  2658. usage_with_options_msg(script_usage, options,
  2659. "`%s' script requires options."
  2660. "\n\n See perf script -l for available "
  2661. "scripts and options.\n", argv[0]);
  2662. }
  2663. }
  2664. if (pipe(live_pipe) < 0) {
  2665. perror("failed to create pipe");
  2666. return -1;
  2667. }
  2668. pid = fork();
  2669. if (pid < 0) {
  2670. perror("failed to fork");
  2671. return -1;
  2672. }
  2673. if (!pid) {
  2674. j = 0;
  2675. dup2(live_pipe[1], 1);
  2676. close(live_pipe[0]);
  2677. if (is_top_script(argv[0])) {
  2678. system_wide = true;
  2679. } else if (!system_wide) {
  2680. if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
  2681. err = -1;
  2682. goto out;
  2683. }
  2684. }
  2685. __argv = malloc((argc + 6) * sizeof(const char *));
  2686. if (!__argv) {
  2687. pr_err("malloc failed\n");
  2688. err = -ENOMEM;
  2689. goto out;
  2690. }
  2691. __argv[j++] = "/bin/sh";
  2692. __argv[j++] = rec_script_path;
  2693. if (system_wide)
  2694. __argv[j++] = "-a";
  2695. __argv[j++] = "-q";
  2696. __argv[j++] = "-o";
  2697. __argv[j++] = "-";
  2698. for (i = rep_args + 1; i < argc; i++)
  2699. __argv[j++] = argv[i];
  2700. __argv[j++] = NULL;
  2701. execvp("/bin/sh", (char **)__argv);
  2702. free(__argv);
  2703. exit(-1);
  2704. }
  2705. dup2(live_pipe[0], 0);
  2706. close(live_pipe[1]);
  2707. __argv = malloc((argc + 4) * sizeof(const char *));
  2708. if (!__argv) {
  2709. pr_err("malloc failed\n");
  2710. err = -ENOMEM;
  2711. goto out;
  2712. }
  2713. j = 0;
  2714. __argv[j++] = "/bin/sh";
  2715. __argv[j++] = rep_script_path;
  2716. for (i = 1; i < rep_args + 1; i++)
  2717. __argv[j++] = argv[i];
  2718. __argv[j++] = "-i";
  2719. __argv[j++] = "-";
  2720. __argv[j++] = NULL;
  2721. execvp("/bin/sh", (char **)__argv);
  2722. free(__argv);
  2723. exit(-1);
  2724. }
  2725. if (rec_script_path)
  2726. script_path = rec_script_path;
  2727. if (rep_script_path)
  2728. script_path = rep_script_path;
  2729. if (script_path) {
  2730. j = 0;
  2731. if (!rec_script_path)
  2732. system_wide = false;
  2733. else if (!system_wide) {
  2734. if (have_cmd(argc - 1, &argv[1]) != 0) {
  2735. err = -1;
  2736. goto out;
  2737. }
  2738. }
  2739. __argv = malloc((argc + 2) * sizeof(const char *));
  2740. if (!__argv) {
  2741. pr_err("malloc failed\n");
  2742. err = -ENOMEM;
  2743. goto out;
  2744. }
  2745. __argv[j++] = "/bin/sh";
  2746. __argv[j++] = script_path;
  2747. if (system_wide)
  2748. __argv[j++] = "-a";
  2749. for (i = 2; i < argc; i++)
  2750. __argv[j++] = argv[i];
  2751. __argv[j++] = NULL;
  2752. execvp("/bin/sh", (char **)__argv);
  2753. free(__argv);
  2754. exit(-1);
  2755. }
  2756. if (!script_name)
  2757. setup_pager();
  2758. session = perf_session__new(&data, false, &script.tool);
  2759. if (session == NULL)
  2760. return -1;
  2761. if (header || header_only) {
  2762. script.tool.show_feat_hdr = SHOW_FEAT_HEADER;
  2763. perf_session__fprintf_info(session, stdout, show_full_info);
  2764. if (header_only)
  2765. goto out_delete;
  2766. }
  2767. if (show_full_info)
  2768. script.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
  2769. if (symbol__init(&session->header.env) < 0)
  2770. goto out_delete;
  2771. script.session = session;
  2772. script__setup_sample_type(&script);
  2773. if (output[PERF_TYPE_HARDWARE].fields & PERF_OUTPUT_CALLINDENT)
  2774. itrace_synth_opts.thread_stack = true;
  2775. session->itrace_synth_opts = &itrace_synth_opts;
  2776. if (cpu_list) {
  2777. err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
  2778. if (err < 0)
  2779. goto out_delete;
  2780. itrace_synth_opts.cpu_bitmap = cpu_bitmap;
  2781. }
  2782. if (!no_callchain)
  2783. symbol_conf.use_callchain = true;
  2784. else
  2785. symbol_conf.use_callchain = false;
  2786. if (session->tevent.pevent &&
  2787. pevent_set_function_resolver(session->tevent.pevent,
  2788. machine__resolve_kernel_addr,
  2789. &session->machines.host) < 0) {
  2790. pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
  2791. err = -1;
  2792. goto out_delete;
  2793. }
  2794. if (generate_script_lang) {
  2795. struct stat perf_stat;
  2796. int input;
  2797. if (output_set_by_user()) {
  2798. fprintf(stderr,
  2799. "custom fields not supported for generated scripts");
  2800. err = -EINVAL;
  2801. goto out_delete;
  2802. }
  2803. input = open(data.file.path, O_RDONLY); /* input_name */
  2804. if (input < 0) {
  2805. err = -errno;
  2806. perror("failed to open file");
  2807. goto out_delete;
  2808. }
  2809. err = fstat(input, &perf_stat);
  2810. if (err < 0) {
  2811. perror("failed to stat file");
  2812. goto out_delete;
  2813. }
  2814. if (!perf_stat.st_size) {
  2815. fprintf(stderr, "zero-sized file, nothing to do!\n");
  2816. goto out_delete;
  2817. }
  2818. scripting_ops = script_spec__lookup(generate_script_lang);
  2819. if (!scripting_ops) {
  2820. fprintf(stderr, "invalid language specifier");
  2821. err = -ENOENT;
  2822. goto out_delete;
  2823. }
  2824. err = scripting_ops->generate_script(session->tevent.pevent,
  2825. "perf-script");
  2826. goto out_delete;
  2827. }
  2828. if (script_name) {
  2829. err = scripting_ops->start_script(script_name, argc, argv);
  2830. if (err)
  2831. goto out_delete;
  2832. pr_debug("perf script started with script %s\n\n", script_name);
  2833. script_started = true;
  2834. }
  2835. err = perf_session__check_output_opt(session);
  2836. if (err < 0)
  2837. goto out_delete;
  2838. /* needs to be parsed after looking up reference time */
  2839. if (perf_time__parse_str(&script.ptime, script.time_str) != 0) {
  2840. pr_err("Invalid time string\n");
  2841. err = -EINVAL;
  2842. goto out_delete;
  2843. }
  2844. err = __cmd_script(&script);
  2845. flush_scripting();
  2846. out_delete:
  2847. perf_evlist__free_stats(session->evlist);
  2848. perf_session__delete(session);
  2849. if (script_started)
  2850. cleanup_scripting();
  2851. out:
  2852. return err;
  2853. }