common: move bytecode utilities from filter to its own file
authorSimon Marchi <simon.marchi@efficios.com>
Fri, 3 Apr 2020 17:50:57 +0000 (13:50 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 9 Mar 2021 04:50:57 +0000 (23:50 -0500)
We'll want to re-use the filter bytecode to implement the trigger event
rule condition field captures.  This is a preparatory patch that moves
some filter bytecode code in a location that is not filter-specific, so
it can be used for both filters and captures.

The content of common/filter/filter-bytecode.h is moved to
common/bytecode/bytecode.h.  Some declarations for the various bytecode
helpers are added to that file.  The implementation for these helpers is
moved from common/filter/filter-visitor-generate-bytecode.c to
common/bytecode/bytecode.c.

The content of src/common/bytecode is built as a library, so it can be
used by the filter-grammar-test program.

A following patch renames the content of bytecode/bytecode.h to remove
the "filter" part.

The rest of the changes is just to adapt the code to the changes
mentioned above.

Change-Id: Id602c9046bdc76791026c5b5a928387145d18e43
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
Depends-on: lttng-ust: I5a800fc92e588c2a6a0e26282b0ad5f31c044479

13 files changed:
configure.ac
src/common/Makefile.am
src/common/bytecode/Makefile.am [new file with mode: 0644]
src/common/bytecode/bytecode.c [new file with mode: 0644]
src/common/bytecode/bytecode.h [new file with mode: 0644]
src/common/filter/Makefile.am
src/common/filter/filter-ast.h
src/common/filter/filter-bytecode.h [deleted file]
src/common/filter/filter-grammar-test.c
src/common/filter/filter-parser.y
src/common/filter/filter-visitor-generate-bytecode.c
src/common/runas.c
src/lib/lttng-ctl/lttng-ctl.c

index 954f74d99814a020cd0d764441c5046c3d05fea5..58d4f9292369c4ed9dee90c3b9edbdf9dbefba88 100644 (file)
@@ -1105,6 +1105,7 @@ AC_CONFIG_FILES([
        src/Makefile
        src/common/Makefile
        src/common/argpar/Makefile
+       src/common/bytecode/Makefile
        src/common/kernel-ctl/Makefile
        src/common/kernel-consumer/Makefile
        src/common/consumer/Makefile
index 8a74b728249afe4b5c22d2336a83eb8bc37906da..cfb935c5ad2b2dc6d43c088478f95fdbc8002f43 100644 (file)
@@ -4,6 +4,7 @@ AUTOMAKE_OPTIONS = subdir-objects
 
 SUBDIRS = \
        string-utils \
+       bytecode \
        filter \
        argpar
 
@@ -24,6 +25,7 @@ DIST_SUBDIRS = \
        consumer \
        string-utils \
        fd-tracker \
+       bytecode \
        filter \
        argpar
 
@@ -98,6 +100,7 @@ libcommon_la_SOURCES += \
 endif
 
 libcommon_la_LIBADD = \
+       $(top_builddir)/src/common/bytecode/libbytecode.la \
        $(top_builddir)/src/common/config/libconfig.la \
        $(top_builddir)/src/common/compat/libcompat.la \
        $(top_builddir)/src/common/hashtable/libhashtable.la \
diff --git a/src/common/bytecode/Makefile.am b/src/common/bytecode/Makefile.am
new file mode 100644 (file)
index 0000000..9e1c6dd
--- /dev/null
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0-only
+
+noinst_LTLIBRARIES = libbytecode.la
+
+libbytecode_la_SOURCES = \
+       bytecode.c bytecode.h
diff --git a/src/common/bytecode/bytecode.c b/src/common/bytecode/bytecode.c
new file mode 100644 (file)
index 0000000..b8edd69
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2020 EfficiOS, Inc.
+ *
+ * SPDX-License-Identifier: GPL-2.0-only
+ *
+ */
+
+#include "bytecode.h"
+
+#include <errno.h>
+
+#include "common/align.h"
+
+#define INIT_ALLOC_SIZE 4
+
+static inline int get_count_order(unsigned int count)
+{
+       int order;
+
+       order = lttng_fls(count) - 1;
+       if (count & (count - 1))
+               order++;
+       return order;
+}
+
+LTTNG_HIDDEN
+int bytecode_init(struct lttng_filter_bytecode_alloc **fb)
+{
+       uint32_t alloc_len;
+
+       alloc_len = sizeof(struct lttng_filter_bytecode_alloc) + INIT_ALLOC_SIZE;
+       *fb = calloc(alloc_len, 1);
+       if (!*fb) {
+               return -ENOMEM;
+       } else {
+               (*fb)->alloc_len = alloc_len;
+               return 0;
+       }
+}
+
+LTTNG_HIDDEN
+int32_t bytecode_reserve(struct lttng_filter_bytecode_alloc **fb, uint32_t align, uint32_t len)
+{
+       int32_t ret;
+       uint32_t padding = offset_align((*fb)->b.len, align);
+       uint32_t new_len = (*fb)->b.len + padding + len;
+       uint32_t new_alloc_len = sizeof(struct lttng_filter_bytecode_alloc) + new_len;
+       uint32_t old_alloc_len = (*fb)->alloc_len;
+
+       if (new_len > LTTNG_FILTER_MAX_LEN)
+               return -EINVAL;
+
+       if (new_alloc_len > old_alloc_len) {
+               struct lttng_filter_bytecode_alloc *newptr;
+
+               new_alloc_len =
+                       max_t(uint32_t, 1U << get_count_order(new_alloc_len), old_alloc_len << 1);
+               newptr = realloc(*fb, new_alloc_len);
+               if (!newptr)
+                       return -ENOMEM;
+               *fb = newptr;
+               /* We zero directly the memory from start of allocation. */
+               memset(&((char *) *fb)[old_alloc_len], 0, new_alloc_len - old_alloc_len);
+               (*fb)->alloc_len = new_alloc_len;
+       }
+       (*fb)->b.len += padding;
+       ret = (*fb)->b.len;
+       (*fb)->b.len += len;
+       return ret;
+}
+
+LTTNG_HIDDEN
+int bytecode_push(struct lttng_filter_bytecode_alloc **fb, const void *data,
+               uint32_t align, uint32_t len)
+{
+       int32_t offset;
+
+       offset = bytecode_reserve(fb, align, len);
+       if (offset < 0)
+               return offset;
+       memcpy(&(*fb)->b.data[offset], data, len);
+       return 0;
+}
+
+LTTNG_HIDDEN
+int bytecode_push_logical(struct lttng_filter_bytecode_alloc **fb,
+               struct logical_op *data,
+               uint32_t align, uint32_t len,
+               uint16_t *skip_offset)
+{
+       int32_t offset;
+
+       offset = bytecode_reserve(fb, align, len);
+       if (offset < 0)
+               return offset;
+       memcpy(&(*fb)->b.data[offset], data, len);
+       *skip_offset =
+               (void *) &((struct logical_op *) &(*fb)->b.data[offset])->skip_offset
+                       - (void *) &(*fb)->b.data[0];
+       return 0;
+}
diff --git a/src/common/bytecode/bytecode.h b/src/common/bytecode/bytecode.h
new file mode 100644 (file)
index 0000000..ecf54f9
--- /dev/null
@@ -0,0 +1,248 @@
+/*
+ * Copyright 2020 EfficiOS, Inc.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ */
+
+#ifndef LTTNG_COMMON_BYTECODE_H
+#define LTTNG_COMMON_BYTECODE_H
+
+#include <stdint.h>
+
+#include "common/macros.h"
+#include "common/sessiond-comm/sessiond-comm.h"
+
+/*
+ * offsets are absolute from start of bytecode.
+ */
+
+struct field_ref {
+       /* Initially, symbol offset. After link, field offset. */
+       uint16_t offset;
+} LTTNG_PACKED;
+
+struct get_symbol {
+       /* Symbol offset. */
+       uint16_t offset;
+} LTTNG_PACKED;
+
+struct get_index_u16 {
+       uint16_t index;
+} LTTNG_PACKED;
+
+struct get_index_u64 {
+       uint64_t index;
+} LTTNG_PACKED;
+
+struct literal_numeric {
+       int64_t v;
+} LTTNG_PACKED;
+
+struct literal_double {
+       double v;
+} LTTNG_PACKED;
+
+struct literal_string {
+       char string[0];
+} LTTNG_PACKED;
+
+enum filter_op {
+       FILTER_OP_UNKNOWN                       = 0,
+
+       FILTER_OP_RETURN                        = 1,
+
+       /* binary */
+       FILTER_OP_MUL                           = 2,
+       FILTER_OP_DIV                           = 3,
+       FILTER_OP_MOD                           = 4,
+       FILTER_OP_PLUS                          = 5,
+       FILTER_OP_MINUS                         = 6,
+       FILTER_OP_BIT_RSHIFT                    = 7,
+       FILTER_OP_BIT_LSHIFT                    = 8,
+       FILTER_OP_BIT_AND                       = 9,
+       FILTER_OP_BIT_OR                        = 10,
+       FILTER_OP_BIT_XOR                       = 11,
+
+       /* binary comparators */
+       FILTER_OP_EQ                            = 12,
+       FILTER_OP_NE                            = 13,
+       FILTER_OP_GT                            = 14,
+       FILTER_OP_LT                            = 15,
+       FILTER_OP_GE                            = 16,
+       FILTER_OP_LE                            = 17,
+
+       /* string binary comparator: apply to  */
+       FILTER_OP_EQ_STRING                     = 18,
+       FILTER_OP_NE_STRING                     = 19,
+       FILTER_OP_GT_STRING                     = 20,
+       FILTER_OP_LT_STRING                     = 21,
+       FILTER_OP_GE_STRING                     = 22,
+       FILTER_OP_LE_STRING                     = 23,
+
+       /* s64 binary comparator */
+       FILTER_OP_EQ_S64                        = 24,
+       FILTER_OP_NE_S64                        = 25,
+       FILTER_OP_GT_S64                        = 26,
+       FILTER_OP_LT_S64                        = 27,
+       FILTER_OP_GE_S64                        = 28,
+       FILTER_OP_LE_S64                        = 29,
+
+       /* double binary comparator */
+       FILTER_OP_EQ_DOUBLE                     = 30,
+       FILTER_OP_NE_DOUBLE                     = 31,
+       FILTER_OP_GT_DOUBLE                     = 32,
+       FILTER_OP_LT_DOUBLE                     = 33,
+       FILTER_OP_GE_DOUBLE                     = 34,
+       FILTER_OP_LE_DOUBLE                     = 35,
+
+       /* Mixed S64-double binary comparators */
+       FILTER_OP_EQ_DOUBLE_S64                 = 36,
+       FILTER_OP_NE_DOUBLE_S64                 = 37,
+       FILTER_OP_GT_DOUBLE_S64                 = 38,
+       FILTER_OP_LT_DOUBLE_S64                 = 39,
+       FILTER_OP_GE_DOUBLE_S64                 = 40,
+       FILTER_OP_LE_DOUBLE_S64                 = 41,
+
+       FILTER_OP_EQ_S64_DOUBLE                 = 42,
+       FILTER_OP_NE_S64_DOUBLE                 = 43,
+       FILTER_OP_GT_S64_DOUBLE                 = 44,
+       FILTER_OP_LT_S64_DOUBLE                 = 45,
+       FILTER_OP_GE_S64_DOUBLE                 = 46,
+       FILTER_OP_LE_S64_DOUBLE                 = 47,
+
+       /* unary */
+       FILTER_OP_UNARY_PLUS                    = 48,
+       FILTER_OP_UNARY_MINUS                   = 49,
+       FILTER_OP_UNARY_NOT                     = 50,
+       FILTER_OP_UNARY_PLUS_S64                = 51,
+       FILTER_OP_UNARY_MINUS_S64               = 52,
+       FILTER_OP_UNARY_NOT_S64                 = 53,
+       FILTER_OP_UNARY_PLUS_DOUBLE             = 54,
+       FILTER_OP_UNARY_MINUS_DOUBLE            = 55,
+       FILTER_OP_UNARY_NOT_DOUBLE              = 56,
+
+       /* logical */
+       FILTER_OP_AND                           = 57,
+       FILTER_OP_OR                            = 58,
+
+       /* load field ref */
+       FILTER_OP_LOAD_FIELD_REF                = 59,
+       FILTER_OP_LOAD_FIELD_REF_STRING         = 60,
+       FILTER_OP_LOAD_FIELD_REF_SEQUENCE       = 61,
+       FILTER_OP_LOAD_FIELD_REF_S64            = 62,
+       FILTER_OP_LOAD_FIELD_REF_DOUBLE         = 63,
+
+       /* load immediate from operand */
+       FILTER_OP_LOAD_STRING                   = 64,
+       FILTER_OP_LOAD_S64                      = 65,
+       FILTER_OP_LOAD_DOUBLE                   = 66,
+
+       /* cast */
+       FILTER_OP_CAST_TO_S64                   = 67,
+       FILTER_OP_CAST_DOUBLE_TO_S64            = 68,
+       FILTER_OP_CAST_NOP                      = 69,
+
+       /* get context ref */
+       FILTER_OP_GET_CONTEXT_REF               = 70,
+       FILTER_OP_GET_CONTEXT_REF_STRING        = 71,
+       FILTER_OP_GET_CONTEXT_REF_S64           = 72,
+       FILTER_OP_GET_CONTEXT_REF_DOUBLE        = 73,
+
+       /* load userspace field ref */
+       FILTER_OP_LOAD_FIELD_REF_USER_STRING    = 74,
+       FILTER_OP_LOAD_FIELD_REF_USER_SEQUENCE  = 75,
+
+       /*
+        * load immediate star globbing pattern (literal string)
+        * from immediate
+        */
+       FILTER_OP_LOAD_STAR_GLOB_STRING         = 76,
+
+       /* globbing pattern binary operator: apply to */
+       FILTER_OP_EQ_STAR_GLOB_STRING           = 77,
+       FILTER_OP_NE_STAR_GLOB_STRING           = 78,
+
+       /*
+        * Instructions for recursive traversal through composed types.
+        */
+       FILTER_OP_GET_CONTEXT_ROOT              = 79,
+       FILTER_OP_GET_APP_CONTEXT_ROOT          = 80,
+       FILTER_OP_GET_PAYLOAD_ROOT              = 81,
+
+       FILTER_OP_GET_SYMBOL                    = 82,
+       FILTER_OP_GET_SYMBOL_FIELD              = 83,
+       FILTER_OP_GET_INDEX_U16                 = 84,
+       FILTER_OP_GET_INDEX_U64                 = 85,
+
+       FILTER_OP_LOAD_FIELD                    = 86,
+       FILTER_OP_LOAD_FIELD_S8                 = 87,
+       FILTER_OP_LOAD_FIELD_S16                = 88,
+       FILTER_OP_LOAD_FIELD_S32                = 89,
+       FILTER_OP_LOAD_FIELD_S64                = 90,
+       FILTER_OP_LOAD_FIELD_U8                 = 91,
+       FILTER_OP_LOAD_FIELD_U16                = 92,
+       FILTER_OP_LOAD_FIELD_U32                = 93,
+       FILTER_OP_LOAD_FIELD_U64                = 94,
+       FILTER_OP_LOAD_FIELD_STRING             = 95,
+       FILTER_OP_LOAD_FIELD_SEQUENCE           = 96,
+       FILTER_OP_LOAD_FIELD_DOUBLE             = 97,
+
+       FILTER_OP_UNARY_BIT_NOT                 = 98,
+
+       FILTER_OP_RETURN_S64                    = 99,
+
+       NR_FILTER_OPS,
+};
+
+typedef uint8_t filter_opcode_t;
+
+struct load_op {
+       filter_opcode_t op;
+       char data[0];
+       /* data to load. Size known by enum filter_opcode and null-term char. */
+} LTTNG_PACKED;
+
+struct binary_op {
+       filter_opcode_t op;
+} LTTNG_PACKED;
+
+struct unary_op {
+       filter_opcode_t op;
+} LTTNG_PACKED;
+
+/* skip_offset is absolute from start of bytecode */
+struct logical_op {
+       filter_opcode_t op;
+       uint16_t skip_offset;   /* bytecode insn, if skip second test */
+} LTTNG_PACKED;
+
+struct cast_op {
+       filter_opcode_t op;
+} LTTNG_PACKED;
+
+struct return_op {
+       filter_opcode_t op;
+} LTTNG_PACKED;
+
+struct lttng_filter_bytecode_alloc {
+       uint32_t alloc_len;
+       struct lttng_filter_bytecode b;
+};
+
+LTTNG_HIDDEN int bytecode_init(struct lttng_filter_bytecode_alloc **fb);
+LTTNG_HIDDEN int32_t bytecode_reserve(struct lttng_filter_bytecode_alloc **fb,
+               uint32_t align, uint32_t len);
+LTTNG_HIDDEN int bytecode_push(struct lttng_filter_bytecode_alloc **fb,
+               const void *data, uint32_t align, uint32_t len);
+LTTNG_HIDDEN int bytecode_push_logical(struct lttng_filter_bytecode_alloc **fb,
+               struct logical_op *data, uint32_t align, uint32_t len,
+               uint16_t *skip_offset);
+
+static inline
+unsigned int bytecode_get_len(struct lttng_filter_bytecode *bytecode)
+{
+       return bytecode->len;
+}
+
+#endif /* LTTNG_COMMON_BYTECODE_H */
index cac4cb17575ed820a3031fa1c47c0db59932286f..261acc0861740b82297d342f9183fc4a4446803b 100644 (file)
@@ -19,7 +19,6 @@ libfilter_la_SOURCES = \
        filter-visitor-ir-normalize-glob-patterns.c \
        filter-visitor-generate-bytecode.c \
        filter-ast.h \
-       filter-bytecode.h \
        filter-ir.h \
        memstream.h
 libfilter_la_CFLAGS = -include filter-symbols.h $(AM_CFLAGS)
@@ -63,4 +62,6 @@ all-local: filter-lexer.c
 endif # HAVE_FLEX
 
 filter_grammar_test_SOURCES = filter-grammar-test.c
-filter_grammar_test_LDADD = libfilter.la
+filter_grammar_test_LDADD = \
+       libfilter.la \
+       ../bytecode/libbytecode.la
index 29fde10f8c126f7cd430b9a8f498987386801d03..a82e4f6bfd2cb77f21efdc13e045972029e3f157 100644 (file)
@@ -17,6 +17,7 @@
  */
 
 #include <urcu/list.h>
+#include <stdio.h>
 #include <stdint.h>
 
 #define printf_debug(fmt, args...)                                     \
diff --git a/src/common/filter/filter-bytecode.h b/src/common/filter/filter-bytecode.h
deleted file mode 100644 (file)
index 053bb08..0000000
+++ /dev/null
@@ -1,243 +0,0 @@
-#ifndef _FILTER_BYTECODE_H
-#define _FILTER_BYTECODE_H
-
-/*
- * filter-bytecode.h
- *
- * LTTng filter bytecode
- *
- * Copyright 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- *
- * SPDX-License-Identifier: LGPL-2.1-only
- *
- */
-
-#include <common/sessiond-comm/sessiond-comm.h>
-#include <common/macros.h>
-
-#include "filter-ast.h"
-
-/*
- * offsets are absolute from start of bytecode.
- */
-
-struct field_ref {
-       /* Initially, symbol offset. After link, field offset. */
-       uint16_t offset;
-} LTTNG_PACKED;
-
-struct get_symbol {
-       /* Symbol offset. */
-       uint16_t offset;
-} LTTNG_PACKED;
-
-struct get_index_u16 {
-       uint16_t index;
-} LTTNG_PACKED;
-
-struct get_index_u64 {
-       uint64_t index;
-} LTTNG_PACKED;
-
-struct literal_numeric {
-       int64_t v;
-} LTTNG_PACKED;
-
-struct literal_double {
-       double v;
-} LTTNG_PACKED;
-
-struct literal_string {
-       char string[0];
-} LTTNG_PACKED;
-
-enum filter_op {
-       FILTER_OP_UNKNOWN                       = 0,
-
-       FILTER_OP_RETURN                        = 1,
-
-       /* binary */
-       FILTER_OP_MUL                           = 2,
-       FILTER_OP_DIV                           = 3,
-       FILTER_OP_MOD                           = 4,
-       FILTER_OP_PLUS                          = 5,
-       FILTER_OP_MINUS                         = 6,
-       FILTER_OP_BIT_RSHIFT                    = 7,
-       FILTER_OP_BIT_LSHIFT                    = 8,
-       FILTER_OP_BIT_AND                       = 9,
-       FILTER_OP_BIT_OR                        = 10,
-       FILTER_OP_BIT_XOR                       = 11,
-
-       /* binary comparators */
-       FILTER_OP_EQ                            = 12,
-       FILTER_OP_NE                            = 13,
-       FILTER_OP_GT                            = 14,
-       FILTER_OP_LT                            = 15,
-       FILTER_OP_GE                            = 16,
-       FILTER_OP_LE                            = 17,
-
-       /* string binary comparator: apply to  */
-       FILTER_OP_EQ_STRING                     = 18,
-       FILTER_OP_NE_STRING                     = 19,
-       FILTER_OP_GT_STRING                     = 20,
-       FILTER_OP_LT_STRING                     = 21,
-       FILTER_OP_GE_STRING                     = 22,
-       FILTER_OP_LE_STRING                     = 23,
-
-       /* s64 binary comparator */
-       FILTER_OP_EQ_S64                        = 24,
-       FILTER_OP_NE_S64                        = 25,
-       FILTER_OP_GT_S64                        = 26,
-       FILTER_OP_LT_S64                        = 27,
-       FILTER_OP_GE_S64                        = 28,
-       FILTER_OP_LE_S64                        = 29,
-
-       /* double binary comparator */
-       FILTER_OP_EQ_DOUBLE                     = 30,
-       FILTER_OP_NE_DOUBLE                     = 31,
-       FILTER_OP_GT_DOUBLE                     = 32,
-       FILTER_OP_LT_DOUBLE                     = 33,
-       FILTER_OP_GE_DOUBLE                     = 34,
-       FILTER_OP_LE_DOUBLE                     = 35,
-
-       /* Mixed S64-double binary comparators */
-       FILTER_OP_EQ_DOUBLE_S64                 = 36,
-       FILTER_OP_NE_DOUBLE_S64                 = 37,
-       FILTER_OP_GT_DOUBLE_S64                 = 38,
-       FILTER_OP_LT_DOUBLE_S64                 = 39,
-       FILTER_OP_GE_DOUBLE_S64                 = 40,
-       FILTER_OP_LE_DOUBLE_S64                 = 41,
-
-       FILTER_OP_EQ_S64_DOUBLE                 = 42,
-       FILTER_OP_NE_S64_DOUBLE                 = 43,
-       FILTER_OP_GT_S64_DOUBLE                 = 44,
-       FILTER_OP_LT_S64_DOUBLE                 = 45,
-       FILTER_OP_GE_S64_DOUBLE                 = 46,
-       FILTER_OP_LE_S64_DOUBLE                 = 47,
-
-       /* unary */
-       FILTER_OP_UNARY_PLUS                    = 48,
-       FILTER_OP_UNARY_MINUS                   = 49,
-       FILTER_OP_UNARY_NOT                     = 50,
-       FILTER_OP_UNARY_PLUS_S64                = 51,
-       FILTER_OP_UNARY_MINUS_S64               = 52,
-       FILTER_OP_UNARY_NOT_S64                 = 53,
-       FILTER_OP_UNARY_PLUS_DOUBLE             = 54,
-       FILTER_OP_UNARY_MINUS_DOUBLE            = 55,
-       FILTER_OP_UNARY_NOT_DOUBLE              = 56,
-
-       /* logical */
-       FILTER_OP_AND                           = 57,
-       FILTER_OP_OR                            = 58,
-
-       /* load field ref */
-       FILTER_OP_LOAD_FIELD_REF                = 59,
-       FILTER_OP_LOAD_FIELD_REF_STRING         = 60,
-       FILTER_OP_LOAD_FIELD_REF_SEQUENCE       = 61,
-       FILTER_OP_LOAD_FIELD_REF_S64            = 62,
-       FILTER_OP_LOAD_FIELD_REF_DOUBLE         = 63,
-
-       /* load immediate from operand */
-       FILTER_OP_LOAD_STRING                   = 64,
-       FILTER_OP_LOAD_S64                      = 65,
-       FILTER_OP_LOAD_DOUBLE                   = 66,
-
-       /* cast */
-       FILTER_OP_CAST_TO_S64                   = 67,
-       FILTER_OP_CAST_DOUBLE_TO_S64            = 68,
-       FILTER_OP_CAST_NOP                      = 69,
-
-       /* get context ref */
-       FILTER_OP_GET_CONTEXT_REF               = 70,
-       FILTER_OP_GET_CONTEXT_REF_STRING        = 71,
-       FILTER_OP_GET_CONTEXT_REF_S64           = 72,
-       FILTER_OP_GET_CONTEXT_REF_DOUBLE        = 73,
-
-       /* load userspace field ref */
-       FILTER_OP_LOAD_FIELD_REF_USER_STRING    = 74,
-       FILTER_OP_LOAD_FIELD_REF_USER_SEQUENCE  = 75,
-
-       /*
-        * load immediate star globbing pattern (literal string)
-        * from immediate
-        */
-       FILTER_OP_LOAD_STAR_GLOB_STRING         = 76,
-
-       /* globbing pattern binary operator: apply to */
-       FILTER_OP_EQ_STAR_GLOB_STRING           = 77,
-       FILTER_OP_NE_STAR_GLOB_STRING           = 78,
-
-       /*
-        * Instructions for recursive traversal through composed types.
-        */
-       FILTER_OP_GET_CONTEXT_ROOT              = 79,
-       FILTER_OP_GET_APP_CONTEXT_ROOT          = 80,
-       FILTER_OP_GET_PAYLOAD_ROOT              = 81,
-
-       FILTER_OP_GET_SYMBOL                    = 82,
-       FILTER_OP_GET_SYMBOL_FIELD              = 83,
-       FILTER_OP_GET_INDEX_U16                 = 84,
-       FILTER_OP_GET_INDEX_U64                 = 85,
-
-       FILTER_OP_LOAD_FIELD                    = 86,
-       FILTER_OP_LOAD_FIELD_S8                 = 87,
-       FILTER_OP_LOAD_FIELD_S16                = 88,
-       FILTER_OP_LOAD_FIELD_S32                = 89,
-       FILTER_OP_LOAD_FIELD_S64                = 90,
-       FILTER_OP_LOAD_FIELD_U8                 = 91,
-       FILTER_OP_LOAD_FIELD_U16                = 92,
-       FILTER_OP_LOAD_FIELD_U32                = 93,
-       FILTER_OP_LOAD_FIELD_U64                = 94,
-       FILTER_OP_LOAD_FIELD_STRING             = 95,
-       FILTER_OP_LOAD_FIELD_SEQUENCE           = 96,
-       FILTER_OP_LOAD_FIELD_DOUBLE             = 97,
-
-       FILTER_OP_UNARY_BIT_NOT                 = 98,
-
-       FILTER_OP_RETURN_S64                    = 99,
-
-       NR_FILTER_OPS,
-};
-
-typedef uint8_t filter_opcode_t;
-
-struct load_op {
-       filter_opcode_t op;
-       char data[0];
-       /* data to load. Size known by enum filter_opcode and null-term char. */
-} LTTNG_PACKED;
-
-struct binary_op {
-       filter_opcode_t op;
-} LTTNG_PACKED;
-
-struct unary_op {
-       filter_opcode_t op;
-} LTTNG_PACKED;
-
-/* skip_offset is absolute from start of bytecode */
-struct logical_op {
-       filter_opcode_t op;
-       uint16_t skip_offset;   /* bytecode insn, if skip second test */
-} LTTNG_PACKED;
-
-struct cast_op {
-       filter_opcode_t op;
-} LTTNG_PACKED;
-
-struct return_op {
-       filter_opcode_t op;
-} LTTNG_PACKED;
-
-struct lttng_filter_bytecode_alloc {
-       uint32_t alloc_len;
-       struct lttng_filter_bytecode b;
-};
-
-static inline
-unsigned int bytecode_get_len(struct lttng_filter_bytecode *bytecode)
-{
-       return bytecode->len;
-}
-
-#endif /* _FILTER_BYTECODE_H */
index ba888aec3ead29045747dcab3d900e3b63f6b849..ca0b468c3406f6d6e34c1d2c3f62051f87b607f6 100644 (file)
 #include <inttypes.h>
 
 #include <common/compat/errno.h>
-
+#include "common/bytecode/bytecode.h"
 #include "filter-ast.h"
 #include "filter-parser.h"
-#include "filter-bytecode.h"
 
 int main(int argc, char **argv)
 {
index e52f04eb3e6fa8fcc9bce0384f1e7d3442814737..5009b22280eae285a9da717d31fdcfcea9e32fa6 100644 (file)
@@ -17,9 +17,9 @@
 #include <stdlib.h>
 #include <assert.h>
 #include <inttypes.h>
+#include "common/bytecode/bytecode.h"
 #include "filter-ast.h"
 #include "filter-parser.h"
-#include "filter-bytecode.h"
 #include "memstream.h"
 
 #include <common/compat/errno.h>
index ce35cec6e4563a6c781bc19671ee39712c55b146..a5e7a5afe735588096aba6faaa679bd3ab362db6 100644 (file)
 #include <common/compat/errno.h>
 #include <common/compat/string.h>
 
-#include "filter-bytecode.h"
-#include "filter-ir.h"
+#include "common/align.h"
+#include "common/bytecode/bytecode.h"
+#include "common/compat/string.h"
+#include "common/macros.h"
 #include "filter-ast.h"
-
-#include <common/macros.h>
+#include "filter-ir.h"
 
 #ifndef max_t
 #define max_t(type, a, b)      ((type) ((a) > (b) ? (a) : (b)))
 #endif
 
-#define INIT_ALLOC_SIZE                4
-
 static
 int recursive_visit_gen_bytecode(struct filter_parser_ctx *ctx,
                struct ir_op *node);
 
-static inline int get_count_order(unsigned int count)
-{
-       int order;
-
-       order = lttng_fls(count) - 1;
-       if (count & (count - 1))
-               order++;
-       return order;
-}
-
-static
-int bytecode_init(struct lttng_filter_bytecode_alloc **fb)
-{
-       uint32_t alloc_len;
-
-       alloc_len = sizeof(struct lttng_filter_bytecode_alloc) + INIT_ALLOC_SIZE;
-       *fb = calloc(alloc_len, 1);
-       if (!*fb) {
-               return -ENOMEM;
-       } else {
-               (*fb)->alloc_len = alloc_len;
-               return 0;
-       }
-}
-
-static
-int32_t bytecode_reserve(struct lttng_filter_bytecode_alloc **fb, uint32_t align, uint32_t len)
-{
-       int32_t ret;
-       uint32_t padding = offset_align((*fb)->b.len, align);
-       uint32_t new_len = (*fb)->b.len + padding + len;
-       uint32_t new_alloc_len = sizeof(struct lttng_filter_bytecode_alloc) + new_len;
-       uint32_t old_alloc_len = (*fb)->alloc_len;
-
-       if (new_len > LTTNG_FILTER_MAX_LEN)
-               return -EINVAL;
-
-       if (new_alloc_len > old_alloc_len) {
-               struct lttng_filter_bytecode_alloc *newptr;
-
-               new_alloc_len =
-                       max_t(uint32_t, 1U << get_count_order(new_alloc_len), old_alloc_len << 1);
-               newptr = realloc(*fb, new_alloc_len);
-               if (!newptr)
-                       return -ENOMEM;
-               *fb = newptr;
-               /* We zero directly the memory from start of allocation. */
-               memset(&((char *) *fb)[old_alloc_len], 0, new_alloc_len - old_alloc_len);
-               (*fb)->alloc_len = new_alloc_len;
-       }
-       (*fb)->b.len += padding;
-       ret = (*fb)->b.len;
-       (*fb)->b.len += len;
-       return ret;
-}
-
-static
-int bytecode_push(struct lttng_filter_bytecode_alloc **fb, const void *data,
-               uint32_t align, uint32_t len)
-{
-       int32_t offset;
-
-       offset = bytecode_reserve(fb, align, len);
-       if (offset < 0)
-               return offset;
-       memcpy(&(*fb)->b.data[offset], data, len);
-       return 0;
-}
-
-static
-int bytecode_push_logical(struct lttng_filter_bytecode_alloc **fb,
-               struct logical_op *data,
-               uint32_t align, uint32_t len,
-               uint16_t *skip_offset)
-{
-       int32_t offset;
-
-       offset = bytecode_reserve(fb, align, len);
-       if (offset < 0)
-               return offset;
-       memcpy(&(*fb)->b.data[offset], data, len);
-       *skip_offset =
-               (void *) &((struct logical_op *) &(*fb)->b.data[offset])->skip_offset
-                       - (void *) &(*fb)->b.data[0];
-       return 0;
-}
-
 static
 int bytecode_patch(struct lttng_filter_bytecode_alloc **fb,
                const void *data,
index 1147296ad3357e21d4fd169ef54f73c78f13512f..21c1a91adde55648238359d736c8e7e820009de6 100644 (file)
@@ -22,6 +22,7 @@
 #include <assert.h>
 #include <signal.h>
 
+#include <common/bytecode/bytecode.h>
 #include <common/lttng-kernel.h>
 #include <common/common.h>
 #include <common/utils.h>
@@ -37,7 +38,6 @@
 
 #include <common/sessiond-comm/sessiond-comm.h>
 #include <common/filter/filter-ast.h>
-#include <common/filter/filter-bytecode.h>
 
 #include "runas.h"
 
index 5529b3364f12027c8b143fbdcba8a4e7304dad70..6deab78486efe7b69c18645481ccc23b065b600a 100644 (file)
@@ -18,6 +18,7 @@
 #include <string.h>
 #include <unistd.h>
 
+#include <common/bytecode/bytecode.h>
 #include <common/common.h>
 #include <common/compat/errno.h>
 #include <common/compat/string.h>
@@ -45,7 +46,6 @@
 
 #include <common/filter/filter-ast.h>
 #include <common/filter/filter-parser.h>
-#include <common/filter/filter-bytecode.h>
 #include <common/filter/memstream.h>
 #include "lttng-ctl-helper.h"
 
This page took 0.081353 seconds and 4 git commands to generate.