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