builtin-script.c 31 KB

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