Commit | Line | Data |
---|---|---|
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" | |
eb5e469d | 14 | #include "ctf2-trace-class-visitor.hpp" |
d7bfb9b0 JG |
15 | #include "ust-app.hpp" |
16 | #include "ust-field-convert.hpp" | |
aeeb48c6 JG |
17 | #include "ust-registry.hpp" |
18 | ||
19 | #include <common/compat/directory-handle.hpp> | |
20 | #include <common/error.hpp> | |
21 | #include <common/exception.hpp> | |
d7bfb9b0 | 22 | #include <common/format.hpp> |
97f630d4 | 23 | #include <common/hashtable/utils.hpp> |
17fd219b | 24 | #include <common/macros.hpp> |
d7bfb9b0 | 25 | #include <common/make-unique.hpp> |
17fd219b | 26 | #include <common/pthread-lock.hpp> |
aeeb48c6 | 27 | #include <common/runas.hpp> |
d7bfb9b0 JG |
28 | #include <common/time.hpp> |
29 | #include <common/urcu.hpp> | |
aeeb48c6 JG |
30 | |
31 | #include <fcntl.h> | |
d7bfb9b0 | 32 | #include <functional> |
058a2241 | 33 | #include <initializer_list> |
d7bfb9b0 | 34 | #include <mutex> |
aeeb48c6 JG |
35 | #include <sstream> |
36 | #include <string> | |
37 | ||
d7bfb9b0 JG |
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 = | |
0114db0e | 93 | lttng::utils::container_of(head, &lsu::registry_channel::_rcu_head); |
d7bfb9b0 JG |
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. | |
3691d312 JG |
103 | * |
104 | * Called from ~registry_session(), must not throw. | |
d7bfb9b0 | 105 | */ |
3691d312 | 106 | void destroy_channel(lsu::registry_channel *chan, bool notify) noexcept |
d7bfb9b0 JG |
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( | |
f139a4f9 | 130 | chan->_events->ht, &iter.iter, event, _node) { |
d7bfb9b0 JG |
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 | } | |
97f630d4 JG |
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 = | |
0114db0e | 154 | lttng::utils::container_of(head, &lsu::registry_enum::rcu_head); |
97f630d4 JG |
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 | } | |
d7bfb9b0 JG |
226 | } /* namespace */ |
227 | ||
b0f2e8db | 228 | void lsu::details::locked_registry_session_release(lsu::registry_session *session) |
d7bfb9b0 JG |
229 | { |
230 | pthread_mutex_unlock(&session->_lock); | |
231 | } | |
232 | ||
b0f2e8db | 233 | lsu::registry_session::registry_session(const struct lst::abi& in_abi, |
aeeb48c6 JG |
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) : | |
d7bfb9b0 | 241 | lst::trace_class(in_abi, generate_uuid_or_throw()), |
97f630d4 JG |
242 | _root_shm_path{root_shm_path ? root_shm_path : ""}, |
243 | _shm_path{shm_path ? shm_path : ""}, | |
058a2241 JG |
244 | _metadata_path{_shm_path.size() > 0 ? fmt::format("{}/metadata", _shm_path) : |
245 | std::string("")}, | |
aeeb48c6 JG |
246 | _uid{euid}, |
247 | _gid{egid}, | |
97f630d4 | 248 | _app_tracer_version{.major = major, .minor = minor}, |
d7bfb9b0 | 249 | _tracing_id{tracing_id}, |
c3702561 | 250 | _metadata_generating_visitor{lttng::make_unique<ls::tsdl::trace_class_visitor>( |
058a2241 | 251 | abi, [this](const std::string& fragment) { |
d7bfb9b0 | 252 | _append_metadata_fragment(fragment); |
12eb5cfe JG |
253 | })}, |
254 | _packet_header{_create_packet_header()} | |
aeeb48c6 JG |
255 | { |
256 | pthread_mutex_init(&_lock, NULL); | |
97f630d4 JG |
257 | if (_shm_path.size() > 0) { |
258 | if (run_as_mkdir_recursive(_shm_path.c_str(), S_IRWXU | S_IRWXG, euid, egid)) { | |
aeeb48c6 JG |
259 | LTTNG_THROW_POSIX("run_as_mkdir_recursive", errno); |
260 | } | |
261 | } | |
262 | ||
97f630d4 | 263 | if (_metadata_path.size() > 0) { |
aeeb48c6 | 264 | /* Create metadata file. */ |
97f630d4 | 265 | const int ret = run_as_open(_metadata_path.c_str(), O_WRONLY | O_CREAT | O_EXCL, |
aeeb48c6 | 266 | S_IRUSR | S_IWUSR, euid, egid); |
aeeb48c6 | 267 | if (ret < 0) { |
97f630d4 JG |
268 | LTTNG_THROW_POSIX(fmt::format("Failed to open metadata file during registry session creation: path = {}", |
269 | _metadata_path), errno); | |
aeeb48c6 JG |
270 | } |
271 | ||
272 | _metadata_fd = ret; | |
273 | } | |
274 | ||
275 | _enums.reset(lttng_ht_new(0, LTTNG_HT_TYPE_STRING)); | |
276 | if (!_enums) { | |
277 | LTTNG_THROW_POSIX("Failed to create enums hash table", ENOMEM); | |
278 | } | |
279 | ||
280 | /* hash/match functions are specified at call site. */ | |
281 | _enums->match_fct = NULL; | |
282 | _enums->hash_fct = NULL; | |
283 | ||
284 | _channels.reset(lttng_ht_new(0, LTTNG_HT_TYPE_U64)); | |
285 | if (!_channels) { | |
286 | LTTNG_THROW_POSIX("Failed to create channels hash table", ENOMEM); | |
287 | } | |
aeeb48c6 JG |
288 | } |
289 | ||
12eb5cfe JG |
290 | lst::type::cuptr lsu::registry_session::_create_packet_header() const |
291 | { | |
292 | lst::structure_type::fields packet_header_fields; | |
293 | ||
294 | /* uint32_t magic */ | |
295 | packet_header_fields.emplace_back(lttng::make_unique<lst::field>("magic", | |
296 | lttng::make_unique<lst::integer_type>(abi.uint32_t_alignment, | |
297 | abi.byte_order, 32, lst::integer_type::signedness::UNSIGNED, | |
298 | lst::integer_type::base::HEXADECIMAL, | |
299 | std::initializer_list<lst::integer_type::role>({lst::integer_type::role::PACKET_MAGIC_NUMBER})))); | |
300 | ||
301 | /* uuid */ | |
302 | packet_header_fields.emplace_back(lttng::make_unique<lst::field>("uuid", | |
303 | lttng::make_unique<lst::static_length_blob_type>(0, 16, | |
eb5e469d | 304 | std::initializer_list<lst::static_length_blob_type::role>({lst::static_length_blob_type::role::METADATA_STREAM_UUID})))); |
12eb5cfe JG |
305 | |
306 | /* uint32_t stream_id */ | |
307 | packet_header_fields.emplace_back(lttng::make_unique<lst::field>("stream_id", | |
308 | lttng::make_unique<lst::integer_type>(abi.uint32_t_alignment, | |
309 | abi.byte_order, 32, lst::integer_type::signedness::UNSIGNED, | |
310 | lst::integer_type::base::DECIMAL, | |
311 | std::initializer_list<lst::integer_type::role>({lst::integer_type::role::DATA_STREAM_CLASS_ID})))); | |
312 | ||
313 | /* uint64_t stream_instance_id */ | |
314 | packet_header_fields.emplace_back(lttng::make_unique<lst::field>("stream_instance_id", | |
315 | lttng::make_unique<lst::integer_type>(abi.uint64_t_alignment, | |
316 | abi.byte_order, 64, lst::integer_type::signedness::UNSIGNED, | |
317 | lst::integer_type::base::DECIMAL, | |
318 | std::initializer_list<lst::integer_type::role>({lst::integer_type::role::DATA_STREAM_ID})))); | |
319 | ||
320 | return lttng::make_unique<lst::structure_type>(0, std::move(packet_header_fields)); | |
321 | } | |
322 | ||
323 | const lst::type *lsu::registry_session::get_packet_header() const noexcept | |
324 | { | |
325 | return _packet_header.get(); | |
326 | } | |
327 | ||
97f630d4 JG |
328 | /* |
329 | * For a given enumeration in a registry, delete the entry and destroy | |
330 | * the enumeration. | |
3691d312 JG |
331 | * |
332 | * Note that this is used by ~registry_session() and must not throw. | |
97f630d4 | 333 | */ |
3691d312 | 334 | void lsu::registry_session::_destroy_enum(lsu::registry_enum *reg_enum) noexcept |
97f630d4 JG |
335 | { |
336 | int ret; | |
337 | lttng::urcu::read_lock_guard read_lock_guard; | |
338 | ||
339 | LTTNG_ASSERT(reg_enum); | |
340 | ASSERT_RCU_READ_LOCKED(); | |
341 | ||
342 | /* Delete the node first. */ | |
343 | struct lttng_ht_iter iter; | |
344 | iter.iter.node = ®_enum->node.node; | |
345 | ret = lttng_ht_del(_enums.get(), &iter); | |
346 | LTTNG_ASSERT(!ret); | |
347 | call_rcu(®_enum->rcu_head, destroy_enum_rcu); | |
348 | } | |
349 | ||
b0f2e8db | 350 | lsu::registry_session::~registry_session() |
aeeb48c6 JG |
351 | { |
352 | int ret; | |
353 | struct lttng_ht_iter iter; | |
d7bfb9b0 JG |
354 | lsu::registry_channel *chan; |
355 | lsu::registry_enum *reg_enum; | |
aeeb48c6 JG |
356 | |
357 | /* On error, EBUSY can be returned if lock. Code flow error. */ | |
358 | ret = pthread_mutex_destroy(&_lock); | |
359 | LTTNG_ASSERT(!ret); | |
360 | ||
361 | if (_channels) { | |
d7bfb9b0 JG |
362 | lttng::urcu::read_lock_guard read_lock_guard; |
363 | ||
aeeb48c6 | 364 | /* Destroy all event associated with this registry. */ |
d7bfb9b0 JG |
365 | DIAGNOSTIC_PUSH |
366 | DIAGNOSTIC_IGNORE_INVALID_OFFSETOF | |
367 | cds_lfht_for_each_entry(_channels->ht, &iter.iter, chan, _node.node) { | |
aeeb48c6 JG |
368 | /* Delete the node from the ht and free it. */ |
369 | ret = lttng_ht_del(_channels.get(), &iter); | |
370 | LTTNG_ASSERT(!ret); | |
d7bfb9b0 | 371 | destroy_channel(chan, true); |
aeeb48c6 | 372 | } |
d7bfb9b0 | 373 | DIAGNOSTIC_POP |
aeeb48c6 JG |
374 | } |
375 | ||
376 | free(_metadata); | |
377 | if (_metadata_fd >= 0) { | |
378 | ret = close(_metadata_fd); | |
379 | if (ret) { | |
380 | PERROR("close"); | |
381 | } | |
382 | ||
97f630d4 | 383 | ret = run_as_unlink(_metadata_path.c_str(), _uid, _gid); |
aeeb48c6 JG |
384 | if (ret) { |
385 | PERROR("unlink"); | |
386 | } | |
387 | } | |
388 | ||
389 | if (_root_shm_path[0]) { | |
390 | /* Try to delete the directory hierarchy. */ | |
97f630d4 | 391 | (void) run_as_rmdir_recursive(_root_shm_path.c_str(), _uid, _gid, |
aeeb48c6 JG |
392 | LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG); |
393 | } | |
394 | ||
395 | /* Destroy the enum hash table */ | |
396 | if (_enums) { | |
97f630d4 JG |
397 | lttng::urcu::read_lock_guard read_lock_guard; |
398 | ||
aeeb48c6 | 399 | /* Destroy all enum entries associated with this registry. */ |
d7bfb9b0 JG |
400 | DIAGNOSTIC_PUSH |
401 | DIAGNOSTIC_IGNORE_INVALID_OFFSETOF | |
aeeb48c6 | 402 | cds_lfht_for_each_entry (_enums->ht, &iter.iter, reg_enum, node.node) { |
97f630d4 | 403 | _destroy_enum(reg_enum); |
aeeb48c6 | 404 | } |
d7bfb9b0 | 405 | DIAGNOSTIC_POP |
aeeb48c6 JG |
406 | } |
407 | } | |
408 | ||
97f630d4 | 409 | lsu::registry_session::locked_ptr lsu::registry_session::lock() noexcept |
aeeb48c6 | 410 | { |
d7bfb9b0 JG |
411 | pthread_mutex_lock(&_lock); |
412 | return locked_ptr(this); | |
413 | } | |
414 | ||
415 | /* | |
416 | * Initialize registry with default values. | |
417 | */ | |
b0f2e8db | 418 | void lsu::registry_session::add_channel(uint64_t key) |
d7bfb9b0 JG |
419 | { |
420 | lttng::pthread::lock_guard session_lock_guard(_lock); | |
421 | ||
422 | /* | |
423 | * Assign a channel ID right now since the event notification comes | |
424 | * *before* the channel notify so the ID needs to be set at this point so | |
425 | * the metadata can be dumped for that event. | |
426 | */ | |
427 | if (is_max_channel_id(_used_channel_id)) { | |
428 | LTTNG_THROW_ERROR(fmt::format("Failed to allocate unique id for channel under session while adding channel")); | |
429 | } | |
430 | ||
431 | auto chan = new lsu::registry_channel( | |
058a2241 | 432 | _get_next_channel_id(), abi, _clock.name, |
d7bfb9b0 JG |
433 | /* Registered channel listener. */ |
434 | [this](const lsu::registry_channel& registered_channel) { | |
435 | /* | |
436 | * Channel registration completed, serialize it's layout's | |
437 | * description. | |
438 | */ | |
439 | registered_channel.accept(*_metadata_generating_visitor); | |
440 | }, | |
441 | /* Added event listener. */ | |
442 | [this](const lsu::registry_channel& channel, | |
443 | const lsu::registry_event& added_event) { | |
444 | /* | |
445 | * The channel and its event classes will be dumped at once when | |
446 | * it is registered. This check prevents event classes from being | |
447 | * declared before their stream class. | |
448 | */ | |
449 | if (channel.is_registered()) { | |
450 | added_event.accept(*_metadata_generating_visitor); | |
451 | } | |
452 | }); | |
453 | ||
454 | lttng::urcu::read_lock_guard rcu_read_lock_guard; | |
455 | lttng_ht_node_init_u64(&chan->_node, key); | |
456 | lttng_ht_add_unique_u64(_channels.get(), &chan->_node); | |
457 | } | |
458 | ||
b0f2e8db | 459 | lttng::sessiond::ust::registry_channel& lsu::registry_session::get_channel( |
d7bfb9b0 JG |
460 | uint64_t channel_key) const |
461 | { | |
462 | lttng::urcu::read_lock_guard read_lock_guard; | |
463 | struct lttng_ht_node_u64 *node; | |
464 | struct lttng_ht_iter iter; | |
465 | ||
466 | ASSERT_LOCKED(_lock); | |
467 | ||
468 | lttng_ht_lookup(_channels.get(), &channel_key, &iter); | |
469 | node = lttng_ht_iter_get_node_u64(&iter); | |
470 | if (!node) { | |
471 | LTTNG_THROW_INVALID_ARGUMENT_ERROR(fmt::format( | |
472 | "Invalid channel key provided: channel key = {}", channel_key)); | |
473 | } | |
17fd219b | 474 | |
d7bfb9b0 JG |
475 | DIAGNOSTIC_PUSH |
476 | DIAGNOSTIC_IGNORE_INVALID_OFFSETOF | |
0114db0e | 477 | auto chan = lttng::utils::container_of(node, &lsu::registry_channel::_node); |
d7bfb9b0 JG |
478 | DIAGNOSTIC_POP |
479 | return *chan; | |
480 | } | |
481 | ||
b0f2e8db | 482 | void lsu::registry_session::remove_channel(uint64_t channel_key, bool notify) |
d7bfb9b0 JG |
483 | { |
484 | struct lttng_ht_iter iter; | |
485 | int ret; | |
486 | lttng::urcu::read_lock_guard read_lock_guard; | |
487 | ||
488 | ASSERT_LOCKED(_lock); | |
489 | auto& channel = get_channel(channel_key); | |
490 | ||
491 | iter.iter.node = &channel._node.node; | |
492 | ret = lttng_ht_del(_channels.get(), &iter); | |
493 | LTTNG_ASSERT(!ret); | |
494 | destroy_channel(&channel, notify); | |
495 | } | |
496 | ||
eb5e469d JG |
497 | void lsu::registry_session::accept( |
498 | lttng::sessiond::trace::trace_class_environment_visitor& visitor) const | |
d7bfb9b0 JG |
499 | { |
500 | ASSERT_LOCKED(_lock); | |
501 | ||
502 | visitor.visit(lst::environment_field<const char *>("domain", "ust")); | |
503 | visitor.visit(lst::environment_field<const char *>("tracer_name", "lttng-ust")); | |
97f630d4 JG |
504 | visitor.visit(lst::environment_field<int64_t>("tracer_major", _app_tracer_version.major)); |
505 | visitor.visit(lst::environment_field<int64_t>("tracer_minor", _app_tracer_version.minor)); | |
d7bfb9b0 JG |
506 | visitor.visit(lst::environment_field<const char *>("tracer_buffering_scheme", |
507 | get_buffering_scheme() == LTTNG_BUFFER_PER_PID ? "pid" : "uid")); | |
508 | visitor.visit(lst::environment_field<int64_t>("architecture_bit_width", abi.bits_per_long)); | |
509 | ||
510 | { | |
511 | /* The caller already holds the session and session list locks. */ | |
512 | ASSERT_SESSION_LIST_LOCKED(); | |
513 | const auto session = lttng::sessiond::find_session_by_id(_tracing_id); | |
514 | ||
515 | LTTNG_ASSERT(session); | |
516 | ASSERT_LOCKED(session->lock); | |
517 | ||
518 | visitor.visit(lst::environment_field<const char *>("trace_name", | |
519 | session->has_auto_generated_name ? DEFAULT_SESSION_NAME : | |
520 | session->name)); | |
521 | visitor.visit(lst::environment_field<std::string>("trace_creation_datetime", | |
522 | lttng::utils::time_to_iso8601_str(session->creation_time))); | |
523 | visitor.visit(lst::environment_field<const char *>("hostname", session->hostname)); | |
aeeb48c6 JG |
524 | } |
525 | } | |
d7bfb9b0 | 526 | |
b0f2e8db | 527 | void lsu::registry_session::_accept_on_clock_classes(lst::trace_class_visitor& visitor) const |
d7bfb9b0 JG |
528 | { |
529 | ASSERT_LOCKED(_lock); | |
530 | _clock.accept(visitor); | |
531 | } | |
532 | ||
b0f2e8db | 533 | void lsu::registry_session::_accept_on_stream_classes(lst::trace_class_visitor& visitor) const |
d7bfb9b0 JG |
534 | { |
535 | ASSERT_LOCKED(_lock); | |
536 | ||
537 | std::vector<const lttng::sessiond::ust::registry_channel *> sorted_stream_classes; | |
538 | ||
539 | { | |
540 | lttng::urcu::read_lock_guard rcu_lock_guard; | |
541 | const lsu::registry_channel *channel; | |
542 | lttng_ht_iter channel_it; | |
543 | ||
544 | DIAGNOSTIC_PUSH | |
545 | DIAGNOSTIC_IGNORE_INVALID_OFFSETOF | |
546 | cds_lfht_for_each_entry(_channels->ht, &channel_it.iter, channel, _node.node) { | |
547 | sorted_stream_classes.emplace_back(channel); | |
548 | } | |
549 | DIAGNOSTIC_POP | |
550 | } | |
551 | ||
552 | std::sort(sorted_stream_classes.begin(), sorted_stream_classes.end(), | |
553 | [](const lttng::sessiond::ust::registry_channel *a, | |
554 | const lttng::sessiond::ust::registry_channel *b) { | |
555 | return a->id < b->id; | |
556 | }); | |
557 | ||
558 | for (const auto stream_class : sorted_stream_classes) { | |
559 | stream_class->accept(visitor); | |
560 | } | |
561 | } | |
562 | ||
563 | /* | |
564 | * Return next available channel id and increment the used counter. The | |
565 | * is_max_channel_id function MUST be called before in order to validate | |
566 | * if the maximum number of IDs have been reached. If not, it is safe to call | |
567 | * this function. | |
568 | * | |
569 | * Return a unique channel ID. If max is reached, the used_channel_id counter | |
570 | * is returned. | |
571 | */ | |
b0f2e8db | 572 | uint32_t lsu::registry_session::_get_next_channel_id() |
d7bfb9b0 JG |
573 | { |
574 | if (is_max_channel_id(_used_channel_id)) { | |
575 | return _used_channel_id; | |
576 | } | |
577 | ||
578 | _used_channel_id++; | |
579 | return _next_channel_id++; | |
580 | } | |
581 | ||
b0f2e8db | 582 | void lsu::registry_session::_increase_metadata_size(size_t reservation_length) |
d7bfb9b0 JG |
583 | { |
584 | const auto new_len = _metadata_len + reservation_length; | |
585 | auto new_alloc_len = new_len; | |
586 | const auto old_alloc_len = _metadata_alloc_len; | |
587 | ||
588 | /* Rounding the new allocation length to the next power of 2 would overflow. */ | |
589 | if (new_alloc_len > (UINT32_MAX >> 1)) { | |
590 | LTTNG_THROW_ERROR("Failed to reserve trace metadata storage as the new size would overflow"); | |
591 | } | |
592 | ||
593 | /* The current allocation length is already the largest we can afford. */ | |
594 | if ((old_alloc_len << 1) > (UINT32_MAX >> 1)) { | |
595 | LTTNG_THROW_ERROR("Failed to reserve trace metadata storage as the max size was already reached"); | |
596 | } | |
597 | ||
598 | if (new_alloc_len > old_alloc_len) { | |
599 | new_alloc_len = std::max<size_t>( | |
600 | 1U << get_count_order(new_alloc_len), old_alloc_len << 1); | |
601 | ||
602 | auto newptr = (char *) realloc(_metadata, new_alloc_len); | |
603 | if (!newptr) { | |
604 | LTTNG_THROW_POSIX("Failed to allocate trace metadata storage", errno); | |
605 | } | |
606 | ||
607 | _metadata = newptr; | |
608 | ||
609 | /* We zero directly the memory from start of allocation. */ | |
610 | memset(&_metadata[old_alloc_len], 0, new_alloc_len - old_alloc_len); | |
611 | _metadata_alloc_len = new_alloc_len; | |
612 | } | |
613 | ||
614 | _metadata_len += reservation_length; | |
615 | } | |
616 | ||
b0f2e8db | 617 | void lsu::registry_session::_append_metadata_fragment(const std::string& fragment) |
d7bfb9b0 JG |
618 | { |
619 | const auto offset = _metadata_len; | |
620 | ||
621 | _increase_metadata_size(fragment.size()); | |
622 | memcpy(&_metadata[offset], fragment.c_str(), fragment.size()); | |
623 | ||
624 | if (_metadata_fd >= 0) { | |
625 | const auto bytes_written = | |
626 | lttng_write(_metadata_fd, fragment.c_str(), fragment.size()); | |
627 | ||
628 | if (bytes_written != fragment.size()) { | |
629 | LTTNG_THROW_POSIX("Failed to write trace metadata fragment to file", | |
630 | errno); | |
631 | } | |
632 | } | |
633 | } | |
634 | ||
b0f2e8db | 635 | void lsu::registry_session::_reset_metadata() |
d7bfb9b0 JG |
636 | { |
637 | _metadata_len_sent = 0; | |
638 | memset(_metadata, 0, _metadata_alloc_len); | |
639 | _metadata_len = 0; | |
640 | ||
641 | if (_metadata_fd > 0) { | |
642 | /* Clear the metadata file's content. */ | |
643 | clear_metadata_file(_metadata_fd); | |
644 | } | |
645 | } | |
646 | ||
b0f2e8db | 647 | void lsu::registry_session::_generate_metadata() |
d7bfb9b0 | 648 | { |
eb5e469d | 649 | trace_class::accept(*_metadata_generating_visitor); |
d7bfb9b0 JG |
650 | } |
651 | ||
b0f2e8db | 652 | void lsu::registry_session::regenerate_metadata() |
d7bfb9b0 JG |
653 | { |
654 | lttng::pthread::lock_guard registry_lock(_lock); | |
655 | ||
656 | _metadata_version++; | |
657 | _reset_metadata(); | |
658 | _generate_metadata(); | |
659 | } | |
97f630d4 JG |
660 | |
661 | /* | |
662 | * Lookup enumeration by enum ID. | |
663 | * | |
664 | * Note that there is no need to lock the registry session as this only | |
665 | * performs an RCU-protected look-up. The function also return an rcu-protected | |
666 | * reference, which ensures that the caller keeps the RCU read lock until it | |
667 | * disposes of the object. | |
668 | */ | |
669 | lsu::registry_enum::const_rcu_protected_reference | |
670 | lsu::registry_session::get_enumeration(const char *enum_name, uint64_t enum_id) const | |
671 | { | |
672 | lsu::registry_enum *reg_enum = NULL; | |
673 | struct lttng_ht_node_str *node; | |
674 | struct lttng_ht_iter iter; | |
675 | lttng::urcu::unique_read_lock rcu_lock; | |
676 | /* | |
677 | * Hack: only the name is used for hashing; the rest of the attributes | |
678 | * can be fudged. | |
679 | */ | |
680 | lsu::registry_signed_enum reg_enum_lookup(enum_name, nullptr, 0); | |
681 | ||
682 | ASSERT_RCU_READ_LOCKED(); | |
683 | ||
684 | reg_enum_lookup.id = enum_id; | |
685 | cds_lfht_lookup(_enums->ht, | |
686 | ht_hash_enum((void *) ®_enum_lookup, lttng_ht_seed), | |
687 | ht_match_enum_id, ®_enum_lookup, &iter.iter); | |
688 | node = lttng_ht_iter_get_node_str(&iter); | |
689 | if (!node) { | |
690 | LTTNG_THROW_PROTOCOL_ERROR(fmt::format( | |
691 | "Unknown enumeration referenced by application event field: enum name = `{}`, enum id = {}", | |
692 | enum_name, enum_id)); | |
693 | } | |
694 | ||
695 | DIAGNOSTIC_PUSH | |
696 | DIAGNOSTIC_IGNORE_INVALID_OFFSETOF | |
0114db0e | 697 | reg_enum = lttng::utils::container_of(node, &lsu::registry_enum::node); |
97f630d4 JG |
698 | DIAGNOSTIC_POP |
699 | ||
700 | return lsu::registry_enum::const_rcu_protected_reference{*reg_enum, std::move(rcu_lock)}; | |
701 | } | |
702 | ||
703 | /* | |
704 | * Lookup enumeration by name and comparing enumeration entries. | |
705 | * Needs to be called from RCU read-side critical section. | |
706 | */ | |
707 | lsu::registry_enum *lsu::registry_session::_lookup_enum( | |
708 | const lsu::registry_enum *reg_enum_lookup) const | |
709 | { | |
710 | lsu::registry_enum *reg_enum = NULL; | |
711 | struct lttng_ht_node_str *node; | |
712 | struct lttng_ht_iter iter; | |
713 | ||
714 | ASSERT_RCU_READ_LOCKED(); | |
715 | ||
716 | cds_lfht_lookup(_enums->ht, ht_hash_enum((void *) reg_enum_lookup, lttng_ht_seed), | |
717 | ht_match_enum, reg_enum_lookup, &iter.iter); | |
718 | node = lttng_ht_iter_get_node_str(&iter); | |
719 | if (!node) { | |
720 | goto end; | |
721 | } | |
722 | ||
723 | DIAGNOSTIC_PUSH | |
724 | DIAGNOSTIC_IGNORE_INVALID_OFFSETOF | |
0114db0e | 725 | reg_enum = lttng::utils::container_of(node, &lsu::registry_enum::node); |
97f630d4 JG |
726 | DIAGNOSTIC_POP |
727 | ||
728 | end: | |
729 | return reg_enum; | |
730 | } | |
731 | ||
732 | /* | |
733 | * Create a lsu::registry_enum from the given parameters and add it to the | |
734 | * registry hash table, or find it if already there. | |
735 | * | |
736 | * Should be called with session registry mutex held. | |
737 | * | |
738 | * We receive ownership of entries. | |
739 | */ | |
740 | void lsu::registry_session::create_or_find_enum( | |
741 | int session_objd, const char *enum_name, | |
742 | struct lttng_ust_ctl_enum_entry *raw_entries, size_t nr_entries, | |
743 | uint64_t *enum_id) | |
744 | { | |
745 | struct cds_lfht_node *nodep; | |
746 | lsu::registry_enum *reg_enum = NULL, *old_reg_enum; | |
747 | lttng::urcu::read_lock_guard read_lock_guard; | |
748 | auto entries = lttng::make_unique_wrapper<lttng_ust_ctl_enum_entry, lttng::free>(raw_entries); | |
749 | ||
750 | LTTNG_ASSERT(enum_name); | |
751 | ||
752 | /* | |
753 | * This should not happen but since it comes from the UST tracer, an | |
754 | * external party, don't assert and simply validate values. | |
755 | */ | |
756 | if (session_objd < 0) { | |
757 | LTTNG_THROW_INVALID_ARGUMENT_ERROR(fmt::format( | |
758 | "Invalid parameters used to create or look-up enumeration from registry session: session_objd = {}", | |
759 | session_objd)); | |
760 | } | |
761 | if (nr_entries == 0) { | |
762 | LTTNG_THROW_INVALID_ARGUMENT_ERROR(fmt::format( | |
763 | "Invalid parameters used to create or look-up enumeration from registry session: nr_entries = {}", | |
764 | nr_entries)); | |
765 | } | |
766 | if (lttng_strnlen(enum_name, LTTNG_UST_ABI_SYM_NAME_LEN) == | |
767 | LTTNG_UST_ABI_SYM_NAME_LEN) { | |
768 | LTTNG_THROW_INVALID_ARGUMENT_ERROR( | |
769 | "Invalid parameters used to create or look-up enumeration from registry session: enumeration name is not null terminated"); | |
770 | } | |
771 | ||
772 | if (entries->start.signedness) { | |
773 | reg_enum = new lsu::registry_signed_enum( | |
774 | enum_name, entries.get(), nr_entries); | |
775 | } else { | |
776 | reg_enum = new lsu::registry_unsigned_enum( | |
777 | enum_name, entries.get(), nr_entries); | |
778 | } | |
779 | ||
780 | old_reg_enum = _lookup_enum(reg_enum); | |
781 | if (old_reg_enum) { | |
782 | DBG("enum %s already in sess_objd: %u", enum_name, session_objd); | |
783 | /* Fall through. Use prior enum. */ | |
784 | destroy_enum(reg_enum); | |
785 | reg_enum = old_reg_enum; | |
786 | } else { | |
787 | DBG("UST registry creating enum: %s, sess_objd: %u", | |
788 | enum_name, session_objd); | |
789 | if (_next_enum_id == -1ULL) { | |
790 | destroy_enum(reg_enum); | |
791 | LTTNG_THROW_ERROR("Failed to allocate unique enumeration ID as it would overflow"); | |
792 | } | |
793 | ||
794 | reg_enum->id = _next_enum_id++; | |
795 | nodep = cds_lfht_add_unique(_enums->ht, | |
796 | ht_hash_enum(reg_enum, lttng_ht_seed), | |
797 | ht_match_enum_id, reg_enum, | |
798 | ®_enum->node.node); | |
799 | LTTNG_ASSERT(nodep == ®_enum->node.node); | |
800 | } | |
801 | ||
802 | DBG("UST registry reply with enum %s with id %" PRIu64 " in sess_objd: %u", | |
803 | enum_name, reg_enum->id, session_objd); | |
804 | *enum_id = reg_enum->id; | |
f139a4f9 | 805 | } |