| 1 | /* |
| 2 | * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef LTTNG_UST_REGISTRY_SESSION_H |
| 9 | #define LTTNG_UST_REGISTRY_SESSION_H |
| 10 | |
| 11 | #include "clock-class.hpp" |
| 12 | #include "session.hpp" |
| 13 | #include "trace-class.hpp" |
| 14 | #include "ust-clock-class.hpp" |
| 15 | #include "ust-registry-channel.hpp" |
| 16 | #include "ust-registry.hpp" |
| 17 | |
| 18 | #include <common/make-unique-wrapper.hpp> |
| 19 | |
| 20 | #include <cstdint> |
| 21 | #include <ctime> |
| 22 | #include <lttng/lttng.h> |
| 23 | #include <string> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | namespace lttng { |
| 27 | namespace sessiond { |
| 28 | namespace ust { |
| 29 | |
| 30 | class registry_enum; |
| 31 | class registry_session; |
| 32 | |
| 33 | namespace details { |
| 34 | void locked_registry_session_release(registry_session *session); |
| 35 | } /* namespace details */ |
| 36 | |
| 37 | class registry_session : public lttng::sessiond::trace::trace_class { |
| 38 | public: |
| 39 | using locked_ptr = std::unique_ptr<registry_session, |
| 40 | lttng::details::create_unique_class<registry_session, |
| 41 | details::locked_registry_session_release>:: |
| 42 | deleter>; |
| 43 | |
| 44 | virtual lttng_buffer_type get_buffering_scheme() const noexcept = 0; |
| 45 | locked_ptr lock() noexcept; |
| 46 | |
| 47 | void add_channel(uint64_t channel_key); |
| 48 | |
| 49 | /* A channel is protected by its parent registry session's lock. */ |
| 50 | lttng::sessiond::ust::registry_channel& get_channel(uint64_t channel_key) const; |
| 51 | |
| 52 | void remove_channel(uint64_t channel_key, bool notify); |
| 53 | |
| 54 | void create_or_find_enum(int session_objd, |
| 55 | const char *enum_name, |
| 56 | struct lttng_ust_ctl_enum_entry *raw_entries, |
| 57 | size_t nr_entries, |
| 58 | uint64_t *enum_id); |
| 59 | registry_enum::const_rcu_protected_reference get_enumeration( |
| 60 | const char *enum_name, uint64_t enum_id) const; |
| 61 | |
| 62 | void regenerate_metadata(); |
| 63 | virtual ~registry_session(); |
| 64 | |
| 65 | /* |
| 66 | * With multiple writers and readers, use this lock to access |
| 67 | * the registry. Can nest within the ust app session lock. |
| 68 | * Also acts as a registry serialization lock. Used by registry |
| 69 | * readers to serialize the registry information sent from the |
| 70 | * sessiond to the consumerd. |
| 71 | * |
| 72 | * The consumer socket lock nests within this lock. |
| 73 | */ |
| 74 | mutable pthread_mutex_t _lock; |
| 75 | |
| 76 | /* Generated metadata, not null-terminated. */ |
| 77 | char *_metadata = nullptr; /* */ |
| 78 | size_t _metadata_len = 0; |
| 79 | /* Length of bytes sent to the consumer. */ |
| 80 | size_t _metadata_len_sent = 0; |
| 81 | /* Current version of the metadata. */ |
| 82 | uint64_t _metadata_version = 0; |
| 83 | |
| 84 | /* |
| 85 | * Unique key to identify the metadata on the consumer side. |
| 86 | */ |
| 87 | uint64_t _metadata_key = 0; |
| 88 | /* |
| 89 | * Indicates if the metadata is closed on the consumer side. This is to |
| 90 | * avoid double close of metadata when an application unregisters AND |
| 91 | * deletes its sessions. |
| 92 | */ |
| 93 | bool _metadata_closed = false; |
| 94 | |
| 95 | protected: |
| 96 | /* Prevent instanciation of this base class. */ |
| 97 | registry_session(const struct lttng::sessiond::trace::abi& abi, |
| 98 | unsigned int app_tracer_version_major, |
| 99 | unsigned int app_tracer_version_minor, |
| 100 | const char *root_shm_path, |
| 101 | const char *shm_path, |
| 102 | uid_t euid, |
| 103 | gid_t egid, |
| 104 | uint64_t tracing_id); |
| 105 | virtual void _visit_environment( |
| 106 | lttng::sessiond::trace::trace_class_visitor& trace_class_visitor) |
| 107 | const override; |
| 108 | void _generate_metadata(); |
| 109 | |
| 110 | private: |
| 111 | uint32_t _get_next_channel_id(); |
| 112 | void _increase_metadata_size(size_t reservation_length); |
| 113 | void _append_metadata_fragment(const std::string& fragment); |
| 114 | void _reset_metadata(); |
| 115 | void _destroy_enum(registry_enum *reg_enum) noexcept; |
| 116 | registry_enum *_lookup_enum(const registry_enum *target_enum) const; |
| 117 | |
| 118 | virtual void _accept_on_clock_classes( |
| 119 | lttng::sessiond::trace::trace_class_visitor& trace_class_visitor) |
| 120 | const override final; |
| 121 | virtual void _accept_on_stream_classes( |
| 122 | lttng::sessiond::trace::trace_class_visitor& trace_class_visitor) |
| 123 | const override final; |
| 124 | |
| 125 | /* Next channel ID available for a newly registered channel. */ |
| 126 | uint32_t _next_channel_id = 0; |
| 127 | |
| 128 | /* Once this value reaches UINT32_MAX, no more id can be allocated. */ |
| 129 | uint32_t _used_channel_id = 0; |
| 130 | |
| 131 | /* Next enumeration ID available. */ |
| 132 | uint64_t _next_enum_id = 0; |
| 133 | |
| 134 | size_t _metadata_alloc_len = 0; |
| 135 | |
| 136 | /* |
| 137 | * Those fields are only used when a session is created with |
| 138 | * the --shm-path option. In this case, the metadata is output |
| 139 | * twice: once to the consumer, as ususal, but a second time |
| 140 | * also in the shm path directly. This is done so that a copy |
| 141 | * of the metadata that is as fresh as possible is available |
| 142 | * on the event of a crash. |
| 143 | * |
| 144 | * root_shm_path contains the shm-path provided by the user, along with |
| 145 | * the session's name and timestamp: |
| 146 | * e.g. /tmp/my_shm/my_session-20180612-135822 |
| 147 | * |
| 148 | * shm_path contains the full path of the memory buffers: |
| 149 | * e.g. /tmp/my_shm/my_session-20180612-135822/ust/uid/1000/64-bit |
| 150 | * |
| 151 | * metadata_path contains the full path to the metadata file that |
| 152 | * is kept for the "crash buffer" extraction: |
| 153 | * e.g. |
| 154 | * /tmp/my_shm/my_session-20180612-135822/ust/uid/1000/64-bit/metadata |
| 155 | * |
| 156 | * Note that this is not the trace's final metadata file. It is |
| 157 | * only meant to be used to read the contents of the ring buffers |
| 158 | * in the event of a crash. |
| 159 | * |
| 160 | * metadata_fd is a file descriptor that points to the file at |
| 161 | * 'metadata_path'. |
| 162 | */ |
| 163 | const std::string _root_shm_path; |
| 164 | const std::string _shm_path; |
| 165 | const std::string _metadata_path; |
| 166 | |
| 167 | /* File-backed metadata FD */ |
| 168 | int _metadata_fd = -1; |
| 169 | |
| 170 | /* |
| 171 | * Hash table containing channels sent by the UST tracer. MUST |
| 172 | * be accessed with a RCU read side lock acquired. |
| 173 | */ |
| 174 | lttng_ht::uptr _channels; |
| 175 | |
| 176 | /* Enumerations table. */ |
| 177 | lttng_ht::uptr _enums; |
| 178 | |
| 179 | /* User and group owning the session. */ |
| 180 | const uid_t _uid; |
| 181 | const gid_t _gid; |
| 182 | |
| 183 | /* |
| 184 | * Copy of the tracer version when the first app is registered. |
| 185 | * It is used if we need to regenerate the metadata. |
| 186 | */ |
| 187 | const struct { |
| 188 | uint32_t major, minor; |
| 189 | } _app_tracer_version; |
| 190 | |
| 191 | /* The id of the parent session. */ |
| 192 | const ltt_session::id_t _tracing_id; |
| 193 | |
| 194 | lttng::sessiond::ust::clock_class::cuptr _clock; |
| 195 | const lttng::sessiond::trace::trace_class_visitor::cuptr _metadata_generating_visitor; |
| 196 | }; |
| 197 | |
| 198 | } /* namespace ust */ |
| 199 | } /* namespace sessiond */ |
| 200 | } /* namespace lttng */ |
| 201 | |
| 202 | #endif /* LTTNG_UST_REGISTRY_SESSION_H */ |