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