builtin-script.c 70 KB

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