sessiond: introduce ltt_session::locked_ref look-up functions
[lttng-tools.git] / src / bin / lttng-sessiond / session.hpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #ifndef _LTT_SESSION_H
9 #define _LTT_SESSION_H
10
11 #include "consumer.hpp"
12 #include "snapshot.hpp"
13 #include "trace-kernel.hpp"
14
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
21 #include <lttng/location.h>
22 #include <lttng/lttng-error.h>
23 #include <lttng/rotation.h>
24
25 #include <condition_variable>
26 #include <limits.h>
27 #include <mutex>
28 #include <stdbool.h>
29 #include <urcu/list.h>
30
31 #define ASSERT_SESSION_LIST_LOCKED() LTTNG_ASSERT(session_trylock_list())
32
33 struct ltt_ust_session;
34
35 using ltt_session_destroy_notifier = void (*)(const struct ltt_session *, void *);
36 using ltt_session_clear_notifier = void (*)(const struct ltt_session *, void *);
37
38 struct ltt_session;
39 struct ltt_session_list;
40
41 enum lttng_error_code
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);
45
46 bool session_get(struct ltt_session *session);
47 void session_put(struct ltt_session *session);
48
49 /*
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.
54 *
55 * In other words, it prevents tracer configurations from changing while they
56 * are being transmitted to the various applications.
57 */
58 int session_trylock_list() noexcept;
59
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())
65
66 void session_destroy(struct ltt_session *session);
67 int session_add_destroy_notifier(struct ltt_session *session,
68 ltt_session_destroy_notifier notifier,
69 void *user_data);
70
71 int session_add_clear_notifier(struct ltt_session *session,
72 ltt_session_clear_notifier notifier,
73 void *user_data);
74 void session_notify_clear(ltt_session& session);
75
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,
80 uint16_t *data_port);
81 struct lttng_trace_archive_location *
82 session_get_trace_archive_location(const struct ltt_session *session);
83
84 struct ltt_session_list *session_get_list();
85 void session_list_wait_empty(std::unique_lock<std::mutex> list_lock);
86
87 bool session_access_ok(struct ltt_session *session, uid_t uid);
88
89 int session_reset_rotation_state(ltt_session& session, enum lttng_rotation_state result);
90
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);
97
98 /*
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).
102 *
103 * A reference to the session's current trace chunk is returned through
104 * `current_session_trace_chunk` on success.
105 */
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);
109
110 /*
111 * Close a chunk on the remote peers of a session. Has no effect on the
112 * ltt_session itself.
113 */
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,
117 char *path);
118
119 /* Open a packet in all channels of a given session. */
120 enum lttng_error_code session_open_packets(struct ltt_session *session);
121
122 bool session_output_supports_trace_chunks(const struct ltt_session *session);
123
124 /*
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.
129 *
130 * Return 0 when the session is not found,
131 * Return 1 when the session is found and set `id`.
132 */
133 bool sample_session_id_by_name(const char *name, uint64_t *id);
134
135 /*
136 * Tracing session list
137 *
138 * Statically declared in session.c and can be accessed by using
139 * session_get_list() function that returns the pointer to the list.
140 */
141 struct ltt_session_list {
142 /*
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.
148 */
149 std::mutex lock;
150 /*
151 * This condition variable is signaled on every removal from
152 * the session list.
153 */
154 std::condition_variable removal_cond;
155
156 /*
157 * Session unique ID generator. The session list lock MUST be
158 * upon update and read of this counter.
159 */
160 uint64_t next_uuid = 0;
161
162 /* Linked list head */
163 struct cds_list_head head = CDS_LIST_HEAD_INIT(head);
164 };
165
166 /*
167 * This data structure contains information needed to identify a tracing
168 * session for both LTTng and UST.
169 */
170 struct ltt_session {
171 using id_t = uint64_t;
172
173 private:
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);
178
179 public:
180 using locked_ref =
181 std::unique_ptr<ltt_session,
182 lttng::memory::create_deleter_class<
183 ltt_session,
184 ltt_session::_locked_session_release>::deleter>;
185 using ref = std::unique_ptr<
186 ltt_session,
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<
191 const ltt_session,
192 ltt_session::_locked_const_session_release>::deleter>;
193 using const_ref = std::unique_ptr<
194 const ltt_session,
195 lttng::memory::create_deleter_class<const ltt_session, ltt_session::_const_session_put>::deleter>;
196
197 void lock() const noexcept;
198 void unlock() const noexcept;
199
200 /*
201 * Session list lock must be acquired by the caller.
202 *
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()`.
206 */
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);
211
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);
216
217 char name[NAME_MAX];
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;
227 /*
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.
231 */
232 mutable pthread_mutex_t _lock;
233 struct cds_list_head list;
234 /* session unique identifier */
235 id_t id;
236 /* Indicates if the session has been added to the session list and ht.*/
237 bool published;
238 /* Indicates if a destroy command has been applied to this session. */
239 bool destroyed;
240 /* UID/GID of the user owning the session */
241 uid_t uid;
242 gid_t gid;
243 /*
244 * Network session handle. A value of 0 means that there is no remote
245 * session established.
246 */
247 uint64_t net_handle;
248 /*
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.
253 */
254 struct consumer_output *consumer;
255 /*
256 * Indicates whether or not the user has specified an output directory
257 * or if it was configured using the default configuration.
258 */
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? */
263 bool active;
264
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;
269 /*
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
274 * creation defaults.
275 */
276 unsigned int snapshot_mode;
277 /*
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.
281 */
282 bool has_non_mmap_channel;
283 /*
284 * Timer set when the session is created for live reading.
285 */
286 unsigned int live_timer;
287 /*
288 * Path where to keep the shared memory files.
289 */
290 char shm_path[PATH_MAX];
291 /*
292 * Node in ltt_sessions_ht_by_id.
293 */
294 struct lttng_ht_node_u64 node;
295 /*
296 * Node in ltt_sessions_ht_by_name.
297 */
298 struct lttng_ht_node_str node_by_name;
299 /*
300 * Timer to check periodically if a relay and/or consumer has completed
301 * the last rotation.
302 */
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;
312 /*
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.
318 */
319 bool rotated_after_last_stop;
320 /*
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.
324 */
325 bool cleared_after_last_stop;
326 /*
327 * True if the session has had an explicit non-quiet rotation.
328 */
329 bool rotated;
330 /*
331 * Trigger for size-based rotations.
332 */
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;
339 bool quiet_rotation;
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. */
345 char *base_path;
346
347
348 };
349
350 namespace lttng {
351 namespace sessiond {
352
353 std::unique_lock<std::mutex> lock_session_list();
354
355 namespace exceptions {
356 /**
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.
360 */
361 class session_not_found_error : public lttng::runtime_error {
362 public:
363 class query_parameter {
364 public:
365 enum class query_type : std::uint8_t { BY_NAME, BY_ID };
366
367 /*
368 * Intentionally not explicit to allow construction from c-style strings,
369 * std::string, and lttng::c_string_view.
370 *
371 * NOLINTBEGIN(google-explicit-constructor)
372 */
373 query_parameter(const std::string& session_name) :
374 type(query_type::BY_NAME), parameter(session_name)
375 {
376 }
377 /* NOLINTEND(google-explicit-constructor) */
378
379 explicit query_parameter(ltt_session::id_t id_) : type(query_type::BY_ID), parameter(id_)
380 {
381 }
382
383 query_parameter(const query_parameter& other) : type(other.type)
384 {
385 if (type == query_type::BY_NAME) {
386 new (&parameter.name) std::string(other.parameter.name);
387 } else {
388 parameter.id = other.parameter.id;
389 }
390 }
391
392 ~query_parameter()
393 {
394 if (type == query_type::BY_NAME) {
395 parameter.name.~basic_string();
396 }
397 }
398
399 query_type type;
400 union parameter {
401 explicit parameter(std::string name_) : name(std::move(name_))
402 {
403 }
404
405 explicit parameter(ltt_session::id_t id_) : id(id_)
406 {
407 }
408
409 /*
410 * parameter doesn't have enough information to do this safely; it it
411 * delegated to its parent which uses placement new.
412 */
413 parameter()
414 {
415 }
416
417 parameter(const parameter&) = delete;
418 parameter(const parameter&&) = delete;
419 parameter& operator=(parameter&) = delete;
420 parameter& operator=(parameter&&) = delete;
421
422 ~parameter()
423 {
424 }
425
426 std::string name;
427 ltt_session::id_t id;
428 } parameter;
429 };
430
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),
434 source_location_),
435 query_parameter(session_name)
436 {
437 }
438
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),
442 source_location_),
443 query_parameter(session_id)
444 {
445 }
446
447 session_not_found_error(const session_not_found_error& other) = default;
448 ~session_not_found_error() noexcept override = default;
449
450 query_parameter query_parameter;
451 };
452 } // namespace exceptions
453 } /* namespace sessiond */
454 } /* namespace lttng */
455
456 #endif /* _LTT_SESSION_H */
This page took 0.038794 seconds and 4 git commands to generate.