parse-events.y 13.1 KB
Newer Older
1
%pure-parser
2
%parse-param {void *_data}
3 4
%parse-param {void *scanner}
%lex-param {void* scanner}
5
%locations
6 7 8 9 10 11 12

%{

#define YYDEBUG 1

#include <linux/compiler.h>
#include <linux/list.h>
B
Borislav Petkov 已提交
13
#include <linux/types.h>
14 15
#include "util.h"
#include "parse-events.h"
16
#include "parse-events-bison.h"
17 18 19 20 21 22 23

#define ABORT_ON(val) \
do { \
	if (val) \
		YYABORT; \
} while (0)

24 25 26 27 28 29 30
#define ALLOC_LIST(list) \
do { \
	list = malloc(sizeof(*list)); \
	ABORT_ON(!list);              \
	INIT_LIST_HEAD(list);         \
} while (0)

31
static void inc_group_count(struct list_head *list,
32 33 34 35 36 37 38
		       struct parse_events_evlist *data)
{
	/* Count groups only have more than 1 members */
	if (!list_is_last(list->next, list))
		data->nr_groups++;
}

39 40
%}

41
%token PE_START_EVENTS PE_START_TERMS
42
%token PE_VALUE PE_VALUE_SYM_HW PE_VALUE_SYM_SW PE_RAW PE_TERM
43
%token PE_EVENT_NAME
44
%token PE_NAME
45
%token PE_BPF_OBJECT PE_BPF_SOURCE
46 47
%token PE_MODIFIER_EVENT PE_MODIFIER_BP
%token PE_NAME_CACHE_TYPE PE_NAME_CACHE_OP_RESULT
48
%token PE_PREFIX_MEM PE_PREFIX_RAW PE_PREFIX_GROUP
49
%token PE_ERROR
50
%token PE_PMU_EVENT_PRE PE_PMU_EVENT_SUF PE_KERNEL_PMU_EVENT
51
%token PE_ARRAY_ALL PE_ARRAY_RANGE
52
%token PE_DRV_CFG_TERM
53
%type <num> PE_VALUE
54 55
%type <num> PE_VALUE_SYM_HW
%type <num> PE_VALUE_SYM_SW
56
%type <num> PE_RAW
57
%type <num> PE_TERM
58
%type <str> PE_NAME
59
%type <str> PE_BPF_OBJECT
60
%type <str> PE_BPF_SOURCE
61 62 63 64
%type <str> PE_NAME_CACHE_TYPE
%type <str> PE_NAME_CACHE_OP_RESULT
%type <str> PE_MODIFIER_EVENT
%type <str> PE_MODIFIER_BP
65
%type <str> PE_EVENT_NAME
66
%type <str> PE_PMU_EVENT_PRE PE_PMU_EVENT_SUF PE_KERNEL_PMU_EVENT
67
%type <str> PE_DRV_CFG_TERM
68
%type <num> value_sym
69
%type <head> event_config
70
%type <head> opt_event_config
71
%type <term> event_term
72 73 74 75 76
%type <head> event_pmu
%type <head> event_legacy_symbol
%type <head> event_legacy_cache
%type <head> event_legacy_mem
%type <head> event_legacy_tracepoint
77
%type <tracepoint_name> tracepoint_name
78 79
%type <head> event_legacy_numeric
%type <head> event_legacy_raw
80
%type <head> event_bpf_file
81
%type <head> event_def
82 83
%type <head> event_mod
%type <head> event_name
84 85 86 87 88
%type <head> event
%type <head> events
%type <head> group_def
%type <head> group
%type <head> groups
89 90 91
%type <array> array
%type <array> array_term
%type <array> array_terms
92 93 94 95

%union
{
	char *str;
96
	u64 num;
97
	struct list_head *head;
98
	struct parse_events_term *term;
99 100 101 102
	struct tracepoint_name {
		char *sys;
		char *event;
	} tracepoint_name;
103
	struct parse_events_array array;
104 105 106
}
%%

107
start:
108
PE_START_EVENTS start_events
109
|
110 111 112 113
PE_START_TERMS  start_terms

