MySQL 8.0.39
Source Code Documentation
plugin_trace.h
Go to the documentation of this file.
1/* Copyright (c) 2012, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef PLUGIN_TRACE_INCLUDED
25#define PLUGIN_TRACE_INCLUDED
26/**
27 @file include/mysql/plugin_trace.h
28
29 Declarations for client-side plugins of type MYSQL_CLIENT_TRACE_PLUGIN.
30
31 See libmysql/mysql_trace.c for a brief description of the client-side
32 protocol tracing infrastructure.
33*/
34
35#include <mysql/client_plugin.h>
36
37/*
38 Lists of protocol stages and trace events
39 =========================================
40
41 These lists are defined with PROTOCOL_STAGE_LIST() and TRACE_EVENT_LIST(),
42 respectively. Macros accept a disposition name as an argument.
43
44 For example, to process list of protocol stages using disposition "foo",
45 define protocol_stage_foo(Stage) macro and then put
46
47 PROTOCOL_STAGE_LIST(foo)
48
49 in your code. This will expand to sequence of protocol_stage_foo(X)
50 macros where X ranges over the list of protocol stages, and these macros
51 should generate the actual code. See below how this technique is used
52 to generate protocol_stage and trace_events enums.
53*/
54
55/**
56 Protocol stages
57 ---------------
58
59 A client following the MySQL protocol goes through several stages of it. Each
60 stage determines what packets can be expected from the server or can be send
61 by the client.
62
63 Upon receiving each trace event, trace plugin will be notified of the current
64 protocol stage so that it can correctly interpret the event.
65
66 These are the possible protocol stages and the transitions between them.
67
68 .. digraph:: protocol_stages
69
70 CONNECTING -> WAIT_FOR_INIT_PACKET;
71 CONNECTING -> DISCONNECTED [ label = "failed connection" ];
72
73 WAIT_FOR_INIT_PACKET -> AUTHENTICATE;
74 WAIT_FOR_INIT_PACKET -> SSL_NEGOTIATION -> AUTHENTICATE;
75
76 AUTHENTICATE -> READY_FOR_COMMAND [ label = "accepted" ];
77 AUTHENTICATE -> DISCONNECTED [ label = "rejected" ];
78
79 READY_FOR_COMMAND -> DISCONNECTED [ label = "COM_QUIT" ];
80 READY_FOR_COMMAND -> AUTHENTICATE [ label="after change user" ];
81 READY_FOR_COMMAND -> WAIT_FOR_PACKET
82 [ label="wait for a single packet after, e.g., COM_STATISTICS" ];
83 READY_FOR_COMMAND -> WAIT_FOR_RESULT;
84 READY_FOR_COMMAND -> WAIT_FOR_PS_DESCRIPTION
85 [ label="after prepare command" ];
86
87 WAIT_FOR_PACKET -> READY_FOR_COMAND;
88
89 WAIT_FOR_RESULT -> READY_FOR_COMMAND [ label="simple reply" ];
90 WAIT_FOR_RESULT -> WAIT_FOR_FIELD_DEF;
91 WAIT_FOR_RESULT -> FILE_REQUEST;
92
93 WAIT_FOR_FIELD_DEF -> WAIT_FOR_ROW [ label="in a resultset" ];
94 WAIT_FOR_FIELD_DEF -> READY_FOR_COMMAND
95 [ label="after describe table or prepare command" ];
96
97 WAIT_FOR_ROW -> READY_FOR_COMMAND;
98 WAIT_FOR_ROW -> WAIT_FOR_RESULT [ label="multi-resultset" ];
99
100 WAIT_FOR_PS_DESCRIPTION -> WAIT_FOR_PARAM_DEF;
101 WAIT_FOR_PS_DESCRIPTION -> READY_FOR_COMMAND
102 [ label="no params and result" ];
103 WAIT_FOR_PS_DESCRIPTION -> WAIT_FOR_FIELD_DEF [ label="no params" ];
104
105 WAIT_FOR_PARAM_DEF -> WAIT_FOR_FIELD_DEF;
106 WAIT_FOR_PARAM_DEF -> READY_FOR_COMMAND [ label="no result" ];
107
108 FILE_REQUEST -> WAIT_FOR_RESULT [label="when whole file sent"];
109*/
110
111#define PROTOCOL_STAGE_LIST(X) \
112 protocol_stage_##X(CONNECTING) protocol_stage_##X(WAIT_FOR_INIT_PACKET) \
113 protocol_stage_##X(AUTHENTICATE) protocol_stage_##X(SSL_NEGOTIATION) \
114 protocol_stage_##X(READY_FOR_COMMAND) \
115 protocol_stage_##X(WAIT_FOR_PACKET) \
116 protocol_stage_##X(WAIT_FOR_RESULT) \
117 protocol_stage_##X(WAIT_FOR_FIELD_DEF) \
118 protocol_stage_##X(WAIT_FOR_ROW) \
119 protocol_stage_##X(FILE_REQUEST) \
120 protocol_stage_##X(WAIT_FOR_PS_DESCRIPTION) \
121 protocol_stage_##X(WAIT_FOR_PARAM_DEF) \
122 protocol_stage_##X(DISCONNECTED)
123
124/**
125 Trace events
126 ------------
127
128 The following events are generated during the various stages of the
129 client-server conversation.
130
131 ---------------------- -----------------------------------------------------
132 Connection events
133 ---------------------- -----------------------------------------------------
134 CONNECTING Client is connecting to the server.
135 CONNECTED Physical connection has been established.
136 DISCONNECTED Connection with server was broken.
137 ---------------------- -----------------------------------------------------
138 SSL events
139 ---------------------- -----------------------------------------------------
140 SEND_SSL_REQUEST Client is sending SSL connection request.
141 SSL_CONNECT Client is initiating SSL handshake.
142 SSL_CONNECTED SSL connection has been established.
143 ---------------------- -----------------------------------------------------
144 Authentication events
145 ---------------------- -----------------------------------------------------
146 CHALLENGE_RECEIVED Client received authentication challenge.
147 AUTH_PLUGIN Client selects an authentication plugin to be used
148 in the following authentication exchange.
149 SEND_AUTH_RESPONSE Client sends response to the authentication
150 challenge.
151 SEND_AUTH_DATA Client sends extra authentication data packet.
152 AUTHENTICATED Server has accepted connection.
153 ---------------------- -----------------------------------------------------
154 Command phase events
155 ---------------------- -----------------------------------------------------
156 SEND_COMMAND Client is sending a command to the server.
157 SEND_FILE Client is sending local file contents to the server.
158 ---------------------- -----------------------------------------------------
159 General events
160 ---------------------- -----------------------------------------------------
161 READ_PACKET Client starts waiting for a packet from server.
162 PACKET_RECEIVED A packet from server has been received.
163 PACKET_SENT After successful sending of a packet to the server.
164 ERROR Client detected an error.
165 ---------------------- -----------------------------------------------------
166*/
167
168#define TRACE_EVENT_LIST(X) \
169 trace_event_##X(ERROR) trace_event_##X(CONNECTING) trace_event_##X( \
170 CONNECTED) trace_event_##X(DISCONNECTED) \
171 trace_event_##X(SEND_SSL_REQUEST) trace_event_##X(SSL_CONNECT) \
172 trace_event_##X(SSL_CONNECTED) trace_event_##X(INIT_PACKET_RECEIVED) \
173 trace_event_##X(AUTH_PLUGIN) trace_event_##X(SEND_AUTH_RESPONSE) \
174 trace_event_##X(SEND_AUTH_DATA) \
175 trace_event_##X(AUTHENTICATED) \
176 trace_event_##X(SEND_COMMAND) \
177 trace_event_##X(SEND_FILE) \
178 trace_event_##X(READ_PACKET) \
179 trace_event_##X(PACKET_RECEIVED) \
180 trace_event_##X(PACKET_SENT)
181
182/**
183 Some trace events have additional arguments. These are stored in
184 st_trace_event_args structure. Various events store their arguments in the
185 structure as follows. Unused members are set to 0/NULL.
186
187 AUTH_PLUGIN
188 ------------- ----------------------------------
189 plugin_name the name of the plugin
190 ------------- ----------------------------------
191
192 SEND_COMMAND
193 ------------- ----------------------------------
194 cmd the command code
195 hdr pointer to command packet header
196 hdr_len length of the header
197 pkt pointer to command arguments
198 pkt_len length of arguments
199 ------------- ----------------------------------
200
201 Other SEND_* and *_RECEIVED events
202 ------------- ----------------------------------
203 pkt the data sent or received
204 pkt_len length of the data
205 ------------- ----------------------------------
206
207 PACKET_SENT
208 ------------- ----------------------------------
209 pkt_len number of bytes sent
210 ------------- ----------------------------------
211*/
212
214 const char *plugin_name;
215 int cmd;
216 const unsigned char *hdr;
217 size_t hdr_len;
218 const unsigned char *pkt;
219 size_t pkt_len;
220};
221
222/* Definitions of protocol_stage and trace_event enums. */
223
224#define protocol_stage_enum(X) PROTOCOL_STAGE_##X,
225
226enum protocol_stage { PROTOCOL_STAGE_LIST(enum) PROTOCOL_STAGE_LAST };
227
228#define trace_event_enum(X) TRACE_EVENT_##X,
229
230enum trace_event { TRACE_EVENT_LIST(enum) TRACE_EVENT_LAST };
231
232/*
233 Trace plugin methods
234 ====================
235*/
236
238struct MYSQL;
239
240/**
241 Trace plugin tracing_start() method.
242
243 Called when tracing with this plugin starts on a connection. A trace
244 plugin might want to maintain per-connection information. It can
245 return a pointer to memory area holding such information. It will be
246 stored in a connection handle and passed to other plugin methods.
247
248 @param self pointer to the plugin instance
249 @param connection_handle Session
250 @param stage protocol stage in which tracing has started - currently
251 it is always CONNECTING stage.
252
253 @return A pointer to plugin-specific, per-connection data if any.
254*/
255
257 struct st_mysql_client_plugin_TRACE *self, MYSQL *connection_handle,
258 enum protocol_stage stage);
259
260/**
261 Trace plugin tracing_stop() method.
262
263 Called when tracing of the connection has ended. If a plugin
264 allocated any per-connection resources, it should de-allocate them
265 here.
266
267 @param self pointer to the plugin instance
268 @param connection_handle Session
269 @param plugin_data pointer to plugin's per-connection data.
270*/
271
273 MYSQL *connection_handle,
274 void *plugin_data);
275
276/**
277 Trace plugin trace_event() method.
278
279 Called when a trace event occurs. Plugin can decide to stop tracing
280 this connection by returning non-zero value.
281
282 @param self pointer to the plugin instance
283 @param plugin_data pointer to plugin's per-connection data
284 @param connection_handle Session
285 @param stage current protocol stage
286 @param event the trace event
287 @param args trace event arguments
288
289 @return Non-zero if tracing of the connection should end here.
290*/
291
293 void *plugin_data, MYSQL *connection_handle,
294 enum protocol_stage stage,
295 enum trace_event event,
296 struct st_trace_event_args args);
297
303};
304
305/**
306 The global trace_plugin pointer. If it is not NULL, it points at a
307 loaded trace plugin which should be used to trace all connections made
308 to the server.
309*/
311
312#ifndef NDEBUG
313
314/*
315 Functions for getting names of trace events and protocol
316 stages for debugging purposes.
317*/
318const char *protocol_stage_name(enum protocol_stage stage);
319const char *trace_event_name(enum trace_event ev);
320
321#endif
322
323#endif
MySQL Client Plugin API.
#define MYSQL_CLIENT_PLUGIN_HEADER
Definition: client_plugin.h:98
int() trace_event_handler(struct st_mysql_client_plugin_TRACE *self, void *plugin_data, MYSQL *connection_handle, enum protocol_stage stage, enum trace_event event, struct st_trace_event_args args)
Trace plugin trace_event() method.
Definition: plugin_trace.h:292
struct st_mysql_client_plugin_TRACE * trace_plugin
The global trace_plugin pointer.
Definition: mysql_trace.cc:69
const char * trace_event_name(enum trace_event ev)
Definition: mysql_trace.cc:209
#define TRACE_EVENT_LIST(X)
Definition: plugin_trace.h:168
const char * protocol_stage_name(enum protocol_stage stage)
Definition: mysql_trace.cc:197
trace_event
Definition: plugin_trace.h:230
protocol_stage
Definition: plugin_trace.h:226
void *() tracing_start_callback(struct st_mysql_client_plugin_TRACE *self, MYSQL *connection_handle, enum protocol_stage stage)
Trace plugin tracing_start() method.
Definition: plugin_trace.h:256
void() tracing_stop_callback(struct st_mysql_client_plugin_TRACE *self, MYSQL *connection_handle, void *plugin_data)
Trace plugin tracing_stop() method.
Definition: plugin_trace.h:272
#define PROTOCOL_STAGE_LIST(X)
Definition: plugin_trace.h:111
required string event
Definition: replication_group_member_actions.proto:32
T plugin_data(st_plugin_int **ref)
Definition: sql_plugin_ref.h:92
Definition: mysql.h:299
Definition: plugin_trace.h:298
tracing_stop_callback * tracing_stop
Definition: plugin_trace.h:301
trace_event_handler * trace_event
Definition: plugin_trace.h:302
MYSQL_CLIENT_PLUGIN_HEADER tracing_start_callback * tracing_start
Definition: plugin_trace.h:300
Some trace events have additional arguments.
Definition: plugin_trace.h:213
const unsigned char * hdr
Definition: plugin_trace.h:216
size_t hdr_len
Definition: plugin_trace.h:217
const unsigned char * pkt
Definition: plugin_trace.h:218
int cmd
Definition: plugin_trace.h:215
size_t pkt_len
Definition: plugin_trace.h:219
const char * plugin_name
Definition: plugin_trace.h:214