builtin-script.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939
  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. goto out_put;
  510. if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
  511. goto out_put;
  512. scripting_ops->process_event(event, sample, evsel, &al);
  513. out_put:
  514. addr_location__put(&al);
  515. return 0;
  516. }
  517. struct perf_script {
  518. struct perf_tool tool;
  519. struct perf_session *session;
  520. bool show_task_events;
  521. bool show_mmap_events;
  522. bool show_switch_events;
  523. };
  524. static int process_attr(struct perf_tool *tool, union perf_event *event,
  525. struct perf_evlist **pevlist)
  526. {
  527. struct perf_script *scr = container_of(tool, struct perf_script, tool);
  528. struct perf_evlist *evlist;
  529. struct perf_evsel *evsel, *pos;
  530. int err;
  531. err = perf_event__process_attr(tool, event, pevlist);
  532. if (err)
  533. return err;
  534. evlist = *pevlist;
  535. evsel = perf_evlist__last(*pevlist);
  536. if (evsel->attr.type >= PERF_TYPE_MAX)
  537. return 0;
  538. evlist__for_each(evlist, pos) {
  539. if (pos->attr.type == evsel->attr.type && pos != evsel)
  540. return 0;
  541. }
  542. set_print_ip_opts(&evsel->attr);
  543. return perf_evsel__check_attr(evsel, scr->session);
  544. }
  545. static int process_comm_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__id2evsel(session->evlist, sample->id);
  554. int ret = -1;
  555. thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
  556. if (thread == NULL) {
  557. pr_debug("problem processing COMM event, skipping it.\n");
  558. return -1;
  559. }
  560. if (perf_event__process_comm(tool, event, sample, machine) < 0)
  561. goto out;
  562. if (!evsel->attr.sample_id_all) {
  563. sample->cpu = 0;
  564. sample->time = 0;
  565. sample->tid = event->comm.tid;
  566. sample->pid = event->comm.pid;
  567. }
  568. print_sample_start(sample, thread, evsel);
  569. perf_event__fprintf(event, stdout);
  570. ret = 0;
  571. out:
  572. thread__put(thread);
  573. return ret;
  574. }
  575. static int process_fork_event(struct perf_tool *tool,
  576. union perf_event *event,
  577. struct perf_sample *sample,
  578. struct machine *machine)
  579. {
  580. struct thread *thread;
  581. struct perf_script *script = container_of(tool, struct perf_script, tool);
  582. struct perf_session *session = script->session;
  583. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  584. if (perf_event__process_fork(tool, event, sample, machine) < 0)
  585. return -1;
  586. thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
  587. if (thread == NULL) {
  588. pr_debug("problem processing FORK event, skipping it.\n");
  589. return -1;
  590. }
  591. if (!evsel->attr.sample_id_all) {
  592. sample->cpu = 0;
  593. sample->time = event->fork.time;
  594. sample->tid = event->fork.tid;
  595. sample->pid = event->fork.pid;
  596. }
  597. print_sample_start(sample, thread, evsel);
  598. perf_event__fprintf(event, stdout);
  599. thread__put(thread);
  600. return 0;
  601. }
  602. static int process_exit_event(struct perf_tool *tool,
  603. union perf_event *event,
  604. struct perf_sample *sample,
  605. struct machine *machine)
  606. {
  607. int err = 0;
  608. struct thread *thread;
  609. struct perf_script *script = container_of(tool, struct perf_script, tool);
  610. struct perf_session *session = script->session;
  611. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  612. thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
  613. if (thread == NULL) {
  614. pr_debug("problem processing EXIT event, skipping it.\n");
  615. return -1;
  616. }
  617. if (!evsel->attr.sample_id_all) {
  618. sample->cpu = 0;
  619. sample->time = 0;
  620. sample->tid = event->comm.tid;
  621. sample->pid = event->comm.pid;
  622. }
  623. print_sample_start(sample, thread, evsel);
  624. perf_event__fprintf(event, stdout);
  625. if (perf_event__process_exit(tool, event, sample, machine) < 0)
  626. err = -1;
  627. thread__put(thread);
  628. return err;
  629. }
  630. static int process_mmap_event(struct perf_tool *tool,
  631. union perf_event *event,
  632. struct perf_sample *sample,
  633. struct machine *machine)
  634. {
  635. struct thread *thread;
  636. struct perf_script *script = container_of(tool, struct perf_script, tool);
  637. struct perf_session *session = script->session;
  638. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  639. if (perf_event__process_mmap(tool, event, sample, machine) < 0)
  640. return -1;
  641. thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
  642. if (thread == NULL) {
  643. pr_debug("problem processing MMAP event, skipping it.\n");
  644. return -1;
  645. }
  646. if (!evsel->attr.sample_id_all) {
  647. sample->cpu = 0;
  648. sample->time = 0;
  649. sample->tid = event->mmap.tid;
  650. sample->pid = event->mmap.pid;
  651. }
  652. print_sample_start(sample, thread, evsel);
  653. perf_event__fprintf(event, stdout);
  654. thread__put(thread);
  655. return 0;
  656. }
  657. static int process_mmap2_event(struct perf_tool *tool,
  658. union perf_event *event,
  659. struct perf_sample *sample,
  660. struct machine *machine)
  661. {
  662. struct thread *thread;
  663. struct perf_script *script = container_of(tool, struct perf_script, tool);
  664. struct perf_session *session = script->session;
  665. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  666. if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
  667. return -1;
  668. thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
  669. if (thread == NULL) {
  670. pr_debug("problem processing MMAP2 event, skipping it.\n");
  671. return -1;
  672. }
  673. if (!evsel->attr.sample_id_all) {
  674. sample->cpu = 0;
  675. sample->time = 0;
  676. sample->tid = event->mmap2.tid;
  677. sample->pid = event->mmap2.pid;
  678. }
  679. print_sample_start(sample, thread, evsel);
  680. perf_event__fprintf(event, stdout);
  681. thread__put(thread);
  682. return 0;
  683. }
  684. static int process_switch_event(struct perf_tool *tool,
  685. union perf_event *event,
  686. struct perf_sample *sample,
  687. struct machine *machine)
  688. {
  689. struct thread *thread;
  690. struct perf_script *script = container_of(tool, struct perf_script, tool);
  691. struct perf_session *session = script->session;
  692. struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist, sample->id);
  693. if (perf_event__process_switch(tool, event, sample, machine) < 0)
  694. return -1;
  695. thread = machine__findnew_thread(machine, sample->pid,
  696. sample->tid);
  697. if (thread == NULL) {
  698. pr_debug("problem processing SWITCH event, skipping it.\n");
  699. return -1;
  700. }
  701. print_sample_start(sample, thread, evsel);
  702. perf_event__fprintf(event, stdout);
  703. thread__put(thread);
  704. return 0;
  705. }
  706. static void sig_handler(int sig __maybe_unused)
  707. {
  708. session_done = 1;
  709. }
  710. static int __cmd_script(struct perf_script *script)
  711. {
  712. int ret;
  713. signal(SIGINT, sig_handler);
  714. /* override event processing functions */
  715. if (script->show_task_events) {
  716. script->tool.comm = process_comm_event;
  717. script->tool.fork = process_fork_event;
  718. script->tool.exit = process_exit_event;
  719. }
  720. if (script->show_mmap_events) {
  721. script->tool.mmap = process_mmap_event;
  722. script->tool.mmap2 = process_mmap2_event;
  723. }
  724. if (script->show_switch_events)
  725. script->tool.context_switch = process_switch_event;
  726. ret = perf_session__process_events(script->session);
  727. if (debug_mode)
  728. pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
  729. return ret;
  730. }
  731. struct script_spec {
  732. struct list_head node;
  733. struct scripting_ops *ops;
  734. char spec[0];
  735. };
  736. static LIST_HEAD(script_specs);
  737. static struct script_spec *script_spec__new(const char *spec,
  738. struct scripting_ops *ops)
  739. {
  740. struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
  741. if (s != NULL) {
  742. strcpy(s->spec, spec);
  743. s->ops = ops;
  744. }
  745. return s;
  746. }
  747. static void script_spec__add(struct script_spec *s)
  748. {
  749. list_add_tail(&s->node, &script_specs);
  750. }
  751. static struct script_spec *script_spec__find(const char *spec)
  752. {
  753. struct script_spec *s;
  754. list_for_each_entry(s, &script_specs, node)
  755. if (strcasecmp(s->spec, spec) == 0)
  756. return s;
  757. return NULL;
  758. }
  759. static struct script_spec *script_spec__findnew(const char *spec,
  760. struct scripting_ops *ops)
  761. {
  762. struct script_spec *s = script_spec__find(spec);
  763. if (s)
  764. return s;
  765. s = script_spec__new(spec, ops);
  766. if (!s)
  767. return NULL;
  768. script_spec__add(s);
  769. return s;
  770. }
  771. int script_spec_register(const char *spec, struct scripting_ops *ops)
  772. {
  773. struct script_spec *s;
  774. s = script_spec__find(spec);
  775. if (s)
  776. return -1;
  777. s = script_spec__findnew(spec, ops);
  778. if (!s)
  779. return -1;
  780. return 0;
  781. }
  782. static struct scripting_ops *script_spec__lookup(const char *spec)
  783. {
  784. struct script_spec *s = script_spec__find(spec);
  785. if (!s)
  786. return NULL;
  787. return s->ops;
  788. }
  789. static void list_available_languages(void)
  790. {
  791. struct script_spec *s;
  792. fprintf(stderr, "\n");
  793. fprintf(stderr, "Scripting language extensions (used in "
  794. "perf script -s [spec:]script.[spec]):\n\n");
  795. list_for_each_entry(s, &script_specs, node)
  796. fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
  797. fprintf(stderr, "\n");
  798. }
  799. static int parse_scriptname(const struct option *opt __maybe_unused,
  800. const char *str, int unset __maybe_unused)
  801. {
  802. char spec[PATH_MAX];
  803. const char *script, *ext;
  804. int len;
  805. if (strcmp(str, "lang") == 0) {
  806. list_available_languages();
  807. exit(0);
  808. }
  809. script = strchr(str, ':');
  810. if (script) {
  811. len = script - str;
  812. if (len >= PATH_MAX) {
  813. fprintf(stderr, "invalid language specifier");
  814. return -1;
  815. }
  816. strncpy(spec, str, len);
  817. spec[len] = '\0';
  818. scripting_ops = script_spec__lookup(spec);
  819. if (!scripting_ops) {
  820. fprintf(stderr, "invalid language specifier");
  821. return -1;
  822. }
  823. script++;
  824. } else {
  825. script = str;
  826. ext = strrchr(script, '.');
  827. if (!ext) {
  828. fprintf(stderr, "invalid script extension");
  829. return -1;
  830. }
  831. scripting_ops = script_spec__lookup(++ext);
  832. if (!scripting_ops) {
  833. fprintf(stderr, "invalid script extension");
  834. return -1;
  835. }
  836. }
  837. script_name = strdup(script);
  838. return 0;
  839. }
  840. static int parse_output_fields(const struct option *opt __maybe_unused,
  841. const char *arg, int unset __maybe_unused)
  842. {
  843. char *tok;
  844. int i, imax = ARRAY_SIZE(all_output_options);
  845. int j;
  846. int rc = 0;
  847. char *str = strdup(arg);
  848. int type = -1;
  849. if (!str)
  850. return -ENOMEM;
  851. /* first word can state for which event type the user is specifying
  852. * the fields. If no type exists, the specified fields apply to all
  853. * event types found in the file minus the invalid fields for a type.
  854. */
  855. tok = strchr(str, ':');
  856. if (tok) {
  857. *tok = '\0';
  858. tok++;
  859. if (!strcmp(str, "hw"))
  860. type = PERF_TYPE_HARDWARE;
  861. else if (!strcmp(str, "sw"))
  862. type = PERF_TYPE_SOFTWARE;
  863. else if (!strcmp(str, "trace"))
  864. type = PERF_TYPE_TRACEPOINT;
  865. else if (!strcmp(str, "raw"))
  866. type = PERF_TYPE_RAW;
  867. else {
  868. fprintf(stderr, "Invalid event type in field string.\n");
  869. rc = -EINVAL;
  870. goto out;
  871. }
  872. if (output[type].user_set)
  873. pr_warning("Overriding previous field request for %s events.\n",
  874. event_type(type));
  875. output[type].fields = 0;
  876. output[type].user_set = true;
  877. output[type].wildcard_set = false;
  878. } else {
  879. tok = str;
  880. if (strlen(str) == 0) {
  881. fprintf(stderr,
  882. "Cannot set fields to 'none' for all event types.\n");
  883. rc = -EINVAL;
  884. goto out;
  885. }
  886. if (output_set_by_user())
  887. pr_warning("Overriding previous field request for all events.\n");
  888. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  889. output[j].fields = 0;
  890. output[j].user_set = true;
  891. output[j].wildcard_set = true;
  892. }
  893. }
  894. for (tok = strtok(tok, ","); tok; tok = strtok(NULL, ",")) {
  895. for (i = 0; i < imax; ++i) {
  896. if (strcmp(tok, all_output_options[i].str) == 0)
  897. break;
  898. }
  899. if (i == imax && strcmp(tok, "flags") == 0) {
  900. print_flags = true;
  901. continue;
  902. }
  903. if (i == imax) {
  904. fprintf(stderr, "Invalid field requested.\n");
  905. rc = -EINVAL;
  906. goto out;
  907. }
  908. if (type == -1) {
  909. /* add user option to all events types for
  910. * which it is valid
  911. */
  912. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  913. if (output[j].invalid_fields & all_output_options[i].field) {
  914. pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
  915. all_output_options[i].str, event_type(j));
  916. } else
  917. output[j].fields |= all_output_options[i].field;
  918. }
  919. } else {
  920. if (output[type].invalid_fields & all_output_options[i].field) {
  921. fprintf(stderr, "\'%s\' not valid for %s events.\n",
  922. all_output_options[i].str, event_type(type));
  923. rc = -EINVAL;
  924. goto out;
  925. }
  926. output[type].fields |= all_output_options[i].field;
  927. }
  928. }
  929. if (type >= 0) {
  930. if (output[type].fields == 0) {
  931. pr_debug("No fields requested for %s type. "
  932. "Events will not be displayed.\n", event_type(type));
  933. }
  934. }
  935. out:
  936. free(str);
  937. return rc;
  938. }
  939. /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
  940. static int is_directory(const char *base_path, const struct dirent *dent)
  941. {
  942. char path[PATH_MAX];
  943. struct stat st;
  944. sprintf(path, "%s/%s", base_path, dent->d_name);
  945. if (stat(path, &st))
  946. return 0;
  947. return S_ISDIR(st.st_mode);
  948. }
  949. #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
  950. while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
  951. lang_next) \
  952. if ((lang_dirent.d_type == DT_DIR || \
  953. (lang_dirent.d_type == DT_UNKNOWN && \
  954. is_directory(scripts_path, &lang_dirent))) && \
  955. (strcmp(lang_dirent.d_name, ".")) && \
  956. (strcmp(lang_dirent.d_name, "..")))
  957. #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
  958. while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
  959. script_next) \
  960. if (script_dirent.d_type != DT_DIR && \
  961. (script_dirent.d_type != DT_UNKNOWN || \
  962. !is_directory(lang_path, &script_dirent)))
  963. #define RECORD_SUFFIX "-record"
  964. #define REPORT_SUFFIX "-report"
  965. struct script_desc {
  966. struct list_head node;
  967. char *name;
  968. char *half_liner;
  969. char *args;
  970. };
  971. static LIST_HEAD(script_descs);
  972. static struct script_desc *script_desc__new(const char *name)
  973. {
  974. struct script_desc *s = zalloc(sizeof(*s));
  975. if (s != NULL && name)
  976. s->name = strdup(name);
  977. return s;
  978. }
  979. static void script_desc__delete(struct script_desc *s)
  980. {
  981. zfree(&s->name);
  982. zfree(&s->half_liner);
  983. zfree(&s->args);
  984. free(s);
  985. }
  986. static void script_desc__add(struct script_desc *s)
  987. {
  988. list_add_tail(&s->node, &script_descs);
  989. }
  990. static struct script_desc *script_desc__find(const char *name)
  991. {
  992. struct script_desc *s;
  993. list_for_each_entry(s, &script_descs, node)
  994. if (strcasecmp(s->name, name) == 0)
  995. return s;
  996. return NULL;
  997. }
  998. static struct script_desc *script_desc__findnew(const char *name)
  999. {
  1000. struct script_desc *s = script_desc__find(name);
  1001. if (s)
  1002. return s;
  1003. s = script_desc__new(name);
  1004. if (!s)
  1005. goto out_delete_desc;
  1006. script_desc__add(s);
  1007. return s;
  1008. out_delete_desc:
  1009. script_desc__delete(s);
  1010. return NULL;
  1011. }
  1012. static const char *ends_with(const char *str, const char *suffix)
  1013. {
  1014. size_t suffix_len = strlen(suffix);
  1015. const char *p = str;
  1016. if (strlen(str) > suffix_len) {
  1017. p = str + strlen(str) - suffix_len;
  1018. if (!strncmp(p, suffix, suffix_len))
  1019. return p;
  1020. }
  1021. return NULL;
  1022. }
  1023. static int read_script_info(struct script_desc *desc, const char *filename)
  1024. {
  1025. char line[BUFSIZ], *p;
  1026. FILE *fp;
  1027. fp = fopen(filename, "r");
  1028. if (!fp)
  1029. return -1;
  1030. while (fgets(line, sizeof(line), fp)) {
  1031. p = ltrim(line);
  1032. if (strlen(p) == 0)
  1033. continue;
  1034. if (*p != '#')
  1035. continue;
  1036. p++;
  1037. if (strlen(p) && *p == '!')
  1038. continue;
  1039. p = ltrim(p);
  1040. if (strlen(p) && p[strlen(p) - 1] == '\n')
  1041. p[strlen(p) - 1] = '\0';
  1042. if (!strncmp(p, "description:", strlen("description:"))) {
  1043. p += strlen("description:");
  1044. desc->half_liner = strdup(ltrim(p));
  1045. continue;
  1046. }
  1047. if (!strncmp(p, "args:", strlen("args:"))) {
  1048. p += strlen("args:");
  1049. desc->args = strdup(ltrim(p));
  1050. continue;
  1051. }
  1052. }
  1053. fclose(fp);
  1054. return 0;
  1055. }
  1056. static char *get_script_root(struct dirent *script_dirent, const char *suffix)
  1057. {
  1058. char *script_root, *str;
  1059. script_root = strdup(script_dirent->d_name);
  1060. if (!script_root)
  1061. return NULL;
  1062. str = (char *)ends_with(script_root, suffix);
  1063. if (!str) {
  1064. free(script_root);
  1065. return NULL;
  1066. }
  1067. *str = '\0';
  1068. return script_root;
  1069. }
  1070. static int list_available_scripts(const struct option *opt __maybe_unused,
  1071. const char *s __maybe_unused,
  1072. int unset __maybe_unused)
  1073. {
  1074. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  1075. char scripts_path[MAXPATHLEN];
  1076. DIR *scripts_dir, *lang_dir;
  1077. char script_path[MAXPATHLEN];
  1078. char lang_path[MAXPATHLEN];
  1079. struct script_desc *desc;
  1080. char first_half[BUFSIZ];
  1081. char *script_root;
  1082. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  1083. scripts_dir = opendir(scripts_path);
  1084. if (!scripts_dir)
  1085. return -1;
  1086. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  1087. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  1088. lang_dirent.d_name);
  1089. lang_dir = opendir(lang_path);
  1090. if (!lang_dir)
  1091. continue;
  1092. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  1093. script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
  1094. if (script_root) {
  1095. desc = script_desc__findnew(script_root);
  1096. snprintf(script_path, MAXPATHLEN, "%s/%s",
  1097. lang_path, script_dirent.d_name);
  1098. read_script_info(desc, script_path);
  1099. free(script_root);
  1100. }
  1101. }
  1102. }
  1103. fprintf(stdout, "List of available trace scripts:\n");
  1104. list_for_each_entry(desc, &script_descs, node) {
  1105. sprintf(first_half, "%s %s", desc->name,
  1106. desc->args ? desc->args : "");
  1107. fprintf(stdout, " %-36s %s\n", first_half,
  1108. desc->half_liner ? desc->half_liner : "");
  1109. }
  1110. exit(0);
  1111. }
  1112. /*
  1113. * Some scripts specify the required events in their "xxx-record" file,
  1114. * this function will check if the events in perf.data match those
  1115. * mentioned in the "xxx-record".
  1116. *
  1117. * Fixme: All existing "xxx-record" are all in good formats "-e event ",
  1118. * which is covered well now. And new parsing code should be added to
  1119. * cover the future complexing formats like event groups etc.
  1120. */
  1121. static int check_ev_match(char *dir_name, char *scriptname,
  1122. struct perf_session *session)
  1123. {
  1124. char filename[MAXPATHLEN], evname[128];
  1125. char line[BUFSIZ], *p;
  1126. struct perf_evsel *pos;
  1127. int match, len;
  1128. FILE *fp;
  1129. sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
  1130. fp = fopen(filename, "r");
  1131. if (!fp)
  1132. return -1;
  1133. while (fgets(line, sizeof(line), fp)) {
  1134. p = ltrim(line);
  1135. if (*p == '#')
  1136. continue;
  1137. while (strlen(p)) {
  1138. p = strstr(p, "-e");
  1139. if (!p)
  1140. break;
  1141. p += 2;
  1142. p = ltrim(p);
  1143. len = strcspn(p, " \t");
  1144. if (!len)
  1145. break;
  1146. snprintf(evname, len + 1, "%s", p);
  1147. match = 0;
  1148. evlist__for_each(session->evlist, pos) {
  1149. if (!strcmp(perf_evsel__name(pos), evname)) {
  1150. match = 1;
  1151. break;
  1152. }
  1153. }
  1154. if (!match) {
  1155. fclose(fp);
  1156. return -1;
  1157. }
  1158. }
  1159. }
  1160. fclose(fp);
  1161. return 0;
  1162. }
  1163. /*
  1164. * Return -1 if none is found, otherwise the actual scripts number.
  1165. *
  1166. * Currently the only user of this function is the script browser, which
  1167. * will list all statically runnable scripts, select one, execute it and
  1168. * show the output in a perf browser.
  1169. */
  1170. int find_scripts(char **scripts_array, char **scripts_path_array)
  1171. {
  1172. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  1173. char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
  1174. DIR *scripts_dir, *lang_dir;
  1175. struct perf_session *session;
  1176. struct perf_data_file file = {
  1177. .path = input_name,
  1178. .mode = PERF_DATA_MODE_READ,
  1179. };
  1180. char *temp;
  1181. int i = 0;
  1182. session = perf_session__new(&file, false, NULL);
  1183. if (!session)
  1184. return -1;
  1185. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  1186. scripts_dir = opendir(scripts_path);
  1187. if (!scripts_dir) {
  1188. perf_session__delete(session);
  1189. return -1;
  1190. }
  1191. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  1192. snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
  1193. lang_dirent.d_name);
  1194. #ifdef NO_LIBPERL
  1195. if (strstr(lang_path, "perl"))
  1196. continue;
  1197. #endif
  1198. #ifdef NO_LIBPYTHON
  1199. if (strstr(lang_path, "python"))
  1200. continue;
  1201. #endif
  1202. lang_dir = opendir(lang_path);
  1203. if (!lang_dir)
  1204. continue;
  1205. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  1206. /* Skip those real time scripts: xxxtop.p[yl] */
  1207. if (strstr(script_dirent.d_name, "top."))
  1208. continue;
  1209. sprintf(scripts_path_array[i], "%s/%s", lang_path,
  1210. script_dirent.d_name);
  1211. temp = strchr(script_dirent.d_name, '.');
  1212. snprintf(scripts_array[i],
  1213. (temp - script_dirent.d_name) + 1,
  1214. "%s", script_dirent.d_name);
  1215. if (check_ev_match(lang_path,
  1216. scripts_array[i], session))
  1217. continue;
  1218. i++;
  1219. }
  1220. closedir(lang_dir);
  1221. }
  1222. closedir(scripts_dir);
  1223. perf_session__delete(session);
  1224. return i;
  1225. }
  1226. static char *get_script_path(const char *script_root, const char *suffix)
  1227. {
  1228. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  1229. char scripts_path[MAXPATHLEN];
  1230. char script_path[MAXPATHLEN];
  1231. DIR *scripts_dir, *lang_dir;
  1232. char lang_path[MAXPATHLEN];
  1233. char *__script_root;
  1234. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  1235. scripts_dir = opendir(scripts_path);
  1236. if (!scripts_dir)
  1237. return NULL;
  1238. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  1239. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  1240. lang_dirent.d_name);
  1241. lang_dir = opendir(lang_path);
  1242. if (!lang_dir)
  1243. continue;
  1244. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  1245. __script_root = get_script_root(&script_dirent, suffix);
  1246. if (__script_root && !strcmp(script_root, __script_root)) {
  1247. free(__script_root);
  1248. closedir(lang_dir);
  1249. closedir(scripts_dir);
  1250. snprintf(script_path, MAXPATHLEN, "%s/%s",
  1251. lang_path, script_dirent.d_name);
  1252. return strdup(script_path);
  1253. }
  1254. free(__script_root);
  1255. }
  1256. closedir(lang_dir);
  1257. }
  1258. closedir(scripts_dir);
  1259. return NULL;
  1260. }
  1261. static bool is_top_script(const char *script_path)
  1262. {
  1263. return ends_with(script_path, "top") == NULL ? false : true;
  1264. }
  1265. static int has_required_arg(char *script_path)
  1266. {
  1267. struct script_desc *desc;
  1268. int n_args = 0;
  1269. char *p;
  1270. desc = script_desc__new(NULL);
  1271. if (read_script_info(desc, script_path))
  1272. goto out;
  1273. if (!desc->args)
  1274. goto out;
  1275. for (p = desc->args; *p; p++)
  1276. if (*p == '<')
  1277. n_args++;
  1278. out:
  1279. script_desc__delete(desc);
  1280. return n_args;
  1281. }
  1282. static int have_cmd(int argc, const char **argv)
  1283. {
  1284. char **__argv = malloc(sizeof(const char *) * argc);
  1285. if (!__argv) {
  1286. pr_err("malloc failed\n");
  1287. return -1;
  1288. }
  1289. memcpy(__argv, argv, sizeof(const char *) * argc);
  1290. argc = parse_options(argc, (const char **)__argv, record_options,
  1291. NULL, PARSE_OPT_STOP_AT_NON_OPTION);
  1292. free(__argv);
  1293. system_wide = (argc == 0);
  1294. return 0;
  1295. }
  1296. int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
  1297. {
  1298. bool show_full_info = false;
  1299. bool header = false;
  1300. bool header_only = false;
  1301. bool script_started = false;
  1302. char *rec_script_path = NULL;
  1303. char *rep_script_path = NULL;
  1304. struct perf_session *session;
  1305. struct itrace_synth_opts itrace_synth_opts = { .set = false, };
  1306. char *script_path = NULL;
  1307. const char **__argv;
  1308. int i, j, err = 0;
  1309. struct perf_script script = {
  1310. .tool = {
  1311. .sample = process_sample_event,
  1312. .mmap = perf_event__process_mmap,
  1313. .mmap2 = perf_event__process_mmap2,
  1314. .comm = perf_event__process_comm,
  1315. .exit = perf_event__process_exit,
  1316. .fork = perf_event__process_fork,
  1317. .attr = process_attr,
  1318. .tracing_data = perf_event__process_tracing_data,
  1319. .build_id = perf_event__process_build_id,
  1320. .id_index = perf_event__process_id_index,
  1321. .auxtrace_info = perf_event__process_auxtrace_info,
  1322. .auxtrace = perf_event__process_auxtrace,
  1323. .auxtrace_error = perf_event__process_auxtrace_error,
  1324. .ordered_events = true,
  1325. .ordering_requires_timestamps = true,
  1326. },
  1327. };
  1328. struct perf_data_file file = {
  1329. .mode = PERF_DATA_MODE_READ,
  1330. };
  1331. const struct option options[] = {
  1332. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1333. "dump raw trace in ASCII"),
  1334. OPT_INCR('v', "verbose", &verbose,
  1335. "be more verbose (show symbol address, etc)"),
  1336. OPT_BOOLEAN('L', "Latency", &latency_format,
  1337. "show latency attributes (irqs/preemption disabled, etc)"),
  1338. OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
  1339. list_available_scripts),
  1340. OPT_CALLBACK('s', "script", NULL, "name",
  1341. "script file name (lang:script name, script name, or *)",
  1342. parse_scriptname),
  1343. OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
  1344. "generate perf-script.xx script in specified language"),
  1345. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  1346. OPT_BOOLEAN('d', "debug-mode", &debug_mode,
  1347. "do various checks like samples ordering and lost events"),
  1348. OPT_BOOLEAN(0, "header", &header, "Show data header."),
  1349. OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
  1350. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  1351. "file", "vmlinux pathname"),
  1352. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  1353. "file", "kallsyms pathname"),
  1354. OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
  1355. "When printing symbols do not display call chain"),
  1356. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  1357. "Look for files with symbols relative to this directory"),
  1358. OPT_CALLBACK('F', "fields", NULL, "str",
  1359. "comma separated output fields prepend with 'type:'. "
  1360. "Valid types: hw,sw,trace,raw. "
  1361. "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
  1362. "addr,symoff,period,flags", parse_output_fields),
  1363. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  1364. "system-wide collection from all CPUs"),
  1365. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  1366. "only consider these symbols"),
  1367. OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
  1368. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  1369. "only display events for these comms"),
  1370. OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
  1371. "only consider symbols in these pids"),
  1372. OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
  1373. "only consider symbols in these tids"),
  1374. OPT_BOOLEAN('I', "show-info", &show_full_info,
  1375. "display extended information from perf.data file"),
  1376. OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
  1377. "Show the path of [kernel.kallsyms]"),
  1378. OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
  1379. "Show the fork/comm/exit events"),
  1380. OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
  1381. "Show the mmap events"),
  1382. OPT_BOOLEAN('\0', "show-switch-events", &script.show_switch_events,
  1383. "Show context switch events (if recorded)"),
  1384. OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
  1385. OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
  1386. "Instruction Tracing options",
  1387. itrace_parse_synth_opts),
  1388. OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
  1389. "Show full source file name path for source lines"),
  1390. OPT_END()
  1391. };
  1392. const char * const script_subcommands[] = { "record", "report", NULL };
  1393. const char *script_usage[] = {
  1394. "perf script [<options>]",
  1395. "perf script [<options>] record <script> [<record-options>] <command>",
  1396. "perf script [<options>] report <script> [script-args]",
  1397. "perf script [<options>] <script> [<record-options>] <command>",
  1398. "perf script [<options>] <top-script> [script-args]",
  1399. NULL
  1400. };
  1401. setup_scripting();
  1402. argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,
  1403. PARSE_OPT_STOP_AT_NON_OPTION);
  1404. file.path = input_name;
  1405. if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
  1406. rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
  1407. if (!rec_script_path)
  1408. return cmd_record(argc, argv, NULL);
  1409. }
  1410. if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
  1411. rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
  1412. if (!rep_script_path) {
  1413. fprintf(stderr,
  1414. "Please specify a valid report script"
  1415. "(see 'perf script -l' for listing)\n");
  1416. return -1;
  1417. }
  1418. }
  1419. /* make sure PERF_EXEC_PATH is set for scripts */
  1420. perf_set_argv_exec_path(perf_exec_path());
  1421. if (argc && !script_name && !rec_script_path && !rep_script_path) {
  1422. int live_pipe[2];
  1423. int rep_args;
  1424. pid_t pid;
  1425. rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
  1426. rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
  1427. if (!rec_script_path && !rep_script_path) {
  1428. fprintf(stderr, " Couldn't find script %s\n\n See perf"
  1429. " script -l for available scripts.\n", argv[0]);
  1430. usage_with_options(script_usage, options);
  1431. }
  1432. if (is_top_script(argv[0])) {
  1433. rep_args = argc - 1;
  1434. } else {
  1435. int rec_args;
  1436. rep_args = has_required_arg(rep_script_path);
  1437. rec_args = (argc - 1) - rep_args;
  1438. if (rec_args < 0) {
  1439. fprintf(stderr, " %s script requires options."
  1440. "\n\n See perf script -l for available "
  1441. "scripts and options.\n", argv[0]);
  1442. usage_with_options(script_usage, options);
  1443. }
  1444. }
  1445. if (pipe(live_pipe) < 0) {
  1446. perror("failed to create pipe");
  1447. return -1;
  1448. }
  1449. pid = fork();
  1450. if (pid < 0) {
  1451. perror("failed to fork");
  1452. return -1;
  1453. }
  1454. if (!pid) {
  1455. j = 0;
  1456. dup2(live_pipe[1], 1);
  1457. close(live_pipe[0]);
  1458. if (is_top_script(argv[0])) {
  1459. system_wide = true;
  1460. } else if (!system_wide) {
  1461. if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
  1462. err = -1;
  1463. goto out;
  1464. }
  1465. }
  1466. __argv = malloc((argc + 6) * sizeof(const char *));
  1467. if (!__argv) {
  1468. pr_err("malloc failed\n");
  1469. err = -ENOMEM;
  1470. goto out;
  1471. }
  1472. __argv[j++] = "/bin/sh";
  1473. __argv[j++] = rec_script_path;
  1474. if (system_wide)
  1475. __argv[j++] = "-a";
  1476. __argv[j++] = "-q";
  1477. __argv[j++] = "-o";
  1478. __argv[j++] = "-";
  1479. for (i = rep_args + 1; i < argc; i++)
  1480. __argv[j++] = argv[i];
  1481. __argv[j++] = NULL;
  1482. execvp("/bin/sh", (char **)__argv);
  1483. free(__argv);
  1484. exit(-1);
  1485. }
  1486. dup2(live_pipe[0], 0);
  1487. close(live_pipe[1]);
  1488. __argv = malloc((argc + 4) * sizeof(const char *));
  1489. if (!__argv) {
  1490. pr_err("malloc failed\n");
  1491. err = -ENOMEM;
  1492. goto out;
  1493. }
  1494. j = 0;
  1495. __argv[j++] = "/bin/sh";
  1496. __argv[j++] = rep_script_path;
  1497. for (i = 1; i < rep_args + 1; i++)
  1498. __argv[j++] = argv[i];
  1499. __argv[j++] = "-i";
  1500. __argv[j++] = "-";
  1501. __argv[j++] = NULL;
  1502. execvp("/bin/sh", (char **)__argv);
  1503. free(__argv);
  1504. exit(-1);
  1505. }
  1506. if (rec_script_path)
  1507. script_path = rec_script_path;
  1508. if (rep_script_path)
  1509. script_path = rep_script_path;
  1510. if (script_path) {
  1511. j = 0;
  1512. if (!rec_script_path)
  1513. system_wide = false;
  1514. else if (!system_wide) {
  1515. if (have_cmd(argc - 1, &argv[1]) != 0) {
  1516. err = -1;
  1517. goto out;
  1518. }
  1519. }
  1520. __argv = malloc((argc + 2) * sizeof(const char *));
  1521. if (!__argv) {
  1522. pr_err("malloc failed\n");
  1523. err = -ENOMEM;
  1524. goto out;
  1525. }
  1526. __argv[j++] = "/bin/sh";
  1527. __argv[j++] = script_path;
  1528. if (system_wide)
  1529. __argv[j++] = "-a";
  1530. for (i = 2; i < argc; i++)
  1531. __argv[j++] = argv[i];
  1532. __argv[j++] = NULL;
  1533. execvp("/bin/sh", (char **)__argv);
  1534. free(__argv);
  1535. exit(-1);
  1536. }
  1537. if (!script_name)
  1538. setup_pager();
  1539. session = perf_session__new(&file, false, &script.tool);
  1540. if (session == NULL)
  1541. return -1;
  1542. if (header || header_only) {
  1543. perf_session__fprintf_info(session, stdout, show_full_info);
  1544. if (header_only)
  1545. goto out_delete;
  1546. }
  1547. if (symbol__init(&session->header.env) < 0)
  1548. goto out_delete;
  1549. script.session = session;
  1550. session->itrace_synth_opts = &itrace_synth_opts;
  1551. if (cpu_list) {
  1552. err = perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap);
  1553. if (err < 0)
  1554. goto out_delete;
  1555. }
  1556. if (!no_callchain)
  1557. symbol_conf.use_callchain = true;
  1558. else
  1559. symbol_conf.use_callchain = false;
  1560. if (session->tevent.pevent &&
  1561. pevent_set_function_resolver(session->tevent.pevent,
  1562. machine__resolve_kernel_addr,
  1563. &session->machines.host) < 0) {
  1564. pr_err("%s: failed to set libtraceevent function resolver\n", __func__);
  1565. return -1;
  1566. }
  1567. if (generate_script_lang) {
  1568. struct stat perf_stat;
  1569. int input;
  1570. if (output_set_by_user()) {
  1571. fprintf(stderr,
  1572. "custom fields not supported for generated scripts");
  1573. err = -EINVAL;
  1574. goto out_delete;
  1575. }
  1576. input = open(file.path, O_RDONLY); /* input_name */
  1577. if (input < 0) {
  1578. err = -errno;
  1579. perror("failed to open file");
  1580. goto out_delete;
  1581. }
  1582. err = fstat(input, &perf_stat);
  1583. if (err < 0) {
  1584. perror("failed to stat file");
  1585. goto out_delete;
  1586. }
  1587. if (!perf_stat.st_size) {
  1588. fprintf(stderr, "zero-sized file, nothing to do!\n");
  1589. goto out_delete;
  1590. }
  1591. scripting_ops = script_spec__lookup(generate_script_lang);
  1592. if (!scripting_ops) {
  1593. fprintf(stderr, "invalid language specifier");
  1594. err = -ENOENT;
  1595. goto out_delete;
  1596. }
  1597. err = scripting_ops->generate_script(session->tevent.pevent,
  1598. "perf-script");
  1599. goto out_delete;
  1600. }
  1601. if (script_name) {
  1602. err = scripting_ops->start_script(script_name, argc, argv);
  1603. if (err)
  1604. goto out_delete;
  1605. pr_debug("perf script started with script %s\n\n", script_name);
  1606. script_started = true;
  1607. }
  1608. err = perf_session__check_output_opt(session);
  1609. if (err < 0)
  1610. goto out_delete;
  1611. err = __cmd_script(&script);
  1612. flush_scripting();
  1613. out_delete:
  1614. perf_session__delete(session);
  1615. if (script_started)
  1616. cleanup_scripting();
  1617. out:
  1618. return err;
  1619. }