MySQL  8.0.18
Source Code Documentation
my_dbug.h
Go to the documentation of this file.
1 /* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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 also distributed 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 included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
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 
23 #ifndef MY_DBUG_INCLUDED
24 #define MY_DBUG_INCLUDED
25 
26 /**
27  @file include/my_dbug.h
28 */
29 
30 #ifdef MY_MSCRT_DEBUG
31 #include <crtdbg.h>
32 #endif
33 #include <stdlib.h>
34 
35 #include "my_compiler.h"
36 
37 #include <string.h>
38 
39 #if !defined(DBUG_OFF)
40 #include <assert.h> // IWYU pragma: keep
41 #include <stdio.h>
42 #endif
43 
44 #if !defined(DBUG_OFF)
45 
47  const char *func; /* function name of the previous stack frame */
48  int func_len; /* how much to print from func */
49  const char *file; /* filename of the function of previous frame */
50  unsigned int level; /* this nesting level, highest bit enables tracing */
51  struct _db_stack_frame_ *prev; /* pointer to the previous frame */
52 };
53 
54 struct CODE_STATE;
55 
56 extern int _db_keyword_(struct CODE_STATE *, const char *, int);
57 extern int _db_explain_(struct CODE_STATE *cs, char *buf, size_t len);
58 extern int _db_explain_init_(char *buf, size_t len);
59 extern int _db_is_pushed_(void);
60 extern void _db_process_(const char *name);
61 extern void _db_push_(const char *control);
62 extern void _db_pop_(void);
63 extern void _db_set_(const char *control);
64 extern void _db_set_init_(const char *control);
65 extern void _db_enter_(const char *_func_, int func_len, const char *_file_,
66  unsigned int _line_,
67  struct _db_stack_frame_ *_stack_frame_);
68 extern void _db_return_(unsigned int _line_,
69  struct _db_stack_frame_ *_stack_frame_);
70 extern void _db_pargs_(unsigned int _line_, const char *keyword);
71 extern int _db_enabled_();
72 extern void _db_doprnt_(const char *format, ...)
73  MY_ATTRIBUTE((format(printf, 1, 2)));
74 extern void _db_dump_(unsigned int _line_, const char *keyword,
75  const unsigned char *memory, size_t length);
76 extern void _db_end_(void);
77 extern void _db_lock_file_(void);
78 extern void _db_unlock_file_(void);
79 extern FILE *_db_fp_(void);
80 extern void _db_flush_();
81 
82 #ifdef __cplusplus
83 
84 #if defined(__GNUC__)
85 // GCC, Clang, and compatible compilers.
86 #define DBUG_PRETTY_FUNCTION __PRETTY_FUNCTION__
87 #elif defined(__FUNCSIG__)
88 // For MSVC; skips the __cdecl. (__PRETTY_FUNCTION__ in GCC is not a
89 // preprocessor constant, but __FUNCSIG__ in MSVC is.)
90 #define DBUG_PRETTY_FUNCTION strchr(__FUNCSIG__, ' ') + 1
91 #else
92 // Standard C++; does not include the class name.
93 #define DBUG_PRETTY_FUNCTION __func__
94 #endif
95 
96 /**
97  A RAII helper to do DBUG_ENTER / DBUG_RETURN for you automatically. Use like
98  this:
99 
100  int foo() {
101  DBUG_TRACE;
102  return 42;
103  }
104  */
106  public:
107  AutoDebugTrace(const char *function, const char *filename, int line) {
108  // Remove the return type, if it's there.
109  const char *begin = strchr(function, ' ');
110  if (begin != nullptr) {
111  function = begin + 1;
112  }
113 
114  // Cut it off at the first parenthesis; the argument list is
115  // often too long to be interesting.
116  const char *end = strchr(function, '(');
117 
118  if (end == nullptr) {
119  _db_enter_(function, strlen(function), filename, line, &m_stack_frame);
120  } else {
121  _db_enter_(function, end - function, filename, line, &m_stack_frame);
122  }
123  }
124 
126 
127  private:
129 };
130 
131 #ifdef __SUNPRO_CC
132 // Disable debug tracing for Developer Studio, because we may get
133 // a fatal error from ld when linking large executables.
134 // section .eh_frame%__gthread_trigger():
135 // unexpected negative integer encountered: offset 0x630
136 #define DBUG_TRACE \
137  do { \
138  } while (false)
139 
140 #else
141 
142 #define DBUG_TRACE \
143  AutoDebugTrace _db_trace(DBUG_PRETTY_FUNCTION, __FILE__, __LINE__)
144 
145 #endif // __SUNPRO_CC
146 
147 #endif
148 
149 #define DBUG_ENTER(a) \
150  struct _db_stack_frame_ _db_stack_frame_; \
151  _db_enter_(a, ::strlen(a), __FILE__, __LINE__, &_db_stack_frame_)
152 
153 #define DBUG_RETURN(a1) \
154  do { \
155  _db_return_(__LINE__, &_db_stack_frame_); \
156  return (a1); \
157  } while (0)
158 #define DBUG_VOID_RETURN \
159  do { \
160  _db_return_(__LINE__, &_db_stack_frame_); \
161  return; \
162  } while (0)
163 #define DBUG_EXECUTE(keyword, a1) \
164  do { \
165  if (_db_keyword_(0, (keyword), 0)) { \
166  a1 \
167  } \
168  } while (0)
169 #define DBUG_EXECUTE_IF(keyword, a1) \
170  do { \
171  if (_db_keyword_(0, (keyword), 1)) { \
172  a1 \
173  } \
174  } while (0)
175 #define DBUG_EVALUATE(keyword, a1, a2) \
176  (_db_keyword_(0, (keyword), 0) ? (a1) : (a2))
177 #define DBUG_EVALUATE_IF(keyword, a1, a2) \
178  (_db_keyword_(0, (keyword), 1) ? (a1) : (a2))
179 #define DBUG_PRINT(keyword, arglist) \
180  do { \
181  _db_pargs_(__LINE__, keyword); \
182  if (_db_enabled_()) { \
183  _db_doprnt_ arglist; \
184  } \
185  } while (0)
186 
187 #define DBUG_PUSH(a1) _db_push_(a1)
188 #define DBUG_POP() _db_pop_()
189 #define DBUG_SET(a1) _db_set_(a1)
190 #define DBUG_SET_INITIAL(a1) _db_set_init_(a1)
191 #define DBUG_PROCESS(a1) _db_process_(a1)
192 #define DBUG_FILE _db_fp_()
193 #define DBUG_DUMP(keyword, a1, a2) _db_dump_(__LINE__, keyword, a1, a2)
194 #define DBUG_END() _db_end_()
195 #define DBUG_LOCK_FILE _db_lock_file_()
196 #define DBUG_UNLOCK_FILE _db_unlock_file_()
197 #define DBUG_ASSERT(A) assert(A)
198 #define DBUG_EXPLAIN(buf, len) _db_explain_(0, (buf), (len))
199 #define DBUG_EXPLAIN_INITIAL(buf, len) _db_explain_init_((buf), (len))
200 #ifndef _WIN32
201 #define DBUG_ABORT() (_db_flush_(), abort())
202 #define DBUG_EXIT() (_db_flush_(), exit(2))
203 #else
204 #include <crtdbg.h>
205 
206 #define DBUG_ABORT() \
207  (_db_flush_(), (void)_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE), \
208  (void)_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR), abort())
209 #define DBUG_EXIT() \
210  (_db_flush_(), (void)_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE), \
211  (void)_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR), _exit(2))
212 #endif
213 
214 /*
215  Make the program fail, without creating a core file.
216  abort() will send SIGABRT which (most likely) generates core.
217  Use SIGKILL instead, which cannot be caught.
218  We also pause the current thread, until the signal is actually delivered.
219  An alternative would be to use _exit(EXIT_FAILURE),
220  but then valgrind would report lots of memory leaks.
221  */
222 #ifdef _WIN32
223 #define DBUG_SUICIDE() DBUG_EXIT()
224 #else
225 extern void _db_suicide_() MY_ATTRIBUTE((noreturn));
226 extern void _db_flush_gcov_();
227 #define DBUG_SUICIDE() (_db_flush_(), _db_suicide_())
228 #endif
229 
230 #else /* No debugger */
231 
232 #ifdef __cplusplus
233 #define DBUG_TRACE \
234  do { \
235  } while (false)
236 #endif
237 #define DBUG_ENTER(a1)
238 #define DBUG_RETURN(a1) \
239  do { \
240  return (a1); \
241  } while (0)
242 #define DBUG_VOID_RETURN \
243  do { \
244  return; \
245  } while (0)
246 #define DBUG_EXECUTE(keyword, a1) \
247  do { \
248  } while (0)
249 #define DBUG_EXECUTE_IF(keyword, a1) \
250  do { \
251  } while (0)
252 #define DBUG_EVALUATE(keyword, a1, a2) (a2)
253 #define DBUG_EVALUATE_IF(keyword, a1, a2) (a2)
254 #define DBUG_PRINT(keyword, arglist) \
255  do { \
256  } while (0)
257 #define DBUG_PUSH(a1) \
258  do { \
259  } while (0)
260 #define DBUG_SET(a1) \
261  do { \
262  } while (0)
263 #define DBUG_SET_INITIAL(a1) \
264  do { \
265  } while (0)
266 #define DBUG_POP() \
267  do { \
268  } while (0)
269 #define DBUG_PROCESS(a1) \
270  do { \
271  } while (0)
272 #define DBUG_DUMP(keyword, a1, a2) \
273  do { \
274  } while (0)
275 #define DBUG_END() \
276  do { \
277  } while (0)
278 #define DBUG_ASSERT(A) \
279  do { \
280  } while (0)
281 #define DBUG_LOCK_FILE \
282  do { \
283  } while (0)
284 #define DBUG_FILE (stderr)
285 #define DBUG_UNLOCK_FILE \
286  do { \
287  } while (0)
288 #define DBUG_EXPLAIN(buf, len)
289 #define DBUG_EXPLAIN_INITIAL(buf, len)
290 #define DBUG_ABORT() \
291  do { \
292  } while (0)
293 #define DBUG_SUICIDE() \
294  do { \
295  } while (0)
296 
297 #endif
298 
299 #ifdef __cplusplus
300 #if !defined(DBUG_OFF)
301 #include <sstream>
302 #include <string>
303 
304 /*
305  A C++ interface to the DBUG_PRINT macro. The DBUG_LOG macro takes two
306  arguments. The first argument is the keyword, as that of the
307  DBUG_PRINT. The 2nd argument 'v' will be passed to a C++ output stream.
308  This enables the use of C++ style output stream operator. In the code, it
309  will be used as follows:
310 
311  DBUG_LOG("blob", "space: " << space_id);
312 
313  Note: DBUG_PRINT() has a limitation of 1024 bytes for a single
314  print out. So, this limitation is there for DBUG_LOG macro also.
315 */
316 
317 #define DBUG_LOG(keyword, v) \
318  do { \
319  if (_db_enabled_()) { \
320  std::ostringstream sout; \
321  sout << v; \
322  DBUG_PRINT(keyword, ("%s", sout.str().c_str())); \
323  } \
324  } while (0)
325 
326 #else /* DBUG_OFF */
327 #define DBUG_LOG(keyword, v) \
328  do { \
329  } while (0)
330 #endif /* DBUG_OFF */
331 #endif /* __cplusplus */
332 
333 #endif /* MY_DBUG_INCLUDED */
void _db_return_(unsigned int _line_, struct _db_stack_frame_ *_stack_frame_)
Definition: dbug.cc:1186
int func_len
Definition: my_dbug.h:48
struct _db_stack_frame_ * prev
Definition: my_dbug.h:51
const string name("\ame\)
void _db_end_(void)
Definition: dbug.cc:1658
int _db_explain_init_(char *buf, size_t len)
Definition: dbug.cc:1075
void _db_set_init_(const char *control)
Definition: dbug.cc:876
A RAII helper to do DBUG_ENTER / DBUG_RETURN for you automatically.
Definition: my_dbug.h:105
void _db_suicide_()
Definition: dbug.cc:2142
Definition: my_dbug.h:46
const std::string FILE("FILE")
void _db_pop_(void)
Definition: dbug.cc:901
void _db_flush_()
Definition: dbug.cc:2121
void _db_doprnt_(const char *format,...)
Definition: dbug.cc:1308
void _db_enter_(const char *_func_, int func_len, const char *_file_, unsigned int _line_, struct _db_stack_frame_ *_stack_frame_)
Definition: dbug.cc:1117
void _db_set_(const char *control)
Definition: dbug.cc:797
int _db_explain_(struct CODE_STATE *cs, char *buf, size_t len)
Definition: dbug.cc:1015
const char * file
Definition: my_dbug.h:49
void _db_pargs_(unsigned int _line_, const char *keyword)
Definition: dbug.cc:1248
_db_stack_frame_ m_stack_frame
Definition: my_dbug.h:128
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:188
Header for compiler-dependent features.
const char * func
Definition: my_dbug.h:47
void _db_dump_(unsigned int _line_, const char *keyword, const unsigned char *memory, size_t length)
Definition: dbug.cc:1366
FILE * _db_fp_(void)
Definition: dbug.cc:1730
unsigned int level
Definition: my_dbug.h:50
void _db_unlock_file_(void)
Definition: dbug.cc:2172
void _db_push_(const char *control)
Definition: dbug.cc:833
~AutoDebugTrace()
Definition: my_dbug.h:125
const char * filename
Definition: pfs_example_component_population.cc:64
void _db_lock_file_(void)
Definition: dbug.cc:2165
int _db_keyword_(struct CODE_STATE *, const char *, int)
Definition: dbug.cc:1756
int _db_enabled_()
Definition: dbug.cc:1272
AutoDebugTrace(const char *function, const char *filename, int line)
Definition: my_dbug.h:107
Definition: dbug.cc:215
int func_len
Definition: dbug.cc:218
int _db_is_pushed_(void)
Returns true if session-local settings have been set.
Definition: dbug.cc:855
void _db_flush_gcov_()
Definition: dbug.cc:2133
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:75
void _db_process_(const char *name)
Definition: dbug.cc:394