builtin-script.c 37 KB

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