| 1 | /* |
| 2 | * Copyright (C) 2013 David Goulet <dgoulet@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef LTTNG_UST_REGISTRY_H |
| 9 | #define LTTNG_UST_REGISTRY_H |
| 10 | |
| 11 | #include <pthread.h> |
| 12 | #include <stdint.h> |
| 13 | |
| 14 | #include <common/hashtable/hashtable.h> |
| 15 | #include <common/uuid.h> |
| 16 | |
| 17 | #include "lttng-ust-ctl.h" |
| 18 | |
| 19 | #define CTF_SPEC_MAJOR 1 |
| 20 | #define CTF_SPEC_MINOR 8 |
| 21 | |
| 22 | struct ust_app; |
| 23 | |
| 24 | struct ust_registry_session { |
| 25 | /* |
| 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. |
| 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; |
| 38 | /* Next enumeration ID available. */ |
| 39 | uint64_t next_enum_id; |
| 40 | /* Universal unique identifier used by the tracer. */ |
| 41 | unsigned char uuid[LTTNG_UUID_LEN]; |
| 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; |
| 59 | /* Length of bytes sent to the consumer. */ |
| 60 | size_t metadata_len_sent; |
| 61 | /* Current version of the metadata. */ |
| 62 | uint64_t metadata_version; |
| 63 | |
| 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 | */ |
| 90 | char root_shm_path[PATH_MAX]; |
| 91 | char shm_path[PATH_MAX]; |
| 92 | char metadata_path[PATH_MAX]; |
| 93 | int metadata_fd; /* file-backed metadata FD */ |
| 94 | |
| 95 | /* |
| 96 | * Hash table containing channels sent by the UST tracer. MUST |
| 97 | * be accessed with a RCU read side lock acquired. |
| 98 | */ |
| 99 | struct lttng_ht *channels; |
| 100 | /* |
| 101 | * Unique key to identify the metadata on the consumer side. |
| 102 | */ |
| 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; |
| 110 | |
| 111 | /* User and group owning the session. */ |
| 112 | uid_t uid; |
| 113 | gid_t gid; |
| 114 | |
| 115 | /* Enumerations table. */ |
| 116 | struct lttng_ht *enums; |
| 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; |
| 124 | |
| 125 | /* The id of the parent session */ |
| 126 | uint64_t tracing_id; |
| 127 | uid_t tracing_uid; |
| 128 | }; |
| 129 | |
| 130 | struct ust_registry_channel { |
| 131 | uint64_t key; |
| 132 | uint64_t consumer_key; |
| 133 | /* Id set when replying to a register channel. */ |
| 134 | uint32_t chan_id; |
| 135 | enum lttng_ust_ctl_channel_header header_type; |
| 136 | |
| 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 | |
| 145 | /* |
| 146 | * Hash table containing events sent by the UST tracer. MUST be accessed |
| 147 | * with a RCU read side lock acquired. |
| 148 | */ |
| 149 | struct lttng_ht *ht; |
| 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; |
| 159 | struct lttng_ust_ctl_field *ctx_fields; |
| 160 | struct lttng_ht_node_u64 node; |
| 161 | /* For delayed reclaim */ |
| 162 | struct rcu_head rcu_head; |
| 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 | */ |
| 169 | struct 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. */ |
| 175 | char name[LTTNG_UST_ABI_SYM_NAME_LEN]; |
| 176 | char *signature; |
| 177 | int loglevel_value; |
| 178 | size_t nr_fields; |
| 179 | struct lttng_ust_ctl_field *fields; |
| 180 | char *model_emf_uri; |
| 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; |
| 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 | */ |
| 190 | struct lttng_ht_node_u64 node; |
| 191 | }; |
| 192 | |
| 193 | struct ust_registry_enum { |
| 194 | char name[LTTNG_UST_ABI_SYM_NAME_LEN]; |
| 195 | struct lttng_ust_ctl_enum_entry *entries; |
| 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 | |
| 204 | /* |
| 205 | * Validate that the id has reached the maximum allowed or not. |
| 206 | * |
| 207 | * Return 0 if NOT else 1. |
| 208 | */ |
| 209 | static 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 | */ |
| 223 | static 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 | */ |
| 243 | static 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 | */ |
| 257 | static 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 | |
| 263 | #ifdef HAVE_LIBLTTNG_UST_CTL |
| 264 | |
| 265 | void ust_registry_channel_destroy(struct ust_registry_session *session, |
| 266 | struct ust_registry_channel *chan); |
| 267 | struct ust_registry_channel *ust_registry_channel_find( |
| 268 | struct ust_registry_session *session, uint64_t key); |
| 269 | int ust_registry_channel_add(struct ust_registry_session *session, |
| 270 | uint64_t key); |
| 271 | void ust_registry_channel_del_free(struct ust_registry_session *session, |
| 272 | uint64_t key, bool notif); |
| 273 | |
| 274 | int ust_registry_session_init(struct ust_registry_session **sessionp, |
| 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, |
| 282 | int byte_order, |
| 283 | uint32_t major, |
| 284 | uint32_t minor, |
| 285 | const char *root_shm_path, |
| 286 | const char *shm_path, |
| 287 | uid_t euid, |
| 288 | gid_t egid, |
| 289 | uint64_t tracing_id, |
| 290 | uid_t tracing_uid); |
| 291 | void ust_registry_session_destroy(struct ust_registry_session *session); |
| 292 | |
| 293 | int ust_registry_create_event(struct ust_registry_session *session, |
| 294 | uint64_t chan_key, int session_objd, int channel_objd, char *name, |
| 295 | char *sig, size_t nr_fields, struct lttng_ust_ctl_field *fields, |
| 296 | int loglevel_value, char *model_emf_uri, int buffer_type, |
| 297 | uint32_t *event_id_p, struct ust_app *app); |
| 298 | struct ust_registry_event *ust_registry_find_event( |
| 299 | struct ust_registry_channel *chan, char *name, char *sig); |
| 300 | void 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. */ |
| 304 | int ust_metadata_session_statedump(struct ust_registry_session *session, |
| 305 | struct ust_app *app, uint32_t major, uint32_t minor); |
| 306 | int ust_metadata_channel_statedump(struct ust_registry_session *session, |
| 307 | struct ust_registry_channel *chan); |
| 308 | int ust_metadata_event_statedump(struct ust_registry_session *session, |
| 309 | struct ust_registry_channel *chan, |
| 310 | struct ust_registry_event *event); |
| 311 | int ust_registry_create_or_find_enum(struct ust_registry_session *session, |
| 312 | int session_objd, char *name, |
| 313 | struct lttng_ust_ctl_enum_entry *entries, size_t nr_entries, |
| 314 | uint64_t *enum_id); |
| 315 | struct ust_registry_enum * |
| 316 | ust_registry_lookup_enum_by_id(struct ust_registry_session *session, |
| 317 | const char *name, uint64_t id); |
| 318 | |
| 319 | #else /* HAVE_LIBLTTNG_UST_CTL */ |
| 320 | |
| 321 | static inline |
| 322 | void ust_registry_channel_destroy(struct ust_registry_session *session, |
| 323 | struct ust_registry_channel *chan) |
| 324 | {} |
| 325 | static inline |
| 326 | struct ust_registry_channel *ust_registry_channel_find( |
| 327 | struct ust_registry_session *session, uint64_t key) |
| 328 | { |
| 329 | return NULL; |
| 330 | } |
| 331 | static inline |
| 332 | int ust_registry_channel_add(struct ust_registry_session *session, |
| 333 | uint64_t key) |
| 334 | { |
| 335 | return 0; |
| 336 | } |
| 337 | static inline |
| 338 | void ust_registry_channel_del_free(struct ust_registry_session *session, |
| 339 | uint64_t key, bool notif) |
| 340 | {} |
| 341 | static inline |
| 342 | int ust_registry_session_init(struct ust_registry_session **sessionp, |
| 343 | struct ust_app *app, |
| 344 | uint32_t bits_per_long, |
| 345 | uint32_t uint8_t_alignment, |
| 346 | uint32_t uint16_t_alignment, |
| 347 | uint32_t uint32_t_alignment, |
| 348 | uint32_t uint64_t_alignment, |
| 349 | uint32_t long_alignment, |
| 350 | int byte_order, |
| 351 | uint32_t major, |
| 352 | uint32_t minor, |
| 353 | const char *root_shm_path, |
| 354 | const char *shm_path, |
| 355 | uid_t euid, |
| 356 | gid_t egid, |
| 357 | uint64_t tracing_id, |
| 358 | uid_t tracing_uid) |
| 359 | { |
| 360 | return 0; |
| 361 | } |
| 362 | static inline |
| 363 | void ust_registry_session_destroy(struct ust_registry_session *session) |
| 364 | {} |
| 365 | static inline |
| 366 | int ust_registry_create_event(struct ust_registry_session *session, |
| 367 | uint64_t chan_key, int session_objd, int channel_objd, char *name, |
| 368 | char *sig, size_t nr_fields, struct lttng_ust_ctl_field *fields, |
| 369 | int loglevel_value, char *model_emf_uri, int buffer_type, |
| 370 | uint32_t *event_id_p) |
| 371 | { |
| 372 | return 0; |
| 373 | } |
| 374 | static inline |
| 375 | struct ust_registry_event *ust_registry_find_event( |
| 376 | struct ust_registry_channel *chan, char *name, char *sig) |
| 377 | { |
| 378 | return NULL; |
| 379 | } |
| 380 | static inline |
| 381 | void ust_registry_destroy_event(struct ust_registry_channel *chan, |
| 382 | struct ust_registry_event *event) |
| 383 | {} |
| 384 | |
| 385 | /* The app object can be NULL for registry shared across applications. */ |
| 386 | static inline |
| 387 | int ust_metadata_session_statedump(struct ust_registry_session *session, |
| 388 | struct ust_app *app, uint32_t major, uint32_t minor) |
| 389 | { |
| 390 | return 0; |
| 391 | } |
| 392 | static inline |
| 393 | int ust_metadata_channel_statedump(struct ust_registry_session *session, |
| 394 | struct ust_registry_channel *chan) |
| 395 | { |
| 396 | return 0; |
| 397 | } |
| 398 | static inline |
| 399 | int ust_metadata_event_statedump(struct ust_registry_session *session, |
| 400 | struct ust_registry_channel *chan, |
| 401 | struct ust_registry_event *event) |
| 402 | { |
| 403 | return 0; |
| 404 | } |
| 405 | static inline |
| 406 | int ust_registry_create_or_find_enum(struct ust_registry_session *session, |
| 407 | int session_objd, char *name, |
| 408 | struct lttng_ust_ctl_enum_entry *entries, size_t nr_entries, |
| 409 | uint64_t *enum_id) |
| 410 | { |
| 411 | return 0; |
| 412 | } |
| 413 | static inline |
| 414 | struct ust_registry_enum * |
| 415 | ust_registry_lookup_enum_by_id(struct ust_registry_session *session, |
| 416 | const char *name, uint64_t id) |
| 417 | { |
| 418 | return NULL; |
| 419 | } |
| 420 | |
| 421 | #endif /* HAVE_LIBLTTNG_UST_CTL */ |
| 422 | |
| 423 | #endif /* LTTNG_UST_REGISTRY_H */ |