parse-events.y 10.7 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 32 33 34 35 36 37 38
static inc_group_count(struct list_head *list,
		       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
%type <num> PE_VALUE
52 53
%type <num> PE_VALUE_SYM_HW
%type <num> PE_VALUE_SYM_SW
54
%type <num> PE_RAW
55
%type <num> PE_TERM
56
%type <str> PE_NAME
57
%type <str> PE_BPF_OBJECT
58
%type <str> PE_BPF_SOURCE
59 60 61 62
%type <str> PE_NAME_CACHE_TYPE
%type <str> PE_NAME_CACHE_OP_RESULT
%type <str> PE_MODIFIER_EVENT
%type <str> PE_MODIFIER_BP
63
%type <str> PE_EVENT_NAME
64
%type <str> PE_PMU_EVENT_PRE PE_PMU_EVENT_SUF PE_KERNEL_PMU_EVENT
65
%type <num> value_sym
66
%type <head> event_config
67
%type <head> opt_event_config
68
%type <term> event_term
69 70 71 72 73
%type <head> event_pmu
%type <head> event_legacy_symbol
%type <head> event_legacy_cache
%type <head> event_legacy_mem
%type <head> event_legacy_tracepoint
74
%type <tracepoint_name> tracepoint_name
75 76
%type <head> event_legacy_numeric
%type <head> event_legacy_raw
77
%type <head> event_bpf_file
78
%type <head> event_def
79 80
%type <head> event_mod
%type <head> event_name
81 82 83 84 85
%type <head> event
%type <head> events
%type <head> group_def
%type <head> group
%type <head> groups
86 87 88 89

%union
{
	char *str;
90
	u64 num;
91
	struct list_head *head;
92
	struct parse_events_term *term;
93 94 95 96
	struct tracepoint_name {
		char *sys;
		char *event;
	} tracepoint_name;
97 98 99
}
%%

100
start:
101
PE_START_EVENTS start_events
102
|
103 104 105 106
PE_START_TERMS  start_terms

start_events: groups
{
107
	struct parse_events_evlist *data = _data;
108 109 110 111 112 113 114 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

	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;

151
	inc_group_count(list, _data);
152
	parse_events__set_leader($1, list);
153 154 155 156 157 158 159
	$$ = list;
}
|
'{' events '}'
{
	struct list_head *list = $2;

160
	inc_group_count(list, _data);
161
	parse_events__set_leader(NULL, list);
162 163
	$$ = list;
}
164

165
events:
166 167 168 169 170 171 172 173 174 175
events ',' event
{
	struct list_head *event = $3;
	struct list_head *list  = $1;

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

177 178 179 180
event: event_mod

event_mod:
event_name PE_MODIFIER_EVENT
181
{
182
	struct list_head *list = $1;
183

184 185 186 187 188
	/*
	 * Apply modifier on all events added by single event definition
	 * (there could be more events added for multiple tracepoint
	 * definitions via '*?'.
	 */
189
	ABORT_ON(parse_events__modifier_event(list, $2, false));
190
	$$ = list;
191 192
}
|
193 194 195 196 197 198 199 200 201 202
event_name

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

205 206
event_def: event_pmu |
	   event_legacy_symbol |
207 208 209 210
	   event_legacy_cache sep_dc |
	   event_legacy_mem |
	   event_legacy_tracepoint sep_dc |
	   event_legacy_numeric sep_dc |
211 212
	   event_legacy_raw sep_dc |
	   event_bpf_file
213

214 215 216
event_pmu:
PE_NAME '/' event_config '/'
{
217
	struct parse_events_evlist *data = _data;
218
	struct list_head *list;
219

220
	ALLOC_LIST(list);
221
	ABORT_ON(parse_events_add_pmu(data, list, $1, $3));
222
	parse_events_terms__delete($3);
223
	$$ = list;
224
}
225
|
226 227 228 229 230 231 232 233 234
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,
235
					$1, 1, &@1, NULL));
236 237 238
	list_add_tail(&term->list, head);

	ALLOC_LIST(list);
239
	ABORT_ON(parse_events_add_pmu(data, list, "cpu", head));
