builtin-script.c 32 KB

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