start_events: groups
{
114
	struct parse_events_evlist *data = _data;
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

	parse_events_update_lists($1, &data->list);
}

groups:
groups ',' group
{
	struct list_head *list  = $1;
	struct list_head *group = $3;

	parse_events_update_lists(group, list);
	$$ = list;
}
|
groups ',' event
{
	struct list_head *list  = $1;
	struct list_head *event = $3;

	parse_events_update_lists(event, list);
	$$ = list;
}
|
group
|
event

group:
group_def ':' PE_MODIFIER_EVENT
{
	struct list_head *list = $1;

	ABORT_ON(parse_events__modifier_group(list, $3));
	$$ = list;
}
|
group_def

group_def:
PE_NAME '{' events '}'
{
	struct list_head *list = $3;

158
	inc_group_count(list, _data);
159
	parse_events__set_leader($1, list);
160 161 162 163 164 165 166
	$$ = list;
}
|
'{' events '}'
{
	struct list_head *list = $2;

167
	inc_group_count(list, _data);
168
	parse_events__set_leader(NULL, list);
169 170
	$$ = list;
}
171

172
events:
173 174 175 176 177 178 179 180 181 182
events ',' event
{
	struct list_head *event = $3;
	struct list_head *list  = $1;

	parse_events_update_lists(event, list);
	$$ = list;
}
|
event
183

184 185 186 187
event: event_mod

event_mod:
event_name PE_MODIFIER_EVENT
188
{
189
	struct list_head *list = $1;
190

191 192 193 194 195
	/*
	 * Apply modifier on all events added by single event definition
	 * (there could be more events added for multiple tracepoint
	 * definitions via '*?'.
	 */
196
	ABORT_ON(parse_events__modifier_event(list, $2, false));
197
	$$ = list;
198 199
}
|
200 201 202 203 204 205 206 207 208 209
event_name

event_name:
PE_EVENT_NAME event_def
{
	ABORT_ON(parse_events_name($2, $1));
	free($1);
	$$ = $2;
}
|
210 211
event_def

212 213
event_def: event_pmu |
	   event_legacy_symbol |
214 215 216 217
	   event_legacy_cache sep_dc |
	   event_legacy_mem |
	   event_legacy_tracepoint sep_dc |
	   event_legacy_numeric sep_dc |
218 219
	   event_legacy_raw sep_dc |
	   event_bpf_file
220

221
event_pmu:
222
PE_NAME opt_event_config
223
{
224
	struct parse_events_evlist *data = _data;
225
	struct list_head *list;
226

227
	ALLOC_LIST(list);
228 229
	ABORT_ON(parse_events_add_pmu(data, list, $1, $2));
	parse_events_terms__delete($2);
230
	$$ = list;
231
}
232
|
233 234 235 236 237 238 239 240 241
PE_KERNEL_PMU_EVENT sep_dc
{
	struct parse_events_evlist *data = _data;
	struct list_head *head;
	struct parse_events_term *term;
	struct list_head *list;

	ALLOC_LIST(head);
	ABORT_ON(parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
242
					$1, 1, &@1, NULL));
243 244 245
	list_add_tail(&term->list, head);

	ALLOC_LIST(list);
246
	ABORT_ON(parse_events_add_pmu(data, list, "cpu", head));
247
	parse_events_terms__delete(head);
248 249 250 251 252 253 254 255 256 257 258 259 260 261
	$$ = list;
}
|
PE_PMU_EVENT_PRE '-' PE_PMU_EVENT_SUF sep_dc
{
	struct parse_events_evlist *data = _data;
	struct list_head *head;
	struct parse_events_term *term;
	struct list_head *list;
	char pmu_name[128];
	snprintf(&pmu_name, 128, "%s-%s", $1, $3);

	ALLOC_LIST(head);
	ABORT_ON(parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
262
					&pmu_name, 1, &@1, NULL));
263 264 265
	list_add_tail(&term->list, head);

	ALLOC_LIST(list);
266
	ABORT_ON(parse_events_add_pmu(data, list, "cpu", head));
267
	parse_events_terms__delete(head);
268 269
	$$ = list;
}
270

271 272 273 274 275
value_sym:
PE_VALUE_SYM_HW
|
PE_VALUE_SYM_SW

