my_dbug.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* Copyright (c) 2000, 2016, 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. Without limiting anything contained in the foregoing, this file,
  12. which is part of C Driver for MySQL (Connector/C), is also subject to the
  13. Universal FOSS Exception, version 1.0, a copy of which can be found at
  14. http://oss.oracle.com/licenses/universal-foss-exception.
  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. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  22. #ifndef MY_DBUG_INCLUDED
  23. #define MY_DBUG_INCLUDED
  24. #include "my_global.h" /* MYSQL_PLUGIN_IMPORT */
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. #if !defined(DBUG_OFF)
  29. struct _db_stack_frame_ {
  30. const char *func; /* function name of the previous stack frame */
  31. const char *file; /* filename of the function of previous frame */
  32. uint level; /* this nesting level, highest bit enables tracing */
  33. struct _db_stack_frame_ *prev; /* pointer to the previous frame */
  34. };
  35. struct _db_code_state_;
  36. extern MYSQL_PLUGIN_IMPORT my_bool _dbug_on_;
  37. extern my_bool _db_keyword_(struct _db_code_state_ *, const char *, int);
  38. extern int _db_explain_(struct _db_code_state_ *cs, char *buf, size_t len);
  39. extern int _db_explain_init_(char *buf, size_t len);
  40. extern int _db_is_pushed_(void);
  41. extern void _db_setjmp_(void);
  42. extern void _db_longjmp_(void);
  43. extern void _db_process_(const char *name);
  44. extern void _db_push_(const char *control);
  45. extern void _db_pop_(void);
  46. extern void _db_set_(const char *control);
  47. extern void _db_set_init_(const char *control);
  48. extern void _db_enter_(const char *_func_, const char *_file_, uint _line_,
  49. struct _db_stack_frame_ *_stack_frame_);
  50. extern void _db_return_(uint _line_, struct _db_stack_frame_ *_stack_frame_);
  51. extern void _db_pargs_(uint _line_,const char *keyword);
  52. extern int _db_enabled_();
  53. extern void _db_doprnt_(const char *format,...)
  54. MY_ATTRIBUTE((format(printf, 1, 2)));
  55. extern void _db_doputs_(const char *log);
  56. extern void _db_dump_(uint _line_,const char *keyword,
  57. const unsigned char *memory, size_t length);
  58. extern void _db_end_(void);
  59. extern void _db_lock_file_(void);
  60. extern void _db_unlock_file_(void);
  61. extern FILE *_db_fp_(void);
  62. extern void _db_flush_();
  63. extern const char* _db_get_func_(void);
  64. #define DBUG_ENTER(a) struct _db_stack_frame_ _db_stack_frame_; \
  65. _db_enter_ (a,__FILE__,__LINE__,&_db_stack_frame_)
  66. #define DBUG_LEAVE _db_return_ (__LINE__, &_db_stack_frame_)
  67. #define DBUG_RETURN(a1) do {DBUG_LEAVE; return(a1);} while(0)
  68. #define DBUG_VOID_RETURN do {DBUG_LEAVE; return;} while(0)
  69. #define DBUG_EXECUTE(keyword,a1) \
  70. do {if (_db_keyword_(0, (keyword), 0)) { a1 }} while(0)
  71. #define DBUG_EXECUTE_IF(keyword,a1) \
  72. do {if (_db_keyword_(0, (keyword), 1)) { a1 }} while(0)
  73. #define DBUG_EVALUATE(keyword,a1,a2) \
  74. (_db_keyword_(0,(keyword), 0) ? (a1) : (a2))
  75. #define DBUG_EVALUATE_IF(keyword,a1,a2) \
  76. (_db_keyword_(0,(keyword), 1) ? (a1) : (a2))
  77. #define DBUG_PRINT(keyword,arglist) \
  78. do \
  79. { \
  80. if (_dbug_on_) \
  81. { \
  82. _db_pargs_(__LINE__,keyword); \
  83. if (_db_enabled_()) \
  84. { \
  85. _db_doprnt_ arglist; \
  86. } \
  87. } \
  88. } while(0)
  89. /*
  90. An alternate to DBUG_PRINT() macro, which takes a single string
  91. as the second argument.
  92. */
  93. #define DBUG_PUTS(keyword,arg) \
  94. do \
  95. { \
  96. if (_dbug_on_) \
  97. { \
  98. _db_pargs_(__LINE__,keyword); \
  99. if (_db_enabled_()) \
  100. { \
  101. _db_doputs_(arg); \
  102. } \
  103. } \
  104. } while(0)
  105. #define DBUG_PUSH(a1) _db_push_ (a1)
  106. #define DBUG_POP() _db_pop_ ()
  107. #define DBUG_SET(a1) _db_set_ (a1)
  108. #define DBUG_SET_INITIAL(a1) _db_set_init_ (a1)
  109. #define DBUG_PROCESS(a1) _db_process_(a1)
  110. #define DBUG_FILE _db_fp_()
  111. #define DBUG_SETJMP(a1) (_db_setjmp_ (), setjmp (a1))
  112. #define DBUG_LONGJMP(a1,a2) (_db_longjmp_ (), longjmp (a1, a2))
  113. #define DBUG_DUMP(keyword,a1,a2) _db_dump_(__LINE__,keyword,a1,a2)
  114. #define DBUG_END() _db_end_ ()
  115. #define DBUG_LOCK_FILE _db_lock_file_()
  116. #define DBUG_UNLOCK_FILE _db_unlock_file_()
  117. #define DBUG_ASSERT(A) assert(A)
  118. #define DBUG_EXPLAIN(buf,len) _db_explain_(0, (buf),(len))
  119. #define DBUG_EXPLAIN_INITIAL(buf,len) _db_explain_init_((buf),(len))
  120. #define DEBUGGER_OFF do { _dbug_on_= 0; } while(0)
  121. #define DEBUGGER_ON do { _dbug_on_= 1; } while(0)
  122. #ifndef _WIN32
  123. #define DBUG_ABORT() (_db_flush_(), abort())
  124. #else
  125. /*
  126. Avoid popup with abort/retry/ignore buttons. When BUG#31745 is fixed we can
  127. call abort() instead of _exit(2) (now it would cause a "test signal" popup).
  128. */
  129. #include <crtdbg.h>
  130. #define DBUG_ABORT() (_db_flush_(),\
  131. (void)_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE),\
  132. (void)_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR),\
  133. _exit(2))
  134. #endif
  135. #define DBUG_CHECK_CRASH(func, op) \
  136. do { char _dbuf_[255]; strxnmov(_dbuf_, sizeof(_dbuf_)-1, (func), (op)); \
  137. DBUG_EXECUTE_IF(_dbuf_, DBUG_ABORT()); } while(0)
  138. #define DBUG_CRASH_ENTER(func) \
  139. DBUG_ENTER(func); DBUG_CHECK_CRASH(func, "_crash_enter")
  140. #define DBUG_CRASH_RETURN(val) \
  141. DBUG_CHECK_CRASH(_db_get_func_(), "_crash_return")
  142. #define DBUG_CRASH_VOID_RETURN \
  143. DBUG_CHECK_CRASH (_db_get_func_(), "_crash_return")
  144. /*
  145. Make the program fail, without creating a core file.
  146. abort() will send SIGABRT which (most likely) generates core.
  147. Use SIGKILL instead, which cannot be caught.
  148. We also pause the current thread, until the signal is actually delivered.
  149. An alternative would be to use _exit(EXIT_FAILURE),
  150. but then valgrind would report lots of memory leaks.
  151. */
  152. #ifdef _WIN32
  153. #define DBUG_SUICIDE() DBUG_ABORT()
  154. #else
  155. extern void _db_suicide_();
  156. extern void _db_flush_gcov_();
  157. #define DBUG_SUICIDE() (_db_flush_(), _db_suicide_())
  158. #endif
  159. #else /* No debugger */
  160. #define DBUG_ENTER(a1)
  161. #define DBUG_LEAVE
  162. #define DBUG_RETURN(a1) do { return(a1); } while(0)
  163. #define DBUG_VOID_RETURN do { return; } while(0)
  164. #define DBUG_EXECUTE(keyword,a1) do { } while(0)
  165. #define DBUG_EXECUTE_IF(keyword,a1) do { } while(0)
  166. #define DBUG_EVALUATE(keyword,a1,a2) (a2)
  167. #define DBUG_EVALUATE_IF(keyword,a1,a2) (a2)
  168. #define DBUG_PRINT(keyword,arglist) do { } while(0)
  169. #define DBUG_PUTS(keyword,arg) do { } while(0)
  170. #define DBUG_LOG(keyword,arglist) do { } while(0)
  171. #define DBUG_PUSH(a1) do { } while(0)
  172. #define DBUG_SET(a1) do { } while(0)
  173. #define DBUG_SET_INITIAL(a1) do { } while(0)
  174. #define DBUG_POP() do { } while(0)
  175. #define DBUG_PROCESS(a1) do { } while(0)
  176. #define DBUG_SETJMP(a1) setjmp(a1)
  177. #define DBUG_LONGJMP(a1) longjmp(a1)
  178. #define DBUG_DUMP(keyword,a1,a2) do { } while(0)
  179. #define DBUG_END() do { } while(0)
  180. #define DBUG_ASSERT(A) do { } while(0)
  181. #define DBUG_LOCK_FILE do { } while(0)
  182. #define DBUG_FILE (stderr)
  183. #define DBUG_UNLOCK_FILE do { } while(0)
  184. #define DBUG_EXPLAIN(buf,len)
  185. #define DBUG_EXPLAIN_INITIAL(buf,len)
  186. #define DEBUGGER_OFF do { } while(0)
  187. #define DEBUGGER_ON do { } while(0)
  188. #define DBUG_ABORT() do { } while(0)
  189. #define DBUG_CRASH_ENTER(func)
  190. #define DBUG_CRASH_RETURN(val) do { return(val); } while(0)
  191. #define DBUG_CRASH_VOID_RETURN do { return; } while(0)
  192. #define DBUG_SUICIDE() do { } while(0)
  193. #endif
  194. #ifdef EXTRA_DEBUG
  195. /**
  196. Sync points allow us to force the server to reach a certain line of code
  197. and block there until the client tells the server it is ok to go on.
  198. The client tells the server to block with SELECT GET_LOCK()
  199. and unblocks it with SELECT RELEASE_LOCK(). Used for debugging difficult
  200. concurrency problems
  201. */
  202. #define DBUG_SYNC_POINT(lock_name,lock_timeout) \
  203. debug_sync_point(lock_name,lock_timeout)
  204. void debug_sync_point(const char* lock_name, uint lock_timeout);
  205. #else
  206. #define DBUG_SYNC_POINT(lock_name,lock_timeout)
  207. #endif /* EXTRA_DEBUG */
  208. #ifdef __cplusplus
  209. }
  210. #endif
  211. #ifdef __cplusplus
  212. #if !defined(DBUG_OFF)
  213. #include <sstream>
  214. /*
  215. A C++ interface to the DBUG_PUTS macro. The DBUG_LOG macro also
  216. takes two arguments. The first argument is the keyword, as that of the
  217. DBUG_PRINT. The 2nd argument 'v' will be passed to a C++ output stream.
  218. This enables the use of C++ style output stream operator. In the code, it
  219. will be used as follows:
  220. DBUG_LOG("blob", "space: " << space_id);
  221. Note: DBUG_PRINT() has a limitation of 1024 bytes for a single
  222. print out. This limitation is not there for DBUG_PUTS and DBUG_LOG
  223. macros.
  224. */
  225. #define DBUG_LOG(keyword, v) do { \
  226. std::ostringstream sout; \
  227. sout << v; \
  228. DBUG_PUTS(keyword, sout.str().c_str()); \
  229. } while(0)
  230. #endif /* DBUG_OFF */
  231. #endif /* __cplusplus */
  232. #endif /* MY_DBUG_INCLUDED */