Tests: Add get_possible_cpus_count utility
[lttng-tools.git] / src / common / filter / filter-visitor-generate-bytecode.c
index 51ebad130c3a30a9db044f4d3171d9a805728848..938f7d8a903febf4151183554f179b8bb17c25d8 100644 (file)
@@ -19,6 +19,7 @@
 #include "common/bytecode/bytecode.h"
 #include "common/compat/string.h"
 #include "common/macros.h"
+#include "common/string-utils/string-utils.h"
 #include "filter-ast.h"
 #include "filter-ir.h"
 
@@ -59,27 +60,6 @@ int visit_node_root(struct filter_parser_ctx *ctx, struct ir_op *node)
        return bytecode_push(&ctx->bytecode, &insn, 1, sizeof(insn));
 }
 
-static
-int append_str(char **s, const char *append)
-{
-       char *old = *s;
-       char *new;
-       size_t oldlen = (old == NULL) ? 0 : strlen(old);
-       size_t appendlen = strlen(append);
-
-       new = calloc(oldlen + appendlen + 1, 1);
-       if (!new) {
-               return -ENOMEM;
-       }
-       if (oldlen) {
-               strcpy(new, old);
-       }
-       strcat(new, append);
-       *s = new;
-       free(old);
-       return 0;
-}
-
 /*
  * 1: match
  * 0: no match
@@ -97,14 +77,14 @@ int load_expression_legacy_match(const struct ir_load_expression *exp,
        switch (op->type) {
        case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT:
                *op_type = BYTECODE_OP_GET_CONTEXT_REF;
-               if (append_str(symbol, "$ctx.")) {
+               if (strutils_append_str(symbol, "$ctx.")) {
                        return -ENOMEM;
                }
                need_dot = false;
                break;
        case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT:
                *op_type = BYTECODE_OP_GET_CONTEXT_REF;
-               if (append_str(symbol, "$app.")) {
+               if (strutils_append_str(symbol, "$app.")) {
                        return -ENOMEM;
                }
                need_dot = false;
@@ -130,10 +110,10 @@ int load_expression_legacy_match(const struct ir_load_expression *exp,
                case IR_LOAD_EXPRESSION_LOAD_FIELD:
                        goto end;
                case IR_LOAD_EXPRESSION_GET_SYMBOL:
-                       if (need_dot && append_str(symbol, ".")) {
+                       if (need_dot && strutils_append_str(symbol, ".")) {
                                return -ENOMEM;
                        }
-                       if (append_str(symbol, op->u.symbol)) {
+                       if (strutils_append_str(symbol, op->u.symbol)) {
                                return -ENOMEM;
                        }
                        break;
@@ -240,131 +220,61 @@ int visit_node_load_expression(struct filter_parser_ctx *ctx,
                switch (op->type) {
                case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT:
                {
-                       struct load_op *insn;
-                       uint32_t insn_len = sizeof(struct load_op);
-                       int ret;
+                       ret = bytecode_push_get_context_root(&ctx->bytecode);
 
-                       insn = calloc(insn_len, 1);
-                       if (!insn)
-                               return -ENOMEM;
-                       insn->op = BYTECODE_OP_GET_CONTEXT_ROOT;
-                       ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len);
-                       free(insn);
                        if (ret) {
                                return ret;
                        }
+
                        break;
                }
                case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT:
                {
-                       struct load_op *insn;
-                       uint32_t insn_len = sizeof(struct load_op);
-                       int ret;
+                       ret = bytecode_push_get_app_context_root(
+                                       &ctx->bytecode);
 
-                       insn = calloc(insn_len, 1);
-                       if (!insn)
-                               return -ENOMEM;
-                       insn->op = BYTECODE_OP_GET_APP_CONTEXT_ROOT;
-                       ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len);
-                       free(insn);
                        if (ret) {
                                return ret;
                        }
+
                        break;
                }
                case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT:
                {
-                       struct load_op *insn;
-                       uint32_t insn_len = sizeof(struct load_op);
-                       int ret;
+                       ret = bytecode_push_get_payload_root(&ctx->bytecode);
 
-                       insn = calloc(insn_len, 1);
-                       if (!insn)
-                               return -ENOMEM;
-                       insn->op = BYTECODE_OP_GET_PAYLOAD_ROOT;
-                       ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len);
-                       free(insn);
                        if (ret) {
                                return ret;
                        }
+
                        break;
                }
                case IR_LOAD_EXPRESSION_GET_SYMBOL:
                {
-                       struct load_op *insn;
-                       uint32_t insn_len = sizeof(struct load_op)
-                               + sizeof(struct get_symbol);
-                       struct get_symbol symbol_offset;
-                       uint32_t reloc_offset_u32;
-                       uint16_t reloc_offset;
-                       uint32_t bytecode_reloc_offset_u32;
-                       int ret;
+                       ret = bytecode_push_get_symbol(&ctx->bytecode,
+                                       &ctx->bytecode_reloc, op->u.symbol);
 
-                       insn = calloc(insn_len, 1);
-                       if (!insn)
-                               return -ENOMEM;
-                       insn->op = BYTECODE_OP_GET_SYMBOL;
-                       bytecode_reloc_offset_u32 =
-                                       bytecode_get_len(&ctx->bytecode_reloc->b)
-                                       + sizeof(reloc_offset);
-                       symbol_offset.offset =
-                                       (uint16_t) bytecode_reloc_offset_u32;
-                       memcpy(insn->data, &symbol_offset,
-                                       sizeof(symbol_offset));
-                       /* reloc_offset points to struct load_op */
-                       reloc_offset_u32 = bytecode_get_len(&ctx->bytecode->b);
-                       if (reloc_offset_u32 > LTTNG_FILTER_MAX_LEN - 1) {
-                               free(insn);
-                               return -EINVAL;
-                       }
-                       reloc_offset = (uint16_t) reloc_offset_u32;
-                       ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len);
-                       if (ret) {
-                               free(insn);
-                               return ret;
-                       }
-                       /* append reloc */
-                       ret = bytecode_push(&ctx->bytecode_reloc, &reloc_offset,
-                                       1, sizeof(reloc_offset));
-                       if (ret) {
-                               free(insn);
-                               return ret;
-                       }
-                       ret = bytecode_push(&ctx->bytecode_reloc,
-                                       op->u.symbol,
-                                       1, strlen(op->u.symbol) + 1);
-                       free(insn);
                        if (ret) {
                                return ret;
                        }
+
                        break;
                }
                case IR_LOAD_EXPRESSION_GET_INDEX:
                {
-                       struct load_op *insn;
-                       uint32_t insn_len = sizeof(struct load_op)
-                               + sizeof(struct get_index_u64);
-                       struct get_index_u64 index;
-                       int ret;
+                       ret = bytecode_push_get_index_u64(
+                                       &ctx->bytecode, op->u.index);
 
-                       insn = calloc(insn_len, 1);
-                       if (!insn)
-                               return -ENOMEM;
-                       insn->op = BYTECODE_OP_GET_INDEX_U64;
-                       index.index = op->u.index;
-                       memcpy(insn->data, &index, sizeof(index));
-                       ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len);
-                       free(insn);
                        if (ret) {
                                return ret;
                        }
+
                        break;
                }
                case IR_LOAD_EXPRESSION_LOAD_FIELD:
                {
                        struct load_op *insn;
                        uint32_t insn_len = sizeof(struct load_op);
-                       int ret;
 
                        insn = calloc(insn_len, 1);
                        if (!insn)
This page took 0.045783 seconds and 4 git commands to generate.