builtin-script.c 78 KB

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