2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
9 #ifndef LTTNG_UST_REGISTRY_H
10 #define LTTNG_UST_REGISTRY_H
18 #include <common/hashtable/hashtable.hpp>
19 #include <common/uuid.hpp>
21 #include <lttng/domain.h>
23 #include "lttng-ust-ctl.hpp"
25 #define CTF_SPEC_MAJOR 1
26 #define CTF_SPEC_MINOR 8
30 class ust_registry_session {
32 virtual lttng_buffer_type get_buffering_scheme() const = 0;
33 virtual ~ust_registry_session();
36 /* Prevent instanciation of this base class. */
37 ust_registry_session(unsigned int bits_per_long,
38 unsigned int uint8_t_alignment,
39 unsigned int uint16_t_alignment,
40 unsigned int uint32_t_alignment,
41 unsigned int uint64_t_alignment,
42 unsigned int long_alignment,
44 unsigned int app_tracer_version_major,
45 unsigned int app_tracer_version_minor,
46 const char *root_shm_path,
56 * With multiple writers and readers, use this lock to access
57 * the registry. Can nest within the ust app session lock.
58 * Also acts as a registry serialization lock. Used by registry
59 * readers to serialize the registry information sent from the
60 * sessiond to the consumerd.
61 * The consumer socket lock nests within this lock.
63 pthread_mutex_t _lock;
64 /* Next channel ID available for a newly registered channel. */
65 uint32_t _next_channel_id = 0;
66 /* Once this value reaches UINT32_MAX, no more id can be allocated. */
67 uint32_t _used_channel_id = 0;
68 /* Next enumeration ID available. */
69 uint64_t _next_enum_id = 0;
70 /* Universal unique identifier used by the tracer. */
71 unsigned char _uuid[LTTNG_UUID_LEN] = {};
73 /* session ABI description */
75 /* Size of long, in bits */
76 unsigned int _bits_per_long;
77 /* Alignment, in bits */
78 unsigned int _uint8_t_alignment, _uint16_t_alignment, _uint32_t_alignment,
79 _uint64_t_alignment, _long_alignment;
80 /* endianness: BIG_ENDIAN or LITTLE_ENDIAN */
83 /* Generated metadata. */
84 char *_metadata = nullptr; /* NOT null-terminated ! Use memcpy. */
85 size_t _metadata_len = 0, _metadata_alloc_len = 0;
86 /* Length of bytes sent to the consumer. */
87 size_t _metadata_len_sent = 0;
88 /* Current version of the metadata. */
89 uint64_t _metadata_version = 0;
92 * Those fields are only used when a session is created with
93 * the --shm-path option. In this case, the metadata is output
94 * twice: once to the consumer, as ususal, but a second time
95 * also in the shm path directly. This is done so that a copy
96 * of the metadata that is as fresh as possible is available
97 * on the event of a crash.
99 * root_shm_path contains the shm-path provided by the user, along with
100 * the session's name and timestamp:
101 * e.g. /tmp/my_shm/my_session-20180612-135822
103 * shm_path contains the full path of the memory buffers:
104 * e.g. /tmp/my_shm/my_session-20180612-135822/ust/uid/1000/64-bit
106 * metadata_path contains the full path to the metadata file that
107 * is kept for the "crash buffer" extraction:
109 * /tmp/my_shm/my_session-20180612-135822/ust/uid/1000/64-bit/metadata
111 * Note that this is not the trace's final metadata file. It is
112 * only meant to be used to read the contents of the ring buffers
113 * in the event of a crash.
115 * metadata_fd is a file descriptor that points to the file at
118 char _root_shm_path[PATH_MAX] = {};
119 char _shm_path[PATH_MAX] = {};
120 char _metadata_path[PATH_MAX] = {};
121 /* File-backed metadata FD */
122 int _metadata_fd = -1;
125 * Hash table containing channels sent by the UST tracer. MUST
126 * be accessed with a RCU read side lock acquired.
128 lttng_ht::uptr _channels;
131 * Unique key to identify the metadata on the consumer side.
133 uint64_t _metadata_key = 0;
135 * Indicates if the metadata is closed on the consumer side. This is to
136 * avoid double close of metadata when an application unregisters AND
137 * deletes its sessions.
139 bool _metadata_closed = false;
141 /* User and group owning the session. */
145 /* Enumerations table. */
146 lttng_ht::uptr _enums;
149 * Copy of the tracer version when the first app is registered.
150 * It is used if we need to regenerate the metadata.
152 uint32_t _app_tracer_version_major = 0;
153 uint32_t _app_tracer_version_minor = 0;
155 /* The id of the parent session */
156 uint64_t _tracing_id = -1ULL;
159 class ust_registry_session_per_uid : public ust_registry_session {
161 ust_registry_session_per_uid(uint32_t bits_per_long,
162 uint32_t uint8_t_alignment,
163 uint32_t uint16_t_alignment,
164 uint32_t uint32_t_alignment,
165 uint32_t uint64_t_alignment,
166 uint32_t long_alignment,
170 const char *root_shm_path,
171 const char *shm_path,
177 virtual lttng_buffer_type get_buffering_scheme() const noexcept override final;
179 const uid_t _tracing_uid;
182 class ust_registry_session_per_pid : public ust_registry_session {
184 ust_registry_session_per_pid(const struct ust_app &app,
185 uint32_t bits_per_long,
186 uint32_t uint8_t_alignment,
187 uint32_t uint16_t_alignment,
188 uint32_t uint32_t_alignment,
189 uint32_t uint64_t_alignment,
190 uint32_t long_alignment,
194 const char *root_shm_path,
195 const char *shm_path,
198 uint64_t tracing_id);
200 virtual lttng_buffer_type get_buffering_scheme() const noexcept override final;
202 pid_t get_vpid() const
207 const unsigned int _tracer_patch_level_version;
209 const std::string _procname;
210 const std::time_t _app_creation_time;
213 struct ust_registry_channel {
215 uint64_t consumer_key;
216 /* Id set when replying to a register channel. */
218 enum lttng_ust_ctl_channel_header header_type;
221 * Flag for this channel if the metadata was dumped once during
222 * registration. 0 means no, 1 yes.
224 unsigned int metadata_dumped;
225 /* Indicates if this channel registry has already been registered. */
226 unsigned int register_done;
229 * Hash table containing events sent by the UST tracer. MUST be accessed
230 * with a RCU read side lock acquired.
232 struct lttng_ht *events;
233 /* Next event ID available for a newly registered event. */
234 uint32_t next_event_id;
235 /* Once this value reaches UINT32_MAX, no more id can be allocated. */
236 uint32_t used_event_id;
238 * Context fields of the registry. Context are per channel. Allocated by a
239 * register channel notification from the UST tracer.
241 size_t nr_ctx_fields;
242 struct lttng_ust_ctl_field *ctx_fields;
243 struct lttng_ht_node_u64 node;
244 /* For delayed reclaim */
245 struct rcu_head rcu_head;
249 * Event registered from a UST tracer sent to the session daemon. This is
250 * indexed and matched by <event_name/signature>.
252 struct ust_registry_event {
254 /* Both objd are set by the tracer. */
257 /* Name of the event returned by the tracer. */
258 char name[LTTNG_UST_ABI_SYM_NAME_LEN];
262 struct lttng_ust_ctl_field *fields;
265 * Flag for this channel if the metadata was dumped once during
266 * registration. 0 means no, 1 yes.
268 unsigned int metadata_dumped;
270 * Node in the ust-registry hash table. The event name is used to
271 * initialize the node and the event_name/signature for the match function.
273 struct lttng_ht_node_u64 node;
276 struct ust_registry_enum {
277 char name[LTTNG_UST_ABI_SYM_NAME_LEN];
278 struct lttng_ust_ctl_enum_entry *entries;
280 uint64_t id; /* enum id in session */
281 /* Enumeration node in session hash table. */
282 struct lttng_ht_node_str node;
283 /* For delayed reclaim. */
284 struct rcu_head rcu_head;
288 * Validate that the id has reached the maximum allowed or not.
290 * Return 0 if NOT else 1.
292 static inline int ust_registry_is_max_id(uint32_t id)
294 return (id == UINT32_MAX) ? 1 : 0;
298 * Return next available event id and increment the used counter. The
299 * ust_registry_is_max_id function MUST be called before in order to validate
300 * if the maximum number of IDs have been reached. If not, it is safe to call
303 * Return a unique channel ID. If max is reached, the used_event_id counter is
306 static inline uint32_t ust_registry_get_next_event_id(
307 struct ust_registry_channel *r)
309 if (ust_registry_is_max_id(r->used_event_id)) {
310 return r->used_event_id;
314 return r->next_event_id++;
318 * Return next available channel id and increment the used counter. The
319 * ust_registry_is_max_id function MUST be called before in order to validate
320 * if the maximum number of IDs have been reached. If not, it is safe to call
323 * Return a unique channel ID. If max is reached, the used_channel_id counter
326 static inline uint32_t ust_registry_get_next_chan_id(
327 ust_registry_session *r)
329 if (ust_registry_is_max_id(r->_used_channel_id)) {
330 return r->_used_channel_id;
333 r->_used_channel_id++;
334 return r->_next_channel_id++;
338 * Return registry event count. This is read atomically.
340 static inline uint32_t ust_registry_get_event_count(
341 struct ust_registry_channel *r)
343 return (uint32_t) uatomic_read(&r->used_event_id);
346 #ifdef HAVE_LIBLTTNG_UST_CTL
348 void ust_registry_channel_destroy(ust_registry_session *session,
349 struct ust_registry_channel *chan);
350 struct ust_registry_channel *ust_registry_channel_find(
351 ust_registry_session *session, uint64_t key);
352 int ust_registry_channel_add(ust_registry_session *session,
354 void ust_registry_channel_del_free(ust_registry_session *session,
355 uint64_t key, bool notif);
356 void ust_registry_channel_destroy(struct ust_registry_channel *chan, bool notify);
359 * Create per-uid registry with default values.
361 * Return new instance on success, nullptr on error.
363 ust_registry_session *ust_registry_session_per_uid_create(
364 uint32_t bits_per_long,
365 uint32_t uint8_t_alignment,
366 uint32_t uint16_t_alignment,
367 uint32_t uint32_t_alignment,
368 uint32_t uint64_t_alignment,
369 uint32_t long_alignment,
373 const char *root_shm_path,
374 const char *shm_path,
381 * Create per-pid registry with default values.
383 * Return new instance on success, nullptr on error.
385 ust_registry_session *ust_registry_session_per_pid_create(struct ust_app *app,
386 uint32_t bits_per_long,
387 uint32_t uint8_t_alignment,
388 uint32_t uint16_t_alignment,
389 uint32_t uint32_t_alignment,
390 uint32_t uint64_t_alignment,
391 uint32_t long_alignment,
395 const char *root_shm_path,
396 const char *shm_path,
399 uint64_t tracing_id);
400 void ust_registry_session_destroy(ust_registry_session *session);
402 int ust_registry_create_event(ust_registry_session *session,
403 uint64_t chan_key, int session_objd, int channel_objd, char *name,
404 char *sig, size_t nr_fields, struct lttng_ust_ctl_field *fields,
405 int loglevel_value, char *model_emf_uri, int buffer_type,
406 uint32_t *event_id_p, struct ust_app *app);
407 struct ust_registry_event *ust_registry_find_event(
408 struct ust_registry_channel *chan, char *name, char *sig);
409 void ust_registry_destroy_event(struct ust_registry_channel *chan,
410 struct ust_registry_event *event);
412 /* app can be NULL for registry shared across applications. */
413 int ust_metadata_session_statedump(ust_registry_session *session);
414 int ust_metadata_channel_statedump(ust_registry_session *session,
415 struct ust_registry_channel *chan);
416 int ust_metadata_event_statedump(ust_registry_session *session,
417 struct ust_registry_channel *chan,
418 struct ust_registry_event *event);
419 int ust_registry_create_or_find_enum(ust_registry_session *session,
420 int session_objd, char *name,
421 struct lttng_ust_ctl_enum_entry *entries, size_t nr_entries,
423 struct ust_registry_enum *
424 ust_registry_lookup_enum_by_id(ust_registry_session *session,
425 const char *name, uint64_t id);
426 void ust_registry_destroy_enum(ust_registry_session *reg_session,
427 struct ust_registry_enum *reg_enum);
429 #else /* HAVE_LIBLTTNG_UST_CTL */
431 static inline void ust_registry_channel_destroy(
432 struct ust_registry_channel *chan __attribute__((unused)),
433 bool notify __attribute__((unused)))
438 struct ust_registry_channel *ust_registry_channel_find(
439 ust_registry_session *session __attribute__((unused)),
440 uint64_t key __attribute__((unused)))
446 int ust_registry_channel_add(
447 ust_registry_session *session __attribute__((unused)),
448 uint64_t key __attribute__((unused)))
454 void ust_registry_channel_del_free(
455 ust_registry_session *session __attribute__((unused)),
456 uint64_t key __attribute__((unused)),
457 bool notif __attribute__((unused)))
461 ust_registry_session *ust_registry_session_per_uid_create(
462 uint32_t bits_per_long __attribute__((unused)),
463 uint32_t uint8_t_alignment __attribute__((unused)),
464 uint32_t uint16_t_alignment __attribute__((unused)),
465 uint32_t uint32_t_alignment __attribute__((unused)),
466 uint32_t uint64_t_alignment __attribute__((unused)),
467 uint32_t long_alignment __attribute__((unused)),
468 int byte_order __attribute__((unused)),
469 uint32_t major __attribute__((unused)),
470 uint32_t minor __attribute__((unused)),
471 const char *root_shm_path __attribute__((unused)),
472 const char *shm_path __attribute__((unused)),
473 uid_t euid __attribute__((unused)),
474 gid_t egid __attribute__((unused)),
475 uint64_t tracing_id __attribute__((unused)),
476 uid_t tracing_uid __attribute__((unused)))
482 ust_registry_session *ust_registry_session_per_pid_create(
483 struct ust_app *app __attribute__((unused)),
484 uint32_t bits_per_long __attribute__((unused)),
485 uint32_t uint8_t_alignment __attribute__((unused)),
486 uint32_t uint16_t_alignment __attribute__((unused)),
487 uint32_t uint32_t_alignment __attribute__((unused)),
488 uint32_t uint64_t_alignment __attribute__((unused)),
489 uint32_t long_alignment __attribute__((unused)),
490 int byte_order __attribute__((unused)),
491 uint32_t major __attribute__((unused)),
492 uint32_t minor __attribute__((unused)),
493 const char *root_shm_path __attribute__((unused)),
494 const char *shm_path __attribute__((unused)),
495 uid_t euid __attribute__((unused)),
496 gid_t egid __attribute__((unused)),
497 uint64_t tracing_id __attribute__((unused)))
503 void ust_registry_session_destroy(
504 ust_registry_session *session __attribute__((unused)))
508 int ust_registry_create_event(
509 ust_registry_session *session __attribute__((unused)),
510 uint64_t chan_key __attribute__((unused)),
511 int session_objd __attribute__((unused)),
512 int channel_objd __attribute__((unused)),
513 char *name __attribute__((unused)),
514 char *sig __attribute__((unused)),
515 size_t nr_fields __attribute__((unused)),
516 struct lttng_ust_ctl_field *fields __attribute__((unused)),
517 int loglevel_value __attribute__((unused)),
518 char *model_emf_uri __attribute__((unused)),
519 int buffer_type __attribute__((unused)),
520 uint32_t *event_id_p __attribute__((unused)))
525 struct ust_registry_event *ust_registry_find_event(
526 struct ust_registry_channel *chan __attribute__((unused)),
527 char *name __attribute__((unused)),
528 char *sig __attribute__((unused)))
534 void ust_registry_destroy_event(
535 struct ust_registry_channel *chan __attribute__((unused)),
536 struct ust_registry_event *event __attribute__((unused)))
539 /* The app object can be NULL for registry shared across applications. */
541 int ust_metadata_session_statedump(
542 ust_registry_session *session __attribute__((unused)))
548 int ust_metadata_channel_statedump(
549 ust_registry_session *session __attribute__((unused)),
550 struct ust_registry_channel *chan __attribute__((unused)))
556 int ust_metadata_event_statedump(
557 ust_registry_session *session __attribute__((unused)),
558 struct ust_registry_channel *chan __attribute__((unused)),
559 struct ust_registry_event *event __attribute__((unused)))
565 int ust_registry_create_or_find_enum(
566 ust_registry_session *session __attribute__((unused)),
567 int session_objd __attribute__((unused)),
568 char *name __attribute__((unused)),
569 struct lttng_ust_ctl_enum_entry *entries __attribute__((unused)),
570 size_t nr_entries __attribute__((unused)),
571 uint64_t *enum_id __attribute__((unused)))
577 struct ust_registry_enum *
578 ust_registry_lookup_enum_by_id(
579 ust_registry_session *session __attribute__((unused)),
580 const char *name __attribute__((unused)),
581 uint64_t id __attribute__((unused)))
587 void ust_registry_destroy_enum(ust_registry_session *reg_session __attribute__((unused)),
588 struct ust_registry_enum *reg_enum __attribute__((unused)))
591 #endif /* HAVE_LIBLTTNG_UST_CTL */
593 #endif /* LTTNG_UST_REGISTRY_H */