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