Introduce lttng_event_rule_jul_logging
authorJonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Wed, 12 May 2021 03:38:17 +0000 (23:38 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 11 Jun 2021 20:51:57 +0000 (16:51 -0400)
A following patch will remove lttng_event_rule_tracepoint.

Signed-off-by: Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
Change-Id: I95f40920b73b5deb42c2e92ad50c43dba0b0e436

include/Makefile.am
include/lttng/event-rule/event-rule.h
include/lttng/event-rule/jul-logging-internal.h [new file with mode: 0644]
include/lttng/event-rule/jul-logging.h [new file with mode: 0644]
include/lttng/lttng.h
src/common/Makefile.am
src/common/event-rule/event-rule.c
src/common/event-rule/jul-logging.c [new file with mode: 0644]
tests/unit/test_event_rule.c

index a22803cf38327135cc7c122438b47dee98d954f9..05ec828727ca1428c6078b4dfa0ac7a08f931c4d 100644 (file)
@@ -152,6 +152,7 @@ lttngtriggerinclude_HEADERS= \
 
 lttngeventruleinclude_HEADERS= \
        lttng/event-rule/event-rule.h \
+       lttng/event-rule/jul-logging.h \
        lttng/event-rule/kernel-kprobe.h \
        lttng/event-rule/kernel-syscall.h \
        lttng/event-rule/tracepoint.h \
@@ -183,6 +184,7 @@ noinst_HEADERS = \
        lttng/event-field-value-internal.h \
        lttng/event-internal.h \
        lttng/event-rule/event-rule-internal.h \
+       lttng/event-rule/jul-logging-internal.h \
        lttng/event-rule/kernel-kprobe-internal.h \
        lttng/event-rule/kernel-syscall-internal.h \
        lttng/event-rule/tracepoint-internal.h \
index d8856efd99e02daf8c979cce2e660b1aa5ea7ff9..d403608eaf15455b6a77ce59bacc7c289a40cf7c 100644 (file)
@@ -23,6 +23,7 @@ enum lttng_event_rule_type {
        LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT = 4,
        LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE = 5,
        LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT = 6,
+       LTTNG_EVENT_RULE_TYPE_JUL_LOGGING = 7,
 };
 
 enum lttng_event_rule_status {
diff --git a/include/lttng/event-rule/jul-logging-internal.h b/include/lttng/event-rule/jul-logging-internal.h
new file mode 100644 (file)
index 0000000..b1f6ee5
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2021 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ */
+
+#ifndef LTTNG_EVENT_RULE_JUL_LOGGING_INTERNAL_H
+#define LTTNG_EVENT_RULE_JUL_LOGGING_INTERNAL_H
+
+#include <common/payload-view.h>
+#include <common/macros.h>
+#include <common/optional.h>
+#include <lttng/event-rule/event-rule-internal.h>
+#include <lttng/event-rule/jul-logging.h>
+#include <lttng/event.h>
+#include <lttng/log-level-rule-internal.h>
+
+struct lttng_event_rule_jul_logging {
+       struct lttng_event_rule parent;
+
+       /* Name pattern. */
+       char *pattern;
+
+       /* Filter. */
+       char *filter_expression;
+
+       /* Log level. */
+       struct lttng_log_level_rule *log_level_rule;
+
+       /* internal use only. */
+       struct {
+               char *filter;
+               struct lttng_bytecode *bytecode;
+       } internal_filter;
+};
+
+struct lttng_event_rule_jul_logging_comm {
+       /* Includes terminator `\0`. */
+       uint32_t pattern_len;
+       /* Includes terminator `\0`. */
+       uint32_t filter_expression_len;
+       /*  enum lttng_log_level_rule_comm + payload if any */
+       uint32_t log_level_rule_len;
+       /*
+        * Payload is composed of, in that order:
+        *   - pattern (null terminated),
+        *   - filter expression (null terminated),
+        *   - log level rule serialized object,
+        */
+       char payload[];
+} LTTNG_PACKED;
+
+LTTNG_HIDDEN
+ssize_t lttng_event_rule_jul_logging_create_from_payload(
+               struct lttng_payload_view *view,
+               struct lttng_event_rule **rule);
+
+#endif /* LTTNG_EVENT_RULE_JUL_LOGGING_INTERNAL_H */
diff --git a/include/lttng/event-rule/jul-logging.h b/include/lttng/event-rule/jul-logging.h
new file mode 100644 (file)
index 0000000..943adde
--- /dev/null
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2021 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ */
+
+#ifndef LTTNG_EVENT_RULE_JUL_LOGGING_H
+#define LTTNG_EVENT_RULE_JUL_LOGGING_H
+
+#include <lttng/event-rule/event-rule.h>
+#include <lttng/log-level-rule.h>
+#include <lttng/event.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Create a newly allocated jul logging event rule.
+ *
+ * The default name pattern is '*'.
+ *
+ * Returns a new event rule on success, NULL on failure. This event rule must be
+ * destroyed using lttng_event_rule_destroy().
+ */
+extern struct lttng_event_rule *lttng_event_rule_jul_logging_create(void);
+
+/*
+ * Set the name pattern of a jul logging event rule.
+ *
+ * Pattern can contain wildcard '*'. See man lttng-enable-event.
+ *
+ * Return LTTNG_EVENT_RULE_STATUS_OK on success, LTTNG_EVENT_RULE_STATUS_INVALID
+ * if invalid parameters are passed.
+ */
+extern enum lttng_event_rule_status lttng_event_rule_jul_logging_set_name_pattern(
+               struct lttng_event_rule *rule, const char *pattern);
+
+/*
+ * Get the name pattern of a jul logging event rule.
+ *
+ * The caller does not assume the ownership of the returned pattern. The
+ * pattern shall only only be used for the duration of the event rule's
+ * lifetime, or before a different pattern is set.
+ *
+ * Returns LTTNG_EVENT_RULE_STATUS_OK and a pointer to the event rule's pattern
+ * on success, LTTNG_EVENT_RULE_STATUS_INVALID if an invalid
+ * parameter is passed, or LTTNG_EVENT_RULE_STATUS_UNSET if a pattern
+ * was not set prior to this call.
+ */
+extern enum lttng_event_rule_status lttng_event_rule_jul_logging_get_name_pattern(
+               const struct lttng_event_rule *rule, const char **pattern);
+
+/*
+ * Set the filter expression of a jul logging event rule.
+ *
+ * The expression is copied internally.
+ *
+ * Return LTTNG_EVENT_RULE_STATUS_OK on success, LTTNG_EVENT_RULE_STATUS_INVALID
+ * if invalid parameters are passed.
+ */
+extern enum lttng_event_rule_status lttng_event_rule_jul_logging_set_filter(
+               struct lttng_event_rule *rule, const char *expression);
+
+/*
+ * Get the filter expression of a jul logging event rule.
+ *
+ * The caller does not assume the ownership of the returned filter expression.
+ * The filter expression shall only only be used for the duration of the event
+ * rule's lifetime, or before a different filter expression is set.
+ *
+ * Returns LTTNG_EVENT_RULE_STATUS_OK and a pointer to the event rule's filter
+ * expression on success, LTTNG_EVENT_RULE_STATUS_INVALID if an invalid
+ * parameter is passed, or LTTNG_EVENT_RULE_STATUS_UNSET if a filter expression
+ * was not set prior to this call.
+ */
+extern enum lttng_event_rule_status lttng_event_rule_jul_logging_get_filter(
+               const struct lttng_event_rule *rule, const char **expression);
+
+/*
+ * Set the log level rule of a jul logging event rule.
+ *
+ * The log level rule is copied internally.
+ *
+ * Return LTTNG_EVENT_RULE_STATUS_OK on success, LTTNG_EVENT_RULE_STATUS_INVALID
+ * if invalid parameters are passed.
+ */
+extern enum lttng_event_rule_status
+lttng_event_rule_jul_logging_set_log_level_rule(struct lttng_event_rule *rule,
+               const struct lttng_log_level_rule *log_level_rule);
+
+/*
+ * Get the log level rule of a jul logging event rule.
+ *
+ * The caller does not assume the ownership of the returned log level rule. The
+ * log level rule shall only only be used for the duration of the event rule's
+ * lifetime, or before a different log level rule is set.
+ *
+ * Returns LTTNG_EVENT_RULE_STATUS_OK and sets the log level rule output
+ * parameter on success, LTTNG_EVENT_RULE_STATUS_INVALID if an invalid parameter
+ * is passed, or LTTNG_EVENT_RULE_STATUS_UNSET if a log level rule was not set prior
+ * to this call.
+ */
+extern enum lttng_event_rule_status
+lttng_event_rule_jul_logging_get_log_level_rule(
+               const struct lttng_event_rule *rule,
+               const struct lttng_log_level_rule **log_level_rule);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* LTTNG_EVENT_RULE_JUL_LOGGING_H */
index 22d9d5669786230e4181526d4e5020a5ecb4c654..a849d6b539800cece9e25b90ccccdf2c0abe0656 100644 (file)
@@ -42,6 +42,7 @@
 #include <lttng/event-expr.h>
 #include <lttng/event-field-value.h>
 #include <lttng/event-rule/event-rule.h>
+#include <lttng/event-rule/jul-logging.h>
 #include <lttng/event-rule/kernel-kprobe.h>
 #include <lttng/event-rule/kernel-syscall.h>
 #include <lttng/event-rule/tracepoint.h>
index d10d8ba994278dc5f8d685fc25e7e2099f73db98..b07ac04a1f932b82a7a7953548a965dc5426dfa7 100644 (file)
@@ -71,6 +71,7 @@ libcommon_la_SOURCES = \
        event-rule/tracepoint.c \
        event-rule/kernel-tracepoint.c \
        event-rule/user-tracepoint.c \
+       event-rule/jul-logging.c \
        filter.c filter.h \
        fd-handle.c fd-handle.h \
        fs-handle.c fs-handle.h fs-handle-internal.h \
index a664ee0e851037e17add207c7b2191bc07983231..8c68d62b88d8444738d867acc7bd5cb7b18deb27 100644 (file)
@@ -16,6 +16,7 @@
 #include <lttng/event-rule/event-rule-internal.h>
 #include <lttng/event-rule/kernel-kprobe-internal.h>
 #include <lttng/event-rule/kernel-syscall-internal.h>
+#include <lttng/event-rule/jul-logging-internal.h>
 #include <lttng/event-rule/tracepoint-internal.h>
 #include <lttng/event-rule/kernel-tracepoint-internal.h>
 #include <lttng/event-rule/kernel-uprobe-internal.h>
@@ -45,6 +46,9 @@ enum lttng_domain_type lttng_event_rule_get_domain_type(
        case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT:
                domain_type = LTTNG_DOMAIN_UST;
                break;
+       case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
+               domain_type = LTTNG_DOMAIN_JUL;
+               break;
        case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL:
        case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE:
        case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION:
@@ -199,6 +203,10 @@ ssize_t lttng_event_rule_create_from_payload(
                create_from_payload =
                                lttng_event_rule_user_tracepoint_create_from_payload;
                break;
+       case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
+               create_from_payload =
+                               lttng_event_rule_jul_logging_create_from_payload;
+               break;
        default:
                ERR("Attempted to create event rule of unknown type (%i)",
                                (int) event_rule_comm->event_rule_type);
@@ -339,6 +347,8 @@ const char *lttng_event_rule_type_str(enum lttng_event_rule_type type)
                return "kernel tracepoint";
        case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT:
                return "user tracepoint";
+       case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
+               return "jul logging";
        default:
                abort();
        }
diff --git a/src/common/event-rule/jul-logging.c b/src/common/event-rule/jul-logging.c
new file mode 100644 (file)
index 0000000..5888359
--- /dev/null
@@ -0,0 +1,845 @@
+/*
+ * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ */
+
+#include <assert.h>
+#include <common/credentials.h>
+#include <common/error.h>
+#include <common/macros.h>
+#include <common/optional.h>
+#include <common/payload.h>
+#include <common/payload-view.h>
+#include <common/runas.h>
+#include <common/hashtable/hashtable.h>
+#include <common/hashtable/utils.h>
+#include <common/string-utils/string-utils.h>
+#include <lttng/event-rule/event-rule-internal.h>
+#include <lttng/event-rule/jul-logging-internal.h>
+#include <lttng/log-level-rule.h>
+#include <lttng/event.h>
+
+#define IS_JUL_LOGGING_EVENT_RULE(rule) \
+       (lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_JUL_LOGGING)
+
+static void lttng_event_rule_jul_logging_destroy(struct lttng_event_rule *rule)
+{
+       struct lttng_event_rule_jul_logging *jul_logging;
+
+       if (rule == NULL) {
+               return;
+       }
+
+       jul_logging = container_of(
+                       rule, struct lttng_event_rule_jul_logging, parent);
+
+       lttng_log_level_rule_destroy(jul_logging->log_level_rule);
+       free(jul_logging->pattern);
+       free(jul_logging->filter_expression);
+       free(jul_logging->internal_filter.filter);
+       free(jul_logging->internal_filter.bytecode);
+       free(jul_logging);
+}
+
+static bool lttng_event_rule_jul_logging_validate(
+               const struct lttng_event_rule *rule)
+{
+       bool valid = false;
+       struct lttng_event_rule_jul_logging *jul_logging;
+
+       if (!rule) {
+               goto end;
+       }
+
+       jul_logging = container_of(
+                       rule, struct lttng_event_rule_jul_logging, parent);
+
+       /* Required field. */
+       if (!jul_logging->pattern) {
+               ERR("Invalid jul_logging event rule: a pattern must be set.");
+               goto end;
+       }
+
+       valid = true;
+end:
+       return valid;
+}
+
+static int lttng_event_rule_jul_logging_serialize(
+               const struct lttng_event_rule *rule,
+               struct lttng_payload *payload)
+{
+       int ret;
+       size_t pattern_len, filter_expression_len, header_offset;
+       size_t size_before_log_level_rule;
+       struct lttng_event_rule_jul_logging *jul_logging;
+       struct lttng_event_rule_jul_logging_comm jul_logging_comm;
+       struct lttng_event_rule_jul_logging_comm *header;
+
+       if (!rule || !IS_JUL_LOGGING_EVENT_RULE(rule)) {
+               ret = -1;
+               goto end;
+       }
+
+       header_offset = payload->buffer.size;
+
+       DBG("Serializing jul_logging event rule.");
+       jul_logging = container_of(
+                       rule, struct lttng_event_rule_jul_logging, parent);
+
+       pattern_len = strlen(jul_logging->pattern) + 1;
+
+       if (jul_logging->filter_expression != NULL) {
+               filter_expression_len =
+                               strlen(jul_logging->filter_expression) + 1;
+       } else {
+               filter_expression_len = 0;
+       }
+
+       jul_logging_comm.pattern_len = pattern_len;
+       jul_logging_comm.filter_expression_len = filter_expression_len;
+
+       ret = lttng_dynamic_buffer_append(&payload->buffer, &jul_logging_comm,
+                       sizeof(jul_logging_comm));
+       if (ret) {
+               goto end;
+       }
+
+       ret = lttng_dynamic_buffer_append(
+                       &payload->buffer, jul_logging->pattern, pattern_len);
+       if (ret) {
+               goto end;
+       }
+
+       ret = lttng_dynamic_buffer_append(&payload->buffer, jul_logging->filter_expression,
+                       filter_expression_len);
+       if (ret) {
+               goto end;
+       }
+
+       size_before_log_level_rule = payload->buffer.size;
+
+       ret = lttng_log_level_rule_serialize(jul_logging->log_level_rule, payload);
+       if (ret < 0) {
+               goto end;
+       }
+
+       header = (typeof(header)) ((char *) payload->buffer.data + header_offset);
+       header->log_level_rule_len =
+                       payload->buffer.size - size_before_log_level_rule;
+
+end:
+       return ret;
+}
+
+static bool lttng_event_rule_jul_logging_is_equal(
+               const struct lttng_event_rule *_a,
+               const struct lttng_event_rule *_b)
+{
+       bool is_equal = false;
+       struct lttng_event_rule_jul_logging *a, *b;
+
+       a = container_of(_a, struct lttng_event_rule_jul_logging, parent);
+       b = container_of(_b, struct lttng_event_rule_jul_logging, parent);
+
+       /* Quick checks. */
+
+       if (!!a->filter_expression != !!b->filter_expression) {
+               goto end;
+       }
+
+       /* Long check. */
+       assert(a->pattern);
+       assert(b->pattern);
+       if (strcmp(a->pattern, b->pattern)) {
+               goto end;
+       }
+
+       if (a->filter_expression && b->filter_expression) {
+               if (strcmp(a->filter_expression, b->filter_expression)) {
+                       goto end;
+               }
+       } else if (!!a->filter_expression != !!b->filter_expression) {
+               /* One is set; not the other. */
+               goto end;
+       }
+
+       if (!lttng_log_level_rule_is_equal(
+                               a->log_level_rule, b->log_level_rule)) {
+               goto end;
+       }
+
+       is_equal = true;
+end:
+       return is_equal;
+}
+
+/*
+ * On success ret is 0;
+ *
+ * On error ret is negative.
+ *
+ * An event with NO loglevel and the name is * will return NULL.
+ */
+static int generate_agent_filter(
+               const struct lttng_event_rule *rule, char **_agent_filter)
+{
+       int err;
+       int ret = 0;
+       char *agent_filter = NULL;
+       const char *pattern;
+       const char *filter;
+       const struct lttng_log_level_rule *log_level_rule = NULL;
+       enum lttng_event_rule_status status;
+
+       assert(rule);
+       assert(_agent_filter);
+
+       status = lttng_event_rule_jul_logging_get_name_pattern(rule, &pattern);
+       if (status != LTTNG_EVENT_RULE_STATUS_OK) {
+               ret = -1;
+               goto end;
+       }
+
+       status = lttng_event_rule_jul_logging_get_filter(rule, &filter);
+       if (status == LTTNG_EVENT_RULE_STATUS_UNSET) {
+               filter = NULL;
+       } else if (status != LTTNG_EVENT_RULE_STATUS_OK) {
+               ret = -1;
+               goto end;
+       }
+
+
+       /* Don't add filter for the '*' event. */
+       if (strcmp(pattern, "*") != 0) {
+               if (filter) {
+                       err = asprintf(&agent_filter,
+                                       "(%s) && (logger_name == \"%s\")",
+                                       filter, pattern);
+               } else {
+                       err = asprintf(&agent_filter, "logger_name == \"%s\"",
+                                       pattern);
+               }
+
+               if (err < 0) {
+                       PERROR("Failed to format agent filter string");
+                       ret = -1;
+                       goto end;
+               }
+       }
+
+       status = lttng_event_rule_jul_logging_get_log_level_rule(
+                       rule, &log_level_rule);
+       if (status == LTTNG_EVENT_RULE_STATUS_OK) {
+               enum lttng_log_level_rule_status llr_status;
+               const char *op;
+               int level;
+
+               switch (lttng_log_level_rule_get_type(log_level_rule))
+               {
+               case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY:
+                       llr_status = lttng_log_level_rule_exactly_get_level(
+                                       log_level_rule, &level);
+                       op = "==";
+                       break;
+               case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS:
+                       llr_status = lttng_log_level_rule_at_least_as_severe_as_get_level(
+                                       log_level_rule, &level);
+                       op = ">=";
+                       break;
+               default:
+                       abort();
+               }
+
+               if (llr_status != LTTNG_LOG_LEVEL_RULE_STATUS_OK) {
+                       ret = -1;
+                       goto end;
+               }
+
+               if (filter || agent_filter) {
+                       char *new_filter;
+
+                       err = asprintf(&new_filter,
+                                       "(%s) && (int_loglevel %s %d)",
+                                       agent_filter ? agent_filter : filter,
+                                       op, level);
+                       if (agent_filter) {
+                               free(agent_filter);
+                       }
+                       agent_filter = new_filter;
+               } else {
+                       err = asprintf(&agent_filter, "int_loglevel %s %d", op,
+                                       level);
+               }
+
+               if (err < 0) {
+                       PERROR("Failed to format agent filter string");
+                       ret = -1;
+                       goto end;
+               }
+       }
+
+       *_agent_filter = agent_filter;
+       agent_filter = NULL;
+
+end:
+       free(agent_filter);
+       return ret;
+}
+
+static enum lttng_error_code
+lttng_event_rule_jul_logging_generate_filter_bytecode(
+               struct lttng_event_rule *rule,
+               const struct lttng_credentials *creds)
+{
+       int ret;
+       enum lttng_error_code ret_code;
+       struct lttng_event_rule_jul_logging *jul_logging;
+       enum lttng_event_rule_status status;
+       const char *filter;
+       struct lttng_bytecode *bytecode = NULL;
+       char *agent_filter;
+
+       assert(rule);
+
+       jul_logging = container_of(
+                       rule, struct lttng_event_rule_jul_logging, parent);
+
+       status = lttng_event_rule_jul_logging_get_filter(rule, &filter);
+       if (status == LTTNG_EVENT_RULE_STATUS_UNSET) {
+               filter = NULL;
+       } else if (status != LTTNG_EVENT_RULE_STATUS_OK) {
+               ret_code = LTTNG_ERR_FILTER_INVAL;
+               goto end;
+       }
+
+       if (filter && filter[0] == '\0') {
+               ret_code = LTTNG_ERR_FILTER_INVAL;
+               goto error;
+       }
+
+       ret = generate_agent_filter(rule, &agent_filter);
+       if (ret) {
+               ret_code = LTTNG_ERR_FILTER_INVAL;
+               goto error;
+       }
+
+       jul_logging->internal_filter.filter = agent_filter;
+
+       if (jul_logging->internal_filter.filter == NULL) {
+               ret_code = LTTNG_OK;
+               goto end;
+       }
+
+       ret = run_as_generate_filter_bytecode(
+                       jul_logging->internal_filter.filter, creds,
+                       &bytecode);
+       if (ret) {
+               ret_code = LTTNG_ERR_FILTER_INVAL;
+               goto end;
+       }
+
+       jul_logging->internal_filter.bytecode = bytecode;
+       bytecode = NULL;
+       ret_code = LTTNG_OK;
+
+error:
+end:
+       free(bytecode);
+       return ret_code;
+}
+
+static const char *lttng_event_rule_jul_logging_get_internal_filter(
+               const struct lttng_event_rule *rule)
+{
+       struct lttng_event_rule_jul_logging *jul_logging;
+
+       assert(rule);
+       jul_logging = container_of(
+                       rule, struct lttng_event_rule_jul_logging, parent);
+       return jul_logging->internal_filter.filter;
+}
+
+static const struct lttng_bytecode *
+lttng_event_rule_jul_logging_get_internal_filter_bytecode(
+               const struct lttng_event_rule *rule)
+{
+       struct lttng_event_rule_jul_logging *jul_logging;
+
+       assert(rule);
+       jul_logging = container_of(
+                       rule, struct lttng_event_rule_jul_logging, parent);
+       return jul_logging->internal_filter.bytecode;
+}
+
+static enum lttng_event_rule_generate_exclusions_status
+lttng_event_rule_jul_logging_generate_exclusions(
+               const struct lttng_event_rule *rule,
+               struct lttng_event_exclusion **_exclusions)
+{
+       /* Unsupported. */
+       *_exclusions = NULL;
+       return LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE;
+}
+
+static unsigned long lttng_event_rule_jul_logging_hash(
+               const struct lttng_event_rule *rule)
+{
+       unsigned long hash;
+       struct lttng_event_rule_jul_logging *tp_rule =
+                       container_of(rule, typeof(*tp_rule), parent);
+
+       hash = hash_key_ulong((void *) LTTNG_EVENT_RULE_TYPE_JUL_LOGGING,
+                       lttng_ht_seed);
+       hash ^= hash_key_str(tp_rule->pattern, lttng_ht_seed);
+
+       if (tp_rule->filter_expression) {
+               hash ^= hash_key_str(tp_rule->filter_expression, lttng_ht_seed);
+       }
+
+       if (tp_rule->log_level_rule) {
+               hash ^= lttng_log_level_rule_hash(tp_rule->log_level_rule);
+       }
+
+       return hash;
+}
+
+static struct lttng_event *lttng_event_rule_jul_logging_generate_lttng_event(
+               const struct lttng_event_rule *rule)
+{
+       int ret;
+       const struct lttng_event_rule_jul_logging *jul_logging;
+       struct lttng_event *local_event = NULL;
+       struct lttng_event *event = NULL;
+       enum lttng_loglevel_type loglevel_type;
+       int loglevel_value = 0;
+       enum lttng_event_rule_status status;
+       const struct lttng_log_level_rule *log_level_rule;
+
+       jul_logging = container_of(
+                       rule, const struct lttng_event_rule_jul_logging, parent);
+
+       local_event = zmalloc(sizeof(*local_event));
+       if (!local_event) {
+               goto error;
+       }
+
+       local_event->type = LTTNG_EVENT_TRACEPOINT;
+       ret = lttng_strncpy(local_event->name, jul_logging->pattern,
+                           sizeof(local_event->name));
+       if (ret) {
+               ERR("Truncation occurred when copying event rule pattern to `lttng_event` structure: pattern = '%s'",
+                               jul_logging->pattern);
+               goto error;
+       }
+
+
+       /* Map the log level rule to an equivalent lttng_loglevel. */
+       status = lttng_event_rule_jul_logging_get_log_level_rule(
+                       rule, &log_level_rule);
+       if (status == LTTNG_EVENT_RULE_STATUS_UNSET) {
+               loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
+               loglevel_value = 0;
+       } else if (status == LTTNG_EVENT_RULE_STATUS_OK) {
+               enum lttng_log_level_rule_status llr_status;
+
+               switch (lttng_log_level_rule_get_type(log_level_rule)) {
+               case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY:
+                       llr_status = lttng_log_level_rule_exactly_get_level(
+                                       log_level_rule, &loglevel_value);
+                       loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
+                       break;
+               case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS:
+                       llr_status = lttng_log_level_rule_at_least_as_severe_as_get_level(
+                                       log_level_rule, &loglevel_value);
+                       loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
+                       break;
+               default:
+                       abort();
+                       break;
+               }
+
+               if (llr_status != LTTNG_LOG_LEVEL_RULE_STATUS_OK) {
+                       goto error;
+               }
+       } else {
+               goto error;
+       }
+
+       local_event->loglevel_type = loglevel_type;
+       local_event->loglevel = loglevel_value;
+
+       event = local_event;
+       local_event = NULL;
+error:
+       free(local_event);
+       return event;
+}
+
+struct lttng_event_rule *lttng_event_rule_jul_logging_create(void)
+{
+       struct lttng_event_rule *rule = NULL;
+       struct lttng_event_rule_jul_logging *tp_rule;
+       enum lttng_event_rule_status status;
+
+       tp_rule = zmalloc(sizeof(struct lttng_event_rule_jul_logging));
+       if (!tp_rule) {
+               goto end;
+       }
+
+       rule = &tp_rule->parent;
+       lttng_event_rule_init(&tp_rule->parent, LTTNG_EVENT_RULE_TYPE_JUL_LOGGING);
+       tp_rule->parent.validate = lttng_event_rule_jul_logging_validate;
+       tp_rule->parent.serialize = lttng_event_rule_jul_logging_serialize;
+       tp_rule->parent.equal = lttng_event_rule_jul_logging_is_equal;
+       tp_rule->parent.destroy = lttng_event_rule_jul_logging_destroy;
+       tp_rule->parent.generate_filter_bytecode =
+                       lttng_event_rule_jul_logging_generate_filter_bytecode;
+       tp_rule->parent.get_filter =
+                       lttng_event_rule_jul_logging_get_internal_filter;
+       tp_rule->parent.get_filter_bytecode =
+                       lttng_event_rule_jul_logging_get_internal_filter_bytecode;
+       tp_rule->parent.generate_exclusions =
+                       lttng_event_rule_jul_logging_generate_exclusions;
+       tp_rule->parent.hash = lttng_event_rule_jul_logging_hash;
+       tp_rule->parent.generate_lttng_event =
+                       lttng_event_rule_jul_logging_generate_lttng_event;
+
+       tp_rule->log_level_rule = NULL;
+
+       /* Default pattern is '*'. */
+       status = lttng_event_rule_jul_logging_set_name_pattern(rule, "*");
+       if (status != LTTNG_EVENT_RULE_STATUS_OK) {
+               lttng_event_rule_destroy(rule);
+               rule = NULL;
+       }
+
+end:
+       return rule;
+}
+
+LTTNG_HIDDEN
+ssize_t lttng_event_rule_jul_logging_create_from_payload(
+               struct lttng_payload_view *view,
+               struct lttng_event_rule **_event_rule)
+{
+       ssize_t ret, offset = 0;
+       enum lttng_event_rule_status status;
+       const struct lttng_event_rule_jul_logging_comm *jul_logging_comm;
+       const char *pattern;
+       const char *filter_expression = NULL;
+       struct lttng_buffer_view current_buffer_view;
+       struct lttng_event_rule *rule = NULL;
+       struct lttng_log_level_rule *log_level_rule = NULL;
+
+       if (!_event_rule) {
+               ret = -1;
+               goto end;
+       }
+
+       current_buffer_view = lttng_buffer_view_from_view(
+                       &view->buffer, offset, sizeof(*jul_logging_comm));
+       if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
+               ERR("Failed to initialize from malformed event rule jul_logging: buffer too short to contain header.");
+               ret = -1;
+               goto end;
+       }
+
+       jul_logging_comm = (typeof(jul_logging_comm)) current_buffer_view.data;
+
+       rule = lttng_event_rule_jul_logging_create();
+       if (!rule) {
+               ERR("Failed to create event rule jul_logging.");
+               ret = -1;
+               goto end;
+       }
+
+       /* Skip to payload. */
+       offset += current_buffer_view.size;
+
+       /* Map the pattern. */
+       current_buffer_view = lttng_buffer_view_from_view(
+                       &view->buffer, offset, jul_logging_comm->pattern_len);
+
+       if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
+               ret = -1;
+               goto end;
+       }
+
+       pattern = current_buffer_view.data;
+       if (!lttng_buffer_view_contains_string(&current_buffer_view, pattern,
+                       jul_logging_comm->pattern_len)) {
+               ret = -1;
+               goto end;
+       }
+
+       /* Skip after the pattern. */
+       offset += jul_logging_comm->pattern_len;
+
+       if (!jul_logging_comm->filter_expression_len) {
+               goto skip_filter_expression;
+       }
+
+       /* Map the filter_expression. */
+       current_buffer_view = lttng_buffer_view_from_view(&view->buffer, offset,
+                       jul_logging_comm->filter_expression_len);
+       if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
+               ret = -1;
+               goto end;
+       }
+
+       filter_expression = current_buffer_view.data;
+       if (!lttng_buffer_view_contains_string(&current_buffer_view,
+                       filter_expression,
+                       jul_logging_comm->filter_expression_len)) {
+               ret = -1;
+               goto end;
+       }
+
+       /* Skip after the pattern. */
+       offset += jul_logging_comm->filter_expression_len;
+
+skip_filter_expression:
+       if (!jul_logging_comm->log_level_rule_len) {
+               goto skip_log_level_rule;
+       }
+
+       {
+               /* Map the log level rule. */
+               struct lttng_payload_view current_payload_view =
+                               lttng_payload_view_from_view(view, offset,
+                                               jul_logging_comm->log_level_rule_len);
+
+               ret = lttng_log_level_rule_create_from_payload(
+                               &current_payload_view, &log_level_rule);
+               if (ret < 0) {
+                       ret = -1;
+                       goto end;
+               }
+
+               assert(ret == jul_logging_comm->log_level_rule_len);
+       }
+
+       /* Skip after the log level rule. */
+       offset += jul_logging_comm->log_level_rule_len;
+
+skip_log_level_rule:
+
+       status = lttng_event_rule_jul_logging_set_name_pattern(rule, pattern);
+       if (status != LTTNG_EVENT_RULE_STATUS_OK) {
+               ERR("Failed to set event rule jul_logging pattern.");
+               ret = -1;
+               goto end;
+       }
+
+       if (filter_expression) {
+               status = lttng_event_rule_jul_logging_set_filter(
+                               rule, filter_expression);
+               if (status != LTTNG_EVENT_RULE_STATUS_OK) {
+                       ERR("Failed to set event rule jul_logging pattern.");
+                       ret = -1;
+                       goto end;
+               }
+       }
+
+       if (log_level_rule) {
+               status = lttng_event_rule_jul_logging_set_log_level_rule(
+                               rule, log_level_rule);
+               if (status != LTTNG_EVENT_RULE_STATUS_OK) {
+                       ERR("Failed to set event rule jul_logging log level rule.");
+                       ret = -1;
+                       goto end;
+               }
+       }
+
+       *_event_rule = rule;
+       rule = NULL;
+       ret = offset;
+end:
+       lttng_log_level_rule_destroy(log_level_rule);
+       lttng_event_rule_destroy(rule);
+       return ret;
+}
+
+enum lttng_event_rule_status lttng_event_rule_jul_logging_set_name_pattern(
+               struct lttng_event_rule *rule, const char *pattern)
+{
+       char *pattern_copy = NULL;
+       struct lttng_event_rule_jul_logging *jul_logging;
+       enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
+
+       if (!rule || !IS_JUL_LOGGING_EVENT_RULE(rule) || !pattern ||
+                       strlen(pattern) == 0) {
+               status = LTTNG_EVENT_RULE_STATUS_INVALID;
+               goto end;
+       }
+
+       jul_logging = container_of(
+                       rule, struct lttng_event_rule_jul_logging, parent);
+       pattern_copy = strdup(pattern);
+       if (!pattern_copy) {
+               status = LTTNG_EVENT_RULE_STATUS_ERROR;
+               goto end;
+       }
+
+       /* Normalize the pattern. */
+       strutils_normalize_star_glob_pattern(pattern_copy);
+
+       free(jul_logging->pattern);
+
+       jul_logging->pattern = pattern_copy;
+       pattern_copy = NULL;
+end:
+       return status;
+}
+
+enum lttng_event_rule_status lttng_event_rule_jul_logging_get_name_pattern(
+               const struct lttng_event_rule *rule, const char **pattern)
+{
+       struct lttng_event_rule_jul_logging *jul_logging;
+       enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
+
+       if (!rule || !IS_JUL_LOGGING_EVENT_RULE(rule) || !pattern) {
+               status = LTTNG_EVENT_RULE_STATUS_INVALID;
+               goto end;
+       }
+
+       jul_logging = container_of(
+                       rule, struct lttng_event_rule_jul_logging, parent);
+       if (!jul_logging->pattern) {
+               status = LTTNG_EVENT_RULE_STATUS_UNSET;
+               goto end;
+       }
+
+       *pattern = jul_logging->pattern;
+end:
+       return status;
+}
+
+enum lttng_event_rule_status lttng_event_rule_jul_logging_set_filter(
+               struct lttng_event_rule *rule, const char *expression)
+{
+       char *expression_copy = NULL;
+       struct lttng_event_rule_jul_logging *jul_logging;
+       enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
+
+       if (!rule || !IS_JUL_LOGGING_EVENT_RULE(rule) || !expression ||
+                       strlen(expression) == 0) {
+               status = LTTNG_EVENT_RULE_STATUS_INVALID;
+               goto end;
+       }
+
+       jul_logging = container_of(
+                       rule, struct lttng_event_rule_jul_logging, parent);
+       expression_copy = strdup(expression);
+       if (!expression_copy) {
+               PERROR("Failed to copy filter expression");
+               status = LTTNG_EVENT_RULE_STATUS_ERROR;
+               goto end;
+       }
+
+       if (jul_logging->filter_expression) {
+               free(jul_logging->filter_expression);
+       }
+
+       jul_logging->filter_expression = expression_copy;
+       expression_copy = NULL;
+end:
+       return status;
+}
+
+enum lttng_event_rule_status lttng_event_rule_jul_logging_get_filter(
+               const struct lttng_event_rule *rule, const char **expression)
+{
+       struct lttng_event_rule_jul_logging *jul_logging;
+       enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
+
+       if (!rule || !IS_JUL_LOGGING_EVENT_RULE(rule) || !expression) {
+               status = LTTNG_EVENT_RULE_STATUS_INVALID;
+               goto end;
+       }
+
+       jul_logging = container_of(
+                       rule, struct lttng_event_rule_jul_logging, parent);
+       if (!jul_logging->filter_expression) {
+               status = LTTNG_EVENT_RULE_STATUS_UNSET;
+               goto end;
+       }
+
+       *expression = jul_logging->filter_expression;
+end:
+       return status;
+}
+
+static bool log_level_rule_valid(const struct lttng_log_level_rule *rule)
+{
+       /*
+        * For both JUL and LOG4J custom log level are possible and can
+        * span the entire int32 range.
+        */
+       return true;
+}
+
+enum lttng_event_rule_status lttng_event_rule_jul_logging_set_log_level_rule(
+               struct lttng_event_rule *rule,
+               const struct lttng_log_level_rule *log_level_rule)
+{
+       struct lttng_event_rule_jul_logging *jul_logging;
+       enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
+       struct lttng_log_level_rule *copy = NULL;
+
+       if (!rule || !IS_JUL_LOGGING_EVENT_RULE(rule)) {
+               status = LTTNG_EVENT_RULE_STATUS_INVALID;
+               goto end;
+       }
+
+       jul_logging = container_of(
+                       rule, struct lttng_event_rule_jul_logging, parent);
+
+       if (!log_level_rule_valid(log_level_rule)) {
+               status = LTTNG_EVENT_RULE_STATUS_INVALID;
+               goto end;
+       }
+
+       copy = lttng_log_level_rule_copy(log_level_rule);
+       if (copy == NULL) {
+               status = LTTNG_EVENT_RULE_STATUS_ERROR;
+               goto end;
+       }
+
+       if (jul_logging->log_level_rule) {
+               lttng_log_level_rule_destroy(jul_logging->log_level_rule);
+       }
+
+       jul_logging->log_level_rule = copy;
+
+end:
+       return status;
+}
+
+enum lttng_event_rule_status lttng_event_rule_jul_logging_get_log_level_rule(
+               const struct lttng_event_rule *rule,
+               const struct lttng_log_level_rule **log_level_rule
+               )
+{
+       struct lttng_event_rule_jul_logging *jul_logging;
+       enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
+
+       if (!rule || !IS_JUL_LOGGING_EVENT_RULE(rule) || !log_level_rule) {
+               status = LTTNG_EVENT_RULE_STATUS_INVALID;
+               goto end;
+       }
+
+       jul_logging = container_of(
+                       rule, struct lttng_event_rule_jul_logging, parent);
+       if (jul_logging->log_level_rule == NULL) {
+               status = LTTNG_EVENT_RULE_STATUS_UNSET;
+               goto end;
+       }
+
+       *log_level_rule = jul_logging->log_level_rule;
+end:
+       return status;
+}
index e6a55bc49d3c0973ef4b5b9767b27e45d5c106a6..e0adb133db3b60c848b4df6596450cd4c54e4a18 100644 (file)
@@ -18,6 +18,8 @@
 #include <common/payload-view.h>
 #include <common/payload.h>
 #include <lttng/domain.h>
