parse-events.y 13 KB

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