Re: [PATCH v2 10/11] perf expr: print a debug message for division by zero

Ian Rogers
2020-04-23T14:17:04Z

On Thu, Apr 23, 2020 at 4:28 AM Jiri Olsa <jolsa@redhat.com> wrote:
>
> On Wed, Apr 22, 2020 at 03:04:29PM -0700, Ian Rogers wrote:
> > If an expression yields 0 and is then divided-by/modulus-by then the
> > parsing aborts. Add a debug error message to better enable debugging
> > when this happens.
> >
> > Signed-off-by: Ian Rogers <irogers@google.com>
> > ---
> > tools/perf/util/expr.y | 14 ++++++++++++--
> > 1 file changed, 12 insertions(+), 2 deletions(-)
> >
> > diff --git a/tools/perf/util/expr.y b/tools/perf/util/expr.y
> > index 54260094b947..21e82a1e11a2 100644
> > --- a/tools/perf/util/expr.y
> > +++ b/tools/perf/util/expr.y
> > @@ -103,8 +103,18 @@ expr: NUMBER
> > | expr '+' expr { $$ = $1 + $3; }
> > | expr '-' expr { $$ = $1 - $3; }
> > | expr '*' expr { $$ = $1 * $3; }
> > - | expr '/' expr { if ($3 == 0) YYABORT; $$ = $1 / $3; }
> > - | expr '%' expr { if ((long)$3 == 0) YYABORT; $$ = (long)$1 % (long)$3; }
> > + | expr '/' expr { if ($3 == 0) {
> > + pr_debug("division by zero\n");
> > + YYABORT;
> > + }
> > + $$ = $1 / $3;
> > + }
> > + | expr '%' expr { if ((long)$3 == 0) {
>
> is that (long) cast necessary? it's missing for the '/' case

Andi Kleen could say for sure :-) From my observation, the values are
typically doubles and definitely need to be in the divide case. There
is also code handling them as longs such as |, &, ^:
https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/tree/tools/perf/util/expr.y#n100
There's an experiment to see if all of the long handling code could be
removed. This change isn't modifying the existing behavior.

Thanks,
Ian

> jirka
>
> > + pr_debug("division by zero\n");
> > + YYABORT;
> > + }
> > + $$ = (long)$1 % (long)$3;
> > + }
> > | '-' expr %prec NEG { $$ = -$2; }
> > | '(' if_expr ')' { $$ = $2; }
> > | MIN '(' expr ',' expr ')' { $$ = $3 < $5 ? $3 : $5; }
> > --
> > 2.26.2.303.gf8c07b1a785-goog
> >
>