diff mbox series

[v2,11/16] perf parse-events: Improve error message for bad numbers

Message ID 20240416061533.921723-12-irogers@google.com (mailing list archive)
State Not Applicable
Headers show
Series Consistently prefer sysfs/json events | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Ian Rogers April 16, 2024, 6:15 a.m. UTC
Use the error handler from the parse_state to give a more informative
error message.

Before:
```
$ perf stat -e 'cycles/period=99999999999999999999/' true
event syntax error: 'cycles/period=99999999999999999999/'
                                  \___ parser error
Run 'perf list' for a list of valid events

 Usage: perf stat [<options>] [<command>]

    -e, --event <event>   event selector. use 'perf list' to list available events
```

After:
```
$ perf stat -e 'cycles/period=99999999999999999999/' true
event syntax error: 'cycles/period=99999999999999999999/'
                                  \___ parser error

event syntax error: '..les/period=99999999999999999999/'
                                  \___ Bad base 10 number "99999999999999999999"
Run 'perf list' for a list of valid events

 Usage: perf stat [<options>] [<command>]

    -e, --event <event>   event selector. use 'perf list' to list available events
```

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/parse-events.l | 40 ++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 16 deletions(-)

Comments

Liang, Kan April 18, 2024, 8:27 p.m. UTC | #1
On 2024-04-16 2:15 a.m., Ian Rogers wrote:
> Use the error handler from the parse_state to give a more informative
> error message.
> 
> Before:
> ```
> $ perf stat -e 'cycles/period=99999999999999999999/' true
> event syntax error: 'cycles/period=99999999999999999999/'
>                                   \___ parser error
> Run 'perf list' for a list of valid events
> 
>  Usage: perf stat [<options>] [<command>]
> 
>     -e, --event <event>   event selector. use 'perf list' to list available events
> ```
> 
> After:
> ```
> $ perf stat -e 'cycles/period=99999999999999999999/' true
> event syntax error: 'cycles/period=99999999999999999999/'
>                                   \___ parser error
> 
> event syntax error: '..les/period=99999999999999999999/'
>                                   \___ Bad base 10 number "99999999999999999999"


It seems the patch only works for decimal?

./perf stat -e 'cycles/period=0xaaaaaaaaaaaaaaaaaaaaaa/' true
event syntax error: '..les/period=0xaaaaaaaaaaaaaaaaaaaaaa/'
                                   \___ parser error
 Run 'perf list' for a list of valid events

  Usage: perf stat [<options>] [<command>]

     -e, --event <event>   event selector. use 'perf list' to list
available events

Thanks,
Kan

> Run 'perf list' for a list of valid events
> 
>  Usage: perf stat [<options>] [<command>]
> 
>     -e, --event <event>   event selector. use 'perf list' to list available events
> ```
> 
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
>  tools/perf/util/parse-events.l | 40 ++++++++++++++++++++--------------
>  1 file changed, 24 insertions(+), 16 deletions(-)
> 
> diff --git a/tools/perf/util/parse-events.l b/tools/perf/util/parse-events.l
> index 6fe37003ab7b..0cd68c9f0d4f 100644
> --- a/tools/perf/util/parse-events.l
> +++ b/tools/perf/util/parse-events.l
> @@ -18,26 +18,34 @@
>  
>  char *parse_events_get_text(yyscan_t yyscanner);
>  YYSTYPE *parse_events_get_lval(yyscan_t yyscanner);
> +int parse_events_get_column(yyscan_t yyscanner);
> +int parse_events_get_leng(yyscan_t yyscanner);
>  
> -static int __value(YYSTYPE *yylval, char *str, int base, int token)
> +static int get_column(yyscan_t scanner)
>  {
> -	u64 num;
> -
> -	errno = 0;
> -	num = strtoull(str, NULL, base);
> -	if (errno)
> -		return PE_ERROR;
> -
> -	yylval->num = num;
> -	return token;
> +	return parse_events_get_column(scanner) - parse_events_get_leng(scanner);
>  }
>  
> -static int value(yyscan_t scanner, int base)
> +static int value(struct parse_events_state *parse_state, yyscan_t scanner, int base)
>  {
>  	YYSTYPE *yylval = parse_events_get_lval(scanner);
>  	char *text = parse_events_get_text(scanner);
> +	u64 num;
>  
> -	return __value(yylval, text, base, PE_VALUE);
> +	errno = 0;
> +	num = strtoull(text, NULL, base);
> +	if (errno) {
> +		struct parse_events_error *error = parse_state->error;
> +		char *help = NULL;
> +
> +		if (asprintf(&help, "Bad base %d number \"%s\"", base, text) > 0)
> +			parse_events_error__handle(error, get_column(scanner), help , NULL);
> +
> +		return PE_ERROR;
> +	}
> +
> +	yylval->num = num;
> +	return PE_VALUE;
>  }
>  
>  static int str(yyscan_t scanner, int token)
> @@ -283,8 +291,8 @@ r0x{num_raw_hex}	{ return str(yyscanner, PE_RAW); }
>  	 */
>  "/"/{digit}		{ return PE_BP_SLASH; }
>  "/"/{non_digit}		{ BEGIN(config); return '/'; }
> -{num_dec}		{ return value(yyscanner, 10); }
> -{num_hex}		{ return value(yyscanner, 16); }
> +{num_dec}		{ return value(_parse_state, yyscanner, 10); }
> +{num_hex}		{ return value(_parse_state, yyscanner, 16); }
>  	/*
>  	 * We need to separate 'mem:' scanner part, in order to get specific
>  	 * modifier bits parsed out. Otherwise we would need to handle PE_NAME
> @@ -330,8 +338,8 @@ cgroup-switches					{ return sym(yyscanner, PERF_COUNT_SW_CGROUP_SWITCHES); }
>  {lc_type}-{lc_op_result}-{lc_op_result}	{ return str(yyscanner, PE_LEGACY_CACHE); }
>  mem:			{ BEGIN(mem); return PE_PREFIX_MEM; }
>  r{num_raw_hex}		{ return str(yyscanner, PE_RAW); }
> -{num_dec}		{ return value(yyscanner, 10); }
> -{num_hex}		{ return value(yyscanner, 16); }
> +{num_dec}		{ return value(_parse_state, yyscanner, 10); }
> +{num_hex}		{ return value(_parse_state, yyscanner, 16); }
>  
>  {modifier_event}	{ return str(yyscanner, PE_MODIFIER_EVENT); }
>  {name}			{ return str(yyscanner, PE_NAME); }
Ian Rogers April 18, 2024, 9:07 p.m. UTC | #2
On Thu, Apr 18, 2024 at 1:27 PM Liang, Kan <kan.liang@linux.intel.com> wrote:
>
>
>
> On 2024-04-16 2:15 a.m., Ian Rogers wrote:
> > Use the error handler from the parse_state to give a more informative
> > error message.
> >
> > Before:
> > ```
> > $ perf stat -e 'cycles/period=99999999999999999999/' true
> > event syntax error: 'cycles/period=99999999999999999999/'
> >                                   \___ parser error
> > Run 'perf list' for a list of valid events
> >
> >  Usage: perf stat [<options>] [<command>]
> >
> >     -e, --event <event>   event selector. use 'perf list' to list available events
> > ```
> >
> > After:
> > ```
> > $ perf stat -e 'cycles/period=99999999999999999999/' true
> > event syntax error: 'cycles/period=99999999999999999999/'
> >                                   \___ parser error
> >
> > event syntax error: '..les/period=99999999999999999999/'
> >                                   \___ Bad base 10 number "99999999999999999999"
>
>
> It seems the patch only works for decimal?
>
> ./perf stat -e 'cycles/period=0xaaaaaaaaaaaaaaaaaaaaaa/' true
> event syntax error: '..les/period=0xaaaaaaaaaaaaaaaaaaaaaa/'
>                                    \___ parser error
>  Run 'perf list' for a list of valid events
>
>   Usage: perf stat [<options>] [<command>]
>
>      -e, --event <event>   event selector. use 'perf list' to list
> available events
>
> Thanks,
> Kan

Right, for hexadecimal we say the number of digits is at most 16, so
when you exceed this the token is no longer recognized. It just
becomes input that can't be parsed, hence parser error. Doing this
means we can simplify other strtoull checks, but I agree having a
better error message for hexadecimal would be good. Let's do it as
follow up.

Thanks,
Ian

> > Run 'perf list' for a list of valid events
> >
> >  Usage: perf stat [<options>] [<command>]
> >
> >     -e, --event <event>   event selector. use 'perf list' to list available events
> > ```
> >
> > Signed-off-by: Ian Rogers <irogers@google.com>
> > ---
> >  tools/perf/util/parse-events.l | 40 ++++++++++++++++++++--------------
> >  1 file changed, 24 insertions(+), 16 deletions(-)
> >
> > diff --git a/tools/perf/util/parse-events.l b/tools/perf/util/parse-events.l
> > index 6fe37003ab7b..0cd68c9f0d4f 100644
> > --- a/tools/perf/util/parse-events.l
> > +++ b/tools/perf/util/parse-events.l
> > @@ -18,26 +18,34 @@
> >
> >  char *parse_events_get_text(yyscan_t yyscanner);
> >  YYSTYPE *parse_events_get_lval(yyscan_t yyscanner);
> > +int parse_events_get_column(yyscan_t yyscanner);
> > +int parse_events_get_leng(yyscan_t yyscanner);
> >
> > -static int __value(YYSTYPE *yylval, char *str, int base, int token)
> > +static int get_column(yyscan_t scanner)
> >  {
> > -     u64 num;
> > -
> > -     errno = 0;
> > -     num = strtoull(str, NULL, base);
> > -     if (errno)
> > -             return PE_ERROR;
> > -
> > -     yylval->num = num;
> > -     return token;
> > +     return parse_events_get_column(scanner) - parse_events_get_leng(scanner);
> >  }
> >
> > -static int value(yyscan_t scanner, int base)
> > +static int value(struct parse_events_state *parse_state, yyscan_t scanner, int base)
> >  {
> >       YYSTYPE *yylval = parse_events_get_lval(scanner);
> >       char *text = parse_events_get_text(scanner);
> > +     u64 num;
> >
> > -     return __value(yylval, text, base, PE_VALUE);
> > +     errno = 0;
> > +     num = strtoull(text, NULL, base);
> > +     if (errno) {
> > +             struct parse_events_error *error = parse_state->error;
> > +             char *help = NULL;
> > +
> > +             if (asprintf(&help, "Bad base %d number \"%s\"", base, text) > 0)
> > +                     parse_events_error__handle(error, get_column(scanner), help , NULL);
> > +
> > +             return PE_ERROR;
> > +     }
> > +
> > +     yylval->num = num;
> > +     return PE_VALUE;
> >  }
> >
> >  static int str(yyscan_t scanner, int token)
> > @@ -283,8 +291,8 @@ r0x{num_raw_hex}  { return str(yyscanner, PE_RAW); }
> >        */
> >  "/"/{digit}          { return PE_BP_SLASH; }
> >  "/"/{non_digit}              { BEGIN(config); return '/'; }
> > -{num_dec}            { return value(yyscanner, 10); }
> > -{num_hex}            { return value(yyscanner, 16); }
> > +{num_dec}            { return value(_parse_state, yyscanner, 10); }
> > +{num_hex}            { return value(_parse_state, yyscanner, 16); }
> >       /*
> >        * We need to separate 'mem:' scanner part, in order to get specific
> >        * modifier bits parsed out. Otherwise we would need to handle PE_NAME
> > @@ -330,8 +338,8 @@ cgroup-switches                                   { return sym(yyscanner, PERF_COUNT_SW_CGROUP_SWITCHES); }
> >  {lc_type}-{lc_op_result}-{lc_op_result}      { return str(yyscanner, PE_LEGACY_CACHE); }
> >  mem:                 { BEGIN(mem); return PE_PREFIX_MEM; }
> >  r{num_raw_hex}               { return str(yyscanner, PE_RAW); }
> > -{num_dec}            { return value(yyscanner, 10); }
> > -{num_hex}            { return value(yyscanner, 16); }
> > +{num_dec}            { return value(_parse_state, yyscanner, 10); }
> > +{num_hex}            { return value(_parse_state, yyscanner, 16); }
> >
> >  {modifier_event}     { return str(yyscanner, PE_MODIFIER_EVENT); }
> >  {name}                       { return str(yyscanner, PE_NAME); }
Liang, Kan April 19, 2024, 1:29 p.m. UTC | #3
On 2024-04-18 5:07 p.m., Ian Rogers wrote:
> On Thu, Apr 18, 2024 at 1:27 PM Liang, Kan <kan.liang@linux.intel.com> wrote:
>>
>>
>>
>> On 2024-04-16 2:15 a.m., Ian Rogers wrote:
>>> Use the error handler from the parse_state to give a more informative
>>> error message.
>>>
>>> Before:
>>> ```
>>> $ perf stat -e 'cycles/period=99999999999999999999/' true
>>> event syntax error: 'cycles/period=99999999999999999999/'
>>>                                   \___ parser error
>>> Run 'perf list' for a list of valid events
>>>
>>>  Usage: perf stat [<options>] [<command>]
>>>
>>>     -e, --event <event>   event selector. use 'perf list' to list available events
>>> ```
>>>
>>> After:
>>> ```
>>> $ perf stat -e 'cycles/period=99999999999999999999/' true
>>> event syntax error: 'cycles/period=99999999999999999999/'
>>>                                   \___ parser error
>>>
>>> event syntax error: '..les/period=99999999999999999999/'
>>>                                   \___ Bad base 10 number "99999999999999999999"
>>
>>
>> It seems the patch only works for decimal?
>>
>> ./perf stat -e 'cycles/period=0xaaaaaaaaaaaaaaaaaaaaaa/' true
>> event syntax error: '..les/period=0xaaaaaaaaaaaaaaaaaaaaaa/'
>>                                    \___ parser error
>>  Run 'perf list' for a list of valid events
>>
>>   Usage: perf stat [<options>] [<command>]
>>
>>      -e, --event <event>   event selector. use 'perf list' to list
>> available events
>>
>> Thanks,
>> Kan
> 
> Right, for hexadecimal we say the number of digits is at most 16, so
> when you exceed this the token is no longer recognized. It just
> becomes input that can't be parsed, hence parser error. Doing this
> means we can simplify other strtoull checks, but I agree having a
> better error message for hexadecimal would be good. Let's do it as
> follow up.

OK. There is already a warning. It's fine to provide a follow-up for a
better error message later.

Thanks,
Kan

> 
> Thanks,
> Ian
> 
>>> Run 'perf list' for a list of valid events
>>>
>>>  Usage: perf stat [<options>] [<command>]
>>>
>>>     -e, --event <event>   event selector. use 'perf list' to list available events
>>> ```
>>>
>>> Signed-off-by: Ian Rogers <irogers@google.com>
>>> ---
>>>  tools/perf/util/parse-events.l | 40 ++++++++++++++++++++--------------
>>>  1 file changed, 24 insertions(+), 16 deletions(-)
>>>
>>> diff --git a/tools/perf/util/parse-events.l b/tools/perf/util/parse-events.l
>>> index 6fe37003ab7b..0cd68c9f0d4f 100644
>>> --- a/tools/perf/util/parse-events.l
>>> +++ b/tools/perf/util/parse-events.l
>>> @@ -18,26 +18,34 @@
>>>
>>>  char *parse_events_get_text(yyscan_t yyscanner);
>>>  YYSTYPE *parse_events_get_lval(yyscan_t yyscanner);
>>> +int parse_events_get_column(yyscan_t yyscanner);
>>> +int parse_events_get_leng(yyscan_t yyscanner);
>>>
>>> -static int __value(YYSTYPE *yylval, char *str, int base, int token)
>>> +static int get_column(yyscan_t scanner)
>>>  {
>>> -     u64 num;
>>> -
>>> -     errno = 0;
>>> -     num = strtoull(str, NULL, base);
>>> -     if (errno)
>>> -             return PE_ERROR;
>>> -
>>> -     yylval->num = num;
>>> -     return token;
>>> +     return parse_events_get_column(scanner) - parse_events_get_leng(scanner);
>>>  }
>>>
>>> -static int value(yyscan_t scanner, int base)
>>> +static int value(struct parse_events_state *parse_state, yyscan_t scanner, int base)
>>>  {
>>>       YYSTYPE *yylval = parse_events_get_lval(scanner);
>>>       char *text = parse_events_get_text(scanner);
>>> +     u64 num;
>>>
>>> -     return __value(yylval, text, base, PE_VALUE);
>>> +     errno = 0;
>>> +     num = strtoull(text, NULL, base);
>>> +     if (errno) {
>>> +             struct parse_events_error *error = parse_state->error;
>>> +             char *help = NULL;
>>> +
>>> +             if (asprintf(&help, "Bad base %d number \"%s\"", base, text) > 0)
>>> +                     parse_events_error__handle(error, get_column(scanner), help , NULL);
>>> +
>>> +             return PE_ERROR;
>>> +     }
>>> +
>>> +     yylval->num = num;
>>> +     return PE_VALUE;
>>>  }
>>>
>>>  static int str(yyscan_t scanner, int token)
>>> @@ -283,8 +291,8 @@ r0x{num_raw_hex}  { return str(yyscanner, PE_RAW); }
>>>        */
>>>  "/"/{digit}          { return PE_BP_SLASH; }
>>>  "/"/{non_digit}              { BEGIN(config); return '/'; }
>>> -{num_dec}            { return value(yyscanner, 10); }
>>> -{num_hex}            { return value(yyscanner, 16); }
>>> +{num_dec}            { return value(_parse_state, yyscanner, 10); }
>>> +{num_hex}            { return value(_parse_state, yyscanner, 16); }
>>>       /*
>>>        * We need to separate 'mem:' scanner part, in order to get specific
>>>        * modifier bits parsed out. Otherwise we would need to handle PE_NAME
>>> @@ -330,8 +338,8 @@ cgroup-switches                                   { return sym(yyscanner, PERF_COUNT_SW_CGROUP_SWITCHES); }
>>>  {lc_type}-{lc_op_result}-{lc_op_result}      { return str(yyscanner, PE_LEGACY_CACHE); }
>>>  mem:                 { BEGIN(mem); return PE_PREFIX_MEM; }
>>>  r{num_raw_hex}               { return str(yyscanner, PE_RAW); }
>>> -{num_dec}            { return value(yyscanner, 10); }
>>> -{num_hex}            { return value(yyscanner, 16); }
>>> +{num_dec}            { return value(_parse_state, yyscanner, 10); }
>>> +{num_hex}            { return value(_parse_state, yyscanner, 16); }
>>>
>>>  {modifier_event}     { return str(yyscanner, PE_MODIFIER_EVENT); }
>>>  {name}                       { return str(yyscanner, PE_NAME); }
>
Arnaldo Carvalho de Melo April 27, 2024, 1:35 a.m. UTC | #4
On Mon, Apr 15, 2024 at 11:15:27PM -0700, Ian Rogers wrote:
> Use the error handler from the parse_state to give a more informative
> error message.
> 
> Before:
> ```
> $ perf stat -e 'cycles/period=99999999999999999999/' true
> event syntax error: 'cycles/period=99999999999999999999/'
>                                   \___ parser error
> Run 'perf list' for a list of valid events
> 
>  Usage: perf stat [<options>] [<command>]
> 
>     -e, --event <event>   event selector. use 'perf list' to list available events
> ```
> 
> After:
> ```
> $ perf stat -e 'cycles/period=99999999999999999999/' true
> event syntax error: 'cycles/period=99999999999999999999/'
>                                   \___ parser error
> 

This ended up in perf-tools-next, will have to look at what this problem
is:

   9    11.46 amazonlinux:2                 : FAIL gcc version 7.3.1 20180712 (Red Hat 7.3.1-17) (GCC) 
     yy_size_t parse_events_get_leng (yyscan_t yyscanner );
               ^~~~~~~~~~~~~~~~~~~~~
    util/parse-events.l:22:5: note: previous declaration of 'parse_events_get_leng' was here
     int parse_events_get_leng(yyscan_t yyscanner);
         ^~~~~~~~~~~~~~~~~~~~~
     yy_size_t parse_events_get_leng  (yyscan_t yyscanner)
               ^~~~~~~~~~~~~~~~~~~~~
    util/parse-events.l:22:5: note: previous declaration of 'parse_events_get_leng' was here
     int parse_events_get_leng(yyscan_t yyscanner);
         ^~~~~~~~~~~~~~~~~~~~~
    make[3]: *** [util] Error 2


Unsure if this will appear on the radar on other distros, maybe this is
just something that pops up with older distros...

Ran out of time today...

- Arnaldo
Arnaldo Carvalho de Melo April 27, 2024, 1:36 a.m. UTC | #5
On Fri, Apr 26, 2024 at 10:36:02PM -0300, Arnaldo Carvalho de Melo wrote:
> On Mon, Apr 15, 2024 at 11:15:27PM -0700, Ian Rogers wrote:
> > Use the error handler from the parse_state to give a more informative
> > error message.
> > 
> > Before:
> > ```
> > $ perf stat -e 'cycles/period=99999999999999999999/' true
> > event syntax error: 'cycles/period=99999999999999999999/'
> >                                   \___ parser error
> > Run 'perf list' for a list of valid events
> > 
> >  Usage: perf stat [<options>] [<command>]
> > 
> >     -e, --event <event>   event selector. use 'perf list' to list available events
> > ```
> > 
> > After:
> > ```
> > $ perf stat -e 'cycles/period=99999999999999999999/' true
> > event syntax error: 'cycles/period=99999999999999999999/'
> >                                   \___ parser error
> > 
> 
> This ended up in perf-tools-next, will have to look at what this problem
> is:
> 
>    9    11.46 amazonlinux:2                 : FAIL gcc version 7.3.1 20180712 (Red Hat 7.3.1-17) (GCC) 
>      yy_size_t parse_events_get_leng (yyscan_t yyscanner );
>                ^~~~~~~~~~~~~~~~~~~~~
>     util/parse-events.l:22:5: note: previous declaration of 'parse_events_get_leng' was here
>      int parse_events_get_leng(yyscan_t yyscanner);
>          ^~~~~~~~~~~~~~~~~~~~~
>      yy_size_t parse_events_get_leng  (yyscan_t yyscanner)
>                ^~~~~~~~~~~~~~~~~~~~~
>     util/parse-events.l:22:5: note: previous declaration of 'parse_events_get_leng' was here
>      int parse_events_get_leng(yyscan_t yyscanner);
>          ^~~~~~~~~~~~~~~~~~~~~
>     make[3]: *** [util] Error 2
> 
> 
> Unsure if this will appear on the radar on other distros, maybe this is
> just something that pops up with older distros...
> 
> Ran out of time today...

Context:

perfbuilder@number:~$ export BUILD_TARBALL=http://192.168.86.42/perf/perf-6.9.0-rc5.tar.xz
perfbuilder@number:~$ time dm
   1   102.33 almalinux:8                   : Ok   gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-20) , clang version 16.0.6 (Red Hat 16.0.6-2.module_el8.9.0+3621+df7f7146) flex 2.6.1
   2   102.44 almalinux:9                   : Ok   gcc (GCC) 11.4.1 20230605 (Red Hat 11.4.1-2) , clang version 16.0.6 (Red Hat 16.0.6-1.el9) flex 2.6.4
   3   124.34 alpine:3.15                   : Ok   gcc (Alpine 10.3.1_git20211027) 10.3.1 20211027 , Alpine clang version 12.0.1 flex 2.6.4
   4   109.42 alpine:3.16                   : Ok   gcc (Alpine 11.2.1_git20220219) 11.2.1 20220219 , Alpine clang version 13.0.1 flex 2.6.4
   5    90.08 alpine:3.17                   : Ok   gcc (Alpine 12.2.1_git20220924-r4) 12.2.1 20220924 , Alpine clang version 15.0.7 flex 2.6.4
   6    84.85 alpine:3.18                   : Ok   gcc (Alpine 12.2.1_git20220924-r10) 12.2.1 20220924 , Alpine clang version 16.0.6 flex 2.6.4
   7    94.18 alpine:3.19                   : Ok   gcc (Alpine 13.2.1_git20231014) 13.2.1 20231014 , Alpine clang version 17.0.5 flex 2.6.4
   8    95.45 alpine:edge                   : Ok   gcc (Alpine 13.2.1_git20240309) 13.2.1 20240309 , Alpine clang version 17.0.6 flex 2.6.4
   9    11.46 amazonlinux:2                 : FAIL gcc version 7.3.1 20180712 (Red Hat 7.3.1-17) (GCC) 
     yy_size_t parse_events_get_leng (yyscan_t yyscanner );
               ^~~~~~~~~~~~~~~~~~~~~
    util/parse-events.l:22:5: note: previous declaration of 'parse_events_get_leng' was here
     int parse_events_get_leng(yyscan_t yyscanner);
         ^~~~~~~~~~~~~~~~~~~~~
     yy_size_t parse_events_get_leng  (yyscan_t yyscanner)
               ^~~~~~~~~~~~~~~~~~~~~
    util/parse-events.l:22:5: note: previous declaration of 'parse_events_get_leng' was here
     int parse_events_get_leng(yyscan_t yyscanner);
         ^~~~~~~~~~~~~~~~~~~~~
    make[3]: *** [util] Error 2
  10    88.41 amazonlinux:2023              : Ok   gcc (GCC) 11.4.1 20230605 (Red Hat 11.4.1-2) , clang version 15.0.7 (Amazon Linux 15.0.7-3.amzn2023.0.1) flex 2.6.4
  11    89.72 amazonlinux:devel             : Ok   gcc (GCC) 11.3.1 20221121 (Red Hat 11.3.1-4) , clang version 15.0.6 (Amazon Linux 15.0.6-3.amzn2023.0.2) flex 2.6.4
  12   115.65 archlinux:base                : Ok   gcc (GCC) 13.2.1 20230801 , clang version 17.0.6 flex 2.6.4
  13    93.87 centos:stream                 : Ok   gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-21) , clang version 17.0.6 (Red Hat 17.0.6-1.module_el8+767+9fa966b8) flex 2.6.1
Ian Rogers April 27, 2024, 10:05 p.m. UTC | #6
On Fri, Apr 26, 2024 at 6:36 PM Arnaldo Carvalho de Melo
<acme@kernel.org> wrote:
>
> On Fri, Apr 26, 2024 at 10:36:02PM -0300, Arnaldo Carvalho de Melo wrote:
> > On Mon, Apr 15, 2024 at 11:15:27PM -0700, Ian Rogers wrote:
> > > Use the error handler from the parse_state to give a more informative
> > > error message.
> > >
> > > Before:
> > > ```
> > > $ perf stat -e 'cycles/period=99999999999999999999/' true
> > > event syntax error: 'cycles/period=99999999999999999999/'
> > >                                   \___ parser error
> > > Run 'perf list' for a list of valid events
> > >
> > >  Usage: perf stat [<options>] [<command>]
> > >
> > >     -e, --event <event>   event selector. use 'perf list' to list available events
> > > ```
> > >
> > > After:
> > > ```
> > > $ perf stat -e 'cycles/period=99999999999999999999/' true
> > > event syntax error: 'cycles/period=99999999999999999999/'
> > >                                   \___ parser error
> > >
> >
> > This ended up in perf-tools-next, will have to look at what this problem
> > is:
> >
> >    9    11.46 amazonlinux:2                 : FAIL gcc version 7.3.1 20180712 (Red Hat 7.3.1-17) (GCC)
> >      yy_size_t parse_events_get_leng (yyscan_t yyscanner );
> >                ^~~~~~~~~~~~~~~~~~~~~
> >     util/parse-events.l:22:5: note: previous declaration of 'parse_events_get_leng' was here
> >      int parse_events_get_leng(yyscan_t yyscanner);
> >          ^~~~~~~~~~~~~~~~~~~~~
> >      yy_size_t parse_events_get_leng  (yyscan_t yyscanner)
> >                ^~~~~~~~~~~~~~~~~~~~~
> >     util/parse-events.l:22:5: note: previous declaration of 'parse_events_get_leng' was here
> >      int parse_events_get_leng(yyscan_t yyscanner);
> >          ^~~~~~~~~~~~~~~~~~~~~
> >     make[3]: *** [util] Error 2
> >
> >
> > Unsure if this will appear on the radar on other distros, maybe this is
> > just something that pops up with older distros...
> >
> > Ran out of time today...
>
> Context:
>
> perfbuilder@number:~$ export BUILD_TARBALL=http://192.168.86.42/perf/perf-6.9.0-rc5.tar.xz
> perfbuilder@number:~$ time dm
>    1   102.33 almalinux:8                   : Ok   gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-20) , clang version 16.0.6 (Red Hat 16.0.6-2.module_el8.9.0+3621+df7f7146) flex 2.6.1
>    2   102.44 almalinux:9                   : Ok   gcc (GCC) 11.4.1 20230605 (Red Hat 11.4.1-2) , clang version 16.0.6 (Red Hat 16.0.6-1.el9) flex 2.6.4
>    3   124.34 alpine:3.15                   : Ok   gcc (Alpine 10.3.1_git20211027) 10.3.1 20211027 , Alpine clang version 12.0.1 flex 2.6.4
>    4   109.42 alpine:3.16                   : Ok   gcc (Alpine 11.2.1_git20220219) 11.2.1 20220219 , Alpine clang version 13.0.1 flex 2.6.4
>    5    90.08 alpine:3.17                   : Ok   gcc (Alpine 12.2.1_git20220924-r4) 12.2.1 20220924 , Alpine clang version 15.0.7 flex 2.6.4
>    6    84.85 alpine:3.18                   : Ok   gcc (Alpine 12.2.1_git20220924-r10) 12.2.1 20220924 , Alpine clang version 16.0.6 flex 2.6.4
>    7    94.18 alpine:3.19                   : Ok   gcc (Alpine 13.2.1_git20231014) 13.2.1 20231014 , Alpine clang version 17.0.5 flex 2.6.4
>    8    95.45 alpine:edge                   : Ok   gcc (Alpine 13.2.1_git20240309) 13.2.1 20240309 , Alpine clang version 17.0.6 flex 2.6.4
>    9    11.46 amazonlinux:2                 : FAIL gcc version 7.3.1 20180712 (Red Hat 7.3.1-17) (GCC)
>      yy_size_t parse_events_get_leng (yyscan_t yyscanner );
>                ^~~~~~~~~~~~~~~~~~~~~
>     util/parse-events.l:22:5: note: previous declaration of 'parse_events_get_leng' was here
>      int parse_events_get_leng(yyscan_t yyscanner);
>          ^~~~~~~~~~~~~~~~~~~~~
>      yy_size_t parse_events_get_leng  (yyscan_t yyscanner)
>                ^~~~~~~~~~~~~~~~~~~~~
>     util/parse-events.l:22:5: note: previous declaration of 'parse_events_get_leng' was here
>      int parse_events_get_leng(yyscan_t yyscanner);
>          ^~~~~~~~~~~~~~~~~~~~~
>     make[3]: *** [util] Error 2
>   10    88.41 amazonlinux:2023              : Ok   gcc (GCC) 11.4.1 20230605 (Red Hat 11.4.1-2) , clang version 15.0.7 (Amazon Linux 15.0.7-3.amzn2023.0.1) flex 2.6.4
>   11    89.72 amazonlinux:devel             : Ok   gcc (GCC) 11.3.1 20221121 (Red Hat 11.3.1-4) , clang version 15.0.6 (Amazon Linux 15.0.6-3.amzn2023.0.2) flex 2.6.4
>   12   115.65 archlinux:base                : Ok   gcc (GCC) 13.2.1 20230801 , clang version 17.0.6 flex 2.6.4
>   13    93.87 centos:stream                 : Ok   gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-21) , clang version 17.0.6 (Red Hat 17.0.6-1.module_el8+767+9fa966b8) flex 2.6.1

Did RedHat do value add on flex output? yyget_leng is documented to
have an int return type:
https://github.com/westes/flex/blob/master/doc/flex.texi#L4613
This patch just adds a forward declaration in order to use it in a
helper function.

Thanks,
Ian
diff mbox series

Patch

diff --git a/tools/perf/util/parse-events.l b/tools/perf/util/parse-events.l
index 6fe37003ab7b..0cd68c9f0d4f 100644
--- a/tools/perf/util/parse-events.l
+++ b/tools/perf/util/parse-events.l
@@ -18,26 +18,34 @@ 
 
 char *parse_events_get_text(yyscan_t yyscanner);
 YYSTYPE *parse_events_get_lval(yyscan_t yyscanner);
+int parse_events_get_column(yyscan_t yyscanner);
+int parse_events_get_leng(yyscan_t yyscanner);
 
-static int __value(YYSTYPE *yylval, char *str, int base, int token)
+static int get_column(yyscan_t scanner)
 {
-	u64 num;
-
-	errno = 0;
-	num = strtoull(str, NULL, base);
-	if (errno)
-		return PE_ERROR;
-
-	yylval->num = num;
-	return token;
+	return parse_events_get_column(scanner) - parse_events_get_leng(scanner);
 }
 
-static int value(yyscan_t scanner, int base)
+static int value(struct parse_events_state *parse_state, yyscan_t scanner, int base)
 {
 	YYSTYPE *yylval = parse_events_get_lval(scanner);
 	char *text = parse_events_get_text(scanner);
+	u64 num;
 
-	return __value(yylval, text, base, PE_VALUE);
+	errno = 0;
+	num = strtoull(text, NULL, base);
+	if (errno) {
+		struct parse_events_error *error = parse_state->error;
+		char *help = NULL;
+
+		if (asprintf(&help, "Bad base %d number \"%s\"", base, text) > 0)
+			parse_events_error__handle(error, get_column(scanner), help , NULL);
+
+		return PE_ERROR;
+	}
+
+	yylval->num = num;
+	return PE_VALUE;
 }
 
 static int str(yyscan_t scanner, int token)
@@ -283,8 +291,8 @@  r0x{num_raw_hex}	{ return str(yyscanner, PE_RAW); }
 	 */
 "/"/{digit}		{ return PE_BP_SLASH; }
 "/"/{non_digit}		{ BEGIN(config); return '/'; }
