builtin-script.c 31 KB

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