parse-events.y 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. %define api.pure full
  2. %parse-param {void *_parse_state}
  3. %parse-param {void *scanner}
  4. %lex-param {void* scanner}
  5. %locations
  6. %{
  7. #define YYDEBUG 1
  8. #include <fnmatch.h>
  9. #include <stdio.h>
  10. #include <linux/compiler.h>
  11. #include <linux/types.h>
  12. #include "pmu.h"
  13. #include "evsel.h"
  14. #include "parse-events.h"
  15. #include "parse-events-bison.h"
  16. void parse_events_error(YYLTYPE *loc, void *parse_state, void *scanner, char const *msg);
  17. #define ABORT_ON(val) \
  18. do { \
  19. if (val) \
  20. YYABORT; \
  21. } while (0)
  22. #define ALLOC_LIST(list) \
  23. do { \
  24. list = malloc(sizeof(*list)); \
  25. ABORT_ON(!list); \
  26. INIT_LIST_HEAD(list); \
  27. } while (0)
  28. static void inc_group_count(struct list_head *list,
  29. struct parse_events_state *parse_state)
  30. {
  31. /* Count groups only have more than 1 members */
  32. if (!list_is_last(list->next, list))
  33. parse_state->nr_groups++;
  34. }
  35. %}
  36. %token PE_START_EVENTS PE_START_TERMS
  37. %token PE_VALUE PE_VALUE_SYM_HW PE_VALUE_SYM_SW PE_RAW PE_TERM
  38. %token PE_VALUE_SYM_TOOL
  39. %token PE_EVENT_NAME
  40. %token PE_NAME
  41. %token PE_BPF_OBJECT PE_BPF_SOURCE
  42. %token PE_MODIFIER_EVENT PE_MODIFIER_BP
  43. %token PE_NAME_CACHE_TYPE PE_NAME_CACHE_OP_RESULT
  44. %token PE_PREFIX_MEM PE_PREFIX_RAW PE_PREFIX_GROUP
  45. %token PE_ERROR
  46. %token PE_PMU_EVENT_PRE PE_PMU_EVENT_SUF PE_KERNEL_PMU_EVENT
  47. %token PE_ARRAY_ALL PE_ARRAY_RANGE
  48. %token PE_DRV_CFG_TERM
  49. %type <num> PE_VALUE
  50. %type <num> PE_VALUE_SYM_HW
  51. %type <num> PE_VALUE_SYM_SW
  52. %type <num> PE_VALUE_SYM_TOOL
  53. %type <num> PE_RAW
  54. %type <num> PE_TERM
  55. %type <str> PE_NAME
  56. %type <str> PE_BPF_OBJECT
  57. %type <str> PE_BPF_SOURCE
  58. %type <str> PE_NAME_CACHE_TYPE
  59. %type <str> PE_NAME_CACHE_OP_RESULT
  60. %type <str> PE_MODIFIER_EVENT
  61. %type <str> PE_MODIFIER_BP
  62. %type <str> PE_EVENT_NAME
  63. %type <str> PE_PMU_EVENT_PRE PE_PMU_EVENT_SUF PE_KERNEL_PMU_EVENT
  64. %type <str> PE_DRV_CFG_TERM
  65. %type <num> value_sym
  66. %type <head> event_config
  67. %type <head> opt_event_config
  68. %type <head> opt_pmu_config
  69. %type <term> event_term
  70. %type <head> event_pmu
  71. %type <head> event_legacy_symbol
  72. %type <head> event_legacy_cache
  73. %type <head> event_legacy_mem
  74. %type <head> event_legacy_tracepoint
  75. %type <tracepoint_name> tracepoint_name
  76. %type <head> event_legacy_numeric
  77. %type <head> event_legacy_raw
  78. %type <head> event_bpf_file
  79. %type <head> event_def
  80. %type <head> event_mod
  81. %type <head> event_name
  82. %type <head> event
  83. %type <head> events
  84. %type <head> group_def
  85. %type <head> group
  86. %type <head> groups
  87. %type <array> array
  88. %type <array> array_term
  89. %type <array> array_terms
  90. %union
  91. {
  92. char *str;
  93. u64 num;
  94. struct list_head *head;
  95. struct parse_events_term *term;
  96. struct tracepoint_name {
  97. char *sys;
  98. char *event;
  99. } tracepoint_name;
  100. struct parse_events_array array;
  101. }
  102. %%
  103. start:
  104. PE_START_EVENTS start_events
  105. |
  106. PE_START_TERMS start_terms
  107. start_events: groups
  108. {
  109. struct parse_events_state *parse_state = _parse_state;
  110. parse_events_update_lists($1, &parse_state->list);
  111. }
  112. groups:
  113. groups ',' group
  114. {
  115. struct list_head *list = $1;
  116. struct list_head *group = $3;
  117. parse_events_update_lists(group, list);
  118. $$ = list;
  119. }
  120. |
  121. groups ',' event
  122. {
  123. struct list_head *list = $1;
  124. struct list_head *event = $3;
  125. parse_events_update_lists(event, list);
  126. $$ = list;
  127. }
  128. |
  129. group
  130. |
  131. event
  132. group:
  133. group_def ':' PE_MODIFIER_EVENT
  134. {
  135. struct list_head *list = $1;
  136. ABORT_ON(parse_events__modifier_group(list, $3));
  137. $$ = list;
  138. }
  139. |
  140. group_def
  141. group_def:
  142. PE_NAME '{' events '}'
  143. {
  144. struct list_head *list = $3;
  145. inc_group_count(list, _parse_state);
  146. parse_events__set_leader($1, list, _parse_state);
  147. $$ = list;
  148. }
  149. |
  150. '{' events '}'
  151. {
  152. struct list_head *list = $2;
  153. inc_group_count(list, _parse_state);
  154. parse_events__set_leader(NULL, list, _parse_state);
  155. $$ = list;
  156. }
  157. events:
  158. events ',' event
  159. {
  160. struct list_head *event = $3;
  161. struct list_head *list = $1;
  162. parse_events_update_lists(event, list);
  163. $$ = list;
  164. }
  165. |
  166. event
  167. event: event_mod
  168. event_mod:
  169. event_name PE_MODIFIER_EVENT
  170. {
  171. struct list_head *list = $1;
  172. /*
  173. * Apply modifier on all events added by single event definition
  174. * (there could be more events added for multiple tracepoint
  175. * definitions via '*?'.
  176. */
  177. ABORT_ON(parse_events__modifier_event(list, $2, false));
  178. $$ = list;
  179. }
  180. |
  181. event_name
  182. event_name:
  183. PE_EVENT_NAME event_def
  184. {
  185. ABORT_ON(parse_events_name($2, $1));
  186. free($1);
  187. $$ = $2;
  188. }
  189. |
  190. event_def
  191. event_def: event_pmu |
  192. event_legacy_symbol |
  193. event_legacy_cache sep_dc |
  194. event_legacy_mem |
  195. event_legacy_tracepoint sep_dc |
  196. event_legacy_numeric sep_dc |
  197. event_legacy_raw sep_dc |
  198. event_bpf_file
  199. event_pmu:
  200. PE_NAME opt_pmu_config
  201. {
  202. struct parse_events_state *parse_state = _parse_state;
  203. struct parse_events_error *error = parse_state->error;
  204. struct list_head *list, *orig_terms, *terms;
  205. if (parse_events_copy_term_list($2, &orig_terms))
  206. YYABORT;
  207. if (error)
  208. error->idx = @1.first_column;
  209. ALLOC_LIST(list);
  210. if (parse_events_add_pmu(_parse_state, list, $1, $2, false, false)) {
  211. struct perf_pmu *pmu = NULL;
  212. int ok = 0;
  213. char *pattern;
  214. if (asprintf(&pattern, "%s*", $1) < 0)
  215. YYABORT;
  216. while ((pmu = perf_pmu__scan(pmu)) != NULL) {
  217. char *name = pmu->name;
  218. if (!strncmp(name, "uncore_", 7) &&
  219. strncmp($1, "uncore_", 7))
  220. name += 7;
  221. if (!fnmatch(pattern, name, 0)) {
  222. if (parse_events_copy_term_list(orig_terms, &terms)) {
  223. free(pattern);
  224. YYABORT;
  225. }
  226. if (!parse_events_add_pmu(_parse_state, list, pmu->name, terms, true, false))
  227. ok++;
  228. parse_events_terms__delete(terms);
  229. }
  230. }
  231. free(pattern);
  232. if (!ok)
  233. YYABORT;
  234. }
  235. parse_events_terms__delete($2);
  236. parse_events_terms__delete(orig_terms);
  237. $$ = list;
  238. }
  239. |
  240. PE_KERNEL_PMU_EVENT sep_dc
  241. {
  242. struct list_head *list;
  243. if (parse_events_multi_pmu_add(_parse_state, $1, &list) < 0)
  244. YYABORT;
  245. $$ = list;
  246. }
  247. |
  248. PE_PMU_EVENT_PRE '-' PE_PMU_EVENT_SUF sep_dc
  249. {
  250. struct list_head *list;
  251. char pmu_name[128];
  252. snprintf(&pmu_name, 128, "%s-%s", $1, $3);
  253. if (parse_events_multi_pmu_add(_parse_state, pmu_name, &list) < 0)
  254. YYABORT;
  255. $$ = list;
  256. }
  257. value_sym:
  258. PE_VALUE_SYM_HW
  259. |
  260. PE_VALUE_SYM_SW
  261. event_legacy_symbol:
  262. value_sym '/' event_config '/'
  263. {
  264. struct list_head *list;
  265. int type = $1 >> 16;
  266. int config = $1 & 255;
  267. ALLOC_LIST(list);
  268. ABORT_ON(parse_events_add_numeric(_parse_state, list, type, config, $3));
  269. parse_events_terms__delete($3);
  270. $$ = list;
  271. }
  272. |
  273. value_sym sep_slash_slash_dc
  274. {
  275. struct list_head *list;
  276. int type = $1 >> 16;
  277. int config = $1 & 255;
  278. ALLOC_LIST(list);
  279. ABORT_ON(parse_events_add_numeric(_parse_state, list, type, config, NULL));
  280. $$ = list;
  281. }
  282. |
  283. PE_VALUE_SYM_TOOL sep_slash_slash_dc
  284. {
  285. struct list_head *list;
  286. ALLOC_LIST(list);
  287. ABORT_ON(parse_events_add_tool(_parse_state, list, $1));
  288. $$ = list;
  289. }
  290. event_legacy_cache:
  291. PE_NAME_CACHE_TYPE '-' PE_NAME_CACHE_OP_RESULT '-' PE_NAME_CACHE_OP_RESULT opt_event_config
  292. {
  293. struct parse_events_state *parse_state = _parse_state;
  294. struct parse_events_error *error = parse_state->error;
  295. struct list_head *list;
  296. ALLOC_LIST(list);
  297. ABORT_ON(parse_events_add_cache(list, &parse_state->idx, $1, $3, $5, error, $6));
  298. parse_events_terms__delete($6);
  299. $$ = list;
  300. }
  301. |
  302. PE_NAME_CACHE_TYPE '-' PE_NAME_CACHE_OP_RESULT opt_event_config
  303. {
  304. struct parse_events_state *parse_state = _parse_state;
  305. struct parse_events_error *error = parse_state->error;
  306. struct list_head *list;
  307. ALLOC_LIST(list);
  308. ABORT_ON(parse_events_add_cache(list, &parse_state->idx, $1, $3, NULL, error, $4));
  309. parse_events_terms__delete($4);
  310. $$ = list;
  311. }
  312. |
  313. PE_NAME_CACHE_TYPE opt_event_config
  314. {
  315. struct parse_events_state *parse_state = _parse_state;
  316. struct parse_events_error *error = parse_state->error;
  317. struct list_head *list;
  318. ALLOC_LIST(list);
  319. ABORT_ON(parse_events_add_cache(list, &parse_state->idx, $1, NULL, NULL, error, $2));
  320. parse_events_terms__delete($2);
  321. $$ = list;
  322. }
  323. event_legacy_mem:
  324. PE_PREFIX_MEM PE_VALUE '/' PE_VALUE ':' PE_MODIFIER_BP sep_dc
  325. {
  326. struct parse_events_state *parse_state = _parse_state;
  327. struct list_head *list;
  328. ALLOC_LIST(list);
  329. ABORT_ON(parse_events_add_breakpoint(list, &parse_state->idx,
  330. (void *) $2, $6, $4));
  331. $$ = list;
  332. }
  333. |
  334. PE_PREFIX_MEM PE_VALUE '/' PE_VALUE sep_dc
  335. {
  336. struct parse_events_state *parse_state = _parse_state;
  337. struct list_head *list;
  338. ALLOC_LIST(list);
  339. ABORT_ON(parse_events_add_breakpoint(list, &parse_state->idx,
  340. (void *) $2, NULL, $4));
  341. $$ = list;
  342. }
  343. |
  344. PE_PREFIX_MEM PE_VALUE ':' PE_MODIFIER_BP sep_dc
  345. {
  346. struct parse_events_state *parse_state = _parse_state;
  347. struct list_head *list;
  348. ALLOC_LIST(list);
  349. ABORT_ON(parse_events_add_breakpoint(list, &parse_state->idx,
  350. (void *) $2, $4, 0));
  351. $$ = list;
  352. }
  353. |
  354. PE_PREFIX_MEM PE_VALUE sep_dc
  355. {
  356. struct parse_events_state *parse_state = _parse_state;
  357. struct list_head *list;
  358. ALLOC_LIST(list);
  359. ABORT_ON(parse_events_add_breakpoint(list, &parse_state->idx,
  360. (void *) $2, NULL, 0));
  361. $$ = list;
  362. }
  363. event_legacy_tracepoint:
  364. tracepoint_name opt_event_config
  365. {
  366. struct parse_events_state *parse_state = _parse_state;
  367. struct parse_events_error *error = parse_state->error;
  368. struct list_head *list;
  369. ALLOC_LIST(list);
  370. if (error)
  371. error->idx = @1.first_column;
  372. if (parse_events_add_tracepoint(list, &parse_state->idx, $1.sys, $1.event,
  373. error, $2))
  374. return -1;
  375. $$ = list;
  376. }
  377. tracepoint_name:
  378. PE_NAME '-' PE_NAME ':' PE_NAME
  379. {
  380. char sys_name[128];
  381. struct tracepoint_name tracepoint;
  382. snprintf(&sys_name, 128, "%s-%s", $1, $3);
  383. tracepoint.sys = &sys_name;
  384. tracepoint.event = $5;
  385. $$ = tracepoint;
  386. }
  387. |
  388. PE_NAME ':' PE_NAME
  389. {
  390. struct tracepoint_name tracepoint = {$1, $3};
  391. $$ = tracepoint;
  392. }
  393. event_legacy_numeric:
  394. PE_VALUE ':' PE_VALUE opt_event_config
  395. {
  396. struct list_head *list;
  397. ALLOC_LIST(list);
  398. ABORT_ON(parse_events_add_numeric(_parse_state, list, (u32)$1, $3, $4));
  399. parse_events_terms__delete($4);
  400. $$ = list;
  401. }
  402. event_legacy_raw:
  403. PE_RAW opt_event_config
  404. {
  405. struct list_head *list;
  406. ALLOC_LIST(list);
  407. ABORT_ON(parse_events_add_numeric(_parse_state, list, PERF_TYPE_RAW, $1, $2));
  408. parse_events_terms__delete($2);
  409. $$ = list;
  410. }
  411. event_bpf_file:
  412. PE_BPF_OBJECT opt_event_config
  413. {
  414. struct parse_events_state *parse_state = _parse_state;
  415. struct list_head *list;
  416. ALLOC_LIST(list);
  417. ABORT_ON(parse_events_load_bpf(parse_state, list, $1, false, $2));
  418. parse_events_terms__delete($2);
  419. $$ = list;
  420. }
  421. |
  422. PE_BPF_SOURCE opt_event_config
  423. {
  424. struct list_head *list;
  425. ALLOC_LIST(list);
  426. ABORT_ON(parse_events_load_bpf(_parse_state, list, $1, true, $2));
  427. parse_events_terms__delete($2);
  428. $$ = list;
  429. }
  430. opt_event_config:
  431. '/' event_config '/'
  432. {
  433. $$ = $2;
  434. }
  435. |
  436. '/' '/'
  437. {
  438. $$ = NULL;
  439. }
  440. |
  441. {
  442. $$ = NULL;
  443. }
  444. opt_pmu_config:
  445. '/' event_config '/'
  446. {
  447. $$ = $2;
  448. }
  449. |
  450. '/' '/'
  451. {
  452. $$ = NULL;
  453. }
  454. start_terms: event_config
  455. {
  456. struct parse_events_state *parse_state = _parse_state;
  457. parse_state->terms = $1;
  458. }
  459. event_config:
  460. event_config ',' event_term
  461. {
  462. struct list_head *head = $1;
  463. struct parse_events_term *term = $3;
  464. ABORT_ON(!head);
  465. list_add_tail(&term->list, head);
  466. $$ = $1;
  467. }
  468. |
  469. event_term
  470. {
  471. struct list_head *head = malloc(sizeof(*head));
  472. struct parse_events_term *term = $1;
  473. ABORT_ON(!head);
  474. INIT_LIST_HEAD(head);
  475. list_add_tail(&term->list, head);
  476. $$ = head;
  477. }
  478. event_term:
  479. PE_NAME '=' PE_NAME
  480. {
  481. struct parse_events_term *term;
  482. ABORT_ON(parse_events_term__str(&term, PARSE_EVENTS__TERM_TYPE_USER,
  483. $1, $3, &@1, &@3));
  484. $$ = term;
  485. }
  486. |
  487. PE_NAME '=' PE_VALUE
  488. {
  489. struct parse_events_term *term;
  490. ABORT_ON(parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
  491. $1, $3, false, &@1, &@3));
  492. $$ = term;
  493. }
  494. |
  495. PE_NAME '=' PE_VALUE_SYM_HW
  496. {
  497. struct parse_events_term *term;
  498. int config = $3 & 255;
  499. ABORT_ON(parse_events_term__sym_hw(&term, $1, config));
  500. $$ = term;
  501. }
  502. |
  503. PE_NAME
  504. {
  505. struct parse_events_term *term;
  506. ABORT_ON(parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
  507. $1, 1, true, &@1, NULL));
  508. $$ = term;
  509. }
  510. |
  511. PE_VALUE_SYM_HW
  512. {
  513. struct parse_events_term *term;
  514. int config = $1 & 255;
  515. ABORT_ON(parse_events_term__sym_hw(&term, NULL, config));
  516. $$ = term;
  517. }
  518. |
  519. PE_TERM '=' PE_NAME
  520. {
  521. struct parse_events_term *term;
  522. ABORT_ON(parse_events_term__str(&term, (int)$1, NULL, $3, &@1, &@3));
  523. $$ = term;
  524. }
  525. |
  526. PE_TERM '=' PE_VALUE
  527. {
  528. struct parse_events_term *term;
  529. ABORT_ON(parse_events_term__num(&term, (int)$1, NULL, $3, false, &@1, &@3));
  530. $$ = term;
  531. }
  532. |
  533. PE_TERM
  534. {
  535. struct parse_events_term *term;
  536. ABORT_ON(parse_events_term__num(&term, (int)$1, NULL, 1, true, &@1, NULL));
  537. $$ = term;
  538. }
  539. |
  540. PE_NAME array '=' PE_NAME
  541. {
  542. struct parse_events_term *term;
  543. ABORT_ON(parse_events_term__str(&term, PARSE_EVENTS__TERM_TYPE_USER,
  544. $1, $4, &@1, &@4));
  545. term->array = $2;
  546. $$ = term;
  547. }
  548. |
  549. PE_NAME array '=' PE_VALUE
  550. {
  551. struct parse_events_term *term;
  552. ABORT_ON(parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
  553. $1, $4, false, &@1, &@4));
  554. term->array = $2;
  555. $$ = term;
  556. }
  557. |
  558. PE_DRV_CFG_TERM
  559. {
  560. struct parse_events_term *term;
  561. ABORT_ON(parse_events_term__str(&term, PARSE_EVENTS__TERM_TYPE_DRV_CFG,
  562. $1, $1, &@1, NULL));
  563. $$ = term;
  564. }
  565. array:
  566. '[' array_terms ']'
  567. {
  568. $$ = $2;
  569. }
  570. |
  571. PE_ARRAY_ALL
  572. {
  573. $$.nr_ranges = 0;
  574. $$.ranges = NULL;
  575. }
  576. array_terms:
  577. array_terms ',' array_term
  578. {
  579. struct parse_events_array new_array;
  580. new_array.nr_ranges = $1.nr_ranges + $3.nr_ranges;
  581. new_array.ranges = malloc(sizeof(new_array.ranges[0]) *
  582. new_array.nr_ranges);
  583. ABORT_ON(!new_array.ranges);
  584. memcpy(&new_array.ranges[0], $1.ranges,
  585. $1.nr_ranges * sizeof(new_array.ranges[0]));
  586. memcpy(&new_array.ranges[$1.nr_ranges], $3.ranges,
  587. $3.nr_ranges * sizeof(new_array.ranges[0]));
  588. free($1.ranges);
  589. free($3.ranges);
  590. $$ = new_array;
  591. }
  592. |
  593. array_term
  594. array_term:
  595. PE_VALUE
  596. {
  597. struct parse_events_array array;
  598. array.nr_ranges = 1;
  599. array.ranges = malloc(sizeof(array.ranges[0]));
  600. ABORT_ON(!array.ranges);
  601. array.ranges[0].start = $1;
  602. array.ranges[0].length = 1;
  603. $$ = array;
  604. }
  605. |
  606. PE_VALUE PE_ARRAY_RANGE PE_VALUE
  607. {
  608. struct parse_events_array array;
  609. ABORT_ON($3 < $1);
  610. array.nr_ranges = 1;
  611. array.ranges = malloc(sizeof(array.ranges[0]));
  612. ABORT_ON(!array.ranges);
  613. array.ranges[0].start = $1;
  614. array.ranges[0].length = $3 - $1 + 1;
  615. $$ = array;
  616. }
  617. sep_dc: ':' |
  618. sep_slash_slash_dc: '/' '/' | ':' |
  619. %%
  620. void parse_events_error(YYLTYPE *loc, void *parse_state,
  621. void *scanner __maybe_unused,
  622. char const *msg __maybe_unused)
  623. {
  624. parse_events_evlist_error(parse_state, loc->last_column, "parser error");
  625. }