builtin-script.c 70 KB

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