builtin-script.c 70 KB

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