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