builtin-script.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  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);
  305. if (!al.map)
  306. thread__find_addr_map(thread, machine, cpumode, MAP__VARIABLE,
  307. sample->addr, &al);
  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. PERF_MAX_STACK_DEPTH);
  341. }
  342. printf(" => ");
  343. /* print branch_to information */
  344. if (PRINT_FIELD(ADDR))
  345. print_sample_addr(event, sample, machine, thread, attr);
  346. printf("\n");
  347. }
  348. static void process_event(union perf_event *event, struct perf_sample *sample,
  349. struct perf_evsel *evsel, struct machine *machine,
  350. struct thread *thread,
  351. struct addr_location *al __maybe_unused)
  352. {
  353. struct perf_event_attr *attr = &evsel->attr;
  354. if (output[attr->type].fields == 0)
  355. return;
  356. print_sample_start(sample, thread, evsel);
  357. if (is_bts_event(attr)) {
  358. print_sample_bts(event, sample, evsel, machine, thread);
  359. return;
  360. }
  361. if (PRINT_FIELD(TRACE))
  362. event_format__print(evsel->tp_format, sample->cpu,
  363. sample->raw_data, sample->raw_size);
  364. if (PRINT_FIELD(ADDR))
  365. print_sample_addr(event, sample, machine, thread, attr);
  366. if (PRINT_FIELD(IP)) {
  367. if (!symbol_conf.use_callchain)
  368. printf(" ");
  369. else
  370. printf("\n");
  371. perf_evsel__print_ip(evsel, event, sample, machine,
  372. output[attr->type].print_ip_opts,
  373. PERF_MAX_STACK_DEPTH);
  374. }
  375. printf("\n");
  376. }
  377. static int default_start_script(const char *script __maybe_unused,
  378. int argc __maybe_unused,
  379. const char **argv __maybe_unused)
  380. {
  381. return 0;
  382. }
  383. static int default_stop_script(void)
  384. {
  385. return 0;
  386. }
  387. static int default_generate_script(struct pevent *pevent __maybe_unused,
  388. const char *outfile __maybe_unused)
  389. {
  390. return 0;
  391. }
  392. static struct scripting_ops default_scripting_ops = {
  393. .start_script = default_start_script,
  394. .stop_script = default_stop_script,
  395. .process_event = process_event,
  396. .generate_script = default_generate_script,
  397. };
  398. static struct scripting_ops *scripting_ops;
  399. static void setup_scripting(void)
  400. {
  401. setup_perl_scripting();
  402. setup_python_scripting();
  403. scripting_ops = &default_scripting_ops;
  404. }
  405. static int cleanup_scripting(void)
  406. {
  407. pr_debug("\nperf script stopped\n");
  408. return scripting_ops->stop_script();
  409. }
  410. static int process_sample_event(struct perf_tool *tool __maybe_unused,
  411. union perf_event *event,
  412. struct perf_sample *sample,
  413. struct perf_evsel *evsel,
  414. struct machine *machine)
  415. {
  416. struct addr_location al;
  417. struct thread *thread = machine__findnew_thread(machine, sample->pid,
  418. sample->tid);
  419. if (thread == NULL) {
  420. pr_debug("problem processing %d event, skipping it.\n",
  421. event->header.type);
  422. return -1;
  423. }
  424. if (debug_mode) {
  425. if (sample->time < last_timestamp) {
  426. pr_err("Samples misordered, previous: %" PRIu64
  427. " this: %" PRIu64 "\n", last_timestamp,
  428. sample->time);
  429. nr_unordered++;
  430. }
  431. last_timestamp = sample->time;
  432. return 0;
  433. }
  434. if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
  435. pr_err("problem processing %d event, skipping it.\n",
  436. event->header.type);
  437. return -1;
  438. }
  439. if (al.filtered)
  440. return 0;
  441. if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
  442. return 0;
  443. scripting_ops->process_event(event, sample, evsel, machine, thread, &al);
  444. evsel->hists.stats.total_period += sample->period;
  445. return 0;
  446. }
  447. static struct perf_tool perf_script = {
  448. .sample = process_sample_event,
  449. .mmap = perf_event__process_mmap,
  450. .comm = perf_event__process_comm,
  451. .exit = perf_event__process_exit,
  452. .fork = perf_event__process_fork,
  453. .attr = perf_event__process_attr,
  454. .tracing_data = perf_event__process_tracing_data,
  455. .build_id = perf_event__process_build_id,
  456. .ordered_samples = true,
  457. .ordering_requires_timestamps = true,
  458. };
  459. extern volatile int session_done;
  460. static void sig_handler(int sig __maybe_unused)
  461. {
  462. session_done = 1;
  463. }
  464. static int __cmd_script(struct perf_session *session)
  465. {
  466. int ret;
  467. signal(SIGINT, sig_handler);
  468. ret = perf_session__process_events(session, &perf_script);
  469. if (debug_mode)
  470. pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
  471. return ret;
  472. }
  473. struct script_spec {
  474. struct list_head node;
  475. struct scripting_ops *ops;
  476. char spec[0];
  477. };
  478. static LIST_HEAD(script_specs);
  479. static struct script_spec *script_spec__new(const char *spec,
  480. struct scripting_ops *ops)
  481. {
  482. struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
  483. if (s != NULL) {
  484. strcpy(s->spec, spec);
  485. s->ops = ops;
  486. }
  487. return s;
  488. }
  489. static void script_spec__add(struct script_spec *s)
  490. {
  491. list_add_tail(&s->node, &script_specs);
  492. }
  493. static struct script_spec *script_spec__find(const char *spec)
  494. {
  495. struct script_spec *s;
  496. list_for_each_entry(s, &script_specs, node)
  497. if (strcasecmp(s->spec, spec) == 0)
  498. return s;
  499. return NULL;
  500. }
  501. static struct script_spec *script_spec__findnew(const char *spec,
  502. struct scripting_ops *ops)
  503. {
  504. struct script_spec *s = script_spec__find(spec);
  505. if (s)
  506. return s;
  507. s = script_spec__new(spec, ops);
  508. if (!s)
  509. return NULL;
  510. script_spec__add(s);
  511. return s;
  512. }
  513. int script_spec_register(const char *spec, struct scripting_ops *ops)
  514. {
  515. struct script_spec *s;
  516. s = script_spec__find(spec);
  517. if (s)
  518. return -1;
  519. s = script_spec__findnew(spec, ops);
  520. if (!s)
  521. return -1;
  522. return 0;
  523. }
  524. static struct scripting_ops *script_spec__lookup(const char *spec)
  525. {
  526. struct script_spec *s = script_spec__find(spec);
  527. if (!s)
  528. return NULL;
  529. return s->ops;
  530. }
  531. static void list_available_languages(void)
  532. {
  533. struct script_spec *s;
  534. fprintf(stderr, "\n");
  535. fprintf(stderr, "Scripting language extensions (used in "
  536. "perf script -s [spec:]script.[spec]):\n\n");
  537. list_for_each_entry(s, &script_specs, node)
  538. fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
  539. fprintf(stderr, "\n");
  540. }
  541. static int parse_scriptname(const struct option *opt __maybe_unused,
  542. const char *str, int unset __maybe_unused)
  543. {
  544. char spec[PATH_MAX];
  545. const char *script, *ext;
  546. int len;
  547. if (strcmp(str, "lang") == 0) {
  548. list_available_languages();
  549. exit(0);
  550. }
  551. script = strchr(str, ':');
  552. if (script) {
  553. len = script - str;
  554. if (len >= PATH_MAX) {
  555. fprintf(stderr, "invalid language specifier");
  556. return -1;
  557. }
  558. strncpy(spec, str, len);
  559. spec[len] = '\0';
  560. scripting_ops = script_spec__lookup(spec);
  561. if (!scripting_ops) {
  562. fprintf(stderr, "invalid language specifier");
  563. return -1;
  564. }
  565. script++;
  566. } else {
  567. script = str;
  568. ext = strrchr(script, '.');
  569. if (!ext) {
  570. fprintf(stderr, "invalid script extension");
  571. return -1;
  572. }
  573. scripting_ops = script_spec__lookup(++ext);
  574. if (!scripting_ops) {
  575. fprintf(stderr, "invalid script extension");
  576. return -1;
  577. }
  578. }
  579. script_name = strdup(script);
  580. return 0;
  581. }
  582. static int parse_output_fields(const struct option *opt __maybe_unused,
  583. const char *arg, int unset __maybe_unused)
  584. {
  585. char *tok;
  586. int i, imax = ARRAY_SIZE(all_output_options);
  587. int j;
  588. int rc = 0;
  589. char *str = strdup(arg);
  590. int type = -1;
  591. if (!str)
  592. return -ENOMEM;
  593. /* first word can state for which event type the user is specifying
  594. * the fields. If no type exists, the specified fields apply to all
  595. * event types found in the file minus the invalid fields for a type.
  596. */
  597. tok = strchr(str, ':');
  598. if (tok) {
  599. *tok = '\0';
  600. tok++;
  601. if (!strcmp(str, "hw"))
  602. type = PERF_TYPE_HARDWARE;
  603. else if (!strcmp(str, "sw"))
  604. type = PERF_TYPE_SOFTWARE;
  605. else if (!strcmp(str, "trace"))
  606. type = PERF_TYPE_TRACEPOINT;
  607. else if (!strcmp(str, "raw"))
  608. type = PERF_TYPE_RAW;
  609. else {
  610. fprintf(stderr, "Invalid event type in field string.\n");
  611. rc = -EINVAL;
  612. goto out;
  613. }
  614. if (output[type].user_set)
  615. pr_warning("Overriding previous field request for %s events.\n",
  616. event_type(type));
  617. output[type].fields = 0;
  618. output[type].user_set = true;
  619. output[type].wildcard_set = false;
  620. } else {
  621. tok = str;
  622. if (strlen(str) == 0) {
  623. fprintf(stderr,
  624. "Cannot set fields to 'none' for all event types.\n");
  625. rc = -EINVAL;
  626. goto out;
  627. }
  628. if (output_set_by_user())
  629. pr_warning("Overriding previous field request for all events.\n");
  630. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  631. output[j].fields = 0;
  632. output[j].user_set = true;
  633. output[j].wildcard_set = true;
  634. }
  635. }
  636. tok = strtok(tok, ",");
  637. while (tok) {
  638. for (i = 0; i < imax; ++i) {
  639. if (strcmp(tok, all_output_options[i].str) == 0)
  640. break;
  641. }
  642. if (i == imax) {
  643. fprintf(stderr, "Invalid field requested.\n");
  644. rc = -EINVAL;
  645. goto out;
  646. }
  647. if (type == -1) {
  648. /* add user option to all events types for
  649. * which it is valid
  650. */
  651. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  652. if (output[j].invalid_fields & all_output_options[i].field) {
  653. pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
  654. all_output_options[i].str, event_type(j));
  655. } else
  656. output[j].fields |= all_output_options[i].field;
  657. }
  658. } else {
  659. if (output[type].invalid_fields & all_output_options[i].field) {
  660. fprintf(stderr, "\'%s\' not valid for %s events.\n",
  661. all_output_options[i].str, event_type(type));
  662. rc = -EINVAL;
  663. goto out;
  664. }
  665. output[type].fields |= all_output_options[i].field;
  666. }
  667. tok = strtok(NULL, ",");
  668. }
  669. if (type >= 0) {
  670. if (output[type].fields == 0) {
  671. pr_debug("No fields requested for %s type. "
  672. "Events will not be displayed.\n", event_type(type));
  673. }
  674. }
  675. out:
  676. free(str);
  677. return rc;
  678. }
  679. /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
  680. static int is_directory(const char *base_path, const struct dirent *dent)
  681. {
  682. char path[PATH_MAX];
  683. struct stat st;
  684. sprintf(path, "%s/%s", base_path, dent->d_name);
  685. if (stat(path, &st))
  686. return 0;
  687. return S_ISDIR(st.st_mode);
  688. }
  689. #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
  690. while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
  691. lang_next) \
  692. if ((lang_dirent.d_type == DT_DIR || \
  693. (lang_dirent.d_type == DT_UNKNOWN && \
  694. is_directory(scripts_path, &lang_dirent))) && \
  695. (strcmp(lang_dirent.d_name, ".")) && \
  696. (strcmp(lang_dirent.d_name, "..")))
  697. #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
  698. while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
  699. script_next) \
  700. if (script_dirent.d_type != DT_DIR && \
  701. (script_dirent.d_type != DT_UNKNOWN || \
  702. !is_directory(lang_path, &script_dirent)))
  703. #define RECORD_SUFFIX "-record"
  704. #define REPORT_SUFFIX "-report"
  705. struct script_desc {
  706. struct list_head node;
  707. char *name;
  708. char *half_liner;
  709. char *args;
  710. };
  711. static LIST_HEAD(script_descs);
  712. static struct script_desc *script_desc__new(const char *name)
  713. {
  714. struct script_desc *s = zalloc(sizeof(*s));
  715. if (s != NULL && name)
  716. s->name = strdup(name);
  717. return s;
  718. }
  719. static void script_desc__delete(struct script_desc *s)
  720. {
  721. free(s->name);
  722. free(s->half_liner);
  723. free(s->args);
  724. free(s);
  725. }
  726. static void script_desc__add(struct script_desc *s)
  727. {
  728. list_add_tail(&s->node, &script_descs);
  729. }
  730. static struct script_desc *script_desc__find(const char *name)
  731. {
  732. struct script_desc *s;
  733. list_for_each_entry(s, &script_descs, node)
  734. if (strcasecmp(s->name, name) == 0)
  735. return s;
  736. return NULL;
  737. }
  738. static struct script_desc *script_desc__findnew(const char *name)
  739. {
  740. struct script_desc *s = script_desc__find(name);
  741. if (s)
  742. return s;
  743. s = script_desc__new(name);
  744. if (!s)
  745. goto out_delete_desc;
  746. script_desc__add(s);
  747. return s;
  748. out_delete_desc:
  749. script_desc__delete(s);
  750. return NULL;
  751. }
  752. static const char *ends_with(const char *str, const char *suffix)
  753. {
  754. size_t suffix_len = strlen(suffix);
  755. const char *p = str;
  756. if (strlen(str) > suffix_len) {
  757. p = str + strlen(str) - suffix_len;
  758. if (!strncmp(p, suffix, suffix_len))
  759. return p;
  760. }
  761. return NULL;
  762. }
  763. static int read_script_info(struct script_desc *desc, const char *filename)
  764. {
  765. char line[BUFSIZ], *p;
  766. FILE *fp;
  767. fp = fopen(filename, "r");
  768. if (!fp)
  769. return -1;
  770. while (fgets(line, sizeof(line), fp)) {
  771. p = ltrim(line);
  772. if (strlen(p) == 0)
  773. continue;
  774. if (*p != '#')
  775. continue;
  776. p++;
  777. if (strlen(p) && *p == '!')
  778. continue;
  779. p = ltrim(p);
  780. if (strlen(p) && p[strlen(p) - 1] == '\n')
  781. p[strlen(p) - 1] = '\0';
  782. if (!strncmp(p, "description:", strlen("description:"))) {
  783. p += strlen("description:");
  784. desc->half_liner = strdup(ltrim(p));
  785. continue;
  786. }
  787. if (!strncmp(p, "args:", strlen("args:"))) {
  788. p += strlen("args:");
  789. desc->args = strdup(ltrim(p));
  790. continue;
  791. }
  792. }
  793. fclose(fp);
  794. return 0;
  795. }
  796. static char *get_script_root(struct dirent *script_dirent, const char *suffix)
  797. {
  798. char *script_root, *str;
  799. script_root = strdup(script_dirent->d_name);
  800. if (!script_root)
  801. return NULL;
  802. str = (char *)ends_with(script_root, suffix);
  803. if (!str) {
  804. free(script_root);
  805. return NULL;
  806. }
  807. *str = '\0';
  808. return script_root;
  809. }
  810. static int list_available_scripts(const struct option *opt __maybe_unused,
  811. const char *s __maybe_unused,
  812. int unset __maybe_unused)
  813. {
  814. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  815. char scripts_path[MAXPATHLEN];
  816. DIR *scripts_dir, *lang_dir;
  817. char script_path[MAXPATHLEN];
  818. char lang_path[MAXPATHLEN];
  819. struct script_desc *desc;
  820. char first_half[BUFSIZ];
  821. char *script_root;
  822. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  823. scripts_dir = opendir(scripts_path);
  824. if (!scripts_dir)
  825. return -1;
  826. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  827. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  828. lang_dirent.d_name);
  829. lang_dir = opendir(lang_path);
  830. if (!lang_dir)
  831. continue;
  832. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  833. script_root = get_script_root(&script_dirent, REPORT_SUFFIX);
  834. if (script_root) {
  835. desc = script_desc__findnew(script_root);
  836. snprintf(script_path, MAXPATHLEN, "%s/%s",
  837. lang_path, script_dirent.d_name);
  838. read_script_info(desc, script_path);
  839. free(script_root);
  840. }
  841. }
  842. }
  843. fprintf(stdout, "List of available trace scripts:\n");
  844. list_for_each_entry(desc, &script_descs, node) {
  845. sprintf(first_half, "%s %s", desc->name,
  846. desc->args ? desc->args : "");
  847. fprintf(stdout, " %-36s %s\n", first_half,
  848. desc->half_liner ? desc->half_liner : "");
  849. }
  850. exit(0);
  851. }
  852. /*
  853. * Some scripts specify the required events in their "xxx-record" file,
  854. * this function will check if the events in perf.data match those
  855. * mentioned in the "xxx-record".
  856. *
  857. * Fixme: All existing "xxx-record" are all in good formats "-e event ",
  858. * which is covered well now. And new parsing code should be added to
  859. * cover the future complexing formats like event groups etc.
  860. */
  861. static int check_ev_match(char *dir_name, char *scriptname,
  862. struct perf_session *session)
  863. {
  864. char filename[MAXPATHLEN], evname[128];
  865. char line[BUFSIZ], *p;
  866. struct perf_evsel *pos;
  867. int match, len;
  868. FILE *fp;
  869. sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
  870. fp = fopen(filename, "r");
  871. if (!fp)
  872. return -1;
  873. while (fgets(line, sizeof(line), fp)) {
  874. p = ltrim(line);
  875. if (*p == '#')
  876. continue;
  877. while (strlen(p)) {
  878. p = strstr(p, "-e");
  879. if (!p)
  880. break;
  881. p += 2;
  882. p = ltrim(p);
  883. len = strcspn(p, " \t");
  884. if (!len)
  885. break;
  886. snprintf(evname, len + 1, "%s", p);
  887. match = 0;
  888. list_for_each_entry(pos,
  889. &session->evlist->entries, node) {
  890. if (!strcmp(perf_evsel__name(pos), evname)) {
  891. match = 1;
  892. break;
  893. }
  894. }
  895. if (!match) {
  896. fclose(fp);
  897. return -1;
  898. }
  899. }
  900. }
  901. fclose(fp);
  902. return 0;
  903. }
  904. /*
  905. * Return -1 if none is found, otherwise the actual scripts number.
  906. *
  907. * Currently the only user of this function is the script browser, which
  908. * will list all statically runnable scripts, select one, execute it and
  909. * show the output in a perf browser.
  910. */
  911. int find_scripts(char **scripts_array, char **scripts_path_array)
  912. {
  913. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  914. char scripts_path[MAXPATHLEN], lang_path[MAXPATHLEN];
  915. DIR *scripts_dir, *lang_dir;
  916. struct perf_session *session;
  917. char *temp;
  918. int i = 0;
  919. session = perf_session__new(input_name, O_RDONLY, 0, false, NULL);
  920. if (!session)
  921. return -1;
  922. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  923. scripts_dir = opendir(scripts_path);
  924. if (!scripts_dir) {
  925. perf_session__delete(session);
  926. return -1;
  927. }
  928. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  929. snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
  930. lang_dirent.d_name);
  931. #ifdef NO_LIBPERL
  932. if (strstr(lang_path, "perl"))
  933. continue;
  934. #endif
  935. #ifdef NO_LIBPYTHON
  936. if (strstr(lang_path, "python"))
  937. continue;
  938. #endif
  939. lang_dir = opendir(lang_path);
  940. if (!lang_dir)
  941. continue;
  942. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  943. /* Skip those real time scripts: xxxtop.p[yl] */
  944. if (strstr(script_dirent.d_name, "top."))
  945. continue;
  946. sprintf(scripts_path_array[i], "%s/%s", lang_path,
  947. script_dirent.d_name);
  948. temp = strchr(script_dirent.d_name, '.');
  949. snprintf(scripts_array[i],
  950. (temp - script_dirent.d_name) + 1,
  951. "%s", script_dirent.d_name);
  952. if (check_ev_match(lang_path,
  953. scripts_array[i], session))
  954. continue;
  955. i++;
  956. }
  957. closedir(lang_dir);
  958. }
  959. closedir(scripts_dir);
  960. perf_session__delete(session);
  961. return i;
  962. }
  963. static char *get_script_path(const char *script_root, const char *suffix)
  964. {
  965. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  966. char scripts_path[MAXPATHLEN];
  967. char script_path[MAXPATHLEN];
  968. DIR *scripts_dir, *lang_dir;
  969. char lang_path[MAXPATHLEN];
  970. char *__script_root;
  971. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  972. scripts_dir = opendir(scripts_path);
  973. if (!scripts_dir)
  974. return NULL;
  975. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  976. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  977. lang_dirent.d_name);
  978. lang_dir = opendir(lang_path);
  979. if (!lang_dir)
  980. continue;
  981. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  982. __script_root = get_script_root(&script_dirent, suffix);
  983. if (__script_root && !strcmp(script_root, __script_root)) {
  984. free(__script_root);
  985. closedir(lang_dir);
  986. closedir(scripts_dir);
  987. snprintf(script_path, MAXPATHLEN, "%s/%s",
  988. lang_path, script_dirent.d_name);
  989. return strdup(script_path);
  990. }
  991. free(__script_root);
  992. }
  993. closedir(lang_dir);
  994. }
  995. closedir(scripts_dir);
  996. return NULL;
  997. }
  998. static bool is_top_script(const char *script_path)
  999. {
  1000. return ends_with(script_path, "top") == NULL ? false : true;
  1001. }
  1002. static int has_required_arg(char *script_path)
  1003. {
  1004. struct script_desc *desc;
  1005. int n_args = 0;
  1006. char *p;
  1007. desc = script_desc__new(NULL);
  1008. if (read_script_info(desc, script_path))
  1009. goto out;
  1010. if (!desc->args)
  1011. goto out;
  1012. for (p = desc->args; *p; p++)
  1013. if (*p == '<')
  1014. n_args++;
  1015. out:
  1016. script_desc__delete(desc);
  1017. return n_args;
  1018. }
  1019. static int have_cmd(int argc, const char **argv)
  1020. {
  1021. char **__argv = malloc(sizeof(const char *) * argc);
  1022. if (!__argv) {
  1023. pr_err("malloc failed\n");
  1024. return -1;
  1025. }
  1026. memcpy(__argv, argv, sizeof(const char *) * argc);
  1027. argc = parse_options(argc, (const char **)__argv, record_options,
  1028. NULL, PARSE_OPT_STOP_AT_NON_OPTION);
  1029. free(__argv);
  1030. system_wide = (argc == 0);
  1031. return 0;
  1032. }
  1033. int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
  1034. {
  1035. bool show_full_info = false;
  1036. char *rec_script_path = NULL;
  1037. char *rep_script_path = NULL;
  1038. struct perf_session *session;
  1039. char *script_path = NULL;
  1040. const char **__argv;
  1041. int i, j, err;
  1042. const struct option options[] = {
  1043. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1044. "dump raw trace in ASCII"),
  1045. OPT_INCR('v', "verbose", &verbose,
  1046. "be more verbose (show symbol address, etc)"),
  1047. OPT_BOOLEAN('L', "Latency", &latency_format,
  1048. "show latency attributes (irqs/preemption disabled, etc)"),
  1049. OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
  1050. list_available_scripts),
  1051. OPT_CALLBACK('s', "script", NULL, "name",
  1052. "script file name (lang:script name, script name, or *)",
  1053. parse_scriptname),
  1054. OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
  1055. "generate perf-script.xx script in specified language"),
  1056. OPT_STRING('i', "input", &input_name, "file", "input file name"),
  1057. OPT_BOOLEAN('d', "debug-mode", &debug_mode,
  1058. "do various checks like samples ordering and lost events"),
  1059. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  1060. "file", "vmlinux pathname"),
  1061. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  1062. "file", "kallsyms pathname"),
  1063. OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
  1064. "When printing symbols do not display call chain"),
  1065. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  1066. "Look for files with symbols relative to this directory"),
  1067. OPT_CALLBACK('f', "fields", NULL, "str",
  1068. "comma separated output fields prepend with 'type:'. "
  1069. "Valid types: hw,sw,trace,raw. "
  1070. "Fields: comm,tid,pid,time,cpu,event,trace,ip,sym,dso,"
  1071. "addr,symoff", parse_output_fields),
  1072. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  1073. "system-wide collection from all CPUs"),
  1074. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  1075. "only consider these symbols"),
  1076. OPT_STRING('C', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
  1077. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  1078. "only display events for these comms"),
  1079. OPT_BOOLEAN('I', "show-info", &show_full_info,
  1080. "display extended information from perf.data file"),
  1081. OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
  1082. "Show the path of [kernel.kallsyms]"),
  1083. OPT_END()
  1084. };
  1085. const char * const script_usage[] = {
  1086. "perf script [<options>]",
  1087. "perf script [<options>] record <script> [<record-options>] <command>",
  1088. "perf script [<options>] report <script> [script-args]",
  1089. "perf script [<options>] <script> [<record-options>] <command>",
  1090. "perf script [<options>] <top-script> [script-args]",
  1091. NULL
  1092. };
  1093. setup_scripting();
  1094. argc = parse_options(argc, argv, options, script_usage,
  1095. PARSE_OPT_STOP_AT_NON_OPTION);
  1096. if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
  1097. rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
  1098. if (!rec_script_path)
  1099. return cmd_record(argc, argv, NULL);
  1100. }
  1101. if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
  1102. rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
  1103. if (!rep_script_path) {
  1104. fprintf(stderr,
  1105. "Please specify a valid report script"
  1106. "(see 'perf script -l' for listing)\n");
  1107. return -1;
  1108. }
  1109. }
  1110. /* make sure PERF_EXEC_PATH is set for scripts */
  1111. perf_set_argv_exec_path(perf_exec_path());
  1112. if (argc && !script_name && !rec_script_path && !rep_script_path) {
  1113. int live_pipe[2];
  1114. int rep_args;
  1115. pid_t pid;
  1116. rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
  1117. rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
  1118. if (!rec_script_path && !rep_script_path) {
  1119. fprintf(stderr, " Couldn't find script %s\n\n See perf"
  1120. " script -l for available scripts.\n", argv[0]);
  1121. usage_with_options(script_usage, options);
  1122. }
  1123. if (is_top_script(argv[0])) {
  1124. rep_args = argc - 1;
  1125. } else {
  1126. int rec_args;
  1127. rep_args = has_required_arg(rep_script_path);
  1128. rec_args = (argc - 1) - rep_args;
  1129. if (rec_args < 0) {
  1130. fprintf(stderr, " %s script requires options."
  1131. "\n\n See perf script -l for available "
  1132. "scripts and options.\n", argv[0]);
  1133. usage_with_options(script_usage, options);
  1134. }
  1135. }
  1136. if (pipe(live_pipe) < 0) {
  1137. perror("failed to create pipe");
  1138. return -1;
  1139. }
  1140. pid = fork();
  1141. if (pid < 0) {
  1142. perror("failed to fork");
  1143. return -1;
  1144. }
  1145. if (!pid) {
  1146. j = 0;
  1147. dup2(live_pipe[1], 1);
  1148. close(live_pipe[0]);
  1149. if (is_top_script(argv[0])) {
  1150. system_wide = true;
  1151. } else if (!system_wide) {
  1152. if (have_cmd(argc - rep_args, &argv[rep_args]) != 0) {
  1153. err = -1;
  1154. goto out;
  1155. }
  1156. }
  1157. __argv = malloc((argc + 6) * sizeof(const char *));
  1158. if (!__argv) {
  1159. pr_err("malloc failed\n");
  1160. err = -ENOMEM;
  1161. goto out;
  1162. }
  1163. __argv[j++] = "/bin/sh";
  1164. __argv[j++] = rec_script_path;
  1165. if (system_wide)
  1166. __argv[j++] = "-a";
  1167. __argv[j++] = "-q";
  1168. __argv[j++] = "-o";
  1169. __argv[j++] = "-";
  1170. for (i = rep_args + 1; i < argc; i++)
  1171. __argv[j++] = argv[i];
  1172. __argv[j++] = NULL;
  1173. execvp("/bin/sh", (char **)__argv);
  1174. free(__argv);
  1175. exit(-1);
  1176. }
  1177. dup2(live_pipe[0], 0);
  1178. close(live_pipe[1]);
  1179. __argv = malloc((argc + 4) * sizeof(const char *));
  1180. if (!__argv) {
  1181. pr_err("malloc failed\n");
  1182. err = -ENOMEM;
  1183. goto out;
  1184. }
  1185. j = 0;
  1186. __argv[j++] = "/bin/sh";
  1187. __argv[j++] = rep_script_path;
  1188. for (i = 1; i < rep_args + 1; i++)
  1189. __argv[j++] = argv[i];
  1190. __argv[j++] = "-i";
  1191. __argv[j++] = "-";
  1192. __argv[j++] = NULL;
  1193. execvp("/bin/sh", (char **)__argv);
  1194. free(__argv);
  1195. exit(-1);
  1196. }
  1197. if (rec_script_path)
  1198. script_path = rec_script_path;
  1199. if (rep_script_path)
  1200. script_path = rep_script_path;
  1201. if (script_path) {
  1202. j = 0;
  1203. if (!rec_script_path)
  1204. system_wide = false;
  1205. else if (!system_wide) {
  1206. if (have_cmd(argc - 1, &argv[1]) != 0) {
  1207. err = -1;
  1208. goto out;
  1209. }
  1210. }
  1211. __argv = malloc((argc + 2) * sizeof(const char *));
  1212. if (!__argv) {
  1213. pr_err("malloc failed\n");
  1214. err = -ENOMEM;
  1215. goto out;
  1216. }
  1217. __argv[j++] = "/bin/sh";
  1218. __argv[j++] = script_path;
  1219. if (system_wide)
  1220. __argv[j++] = "-a";
  1221. for (i = 2; i < argc; i++)
  1222. __argv[j++] = argv[i];
  1223. __argv[j++] = NULL;
  1224. execvp("/bin/sh", (char **)__argv);
  1225. free(__argv);
  1226. exit(-1);
  1227. }
  1228. if (symbol__init() < 0)
  1229. return -1;
  1230. if (!script_name)
  1231. setup_pager();
  1232. session = perf_session__new(input_name, O_RDONLY, 0, false,
  1233. &perf_script);
  1234. if (session == NULL)
  1235. return -ENOMEM;
  1236. if (cpu_list) {
  1237. if (perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap))
  1238. return -1;
  1239. }
  1240. if (!script_name && !generate_script_lang)
  1241. perf_session__fprintf_info(session, stdout, show_full_info);
  1242. if (!no_callchain)
  1243. symbol_conf.use_callchain = true;
  1244. else
  1245. symbol_conf.use_callchain = false;
  1246. if (generate_script_lang) {
  1247. struct stat perf_stat;
  1248. int input;
  1249. if (output_set_by_user()) {
  1250. fprintf(stderr,
  1251. "custom fields not supported for generated scripts");
  1252. return -1;
  1253. }
  1254. input = open(session->filename, O_RDONLY); /* input_name */
  1255. if (input < 0) {
  1256. perror("failed to open file");
  1257. return -1;
  1258. }
  1259. err = fstat(input, &perf_stat);
  1260. if (err < 0) {
  1261. perror("failed to stat file");
  1262. return -1;
  1263. }
  1264. if (!perf_stat.st_size) {
  1265. fprintf(stderr, "zero-sized file, nothing to do!\n");
  1266. return 0;
  1267. }
  1268. scripting_ops = script_spec__lookup(generate_script_lang);
  1269. if (!scripting_ops) {
  1270. fprintf(stderr, "invalid language specifier");
  1271. return -1;
  1272. }
  1273. err = scripting_ops->generate_script(session->pevent,
  1274. "perf-script");
  1275. goto out;
  1276. }
  1277. if (script_name) {
  1278. err = scripting_ops->start_script(script_name, argc, argv);
  1279. if (err)
  1280. goto out;
  1281. pr_debug("perf script started with script %s\n\n", script_name);
  1282. }
  1283. err = perf_session__check_output_opt(session);
  1284. if (err < 0)
  1285. goto out;
  1286. err = __cmd_script(session);
  1287. perf_session__delete(session);
  1288. cleanup_scripting();
  1289. out:
  1290. return err;
  1291. }