Skip to content

Commit

Permalink
tools lib traceevent: Refactor process_filter()
Browse files Browse the repository at this point in the history
So that it can return a proper pevent_errno value.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Steven Rostedt <rostedt@goodmis.org>
Link: http://lkml.kernel.org/r/1386833777-3790-11-git-send-email-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
  • Loading branch information
Namhyung Kim authored and Arnaldo Carvalho de Melo committed Dec 13, 2013
1 parent c8ea690 commit 42d6194
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
6 changes: 5 additions & 1 deletion tools/lib/traceevent/event-parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,11 @@ enum pevent_flag {
_PE(REPARENT_NOT_OP, "cannot reparent other than OP"), \
_PE(REPARENT_FAILED, "failed to reparent filter OP"), \
_PE(BAD_FILTER_ARG, "bad arg in filter tree"), \
_PE(UNEXPECTED_TYPE, "unexpected type (not a value)")
_PE(UNEXPECTED_TYPE, "unexpected type (not a value)"), \
_PE(ILLEGAL_TOKEN, "illegal token"), \
_PE(INVALID_PAREN, "open parenthesis cannot come here"), \
_PE(UNBALANCED_PAREN, "unbalanced number of parenthesis"), \
_PE(UNKNOWN_TOKEN, "unknown token")

#undef _PE
#define _PE(__code, __str) PEVENT_ERRNO__ ## __code
Expand Down
64 changes: 37 additions & 27 deletions tools/lib/traceevent/parse-filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -937,9 +937,10 @@ static int test_arg(struct filter_arg *parent, struct filter_arg *arg,
}

/* Remove any unknown event fields */
static struct filter_arg *collapse_tree(struct filter_arg *arg, char **error_str)
static int collapse_tree(struct filter_arg *arg,
struct filter_arg **arg_collapsed, char **error_str)
{
enum filter_vals ret;
int ret;

ret = test_arg(arg, arg, error_str);
switch (ret) {
Expand All @@ -955,6 +956,7 @@ static struct filter_arg *collapse_tree(struct filter_arg *arg, char **error_str
arg->boolean.value = ret == FILTER_VAL_TRUE;
} else {
show_error(error_str, "Failed to allocate filter arg");
ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
}
break;

Expand All @@ -965,10 +967,11 @@ static struct filter_arg *collapse_tree(struct filter_arg *arg, char **error_str
break;
}

return arg;
*arg_collapsed = arg;
return ret;
}

static int
static enum pevent_errno
process_filter(struct event_format *event, struct filter_arg **parg,
char **error_str, int not)
{
Expand All @@ -982,7 +985,7 @@ process_filter(struct event_format *event, struct filter_arg **parg,
enum filter_op_type btype;
enum filter_exp_type etype;
enum filter_cmp_type ctype;
int ret;
enum pevent_errno ret;

*parg = NULL;

Expand All @@ -1007,48 +1010,52 @@ process_filter(struct event_format *event, struct filter_arg **parg,
if (not) {
arg = NULL;
if (current_op)
goto fail_print;
goto fail_syntax;
free(token);
*parg = current_exp;
return 0;
}
} else
goto fail_print;
goto fail_syntax;
arg = NULL;
break;

case EVENT_DELIM:
if (*token == ',') {
show_error(error_str,
"Illegal token ','");
show_error(error_str, "Illegal token ','");
ret = PEVENT_ERRNO__ILLEGAL_TOKEN;
goto fail;
}

if (*token == '(') {
if (left_item) {
show_error(error_str,
"Open paren can not come after item");
ret = PEVENT_ERRNO__INVALID_PAREN;
goto fail;
}
if (current_exp) {
show_error(error_str,
"Open paren can not come after expression");
ret = PEVENT_ERRNO__INVALID_PAREN;
goto fail;
}

ret = process_filter(event, &arg, error_str, 0);
if (ret != 1) {
if (ret == 0)
if (ret != PEVENT_ERRNO__UNBALANCED_PAREN) {
if (ret == 0) {
show_error(error_str,
"Unbalanced number of '('");
ret = PEVENT_ERRNO__UNBALANCED_PAREN;
}
goto fail;
}
ret = 0;

/* A not wants just one expression */
if (not) {
if (current_op)
goto fail_print;
goto fail_syntax;
*parg = arg;
return 0;
}
Expand All @@ -1063,19 +1070,19 @@ process_filter(struct event_format *event, struct filter_arg **parg,

} else { /* ')' */
if (!current_op && !current_exp)
goto fail_print;
goto fail_syntax;

/* Make sure everything is finished at this level */
if (current_exp && !check_op_done(current_exp))
goto fail_print;
goto fail_syntax;
if (current_op && !check_op_done(current_op))
goto fail_print;
goto fail_syntax;

if (current_op)
*parg = current_op;
else
*parg = current_exp;
return 1;
return PEVENT_ERRNO__UNBALANCED_PAREN;
}
break;

Expand All @@ -1087,21 +1094,22 @@ process_filter(struct event_format *event, struct filter_arg **parg,
case OP_BOOL:
/* Logic ops need a left expression */
if (!current_exp && !current_op)
goto fail_print;
goto fail_syntax;
/* fall through */
case OP_NOT:
/* logic only processes ops and exp */
if (left_item)
goto fail_print;
goto fail_syntax;
break;
case OP_EXP:
case OP_CMP:
if (!left_item)
goto fail_print;
goto fail_syntax;
break;
case OP_NONE:
show_error(error_str,
"Unknown op token %s", token);
ret = PEVENT_ERRNO__UNKNOWN_TOKEN;
goto fail;
}

Expand Down Expand Up @@ -1152,7 +1160,7 @@ process_filter(struct event_format *event, struct filter_arg **parg,
ret = add_left(arg, left_item);
if (ret < 0) {
arg = NULL;
goto fail_print;
goto fail_syntax;
}
current_exp = arg;
break;
Expand All @@ -1161,25 +1169,25 @@ process_filter(struct event_format *event, struct filter_arg **parg,
}
arg = NULL;
if (ret < 0)
goto fail_print;
goto fail_syntax;
break;
case EVENT_NONE:
break;
case EVENT_ERROR:
goto fail_alloc;
default:
goto fail_print;
goto fail_syntax;
}
} while (type != EVENT_NONE);

if (!current_op && !current_exp)
goto fail_print;
goto fail_syntax;

if (!current_op)
current_op = current_exp;

current_op = collapse_tree(current_op, error_str);
if (current_op == NULL)
ret = collapse_tree(current_op, parg, error_str);
if (ret < 0)
goto fail;

*parg = current_op;
Expand All @@ -1188,15 +1196,17 @@ process_filter(struct event_format *event, struct filter_arg **parg,

fail_alloc:
show_error(error_str, "failed to allocate filter arg");
ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
goto fail;
fail_print:
fail_syntax:
show_error(error_str, "Syntax error");
ret = PEVENT_ERRNO__SYNTAX_ERROR;
fail:
free_arg(current_op);
free_arg(current_exp);
free_arg(arg);
free(token);
return -1;
return ret;
}

static int
Expand Down

0 comments on commit 42d6194

Please sign in to comment.