builtin-script.c 31 KB

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