plugin_trace.h 12 KB

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