Display line number on when parsing custom metrics file, like:

  $ cat metrics
  // IPC
  mine1 = inst_retired.any / cpu_clk_unhalted.thread;

  krava
  $ sudo perf stat --metrics-file ./metrics -M mine1 -a -I 1000 --metric-only
  failed to parse metrics file: ./metrics:4

Please note that because the grammar is flexible on new lines,
the syntax could be broken on the next 'not fitting' item and
not the first wrong word, like:

  $ cat metrics
  // IPC
  krava
  mine1 = inst_retired.any / cpu_clk_unhalted.thread;
  $ sudo perf stat --metrics-file ./metrics -M mine1 -a -I 1000 --metric-only
  failed to parse metrics file: ./metrics:3

Signed-off-by: Jiri Olsa <[email protected]>
---
 tools/perf/tests/expr.c       |  5 +++++
 tools/perf/util/expr.h        |  2 ++
 tools/perf/util/expr.l        | 10 ++++++++++
 tools/perf/util/expr.y        |  8 +++++---
 tools/perf/util/metricgroup.c |  3 ++-
 5 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/tools/perf/tests/expr.c b/tools/perf/tests/expr.c
index c62e122fe719..3bffcd9f8397 100644
--- a/tools/perf/tests/expr.c
+++ b/tools/perf/tests/expr.c
@@ -84,5 +84,10 @@ int test__expr(struct test *t __maybe_unused, int subtest 
__maybe_unused)
                zfree(&ctx.custom[i].name);
                zfree(&ctx.custom[i].expr);
        }
+
+       expr__ctx_init(&ctx);
+       ret = expr__parse_custom(&ctx, "IPC=INSTRUCTIONS / CYCLES;\n error");
+       TEST_ASSERT_VAL("parse custom failed", ret != 0);
+       TEST_ASSERT_VAL("parse custom count", ctx.lineno == 2);
        return 0;
 }
diff --git a/tools/perf/util/expr.h b/tools/perf/util/expr.h
index ef116b58a5d4..ce95dfd2ad5a 100644
--- a/tools/perf/util/expr.h
+++ b/tools/perf/util/expr.h
@@ -27,6 +27,8 @@ struct expr_parse_ctx {
                        struct expr_parse_custom custom[EXPR_MAX];
                };
        };
+       /* Set on error for custom metrics. */
+       int lineno;
 };
 
 struct expr_scanner_ctx {
diff --git a/tools/perf/util/expr.l b/tools/perf/util/expr.l
index e9b294ba09fc..718aac4316d7 100644
--- a/tools/perf/util/expr.l
+++ b/tools/perf/util/expr.l
@@ -1,6 +1,8 @@
 %option prefix="expr_"
 %option reentrant
 %option bison-bridge
+%option bison-locations
+%option yylineno
 
 %{
 #include <linux/compiler.h>
@@ -79,6 +81,13 @@ static int str(yyscan_t scanner, int token, int runtime)
        yylval->str = normalize(yylval->str, runtime);
        return token;
 }
+
+#define YY_USER_ACTION                         \
+do {                                           \
+       yylloc->last_line = yylloc->first_line; \
+       yylloc->first_line = yylineno;          \
+} while (0);
+
 %}
 
 %x custom
@@ -101,6 +110,7 @@ all         [^;]+
 
                if (sctx->start_token) {
                        sctx->start_token = 0;
+                       yylineno = 1;
                        return start_token;
                }
        }
diff --git a/tools/perf/util/expr.y b/tools/perf/util/expr.y
index 0521e48fa5e3..812d893bcd31 100644
--- a/tools/perf/util/expr.y
+++ b/tools/perf/util/expr.y
@@ -18,6 +18,7 @@
 %parse-param { struct expr_parse_ctx *ctx }
 %parse-param {void *scanner}
 %lex-param {void* scanner}
+%locations
 
 %union {
        double   num;
@@ -39,12 +40,13 @@
 %type <num> expr if_expr
 
 %{
-static void expr_error(double *final_val __maybe_unused,
-                      struct expr_parse_ctx *ctx __maybe_unused,
+static void expr_error(YYLTYPE *loc, double *final_val __maybe_unused,
+                      struct expr_parse_ctx *ctx,
                       void *scanner,
                       const char *s)
 {
-       pr_debug("%s\n", s);
+       pr_debug("%s, line %d\n", s, loc->last_line);
+       ctx->lineno = loc->last_line;
 }
 
 static int lookup_id(struct expr_parse_ctx *ctx, char *id, double *val)
diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
index 3b4d5bdb5ac6..36df43546e84 100644
--- a/tools/perf/util/metricgroup.c
+++ b/tools/perf/util/metricgroup.c
@@ -615,7 +615,8 @@ static int metricgroup__add_metric_list(const char *list, 
struct strbuf *events,
                ret = expr__parse_custom(&ctx, buf);
                free(buf);
                if (ret) {
-                       pr_err("failed to parse metrics file: %s\n", 
metrics_file);
+                       pr_err("failed to parse metrics file: %s:%d\n",
+                               metrics_file, ctx.lineno);
                        return -1;
                }
        }
-- 
2.25.4

Reply via email to