276
event_legacy_symbol:
277
value_sym '/' event_config '/'
278
{
279
	struct parse_events_evlist *data = _data;
280
	struct list_head *list;
281 282 283
	int type = $1 >> 16;
	int config = $1 & 255;

284
	ALLOC_LIST(list);
285
	ABORT_ON(parse_events_add_numeric(data, list, type, config, $3));
286
	parse_events_terms__delete($3);
287
	$$ = list;
288 289
}
|
290
value_sym sep_slash_dc
291
{
292
	struct parse_events_evlist *data = _data;
293
	struct list_head *list;
294 295 296
	int type = $1 >> 16;
	int config = $1 & 255;

297
	ALLOC_LIST(list);
298
	ABORT_ON(parse_events_add_numeric(data, list, type, config, NULL));
299
	$$ = list;
300 301 302
}

event_legacy_cache:
303
PE_NAME_CACHE_TYPE '-' PE_NAME_CACHE_OP_RESULT '-' PE_NAME_CACHE_OP_RESULT opt_event_config
304
{
305
	struct parse_events_evlist *data = _data;
306
	struct parse_events_error *error = data->error;
307
	struct list_head *list;
308

309
	ALLOC_LIST(list);
310 311
	ABORT_ON(parse_events_add_cache(list, &data->idx, $1, $3, $5, error, $6));
	parse_events_terms__delete($6);
312
	$$ = list;
313 314
}
|
315
PE_NAME_CACHE_TYPE '-' PE_NAME_CACHE_OP_RESULT opt_event_config
316
{
317
	struct parse_events_evlist *data = _data;
318
	struct parse_events_error *error = data->error;
319
	struct list_head *list;
320

321
	ALLOC_LIST(list);
322 323
	ABORT_ON(parse_events_add_cache(list, &data->idx, $1, $3, NULL, error, $4));
	parse_events_terms__delete($4);
324
	$$ = list;
325 326
}
|
327
PE_NAME_CACHE_TYPE opt_event_config
328
{
329
	struct parse_events_evlist *data = _data;
330
	struct parse_events_error *error = data->error;
331
	struct list_head *list;
332

333
	ALLOC_LIST(list);
334 335
	ABORT_ON(parse_events_add_cache(list, &data->idx, $1, NULL, NULL, error, $2));
	parse_events_terms__delete($2);
336
	$$ = list;
337 338 339
}

event_legacy_mem:
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
PE_PREFIX_MEM PE_VALUE '/' PE_VALUE ':' PE_MODIFIER_BP sep_dc
{
	struct parse_events_evlist *data = _data;
	struct list_head *list;

	ALLOC_LIST(list);
	ABORT_ON(parse_events_add_breakpoint(list, &data->idx,
					     (void *) $2, $6, $4));
	$$ = list;
}
|
PE_PREFIX_MEM PE_VALUE '/' PE_VALUE sep_dc
{
	struct parse_events_evlist *data = _data;
	struct list_head *list;

	ALLOC_LIST(list);
	ABORT_ON(parse_events_add_breakpoint(list, &data->idx,
					     (void *) $2, NULL, $4));
	$$ = list;
}
|
362 363
PE_PREFIX_MEM PE_VALUE ':' PE_MODIFIER_BP sep_dc
{
364
	struct parse_events_evlist *data = _data;
365
	struct list_head *list;
366

367 368
	ALLOC_LIST(list);
	ABORT_ON(parse_events_add_breakpoint(list, &data->idx,
369
					     (void *) $2, $4, 0));
370
	$$ = list;
371 372 373 374
}
|
PE_PREFIX_MEM PE_VALUE sep_dc
{
375
	struct parse_events_evlist *data = _data;
376
	struct list_head *list;
377

378 379
	ALLOC_LIST(list);
	ABORT_ON(parse_events_add_breakpoint(list, &data->idx,
380
					     (void *) $2, NULL, 0));
381
	$$ = list;
382 383 384
}

