| 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 "event-class.hpp" |
| 13 | #include "field.hpp" |
| 14 | #include "lttng-ust-ctl.hpp" |
| 15 | #include "session.hpp" |
| 16 | #include "stream-class.hpp" |
| 17 | #include "trace-class.hpp" |
| 18 | #include "ust-clock-class.hpp" |
| 19 | #include "ust-registry-channel.hpp" |
| 20 | #include "ust-registry-event.hpp" |
| 21 | #include "ust-registry-session.hpp" |
| 22 | |
| 23 | #include <common/format.hpp> |
| 24 | #include <common/hashtable/hashtable.hpp> |
| 25 | #include <common/locked-reference.hpp> |
| 26 | #include <common/urcu.hpp> |
| 27 | #include <common/uuid.hpp> |
| 28 | |
| 29 | #include <lttng/domain.h> |
| 30 | |
| 31 | #include <ctime> |
| 32 | #include <memory> |
| 33 | #include <pthread.h> |
| 34 | #include <stdint.h> |
| 35 | #include <string> |
| 36 | #include <type_traits> |
| 37 | |
| 38 | #define CTF_SPEC_MAJOR 1 |
| 39 | #define CTF_SPEC_MINOR 8 |
| 40 | |
| 41 | struct ust_app; |
| 42 | |
| 43 | namespace lttng { |
| 44 | namespace sessiond { |
| 45 | namespace ust { |
| 46 | namespace details { |
| 47 | |
| 48 | template <class MappingIntegerType> |
| 49 | typename trace::typed_enumeration_type<MappingIntegerType>::mapping mapping_from_ust_ctl_entry( |
| 50 | const lttng_ust_ctl_enum_entry& entry) |
| 51 | { |
| 52 | if (entry.u.extra.options & LTTNG_UST_CTL_UST_ENUM_ENTRY_OPTION_IS_AUTO) { |
| 53 | return {entry.string}; |
| 54 | |
| 55 | } else { |
| 56 | return {entry.string, |
| 57 | {(MappingIntegerType) entry.start.value, |
| 58 | (MappingIntegerType) entry.end.value}}; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | template <class MappingIntegerType> |
| 63 | typename trace::typed_enumeration_type<MappingIntegerType>::mappings mappings_from_ust_ctl_entries( |
| 64 | const lttng_ust_ctl_enum_entry *in_entries, size_t in_entry_count) |
| 65 | { |
| 66 | typename trace::typed_enumeration_type<MappingIntegerType>::mappings mappings; |
| 67 | |
| 68 | for (size_t entry_idx = 0; entry_idx < in_entry_count; entry_idx++) { |
| 69 | const auto& entry = in_entries[entry_idx]; |
| 70 | |
| 71 | mappings.emplace_back(mapping_from_ust_ctl_entry<MappingIntegerType>(entry)); |
| 72 | } |
| 73 | |
| 74 | return mappings; |
| 75 | } |
| 76 | } /* namespace details */ |
| 77 | |
| 78 | class registry_enum { |
| 79 | public: |
| 80 | using const_rcu_protected_reference = lttng::locked_reference<const registry_enum, lttng::urcu::unique_read_lock>; |
| 81 | |
| 82 | registry_enum(std::string name, enum lttng::sessiond::trace::integer_type::signedness signedness); |
| 83 | virtual ~registry_enum() = default; |
| 84 | |
| 85 | std::string name; |
| 86 | enum lttng::sessiond::trace::integer_type::signedness signedness; |
| 87 | /* enum id in session */ |
| 88 | uint64_t id = -1ULL; |
| 89 | /* Enumeration node in session hash table. */ |
| 90 | struct lttng_ht_node_str node; |
| 91 | /* For delayed reclaim. */ |
| 92 | struct rcu_head rcu_head; |
| 93 | |
| 94 | friend bool operator==(const registry_enum& lhs, const registry_enum& rhs) noexcept; |
| 95 | protected: |
| 96 | virtual bool _is_equal(const registry_enum& other) const noexcept = 0; |
| 97 | }; |
| 98 | |
| 99 | bool operator==(const registry_enum& lhs, const registry_enum& rhs) noexcept; |
| 100 | |
| 101 | template <class MappingIntegerType> |
| 102 | class registry_typed_enum : public registry_enum { |
| 103 | public: |
| 104 | registry_typed_enum(const char *in_name, |
| 105 | const lttng_ust_ctl_enum_entry *entries, |
| 106 | size_t entry_count) : |
| 107 | registry_enum(in_name, |
| 108 | std::is_signed<MappingIntegerType>::value ? |
| 109 | lttng::sessiond::trace::integer_type::signedness::SIGNED : |
| 110 | lttng::sessiond::trace::integer_type::signedness::UNSIGNED), |
| 111 | _mappings{std::make_shared< |
| 112 | typename trace::typed_enumeration_type<MappingIntegerType>::mappings>( |
| 113 | details::mappings_from_ust_ctl_entries<MappingIntegerType>( |
| 114 | entries, entry_count))} |
| 115 | { |
| 116 | } |
| 117 | |
| 118 | const typename std::shared_ptr<const typename lttng::sessiond::trace::typed_enumeration_type< |
| 119 | MappingIntegerType>::mappings> |
| 120 | _mappings; |
| 121 | |
| 122 | protected: |
| 123 | virtual bool _is_equal(const registry_enum& base_other) const noexcept |
| 124 | { |
| 125 | const auto &other = static_cast<decltype(*this)&>(base_other); |
| 126 | |
| 127 | /* Don't compare IDs as some comparisons are performed before an id is assigned. */ |
| 128 | return this->name == other.name && *this->_mappings == *other._mappings; |
| 129 | } |
| 130 | }; |
| 131 | |
| 132 | using registry_signed_enum = registry_typed_enum<int64_t>; |
| 133 | using registry_unsigned_enum = registry_typed_enum<uint64_t>; |
| 134 | |
| 135 | } /* namespace ust */ |
| 136 | } /* namespace sessiond */ |
| 137 | } /* namespace lttng */ |
| 138 | |
| 139 | #ifdef HAVE_LIBLTTNG_UST_CTL |
| 140 | |
| 141 | /* |
| 142 | * Create per-uid registry with default values. |
| 143 | * |
| 144 | * Return new instance on success, nullptr on error. |
| 145 | */ |
| 146 | lttng::sessiond::ust::registry_session *ust_registry_session_per_uid_create( |
| 147 | const lttng::sessiond::trace::abi& abi, |
| 148 | uint32_t major, |
| 149 | uint32_t minor, |
| 150 | const char *root_shm_path, |
| 151 | const char *shm_path, |
| 152 | uid_t euid, |
| 153 | gid_t egid, |
| 154 | uint64_t tracing_id, |
| 155 | uid_t tracing_uid); |
| 156 | |
| 157 | /* |
| 158 | * Create per-pid registry with default values. |
| 159 | * |
| 160 | * Return new instance on success, nullptr on error. |
| 161 | */ |
| 162 | lttng::sessiond::ust::registry_session *ust_registry_session_per_pid_create(struct ust_app *app, |
| 163 | const lttng::sessiond::trace::abi& abi, |
| 164 | uint32_t major, |
| 165 | uint32_t minor, |
| 166 | const char *root_shm_path, |
| 167 | const char *shm_path, |
| 168 | uid_t euid, |
| 169 | gid_t egid, |
| 170 | uint64_t tracing_id); |
| 171 | void ust_registry_session_destroy(lttng::sessiond::ust::registry_session *session); |
| 172 | |
| 173 | void ust_registry_channel_destroy_event(lttng::sessiond::ust::registry_channel *chan, |
| 174 | lttng::sessiond::ust::registry_event *event); |
| 175 | |
| 176 | int ust_registry_create_or_find_enum(lttng::sessiond::ust::registry_session *session, |
| 177 | int session_objd, char *name, |
| 178 | struct lttng_ust_ctl_enum_entry *entries, size_t nr_entries, |
| 179 | uint64_t *enum_id); |
| 180 | lttng::sessiond::ust::registry_enum::const_rcu_protected_reference |
| 181 | ust_registry_lookup_enum_by_id(const lttng::sessiond::ust::registry_session *session, |
| 182 | const char *name, uint64_t id); |
| 183 | void ust_registry_destroy_enum(lttng::sessiond::ust::registry_session *reg_session, |
| 184 | lttng::sessiond::ust::registry_enum *reg_enum); |
| 185 | #else /* HAVE_LIBLTTNG_UST_CTL */ |
| 186 | |
| 187 | static inline |
| 188 | lttng::sessiond::ust::registry_session *ust_registry_session_per_uid_create( |
| 189 | uint32_t bits_per_long __attribute__((unused)), |
| 190 | uint32_t uint8_t_alignment __attribute__((unused)), |
| 191 | uint32_t uint16_t_alignment __attribute__((unused)), |
| 192 | uint32_t uint32_t_alignment __attribute__((unused)), |
| 193 | uint32_t uint64_t_alignment __attribute__((unused)), |
| 194 | uint32_t long_alignment __attribute__((unused)), |
| 195 | int byte_order __attribute__((unused)), |
| 196 | uint32_t major __attribute__((unused)), |
| 197 | uint32_t minor __attribute__((unused)), |
| 198 | const char *root_shm_path __attribute__((unused)), |
| 199 | const char *shm_path __attribute__((unused)), |
| 200 | uid_t euid __attribute__((unused)), |
| 201 | gid_t egid __attribute__((unused)), |
| 202 | uint64_t tracing_id __attribute__((unused)), |
| 203 | uid_t tracing_uid __attribute__((unused))) |
| 204 | { |
| 205 | return nullptr; |
| 206 | } |
| 207 | |
| 208 | static inline |
| 209 | lttng::sessiond::ust::registry_session *ust_registry_session_per_pid_create( |
| 210 | struct ust_app *app __attribute__((unused)), |
| 211 | uint32_t bits_per_long __attribute__((unused)), |
| 212 | uint32_t uint8_t_alignment __attribute__((unused)), |
| 213 | uint32_t uint16_t_alignment __attribute__((unused)), |
| 214 | uint32_t uint32_t_alignment __attribute__((unused)), |
| 215 | uint32_t uint64_t_alignment __attribute__((unused)), |
| 216 | uint32_t long_alignment __attribute__((unused)), |
| 217 | int byte_order __attribute__((unused)), |
| 218 | uint32_t major __attribute__((unused)), |
| 219 | uint32_t minor __attribute__((unused)), |
| 220 | const char *root_shm_path __attribute__((unused)), |
| 221 | const char *shm_path __attribute__((unused)), |
| 222 | uid_t euid __attribute__((unused)), |
| 223 | gid_t egid __attribute__((unused)), |
| 224 | uint64_t tracing_id __attribute__((unused))) |
| 225 | { |
| 226 | return nullptr; |
| 227 | } |
| 228 | |
| 229 | static inline |
| 230 | void ust_registry_session_destroy( |
| 231 | lttng::sessiond::ust::registry_session *session __attribute__((unused))) |
| 232 | {} |
| 233 | |
| 234 | static inline |
| 235 | void ust_registry_destroy_event( |
| 236 | lttng::sessiond::ust::registry_channel *chan __attribute__((unused)), |
| 237 | lttng::sessiond::ust::registry_event *event __attribute__((unused))) |
| 238 | {} |
| 239 | |
| 240 | /* The app object can be NULL for registry shared across applications. */ |
| 241 | static inline |
| 242 | int ust_metadata_session_statedump( |
| 243 | lttng::sessiond::ust::registry_session *session __attribute__((unused))) |
| 244 | { |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static inline |
| 249 | int ust_metadata_channel_statedump( |
| 250 | lttng::sessiond::ust::registry_session *session __attribute__((unused)), |
| 251 | lttng::sessiond::ust::registry_channel *chan __attribute__((unused))) |
| 252 | { |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static inline |
| 257 | int ust_metadata_event_statedump( |
| 258 | lttng::sessiond::ust::registry_session *session __attribute__((unused)), |
| 259 | lttng::sessiond::ust::registry_channel *chan __attribute__((unused)), |
| 260 | lttng::sessiond::ust::registry_event *event __attribute__((unused))) |
| 261 | { |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static inline |
| 266 | int ust_registry_create_or_find_enum( |
| 267 | lttng::sessiond::ust::registry_session *session __attribute__((unused)), |
| 268 | int session_objd __attribute__((unused)), |
| 269 | char *name __attribute__((unused)), |
| 270 | struct lttng_ust_ctl_enum_entry *entries __attribute__((unused)), |
| 271 | size_t nr_entries __attribute__((unused)), |
| 272 | uint64_t *enum_id __attribute__((unused))) |
| 273 | { |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | static inline |
| 278 | struct ust_registry_enum * |
| 279 | ust_registry_lookup_enum_by_id( |
| 280 | const lttng::sessiond::ust::registry_session *session __attribute__((unused)), |
| 281 | const char *name __attribute__((unused)), |
| 282 | uint64_t id __attribute__((unused))) |
| 283 | { |
| 284 | return NULL; |
| 285 | } |
| 286 | |
| 287 | static inline |
| 288 | void ust_registry_destroy_enum(lttng::sessiond::ust::registry_session *reg_session __attribute__((unused)), |
| 289 | struct ust_registry_enum *reg_enum __attribute__((unused))) |
| 290 | {} |
| 291 | |
| 292 | #endif /* HAVE_LIBLTTNG_UST_CTL */ |
| 293 | |
| 294 | #endif /* LTTNG_UST_REGISTRY_H */ |