builtin-script.c 44 KB

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