Fix: pass private data to context callbacks
[lttng-ust.git] / liblttng-ust / lttng-bytecode.c
index 47efdecafd25be983b0e4622f300cf0e6a9d3509..ea5827a447adffb671c879fdf6730e7ac601944f 100644 (file)
@@ -1,27 +1,9 @@
 /*
- * lttng-bytecode.c
- *
- * LTTng UST bytecode code.
+ * SPDX-License-Identifier: MIT
  *
  * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
+ * LTTng UST bytecode code.
  */
 
 #define _LGPL_SOURCE
 
 #include <urcu/rculist.h>
 
+#include "context-internal.h"
 #include "lttng-bytecode.h"
 #include "ust-events-internal.h"
+#include "ust-helper.h"
 
 static const char *opnames[] = {
        [ BYTECODE_OP_UNKNOWN ] = "UNKNOWN",
@@ -179,7 +163,7 @@ static const char *opnames[] = {
        [ BYTECODE_OP_RETURN_S64 ] = "RETURN_S64",
 };
 
-const char *print_op(enum bytecode_op op)
+const char *lttng_bytecode_print_op(enum bytecode_op op)
 {
        if (op >= NR_BYTECODE_OPS)
                return "UNKNOWN";
@@ -188,14 +172,14 @@ const char *print_op(enum bytecode_op op)
 }
 
 static
-int apply_field_reloc(const struct lttng_event_desc *event_desc,
+int apply_field_reloc(const struct lttng_ust_event_desc *event_desc,
                struct bytecode_runtime *runtime,
-               uint32_t runtime_len,
+               uint32_t runtime_len __attribute__((unused)),
                uint32_t reloc_offset,
                const char *field_name,
                enum bytecode_op bytecode_op)
 {
-       const struct lttng_event_field *fields, *field = NULL;
+       const struct lttng_ust_event_field **fields, *field = NULL;
        unsigned int nr_fields, i;
        struct load_op *op;
        uint32_t field_offset = 0;
@@ -210,31 +194,28 @@ int apply_field_reloc(const struct lttng_event_desc *event_desc,
                return -EINVAL;
        nr_fields = event_desc->nr_fields;
        for (i = 0; i < nr_fields; i++) {
-               if (fields[i].u.ext.nofilter) {
+               if (fields[i]->nofilter) {
                        continue;
                }
-               if (!strcmp(fields[i].name, field_name)) {
-                       field = &fields[i];
+               if (!strcmp(fields[i]->name, field_name)) {
+                       field = fields[i];
                        break;
                }
                /* compute field offset */
-               switch (fields[i].type.atype) {
-               case atype_integer:
-               case atype_enum:
-               case atype_enum_nestable:
+               switch (fields[i]->type->type) {
+               case lttng_ust_type_integer:
+               case lttng_ust_type_enum:
                        field_offset += sizeof(int64_t);
                        break;
-               case atype_array:
-               case atype_array_nestable:
-               case atype_sequence:
-               case atype_sequence_nestable:
+               case lttng_ust_type_array:
+               case lttng_ust_type_sequence:
                        field_offset += sizeof(unsigned long);
                        field_offset += sizeof(void *);
                        break;
-               case atype_string:
+               case lttng_ust_type_string:
                        field_offset += sizeof(void *);
                        break;
-               case atype_float:
+               case lttng_ust_type_float:
                        field_offset += sizeof(double);
                        break;
                default:
@@ -245,7 +226,7 @@ int apply_field_reloc(const struct lttng_event_desc *event_desc,
                return -EINVAL;
 
        /* Check if field offset is too large for 16-bit offset */
-       if (field_offset > FILTER_BYTECODE_MAX_LEN - 1)
+       if (field_offset > LTTNG_UST_ABI_FILTER_BYTECODE_MAX_LEN - 1)
                return -EINVAL;
 
        /* set type */
@@ -257,22 +238,33 @@ int apply_field_reloc(const struct lttng_event_desc *event_desc,
                struct field_ref *field_ref;
 
                field_ref = (struct field_ref *) op->data;
-               switch (field->type.atype) {
-               case atype_integer:
-               case atype_enum:
-               case atype_enum_nestable:
+               switch (field->type->type) {
+               case lttng_ust_type_integer:
+               case lttng_ust_type_enum:
                        op->op = BYTECODE_OP_LOAD_FIELD_REF_S64;
                        break;
-               case atype_array:
-               case atype_array_nestable:
-               case atype_sequence:
-               case atype_sequence_nestable:
+               case lttng_ust_type_array:
+               {
+                       struct lttng_ust_type_array *array = (struct lttng_ust_type_array *) field->type;
+
+                       if (array->encoding == lttng_ust_string_encoding_none)
+                               return -EINVAL;
+                       op->op = BYTECODE_OP_LOAD_FIELD_REF_SEQUENCE;
+                       break;
+               }
+               case lttng_ust_type_sequence:
+               {
+                       struct lttng_ust_type_sequence *sequence = (struct lttng_ust_type_sequence *) field->type;
+
+                       if (sequence->encoding == lttng_ust_string_encoding_none)
+                               return -EINVAL;
                        op->op = BYTECODE_OP_LOAD_FIELD_REF_SEQUENCE;
                        break;
-               case atype_string:
+               }
+               case lttng_ust_type_string:
                        op->op = BYTECODE_OP_LOAD_FIELD_REF_STRING;
                        break;
-               case atype_float:
+               case lttng_ust_type_float:
                        op->op = BYTECODE_OP_LOAD_FIELD_REF_DOUBLE;
                        break;
                default:
@@ -290,29 +282,29 @@ int apply_field_reloc(const struct lttng_event_desc *event_desc,
 
 static
 int apply_context_reloc(struct bytecode_runtime *runtime,
-               uint32_t runtime_len,
+               uint32_t runtime_len __attribute__((unused)),
                uint32_t reloc_offset,
                const char *context_name,
                enum bytecode_op bytecode_op)
 {
        struct load_op *op;
-       struct lttng_ctx_field *ctx_field;
+       const struct lttng_ust_ctx_field *ctx_field;
        int idx;
-       struct lttng_ctx *ctx = *runtime->p.pctx;
+       struct lttng_ust_ctx **pctx = runtime->p.pctx;
 
        dbg_printf("Apply context reloc: %u %s\n", reloc_offset, context_name);
 
        /* Get context index */
-       idx = lttng_get_context_index(ctx, context_name);
+       idx = lttng_get_context_index(*pctx, context_name);
        if (idx < 0) {
                if (lttng_context_is_app(context_name)) {
                        int ret;
 
                        ret = lttng_ust_add_app_context_to_ctx_rcu(context_name,
-                                       &ctx);
+                                       pctx);
                        if (ret)
                                return ret;
-                       idx = lttng_get_context_index(ctx, context_name);
+                       idx = lttng_get_context_index(*pctx, context_name);
                        if (idx < 0)
                                return -ENOENT;
                } else {
@@ -320,11 +312,11 @@ int apply_context_reloc(struct bytecode_runtime *runtime,
                }
        }
        /* Check if idx is too large for 16-bit offset */
-       if (idx > FILTER_BYTECODE_MAX_LEN - 1)
+       if (idx > LTTNG_UST_ABI_FILTER_BYTECODE_MAX_LEN - 1)
                return -EINVAL;
 
        /* Get context return type */
-       ctx_field = &ctx->fields[idx];
+       ctx_field = &(*pctx)->fields[idx];
        op = (struct load_op *) &runtime->code[reloc_offset];
 
        switch (bytecode_op) {
@@ -333,24 +325,37 @@ int apply_context_reloc(struct bytecode_runtime *runtime,
                struct field_ref *field_ref;
 
                field_ref = (struct field_ref *) op->data;
-               switch (ctx_field->event_field.type.atype) {
-               case atype_integer:
-               case atype_enum:
-               case atype_enum_nestable:
+               switch (ctx_field->event_field->type->type) {
+               case lttng_ust_type_integer:
+               case lttng_ust_type_enum:
                        op->op = BYTECODE_OP_GET_CONTEXT_REF_S64;
                        break;
-                       /* Sequence and array supported as string */
-               case atype_string:
-               case atype_array:
-               case atype_array_nestable:
-               case atype_sequence:
-               case atype_sequence_nestable:
+                       /* Sequence and array supported only as string */
+               case lttng_ust_type_array:
+               {
+                       struct lttng_ust_type_array *array = (struct lttng_ust_type_array *) ctx_field->event_field->type;
+
+                       if (array->encoding == lttng_ust_string_encoding_none)
+                               return -EINVAL;
+                       op->op = BYTECODE_OP_GET_CONTEXT_REF_STRING;
+                       break;
+               }
+               case lttng_ust_type_sequence:
+               {
+                       struct lttng_ust_type_sequence *sequence = (struct lttng_ust_type_sequence *) ctx_field->event_field->type;
+
+                       if (sequence->encoding == lttng_ust_string_encoding_none)
+                               return -EINVAL;
+                       op->op = BYTECODE_OP_GET_CONTEXT_REF_STRING;
+                       break;
+               }
+               case lttng_ust_type_string:
                        op->op = BYTECODE_OP_GET_CONTEXT_REF_STRING;
                        break;
-               case atype_float:
+               case lttng_ust_type_float:
                        op->op = BYTECODE_OP_GET_CONTEXT_REF_DOUBLE;
                        break;
-               case atype_dynamic:
+               case lttng_ust_type_dynamic:
                        op->op = BYTECODE_OP_GET_CONTEXT_REF;
                        break;
                default:
@@ -367,7 +372,7 @@ int apply_context_reloc(struct bytecode_runtime *runtime,
 }
 
 static
-int apply_reloc(const struct lttng_event_desc *event_desc,
+int apply_reloc(const struct lttng_ust_event_desc *event_desc,
                struct bytecode_runtime *runtime,
                uint32_t runtime_len,
                uint32_t reloc_offset,
@@ -407,7 +412,7 @@ static
 int bytecode_is_linked(struct lttng_ust_bytecode_node *bytecode,
                struct cds_list_head *bytecode_runtime_head)
 {
-       struct lttng_bytecode_runtime *bc_runtime;
+       struct lttng_ust_bytecode_runtime *bc_runtime;
 
        cds_list_for_each_entry(bc_runtime, bytecode_runtime_head, node) {
                if (bc_runtime->bc == bytecode)
@@ -421,9 +426,10 @@ int bytecode_is_linked(struct lttng_ust_bytecode_node *bytecode,
  * bytecode runtime.
  */
 static
-int _lttng_filter_link_bytecode(const struct lttng_event_desc *event_desc,
-               struct lttng_ctx **ctx,
+int link_bytecode(const struct lttng_ust_event_desc *event_desc,
+               struct lttng_ust_ctx **ctx,
                struct lttng_ust_bytecode_node *bytecode,
+               struct cds_list_head *bytecode_runtime_head,
                struct cds_list_head *insert_loc)
 {
        int ret, offset, next_offset;
@@ -433,7 +439,7 @@ int _lttng_filter_link_bytecode(const struct lttng_event_desc *event_desc,
        if (!bytecode)
                return 0;
        /* Bytecode already linked */
-       if (bytecode_is_linked(bytecode, insert_loc))
+       if (bytecode_is_linked(bytecode, bytecode_runtime_head))
                return 0;
 
        dbg_printf("Linking...\n");
@@ -445,6 +451,7 @@ int _lttng_filter_link_bytecode(const struct lttng_event_desc *event_desc,
                ret = -ENOMEM;
                goto alloc_error;
        }
+       runtime->p.type = bytecode->type;
        runtime->p.bc = bytecode;
        runtime->p.pctx = ctx;
        runtime->len = bytecode->bc.reloc_offset;
@@ -478,14 +485,15 @@ int _lttng_filter_link_bytecode(const struct lttng_event_desc *event_desc,
        if (ret) {
                goto link_error;
        }
-       runtime->p.filter = lttng_bytecode_filter_interpret;
+
+       runtime->p.interpreter_func = lttng_bytecode_interpret;
        runtime->p.link_failed = 0;
        cds_list_add_rcu(&runtime->p.node, insert_loc);
        dbg_printf("Linking successful.\n");
        return 0;
 
 link_error:
-       runtime->p.filter = lttng_bytecode_filter_interpret_false;
+       runtime->p.interpreter_func = lttng_bytecode_interpret_error;
        runtime->p.link_failed = 1;
        cds_list_add_rcu(&runtime->p.node, insert_loc);
 alloc_error:
@@ -493,42 +501,54 @@ alloc_error:
        return ret;
 }
 
-void lttng_bytecode_filter_sync_state(struct lttng_bytecode_runtime *runtime)
+void lttng_bytecode_sync_state(struct lttng_ust_bytecode_runtime *runtime)
 {
        struct lttng_ust_bytecode_node *bc = runtime->bc;
 
        if (!bc->enabler->enabled || runtime->link_failed)
-               runtime->filter = lttng_bytecode_filter_interpret_false;
+               runtime->interpreter_func = lttng_bytecode_interpret_error;
        else
-               runtime->filter = lttng_bytecode_filter_interpret;
+               runtime->interpreter_func = lttng_bytecode_interpret;
 }
 
 /*
- * Link all bytecodes of the enabler referenced in the provided bytecode list.
+ * Given the lists of bytecode programs of an instance (trigger or event) and
+ * of a matching enabler, try to link all the enabler's bytecode programs with
+ * the instance.
+ *
+ * This function is called after we confirmed that name enabler and the
+ * instance are name matching (or glob pattern matching).
  */
-void lttng_enabler_link_bytecode(const struct lttng_event_desc *event_desc,
-               struct lttng_ctx **ctx,
-               struct cds_list_head *bytecode_runtime_head,
-               struct lttng_enabler *enabler)
+void lttng_enabler_link_bytecode(const struct lttng_ust_event_desc *event_desc,
+               struct lttng_ust_ctx **ctx,
+               struct cds_list_head *instance_bytecode_head,
+               struct cds_list_head *enabler_bytecode_head)
 {
-       struct lttng_ust_bytecode_node *bc;
-       struct lttng_bytecode_runtime *runtime;
+       struct lttng_ust_bytecode_node *enabler_bc;
+       struct lttng_ust_bytecode_runtime *runtime;
 
        assert(event_desc);
 
-       /* Link each bytecode. */
-       cds_list_for_each_entry(bc, &enabler->filter_bytecode_head, node) {
+       /* Go over all the bytecode programs of the enabler. */
+       cds_list_for_each_entry(enabler_bc, enabler_bytecode_head, node) {
                int found = 0, ret;
                struct cds_list_head *insert_loc;
 
-               cds_list_for_each_entry(runtime,
-                               bytecode_runtime_head, node) {
-                       if (runtime->bc == bc) {
+               /*
+                * Check if the current enabler bytecode program is already
+                * linked with the instance.
+                */
+               cds_list_for_each_entry(runtime, instance_bytecode_head, node) {
+                       if (runtime->bc == enabler_bc) {
                                found = 1;
                                break;
                        }
                }
-               /* Skip bytecode already linked */
+
+               /*
+                * Skip bytecode already linked, go to the next enabler
+                * bytecode program.
+                */
                if (found)
                        continue;
 
@@ -538,8 +558,8 @@ void lttng_enabler_link_bytecode(const struct lttng_event_desc *event_desc,
                 * insert the new bytecode right after it.
                 */
                cds_list_for_each_entry_reverse(runtime,
-                               bytecode_runtime_head, node) {
-                       if (runtime->bc->bc.seqnum <= bc->bc.seqnum) {
+                               instance_bytecode_head, node) {
+                       if (runtime->bc->bc.seqnum <= enabler_bc->bc.seqnum) {
                                /* insert here */
                                insert_loc = &runtime->node;
                                goto add_within;
@@ -547,27 +567,16 @@ void lttng_enabler_link_bytecode(const struct lttng_event_desc *event_desc,
                }
 
                /* Add to head to list */
-               insert_loc = bytecode_runtime_head;
+               insert_loc = instance_bytecode_head;
        add_within:
                dbg_printf("linking bytecode\n");
-               ret = _lttng_filter_link_bytecode(event_desc, ctx, bc,
-                       insert_loc);
+               ret = link_bytecode(event_desc, ctx, enabler_bc, instance_bytecode_head, insert_loc);
                if (ret) {
                        dbg_printf("[lttng filter] warning: cannot link event bytecode\n");
                }
        }
 }
 
-/*
- * We own the bytecode if we return success.
- */
-int lttng_filter_enabler_attach_bytecode(struct lttng_enabler *enabler,
-               struct lttng_ust_bytecode_node *bytecode)
-{
-       cds_list_add(&bytecode->node, &enabler->filter_bytecode_head);
-       return 0;
-}
-
 static
 void free_filter_runtime(struct cds_list_head *bytecode_runtime_head)
 {
@@ -580,18 +589,7 @@ void free_filter_runtime(struct cds_list_head *bytecode_runtime_head)
        }
 }
 
-void lttng_free_event_filter_runtime(struct lttng_event *event)
-{
-       free_filter_runtime(&event->filter_bytecode_runtime_head);
-}
-
-void lttng_free_event_notifier_filter_runtime(
-               struct lttng_event_notifier *event_notifier)
-{
-       free_filter_runtime(&event_notifier->filter_bytecode_runtime_head);
-}
-
-/* For backward compatibility. Leave those exported symbols in place. */
-void lttng_filter_sync_state(struct lttng_bytecode_runtime *runtime)
+void lttng_free_event_filter_runtime(struct lttng_ust_event_common *event)
 {
+       free_filter_runtime(&event->priv->filter_bytecode_runtime_head);
 }
This page took 0.029065 seconds and 4 git commands to generate.