builtin-script.c 32 KB

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