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