builtin-script.c 82 KB

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