sessiond: Split ust_registry_session into per-type classes
[lttng-tools.git] / src / bin / lttng-sessiond / ust-registry.hpp
1 /*
2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 #ifndef LTTNG_UST_REGISTRY_H
10 #define LTTNG_UST_REGISTRY_H
11
12 #include <pthread.h>
13 #include <stdint.h>
14 #include <ctime>
15 #include <string>
16 #include <memory>
17
18 #include <common/hashtable/hashtable.hpp>
19 #include <common/uuid.hpp>
20
21 #include <lttng/domain.h>
22
23 #include "lttng-ust-ctl.hpp"
24
25 #define CTF_SPEC_MAJOR 1
26 #define CTF_SPEC_MINOR 8
27
28 struct ust_app;
29
30 class ust_registry_session {
31 public:
32 virtual lttng_buffer_type get_buffering_scheme() const = 0;
33 virtual ~ust_registry_session();
34
35 protected:
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,
43 int byte_order,
44 unsigned int app_tracer_version_major,
45 unsigned int app_tracer_version_minor,
46 const char *root_shm_path,
47 const char *shm_path,
48 uid_t euid,
49 gid_t egid,
50 uint64_t tracing_id);
51
52 void statedump();
53
54 public:
55 /*
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.
62 */
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] = {};
72
73 /* session ABI description */
74
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 */
81 int _byte_order;
82
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;
90
91 /*
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.
98 *
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
102 *
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
105 *
106 * metadata_path contains the full path to the metadata file that
107 * is kept for the "crash buffer" extraction:
108 * e.g.
109 * /tmp/my_shm/my_session-20180612-135822/ust/uid/1000/64-bit/metadata
110 *
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.
114 *
115 * metadata_fd is a file descriptor that points to the file at
116 * 'metadata_path'.
117 */
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;
123
124 /*
125 * Hash table containing channels sent by the UST tracer. MUST
126 * be accessed with a RCU read side lock acquired.
127 */
128 lttng_ht::uptr _channels;
129
130 /*
131 * Unique key to identify the metadata on the consumer side.
132 */
133 uint64_t _metadata_key = 0;
134 /*
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.
138 */
139 bool _metadata_closed = false;
140
141 /* User and group owning the session. */
142 uid_t _uid = -1;
143 gid_t _gid = -1;
144
145 /* Enumerations table. */
146 lttng_ht::uptr _enums;
147
148 /*
149 * Copy of the tracer version when the first app is registered.
150 * It is used if we need to regenerate the metadata.
151 */
152 uint32_t _app_tracer_version_major = 0;
153 uint32_t _app_tracer_version_minor = 0;
154
155 /* The id of the parent session */
156 uint64_t _tracing_id = -1ULL;
157 };
158
159 class ust_registry_session_per_uid : public ust_registry_session {
160 public:
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,
167 int byte_order,
168 uint32_t major,
169 uint32_t minor,
170 const char *root_shm_path,
171 const char *shm_path,
172 uid_t euid,
173 gid_t egid,
174 uint64_t tracing_id,
175 uid_t tracing_uid);
176
177 virtual lttng_buffer_type get_buffering_scheme() const noexcept override final;
178
179 const uid_t _tracing_uid;
180 };
181
182 class ust_registry_session_per_pid : public ust_registry_session {
183 public:
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,
191 int byte_order,
192 uint32_t major,
193 uint32_t minor,
194 const char *root_shm_path,
195 const char *shm_path,
196 uid_t euid,
197 gid_t egid,
198 uint64_t tracing_id);
199
200 virtual lttng_buffer_type get_buffering_scheme() const noexcept override final;
201
202 pid_t get_vpid() const
203 {
204 return _vpid;
205 }
206
207 const unsigned int _tracer_patch_level_version;
208 const pid_t _vpid;
209 const std::string _procname;
210 const std::time_t _app_creation_time;
211 };
212
213 struct ust_registry_channel {
214 uint64_t key;
215 uint64_t consumer_key;
216 /* Id set when replying to a register channel. */
217 uint32_t chan_id;
218 enum lttng_ust_ctl_channel_header header_type;
219
220 /*
221 * Flag for this channel if the metadata was dumped once during
222 * registration. 0 means no, 1 yes.
223 */
224 unsigned int metadata_dumped;
225 /* Indicates if this channel registry has already been registered. */
226 unsigned int register_done;
227
228 /*
229 * Hash table containing events sent by the UST tracer. MUST be accessed
230 * with a RCU read side lock acquired.
231 */
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;
237 /*
238 * Context fields of the registry. Context are per channel. Allocated by a
239 * register channel notification from the UST tracer.
240 */
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;
246 };
247
248 /*
249 * Event registered from a UST tracer sent to the session daemon. This is
250 * indexed and matched by <event_name/signature>.
251 */
252 struct ust_registry_event {
253 int id;
254 /* Both objd are set by the tracer. */
255 int session_objd;
256 int channel_objd;
257 /* Name of the event returned by the tracer. */
258 char name[LTTNG_UST_ABI_SYM_NAME_LEN];
259 char *signature;
260 int loglevel_value;
261 size_t nr_fields;
262 struct lttng_ust_ctl_field *fields;
263 char *model_emf_uri;
264 /*
265 * Flag for this channel if the metadata was dumped once during
266 * registration. 0 means no, 1 yes.
267 */
268 unsigned int metadata_dumped;
269 /*
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.
272 */
273 struct lttng_ht_node_u64 node;
274 };
275
276 struct ust_registry_enum {
277 char name[LTTNG_UST_ABI_SYM_NAME_LEN];
278 struct lttng_ust_ctl_enum_entry *entries;
279 size_t nr_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;
285 };
286
287 /*
288 * Validate that the id has reached the maximum allowed or not.
289 *
290 * Return 0 if NOT else 1.
291 */
292 static inline int ust_registry_is_max_id(uint32_t id)
293 {
294 return (id == UINT32_MAX) ? 1 : 0;
295 }
296
297 /*
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
301 * this function.
302 *
303 * Return a unique channel ID. If max is reached, the used_event_id counter is
304 * returned.
305 */
306 static inline uint32_t ust_registry_get_next_event_id(
307 struct ust_registry_channel *r)
308 {
309 if (ust_registry_is_max_id(r->used_event_id)) {
310 return r->used_event_id;
311 }
312
313 r->used_event_id++;
314 return r->next_event_id++;
315 }
316
317 /*
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
321 * this function.
322 *
323 * Return a unique channel ID. If max is reached, the used_channel_id counter
324 * is returned.
325 */
326 static inline uint32_t ust_registry_get_next_chan_id(
327 ust_registry_session *r)
328 {
329 if (ust_registry_is_max_id(r->_used_channel_id)) {
330 return r->_used_channel_id;
331 }
332
333 r->_used_channel_id++;
334 return r->_next_channel_id++;
335 }
336
337 /*
338 * Return registry event count. This is read atomically.
339 */
340 static inline uint32_t ust_registry_get_event_count(
341 struct ust_registry_channel *r)
342 {
343 return (uint32_t) uatomic_read(&r->used_event_id);
344 }
345
346 #ifdef HAVE_LIBLTTNG_UST_CTL
347
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,
353 uint64_t key);
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);
357
358 /*
359 * Create per-uid registry with default values.
360 *
361 * Return new instance on success, nullptr on error.
362 */
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,
370 int byte_order,
371 uint32_t major,
372 uint32_t minor,
373 const char *root_shm_path,
374 const char *shm_path,
375 uid_t euid,
376 gid_t egid,
377 uint64_t tracing_id,
378 uid_t tracing_uid);
379
380 /*
381 * Create per-pid registry with default values.
382 *
383 * Return new instance on success, nullptr on error.
384 */
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,
392 int byte_order,
393 uint32_t major,
394 uint32_t minor,
395 const char *root_shm_path,
396 const char *shm_path,
397 uid_t euid,
398 gid_t egid,
399 uint64_t tracing_id);
400 void ust_registry_session_destroy(ust_registry_session *session);
401
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);
411
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,
422 uint64_t *enum_id);
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);
428
429 #else /* HAVE_LIBLTTNG_UST_CTL */
430
431 static inline void ust_registry_channel_destroy(
432 struct ust_registry_channel *chan __attribute__((unused)),
433 bool notify __attribute__((unused)))
434 {
435 }
436
437 static inline
438 struct ust_registry_channel *ust_registry_channel_find(
439 ust_registry_session *session __attribute__((unused)),
440 uint64_t key __attribute__((unused)))
441 {
442 return NULL;
443 }
444
445 static inline
446 int ust_registry_channel_add(
447 ust_registry_session *session __attribute__((unused)),
448 uint64_t key __attribute__((unused)))
449 {
450 return 0;
451 }
452
453 static inline
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)))
458 {}
459
460 static inline
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)))
477 {
478 return nullptr;
479 }
480
481 static inline
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)))
498 {
499 return nullptr;
500 }
501
502 static inline
503 void ust_registry_session_destroy(
504 ust_registry_session *session __attribute__((unused)))
505 {}
506
507 static inline
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)))
521 {
522 return 0;
523 }
524 static inline
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)))
529 {
530 return NULL;
531 }
532
533 static inline
534 void ust_registry_destroy_event(
535 struct ust_registry_channel *chan __attribute__((unused)),
536 struct ust_registry_event *event __attribute__((unused)))
537 {}
538
539 /* The app object can be NULL for registry shared across applications. */
540 static inline
541 int ust_metadata_session_statedump(
542 ust_registry_session *session __attribute__((unused)))
543 {
544 return 0;
545 }
546
547 static inline
548 int ust_metadata_channel_statedump(
549 ust_registry_session *session __attribute__((unused)),
550 struct ust_registry_channel *chan __attribute__((unused)))
551 {
552 return 0;
553 }
554
555 static inline
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)))
560 {
561 return 0;
562 }
563
564 static inline
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)))
572 {
573 return 0;
574 }
575
576 static inline
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)))
582 {
583 return NULL;
584 }
585
586 static inline
587 void ust_registry_destroy_enum(ust_registry_session *reg_session __attribute__((unused)),
588 struct ust_registry_enum *reg_enum __attribute__((unused)))
589 {}
590
591 #endif /* HAVE_LIBLTTNG_UST_CTL */
592
593 #endif /* LTTNG_UST_REGISTRY_H */
This page took 0.042233 seconds and 5 git commands to generate.