Fix: lttng: enable-channel: leak of popt arguments
[lttng-tools.git] / src / bin / lttng-sessiond / ust-registry.hpp
CommitLineData
d0b96690 1/*
ab5be9fa 2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
d0b96690 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
d0b96690 5 *
d0b96690
DG
6 */
7
8#ifndef LTTNG_UST_REGISTRY_H
9#define LTTNG_UST_REGISTRY_H
10
11#include <pthread.h>
12#include <stdint.h>
d0b96690 13
c9e313bc
SM
14#include <common/hashtable/hashtable.hpp>
15#include <common/uuid.hpp>
d0b96690 16
c9e313bc 17#include "lttng-ust-ctl.hpp"
7972aab2 18
d0b96690
DG
19#define CTF_SPEC_MAJOR 1
20#define CTF_SPEC_MINOR 8
21
22struct ust_app;
23
24struct ust_registry_session {
25 /*
dc2bbdae
MD
26 * With multiple writers and readers, use this lock to access
27 * the registry. Can nest within the ust app session lock.
28 * Also acts as a registry serialization lock. Used by registry
29 * readers to serialize the registry information sent from the
30 * sessiond to the consumerd.
31 * The consumer socket lock nests within this lock.
d0b96690
DG
32 */
33 pthread_mutex_t lock;
34 /* Next channel ID available for a newly registered channel. */
35 uint32_t next_channel_id;
36 /* Once this value reaches UINT32_MAX, no more id can be allocated. */
37 uint32_t used_channel_id;
10b56aef
MD
38 /* Next enumeration ID available. */
39 uint64_t next_enum_id;
d0b96690 40 /* Universal unique identifier used by the tracer. */
c70636a7 41 unsigned char uuid[LTTNG_UUID_LEN];
d0b96690
DG
42
43 /* session ABI description */
44
45 /* Size of long, in bits */
46 unsigned int bits_per_long;
47 /* Alignment, in bits */
48 unsigned int uint8_t_alignment,
49 uint16_t_alignment,
50 uint32_t_alignment,
51 uint64_t_alignment,
52 long_alignment;
53 /* endianness */
54 int byte_order; /* BIG_ENDIAN or LITTLE_ENDIAN */
55
56 /* Generated metadata. */
57 char *metadata; /* NOT null-terminated ! Use memcpy. */
58 size_t metadata_len, metadata_alloc_len;
d88aee68
DG
59 /* Length of bytes sent to the consumer. */
60 size_t metadata_len_sent;
93ec662e
JD
61 /* Current version of the metadata. */
62 uint64_t metadata_version;
d7ba1388 63
9aed4377
JG
64 /*
65 * Those fields are only used when a session is created with
66 * the --shm-path option. In this case, the metadata is output
67 * twice: once to the consumer, as ususal, but a second time
68 * also in the shm path directly. This is done so that a copy
69 * of the metadata that is as fresh as possible is available
70 * on the event of a crash.
71 *
72 * root_shm_path contains the shm-path provided by the user, along with
73 * the session's name and timestamp:
74 * e.g. /tmp/my_shm/my_session-20180612-135822
75 *
76 * shm_path contains the full path of the memory buffers:
77 * e.g. /tmp/my_shm/my_session-20180612-135822/ust/uid/1000/64-bit
78 *
79 * metadata_path contains the full path to the metadata file that
80 * is kept for the "crash buffer" extraction:
81 * e.g. /tmp/my_shm/my_session-20180612-135822/ust/uid/1000/64-bit/metadata
82 *
83 * Note that this is not the trace's final metadata file. It is
84 * only meant to be used to read the contents of the ring buffers
85 * in the event of a crash.
86 *
87 * metadata_fd is a file descriptor that points to the file at
88 * 'metadata_path'.
89 */
3d071855 90 char root_shm_path[PATH_MAX];
d7ba1388
MD
91 char shm_path[PATH_MAX];
92 char metadata_path[PATH_MAX];
93 int metadata_fd; /* file-backed metadata FD */
94
45893984 95 /*
dc2bbdae
MD
96 * Hash table containing channels sent by the UST tracer. MUST
97 * be accessed with a RCU read side lock acquired.
45893984
DG
98 */
99 struct lttng_ht *channels;
dc2bbdae
MD
100 /*
101 * Unique key to identify the metadata on the consumer side.
102 */
7972aab2
DG
103 uint64_t metadata_key;
104 /*
105 * Indicates if the metadata is closed on the consumer side. This is to
106 * avoid double close of metadata when an application unregisters AND
107 * deletes its sessions.
108 */
109 unsigned int metadata_closed;
4628484a
MD
110
111 /* User and group owning the session. */
112 uid_t uid;
113 gid_t gid;
10b56aef
MD
114
115 /* Enumerations table. */
116 struct lttng_ht *enums;
7062f070
JD
117
118 /*
119 * Copy of the tracer version when the first app is registered.
120 * It is used if we need to regenerate the metadata.
121 */
122 uint32_t major;
123 uint32_t minor;
8de88061
JR
124
125 /* The id of the parent session */
126 uint64_t tracing_id;
127 uid_t tracing_uid;
d0b96690
DG
128};
129
130struct ust_registry_channel {
45893984 131 uint64_t key;
e9404c27 132 uint64_t consumer_key;
d0b96690
DG
133 /* Id set when replying to a register channel. */
134 uint32_t chan_id;
b623cb6a 135 enum lttng_ust_ctl_channel_header header_type;
d0b96690 136
7972aab2
DG
137 /*
138 * Flag for this channel if the metadata was dumped once during
139 * registration. 0 means no, 1 yes.
140 */
141 unsigned int metadata_dumped;
142 /* Indicates if this channel registry has already been registered. */
143 unsigned int register_done;
144
d0b96690
DG
145 /*
146 * Hash table containing events sent by the UST tracer. MUST be accessed
147 * with a RCU read side lock acquired.
148 */
0d19be9d 149 struct lttng_ht *events;
d0b96690
DG
150 /* Next event ID available for a newly registered event. */
151 uint32_t next_event_id;
152 /* Once this value reaches UINT32_MAX, no more id can be allocated. */
153 uint32_t used_event_id;
154 /*
155 * Context fields of the registry. Context are per channel. Allocated by a
156 * register channel notification from the UST tracer.
157 */
158 size_t nr_ctx_fields;
b623cb6a 159 struct lttng_ust_ctl_field *ctx_fields;
45893984 160 struct lttng_ht_node_u64 node;
36b588ed
MD
161 /* For delayed reclaim */
162 struct rcu_head rcu_head;
d0b96690
DG
163};
164
165/*
166 * Event registered from a UST tracer sent to the session daemon. This is
167 * indexed and matched by <event_name/signature>.
168 */
169struct ust_registry_event {
170 int id;
171 /* Both objd are set by the tracer. */
172 int session_objd;
173 int channel_objd;
174 /* Name of the event returned by the tracer. */
fc4b93fa 175 char name[LTTNG_UST_ABI_SYM_NAME_LEN];
d0b96690 176 char *signature;
2106efa0 177 int loglevel_value;
d0b96690 178 size_t nr_fields;
b623cb6a 179 struct lttng_ust_ctl_field *fields;
d0b96690 180 char *model_emf_uri;
7972aab2
DG
181 /*
182 * Flag for this channel if the metadata was dumped once during
183 * registration. 0 means no, 1 yes.
184 */
185 unsigned int metadata_dumped;
d0b96690
DG
186 /*
187 * Node in the ust-registry hash table. The event name is used to
188 * initialize the node and the event_name/signature for the match function.
189 */
7972aab2 190 struct lttng_ht_node_u64 node;
d0b96690
DG
191};
192
10b56aef 193struct ust_registry_enum {
fc4b93fa 194 char name[LTTNG_UST_ABI_SYM_NAME_LEN];
b623cb6a 195 struct lttng_ust_ctl_enum_entry *entries;
10b56aef
MD
196 size_t nr_entries;
197 uint64_t id; /* enum id in session */
198 /* Enumeration node in session hash table. */
199 struct lttng_ht_node_str node;
200 /* For delayed reclaim. */
201 struct rcu_head rcu_head;
202};
203
d0b96690
DG
204/*
205 * Validate that the id has reached the maximum allowed or not.
206 *
207 * Return 0 if NOT else 1.
208 */
209static inline int ust_registry_is_max_id(uint32_t id)
210{
211 return (id == UINT32_MAX) ? 1 : 0;
212}
213
214/*
215 * Return next available event id and increment the used counter. The
216 * ust_registry_is_max_id function MUST be called before in order to validate
217 * if the maximum number of IDs have been reached. If not, it is safe to call
218 * this function.
219 *
220 * Return a unique channel ID. If max is reached, the used_event_id counter is
221 * returned.
222 */
223static inline uint32_t ust_registry_get_next_event_id(
224 struct ust_registry_channel *r)
225{
226 if (ust_registry_is_max_id(r->used_event_id)) {
227 return r->used_event_id;
228 }
229
230 r->used_event_id++;
231 return r->next_event_id++;
232}
233
234/*
235 * Return next available channel id and increment the used counter. The
236 * ust_registry_is_max_id function MUST be called before in order to validate
237 * if the maximum number of IDs have been reached. If not, it is safe to call
238 * this function.
239 *
240 * Return a unique channel ID. If max is reached, the used_channel_id counter
241 * is returned.
242 */
243static inline uint32_t ust_registry_get_next_chan_id(
244 struct ust_registry_session *r)
245{
246 if (ust_registry_is_max_id(r->used_channel_id)) {
247 return r->used_channel_id;
248 }
249
250 r->used_channel_id++;
251 return r->next_channel_id++;
252}
253
254/*
255 * Return registry event count. This is read atomically.
256 */
257static inline uint32_t ust_registry_get_event_count(
258 struct ust_registry_channel *r)
259{
260 return (uint32_t) uatomic_read(&r->used_event_id);
261}
262
7972aab2
DG
263#ifdef HAVE_LIBLTTNG_UST_CTL
264
d0b96690
DG
265void ust_registry_channel_destroy(struct ust_registry_session *session,
266 struct ust_registry_channel *chan);
45893984
DG
267struct ust_registry_channel *ust_registry_channel_find(
268 struct ust_registry_session *session, uint64_t key);
269int ust_registry_channel_add(struct ust_registry_session *session,
270 uint64_t key);
271void ust_registry_channel_del_free(struct ust_registry_session *session,
e9404c27 272 uint64_t key, bool notif);
45893984
DG
273
274int ust_registry_session_init(struct ust_registry_session **sessionp,
d0b96690
DG
275 struct ust_app *app,
276 uint32_t bits_per_long,
277 uint32_t uint8_t_alignment,
278 uint32_t uint16_t_alignment,
279 uint32_t uint32_t_alignment,
280 uint32_t uint64_t_alignment,
281 uint32_t long_alignment,
af6142cf
MD
282 int byte_order,
283 uint32_t major,
d7ba1388 284 uint32_t minor,
3d071855 285 const char *root_shm_path,
d7ba1388
MD
286 const char *shm_path,
287 uid_t euid,
8de88061
JR
288 gid_t egid,
289 uint64_t tracing_id,
290 uid_t tracing_uid);
d0b96690
DG
291void ust_registry_session_destroy(struct ust_registry_session *session);
292
293int ust_registry_create_event(struct ust_registry_session *session,
45893984 294 uint64_t chan_key, int session_objd, int channel_objd, char *name,
b623cb6a 295 char *sig, size_t nr_fields, struct lttng_ust_ctl_field *fields,
2106efa0
PP
296 int loglevel_value, char *model_emf_uri, int buffer_type,
297 uint32_t *event_id_p, struct ust_app *app);
d0b96690
DG
298struct ust_registry_event *ust_registry_find_event(
299 struct ust_registry_channel *chan, char *name, char *sig);
300void ust_registry_destroy_event(struct ust_registry_channel *chan,
301 struct ust_registry_event *event);
302
303/* app can be NULL for registry shared across applications. */
304int ust_metadata_session_statedump(struct ust_registry_session *session,
af6142cf 305 struct ust_app *app, uint32_t major, uint32_t minor);
d0b96690
DG
306int ust_metadata_channel_statedump(struct ust_registry_session *session,
307 struct ust_registry_channel *chan);
308int ust_metadata_event_statedump(struct ust_registry_session *session,
309 struct ust_registry_channel *chan,
310 struct ust_registry_event *event);
10b56aef
MD
311int ust_registry_create_or_find_enum(struct ust_registry_session *session,
312 int session_objd, char *name,
b623cb6a 313 struct lttng_ust_ctl_enum_entry *entries, size_t nr_entries,
10b56aef
MD
314 uint64_t *enum_id);
315struct ust_registry_enum *
316 ust_registry_lookup_enum_by_id(struct ust_registry_session *session,
317 const char *name, uint64_t id);
d0b96690 318
7972aab2
DG
319#else /* HAVE_LIBLTTNG_UST_CTL */
320
321static inline
f46376a1
MJ
322void ust_registry_channel_destroy(
323 struct ust_registry_session *session __attribute__((unused)),
324 struct ust_registry_channel *chan __attribute__((unused)))
7972aab2 325{}
f46376a1 326
7972aab2
DG
327static inline
328struct ust_registry_channel *ust_registry_channel_find(
f46376a1
MJ
329 struct ust_registry_session *session __attribute__((unused)),
330 uint64_t key __attribute__((unused)))
7972aab2
DG
331{
332 return NULL;
333}
f46376a1 334
7972aab2 335static inline
f46376a1
MJ
336int ust_registry_channel_add(
337 struct ust_registry_session *session __attribute__((unused)),
338 uint64_t key __attribute__((unused)))
7972aab2
DG
339{
340 return 0;
341}
f46376a1 342
7972aab2 343static inline
f46376a1
MJ
344void ust_registry_channel_del_free(
345 struct ust_registry_session *session __attribute__((unused)),
346 uint64_t key __attribute__((unused)),
347 bool notif __attribute__((unused)))
7972aab2 348{}
f46376a1 349
7972aab2 350static inline
f46376a1
MJ
351int ust_registry_session_init(
352 struct ust_registry_session **sessionp __attribute__((unused)),
353 struct ust_app *app __attribute__((unused)),
354 uint32_t bits_per_long __attribute__((unused)),
355 uint32_t uint8_t_alignment __attribute__((unused)),
356 uint32_t uint16_t_alignment __attribute__((unused)),
357 uint32_t uint32_t_alignment __attribute__((unused)),
358 uint32_t uint64_t_alignment __attribute__((unused)),
359 uint32_t long_alignment __attribute__((unused)),
360 int byte_order __attribute__((unused)),
361 uint32_t major __attribute__((unused)),
362 uint32_t minor __attribute__((unused)),
363 const char *root_shm_path __attribute__((unused)),
364 const char *shm_path __attribute__((unused)),
365 uid_t euid __attribute__((unused)),
366 gid_t egid __attribute__((unused)),
367 uint64_t tracing_id __attribute__((unused)),
368 uid_t tracing_uid __attribute__((unused)))
7972aab2
DG
369{
370 return 0;
371}
f46376a1 372
7972aab2 373static inline
f46376a1
MJ
374void ust_registry_session_destroy(
375 struct ust_registry_session *session __attribute__((unused)))
7972aab2 376{}
f46376a1 377
7972aab2 378static inline
f46376a1
MJ
379int ust_registry_create_event(
380 struct ust_registry_session *session __attribute__((unused)),
381 uint64_t chan_key __attribute__((unused)),
382 int session_objd __attribute__((unused)),
383 int channel_objd __attribute__((unused)),
384 char *name __attribute__((unused)),
385 char *sig __attribute__((unused)),
386 size_t nr_fields __attribute__((unused)),
387 struct lttng_ust_ctl_field *fields __attribute__((unused)),
388 int loglevel_value __attribute__((unused)),
389 char *model_emf_uri __attribute__((unused)),
390 int buffer_type __attribute__((unused)),
391 uint32_t *event_id_p __attribute__((unused)))
7972aab2
DG
392{
393 return 0;
394}
395static inline
396struct ust_registry_event *ust_registry_find_event(
f46376a1
MJ
397 struct ust_registry_channel *chan __attribute__((unused)),
398 char *name __attribute__((unused)),
399 char *sig __attribute__((unused)))
7972aab2
DG
400{
401 return NULL;
402}
f46376a1 403
7972aab2 404static inline
f46376a1
MJ
405void ust_registry_destroy_event(
406 struct ust_registry_channel *chan __attribute__((unused)),
407 struct ust_registry_event *event __attribute__((unused)))
7972aab2
DG
408{}
409
410/* The app object can be NULL for registry shared across applications. */
411static inline
f46376a1
MJ
412int ust_metadata_session_statedump(
413 struct ust_registry_session *session __attribute__((unused)),
414 struct ust_app *app __attribute__((unused)),
415 uint32_t major __attribute__((unused)),
416 uint32_t minor __attribute__((unused)))
7972aab2
DG
417{
418 return 0;
419}
f46376a1 420
7972aab2 421static inline
f46376a1
MJ
422int ust_metadata_channel_statedump(
423 struct ust_registry_session *session __attribute__((unused)),
424 struct ust_registry_channel *chan __attribute__((unused)))
7972aab2
DG
425{
426 return 0;
427}
f46376a1 428
7972aab2 429static inline
f46376a1
MJ
430int ust_metadata_event_statedump(
431 struct ust_registry_session *session __attribute__((unused)),
432 struct ust_registry_channel *chan __attribute__((unused)),
433 struct ust_registry_event *event __attribute__((unused)))
7972aab2
DG
434{
435 return 0;
436}
f46376a1 437
10b56aef 438static inline
f46376a1
MJ
439int ust_registry_create_or_find_enum(
440 struct ust_registry_session *session __attribute__((unused)),
441 int session_objd __attribute__((unused)),
442 char *name __attribute__((unused)),
443 struct lttng_ust_ctl_enum_entry *entries __attribute__((unused)),
444 size_t nr_entries __attribute__((unused)),
445 uint64_t *enum_id __attribute__((unused)))
10b56aef
MD
446{
447 return 0;
448}
f46376a1 449
10b56aef
MD
450static inline
451struct ust_registry_enum *
f46376a1
MJ
452 ust_registry_lookup_enum_by_id(
453 struct ust_registry_session *session __attribute__((unused)),
454 const char *name __attribute__((unused)),
455 uint64_t id __attribute__((unused)))
10b56aef
MD
456{
457 return NULL;
458}
7972aab2
DG
459
460#endif /* HAVE_LIBLTTNG_UST_CTL */
461
d0b96690 462#endif /* LTTNG_UST_REGISTRY_H */
This page took 0.090632 seconds and 4 git commands to generate.