MySQL  8.0.17
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 #if !defined(DBUG_OFF)
38 #include <assert.h> // IWYU pragma: keep
39 #include <stdio.h>
40 #include <string.h>
41 #endif
42 
43 #if !defined(DBUG_OFF)
44 
46  const char *func; /* function name of the previous stack frame */
47  int func_len; /* how much to print from func */
48  const char *file; /* filename of the function of previous frame */
49  unsigned int level; /* this nesting level, highest bit enables tracing */
50  struct _db_stack_frame_ *prev; /* pointer to the previous frame */
51 };
52 
53 struct CODE_STATE;
54 
55 extern int _db_keyword_(struct CODE_STATE *, const char *, int);
56 extern int _db_explain_(struct CODE_STATE *cs, char *buf, size_t len);
57 extern int _db_explain_init_(char *buf, size_t len);
58 extern int _db_is_pushed_(void);
59 extern void _db_process_(const char *name);
60 extern void _db_push_(const char *control);
61 extern void _db_pop_(void);
62 extern void _db_set_(const char *control);
63 extern void _db_set_init_(const char *control);
64 extern void _db_enter_(const char *_func_, int func_len, const char *_file_,
65  unsigned int _line_,
66  struct _db_stack_frame_ *_stack_frame_);
67 extern void _db_return_(unsigned int _line_,
68  struct _db_stack_frame_ *_stack_frame_);
69 extern void _db_pargs_(unsigned int _line_, const char *keyword);
70 extern int _db_enabled_();
71 extern void _db_doprnt_(const char *format, ...)
72  MY_ATTRIBUTE((format(printf, 1, 2)));
73 extern void _db_dump_(unsigned int _line_, const char *keyword,
74  const unsigned char *memory, size_t length);
75 extern void _db_end_(void);
76 extern void _db_lock_file_(void);
77 extern void _db_unlock_file_(void);
78 extern FILE *_db_fp_(void);
79 extern void _db_flush_();
80 extern const char *_db_get_func_(void);
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 #define DBUG_TRACE \
132  AutoDebugTrace _db_trace(DBUG_PRETTY_FUNCTION, __FILE__, __LINE__)
133 #endif
134 
135 #define DBUG_ENTER(a) \
136  struct _db_stack_frame_ _db_stack_frame_; \
137  _db_enter_(a, ::strlen(a), __FILE__, __LINE__, &_db_stack_frame_)
138 #define DBUG_LEAVE _db_return_(__LINE__, &_db_stack_frame_)
139 #define DBUG_RETURN(a1) \
140  do { \
141  DBUG_LEAVE; \
142  return (a1); \
143  } while (0)
144 #define DBUG_VOID_RETURN \
145  do { \
146  DBUG_LEAVE; \
147  return; \
148  } while (0)
149 #define DBUG_EXECUTE(keyword, a1) \
150  do { \
151  if (_db_keyword_(0, (keyword), 0)) { \
152  a1 \
153  } \
154  } while (0)
155 #define DBUG_EXECUTE_IF(keyword, a1) \
156  do { \
157  if (_db_keyword_(0, (keyword), 1)) { \
158  a1 \
159  } \
160  } while (0)
161 #define DBUG_EVALUATE(keyword, a1, a2) \
162  (_db_keyword_(0, (keyword), 0) ? (a1) : (a2))
163 #define DBUG_EVALUATE_IF(keyword, a1, a2) \
164  (_db_keyword_(0, (keyword), 1) ? (a1) : (a2))
165 #define DBUG_PRINT(keyword, arglist) \
166  do { \
167  _db_pargs_(__LINE__, keyword); \
168  if (_db_enabled_()) { \
169  _db_doprnt_ arglist; \
170  } \
171  } while (0)
172 
173 #define DBUG_PUSH(a1) _db_push_(a1)
174 #define DBUG_POP() _db_pop_()
175 #define DBUG_SET(a1) _db_set_(a1)
176 #define DBUG_SET_INITIAL(a1) _db_set_init_(a1)
177 #define DBUG_PROCESS(a1) _db_process_(a1)
178 #define DBUG_FILE _db_fp_()
179 #define DBUG_DUMP(keyword, a1, a2) _db_dump_(__LINE__, keyword, a1, a2)
180 #define DBUG_END() _db_end_()
181 #define DBUG_LOCK_FILE _db_lock_file_()
182 #define DBUG_UNLOCK_FILE _db_unlock_file_()
183 #define DBUG_ASSERT(A) assert(A)
184 #define DBUG_EXPLAIN(buf, len) _db_explain_(0, (buf), (len))
185 #define DBUG_EXPLAIN_INITIAL(buf, len) _db_explain_init_((buf), (len))
186 #ifndef _WIN32
187 #define DBUG_ABORT() (_db_flush_(), abort())
188 #define DBUG_EXIT() (_db_flush_(), exit(2))
189 #else
190 #include <crtdbg.h>
191 
192 #define DBUG_ABORT() \
193  (_db_flush_(), (void)_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE), \
194  (void)_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR), abort())
195 #define DBUG_EXIT() \
196  (_db_flush_(), (void)_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE), \
197  (void)_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR), _exit(2))
198 #endif
199 
200 /*
201  Make the program fail, without creating a core file.
202  abort() will send SIGABRT which (most likely) generates core.
203  Use SIGKILL instead, which cannot be caught.
204  We also pause the current thread, until the signal is actually delivered.
205  An alternative would be to use _exit(EXIT_FAILURE),
206  but then valgrind would report lots of memory leaks.
207  */
208 #ifdef _WIN32
209 #define DBUG_SUICIDE() DBUG_EXIT()
210 #else
211 extern void _db_suicide_() MY_ATTRIBUTE((noreturn));
212 extern void _db_flush_gcov_();
213 #define DBUG_SUICIDE() (_db_flush_(), _db_suicide_())
214 #endif
215 
216 #else /* No debugger */
217 
218 #ifdef __cplusplus
219 #define DBUG_TRACE \
220  do { \
221  } while (false)
222 #endif
223 #define DBUG_ENTER(a1)
224 #define DBUG_LEAVE
225 #define DBUG_RETURN(a1) \
226  do { \
227  return (a1); \
228  } while (0)
229 #define DBUG_VOID_RETURN \
230  do { \
231  return; \
232  } while (0)
233 #define DBUG_EXECUTE(keyword, a1) \
234  do { \
235  } while (0)
236 #define DBUG_EXECUTE_IF(keyword, a1) \
237  do { \
238  } while (0)
239 #define DBUG_EVALUATE(keyword, a1, a2) (a2)
240 #define DBUG_EVALUATE_IF(keyword, a1, a2) (a2)
241 #define DBUG_PRINT(keyword, arglist) \
242  do { \
243  } while (0)
244 #define DBUG_PUSH(a1) \
245  do { \
246  } while (0)
247 #define DBUG_SET(a1) \
248  do { \
249  } while (0)
250 #define DBUG_SET_INITIAL(a1) \
251  do { \
252  } while (0)
253 #define DBUG_POP() \
254  do { \
255  } while (0)
256 #define DBUG_PROCESS(a1) \
257  do { \
258  } while (0)
259 #define DBUG_DUMP(keyword, a1, a2) \
260  do { \
261  } while (0)
262 #define DBUG_END() \
263  do { \
264  } while (0)
265 #define DBUG_ASSERT(A) \
266  do { \
267  } while (0)
268 #define DBUG_LOCK_FILE \
269  do { \
270  } while (0)
271 #define DBUG_FILE (stderr)
272 #define DBUG_UNLOCK_FILE \
273  do { \
274  } while (0)
275 #define DBUG_EXPLAIN(buf, len)
276 #define DBUG_EXPLAIN_INITIAL(buf, len)
277 #define DBUG_ABORT() \
278  do { \
279  } while (0)
280 #define DBUG_SUICIDE() \
281  do { \
282  } while (0)
283 
284 #endif
285 
286 #ifdef __cplusplus
287 #if !defined(DBUG_OFF)
288 #include <sstream>
289 #include <string>
290 
291 /*
292  A C++ interface to the DBUG_PRINT macro. The DBUG_LOG macro takes two
293  arguments. The first argument is the keyword, as that of the
294  DBUG_PRINT. The 2nd argument 'v' will be passed to a C++ output stream.
295  This enables the use of C++ style output stream operator. In the code, it
296  will be used as follows:
297 
298  DBUG_LOG("blob", "space: " << space_id);
299 
300  Note: DBUG_PRINT() has a limitation of 1024 bytes for a single
301  print out. So, this limitation is there for DBUG_LOG macro also.
302 */
303 
304 #define DBUG_LOG(keyword, v) \
305  do { \
306  std::ostringstream sout; \
307  sout << v; \
308  DBUG_PRINT(keyword, ("%s", sout.str().c_str())); \
309  } while (0)
310 
311 void dump_trace();
312 
313 #else /* DBUG_OFF */
314 #define DBUG_LOG(keyword, v) \
315  do { \
316  } while (0)
317 #endif /* DBUG_OFF */
318 #endif /* __cplusplus */
319 
320 #endif /* MY_DBUG_INCLUDED */
void _db_return_(unsigned int _line_, struct _db_stack_frame_ *_stack_frame_)
Definition: dbug.cc:1189
int func_len
Definition: my_dbug.h:47
struct _db_stack_frame_ * prev
Definition: my_dbug.h:50
const string name("\ame\)
void _db_end_(void)
Definition: dbug.cc:1661
int _db_explain_init_(char *buf, size_t len)
Definition: dbug.cc:1078
void _db_set_init_(const char *control)
Definition: dbug.cc:879
A RAII helper to do DBUG_ENTER / DBUG_RETURN for you automatically.
Definition: my_dbug.h:105
printf("== done ==\)
void _db_suicide_()
Definition: dbug.cc:2145
Definition: my_dbug.h:45
const std::string FILE("FILE")
void _db_pop_(void)
Definition: dbug.cc:904
void _db_flush_()
Definition: dbug.cc:2124
void _db_doprnt_(const char *format,...)
Definition: dbug.cc:1311
void _db_enter_(const char *_func_, int func_len, const char *_file_, unsigned int _line_, struct _db_stack_frame_ *_stack_frame_)
Definition: dbug.cc:1120
void _db_set_(const char *control)
Definition: dbug.cc:800
int _db_explain_(struct CODE_STATE *cs, char *buf, size_t len)
Definition: dbug.cc:1018
const char * file
Definition: my_dbug.h:48
void _db_pargs_(unsigned int _line_, const char *keyword)
Definition: dbug.cc:1251
_db_stack_frame_ m_stack_frame
Definition: my_dbug.h:128
void dump_trace()
Definition: trace.cc:47
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:191
Header for compiler-dependent features.
unsigned int len
Definition: dbug_analyze.cc:216
const char * func
Definition: my_dbug.h:46
void _db_dump_(unsigned int _line_, const char *keyword, const unsigned char *memory, size_t length)
Definition: dbug.cc:1369
FILE * _db_fp_(void)
Definition: dbug.cc:1733
unsigned int level
Definition: my_dbug.h:49
void _db_unlock_file_(void)
Definition: dbug.cc:2175
void _db_push_(const char *control)
Definition: dbug.cc:836
~AutoDebugTrace()
Definition: my_dbug.h:125
const char * filename
Definition: pfs_example_component_population.cc:64
void _db_lock_file_(void)
Definition: dbug.cc:2168
const char * _db_get_func_(void)
Definition: dbug.cc:2182
int _db_keyword_(struct CODE_STATE *, const char *, int)
Definition: dbug.cc:1759
int _db_enabled_()
Definition: dbug.cc:1275
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:858
void _db_flush_gcov_()
Definition: dbug.cc:2136
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:397