builtin-script.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990
  1. #include "builtin.h"
  2. #include "perf.h"
  3. #include "util/cache.h"
  4. #include "util/debug.h"
  5. #include "util/exec_cmd.h"
  6. #include "util/header.h"
  7. #include "util/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 <linux/bitmap.h>
  21. static char const *script_name;
  22. static char const *generate_script_lang;
  23. static bool debug_mode;
  24. static u64 last_timestamp;
  25. static u64 nr_unordered;
  26. static bool no_callchain;
  27. static bool latency_format;
  28. static bool system_wide;
  29. static bool print_flags;
  30. static const char *cpu_list;
  31. static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
  32. enum perf_output_field {
  33. PERF_OUTPUT_COMM = 1U << 0,
  34. PERF_OUTPUT_TID = 1U << 1,
  35. PERF_OUTPUT_PID = 1U << 2,
  36. PERF_OUTPUT_TIME = 1U << 3,
  37. PERF_OUTPUT_CPU = 1U << 4,
  38. PERF_OUTPUT_EVNAME = 1U << 5,
  39. PERF_OUTPUT_TRACE = 1U << 6,
  40. PERF_OUTPUT_IP = 1U << 7,
  41. PERF_OUTPUT_SYM = 1U << 8,
  42. PERF_OUTPUT_DSO = 1U << 9,
  43. PERF_OUTPUT_ADDR = 1U << 10,
  44. PERF_OUTPUT_SYMOFFSET = 1U << 11,
  45. PERF_OUTPUT_SRCLINE = 1U << 12,
  46. PERF_OUTPUT_PERIOD = 1U << 13,
  47. PERF_OUTPUT_IREGS = 1U << 14,
  48. };
  49. struct output_option {
  50. const char *str;
  51. enum perf_output_field field;
  52. } all_output_options[] = {
  53. {.str = "comm", .field = PERF_OUTPUT_COMM},
  54. {.str = "tid", .field = PERF_OUTPUT_TID},
  55. {.str = "pid", .field = PERF_OUTPUT_PID},
  56. {.str = "time", .field = PERF_OUTPUT_TIME},
  57. {.str = "cpu", .field = PERF_OUTPUT_CPU},
  58. {.str = "event", .field = PERF_OUTPUT_EVNAME},
  59. {.str = "trace", .field = PERF_OUTPUT_TRACE},
  60. {.str = "ip", .field = PERF_OUTPUT_IP},
  61. {.str = "sym", .field = PERF_OUTPUT_SYM},
  62. {.str = "dso", .field = PERF_OUTPUT_DSO},
  63. {.str = "addr", .field = PERF_OUTPUT_ADDR},
  64. {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
  65. {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
  66. {.str = "period", .field = PERF_OUTPUT_PERIOD},
  67. {.str = "iregs", .field = PERF_OUTPUT_IREGS},
  68. };
  69. /* default set to maintain compatibility with current format */
  70. static struct {
  71. bool user_set;
  72. bool wildcard_set;
  73. unsigned int print_ip_opts;
  74. u64 fields;
  75. u64 invalid_fields;
  76. } output[PERF_TYPE_MAX] = {
  77. [PERF_TYPE_HARDWARE] = {
  78. .user_set = false,
  79. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  80. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  81. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  82. PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
  83. PERF_OUTPUT_PERIOD,
  84. .invalid_fields = PERF_OUTPUT_TRACE,
  85. },
  86. [PERF_TYPE_SOFTWARE] = {
  87. .user_set = false,
  88. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  89. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  90. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  91. PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
  92. PERF_OUTPUT_PERIOD,
  93. .invalid_fields = PERF_OUTPUT_TRACE,
  94. },
  95. [PERF_TYPE_TRACEPOINT] = {
  96. .user_set = false,
  97. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  98. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  99. PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE,
  100. },
  101. [PERF_TYPE_RAW] = {
  102. .user_set = false,
  103. .fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID |
  104. PERF_OUTPUT_CPU | PERF_OUTPUT_TIME |
  105. PERF_OUTPUT_EVNAME | PERF_OUTPUT_IP |
  106. PERF_OUTPUT_SYM | PERF_OUTPUT_DSO |
  107. PERF_OUTPUT_PERIOD,
  108. .invalid_fields = PERF_OUTPUT_TRACE,
  109. },
  110. };
  111. static bool output_set_by_user(void)
  112. {
  113. int j;
  114. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  115. if (output[j].user_set)
  116. return true;
  117. }
  118. return false;
  119. }
  120. static const char *output_field2str(enum perf_output_field field)
  121. {
  122. int i, imax = ARRAY_SIZE(all_output_options);
  123. const char *str = "";
  124. for (i = 0; i < imax; ++i) {
  125. if (all_output_options[i].field == field) {
  126. str = all_output_options[i].str;
  127. break;
  128. }
  129. }
  130. return str;
  131. }
  132. #define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
  133. static int perf_evsel__do_check_stype(struct perf_evsel *evsel,
  134. u64 sample_type, const char *sample_msg,
  135. enum perf_output_field field,
  136. bool allow_user_set)
  137. {
  138. struct perf_event_attr *attr = &evsel->attr;
  139. int type = attr->type;
  140. const char *evname;
  141. if (attr->sample_type & sample_type)
  142. return 0;
  143. if (output[type].user_set) {
  144. if (allow_user_set)
  145. return 0;
  146. evname = perf_evsel__name(evsel);
  147. pr_err("Samples for '%s' event do not have %s attribute set. "
  148. "Cannot print '%s' field.\n",
  149. evname, sample_msg, output_field2str(field));
  150. return -1;
  151. }
  152. /* user did not ask for it explicitly so remove from the default list */
  153. output[type].fields &= ~field;
  154. evname = perf_evsel__name(evsel);
  155. pr_debug("Samples for '%s' event do not have %s attribute set. "
  156. "Skipping '%s' field.\n",
  157. evname, sample_msg, output_field2str(field));
  158. return 0;
  159. }
  160. static int perf_evsel__check_stype(struct perf_evsel *evsel,
  161. u64 sample_type, const char *sample_msg,
  162. enum perf_output_field field)
  163. {
  164. return perf_evsel__do_check_stype(evsel, sample_type, sample_msg, field,
  165. false);
  166. }
  167. static int perf_evsel__check_attr(struct perf_evsel *evsel,
  168. struct perf_session *session)
  169. {
  170. struct perf_event_attr *attr = &evsel->attr;
  171. bool allow_user_set;
  172. allow_user_set = perf_header__has_feat(&session->header,
  173. HEADER_AUXTRACE);
  174. if (PRINT_FIELD(TRACE) &&
  175. !perf_session__has_traces(session, "record -R"))
  176. return -EINVAL;
  177. if (PRINT_FIELD(IP)) {
  178. if (perf_evsel__check_stype(evsel, PERF_SAMPLE_IP, "IP",
  179. PERF_OUTPUT_IP))
  180. return -EINVAL;
  181. }
  182. if (PRINT_FIELD(ADDR) &&
  183. perf_evsel__do_check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
  184. PERF_OUTPUT_ADDR, allow_user_set))
  185. return -EINVAL;
  186. if (PRINT_FIELD(SYM) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
  187. pr_err("Display of symbols requested but neither sample IP nor "
  188. "sample address\nis selected. Hence, no addresses to convert "
  189. "to symbols.\n");
  190. return -EINVAL;
  191. }
  192. if (PRINT_FIELD(SYMOFFSET) && !PRINT_FIELD(SYM)) {
  193. pr_err("Display of offsets requested but symbol is not"
  194. "selected.\n");
  195. return -EINVAL;
  196. }
  197. if (PRINT_FIELD(DSO) && !PRINT_FIELD(IP) && !PRINT_FIELD(ADDR)) {
  198. pr_err("Display of DSO requested but neither sample IP nor "
  199. "sample address\nis selected. Hence, no addresses to convert "
  200. "to DSO.\n");
  201. return -EINVAL;
  202. }
  203. if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
  204. pr_err("Display of source line number requested but sample IP is not\n"
  205. "selected. Hence, no address to lookup the source line number.\n");
  206. return -EINVAL;
  207. }
  208. if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
  209. perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
  210. PERF_OUTPUT_TID|PERF_OUTPUT_PID))
  211. return -EINVAL;
  212. if (PRINT_FIELD(TIME) &&
  213. perf_evsel__check_stype(evsel, PERF_SAMPLE_TIME, "TIME",
  214. PERF_OUTPUT_TIME))
  215. return -EINVAL;
  216. if (PRINT_FIELD(CPU) &&
  217. perf_evsel__do_check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
  218. PERF_OUTPUT_CPU, allow_user_set))
  219. return -EINVAL;
  220. if (PRINT_FIELD(PERIOD) &&
  221. perf_evsel__check_stype(evsel, PERF_SAMPLE_PERIOD, "PERIOD",
  222. PERF_OUTPUT_PERIOD))
  223. return -EINVAL;
  224. if (PRINT_FIELD(IREGS) &&
  225. perf_evsel__check_stype(evsel, PERF_SAMPLE_REGS_INTR, "IREGS",
  226. PERF_OUTPUT_IREGS))
  227. return -EINVAL;
  228. return 0;
  229. }
  230. static void set_print_ip_opts(struct perf_event_attr *attr)
  231. {
  232. unsigned int type = attr->type;
  233. output[type].print_ip_opts = 0;
  234. if (PRINT_FIELD(IP))
  235. output[type].print_ip_opts |= PRINT_IP_OPT_IP;
  236. if (PRINT_FIELD(SYM))
  237. output[type].print_ip_opts |= PRINT_IP_OPT_SYM;
  238. if (PRINT_FIELD(DSO))
  239. output[type].print_ip_opts |= PRINT_IP_OPT_DSO;
  240. if (PRINT_FIELD(SYMOFFSET))
  241. output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET;
  242. if (PRINT_FIELD(SRCLINE))
  243. output[type].print_ip_opts |= PRINT_IP_OPT_SRCLINE;
  244. }
  245. /*
  246. * verify all user requested events exist and the samples
  247. * have the expected data
  248. */
  249. static int perf_session__check_output_opt(struct perf_session *session)
  250. {
  251. int j;
  252. struct perf_evsel *evsel;
  253. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  254. evsel = perf_session__find_first_evtype(session, j);
  255. /*
  256. * even if fields is set to 0 (ie., show nothing) event must
  257. * exist if user explicitly includes it on the command line
  258. */
  259. if (!evsel && output[j].user_set && !output[j].wildcard_set) {
  260. pr_err("%s events do not exist. "
  261. "Remove corresponding -f option to proceed.\n",
  262. event_type(j));
  263. return -1;
  264. }
  265. if (evsel && output[j].fields &&
  266. perf_evsel__check_attr(evsel, session))
  267. return -1;
  268. if (evsel == NULL)
  269. continue;
  270. set_print_ip_opts(&evsel->attr);
  271. }
  272. if (!no_callchain) {
  273. bool use_callchain = false;
  274. evlist__for_each(session->evlist, evsel) {
  275. if (evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
  276. use_callchain = true;
  277. break;
  278. }
  279. }
  280. if (!use_callchain)
  281. symbol_conf.use_callchain = false;
  282. }
  283. /*
  284. * set default for tracepoints to print symbols only
  285. * if callchains are present
  286. */
  287. if (symbol_conf.use_callchain &&
  288. !output[PERF_TYPE_TRACEPOINT].user_set) {
  289. struct perf_event_attr *attr;
  290. j = PERF_TYPE_TRACEPOINT;
  291. evsel = perf_session__find_first_evtype(session, j);
  292. if (evsel == NULL)
  293. goto out;
  294. attr = &evsel->attr;
  295. if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
  296. output[j].fields |= PERF_OUTPUT_IP;
  297. output[j].fields |= PERF_OUTPUT_SYM;
  298. output[j].fields |= PERF_OUTPUT_DSO;
  299. set_print_ip_opts(attr);
  300. }
  301. }
  302. out:
  303. return 0;
  304. }
  305. static void print_sample_iregs(union perf_event *event __maybe_unused,
  306. struct perf_sample *sample,
  307. struct thread *thread __maybe_unused,
  308. struct perf_event_attr *attr)
  309. {
  310. struct regs_dump *regs = &sample->intr_regs;
  311. uint64_t mask = attr->sample_regs_intr;
  312. unsigned i = 0, r;
  313. if (!regs)
  314. return;
  315. for_each_set_bit(r, (unsigned long *) &mask, sizeof(mask) * 8) {
  316. u64 val = regs->regs[i++];
  317. printf("%5s:0x%"PRIx64" ", perf_reg_name(r), val);
  318. }
  319. }
  320. static void print_sample_start(struct perf_sample *sample,
  321. struct thread *thread,
  322. struct perf_evsel *evsel)
  323. {
  324. struct perf_event_attr *attr = &evsel->attr;
  325. unsigned long secs;
  326. unsigned long usecs;
  327. unsigned long long nsecs;
  328. if (PRINT_FIELD(COMM)) {
  329. if (latency_format)
  330. printf("%8.8s ", thread__comm_str(thread));
  331. else if (PRINT_FIELD(IP) && symbol_conf.use_callchain)
  332. printf("%s ", thread__comm_str(thread));
  333. else
  334. printf("%16s ", thread__comm_str(thread));
  335. }
  336. if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
  337. printf("%5d/%-5d ", sample->pid, sample->tid);
  338. else if (PRINT_FIELD(PID))
  339. printf("%5d ", sample->pid);
  340. else if (PRINT_FIELD(TID))
  341. printf("%5d ", sample->tid);
  342. if (PRINT_FIELD(CPU)) {
  343. if (latency_format)
  344. printf("%3d ", sample->cpu);
  345. else
  346. printf("[%03d] ", sample->cpu);
  347. }
  348. if (PRINT_FIELD(TIME)) {
  349. nsecs = sample->time;
  350. secs = nsecs / NSECS_PER_SEC;
  351. nsecs -= secs * NSECS_PER_SEC;
  352. usecs = nsecs / NSECS_PER_USEC;
  353. printf("%5lu.%06lu: ", secs, usecs);
  354. }
  355. }
  356. static void print_sample_addr(union perf_event *event,
  357. struct perf_sample *sample,
  358. struct thread *thread,
  359. struct perf_event_attr *attr)
  360. {
  361. struct addr_location al;
  362. printf("%16" PRIx64, sample->addr);
  363. if (!sample_addr_correlates_sym(attr))
  364. return;
  365. perf_event__preprocess_sample_addr(event, sample, thread, &al);
  366. if (PRINT_FIELD(SYM)) {
  367. printf(" ");
  368. if (PRINT_FIELD(SYMOFFSET))
  369. symbol__fprintf_symname_offs(al.sym, &al, stdout);
  370. else
  371. symbol__fprintf_symname(al.sym, stdout);
  372. }
  373. if (PRINT_FIELD(DSO)) {
  374. printf(" (");
  375. map__fprintf_dsoname(al.map, stdout);
  376. printf(")");
  377. }
  378. }
  379. static void print_sample_bts(union perf_event *event,
  380. struct perf_sample *sample,
  381. struct perf_evsel *evsel,
  382. struct thread *thread,
  383. struct addr_location *al)
  384. {
  385. struct perf_event_attr *attr = &evsel->attr;
  386. bool print_srcline_last = false;
  387. /* print branch_from information */
  388. if (PRINT_FIELD(IP)) {
  389. unsigned int print_opts = output[attr->type].print_ip_opts;
  390. if (symbol_conf.use_callchain && sample->callchain) {
  391. printf("\n");
  392. } else {
  393. printf(" ");
  394. if (print_opts & PRINT_IP_OPT_SRCLINE) {
  395. print_srcline_last = true;
  396. print_opts &= ~PRINT_IP_OPT_SRCLINE;
  397. }
  398. }
  399. perf_evsel__print_ip(evsel, sample, al, print_opts,
  400. PERF_MAX_STACK_DEPTH);
  401. }
  402. /* print branch_to information */
  403. if (PRINT_FIELD(ADDR) ||
  404. ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
  405. !output[attr->type].user_set)) {
  406. printf(" => ");
  407. print_sample_addr(event, sample, thread, attr);
  408. }
  409. if (print_srcline_last)
  410. map__fprintf_srcline(al->map, al->addr, "\n ", stdout);
  411. printf("\n");
  412. }
  413. static void print_sample_flags(u32 flags)
  414. {
  415. const char *chars = PERF_IP_FLAG_CHARS;
  416. const int n = strlen(PERF_IP_FLAG_CHARS);
  417. char str[33];
  418. int i, pos = 0;
  419. for (i = 0; i < n; i++, flags >>= 1) {
  420. if (flags & 1)
  421. str[pos++] = chars[i];
  422. }
  423. for (; i < 32; i++, flags >>= 1) {
  424. if (flags & 1)
  425. str[pos++] = '?';
  426. }
  427. str[pos] = 0;
  428. printf(" %-4s ", str);
  429. }
  430. static void process_event(union perf_event *event, struct perf_sample *sample,
  431. struct perf_evsel *evsel, struct addr_location *al)
  432. {
  433. struct thread *thread = al->thread;
  434. struct perf_event_attr *attr = &evsel->attr;
  435. if (output[attr->type].fields == 0)
  436. return;
  437. print_sample_start(sample, thread, evsel);
  438. if (PRINT_FIELD(PERIOD))
  439. printf("%10" PRIu64 " ", sample->period);
  440. if (PRINT_FIELD(EVNAME)) {
  441. const char *evname = perf_evsel__name(evsel);
  442. printf("%s: ", evname ? evname : "[unknown]");
  443. }
  444. if (print_flags)
  445. print_sample_flags(sample->flags);
  446. if (is_bts_event(attr)) {
  447. print_sample_bts(event, sample, evsel, thread, al);
  448. return;
  449. }
  450. if (PRINT_FIELD(TRACE))
  451. event_format__print(evsel->tp_format, sample->cpu,
  452. sample->raw_data, sample->raw_size);
  453. if (PRINT_FIELD(ADDR))
  454. print_sample_addr(event, sample, thread, attr);
  455. if (PRINT_FIELD(IP)) {
  456. if (!symbol_conf.use_callchain)
  457. printf(" ");
  458. else
  459. printf("\n");
  460. perf_evsel__print_ip(evsel, sample, al,
  461. output[attr->type].print_ip_opts,
  462. PERF_MAX_STACK_DEPTH);
  463. }
  464. if (PRINT_FIELD(IREGS))
  465. print_sample_iregs(event, sample, thread, attr);
  466. printf("\n");
  467. }
  468. static int default_start_script(const char *script __maybe_unused,
  469. int argc __maybe_unused,
  470. const char **argv __maybe_unused)
  471. {
  472. return 0;
  473. }
  474. static int default_flush_script(void)
  475. {
  476. return 0;
  477. }
  478. static int default_stop_script(void)
  479. {
  480. return 0;
  481. }
  482. static int default_generate_script(struct pevent *pevent __maybe_unused,
  483. const char *outfile __maybe_unused)
  484. {
  485. return 0;
  486. }
  487. static struct scripting_ops default_scripting_ops = {
  488. .start_script = default_start_script,
  489. .flush_script = default_flush_script,
  490. .stop_script = default_stop_script,
  491. .process_event = process_event,
  492. .generate_script = default_generate_script,
  493. };
  494. static struct scripting_ops *scripting_ops;
  495. static void setup_scripting(void)
  496. {
  497. setup_perl_scripting();
  498. setup_python_scripting();
  499. scripting_ops = &default_scripting_ops;
  500. }
  501. static int flush_scripting(void)
  502. {
  503. return scripting_ops->flush_script();
  504. }
  505. static int cleanup_scripting(void)
  506. {
  507. pr_debug("\nperf script stopped\n");
  508. return scripting_ops->stop_script();
  509. }
  510. static int process_sample_event(struct perf_tool *tool __maybe_unused,
  511. union perf_event *event,
  512. struct perf_sample *sample,
  513. struct perf_evsel *evsel,
  514. struct machine *machine)
  515. {
  516. struct addr_location al;
  517. if (debug_mode) {
  518. if (sample->time < last_timestamp) {
  519. pr_err("Samples misordered, previous: %" PRIu64
  520. " this: %" PRIu64 "\n", last_timestamp,
  521. sample->time);
  522. nr_unordered++;
  523. }
  524. last_timestamp = sample->time;
  525. return 0;
  526. }
  527. if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
  528. pr_err("problem processing %d event, skipping it.\n",
  529. event->header.type);
  530. return -1;
  531. }
  532. if (al.filtered)
  533. goto out_put;
  534. if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
  535. goto out_put;
  536. scripting_ops->process_event(event, sample, evsel, &al);
  537. out_put:
  538. addr_location__put(&al);
  539. return 0;
  540. }
  541. struct perf_script {
  542. struct perf_tool tool;
  543. struct perf_session *session;
  544. bool show_task_events;
  545. bool show_mmap_events;
  546. bool show_switch_events;
  547. };
  548. static int process_attr(struct perf_tool *tool, union perf_event *event,
  549. struct perf_evlist **pevlist)
  550. {
  551. struct perf_script *scr = container_of(tool, struct perf_script, tool);
  552. struct perf_evlist *evlist;
  553. struct perf_evsel *evsel, *pos;
  554. int err;
  555. err = perf_event__process_attr(tool, event, pevlist);
  556. if (err)
  557. return err;
  558. evlist = *pevlist;
  559. evsel = perf_evlist__last(*pevlist);
  560. if (evsel->attr.type >= PERF_TYPE_MAX)
  561. return 0;
  562. evlist__for_each(evlist, pos) {
  563. if (pos->attr.type == evsel->attr.type && pos != evsel)
  564. return 0;
  565. }
  566. set_print_ip_opts(&evsel->attr);
  567. return perf_evsel__check_attr(evsel, scr->session);
  568. }
  569. static int process_comm_event(struct perf_tool *tool,
  570. union perf_event *event,
  571. struct perf_sample *sample,
  572. struct machine *machine)
  573. {
  574. struct thread *thread;
  575. struct perf_script *script = container_of(tool, struct perf_script, tool);
  576. struct perf_session *session = script->session;
  577. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  578. int ret = -1;
  579. thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
  580. if (thread == NULL) {
  581. pr_debug("problem processing COMM event, skipping it.\n");
  582. return -1;
  583. }
  584. if (perf_event__process_comm(tool, event, sample, machine) < 0)
  585. goto out;
  586. if (!evsel->attr.sample_id_all) {
  587. sample->cpu = 0;
  588. sample->time = 0;
  589. sample->tid = event->comm.tid;
  590. sample->pid = event->comm.pid;
  591. }
  592. print_sample_start(sample, thread, evsel);
  593. perf_event__fprintf(event, stdout);
  594. ret = 0;
  595. out:
  596. thread__put(thread);
  597. return ret;
  598. }
  599. static int process_fork_event(struct perf_tool *tool,
  600. union perf_event *event,
  601. struct perf_sample *sample,
  602. struct machine *machine)
  603. {
  604. struct thread *thread;
  605. struct perf_script *script = container_of(tool, struct perf_script, tool);
  606. struct perf_session *session = script->session;
  607. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  608. if (perf_event__process_fork(tool, event, sample, machine) < 0)
  609. return -1;
  610. thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
  611. if (thread == NULL) {
  612. pr_debug("problem processing FORK event, skipping it.\n");
  613. return -1;
  614. }
  615. if (!evsel->attr.sample_id_all) {
  616. sample->cpu = 0;
  617. sample->time = event->fork.time;
  618. sample->tid = event->fork.tid;
  619. sample->pid = event->fork.pid;
  620. }
  621. print_sample_start(sample, thread, evsel);
  622. perf_event__fprintf(event, stdout);
  623. thread__put(thread);
  624. return 0;
  625. }
  626. static int process_exit_event(struct perf_tool *tool,
  627. union perf_event *event,
  628. struct perf_sample *sample,
  629. struct machine *machine)
  630. {
  631. int err = 0;
  632. struct thread *thread;
  633. struct perf_script *script = container_of(tool, struct perf_script, tool);
  634. struct perf_session *session = script->session;
  635. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  636. thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
  637. if (thread == NULL) {
  638. pr_debug("problem processing EXIT event, skipping it.\n");
  639. return -1;
  640. }
  641. if (!evsel->attr.sample_id_all) {
  642. sample->cpu = 0;
  643. sample->time = 0;
  644. sample->tid = event->fork.tid;
  645. sample->pid = event->fork.pid;
  646. }
  647. print_sample_start(sample, thread, evsel);
  648. perf_event__fprintf(event, stdout);
  649. if (perf_event__process_exit(tool, event, sample, machine) < 0)
  650. err = -1;
  651. thread__put(thread);
  652. return err;
  653. }
  654. static int process_mmap_event(struct perf_tool *tool,
  655. union perf_event *event,
  656. struct perf_sample *sample,
  657. struct machine *machine)
  658. {
  659. struct thread *thread;
  660. struct perf_script *script = container_of(tool, struct perf_script, tool);
  661. struct perf_session *session = script->session;
  662. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  663. if (perf_event__process_mmap(tool, event, sample, machine) < 0)
  664. return -1;
  665. thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
  666. if (thread == NULL) {
  667. pr_debug("problem processing MMAP event, skipping it.\n");
  668. return -1;
  669. }
  670. if (!evsel->attr.sample_id_all) {
  671. sample->cpu = 0;
  672. sample->time = 0;
  673. sample->tid = event->mmap.tid;
  674. sample->pid = event->mmap.pid;
  675. }
  676. print_sample_start(sample, thread, evsel);
  677. perf_event__fprintf(event, stdout);
  678. thread__put(thread);
  679. return 0;
  680. }
  681. static int process_mmap2_event(struct perf_tool *tool,
  682. union perf_event *event,
  683. struct perf_sample *sample,
  684. struct machine *machine)
  685. {
  686. struct thread *thread;
  687. struct perf_script *script = container_of(tool, struct perf_script, tool);
  688. struct perf_session *session = script->session;
  689. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  690. if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
  691. return -1;
  692. thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
  693. if (thread == NULL) {
  694. pr_debug("problem processing MMAP2 event, skipping it.\n");
  695. return -1;
  696. }
  697. if (!evsel->attr.sample_id_all) {
  698. sample->cpu = 0;
  699. sample->time = 0;
  700. sample->tid = event->mmap2.tid;
  701. sample->pid = event->mmap2.pid;
  702. }
  703. print_sample_start(sample, thread, evsel);
  704. perf_event__fprintf(event, stdout);
  705. thread__put(thread);
  706. return 0;
  707. }
  708. static int process_switch_event(struct perf_tool *tool,
  709. union perf_event *event,
  710. struct perf_sample *sample,
  711. struct machine *machine)
  712. {
  713. struct thread *thread;
  714. struct perf_script *script = container_of(tool, struct perf_script, tool);
  715. struct perf_session *session = script->session;
  716. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  717. if (perf_event__process_switch(tool, event, sample, machine) < 0)
  718. return -1;
  719. thread = machine__findnew_thread(machine, sample->pid,
  720. sample->tid);
  721. if (thread == NULL) {
  722. pr_debug("problem processing SWITCH event, skipping it.\n");
  723. return -1;
  724. }
  725. print_sample_start(sample, thread, evsel);
  726. perf_event__fprintf(event, stdout);
  727. thread__put(thread);
  728. return 0;
  729. }
  730. static void sig_handler(int sig __maybe_unused)
  731. {
  732. session_done = 1;
  733. }
  734. static int __cmd_script(struct perf_script *script)
  735. {
  736. int ret;
  737. signal(SIGINT, sig_handler);
  738. /* override event processing functions */
  739. if (script->show_task_events) {
  740. script->tool.comm = process_comm_event;
  741. script->tool.fork = process_fork_event;
  742. script->tool.exit = process_exit_event;
  743. }
  744. if (script->show_mmap_events) {
  745. script->tool.mmap = process_mmap_event;
  746. script->tool.mmap2 = process_mmap2_event;
  747. }
  748. if (script->show_switch_events)
  749. script->tool.context_switch = process_switch_event;
  750. ret = perf_session__process_events(script->session);
  751. if (debug_mode)
  752. pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
  753. return ret;
  754. }
  755. struct script_spec {
  756. struct list_head node;
  757. struct scripting_ops *ops;
  758. char spec[0];
  759. };
  760. static LIST_HEAD(script_specs);
  761. static struct script_spec *script_spec__new(const char *spec,
  762. struct scripting_ops *ops)
  763. {
  764. struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
  765. if (s != NULL) {
  766. strcpy(s->spec, spec);
  767. s->ops = ops;
  768. }
  769. return s;
  770. }
  771. static void script_spec__add(struct script_spec *s)
  772. {
  773. list_add_tail(&s->node, &script_specs);
  774. }
  775. static struct script_spec *script_spec__find(const char *spec)
  776. {
  777. struct script_spec *s;
  778. list_for_each_entry(s, &script_specs, node)
  779. if (strcasecmp(s->spec, spec) == 0)
  780. return s;
  781. return NULL;
  782. }
  783. static struct script_spec *script_spec__findnew(const char *spec,
  784. struct scripting_ops *ops)
  785. {
  786. struct script_spec *s = script_spec__find(spec);
  787. if (s)
  788. return s;
  789. s = script_spec__new(spec, ops);
  790. if (!s)
  791. return NULL;
  792. script_spec__add(s);
  793. return s;
  794. }
  795. int script_spec_register(const char *spec, struct scripting_ops *ops)
  796. {
  797. struct script_spec *s;
  798. s = script_spec__find(spec);
  799. if (s)
  800. return -1;
  801. s = script_spec__findnew(spec, ops);
  802. if (!s)
  803. return -1;
  804. return 0;
  805. }
  806. static struct scripting_ops *script_spec__lookup(const char *spec)
  807. {
  808. struct script_spec *s = script_spec__find(spec);
  809. if (!s)
  810. return NULL;
  811. return s->ops;
  812. }
  813. static void list_available_languages(void)
  814. {
  815. struct script_spec *s;
  816. fprintf(stderr, "\n");
  817. fprintf(stderr, "Scripting language extensions (used in "
  818. "perf script -s [spec:]script.[spec]):\n\n");
  819. list_for_each_entry(s, &script_specs, node)
  820. fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
  821. fprintf(stderr, "\n");
  822. }
  823. static int parse_scriptname(const struct option *opt __maybe_unused,
  824. const char *str, int unset __maybe_unused)
  825. {
  826. char spec[PATH_MAX];
  827. const char *script, *ext;
  828. int len;
  829. if (strcmp(str, "lang") == 0) {
  830. list_available_languages();
  831. exit(0);
  832. }
  833. script = strchr(str, ':');
  834. if (script) {
  835. len = script - str;
  836. if (len >= PATH_MAX) {
  837. fprintf(stderr, "invalid language specifier");
  838. return -1;
  839. }
  840. strncpy(spec, str, len);
  841. spec[len] = '\0';
  842. scripting_ops = script_spec__lookup(spec);
  843. if (!scripting_ops) {
  844. fprintf(stderr, "invalid language specifier");
  845. return -1;
  846. }
  847. script++;
  848. } else {
  849. script = str;
  850. ext = strrchr(script, '.');
  851. if (!ext) {
  852. fprintf(stderr, "invalid script extension");
  853. return -1;
  854. }
  855. scripting_ops = script_spec__lookup(++ext);
  856. if (!scripting_ops) {
  857. fprintf(stderr, "invalid script extension");
  858. return -1;
  859. }
  860. }
  861. script_name = strdup(script);
  862. return 0;
  863. }
  864. static int parse_output_fields(const struct option *opt __maybe_unused,
  865. const char *arg, int unset __maybe_unused)
  866. {
  867. char *tok;
  868. int i, imax = ARRAY_SIZE(all_output_options);
  869. int j;
  870. int rc = 0;
  871. char *str = strdup(arg);
  872. int type = -1;
  873. if (!str)
  874. return -ENOMEM;
  875. /* first word can state for which event type the user is specifying
  876. * the fields. If no type exists, the specified fields apply to all
  877. * event types found in the file minus the invalid fields for a type.
  878. */
  879. tok = strchr(str, ':');
  880. if (tok) {
  881. *tok = '\0';
  882. tok++;
  883. if (!strcmp(str, "hw"))
  884. type = PERF_TYPE_HARDWARE;
  885. else if (!strcmp(str, "sw"))
  886. type = PERF_TYPE_SOFTWARE;
  887. else if (!strcmp(str, "trace"))
  888. type = PERF_TYPE_TRACEPOINT;
  889. else if (!strcmp(str, "raw"))
  890. type = PERF_TYPE_RAW;
  891. else {
  892. fprintf(stderr, "Invalid event type in field string.\n");
  893. rc = -EINVAL;
  894. goto out;
  895. }
  896. if (output[type].user_set)
  897. pr_warning("Overriding previous field request for %s events.\n",
  898. event_type(type));
  899. output[type].fields = 0;
  900. output[type].user_set = true;
  901. output[type].wildcard_set = false;
  902. } else {
  903. tok = str;
  904. if (strlen(str) == 0) {
  905. fprintf(stderr,
  906. "Cannot set fields to 'none' for all event types.\n");
  907. rc = -EINVAL;
  908. goto out;
  909. }
  910. if (output_set_by_user())
  911. pr_warning("Overriding previous field request for all events.\n");
  912. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  913. output[j].fields = 0;
  914. output[j].user_set = true;
  915. output[j].wildcard_set = true;
  916. }
  917. }
  918. for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
  919. for (i = 0; i < imax; ++i) {
  920. if (strcmp(tok, all_output_options[i].str) == 0)
  921. break;
  922. }
  923. if (i == imax && strcmp(tok, "flags") == 0) {
  924. print_flags = true;
  925. continue;
  926. }
  927. if (i == imax) {
  928. fprintf(stderr, "Invalid field requested.\n");
  929. rc = -EINVAL;
  930. goto out;
  931. }
  932. if (type == -1) {
  933. /* add user option to all events types for
  934. * which it is valid
  935. */
  936. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  937. if (output[j].invalid_fields & all_output_options[i].field) {
  938. pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
  939. all_output_options[i].str, event_type(j));
  940. } else
  941. output[j].fields |= all_output_options[i].field;
  942. }
  943. } else {
  944. if (output[type].invalid_fields & all_output_options[i].field) {
  945. fprintf(stderr, "\'%s\' not valid for %s events.\n",
  946. all_output_options[i].str, event_type(type));
  947. rc = -EINVAL;
  948. goto out;
  949. }
  950. output[type].fields |= all_output_options[i].field;
  951. }
  952. }
  953. if (type >= 0) {
  954. if (output[type].fields == 0) {
  955. pr_debug("No fields requested for %s type. "
  956. "Events will not be displayed.\n", event_type(type));
  957. }
  958. }
  959. out:
  960. free(str);
  961. return rc;
  962. }
  963. /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
  964. static int is_directory(const char *base_path, const struct dirent *dent)
  965. {
  966. char path[PATH_MAX];
  967. struct stat st;
  968. sprintf(path, "%s/%s", base_path, dent->d_name);
  969. if (stat(path, &st))
  970. return 0;
  971. return S_ISDIR(st.st_mode);
  972. }
  973. #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
  974. while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
  975. lang_next) \
  976. if ((lang_dirent.d_type == DT_DIR || \
  977. (lang_dirent.d_type == DT_UNKNOWN && \
  978. is_directory(scripts_path, &lang_dirent))) && \
  979. (strcmp(lang_dirent.d_name, ".")) && \
  980. (strcmp(lang_dirent.d_name, "..")))
  981. #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
  982. while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
  983. script_next) \
  984. if (script_dirent.d_type != DT_DIR && \
  985. (script_dirent.d_type != DT_UNKNOWN || \
  986. !is_directory(lang_path, &script_dirent)))
  987. #define RECORD_SUFFIX "-record"
  988. #define REPORT_SUFFIX "-report"
  989. struct script_desc {
  990. struct list_head node;
  991. char *name;
  992. char *half_liner;
  993. char *args;
  994. };
  995. static LIST_HEAD(script_descs);
  996. static struct script_desc *script_desc__new(const char *name)
  997. {
  998. struct script_desc *s = zalloc(sizeof(*s));
  999. if (s != NULL && name)
  1000. s->name = strdup(name);
  1001. return s;
  1002. }
  1003. static void script_desc__delete(struct script_desc *s)
  1004. {
  1005. zfree(&s->name);
  1006. zfree(&s->half_liner);
  1007. zfree(&s->args);
  1008. free(s);
  1009. }
  1010. static void script_desc__add(struct script_desc *s)
  1011. {
  1012. list_add_tail(&s->node, &script_descs);
  1013. }
  1014. static struct script_desc *script_desc__find(const char *name)
  1015. {
  1016. struct script_desc *s;
  1017. list_for_each_entry(s, &script_descs, node)
  1018. if (strcasecmp(s->name, name) == 0)
  1019. return s;
  1020. return NULL;
  1021. }
  1022. static struct script_desc *script_desc__findnew(const char *name)
  1023. {
  1024. struct script_desc *s = script_desc__find(name);
  1025. if (s)
  1026. return s;
  1027. s = script_desc__new(name);
  1028. if (!s)
  1029. goto out_delete_desc;
  1030. script_desc__add(s);
  1031. return s;
  1032. out_delete_desc:
  1033. script_desc__delete(s);
  1034. return NULL;
  1035. }
  1036. static const char *ends_with(const char *str, const char *suffix)
  1037. {
  1038. size_t suffix_len = strlen(suffix);
  1039. const char *p = str;
  1040. if (strlen(str) > suffix_len) {
  1041. p = str + strlen(str) - suffix_len;
  1042. if (!strncmp(p, suffix, suffix_len))
  1043. return p;
  1044. }
  1045. return NULL;
  1046. }
  1047. static int read_script_info(struct script_desc *desc, const char *filename)
  1048. {
  1049. char line[BUFSIZ], *p;
  1050. FILE *fp;
  1051. fp = fopen(filename, "r");
  1052. if (!fp)
  1053. return -1;
  1054. while (fgets(line, sizeof(line), fp)) {
  1055. p = ltrim(line);
  1056. if (strlen(p) == 0)
  1057. continue;
  1058. if (*p != '#')
  1059. continue;
  1060. p++;
  1061. if (strlen(p) && *p == '!')
  1062. continue;
  1063. p = ltrim(p);
  1064. if (strlen(p) && p[strlen(p) - 1] == '\n')
  1065. p[strlen(p) - 1] = '\0';
  1066. if (!strncmp(p, "description:", strlen("description:"))) {
  1067. p += strlen("description:");
  1068. desc->half_liner = strdup(ltrim(p));
  1069. continue;
  1070. }
  1071. if (!strncmp(p, "args:", strlen("args:"))) {
  1072. p += strlen("args:");
  1073. desc->args = strdup(ltrim(p));
  1074. continue;
  1075. }
  1076. }
  1077. fclose(fp);
  1078. return 0;
  1079. }
  1080. static char *get_script_root(struct dirent *script_dirent, const char *suffix)
  1081. {
  1082. char *script_root, *str;
  1083. script_root = strdup(script_dirent->d_name);
  1084. if (!script_root)
  1085. return NULL;
  1086. str = (char *)ends_with(script_root, suffix);
  1087. if (!str) {
  1088. free(script_root);
  1089. return NULL;
  1090. }
  1091. *str = '\0';
  1092. return script_root;
  1093. }
  1094. static int list_available_scripts(const struct option *opt __maybe_unused,
  1095. const char *s __maybe_unused,
  1096. int unset __maybe_unused)
  1097. {
  1098. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  1099. char scripts_path[MAXPATHLEN];
  1100. DIR *scripts_dir, *lang_dir;
  1101. char script_path[MAXPATHLEN];
  1102. char lang_path[MAXPATHLEN];
  1103. struct script_desc *desc;
  1104. char first_half[BUFSIZ];
  1105. char *script_root;
  1106. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  1107. scripts_dir = opendir(scripts_path);
  1108. if (!scripts_dir)
  1109. return -1;
  1110. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  1111. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  1112. lang_dirent.d_name);
  1113. lang_dir = opendir(lang_path);
  1114. if (!lang_dir)
  1115. continue;
  1116. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  1117. script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
  1118. if (script_root) {
  1119. desc = script_desc__findnew(script_root);
  1120. snprintf(script_path, MAXPATHLEN, "%s/%s",
  1121. lang_path, script_dirent.d_name);
  1122. read_script_info(desc, script_path);
  1123. free(script_root);
  1124. }
  1125. }
  1126. }
  1127. fprintf(stdout, "List of available trace scripts:\n");
  1128. list_for_each_entry(desc, &script_descs, node) {
  1129. sprintf(first_half, "%s %s", desc->name,
  1130. desc->args ? desc->args : "");
  1131. fprintf(stdout, " %-36s %s\n", first_half,
  1132. desc->half_liner ? desc->half_liner : "");
  1133. }
  1134. exit(0);
  1135. }
  1136. /*
  1137. * Some scripts specify the required events in their "xxx-record" file,
  1138. * this function will check if the events in perf.data match those
  1139. * mentioned in the "xxx-record".
  1140. *
  1141. * Fixme: All existing "xxx-record" are all in good formats "-e event ",
  1142. * which is covered well now. And new parsing code should be added to
  1143. * cover the future complexing formats like event groups etc.
  1144. */
  1145. static int check_ev_match(char *dir_name, char *scriptname,
  1146. struct perf_session *session)
  1147. {
  1148. char filename[MAXPATHLEN], evname[128];
  1149. char line[BUFSIZ], *p;
  1150. struct perf_evsel *pos;
  1151. int match, len;
  1152. FILE *fp;
  1153. sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
  1154. fp = fopen(filename, "r");
  1155. if (!fp)
  1156. return -1;
  1157. while (fgets(line, sizeof(line), fp)) {
  1158. p = ltrim(line);
  1159. if (*p == '#')
  1160. continue;
  1161. while (strlen(p)) {
  1162. p = strstr(p, "-e");
  1163. if (!p)
  1164. break;
  1165. p += 2;
  1166. p = ltrim(p);
  1167. len = strcspn(p, " \t");
  1168. if (!len)
  1169. break;
  1170. snprintf(evname, len + 1, "%s", p);
  1171. match = 0;
  1172. evlist__for_each(session->evlist, pos) {
  1173. if (!strcmp(perf_evsel__name(pos), evname)) {
  1174. match = 1;
  1175. break;
  1176. }
  1177. }
  1178. if (!match) {
  1179. fclose(fp);
  1180. return -1;
  1181. }
  1182. }
  1183. }
  1184. fclose(fp);
  1185. return 0;
  1186. }
  1187. /*
  1188. * Return -1 if none is found, otherwise the actual scripts number.
  1189. *
  1190. * Currently the only user of this function is the script browser, which
  1191. * will list all statically runnable scripts, select one, execute it and
  1192. * show the output in a perf browser.
  1193. */
  1194. int find_scripts(char **scripts_array, char **scripts_path_array)
  1195. {
  1196. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  1197. char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
  1198. DIR *scripts_dir, *lang_dir;
  1199. struct perf_session *session;
  1200. struct perf_data_file file = {
  1201. .path = input_name,
  1202. .mode = PERF_DATA_MODE_READ,
  1203. };
  1204. char *temp;
  1205. int i = 0;
  1206. session = perf_session__new(&file, false, NULL);
  1207. if (!session)
  1208. return -1;
  1209. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  1210. scripts_dir = opendir(scripts_path);
  1211. if (!scripts_dir) {
  1212. perf_session__delete(session);
  1213. return -1;
  1214. }
  1215. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  1216. snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
  1217. lang_dirent.d_name);
  1218. #ifdef NO_LIBPERL
  1219. if (strstr(lang_path, "perl"))
  1220. continue;
  1221. #endif
  1222. #ifdef NO_LIBPYTHON
  1223. if (strstr(lang_path, "python"))
  1224. continue;
  1225. #endif
  1226. lang_dir = opendir(lang_path);
  1227. if (!lang_dir)
  1228. continue;
  1229. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  1230. /* Skip those real time scripts: xxxtop.p[yl] */
  1231. if (strstr(script_dirent.d_name, "top."))
  1232. continue;
  1233. sprintf(scripts_path_array[i], "%s/%s", lang_path,
  1234. script_dirent.d_name);
  1235. temp = strchr(script_dirent.d_name, '.');
  1236. snprintf(scripts_array[i],
  1237. (temp - script_dirent.d_name) + 1,
  1238. "%s", script_dirent.d_name);
  1239. if (check_ev_match(lang_path,
  1240. scripts_array[i], session))
  1241. continue;
  1242. i++;
  1243. }
  1244. closedir(lang_dir);
  1245. }
  1246. closedir(scripts_dir);
  1247. perf_session__delete(session);
  1248. return i;
  1249. }
  1250. static char *get_script_path(const char *script_root, const char *suffix)
  1251. {
  1252. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  1253. char scripts_path[MAXPATHLEN];
  1254. char script_path[MAXPATHLEN];
  1255. DIR *scripts_dir, *lang_dir;
  1256. char lang_path[MAXPATHLEN];
  1257. char *__script_root;
  1258. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  1259. scripts_dir = opendir(scripts_path);
  1260. if (!scripts_dir)
  1261. return NULL;
  1262. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  1263. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  1264. lang_dirent.d_name);
  1265. lang_dir = opendir(lang_path);
  1266. if (!lang_dir)
  1267. continue;
  1268. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  1269. __script_root = get_script_root(&script_dirent, suffix);
  1270. if (__script_root && !strcmp(script_root, __script_root)) {
  1271. free(__script_root);
  1272. closedir(lang_dir);
  1273. closedir(scripts_dir);
  1274. snprintf(script_path, MAXPATHLEN, "%s/%s",
  1275. lang_path, script_dirent.d_name);
  1276. return strdup(script_path);
  1277. }
  1278. free(__script_root);
  1279. }
  1280. closedir(lang_dir);
  1281. }
  1282. closedir(scripts_dir);
  1283. return NULL;
  1284. }
  1285. static bool is_top_script(const char *script_path)
  1286. {
  1287. return ends_with(script_path, "top") == NULL ? false : true;
  1288. }
  1289. static int has_required_arg(char *script_path)
  1290. {
  1291. struct script_desc *desc;
  1292. int n_args = 0;
  1293. char *p;
  1294. desc = script_desc__new(NULL);
  1295. if (read_script_info(desc, script_path))
  1296. goto out;
  1297. if (!desc->args)
  1298. goto out;
  1299. for (p = desc->args; *p; p++)
  1300. if (*p == '<')
  1301. n_args++;
  1302. out:
  1303. script_desc__delete(desc);
  1304. return n_args;
  1305. }
  1306. static int have_cmd(int argc, const char **argv)
  1307. {
  1308. char **__argv = malloc(sizeof(const char *) * argc);
  1309. if (!__argv) {
  1310. pr_err("malloc failed\n");
  1311. return -1;
  1312. }
  1313. memcpy(__argv, argv, sizeof(const char *) * argc);
  1314. argc = parse_options(argc, (const char **)__argv, record_options,
  1315. NULL, PARSE_OPT_STOP_AT_NON_OPTION);
  1316. free(__argv);
  1317. system_wide = (argc == 0);
  1318. return 0;
  1319. }
  1320. static void script__setup_sample_type(struct perf_script *script)
  1321. {
  1322. struct perf_session *session = script->session;
  1323. u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
  1324. if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
  1325. if ((sample_type & PERF_SAMPLE_REGS_USER) &&
  1326. (sample_type & PERF_SAMPLE_STACK_USER))
  1327. callchain_param.record_mode = CALLCHAIN_DWARF;
  1328. else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
  1329. callchain_param.record_mode = CALLCHAIN_LBR;
  1330. else
  1331. callchain_param.record_mode = CALLCHAIN_FP;
  1332. }
  1333. }
  1334. int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
  1335. {
  1336. bool show_full_info = false;
  1337. bool header = false;
  1338. bool header_only = false;
  1339. bool script_started = false;
  1340. char *rec_script_path = NULL;
  1341. char *rep_script_path = NULL;
  1342. struct perf_session *session;
  1343. struct itrace_synth_opts itrace_synth_opts = { .set = false, };
  1344. char *script_path = NULL;
  1345. const char **__argv;
  1346. int i, j, err = 0;
  1347. struct perf_script script = {
  1348. .tool = {
  1349. .sample = process_sample_event,
  1350. .mmap = perf_event__process_mmap,
  1351. .mmap2 = perf_event__process_mmap2,
  1352. .comm = perf_event__process_comm,
  1353. .exit = perf_event__process_exit,
  1354. .fork = perf_event__process_fork,
  1355. .attr = process_attr,
  1356. .tracing_data = perf_event__process_tracing_data,
  1357. .build_id = perf_event__process_build_id,
  1358. .id_index = perf_event__process_id_index,
  1359. .auxtrace_info = perf_event__process_auxtrace_info,
  1360. .auxtrace = perf_event__process_auxtrace,
  1361. .auxtrace_error = perf_event__process_auxtrace_error,
  1362. .ordered_events = true,
  1363. .ordering_requires_timestamps = true,
  1364. },
  1365. };
  1366. struct perf_data_file file = {
  1367. .mode = PERF_DATA_MODE_READ,
  1368. };
  1369. const struct option options[] = {
  1370. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1371. "dump raw trace in ASCII"),
  1372. OPT_INCR('v', "verbose", &verbose,
  1373. "be more verbose (show symbol address, etc)"),
  1374. OPT_BOOLEAN('L', "Latency", &latency_format,
  1375. "show latency attributes (irqs/preemption disabled, etc)"),
  1376. OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
  1377. list_available_scripts),
  1378. OPT_CALLBACK('s', "script", NULL, "name",
  1379. "script file name (lang:script name, script name, or *)",
  1380. parse_scriptname),
  1381. OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
  1382. "generate perf-script.xx script in specified language"),
  1383. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  1384. OPT_BOOLEAN('d', "debug-mode", &debug_mode,
  1385. "do various checks like samples ordering and lost events"),
  1386. OPT_BOOLEAN(0, "header", &header, "Show data header."),
  1387. OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
  1388. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  1389. "file", "vmlinux pathname"),
  1390. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  1391. "file", "kallsyms pathname"),
  1392. OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
  1393. "When printing symbols do not display call chain"),
  1394. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  1395. "Look for files with symbols relative to this directory"),
  1396. OPT_CALLBACK('F', "fields", NULL, "str",
  1397. "comma separated output fields prepend with 'type:'. "
  1398. "Valid types: hw,sw,trace,raw. "
  1399. "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
  1400. "addr,symoff,period,iregs,flags", parse_output_fields),
  1401. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  1402. "system-wide collection from all CPUs"),
  1403. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  1404. "only consider these symbols"),
  1405. OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
  1406. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  1407. "only display events for these comms"),
  1408. OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
  1409. "only consider symbols in these pids"),
  1410. OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
  1411. "only consider symbols in these tids"),
  1412. OPT_BOOLEAN('I', "show-info", &show_full_info,
  1413. "display extended information from perf.data file"),
  1414. OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
  1415. "Show the path of [kernel.kallsyms]"),
  1416. OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
  1417. "Show the fork/comm/exit events"),
  1418. OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
  1419. "Show the mmap events"),
  1420. OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
  1421. "Show context switch events (if recorded)"),
  1422. OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
  1423. OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
  1424. "Instruction Tracing options",
  1425. itrace_parse_synth_opts),
  1426. OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
  1427. "Show full source file name path for source lines"),
  1428. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  1429. "Enable symbol demangling"),
  1430. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  1431. "Enable kernel symbol demangling"),
  1432. OPT_END()
  1433. };
  1434. const char * const script_subcommands[] = { "record", "report", NULL };
  1435. const char *script_usage[] = {
  1436. "perf script [<options>]",
  1437. "perf script [<options>] record <script> [<record-options>] <command>",
  1438. "perf script [<options>] report <script> [script-args]",
  1439. "perf script [<options>] <script> [<record-options>] <command>",
  1440. "perf script [<options>] <top-script> [script-args]",
  1441. NULL
  1442. };
  1443. setup_scripting();
  1444. argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
  1445. PARSE_OPT_STOP_AT_NON_OPTION);
  1446. file.path = input_name;
  1447. if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
  1448. rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
  1449. if (!rec_script_path)
  1450. return cmd_record(argc, argv, NULL);
  1451. }
  1452. if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
  1453. rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
  1454. if (!rep_script_path) {
  1455. fprintf(stderr,
  1456. "Please specify a valid report script"
  1457. "(see 'perf script -l' for listing)\n");
  1458. return -1;
  1459. }
  1460. }
  1461. /* make sure PERF_EXEC_PATH is set for scripts */
  1462. perf_set_argv_exec_path(perf_exec_path());
  1463. if (argc && !script_name && !rec_script_path && !rep_script_path) {
  1464. int live_pipe[2];
  1465. int rep_args;
  1466. pid_t pid;
  1467. rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
  1468. rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
  1469. if (!rec_script_path && !rep_script_path) {
  1470. fprintf(stderr, " Couldn't find script %s\n\n See perf"
  1471. " script -l for available scripts.\n", argv[0]);
  1472. usage_with_options(script_usage, options);
  1473. }
  1474. if (is_top_script(argv[0])) {
  1475. rep_args = argc - 1;
  1476. } else {
  1477. int rec_args;
  1478. rep_args = has_required_arg(rep_script_path);
  1479. rec_args = (argc - 1) - rep_args;
  1480. if (rec_args < 0) {
  1481. fprintf(stderr, " %s script requires options."
  1482. "\n\n See perf script -l for available "
  1483. "scripts and options.\n", argv[0]);
  1484. usage_with_options(script_usage, options);
  1485. }
  1486. }
  1487. if (pipe(live_pipe) < 0) {
  1488. perror("failed to create pipe");
  1489. return -1;
  1490. }
  1491. pid = fork();
  1492. if (pid < 0) {
  1493. perror("failed to fork");
  1494. return -1;
  1495. }
  1496. if (!pid) {
  1497. j = 0;
  1498. dup2(live_pipe[1], 1);
  1499. close(live_pipe[0]);
  1500. if (is_top_script(argv[0])) {
  1501. system_wide = true;
  1502. } else if (!system_wide) {
  1503. if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
  1504. err = -1;
  1505. goto out;
  1506. }
  1507. }
  1508. __argv = malloc((argc + 6) * sizeof(const char *));
  1509. if (!__argv) {
  1510. pr_err("malloc failed\n");
  1511. err = -ENOMEM;
  1512. goto out;
  1513. }
  1514. __argv[j++] = "/bin/sh";
  1515. __argv[j++] = rec_script_path;
  1516. if (system_wide)
  1517. __argv[j++] = "-a";
  1518. __argv[j++] = "-q";
  1519. __argv[j++] = "-o";
  1520. __argv[j++] = "-";
  1521. for (i = rep_args + 1; i < argc; i++)
  1522. __argv[j++] = argv[i];
  1523. __argv[j++] = NULL;
  1524. execvp("/bin/sh", (char **)__argv);
  1525. free(__argv);
  1526. exit(-1);
  1527. }
  1528. dup2(live_pipe[0], 0);
  1529. close(live_pipe[1]);
  1530. __argv = malloc((argc + 4) * sizeof(const char *));
  1531. if (!__argv) {
  1532. pr_err("malloc failed\n");
  1533. err = -ENOMEM;
  1534. goto out;
  1535. }
  1536. j = 0;
  1537. __argv[j++] = "/bin/sh";
  1538. __argv[j++] = rep_script_path;
  1539. for (i = 1; i < rep_args + 1; i++)
  1540. __argv[j++] = argv[i];
  1541. __argv[j++] = "-i";
  1542. __argv[j++] = "-";
  1543. __argv[j++] = NULL;
  1544. execvp("/bin/sh", (char **)__argv);
  1545. free(__argv);
  1546. exit(-1);
  1547. }
  1548. if (rec_script_path)
  1549. script_path = rec_script_path;
  1550. if (rep_script_path)
  1551. script_path = rep_script_path;
  1552. if (script_path) {
  1553. j = 0;
  1554. if (!rec_script_path)
  1555. system_wide = false;
  1556. else if (!system_wide) {
  1557. if (have_cmd(argc - 1, &argv[1]) != 0) {
  1558. err = -1;
  1559. goto out;
  1560. }
  1561. }
  1562. __argv = malloc((argc + 2) * sizeof(const char *));
  1563. if (!__argv) {
  1564. pr_err("malloc failed\n");
  1565. err = -ENOMEM;
  1566. goto out;
  1567. }
  1568. __argv[j++] = "/bin/sh";
  1569. __argv[j++] = script_path;
  1570. if (system_wide)
  1571. __argv[j++] = "-a";
  1572. for (i = 2; i < argc; i++)
  1573. __argv[j++] = argv[i];
  1574. __argv[j++] = NULL;
  1575. execvp("/bin/sh", (char **)__argv);
  1576. free(__argv);
  1577. exit(-1);
  1578. }
  1579. if (!script_name)
  1580. setup_pager();
  1581. session = perf_session__new(&file, false, &script.tool);
  1582. if (session == NULL)
  1583. return -1;
  1584. if (header || header_only) {
  1585. perf_session__fprintf_info(session, stdout, show_full_info);
  1586. if (header_only)
  1587. goto out_delete;
  1588. }
  1589. if (symbol__init(&session->header.env) < 0)
  1590. goto out_delete;
  1591. script.session = session;
  1592. script__setup_sample_type(&script);
  1593. session->itrace_synth_opts = &itrace_synth_opts;
  1594. if (cpu_list) {
  1595. err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
  1596. if (err < 0)
  1597. goto out_delete;
  1598. }
  1599. if (!no_callchain)
  1600. symbol_conf.use_callchain = true;
  1601. else
  1602. symbol_conf.use_callchain = false;
  1603. if (session->tevent.pevent &&
  1604. pevent_set_function_resolver(session->tevent.pevent,
  1605. machine__resolve_kernel_addr,
  1606. &session->machines.host) < 0) {
  1607. pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
  1608. return -1;
  1609. }
  1610. if (generate_script_lang) {
  1611. struct stat perf_stat;
  1612. int input;
  1613. if (output_set_by_user()) {
  1614. fprintf(stderr,
  1615. "custom fields not supported for generated scripts");
  1616. err = -EINVAL;
  1617. goto out_delete;
  1618. }
  1619. input = open(file.path, O_RDONLY); /* input_name */
  1620. if (input < 0) {
  1621. err = -errno;
  1622. perror("failed to open file");
  1623. goto out_delete;
  1624. }
  1625. err = fstat(input, &perf_stat);
  1626. if (err < 0) {
  1627. perror("failed to stat file");
  1628. goto out_delete;
  1629. }
  1630. if (!perf_stat.st_size) {
  1631. fprintf(stderr, "zero-sized file, nothing to do!\n");
  1632. goto out_delete;
  1633. }
  1634. scripting_ops = script_spec__lookup(generate_script_lang);
  1635. if (!scripting_ops) {
  1636. fprintf(stderr, "invalid language specifier");
  1637. err = -ENOENT;
  1638. goto out_delete;
  1639. }
  1640. err = scripting_ops->generate_script(session->tevent.pevent,
  1641. "perf-script");
  1642. goto out_delete;
  1643. }
  1644. if (script_name) {
  1645. err = scripting_ops->start_script(script_name, argc, argv);
  1646. if (err)
  1647. goto out_delete;
  1648. pr_debug("perf script started with script %s\n\n", script_name);
  1649. script_started = true;
  1650. }
  1651. err = perf_session__check_output_opt(session);
  1652. if (err < 0)
  1653. goto out_delete;
  1654. err = __cmd_script(&script);
  1655. flush_scripting();
  1656. out_delete:
  1657. perf_session__delete(session);
  1658. if (script_started)
  1659. cleanup_scripting();
  1660. out:
  1661. return err;
  1662. }