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