builtin-script.c 74 KB

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