| 1 | /* |
| 2 | * Copyright (C) 2022 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef LTTNG_TRACE_FORMAT_DESCRIPTOR_INTERNAL_HPP |
| 9 | #define LTTNG_TRACE_FORMAT_DESCRIPTOR_INTERNAL_HPP |
| 10 | |
| 11 | #include <bits/stdint-uintn.h> |
| 12 | #include <common/config/session-config.hpp> |
| 13 | #include <common/dynamic-array.hpp> |
| 14 | #include <common/macros.hpp> |
| 15 | #include <common/optional.hpp> |
| 16 | #include <common/sessiond-comm/relayd.hpp> |
| 17 | #include <cstdint> |
| 18 | #include <functional> |
| 19 | #include <lttng/lttng.h> |
| 20 | #include <lttng/trace-format-descriptor.h> |
| 21 | #include <memory> |
| 22 | #include <pthread.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <unordered_map> |
| 25 | #include <urcu/ref.h> |
| 26 | |
| 27 | struct lttng_payload; |
| 28 | struct lttng_payload_view; |
| 29 | struct mi_writer; |
| 30 | |
| 31 | struct lttng_trace_format_descriptor_comm { |
| 32 | uint8_t type; |
| 33 | }; |
| 34 | |
| 35 | namespace lttng { |
| 36 | class trace_format_descriptor { |
| 37 | public: |
| 38 | using uptr = std::unique_ptr<trace_format_descriptor>; |
| 39 | using sptr = std::shared_ptr<trace_format_descriptor>; |
| 40 | using csptr = std::shared_ptr<const trace_format_descriptor>; |
| 41 | explicit trace_format_descriptor(enum lttng_trace_format_descriptor_type type) : _type(type) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | virtual ~trace_format_descriptor(); |
| 46 | virtual uptr clone() const = 0; // Virtual constructor (copying) |
| 47 | |
| 48 | enum lttng_trace_format_descriptor_type type() const |
| 49 | { |
| 50 | return _type; |
| 51 | } |
| 52 | |
| 53 | enum relayd_trace_format relayd_type() const |
| 54 | { |
| 55 | switch (_type) { |
| 56 | case LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_1: |
| 57 | return RELAYD_TRACE_FORMAT_CTF_1; |
| 58 | case LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_2: |
| 59 | return RELAYD_TRACE_FORMAT_CTF_2; |
| 60 | default: |
| 61 | /* TODO/FIXME: throw??? */ |
| 62 | abort(); |
| 63 | } |
| 64 | } |
| 65 | virtual lttng_error_code mi_serialize(mi_writer *writer) const final; |
| 66 | virtual lttng_error_code config_serialize(config_writer *writer) const final; |
| 67 | virtual int serialize(lttng_payload *payload) const; |
| 68 | static ssize_t create_from_payload(lttng_payload_view *view, uptr& descriptor); |
| 69 | |
| 70 | friend bool operator==( |
| 71 | trace_format_descriptor const& lhs, trace_format_descriptor const& rhs) |
| 72 | { |
| 73 | if (lhs.type() != rhs.type()) { |
| 74 | return false; |
| 75 | } |
| 76 | return lhs.equal_to(rhs); |
| 77 | } |
| 78 | |
| 79 | friend bool operator!=( |
| 80 | trace_format_descriptor const& lhs, trace_format_descriptor const& rhs) |
| 81 | { |
| 82 | return !(lhs == rhs); |
| 83 | } |
| 84 | |
| 85 | protected: |
| 86 | using DeserializerFunction = |
| 87 | std::function<ssize_t(lttng_payload_view *view, uptr& descriptor)>; |
| 88 | using deserializer_map = |
| 89 | std::unordered_map<lttng_trace_format_descriptor_type, DeserializerFunction>; |
| 90 | |
| 91 | static deserializer_map _deserializer_map; |
| 92 | virtual bool equal_to(trace_format_descriptor const& rhs) const = 0; |
| 93 | |
| 94 | private: |
| 95 | enum lttng_trace_format_descriptor_type _type = LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_UNKNOWN; |
| 96 | |
| 97 | virtual lttng_error_code subtype_mi_serialize(mi_writer *writer) const = 0; |
| 98 | virtual lttng_error_code subtype_config_serialize(config_writer *writer) const = 0; |
| 99 | }; |
| 100 | |
| 101 | class trace_format_descriptor_ctf1 : public trace_format_descriptor { |
| 102 | public: |
| 103 | trace_format_descriptor_ctf1() : |
| 104 | trace_format_descriptor(LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_1) |
| 105 | { |
| 106 | } // Default constructor. |
| 107 | |
| 108 | trace_format_descriptor_ctf1(trace_format_descriptor_ctf1 const&) : |
| 109 | trace_format_descriptor_ctf1() |
| 110 | { |
| 111 | } // Copy constructor |
| 112 | |
| 113 | uptr clone() const |
| 114 | { |
| 115 | return uptr(new trace_format_descriptor_ctf1(*this)); |
| 116 | } |
| 117 | |
| 118 | uint64_t getMajor() const |
| 119 | { |
| 120 | return _major; |
| 121 | } |
| 122 | uint64_t getMinor() const |
| 123 | { |
| 124 | return _minor; |
| 125 | } |
| 126 | |
| 127 | protected: |
| 128 | static ssize_t deserialize(lttng_payload_view *view, uptr& descriptor); |
| 129 | bool equal_to(trace_format_descriptor const& rhs) const override; |
| 130 | |
| 131 | private: |
| 132 | lttng_error_code subtype_mi_serialize(mi_writer *writer) const override; |
| 133 | lttng_error_code subtype_config_serialize(config_writer *writer) const override; |
| 134 | |
| 135 | const uint64_t _major = 1; |
| 136 | const uint64_t _minor = 8; |
| 137 | }; |
| 138 | |
| 139 | class trace_format_descriptor_ctf2 : public trace_format_descriptor { |
| 140 | public: |
| 141 | trace_format_descriptor_ctf2() : |
| 142 | trace_format_descriptor(LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_2) |
| 143 | { |
| 144 | } |
| 145 | |
| 146 | trace_format_descriptor_ctf2(trace_format_descriptor_ctf2 const&) : |
| 147 | trace_format_descriptor_ctf2() |
| 148 | { |
| 149 | } // Copy constructor |
| 150 | |
| 151 | uptr clone() const |
| 152 | { |
| 153 | return uptr(new trace_format_descriptor_ctf2(*this)); |
| 154 | } |
| 155 | |
| 156 | uint64_t getMajor() const |
| 157 | { |
| 158 | return _major; |
| 159 | } |
| 160 | uint64_t getMinor() const |
| 161 | { |
| 162 | return _minor; |
| 163 | } |
| 164 | |
| 165 | protected: |
| 166 | static ssize_t deserialize(lttng_payload_view *view, uptr& descriptor); |
| 167 | bool equal_to(trace_format_descriptor const& rhs) const override; |
| 168 | |
| 169 | private: |
| 170 | lttng_error_code subtype_mi_serialize(mi_writer *writer) const override; |
| 171 | lttng_error_code subtype_config_serialize(config_writer *writer) const override; |
| 172 | const uint64_t _major = 2; |
| 173 | const uint64_t _minor = 0; |
| 174 | }; |
| 175 | } // namespace lttng |
| 176 | |
| 177 | #endif /* LTTNG_TRACE_FORMAT_DESCRIPTOR_INTERNAL_HPP */ |