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