240
	parse_events_terms__delete(head);
241 242 243 244 245 246 247 248 249 250 251 252 253 254
	$$ = 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,
255
					&pmu_name, 1, &@1, NULL));
256 257 258
	list_add_tail(&term->list, head);

	ALLOC_LIST(list);
259
	ABORT_ON(parse_events_add_pmu(data, list, "cpu", head));
260
	parse_events_terms__delete(head);
261 262
	$$ = list;
}
263

264 265 266 267 268
value_sym:
PE_VALUE_SYM_HW
|
PE_VALUE_SYM_SW

269
event_legacy_symbol:
270
value_sym '/' event_config '/'
271
{
272
	struct parse_events_evlist *data = _data;
273
	struct list_head *list;
274 275 276
	int type = $1 >> 16;
	int config = $1 & 255;

277
	ALLOC_LIST(list);
278
	ABORT_ON(parse_events_add_numeric(data, list, type, config, $3));
279
	parse_events_terms__delete($3);
280
	$$ = list;
281 282
}
|
283
value_sym sep_slash_dc
284
{
285
	struct parse_events_evlist *data = _data;
286
	struct list_head *list;
287 288 289
	int type = $1 >> 16;
	int config = $1 & 255;

290
	ALLOC_LIST(list);
291
	ABORT_ON(parse_events_add_numeric(data, list, type, config, NULL));
292
	$$ = list;
293 294 295 296 297
}

event_legacy_cache:
PE_NAME_CACHE_TYPE '-' PE_NAME_CACHE_OP_RESULT '-' PE_NAME_CACHE_OP_RESULT
{
298
	struct parse_events_evlist *data = _data;
299
	struct list_head *list;
300

301 302
	ALLOC_LIST(list);
	ABORT_ON(parse_events_add_cache(list, &data->idx, $1, $3, $5));
303
	$$ = list;
304 305 306 307
}
|
PE_NAME_CACHE_TYPE '-' PE_NAME_CACHE_OP_RESULT
{
308
	struct parse_events_evlist *data = _data;
309
	struct list_head *list;
310

311 312
	ALLOC_LIST(list);
	ABORT_ON(parse_events_add_cache(list, &data->idx, $1, $3, NULL));
313
	$$ = list;
314 315 316 317
}
|
PE_NAME_CACHE_TYPE
{
318
	struct parse_events_evlist *data = _data;
319
	struct list_head *list;
320

321 322
	ALLOC_LIST(list);
	ABORT_ON(parse_events_add_cache(list, &data->idx, $1, NULL, NULL));
323
	$$ = list;
324 325 326
}

event_legacy_mem:
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
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;
}
|
349 350
PE_PREFIX_MEM PE_VALUE ':' PE_MODIFIER_BP sep_dc
{
351
	struct parse_events_evlist *data = _data;
352
	struct list_head *list;
353

354 355
	ALLOC_LIST(list);
	ABORT_ON(parse_events_add_breakpoint(list, &data->idx,
356
					     (void *) $2, $4, 0));
357
	$$ = list;
358 359 360 361
}
|
PE_PREFIX_MEM PE_VALUE sep_dc
{
362
	struct parse_events_evlist *data = _data;
363
	struct list_head *list;
364

365 366
	ALLOC_LIST(list);
	ABORT_ON(parse_events_add_breakpoint(list, &data->idx,
367
					     (void *) $2, NULL, 0));
368
	$$ = list;
369 370 371
}

event_legacy_tracepoint:
372
tracepoint_name opt_event_config
373 374
{
	struct parse_events_evlist *data = _data;
375
	struct parse_events_error *error = data->error;
376 377 378
	struct list_head *list;

	ALLOC_LIST(list);
379 380 381
	if (error)
		error->idx = @1.first_column;

382
	if (parse_events_add_tracepoint(list, &data->idx, $1.sys, $1.event,
383
					error, $2))
384 385
		return -1;

386 387
	$$ = list;
}
388 389 390 391 392 393 394 395 396 397 398 399 400

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;
}
401
|
402 403
PE_NAME ':' PE_NAME
{
404
	struct tracepoint_name tracepoint = {$1, $3};
405

406
	$$ = tracepoint;
407 408 409 410 411
}