event_legacy_tracepoint:
385
tracepoint_name opt_event_config
386 387
{
	struct parse_events_evlist *data = _data;
388
	struct parse_events_error *error = data->error;
389 390 391
	struct list_head *list;

	ALLOC_LIST(list);
392 393 394
	if (error)
		error->idx = @1.first_column;

395
	if (parse_events_add_tracepoint(list, &data->idx, $1.sys, $1.event,
396
					error, $2))
397 398
		return -1;

399 400
	$$ = list;
}
401 402 403 404 405 406 407 408 409 410 411 412 413

tracepoint_name:
PE_NAME '-' PE_NAME ':' PE_NAME
{
	char sys_name[128];
	struct tracepoint_name tracepoint;

	snprintf(&sys_name, 128, "%s-%s", $1, $3);
	tracepoint.sys = &sys_name;
	tracepoint.event = $5;

	$$ = tracepoint;
}
414
|
415 416
PE_NAME ':' PE_NAME
{
417
	struct tracepoint_name tracepoint = {$1, $3};
418

419
	$$ = tracepoint;
420 421 422
}

event_legacy_numeric:
423
PE_VALUE ':' PE_VALUE opt_event_config
424
{
425
	struct parse_events_evlist *data = _data;
426
	struct list_head *list;
427

428
	ALLOC_LIST(list);
429 430
	ABORT_ON(parse_events_add_numeric(data, list, (u32)$1, $3, $4));
	parse_events_terms__delete($4);
431
	$$ = list;
432 433 434
}

event_legacy_raw:
435
PE_RAW opt_event_config
436
{
437
	struct parse_events_evlist *data = _data;
438
	struct list_head *list;
439

440
	ALLOC_LIST(list);
441 442
	ABORT_ON(parse_events_add_numeric(data, list, PERF_TYPE_RAW, $1, $2));
	parse_events_terms__delete($2);
443
	$$ = list;
444 445
}

446
event_bpf_file:
447
PE_BPF_OBJECT opt_event_config
448 449 450 451 452 453
{
	struct parse_events_evlist *data = _data;
	struct parse_events_error *error = data->error;
	struct list_head *list;

	ALLOC_LIST(list);
454 455
	ABORT_ON(parse_events_load_bpf(data, list, $1, false, $2));
	parse_events_terms__delete($2);
456 457 458
	$$ = list;
}
|
459
PE_BPF_SOURCE opt_event_config
460 461 462 463 464
{
	struct parse_events_evlist *data = _data;
	struct list_head *list;

	ALLOC_LIST(list);
465 466
	ABORT_ON(parse_events_load_bpf(data, list, $1, true, $2));
	parse_events_terms__delete($2);
467 468 469
	$$ = list;
}

470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
opt_event_config:
'/' event_config '/'
{
	$$ = $2;
}
|
'/' '/'
{
	$$ = NULL;
}
|
{
	$$ = NULL;
}

485
start_terms: event_config
486
{
487
	struct parse_events_terms *data = _data;
488 489 490
	data->terms = $1;
}

491 492 493 494
event_config:
event_config ',' event_term
{
	struct list_head *head = $1;
495
	struct parse_events_term *term = $3;
496 497 498 499 500 501 502 503 504

	ABORT_ON(!head);
	list_add_tail(&term->list, head);
	$$ = $1;
}
|
event_term
{
	struct list_head *head = malloc(sizeof(*head));
505
	struct parse_events_term *term = $1;
506 507 508 509 510 511 512 513 514 515

	ABORT_ON(!head);
	INIT_LIST_HEAD(head);
	list_add_tail(&term->list, head);
	$$ = head;
}

event_term:
PE_NAME '=' PE_NAME
{
516
	struct parse_events_term *term;
517

518
	ABORT_ON(parse_events_term__str(&term, PARSE_EVENTS__TERM_TYPE_USER,
519
					$1, $3, &@1, &@3));
520 521 522 523 524
	$$ = term;
}
|
PE_NAME '=' PE_VALUE
{
525
	struct parse_events_term *term;
526

527
	ABORT_ON(parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
528
					$1, $3, &@1, &@3));
