From e4d2f27a74baf6942ac8fcafd5ea53e775ceceb3 Mon Sep 17 00:00:00 2001 From: Jonathan Rajotte Date: Thu, 28 Nov 2019 16:09:16 -0500 Subject: [PATCH] Move filter related code to libfilter under libcommon MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jonathan Rajotte Signed-off-by: Jérémie Galarneau Change-Id: I006dcac801ce1ec742de3a03f21a1c5f8b698298 --- .gitignore | 10 +- configure.ac | 4 +- src/bin/lttng/Makefile.am | 1 + src/common/Makefile.am | 9 +- .../lttng-ctl => common}/filter/Makefile.am | 0 .../lttng-ctl => common}/filter/filter-ast.h | 2 + .../filter/filter-bytecode.h | 0 .../filter/filter-grammar-test.c | 0 .../lttng-ctl => common}/filter/filter-ir.h | 0 .../filter/filter-lexer.l | 0 .../filter/filter-parser.y | 133 +++++++++++++++ .../filter/filter-symbols.h | 0 .../filter/filter-visitor-generate-bytecode.c | 0 .../filter/filter-visitor-generate-ir.c | 0 ...ilter-visitor-ir-check-binary-comparator.c | 0 ...ilter-visitor-ir-check-binary-op-nesting.c | 0 ...ilter-visitor-ir-normalize-glob-patterns.c | 0 .../filter-visitor-ir-validate-globbing.c | 0 .../filter-visitor-ir-validate-string.c | 0 .../filter/filter-visitor-xml.c | 0 .../lttng-ctl => common}/filter/memstream.h | 0 src/lib/lttng-ctl/Makefile.am | 5 +- src/lib/lttng-ctl/lttng-ctl.c | 161 ++---------------- 23 files changed, 164 insertions(+), 161 deletions(-) rename src/{lib/lttng-ctl => common}/filter/Makefile.am (100%) rename src/{lib/lttng-ctl => common}/filter/filter-ast.h (97%) rename src/{lib/lttng-ctl => common}/filter/filter-bytecode.h (100%) rename src/{lib/lttng-ctl => common}/filter/filter-grammar-test.c (100%) rename src/{lib/lttng-ctl => common}/filter/filter-ir.h (100%) rename src/{lib/lttng-ctl => common}/filter/filter-lexer.l (100%) rename src/{lib/lttng-ctl => common}/filter/filter-parser.y (83%) rename src/{lib/lttng-ctl => common}/filter/filter-symbols.h (100%) rename src/{lib/lttng-ctl => common}/filter/filter-visitor-generate-bytecode.c (100%) rename src/{lib/lttng-ctl => common}/filter/filter-visitor-generate-ir.c (100%) rename src/{lib/lttng-ctl => common}/filter/filter-visitor-ir-check-binary-comparator.c (100%) rename src/{lib/lttng-ctl => common}/filter/filter-visitor-ir-check-binary-op-nesting.c (100%) rename src/{lib/lttng-ctl => common}/filter/filter-visitor-ir-normalize-glob-patterns.c (100%) rename src/{lib/lttng-ctl => common}/filter/filter-visitor-ir-validate-globbing.c (100%) rename src/{lib/lttng-ctl => common}/filter/filter-visitor-ir-validate-string.c (100%) rename src/{lib/lttng-ctl => common}/filter/filter-visitor-xml.c (100%) rename src/{lib/lttng-ctl => common}/filter/memstream.h (100%) diff --git a/.gitignore b/.gitignore index 78c99b781..49aa0d1c1 100644 --- a/.gitignore +++ b/.gitignore @@ -61,11 +61,11 @@ compile_commands.json /src/bin/lttng-crash/lttng-crash /src/bin/lttng-relayd/lttng-relayd /src/lib/lttng-ctl/lttng-ctl.pc -/src/lib/lttng-ctl/filter/filter-grammar-test -/src/lib/lttng-ctl/filter/filter-lexer.c -/src/lib/lttng-ctl/filter/filter-parser.c -/src/lib/lttng-ctl/filter/filter-parser.h -/src/lib/lttng-ctl/filter/filter-parser.output +/src/common/filter/filter-grammar-test +/src/common/filter/filter-lexer.c +/src/common/filter/filter-parser.c +/src/common/filter/filter-parser.h +/src/common/filter/filter-parser.output /extras/bindings/swig/python/lttng.i /extras/bindings/swig/python/lttng.py diff --git a/configure.ac b/configure.ac index 4e186d471..c6b83b78a 100644 --- a/configure.ac +++ b/configure.ac @@ -1081,7 +1081,7 @@ AC_SUBST(AM_LDFLAGS) # In a scenario where lttng-tools is built from a distribution tarball and in a # out-of-tree manner, the generated "version.i" has priority on the one from # the source (distribution tarball) and must be found first. -AM_CPPFLAGS="-I\$(top_builddir)/include -I\$(top_srcdir)/include -I\$(top_srcdir)/src -include config.h $AM_CPPFLAGS" +AM_CPPFLAGS="-I\$(top_builddir)/include -I\$(top_srcdir)/include -I\$(top_builddir)/src -I\$(top_srcdir)/src -include config.h $AM_CPPFLAGS" AC_SUBST(AM_CPPFLAGS) lttngincludedir="${includedir}/lttng" @@ -1129,9 +1129,9 @@ AC_CONFIG_FILES([ src/common/config/Makefile src/common/string-utils/Makefile src/common/fd-tracker/Makefile + src/common/filter/Makefile src/lib/Makefile src/lib/lttng-ctl/Makefile - src/lib/lttng-ctl/filter/Makefile src/lib/lttng-ctl/lttng-ctl.pc src/bin/Makefile src/bin/lttng-consumerd/Makefile diff --git a/src/bin/lttng/Makefile.am b/src/bin/lttng/Makefile.am index d094c5aaf..a6bd7a9d7 100644 --- a/src/bin/lttng/Makefile.am +++ b/src/bin/lttng/Makefile.am @@ -37,4 +37,5 @@ lttng_LDADD = $(top_builddir)/src/lib/lttng-ctl/liblttng-ctl.la \ $(top_builddir)/src/common/libcommon.la \ $(top_builddir)/src/common/config/libconfig.la \ $(top_builddir)/src/common/string-utils/libstring-utils.la \ + $(top_builddir)/src/common/filter/libfilter.la \ $(POPT_LIBS) diff --git a/src/common/Makefile.am b/src/common/Makefile.am index c855dcd7a..6a841e92a 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -2,7 +2,7 @@ AUTOMAKE_OPTIONS = subdir-objects -SUBDIRS = string-utils +SUBDIRS = string-utils filter # Make sure to always distribute all folders # since SUBDIRS is decided at configure time. @@ -20,8 +20,8 @@ DIST_SUBDIRS = \ config \ consumer \ string-utils \ - fd-tracker - + fd-tracker \ + filter # Common library noinst_LTLIBRARIES = libcommon.la EXTRA_DIST = mi-lttng-4.0.xsd @@ -87,7 +87,8 @@ libcommon_la_LIBADD = \ $(top_builddir)/src/common/config/libconfig.la \ $(top_builddir)/src/common/compat/libcompat.la \ $(top_builddir)/src/common/hashtable/libhashtable.la \ - $(top_builddir)/src/common/fd-tracker/libfd-tracker.la + $(top_builddir)/src/common/fd-tracker/libfd-tracker.la \ + $(top_builddir)/src/common/filter/libfilter.la if BUILD_LIB_COMPAT SUBDIRS += compat diff --git a/src/lib/lttng-ctl/filter/Makefile.am b/src/common/filter/Makefile.am similarity index 100% rename from src/lib/lttng-ctl/filter/Makefile.am rename to src/common/filter/Makefile.am diff --git a/src/lib/lttng-ctl/filter/filter-ast.h b/src/common/filter/filter-ast.h similarity index 97% rename from src/lib/lttng-ctl/filter/filter-ast.h rename to src/common/filter/filter-ast.h index 926d8dd64..29fde10f8 100644 --- a/src/lib/lttng-ctl/filter/filter-ast.h +++ b/src/common/filter/filter-ast.h @@ -164,6 +164,8 @@ struct filter_parser_ctx { struct filter_parser_ctx *filter_parser_ctx_alloc(FILE *input); void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx); int filter_parser_ctx_append_ast(struct filter_parser_ctx *parser_ctx); +int filter_parser_ctx_create_from_filter_expression( + const char *filter_expression, struct filter_parser_ctx **ctxp); static inline struct filter_ast *filter_parser_get_ast(struct filter_parser_ctx *parser_ctx) diff --git a/src/lib/lttng-ctl/filter/filter-bytecode.h b/src/common/filter/filter-bytecode.h similarity index 100% rename from src/lib/lttng-ctl/filter/filter-bytecode.h rename to src/common/filter/filter-bytecode.h diff --git a/src/lib/lttng-ctl/filter/filter-grammar-test.c b/src/common/filter/filter-grammar-test.c similarity index 100% rename from src/lib/lttng-ctl/filter/filter-grammar-test.c rename to src/common/filter/filter-grammar-test.c diff --git a/src/lib/lttng-ctl/filter/filter-ir.h b/src/common/filter/filter-ir.h similarity index 100% rename from src/lib/lttng-ctl/filter/filter-ir.h rename to src/common/filter/filter-ir.h diff --git a/src/lib/lttng-ctl/filter/filter-lexer.l b/src/common/filter/filter-lexer.l similarity index 100% rename from src/lib/lttng-ctl/filter/filter-lexer.l rename to src/common/filter/filter-lexer.l diff --git a/src/lib/lttng-ctl/filter/filter-parser.y b/src/common/filter/filter-parser.y similarity index 83% rename from src/lib/lttng-ctl/filter/filter-parser.y rename to src/common/filter/filter-parser.y index ba2d3007b..245d83c26 100644 --- a/src/lib/lttng-ctl/filter/filter-parser.y +++ b/src/common/filter/filter-parser.y @@ -20,6 +20,8 @@ #include #include "filter-ast.h" #include "filter-parser.h" +#include "filter-bytecode.h" +#include "memstream.h" #include @@ -28,6 +30,20 @@ #define WIDTH_x64_SCANF_IS_A_BROKEN_API "17" #define WIDTH_lg_SCANF_IS_A_BROKEN_API "4096" /* Hugely optimistic approximation */ +#ifdef DEBUG +static const int print_xml = 1; +#define dbg_printf(fmt, args...) \ + printf("[debug filter_parser] " fmt, ## args) +#else +static const int print_xml = 0; +#define dbg_printf(fmt, args...) \ +do { \ + /* do nothing but check printf format */ \ + if (0) \ + printf("[debug filter_parser] " fmt, ## args); \ +} while (0) +#endif + LTTNG_HIDDEN int yydebug; LTTNG_HIDDEN @@ -288,6 +304,123 @@ void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx) free(parser_ctx); } +LTTNG_HIDDEN +int filter_parser_ctx_create_from_filter_expression( + const char *filter_expression, struct filter_parser_ctx **ctxp) +{ + int ret; + struct filter_parser_ctx *ctx = NULL; + FILE *fmem = NULL; + + assert(filter_expression); + assert(ctxp); + + /* + * Casting const to non-const, as the underlying function will use it in + * read-only mode. + */ + fmem = lttng_fmemopen((void *) filter_expression, + strlen(filter_expression), "r"); + if (!fmem) { + fprintf(stderr, "Error opening memory as stream\n"); + ret = -LTTNG_ERR_FILTER_NOMEM; + goto error; + } + ctx = filter_parser_ctx_alloc(fmem); + if (!ctx) { + fprintf(stderr, "Error allocating parser\n"); + ret = -LTTNG_ERR_FILTER_NOMEM; + goto filter_alloc_error; + } + ret = filter_parser_ctx_append_ast(ctx); + if (ret) { + fprintf(stderr, "Parse error\n"); + ret = -LTTNG_ERR_FILTER_INVAL; + goto parse_error; + } + if (print_xml) { + ret = filter_visitor_print_xml(ctx, stdout, 0); + if (ret) { + fflush(stdout); + fprintf(stderr, "XML print error\n"); + ret = -LTTNG_ERR_FILTER_INVAL; + goto parse_error; + } + } + + dbg_printf("Generating IR... "); + fflush(stdout); + ret = filter_visitor_ir_generate(ctx); + if (ret) { + fprintf(stderr, "Generate IR error\n"); + ret = -LTTNG_ERR_FILTER_INVAL; + goto parse_error; + } + dbg_printf("done\n"); + + dbg_printf("Validating IR... "); + fflush(stdout); + ret = filter_visitor_ir_check_binary_op_nesting(ctx); + if (ret) { + ret = -LTTNG_ERR_FILTER_INVAL; + goto parse_error; + } + + /* Normalize globbing patterns in the expression. */ + ret = filter_visitor_ir_normalize_glob_patterns(ctx); + if (ret) { + ret = -LTTNG_ERR_FILTER_INVAL; + goto parse_error; + } + + /* Validate strings used as literals in the expression. */ + ret = filter_visitor_ir_validate_string(ctx); + if (ret) { + ret = -LTTNG_ERR_FILTER_INVAL; + goto parse_error; + } + + /* Validate globbing patterns in the expression. */ + ret = filter_visitor_ir_validate_globbing(ctx); + if (ret) { + ret = -LTTNG_ERR_FILTER_INVAL; + goto parse_error; + } + + dbg_printf("done\n"); + + dbg_printf("Generating bytecode... "); + fflush(stdout); + ret = filter_visitor_bytecode_generate(ctx); + if (ret) { + fprintf(stderr, "Generate bytecode error\n"); + ret = -LTTNG_ERR_FILTER_INVAL; + goto parse_error; + } + dbg_printf("done\n"); + dbg_printf("Size of bytecode generated: %u bytes.\n", + bytecode_get_len(&ctx->bytecode->b)); + + /* No need to keep the memory stream. */ + if (fclose(fmem) != 0) { + fprintf(stderr, "fclose (%d) \n", errno); + ret = -LTTNG_ERR_FILTER_INVAL; + } + + *ctxp = ctx; + return 0; + +parse_error: + filter_ir_free(ctx); + filter_parser_ctx_free(ctx); +filter_alloc_error: + if (fclose(fmem) != 0) { + fprintf(stderr, "fclose (%d) \n", errno); + } +error: + return ret; +} + %} %code provides diff --git a/src/lib/lttng-ctl/filter/filter-symbols.h b/src/common/filter/filter-symbols.h similarity index 100% rename from src/lib/lttng-ctl/filter/filter-symbols.h rename to src/common/filter/filter-symbols.h diff --git a/src/lib/lttng-ctl/filter/filter-visitor-generate-bytecode.c b/src/common/filter/filter-visitor-generate-bytecode.c similarity index 100% rename from src/lib/lttng-ctl/filter/filter-visitor-generate-bytecode.c rename to src/common/filter/filter-visitor-generate-bytecode.c diff --git a/src/lib/lttng-ctl/filter/filter-visitor-generate-ir.c b/src/common/filter/filter-visitor-generate-ir.c similarity index 100% rename from src/lib/lttng-ctl/filter/filter-visitor-generate-ir.c rename to src/common/filter/filter-visitor-generate-ir.c diff --git a/src/lib/lttng-ctl/filter/filter-visitor-ir-check-binary-comparator.c b/src/common/filter/filter-visitor-ir-check-binary-comparator.c similarity index 100% rename from src/lib/lttng-ctl/filter/filter-visitor-ir-check-binary-comparator.c rename to src/common/filter/filter-visitor-ir-check-binary-comparator.c diff --git a/src/lib/lttng-ctl/filter/filter-visitor-ir-check-binary-op-nesting.c b/src/common/filter/filter-visitor-ir-check-binary-op-nesting.c similarity index 100% rename from src/lib/lttng-ctl/filter/filter-visitor-ir-check-binary-op-nesting.c rename to src/common/filter/filter-visitor-ir-check-binary-op-nesting.c diff --git a/src/lib/lttng-ctl/filter/filter-visitor-ir-normalize-glob-patterns.c b/src/common/filter/filter-visitor-ir-normalize-glob-patterns.c similarity index 100% rename from src/lib/lttng-ctl/filter/filter-visitor-ir-normalize-glob-patterns.c rename to src/common/filter/filter-visitor-ir-normalize-glob-patterns.c diff --git a/src/lib/lttng-ctl/filter/filter-visitor-ir-validate-globbing.c b/src/common/filter/filter-visitor-ir-validate-globbing.c similarity index 100% rename from src/lib/lttng-ctl/filter/filter-visitor-ir-validate-globbing.c rename to src/common/filter/filter-visitor-ir-validate-globbing.c diff --git a/src/lib/lttng-ctl/filter/filter-visitor-ir-validate-string.c b/src/common/filter/filter-visitor-ir-validate-string.c similarity index 100% rename from src/lib/lttng-ctl/filter/filter-visitor-ir-validate-string.c rename to src/common/filter/filter-visitor-ir-validate-string.c diff --git a/src/lib/lttng-ctl/filter/filter-visitor-xml.c b/src/common/filter/filter-visitor-xml.c similarity index 100% rename from src/lib/lttng-ctl/filter/filter-visitor-xml.c rename to src/common/filter/filter-visitor-xml.c diff --git a/src/lib/lttng-ctl/filter/memstream.h b/src/common/filter/memstream.h similarity index 100% rename from src/lib/lttng-ctl/filter/memstream.h rename to src/common/filter/memstream.h diff --git a/src/lib/lttng-ctl/Makefile.am b/src/lib/lttng-ctl/Makefile.am index 1bee05f0c..8c271f5ef 100644 --- a/src/lib/lttng-ctl/Makefile.am +++ b/src/lib/lttng-ctl/Makefile.am @@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only -SUBDIRS = filter +SUBDIRS = AM_CPPFLAGS += -I$(srcdir) -I$(builddir) @@ -16,8 +16,7 @@ liblttng_ctl_la_LDFLAGS = \ liblttng_ctl_la_LIBADD = \ $(top_builddir)/src/common/sessiond-comm/libsessiond-comm.la \ - $(top_builddir)/src/common/libcommon.la \ - $(top_builddir)/src/lib/lttng-ctl/filter/libfilter.la + $(top_builddir)/src/common/libcommon.la pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = lttng-ctl.pc diff --git a/src/lib/lttng-ctl/lttng-ctl.c b/src/lib/lttng-ctl/lttng-ctl.c index 81ddeb74a..4eb36a261 100644 --- a/src/lib/lttng-ctl/lttng-ctl.c +++ b/src/lib/lttng-ctl/lttng-ctl.c @@ -43,26 +43,12 @@ #include #include -#include "filter/filter-ast.h" -#include "filter/filter-parser.h" -#include "filter/filter-bytecode.h" -#include "filter/memstream.h" +#include +#include +#include +#include #include "lttng-ctl-helper.h" -#ifdef DEBUG -static const int print_xml = 1; -#define dbg_printf(fmt, args...) \ - printf("[debug liblttng-ctl] " fmt, ## args) -#else -static const int print_xml = 0; -#define dbg_printf(fmt, args...) \ -do { \ - /* do nothing but check printf format */ \ - if (0) \ - printf("[debug liblttnctl] " fmt, ## args); \ -} while (0) -#endif - #define COPY_DOMAIN_PACKED(dst, src) \ do { \ struct lttng_domain _tmp_domain; \ @@ -1050,133 +1036,6 @@ error: return NULL; } -/* - * Generate the filter bytecode from a given filter expression string. Put the - * newly allocated parser context in ctxp and populate the lsm object with the - * expression len. - * - * Return 0 on success else a LTTNG_ERR_* code and ctxp is untouched. - */ -static int generate_filter(char *filter_expression, - struct lttcomm_session_msg *lsm, struct filter_parser_ctx **ctxp) -{ - int ret; - struct filter_parser_ctx *ctx = NULL; - FILE *fmem = NULL; - - assert(filter_expression); - assert(lsm); - assert(ctxp); - - /* - * Casting const to non-const, as the underlying function will use it in - * read-only mode. - */ - fmem = lttng_fmemopen((void *) filter_expression, - strlen(filter_expression), "r"); - if (!fmem) { - fprintf(stderr, "Error opening memory as stream\n"); - ret = -LTTNG_ERR_FILTER_NOMEM; - goto error; - } - ctx = filter_parser_ctx_alloc(fmem); - if (!ctx) { - fprintf(stderr, "Error allocating parser\n"); - ret = -LTTNG_ERR_FILTER_NOMEM; - goto filter_alloc_error; - } - ret = filter_parser_ctx_append_ast(ctx); - if (ret) { - fprintf(stderr, "Parse error\n"); - ret = -LTTNG_ERR_FILTER_INVAL; - goto parse_error; - } - if (print_xml) { - ret = filter_visitor_print_xml(ctx, stdout, 0); - if (ret) { - fflush(stdout); - fprintf(stderr, "XML print error\n"); - ret = -LTTNG_ERR_FILTER_INVAL; - goto parse_error; - } - } - - dbg_printf("Generating IR... "); - fflush(stdout); - ret = filter_visitor_ir_generate(ctx); - if (ret) { - fprintf(stderr, "Generate IR error\n"); - ret = -LTTNG_ERR_FILTER_INVAL; - goto parse_error; - } - dbg_printf("done\n"); - - dbg_printf("Validating IR... "); - fflush(stdout); - ret = filter_visitor_ir_check_binary_op_nesting(ctx); - if (ret) { - ret = -LTTNG_ERR_FILTER_INVAL; - goto parse_error; - } - - /* Normalize globbing patterns in the expression. */ - ret = filter_visitor_ir_normalize_glob_patterns(ctx); - if (ret) { - ret = -LTTNG_ERR_FILTER_INVAL; - goto parse_error; - } - - /* Validate strings used as literals in the expression. */ - ret = filter_visitor_ir_validate_string(ctx); - if (ret) { - ret = -LTTNG_ERR_FILTER_INVAL; - goto parse_error; - } - - /* Validate globbing patterns in the expression. */ - ret = filter_visitor_ir_validate_globbing(ctx); - if (ret) { - ret = -LTTNG_ERR_FILTER_INVAL; - goto parse_error; - } - - dbg_printf("done\n"); - - dbg_printf("Generating bytecode... "); - fflush(stdout); - ret = filter_visitor_bytecode_generate(ctx); - if (ret) { - fprintf(stderr, "Generate bytecode error\n"); - ret = -LTTNG_ERR_FILTER_INVAL; - goto parse_error; - } - dbg_printf("done\n"); - dbg_printf("Size of bytecode generated: %u bytes.\n", - bytecode_get_len(&ctx->bytecode->b)); - - lsm->u.enable.bytecode_len = sizeof(ctx->bytecode->b) - + bytecode_get_len(&ctx->bytecode->b); - lsm->u.enable.expression_len = strlen(filter_expression) + 1; - - /* No need to keep the memory stream. */ - if (fclose(fmem) != 0) { - PERROR("fclose"); - } - - *ctxp = ctx; - return 0; - -parse_error: - filter_ir_free(ctx); - filter_parser_ctx_free(ctx); -filter_alloc_error: - if (fclose(fmem) != 0) { - PERROR("fclose"); - } -error: - return ret; -} - /* * Enable event(s) for a channel, possibly with exclusions and a filter. * If no event name is specified, all events are enabled. @@ -1279,10 +1138,14 @@ int lttng_enable_event_with_exclusions(struct lttng_handle *handle, } } - ret = generate_filter(filter_expression, &lsm, &ctx); + ret = filter_parser_ctx_create_from_filter_expression(filter_expression, &ctx); if (ret) { goto filter_error; } + + lsm.u.enable.bytecode_len = sizeof(ctx->bytecode->b) + + bytecode_get_len(&ctx->bytecode->b); + lsm.u.enable.expression_len = strlen(filter_expression) + 1; } ret = lttng_dynamic_buffer_set_capacity(&payload.buffer, @@ -1509,10 +1372,14 @@ int lttng_disable_event_ext(struct lttng_handle *handle, } } - ret = generate_filter(filter_expression, &lsm, &ctx); + ret = filter_parser_ctx_create_from_filter_expression(filter_expression, &ctx); if (ret) { goto filter_error; } + + lsm.u.enable.bytecode_len = sizeof(ctx->bytecode->b) + + bytecode_get_len(&ctx->bytecode->b); + lsm.u.enable.expression_len = strlen(filter_expression) + 1; } varlen_data = zmalloc(lsm.u.disable.bytecode_len -- 2.34.1