2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include "consumer.hpp"
12 #include "snapshot.hpp"
13 #include "trace-kernel.hpp"
14 #include "trace-ust.hpp"
15 #include "ust-app.hpp"
17 #include <common/dynamic-array.hpp>
18 #include <common/exception.hpp>
19 #include <common/hashtable/hashtable.hpp>
20 #include <common/make-unique-wrapper.hpp>
21 #include <common/pthread-lock.hpp>
22 #include <common/reference.hpp>
23 #include <common/urcu.hpp>
25 #include <lttng/location.h>
26 #include <lttng/lttng-error.h>
27 #include <lttng/rotation.h>
29 #include <condition_variable>
33 #include <urcu/list.h>
35 #define ASSERT_SESSION_LIST_LOCKED() LTTNG_ASSERT(session_trylock_list())
37 struct ltt_ust_session;
40 struct ltt_session_list;
41 struct buffer_reg_uid;
44 session_create(const char *name, uid_t uid, gid_t gid, struct ltt_session **out_session);
45 void session_lock(const ltt_session *session);
46 void session_unlock(const ltt_session *session);
48 bool session_get(struct ltt_session *session);
49 void session_put(struct ltt_session *session);
52 * The session list lock covers more ground than its name implies. While
53 * it does protect against concurent mutations of the session list, it is
54 * also used as a multi-session lock when synchronizing newly-registered
55 * 'user space tracer' and 'agent' applications.
57 * In other words, it prevents tracer configurations from changing while they
58 * are being transmitted to the various applications.
60 int session_trylock_list() noexcept;
62 #define LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(session_name) \
63 throw lttng::sessiond::exceptions::session_not_found_error(session_name, \
64 LTTNG_SOURCE_LOCATION())
65 #define LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id) \
66 throw lttng::sessiond::exceptions::session_not_found_error(id, LTTNG_SOURCE_LOCATION())
69 * Tracing session list
71 * Statically declared in session.c and can be accessed by using
72 * session_get_list() function that returns the pointer to the list.
74 struct ltt_session_list {
76 * This lock protects any read/write access to the list and
77 * next_uuid. All public functions in session.c acquire this
78 * lock and release it before returning. If none of those
79 * functions are used, the lock MUST be acquired in order to
80 * iterate or/and do any actions on that list.
84 * This condition variable is signaled on every removal from
87 std::condition_variable removal_cond;
90 * Session unique ID generator. The session list lock MUST be
91 * upon update and read of this counter.
93 uint64_t next_uuid = 0;
95 /* Linked list head */
96 struct cds_list_head head = CDS_LIST_HEAD_INIT(head);
101 class user_space_consumer_channel_keys {
107 enum class consumer_bitness : std::uint8_t {
112 enum class channel_type : std::uint8_t {
117 iterator begin() const noexcept;
118 iterator end() const noexcept;
121 enum class _iteration_mode : std::uint8_t {
127 struct _iterator_creation_context {
128 const _iteration_mode _mode;
129 const ltt_ust_session& _session;
132 const cds_list_head *buffer_registry;
137 class iterator : public std::iterator<std::input_iterator_tag, std::uint64_t> {
138 friend user_space_consumer_channel_keys;
142 /* Bitness is needed to query the appropriate consumer daemon. */
143 consumer_bitness bitness;
144 std::uint64_t key_value;
147 bool operator==(const key& other)
149 return bitness == other.bitness && key_value == other.key_value &&
155 * Copy constructor disabled since it would require handling the copy of locked
158 iterator(const iterator& other) = delete;
159 iterator(iterator&& other) = default;
160 ~iterator() = default;
162 iterator& operator++();
163 bool operator==(const iterator& other) const noexcept;
164 bool operator!=(const iterator& other) const noexcept;
165 key operator*() const;
168 * Get the session registry of the channel currently
169 * pointed by the iterator. Never returns nullptr.
171 lttng::sessiond::ust::registry_session *get_registry_session();
174 struct _iterator_position {
176 lttng_ht_iter app_iterator = {};
177 nonstd::optional<ust_app_session::locked_weak_ref>
179 lttng::sessiond::ust::registry_session *current_registry_session =
183 buffer_reg_uid *current_registry = nullptr;
186 lttng_ht_iter channel_iterator = {};
189 explicit iterator(const _iterator_creation_context& creation_context,
190 bool is_end = false);
192 void _init_per_pid() noexcept;
193 void _skip_to_next_app_per_pid(bool try_current) noexcept;
194 void _advance_one_per_pid();
195 key _get_current_value_per_pid() const noexcept;
196 lttng::sessiond::ust::registry_session *_get_registry_session_per_pid();
198 void _init_per_uid() noexcept;
199 void _advance_one_per_uid();
200 key _get_current_value_per_uid() const noexcept;
201 lttng::sessiond::ust::registry_session *_get_registry_session_per_uid();
203 const _iterator_creation_context& _creation_context;
204 _iterator_position _position;
209 user_space_consumer_channel_keys(const ltt_ust_session& ust_session, lttng_ht& apps) :
210 _creation_context{ _iteration_mode::PER_PID, ust_session, { .apps = &apps } }
214 user_space_consumer_channel_keys(const ltt_ust_session& ust_session,
215 const cds_list_head& buffer_registry) :
216 _creation_context{ _iteration_mode::PER_UID,
218 { .buffer_registry = &buffer_registry } }
222 class _scoped_rcu_read_lock {
224 _scoped_rcu_read_lock()
229 ~_scoped_rcu_read_lock()
236 _scoped_rcu_read_lock(_scoped_rcu_read_lock&& other)
238 other._armed = false;
245 _scoped_rcu_read_lock _read_lock;
246 _iterator_creation_context _creation_context;
248 } /* namespace sessiond */
249 } /* namespace lttng */
252 * This data structure contains information needed to identify a tracing
253 * session for both LTTng and UST.
256 using id_t = uint64_t;
257 friend lttng::sessiond::user_space_consumer_channel_keys::iterator;
260 static void _locked_session_release(ltt_session *session);
261 static void _locked_const_session_release(const ltt_session *session);
262 static void _const_session_put(const ltt_session *session);
263 static void _const_session_unlock(const ltt_session& session);
266 using locked_ref = lttng::non_copyable_reference<
268 lttng::memory::create_deleter_class<ltt_session,
269 ltt_session::_locked_session_release>::deleter>;
270 using ref = lttng::non_copyable_reference<
272 lttng::memory::create_deleter_class<ltt_session, session_put>::deleter>;
273 using const_locked_ref = lttng::non_copyable_reference<
275 lttng::memory::create_deleter_class<
277 ltt_session::_locked_const_session_release>::deleter>;
278 using const_ref = lttng::non_copyable_reference<
280 lttng::memory::create_deleter_class<const ltt_session,
281 ltt_session::_const_session_put>::deleter>;
283 static locked_ref make_locked_ref(ltt_session& session)
285 return lttng::make_non_copyable_reference<locked_ref::referenced_type,
286 locked_ref::deleter>(session);
289 static const_locked_ref make_locked_ref(const ltt_session& session)
291 return lttng::make_non_copyable_reference<const_locked_ref::referenced_type,
292 const_locked_ref::deleter>(session);
295 static ref make_ref(ltt_session& session)
297 return lttng::make_non_copyable_reference<ref::referenced_type, ref::deleter>(
301 static const_ref make_ref(const ltt_session& session)
303 return lttng::make_non_copyable_reference<const_ref::referenced_type,
304 const_ref::deleter>(session);
307 void lock() const noexcept;
308 void unlock() const noexcept;
310 lttng::sessiond::user_space_consumer_channel_keys user_space_consumer_channel_keys() const;
313 * Session list lock must be acquired by the caller.
315 * The caller must not keep the ownership of the returned locked session
316 * for longer than strictly necessary. If your intention is to acquire
317 * a reference to an ltt_session, see `find_session()`.
319 static locked_ref find_locked_session(ltt_session::id_t id);
320 static locked_ref find_locked_session(lttng::c_string_view name);
321 static const_locked_ref find_locked_const_session(ltt_session::id_t id);
322 static const_locked_ref find_locked_const_session(lttng::c_string_view name);
324 static ref find_session(ltt_session::id_t id);
325 static ref find_session(lttng::c_string_view name);
326 static const_ref find_const_session(ltt_session::id_t id);
327 static const_ref find_const_session(lttng::c_string_view name);
330 bool has_auto_generated_name;
331 bool name_contains_creation_time;
332 char hostname[LTTNG_HOST_NAME_MAX]; /* Local hostname. */
333 /* Path of the last closed chunk. */
334 char last_chunk_path[LTTNG_PATH_MAX];
335 time_t creation_time;
336 struct ltt_kernel_session *kernel_session;
337 struct ltt_ust_session *ust_session;
338 mutable struct urcu_ref ref_count;
340 * Protect any read/write on this session data structure. This lock must be
341 * acquired *before* using any public functions declared below. Use
342 * session_lock() and session_unlock() for that.
344 mutable pthread_mutex_t _lock;
345 struct cds_list_head list;
346 /* session unique identifier */
348 /* Indicates if the session has been added to the session list and ht.*/
350 /* Indicates if a destroy command has been applied to this session. */
352 /* UID/GID of the user owning the session */
356 * Network session handle. A value of 0 means that there is no remote
357 * session established.
361 * This consumer is only set when the create_session_uri call is made.
362 * This contains the temporary information for a consumer output. Upon
363 * creation of the UST or kernel session, this consumer, if available, is
364 * copied into those sessions.
366 struct consumer_output *consumer;
368 * Indicates whether or not the user has specified an output directory
369 * or if it was configured using the default configuration.
371 bool has_user_specified_directory;
372 /* Did at least ONE start command has been triggered?. */
373 bool has_been_started;
374 /* Is the session active? */
377 /* Snapshot representation in a session. */
378 struct snapshot snapshot;
379 /* Indicate if the session has to output the traces or not. */
380 unsigned int output_traces;
382 * This session is in snapshot mode. This means that channels enabled
383 * will be set in overwrite mode by default and must be in mmap
384 * output mode. Note that snapshots can be taken on a session that
385 * is not in "snapshot_mode". This parameter only affects channel
388 unsigned int snapshot_mode;
390 * A session that has channels that don't use 'mmap' output can't be
391 * used to capture snapshots. This is set to true whenever a
392 * 'splice' kernel channel is enabled.
394 bool has_non_mmap_channel;
396 * Timer set when the session is created for live reading.
398 unsigned int live_timer;
400 * Path where to keep the shared memory files.
402 char shm_path[PATH_MAX];
404 * Node in ltt_sessions_ht_by_id.
406 struct lttng_ht_node_u64 node;
408 * Node in ltt_sessions_ht_by_name.
410 struct lttng_ht_node_str node_by_name;
412 * Timer to check periodically if a relay and/or consumer has completed
415 bool rotation_pending_check_timer_enabled;
416 timer_t rotation_pending_check_timer;
417 /* Timer to periodically rotate a session. */
418 bool rotation_schedule_timer_enabled;
419 timer_t rotation_schedule_timer;
420 /* Value for periodic rotations, 0 if disabled. */
421 uint64_t rotate_timer_period;
422 /* Value for size-based rotations, 0 if disabled. */
423 uint64_t rotate_size;
425 * Keep a state if this session was rotated after the last stop command.
426 * We only allow one rotation after a stop. At destroy, we also need to
427 * know if a rotation occurred since the last stop to rename the current
428 * chunk. After a stop followed by rotate, all subsequent clear
429 * (without prior start) will succeed, but will be effect-less.
431 bool rotated_after_last_stop;
433 * Track whether the session was cleared after last stop. All subsequent
434 * clear (without prior start) will succeed, but will be effect-less. A
435 * subsequent rotate (without prior start) will return an error.
437 bool cleared_after_last_stop;
439 * True if the session has had an explicit non-quiet rotation.
443 * Trigger for size-based rotations.
445 struct lttng_trigger *rotate_trigger;
446 LTTNG_OPTIONAL(uint64_t) most_recent_chunk_id;
447 struct lttng_trace_chunk *current_trace_chunk;
448 struct lttng_trace_chunk *chunk_being_archived;
449 /* Current state of a rotation. */
450 enum lttng_rotation_state rotation_state;
452 char *last_archived_chunk_name;
453 LTTNG_OPTIONAL(uint64_t) last_archived_chunk_id;
454 struct lttng_dynamic_array destroy_notifiers;
455 struct lttng_dynamic_array clear_notifiers;
456 /* Session base path override. Set non-null. */
461 * Destruction notifiers are invoked in an exclusive context. There is no need for the session to be
462 * locked nor for a reference to be acquired.
464 using ltt_session_destroy_notifier = void (*)(const ltt_session::locked_ref&, void *);
465 using ltt_session_clear_notifier = void (*)(const ltt_session::locked_ref&, void *);
470 std::unique_lock<std::mutex> lock_session_list();
472 namespace exceptions {
474 * @class session_not_found_error
475 * @brief Represents a session-not-found error and provides the parameters used to query the session
476 * for use by error-reporting code.
478 class session_not_found_error : public lttng::runtime_error {
480 class query_parameter {
482 enum class query_type : std::uint8_t { BY_NAME, BY_ID };
485 * Intentionally not explicit to allow construction from c-style strings,
486 * std::string, and lttng::c_string_view.
488 * NOLINTBEGIN(google-explicit-constructor)
490 query_parameter(const std::string& session_name) :
491 type(query_type::BY_NAME), parameter(session_name)
494 /* NOLINTEND(google-explicit-constructor) */
496 explicit query_parameter(ltt_session::id_t id_) :
497 type(query_type::BY_ID), parameter(id_)
501 query_parameter(const query_parameter& other) : type(other.type)
503 if (type == query_type::BY_NAME) {
504 new (¶meter.name) std::string(other.parameter.name);
506 parameter.id = other.parameter.id;
512 if (type == query_type::BY_NAME) {
513 parameter.name.~basic_string();
519 explicit parameter(std::string name_) : name(std::move(name_))
523 explicit parameter(ltt_session::id_t id_) : id(id_)
528 * parameter doesn't have enough information to do this safely; it it
529 * delegated to its parent which uses placement new.
535 parameter(const parameter&) = delete;
536 parameter(const parameter&&) = delete;
537 parameter& operator=(parameter&) = delete;
538 parameter& operator=(parameter&&) = delete;
545 ltt_session::id_t id;
549 session_not_found_error(const std::string& session_name,
550 const lttng::source_location& source_location_) :
551 lttng::runtime_error(fmt::format("Session not found: name=`{}`", session_name),
553 query_parameter(session_name)
557 session_not_found_error(ltt_session::id_t session_id,
558 const lttng::source_location& source_location_) :
559 lttng::runtime_error("Session not found: id=" + std::to_string(session_id),
561 query_parameter(session_id)
565 session_not_found_error(const session_not_found_error& other) = default;
566 ~session_not_found_error() noexcept override = default;
568 query_parameter query_parameter;
570 } // namespace exceptions
571 } /* namespace sessiond */
572 } /* namespace lttng */
574 void session_destroy(struct ltt_session *session);
575 int session_add_destroy_notifier(const ltt_session::locked_ref& session,
576 ltt_session_destroy_notifier notifier,
579 int session_add_clear_notifier(const ltt_session::locked_ref& session,
580 ltt_session_clear_notifier notifier,
582 void session_notify_clear(const ltt_session::locked_ref& session);
584 enum consumer_dst_type
585 session_get_consumer_destination_type(const ltt_session::locked_ref& session);
586 const char *session_get_net_consumer_hostname(const ltt_session::locked_ref& session);
587 void session_get_net_consumer_ports(const ltt_session::locked_ref& session,
588 uint16_t *control_port,
589 uint16_t *data_port);
590 struct lttng_trace_archive_location *
591 session_get_trace_archive_location(const ltt_session::locked_ref& session);
593 struct ltt_session_list *session_get_list();
594 void session_list_wait_empty(std::unique_lock<std::mutex> list_lock);
596 bool session_access_ok(const ltt_session::locked_ref& session, uid_t uid);
598 int session_reset_rotation_state(const ltt_session::locked_ref& session,
599 enum lttng_rotation_state result);
601 /* Create a new trace chunk object from the session's configuration. */
602 struct lttng_trace_chunk *
603 session_create_new_trace_chunk(const ltt_session::locked_ref& session,
604 const struct consumer_output *consumer_output_override,
605 const char *session_base_path_override,
606 const char *chunk_name_override);
609 * Set `new_trace_chunk` as the session's current trace chunk. A reference
610 * to `new_trace_chunk` is acquired by the session. The chunk is created
611 * on remote peers (consumer and relay daemons).
613 * A reference to the session's current trace chunk is returned through
614 * `current_session_trace_chunk` on success.
616 int session_set_trace_chunk(const ltt_session::locked_ref& session,
617 struct lttng_trace_chunk *new_trace_chunk,
618 struct lttng_trace_chunk **current_session_trace_chunk);
621 * Close a chunk on the remote peers of a session. Has no effect on the
622 * ltt_session itself.
624 int session_close_trace_chunk(const ltt_session::locked_ref& session,
625 struct lttng_trace_chunk *trace_chunk,
626 enum lttng_trace_chunk_command_type close_command,
629 /* Open a packet in all channels of a given session. */
630 enum lttng_error_code session_open_packets(const ltt_session::locked_ref& session);
632 bool session_output_supports_trace_chunks(const struct ltt_session *session);
635 * Sample the id of a session looked up via its name.
636 * Here the term "sampling" hint the caller that this return the id at a given
637 * point in time with no guarantee that the session for which the id was
638 * sampled still exist at that point.
640 * Return 0 when the session is not found,
641 * Return 1 when the session is found and set `id`.
643 bool sample_session_id_by_name(const char *name, uint64_t *id);
645 const char *session_get_base_path(const ltt_session::locked_ref& session);
647 #ifdef HAVE_LIBLTTNG_UST_CTL
649 enum lttng_error_code ust_app_rotate_session(const ltt_session::locked_ref& session);
650 enum lttng_error_code ust_app_clear_session(const ltt_session::locked_ref& session);
651 enum lttng_error_code ust_app_open_packets(const ltt_session::locked_ref& session);
653 #else /* HAVE_LIBLTTNG_UST_CTL */
655 static inline enum lttng_error_code ust_app_rotate_session(const ltt_session::locked_ref& session
656 __attribute__((unused)))
658 return LTTNG_ERR_UNK;
661 static inline enum lttng_error_code ust_app_clear_session(const ltt_session::locked_ref& session
662 __attribute__((unused)))
664 return LTTNG_ERR_UNK;
667 static inline enum lttng_error_code ust_app_open_packets(const ltt_session::locked_ref& session
668 __attribute__((unused)))
670 return LTTNG_ERR_UNK;
673 #endif /* HAVE_LIBLTTNG_UST_CTL */
675 #endif /* _LTT_SESSION_H */