builtin-script.c 45 KB

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