sessiond: propagate the use of ltt_session::locked_ref
[lttng-tools.git] / src / bin / lttng-sessiond / session.hpp
CommitLineData
91d76f53 1/*
21cf9b6b 2 * Copyright (C) 2011 EfficiOS Inc.
5b74c7b1 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
5b74c7b1 5 *
5b74c7b1
DG
6 */
7
8#ifndef _LTT_SESSION_H
9#define _LTT_SESSION_H
10
28f23191
JG
11#include "consumer.hpp"
12#include "snapshot.hpp"
13#include "trace-kernel.hpp"
d4a2a84a 14
c9e313bc 15#include <common/dynamic-array.hpp>
d9a970b7 16#include <common/exception.hpp>
28f23191 17#include <common/hashtable/hashtable.hpp>
e99e3664 18#include <common/make-unique-wrapper.hpp>
d7bfb9b0 19#include <common/pthread-lock.hpp>
a0a4f314 20#include <common/reference.hpp>
28f23191 21
5d65beab 22#include <lttng/location.h>
82b69413 23#include <lttng/lttng-error.h>
28f23191 24#include <lttng/rotation.h>
6dc3064a 25
1a496780 26#include <condition_variable>
28f23191 27#include <limits.h>
1a496780 28#include <mutex>
28f23191
JG
29#include <stdbool.h>
30#include <urcu/list.h>
7972aab2 31
3130a40c
JG
32#define ASSERT_SESSION_LIST_LOCKED() LTTNG_ASSERT(session_trylock_list())
33
7972aab2 34struct ltt_ust_session;
00187dd4 35
d9a970b7
JG
36struct ltt_session;
37struct ltt_session_list;
38
39enum lttng_error_code
40session_create(const char *name, uid_t uid, gid_t gid, struct ltt_session **out_session);
41void session_lock(struct ltt_session *session);
42void session_unlock(struct ltt_session *session);
43
44bool session_get(struct ltt_session *session);
45void session_put(struct ltt_session *session);
46
47/*
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.
52 *
53 * In other words, it prevents tracer configurations from changing while they
54 * are being transmitted to the various applications.
55 */
56int session_trylock_list() noexcept;
57
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())
63
b5541356
DG
64/*
65 * Tracing session list
66 *
67 * Statically declared in session.c and can be accessed by using
54d01ffb 68 * session_get_list() function that returns the pointer to the list.
b5541356 69 */
5b74c7b1 70struct ltt_session_list {
b5541356 71 /*
a24f7994
MD
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.
b5541356 77 */
1a496780 78 std::mutex lock;
99d688f2
JG
79 /*
80 * This condition variable is signaled on every removal from
81 * the session list.
82 */
1a496780 83 std::condition_variable removal_cond;
6c9cc2ab 84
b5541356 85 /*
a24f7994
MD
86 * Session unique ID generator. The session list lock MUST be
87 * upon update and read of this counter.
b5541356 88 */
1a496780 89 uint64_t next_uuid = 0;
6c9cc2ab
DG
90
91 /* Linked list head */
1a496780 92 struct cds_list_head head = CDS_LIST_HEAD_INIT(head);
5b74c7b1
DG
93};
94
b5541356
DG
95/*
96 * This data structure contains information needed to identify a tracing
97 * session for both LTTng and UST.
5b74c7b1
DG
98 */
99struct ltt_session {
e99e3664 100 using id_t = uint64_t;
d9a970b7
JG
101
102private:
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);
a0a4f314 106 static void _const_session_unlock(const ltt_session& session);
d9a970b7
JG
107
108public:
a0a4f314
JG
109 using locked_ref = lttng::non_copyable_reference<
110 ltt_session,
111 lttng::memory::create_deleter_class<ltt_session,
112 ltt_session::_locked_session_release>::deleter>;
113 using ref = lttng::non_copyable_reference<
d9a970b7
JG
114 ltt_session,
115 lttng::memory::create_deleter_class<ltt_session, session_put>::deleter>;
a0a4f314
JG
116 using const_locked_ref = lttng::non_copyable_reference<
117 const ltt_session,
118 lttng::memory::create_deleter_class<
119 const ltt_session,
120 ltt_session::_locked_const_session_release>::deleter>;
121 using const_ref = lttng::non_copyable_reference<
d9a970b7 122 const ltt_session,
a0a4f314
JG
123 lttng::memory::create_deleter_class<const ltt_session,
124 ltt_session::_const_session_put>::deleter>;
d9a970b7
JG
125
126 void lock() const noexcept;
127 void unlock() const noexcept;
128
129 /*
130 * Session list lock must be acquired by the caller.
131 *
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()`.
135 */
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);
140
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);
e99e3664 145
74babd95 146 char name[NAME_MAX];
b178f53e 147 bool has_auto_generated_name;
46ef2188 148 bool name_contains_creation_time;
32aef76c 149 char hostname[LTTNG_HOST_NAME_MAX]; /* Local hostname. */
ecd1a12f
MD
150 /* Path of the last closed chunk. */
151 char last_chunk_path[LTTNG_PATH_MAX];
b178f53e 152 time_t creation_time;
20fe2104 153 struct ltt_kernel_session *kernel_session;
f6a9efaa 154 struct ltt_ust_session *ust_session;
d9a970b7 155 mutable struct urcu_ref ref_count;
b5541356
DG
156 /*
157 * Protect any read/write on this session data structure. This lock must be
158 * acquired *before* using any public functions declared below. Use
54d01ffb 159 * session_lock() and session_unlock() for that.
b5541356 160 */
d9a970b7 161 mutable pthread_mutex_t _lock;
b5541356 162 struct cds_list_head list;
e99e3664
JG
163 /* session unique identifier */
164 id_t id;
f4cc5e83
JG
165 /* Indicates if the session has been added to the session list and ht.*/
166 bool published;
e32d7f27
JG
167 /* Indicates if a destroy command has been applied to this session. */
168 bool destroyed;
6df2e2c9
MD
169 /* UID/GID of the user owning the session */
170 uid_t uid;
171 gid_t gid;
00e2e675
DG
172 /*
173 * Network session handle. A value of 0 means that there is no remote
174 * session established.
175 */
176 uint64_t net_handle;
177 /*
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.
182 */
183 struct consumer_output *consumer;
b178f53e
JG
184 /*
185 * Indicates whether or not the user has specified an output directory
186 * or if it was configured using the default configuration.
187 */
188 bool has_user_specified_directory;
8382cf6f 189 /* Did at least ONE start command has been triggered?. */
66cefebd
JG
190 bool has_been_started;
191 /* Is the session active? */
192 bool active;
6dc3064a
DG
193
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;
27babd3a 198 /*
92fe5ca1
JG
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
203 * creation defaults.
27babd3a
DG
204 */
205 unsigned int snapshot_mode;
54213acc
JG
206 /*
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.
210 */
211 bool has_non_mmap_channel;
ecc48a90
JD
212 /*
213 * Timer set when the session is created for live reading.
214 */
d98ad589 215 unsigned int live_timer;
d7ba1388
MD
216 /*
217 * Path where to keep the shared memory files.
218 */
219 char shm_path[PATH_MAX];
23324029
JD
220 /*
221 * Node in ltt_sessions_ht_by_id.
222 */
223 struct lttng_ht_node_u64 node;
e1bbf989
JR
224 /*
225 * Node in ltt_sessions_ht_by_name.
226 */
227 struct lttng_ht_node_str node_by_name;
d88744a4 228 /*
92816cc3
JG
229 * Timer to check periodically if a relay and/or consumer has completed
230 * the last rotation.
d88744a4 231 */
92816cc3
JG
232 bool rotation_pending_check_timer_enabled;
233 timer_t rotation_pending_check_timer;
259c2674 234 /* Timer to periodically rotate a session. */
92816cc3
JG
235 bool rotation_schedule_timer_enabled;
236 timer_t rotation_schedule_timer;
66ea93b1 237 /* Value for periodic rotations, 0 if disabled. */
259c2674 238 uint64_t rotate_timer_period;
66ea93b1 239 /* Value for size-based rotations, 0 if disabled. */
259c2674 240 uint64_t rotate_size;
5c408ad8
JD
241 /*
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
a9577b76 244 * know if a rotation occurred since the last stop to rename the current
a2b988e2
MD
245 * chunk. After a stop followed by rotate, all subsequent clear
246 * (without prior start) will succeed, but will be effect-less.
5c408ad8
JD
247 */
248 bool rotated_after_last_stop;
b02f5986
MD
249 /*
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.
253 */
254 bool cleared_after_last_stop;
a7ceb342
MD
255 /*
256 * True if the session has had an explicit non-quiet rotation.
257 */
258 bool rotated;
90936dcf 259 /*
c08136a3 260 * Trigger for size-based rotations.
90936dcf 261 */
90936dcf 262 struct lttng_trigger *rotate_trigger;
d2956687 263 LTTNG_OPTIONAL(uint64_t) most_recent_chunk_id;
82b69413 264 struct lttng_trace_chunk *current_trace_chunk;
d2956687
JG
265 struct lttng_trace_chunk *chunk_being_archived;
266 /* Current state of a rotation. */
267 enum lttng_rotation_state rotation_state;
7fdbed1c 268 bool quiet_rotation;
d2956687
JG
269 char *last_archived_chunk_name;
270 LTTNG_OPTIONAL(uint64_t) last_archived_chunk_id;
3e3665b8 271 struct lttng_dynamic_array destroy_notifiers;
ccbdaca4 272 struct lttng_dynamic_array clear_notifiers;
6fa5fe7c
MD
273 /* Session base path override. Set non-null. */
274 char *base_path;
d9a970b7 275};
e1bbf989 276
a0a4f314
JG
277/*
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.
280 */
281using ltt_session_destroy_notifier = void (*)(const ltt_session::locked_ref&, void *);
282using ltt_session_clear_notifier = void (*)(const ltt_session::locked_ref&, void *);
283
e99e3664
JG
284namespace lttng {
285namespace sessiond {
286
d9a970b7 287std::unique_lock<std::mutex> lock_session_list();
e99e3664 288
d9a970b7
JG
289namespace exceptions {
290/**
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.
294 */
295class session_not_found_error : public lttng::runtime_error {
296public:
297 class query_parameter {
298 public:
299 enum class query_type : std::uint8_t { BY_NAME, BY_ID };
300
301 /*
302 * Intentionally not explicit to allow construction from c-style strings,
303 * std::string, and lttng::c_string_view.
304 *
305 * NOLINTBEGIN(google-explicit-constructor)
306 */
307 query_parameter(const std::string& session_name) :
308 type(query_type::BY_NAME), parameter(session_name)
309 {
310 }
311 /* NOLINTEND(google-explicit-constructor) */
312
a0a4f314
JG
313 explicit query_parameter(ltt_session::id_t id_) :
314 type(query_type::BY_ID), parameter(id_)
d9a970b7
JG
315 {
316 }
317
318 query_parameter(const query_parameter& other) : type(other.type)
319 {
320 if (type == query_type::BY_NAME) {
321 new (&parameter.name) std::string(other.parameter.name);
322 } else {
323 parameter.id = other.parameter.id;
324 }
325 }
326
327 ~query_parameter()
328 {
329 if (type == query_type::BY_NAME) {
330 parameter.name.~basic_string();
331 }
332 }
333
334 query_type type;
335 union parameter {
336 explicit parameter(std::string name_) : name(std::move(name_))
337 {
338 }
339
340 explicit parameter(ltt_session::id_t id_) : id(id_)
341 {
342 }
343
344 /*
345 * parameter doesn't have enough information to do this safely; it it
346 * delegated to its parent which uses placement new.
347 */
348 parameter()
349 {
350 }
351
352 parameter(const parameter&) = delete;
353 parameter(const parameter&&) = delete;
354 parameter& operator=(parameter&) = delete;
355 parameter& operator=(parameter&&) = delete;
356
357 ~parameter()
358 {
359 }
360
361 std::string name;
362 ltt_session::id_t id;
363 } parameter;
364 };
365
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),
369 source_location_),
370 query_parameter(session_name)
371 {
372 }
373
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),
377 source_location_),
378 query_parameter(session_id)
379 {
380 }
381
382 session_not_found_error(const session_not_found_error& other) = default;
383 ~session_not_found_error() noexcept override = default;
384
385 query_parameter query_parameter;
386};
387} // namespace exceptions
e99e3664
JG
388} /* namespace sessiond */
389} /* namespace lttng */
390
a0a4f314
JG
391void session_destroy(struct ltt_session *session);
392int session_add_destroy_notifier(const ltt_session::locked_ref& session,
393 ltt_session_destroy_notifier notifier,
394 void *user_data);
395
396int session_add_clear_notifier(const ltt_session::locked_ref& session,
397 ltt_session_clear_notifier notifier,
398 void *user_data);
399void session_notify_clear(const ltt_session::locked_ref& session);
400
401enum consumer_dst_type
402session_get_consumer_destination_type(const ltt_session::locked_ref& session);
403const char *session_get_net_consumer_hostname(const ltt_session::locked_ref& session);
404void session_get_net_consumer_ports(const ltt_session::locked_ref& session,
405 uint16_t *control_port,
406 uint16_t *data_port);
407struct lttng_trace_archive_location *
408session_get_trace_archive_location(const ltt_session::locked_ref& session);
409
410struct ltt_session_list *session_get_list();
411void session_list_wait_empty(std::unique_lock<std::mutex> list_lock);
412
413bool session_access_ok(const ltt_session::locked_ref& session, uid_t uid);
414
415int session_reset_rotation_state(const ltt_session::locked_ref& session,
416 enum lttng_rotation_state result);
417
418/* Create a new trace chunk object from the session's configuration. */
419struct lttng_trace_chunk *
420session_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);
424
425/*
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).
429 *
430 * A reference to the session's current trace chunk is returned through
431 * `current_session_trace_chunk` on success.
432 */
433int 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);
436
437/*
438 * Close a chunk on the remote peers of a session. Has no effect on the
439 * ltt_session itself.
440 */
441int 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,
444 char *path);
445
446/* Open a packet in all channels of a given session. */
447enum lttng_error_code session_open_packets(const ltt_session::locked_ref& session);
448
449bool session_output_supports_trace_chunks(const struct ltt_session *session);
450
451/*
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.
456 *
457 * Return 0 when the session is not found,
458 * Return 1 when the session is found and set `id`.
459 */
460bool sample_session_id_by_name(const char *name, uint64_t *id);
461
5b74c7b1 462#endif /* _LTT_SESSION_H */
This page took 0.118336 seconds and 4 git commands to generate.