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"
15 #include <common/dynamic-array.hpp>
16 #include <common/exception.hpp>
17 #include <common/hashtable/hashtable.hpp>
18 #include <common/make-unique-wrapper.hpp>
19 #include <common/pthread-lock.hpp>
20 #include <common/reference.hpp>
22 #include <lttng/location.h>
23 #include <lttng/lttng-error.h>
24 #include <lttng/rotation.h>
26 #include <condition_variable>
30 #include <urcu/list.h>
32 #define ASSERT_SESSION_LIST_LOCKED() LTTNG_ASSERT(session_trylock_list())
34 struct ltt_ust_session;
37 struct ltt_session_list;
40 session_create(const char *name, uid_t uid, gid_t gid, struct ltt_session **out_session);
41 void session_lock(struct ltt_session *session);
42 void session_unlock(struct ltt_session *session);
44 bool session_get(struct ltt_session *session);
45 void session_put(struct ltt_session *session);
48 * The session list lock covers more ground than its name implies. While
49 * it does protect against concurent mutations of the session list, it is
50 * also used as a multi-session lock when synchronizing newly-registered
51 * 'user space tracer' and 'agent' applications.
53 * In other words, it prevents tracer configurations from changing while they
54 * are being transmitted to the various applications.
56 int session_trylock_list() noexcept;
58 #define LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(session_name) \
59 throw lttng::sessiond::exceptions::session_not_found_error(session_name, \
60 LTTNG_SOURCE_LOCATION())
61 #define LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id) \
62 throw lttng::sessiond::exceptions::session_not_found_error(id, LTTNG_SOURCE_LOCATION())
65 * Tracing session list
67 * Statically declared in session.c and can be accessed by using
68 * session_get_list() function that returns the pointer to the list.
70 struct ltt_session_list {
72 * This lock protects any read/write access to the list and
73 * next_uuid. All public functions in session.c acquire this
74 * lock and release it before returning. If none of those
75 * functions are used, the lock MUST be acquired in order to
76 * iterate or/and do any actions on that list.
80 * This condition variable is signaled on every removal from
83 std::condition_variable removal_cond;
86 * Session unique ID generator. The session list lock MUST be
87 * upon update and read of this counter.
89 uint64_t next_uuid = 0;
91 /* Linked list head */
92 struct cds_list_head head = CDS_LIST_HEAD_INIT(head);
96 * This data structure contains information needed to identify a tracing
97 * session for both LTTng and UST.
100 using id_t = uint64_t;
103 static void _locked_session_release(ltt_session *session);
104 static void _locked_const_session_release(const ltt_session *session);
105 static void _const_session_put(const ltt_session *session);
106 static void _const_session_unlock(const ltt_session& session);
109 using locked_ref = lttng::non_copyable_reference<
111 lttng::memory::create_deleter_class<ltt_session,
112 ltt_session::_locked_session_release>::deleter>;
113 using ref = lttng::non_copyable_reference<
115 lttng::memory::create_deleter_class<ltt_session, session_put>::deleter>;
116 using const_locked_ref = lttng::non_copyable_reference<
118 lttng::memory::create_deleter_class<
120 ltt_session::_locked_const_session_release>::deleter>;
121 using const_ref = lttng::non_copyable_reference<
123 lttng::memory::create_deleter_class<const ltt_session,
124 ltt_session::_const_session_put>::deleter>;
126 void lock() const noexcept;
127 void unlock() const noexcept;
130 * Session list lock must be acquired by the caller.
132 * The caller must not keep the ownership of the returned locked session
133 * for longer than strictly necessary. If your intention is to acquire
134 * a reference to an ltt_session, see `find_session()`.
136 static locked_ref find_locked_session(ltt_session::id_t id);
137 static locked_ref find_locked_session(lttng::c_string_view name);
138 static const_locked_ref find_locked_const_session(ltt_session::id_t id);
139 static const_locked_ref find_locked_const_session(lttng::c_string_view name);
141 static ref find_session(ltt_session::id_t id);
142 static ref find_session(lttng::c_string_view name);
143 static const_ref find_const_session(ltt_session::id_t id);
144 static const_ref find_const_session(lttng::c_string_view name);
147 bool has_auto_generated_name;
148 bool name_contains_creation_time;
149 char hostname[LTTNG_HOST_NAME_MAX]; /* Local hostname. */
150 /* Path of the last closed chunk. */
151 char last_chunk_path[LTTNG_PATH_MAX];
152 time_t creation_time;
153 struct ltt_kernel_session *kernel_session;
154 struct ltt_ust_session *ust_session;
155 mutable struct urcu_ref ref_count;
157 * Protect any read/write on this session data structure. This lock must be
158 * acquired *before* using any public functions declared below. Use
159 * session_lock() and session_unlock() for that.
161 mutable pthread_mutex_t _lock;
162 struct cds_list_head list;
163 /* session unique identifier */
165 /* Indicates if the session has been added to the session list and ht.*/
167 /* Indicates if a destroy command has been applied to this session. */
169 /* UID/GID of the user owning the session */
173 * Network session handle. A value of 0 means that there is no remote
174 * session established.
178 * This consumer is only set when the create_session_uri call is made.
179 * This contains the temporary information for a consumer output. Upon
180 * creation of the UST or kernel session, this consumer, if available, is
181 * copied into those sessions.
183 struct consumer_output *consumer;
185 * Indicates whether or not the user has specified an output directory
186 * or if it was configured using the default configuration.
188 bool has_user_specified_directory;
189 /* Did at least ONE start command has been triggered?. */
190 bool has_been_started;
191 /* Is the session active? */
194 /* Snapshot representation in a session. */
195 struct snapshot snapshot;
196 /* Indicate if the session has to output the traces or not. */
197 unsigned int output_traces;
199 * This session is in snapshot mode. This means that channels enabled
200 * will be set in overwrite mode by default and must be in mmap
201 * output mode. Note that snapshots can be taken on a session that
202 * is not in "snapshot_mode". This parameter only affects channel
205 unsigned int snapshot_mode;
207 * A session that has channels that don't use 'mmap' output can't be
208 * used to capture snapshots. This is set to true whenever a
209 * 'splice' kernel channel is enabled.
211 bool has_non_mmap_channel;
213 * Timer set when the session is created for live reading.
215 unsigned int live_timer;
217 * Path where to keep the shared memory files.
219 char shm_path[PATH_MAX];
221 * Node in ltt_sessions_ht_by_id.
223 struct lttng_ht_node_u64 node;
225 * Node in ltt_sessions_ht_by_name.
227 struct lttng_ht_node_str node_by_name;
229 * Timer to check periodically if a relay and/or consumer has completed
232 bool rotation_pending_check_timer_enabled;
233 timer_t rotation_pending_check_timer;
234 /* Timer to periodically rotate a session. */
235 bool rotation_schedule_timer_enabled;
236 timer_t rotation_schedule_timer;
237 /* Value for periodic rotations, 0 if disabled. */
238 uint64_t rotate_timer_period;
239 /* Value for size-based rotations, 0 if disabled. */
240 uint64_t rotate_size;
242 * Keep a state if this session was rotated after the last stop command.
243 * We only allow one rotation after a stop. At destroy, we also need to
244 * know if a rotation occurred since the last stop to rename the current
245 * chunk. After a stop followed by rotate, all subsequent clear
246 * (without prior start) will succeed, but will be effect-less.
248 bool rotated_after_last_stop;
250 * Track whether the session was cleared after last stop. All subsequent
251 * clear (without prior start) will succeed, but will be effect-less. A
252 * subsequent rotate (without prior start) will return an error.
254 bool cleared_after_last_stop;
256 * True if the session has had an explicit non-quiet rotation.
260 * Trigger for size-based rotations.
262 struct lttng_trigger *rotate_trigger;
263 LTTNG_OPTIONAL(uint64_t) most_recent_chunk_id;
264 struct lttng_trace_chunk *current_trace_chunk;
265 struct lttng_trace_chunk *chunk_being_archived;
266 /* Current state of a rotation. */
267 enum lttng_rotation_state rotation_state;
269 char *last_archived_chunk_name;
270 LTTNG_OPTIONAL(uint64_t) last_archived_chunk_id;
271 struct lttng_dynamic_array destroy_notifiers;
272 struct lttng_dynamic_array clear_notifiers;
273 /* Session base path override. Set non-null. */
278 * Destruction notifiers are invoked in an exclusive context. There is no need for the session to be
279 * locked nor for a reference to be acquired.
281 using ltt_session_destroy_notifier = void (*)(const ltt_session::locked_ref&, void *);
282 using ltt_session_clear_notifier = void (*)(const ltt_session::locked_ref&, void *);
287 std::unique_lock<std::mutex> lock_session_list();
289 namespace exceptions {
291 * @class session_not_found_error
292 * @brief Represents a session-not-found error and provides the parameters used to query the session
293 * for use by error-reporting code.
295 class session_not_found_error : public lttng::runtime_error {
297 class query_parameter {
299 enum class query_type : std::uint8_t { BY_NAME, BY_ID };
302 * Intentionally not explicit to allow construction from c-style strings,
303 * std::string, and lttng::c_string_view.
305 * NOLINTBEGIN(google-explicit-constructor)
307 query_parameter(const std::string& session_name) :
308 type(query_type::BY_NAME), parameter(session_name)
311 /* NOLINTEND(google-explicit-constructor) */
313 explicit query_parameter(ltt_session::id_t id_) :
314 type(query_type::BY_ID), parameter(id_)
318 query_parameter(const query_parameter& other) : type(other.type)
320 if (type == query_type::BY_NAME) {
321 new (¶meter.name) std::string(other.parameter.name);
323 parameter.id = other.parameter.id;
329 if (type == query_type::BY_NAME) {
330 parameter.name.~basic_string();
336 explicit parameter(std::string name_) : name(std::move(name_))
340 explicit parameter(ltt_session::id_t id_) : id(id_)
345 * parameter doesn't have enough information to do this safely; it it
346 * delegated to its parent which uses placement new.
352 parameter(const parameter&) = delete;
353 parameter(const parameter&&) = delete;
354 parameter& operator=(parameter&) = delete;
355 parameter& operator=(parameter&&) = delete;
362 ltt_session::id_t id;
366 session_not_found_error(const std::string& session_name,
367 const lttng::source_location& source_location_) :
368 lttng::runtime_error(fmt::format("Session not found: name=`{}`", session_name),
370 query_parameter(session_name)
374 session_not_found_error(ltt_session::id_t session_id,
375 const lttng::source_location& source_location_) :
376 lttng::runtime_error("Session not found: id=" + std::to_string(session_id),
378 query_parameter(session_id)
382 session_not_found_error(const session_not_found_error& other) = default;
383 ~session_not_found_error() noexcept override = default;
385 query_parameter query_parameter;
387 } // namespace exceptions
388 } /* namespace sessiond */
389 } /* namespace lttng */
391 void session_destroy(struct ltt_session *session);
392 int session_add_destroy_notifier(const ltt_session::locked_ref& session,
393 ltt_session_destroy_notifier notifier,
396 int session_add_clear_notifier(const ltt_session::locked_ref& session,
397 ltt_session_clear_notifier notifier,
399 void session_notify_clear(const ltt_session::locked_ref& session);
401 enum consumer_dst_type
402 session_get_consumer_destination_type(const ltt_session::locked_ref& session);
403 const char *session_get_net_consumer_hostname(const ltt_session::locked_ref& session);
404 void session_get_net_consumer_ports(const ltt_session::locked_ref& session,
405 uint16_t *control_port,
406 uint16_t *data_port);
407 struct lttng_trace_archive_location *
408 session_get_trace_archive_location(const ltt_session::locked_ref& session);
410 struct ltt_session_list *session_get_list();
411 void session_list_wait_empty(std::unique_lock<std::mutex> list_lock);
413 bool session_access_ok(const ltt_session::locked_ref& session, uid_t uid);
415 int session_reset_rotation_state(const ltt_session::locked_ref& session,
416 enum lttng_rotation_state result);
418 /* Create a new trace chunk object from the session's configuration. */
419 struct lttng_trace_chunk *
420 session_create_new_trace_chunk(const ltt_session::locked_ref& session,
421 const struct consumer_output *consumer_output_override,
422 const char *session_base_path_override,
423 const char *chunk_name_override);
426 * Set `new_trace_chunk` as the session's current trace chunk. A reference
427 * to `new_trace_chunk` is acquired by the session. The chunk is created
428 * on remote peers (consumer and relay daemons).
430 * A reference to the session's current trace chunk is returned through
431 * `current_session_trace_chunk` on success.
433 int session_set_trace_chunk(const ltt_session::locked_ref& session,
434 struct lttng_trace_chunk *new_trace_chunk,
435 struct lttng_trace_chunk **current_session_trace_chunk);
438 * Close a chunk on the remote peers of a session. Has no effect on the
439 * ltt_session itself.
441 int session_close_trace_chunk(const ltt_session::locked_ref& session,
442 struct lttng_trace_chunk *trace_chunk,
443 enum lttng_trace_chunk_command_type close_command,
446 /* Open a packet in all channels of a given session. */
447 enum lttng_error_code session_open_packets(const ltt_session::locked_ref& session);
449 bool session_output_supports_trace_chunks(const struct ltt_session *session);
452 * Sample the id of a session looked up via its name.
453 * Here the term "sampling" hint the caller that this return the id at a given
454 * point in time with no guarantee that the session for which the id was
455 * sampled still exist at that point.
457 * Return 0 when the session is not found,
458 * Return 1 when the session is found and set `id`.
460 bool sample_session_id_by_name(const char *name, uint64_t *id);
462 #endif /* _LTT_SESSION_H */