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