From 0ae3cfc61fedae38ef31fe5a99458c4f2161c3b5 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Fri, 3 Apr 2020 13:50:57 -0400 Subject: [PATCH] common: move bytecode utilities from filter to its own file MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau Depends-on: lttng-ust: I5a800fc92e588c2a6a0e26282b0ad5f31c044479 --- configure.ac | 1 + src/common/Makefile.am | 3 + src/common/bytecode/Makefile.am | 6 ++ src/common/bytecode/bytecode.c | 101 ++++++++++++++++++ .../filter-bytecode.h => bytecode/bytecode.h} | 29 ++--- src/common/filter/Makefile.am | 5 +- src/common/filter/filter-ast.h | 1 + src/common/filter/filter-grammar-test.c | 3 +- src/common/filter/filter-parser.y | 2 +- .../filter/filter-visitor-generate-bytecode.c | 98 +---------------- src/common/runas.c | 2 +- src/lib/lttng-ctl/lttng-ctl.c | 2 +- 12 files changed, 141 insertions(+), 112 deletions(-) create mode 100644 src/common/bytecode/Makefile.am create mode 100644 src/common/bytecode/bytecode.c rename src/common/{filter/filter-bytecode.h => bytecode/bytecode.h} (87%) diff --git a/configure.ac b/configure.ac index 954f74d99..58d4f9292 100644 --- a/configure.ac +++ b/configure.ac @@ -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 diff --git a/src/common/Makefile.am b/src/common/Makefile.am index 8a74b7282..cfb935c5a 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -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 index 000000000..9e1c6ddd1 --- /dev/null +++ b/src/common/bytecode/Makefile.am @@ -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 index 000000000..b8edd6913 --- /dev/null +++ b/src/common/bytecode/bytecode.c @@ -0,0 +1,101 @@ +/* + * Copyright 2020 EfficiOS, Inc. + * + * SPDX-License-Identifier: GPL-2.0-only + * + */ + +#include "bytecode.h" + +#include + +#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/filter/filter-bytecode.h b/src/common/bytecode/bytecode.h similarity index 87% rename from src/common/filter/filter-bytecode.h rename to src/common/bytecode/bytecode.h index 053bb08bb..ecf54f92e 100644 --- a/src/common/filter/filter-bytecode.h +++ b/src/common/bytecode/bytecode.h @@ -1,21 +1,17 @@ -#ifndef _FILTER_BYTECODE_H -#define _FILTER_BYTECODE_H - /* - * filter-bytecode.h - * - * LTTng filter bytecode - * - * Copyright 2012 Mathieu Desnoyers + * Copyright 2020 EfficiOS, Inc. * * SPDX-License-Identifier: LGPL-2.1-only * */ -#include -#include +#ifndef LTTNG_COMMON_BYTECODE_H +#define LTTNG_COMMON_BYTECODE_H -#include "filter-ast.h" +#include + +#include "common/macros.h" +#include "common/sessiond-comm/sessiond-comm.h" /* * offsets are absolute from start of bytecode. @@ -234,10 +230,19 @@ struct lttng_filter_bytecode_alloc { 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 /* _FILTER_BYTECODE_H */ +#endif /* LTTNG_COMMON_BYTECODE_H */ diff --git a/src/common/filter/Makefile.am b/src/common/filter/Makefile.am index cac4cb175..261acc086 100644 --- a/src/common/filter/Makefile.am +++ b/src/common/filter/Makefile.am @@ -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 diff --git a/src/common/filter/filter-ast.h b/src/common/filter/filter-ast.h index 29fde10f8..a82e4f6bf 100644 --- a/src/common/filter/filter-ast.h +++ b/src/common/filter/filter-ast.h @@ -17,6 +17,7 @@ */ #include +#include #include #define printf_debug(fmt, args...) \ diff --git a/src/common/filter/filter-grammar-test.c b/src/common/filter/filter-grammar-test.c index ba888aec3..ca0b468c3 100644 --- a/src/common/filter/filter-grammar-test.c +++ b/src/common/filter/filter-grammar-test.c @@ -17,10 +17,9 @@ #include #include - +#include "common/bytecode/bytecode.h" #include "filter-ast.h" #include "filter-parser.h" -#include "filter-bytecode.h" int main(int argc, char **argv) { diff --git a/src/common/filter/filter-parser.y b/src/common/filter/filter-parser.y index e52f04eb3..5009b2228 100644 --- a/src/common/filter/filter-parser.y +++ b/src/common/filter/filter-parser.y @@ -17,9 +17,9 @@ #include #include #include +#include "common/bytecode/bytecode.h" #include "filter-ast.h" #include "filter-parser.h" -#include "filter-bytecode.h" #include "memstream.h" #include diff --git a/src/common/filter/filter-visitor-generate-bytecode.c b/src/common/filter/filter-visitor-generate-bytecode.c index ce35cec6e..a5e7a5afe 100644 --- a/src/common/filter/filter-visitor-generate-bytecode.c +++ b/src/common/filter/filter-visitor-generate-bytecode.c @@ -15,109 +15,21 @@ #include #include -#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 +#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, diff --git a/src/common/runas.c b/src/common/runas.c index 1147296ad..21c1a91ad 100644 --- a/src/common/runas.c +++ b/src/common/runas.c @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -37,7 +38,6 @@ #include #include -#include #include "runas.h" diff --git a/src/lib/lttng-ctl/lttng-ctl.c b/src/lib/lttng-ctl/lttng-ctl.c index 5529b3364..6deab7848 100644 --- a/src/lib/lttng-ctl/lttng-ctl.c +++ b/src/lib/lttng-ctl/lttng-ctl.c @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -45,7 +46,6 @@ #include #include -#include #include #include "lttng-ctl-helper.h" -- 2.34.1