2 * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "lttng-sessiond.hpp"
10 #include "notification-thread-commands.hpp"
11 #include "session.hpp"
12 #include "trace-class.hpp"
13 #include "tsdl-trace-class-visitor.hpp"
14 #include "ust-app.hpp"
15 #include "ust-field-convert.hpp"
16 #include "ust-registry.hpp"
18 #include <common/compat/directory-handle.hpp>
19 #include <common/error.hpp>
20 #include <common/exception.hpp>
21 #include <common/format.hpp>
22 #include <common/macros.hpp>
23 #include <common/make-unique.hpp>
24 #include <common/pthread-lock.hpp>
25 #include <common/runas.hpp>
26 #include <common/time.hpp>
27 #include <common/urcu.hpp>
35 namespace ls
= lttng::sessiond
;
36 namespace lst
= lttng::sessiond::trace
;
37 namespace lsu
= lttng::sessiond::ust
;
40 lttng_uuid
generate_uuid_or_throw()
44 if (lttng_uuid_generate(new_uuid
)) {
45 LTTNG_THROW_POSIX("Failed to generate UST uuid", errno
);
51 int get_count_order(unsigned int count
)
55 order
= lttng_fls(count
) - 1;
56 if (count
& (count
- 1)) {
60 LTTNG_ASSERT(order
>= 0);
64 void clear_metadata_file(int fd
)
66 const auto lseek_ret
= lseek(fd
, 0, SEEK_SET
);
68 LTTNG_THROW_POSIX("Failed to seek to the beginning of the metadata file while clearing it", errno
);
71 const auto ret
= ftruncate(fd
, 0);
73 LTTNG_THROW_POSIX("Failed to truncate the metadata file while clearing it", errno
);
78 * Validate that the id has reached the maximum allowed or not.
80 bool is_max_channel_id(uint32_t id
)
82 return id
== UINT32_MAX
;
85 void destroy_channel_rcu(struct rcu_head
*head
)
88 DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
89 lsu::registry_channel
*chan
=
90 caa_container_of(head
, lsu::registry_channel
, _rcu_head
);
97 * Destroy every element of the registry and free the memory. This does NOT
98 * free the registry pointer since it might not have been allocated before so
99 * it's the caller responsability.
101 void destroy_channel(lsu::registry_channel
*chan
, bool notify
)
103 struct lttng_ht_iter iter
;
104 lttng::sessiond::ust::registry_event
*event
;
105 enum lttng_error_code cmd_ret
;
110 cmd_ret
= notification_thread_command_remove_channel(
111 the_notification_thread_handle
,
112 chan
->_consumer_key
, LTTNG_DOMAIN_UST
);
113 if (cmd_ret
!= LTTNG_OK
) {
114 ERR("Failed to remove channel from notification thread");
119 lttng::urcu::read_lock_guard read_lock_guard
;
121 /* Destroy all event associated with this registry. */
123 DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
124 cds_lfht_for_each_entry(
125 chan
->_events
->ht
, &iter
.iter
, event
, _node
.node
) {
126 /* Delete the node from the ht and free it. */
127 ust_registry_channel_destroy_event(chan
, event
);
132 call_rcu(&chan
->_rcu_head
, destroy_channel_rcu
);
136 void ls::details::locked_ust_registry_session_release(ust_registry_session
*session
)
138 pthread_mutex_unlock(&session
->_lock
);
141 ust_registry_session::ust_registry_session(const struct lst::abi
& in_abi
,
144 const char *root_shm_path
,
145 const char *shm_path
,
148 uint64_t tracing_id
) :
149 lst::trace_class(in_abi
, generate_uuid_or_throw()),
152 _app_tracer_version_major
{major
},
153 _app_tracer_version_minor
{minor
},
154 _tracing_id
{tracing_id
},
155 _metadata_generating_visitor
{lttng::make_unique
<ls::tsdl::trace_class_visitor
>(
156 abi
, [this](const std::string
& fragment
) {
157 _append_metadata_fragment(fragment
);
160 pthread_mutex_init(&_lock
, NULL
);
161 strncpy(_root_shm_path
, root_shm_path
, sizeof(_root_shm_path
));
162 _root_shm_path
[sizeof(_root_shm_path
) - 1] = '\0';
164 strncpy(_shm_path
, shm_path
, sizeof(_shm_path
));
165 _shm_path
[sizeof(_shm_path
) - 1] = '\0';
166 strncpy(_metadata_path
, shm_path
, sizeof(_metadata_path
));
167 _metadata_path
[sizeof(_metadata_path
) - 1] = '\0';
168 strncat(_metadata_path
, "/metadata",
169 sizeof(_metadata_path
) - strlen(_metadata_path
) - 1);
173 if (run_as_mkdir_recursive(_shm_path
, S_IRWXU
| S_IRWXG
, euid
, egid
)) {
174 LTTNG_THROW_POSIX("run_as_mkdir_recursive", errno
);
178 if (_metadata_path
[0]) {
179 /* Create metadata file. */
180 const int ret
= run_as_open(_metadata_path
, O_WRONLY
| O_CREAT
| O_EXCL
,
181 S_IRUSR
| S_IWUSR
, euid
, egid
);
184 std::stringstream ss
;
186 ss
<< "Opening metadata file '" << _metadata_path
<< "'";
187 LTTNG_THROW_POSIX(ss
.str(), errno
);
193 _enums
.reset(lttng_ht_new(0, LTTNG_HT_TYPE_STRING
));
195 LTTNG_THROW_POSIX("Failed to create enums hash table", ENOMEM
);
198 /* hash/match functions are specified at call site. */
199 _enums
->match_fct
= NULL
;
200 _enums
->hash_fct
= NULL
;
202 _channels
.reset(lttng_ht_new(0, LTTNG_HT_TYPE_U64
));
204 LTTNG_THROW_POSIX("Failed to create channels hash table", ENOMEM
);
208 ust_registry_session::~ust_registry_session()
211 struct lttng_ht_iter iter
;
212 lsu::registry_channel
*chan
;
213 lsu::registry_enum
*reg_enum
;
215 /* On error, EBUSY can be returned if lock. Code flow error. */
216 ret
= pthread_mutex_destroy(&_lock
);
220 lttng::urcu::read_lock_guard read_lock_guard
;
222 /* Destroy all event associated with this registry. */
224 DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
225 cds_lfht_for_each_entry(_channels
->ht
, &iter
.iter
, chan
, _node
.node
) {
226 /* Delete the node from the ht and free it. */
227 ret
= lttng_ht_del(_channels
.get(), &iter
);
229 destroy_channel(chan
, true);
235 if (_metadata_fd
>= 0) {
236 ret
= close(_metadata_fd
);
241 ret
= run_as_unlink(_metadata_path
, _uid
, _gid
);
247 if (_root_shm_path
[0]) {
248 /* Try to delete the directory hierarchy. */
249 (void) run_as_rmdir_recursive(_root_shm_path
, _uid
, _gid
,
250 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
253 /* Destroy the enum hash table */
256 /* Destroy all enum entries associated with this registry. */
258 DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
259 cds_lfht_for_each_entry (_enums
->ht
, &iter
.iter
, reg_enum
, node
.node
) {
260 ust_registry_destroy_enum(this, reg_enum
);
268 ust_registry_session::locked_ptr
ust_registry_session::lock()
270 pthread_mutex_lock(&_lock
);
271 return locked_ptr(this);
275 * Initialize registry with default values.
277 void ust_registry_session::add_channel(uint64_t key
)
279 lttng::pthread::lock_guard
session_lock_guard(_lock
);
282 * Assign a channel ID right now since the event notification comes
283 * *before* the channel notify so the ID needs to be set at this point so
284 * the metadata can be dumped for that event.
286 if (is_max_channel_id(_used_channel_id
)) {
287 LTTNG_THROW_ERROR(fmt::format("Failed to allocate unique id for channel under session while adding channel"));
290 auto chan
= new lsu::registry_channel(
291 _get_next_channel_id(),
292 /* Registered channel listener. */
293 [this](const lsu::registry_channel
& registered_channel
) {
295 * Channel registration completed, serialize it's layout's
298 registered_channel
.accept(*_metadata_generating_visitor
);
300 /* Added event listener. */
301 [this](const lsu::registry_channel
& channel
,
302 const lsu::registry_event
& added_event
) {
304 * The channel and its event classes will be dumped at once when
305 * it is registered. This check prevents event classes from being
306 * declared before their stream class.
308 if (channel
.is_registered()) {
309 added_event
.accept(*_metadata_generating_visitor
);
313 lttng::urcu::read_lock_guard rcu_read_lock_guard
;
314 lttng_ht_node_init_u64(&chan
->_node
, key
);
315 lttng_ht_add_unique_u64(_channels
.get(), &chan
->_node
);
318 lttng::sessiond::ust::registry_channel
& ust_registry_session::get_channel(
319 uint64_t channel_key
) const
321 lttng::urcu::read_lock_guard read_lock_guard
;
322 struct lttng_ht_node_u64
*node
;
323 struct lttng_ht_iter iter
;
325 ASSERT_LOCKED(_lock
);
327 lttng_ht_lookup(_channels
.get(), &channel_key
, &iter
);
328 node
= lttng_ht_iter_get_node_u64(&iter
);
330 LTTNG_THROW_INVALID_ARGUMENT_ERROR(fmt::format(
331 "Invalid channel key provided: channel key = {}", channel_key
));
335 DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
336 auto chan
= caa_container_of(node
, lsu::registry_channel
, _node
);
341 void ust_registry_session::remove_channel(uint64_t channel_key
, bool notify
)
343 struct lttng_ht_iter iter
;
345 lttng::urcu::read_lock_guard read_lock_guard
;
347 ASSERT_LOCKED(_lock
);
348 auto& channel
= get_channel(channel_key
);
350 iter
.iter
.node
= &channel
._node
.node
;
351 ret
= lttng_ht_del(_channels
.get(), &iter
);
353 destroy_channel(&channel
, notify
);
356 void ust_registry_session::_visit_environment(
357 lttng::sessiond::trace::trace_class_visitor
& visitor
) const
359 ASSERT_LOCKED(_lock
);
361 visitor
.visit(lst::environment_field
<const char *>("domain", "ust"));
362 visitor
.visit(lst::environment_field
<const char *>("tracer_name", "lttng-ust"));
363 visitor
.visit(lst::environment_field
<int64_t>("tracer_major", _app_tracer_version_major
));
364 visitor
.visit(lst::environment_field
<int64_t>("tracer_minor", _app_tracer_version_minor
));
365 visitor
.visit(lst::environment_field
<const char *>("tracer_buffering_scheme",
366 get_buffering_scheme() == LTTNG_BUFFER_PER_PID
? "pid" : "uid"));
367 visitor
.visit(lst::environment_field
<int64_t>("architecture_bit_width", abi
.bits_per_long
));
370 /* The caller already holds the session and session list locks. */
371 ASSERT_SESSION_LIST_LOCKED();
372 const auto session
= lttng::sessiond::find_session_by_id(_tracing_id
);
374 LTTNG_ASSERT(session
);
375 ASSERT_LOCKED(session
->lock
);
377 visitor
.visit(lst::environment_field
<const char *>("trace_name",
378 session
->has_auto_generated_name
? DEFAULT_SESSION_NAME
:
380 visitor
.visit(lst::environment_field
<std::string
>("trace_creation_datetime",
381 lttng::utils::time_to_iso8601_str(session
->creation_time
)));
382 visitor
.visit(lst::environment_field
<const char *>("hostname", session
->hostname
));
386 void ust_registry_session::_accept_on_clock_classes(lst::trace_class_visitor
& visitor
) const
388 ASSERT_LOCKED(_lock
);
389 _clock
.accept(visitor
);
392 void ust_registry_session::_accept_on_stream_classes(lst::trace_class_visitor
& visitor
) const
394 ASSERT_LOCKED(_lock
);
396 std::vector
<const lttng::sessiond::ust::registry_channel
*> sorted_stream_classes
;
399 lttng::urcu::read_lock_guard rcu_lock_guard
;
400 const lsu::registry_channel
*channel
;
401 lttng_ht_iter channel_it
;
404 DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
405 cds_lfht_for_each_entry(_channels
->ht
, &channel_it
.iter
, channel
, _node
.node
) {
406 sorted_stream_classes
.emplace_back(channel
);
411 std::sort(sorted_stream_classes
.begin(), sorted_stream_classes
.end(),
412 [](const lttng::sessiond::ust::registry_channel
*a
,
413 const lttng::sessiond::ust::registry_channel
*b
) {
414 return a
->id
< b
->id
;
417 for (const auto stream_class
: sorted_stream_classes
) {
418 stream_class
->accept(visitor
);
423 * Return next available channel id and increment the used counter. The
424 * is_max_channel_id function MUST be called before in order to validate
425 * if the maximum number of IDs have been reached. If not, it is safe to call
428 * Return a unique channel ID. If max is reached, the used_channel_id counter
431 uint32_t ust_registry_session::_get_next_channel_id()
433 if (is_max_channel_id(_used_channel_id
)) {
434 return _used_channel_id
;
438 return _next_channel_id
++;
441 void ust_registry_session::_increase_metadata_size(size_t reservation_length
)
443 const auto new_len
= _metadata_len
+ reservation_length
;
444 auto new_alloc_len
= new_len
;
445 const auto old_alloc_len
= _metadata_alloc_len
;
447 /* Rounding the new allocation length to the next power of 2 would overflow. */
448 if (new_alloc_len
> (UINT32_MAX
>> 1)) {
449 LTTNG_THROW_ERROR("Failed to reserve trace metadata storage as the new size would overflow");
452 /* The current allocation length is already the largest we can afford. */
453 if ((old_alloc_len
<< 1) > (UINT32_MAX
>> 1)) {
454 LTTNG_THROW_ERROR("Failed to reserve trace metadata storage as the max size was already reached");
457 if (new_alloc_len
> old_alloc_len
) {
458 new_alloc_len
= std::max
<size_t>(
459 1U << get_count_order(new_alloc_len
), old_alloc_len
<< 1);
461 auto newptr
= (char *) realloc(_metadata
, new_alloc_len
);
463 LTTNG_THROW_POSIX("Failed to allocate trace metadata storage", errno
);
468 /* We zero directly the memory from start of allocation. */
469 memset(&_metadata
[old_alloc_len
], 0, new_alloc_len
- old_alloc_len
);
470 _metadata_alloc_len
= new_alloc_len
;
473 _metadata_len
+= reservation_length
;
476 void ust_registry_session::_append_metadata_fragment(const std::string
& fragment
)
478 const auto offset
= _metadata_len
;
480 _increase_metadata_size(fragment
.size());
481 memcpy(&_metadata
[offset
], fragment
.c_str(), fragment
.size());
483 if (_metadata_fd
>= 0) {
484 const auto bytes_written
=
485 lttng_write(_metadata_fd
, fragment
.c_str(), fragment
.size());
487 if (bytes_written
!= fragment
.size()) {
488 LTTNG_THROW_POSIX("Failed to write trace metadata fragment to file",
494 void ust_registry_session::_reset_metadata()
496 _metadata_len_sent
= 0;
497 memset(_metadata
, 0, _metadata_alloc_len
);
500 if (_metadata_fd
> 0) {
501 /* Clear the metadata file's content. */
502 clear_metadata_file(_metadata_fd
);
506 void ust_registry_session::_generate_metadata()
508 accept(*_metadata_generating_visitor
);
511 void ust_registry_session::regenerate_metadata()
513 lttng::pthread::lock_guard
registry_lock(_lock
);
517 _generate_metadata();