Commit | Line | Data |
---|---|---|
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> |
28f23191 | 20 | |
5d65beab | 21 | #include <lttng/location.h> |
82b69413 | 22 | #include <lttng/lttng-error.h> |
28f23191 | 23 | #include <lttng/rotation.h> |
6dc3064a | 24 | |
1a496780 | 25 | #include <condition_variable> |
28f23191 | 26 | #include <limits.h> |
1a496780 | 27 | #include <mutex> |
28f23191 JG |
28 | #include <stdbool.h> |
29 | #include <urcu/list.h> | |
7972aab2 | 30 | |
3130a40c JG |
31 | #define ASSERT_SESSION_LIST_LOCKED() LTTNG_ASSERT(session_trylock_list()) |
32 | ||
7972aab2 | 33 | struct ltt_ust_session; |
00187dd4 | 34 | |
e665dfbc JG |
35 | using ltt_session_destroy_notifier = void (*)(const struct ltt_session *, void *); |
36 | using ltt_session_clear_notifier = void (*)(const struct ltt_session *, void *); | |
3e3665b8 | 37 | |
d9a970b7 JG |
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); | |
e99e3664 | 134 | |
b5541356 DG |
135 | /* |
136 | * Tracing session list | |
137 | * | |
138 | * Statically declared in session.c and can be accessed by using | |
54d01ffb | 139 | * session_get_list() function that returns the pointer to the list. |
b5541356 | 140 | */ |
5b74c7b1 | 141 | struct ltt_session_list { |
b5541356 | 142 | /* |
a24f7994 MD |
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. | |
b5541356 | 148 | */ |
1a496780 | 149 | std::mutex lock; |
99d688f2 JG |
150 | /* |
151 | * This condition variable is signaled on every removal from | |
152 | * the session list. | |
153 | */ | |
1a496780 | 154 | std::condition_variable removal_cond; |
6c9cc2ab | 155 | |
b5541356 | 156 | /* |
a24f7994 MD |
157 | * Session unique ID generator. The session list lock MUST be |
158 | * upon update and read of this counter. | |
b5541356 | 159 | */ |
1a496780 | 160 | uint64_t next_uuid = 0; |
6c9cc2ab DG |
161 | |
162 | /* Linked list head */ | |
1a496780 | 163 | struct cds_list_head head = CDS_LIST_HEAD_INIT(head); |
5b74c7b1 DG |
164 | }; |
165 | ||
b5541356 DG |
166 | /* |
167 | * This data structure contains information needed to identify a tracing | |
168 | * session for both LTTng and UST. | |
5b74c7b1 DG |
169 | */ |
170 | struct ltt_session { | |
e99e3664 | 171 | using id_t = uint64_t; |
d9a970b7 JG |
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: | |
77682be9 | 180 | using locked_ref = |
28f23191 | 181 | std::unique_ptr<ltt_session, |
f053d40c | 182 | lttng::memory::create_deleter_class< |
28f23191 | 183 | ltt_session, |
d9a970b7 JG |
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); | |
e99e3664 | 216 | |
74babd95 | 217 | char name[NAME_MAX]; |
b178f53e | 218 | bool has_auto_generated_name; |
46ef2188 | 219 | bool name_contains_creation_time; |
32aef76c | 220 | char hostname[LTTNG_HOST_NAME_MAX]; /* Local hostname. */ |
ecd1a12f MD |
221 | /* Path of the last closed chunk. */ |
222 | char last_chunk_path[LTTNG_PATH_MAX]; | |
b178f53e | 223 | time_t creation_time; |
20fe2104 | 224 | struct ltt_kernel_session *kernel_session; |
f6a9efaa | 225 | struct ltt_ust_session *ust_session; |
d9a970b7 | 226 | mutable struct urcu_ref ref_count; |
b5541356 DG |
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 | |
54d01ffb | 230 | * session_lock() and session_unlock() for that. |
b5541356 | 231 | */ |
d9a970b7 | 232 | mutable pthread_mutex_t _lock; |
b5541356 | 233 | struct cds_list_head list; |
e99e3664 JG |
234 | /* session unique identifier */ |
235 | id_t id; | |
f4cc5e83 JG |
236 | /* Indicates if the session has been added to the session list and ht.*/ |
237 | bool published; | |
e32d7f27 JG |
238 | /* Indicates if a destroy command has been applied to this session. */ |
239 | bool destroyed; | |
6df2e2c9 MD |
240 | /* UID/GID of the user owning the session */ |
241 | uid_t uid; | |
242 | gid_t gid; | |
00e2e675 DG |
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; | |
b178f53e JG |
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; | |
8382cf6f | 260 | /* Did at least ONE start command has been triggered?. */ |
66cefebd JG |
261 | bool has_been_started; |
262 | /* Is the session active? */ | |
263 | bool active; | |
6dc3064a DG |
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; | |
27babd3a | 269 | /* |
92fe5ca1 JG |
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. | |
27babd3a DG |
275 | */ |
276 | unsigned int snapshot_mode; | |
54213acc JG |
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; | |
ecc48a90 JD |
283 | /* |
284 | * Timer set when the session is created for live reading. | |
285 | */ | |
d98ad589 | 286 | unsigned int live_timer; |
d7ba1388 MD |
287 | /* |
288 | * Path where to keep the shared memory files. | |
289 | */ | |
290 | char shm_path[PATH_MAX]; | |
23324029 JD |
291 | /* |
292 | * Node in ltt_sessions_ht_by_id. | |
293 | */ | |
294 | struct lttng_ht_node_u64 node; | |
e1bbf989 JR |
295 | /* |
296 | * Node in ltt_sessions_ht_by_name. | |
297 | */ | |
298 | struct lttng_ht_node_str node_by_name; | |
d88744a4 | 299 | /* |
92816cc3 JG |
300 | * Timer to check periodically if a relay and/or consumer has completed |
301 | * the last rotation. | |
d88744a4 | 302 | */ |
92816cc3 JG |
303 | bool rotation_pending_check_timer_enabled; |
304 | timer_t rotation_pending_check_timer; | |
259c2674 | 305 | /* Timer to periodically rotate a session. */ |
92816cc3 JG |
306 | bool rotation_schedule_timer_enabled; |
307 | timer_t rotation_schedule_timer; | |
66ea93b1 | 308 | /* Value for periodic rotations, 0 if disabled. */ |
259c2674 | 309 | uint64_t rotate_timer_period; |
66ea93b1 | 310 | /* Value for size-based rotations, 0 if disabled. */ |
259c2674 | 311 | uint64_t rotate_size; |
5c408ad8 JD |
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 | |
a9577b76 | 315 | * know if a rotation occurred since the last stop to rename the current |
a2b988e2 MD |
316 | * chunk. After a stop followed by rotate, all subsequent clear |
317 | * (without prior start) will succeed, but will be effect-less. | |
5c408ad8 JD |
318 | */ |
319 | bool rotated_after_last_stop; | |
b02f5986 MD |
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; | |
a7ceb342 MD |
326 | /* |
327 | * True if the session has had an explicit non-quiet rotation. | |
328 | */ | |
329 | bool rotated; | |
90936dcf | 330 | /* |
c08136a3 | 331 | * Trigger for size-based rotations. |
90936dcf | 332 | */ |
90936dcf | 333 | struct lttng_trigger *rotate_trigger; |
d2956687 | 334 | LTTNG_OPTIONAL(uint64_t) most_recent_chunk_id; |
82b69413 | 335 | struct lttng_trace_chunk *current_trace_chunk; |
d2956687 JG |
336 | struct lttng_trace_chunk *chunk_being_archived; |
337 | /* Current state of a rotation. */ | |
338 | enum lttng_rotation_state rotation_state; | |
7fdbed1c | 339 | bool quiet_rotation; |
d2956687 JG |
340 | char *last_archived_chunk_name; |
341 | LTTNG_OPTIONAL(uint64_t) last_archived_chunk_id; | |
3e3665b8 | 342 | struct lttng_dynamic_array destroy_notifiers; |
ccbdaca4 | 343 | struct lttng_dynamic_array clear_notifiers; |
6fa5fe7c MD |
344 | /* Session base path override. Set non-null. */ |
345 | char *base_path; | |
2961f09e | 346 | |
04ed9e10 | 347 | |
d9a970b7 | 348 | }; |
e1bbf989 | 349 | |
e99e3664 JG |
350 | namespace lttng { |
351 | namespace sessiond { | |
352 | ||
d9a970b7 | 353 | std::unique_lock<std::mutex> lock_session_list(); |
e99e3664 | 354 | |
d9a970b7 JG |
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 (¶meter.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 | |
e99e3664 JG |
453 | } /* namespace sessiond */ |
454 | } /* namespace lttng */ | |
455 | ||
5b74c7b1 | 456 | #endif /* _LTT_SESSION_H */ |