public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Jiri Olsa <jolsa@kernel.org>
To: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: lkml <linux-kernel@vger.kernel.org>,
	Ingo Molnar <mingo@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Peter Zijlstra <a.p.zijlstra@chello.nl>,
	Michael Petlan <mpetlan@redhat.com>,
	Joe Mario <jmario@redhat.com>, Andi Kleen <ak@linux.intel.com>,
	Kajol Jain <kjain@linux.ibm.com>,
	John Garry <john.garry@huawei.com>
Subject: [PATCH 4/4] perf expr: Report line number with error
Date: Mon, 11 May 2020 22:53:07 +0200	[thread overview]
Message-ID: <20200511205307.3107775-5-jolsa@kernel.org> (raw)
In-Reply-To: <20200511205307.3107775-1-jolsa@kernel.org>

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 <jolsa@kernel.org>
---
 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


  parent reply	other threads:[~2020-05-11 20:53 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-11 20:53 [PATCHv3 0/4] perf tools: Add support for user defined metric Jiri Olsa
2020-05-11 20:53 ` [PATCH 1/4] perf expr: Add parsing support for multiple expressions Jiri Olsa
2020-05-13  6:50   ` Ian Rogers
2020-05-13 11:25     ` Jiri Olsa
2020-05-11 20:53 ` [PATCH 2/4] perf expr: Allow comments in custom metric file Jiri Olsa
2020-05-11 20:53 ` [PATCH 3/4] perf stat: Add --metrics-file option Jiri Olsa
2020-05-13  7:04   ` Ian Rogers
2020-05-13 11:33     ` Jiri Olsa
2020-05-14  3:41       ` Ian Rogers
2020-05-11 20:53 ` Jiri Olsa [this message]
2020-05-13  7:09   ` [PATCH 4/4] perf expr: Report line number with error Ian Rogers
2020-05-13 11:34     ` Jiri Olsa
2020-05-13 14:08       ` Arnaldo Carvalho de Melo
2020-05-13 14:46         ` Jiri Olsa
2020-05-13 15:14           ` Arnaldo Carvalho de Melo
2022-01-25 12:34 ` [PATCHv3 0/4] perf tools: Add support for user defined metric John Garry
2022-01-25 12:41   ` Arnaldo Carvalho de Melo
2022-01-25 12:51     ` John Garry

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200511205307.3107775-5-jolsa@kernel.org \
    --to=jolsa@kernel.org \
    --cc=a.p.zijlstra@chello.nl \
    --cc=acme@kernel.org \
    --cc=ak@linux.intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=jmario@redhat.com \
    --cc=john.garry@huawei.com \
    --cc=kjain@linux.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=mpetlan@redhat.com \
    --cc=namhyung@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox