builtin-script.c 32 KB

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