builtin-script.c 71 KB

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