MySQL 9.1.0
Source Code Documentation
trace_stream.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2021, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_SRC_OPENSSL_INCLUDE_TLS_TRACE_STREAM_H_
27#define ROUTER_SRC_OPENSSL_INCLUDE_TLS_TRACE_STREAM_H_
28
29#include <algorithm>
30#include <functional>
31#include <iomanip>
32#include <mutex>
33#include <string>
34#include <utility>
35#include <vector>
36
38#ifndef USE_CUSTOM_HOLDER
40#endif // USE_CUSTOM_HOLDER
42
43// In future instead on defining output-stream methods here and a mutex to
44// access it, extract Console/terminal class.
46
47template <typename NameType, typename LowerLevelStream>
48class TraceStream : Mutex_static_holder<Trace_stream_static_holder> {
49 // Mutex_static_holder<Trace_stream_static_holder> - makes mutex static
50 // variable common for different instantiations of this template
51
52 public:
53 using native_handle_type = std::nullptr_t;
54 using protocol_type = std::nullptr_t;
55 using endpoint_type = typename LowerLevelStream::endpoint_type;
56 using VectorOfBuffers = std::vector<net::mutable_buffer>;
57 using VectorOfConstBuffers = std::vector<net::const_buffer>;
58
62 lower_layer_{std::move(other.lower_layer_)},
63 out_{other.out_} {
64 print("ctor move");
65 }
66
67 template <typename... Args>
68 TraceStream(Args &&...args) // NOLINT(runtime/explicit)
69 : lower_layer_{std::forward<Args>(args)...}, out_{NameType::get_out()} {
70 print("ctor");
71 }
72
73 auto get_executor() { return lower_layer_.get_executor(); }
74
75 LowerLevelStream &lower_layer() { return lower_layer_; }
76 const LowerLevelStream &lower_layer() const { return lower_layer_; }
77
78 template <class MutableBufferSequence>
80 const MutableBufferSequence &buffers) {
81 return lower_layer_.read_some(buffers);
82 }
83
84 template <class ConstBufferSequence>
86 const ConstBufferSequence &buffers) {
87 return lower_layer_.write_some(buffers);
88 }
89
90 template <typename Buffer, typename Handler>
91 void async_send(const Buffer &buffer, Handler &&handler) {
93 print("async_send buffer-size: ", net::buffer_size(buffer));
96 }
97
98 template <class HandshakeType, class CompletionToken>
99 auto async_handshake(HandshakeType type, CompletionToken &&token) {
100 print("async_handshake type: ", type);
102 lower_layer_.async_handshake(type, get_handshake_handler(ref, token));
103 }
104
105 template <typename Buffer, typename Handler>
106 void async_receive(const Buffer &buffer, Handler &&handler) {
108 print("async_receive buffer-size: ", net::buffer_size(buffer));
111 }
112
113 void set_parent(const char *parent) { parent_ = parent; }
114
115 auto cancel() {
116 print("cancel");
117 return lower_layer_.cancel();
118 }
119
120 auto close() {
121 print("close");
122 return lower_layer_.close();
123 }
124
125 void handle_read(std::error_code ec, size_t size) {
126 print("handle_read error:", ec, ", size:", size);
127
129 }
130
131 void handle_write(std::error_code ec, size_t size) {
132 print("handle_write error:", ec, ", size:", size);
133
135 }
136
137 void handle_handshake(std::error_code ec, size_t size) {
138 print("handle_handshake error:", ec, ", size:", size);
139 }
140
141 template <typename SettableSocketOption>
143 const SettableSocketOption &option) {
144 return lower_layer_.set_option(option);
145 }
146
147 template <typename... Args>
148 void print(const Args &...args) {
149 std::unique_lock<std::mutex> l{mutex_};
150 *out_ << "this:" << parent_ << ", thread:" << std::this_thread::get_id()
151 << ", " << NameType::get_name() << ": ";
152 print_single(args...);
153 *out_ << std::endl;
154 out_->flush();
155 }
156
157 protected:
159 public:
160 WrappedTraceStream(TraceStream *parent = nullptr) : parent_{parent} {}
162 : parent_{other.parent_} {}
164
165 void handle_read(std::error_code ec, size_t size) {
166 if (parent_) parent_->handle_read(ec, size);
167 }
168
169 void handle_write(std::error_code ec, size_t size) {
170 if (parent_) parent_->handle_write(ec, size);
171 }
172
173 void handle_handshake(std::error_code ec, size_t size) {
175 }
176
177 private:
179 };
180
181 template <typename Vector>
182 void dump(Vector &data, size_t s) {
183 std::unique_lock<std::mutex> l{mutex_};
184 auto it = data.begin();
185 size_t offset = 0;
186
187 while (it != data.end() && s) {
188 auto data_on_page = std::min(s, it->size());
189 const char *data_ptr = (const char *)it->data();
190 auto to_print = data_on_page;
191
192 while (to_print) {
193 auto line = std::min(to_print, 16UL);
194 *out_ << "this:" << parent_ << " " << std::hex << std::setfill('0')
195 << std::setw(8) << offset << " | ";
196 for (size_t i = 0; i < line; ++i) {
197 *out_ << " 0x" << std::setw(2) << (int)((const uint8_t *)data_ptr)[i];
198 }
199
200 *out_ << std::string((16 - line) * 5, ' ');
201
202 *out_ << " - ";
203 for (size_t i = 0; i < line; ++i) {
204 *out_ << (iscntrl(data_ptr[i]) ? '?' : data_ptr[i]);
205 }
206
207 *out_ << std::dec << std::setw(1) << std::endl;
208
209 to_print -= line;
210 data_ptr += line;
211 s -= line;
212 offset += line;
213 }
214 }
215
216 out_->flush();
217 }
218
219 template <typename Vector, typename SrcBuffers>
220 void copy(Vector &dst, const SrcBuffers &src) {
221 dst.resize(0);
222 auto it = net::buffer_sequence_begin(src);
223 auto end = net::buffer_sequence_end(src);
224 while (it != end) {
225 dst.emplace_back(it->data(), it->size());
226 ++it;
227 }
228 }
229
230 void print_single() {}
231
232 template <typename Arg, typename... Args>
233 void print_single(const Arg &arg, const Args... args) {
234 *out_ << arg;
235 print_single(args...);
236 }
237
238 template <typename WriteToken, typename StandardToken>
240 get_write_handler(WriteToken &write_token, StandardToken &std_token) {
241 using Write_token_result = std::decay_t<WriteToken>;
242 using Write_token_handler =
243 std::conditional_t<std::is_same<WriteToken, Write_token_result>::value,
244 Write_token_result &, Write_token_result>;
245
246 using Standard_token_result = std::decay_t<StandardToken>;
247 using Standard_token_handler = std::conditional_t<
248 std::is_same<StandardToken, Standard_token_result>::value,
249 Standard_token_result &, Standard_token_result>;
250
252 std::forward<Write_token_handler>(write_token),
253 std::forward<Standard_token_handler>(std_token));
254 }
255
256 template <typename ReadToken, typename StandardToken>
258 get_read_handler(ReadToken &read_token, StandardToken &std_token) {
259 using Read_token_result = std::decay_t<ReadToken>;
260 using Read_token_handler =
261 std::conditional_t<std::is_same<ReadToken, Read_token_result>::value,
262 Read_token_result &, Read_token_result>;
263
264 using Standard_token_result = std::decay_t<StandardToken>;
265 using Standard_token_handler = std::conditional_t<
266 std::is_same<StandardToken, Standard_token_result>::value,
267 Standard_token_result &, Standard_token_result>;
269 std::forward<Read_token_handler>(read_token),
270 std::forward<Standard_token_handler>(std_token));
271 }
272
273 template <typename HandshakeToken, typename StandardToken>
275 get_handshake_handler(HandshakeToken &handshake_token,
276 StandardToken &std_token) {
277 using Handshake_token_result = std::decay_t<HandshakeToken>;
278 using Handshake_token_handler = std::conditional_t<
279 std::is_same<HandshakeToken, Handshake_token_result>::value,
280 Handshake_token_result &, Handshake_token_result>;
281
282 using Standard_token_result = std::decay_t<StandardToken>;
283 using Standard_token_handler = std::conditional_t<
284 std::is_same<StandardToken, Standard_token_result>::value,
285 Standard_token_result &, Standard_token_result>;
287 StandardToken>(
288 std::forward<Handshake_token_handler>(handshake_token),
289 std::forward<Standard_token_handler>(std_token));
290 }
291
294 LowerLevelStream lower_layer_;
295 std::ostream *out_;
296 std::string parent_;
297};
298
299template <typename NameType, typename LowerLevelStream>
300class TlsTraceStream : public TraceStream<NameType, LowerLevelStream> {
301 public:
303 using This::TraceStream;
304 using LowerLayerType = typename LowerLevelStream::LowerLayerType;
305
306 LowerLayerType &lower_layer() { return this->lower_layer_.lower_layer(); }
308 return this->lower_layer_.lower_layer();
309 }
310};
311
312#endif // ROUTER_SRC_OPENSSL_INCLUDE_TLS_TRACE_STREAM_H_
Definition: trace_stream.h:300
const LowerLayerType & lower_layer() const
Definition: trace_stream.h:307
typename LowerLevelStream::LowerLayerType LowerLayerType
Definition: trace_stream.h:304
LowerLayerType & lower_layer()
Definition: trace_stream.h:306
Definition: trace_stream.h:158
void handle_write(std::error_code ec, size_t size)
Definition: trace_stream.h:169
WrappedTraceStream(TraceStream *parent=nullptr)
Definition: trace_stream.h:160
WrappedTraceStream(const WrappedTraceStream &other)
Definition: trace_stream.h:161
void handle_handshake(std::error_code ec, size_t size)
Definition: trace_stream.h:173
WrappedTraceStream(WrappedTraceStream &&other)
Definition: trace_stream.h:163
void handle_read(std::error_code ec, size_t size)
Definition: trace_stream.h:165
TraceStream * parent_
Definition: trace_stream.h:178
Definition: trace_stream.h:48
auto cancel()
Definition: trace_stream.h:115
typename LowerLevelStream::endpoint_type endpoint_type
Definition: trace_stream.h:55
VectorOfConstBuffers send_buffer_
Definition: trace_stream.h:293
auto async_handshake(HandshakeType type, CompletionToken &&token)
Definition: trace_stream.h:99
const LowerLevelStream & lower_layer() const
Definition: trace_stream.h:76
void handle_read(std::error_code ec, size_t size)
Definition: trace_stream.h:125
net::tls::LowerLayerWriteCompletionToken< WriteToken, StandardToken > get_write_handler(WriteToken &write_token, StandardToken &std_token)
Definition: trace_stream.h:240
void async_receive(const Buffer &buffer, Handler &&handler)
Definition: trace_stream.h:106
void handle_write(std::error_code ec, size_t size)
Definition: trace_stream.h:131
void copy(Vector &dst, const SrcBuffers &src)
Definition: trace_stream.h:220
std::vector< net::const_buffer > VectorOfConstBuffers
Definition: trace_stream.h:57
net::tls::LowerLayerHandshakeCompletionToken< HandshakeToken, StandardToken > get_handshake_handler(HandshakeToken &handshake_token, StandardToken &std_token)
Definition: trace_stream.h:275
std::nullptr_t protocol_type
Definition: trace_stream.h:54
net::tls::LowerLayerReadCompletionToken< ReadToken, StandardToken > get_read_handler(ReadToken &read_token, StandardToken &std_token)
Definition: trace_stream.h:258
void print_single()
Definition: trace_stream.h:230
stdx::expected< void, std::error_code > set_option(const SettableSocketOption &option)
Definition: trace_stream.h:142
TraceStream(TraceStream &&other)
Definition: trace_stream.h:59
TraceStream(Args &&...args)
Definition: trace_stream.h:68
std::ostream * out_
Definition: trace_stream.h:295
stdx::expected< size_t, std::error_code > write_some(const ConstBufferSequence &buffers)
Definition: trace_stream.h:85
std::vector< net::mutable_buffer > VectorOfBuffers
Definition: trace_stream.h:56
void set_parent(const char *parent)
Definition: trace_stream.h:113
void print(const Args &...args)
Definition: trace_stream.h:148
VectorOfBuffers recv_buffer_
Definition: trace_stream.h:292
auto get_executor()
Definition: trace_stream.h:73
std::nullptr_t native_handle_type
Definition: trace_stream.h:53
LowerLevelStream & lower_layer()
Definition: trace_stream.h:75
void handle_handshake(std::error_code ec, size_t size)
Definition: trace_stream.h:137
void async_send(const Buffer &buffer, Handler &&handler)
Definition: trace_stream.h:91
std::string parent_
Definition: trace_stream.h:296
LowerLevelStream lower_layer_
Definition: trace_stream.h:294
stdx::expected< size_t, std::error_code > read_some(const MutableBufferSequence &buffers)
Definition: trace_stream.h:79
void dump(Vector &data, size_t s)
Definition: trace_stream.h:182
void print_single(const Arg &arg, const Args... args)
Definition: trace_stream.h:233
auto close()
Definition: trace_stream.h:120
The handler class is the interface for dynamically loadable storage engines.
Definition: handler.h:4583
Definition: lower_layer_completion.h:115
Definition: lower_layer_completion.h:40
Definition: lower_layer_completion.h:77
static const std::string dec("DECRYPTION")
PT & ref(PT *tp)
Definition: tablespace_impl.cc:359
size_t size(const char *const c)
Definition: base64.h:46
HandshakeType
Definition: tls_stream.h:44
const const_buffer * buffer_sequence_end(const const_buffer &b) noexcept
Definition: buffer.h:185
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:418
const const_buffer * buffer_sequence_begin(const const_buffer &b) noexcept
Definition: buffer.h:180
size_t buffer_size(const ConstBufferSequence &buffers) noexcept
Definition: buffer.h:313
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:192
Definition: gcs_xcom_synode.h:64
pid_type get_id()
Definition: process.h:48
required string type
Definition: replication_group_member_actions.proto:34
uint read_token(const sql_digest_storage *digest_storage, uint index, uint *tok)
Read a single token from token array.
Definition: sql_digest.cc:56
Definition: mutex_static_holder.h:32
static std::mutex mutex_
Definition: mutex_static_holder.h:33
Definition: trace_stream.h:45