parse-events.l (12758B)
1 2%option reentrant 3%option bison-bridge 4%option prefix="parse_events_" 5%option stack 6%option bison-locations 7%option yylineno 8%option reject 9 10%{ 11#include <errno.h> 12#include <sys/types.h> 13#include <sys/stat.h> 14#include <unistd.h> 15#include "parse-events.h" 16#include "parse-events-bison.h" 17#include "evsel.h" 18 19char *parse_events_get_text(yyscan_t yyscanner); 20YYSTYPE *parse_events_get_lval(yyscan_t yyscanner); 21 22static int __value(YYSTYPE *yylval, char *str, int base, int token) 23{ 24 u64 num; 25 26 errno = 0; 27 num = strtoull(str, NULL, base); 28 if (errno) 29 return PE_ERROR; 30 31 yylval->num = num; 32 return token; 33} 34 35static int value(yyscan_t scanner, int base) 36{ 37 YYSTYPE *yylval = parse_events_get_lval(scanner); 38 char *text = parse_events_get_text(scanner); 39 40 return __value(yylval, text, base, PE_VALUE); 41} 42 43static int str(yyscan_t scanner, int token) 44{ 45 YYSTYPE *yylval = parse_events_get_lval(scanner); 46 char *text = parse_events_get_text(scanner); 47 48 if (text[0] != '\'') { 49 yylval->str = strdup(text); 50 } else { 51 /* 52 * If a text tag specified on the command line 53 * contains opening single quite ' then it is 54 * expected that the tag ends with single quote 55 * as well, like this: 56 * name=\'CPU_CLK_UNHALTED.THREAD:cmask=1\' 57 * quotes need to be escaped to bypass shell 58 * processing. 59 */ 60 yylval->str = strndup(&text[1], strlen(text) - 2); 61 } 62 63 return token; 64} 65 66static int raw(yyscan_t scanner) 67{ 68 YYSTYPE *yylval = parse_events_get_lval(scanner); 69 char *text = parse_events_get_text(scanner); 70 71 if (perf_pmu__parse_check(text) == PMU_EVENT_SYMBOL) 72 return str(scanner, PE_NAME); 73 74 return __value(yylval, text + 1, 16, PE_RAW); 75} 76 77static bool isbpf_suffix(char *text) 78{ 79 int len = strlen(text); 80 81 if (len < 2) 82 return false; 83 if ((text[len - 1] == 'c' || text[len - 1] == 'o') && 84 text[len - 2] == '.') 85 return true; 86 if (len > 4 && !strcmp(text + len - 4, ".obj")) 87 return true; 88 return false; 89} 90 91static bool isbpf(yyscan_t scanner) 92{ 93 char *text = parse_events_get_text(scanner); 94 struct stat st; 95 96 if (!isbpf_suffix(text)) 97 return false; 98 99 return stat(text, &st) == 0; 100} 101 102/* 103 * This function is called when the parser gets two kind of input: 104 * 105 * @cfg1 or @cfg2=config 106 * 107 * The leading '@' is stripped off before 'cfg1' and 'cfg2=config' are given to 108 * bison. In the latter case it is necessary to keep the string intact so that 109 * the PMU kernel driver can determine what configurable is associated to 110 * 'config'. 111 */ 112static int drv_str(yyscan_t scanner, int token) 113{ 114 YYSTYPE *yylval = parse_events_get_lval(scanner); 115 char *text = parse_events_get_text(scanner); 116 117 /* Strip off the '@' */ 118 yylval->str = strdup(text + 1); 119 return token; 120} 121 122#define REWIND(__alloc) \ 123do { \ 124 YYSTYPE *__yylval = parse_events_get_lval(yyscanner); \ 125 char *text = parse_events_get_text(yyscanner); \ 126 \ 127 if (__alloc) \ 128 __yylval->str = strdup(text); \ 129 \ 130 yycolumn -= strlen(text); \ 131 yyless(0); \ 132} while (0) 133 134static int pmu_str_check(yyscan_t scanner, struct parse_events_state *parse_state) 135{ 136 YYSTYPE *yylval = parse_events_get_lval(scanner); 137 char *text = parse_events_get_text(scanner); 138 139 yylval->str = strdup(text); 140 141 /* 142 * If we're not testing then parse check determines the PMU event type 143 * which if it isn't a PMU returns PE_NAME. When testing the result of 144 * parse check can't be trusted so we return PE_PMU_EVENT_FAKE unless 145 * an '!' is present in which case the text can't be a PMU name. 146 */ 147 switch (perf_pmu__parse_check(text)) { 148 case PMU_EVENT_SYMBOL_PREFIX: 149 return PE_PMU_EVENT_PRE; 150 case PMU_EVENT_SYMBOL_SUFFIX: 151 return PE_PMU_EVENT_SUF; 152 case PMU_EVENT_SYMBOL_SUFFIX2: 153 return PE_PMU_EVENT_SUF2; 154 case PMU_EVENT_SYMBOL: 155 return parse_state->fake_pmu 156 ? PE_PMU_EVENT_FAKE : PE_KERNEL_PMU_EVENT; 157 default: 158 return parse_state->fake_pmu && !strchr(text,'!') 159 ? PE_PMU_EVENT_FAKE : PE_NAME; 160 } 161} 162 163static int sym(yyscan_t scanner, int type, int config) 164{ 165 YYSTYPE *yylval = parse_events_get_lval(scanner); 166 167 yylval->num = (type << 16) + config; 168 return type == PERF_TYPE_HARDWARE ? PE_VALUE_SYM_HW : PE_VALUE_SYM_SW; 169} 170 171static int tool(yyscan_t scanner, enum perf_tool_event event) 172{ 173 YYSTYPE *yylval = parse_events_get_lval(scanner); 174 175 yylval->num = event; 176 return PE_VALUE_SYM_TOOL; 177} 178 179static int term(yyscan_t scanner, int type) 180{ 181 YYSTYPE *yylval = parse_events_get_lval(scanner); 182 183 yylval->num = type; 184 return PE_TERM; 185} 186 187#define YY_USER_ACTION \ 188do { \ 189 yylloc->last_column = yylloc->first_column; \ 190 yylloc->first_column = yycolumn; \ 191 yycolumn += yyleng; \ 192} while (0); 193 194#define USER_REJECT \ 195 yycolumn -= yyleng; \ 196 REJECT 197 198%} 199 200%x mem 201%s config 202%x event 203%x array 204 205group [^,{}/]*[{][^}]*[}][^,{}/]* 206event_pmu [^,{}/]+[/][^/]*[/][^,{}/]* 207event [^,{}/]+ 208bpf_object [^,{}]+\.(o|bpf)[a-zA-Z0-9._]* 209bpf_source [^,{}]+\.c[a-zA-Z0-9._]* 210 211num_dec [0-9]+ 212num_hex 0x[a-fA-F0-9]+ 213num_raw_hex [a-fA-F0-9]+ 214name [a-zA-Z_*?\[\]][a-zA-Z0-9_*?.\[\]!]* 215name_tag [\'][a-zA-Z_*?\[\]][a-zA-Z0-9_*?\-,\.\[\]:=]*[\'] 216name_minus [a-zA-Z_*?][a-zA-Z0-9\-_*?.:]* 217drv_cfg_term [a-zA-Z0-9_\.]+(=[a-zA-Z0-9_*?\.:]+)? 218/* If you add a modifier you need to update check_modifier() */ 219modifier_event [ukhpPGHSDIWeb]+ 220modifier_bp [rwx]{1,3} 221 222%% 223 224%{ 225 struct parse_events_state *_parse_state = parse_events_get_extra(yyscanner); 226 227 { 228 int start_token = _parse_state->stoken; 229 230 if (start_token == PE_START_TERMS) 231 BEGIN(config); 232 else if (start_token == PE_START_EVENTS) 233 BEGIN(event); 234 235 if (start_token) { 236 _parse_state->stoken = 0; 237 /* 238 * The flex parser does not init locations variable 239 * via the scan_string interface, so we need do the 240 * init in here. 241 */ 242 yycolumn = 0; 243 return start_token; 244 } 245 } 246%} 247 248<event>{ 249 250{group} { 251 BEGIN(INITIAL); 252 REWIND(0); 253 } 254 255{event_pmu} | 256{bpf_object} | 257{bpf_source} | 258{event} { 259 BEGIN(INITIAL); 260 REWIND(1); 261 return PE_EVENT_NAME; 262 } 263 264<<EOF>> { 265 BEGIN(INITIAL); 266 REWIND(0); 267 } 268, { 269 return ','; 270 } 271} 272 273<array>{ 274"]" { BEGIN(config); return ']'; } 275{num_dec} { return value(yyscanner, 10); } 276{num_hex} { return value(yyscanner, 16); } 277, { return ','; } 278"\.\.\." { return PE_ARRAY_RANGE; } 279} 280 281<config>{ 282 /* 283 * Please update config_term_names when new static term is added. 284 */ 285config { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG); } 286config1 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG1); } 287config2 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG2); } 288name { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NAME); } 289period { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD); } 290freq { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ); } 291branch_type { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE); } 292time { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_TIME); } 293call-graph { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CALLGRAPH); } 294stack-size { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_STACKSIZE); } 295max-stack { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_MAX_STACK); } 296nr { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_MAX_EVENTS); } 297inherit { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_INHERIT); } 298no-inherit { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOINHERIT); } 299overwrite { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_OVERWRITE); } 300no-overwrite { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOOVERWRITE); } 301percore { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_PERCORE); } 302aux-output { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT); } 303aux-sample-size { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE); } 304metric-id { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_METRIC_ID); } 305r{num_raw_hex} { return raw(yyscanner); } 306r0x{num_raw_hex} { return raw(yyscanner); } 307, { return ','; } 308"/" { BEGIN(INITIAL); return '/'; } 309{name_minus} { return str(yyscanner, PE_NAME); } 310\[all\] { return PE_ARRAY_ALL; } 311"[" { BEGIN(array); return '['; } 312@{drv_cfg_term} { return drv_str(yyscanner, PE_DRV_CFG_TERM); } 313} 314 315<mem>{ 316{modifier_bp} { return str(yyscanner, PE_MODIFIER_BP); } 317: { return ':'; } 318"/" { return '/'; } 319{num_dec} { return value(yyscanner, 10); } 320{num_hex} { return value(yyscanner, 16); } 321 /* 322 * We need to separate 'mem:' scanner part, in order to get specific 323 * modifier bits parsed out. Otherwise we would need to handle PE_NAME 324 * and we'd need to parse it manually. During the escape from <mem> 325 * state we need to put the escaping char back, so we dont miss it. 326 */ 327. { unput(*yytext); BEGIN(INITIAL); } 328 /* 329 * We destroy the scanner after reaching EOF, 330 * but anyway just to be sure get back to INIT state. 331 */ 332<<EOF>> { BEGIN(INITIAL); } 333} 334 335cpu-cycles|cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES); } 336stalled-cycles-frontend|idle-cycles-frontend { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); } 337stalled-cycles-backend|idle-cycles-backend { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); } 338instructions { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS); } 339cache-references { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_REFERENCES); } 340cache-misses { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_MISSES); } 341branch-instructions|branches { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_INSTRUCTIONS); } 342branch-misses { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_MISSES); } 343bus-cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BUS_CYCLES); } 344ref-cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_REF_CPU_CYCLES); } 345cpu-clock { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_CLOCK); } 346task-clock { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_TASK_CLOCK); } 347page-faults|faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS); } 348minor-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MIN); } 349major-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MAJ); } 350context-switches|cs { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CONTEXT_SWITCHES); } 351cpu-migrations|migrations { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_MIGRATIONS); } 352alignment-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_ALIGNMENT_FAULTS); } 353emulation-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_EMULATION_FAULTS); } 354dummy { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); } 355duration_time { return tool(yyscanner, PERF_TOOL_DURATION_TIME); } 356user_time { return tool(yyscanner, PERF_TOOL_USER_TIME); } 357system_time { return tool(yyscanner, PERF_TOOL_SYSTEM_TIME); } 358bpf-output { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_BPF_OUTPUT); } 359cgroup-switches { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CGROUP_SWITCHES); } 360 361 /* 362 * We have to handle the kernel PMU event cycles-ct/cycles-t/mem-loads/mem-stores separately. 363 * Because the prefix cycles is mixed up with cpu-cycles. 364 * loads and stores are mixed up with cache event 365 */ 366cycles-ct | 367cycles-t | 368mem-loads | 369mem-loads-aux | 370mem-stores | 371topdown-[a-z-]+ | 372tx-capacity-[a-z-]+ | 373el-capacity-[a-z-]+ { return str(yyscanner, PE_KERNEL_PMU_EVENT); } 374 375L1-dcache|l1-d|l1d|L1-data | 376L1-icache|l1-i|l1i|L1-instruction | 377LLC|L2 | 378dTLB|d-tlb|Data-TLB | 379iTLB|i-tlb|Instruction-TLB | 380branch|branches|bpu|btb|bpc | 381node { return str(yyscanner, PE_NAME_CACHE_TYPE); } 382 383load|loads|read | 384store|stores|write | 385prefetch|prefetches | 386speculative-read|speculative-load | 387refs|Reference|ops|access | 388misses|miss { return str(yyscanner, PE_NAME_CACHE_OP_RESULT); } 389 390mem: { BEGIN(mem); return PE_PREFIX_MEM; } 391r{num_raw_hex} { return raw(yyscanner); } 392{num_dec} { return value(yyscanner, 10); } 393{num_hex} { return value(yyscanner, 16); } 394 395{modifier_event} { return str(yyscanner, PE_MODIFIER_EVENT); } 396{bpf_object} { if (!isbpf(yyscanner)) { USER_REJECT }; return str(yyscanner, PE_BPF_OBJECT); } 397{bpf_source} { if (!isbpf(yyscanner)) { USER_REJECT }; return str(yyscanner, PE_BPF_SOURCE); } 398{name} { return pmu_str_check(yyscanner, _parse_state); } 399{name_tag} { return str(yyscanner, PE_NAME); } 400"/" { BEGIN(config); return '/'; } 401- { return '-'; } 402, { BEGIN(event); return ','; } 403: { return ':'; } 404"{" { BEGIN(event); return '{'; } 405"}" { return '}'; } 406= { return '='; } 407\n { } 408. { } 409 410%% 411 412int parse_events_wrap(void *scanner __maybe_unused) 413{ 414 return 1; 415}