builtin-script.c 30 KB

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