#include "lttng-syscall.hpp"
#include "modprobe.hpp"
#include "notification-thread-commands.hpp"
-#include "rotate.hpp"
#include "sessiond-config.hpp"
#include "tracker.hpp"
#include "utils.hpp"
#include <common/sessiond-comm/sessiond-comm.hpp>
#include <common/trace-chunk.hpp>
#include <common/tracker.hpp>
+#include <common/urcu.hpp>
#include <common/utils.hpp>
#include <lttng/condition/event-rule-matches-internal.hpp>
static int kernel_tracer_event_notifier_group_notification_fd = -1;
static struct cds_lfht *kernel_token_to_event_notifier_rule_ht;
+namespace {
+/*
+ * On some architectures, calling convention details are embedded in the symbol
+ * addresses. Uprobe requires a "clean" symbol offset (or at least, an address
+ * where an instruction boundary would be legal) to add
+ * instrumentation. sanitize_uprobe_offset implements that sanitization logic on
+ * a per-architecture basis.
+ */
+#if defined(__arm__) || defined(__aarch64__)
+static inline uint64_t sanitize_uprobe_offset(uint64_t raw_offset)
+{
+ /*
+ * The least significant bit is used when branching to switch to thumb
+ * ISA. However, it's an invalid address for us; mask the least
+ * significant bit.
+ */
+ return raw_offset &= ~0b1;
+}
+#else /* defined(__arm__) || defined(__aarch64__) */
+static inline uint64_t sanitize_uprobe_offset(uint64_t raw_offset)
+{
+ return raw_offset;
+}
+#endif
+} /* namespace */
+
/*
* Add context on a kernel channel.
*
end:
cds_list_add_tail(&ctx->list, &chan->ctx_list);
ctx->in_list = true;
- ctx = NULL;
+ ctx = nullptr;
error:
if (ctx) {
trace_kernel_destroy_context(ctx);
/* Allocate data structure */
lks = trace_kernel_create_session();
- if (lks == NULL) {
+ if (lks == nullptr) {
ret = -1;
goto error;
}
/* Allocate kernel channel */
lkc = trace_kernel_create_channel(chan);
- if (lkc == NULL) {
+ if (lkc == nullptr) {
goto error;
}
{
int fd;
int ret = 0;
- const char *symbol = NULL;
- const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
+ const char *symbol = nullptr;
+ const struct lttng_userspace_probe_location_lookup_method *lookup = nullptr;
enum lttng_userspace_probe_location_lookup_method_type lookup_method_type;
LTTNG_ASSERT(lttng_userspace_probe_location_get_type(probe_location) ==
uint32_t *offsets_count)
{
enum lttng_userspace_probe_location_lookup_method_type lookup_method_type;
- const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
- const char *probe_name = NULL, *provider_name = NULL;
+ const struct lttng_userspace_probe_location_lookup_method *lookup = nullptr;
+ const char *probe_name = nullptr, *provider_name = nullptr;
int ret = 0;
int fd, i;
gid_t gid,
int fd)
{
- const struct lttng_userspace_probe_location_lookup_method *lookup_method = NULL;
+ const struct lttng_userspace_probe_location_lookup_method *lookup_method = nullptr;
enum lttng_userspace_probe_location_lookup_method_type type;
int ret;
goto end;
}
- callsite.u.uprobe.offset = offset;
+ callsite.u.uprobe.offset = sanitize_uprobe_offset(offset);
ret = kernctl_add_callsite(fd, &callsite);
if (ret) {
WARN("Failed to add callsite to ELF userspace probe.");
case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
{
int i;
- uint64_t *offsets = NULL;
+ uint64_t *offsets = nullptr;
uint32_t offsets_count;
struct lttng_kernel_abi_event_callsite callsite;
goto end;
}
for (i = 0; i < offsets_count; i++) {
- callsite.u.uprobe.offset = offsets[i];
+ callsite.u.uprobe.offset = sanitize_uprobe_offset(offsets[i]);
ret = kernctl_add_callsite(fd, &callsite);
if (ret) {
WARN("Failed to add callsite to SDT userspace probe");
int fd)
{
int ret;
- const struct lttng_userspace_probe_location *location = NULL;
+ const struct lttng_userspace_probe_location *location = nullptr;
LTTNG_ASSERT(ev);
LTTNG_ASSERT(ev->type == LTTNG_EVENT_USERSPACE_PROBE);
int ret;
enum lttng_event_rule_status status;
enum lttng_event_rule_type event_rule_type;
- const struct lttng_userspace_probe_location *location = NULL;
+ const struct lttng_userspace_probe_location *location = nullptr;
LTTNG_ASSERT(rule);
LTTNG_ASSERT(creds);
goto error;
}
- chan->enabled = 0;
+ chan->enabled = false;
DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64 ")",
chan->channel->name,
chan->fd,
goto error;
}
- chan->enabled = 1;
+ chan->enabled = true;
DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64 ")",
chan->channel->name,
chan->fd,
goto error;
}
- event->enabled = 1;
+ event->enabled = true;
DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd);
return 0;
goto error;
}
- event->enabled = 0;
+ event->enabled = false;
DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
return 0;
LTTNG_ASSERT(event);
- rcu_read_lock();
+ lttng::urcu::read_lock_guard read_lock;
cds_lfht_del(kernel_token_to_event_notifier_rule_ht, &event->ht_node);
- rcu_read_unlock();
ret = kernctl_disable(event->fd);
if (ret < 0) {
goto error;
}
- event->enabled = 0;
+ event->enabled = false;
DBG("Disabled kernel event notifier: fd = %d, token = %" PRIu64, event->fd, event->token);
error:
case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
return session->tracker_vgid;
default:
- return NULL;
+ return nullptr;
}
}
int kernel_open_metadata(struct ltt_kernel_session *session)
{
int ret;
- struct ltt_kernel_metadata *lkm = NULL;
+ struct ltt_kernel_metadata *lkm = nullptr;
LTTNG_ASSERT(session);
/* Allocate kernel metadata */
lkm = trace_kernel_create_metadata();
- if (lkm == NULL) {
+ if (lkm == nullptr) {
goto error;
}
/*
* Make a kernel wait to make sure in-flight probe have completed.
*/
-void kernel_wait_quiescent(void)
+void kernel_wait_quiescent()
{
int ret;
int fd = kernel_tracer_fd;
while ((ret = kernctl_create_stream(channel->fd)) >= 0) {
lks = trace_kernel_create_stream(channel->channel->name, channel->stream_count);
- if (lks == NULL) {
+ if (lks == nullptr) {
ret = close(ret);
if (ret) {
PERROR("close");
}
fp = fdopen(fd, "r");
- if (fp == NULL) {
+ if (fp == nullptr) {
PERROR("kernel tracepoint list fdopen");
goto error_fp;
}
*/
nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
elist = calloc<lttng_event>(nbmem);
- if (elist == NULL) {
+ if (elist == nullptr) {
PERROR("alloc list events");
count = -ENOMEM;
goto end;
DBG("Reallocating event list from %zu to %zu bytes", nbmem, new_nbmem);
new_elist = (lttng_event *) realloc(elist,
new_nbmem * sizeof(struct lttng_event));
- if (new_elist == NULL) {
+ if (new_elist == nullptr) {
PERROR("realloc list events");
free(event);
free(elist);
/*
* Kernel work-arounds called at the start of sessiond main().
*/
-int init_kernel_workarounds(void)
+int init_kernel_workarounds()
{
int ret;
FILE *fp;
{
struct lttng_trace_chunk *trace_chunk;
- if (ksess == NULL) {
+ if (ksess == nullptr) {
DBG3("No kernel session when tearing down session");
return;
}
struct lttng_ht_iter iter;
/* For each consumer socket. */
- rcu_read_lock();
+ lttng::urcu::read_lock_guard read_lock;
+
cds_lfht_for_each_entry (
ksess->consumer->socks->ht, &iter.iter, socket, node.node) {
struct ltt_kernel_channel *chan;
}
}
}
- rcu_read_unlock();
}
/* Close any relayd session */
/* Teardown of data required by destroy notifiers. */
void kernel_free_session(struct ltt_kernel_session *ksess)
{
- if (ksess == NULL) {
+ if (ksess == nullptr) {
return;
}
trace_kernel_free_session(ksess);
*/
void kernel_destroy_channel(struct ltt_kernel_channel *kchan)
{
- struct ltt_kernel_session *ksess = NULL;
+ struct ltt_kernel_session *ksess = nullptr;
LTTNG_ASSERT(kchan);
LTTNG_ASSERT(kchan->channel);
struct consumer_socket *socket;
struct lttng_ht_iter iter;
struct ltt_kernel_metadata *saved_metadata;
- char *trace_path = NULL;
+ char *trace_path = nullptr;
size_t consumer_path_offset = 0;
LTTNG_ASSERT(ksess);
saved_metadata = ksess->metadata;
saved_metadata_fd = ksess->metadata_stream_fd;
- rcu_read_lock();
-
ret = kernel_open_metadata(ksess);
if (ret < 0) {
status = LTTNG_ERR_KERN_META_FAIL;
status = LTTNG_ERR_INVALID;
goto error;
}
- /* Send metadata to consumer and snapshot everything. */
- cds_lfht_for_each_entry (output->socks->ht, &iter.iter, socket, node.node) {
- struct ltt_kernel_channel *chan;
- pthread_mutex_lock(socket->lock);
- /* This stream must not be monitored by the consumer. */
- ret = kernel_consumer_add_metadata(socket, ksess, 0);
- pthread_mutex_unlock(socket->lock);
- if (ret < 0) {
- status = LTTNG_ERR_KERN_META_FAIL;
- goto error_consumer;
- }
+ {
+ /* Send metadata to consumer and snapshot everything. */
+ lttng::urcu::read_lock_guard read_lock;
+
+ cds_lfht_for_each_entry (output->socks->ht, &iter.iter, socket, node.node) {
+ struct ltt_kernel_channel *chan;
- /* For each channel, ask the consumer to snapshot it. */
- cds_list_for_each_entry (chan, &ksess->channel_list.head, list) {
+ pthread_mutex_lock(socket->lock);
+ /* This stream must not be monitored by the consumer. */
+ ret = kernel_consumer_add_metadata(socket, ksess, 0);
+ pthread_mutex_unlock(socket->lock);
+ if (ret < 0) {
+ status = LTTNG_ERR_KERN_META_FAIL;
+ goto error_consumer;
+ }
+
+ /* For each channel, ask the consumer to snapshot it. */
+ cds_list_for_each_entry (chan, &ksess->channel_list.head, list) {
+ status =
+ consumer_snapshot_channel(socket,
+ chan->key,
+ output,
+ 0,
+ &trace_path[consumer_path_offset],
+ nb_packets_per_stream);
+ if (status != LTTNG_OK) {
+ (void) kernel_consumer_destroy_metadata(socket,
+ ksess->metadata);
+ goto error_consumer;
+ }
+ }
+
+ /* Snapshot metadata, */
status = consumer_snapshot_channel(socket,
- chan->key,
+ ksess->metadata->key,
output,
- 0,
+ 1,
&trace_path[consumer_path_offset],
- nb_packets_per_stream);
+ 0);
if (status != LTTNG_OK) {
- (void) kernel_consumer_destroy_metadata(socket, ksess->metadata);
goto error_consumer;
}
- }
- /* Snapshot metadata, */
- status = consumer_snapshot_channel(socket,
- ksess->metadata->key,
- output,
- 1,
- &trace_path[consumer_path_offset],
- 0);
- if (status != LTTNG_OK) {
- goto error_consumer;
+ /*
+ * The metadata snapshot is done, ask the consumer to destroy it since
+ * it's not monitored on the consumer side.
+ */
+ (void) kernel_consumer_destroy_metadata(socket, ksess->metadata);
}
-
- /*
- * The metadata snapshot is done, ask the consumer to destroy it since
- * it's not monitored on the consumer side.
- */
- (void) kernel_consumer_destroy_metadata(socket, ksess->metadata);
}
error_consumer:
/* Restore metadata state.*/
ksess->metadata = saved_metadata;
ksess->metadata_stream_fd = saved_metadata_fd;
- rcu_read_unlock();
free(trace_path);
return status;
}
* Return 1 on success, 0 when feature is not supported, negative value in case
* of errors.
*/
-int kernel_supports_ring_buffer_snapshot_sample_positions(void)
+int kernel_supports_ring_buffer_snapshot_sample_positions()
{
/*
* RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
* Return 1 on success, 0 when feature is not supported, negative value in case
* of errors.
*/
-int kernel_supports_ring_buffer_packet_sequence_number(void)
+int kernel_supports_ring_buffer_packet_sequence_number()
{
/*
* Packet sequence number was introduced in LTTng 2.8,
* Return 1 on success, 0 when feature is not supported, negative value in case
* of errors.
*/
-int kernel_supports_event_notifiers(void)
+int kernel_supports_event_notifiers()
{
/*
* Event notifiers were introduced in LTTng 2.13, lttng-modules ABI 2.6.
DBG("Rotate kernel session %s started (session %" PRIu64 ")", session->name, session->id);
- rcu_read_lock();
+ {
+ /*
+ * Note that this loop will end after one iteration given that there is
+ * only one kernel consumer.
+ */
+ lttng::urcu::read_lock_guard read_lock;
+
+ cds_lfht_for_each_entry (
+ ksess->consumer->socks->ht, &iter.iter, socket, node.node) {
+ struct ltt_kernel_channel *chan;
- /*
- * Note that this loop will end after one iteration given that there is
- * only one kernel consumer.
- */
- cds_lfht_for_each_entry (ksess->consumer->socks->ht, &iter.iter, socket, node.node) {
- struct ltt_kernel_channel *chan;
-
- /* For each channel, ask the consumer to rotate it. */
- cds_list_for_each_entry (chan, &ksess->channel_list.head, list) {
- DBG("Rotate kernel channel %" PRIu64 ", session %s",
- chan->key,
- session->name);
+ /* For each channel, ask the consumer to rotate it. */
+ cds_list_for_each_entry (chan, &ksess->channel_list.head, list) {
+ DBG("Rotate kernel channel %" PRIu64 ", session %s",
+ chan->key,
+ session->name);
+ ret = consumer_rotate_channel(socket,
+ chan->key,
+ ksess->consumer,
+ /* is_metadata_channel */ false);
+ if (ret < 0) {
+ status = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
+ goto error;
+ }
+ }
+
+ /*
+ * Rotate the metadata channel.
+ */
ret = consumer_rotate_channel(socket,
- chan->key,
+ ksess->metadata->key,
ksess->consumer,
- /* is_metadata_channel */ false);
+ /* is_metadata_channel */ true);
if (ret < 0) {
status = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
goto error;
}
}
-
- /*
- * Rotate the metadata channel.
- */
- ret = consumer_rotate_channel(socket,
- ksess->metadata->key,
- ksess->consumer,
- /* is_metadata_channel */ true);
- if (ret < 0) {
- status = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
- goto error;
- }
}
error:
- rcu_read_unlock();
return status;
}
enum lttng_error_code ret = LTTNG_OK;
enum lttng_trace_chunk_status chunk_status;
- rcu_read_lock();
+ lttng::urcu::read_lock_guard read_lock;
LTTNG_ASSERT(ksess->current_trace_chunk);
/*
goto error;
}
error:
- rcu_read_unlock();
return ret;
}
/*
* Setup necessary data for kernel tracer action.
*/
-int init_kernel_tracer(void)
+int init_kernel_tracer()
{
int ret;
bool is_root = !getuid();
}
kernel_token_to_event_notifier_rule_ht = cds_lfht_new(
- DEFAULT_HT_SIZE, 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
+ DEFAULT_HT_SIZE, 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, nullptr);
if (!kernel_token_to_event_notifier_rule_ht) {
goto error_token_ht;
}
}
}
-void cleanup_kernel_tracer(void)
+void cleanup_kernel_tracer()
{
DBG2("Closing kernel event notifier group notification file descriptor");
if (kernel_tracer_event_notifier_group_notification_fd >= 0) {
}
if (kernel_token_to_event_notifier_rule_ht) {
- const int ret = cds_lfht_destroy(kernel_token_to_event_notifier_rule_ht, NULL);
+ const int ret = cds_lfht_destroy(kernel_token_to_event_notifier_rule_ht, nullptr);
LTTNG_ASSERT(ret == 0);
}
free(syscall_table);
}
-bool kernel_tracer_is_initialized(void)
+bool kernel_tracer_is_initialized()
{
return kernel_tracer_fd >= 0;
}
DBG("Clear kernel session %s (session %" PRIu64 ")", session->name, session->id);
- rcu_read_lock();
-
if (ksess->active) {
ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id);
status = LTTNG_ERR_FATAL;
goto end;
}
- /*
- * Note that this loop will end after one iteration given that there is
- * only one kernel consumer.
- */
- cds_lfht_for_each_entry (ksess->consumer->socks->ht, &iter.iter, socket, node.node) {
- struct ltt_kernel_channel *chan;
-
- /* For each channel, ask the consumer to clear it. */
- cds_list_for_each_entry (chan, &ksess->channel_list.head, list) {
- DBG("Clear kernel channel %" PRIu64 ", session %s",
- chan->key,
- session->name);
- ret = consumer_clear_channel(socket, chan->key);
- if (ret < 0) {
- goto error;
+ {
+ /*
+ * Note that this loop will end after one iteration given that there is
+ * only one kernel consumer.
+ */
+ lttng::urcu::read_lock_guard read_lock;
+
+ cds_lfht_for_each_entry (
+ ksess->consumer->socks->ht, &iter.iter, socket, node.node) {
+ struct ltt_kernel_channel *chan;
+
+ /* For each channel, ask the consumer to clear it. */
+ cds_list_for_each_entry (chan, &ksess->channel_list.head, list) {
+ DBG("Clear kernel channel %" PRIu64 ", session %s",
+ chan->key,
+ session->name);
+ ret = consumer_clear_channel(socket, chan->key);
+ if (ret < 0) {
+ goto error;
+ }
+ }
+
+ if (!ksess->metadata) {
+ /*
+ * Nothing to do for the metadata.
+ * This is a snapshot session.
+ * The metadata is genererated on the fly.
+ */
+ continue;
}
- }
- if (!ksess->metadata) {
/*
- * Nothing to do for the metadata.
- * This is a snapshot session.
- * The metadata is genererated on the fly.
+ * Clear the metadata channel.
+ * Metadata channel is not cleared per se but we still need to
+ * perform a rotation operation on it behind the scene.
*/
- continue;
- }
-
- /*
- * Clear the metadata channel.
- * Metadata channel is not cleared per se but we still need to
- * perform a rotation operation on it behind the scene.
- */
- ret = consumer_clear_channel(socket, ksess->metadata->key);
- if (ret < 0) {
- goto error;
+ ret = consumer_clear_channel(socket, ksess->metadata->key);
+ if (ret < 0) {
+ goto error;
+ }
}
}
break;
}
end:
- rcu_read_unlock();
return status;
}
struct ltt_kernel_event_notifier_rule *event_notifier_rule;
struct lttng_kernel_abi_event_notifier kernel_event_notifier = {};
unsigned int capture_bytecode_count = 0, i;
- const struct lttng_condition *condition = NULL;
- const struct lttng_event_rule *event_rule = NULL;
+ const struct lttng_condition *condition = nullptr;
+ const struct lttng_event_rule *event_rule = nullptr;
enum lttng_condition_status cond_status;
LTTNG_ASSERT(trigger);
lttng_condition_event_rule_matches_get_capture_bytecode_at_index(condition,
i);
- if (capture_bytecode == NULL) {
+ if (capture_bytecode == nullptr) {
ERR("Unexpected NULL capture bytecode on condition");
error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL;
goto capture_error;
}
/* Add trigger to kernel token mapping in the hash table. */
- rcu_read_lock();
- cds_lfht_add(kernel_token_to_event_notifier_rule_ht,
- hash_trigger(trigger),
- &event_notifier_rule->ht_node);
- rcu_read_unlock();
+ {
+ lttng::urcu::read_lock_guard read_lock;
+ cds_lfht_add(kernel_token_to_event_notifier_rule_ht,
+ hash_trigger(trigger),
+ &event_notifier_rule->ht_node);
+ }
DBG("Created kernel event notifier: name = '%s', fd = %d",
kernel_event_notifier.event.name,
enum lttng_error_code error_code_ret;
int ret;
- rcu_read_lock();
+ lttng::urcu::read_lock_guard read_lock;
cds_lfht_lookup(kernel_token_to_event_notifier_rule_ht,
hash_trigger(trigger),
error_code_ret = LTTNG_OK;
error:
- rcu_read_unlock();
return error_code_ret;
}
-int kernel_get_notification_fd(void)
+int kernel_get_notification_fd()
{
return kernel_tracer_event_notifier_group_notification_fd;
}