builtin-script.c 70 KB

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