builtin-script.c 36 KB

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