Fix: ust metadata: resample clock on regenerate metadata
[lttng-tools.git] / src / bin / lttng-sessiond / ust-registry-session.cpp
CommitLineData
aeeb48c6
JG
1/*
2 * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
d7bfb9b0
JG
8#include "field.hpp"
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"
aeeb48c6
JG
16#include "ust-registry.hpp"
17
18#include <common/compat/directory-handle.hpp>
19#include <common/error.hpp>
20#include <common/exception.hpp>
d7bfb9b0 21#include <common/format.hpp>
97f630d4 22#include <common/hashtable/utils.hpp>
17fd219b 23#include <common/macros.hpp>
d7bfb9b0 24#include <common/make-unique.hpp>
17fd219b 25#include <common/pthread-lock.hpp>
aeeb48c6 26#include <common/runas.hpp>
d7bfb9b0
JG
27#include <common/time.hpp>
28#include <common/urcu.hpp>
aeeb48c6
JG
29
30#include <fcntl.h>
d7bfb9b0
JG
31#include <functional>
32#include <mutex>
aeeb48c6
JG
33#include <sstream>
34#include <string>
35
d7bfb9b0
JG
36namespace ls = lttng::sessiond;
37namespace lst = lttng::sessiond::trace;
38namespace lsu = lttng::sessiond::ust;
39
40namespace {
41lttng_uuid generate_uuid_or_throw()
42{
43 lttng_uuid new_uuid;
44
45 if (lttng_uuid_generate(new_uuid)) {
46 LTTNG_THROW_POSIX("Failed to generate UST uuid", errno);
47 }
48
49 return new_uuid;
50}
51
52int get_count_order(unsigned int count)
53{
54 int order;
55
56 order = lttng_fls(count) - 1;
57 if (count & (count - 1)) {
58 order++;
59 }
60
61 LTTNG_ASSERT(order >= 0);
62 return order;
63}
64
65void clear_metadata_file(int fd)
66{
67 const auto lseek_ret = lseek(fd, 0, SEEK_SET);
68 if (lseek_ret < 0) {
69 LTTNG_THROW_POSIX("Failed to seek to the beginning of the metadata file while clearing it", errno);
70 }
71
72 const auto ret = ftruncate(fd, 0);
73 if (ret < 0) {
74 LTTNG_THROW_POSIX("Failed to truncate the metadata file while clearing it", errno);
75 }
76}
77
78/*
79 * Validate that the id has reached the maximum allowed or not.
80 */
81bool is_max_channel_id(uint32_t id)
82{
83 return id == UINT32_MAX;
84}
85
86void destroy_channel_rcu(struct rcu_head *head)
87{
88 DIAGNOSTIC_PUSH
89 DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
90 lsu::registry_channel *chan =
0114db0e 91 lttng::utils::container_of(head, &lsu::registry_channel::_rcu_head);
d7bfb9b0
JG
92 DIAGNOSTIC_POP
93
94 delete chan;
95}
96
97/*
98 * Destroy every element of the registry and free the memory. This does NOT
99 * free the registry pointer since it might not have been allocated before so
100 * it's the caller responsability.
3691d312
JG
101 *
102 * Called from ~registry_session(), must not throw.
d7bfb9b0 103 */
3691d312 104void destroy_channel(lsu::registry_channel *chan, bool notify) noexcept
d7bfb9b0
JG
105{
106 struct lttng_ht_iter iter;
107 lttng::sessiond::ust::registry_event *event;
108 enum lttng_error_code cmd_ret;
109
110 LTTNG_ASSERT(chan);
111
112 if (notify) {
113 cmd_ret = notification_thread_command_remove_channel(
114 the_notification_thread_handle,
115 chan->_consumer_key, LTTNG_DOMAIN_UST);
116 if (cmd_ret != LTTNG_OK) {
117 ERR("Failed to remove channel from notification thread");
118 }
119 }
120
121 if (chan->_events) {
122 lttng::urcu::read_lock_guard read_lock_guard;
123
124 /* Destroy all event associated with this registry. */
125 DIAGNOSTIC_PUSH
126 DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
127 cds_lfht_for_each_entry(
f139a4f9 128 chan->_events->ht, &iter.iter, event, _node) {
d7bfb9b0
JG
129 /* Delete the node from the ht and free it. */
130 ust_registry_channel_destroy_event(chan, event);
131 }
132 DIAGNOSTIC_POP
133 }
134
135 call_rcu(&chan->_rcu_head, destroy_channel_rcu);
136}
97f630d4
JG
137
138void destroy_enum(lsu::registry_enum *reg_enum)
139{
140 if (!reg_enum) {
141 return;
142 }
143
144 delete reg_enum;
145}
146
147void destroy_enum_rcu(struct rcu_head *head)
148{
149 DIAGNOSTIC_PUSH
150 DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
151 lsu::registry_enum *reg_enum =
0114db0e 152 lttng::utils::container_of(head, &lsu::registry_enum::rcu_head);
97f630d4
JG
153 DIAGNOSTIC_POP
154
155 destroy_enum(reg_enum);
156}
157
158/*
159 * Hash table match function for enumerations in the session. Match is
160 * performed on enumeration name, and confirmed by comparing the enum
161 * entries.
162 */
163int ht_match_enum(struct cds_lfht_node *node, const void *_key)
164{
165 lsu::registry_enum *_enum;
166 const lsu::registry_enum *key;
167
168 LTTNG_ASSERT(node);
169 LTTNG_ASSERT(_key);
170
171 DIAGNOSTIC_PUSH
172 DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
173 _enum = caa_container_of(node, lsu::registry_enum,
174 node.node);
175 DIAGNOSTIC_POP
176
177 LTTNG_ASSERT(_enum);
178 key = (lsu::registry_enum *) _key;
179
180 return *_enum == *key;
181}
182
183/*
184 * Hash table match function for enumerations in the session. Match is
185 * performed by enumeration ID.
186 */
187int ht_match_enum_id(struct cds_lfht_node *node, const void *_key)
188{
189 lsu::registry_enum *_enum;
190 const lsu::registry_enum *key = (lsu::registry_enum *) _key;
191
192 LTTNG_ASSERT(node);
193 LTTNG_ASSERT(_key);
194
195 DIAGNOSTIC_PUSH
196 DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
197 _enum = caa_container_of(node, lsu::registry_enum, node.node);
198 DIAGNOSTIC_POP
199
200 LTTNG_ASSERT(_enum);
201
202 if (_enum->id != key->id) {
203 goto no_match;
204 }
205
206 /* Match. */
207 return 1;
208
209no_match:
210 return 0;
211}
212
213/*
214 * Hash table hash function for enumerations in the session. The
215 * enumeration name is used for hashing.
216 */
217unsigned long ht_hash_enum(void *_key, unsigned long seed)
218{
219 lsu::registry_enum *key = (lsu::registry_enum *) _key;
220
221 LTTNG_ASSERT(key);
222 return hash_key_str(key->name.c_str(), seed);
223}
224
d7bfb9b0
JG
225} /* namespace */
226
b0f2e8db 227void lsu::details::locked_registry_session_release(lsu::registry_session *session)
d7bfb9b0
JG
228{
229 pthread_mutex_unlock(&session->_lock);
230}
231
b0f2e8db 232lsu::registry_session::registry_session(const struct lst::abi& in_abi,
aeeb48c6
JG
233 uint32_t major,
234 uint32_t minor,
235 const char *root_shm_path,
236 const char *shm_path,
237 uid_t euid,
238 gid_t egid,
239 uint64_t tracing_id) :
d7bfb9b0 240 lst::trace_class(in_abi, generate_uuid_or_throw()),
97f630d4
JG
241 _root_shm_path{root_shm_path ? root_shm_path : ""},
242 _shm_path{shm_path ? shm_path : ""},
243 _metadata_path{_shm_path.size() > 0 ?
244 fmt::format("{}/metadata", _shm_path) : std::string("")},
aeeb48c6
JG
245 _uid{euid},
246 _gid{egid},
97f630d4 247 _app_tracer_version{.major = major, .minor = minor},
d7bfb9b0 248 _tracing_id{tracing_id},
042670db 249 _clock{lttng::make_unique<lsu::clock_class>()},
97f630d4
JG
250 _metadata_generating_visitor{lttng::make_unique<ls::tsdl::trace_class_visitor>(abi,
251 [this](const std::string& fragment) {
d7bfb9b0
JG
252 _append_metadata_fragment(fragment);
253 })}
aeeb48c6
JG
254{
255 pthread_mutex_init(&_lock, NULL);
97f630d4
JG
256 if (_shm_path.size() > 0) {
257 if (run_as_mkdir_recursive(_shm_path.c_str(), S_IRWXU | S_IRWXG, euid, egid)) {
aeeb48c6
JG
258 LTTNG_THROW_POSIX("run_as_mkdir_recursive", errno);
259 }
260 }
261
97f630d4 262 if (_metadata_path.size() > 0) {
aeeb48c6 263 /* Create metadata file. */
97f630d4 264 const int ret = run_as_open(_metadata_path.c_str(), O_WRONLY | O_CREAT | O_EXCL,
aeeb48c6 265 S_IRUSR | S_IWUSR, euid, egid);
aeeb48c6 266 if (ret < 0) {
97f630d4
JG
267 LTTNG_THROW_POSIX(fmt::format("Failed to open metadata file during registry session creation: path = {}",
268 _metadata_path), errno);
aeeb48c6
JG
269 }
270
271 _metadata_fd = ret;
272 }
273
274 _enums.reset(lttng_ht_new(0, LTTNG_HT_TYPE_STRING));
275 if (!_enums) {
276 LTTNG_THROW_POSIX("Failed to create enums hash table", ENOMEM);
277 }
278
279 /* hash/match functions are specified at call site. */
280 _enums->match_fct = NULL;
281 _enums->hash_fct = NULL;
282
283 _channels.reset(lttng_ht_new(0, LTTNG_HT_TYPE_U64));
284 if (!_channels) {
285 LTTNG_THROW_POSIX("Failed to create channels hash table", ENOMEM);
286 }
aeeb48c6
JG
287}
288
97f630d4
JG
289/*
290 * For a given enumeration in a registry, delete the entry and destroy
291 * the enumeration.
3691d312
JG
292 *
293 * Note that this is used by ~registry_session() and must not throw.
97f630d4 294 */
3691d312 295void lsu::registry_session::_destroy_enum(lsu::registry_enum *reg_enum) noexcept
97f630d4
JG
296{
297 int ret;
298 lttng::urcu::read_lock_guard read_lock_guard;
299
300 LTTNG_ASSERT(reg_enum);
301 ASSERT_RCU_READ_LOCKED();
302
303 /* Delete the node first. */
304 struct lttng_ht_iter iter;
305 iter.iter.node = &reg_enum->node.node;
306 ret = lttng_ht_del(_enums.get(), &iter);
307 LTTNG_ASSERT(!ret);
308 call_rcu(&reg_enum->rcu_head, destroy_enum_rcu);
309}
310
b0f2e8db 311lsu::registry_session::~registry_session()
aeeb48c6
JG
312{
313 int ret;
314 struct lttng_ht_iter iter;
d7bfb9b0
JG
315 lsu::registry_channel *chan;
316 lsu::registry_enum *reg_enum;
aeeb48c6
JG
317
318 /* On error, EBUSY can be returned if lock. Code flow error. */
319 ret = pthread_mutex_destroy(&_lock);
320 LTTNG_ASSERT(!ret);
321
322 if (_channels) {
d7bfb9b0
JG
323 lttng::urcu::read_lock_guard read_lock_guard;
324
aeeb48c6 325 /* Destroy all event associated with this registry. */
d7bfb9b0
JG
326 DIAGNOSTIC_PUSH
327 DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
328 cds_lfht_for_each_entry(_channels->ht, &iter.iter, chan, _node.node) {
aeeb48c6
JG
329 /* Delete the node from the ht and free it. */
330 ret = lttng_ht_del(_channels.get(), &iter);
331 LTTNG_ASSERT(!ret);
d7bfb9b0 332 destroy_channel(chan, true);
aeeb48c6 333 }
d7bfb9b0 334 DIAGNOSTIC_POP
aeeb48c6
JG
335 }
336
337 free(_metadata);
338 if (_metadata_fd >= 0) {
339 ret = close(_metadata_fd);
340 if (ret) {
341 PERROR("close");
342 }
343
97f630d4 344 ret = run_as_unlink(_metadata_path.c_str(), _uid, _gid);
aeeb48c6
JG
345 if (ret) {
346 PERROR("unlink");
347 }
348 }
349
350 if (_root_shm_path[0]) {
351 /* Try to delete the directory hierarchy. */
97f630d4 352 (void) run_as_rmdir_recursive(_root_shm_path.c_str(), _uid, _gid,
aeeb48c6
JG
353 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG);
354 }
355
356 /* Destroy the enum hash table */
357 if (_enums) {
97f630d4
JG
358 lttng::urcu::read_lock_guard read_lock_guard;
359
aeeb48c6 360 /* Destroy all enum entries associated with this registry. */
d7bfb9b0
JG
361 DIAGNOSTIC_PUSH
362 DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
aeeb48c6 363 cds_lfht_for_each_entry (_enums->ht, &iter.iter, reg_enum, node.node) {
97f630d4 364 _destroy_enum(reg_enum);
aeeb48c6 365 }
d7bfb9b0 366 DIAGNOSTIC_POP
aeeb48c6
JG
367 }
368}
369
97f630d4 370lsu::registry_session::locked_ptr lsu::registry_session::lock() noexcept
aeeb48c6 371{
d7bfb9b0
JG
372 pthread_mutex_lock(&_lock);
373 return locked_ptr(this);
374}
375
376/*
377 * Initialize registry with default values.
378 */
b0f2e8db 379void lsu::registry_session::add_channel(uint64_t key)
d7bfb9b0
JG
380{
381 lttng::pthread::lock_guard session_lock_guard(_lock);
382
383 /*
384 * Assign a channel ID right now since the event notification comes
385 * *before* the channel notify so the ID needs to be set at this point so
386 * the metadata can be dumped for that event.
387 */
388 if (is_max_channel_id(_used_channel_id)) {
389 LTTNG_THROW_ERROR(fmt::format("Failed to allocate unique id for channel under session while adding channel"));
390 }
391
392 auto chan = new lsu::registry_channel(
393 _get_next_channel_id(),
394 /* Registered channel listener. */
395 [this](const lsu::registry_channel& registered_channel) {
396 /*
397 * Channel registration completed, serialize it's layout's
398 * description.
399 */
400 registered_channel.accept(*_metadata_generating_visitor);
401 },
402 /* Added event listener. */
403 [this](const lsu::registry_channel& channel,
404 const lsu::registry_event& added_event) {
405 /*
406 * The channel and its event classes will be dumped at once when
407 * it is registered. This check prevents event classes from being
408 * declared before their stream class.
409 */
410 if (channel.is_registered()) {
411 added_event.accept(*_metadata_generating_visitor);
412 }
413 });
414
415 lttng::urcu::read_lock_guard rcu_read_lock_guard;
416 lttng_ht_node_init_u64(&chan->_node, key);
417 lttng_ht_add_unique_u64(_channels.get(), &chan->_node);
418}
419
b0f2e8db 420lttng::sessiond::ust::registry_channel& lsu::registry_session::get_channel(
d7bfb9b0
JG
421 uint64_t channel_key) const
422{
423 lttng::urcu::read_lock_guard read_lock_guard;
424 struct lttng_ht_node_u64 *node;
425 struct lttng_ht_iter iter;
426
427 ASSERT_LOCKED(_lock);
428
429 lttng_ht_lookup(_channels.get(), &channel_key, &iter);
430 node = lttng_ht_iter_get_node_u64(&iter);
431 if (!node) {
432 LTTNG_THROW_INVALID_ARGUMENT_ERROR(fmt::format(
433 "Invalid channel key provided: channel key = {}", channel_key));
434 }
17fd219b 435
d7bfb9b0
JG
436 DIAGNOSTIC_PUSH
437 DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
0114db0e 438 auto chan = lttng::utils::container_of(node, &lsu::registry_channel::_node);
d7bfb9b0
JG
439 DIAGNOSTIC_POP
440 return *chan;
441}
442
b0f2e8db 443void lsu::registry_session::remove_channel(uint64_t channel_key, bool notify)
d7bfb9b0
JG
444{
445 struct lttng_ht_iter iter;
446 int ret;
447 lttng::urcu::read_lock_guard read_lock_guard;
448
449 ASSERT_LOCKED(_lock);
450 auto& channel = get_channel(channel_key);
451
452 iter.iter.node = &channel._node.node;
453 ret = lttng_ht_del(_channels.get(), &iter);
454 LTTNG_ASSERT(!ret);
455 destroy_channel(&channel, notify);
456}
457
b0f2e8db 458void lsu::registry_session::_visit_environment(
d7bfb9b0
JG
459 lttng::sessiond::trace::trace_class_visitor& visitor) const
460{
461 ASSERT_LOCKED(_lock);
462
463 visitor.visit(lst::environment_field<const char *>("domain", "ust"));
464 visitor.visit(lst::environment_field<const char *>("tracer_name", "lttng-ust"));
97f630d4
JG
465 visitor.visit(lst::environment_field<int64_t>("tracer_major", _app_tracer_version.major));
466 visitor.visit(lst::environment_field<int64_t>("tracer_minor", _app_tracer_version.minor));
d7bfb9b0
JG
467 visitor.visit(lst::environment_field<const char *>("tracer_buffering_scheme",
468 get_buffering_scheme() == LTTNG_BUFFER_PER_PID ? "pid" : "uid"));
469 visitor.visit(lst::environment_field<int64_t>("architecture_bit_width", abi.bits_per_long));
470
471 {
472 /* The caller already holds the session and session list locks. */
473 ASSERT_SESSION_LIST_LOCKED();
474 const auto session = lttng::sessiond::find_session_by_id(_tracing_id);
475
476 LTTNG_ASSERT(session);
477 ASSERT_LOCKED(session->lock);
478
479 visitor.visit(lst::environment_field<const char *>("trace_name",
480 session->has_auto_generated_name ? DEFAULT_SESSION_NAME :
481 session->name));
482 visitor.visit(lst::environment_field<std::string>("trace_creation_datetime",
483 lttng::utils::time_to_iso8601_str(session->creation_time)));
484 visitor.visit(lst::environment_field<const char *>("hostname", session->hostname));
aeeb48c6
JG
485 }
486}
d7bfb9b0 487
b0f2e8db 488void lsu::registry_session::_accept_on_clock_classes(lst::trace_class_visitor& visitor) const
d7bfb9b0
JG
489{
490 ASSERT_LOCKED(_lock);
042670db 491 _clock->accept(visitor);
d7bfb9b0
JG
492}
493
b0f2e8db 494void lsu::registry_session::_accept_on_stream_classes(lst::trace_class_visitor& visitor) const
d7bfb9b0
JG
495{
496 ASSERT_LOCKED(_lock);
497
498 std::vector<const lttng::sessiond::ust::registry_channel *> sorted_stream_classes;
499
500 {
501 lttng::urcu::read_lock_guard rcu_lock_guard;
502 const lsu::registry_channel *channel;
503 lttng_ht_iter channel_it;
504
505 DIAGNOSTIC_PUSH
506 DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
507 cds_lfht_for_each_entry(_channels->ht, &channel_it.iter, channel, _node.node) {
508 sorted_stream_classes.emplace_back(channel);
509 }
510 DIAGNOSTIC_POP
511 }
512
513 std::sort(sorted_stream_classes.begin(), sorted_stream_classes.end(),
514 [](const lttng::sessiond::ust::registry_channel *a,
515 const lttng::sessiond::ust::registry_channel *b) {
516 return a->id < b->id;
517 });
518
519 for (const auto stream_class : sorted_stream_classes) {
520 stream_class->accept(visitor);
521 }
522}
523
524/*
525 * Return next available channel id and increment the used counter. The
526 * is_max_channel_id function MUST be called before in order to validate
527 * if the maximum number of IDs have been reached. If not, it is safe to call
528 * this function.
529 *
530 * Return a unique channel ID. If max is reached, the used_channel_id counter
531 * is returned.
532 */
b0f2e8db 533uint32_t lsu::registry_session::_get_next_channel_id()
d7bfb9b0
JG
534{
535 if (is_max_channel_id(_used_channel_id)) {
536 return _used_channel_id;
537 }
538
539 _used_channel_id++;
540 return _next_channel_id++;
541}
542
b0f2e8db 543void lsu::registry_session::_increase_metadata_size(size_t reservation_length)
d7bfb9b0
JG
544{
545 const auto new_len = _metadata_len + reservation_length;
546 auto new_alloc_len = new_len;
547 const auto old_alloc_len = _metadata_alloc_len;
548
549 /* Rounding the new allocation length to the next power of 2 would overflow. */
550 if (new_alloc_len > (UINT32_MAX >> 1)) {
551 LTTNG_THROW_ERROR("Failed to reserve trace metadata storage as the new size would overflow");
552 }
553
554 /* The current allocation length is already the largest we can afford. */
555 if ((old_alloc_len << 1) > (UINT32_MAX >> 1)) {
556 LTTNG_THROW_ERROR("Failed to reserve trace metadata storage as the max size was already reached");
557 }
558
559 if (new_alloc_len > old_alloc_len) {
560 new_alloc_len = std::max<size_t>(
561 1U << get_count_order(new_alloc_len), old_alloc_len << 1);
562
563 auto newptr = (char *) realloc(_metadata, new_alloc_len);
564 if (!newptr) {
565 LTTNG_THROW_POSIX("Failed to allocate trace metadata storage", errno);
566 }
567
568 _metadata = newptr;
569
570 /* We zero directly the memory from start of allocation. */
571 memset(&_metadata[old_alloc_len], 0, new_alloc_len - old_alloc_len);
572 _metadata_alloc_len = new_alloc_len;
573 }
574
575 _metadata_len += reservation_length;
576}
577
b0f2e8db 578void lsu::registry_session::_append_metadata_fragment(const std::string& fragment)
d7bfb9b0
JG
579{
580 const auto offset = _metadata_len;
581
582 _increase_metadata_size(fragment.size());
583 memcpy(&_metadata[offset], fragment.c_str(), fragment.size());
584
585 if (_metadata_fd >= 0) {
586 const auto bytes_written =
587 lttng_write(_metadata_fd, fragment.c_str(), fragment.size());
588
589 if (bytes_written != fragment.size()) {
590 LTTNG_THROW_POSIX("Failed to write trace metadata fragment to file",
591 errno);
592 }
593 }
594}
595
b0f2e8db 596void lsu::registry_session::_reset_metadata()
d7bfb9b0
JG
597{
598 _metadata_len_sent = 0;
599 memset(_metadata, 0, _metadata_alloc_len);
600 _metadata_len = 0;
601
602 if (_metadata_fd > 0) {
603 /* Clear the metadata file's content. */
604 clear_metadata_file(_metadata_fd);
605 }
606}
607
b0f2e8db 608void lsu::registry_session::_generate_metadata()
d7bfb9b0
JG
609{
610 accept(*_metadata_generating_visitor);
611}
612
b0f2e8db 613void lsu::registry_session::regenerate_metadata()
d7bfb9b0
JG
614{
615 lttng::pthread::lock_guard registry_lock(_lock);
616
042670db
JR
617 /* Resample the clock */
618 _clock = lttng::make_unique<lsu::clock_class>();
619
d7bfb9b0
JG
620 _metadata_version++;
621 _reset_metadata();
622 _generate_metadata();
623}
97f630d4
JG
624
625/*
626 * Lookup enumeration by enum ID.
627 *
628 * Note that there is no need to lock the registry session as this only
629 * performs an RCU-protected look-up. The function also return an rcu-protected
630 * reference, which ensures that the caller keeps the RCU read lock until it
631 * disposes of the object.
632 */
633lsu::registry_enum::const_rcu_protected_reference
634lsu::registry_session::get_enumeration(const char *enum_name, uint64_t enum_id) const
635{
636 lsu::registry_enum *reg_enum = NULL;
637 struct lttng_ht_node_str *node;
638 struct lttng_ht_iter iter;
639 lttng::urcu::unique_read_lock rcu_lock;
640 /*
641 * Hack: only the name is used for hashing; the rest of the attributes
642 * can be fudged.
643 */
644 lsu::registry_signed_enum reg_enum_lookup(enum_name, nullptr, 0);
645
646 ASSERT_RCU_READ_LOCKED();
647
648 reg_enum_lookup.id = enum_id;
649 cds_lfht_lookup(_enums->ht,
650 ht_hash_enum((void *) &reg_enum_lookup, lttng_ht_seed),
651 ht_match_enum_id, &reg_enum_lookup, &iter.iter);
652 node = lttng_ht_iter_get_node_str(&iter);
653 if (!node) {
654 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
655 "Unknown enumeration referenced by application event field: enum name = `{}`, enum id = {}",
656 enum_name, enum_id));
657 }
658
659 DIAGNOSTIC_PUSH
660 DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
0114db0e 661 reg_enum = lttng::utils::container_of(node, &lsu::registry_enum::node);
97f630d4
JG
662 DIAGNOSTIC_POP
663
664 return lsu::registry_enum::const_rcu_protected_reference{*reg_enum, std::move(rcu_lock)};
665}
666
667/*
668 * Lookup enumeration by name and comparing enumeration entries.
669 * Needs to be called from RCU read-side critical section.
670 */
671lsu::registry_enum *lsu::registry_session::_lookup_enum(
672 const lsu::registry_enum *reg_enum_lookup) const
673{
674 lsu::registry_enum *reg_enum = NULL;
675 struct lttng_ht_node_str *node;
676 struct lttng_ht_iter iter;
677
678 ASSERT_RCU_READ_LOCKED();
679
680 cds_lfht_lookup(_enums->ht, ht_hash_enum((void *) reg_enum_lookup, lttng_ht_seed),
681 ht_match_enum, reg_enum_lookup, &iter.iter);
682 node = lttng_ht_iter_get_node_str(&iter);
683 if (!node) {
684 goto end;
685 }
686
687 DIAGNOSTIC_PUSH
688 DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
0114db0e 689 reg_enum = lttng::utils::container_of(node, &lsu::registry_enum::node);
97f630d4
JG
690 DIAGNOSTIC_POP
691
692end:
693 return reg_enum;
694}
695
696/*
697 * Create a lsu::registry_enum from the given parameters and add it to the
698 * registry hash table, or find it if already there.
699 *
700 * Should be called with session registry mutex held.
701 *
702 * We receive ownership of entries.
703 */
704void lsu::registry_session::create_or_find_enum(
705 int session_objd, const char *enum_name,
706 struct lttng_ust_ctl_enum_entry *raw_entries, size_t nr_entries,
707 uint64_t *enum_id)
708{
709 struct cds_lfht_node *nodep;
710 lsu::registry_enum *reg_enum = NULL, *old_reg_enum;
711 lttng::urcu::read_lock_guard read_lock_guard;
712 auto entries = lttng::make_unique_wrapper<lttng_ust_ctl_enum_entry, lttng::free>(raw_entries);
713
714 LTTNG_ASSERT(enum_name);
715
716 /*
717 * This should not happen but since it comes from the UST tracer, an
718 * external party, don't assert and simply validate values.
719 */
720 if (session_objd < 0) {
721 LTTNG_THROW_INVALID_ARGUMENT_ERROR(fmt::format(
722 "Invalid parameters used to create or look-up enumeration from registry session: session_objd = {}",
723 session_objd));
724 }
725 if (nr_entries == 0) {
726 LTTNG_THROW_INVALID_ARGUMENT_ERROR(fmt::format(
727 "Invalid parameters used to create or look-up enumeration from registry session: nr_entries = {}",
728 nr_entries));
729 }
730 if (lttng_strnlen(enum_name, LTTNG_UST_ABI_SYM_NAME_LEN) ==
731 LTTNG_UST_ABI_SYM_NAME_LEN) {
732 LTTNG_THROW_INVALID_ARGUMENT_ERROR(
733 "Invalid parameters used to create or look-up enumeration from registry session: enumeration name is not null terminated");
734 }
735
736 if (entries->start.signedness) {
737 reg_enum = new lsu::registry_signed_enum(
738 enum_name, entries.get(), nr_entries);
739 } else {
740 reg_enum = new lsu::registry_unsigned_enum(
741 enum_name, entries.get(), nr_entries);
742 }
743
744 old_reg_enum = _lookup_enum(reg_enum);
745 if (old_reg_enum) {
746 DBG("enum %s already in sess_objd: %u", enum_name, session_objd);
747 /* Fall through. Use prior enum. */
748 destroy_enum(reg_enum);
749 reg_enum = old_reg_enum;
750 } else {
751 DBG("UST registry creating enum: %s, sess_objd: %u",
752 enum_name, session_objd);
753 if (_next_enum_id == -1ULL) {
754 destroy_enum(reg_enum);
755 LTTNG_THROW_ERROR("Failed to allocate unique enumeration ID as it would overflow");
756 }
757
758 reg_enum->id = _next_enum_id++;
759 nodep = cds_lfht_add_unique(_enums->ht,
760 ht_hash_enum(reg_enum, lttng_ht_seed),
761 ht_match_enum_id, reg_enum,
762 &reg_enum->node.node);
763 LTTNG_ASSERT(nodep == &reg_enum->node.node);
764 }
765
766 DBG("UST registry reply with enum %s with id %" PRIu64 " in sess_objd: %u",
767 enum_name, reg_enum->id, session_objd);
768 *enum_id = reg_enum->id;
f139a4f9 769}
This page took 0.058227 seconds and 4 git commands to generate.