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