builtin-script.c 37 KB

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