builtin-script.c 43 KB

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