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