plugin_trace.h 12 KB

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