+#include <lttng/event-rule/jul-logging-internal.h>
+#include <lttng/event-rule/jul-logging.h>
 #include <lttng/event-rule/kernel-kprobe-internal.h>
 #include <lttng/event-rule/kernel-kprobe.h>
 #include <lttng/event-rule/kernel-syscall-internal.h>
@@ -42,7 +44,7 @@ int lttng_opt_quiet = 1;
 int lttng_opt_verbose;
 int lttng_opt_mi;
 
-#define NUM_TESTS 278
+#define NUM_TESTS 291
 
 struct tracepoint_test {
        enum lttng_domain_type type;
@@ -366,6 +368,71 @@ static void test_event_rule_syscall(void)
        lttng_event_rule_destroy(syscall_from_buffer);
 }
 
+static
+void test_event_rule_jul_logging(void)
+{
+       struct lttng_event_rule *jul_logging = NULL;
+       struct lttng_event_rule *jul_logging_from_buffer = NULL;
+       enum lttng_event_rule_status status;
+       const char *pattern="my_event_*";
+       const char *filter="msg_id == 23 && size >= 2048";
+       const char *tmp;
+       struct lttng_log_level_rule *log_level_rule = NULL;
+       const struct lttng_log_level_rule *log_level_rule_return = NULL;
+       struct lttng_payload payload;
+
+       diag("Testing lttng_event_rule_user_jul_logging.");
+
+       lttng_payload_init(&payload);
+
+       log_level_rule = lttng_log_level_rule_exactly_create(LTTNG_LOGLEVEL_INFO);
+       assert(log_level_rule);
+
+       jul_logging = lttng_event_rule_jul_logging_create();
+       ok(jul_logging, "jul_logging object.");
+
+       status = lttng_event_rule_jul_logging_set_name_pattern(jul_logging, pattern);
+       ok(status == LTTNG_EVENT_RULE_STATUS_OK, "setting pattern.");
+       status = lttng_event_rule_jul_logging_get_name_pattern(jul_logging, &tmp);
+       ok(status == LTTNG_EVENT_RULE_STATUS_OK, "getting pattern.");
+       ok(!strncmp(pattern, tmp, strlen(pattern)), "pattern is equal.");
+
+       status = lttng_event_rule_jul_logging_set_filter(jul_logging, filter);
+       ok(status == LTTNG_EVENT_RULE_STATUS_OK, "setting filter.");
+       status = lttng_event_rule_jul_logging_get_filter(jul_logging, &tmp);
+       ok(status == LTTNG_EVENT_RULE_STATUS_OK, "getting filter.");
+       ok(!strncmp(filter, tmp, strlen(filter)), "filter is equal.");
+
+       status = lttng_event_rule_jul_logging_get_log_level_rule(jul_logging, &log_level_rule_return);
+       ok(status == LTTNG_EVENT_RULE_STATUS_UNSET, "get unset log level rule.");
+
+       status = lttng_event_rule_jul_logging_set_log_level_rule(
+                       jul_logging, log_level_rule);
+       ok(status == LTTNG_EVENT_RULE_STATUS_OK, "setting log level rule.");
+       status = lttng_event_rule_jul_logging_get_log_level_rule(
+                       jul_logging, &log_level_rule_return);
+       ok(status == LTTNG_EVENT_RULE_STATUS_OK, "get log level rule.");
+
+       ok(lttng_event_rule_serialize(jul_logging, &payload) == 0, "Serializing.");
+
+       {
+               struct lttng_payload_view view =
+                               lttng_payload_view_from_payload(
+                                               &payload, 0, -1);
+
+               ok(lttng_event_rule_create_from_payload(
+                               &view, &jul_logging_from_buffer) > 0,
+                               "Deserializing.");
+       }
+
+       ok(lttng_event_rule_is_equal(jul_logging, jul_logging_from_buffer), "serialized and from buffer are equal.");
+
+       lttng_payload_reset(&payload);
+       lttng_event_rule_destroy(jul_logging);
+       lttng_event_rule_destroy(jul_logging_from_buffer);
+       lttng_log_level_rule_destroy(log_level_rule);
+}
+
 static void test_event_rule_userspace_probe(void)
 {
        struct lttng_event_rule *uprobe = NULL;
@@ -726,6 +793,7 @@ int main(int argc, const char *argv[])
        test_event_rule_syscall();
        test_event_rule_userspace_probe();
        test_event_rule_kernel_probe();
+       test_event_rule_jul_logging();
        test_event_rule_log_level_ust();
        test_event_rule_log_level_jul();
        test_event_rule_log_level_log4j();
This page took 0.041015 seconds and 4 git commands to generate.