builtin-script.c 31 KB

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