From: Jonathan Rajotte Date: Wed, 4 Nov 2020 20:45:31 +0000 (-0500) Subject: Rename kprobe file to kernel-probe X-Git-Tag: v2.13.0-rc1~223 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=a3c516531ffff69b8d5f6564c8e4d00b4f18e7da;p=lttng-tools.git Rename kprobe file to kernel-probe Signed-off-by: Jonathan Rajotte Signed-off-by: Jérémie Galarneau Change-Id: I379bd8d88b4fa4a7d5638dc5bee4d7dd86426dc9 --- diff --git a/include/Makefile.am b/include/Makefile.am index b75602b27..dc09f385f 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -148,7 +148,7 @@ lttngtriggerinclude_HEADERS= \ lttngeventruleinclude_HEADERS= \ lttng/event-rule/event-rule.h \ - lttng/event-rule/kprobe.h \ + lttng/event-rule/kernel-probe.h \ lttng/event-rule/syscall.h \ lttng/event-rule/uprobe.h \ lttng/event-rule/tracepoint.h @@ -188,7 +188,7 @@ noinst_HEADERS = \ lttng/session-descriptor-internal.h \ lttng/kernel-probe-internal.h \ lttng/event-rule/event-rule-internal.h \ - lttng/event-rule/kprobe-internal.h \ + lttng/event-rule/kernel-probe-internal.h \ lttng/event-rule/syscall-internal.h \ lttng/event-rule/uprobe-internal.h \ lttng/event-rule/tracepoint-internal.h \ diff --git a/include/lttng/event-rule/kernel-probe-internal.h b/include/lttng/event-rule/kernel-probe-internal.h new file mode 100644 index 000000000..50c02d851 --- /dev/null +++ b/include/lttng/event-rule/kernel-probe-internal.h @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2019 Jonathan Rajotte + * + * SPDX-License-Identifier: LGPL-2.1-only + * + */ + +#ifndef LTTNG_EVENT_RULE_KERNEL_PROBE_INTERNAL_H +#define LTTNG_EVENT_RULE_KERNEL_PROBE_INTERNAL_H + +#include +#include +#include +#include + +struct lttng_event_rule_kernel_probe { + struct lttng_event_rule parent; + char *name; + struct lttng_kernel_probe_location *location; +}; + +struct lttng_event_rule_kernel_probe_comm { + /* Includes terminator `\0`. */ + uint32_t name_len; + uint32_t location_len; + /* + * Payload is composed of, in that order: + * - name (null terminated), + * - kernel probe location object. + */ + char payload[]; +} LTTNG_PACKED; + +LTTNG_HIDDEN +ssize_t lttng_event_rule_kernel_probe_create_from_payload( + struct lttng_payload_view *payload, + struct lttng_event_rule **rule); + +#endif /* LTTNG_EVENT_RULE_KERNEL_PROBE_INTERNAL_H */ diff --git a/include/lttng/event-rule/kernel-probe.h b/include/lttng/event-rule/kernel-probe.h new file mode 100644 index 000000000..2ac173d27 --- /dev/null +++ b/include/lttng/event-rule/kernel-probe.h @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2019 Jonathan Rajotte + * + * SPDX-License-Identifier: LGPL-2.1-only + * + */ + +#ifndef LTTNG_EVENT_RULE_KERNEL_PROBE_H +#define LTTNG_EVENT_RULE_KERNEL_PROBE_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct lttng_kernel_probe_location; + +/* + * Create a newly allocated kernel probe event rule. + * + * Returns a new event rule on success, NULL on failure. The returned event rule + * must be destroyed using lttng_event_rule_destroy(). + */ +extern struct lttng_event_rule *lttng_event_rule_kernel_probe_create(void); + +/* + * Set the kernel probe location of a kernel probe 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_kernel_probe_set_location( + struct lttng_event_rule *rule, + const struct lttng_kernel_probe_location *location); + +/* + * Get the kernel probe location of a kernel probe 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_kernel_probe_get_location( + const struct lttng_event_rule *rule, + const struct lttng_kernel_probe_location **location); + +/* + * Set the name of a kernel probe event rule. + * + * The name 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_kernel_probe_set_name( + struct lttng_event_rule *rule, const char *name); + +/* + * Get the name of a kernel probe 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_kernel_probe_get_name( + const struct lttng_event_rule *rule, const char **name); + +#ifdef __cplusplus +} +#endif + +#endif /* LTTNG_EVENT_RULE_KERNEL_PROBE_H */ diff --git a/include/lttng/event-rule/kprobe-internal.h b/include/lttng/event-rule/kprobe-internal.h deleted file mode 100644 index 3799af955..000000000 --- a/include/lttng/event-rule/kprobe-internal.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (C) 2019 Jonathan Rajotte - * - * SPDX-License-Identifier: LGPL-2.1-only - * - */ - -#ifndef LTTNG_EVENT_RULE_KERNEL_PROBE_INTERNAL_H -#define LTTNG_EVENT_RULE_KERNEL_PROBE_INTERNAL_H - -#include -#include -#include -#include - -struct lttng_event_rule_kernel_probe { - struct lttng_event_rule parent; - char *name; - struct lttng_kernel_probe_location *location; -}; - -struct lttng_event_rule_kernel_probe_comm { - /* Includes terminator `\0`. */ - uint32_t name_len; - uint32_t location_len; - /* - * Payload is composed of, in that order: - * - name (null terminated), - * - kernel probe location object. - */ - char payload[]; -} LTTNG_PACKED; - -LTTNG_HIDDEN -ssize_t lttng_event_rule_kernel_probe_create_from_payload( - struct lttng_payload_view *payload, - struct lttng_event_rule **rule); - -#endif /* LTTNG_EVENT_RULE_KERNEL_PROBE_INTERNAL_H */ diff --git a/include/lttng/event-rule/kprobe.h b/include/lttng/event-rule/kprobe.h deleted file mode 100644 index 2ac173d27..000000000 --- a/include/lttng/event-rule/kprobe.h +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright (C) 2019 Jonathan Rajotte - * - * SPDX-License-Identifier: LGPL-2.1-only - * - */ - -#ifndef LTTNG_EVENT_RULE_KERNEL_PROBE_H -#define LTTNG_EVENT_RULE_KERNEL_PROBE_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -struct lttng_kernel_probe_location; - -/* - * Create a newly allocated kernel probe event rule. - * - * Returns a new event rule on success, NULL on failure. The returned event rule - * must be destroyed using lttng_event_rule_destroy(). - */ -extern struct lttng_event_rule *lttng_event_rule_kernel_probe_create(void); - -/* - * Set the kernel probe location of a kernel probe 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_kernel_probe_set_location( - struct lttng_event_rule *rule, - const struct lttng_kernel_probe_location *location); - -/* - * Get the kernel probe location of a kernel probe 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_kernel_probe_get_location( - const struct lttng_event_rule *rule, - const struct lttng_kernel_probe_location **location); - -/* - * Set the name of a kernel probe event rule. - * - * The name 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_kernel_probe_set_name( - struct lttng_event_rule *rule, const char *name); - -/* - * Get the name of a kernel probe 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_kernel_probe_get_name( - const struct lttng_event_rule *rule, const char **name); - -#ifdef __cplusplus -} -#endif - -#endif /* LTTNG_EVENT_RULE_KERNEL_PROBE_H */ diff --git a/include/lttng/lttng.h b/include/lttng/lttng.h index c665d3c9b..bc5a83d48 100644 --- a/include/lttng/lttng.h +++ b/include/lttng/lttng.h @@ -40,7 +40,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/bin/lttng-sessiond/trace-kernel.c b/src/bin/lttng-sessiond/trace-kernel.c index a9e8e03c3..72de7eadd 100644 --- a/src/bin/lttng-sessiond/trace-kernel.c +++ b/src/bin/lttng-sessiond/trace-kernel.c @@ -18,8 +18,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include diff --git a/src/common/Makefile.am b/src/common/Makefile.am index 4b549aa08..3c076c8ff 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -62,7 +62,7 @@ libcommon_la_SOURCES = \ event-expr-to-bytecode.c event-expr-to-bytecode.h \ event-field-value.c \ event-rule/event-rule.c \ - event-rule/kprobe.c \ + event-rule/kernel-probe.c \ event-rule/syscall.c \ event-rule/uprobe.c \ event-rule/tracepoint.c \ diff --git a/src/common/event-rule/event-rule.c b/src/common/event-rule/event-rule.c index c6ccc8577..88b9d5bc5 100644 --- a/src/common/event-rule/event-rule.c +++ b/src/common/event-rule/event-rule.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/common/event-rule/kernel-probe.c b/src/common/event-rule/kernel-probe.c new file mode 100644 index 000000000..6152a1b50 --- /dev/null +++ b/src/common/event-rule/kernel-probe.c @@ -0,0 +1,437 @@ +/* + * Copyright (C) 2019 Jonathan Rajotte + * + * SPDX-License-Identifier: LGPL-2.1-only + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define IS_KPROBE_EVENT_RULE(rule) \ + (lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE) + +#if (LTTNG_SYMBOL_NAME_LEN == 256) +#define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255" +#endif + +static void lttng_event_rule_kernel_probe_destroy(struct lttng_event_rule *rule) +{ + struct lttng_event_rule_kernel_probe *kprobe; + + kprobe = container_of(rule, struct lttng_event_rule_kernel_probe, parent); + + lttng_kernel_probe_location_destroy(kprobe->location); + free(kprobe->name); + free(kprobe); +} + +static bool lttng_event_rule_kernel_probe_validate( + const struct lttng_event_rule *rule) +{ + bool valid = false; + struct lttng_event_rule_kernel_probe *kprobe; + + if (!rule) { + goto end; + } + + kprobe = container_of(rule, struct lttng_event_rule_kernel_probe, parent); + + /* Required field. */ + if (!kprobe->name) { + ERR("Invalid name event rule: a name must be set."); + goto end; + } + + /* Required field. */ + if(!kprobe->location) { + ERR("Invalid name event rule: a location must be set."); + goto end; + } + + valid = true; +end: + return valid; +} + +static int lttng_event_rule_kernel_probe_serialize( + const struct lttng_event_rule *rule, + struct lttng_payload *payload) +{ + int ret; + size_t name_len, header_offset, size_before_location; + struct lttng_event_rule_kernel_probe *kprobe; + struct lttng_event_rule_kernel_probe_comm kprobe_comm; + struct lttng_event_rule_kernel_probe_comm *header; + + if (!rule || !IS_KPROBE_EVENT_RULE(rule)) { + ret = -1; + goto end; + } + + header_offset = payload->buffer.size; + + DBG("Serializing kprobe event rule."); + kprobe = container_of(rule, struct lttng_event_rule_kernel_probe, parent); + + name_len = strlen(kprobe->name) + 1; + kprobe_comm.name_len = name_len; + + ret = lttng_dynamic_buffer_append( + &payload->buffer, &kprobe_comm, sizeof(kprobe_comm)); + if (ret) { + goto end; + } + + ret = lttng_dynamic_buffer_append(&payload->buffer, kprobe->name, name_len); + if (ret) { + goto end; + } + + size_before_location = payload->buffer.size; + + ret = lttng_kernel_probe_location_serialize(kprobe->location, payload); + if (ret < 0) { + goto end; + } + + /* Update the header regarding the probe size. */ + header = (struct lttng_event_rule_kernel_probe_comm*) ( + (char *) payload->buffer.data + header_offset); + header->location_len = payload->buffer.size - size_before_location; + + ret = 0; + +end: + return ret; +} + +static bool lttng_event_rule_kernel_probe_is_equal(const struct lttng_event_rule *_a, + const struct lttng_event_rule *_b) +{ + bool is_equal = false; + struct lttng_event_rule_kernel_probe *a, *b; + + a = container_of(_a, struct lttng_event_rule_kernel_probe, parent); + b = container_of(_b, struct lttng_event_rule_kernel_probe, parent); + + /* Quick checks */ + if (!!a->name != !!b->name) { + goto end; + } + + /* Long check */ + assert(a->name); + assert(b->name); + if (strcmp(a->name, b->name)) { + goto end; + } + + is_equal = lttng_kernel_probe_location_is_equal( + a->location, b->location); +end: + return is_equal; +} + +static enum lttng_error_code lttng_event_rule_kernel_probe_generate_filter_bytecode( + struct lttng_event_rule *rule, + const struct lttng_credentials *creds) +{ + /* Nothing to do. */ + return LTTNG_OK; +} + +static const char *lttng_event_rule_kernel_probe_get_filter( + const struct lttng_event_rule *rule) +{ + /* Not supported. */ + return NULL; +} + +static const struct lttng_bytecode * +lttng_event_rule_kernel_probe_get_filter_bytecode(const struct lttng_event_rule *rule) +{ + /* Not supported. */ + return NULL; +} + +static enum lttng_event_rule_generate_exclusions_status +lttng_event_rule_kernel_probe_generate_exclusions(const struct lttng_event_rule *rule, + struct lttng_event_exclusion **exclusions) +{ + /* Not supported. */ + *exclusions = NULL; + return LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE; +} + +static unsigned long +lttng_event_rule_kernel_probe_hash( + const struct lttng_event_rule *rule) +{ + unsigned long hash; + struct lttng_event_rule_kernel_probe *krule = + container_of(rule, typeof(*krule), parent); + + hash = hash_key_ulong((void *) LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE, + lttng_ht_seed); + hash ^= hash_key_str(krule->name, lttng_ht_seed); + hash ^= lttng_kernel_probe_location_hash(krule->location); + + return hash; +} + +struct lttng_event_rule *lttng_event_rule_kernel_probe_create(void) +{ + struct lttng_event_rule *rule = NULL; + struct lttng_event_rule_kernel_probe *krule; + + krule = zmalloc(sizeof(struct lttng_event_rule_kernel_probe)); + if (!krule) { + goto end; + } + + rule = &krule->parent; + lttng_event_rule_init(&krule->parent, LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE); + krule->parent.validate = lttng_event_rule_kernel_probe_validate; + krule->parent.serialize = lttng_event_rule_kernel_probe_serialize; + krule->parent.equal = lttng_event_rule_kernel_probe_is_equal; + krule->parent.destroy = lttng_event_rule_kernel_probe_destroy; + krule->parent.generate_filter_bytecode = + lttng_event_rule_kernel_probe_generate_filter_bytecode; + krule->parent.get_filter = lttng_event_rule_kernel_probe_get_filter; + krule->parent.get_filter_bytecode = + lttng_event_rule_kernel_probe_get_filter_bytecode; + krule->parent.generate_exclusions = + lttng_event_rule_kernel_probe_generate_exclusions; + krule->parent.hash = lttng_event_rule_kernel_probe_hash; +end: + return rule; +} + +LTTNG_HIDDEN +ssize_t lttng_event_rule_kernel_probe_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_kernel_probe_comm *kprobe_comm; + const char *name; + struct lttng_buffer_view current_buffer_view; + struct lttng_event_rule *rule = NULL; + struct lttng_event_rule_kernel_probe *kprobe = NULL; + struct lttng_kernel_probe_location *location; + + if (!_event_rule) { + ret = -1; + goto end; + } + + current_buffer_view = lttng_buffer_view_from_view( + &view->buffer, offset, sizeof(*kprobe_comm)); + if (!lttng_buffer_view_is_valid(¤t_buffer_view)) { + ERR("Failed to initialize from malformed event rule kprobe: buffer too short to contain header."); + ret = -1; + goto end; + } + + kprobe_comm = (typeof(kprobe_comm)) current_buffer_view.data; + + rule = lttng_event_rule_kernel_probe_create(); + if (!rule) { + ERR("Failed to create event rule kprobe."); + ret = -1; + goto end; + } + + kprobe = container_of(rule, struct lttng_event_rule_kernel_probe, parent); + + /* Skip to payload */ + offset += current_buffer_view.size; + + { + /* Map the name. */ + struct lttng_payload_view current_payload_view = + lttng_payload_view_from_view(view, offset, + kprobe_comm->name_len); + + if (!lttng_payload_view_is_valid(¤t_payload_view)) { + ret = -1; + goto end; + } + + name = current_payload_view.buffer.data; + if (!lttng_buffer_view_contains_string( + ¤t_payload_view.buffer, name, + kprobe_comm->name_len)) { + ret = -1; + goto end; + } + } + + /* Skip after the name. */ + offset += kprobe_comm->name_len; + + /* Map the kernel probe location. */ + { + struct lttng_payload_view current_payload_view = + lttng_payload_view_from_view(view, offset, + kprobe_comm->location_len); + + if (!lttng_payload_view_is_valid(¤t_payload_view)) { + ret = -1; + goto end; + } + + ret = lttng_kernel_probe_location_create_from_payload( + ¤t_payload_view, &location); + if (ret < 0) { + ret = -1; + goto end; + } + } + + if (ret != kprobe_comm->location_len) { + ret = -1; + goto end; + } + + kprobe->location = location; + + /* Skip after the location */ + offset += kprobe_comm->location_len; + + status = lttng_event_rule_kernel_probe_set_name(rule, name); + if (status != LTTNG_EVENT_RULE_STATUS_OK) { + ERR("Failed to set event rule kprobe name."); + 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_kernel_probe_set_location( + struct lttng_event_rule *rule, + const struct lttng_kernel_probe_location *location) +{ + struct lttng_kernel_probe_location *location_copy = NULL; + struct lttng_event_rule_kernel_probe *kprobe; + enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; + + if (!rule || !IS_KPROBE_EVENT_RULE(rule) || !location) { + status = LTTNG_EVENT_RULE_STATUS_INVALID; + goto end; + } + + kprobe = container_of(rule, struct lttng_event_rule_kernel_probe, parent); + location_copy = lttng_kernel_probe_location_copy(location); + if (!location_copy) { + status = LTTNG_EVENT_RULE_STATUS_ERROR; + goto end; + } + + if (kprobe->location) { + lttng_kernel_probe_location_destroy(kprobe->location); + } + + kprobe->location = location_copy; + location_copy = NULL; +end: + lttng_kernel_probe_location_destroy(location_copy); + return status; +} + +enum lttng_event_rule_status lttng_event_rule_kernel_probe_get_location( + const struct lttng_event_rule *rule, + const struct lttng_kernel_probe_location **location) +{ + enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; + struct lttng_event_rule_kernel_probe *kprobe; + + if (!rule || !IS_KPROBE_EVENT_RULE(rule) || !location) { + status = LTTNG_EVENT_RULE_STATUS_INVALID; + goto end; + } + + kprobe = container_of(rule, struct lttng_event_rule_kernel_probe, parent); + *location = kprobe->location; + + if (!*location) { + status = LTTNG_EVENT_RULE_STATUS_UNSET; + goto end; + } + +end: + return status; +} + +enum lttng_event_rule_status lttng_event_rule_kernel_probe_set_name( + struct lttng_event_rule *rule, const char *name) +{ + char *name_copy = NULL; + struct lttng_event_rule_kernel_probe *kprobe; + enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; + + if (!rule || !IS_KPROBE_EVENT_RULE(rule) || !name || + strlen(name) == 0) { + status = LTTNG_EVENT_RULE_STATUS_INVALID; + goto end; + } + + kprobe = container_of(rule, struct lttng_event_rule_kernel_probe, parent); + name_copy = strdup(name); + if (!name_copy) { + status = LTTNG_EVENT_RULE_STATUS_ERROR; + goto end; + } + + free(kprobe->name); + + kprobe->name = name_copy; + name_copy = NULL; +end: + return status; +} + +enum lttng_event_rule_status lttng_event_rule_kernel_probe_get_name( + const struct lttng_event_rule *rule, const char **name) +{ + struct lttng_event_rule_kernel_probe *kprobe; + enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; + + if (!rule || !IS_KPROBE_EVENT_RULE(rule) || !name) { + status = LTTNG_EVENT_RULE_STATUS_INVALID; + goto end; + } + + kprobe = container_of(rule, struct lttng_event_rule_kernel_probe, parent); + if (!kprobe->name) { + status = LTTNG_EVENT_RULE_STATUS_UNSET; + goto end; + } + + *name = kprobe->name; +end: + return status; +} diff --git a/src/common/event-rule/kprobe.c b/src/common/event-rule/kprobe.c deleted file mode 100644 index 37592962b..000000000 --- a/src/common/event-rule/kprobe.c +++ /dev/null @@ -1,437 +0,0 @@ -/* - * Copyright (C) 2019 Jonathan Rajotte - * - * SPDX-License-Identifier: LGPL-2.1-only - * - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define IS_KPROBE_EVENT_RULE(rule) \ - (lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE) - -#if (LTTNG_SYMBOL_NAME_LEN == 256) -#define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255" -#endif - -static void lttng_event_rule_kernel_probe_destroy(struct lttng_event_rule *rule) -{ - struct lttng_event_rule_kernel_probe *kprobe; - - kprobe = container_of(rule, struct lttng_event_rule_kernel_probe, parent); - - lttng_kernel_probe_location_destroy(kprobe->location); - free(kprobe->name); - free(kprobe); -} - -static bool lttng_event_rule_kernel_probe_validate( - const struct lttng_event_rule *rule) -{ - bool valid = false; - struct lttng_event_rule_kernel_probe *kprobe; - - if (!rule) { - goto end; - } - - kprobe = container_of(rule, struct lttng_event_rule_kernel_probe, parent); - - /* Required field. */ - if (!kprobe->name) { - ERR("Invalid name event rule: a name must be set."); - goto end; - } - - /* Required field. */ - if(!kprobe->location) { - ERR("Invalid name event rule: a location must be set."); - goto end; - } - - valid = true; -end: - return valid; -} - -static int lttng_event_rule_kernel_probe_serialize( - const struct lttng_event_rule *rule, - struct lttng_payload *payload) -{ - int ret; - size_t name_len, header_offset, size_before_location; - struct lttng_event_rule_kernel_probe *kprobe; - struct lttng_event_rule_kernel_probe_comm kprobe_comm; - struct lttng_event_rule_kernel_probe_comm *header; - - if (!rule || !IS_KPROBE_EVENT_RULE(rule)) { - ret = -1; - goto end; - } - - header_offset = payload->buffer.size; - - DBG("Serializing kprobe event rule."); - kprobe = container_of(rule, struct lttng_event_rule_kernel_probe, parent); - - name_len = strlen(kprobe->name) + 1; - kprobe_comm.name_len = name_len; - - ret = lttng_dynamic_buffer_append( - &payload->buffer, &kprobe_comm, sizeof(kprobe_comm)); - if (ret) { - goto end; - } - - ret = lttng_dynamic_buffer_append(&payload->buffer, kprobe->name, name_len); - if (ret) { - goto end; - } - - size_before_location = payload->buffer.size; - - ret = lttng_kernel_probe_location_serialize(kprobe->location, payload); - if (ret < 0) { - goto end; - } - - /* Update the header regarding the probe size. */ - header = (struct lttng_event_rule_kernel_probe_comm*) ( - (char *) payload->buffer.data + header_offset); - header->location_len = payload->buffer.size - size_before_location; - - ret = 0; - -end: - return ret; -} - -static bool lttng_event_rule_kernel_probe_is_equal(const struct lttng_event_rule *_a, - const struct lttng_event_rule *_b) -{ - bool is_equal = false; - struct lttng_event_rule_kernel_probe *a, *b; - - a = container_of(_a, struct lttng_event_rule_kernel_probe, parent); - b = container_of(_b, struct lttng_event_rule_kernel_probe, parent); - - /* Quick checks */ - if (!!a->name != !!b->name) { - goto end; - } - - /* Long check */ - assert(a->name); - assert(b->name); - if (strcmp(a->name, b->name)) { - goto end; - } - - is_equal = lttng_kernel_probe_location_is_equal( - a->location, b->location); -end: - return is_equal; -} - -static enum lttng_error_code lttng_event_rule_kernel_probe_generate_filter_bytecode( - struct lttng_event_rule *rule, - const struct lttng_credentials *creds) -{ - /* Nothing to do. */ - return LTTNG_OK; -} - -static const char *lttng_event_rule_kernel_probe_get_filter( - const struct lttng_event_rule *rule) -{ - /* Not supported. */ - return NULL; -} - -static const struct lttng_bytecode * -lttng_event_rule_kernel_probe_get_filter_bytecode(const struct lttng_event_rule *rule) -{ - /* Not supported. */ - return NULL; -} - -static enum lttng_event_rule_generate_exclusions_status -lttng_event_rule_kernel_probe_generate_exclusions(const struct lttng_event_rule *rule, - struct lttng_event_exclusion **exclusions) -{ - /* Not supported. */ - *exclusions = NULL; - return LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE; -} - -static unsigned long -lttng_event_rule_kernel_probe_hash( - const struct lttng_event_rule *rule) -{ - unsigned long hash; - struct lttng_event_rule_kernel_probe *krule = - container_of(rule, typeof(*krule), parent); - - hash = hash_key_ulong((void *) LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE, - lttng_ht_seed); - hash ^= hash_key_str(krule->name, lttng_ht_seed); - hash ^= lttng_kernel_probe_location_hash(krule->location); - - return hash; -} - -struct lttng_event_rule *lttng_event_rule_kernel_probe_create(void) -{ - struct lttng_event_rule *rule = NULL; - struct lttng_event_rule_kernel_probe *krule; - - krule = zmalloc(sizeof(struct lttng_event_rule_kernel_probe)); - if (!krule) { - goto end; - } - - rule = &krule->parent; - lttng_event_rule_init(&krule->parent, LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE); - krule->parent.validate = lttng_event_rule_kernel_probe_validate; - krule->parent.serialize = lttng_event_rule_kernel_probe_serialize; - krule->parent.equal = lttng_event_rule_kernel_probe_is_equal; - krule->parent.destroy = lttng_event_rule_kernel_probe_destroy; - krule->parent.generate_filter_bytecode = - lttng_event_rule_kernel_probe_generate_filter_bytecode; - krule->parent.get_filter = lttng_event_rule_kernel_probe_get_filter; - krule->parent.get_filter_bytecode = - lttng_event_rule_kernel_probe_get_filter_bytecode; - krule->parent.generate_exclusions = - lttng_event_rule_kernel_probe_generate_exclusions; - krule->parent.hash = lttng_event_rule_kernel_probe_hash; -end: - return rule; -} - -LTTNG_HIDDEN -ssize_t lttng_event_rule_kernel_probe_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_kernel_probe_comm *kprobe_comm; - const char *name; - struct lttng_buffer_view current_buffer_view; - struct lttng_event_rule *rule = NULL; - struct lttng_event_rule_kernel_probe *kprobe = NULL; - struct lttng_kernel_probe_location *location; - - if (!_event_rule) { - ret = -1; - goto end; - } - - current_buffer_view = lttng_buffer_view_from_view( - &view->buffer, offset, sizeof(*kprobe_comm)); - if (!lttng_buffer_view_is_valid(¤t_buffer_view)) { - ERR("Failed to initialize from malformed event rule kprobe: buffer too short to contain header."); - ret = -1; - goto end; - } - - kprobe_comm = (typeof(kprobe_comm)) current_buffer_view.data; - - rule = lttng_event_rule_kernel_probe_create(); - if (!rule) { - ERR("Failed to create event rule kprobe."); - ret = -1; - goto end; - } - - kprobe = container_of(rule, struct lttng_event_rule_kernel_probe, parent); - - /* Skip to payload */ - offset += current_buffer_view.size; - - { - /* Map the name. */ - struct lttng_payload_view current_payload_view = - lttng_payload_view_from_view(view, offset, - kprobe_comm->name_len); - - if (!lttng_payload_view_is_valid(¤t_payload_view)) { - ret = -1; - goto end; - } - - name = current_payload_view.buffer.data; - if (!lttng_buffer_view_contains_string( - ¤t_payload_view.buffer, name, - kprobe_comm->name_len)) { - ret = -1; - goto end; - } - } - - /* Skip after the name. */ - offset += kprobe_comm->name_len; - - /* Map the kernel probe location. */ - { - struct lttng_payload_view current_payload_view = - lttng_payload_view_from_view(view, offset, - kprobe_comm->location_len); - - if (!lttng_payload_view_is_valid(¤t_payload_view)) { - ret = -1; - goto end; - } - - ret = lttng_kernel_probe_location_create_from_payload( - ¤t_payload_view, &location); - if (ret < 0) { - ret = -1; - goto end; - } - } - - if (ret != kprobe_comm->location_len) { - ret = -1; - goto end; - } - - kprobe->location = location; - - /* Skip after the location */ - offset += kprobe_comm->location_len; - - status = lttng_event_rule_kernel_probe_set_name(rule, name); - if (status != LTTNG_EVENT_RULE_STATUS_OK) { - ERR("Failed to set event rule kprobe name."); - 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_kernel_probe_set_location( - struct lttng_event_rule *rule, - const struct lttng_kernel_probe_location *location) -{ - struct lttng_kernel_probe_location *location_copy = NULL; - struct lttng_event_rule_kernel_probe *kprobe; - enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; - - if (!rule || !IS_KPROBE_EVENT_RULE(rule) || !location) { - status = LTTNG_EVENT_RULE_STATUS_INVALID; - goto end; - } - - kprobe = container_of(rule, struct lttng_event_rule_kernel_probe, parent); - location_copy = lttng_kernel_probe_location_copy(location); - if (!location_copy) { - status = LTTNG_EVENT_RULE_STATUS_ERROR; - goto end; - } - - if (kprobe->location) { - lttng_kernel_probe_location_destroy(kprobe->location); - } - - kprobe->location = location_copy; - location_copy = NULL; -end: - lttng_kernel_probe_location_destroy(location_copy); - return status; -} - -enum lttng_event_rule_status lttng_event_rule_kernel_probe_get_location( - const struct lttng_event_rule *rule, - const struct lttng_kernel_probe_location **location) -{ - enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; - struct lttng_event_rule_kernel_probe *kprobe; - - if (!rule || !IS_KPROBE_EVENT_RULE(rule) || !location) { - status = LTTNG_EVENT_RULE_STATUS_INVALID; - goto end; - } - - kprobe = container_of(rule, struct lttng_event_rule_kernel_probe, parent); - *location = kprobe->location; - - if (!*location) { - status = LTTNG_EVENT_RULE_STATUS_UNSET; - goto end; - } - -end: - return status; -} - -enum lttng_event_rule_status lttng_event_rule_kernel_probe_set_name( - struct lttng_event_rule *rule, const char *name) -{ - char *name_copy = NULL; - struct lttng_event_rule_kernel_probe *kprobe; - enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; - - if (!rule || !IS_KPROBE_EVENT_RULE(rule) || !name || - strlen(name) == 0) { - status = LTTNG_EVENT_RULE_STATUS_INVALID; - goto end; - } - - kprobe = container_of(rule, struct lttng_event_rule_kernel_probe, parent); - name_copy = strdup(name); - if (!name_copy) { - status = LTTNG_EVENT_RULE_STATUS_ERROR; - goto end; - } - - free(kprobe->name); - - kprobe->name = name_copy; - name_copy = NULL; -end: - return status; -} - -enum lttng_event_rule_status lttng_event_rule_kernel_probe_get_name( - const struct lttng_event_rule *rule, const char **name) -{ - struct lttng_event_rule_kernel_probe *kprobe; - enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; - - if (!rule || !IS_KPROBE_EVENT_RULE(rule) || !name) { - status = LTTNG_EVENT_RULE_STATUS_INVALID; - goto end; - } - - kprobe = container_of(rule, struct lttng_event_rule_kernel_probe, parent); - if (!kprobe->name) { - status = LTTNG_EVENT_RULE_STATUS_UNSET; - goto end; - } - - *name = kprobe->name; -end: - return status; -} diff --git a/tests/unit/test_event_rule.c b/tests/unit/test_event_rule.c index 7ed1a8bbf..0e83850a7 100644 --- a/tests/unit/test_event_rule.c +++ b/tests/unit/test_event_rule.c @@ -18,8 +18,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include