event-rule: introduce event rule uprobe
authorJonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Wed, 11 Mar 2020 17:56:36 +0000 (13:56 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 11 Sep 2020 15:19:27 +0000 (11:19 -0400)
This is the "--userspace-probe" option of the enable-event command line.

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

include/Makefile.am
include/lttng/event-rule/uprobe-internal.h [new file with mode: 0644]
include/lttng/event-rule/uprobe.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/uprobe.c [new file with mode: 0644]

index 92b5c75bd26c4260b9fe47d2b16973e67988463f..b5875cd6686574b9d33efd5fc96df3005d9dc9e7 100644 (file)
@@ -146,7 +146,8 @@ lttngtriggerinclude_HEADERS= \
 lttngeventruleinclude_HEADERS= \
        lttng/event-rule/event-rule.h \
        lttng/event-rule/kprobe.h \
-       lttng/event-rule/syscall.h
+       lttng/event-rule/syscall.h \
+       lttng/event-rule/uprobe.h
 
 noinst_HEADERS = \
        lttng/snapshot-internal.h \
@@ -181,5 +182,6 @@ noinst_HEADERS = \
        lttng/event-rule/event-rule-internal.h \
        lttng/event-rule/kprobe-internal.h \
        lttng/event-rule/syscall-internal.h \
+       lttng/event-rule/uprobe-internal.h \
        version.h \
        version.i
diff --git a/include/lttng/event-rule/uprobe-internal.h b/include/lttng/event-rule/uprobe-internal.h
new file mode 100644 (file)
index 0000000..6d4b106
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ */
+
+#ifndef LTTNG_EVENT_RULE_UPROBE_INTERNAL_H
+#define LTTNG_EVENT_RULE_UPROBE_INTERNAL_H
+
+#include <common/payload-view.h>
+#include <common/macros.h>
+#include <lttng/event-rule/event-rule-internal.h>
+#include <lttng/event-rule/uprobe.h>
+
+struct lttng_event_rule_uprobe {
+       struct lttng_event_rule parent;
+       char *name;
+       struct lttng_userspace_probe_location *location;
+};
+
+struct lttng_event_rule_uprobe_comm {
+       /* Includes terminator `\0`. */
+       uint32_t name_len;
+       /* Includes terminator `\0`. */
+       uint32_t location_len;
+       /*
+        * Payload is composed of, in that order:
+        *   - name (null terminated),
+        *   - user space probe location object.
+        */
+       char payload[];
+} LTTNG_PACKED;
+
+LTTNG_HIDDEN
+ssize_t lttng_event_rule_uprobe_create_from_payload(
+               struct lttng_payload_view *view,
+               struct lttng_event_rule **rule);
+
+LTTNG_HIDDEN
+struct lttng_userspace_probe_location *
+lttng_event_rule_uprobe_get_location_mutable(
+               const struct lttng_event_rule *rule);
+
+#endif /* LTTNG_EVENT_RULE_UPROBE_INTERNAL_H */
diff --git a/include/lttng/event-rule/uprobe.h b/include/lttng/event-rule/uprobe.h
new file mode 100644 (file)
index 0000000..12aa013
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ */
+
+#ifndef LTTNG_EVENT_RULE_UPROBE_H
+#define LTTNG_EVENT_RULE_UPROBE_H
+
+#include <lttng/event-rule/event-rule.h>
+#include <lttng/userspace-probe.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Create a newly allocated uprobe event rule.
+ *
+ * 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_uprobe_create(void);
+
+/*
+ * Set the location of a uprobe event rule.
+ *
+ * The location is copied internally.
+ *
+ * Returns 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_uprobe_set_location(
+               struct lttng_event_rule *rule,
+               const struct lttng_userspace_probe_location *location);
+
+/*
+ * Get the location of a uprobe event rule.
+ *
+ * The caller does not assume the ownership of the returned location.
+ * The location shall only be used for the duration of the event
+ * rule's lifetime, or before a different location is set.
+ *
+ * Returns LTTNG_EVENT_RULE_STATUS_OK and a pointer to the event rule's location
+ * on success, LTTNG_EVENT_RULE_STATUS_INVALID if an invalid parameter is
+ * passed, or LTTNG_EVENT_RULE_STATUS_UNSET if a location was not set prior to
+ * this call.
+ */
+extern enum lttng_event_rule_status lttng_event_rule_uprobe_get_location(
+               const struct lttng_event_rule *rule,
+               const struct lttng_userspace_probe_location **location);
+
+/*
+ * Set the name of a uprobe event rule.
+ *
+ * The name 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_uprobe_set_name(
+               struct lttng_event_rule *rule, const char *name);
+
+/*
+ * Get the name of a uprobe event rule.
+ *
+ * The caller does not assume the ownership of the returned name.
+ * The name shall only only be used for the duration of the event
+ * rule's lifetime, or before a different name is set.
+ *
+ * Returns LTTNG_EVENT_RULE_STATUS_OK and a pointer to the event rule's name on
+ * success, LTTNG_EVENT_RULE_STATUS_INVALID if an invalid parameter is passed,
+ * or LTTNG_EVENT_RULE_STATUS_UNSET if a name was not set prior to this call.
+ */
+extern enum lttng_event_rule_status lttng_event_rule_uprobe_get_name(
+               const struct lttng_event_rule *rule, const char **name);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* LTTNG_EVENT_RULE_UPROBE_H */
index 8686643b8ae9044ac2034c3e13b23dc22771c9c6..c248eceb93aa83380db875d5ad403436a16e942f 100644 (file)
@@ -37,6 +37,7 @@
 #include <lttng/event-rule/event-rule.h>
 #include <lttng/event-rule/kprobe.h>
 #include <lttng/event-rule/syscall.h>
+#include <lttng/event-rule/uprobe.h>
 #include <lttng/event.h>
 #include <lttng/handle.h>
 #include <lttng/health.h>
index 556c557777bfd35dc1b3a50d65cf84311c30f0ef..7c2ab3e3a193dfa663827a54a8ba8f598cd8fc82 100644 (file)
@@ -51,6 +51,7 @@ libcommon_la_SOURCES = \
        event-rule/event-rule.c \
        event-rule/kprobe.c \
        event-rule/syscall.c \
+       event-rule/uprobe.c \
        filter.c filter.h \
        fd-handle.c fd-handle.h \
        fs-handle.c fs-handle.h fs-handle-internal.h \
index 7070115867d3fd91fb30ef1283e46133d6bf5f9f..8e9b003c8d39ab3ee1f0725ba518548029cc0f8e 100644 (file)
@@ -14,6 +14,7 @@
 #include <lttng/event-rule/event-rule-internal.h>
 #include <lttng/event-rule/kprobe-internal.h>
 #include <lttng/event-rule/syscall-internal.h>
+#include <lttng/event-rule/uprobe-internal.h>
 #include <stdbool.h>
 
 enum lttng_event_rule_type lttng_event_rule_get_type(
@@ -163,7 +164,7 @@ ssize_t lttng_event_rule_create_from_payload(
                /* TODO */
                break;
        case LTTNG_EVENT_RULE_TYPE_UPROBE:
-               /* TODO */
+               create_from_payload = lttng_event_rule_uprobe_create_from_payload;
                break;
        case LTTNG_EVENT_RULE_TYPE_SYSCALL:
                create_from_payload =
diff --git a/src/common/event-rule/uprobe.c b/src/common/event-rule/uprobe.c
new file mode 100644 (file)
index 0000000..8d63975
--- /dev/null
@@ -0,0 +1,410 @@
+/*
+ * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ */
+
+#include <assert.h>
+#include <common/error.h>
+#include <common/macros.h>
+#include <common/payload.h>
+#include <common/payload-view.h>
+#include <common/runas.h>
+#include <lttng/event-rule/event-rule-internal.h>
+#include <lttng/event-rule/uprobe-internal.h>
+#include <lttng/userspace-probe-internal.h>
+
+#define IS_UPROBE_EVENT_RULE(rule) \
+       (lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_UPROBE)
+
+static void lttng_event_rule_uprobe_destroy(struct lttng_event_rule *rule)
+{
+       struct lttng_event_rule_uprobe *uprobe;
+
+       uprobe = container_of(rule, struct lttng_event_rule_uprobe, parent);
+
+       lttng_userspace_probe_location_destroy(uprobe->location);
+       free(uprobe->name);
+       free(uprobe);
+}
+
+static bool lttng_event_rule_uprobe_validate(
+               const struct lttng_event_rule *rule)
+{
+       bool valid = false;
+       struct lttng_event_rule_uprobe *uprobe;
+
+       if (!rule) {
+               goto end;
+       }
+
+       uprobe = container_of(rule, struct lttng_event_rule_uprobe, parent);
+
+       /* Required field. */
+       if (!uprobe->name) {
+               ERR("Invalid uprobe event rule: a pattern must be set.");
+               goto end;
+       }
+
+       if (!uprobe->location) {
+               ERR("Invalid uprobe event rule: a location must be set.");
+               goto end;
+       }
+
+       valid = true;
+end:
+       return valid;
+}
+
+static int lttng_event_rule_uprobe_serialize(
+               const struct lttng_event_rule *rule,
+               struct lttng_payload *payload)
+{
+       int ret;
+       size_t name_len, header_offset, size_before_probe;
+       struct lttng_event_rule_uprobe *uprobe;
+       struct lttng_event_rule_uprobe_comm uprobe_comm = {};
+       struct lttng_event_rule_uprobe_comm *header;
+
+       if (!rule || !IS_UPROBE_EVENT_RULE(rule)) {
+               ret = -1;
+               goto end;
+       }
+
+       header_offset = payload->buffer.size;
+
+       DBG("Serializing uprobe event rule.");
+       uprobe = container_of(rule, struct lttng_event_rule_uprobe, parent);
+
+       name_len = strlen(uprobe->name) + 1;
+
+       uprobe_comm.name_len = name_len;
+
+       ret = lttng_dynamic_buffer_append(
+                       &payload->buffer, &uprobe_comm, sizeof(uprobe_comm));
+       if (ret) {
+               goto end;
+       }
+       ret = lttng_dynamic_buffer_append(
+                       &payload->buffer, uprobe->name, name_len);
+       if (ret) {
+               goto end;
+       }
+
+       size_before_probe = payload->buffer.size;
+
+       /* This serialize return the size taken in the buffer. */
+       ret = lttng_userspace_probe_location_serialize(
+                       uprobe->location, payload);
+       if (ret < 0) {
+               goto end;
+       }
+
+       /* Update the header regarding the probe size. */
+       header = (struct lttng_event_rule_uprobe_comm
+                                       *) ((char *) payload->buffer.data +
+                       header_offset);
+       header->location_len = payload->buffer.size - size_before_probe;
+
+       ret = 0;
+
+end:
+       return ret;
+}
+
+static bool lttng_event_rule_uprobe_is_equal(const struct lttng_event_rule *_a,
+               const struct lttng_event_rule *_b)
+{
+       bool is_equal = false;
+       struct lttng_event_rule_uprobe *a, *b;
+
+       a = container_of(_a, struct lttng_event_rule_uprobe, parent);
+       b = container_of(_b, struct lttng_event_rule_uprobe, parent);
+
+       /* uprobe is invalid if this is not true. */
+       assert(a->name);
+       assert(b->name);
+       if (strcmp(a->name, b->name)) {
+               goto end;
+       }
+
+       assert(a->location);
+       assert(b->location);
+       is_equal = lttng_userspace_probe_location_is_equal(
+                       a->location, b->location);
+end:
+       return is_equal;
+}
+
+static enum lttng_error_code lttng_event_rule_uprobe_generate_filter_bytecode(
+               struct lttng_event_rule *rule, uid_t uid, gid_t gid)
+{
+       /* Nothing to do. */
+       return LTTNG_OK;
+}
+
+static const char *lttng_event_rule_uprobe_get_filter(
+               const struct lttng_event_rule *rule)
+{
+       /* Unsupported. */
+       return NULL;
+}
+
+static const struct lttng_filter_bytecode *
+lttng_event_rule_uprobe_get_filter_bytecode(const struct lttng_event_rule *rule)
+{
+       /* Unsupported. */
+       return NULL;
+}
+
+static struct lttng_event_exclusion *
+lttng_event_rule_uprobe_generate_exclusions(const struct lttng_event_rule *rule)
+{
+       /* Unsupported. */
+       return NULL;
+}
+
+struct lttng_event_rule *lttng_event_rule_uprobe_create()
+{
+       struct lttng_event_rule *rule = NULL;
+       struct lttng_event_rule_uprobe *urule;
+
+       urule = zmalloc(sizeof(struct lttng_event_rule_uprobe));
+       if (!urule) {
+               goto end;
+       }
+
+       rule = &urule->parent;
+       lttng_event_rule_init(&urule->parent, LTTNG_EVENT_RULE_TYPE_UPROBE);
+       urule->parent.validate = lttng_event_rule_uprobe_validate;
+       urule->parent.serialize = lttng_event_rule_uprobe_serialize;
+       urule->parent.equal = lttng_event_rule_uprobe_is_equal;
+       urule->parent.destroy = lttng_event_rule_uprobe_destroy;
+       urule->parent.generate_filter_bytecode =
+                       lttng_event_rule_uprobe_generate_filter_bytecode;
+       urule->parent.get_filter = lttng_event_rule_uprobe_get_filter;
+       urule->parent.get_filter_bytecode =
+                       lttng_event_rule_uprobe_get_filter_bytecode;
+       urule->parent.generate_exclusions =
+                       lttng_event_rule_uprobe_generate_exclusions;
+end:
+       return rule;
+}
+
+LTTNG_HIDDEN
+ssize_t lttng_event_rule_uprobe_create_from_payload(
+               struct lttng_payload_view *view,
+               struct lttng_event_rule **_event_rule)
+{
+       ssize_t ret, offset = 0;
+       const struct lttng_event_rule_uprobe_comm *uprobe_comm;
+       const char *name;
+       struct lttng_buffer_view current_buffer_view;
+       struct lttng_event_rule *rule = NULL;
+       struct lttng_userspace_probe_location *location;
+       struct lttng_event_rule_uprobe *uprobe;
+       enum lttng_event_rule_status status;
+
+       if (!_event_rule) {
+               ret = -1;
+               goto end;
+       }
+
+       if (view->buffer.size < sizeof(*uprobe_comm)) {
+               ERR("Failed to initialize from malformed event rule uprobe: buffer too short to contain header.");
+               ret = -1;
+               goto end;
+       }
+
+       current_buffer_view = lttng_buffer_view_from_view(
+                       &view->buffer, offset, sizeof(*uprobe_comm));
+       uprobe_comm = (typeof(uprobe_comm)) current_buffer_view.data;
+
+       if (!uprobe_comm) {
+               ret = -1;
+               goto end;
+       }
+
+       rule = lttng_event_rule_uprobe_create();
+       if (!rule) {
+               ERR("Failed to create event rule uprobe.");
+               ret = -1;
+               goto end;
+       }
+
+       /* Skip to payload. */
+       offset += current_buffer_view.size;
+
+       /* Map the name. */
+       current_buffer_view = lttng_buffer_view_from_view(
+                       &view->buffer, offset, uprobe_comm->name_len);
+       name = current_buffer_view.data;
+       if (!name) {
+               ret = -1;
+               goto end;
+       }
+
+       if (!lttng_buffer_view_contains_string(&current_buffer_view, name,
+                       uprobe_comm->name_len)) {
+               ret = -1;
+               goto end;
+       }
+
+       /* Skip after the name. */
+       offset += uprobe_comm->name_len;
+
+       /* Map the location. */
+       struct lttng_payload_view current_payload_view =
+                       lttng_payload_view_from_view(view, offset,
+                                       uprobe_comm->location_len);
+       ret = lttng_userspace_probe_location_create_from_payload(
+                       &current_payload_view, &location);
+       if (ret < 0) {
+               ret = -1;
+               goto end;
+       }
+
+       assert(ret == uprobe_comm->location_len);
+
+       /* Skip after the location. */
+       offset += uprobe_comm->location_len;
+
+       uprobe = container_of(rule, struct lttng_event_rule_uprobe, parent);
+       uprobe->location = location;
+
+       status = lttng_event_rule_uprobe_set_name(rule, name);
+       if (status != LTTNG_EVENT_RULE_STATUS_OK) {
+               ret = -1;
+               goto end;
+       }
+
+       if (!lttng_event_rule_uprobe_validate(rule)) {
+               ret = -1;
+               goto end;
+       }
+
+       *_event_rule = rule;
+       rule = NULL;
+       ret = offset;
+end:
+       lttng_event_rule_destroy(rule);
+       return ret;
+}
+
+enum lttng_event_rule_status lttng_event_rule_uprobe_set_location(
+               struct lttng_event_rule *rule,
+               const struct lttng_userspace_probe_location *location)
+{
+       struct lttng_userspace_probe_location *location_copy = NULL;
+       struct lttng_event_rule_uprobe *uprobe;
+       enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
+
+       if (!rule || !IS_UPROBE_EVENT_RULE(rule) || !location) {
+               status = LTTNG_EVENT_RULE_STATUS_INVALID;
+               goto end;
+       }
+
+       uprobe = container_of(rule, struct lttng_event_rule_uprobe, parent);
+       location_copy = lttng_userspace_probe_location_copy(location);
+       if (!location_copy) {
+               status = LTTNG_EVENT_RULE_STATUS_ERROR;
+               goto end;
+       }
+
+       if (uprobe->location) {
+               lttng_userspace_probe_location_destroy(uprobe->location);
+       }
+
+       uprobe->location = location_copy;
+       location_copy = NULL;
+end:
+       lttng_userspace_probe_location_destroy(location_copy);
+       return status;
+}
+
+enum lttng_event_rule_status lttng_event_rule_uprobe_get_location(
+               const struct lttng_event_rule *rule,
+               const struct lttng_userspace_probe_location **location)
+{
+       enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
+
+       if (!rule || !IS_UPROBE_EVENT_RULE(rule) || !location) {
+               status = LTTNG_EVENT_RULE_STATUS_INVALID;
+               goto end;
+       }
+
+       *location = lttng_event_rule_uprobe_get_location_mutable(rule);
+       if (!*location) {
+               status = LTTNG_EVENT_RULE_STATUS_UNSET;
+               goto end;
+       }
+
+end:
+       return status;
+}
+
+LTTNG_HIDDEN
+struct lttng_userspace_probe_location *
+lttng_event_rule_uprobe_get_location_mutable(
+               const struct lttng_event_rule *rule)
+{
+       struct lttng_event_rule_uprobe *uprobe;
+
+       assert(rule);
+       uprobe = container_of(rule, struct lttng_event_rule_uprobe, parent);
+
+       return uprobe->location;
+}
+
+enum lttng_event_rule_status lttng_event_rule_uprobe_set_name(
+               struct lttng_event_rule *rule, const char *name)
+{
+       char *name_copy = NULL;
+       struct lttng_event_rule_uprobe *uprobe;
+       enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
+
+       if (!rule || !IS_UPROBE_EVENT_RULE(rule) || !name ||
+                       strlen(name) == 0) {
+               status = LTTNG_EVENT_RULE_STATUS_INVALID;
+               goto end;
+       }
+
+       uprobe = container_of(rule, struct lttng_event_rule_uprobe, parent);
+       name_copy = strdup(name);
+       if (!name_copy) {
+               status = LTTNG_EVENT_RULE_STATUS_ERROR;
+               goto end;
+       }
+
+       if (uprobe->name) {
+               free(uprobe->name);
+       }
+
+       uprobe->name = name_copy;
+       name_copy = NULL;
+end:
+       return status;
+}
+
+enum lttng_event_rule_status lttng_event_rule_uprobe_get_name(
+               const struct lttng_event_rule *rule, const char **name)
+{
+       struct lttng_event_rule_uprobe *uprobe;
+       enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
+
+       if (!rule || !IS_UPROBE_EVENT_RULE(rule) || !name) {
+               status = LTTNG_EVENT_RULE_STATUS_INVALID;
+               goto end;
+       }
+
+       uprobe = container_of(rule, struct lttng_event_rule_uprobe, parent);
+       if (!uprobe->name) {
+               status = LTTNG_EVENT_RULE_STATUS_UNSET;
+               goto end;
+       }
+
+       *name = uprobe->name;
+end:
+       return status;
+}
This page took 0.032444 seconds and 4 git commands to generate.