529 530 531
	$$ = term;
}
|
532 533
PE_NAME '=' PE_VALUE_SYM_HW
{
534
	struct parse_events_term *term;
535 536
	int config = $3 & 255;

537
	ABORT_ON(parse_events_term__sym_hw(&term, $1, config));
538 539 540
	$$ = term;
}
|
541 542
PE_NAME
{
543
	struct parse_events_term *term;
544

545
	ABORT_ON(parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
546
					$1, 1, &@1, NULL));
547 548 549
	$$ = term;
}
|
550 551
PE_VALUE_SYM_HW
{
552
	struct parse_events_term *term;
553 554
	int config = $1 & 255;

555
	ABORT_ON(parse_events_term__sym_hw(&term, NULL, config));
556 557 558
	$$ = term;
}
|
559 560
PE_TERM '=' PE_NAME
{
561
	struct parse_events_term *term;
562

563
	ABORT_ON(parse_events_term__str(&term, (int)$1, NULL, $3, &@1, &@3));
564 565 566
	$$ = term;
}
|
567 568
PE_TERM '=' PE_VALUE
{
569
	struct parse_events_term *term;
570

571
	ABORT_ON(parse_events_term__num(&term, (int)$1, NULL, $3, &@1, &@3));
572 573 574 575 576
	$$ = term;
}
|
PE_TERM
{
577
	struct parse_events_term *term;
578

579
	ABORT_ON(parse_events_term__num(&term, (int)$1, NULL, 1, &@1, NULL));
580
	$$ = term;
581
}
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
|
PE_NAME array '=' PE_NAME
{
	struct parse_events_term *term;
	int i;

	ABORT_ON(parse_events_term__str(&term, PARSE_EVENTS__TERM_TYPE_USER,
					$1, $4, &@1, &@4));

	term->array = $2;
	$$ = term;
}
|
PE_NAME array '=' PE_VALUE
{
	struct parse_events_term *term;

	ABORT_ON(parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
					$1, $4, &@1, &@4));
	term->array = $2;
	$$ = term;
}
604 605 606 607 608 609 610 611 612
|
PE_DRV_CFG_TERM
{
	struct parse_events_term *term;

	ABORT_ON(parse_events_term__str(&term, PARSE_EVENTS__TERM_TYPE_DRV_CFG,
					$1, $1, &@1, NULL));
	$$ = term;
}
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670

array:
'[' array_terms ']'
{
	$$ = $2;
}
|
PE_ARRAY_ALL
{
	$$.nr_ranges = 0;
	$$.ranges = NULL;
}

array_terms:
array_terms ',' array_term
{
	struct parse_events_array new_array;

	new_array.nr_ranges = $1.nr_ranges + $3.nr_ranges;
	new_array.ranges = malloc(sizeof(new_array.ranges[0]) *
				  new_array.nr_ranges);
	ABORT_ON(!new_array.ranges);
	memcpy(&new_array.ranges[0], $1.ranges,
	       $1.nr_ranges * sizeof(new_array.ranges[0]));
	memcpy(&new_array.ranges[$1.nr_ranges], $3.ranges,
	       $3.nr_ranges * sizeof(new_array.ranges[0]));
	free($1.ranges);
	free($3.ranges);
	$$ = new_array;
}
|
array_term

array_term:
PE_VALUE
{
	struct parse_events_array array;

	array.nr_ranges = 1;
	array.ranges = malloc(sizeof(array.ranges[0]));
	ABORT_ON(!array.ranges);
	array.ranges[0].start = $1;
	array.ranges[0].length = 1;
	$$ = array;
}
|
PE_VALUE PE_ARRAY_RANGE PE_VALUE
{
	struct parse_events_array array;

	ABORT_ON($3 < $1);
	array.nr_ranges = 1;
	array.ranges = malloc(sizeof(array.ranges[0]));
	ABORT_ON(!array.ranges);
	array.ranges[0].start = $1;
	array.ranges[0].length = $3 - $1 + 1;
	$$ = array;
}
671 672 673

sep_dc: ':' |

674 675
sep_slash_dc: '/' | ':' |

676 677
%%

678 679
void parse_events_error(YYLTYPE *loc, void *data,
			void *scanner __maybe_unused,
680
			char const *msg __maybe_unused)
681
{
682
	parse_events_evlist_error(data, loc->last_column, "parser error");
683
}