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