MySQL 8.4.2
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 VectorOfBuffers = std::vector<net::mutable_buffer>;
56 using VectorOfConstBuffers = std::vector<net::const_buffer>;
57
61 lower_layer_{std::move(other.lower_layer_)},
62 out_{other.out_} {
63 print("ctor move");
64 }
65
66 template <typename... Args>
67 TraceStream(Args &&... args) // NOLINT(runtime/explicit)
68 : lower_layer_{std::forward<Args>(args)...}, out_{NameType::get_out()} {
69 print("ctor");
70 }
71
72 auto get_executor() { return lower_layer_.get_executor(); }
73
74 LowerLevelStream &lower_layer() { return lower_layer_; }
75 const LowerLevelStream &lower_layer() const { return lower_layer_; }
76
77 template <class MutableBufferSequence>
79 const MutableBufferSequence &buffers) {
80 return lower_layer_.read_some(buffers);
81 }
82
83 template <class ConstBufferSequence>
85 const ConstBufferSequence &buffers) {
86 return lower_layer_.write_some(buffers);
87 }
88
89 template <typename Buffer, typename Handler>
90 void async_send(const Buffer &buffer, Handler &&handler) {
92 print("async_send buffer-size: ", net::buffer_size(buffer));
95 }
96
97 template <typename Buffer, typename Handler>
98 void async_receive(const Buffer &buffer, Handler &&handler) {
100 print("async_receive buffer-size: ", net::buffer_size(buffer));
103 }
104
105 void set_parent(const char *parent) { parent_ = parent; }
106
107 auto cancel() {
108 print("cancel");
109 return lower_layer_.cancel();
110 }
111
112 auto close() {
113 print("close");
114 return lower_layer_.close();
115 }
116
117 void handle_read(std::error_code ec, size_t size) {
118 print("handle_read error:", ec, ", size:", size);
119
121 }
122
123 void handle_write(std::error_code ec, size_t size) {
124 print("handle_write error:", ec, ", size:", size);
125
127 }
128
129 template <typename SettableSocketOption>
131 const SettableSocketOption &option) {
132 return lower_layer_.set_option(option);
133 }
134
135 template <typename... Args>
136 void print(const Args &... args) {
137 std::unique_lock<std::mutex> l{mutex_};
138 *out_ << "this:" << parent_ << ", thread:" << std::this_thread::get_id()
139 << ", " << NameType::get_name() << ": ";
140 print_single(args...);
141 *out_ << std::endl;
142 out_->flush();
143 }
144
145 protected:
147 public:
148 WrappedTraceStream(TraceStream *parent = nullptr) : parent_{parent} {}
150 : parent_{other.parent_} {}
152
153 void handle_read(std::error_code ec, size_t size) {
154 if (parent_) parent_->handle_read(ec, size);
155 }
156
157 void handle_write(std::error_code ec, size_t size) {
158 if (parent_) parent_->handle_write(ec, size);
159 }
160
161 private:
163 };
164
165 template <typename Vector>
166 void dump(Vector &data, size_t s) {
167 std::unique_lock<std::mutex> l{mutex_};
168 auto it = data.begin();
169 size_t offset = 0;
170
171 while (it != data.end() && s) {
172 auto data_on_page = std::min(s, it->size());
173 const char *data_ptr = (const char *)it->data();
174 auto to_print = data_on_page;
175
176 while (to_print) {
177 auto line = std::min(to_print, 16UL);
178 *out_ << "this:" << parent_ << " " << std::hex << std::setfill('0')
179 << std::setw(8) << offset << " | ";
180 for (size_t i = 0; i < line; ++i) {
181 *out_ << " 0x" << std::setw(2) << (int)((const uint8_t *)data_ptr)[i];
182 }
183
184 *out_ << std::string((16 - line) * 5, ' ');
185
186 *out_ << " - ";
187 for (size_t i = 0; i < line; ++i) {
188 *out_ << (iscntrl(data_ptr[i]) ? '?' : data_ptr[i]);
189 }
190
191 *out_ << std::dec << std::setw(1) << std::endl;
192
193 to_print -= line;
194 data_ptr += line;
195 s -= line;
196 offset += line;
197 }
198 }
199
200 out_->flush();
201 }
202
203 template <typename Vector, typename SrcBuffers>
204 void copy(Vector &dst, const SrcBuffers &src) {
205 dst.resize(0);
206 auto it = net::buffer_sequence_begin(src);
207 auto end = net::buffer_sequence_end(src);
208 while (it != end) {
209 dst.emplace_back(it->data(), it->size());
210 ++it;
211 }
212 }
213
214 void print_single() {}
215
216 template <typename Arg, typename... Args>
217 void print_single(const Arg &arg, const Args... args) {
218 *out_ << arg;
219 print_single(args...);
220 }
221
222 template <typename WriteToken, typename StandardToken>
224 get_write_handler(WriteToken &write_token, StandardToken &std_token) {
225 using Write_token_result = std::decay_t<WriteToken>;
226 using Write_token_handler =
227 std::conditional_t<std::is_same<WriteToken, Write_token_result>::value,
228 Write_token_result &, Write_token_result>;
229
230 using Standard_token_result = std::decay_t<StandardToken>;
231 using Standard_token_handler = std::conditional_t<
232 std::is_same<StandardToken, Standard_token_result>::value,
233 Standard_token_result &, Standard_token_result>;
234
236 std::forward<Write_token_handler>(write_token),
237 std::forward<Standard_token_handler>(std_token));
238 }
239
240 template <typename ReadToken, typename StandardToken>
242 get_read_handler(ReadToken &read_token, StandardToken &std_token) {
243 using Read_token_result = std::decay_t<ReadToken>;
244 using Read_token_handler =
245 std::conditional_t<std::is_same<ReadToken, Read_token_result>::value,
246 Read_token_result &, Read_token_result>;
247
248 using Standard_token_result = std::decay_t<StandardToken>;
249 using Standard_token_handler = std::conditional_t<
250 std::is_same<StandardToken, Standard_token_result>::value,
251 Standard_token_result &, Standard_token_result>;
253 std::forward<Read_token_handler>(read_token),
254 std::forward<Standard_token_handler>(std_token));
255 }
256
259 LowerLevelStream lower_layer_;
260 std::ostream *out_;
261 std::string parent_;
262};
263
264template <typename NameType, typename LowerLevelStream>
265class TlsTraceStream : public TraceStream<NameType, LowerLevelStream> {
266 public:
268 using This::TraceStream;
269 using LowerLayerType = typename LowerLevelStream::LowerLayerType;
270
271 LowerLayerType &lower_layer() { return this->lower_layer_.lower_layer(); }
273 return this->lower_layer_.lower_layer();
274 }
275};
276
277#endif // ROUTER_SRC_OPENSSL_INCLUDE_TLS_TRACE_STREAM_H_
Definition: trace_stream.h:265
const LowerLayerType & lower_layer() const
Definition: trace_stream.h:272
typename LowerLevelStream::LowerLayerType LowerLayerType
Definition: trace_stream.h:269
LowerLayerType & lower_layer()
Definition: trace_stream.h:271
Definition: trace_stream.h:146
void handle_write(std::error_code ec, size_t size)
Definition: trace_stream.h:157
WrappedTraceStream(TraceStream *parent=nullptr)
Definition: trace_stream.h:148
WrappedTraceStream(const WrappedTraceStream &other)
Definition: trace_stream.h:149
WrappedTraceStream(WrappedTraceStream &&other)
Definition: trace_stream.h:151
void handle_read(std::error_code ec, size_t size)
Definition: trace_stream.h:153
TraceStream * parent_
Definition: trace_stream.h:162
Definition: trace_stream.h:48
auto cancel()
Definition: trace_stream.h:107
VectorOfConstBuffers send_buffer_
Definition: trace_stream.h:258
TraceStream(Args &&... args)
Definition: trace_stream.h:67
const LowerLevelStream & lower_layer() const
Definition: trace_stream.h:75
void handle_read(std::error_code ec, size_t size)
Definition: trace_stream.h:117
net::tls::LowerLayerWriteCompletionToken< WriteToken, StandardToken > get_write_handler(WriteToken &write_token, StandardToken &std_token)
Definition: trace_stream.h:224
void async_receive(const Buffer &buffer, Handler &&handler)
Definition: trace_stream.h:98
void handle_write(std::error_code ec, size_t size)
Definition: trace_stream.h:123
void copy(Vector &dst, const SrcBuffers &src)
Definition: trace_stream.h:204
std::vector< net::const_buffer > VectorOfConstBuffers
Definition: trace_stream.h:56
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:242
void print_single()
Definition: trace_stream.h:214
stdx::expected< void, std::error_code > set_option(const SettableSocketOption &option)
Definition: trace_stream.h:130
TraceStream(TraceStream &&other)
Definition: trace_stream.h:58
std::ostream * out_
Definition: trace_stream.h:260
stdx::expected< size_t, std::error_code > write_some(const ConstBufferSequence &buffers)
Definition: trace_stream.h:84
std::vector< net::mutable_buffer > VectorOfBuffers
Definition: trace_stream.h:55
void set_parent(const char *parent)
Definition: trace_stream.h:105
VectorOfBuffers recv_buffer_
Definition: trace_stream.h:257
auto get_executor()
Definition: trace_stream.h:72
std::nullptr_t native_handle_type
Definition: trace_stream.h:53
LowerLevelStream & lower_layer()
Definition: trace_stream.h:74
void async_send(const Buffer &buffer, Handler &&handler)
Definition: trace_stream.h:90
std::string parent_
Definition: trace_stream.h:261
LowerLevelStream lower_layer_
Definition: trace_stream.h:259
stdx::expected< size_t, std::error_code > read_some(const MutableBufferSequence &buffers)
Definition: trace_stream.h:78
void print(const Args &... args)
Definition: trace_stream.h:136
void dump(Vector &data, size_t s)
Definition: trace_stream.h:166
void print_single(const Arg &arg, const Args... args)
Definition: trace_stream.h:217
auto close()
Definition: trace_stream.h:112
The handler class is the interface for dynamically loadable storage engines.
Definition: handler.h:4573
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
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
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