libcruft-util/debug.hpp

552 lines
23 KiB
C++
Raw Normal View History

2011-05-23 17:18:52 +10:00
/*
2018-08-04 15:14:06 +10:00
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
2011-05-23 17:18:52 +10:00
*
* Copyright 2010-2019 Danny Robson <danny@nerdcruft.net>
2011-05-23 17:18:52 +10:00
*/
#pragma once
2011-05-23 17:18:52 +10:00
#include "platform.hpp"
#include "debug/panic.hpp"
//#include "maths.hpp" // XXX: See notes at the end of file for maths.hpp inclusion
2018-04-13 18:49:36 +10:00
#include <cmath>
2016-02-05 14:27:58 +11:00
#include <algorithm>
2015-08-10 15:41:34 +10:00
#include <stdexcept>
#include <string>
2018-03-22 13:20:23 +11:00
#include <thread>
#include <iosfwd>
#include <sstream>
2011-05-23 17:18:52 +10:00
2014-04-16 19:16:48 +10:00
///////////////////////////////////////////////////////////////////////////////
// it is fractionally easier to define a constexpr variable which can be used
// in constexpr-if to enable/disable some codepaths rather than deal with
// macros in some scenarios. eg, templates are complicated enough without
// (more) macros.
#if !defined(NDEBUG)
constexpr bool debug_enabled = true;
2018-04-05 12:13:37 +10:00
constexpr bool assertions_enabled = true;
#else
constexpr bool debug_enabled = false;
2018-04-05 12:13:37 +10:00
constexpr bool assertions_enabled = false;
#endif
2014-04-16 19:16:48 +10:00
///----------------------------------------------------------------------------
2018-03-20 14:49:46 +11:00
/// enable some code only if assertions are enabled
///
/// explicitly does not use constexpr if to remove the code as some paths may
/// refer to variables which do not always exist, and current compiler
/// implementations are a little picky here.
#ifndef NDEBUG
#include <iostream>
2018-03-20 14:49:46 +11:00
#define DEBUG_ONLY(X) do { X; } while (0)
#else
#define DEBUG_ONLY(X) do { ; } while (0)
#endif
//#define DEBUG_ONLY(X) do { if constexpr (debug_enabled) { X } } while (0)
2014-04-16 19:16:48 +10:00
///////////////////////////////////////////////////////////////////////////////
#define EXIT_XSUCCESS 0
#define EXIT_XSKIP 77
#define EXIT_XHARD_ERROR 99
///////////////////////////////////////////////////////////////////////////////
#define TRACE { \
DEBUG_ONLY ( \
std::cerr << "tid: " << std::this_thread::get_id () \
<< "; " << __FILE__ \
<< ":" << __func__ \
<< ":" << __LINE__ \
<< '\n'; \
); \
2011-07-16 14:47:10 +10:00
}
#define WARN(C) do { \
DEBUG_ONLY ( \
if (C) { \
std::cerr << __FILE__ \
<< ":" << __func__ \
<< ":" << __LINE__ \
<< ", " << #C \
<< '\n'; \
} \
); \
2018-04-23 15:39:07 +10:00
} while (0)
#define WARN_RETURN(CONDITION,VALUE) do { \
if (const auto& __warn_return = (CONDITION); !!__warn_return) { \
if constexpr (debug_enabled) { \
std::cerr << __FILE__ << ':' \
2018-04-23 15:39:07 +10:00
<< __LINE__ << ':' \
<< __PRETTY_FUNCTION__ << "; " \
<< #CONDITION << '\n'; \
breakpoint (); \
} \
\
return (VALUE); \
} \
2011-11-04 16:50:41 +11:00
} while (0)
#define RETURN_UNLESS(VALUE,CONDITION) do { \
if (const auto &__return_unless = (CONDITION); !__return_unless) { \
if constexpr (debug_enabled) { \
std::cerr << __FILE__ << ':' \
<< __LINE__ << ':' \
<< __PRETTY_FUNCTION__ << "; " \
<< #CONDITION << '\n'; \
breakpoint (); \
} \
\
return (VALUE); \
} \
} while (0)
///////////////////////////////////////////////////////////////////////////////
#ifdef COMPILER_GCC
#define CHECK(C) do { \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wnonnull-compare\"") \
DEBUG_ONLY ( \
if (!(C)) \
panic (#C); \
); \
_Pragma("GCC diagnostic pop") \
} while (0)
#else
#define CHECK(C) do { \
DEBUG_ONLY ( \
if (!(C)) \
panic (#C); \
); \
} while (0)
#endif
2011-05-23 17:18:52 +10:00
2016-06-24 15:30:41 +10:00
///////////////////////////////////////////////////////////////////////////////
#define SCOPED_SANITY(A) \
::cruft::debug::scoped_sanity PASTE(__scoped_sanity_checker,__LINE__) ((A)); \
(void)PASTE(__scoped_sanity_checker,__LINE__);
2016-06-24 15:30:41 +10:00
///////////////////////////////////////////////////////////////////////////////
#define CHECK_SANITY(A,...) CHECK(::cruft::debug::is_valid ((A) __VA_OPT__(,) __VA_ARGS__))
2014-04-16 19:16:48 +10:00
///////////////////////////////////////////////////////////////////////////////
2015-01-28 15:01:17 +11:00
#define CHECK_EQ(A,B) do { \
2016-12-21 20:22:11 +11:00
DEBUG_ONLY ( \
const auto &__a = (A); \
const auto &__b = (B); \
\
if (!::cruft::almost_equal (__a, __b)) { \
std::cerr << "expected equality\n" \
"__a: " #A " is " << __a << "\n" \
"__b: " #B " is " << __b << "\n"; \
breakpoint (); \
} \
2015-01-28 15:01:17 +11:00
); \
2011-05-23 17:18:52 +10:00
} while (0)
2014-04-16 19:16:48 +10:00
///////////////////////////////////////////////////////////////////////////////
#define CHECK_LT(A,B) do { \
DEBUG_ONLY ( \
const auto &__a = (A); \
const auto &__b = (B); \
\
if (__a >= __b) { \
std::cerr << "expected less than\n" \
"__a: " << #A << " is " << __a << "\n" \
"__b: " << #B << " is " << __b << "\n"; \
breakpoint (); \
}; \
); \
2012-11-09 15:11:18 +11:00
} while (0)
2014-04-16 19:16:48 +10:00
///////////////////////////////////////////////////////////////////////////////
#define CHECK_LE(A,B) do { \
DEBUG_ONLY ( \
const auto &__a = (A); \
const auto &__b = (B); \
\
if (!(__a <= __b)) { \
std::cerr << "expected less than or equal\n" \
"__a: " << #A << " is " << __a << "\n" \
"__b: " << #B << " is " << __b << "\n"; \
breakpoint (); \
} \
); \
2014-02-12 17:05:37 +11:00
} while (0)
2014-04-16 19:16:48 +10:00
///////////////////////////////////////////////////////////////////////////////
#define CHECK_GT(A,B) do { \
DEBUG_ONLY ( \
const auto &__a = (A); \
const auto &__b = (B); \
\
if (__a <= __b) { \
std::cerr << "expected greater than\n" \
"__a: " << #A << " is " << __a << "\n" \
"__b: " << #B << " is " << __b << "\n"; \
breakpoint (); \
} \
); \
2012-11-09 15:11:18 +11:00
} while (0)
2014-04-16 19:16:48 +10:00
///////////////////////////////////////////////////////////////////////////////
#define CHECK_GE(A,B) do { \
DEBUG_ONLY ( \
const auto &__a = (A); \
const auto &__b = (B); \
\
if (__a < __b) { \
std::cerr << "expected greater or equal\n" \
"__a: " << #A << " is " << __a << "\n" \
"__b: " << #B << " is " << __b << "\n"; \
breakpoint (); \
}; \
); \
2014-02-12 17:05:37 +11:00
} while (0)
2015-05-29 15:51:25 +10:00
///////////////////////////////////////////////////////////////////////////////
#define CHECK_LIMIT(V,L,H) do { \
DEBUG_ONLY ( \
const auto &__v = (V); \
const auto &__l = (L); \
const auto &__h = (H); \
\
if (__v < __l || __v > __h) { \
std::cerr << "expected limit\n" \
"__l: " << #L << " is " << +__l << "\n" \
"__h: " << #H << " is " << +__h << "\n" \
"__v: " << #V << " is " << +__v << "\n"; \
breakpoint (); \
}; \
); \
2015-05-29 15:51:25 +10:00
} while (0)
2014-04-16 19:16:48 +10:00
///////////////////////////////////////////////////////////////////////////////
#define CHECK_NEQ(A,B) do { \
DEBUG_ONLY( \
const auto &__a = (A); \
const auto &__b = (B); \
\
if (::cruft::almost_equal (__a, __b)) { \
std::cerr << "expected inequality\n" \
"__a: " << #A << " is " << __a << "\n" \
"__b: " << #B << " is " << __b << "\n"; \
breakpoint (); \
}; \
); \
2011-05-23 17:18:52 +10:00
} while (0)
///////////////////////////////////////////////////////////////////////////////
#define CHECK_ZERO(A) do { \
DEBUG_ONLY ( \
const auto &__a = (A); \
\
if (!::cruft::almost_zero (__a)) { \
std::cerr << "expected zero\n" \
"__a: " << #A << " is " << __a << "\n"; \
breakpoint (); \
}; \
); \
} while (0)
2015-02-05 20:35:11 +11:00
///////////////////////////////////////////////////////////////////////////////
#define CHECK_NEZ(A) do { \
DEBUG_ONLY ( \
const auto &__a = (A); \
\
if (::cruft::exactly_zero (__a)) { \
std::cerr << "expected non-zero\n" \
"__a: " << #A << " is " << __a << '\n'; \
breakpoint (); \
} \
); \
2015-02-05 20:35:11 +11:00
} while (0)
2016-08-03 18:12:05 +10:00
///////////////////////////////////////////////////////////////////////////////
#define CHECK_MOD(V,M) do { \
DEBUG_ONLY ( \
const auto &__check_mod_v = (V); \
const auto &__check_mod_m = (M); \
\
if (!::cruft::exactly_zero (__check_mod_v % __check_mod_m)) { \
std::cerr << "expected zero modulus\n" \
"__v: " << #V << " is " << __check_mod_v << "\n" \
"__m: " << #M << " is " << __check_mod_m << "\n"; \
breakpoint (); \
} \
); \
2016-08-03 18:12:05 +10:00
} while (0)
2016-02-05 14:27:58 +11:00
///////////////////////////////////////////////////////////////////////////////
#if defined(ENABLE_DEBUGGING)
#define CHECK_ENUM(C, ...) do { \
const auto &__c = (C); \
const auto &__e = { __VA_ARGS__ }; \
\
if (std::find (std::cbegin (__e), \
std::cend (__e), \
__c) == std::end (__e)) { \
std::cerr << "expect enum\n" \
"__c: " << #C << " is " << __c << '\n'; \
breakpoint (); \
} \
2016-02-05 14:27:58 +11:00
} while (0)
#else
#define CHECK_ENUM(C,...)
#endif
2018-04-13 18:49:36 +10:00
#if !defined(NDEBUG)
#define CHECK_FINITE(V) do { \
const auto &__v = (V); \
if (!std::isfinite (__v)) { \
std::cerr << "expected finite value\n" \
"__v: " << #V << " is " << __v << '\n'; \
breakpoint (); \
} \
2018-04-13 18:49:36 +10:00
} while (0)
#else
#define CHECK_FINITE(V,...)
#endif
2016-02-05 14:27:58 +11:00
2014-04-16 19:16:48 +10:00
///////////////////////////////////////////////////////////////////////////////
#define CHECK_THROWS(E,C) do { \
DEBUG_ONLY ( \
bool caught = false; \
\
try \
{ C; } \
catch (E const&) \
{ caught = true; } \
\
if (!caught) { \
std::cerr << "expected exception: " << #E << '\n' \
breakpoint (); \
} \
); \
2011-05-23 17:18:52 +10:00
} while (0)
2014-04-16 19:16:48 +10:00
///////////////////////////////////////////////////////////////////////////////
#define CHECK_NOTHROW(C) do { \
DEBUG_ONLY ( \
try { \
C; \
} catch (const std::exception &e) { \
std::cerr << "unexpected exception: " << e.what () << '\n'; \
breakpoint (); \
} catch (...) { \
std::cerr << "unexpected exception: unknown\n"; \
breakpoint (); \
} \
); \
2012-06-20 16:47:48 +10:00
} while (0)
///////////////////////////////////////////////////////////////////////////////
/// make the compiler think a particular variable may now be aliased somewhere.
///
/// useful for preventing optimisations eliding a variable.
///
/// stolen from Chandler Carruth's 2015 talk: "Tuning C++".
namespace cruft::debug {
template <class T>
inline T*
escape (T *t)
{
asm volatile ("": : "g"(t): "memory");
return t;
}
template <class T>
inline const T*
escape (const T *t)
{
asm volatile ("": : "g"(t): "memory");
return t;
}
template <class T>
inline const T&
escape (const T &t)
{
return *escape (&t);
}
template <class T>
inline T&
escape (T &t)
{
return *escape (&t);
}
template <typename T, typename ...Args>
inline void
escape (T t, Args ...args)
{
escape (t);
escape (args...);
}
}
///////////////////////////////////////////////////////////////////////////////
/// force the compiler to conceptually dirty the global memory space.
///
/// stolen from Chandler Carruth's 2015 talk: "Tuning C++".
namespace cruft::debug {
inline void
clobber (void)
{
asm volatile ("": : : "memory");
}
}
void breakpoint (void);
///////////////////////////////////////////////////////////////////////////////
void warn (void);
void warn (const std::string&);
void warn (const char *);
2011-05-23 17:18:52 +10:00
2014-04-16 19:16:48 +10:00
///////////////////////////////////////////////////////////////////////////////
void await_debugger (void);
void prepare_debugger (void);
void force_console (void);
2014-04-16 19:16:48 +10:00
///////////////////////////////////////////////////////////////////////////////
void enable_fpe (void);
void disable_fpe (void);
2014-04-16 19:16:48 +10:00
///////////////////////////////////////////////////////////////////////////////
namespace cruft::debug {
void init (void);
2015-02-13 17:30:19 +11:00
///////////////////////////////////////////////////////////////////////////
// returns true if an instance of type `T' appears to be in a valid state.
//
// written as a struct rather than a function so that behaviour may be
// partially specialised. all users are free to specialise this struct
// with an user types.
//
// all specialisations must be safe to call on arbitrary data without
// exceptions or faults as this mechanism is used to control some
// debugging paths which themselves are the configuration points for
// throwing/logging/etc behaviour.
//
// ArgsT is an optional set of auxiliary values that are required to
// validate the target value.
template <typename T, typename ...ArgsT>
struct validator {
static bool is_valid (T const&, ArgsT const&...) noexcept;
};
//-------------------------------------------------------------------------
template <typename T, typename ...ArgsT>
bool is_valid (const T &t, const ArgsT &...args) noexcept
{
return validator<T,ArgsT...>::is_valid (t, args...);
}
2015-02-13 17:30:19 +11:00
//-------------------------------------------------------------------------
// forwarding validator from a pointer type to a reference type.
//
// null pointers are assumed to be invalid
template <typename T, typename ...ArgsT>
struct validator<T*,ArgsT...> {
static bool is_valid (const T *val, ArgsT const &...args) noexcept
{
return val && ::cruft::debug::is_valid (*val, args...);
}
};
2015-02-13 17:30:19 +11:00
///////////////////////////////////////////////////////////////////////////
// asserts that an instance of type `T' is in a valid state.
//
// behaviour will be controlled by NDEBUG and other assertion machinery and
// so may be optimised out entirely in optimised builds.
2015-02-13 17:30:19 +11:00
template <typename T>
void sanity (const T &t)
{
(void)t;
CHECK (is_valid (t));
}
2015-02-13 17:30:19 +11:00
//-------------------------------------------------------------------------
template <
template<typename...> class T,
typename ...Args
>
2015-02-13 17:30:19 +11:00
void sanity (const T<Args...> &t)
{
(void)t;
CHECK (is_valid (t));
}
template <typename ValueT>
class scoped_sanity {
public:
scoped_sanity (ValueT &_value):
m_value (_value)
{
sanity (m_value);
}
~scoped_sanity ()
{
sanity (m_value);
}
private:
const ValueT& m_value;
};
};
///////////////////////////////////////////////////////////////////////////////
// XXX: maths needs to be included so that CHECK_EQ/NEQ can call almost_equal,
// but maths.hpp might be using CHECK_ macros so we must include maths.hpp
// after we define the CHECK_ macros so the preprocessor can resolve them.
#include "maths.hpp"