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>
21 #include <lttng/location.h>
22 #include <lttng/lttng-error.h>
23 #include <lttng/rotation.h>
25 #include <condition_variable>
29 #include <urcu/list.h>
31 #define ASSERT_SESSION_LIST_LOCKED() LTTNG_ASSERT(session_trylock_list())
33 struct ltt_ust_session;
35 using ltt_session_destroy_notifier = void (*)(const struct ltt_session *, void *);
36 using ltt_session_clear_notifier = void (*)(const struct ltt_session *, void *);
39 struct ltt_session_list;
42 session_create(const char *name, uid_t uid, gid_t gid, struct ltt_session **out_session);
43 void session_lock(struct ltt_session *session);
44 void session_unlock(struct ltt_session *session);
46 bool session_get(struct ltt_session *session);
47 void session_put(struct ltt_session *session);
50 * The session list lock covers more ground than its name implies. While
51 * it does protect against concurent mutations of the session list, it is
52 * also used as a multi-session lock when synchronizing newly-registered
53 * 'user space tracer' and 'agent' applications.
55 * In other words, it prevents tracer configurations from changing while they
56 * are being transmitted to the various applications.
58 int session_trylock_list() noexcept;
60 #define LTTNG_THROW_SESSION_NOT_FOUND_BY_NAME_ERROR(session_name) \
61 throw lttng::sessiond::exceptions::session_not_found_error(session_name, \
62 LTTNG_SOURCE_LOCATION())
63 #define LTTNG_THROW_SESSION_NOT_FOUND_BY_ID_ERROR(id) \
64 throw lttng::sessiond::exceptions::session_not_found_error(id, LTTNG_SOURCE_LOCATION())
66 void session_destroy(struct ltt_session *session);
67 int session_add_destroy_notifier(struct ltt_session *session,
68 ltt_session_destroy_notifier notifier,
71 int session_add_clear_notifier(struct ltt_session *session,
72 ltt_session_clear_notifier notifier,
74 void session_notify_clear(ltt_session& session);
76 enum consumer_dst_type session_get_consumer_destination_type(const struct ltt_session *session);
77 const char *session_get_net_consumer_hostname(const struct ltt_session *session);
78 void session_get_net_consumer_ports(const struct ltt_session *session,
79 uint16_t *control_port,
81 struct lttng_trace_archive_location *
82 session_get_trace_archive_location(const struct ltt_session *session);
84 struct ltt_session_list *session_get_list();
85 void session_list_wait_empty(std::unique_lock<std::mutex> list_lock);
87 bool session_access_ok(struct ltt_session *session, uid_t uid);
89 int session_reset_rotation_state(ltt_session& session, enum lttng_rotation_state result);
91 /* Create a new trace chunk object from the session's configuration. */
92 struct lttng_trace_chunk *
93 session_create_new_trace_chunk(const struct ltt_session *session,
94 const struct consumer_output *consumer_output_override,
95 const char *session_base_path_override,
96 const char *chunk_name_override);
99 * Set `new_trace_chunk` as the session's current trace chunk. A reference
100 * to `new_trace_chunk` is acquired by the session. The chunk is created
101 * on remote peers (consumer and relay daemons).
103 * A reference to the session's current trace chunk is returned through
104 * `current_session_trace_chunk` on success.
106 int session_set_trace_chunk(struct ltt_session *session,
107 struct lttng_trace_chunk *new_trace_chunk,
108 struct lttng_trace_chunk **current_session_trace_chunk);
111 * Close a chunk on the remote peers of a session. Has no effect on the
112 * ltt_session itself.
114 int session_close_trace_chunk(struct ltt_session *session,
115 struct lttng_trace_chunk *trace_chunk,
116 enum lttng_trace_chunk_command_type close_command,
119 /* Open a packet in all channels of a given session. */
120 enum lttng_error_code session_open_packets(struct ltt_session *session);
122 bool session_output_supports_trace_chunks(const struct ltt_session *session);
125 * Sample the id of a session looked up via its name.
126 * Here the term "sampling" hint the caller that this return the id at a given
127 * point in time with no guarantee that the session for which the id was
128 * sampled still exist at that point.
130 * Return 0 when the session is not found,
131 * Return 1 when the session is found and set `id`.
133 bool sample_session_id_by_name(const char *name, uint64_t *id);
136 * Tracing session list
138 * Statically declared in session.c and can be accessed by using
139 * session_get_list() function that returns the pointer to the list.
141 struct ltt_session_list {
143 * This lock protects any read/write access to the list and
144 * next_uuid. All public functions in session.c acquire this
145 * lock and release it before returning. If none of those
146 * functions are used, the lock MUST be acquired in order to
147 * iterate or/and do any actions on that list.
151 * This condition variable is signaled on every removal from
154 std::condition_variable removal_cond;
157 * Session unique ID generator. The session list lock MUST be
158 * upon update and read of this counter.
160 uint64_t next_uuid = 0;
162 /* Linked list head */
163 struct cds_list_head head = CDS_LIST_HEAD_INIT(head);
167 * This data structure contains information needed to identify a tracing
168 * session for both LTTng and UST.
171 using id_t = uint64_t;
174 static void _locked_session_release(ltt_session *session);
175 static void _locked_const_session_release(const ltt_session *session);
176 static void _const_session_put(const ltt_session *session);
177 static void _const_session_unlock(const ltt_session &session);
181 std::unique_ptr<ltt_session,
182 lttng::memory::create_deleter_class<
184 ltt_session::_locked_session_release>::deleter>;
185 using ref = std::unique_ptr<
187 lttng::memory::create_deleter_class<ltt_session, session_put>::deleter>;
188 using const_locked_ref =
189 std::unique_ptr<const ltt_session,
190 lttng::memory::create_deleter_class<
192 ltt_session::_locked_const_session_release>::deleter>;
193 using const_ref = std::unique_ptr<
195 lttng::memory::create_deleter_class<const ltt_session, ltt_session::_const_session_put>::deleter>;
197 void lock() const noexcept;
198 void unlock() const noexcept;
201 * Session list lock must be acquired by the caller.
203 * The caller must not keep the ownership of the returned locked session
204 * for longer than strictly necessary. If your intention is to acquire
205 * a reference to an ltt_session, see `find_session()`.
207 static locked_ref find_locked_session(ltt_session::id_t id);
208 static locked_ref find_locked_session(lttng::c_string_view name);
209 static const_locked_ref find_locked_const_session(ltt_session::id_t id);
210 static const_locked_ref find_locked_const_session(lttng::c_string_view name);
212 static ref find_session(ltt_session::id_t id);
213 static ref find_session(lttng::c_string_view name);
214 static const_ref find_const_session(ltt_session::id_t id);
215 static const_ref find_const_session(lttng::c_string_view name);
218 bool has_auto_generated_name;
219 bool name_contains_creation_time;
220 char hostname[LTTNG_HOST_NAME_MAX]; /* Local hostname. */
221 /* Path of the last closed chunk. */
222 char last_chunk_path[LTTNG_PATH_MAX];
223 time_t creation_time;
224 struct ltt_kernel_session *kernel_session;
225 struct ltt_ust_session *ust_session;
226 mutable struct urcu_ref ref_count;
228 * Protect any read/write on this session data structure. This lock must be
229 * acquired *before* using any public functions declared below. Use
230 * session_lock() and session_unlock() for that.
232 mutable pthread_mutex_t _lock;
233 struct cds_list_head list;
234 /* session unique identifier */
236 /* Indicates if the session has been added to the session list and ht.*/
238 /* Indicates if a destroy command has been applied to this session. */
240 /* UID/GID of the user owning the session */
244 * Network session handle. A value of 0 means that there is no remote
245 * session established.
249 * This consumer is only set when the create_session_uri call is made.
250 * This contains the temporary information for a consumer output. Upon
251 * creation of the UST or kernel session, this consumer, if available, is
252 * copied into those sessions.
254 struct consumer_output *consumer;
256 * Indicates whether or not the user has specified an output directory
257 * or if it was configured using the default configuration.
259 bool has_user_specified_directory;
260 /* Did at least ONE start command has been triggered?. */
261 bool has_been_started;
262 /* Is the session active? */
265 /* Snapshot representation in a session. */
266 struct snapshot snapshot;
267 /* Indicate if the session has to output the traces or not. */
268 unsigned int output_traces;
270 * This session is in snapshot mode. This means that channels enabled
271 * will be set in overwrite mode by default and must be in mmap
272 * output mode. Note that snapshots can be taken on a session that
273 * is not in "snapshot_mode". This parameter only affects channel
276 unsigned int snapshot_mode;
278 * A session that has channels that don't use 'mmap' output can't be
279 * used to capture snapshots. This is set to true whenever a
280 * 'splice' kernel channel is enabled.
282 bool has_non_mmap_channel;
284 * Timer set when the session is created for live reading.
286 unsigned int live_timer;
288 * Path where to keep the shared memory files.
290 char shm_path[PATH_MAX];
292 * Node in ltt_sessions_ht_by_id.
294 struct lttng_ht_node_u64 node;
296 * Node in ltt_sessions_ht_by_name.
298 struct lttng_ht_node_str node_by_name;
300 * Timer to check periodically if a relay and/or consumer has completed
303 bool rotation_pending_check_timer_enabled;
304 timer_t rotation_pending_check_timer;
305 /* Timer to periodically rotate a session. */
306 bool rotation_schedule_timer_enabled;
307 timer_t rotation_schedule_timer;
308 /* Value for periodic rotations, 0 if disabled. */
309 uint64_t rotate_timer_period;
310 /* Value for size-based rotations, 0 if disabled. */
311 uint64_t rotate_size;
313 * Keep a state if this session was rotated after the last stop command.
314 * We only allow one rotation after a stop. At destroy, we also need to
315 * know if a rotation occurred since the last stop to rename the current
316 * chunk. After a stop followed by rotate, all subsequent clear
317 * (without prior start) will succeed, but will be effect-less.
319 bool rotated_after_last_stop;
321 * Track whether the session was cleared after last stop. All subsequent
322 * clear (without prior start) will succeed, but will be effect-less. A
323 * subsequent rotate (without prior start) will return an error.
325 bool cleared_after_last_stop;
327 * True if the session has had an explicit non-quiet rotation.
331 * Trigger for size-based rotations.
333 struct lttng_trigger *rotate_trigger;
334 LTTNG_OPTIONAL(uint64_t) most_recent_chunk_id;
335 struct lttng_trace_chunk *current_trace_chunk;
336 struct lttng_trace_chunk *chunk_being_archived;
337 /* Current state of a rotation. */
338 enum lttng_rotation_state rotation_state;
340 char *last_archived_chunk_name;
341 LTTNG_OPTIONAL(uint64_t) last_archived_chunk_id;
342 struct lttng_dynamic_array destroy_notifiers;
343 struct lttng_dynamic_array clear_notifiers;
344 /* Session base path override. Set non-null. */
353 std::unique_lock<std::mutex> lock_session_list();
355 namespace exceptions {
357 * @class session_not_found_error
358 * @brief Represents a session-not-found error and provides the parameters used to query the session
359 * for use by error-reporting code.
361 class session_not_found_error : public lttng::runtime_error {
363 class query_parameter {
365 enum class query_type : std::uint8_t { BY_NAME, BY_ID };
368 * Intentionally not explicit to allow construction from c-style strings,
369 * std::string, and lttng::c_string_view.
371 * NOLINTBEGIN(google-explicit-constructor)
373 query_parameter(const std::string& session_name) :
374 type(query_type::BY_NAME), parameter(session_name)
377 /* NOLINTEND(google-explicit-constructor) */
379 explicit query_parameter(ltt_session::id_t id_) : type(query_type::BY_ID), parameter(id_)
383 query_parameter(const query_parameter& other) : type(other.type)
385 if (type == query_type::BY_NAME) {
386 new (¶meter.name) std::string(other.parameter.name);
388 parameter.id = other.parameter.id;
394 if (type == query_type::BY_NAME) {
395 parameter.name.~basic_string();
401 explicit parameter(std::string name_) : name(std::move(name_))
405 explicit parameter(ltt_session::id_t id_) : id(id_)
410 * parameter doesn't have enough information to do this safely; it it
411 * delegated to its parent which uses placement new.
417 parameter(const parameter&) = delete;
418 parameter(const parameter&&) = delete;
419 parameter& operator=(parameter&) = delete;
420 parameter& operator=(parameter&&) = delete;
427 ltt_session::id_t id;
431 session_not_found_error(const std::string& session_name,
432 const lttng::source_location& source_location_) :
433 lttng::runtime_error(fmt::format("Session not found: name=`{}`", session_name),
435 query_parameter(session_name)
439 session_not_found_error(ltt_session::id_t session_id,
440 const lttng::source_location& source_location_) :
441 lttng::runtime_error("Session not found: id=" + std::to_string(session_id),
443 query_parameter(session_id)
447 session_not_found_error(const session_not_found_error& other) = default;
448 ~session_not_found_error() noexcept override = default;
450 query_parameter query_parameter;
452 } // namespace exceptions
453 } /* namespace sessiond */
454 } /* namespace lttng */
456 #endif /* _LTT_SESSION_H */