builtin-script.c 46 KB

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