builtin-script.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  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, 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(union perf_event *event,
  361. struct perf_sample *sample,
  362. struct perf_evsel *evsel,
  363. struct perf_session *session)
  364. {
  365. struct thread *thread = perf_session__findnew(session, event->ip.pid);
  366. if (thread == NULL) {
  367. pr_debug("problem processing %d event, skipping it.\n",
  368. event->header.type);
  369. return -1;
  370. }
  371. if (debug_mode) {
  372. if (sample->time < last_timestamp) {
  373. pr_err("Samples misordered, previous: %" PRIu64
  374. " this: %" PRIu64 "\n", last_timestamp,
  375. sample->time);
  376. nr_unordered++;
  377. }
  378. last_timestamp = sample->time;
  379. return 0;
  380. }
  381. if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
  382. return 0;
  383. scripting_ops->process_event(event, sample, evsel, session, thread);
  384. session->hists.stats.total_period += sample->period;
  385. return 0;
  386. }
  387. static struct perf_event_ops event_ops = {
  388. .sample = process_sample_event,
  389. .mmap = perf_event__process_mmap,
  390. .comm = perf_event__process_comm,
  391. .exit = perf_event__process_task,
  392. .fork = perf_event__process_task,
  393. .attr = perf_event__process_attr,
  394. .event_type = perf_event__process_event_type,
  395. .tracing_data = perf_event__process_tracing_data,
  396. .build_id = perf_event__process_build_id,
  397. .ordered_samples = true,
  398. .ordering_requires_timestamps = true,
  399. };
  400. extern volatile int session_done;
  401. static void sig_handler(int sig __unused)
  402. {
  403. session_done = 1;
  404. }
  405. static int __cmd_script(struct perf_session *session)
  406. {
  407. int ret;
  408. signal(SIGINT, sig_handler);
  409. ret = perf_session__process_events(session, &event_ops);
  410. if (debug_mode)
  411. pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered);
  412. return ret;
  413. }
  414. struct script_spec {
  415. struct list_head node;
  416. struct scripting_ops *ops;
  417. char spec[0];
  418. };
  419. static LIST_HEAD(script_specs);
  420. static struct script_spec *script_spec__new(const char *spec,
  421. struct scripting_ops *ops)
  422. {
  423. struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
  424. if (s != NULL) {
  425. strcpy(s->spec, spec);
  426. s->ops = ops;
  427. }
  428. return s;
  429. }
  430. static void script_spec__delete(struct script_spec *s)
  431. {
  432. free(s->spec);
  433. free(s);
  434. }
  435. static void script_spec__add(struct script_spec *s)
  436. {
  437. list_add_tail(&s->node, &script_specs);
  438. }
  439. static struct script_spec *script_spec__find(const char *spec)
  440. {
  441. struct script_spec *s;
  442. list_for_each_entry(s, &script_specs, node)
  443. if (strcasecmp(s->spec, spec) == 0)
  444. return s;
  445. return NULL;
  446. }
  447. static struct script_spec *script_spec__findnew(const char *spec,
  448. struct scripting_ops *ops)
  449. {
  450. struct script_spec *s = script_spec__find(spec);
  451. if (s)
  452. return s;
  453. s = script_spec__new(spec, ops);
  454. if (!s)
  455. goto out_delete_spec;
  456. script_spec__add(s);
  457. return s;
  458. out_delete_spec:
  459. script_spec__delete(s);
  460. return NULL;
  461. }
  462. int script_spec_register(const char *spec, struct scripting_ops *ops)
  463. {
  464. struct script_spec *s;
  465. s = script_spec__find(spec);
  466. if (s)
  467. return -1;
  468. s = script_spec__findnew(spec, ops);
  469. if (!s)
  470. return -1;
  471. return 0;
  472. }
  473. static struct scripting_ops *script_spec__lookup(const char *spec)
  474. {
  475. struct script_spec *s = script_spec__find(spec);
  476. if (!s)
  477. return NULL;
  478. return s->ops;
  479. }
  480. static void list_available_languages(void)
  481. {
  482. struct script_spec *s;
  483. fprintf(stderr, "\n");
  484. fprintf(stderr, "Scripting language extensions (used in "
  485. "perf script -s [spec:]script.[spec]):\n\n");
  486. list_for_each_entry(s, &script_specs, node)
  487. fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
  488. fprintf(stderr, "\n");
  489. }
  490. static int parse_scriptname(const struct option *opt __used,
  491. const char *str, int unset __used)
  492. {
  493. char spec[PATH_MAX];
  494. const char *script, *ext;
  495. int len;
  496. if (strcmp(str, "lang") == 0) {
  497. list_available_languages();
  498. exit(0);
  499. }
  500. script = strchr(str, ':');
  501. if (script) {
  502. len = script - str;
  503. if (len >= PATH_MAX) {
  504. fprintf(stderr, "invalid language specifier");
  505. return -1;
  506. }
  507. strncpy(spec, str, len);
  508. spec[len] = '\0';
  509. scripting_ops = script_spec__lookup(spec);
  510. if (!scripting_ops) {
  511. fprintf(stderr, "invalid language specifier");
  512. return -1;
  513. }
  514. script++;
  515. } else {
  516. script = str;
  517. ext = strrchr(script, '.');
  518. if (!ext) {
  519. fprintf(stderr, "invalid script extension");
  520. return -1;
  521. }
  522. scripting_ops = script_spec__lookup(++ext);
  523. if (!scripting_ops) {
  524. fprintf(stderr, "invalid script extension");
  525. return -1;
  526. }
  527. }
  528. script_name = strdup(script);
  529. return 0;
  530. }
  531. static int parse_output_fields(const struct option *opt __used,
  532. const char *arg, int unset __used)
  533. {
  534. char *tok;
  535. int i, imax = sizeof(all_output_options) / sizeof(struct output_option);
  536. int j;
  537. int rc = 0;
  538. char *str = strdup(arg);
  539. int type = -1;
  540. if (!str)
  541. return -ENOMEM;
  542. /* first word can state for which event type the user is specifying
  543. * the fields. If no type exists, the specified fields apply to all
  544. * event types found in the file minus the invalid fields for a type.
  545. */
  546. tok = strchr(str, ':');
  547. if (tok) {
  548. *tok = '\0';
  549. tok++;
  550. if (!strcmp(str, "hw"))
  551. type = PERF_TYPE_HARDWARE;
  552. else if (!strcmp(str, "sw"))
  553. type = PERF_TYPE_SOFTWARE;
  554. else if (!strcmp(str, "trace"))
  555. type = PERF_TYPE_TRACEPOINT;
  556. else if (!strcmp(str, "raw"))
  557. type = PERF_TYPE_RAW;
  558. else {
  559. fprintf(stderr, "Invalid event type in field string.\n");
  560. return -EINVAL;
  561. }
  562. if (output[type].user_set)
  563. pr_warning("Overriding previous field request for %s events.\n",
  564. event_type(type));
  565. output[type].fields = 0;
  566. output[type].user_set = true;
  567. output[type].wildcard_set = false;
  568. } else {
  569. tok = str;
  570. if (strlen(str) == 0) {
  571. fprintf(stderr,
  572. "Cannot set fields to 'none' for all event types.\n");
  573. rc = -EINVAL;
  574. goto out;
  575. }
  576. if (output_set_by_user())
  577. pr_warning("Overriding previous field request for all events.\n");
  578. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  579. output[j].fields = 0;
  580. output[j].user_set = true;
  581. output[j].wildcard_set = true;
  582. }
  583. }
  584. tok = strtok(tok, ",");
  585. while (tok) {
  586. for (i = 0; i < imax; ++i) {
  587. if (strcmp(tok, all_output_options[i].str) == 0)
  588. break;
  589. }
  590. if (i == imax) {
  591. fprintf(stderr, "Invalid field requested.\n");
  592. rc = -EINVAL;
  593. goto out;
  594. }
  595. if (type == -1) {
  596. /* add user option to all events types for
  597. * which it is valid
  598. */
  599. for (j = 0; j < PERF_TYPE_MAX; ++j) {
  600. if (output[j].invalid_fields & all_output_options[i].field) {
  601. pr_warning("\'%s\' not valid for %s events. Ignoring.\n",
  602. all_output_options[i].str, event_type(j));
  603. } else
  604. output[j].fields |= all_output_options[i].field;
  605. }
  606. } else {
  607. if (output[type].invalid_fields & all_output_options[i].field) {
  608. fprintf(stderr, "\'%s\' not valid for %s events.\n",
  609. all_output_options[i].str, event_type(type));
  610. rc = -EINVAL;
  611. goto out;
  612. }
  613. output[type].fields |= all_output_options[i].field;
  614. }
  615. tok = strtok(NULL, ",");
  616. }
  617. if (type >= 0) {
  618. if (output[type].fields == 0) {
  619. pr_debug("No fields requested for %s type. "
  620. "Events will not be displayed.\n", event_type(type));
  621. }
  622. }
  623. out:
  624. free(str);
  625. return rc;
  626. }
  627. /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
  628. static int is_directory(const char *base_path, const struct dirent *dent)
  629. {
  630. char path[PATH_MAX];
  631. struct stat st;
  632. sprintf(path, "%s/%s", base_path, dent->d_name);
  633. if (stat(path, &st))
  634. return 0;
  635. return S_ISDIR(st.st_mode);
  636. }
  637. #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\
  638. while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \
  639. lang_next) \
  640. if ((lang_dirent.d_type == DT_DIR || \
  641. (lang_dirent.d_type == DT_UNKNOWN && \
  642. is_directory(scripts_path, &lang_dirent))) && \
  643. (strcmp(lang_dirent.d_name, ".")) && \
  644. (strcmp(lang_dirent.d_name, "..")))
  645. #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\
  646. while (!readdir_r(lang_dir, &script_dirent, &script_next) && \
  647. script_next) \
  648. if (script_dirent.d_type != DT_DIR && \
  649. (script_dirent.d_type != DT_UNKNOWN || \
  650. !is_directory(lang_path, &script_dirent)))
  651. #define RECORD_SUFFIX "-record"
  652. #define REPORT_SUFFIX "-report"
  653. struct script_desc {
  654. struct list_head node;
  655. char *name;
  656. char *half_liner;
  657. char *args;
  658. };
  659. static LIST_HEAD(script_descs);
  660. static struct script_desc *script_desc__new(const char *name)
  661. {
  662. struct script_desc *s = zalloc(sizeof(*s));
  663. if (s != NULL && name)
  664. s->name = strdup(name);
  665. return s;
  666. }
  667. static void script_desc__delete(struct script_desc *s)
  668. {
  669. free(s->name);
  670. free(s->half_liner);
  671. free(s->args);
  672. free(s);
  673. }
  674. static void script_desc__add(struct script_desc *s)
  675. {
  676. list_add_tail(&s->node, &script_descs);
  677. }
  678. static struct script_desc *script_desc__find(const char *name)
  679. {
  680. struct script_desc *s;
  681. list_for_each_entry(s, &script_descs, node)
  682. if (strcasecmp(s->name, name) == 0)
  683. return s;
  684. return NULL;
  685. }
  686. static struct script_desc *script_desc__findnew(const char *name)
  687. {
  688. struct script_desc *s = script_desc__find(name);
  689. if (s)
  690. return s;
  691. s = script_desc__new(name);
  692. if (!s)
  693. goto out_delete_desc;
  694. script_desc__add(s);
  695. return s;
  696. out_delete_desc:
  697. script_desc__delete(s);
  698. return NULL;
  699. }
  700. static const char *ends_with(const char *str, const char *suffix)
  701. {
  702. size_t suffix_len = strlen(suffix);
  703. const char *p = str;
  704. if (strlen(str) > suffix_len) {
  705. p = str + strlen(str) - suffix_len;
  706. if (!strncmp(p, suffix, suffix_len))
  707. return p;
  708. }
  709. return NULL;
  710. }
  711. static char *ltrim(char *str)
  712. {
  713. int len = strlen(str);
  714. while (len && isspace(*str)) {
  715. len--;
  716. str++;
  717. }
  718. return str;
  719. }
  720. static int read_script_info(struct script_desc *desc, const char *filename)
  721. {
  722. char line[BUFSIZ], *p;
  723. FILE *fp;
  724. fp = fopen(filename, "r");
  725. if (!fp)
  726. return -1;
  727. while (fgets(line, sizeof(line), fp)) {
  728. p = ltrim(line);
  729. if (strlen(p) == 0)
  730. continue;
  731. if (*p != '#')
  732. continue;
  733. p++;
  734. if (strlen(p) && *p == '!')
  735. continue;
  736. p = ltrim(p);
  737. if (strlen(p) && p[strlen(p) - 1] == '\n')
  738. p[strlen(p) - 1] = '\0';
  739. if (!strncmp(p, "description:", strlen("description:"))) {
  740. p += strlen("description:");
  741. desc->half_liner = strdup(ltrim(p));
  742. continue;
  743. }
  744. if (!strncmp(p, "args:", strlen("args:"))) {
  745. p += strlen("args:");
  746. desc->args = strdup(ltrim(p));
  747. continue;
  748. }
  749. }
  750. fclose(fp);
  751. return 0;
  752. }
  753. static int list_available_scripts(const struct option *opt __used,
  754. const char *s __used, int unset __used)
  755. {
  756. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  757. char scripts_path[MAXPATHLEN];
  758. DIR *scripts_dir, *lang_dir;
  759. char script_path[MAXPATHLEN];
  760. char lang_path[MAXPATHLEN];
  761. struct script_desc *desc;
  762. char first_half[BUFSIZ];
  763. char *script_root;
  764. char *str;
  765. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  766. scripts_dir = opendir(scripts_path);
  767. if (!scripts_dir)
  768. return -1;
  769. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  770. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  771. lang_dirent.d_name);
  772. lang_dir = opendir(lang_path);
  773. if (!lang_dir)
  774. continue;
  775. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  776. script_root = strdup(script_dirent.d_name);
  777. str = (char *)ends_with(script_root, REPORT_SUFFIX);
  778. if (str) {
  779. *str = '\0';
  780. desc = script_desc__findnew(script_root);
  781. snprintf(script_path, MAXPATHLEN, "%s/%s",
  782. lang_path, script_dirent.d_name);
  783. read_script_info(desc, script_path);
  784. }
  785. free(script_root);
  786. }
  787. }
  788. fprintf(stdout, "List of available trace scripts:\n");
  789. list_for_each_entry(desc, &script_descs, node) {
  790. sprintf(first_half, "%s %s", desc->name,
  791. desc->args ? desc->args : "");
  792. fprintf(stdout, " %-36s %s\n", first_half,
  793. desc->half_liner ? desc->half_liner : "");
  794. }
  795. exit(0);
  796. }
  797. static char *get_script_path(const char *script_root, const char *suffix)
  798. {
  799. struct dirent *script_next, *lang_next, script_dirent, lang_dirent;
  800. char scripts_path[MAXPATHLEN];
  801. char script_path[MAXPATHLEN];
  802. DIR *scripts_dir, *lang_dir;
  803. char lang_path[MAXPATHLEN];
  804. char *str, *__script_root;
  805. char *path = NULL;
  806. snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path());
  807. scripts_dir = opendir(scripts_path);
  808. if (!scripts_dir)
  809. return NULL;
  810. for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) {
  811. snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
  812. lang_dirent.d_name);
  813. lang_dir = opendir(lang_path);
  814. if (!lang_dir)
  815. continue;
  816. for_each_script(lang_path, lang_dir, script_dirent, script_next) {
  817. __script_root = strdup(script_dirent.d_name);
  818. str = (char *)ends_with(__script_root, suffix);
  819. if (str) {
  820. *str = '\0';
  821. if (strcmp(__script_root, script_root))
  822. continue;
  823. snprintf(script_path, MAXPATHLEN, "%s/%s",
  824. lang_path, script_dirent.d_name);
  825. path = strdup(script_path);
  826. free(__script_root);
  827. break;
  828. }
  829. free(__script_root);
  830. }
  831. }
  832. return path;
  833. }
  834. static bool is_top_script(const char *script_path)
  835. {
  836. return ends_with(script_path, "top") == NULL ? false : true;
  837. }
  838. static int has_required_arg(char *script_path)
  839. {
  840. struct script_desc *desc;
  841. int n_args = 0;
  842. char *p;
  843. desc = script_desc__new(NULL);
  844. if (read_script_info(desc, script_path))
  845. goto out;
  846. if (!desc->args)
  847. goto out;
  848. for (p = desc->args; *p; p++)
  849. if (*p == '<')
  850. n_args++;
  851. out:
  852. script_desc__delete(desc);
  853. return n_args;
  854. }
  855. static const char * const script_usage[] = {
  856. "perf script [<options>]",
  857. "perf script [<options>] record <script> [<record-options>] <command>",
  858. "perf script [<options>] report <script> [script-args]",
  859. "perf script [<options>] <script> [<record-options>] <command>",
  860. "perf script [<options>] <top-script> [script-args]",
  861. NULL
  862. };
  863. static const struct option options[] = {
  864. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  865. "dump raw trace in ASCII"),
  866. OPT_INCR('v', "verbose", &verbose,
  867. "be more verbose (show symbol address, etc)"),
  868. OPT_BOOLEAN('L', "Latency", &latency_format,
  869. "show latency attributes (irqs/preemption disabled, etc)"),
  870. OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts",
  871. list_available_scripts),
  872. OPT_CALLBACK('s', "script", NULL, "name",
  873. "script file name (lang:script name, script name, or *)",
  874. parse_scriptname),
  875. OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
  876. "generate perf-script.xx script in specified language"),
  877. OPT_STRING('i', "input", &input_name, "file",
  878. "input file name"),
  879. OPT_BOOLEAN('d', "debug-mode", &debug_mode,
  880. "do various checks like samples ordering and lost events"),
  881. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  882. "file", "vmlinux pathname"),
  883. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  884. "file", "kallsyms pathname"),
  885. OPT_BOOLEAN('G', "hide-call-graph", &no_callchain,
  886. "When printing symbols do not display call chain"),
  887. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  888. "Look for files with symbols relative to this directory"),
  889. OPT_CALLBACK('f', "fields", NULL, "str",
  890. "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",
  891. parse_output_fields),
  892. OPT_STRING('c', "cpu", &cpu_list, "cpu", "list of cpus to profile"),
  893. OPT_BOOLEAN('I', "show-info", &show_full_info,
  894. "display extended information from perf.data file"),
  895. OPT_END()
  896. };
  897. static bool have_cmd(int argc, const char **argv)
  898. {
  899. char **__argv = malloc(sizeof(const char *) * argc);
  900. if (!__argv)
  901. die("malloc");
  902. memcpy(__argv, argv, sizeof(const char *) * argc);
  903. argc = parse_options(argc, (const char **)__argv, record_options,
  904. NULL, PARSE_OPT_STOP_AT_NON_OPTION);
  905. free(__argv);
  906. return argc != 0;
  907. }
  908. int cmd_script(int argc, const char **argv, const char *prefix __used)
  909. {
  910. char *rec_script_path = NULL;
  911. char *rep_script_path = NULL;
  912. struct perf_session *session;
  913. char *script_path = NULL;
  914. const char **__argv;
  915. bool system_wide;
  916. int i, j, err;
  917. setup_scripting();
  918. argc = parse_options(argc, argv, options, script_usage,
  919. PARSE_OPT_STOP_AT_NON_OPTION);
  920. if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) {
  921. rec_script_path = get_script_path(argv[1], RECORD_SUFFIX);
  922. if (!rec_script_path)
  923. return cmd_record(argc, argv, NULL);
  924. }
  925. if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) {
  926. rep_script_path = get_script_path(argv[1], REPORT_SUFFIX);
  927. if (!rep_script_path) {
  928. fprintf(stderr,
  929. "Please specify a valid report script"
  930. "(see 'perf script -l' for listing)\n");
  931. return -1;
  932. }
  933. }
  934. /* make sure PERF_EXEC_PATH is set for scripts */
  935. perf_set_argv_exec_path(perf_exec_path());
  936. if (argc && !script_name && !rec_script_path && !rep_script_path) {
  937. int live_pipe[2];
  938. int rep_args;
  939. pid_t pid;
  940. rec_script_path = get_script_path(argv[0], RECORD_SUFFIX);
  941. rep_script_path = get_script_path(argv[0], REPORT_SUFFIX);
  942. if (!rec_script_path && !rep_script_path) {
  943. fprintf(stderr, " Couldn't find script %s\n\n See perf"
  944. " script -l for available scripts.\n", argv[0]);
  945. usage_with_options(script_usage, options);
  946. }
  947. if (is_top_script(argv[0])) {
  948. rep_args = argc - 1;
  949. } else {
  950. int rec_args;
  951. rep_args = has_required_arg(rep_script_path);
  952. rec_args = (argc - 1) - rep_args;
  953. if (rec_args < 0) {
  954. fprintf(stderr, " %s script requires options."
  955. "\n\n See perf script -l for available "
  956. "scripts and options.\n", argv[0]);
  957. usage_with_options(script_usage, options);
  958. }
  959. }
  960. if (pipe(live_pipe) < 0) {
  961. perror("failed to create pipe");
  962. exit(-1);
  963. }
  964. pid = fork();
  965. if (pid < 0) {
  966. perror("failed to fork");
  967. exit(-1);
  968. }
  969. if (!pid) {
  970. system_wide = true;
  971. j = 0;
  972. dup2(live_pipe[1], 1);
  973. close(live_pipe[0]);
  974. if (!is_top_script(argv[0]))
  975. system_wide = !have_cmd(argc - rep_args,
  976. &argv[rep_args]);
  977. __argv = malloc((argc + 6) * sizeof(const char *));
  978. if (!__argv)
  979. die("malloc");
  980. __argv[j++] = "/bin/sh";
  981. __argv[j++] = rec_script_path;
  982. if (system_wide)
  983. __argv[j++] = "-a";
  984. __argv[j++] = "-q";
  985. __argv[j++] = "-o";
  986. __argv[j++] = "-";
  987. for (i = rep_args + 1; i < argc; i++)
  988. __argv[j++] = argv[i];
  989. __argv[j++] = NULL;
  990. execvp("/bin/sh", (char **)__argv);
  991. free(__argv);
  992. exit(-1);
  993. }
  994. dup2(live_pipe[0], 0);
  995. close(live_pipe[1]);
  996. __argv = malloc((argc + 4) * sizeof(const char *));
  997. if (!__argv)
  998. die("malloc");
  999. j = 0;
  1000. __argv[j++] = "/bin/sh";
  1001. __argv[j++] = rep_script_path;
  1002. for (i = 1; i < rep_args + 1; i++)
  1003. __argv[j++] = argv[i];
  1004. __argv[j++] = "-i";
  1005. __argv[j++] = "-";
  1006. __argv[j++] = NULL;
  1007. execvp("/bin/sh", (char **)__argv);
  1008. free(__argv);
  1009. exit(-1);
  1010. }
  1011. if (rec_script_path)
  1012. script_path = rec_script_path;
  1013. if (rep_script_path)
  1014. script_path = rep_script_path;
  1015. if (script_path) {
  1016. system_wide = false;
  1017. j = 0;
  1018. if (rec_script_path)
  1019. system_wide = !have_cmd(argc - 1, &argv[1]);
  1020. __argv = malloc((argc + 2) * sizeof(const char *));
  1021. if (!__argv)
  1022. die("malloc");
  1023. __argv[j++] = "/bin/sh";
  1024. __argv[j++] = script_path;
  1025. if (system_wide)
  1026. __argv[j++] = "-a";
  1027. for (i = 2; i < argc; i++)
  1028. __argv[j++] = argv[i];
  1029. __argv[j++] = NULL;
  1030. execvp("/bin/sh", (char **)__argv);
  1031. free(__argv);
  1032. exit(-1);
  1033. }
  1034. if (symbol__init() < 0)
  1035. return -1;
  1036. if (!script_name)
  1037. setup_pager();
  1038. session = perf_session__new(input_name, O_RDONLY, 0, false, &event_ops);
  1039. if (session == NULL)
  1040. return -ENOMEM;
  1041. if (cpu_list) {
  1042. if (perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap))
  1043. return -1;
  1044. }
  1045. perf_session__fprintf_info(session, stdout, show_full_info);
  1046. if (!no_callchain)
  1047. symbol_conf.use_callchain = true;
  1048. else
  1049. symbol_conf.use_callchain = false;
  1050. if (generate_script_lang) {
  1051. struct stat perf_stat;
  1052. int input;
  1053. if (output_set_by_user()) {
  1054. fprintf(stderr,
  1055. "custom fields not supported for generated scripts");
  1056. return -1;
  1057. }
  1058. input = open(input_name, O_RDONLY);
  1059. if (input < 0) {
  1060. perror("failed to open file");
  1061. exit(-1);
  1062. }
  1063. err = fstat(input, &perf_stat);
  1064. if (err < 0) {
  1065. perror("failed to stat file");
  1066. exit(-1);
  1067. }
  1068. if (!perf_stat.st_size) {
  1069. fprintf(stderr, "zero-sized file, nothing to do!\n");
  1070. exit(0);
  1071. }
  1072. scripting_ops = script_spec__lookup(generate_script_lang);
  1073. if (!scripting_ops) {
  1074. fprintf(stderr, "invalid language specifier");
  1075. return -1;
  1076. }
  1077. err = scripting_ops->generate_script("perf-script");
  1078. goto out;
  1079. }
  1080. if (script_name) {
  1081. err = scripting_ops->start_script(script_name, argc, argv);
  1082. if (err)
  1083. goto out;
  1084. pr_debug("perf script started with script %s\n\n", script_name);
  1085. }
  1086. err = perf_session__check_output_opt(session);
  1087. if (err < 0)
  1088. goto out;
  1089. err = __cmd_script(session);
  1090. perf_session__delete(session);
  1091. cleanup_scripting();
  1092. out:
  1093. return err;
  1094. }