#ifndef _LTT_SESSION_H
#define _LTT_SESSION_H
-#include <limits.h>
-#include <stdbool.h>
-#include <urcu/list.h>
+#include "consumer.hpp"
+#include "snapshot.hpp"
+#include "trace-kernel.hpp"
+#include "trace-ust.hpp"
+#include "ust-app.hpp"
-#include <common/hashtable/hashtable.hpp>
#include <common/dynamic-array.hpp>
-#include <lttng/rotation.h>
+#include <common/exception.hpp>
+#include <common/hashtable/hashtable.hpp>
+#include <common/make-unique-wrapper.hpp>
+#include <common/pthread-lock.hpp>
+#include <common/reference.hpp>
+#include <common/urcu.hpp>
+
#include <lttng/location.h>
#include <lttng/lttng-error.h>
+#include <lttng/rotation.h>
-#include "snapshot.hpp"
-#include "trace-kernel.hpp"
-#include "consumer.hpp"
+#include <condition_variable>
+#include <limits.h>
+#include <mutex>
+#include <stdbool.h>
+#include <urcu/list.h>
+
+#define ASSERT_SESSION_LIST_LOCKED() LTTNG_ASSERT(session_trylock_list())
struct ltt_ust_session;
-typedef void (*ltt_session_destroy_notifier)(const struct ltt_session *session,
- void *user_data);
-typedef void (*ltt_session_clear_notifier)(const struct ltt_session *session,
- void *user_data);
+struct ltt_session;
+struct ltt_session_list;
+struct buffer_reg_uid;
+
+enum lttng_error_code
+session_create(const char *name, uid_t uid, gid_t gid, struct ltt_session **out_session);
+void session_lock(const ltt_session *session);
+void session_unlock(const ltt_session *session);
+
+bool session_get(struct ltt_session *session);
+void session_put(struct ltt_session *session);
+
+/*
+ * The session list lock covers more ground than its name implies. While
+ * it does protect against concurent mutations of the session list, it is
+ * also used as a multi-session lock when synchronizing newly-registered
+ * 'user space tracer' and 'agent' applications.
+ *
+ * In other words, it prevents tracer configurations from changing while they
+ * are being transmitted to the various applications.
+ */
+int session_trylock_list() noexcept;
+
+#define LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(session_name) \
+ throw lttng::sessiond::exceptions::session_not_found_error(session_name, \
+ LTTNG_SOURCE_LOCATION())
+#define LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id) \
+ throw lttng::sessiond::exceptions::session_not_found_error(id, LTTNG_SOURCE_LOCATION())
/*
* Tracing session list
* functions are used, the lock MUST be acquired in order to
* iterate or/and do any actions on that list.
*/
- pthread_mutex_t lock;
+ std::mutex lock;
/*
* This condition variable is signaled on every removal from
* the session list.
*/
- pthread_cond_t removal_cond;
+ std::condition_variable removal_cond;
/*
* Session unique ID generator. The session list lock MUST be
* upon update and read of this counter.
*/
- uint64_t next_uuid;
+ uint64_t next_uuid = 0;
/* Linked list head */
- struct cds_list_head head;
+ struct cds_list_head head = CDS_LIST_HEAD_INIT(head);
};
+namespace lttng {
+namespace sessiond {
+class user_space_consumer_channel_keys {
+ friend ltt_session;
+
+public:
+ class iterator;
+
+ enum class consumer_bitness : std::uint8_t {
+ ABI_32 = 32,
+ ABI_64 = 64,
+ };
+
+ enum class channel_type : std::uint8_t {
+ METADATA,
+ DATA,
+ };
+
+ iterator begin() const noexcept;
+ iterator end() const noexcept;
+
+private:
+ enum class _iteration_mode : std::uint8_t {
+ PER_PID,
+ PER_UID,
+
+ };
+
+ struct _iterator_creation_context {
+ const _iteration_mode _mode;
+ const ltt_ust_session& _session;
+ union {
+ lttng_ht *apps;
+ const cds_list_head *buffer_registry;
+ } _container;
+ };
+
+public:
+ class iterator : public std::iterator<std::input_iterator_tag, std::uint64_t> {
+ friend user_space_consumer_channel_keys;
+
+ public:
+ struct key {
+ /* Bitness is needed to query the appropriate consumer daemon. */
+ consumer_bitness bitness;
+ std::uint64_t key_value;
+ channel_type type;
+
+ bool operator==(const key& other)
+ {
+ return bitness == other.bitness && key_value == other.key_value &&
+ type == other.type;
+ }
+ };
+
+ /*
+ * Copy constructor disabled since it would require handling the copy of locked
+ * references.
+ */
+ iterator(const iterator& other) = delete;
+ iterator(iterator&& other) = default;
+ ~iterator() = default;
+ iterator& operator=(const iterator&) = delete;
+ iterator& operator=(iterator&&) noexcept = delete;
+
+ iterator& operator++();
+ bool operator==(const iterator& other) const noexcept;
+ bool operator!=(const iterator& other) const noexcept;
+ key operator*() const;
+
+ /*
+ * Get the session registry of the channel currently
+ * pointed by the iterator. Never returns nullptr.
+ */
+ lttng::sessiond::ust::registry_session *get_registry_session();
+
+ private:
+ struct _iterator_position {
+ struct {
+ lttng_ht_iter app_iterator = {};
+ nonstd::optional<ust_app_session::locked_weak_ref>
+ current_app_session;
+ lttng::sessiond::ust::registry_session *current_registry_session =
+ nullptr;
+ } _per_pid;
+ struct {
+ buffer_reg_uid *current_registry = nullptr;
+ } _per_uid;
+
+ lttng_ht_iter channel_iterator = {};
+ };
+
+ explicit iterator(const _iterator_creation_context& creation_context,
+ bool is_end = false);
+
+ void _init_per_pid() noexcept;
+ void _skip_to_next_app_per_pid(bool try_current) noexcept;
+ void _advance_one_per_pid();
+ key _get_current_value_per_pid() const noexcept;
+ lttng::sessiond::ust::registry_session *_get_registry_session_per_pid();
+
+ void _init_per_uid() noexcept;
+ void _advance_one_per_uid();
+ key _get_current_value_per_uid() const noexcept;
+ lttng::sessiond::ust::registry_session *_get_registry_session_per_uid();
+
+ const _iterator_creation_context& _creation_context;
+ _iterator_position _position;
+ bool _is_end;
+ };
+
+private:
+ user_space_consumer_channel_keys(const ltt_ust_session& ust_session, lttng_ht& apps) :
+ _creation_context{ _iteration_mode::PER_PID, ust_session, { .apps = &apps } }
+ {
+ }
+
+ user_space_consumer_channel_keys(const ltt_ust_session& ust_session,
+ const cds_list_head& buffer_registry) :
+ _creation_context{ _iteration_mode::PER_UID,
+ ust_session,
+ { .buffer_registry = &buffer_registry } }
+ {
+ }
+
+ class _scoped_rcu_read_lock {
+ public:
+ _scoped_rcu_read_lock()
+ {
+ rcu_read_lock();
+ }
+
+ ~_scoped_rcu_read_lock()
+ {
+ if (_armed) {
+ rcu_read_unlock();
+ }
+ }
+
+ _scoped_rcu_read_lock(_scoped_rcu_read_lock&& other) noexcept
+ {
+ other._armed = false;
+ }
+
+ _scoped_rcu_read_lock(_scoped_rcu_read_lock& other) = delete;
+ _scoped_rcu_read_lock& operator=(const _scoped_rcu_read_lock&) = delete;
+ _scoped_rcu_read_lock& operator=(_scoped_rcu_read_lock&&) noexcept = delete;
+
+ private:
+ bool _armed = true;
+ };
+
+ _scoped_rcu_read_lock _read_lock;
+ _iterator_creation_context _creation_context;
+};
+} /* namespace sessiond */
+} /* namespace lttng */
+
/*
* This data structure contains information needed to identify a tracing
* session for both LTTng and UST.
*/
struct ltt_session {
+ using id_t = uint64_t;
+ friend lttng::sessiond::user_space_consumer_channel_keys::iterator;
+
+private:
+ static void _locked_session_release(ltt_session *session);
+ static void _locked_const_session_release(const ltt_session *session);
+ static void _const_session_put(const ltt_session *session);
+ static void _const_session_unlock(const ltt_session& session);
+
+public:
+ using locked_ref = lttng::non_copyable_reference<
+ ltt_session,
+ lttng::memory::create_deleter_class<ltt_session,
+ ltt_session::_locked_session_release>::deleter>;
+ using ref = lttng::non_copyable_reference<
+ ltt_session,
+ lttng::memory::create_deleter_class<ltt_session, session_put>::deleter>;
+ using const_locked_ref = lttng::non_copyable_reference<
+ const ltt_session,
+ lttng::memory::create_deleter_class<
+ const ltt_session,
+ ltt_session::_locked_const_session_release>::deleter>;
+ using const_ref = lttng::non_copyable_reference<
+ const ltt_session,
+ lttng::memory::create_deleter_class<const ltt_session,
+ ltt_session::_const_session_put>::deleter>;
+
+ static locked_ref make_locked_ref(ltt_session& session)
+ {
+ return lttng::make_non_copyable_reference<locked_ref::referenced_type,
+ locked_ref::deleter>(session);
+ }
+
+ static const_locked_ref make_locked_ref(const ltt_session& session)
+ {
+ return lttng::make_non_copyable_reference<const_locked_ref::referenced_type,
+ const_locked_ref::deleter>(session);
+ }
+
+ static ref make_ref(ltt_session& session)
+ {
+ return lttng::make_non_copyable_reference<ref::referenced_type, ref::deleter>(
+ session);
+ }
+
+ static const_ref make_ref(const ltt_session& session)
+ {
+ return lttng::make_non_copyable_reference<const_ref::referenced_type,
+ const_ref::deleter>(session);
+ }
+
+ void lock() const noexcept;
+ void unlock() const noexcept;
+
+ lttng::sessiond::user_space_consumer_channel_keys user_space_consumer_channel_keys() const;
+
+ /*
+ * Session list lock must be acquired by the caller.
+ *
+ * The caller must not keep the ownership of the returned locked session
+ * for longer than strictly necessary. If your intention is to acquire
+ * a reference to an ltt_session, see `find_session()`.
+ */
+ static locked_ref find_locked_session(ltt_session::id_t id);
+ static locked_ref find_locked_session(lttng::c_string_view name);
+ static const_locked_ref find_locked_const_session(ltt_session::id_t id);
+ static const_locked_ref find_locked_const_session(lttng::c_string_view name);
+
+ static ref find_session(ltt_session::id_t id);
+ static ref find_session(lttng::c_string_view name);
+ static const_ref find_const_session(ltt_session::id_t id);
+ static const_ref find_const_session(lttng::c_string_view name);
+
char name[NAME_MAX];
bool has_auto_generated_name;
bool name_contains_creation_time;
time_t creation_time;
struct ltt_kernel_session *kernel_session;
struct ltt_ust_session *ust_session;
- struct urcu_ref ref;
+ mutable struct urcu_ref ref_count;
/*
* Protect any read/write on this session data structure. This lock must be
* acquired *before* using any public functions declared below. Use
* session_lock() and session_unlock() for that.
*/
- pthread_mutex_t lock;
+ mutable pthread_mutex_t _lock;
struct cds_list_head list;
- uint64_t id; /* session unique identifier */
+ /* session unique identifier */
+ id_t id;
/* Indicates if the session has been added to the session list and ht.*/
bool published;
/* Indicates if a destroy command has been applied to this session. */
*/
bool has_user_specified_directory;
/* Did at least ONE start command has been triggered?. */
- unsigned int has_been_started:1;
- /*
- * Is the session active? Start trace command sets this to 1 and the stop
- * command reset it to 0.
- */
- unsigned int active:1;
+ bool has_been_started;
+ /* Is the session active? */
+ bool active;
/* Snapshot representation in a session. */
struct snapshot snapshot;
char *base_path;
};
-enum lttng_error_code session_create(const char *name, uid_t uid, gid_t gid,
- struct ltt_session **out_session);
-void session_lock(struct ltt_session *session);
-void session_unlock(struct ltt_session *session);
-
/*
- * The session list lock covers more ground than its name implies. While
- * it does protect against concurent mutations of the session list, it is
- * also used as a multi-session lock when synchronizing newly-registered
- * 'user space tracer' and 'agent' applications.
- *
- * In other words, it prevents tracer configurations from changing while they
- * are being transmitted to the various applications.
+ * Destruction notifiers are invoked in an exclusive context. There is no need for the session to be
+ * locked nor for a reference to be acquired.
*/
-void session_lock_list(void);
-int session_trylock_list(void);
-void session_unlock_list(void);
+using ltt_session_destroy_notifier = void (*)(const ltt_session::locked_ref&, void *);
+using ltt_session_clear_notifier = void (*)(const ltt_session::locked_ref&, void *);
-void session_destroy(struct ltt_session *session);
-int session_add_destroy_notifier(struct ltt_session *session,
- ltt_session_destroy_notifier notifier, void *user_data);
+namespace lttng {
+namespace sessiond {
-int session_add_clear_notifier(struct ltt_session *session,
- ltt_session_clear_notifier notifier, void *user_data);
-void session_notify_clear(struct ltt_session *session);
+std::unique_lock<std::mutex> lock_session_list();
-bool session_get(struct ltt_session *session);
-void session_put(struct ltt_session *session);
+namespace exceptions {
+/**
+ * @class session_not_found_error
+ * @brief Represents a session-not-found error and provides the parameters used to query the session
+ * for use by error-reporting code.
+ */
+class session_not_found_error : public lttng::runtime_error {
+public:
+ class query_parameter {
+ public:
+ enum class query_type : std::uint8_t { BY_NAME, BY_ID };
+
+ /*
+ * Intentionally not explicit to allow construction from c-style strings,
+ * std::string, and lttng::c_string_view.
+ *
+ * NOLINTBEGIN(google-explicit-constructor)
+ */
+ query_parameter(const std::string& session_name) :
+ type(query_type::BY_NAME), parameter(session_name)
+ {
+ }
+ /* NOLINTEND(google-explicit-constructor) */
+
+ explicit query_parameter(ltt_session::id_t id_) :
+ type(query_type::BY_ID), parameter(id_)
+ {
+ }
+
+ query_parameter(const query_parameter& other) : type(other.type)
+ {
+ if (type == query_type::BY_NAME) {
+ new (¶meter.name) std::string(other.parameter.name);
+ } else {
+ parameter.id = other.parameter.id;
+ }
+ }
+
+ ~query_parameter()
+ {
+ if (type == query_type::BY_NAME) {
+ parameter.name.~basic_string();
+ }
+ }
+
+ query_parameter(query_parameter&& other) noexcept;
+ query_parameter& operator=(const query_parameter&) = delete;
+ query_parameter& operator=(query_parameter&&) noexcept = delete;
+
+ query_type type;
+ union parameter {
+ explicit parameter(std::string name_) : name(std::move(name_))
+ {
+ }
+
+ explicit parameter(ltt_session::id_t id_) : id(id_)
+ {
+ }
-enum consumer_dst_type session_get_consumer_destination_type(
- const struct ltt_session *session);
-const char *session_get_net_consumer_hostname(
- const struct ltt_session *session);
-void session_get_net_consumer_ports(
- const struct ltt_session *session,
- uint16_t *control_port, uint16_t *data_port);
-struct lttng_trace_archive_location *session_get_trace_archive_location(
- const struct ltt_session *session);
+ /*
+ * parameter doesn't have enough information to do this safely; it it
+ * delegated to its parent which uses placement new.
+ */
+ parameter()
+ {
+ }
-struct ltt_session *session_find_by_name(const char *name);
-struct ltt_session *session_find_by_id(uint64_t id);
+ parameter(const parameter&) = delete;
+ parameter(const parameter&&) = delete;
+ parameter& operator=(parameter&) = delete;
+ parameter& operator=(parameter&&) = delete;
-struct ltt_session_list *session_get_list(void);
-void session_list_wait_empty(void);
+ ~parameter()
+ {
+ }
-bool session_access_ok(struct ltt_session *session, uid_t uid);
+ std::string name;
+ ltt_session::id_t id;
+ } parameter;
+ };
-int session_reset_rotation_state(struct ltt_session *session,
- enum lttng_rotation_state result);
+ session_not_found_error(const std::string& session_name,
+ const lttng::source_location& source_location_) :
+ lttng::runtime_error(fmt::format("Session not found: name=`{}`", session_name),
+ source_location_),
+ query_parameter(session_name)
+ {
+ }
+
+ session_not_found_error(ltt_session::id_t session_id,
+ const lttng::source_location& source_location_) :
+ lttng::runtime_error("Session not found: id=" + std::to_string(session_id),
+ source_location_),
+ query_parameter(session_id)
+ {
+ }
+
+ session_not_found_error(const session_not_found_error& other) = default;
+ ~session_not_found_error() noexcept override = default;
+ /*
+ * Setting an explicit `noexcept` causes compilation failure on gcc < 5.0
+ * @see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59526
+ */
+ session_not_found_error(session_not_found_error&& other) /* noexcept */ = default;
+ session_not_found_error& operator=(const session_not_found_error&) = delete;
+ session_not_found_error& operator=(session_not_found_error&&) noexcept = delete;
+
+ query_parameter query_parameter;
+};
+} // namespace exceptions
+} /* namespace sessiond */
+} /* namespace lttng */
+
+void session_destroy(struct ltt_session *session);
+int session_add_destroy_notifier(const ltt_session::locked_ref& session,
+ ltt_session_destroy_notifier notifier,
+ void *user_data);
+
+int session_add_clear_notifier(const ltt_session::locked_ref& session,
+ ltt_session_clear_notifier notifier,
+ void *user_data);
+void session_notify_clear(const ltt_session::locked_ref& session);
+
+enum consumer_dst_type
+session_get_consumer_destination_type(const ltt_session::locked_ref& session);
+const char *session_get_net_consumer_hostname(const ltt_session::locked_ref& session);
+void session_get_net_consumer_ports(const ltt_session::locked_ref& session,
+ uint16_t *control_port,
+ uint16_t *data_port);
+struct lttng_trace_archive_location *
+session_get_trace_archive_location(const ltt_session::locked_ref& session);
+
+struct ltt_session_list *session_get_list();
+void session_list_wait_empty(std::unique_lock<std::mutex> list_lock);
+
+bool session_access_ok(const ltt_session::locked_ref& session, uid_t uid);
+
+int session_reset_rotation_state(const ltt_session::locked_ref& session,
+ enum lttng_rotation_state result);
/* Create a new trace chunk object from the session's configuration. */
-struct lttng_trace_chunk *session_create_new_trace_chunk(
- const struct ltt_session *session,
- const struct consumer_output *consumer_output_override,
- const char *session_base_path_override,
- const char *chunk_name_override);
+struct lttng_trace_chunk *
+session_create_new_trace_chunk(const ltt_session::locked_ref& session,
+ const struct consumer_output *consumer_output_override,
+ const char *session_base_path_override,
+ const char *chunk_name_override);
/*
* Set `new_trace_chunk` as the session's current trace chunk. A reference
* A reference to the session's current trace chunk is returned through
* `current_session_trace_chunk` on success.
*/
-int session_set_trace_chunk(struct ltt_session *session,
- struct lttng_trace_chunk *new_trace_chunk,
- struct lttng_trace_chunk **current_session_trace_chunk);
+int session_set_trace_chunk(const ltt_session::locked_ref& session,
+ struct lttng_trace_chunk *new_trace_chunk,
+ struct lttng_trace_chunk **current_session_trace_chunk);
/*
* Close a chunk on the remote peers of a session. Has no effect on the
* ltt_session itself.
*/
-int session_close_trace_chunk(struct ltt_session *session,
- struct lttng_trace_chunk *trace_chunk,
- enum lttng_trace_chunk_command_type close_command,
- char *path);
+int session_close_trace_chunk(const ltt_session::locked_ref& session,
+ struct lttng_trace_chunk *trace_chunk,
+ enum lttng_trace_chunk_command_type close_command,
+ char *path);
/* Open a packet in all channels of a given session. */
-enum lttng_error_code session_open_packets(struct ltt_session *session);
+enum lttng_error_code session_open_packets(const ltt_session::locked_ref& session);
bool session_output_supports_trace_chunks(const struct ltt_session *session);
*/
bool sample_session_id_by_name(const char *name, uint64_t *id);
+const char *session_get_base_path(const ltt_session::locked_ref& session);
+
+#ifdef HAVE_LIBLTTNG_UST_CTL
+
+enum lttng_error_code ust_app_rotate_session(const ltt_session::locked_ref& session);
+enum lttng_error_code ust_app_clear_session(const ltt_session::locked_ref& session);
+enum lttng_error_code ust_app_open_packets(const ltt_session::locked_ref& session);
+
+#else /* HAVE_LIBLTTNG_UST_CTL */
+
+static inline enum lttng_error_code ust_app_rotate_session(const ltt_session::locked_ref& session
+ __attribute__((unused)))
+{
+ return LTTNG_ERR_UNK;
+}
+
+static inline enum lttng_error_code ust_app_clear_session(const ltt_session::locked_ref& session
+ __attribute__((unused)))
+{
+ return LTTNG_ERR_UNK;
+}
+
+static inline enum lttng_error_code ust_app_open_packets(const ltt_session::locked_ref& session
+ __attribute__((unused)))
+{
+ return LTTNG_ERR_UNK;
+}
+
+#endif /* HAVE_LIBLTTNG_UST_CTL */
+
#endif /* _LTT_SESSION_H */