From 1aa9c49c60b7d7370ceafc3ac076de423de22b7f Mon Sep 17 00:00:00 2001 From: Jonathan Rajotte Date: Thu, 11 Feb 2021 10:40:39 -0500 Subject: [PATCH] Move event-expr-to-bytecode to event-expr 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: I74a4b823ae7bbcbb062dbb9a2a0f84785bca287a --- include/lttng/event-expr-internal.h | 7 + src/common/Makefile.am | 1 - src/common/conditions/event-rule-matches.c | 1 - src/common/event-expr-to-bytecode.c | 225 --------------------- src/common/event-expr-to-bytecode.h | 20 -- src/common/event-expr/event-expr.c | 217 ++++++++++++++++++++ tests/unit/test_event_expr_to_bytecode.c | 4 +- 7 files changed, 226 insertions(+), 249 deletions(-) delete mode 100644 src/common/event-expr-to-bytecode.c delete mode 100644 src/common/event-expr-to-bytecode.h diff --git a/include/lttng/event-expr-internal.h b/include/lttng/event-expr-internal.h index 88fa695de..048aaff18 100644 --- a/include/lttng/event-expr-internal.h +++ b/include/lttng/event-expr-internal.h @@ -9,8 +9,11 @@ #define LTTNG_EVENT_EXPR_INTERNAL_H #include +#include #include +struct lttng_bytecode; + struct lttng_event_expr { enum lttng_event_expr_type type; }; @@ -54,4 +57,8 @@ bool lttng_event_expr_is_lvalue(const struct lttng_event_expr *expr) expr->type == LTTNG_EVENT_EXPR_TYPE_ARRAY_FIELD_ELEMENT; } +LTTNG_HIDDEN +int lttng_event_expr_to_bytecode(const struct lttng_event_expr *expr, + struct lttng_bytecode **bytecode_out); + #endif /* LTTNG_EVENT_EXPR_INTERNAL_H */ diff --git a/src/common/Makefile.am b/src/common/Makefile.am index ca0973403..9c66a7658 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -67,7 +67,6 @@ libcommon_la_SOURCES = \ evaluation.c \ event.c \ event-expr/event-expr.c \ - event-expr-to-bytecode.c event-expr-to-bytecode.h \ event-field-value.c \ event-rule/event-rule.c \ event-rule/kernel-kprobe.c \ diff --git a/src/common/conditions/event-rule-matches.c b/src/common/conditions/event-rule-matches.c index 887796642..259173a63 100644 --- a/src/common/conditions/event-rule-matches.c +++ b/src/common/conditions/event-rule-matches.c @@ -7,7 +7,6 @@ #include #include -#include #include #include #include diff --git a/src/common/event-expr-to-bytecode.c b/src/common/event-expr-to-bytecode.c deleted file mode 100644 index 9fe3c0350..000000000 --- a/src/common/event-expr-to-bytecode.c +++ /dev/null @@ -1,225 +0,0 @@ -/* - * Copyright 2020 EfficiOS, Inc. - * - * SPDX-License-Identifier: LGPL-2.1-only - * - */ - -#include "event-expr-to-bytecode.h" - -#include -#include -#include -#include - -static -int event_expr_to_bytecode_recursive(const struct lttng_event_expr *expr, - struct lttng_bytecode_alloc **bytecode, - struct lttng_bytecode_alloc **bytecode_reloc) -{ - int status; - enum lttng_event_expr_status event_expr_status; - - switch (lttng_event_expr_get_type(expr)) { - case LTTNG_EVENT_EXPR_TYPE_EVENT_PAYLOAD_FIELD: - { - const char *name; - - status = bytecode_push_get_payload_root(bytecode); - if (status) { - ERR("Failed to get payload root from bytecode"); - goto end; - } - - name = lttng_event_expr_event_payload_field_get_name(expr); - if (!name) { - ERR("Failed to get payload field name from event expression"); - status = -1; - goto end; - } - - status = bytecode_push_get_symbol(bytecode, bytecode_reloc, name); - if (status) { - ERR("Failed to push 'get symbol %s' in bytecode", name); - goto end; - } - - break; - } - case LTTNG_EVENT_EXPR_TYPE_CHANNEL_CONTEXT_FIELD: - { - const char *name; - - status = bytecode_push_get_context_root(bytecode); - if (status) { - ERR("Failed to get context root from bytecode"); - goto end; - } - - name = lttng_event_expr_channel_context_field_get_name(expr); - if (!name) { - ERR("Failed to get channel context field name from event expression"); - status = -1; - goto end; - } - - status = bytecode_push_get_symbol(bytecode, bytecode_reloc, name); - if (status) { - ERR("Failed to push 'get symbol %s' in bytecode", name); - goto end; - } - - break; - } - case LTTNG_EVENT_EXPR_TYPE_APP_SPECIFIC_CONTEXT_FIELD: - { - int ret; - char *name = NULL; - const char *provider_name, *type_name; - - status = bytecode_push_get_app_context_root(bytecode); - if (status) { - ERR("Failed to get application context root from bytecode"); - goto end; - } - - provider_name = lttng_event_expr_app_specific_context_field_get_provider_name( - expr); - if (!provider_name) { - ERR("Failed to get application context provider name from event expression"); - status = -1; - goto end; - } - - type_name = lttng_event_expr_app_specific_context_field_get_type_name( - expr); - if (!type_name) { - ERR("Failed to get application context type name from event expression"); - status = -1; - goto end; - } - - /* - * Reconstitute the app context field name from its two parts. - */ - ret = asprintf(&name, "%s:%s", provider_name, type_name); - if (ret < 0) { - PERROR("Failed to format application specific context: provider_name = '%s', type_name = '%s'", - provider_name, type_name); - status = -1; - goto end; - } - - status = bytecode_push_get_symbol( - bytecode, bytecode_reloc, name); - free(name); - if (status) { - ERR("Failed to push 'get symbol %s:%s' in bytecode", - provider_name, type_name); - goto end; - } - - break; - } - case LTTNG_EVENT_EXPR_TYPE_ARRAY_FIELD_ELEMENT: - { - unsigned int index; - const struct lttng_event_expr *parent; - - parent = lttng_event_expr_array_field_element_get_parent_expr( - expr); - if (!parent) { - ERR("Failed to get parent expression from array event expression"); - status = -1; - goto end; - } - - status = event_expr_to_bytecode_recursive( - parent, bytecode, bytecode_reloc); - if (status) { - goto end; - } - - event_expr_status = lttng_event_expr_array_field_element_get_index( - expr, &index); - if (event_expr_status != LTTNG_EVENT_EXPR_STATUS_OK) { - ERR("Failed to get array field element index from event expression"); - status = -1; - goto end; - } - - status = bytecode_push_get_index_u64(bytecode, index); - if (status) { - ERR("Failed to push 'get index %u' in bytecode", index); - goto end; - } - - break; - } - default: - abort(); - } - - status = 0; -end: - return status; -} - -LTTNG_HIDDEN -int lttng_event_expr_to_bytecode(const struct lttng_event_expr *expr, - struct lttng_bytecode **bytecode_out) -{ - int status; - struct return_op ret_insn; - struct lttng_bytecode_alloc *bytecode = NULL; - struct lttng_bytecode_alloc *bytecode_reloc = NULL; - - status = bytecode_init(&bytecode); - if (status) { - ERR("Failed to initialize bytecode"); - goto end; - } - - status = bytecode_init(&bytecode_reloc); - if (status) { - ERR("Failed to initialize relocation bytecode"); - goto end; - } - - status = event_expr_to_bytecode_recursive( - expr, &bytecode, &bytecode_reloc); - if (status) { - /* Errors already logged. */ - goto end; - } - - ret_insn.op = BYTECODE_OP_RETURN; - bytecode_push(&bytecode, &ret_insn, 1, sizeof(ret_insn)); - - /* Append symbol table to bytecode. */ - bytecode->b.reloc_table_offset = bytecode_get_len(&bytecode->b); - status = bytecode_push(&bytecode, bytecode_reloc->b.data, 1, - bytecode_get_len(&bytecode_reloc->b)); - if (status) { - ERR("Failed to push symbol table to bytecode"); - goto end; - } - - /* Copy the `lttng_bytecode` out of the `lttng_bytecode_alloc`. */ - *bytecode_out = lttng_bytecode_copy(&bytecode->b); - if (!*bytecode_out) { - status = -1; - goto end; - } - -end: - if (bytecode) { - free(bytecode); - } - - if (bytecode_reloc) { - free(bytecode_reloc); - } - - return status; -} diff --git a/src/common/event-expr-to-bytecode.h b/src/common/event-expr-to-bytecode.h deleted file mode 100644 index 9d7d57aea..000000000 --- a/src/common/event-expr-to-bytecode.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef SRC_COMMON_EVENT_EXPR_TO_BYTECODE_H -#define SRC_COMMON_EVENT_EXPR_TO_BYTECODE_H - -/* - * Copyright 2020 EfficiOS, Inc. - * - * SPDX-License-Identifier: LGPL-2.1-only - * - */ - -#include - -struct lttng_bytecode; -struct lttng_event_expr; - -LTTNG_HIDDEN -int lttng_event_expr_to_bytecode (const struct lttng_event_expr *expr, - struct lttng_bytecode **bytecode_out); - -#endif /* SRC_COMMON_EVENT_EXPR_TO_BYTECODE_H */ diff --git a/src/common/event-expr/event-expr.c b/src/common/event-expr/event-expr.c index c0ec2571b..1351a347f 100644 --- a/src/common/event-expr/event-expr.c +++ b/src/common/event-expr/event-expr.c @@ -11,9 +11,12 @@ #include #include +#include #include #include #include +#include +#include enum lttng_event_expr_type lttng_event_expr_get_type( const struct lttng_event_expr *expr) @@ -440,3 +443,217 @@ void lttng_event_expr_destroy(struct lttng_event_expr *expr) end: return; } + +static int event_expr_to_bytecode_recursive(const struct lttng_event_expr *expr, + struct lttng_bytecode_alloc **bytecode, + struct lttng_bytecode_alloc **bytecode_reloc) +{ + int status; + enum lttng_event_expr_status event_expr_status; + + switch (lttng_event_expr_get_type(expr)) { + case LTTNG_EVENT_EXPR_TYPE_EVENT_PAYLOAD_FIELD: + { + const char *name; + + status = bytecode_push_get_payload_root(bytecode); + if (status) { + ERR("Failed to get payload root from bytecode"); + goto end; + } + + name = lttng_event_expr_event_payload_field_get_name(expr); + if (!name) { + ERR("Failed to get payload field name from event expression"); + status = -1; + goto end; + } + + status = bytecode_push_get_symbol( + bytecode, bytecode_reloc, name); + if (status) { + ERR("Failed to push 'get symbol %s' in bytecode", name); + goto end; + } + + break; + } + case LTTNG_EVENT_EXPR_TYPE_CHANNEL_CONTEXT_FIELD: + { + const char *name; + + status = bytecode_push_get_context_root(bytecode); + if (status) { + ERR("Failed to get context root from bytecode"); + goto end; + } + + name = lttng_event_expr_channel_context_field_get_name(expr); + if (!name) { + ERR("Failed to get channel context field name from event expression"); + status = -1; + goto end; + } + + status = bytecode_push_get_symbol( + bytecode, bytecode_reloc, name); + if (status) { + ERR("Failed to push 'get symbol %s' in bytecode", name); + goto end; + } + + break; + } + case LTTNG_EVENT_EXPR_TYPE_APP_SPECIFIC_CONTEXT_FIELD: + { + int ret; + char *name = NULL; + const char *provider_name, *type_name; + + status = bytecode_push_get_app_context_root(bytecode); + if (status) { + ERR("Failed to get application context root from bytecode"); + goto end; + } + + provider_name = lttng_event_expr_app_specific_context_field_get_provider_name( + expr); + if (!provider_name) { + ERR("Failed to get application context provider name from event expression"); + status = -1; + goto end; + } + + type_name = lttng_event_expr_app_specific_context_field_get_type_name( + expr); + if (!type_name) { + ERR("Failed to get application context type name from event expression"); + status = -1; + goto end; + } + + /* + * Reconstitute the app context field name from its two parts. + */ + ret = asprintf(&name, "%s:%s", provider_name, type_name); + if (ret < 0) { + PERROR("Failed to format application specific context: provider_name = '%s', type_name = '%s'", + provider_name, type_name); + status = -1; + goto end; + } + + status = bytecode_push_get_symbol( + bytecode, bytecode_reloc, name); + free(name); + if (status) { + ERR("Failed to push 'get symbol %s:%s' in bytecode", + provider_name, type_name); + goto end; + } + + break; + } + case LTTNG_EVENT_EXPR_TYPE_ARRAY_FIELD_ELEMENT: + { + unsigned int index; + const struct lttng_event_expr *parent; + + parent = lttng_event_expr_array_field_element_get_parent_expr( + expr); + if (!parent) { + ERR("Failed to get parent expression from array event expression"); + status = -1; + goto end; + } + + status = event_expr_to_bytecode_recursive( + parent, bytecode, bytecode_reloc); + if (status) { + goto end; + } + + event_expr_status = + lttng_event_expr_array_field_element_get_index( + expr, &index); + if (event_expr_status != LTTNG_EVENT_EXPR_STATUS_OK) { + ERR("Failed to get array field element index from event expression"); + status = -1; + goto end; + } + + status = bytecode_push_get_index_u64(bytecode, index); + if (status) { + ERR("Failed to push 'get index %u' in bytecode", index); + goto end; + } + + break; + } + default: + abort(); + } + + status = 0; +end: + return status; +} + +LTTNG_HIDDEN +int lttng_event_expr_to_bytecode(const struct lttng_event_expr *expr, + struct lttng_bytecode **bytecode_out) +{ + int status; + struct return_op ret_insn; + struct lttng_bytecode_alloc *bytecode = NULL; + struct lttng_bytecode_alloc *bytecode_reloc = NULL; + + status = bytecode_init(&bytecode); + if (status) { + ERR("Failed to initialize bytecode"); + goto end; + } + + status = bytecode_init(&bytecode_reloc); + if (status) { + ERR("Failed to initialize relocation bytecode"); + goto end; + } + + status = event_expr_to_bytecode_recursive( + expr, &bytecode, &bytecode_reloc); + if (status) { + /* Errors already logged. */ + goto end; + } + + ret_insn.op = BYTECODE_OP_RETURN; + bytecode_push(&bytecode, &ret_insn, 1, sizeof(ret_insn)); + + /* Append symbol table to bytecode. */ + bytecode->b.reloc_table_offset = bytecode_get_len(&bytecode->b); + status = bytecode_push(&bytecode, bytecode_reloc->b.data, 1, + bytecode_get_len(&bytecode_reloc->b)); + if (status) { + ERR("Failed to push symbol table to bytecode"); + goto end; + } + + /* Copy the `lttng_bytecode` out of the `lttng_bytecode_alloc`. */ + *bytecode_out = lttng_bytecode_copy(&bytecode->b); + if (!*bytecode_out) { + status = -1; + goto end; + } + +end: + if (bytecode) { + free(bytecode); + } + + if (bytecode_reloc) { + free(bytecode_reloc); + } + + return status; +} diff --git a/tests/unit/test_event_expr_to_bytecode.c b/tests/unit/test_event_expr_to_bytecode.c index dd26e66e6..fae7eb26e 100644 --- a/tests/unit/test_event_expr_to_bytecode.c +++ b/tests/unit/test_event_expr_to_bytecode.c @@ -5,9 +5,9 @@ * */ -#include -#include #include +#include +#include #include #define NR_TESTS 4 -- 2.34.1