event_legacy_numeric:
PE_VALUE ':' PE_VALUE
{
412
	struct parse_events_evlist *data = _data;
413
	struct list_head *list;
414

415
	ALLOC_LIST(list);
416
	ABORT_ON(parse_events_add_numeric(data, list, (u32)$1, $3, NULL));
417
	$$ = list;
418 419 420 421 422
}

event_legacy_raw:
PE_RAW
{
423
	struct parse_events_evlist *data = _data;
424
	struct list_head *list;
425

426
	ALLOC_LIST(list);
427
	ABORT_ON(parse_events_add_numeric(data, list, PERF_TYPE_RAW, $1, NULL));
428
	$$ = list;
429 430
}

431 432 433 434 435 436 437 438
event_bpf_file:
PE_BPF_OBJECT
{
	struct parse_events_evlist *data = _data;
	struct parse_events_error *error = data->error;
	struct list_head *list;

	ALLOC_LIST(list);
439 440 441 442 443 444 445 446 447 448 449
	ABORT_ON(parse_events_load_bpf(data, list, $1, false));
	$$ = list;
}
|
PE_BPF_SOURCE
{
	struct parse_events_evlist *data = _data;
	struct list_head *list;

	ALLOC_LIST(list);
	ABORT_ON(parse_events_load_bpf(data, list, $1, true));
450 451 452
	$$ = list;
}

453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
opt_event_config:
'/' event_config '/'
{
	$$ = $2;
}
|
'/' '/'
{
	$$ = NULL;
}
|
{
	$$ = NULL;
}

468
start_terms: event_config
469
{
470
	struct parse_events_terms *data = _data;
471 472 473
	data->terms = $1;
}

474 475 476 477
event_config:
event_config ',' event_term
{
	struct list_head *head = $1;
478
	struct parse_events_term *term = $3;
479 480 481 482 483 484 485 486 487

	ABORT_ON(!head);
	list_add_tail(&term->list, head);
	$$ = $1;
}
|
event_term
{
	struct list_head *head = malloc(sizeof(*head));
488
	struct parse_events_term *term = $1;
489 490 491 492 493 494 495 496 497 498

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

event_term:
PE_NAME '=' PE_NAME
{
499
	struct parse_events_term *term;
500

501
	ABORT_ON(parse_events_term__str(&term, PARSE_EVENTS__TERM_TYPE_USER,
502
					$1, $3, &@1, &@3));
503 504 505 506 507
	$$ = term;
}
|
PE_NAME '=' PE_VALUE
{
508
	struct parse_events_term *term;
509

510
	ABORT_ON(parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
511
					$1, $3, &@1, &@3));
512 513 514
	$$ = term;
}
|
515 516
PE_NAME '=' PE_VALUE_SYM_HW
{
517
	struct parse_events_term *term;
518 519
	int config = $3 & 255;

520
	ABORT_ON(parse_events_term__sym_hw(&term, $1, config));
521 522 523
	$$ = term;
}
|
524 525
PE_NAME
{
526
	struct parse_events_term *term;
527

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

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

546
	ABORT_ON(parse_events_term__str(&term, (int)$1, NULL, $3, &@1, &@3));
547 548 549
	$$ = term;
}
|
550 551
PE_TERM '=' PE_VALUE
{
552
	struct parse_events_term *term;
553

554
	ABORT_ON(parse_events_term__num(&term, (int)$1, NULL, $3, &@1, &@3));
555 556 557 558 559
	$$ = term;
}
|
PE_TERM
{
560
	struct parse_events_term *term;
561

562
	ABORT_ON(parse_events_term__num(&term, (int)$1, NULL, 1, &@1, NULL));
563
	$$ = term;
564 565 566 567
}

sep_dc: ':' |

568 569
sep_slash_dc: '/' | ':' |

570 571
%%

572 573
void parse_events_error(YYLTYPE *loc, void *data,
			void *scanner __maybe_unused,
574
			char const *msg __maybe_unused)
575
{
576
	parse_events_evlist_error(data, loc->last_column, "parser error");
577
}