Add base support for event rule hit
[lttng-tools.git] / src / common / event-rule / tracepoint.c
index 14e4c7b01901e9b7706ab5e3b83162ba82764337..a82151a254c6db6d5118754911fef55bfb55b8b4 100644 (file)
@@ -6,11 +6,14 @@
  */
 
 #include <assert.h>
+#include <common/credentials.h>
 #include <common/error.h>
 #include <common/macros.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 <lttng/event-rule/event-rule-internal.h>
 #include <lttng/event-rule/tracepoint-internal.h>
 
@@ -357,7 +360,8 @@ end:
 
 static enum lttng_error_code
 lttng_event_rule_tracepoint_generate_filter_bytecode(
-               struct lttng_event_rule *rule, uid_t uid, gid_t gid)
+               struct lttng_event_rule *rule,
+               const struct lttng_credentials *creds)
 {
        int ret;
        enum lttng_error_code ret_code;
@@ -429,7 +433,7 @@ lttng_event_rule_tracepoint_generate_filter_bytecode(
        }
 
        ret = run_as_generate_filter_bytecode(
-                       tracepoint->internal_filter.filter, uid, gid,
+                       tracepoint->internal_filter.filter, creds,
                        &bytecode);
        if (ret) {
                ret_code = LTTNG_ERR_FILTER_INVAL;
@@ -540,6 +544,48 @@ static void destroy_lttng_exclusions_element(void *ptr)
        free(ptr);
 }
 
+static unsigned long lttng_event_rule_tracepoint_hash(
+               const struct lttng_event_rule *rule)
+{
+       unsigned long hash;
+       unsigned int i, exclusion_count;
+       enum lttng_event_rule_status status;
+       struct lttng_event_rule_tracepoint *tp_rule =
+                       container_of(rule, typeof(*tp_rule), parent);
+
+       hash = hash_key_ulong((void *) LTTNG_EVENT_RULE_TYPE_TRACEPOINT,
+                       lttng_ht_seed);
+       hash ^= hash_key_ulong((void *) tp_rule->domain, 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);
+       }
+
+       hash ^= hash_key_ulong((void *) tp_rule->loglevel.type,
+                              lttng_ht_seed);
+       if (tp_rule->loglevel.type != LTTNG_EVENT_LOGLEVEL_ALL) {
+               hash ^= hash_key_ulong(
+                               (void *) (unsigned long) tp_rule->loglevel.value,
+                               lttng_ht_seed);
+       }
+
+       status = lttng_event_rule_tracepoint_get_exclusions_count(rule,
+                       &exclusion_count);
+       assert(status == LTTNG_EVENT_RULE_STATUS_OK);
+
+       for (i = 0; i < exclusion_count; i++) {
+               const char *exclusion;
+
+               status = lttng_event_rule_tracepoint_get_exclusion_at_index(
+                               rule, i, &exclusion);
+               assert(status == LTTNG_EVENT_RULE_STATUS_OK);
+               hash ^= hash_key_str(exclusion, lttng_ht_seed);
+       }
+
+       return hash;
+}
+
 struct lttng_event_rule *lttng_event_rule_tracepoint_create(
                enum lttng_domain_type domain_type)
 {
@@ -569,6 +615,7 @@ struct lttng_event_rule *lttng_event_rule_tracepoint_create(
                        lttng_event_rule_tracepoint_get_internal_filter_bytecode;
        tp_rule->parent.generate_exclusions =
                        lttng_event_rule_tracepoint_generate_exclusions;
+       tp_rule->parent.hash = lttng_event_rule_tracepoint_hash;
 
        tp_rule->domain = domain_type;
        tp_rule->loglevel.type = LTTNG_EVENT_LOGLEVEL_ALL;
@@ -603,21 +650,16 @@ ssize_t lttng_event_rule_tracepoint_create_from_payload(
                goto end;
        }
 
-       if (view->buffer.size < sizeof(*tracepoint_comm)) {
+       current_buffer_view = lttng_buffer_view_from_view(
+                       &view->buffer, offset, sizeof(*tracepoint_comm));
+       if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
                ERR("Failed to initialize from malformed event rule tracepoint: buffer too short to contain header.");
                ret = -1;
                goto end;
        }
 
-       current_buffer_view = lttng_buffer_view_from_view(
-                       &view->buffer, offset, sizeof(*tracepoint_comm));
        tracepoint_comm = (typeof(tracepoint_comm)) current_buffer_view.data;
 
-       if (!tracepoint_comm) {
-               ret = -1;
-               goto end;
-       }
-
        if (tracepoint_comm->domain_type <= LTTNG_DOMAIN_NONE ||
                        tracepoint_comm->domain_type > LTTNG_DOMAIN_PYTHON) {
                /* Invalid domain value. */
@@ -667,12 +709,13 @@ ssize_t lttng_event_rule_tracepoint_create_from_payload(
        /* Map the pattern. */
        current_buffer_view = lttng_buffer_view_from_view(
                        &view->buffer, offset, tracepoint_comm->pattern_len);
-       pattern = current_buffer_view.data;
-       if (!pattern) {
+
+       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,
                        tracepoint_comm->pattern_len)) {
                ret = -1;
@@ -689,12 +732,12 @@ ssize_t lttng_event_rule_tracepoint_create_from_payload(
        /* Map the filter_expression. */
        current_buffer_view = lttng_buffer_view_from_view(&view->buffer, offset,
                        tracepoint_comm->filter_expression_len);
-       filter_expression = current_buffer_view.data;
-       if (!filter_expression) {
+       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,
                        tracepoint_comm->filter_expression_len)) {
@@ -709,15 +752,21 @@ skip_filter_expression:
        for (i = 0; i < tracepoint_comm->exclusions_count; i++) {
                current_buffer_view = lttng_buffer_view_from_view(
                                &view->buffer, offset, sizeof(*exclusion_len));
-               exclusion_len = (typeof(exclusion_len)) current_buffer_view.data;
-               if (!exclusion_len) {
+               if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
                        ret = -1;
                        goto end;
                }
 
+               exclusion_len = (typeof(exclusion_len)) current_buffer_view.data;
                offset += sizeof(*exclusion_len);
+
                current_buffer_view = lttng_buffer_view_from_view(
                                &view->buffer, offset, *exclusion_len);
+               if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
+                       ret = -1;
+                       goto end;
+               }
+
                exclusion = current_buffer_view.data;
                if (!lttng_buffer_view_contains_string(&current_buffer_view,
                                exclusion, *exclusion_len)) {
This page took 0.034854 seconds and 4 git commands to generate.