Implement support for brackets in filter expressions
[lttng-tools.git] / src / lib / lttng-ctl / filter / filter-visitor-generate-ir.c
index 84122c90478890260591d65b9a7ad0e2a49ca954..85aadcae6fe7baedec1b30d8d34960c65c5b43fd 100644 (file)
@@ -30,6 +30,9 @@
 #include "filter-parser.h"
 #include "filter-ir.h"
 
+#include <common/macros.h>
+#include <common/string-utils/string-utils.h>
+
 static
 struct ir_op *generate_ir_recursive(struct filter_parser_ctx *ctx,
                struct filter_node *node, enum ir_side side);
@@ -54,6 +57,7 @@ struct ir_op *make_op_root(struct ir_op *child, enum ir_side side)
                return NULL;
        case IR_DATA_NUMERIC:
        case IR_DATA_FIELD_REF:
+       case IR_DATA_GET_CONTEXT_REF:
                /* ok */
                break;
        }
@@ -65,6 +69,22 @@ struct ir_op *make_op_root(struct ir_op *child, enum ir_side side)
        return op;
 }
 
+static
+enum ir_load_string_type get_literal_string_type(const char *string)
+{
+       assert(string);
+
+       if (strutils_is_star_glob_pattern(string)) {
+               if (strutils_is_star_at_the_end_only_glob_pattern(string)) {
+                       return IR_LOAD_STRING_TYPE_GLOB_STAR_END;
+               }
+
+               return IR_LOAD_STRING_TYPE_GLOB_STAR;
+       }
+
+       return IR_LOAD_STRING_TYPE_PLAIN;
+}
+
 static
 struct ir_op *make_op_load_string(char *string, enum ir_side side)
 {
@@ -77,8 +97,9 @@ struct ir_op *make_op_load_string(char *string, enum ir_side side)
        op->data_type = IR_DATA_STRING;
        op->signedness = IR_SIGN_UNKNOWN;
        op->side = side;
-       op->u.load.u.string = strdup(string);
-       if (!op->u.load.u.string) {
+       op->u.load.u.string.type = get_literal_string_type(string);
+       op->u.load.u.string.value = strdup(string);
+       if (!op->u.load.u.string.value) {
                free(op);
                return NULL;
        }
@@ -119,7 +140,8 @@ struct ir_op *make_op_load_float(double v, enum ir_side side)
 }
 
 static
-struct ir_op *make_op_load_field_ref(char *string, enum ir_side side)
+struct ir_op *make_op_load_field_ref(char *string,
+               enum ir_side side)
 {
        struct ir_op *op;
 
@@ -132,33 +154,51 @@ struct ir_op *make_op_load_field_ref(char *string, enum ir_side side)
        op->side = side;
        op->u.load.u.ref = strdup(string);
        if (!op->u.load.u.ref) {
-               free(op);
-               return NULL;
+               goto error;
        }
        return op;
+
+error:
+       free(op);
+       return NULL;
 }
 
 static
-struct ir_op *make_op_unary(enum unary_op_type unary_op_type,
-                       const char *op_str, enum ir_op_signedness signedness,
-                       struct ir_op *child, enum ir_side side)
+struct ir_op *make_op_load_field_ref_index(char *string,
+               struct filter_node *next,
+               enum ir_side side)
 {
-       struct ir_op *op = NULL;
-
-       if (child->data_type == IR_DATA_STRING) {
-               fprintf(stderr, "[error] unary operation '%s' not allowed on string literal\n", op_str);
-               goto error;
-       }
+       struct ir_op *op;
 
        op = calloc(sizeof(struct ir_op), 1);
        if (!op)
                return NULL;
-       op->op = IR_OP_UNARY;
-       op->data_type = child->data_type;
-       op->signedness = signedness;
+       op->op = IR_OP_LOAD;
+       op->data_type = IR_DATA_FIELD_REF_INDEX;
+       op->signedness = IR_SIGN_DYN;
        op->side = side;
-       op->u.unary.type = unary_op_type;
-       op->u.unary.child = child;
+       op->u.load.u.ref_index.symbol = strdup(string);
+       if (!op->u.load.u.ref_index.symbol) {
+               goto error;
+       }
+       /* Only positive integer literals accepted as index. */
+       if (next->type == NODE_UNARY_OP) {
+               fprintf(stderr, "[error] Unexpected unary operator as index\n");
+               goto error;
+       }
+       if (next->type != NODE_EXPRESSION) {
+               fprintf(stderr, "[error] Expecting expression as index\n");
+               goto error;
+       }
+       if (next->u.expression.type != AST_EXP_CONSTANT) {
+               fprintf(stderr, "[error] Expecting constant index\n");
+               goto error;
+       }
+       if (next->u.expression.u.constant < 0) {
+               fprintf(stderr, "[error] Expecting positive constant index\n");
+               goto error;
+       }
+       op->u.load.u.ref_index.index = next->u.expression.u.constant;
        return op;
 
 error:
@@ -166,80 +206,23 @@ error:
        return NULL;
 }
 
-/*
- * unary + is pretty much useless.
- */
 static
-struct ir_op *make_op_unary_plus(struct ir_op *child, enum ir_side side)
-{
-       return make_op_unary(AST_UNARY_PLUS, "+", child->signedness,
-                       child, side);
-}
-
-static
-struct ir_op *make_op_unary_minus(struct ir_op *child, enum ir_side side)
-{
-       return make_op_unary(AST_UNARY_MINUS, "-", child->signedness,
-                       child, side);
-}
-
-static
-struct ir_op *make_op_unary_not(struct ir_op *child, enum ir_side side)
-{
-       return make_op_unary(AST_UNARY_NOT, "!", child->signedness,
-                       child, side);
-}
-
-#if 0
-static
-struct ir_op *make_op_binary_numeric(enum op_type bin_op_type,
-               const char *op_str, struct ir_op *left, struct ir_op *right,
+struct ir_op *make_op_load_get_context_ref(char *string,
                enum ir_side side)
 {
-       struct ir_op *op = NULL;
-
-       if (right->data_type == IR_DATA_STRING
-                       || right->data_type == IR_DATA_STRING) {
-               fprintf(stderr, "[error] binary operation '%s' not allowed on string literal\n", op_str);
-               goto error;
-       }
-       if (left->data_type == IR_DATA_UNKNOWN
-               || right->data_type == IR_DATA_UNKNOWN) {
-               fprintf(stderr, "[error] binary operation '%s' has unknown type for both children\n", op_str);
-               goto error;
-
-       }
+       struct ir_op *op;
 
-       op = calloc(sizeof(struct ir_op_binary), 1);
+       op = calloc(sizeof(struct ir_op), 1);
        if (!op)
                return NULL;
-       op->op = IR_OP_BINARY;
-       op->u.binary.type = bin_op_type;
-       op->u.binary.left = left;
-       op->u.binary.right = right;
+       op->op = IR_OP_LOAD;
+       op->data_type = IR_DATA_GET_CONTEXT_REF;
+       op->signedness = IR_SIGN_DYN;
        op->side = side;
-
-       /*
-        * The field that is not a field ref will select type.
-        */
-       if (left->data_type != IR_DATA_FIELD_REF)
-               op->data_type = left->data_type;
-       else
-               op->data_type = right->data_type;
-
-       if (left->signedness == IR_SIGNED
-                       || right->signedness == IR_SIGNED) {
-               op->signedness = IR_SIGNED;
-       } else if (left->signedness == IR_SIGN_DYN
-                       || right->signedness == IR_SIGN_DYN) {
-               op->signedness = IR_SIGN_DYN;
-       } else if (left->signedness == IR_UNSIGNED
-                       && right->signedness == IR_UNSIGNED) {
-               op->signedness = IR_UNSIGNED;
-       } else {
-               op->signedness = IR_SIGN_UNKNOWN;
+       op->u.load.u.ref = strdup(string);
+       if (!op->u.load.u.ref) {
+               goto error;
        }
-
        return op;
 
 error:
@@ -248,75 +231,99 @@ error:
 }
 
 static
-struct ir_op *make_op_binary_mul(struct ir_op *left, struct ir_op *right,
+struct ir_op *make_op_load_get_context_ref_index(char *string,
+               struct filter_node *next,
                enum ir_side side)
 {
-       return make_op_binary_numeric(AST_OP_MUL, "*", left, right, side);
-}
+       struct ir_op *op;
 
-static
-struct ir_op *make_op_binary_div(struct ir_op *left, struct ir_op *right,
-               enum ir_side side)
-{
-       return make_op_binary_numeric(AST_OP_DIV, "/", left, right, side);
-}
+       op = calloc(sizeof(struct ir_op), 1);
+       if (!op)
+               return NULL;
+       op->op = IR_OP_LOAD;
+       op->data_type = IR_DATA_GET_CONTEXT_REF_INDEX;
+       op->signedness = IR_SIGN_DYN;
+       op->side = side;
+       op->u.load.u.ref_index.symbol = strdup(string);
+       if (!op->u.load.u.ref_index.symbol) {
+               goto error;
+       }
+       /* Only positive integer literals accepted as offset. */
+       if (next->type == NODE_UNARY_OP) {
+               fprintf(stderr, "[error] Unexpected unary operator as index\n");
+               goto error;
+       }
+       if (next->type != NODE_EXPRESSION) {
+               fprintf(stderr, "[error] Expecting expression as index\n");
+               goto error;
+       }
+       if (next->u.expression.type != AST_EXP_CONSTANT) {
+               fprintf(stderr, "[error] Expecting constant index\n");
+               goto error;
+       }
+       if (next->u.expression.u.constant < 0) {
+               fprintf(stderr, "[error] Expecting positive constant index\n");
+               goto error;
+       }
+       op->u.load.u.ref_index.index = next->u.expression.u.constant;
+       return op;
 
-static
-struct ir_op *make_op_binary_mod(struct ir_op *left, struct ir_op *right,
-               enum ir_side side)
-{
-       return make_op_binary_numeric(AST_OP_MOD, "%", left, right, side);
+error:
+       free(op);
+       return NULL;
 }
 
 static
-struct ir_op *make_op_binary_plus(struct ir_op *left, struct ir_op *right,
-               enum ir_side side)
+struct ir_op *make_op_unary(enum unary_op_type unary_op_type,
+                       const char *op_str, enum ir_op_signedness signedness,
+                       struct ir_op *child, enum ir_side side)
 {
-       return make_op_binary_numeric(AST_OP_PLUS, "+", left, right, side);
-}
+       struct ir_op *op = NULL;
 
-static
-struct ir_op *make_op_binary_minus(struct ir_op *left, struct ir_op *right,
-               enum ir_side side)
-{
-       return make_op_binary_numeric(AST_OP_MINUS, "-", left, right, side);
-}
+       if (child->data_type == IR_DATA_STRING) {
+               fprintf(stderr, "[error] unary operation '%s' not allowed on string literal\n", op_str);
+               goto error;
+       }
 
-static
-struct ir_op *make_op_binary_rshift(struct ir_op *left, struct ir_op *right,
-               enum ir_side side)
-{
-       return make_op_binary_numeric(AST_OP_RSHIFT, ">>", left, right, side);
-}
+       op = calloc(sizeof(struct ir_op), 1);
+       if (!op)
+               return NULL;
+       op->op = IR_OP_UNARY;
+       op->data_type = child->data_type;
+       op->signedness = signedness;
+       op->side = side;
+       op->u.unary.type = unary_op_type;
+       op->u.unary.child = child;
+       return op;
 
-static
-struct ir_op *make_op_binary_lshift(struct ir_op *left, struct ir_op *right,
-               enum ir_side side)
-{
-       return make_op_binary_numeric(AST_OP_LSHIFT, "<<", left, right, side);
+error:
+       free(op);
+       return NULL;
 }
 
+/*
+ * unary + is pretty much useless.
+ */
 static
-struct ir_op *make_op_binary_and(struct ir_op *left, struct ir_op *right,
-               enum ir_side side)
+struct ir_op *make_op_unary_plus(struct ir_op *child, enum ir_side side)
 {
-       return make_op_binary_numeric(AST_OP_BIN_AND, "&", left, right, side);
+       return make_op_unary(AST_UNARY_PLUS, "+", child->signedness,
+                       child, side);
 }
 
 static
-struct ir_op *make_op_binary_or(struct ir_op *left, struct ir_op *right,
-               enum ir_side side)
+struct ir_op *make_op_unary_minus(struct ir_op *child, enum ir_side side)
 {
-       return make_op_binary_numeric(AST_OP_BIN_OR, "|", left, right, side);
+       return make_op_unary(AST_UNARY_MINUS, "-", child->signedness,
+                       child, side);
 }
 
 static
-struct ir_op *make_op_binary_xor(struct ir_op *left, struct ir_op *right,
-               enum ir_side side)
+struct ir_op *make_op_unary_not(struct ir_op *child, enum ir_side side)
 {
-       return make_op_binary_numeric(AST_OP_BIN_XOR, "^", left, right, side);
+       return make_op_unary(AST_UNARY_NOT, "!", child->signedness,
+                       child, side);
 }
-#endif //0
 
 static
 struct ir_op *make_op_binary_compare(enum op_type bin_op_type,
@@ -471,11 +478,16 @@ void filter_free_ir_recursive(struct ir_op *op)
        case IR_OP_LOAD:
                switch (op->data_type) {
                case IR_DATA_STRING:
-                       free(op->u.load.u.string);
+                       free(op->u.load.u.string.value);
                        break;
-               case IR_DATA_FIELD_REF:
+               case IR_DATA_FIELD_REF:         /* fall-through */
+               case IR_DATA_GET_CONTEXT_REF:
                        free(op->u.load.u.ref);
                        break;
+               case IR_DATA_FIELD_REF_INDEX:   /* fall-through */
+               case IR_DATA_GET_CONTEXT_REF_INDEX:
+                       free(op->u.load.u.ref_index.symbol);
+                       break;
                default:
                        break;
                }
@@ -514,12 +526,57 @@ struct ir_op *make_expression(struct filter_parser_ctx *ctx,
                return make_op_load_float(node->u.expression.u.float_constant,
                                        side);
        case AST_EXP_IDENTIFIER:
-               if (node->u.expression.pre_op != AST_LINK_UNKNOWN) {
+               switch (node->u.expression.pre_op) {
+               case AST_LINK_UNKNOWN:
+                       return make_op_load_field_ref(node->u.expression.u.identifier,
+                                       side);
+               case AST_LINK_BRACKET:
+                       return make_op_load_field_ref_index(node->u.expression.u.identifier,
+                                       node->u.expression.next,
+                                       side);
+               default:
                        fprintf(stderr, "[error] %s: dotted and dereferenced identifiers not supported\n", __func__);
                        return NULL;
                }
-               return make_op_load_field_ref(node->u.expression.u.identifier,
+       case AST_EXP_GLOBAL_IDENTIFIER:
+       {
+               const char *name;
+
+               /*
+                * We currently only support $ctx (context) and $app
+                * identifiers.
+                */
+               if (strncmp(node->u.expression.u.identifier,
+                               "$ctx.", strlen("$ctx.")) != 0
+                       && strncmp(node->u.expression.u.identifier,
+                               "$app.", strlen("$app.")) != 0) {
+                       fprintf(stderr, "[error] %s: \"%s\" global identifier is unknown. Only \"$ctx\" and \"$app\" are currently implemented.\n", __func__, node->u.expression.u.identifier);
+                       return NULL;
+               }
+               name = strchr(node->u.expression.u.identifier, '.');
+               if (!name) {
+                       fprintf(stderr, "[error] %s: Expecting '.'\n", __func__);
+                       return NULL;
+               }
+               name++; /* Skip . */
+               if (!strlen(name)) {
+                       fprintf(stderr, "[error] %s: Expecting a context name, e.g. \'$ctx.name\'.\n", __func__);
+                       return NULL;
+               }
+               switch (node->u.expression.pre_op) {
+               case AST_LINK_UNKNOWN:
+                       return make_op_load_get_context_ref(node->u.expression.u.identifier,
+                                       side);
+               case AST_LINK_BRACKET:
+                       return make_op_load_get_context_ref_index(node->u.expression.u.identifier,
+                                       node->u.expression.next,
                                        side);
+               default:
+                       fprintf(stderr, "[error] %s: dotted and dereferenced identifiers not supported\n", __func__);
+                       return NULL;
+               }
+
+       }
        case AST_EXP_NESTED:
                return generate_ir_recursive(ctx, node->u.expression.u.child,
                                        side);
@@ -755,14 +812,14 @@ struct ir_op *generate_ir_recursive(struct filter_parser_ctx *ctx,
        return 0;
 }
 
-__attribute__((visibility("hidden")))
+LTTNG_HIDDEN
 void filter_ir_free(struct filter_parser_ctx *ctx)
 {
        filter_free_ir_recursive(ctx->ir_root);
        ctx->ir_root = NULL;
 }
 
-__attribute__((visibility("hidden")))
+LTTNG_HIDDEN
 int filter_visitor_ir_generate(struct filter_parser_ctx *ctx)
 {
        struct ir_op *op;
This page took 0.027842 seconds and 4 git commands to generate.