MySQL 26.7.0
Source Code Documentation
ut0dbg.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1994, 2026, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26*****************************************************************************/
27
28/** @file include/ut0dbg.h
29 Debug utilities for Innobase
30
31 Created 1/30/1994 Heikki Tuuri
32 **********************************************************************/
33
34#ifndef ut0dbg_h
35#define ut0dbg_h
36
37#include "my_compiler.h"
38#include "my_dbug.h"
39/* Do not include univ.i because univ.i includes this. */
40
41#include <cstdint>
42#include <cstdio>
43#include <functional>
44#include <sstream>
45
46/** Set a callback function to be called before exiting.
47@param[in] callback user callback function */
48void ut_set_assert_callback(std::function<void()> &callback);
49
50/** Terminates execution of the current process. */
51[[noreturn]] void ut_fatal_error();
52
53/** Report a failed assertion.
54@param[in] expr The failed assertion
55@param[in] file Source file containing the assertion
56@param[in] line Line number of the assertion */
57[[noreturn]] void ut_dbg_assertion_failed(const char *expr, const char *file,
58 uint64_t line);
59
60template <typename L, typename R>
61[[noreturn]] void inline ut_dbg_comparison_failed(
62 const char *lhs_expr, const L &lhs_value, const char *op,
63 const char *rhs_expr, const R &rhs_value, const char *file, uint64_t line) {
65 text << lhs_expr << " == " << lhs_value << ' ' << op << ' ' << rhs_value
66 << " == " << rhs_expr;
67 ut_dbg_assertion_failed(text.str().c_str(), file, line);
68}
69
70/** Assert that LHS OP RHS, where OP is an operator.
71Abort execution otherwise.
72Technical remarks: The LHS and RHS are evaluated exactly once (no short
73circuiting, even if OP is && or ||). Each value is stored in a local variable,
74so it's fine for LHS or RHS to return a temporary.
75In case of assertion failure references to const values of LHS and RHS will be
76passed to std::ostringstream::operator<<, so it must be implemented for them. */
77#define ut_a_op(LHS, OP, RHS) \
78 do { \
79 const auto lhs{LHS}; \
80 const auto rhs{RHS}; \
81 if (unlikely(!(lhs OP rhs))) { \
82 ut_dbg_comparison_failed(#LHS, lhs, #OP, #RHS, rhs, __FILE__, __LINE__); \
83 } \
84 } while (0)
85
86/** Assert that LHS < RHS. Abort execution otherwise. */
87#define ut_a_lt(LHS, RHS) ut_a_op(LHS, <, RHS)
88/** Assert that LHS <= RHS. Abort execution otherwise. */
89#define ut_a_le(LHS, RHS) ut_a_op(LHS, <=, RHS)
90/** Assert that LHS == RHS. Abort execution otherwise. */
91#define ut_a_eq(LHS, RHS) ut_a_op(LHS, ==, RHS)
92/** Assert that LHS != RHS. Abort execution otherwise. */
93#define ut_a_ne(LHS, RHS) ut_a_op(LHS, !=, RHS)
94
95/** Abort execution if EXPR does not evaluate to nonzero.
96@param EXPR assertion expression that should hold */
97#define ut_a(EXPR) \
98 do { \
99 if (unlikely(false == (bool)(EXPR))) { \
100 ut_dbg_assertion_failed(#EXPR, __FILE__, __LINE__); \
101 } \
102 } while (0)
103
104/** Abort execution. */
105#define ut_error ut_dbg_assertion_failed(nullptr, __FILE__, __LINE__)
106
107#ifdef UNIV_DEBUG
108/** Debug assertion. Does nothing unless UNIV_DEBUG is defined. */
109#define ut_ad(EXPR) ut_a(EXPR)
110/** Debug statement. Does nothing unless UNIV_DEBUG is defined. */
111#define ut_d(EXPR) EXPR
112/** Opposite of ut_d(). Does nothing if UNIV_DEBUG is defined. */
113#define ut_o(EXPR)
114/** Debug-only assertion that LHS < RHS. */
115#define ut_ad_lt(LHS, RHS) ut_a_lt(LHS, RHS)
116/** Debug-only assertion that LHS <= RHS. */
117#define ut_ad_le(LHS, RHS) ut_a_le(LHS, RHS)
118/** Debug-only assertion that LHS == RHS. */
119#define ut_ad_eq(LHS, RHS) ut_a_eq(LHS, RHS)
120/** Assert that LHS != RHS. Abort execution otherwise. */
121#define ut_ad_ne(LHS, RHS) ut_a_op(LHS, !=, RHS)
122#else
123/** Debug assertion. Does nothing unless UNIV_DEBUG is defined. */
124#define ut_ad(EXPR)
125/** Debug statement. Does nothing unless UNIV_DEBUG is defined. */
126#define ut_d(EXPR)
127/** Opposite of ut_d(). Does nothing if UNIV_DEBUG is defined. */
128#define ut_o(EXPR) EXPR
129/** Debug-only assertion that LHS < RHS. */
130#define ut_ad_lt(LHS, RHS)
131/** Debug-only assertion that LHS <= RHS. */
132#define ut_ad_le(LHS, RHS)
133/** Debug-only assertion that LHS == RHS. */
134#define ut_ad_eq(LHS, RHS)
135/** Assert that LHS != RHS. */
136#define ut_ad_ne(LHS, RHS)
137#endif
138
139/** Debug crash point */
140
141#ifdef UNIV_DEBUG
142inline void DBUG_INJECT_CRASH(const char *prefix, unsigned count) {
143 char buf[64];
144 snprintf(buf, sizeof buf, "%s_%u", prefix, count);
146}
147#else
148#define DBUG_INJECT_CRASH(prefix, count)
149#endif
150
151/** Silence warnings about an unused variable by doing a null assignment.
152@param A the unused variable */
153#define UT_NOT_USED(A) std::ignore = A
154
155#if defined(HAVE_SYS_TIME_H) && defined(HAVE_SYS_RESOURCE_H)
156
157#define HAVE_UT_CHRONO_T
158
159#include <sys/resource.h>
160#include <sys/time.h>
161#include <sys/types.h>
162
163/** A "chronometer" used to clock snippets of code.
164Example usage:
165 ut_chrono_t ch("this loop");
166 for (;;) { ... }
167 ch.show();
168would print the timings of the for() loop, prefixed with "this loop:" */
170 public:
171 /** Constructor.
172 @param[in] name chrono's name, used when showing the values */
174 reset();
175 }
176
177 /** Resets the chrono (records the current time in it). */
178 void reset() {
179 gettimeofday(&m_tv, nullptr);
180
181 getrusage(RUSAGE_SELF, &m_ru);
182 }
183
184 /** Shows the time elapsed and usage statistics since the last reset. */
185 void show() {
186 struct rusage ru_now;
187 struct timeval tv_now;
188 struct timeval tv_diff;
189
190 getrusage(RUSAGE_SELF, &ru_now);
191
192 gettimeofday(&tv_now, nullptr);
193
194#ifndef timersub
195#define timersub(a, b, r) \
196 do { \
197 (r)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
198 (r)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
199 if ((r)->tv_usec < 0) { \
200 (r)->tv_sec--; \
201 (r)->tv_usec += 1000000; \
202 } \
203 } while (0)
204#endif /* timersub */
205
206#define CHRONO_PRINT(type, tvp) \
207 fprintf(stderr, "%s: %s% 5ld.%06ld sec\n", m_name, type, \
208 static_cast<long>((tvp)->tv_sec), static_cast<long>((tvp)->tv_usec))
209
210 timersub(&tv_now, &m_tv, &tv_diff);
211 CHRONO_PRINT("real", &tv_diff);
212
213 timersub(&ru_now.ru_utime, &m_ru.ru_utime, &tv_diff);
214 CHRONO_PRINT("user", &tv_diff);
215
216 timersub(&ru_now.ru_stime, &m_ru.ru_stime, &tv_diff);
217 CHRONO_PRINT("sys ", &tv_diff);
218 }
219
220 /** Cause the timings not to be printed from the destructor. */
221 void end() { m_show_from_destructor = false; }
222
223 /** Destructor. */
226 show();
227 }
228 }
229
230 private:
231 /** Name of this chronometer. */
232 const char *m_name;
233
234 /** True if the current timings should be printed by the destructor. */
236
237 /** getrusage() result as of the last reset(). */
238 struct rusage m_ru;
239
240 /** gettimeofday() result as of the last reset(). */
241 struct timeval m_tv;
242};
243
244#endif /* HAVE_SYS_TIME_H && HAVE_SYS_RESOURCE_H */
245
246#endif
A "chronometer" used to clock snippets of code.
Definition: ut0dbg.h:169
void show()
Shows the time elapsed and usage statistics since the last reset.
Definition: ut0dbg.h:185
bool m_show_from_destructor
True if the current timings should be printed by the destructor.
Definition: ut0dbg.h:235
struct rusage m_ru
getrusage() result as of the last reset().
Definition: ut0dbg.h:238
ut_chrono_t(const char *name)
Constructor.
Definition: ut0dbg.h:173
const char * m_name
Name of this chronometer.
Definition: ut0dbg.h:232
struct timeval m_tv
gettimeofday() result as of the last reset().
Definition: ut0dbg.h:241
void reset()
Resets the chrono (records the current time in it).
Definition: ut0dbg.h:178
void end()
Cause the timings not to be printed from the destructor.
Definition: ut0dbg.h:221
~ut_chrono_t()
Destructor.
Definition: ut0dbg.h:224
#define L
Definition: ctype-tis620.cc:74
Header for compiler-dependent features.
#define DBUG_EXECUTE_IF(keyword, a1)
Definition: my_dbug.h:171
#define DBUG_SUICIDE()
Definition: my_dbug.h:228
static int count
Definition: myisam_ftdump.cc:45
Definition: buf0block_hint.cc:30
Definition: os0file.h:89
std::basic_ostringstream< char, std::char_traits< char >, ut::allocator< char > > ostringstream
Specialization of basic_ostringstream which uses ut::allocator.
Definition: ut0new.h:2720
Include file for Sun RPC to compile out of the box.
case opt name
Definition: sslopt-case.h:29
#define CHRONO_PRINT(type, tvp)
void ut_dbg_assertion_failed(const char *expr, const char *file, uint64_t line)
Report a failed assertion.
Definition: ut0dbg.cc:56
#define timersub(a, b, r)
void ut_fatal_error()
Terminates execution of the current process.
Definition: ut0dbg.cc:99
void DBUG_INJECT_CRASH(const char *prefix, unsigned count)
Debug crash point.
Definition: ut0dbg.h:142
void ut_dbg_comparison_failed(const char *lhs_expr, const L &lhs_value, const char *op, const char *rhs_expr, const R &rhs_value, const char *file, uint64_t line)
Definition: ut0dbg.h:61
void ut_set_assert_callback(std::function< void()> &callback)
Set a callback function to be called before exiting.
Definition: ut0dbg.cc:48