-{num_dec}		{ return value(yyscanner, 10); }
-{num_hex}		{ return value(yyscanner, 16); }
+{num_dec}		{ return value(_parse_state, yyscanner, 10); }
+{num_hex}		{ return value(_parse_state, yyscanner, 16); }
 	/*
 	 * We need to separate 'mem:' scanner part, in order to get specific
 	 * modifier bits parsed out. Otherwise we would need to handle PE_NAME
@@ -330,8 +338,8 @@  cgroup-switches					{ return sym(yyscanner, PERF_COUNT_SW_CGROUP_SWITCHES); }
 {lc_type}-{lc_op_result}-{lc_op_result}	{ return str(yyscanner, PE_LEGACY_CACHE); }
 mem:			{ BEGIN(mem); return PE_PREFIX_MEM; }
 r{num_raw_hex}		{ return str(yyscanner, PE_RAW); }
-{num_dec}		{ return value(yyscanner, 10); }
-{num_hex}		{ return value(yyscanner, 16); }
+{num_dec}		{ return value(_parse_state, yyscanner, 10); }
+{num_hex}		{ return value(_parse_state, yyscanner, 16); }
 
 {modifier_event}	{ return str(yyscanner, PE_MODIFIER_EVENT); }
 {name}			{ return str(yyscanner, PE_NAME); }