[RFC 05/12] perf event action: Add parsing call expr support

Yang Jihong
2024-11-28T13:36:42Z

Add support for parsing function call expression, the format is similar to
C language function call, with parameters specified after function name,
inside the parentheses and separated with a comma.

Signed-off-by: Yang Jihong <yangjihong@bytedance.com>
---
tools/perf/util/parse-action.c | 25 +++++++++++++++
tools/perf/util/parse-action.h | 5 +++
tools/perf/util/parse-action.l | 3 ++
tools/perf/util/parse-action.y | 56 +++++++++++++++++++++++++++++++---
4 files changed, 85 insertions(+), 4 deletions(-)

diff --git a/tools/perf/util/parse-action.c b/tools/perf/util/parse-action.c
index 7e5ca889d5b9..40e7c8aad7be 100644
--- a/tools/perf/util/parse-action.c
+++ b/tools/perf/util/parse-action.c
@@ -9,6 +9,7 @@
* - constant:
* - integer
* - string
+ * - call:
*/

#include "util/debug.h"
@@ -232,8 +233,32 @@ static struct evtact_expr_class expr_const = {
.set_ops = expr_const_set_ops,
};

+static struct evtact_expr_ops *expr_call_ops_list[EVTACT_EXPR_CALL_TYPE_MAX] = {
+};
+
+static int expr_call_set_ops(struct evtact_expr *expr, u32 opcode)
+{
+ if (opcode >= EVTACT_EXPR_CALL_TYPE_MAX) {
+ pr_err("expr_call opcode invalid: %u\n", opcode);
+ return -EINVAL;
+ }
+
+ if (expr_call_ops_list[opcode] == NULL) {
+ pr_err("expr_call opcode not supported: %u\n", opcode);
+ return -ENOTSUP;
+ }
+
+ expr->ops = expr_call_ops_list[opcode];
+ return 0;
+}
+
+static struct evtact_expr_class expr_call = {
+ .set_ops = expr_call_set_ops,
+};
+
static struct evtact_expr_class *expr_class_list[EVTACT_EXPR_TYPE_MAX] = {
[EVTACT_EXPR_TYPE_CONST] = &expr_const,
+ [EVTACT_EXPR_TYPE_CALL] = &expr_call,
};

int parse_action_expr__set_class(enum evtact_expr_type type,
diff --git a/tools/perf/util/parse-action.h b/tools/perf/util/parse-action.h
index ec422f8a05a7..30c2fd6e81d0 100644
--- a/tools/perf/util/parse-action.h
+++ b/tools/perf/util/parse-action.h
@@ -10,6 +10,7 @@

enum evtact_expr_type {
EVTACT_EXPR_TYPE_CONST,
+ EVTACT_EXPR_TYPE_CALL,
EVTACT_EXPR_TYPE_MAX,
};

@@ -19,6 +20,10 @@ enum evtact_expr_const_type {
EVTACT_EXPR_CONST_TYPE_MAX,
};

+enum evtact_expr_call_type {
+ EVTACT_EXPR_CALL_TYPE_MAX,
+};
+
struct evtact_expr;
struct evtact_expr_ops {
int (*new)(struct evtact_expr *expr, void *data, int size);
diff --git a/tools/perf/util/parse-action.l b/tools/perf/util/parse-action.l
index f76240276b61..189f73dfc3b1 100644
--- a/tools/perf/util/parse-action.l
+++ b/tools/perf/util/parse-action.l
@@ -92,6 +92,9 @@ ident [_a-zA-Z][_a-zA-Z0-9]*
{space} { }

";" { return SEMI; }
+"(" { return LP; }
+")" { return RP; }
+"," { return COM; }

\" {
int ret;
diff --git a/tools/perf/util/parse-action.y b/tools/perf/util/parse-action.y
index 4922b2d94aee..1b162c694218 100644
--- a/tools/perf/util/parse-action.y
+++ b/tools/perf/util/parse-action.y
@@ -35,15 +35,18 @@ static void parse_action_error(struct list_head *expr __maybe_unused,
struct evtact_expr *expr;
struct list_head *list;
unsigned long long num;
+ u32 opcode;
}

-%token IDENT ERROR NUMBER STRING
-%token SEMI
-%type <expr> action_term expr_term
+%token IDENT ERROR NUMBER STRING CALL
+%token SEMI LP RP COM
+%type <expr> action_term expr_term expr_call_term
%destructor { parse_action_expr__free($$); } <expr>
%type <str> IDENT
%type <num> NUMBER
%type <str> STRING
+%type <opcode> CALL
+%type opnds

%%

@@ -64,11 +67,56 @@ action_term
}

action_term:
-expr_term
+expr_call_term
{
$$ = $1;
}

+expr_call_term:
+CALL LP RP
+{
+ $$ = parse_action_expr__new(evtact_expr_id_encode(EVTACT_EXPR_TYPE_CALL, $1), NULL, NULL, 0);
+ if ($$ == NULL)
+ YYERROR;
+}
+|
+CALL LP opnds RP
+{
+ $$ = parse_action_expr__new(evtact_expr_id_encode(EVTACT_EXPR_TYPE_CALL, $1), $3, NULL, 0);
+ if ($$ == NULL)
+ YYERROR;
+}
+|
+IDENT LP RP
+{
+ $$ = NULL;
+ pr_err("unknown function '%s()'\n", $1);
+ free($1);
+ YYERROR;
+}
+|
+IDENT LP opnds RP
+{
+ $$ = NULL;
+ pr_err("unknown function '%s()'\n", $1);
+ parse_action_expr__free_opnds($3);
+ free($1);
+ YYERROR;
+}
+
+opnds:
+opnds COM expr_term
+{
+ list_add_tail(&$3->list, $1);
+ $$ = $1;
+}
+|
+expr_term
+{
+ INIT_LIST_HEAD(&$1->list);
+ $$ = &$1->list;
+}
+
expr_term:
NUMBER
{
--
2.25.1