b091c2ef3d0c17d5c89d3590db71f82e4d9190b3
[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 #include "trace-ust.hpp"
15 #include "ust-app.hpp"
16
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>
24
25 #include <lttng/location.h>
26 #include <lttng/lttng-error.h>
27 #include <lttng/rotation.h>
28
29 #include <condition_variable>
30 #include <limits.h>
31 #include <mutex>
32 #include <stdbool.h>
33 #include <urcu/list.h>
34
35 #define ASSERT_SESSION_LIST_LOCKED() LTTNG_ASSERT(session_trylock_list())
36
37 struct ltt_ust_session;
38
39 struct ltt_session;
40 struct ltt_session_list;
41 struct buffer_reg_uid;
42
43 enum lttng_error_code
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);
47
48 bool session_get(struct ltt_session *session);
49 void session_put(struct ltt_session *session);
50
51 /*
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.
56 *
57 * In other words, it prevents tracer configurations from changing while they
58 * are being transmitted to the various applications.
59 */
60 int session_trylock_list() noexcept;
61
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())
67
68 /*
69 * Tracing session list
70 *
71 * Statically declared in session.c and can be accessed by using
72 * session_get_list() function that returns the pointer to the list.
73 */
74 struct ltt_session_list {
75 /*
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.
81 */
82 std::mutex lock;
83 /*
84 * This condition variable is signaled on every removal from
85 * the session list.
86 */
87 std::condition_variable removal_cond;
88
89 /*
90 * Session unique ID generator. The session list lock MUST be
91 * upon update and read of this counter.
92 */
93 uint64_t next_uuid = 0;
94
95 /* Linked list head */
96 struct cds_list_head head = CDS_LIST_HEAD_INIT(head);
97 };
98
99 namespace lttng {
100 namespace sessiond {
101 class user_space_consumer_channel_keys {
102 friend ltt_session;
103
104 public:
105 class iterator;
106
107 enum class consumer_bitness : std::uint8_t {
108 ABI_32 = 32,
109 ABI_64 = 64,
110 };
111
112 enum class channel_type : std::uint8_t {
113 METADATA,
114 DATA,
115 };
116
117 iterator begin() const noexcept;
118 iterator end() const noexcept;
119
120 private:
121 enum class _iteration_mode : std::uint8_t {
122 PER_PID,
123 PER_UID,
124
125 };
126
127 struct _iterator_creation_context {
128 const _iteration_mode _mode;
129 const ltt_ust_session& _session;
130 union {
131 lttng_ht *apps;
132 const cds_list_head *buffer_registry;
133 } _container;
134 };
135
136 public:
137 class iterator : public std::iterator<std::input_iterator_tag, std::uint64_t> {
138 friend user_space_consumer_channel_keys;
139
140 public:
141 struct key {
142 /* Bitness is needed to query the appropriate consumer daemon. */
143 consumer_bitness bitness;
144 std::uint64_t key_value;
145 channel_type type;
146
147 bool operator==(const key& other)
148 {
149 return bitness == other.bitness && key_value == other.key_value &&
150 type == other.type;
151 }
152 };
153
154 /*
155 * Copy constructor disabled since it would require handling the copy of locked
156 * references.
157 */
158 iterator(const iterator& other) = delete;
159 iterator(iterator&& other) = default;
160 ~iterator() = default;
161 iterator& operator=(const iterator&) = delete;
162 iterator& operator=(iterator&&) noexcept = delete;
163
164 iterator& operator++();
165 bool operator==(const iterator& other) const noexcept;
166 bool operator!=(const iterator& other) const noexcept;
167 key operator*() const;
168
169 /*
170 * Get the session registry of the channel currently
171 * pointed by the iterator. Never returns nullptr.
172 */
173 lttng::sessiond::ust::registry_session *get_registry_session();
174
175 private:
176 struct _iterator_position {
177 struct {
178 lttng_ht_iter app_iterator = {};
179 nonstd::optional<ust_app_session::locked_weak_ref>
180 current_app_session;
181 lttng::sessiond::ust::registry_session *current_registry_session =
182 nullptr;
183 } _per_pid;
184 struct {
185 buffer_reg_uid *current_registry = nullptr;
186 } _per_uid;
187
188 lttng_ht_iter channel_iterator = {};
189 };
190
191 explicit iterator(const _iterator_creation_context& creation_context,
192 bool is_end = false);
193
194 void _init_per_pid() noexcept;
195 void _skip_to_next_app_per_pid(bool try_current) noexcept;
196 void _advance_one_per_pid();
197 key _get_current_value_per_pid() const noexcept;
198 lttng::sessiond::ust::registry_session *_get_registry_session_per_pid();
199
200 void _init_per_uid() noexcept;
201 void _advance_one_per_uid();
202 key _get_current_value_per_uid() const noexcept;
203 lttng::sessiond::ust::registry_session *_get_registry_session_per_uid();
204
205 const _iterator_creation_context& _creation_context;
206 _iterator_position _position;
207 bool _is_end;
208 };
209
210 private:
211 user_space_consumer_channel_keys(const ltt_ust_session& ust_session, lttng_ht& apps) :
212 _creation_context{ _iteration_mode::PER_PID, ust_session, { .apps = &apps } }
213 {
214 }
215
216 user_space_consumer_channel_keys(const ltt_ust_session& ust_session,
217 const cds_list_head& buffer_registry) :
218 _creation_context{ _iteration_mode::PER_UID,
219 ust_session,
220 { .buffer_registry = &buffer_registry } }
221 {
222 }
223
224 class _scoped_rcu_read_lock {
225 public:
226 _scoped_rcu_read_lock()
227 {
228 rcu_read_lock();
229 }
230
231 ~_scoped_rcu_read_lock()
232 {
233 if (_armed) {
234 rcu_read_unlock();
235 }
236 }
237
238 _scoped_rcu_read_lock(_scoped_rcu_read_lock&& other) noexcept
239 {
240 other._armed = false;
241 }
242
243 _scoped_rcu_read_lock(_scoped_rcu_read_lock& other) = delete;
244 _scoped_rcu_read_lock& operator=(const _scoped_rcu_read_lock&) = delete;
245 _scoped_rcu_read_lock& operator=(_scoped_rcu_read_lock&&) noexcept = delete;
246
247 private:
248 bool _armed = true;
249 };
250
251 _scoped_rcu_read_lock _read_lock;
252 _iterator_creation_context _creation_context;
253 };
254 } /* namespace sessiond */
255 } /* namespace lttng */
256
257 /*
258 * This data structure contains information needed to identify a tracing
259 * session for both LTTng and UST.
260 */
261 struct ltt_session {
262 using id_t = uint64_t;
263 friend lttng::sessiond::user_space_consumer_channel_keys::iterator;
264
265 private:
266 static void _locked_session_release(ltt_session *session);
267 static void _locked_const_session_release(const ltt_session *session);
268 static void _const_session_put(const ltt_session *session);
269 static void _const_session_unlock(const ltt_session& session);
270
271 public:
272 using locked_ref = lttng::non_copyable_reference<
273 ltt_session,
274 lttng::memory::create_deleter_class<ltt_session,
275 ltt_session::_locked_session_release>::deleter>;
276 using ref = lttng::non_copyable_reference<
277 ltt_session,
278 lttng::memory::create_deleter_class<ltt_session, session_put>::deleter>;
279 using const_locked_ref = lttng::non_copyable_reference<
280 const ltt_session,
281 lttng::memory::create_deleter_class<
282 const ltt_session,
283 ltt_session::_locked_const_session_release>::deleter>;
284 using const_ref = lttng::non_copyable_reference<
285 const ltt_session,
286 lttng::memory::create_deleter_class<const ltt_session,
287 ltt_session::_const_session_put>::deleter>;
288
289 static locked_ref make_locked_ref(ltt_session& session)
290 {
291 return lttng::make_non_copyable_reference<locked_ref::referenced_type,
292 locked_ref::deleter>(session);
293 }
294
295 static const_locked_ref make_locked_ref(const ltt_session& session)
296 {
297 return lttng::make_non_copyable_reference<const_locked_ref::referenced_type,
298 const_locked_ref::deleter>(session);
299 }
300
301 static ref make_ref(ltt_session& session)
302 {
303 return lttng::make_non_copyable_reference<ref::referenced_type, ref::deleter>(
304 session);
305 }
306
307 static const_ref make_ref(const ltt_session& session)
308 {
309 return lttng::make_non_copyable_reference<const_ref::referenced_type,
310 const_ref::deleter>(session);
311 }
312
313 void lock() const noexcept;
314 void unlock() const noexcept;
315
316 lttng::sessiond::user_space_consumer_channel_keys user_space_consumer_channel_keys() const;
317
318 /*
319 * Session list lock must be acquired by the caller.
320 *
321 * The caller must not keep the ownership of the returned locked session
322 * for longer than strictly necessary. If your intention is to acquire
323 * a reference to an ltt_session, see `find_session()`.
324 */
325 static locked_ref find_locked_session(ltt_session::id_t id);
326 static locked_ref find_locked_session(lttng::c_string_view name);
327 static const_locked_ref find_locked_const_session(ltt_session::id_t id);
328 static const_locked_ref find_locked_const_session(lttng::c_string_view name);
329
330 static ref find_session(ltt_session::id_t id);
331 static ref find_session(lttng::c_string_view name);
332 static const_ref find_const_session(ltt_session::id_t id);
333 static const_ref find_const_session(lttng::c_string_view name);
334
335 char name[NAME_MAX];
336 bool has_auto_generated_name;
337 bool name_contains_creation_time;
338 char hostname[LTTNG_HOST_NAME_MAX]; /* Local hostname. */
339 /* Path of the last closed chunk. */
340 char last_chunk_path[LTTNG_PATH_MAX];
341 time_t creation_time;
342 struct ltt_kernel_session *kernel_session;
343 struct ltt_ust_session *ust_session;
344 mutable struct urcu_ref ref_count;
345 /*
346 * Protect any read/write on this session data structure. This lock must be
347 * acquired *before* using any public functions declared below. Use
348 * session_lock() and session_unlock() for that.
349 */
350 mutable pthread_mutex_t _lock;
351 struct cds_list_head list;
352 /* session unique identifier */
353 id_t id;
354 /* Indicates if the session has been added to the session list and ht.*/
355 bool published;
356 /* Indicates if a destroy command has been applied to this session. */
357 bool destroyed;
358 /* UID/GID of the user owning the session */
359 uid_t uid;
360 gid_t gid;
361 /*
362 * Network session handle. A value of 0 means that there is no remote
363 * session established.
364 */
365 uint64_t net_handle;
366 /*
367 * This consumer is only set when the create_session_uri call is made.
368 * This contains the temporary information for a consumer output. Upon
369 * creation of the UST or kernel session, this consumer, if available, is
370 * copied into those sessions.
371 */
372 struct consumer_output *consumer;
373 /*
374 * Indicates whether or not the user has specified an output directory
375 * or if it was configured using the default configuration.
376 */
377 bool has_user_specified_directory;
378 /* Did at least ONE start command has been triggered?. */
379 bool has_been_started;
380 /* Is the session active? */
381 bool active;
382
383 /* Snapshot representation in a session. */
384 struct snapshot snapshot;
385 /* Indicate if the session has to output the traces or not. */
386 unsigned int output_traces;
387 /*
388 * This session is in snapshot mode. This means that channels enabled
389 * will be set in overwrite mode by default and must be in mmap
390 * output mode. Note that snapshots can be taken on a session that
391 * is not in "snapshot_mode". This parameter only affects channel
392 * creation defaults.
393 */
394 unsigned int snapshot_mode;
395 /*
396 * A session that has channels that don't use 'mmap' output can't be
397 * used to capture snapshots. This is set to true whenever a
398 * 'splice' kernel channel is enabled.
399 */
400 bool has_non_mmap_channel;
401 /*
402 * Timer set when the session is created for live reading.
403 */
404 unsigned int live_timer;
405 /*
406 * Path where to keep the shared memory files.
407 */
408 char shm_path[PATH_MAX];
409 /*
410 * Node in ltt_sessions_ht_by_id.
411 */
412 struct lttng_ht_node_u64 node;
413 /*
414 * Node in ltt_sessions_ht_by_name.
415 */
416 struct lttng_ht_node_str node_by_name;
417 /*
418 * Timer to check periodically if a relay and/or consumer has completed
419 * the last rotation.
420 */
421 bool rotation_pending_check_timer_enabled;
422 timer_t rotation_pending_check_timer;
423 /* Timer to periodically rotate a session. */
424 bool rotation_schedule_timer_enabled;
425 timer_t rotation_schedule_timer;
426 /* Value for periodic rotations, 0 if disabled. */
427 uint64_t rotate_timer_period;
428 /* Value for size-based rotations, 0 if disabled. */
429 uint64_t rotate_size;
430 /*
431 * Keep a state if this session was rotated after the last stop command.
432 * We only allow one rotation after a stop. At destroy, we also need to
433 * know if a rotation occurred since the last stop to rename the current
434 * chunk. After a stop followed by rotate, all subsequent clear
435 * (without prior start) will succeed, but will be effect-less.
436 */
437 bool rotated_after_last_stop;
438 /*
439 * Track whether the session was cleared after last stop. All subsequent
440 * clear (without prior start) will succeed, but will be effect-less. A
441 * subsequent rotate (without prior start) will return an error.
442 */
443 bool cleared_after_last_stop;
444 /*
445 * True if the session has had an explicit non-quiet rotation.
446 */
447 bool rotated;
448 /*
449 * Trigger for size-based rotations.
450 */
451 struct lttng_trigger *rotate_trigger;
452 LTTNG_OPTIONAL(uint64_t) most_recent_chunk_id;
453 struct lttng_trace_chunk *current_trace_chunk;
454 struct lttng_trace_chunk *chunk_being_archived;
455 /* Current state of a rotation. */
456 enum lttng_rotation_state rotation_state;
457 bool quiet_rotation;
458 char *last_archived_chunk_name;
459 LTTNG_OPTIONAL(uint64_t) last_archived_chunk_id;
460 struct lttng_dynamic_array destroy_notifiers;
461 struct lttng_dynamic_array clear_notifiers;
462 /* Session base path override. Set non-null. */
463 char *base_path;
464 };
465
466 /*
467 * Destruction notifiers are invoked in an exclusive context. There is no need for the session to be
468 * locked nor for a reference to be acquired.
469 */
470 using ltt_session_destroy_notifier = void (*)(const ltt_session::locked_ref&, void *);
471 using ltt_session_clear_notifier = void (*)(const ltt_session::locked_ref&, void *);
472
473 namespace lttng {
474 namespace sessiond {
475
476 std::unique_lock<std::mutex> lock_session_list();
477
478 namespace exceptions {
479 /**
480 * @class session_not_found_error
481 * @brief Represents a session-not-found error and provides the parameters used to query the session
482 * for use by error-reporting code.
483 */
484 class session_not_found_error : public lttng::runtime_error {
485 public:
486 class query_parameter {
487 public:
488 enum class query_type : std::uint8_t { BY_NAME, BY_ID };
489
490 /*
491 * Intentionally not explicit to allow construction from c-style strings,
492 * std::string, and lttng::c_string_view.
493 *
494 * NOLINTBEGIN(google-explicit-constructor)
495 */
496 query_parameter(const std::string& session_name) :
497 type(query_type::BY_NAME), parameter(session_name)
498 {
499 }
500 /* NOLINTEND(google-explicit-constructor) */
501
502 explicit query_parameter(ltt_session::id_t id_) :
503 type(query_type::BY_ID), parameter(id_)
504 {
505 }
506
507 query_parameter(const query_parameter& other) : type(other.type)
508 {
509 if (type == query_type::BY_NAME) {
510 new (&parameter.name) std::string(other.parameter.name);
511 } else {
512 parameter.id = other.parameter.id;
513 }
514 }
515
516 ~query_parameter()
517 {
518 if (type == query_type::BY_NAME) {
519 parameter.name.~basic_string();
520 }
521 }
522
523 query_parameter(query_parameter&& other) noexcept;
524 query_parameter& operator=(const query_parameter&) = delete;
525 query_parameter& operator=(query_parameter&&) noexcept = delete;
526
527 query_type type;
528 union parameter {
529 explicit parameter(std::string name_) : name(std::move(name_))
530 {
531 }
532
533 explicit parameter(ltt_session::id_t id_) : id(id_)
534 {
535 }
536
537 /*
538 * parameter doesn't have enough information to do this safely; it it
539 * delegated to its parent which uses placement new.
540 */
541 parameter()
542 {
543 }
544
545 parameter(const parameter&) = delete;
546 parameter(const parameter&&) = delete;
547 parameter& operator=(parameter&) = delete;
548 parameter& operator=(parameter&&) = delete;
549
550 ~parameter()
551 {
552 }
553
554 std::string name;
555 ltt_session::id_t id;
556 } parameter;
557 };
558
559 session_not_found_error(const std::string& session_name,
560 const lttng::source_location& source_location_) :
561 lttng::runtime_error(fmt::format("Session not found: name=`{}`", session_name),
562 source_location_),
563 query_parameter(session_name)
564 {
565 }
566
567 session_not_found_error(ltt_session::id_t session_id,
568 const lttng::source_location& source_location_) :
569 lttng::runtime_error("Session not found: id=" + std::to_string(session_id),
570 source_location_),
571 query_parameter(session_id)
572 {
573 }
574
575 session_not_found_error(const session_not_found_error& other) = default;
576 ~session_not_found_error() noexcept override = default;
577 /*
578 * Setting an explicit `noexcept` causes compilation failure on gcc < 5.0
579 * @see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59526
580 */
581 session_not_found_error(session_not_found_error&& other) /* noexcept */ = default;
582 session_not_found_error& operator=(const session_not_found_error&) = delete;
583 session_not_found_error& operator=(session_not_found_error&&) noexcept = delete;
584
585 query_parameter query_parameter;
586 };
587 } // namespace exceptions
588 } /* namespace sessiond */
589 } /* namespace lttng */
590
591 void session_destroy(struct ltt_session *session);
592 int session_add_destroy_notifier(const ltt_session::locked_ref& session,
593 ltt_session_destroy_notifier notifier,
594 void *user_data);
595
596 int session_add_clear_notifier(const ltt_session::locked_ref& session,
597 ltt_session_clear_notifier notifier,
598 void *user_data);
599 void session_notify_clear(const ltt_session::locked_ref& session);
600
601 enum consumer_dst_type
602 session_get_consumer_destination_type(const ltt_session::locked_ref& session);
603 const char *session_get_net_consumer_hostname(const ltt_session::locked_ref& session);
604 void session_get_net_consumer_ports(const ltt_session::locked_ref& session,
605 uint16_t *control_port,
606 uint16_t *data_port);
607 struct lttng_trace_archive_location *
608 session_get_trace_archive_location(const ltt_session::locked_ref& session);
609
610 struct ltt_session_list *session_get_list();
611 void session_list_wait_empty(std::unique_lock<std::mutex> list_lock);
612
613 bool session_access_ok(const ltt_session::locked_ref& session, uid_t uid);
614
615 int session_reset_rotation_state(const ltt_session::locked_ref& session,
616 enum lttng_rotation_state result);
617
618 /* Create a new trace chunk object from the session's configuration. */
619 struct lttng_trace_chunk *
620 session_create_new_trace_chunk(const ltt_session::locked_ref& session,
621 const struct consumer_output *consumer_output_override,
622 const char *session_base_path_override,
623 const char *chunk_name_override);
624
625 /*
626 * Set `new_trace_chunk` as the session's current trace chunk. A reference
627 * to `new_trace_chunk` is acquired by the session. The chunk is created
628 * on remote peers (consumer and relay daemons).
629 *
630 * A reference to the session's current trace chunk is returned through
631 * `current_session_trace_chunk` on success.
632 */
633 int session_set_trace_chunk(const ltt_session::locked_ref& session,
634 struct lttng_trace_chunk *new_trace_chunk,
635 struct lttng_trace_chunk **current_session_trace_chunk);
636
637 /*
638 * Close a chunk on the remote peers of a session. Has no effect on the
639 * ltt_session itself.
640 */
641 int session_close_trace_chunk(const ltt_session::locked_ref& session,
642 struct lttng_trace_chunk *trace_chunk,
643 enum lttng_trace_chunk_command_type close_command,
644 char *path);
645
646 /* Open a packet in all channels of a given session. */
647 enum lttng_error_code session_open_packets(const ltt_session::locked_ref& session);
648
649 bool session_output_supports_trace_chunks(const struct ltt_session *session);
650
651 /*
652 * Sample the id of a session looked up via its name.
653 * Here the term "sampling" hint the caller that this return the id at a given
654 * point in time with no guarantee that the session for which the id was
655 * sampled still exist at that point.
656 *
657 * Return 0 when the session is not found,
658 * Return 1 when the session is found and set `id`.
659 */
660 bool sample_session_id_by_name(const char *name, uint64_t *id);
661
662 const char *session_get_base_path(const ltt_session::locked_ref& session);
663
664 #ifdef HAVE_LIBLTTNG_UST_CTL
665
666 enum lttng_error_code ust_app_rotate_session(const ltt_session::locked_ref& session);
667 enum lttng_error_code ust_app_clear_session(const ltt_session::locked_ref& session);
668 enum lttng_error_code ust_app_open_packets(const ltt_session::locked_ref& session);
669
670 #else /* HAVE_LIBLTTNG_UST_CTL */
671
672 static inline enum lttng_error_code ust_app_rotate_session(const ltt_session::locked_ref& session
673 __attribute__((unused)))
674 {
675 return LTTNG_ERR_UNK;
676 }
677
678 static inline enum lttng_error_code ust_app_clear_session(const ltt_session::locked_ref& session
679 __attribute__((unused)))
680 {
681 return LTTNG_ERR_UNK;
682 }
683
684 static inline enum lttng_error_code ust_app_open_packets(const ltt_session::locked_ref& session
685 __attribute__((unused)))
686 {
687 return LTTNG_ERR_UNK;
688 }
689
690 #endif /* HAVE_LIBLTTNG_UST_CTL */
691
692 #endif /* _LTT_SESSION_H */
This page took 0.044212 seconds and 5 git commands to generate.