2011-05-23 17:18:52 +10:00
|
|
|
/*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-05-23 17:18:52 +10:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2011-05-23 17:18:52 +10:00
|
|
|
*
|
2017-08-02 16:17:24 +10:00
|
|
|
* Copyright 2010-2017 Danny Robson <danny@nerdcruft.net>
|
2011-05-23 17:18:52 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __DEBUG_HPP
|
|
|
|
#define __DEBUG_HPP
|
|
|
|
|
2015-02-05 20:35:25 +11:00
|
|
|
//#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>
|
2018-03-22 16:10:06 +11:00
|
|
|
#include <iostream>
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2016-02-03 12:04:47 +11:00
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2017-09-13 12:37:53 +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.
|
2016-09-28 17:20:46 +10:00
|
|
|
#if !defined(NDEBUG)
|
2017-09-13 12:37:53 +10:00
|
|
|
constexpr bool debug_enabled = true;
|
2018-04-05 12:13:37 +10:00
|
|
|
constexpr bool assertions_enabled = true;
|
2012-05-03 18:11:17 +10:00
|
|
|
#else
|
2017-09-13 12:37:53 +10:00
|
|
|
constexpr bool debug_enabled = false;
|
2018-04-05 12:13:37 +10:00
|
|
|
constexpr bool assertions_enabled = false;
|
2012-05-03 18:11:17 +10:00
|
|
|
#endif
|
2012-04-12 14:06:59 +10:00
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
|
2017-09-13 12:37:53 +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
|
|
|
|
#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)
|
2017-09-13 12:37:53 +10:00
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#define EXIT_XSUCCESS 0
|
|
|
|
#define EXIT_XSKIP 77
|
|
|
|
#define EXIT_XHARD_ERROR 99
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-01-28 15:01:17 +11:00
|
|
|
#define TRACE { \
|
2016-12-21 20:22:11 +11:00
|
|
|
DEBUG_ONLY ( \
|
2018-03-22 13:20:23 +11:00
|
|
|
std::clog << "tid: " << std::this_thread::get_id () << "; " << __FILE__ << ":" << __func__ << ":" << __LINE__ << std::endl; \
|
2015-01-28 15:01:17 +11:00
|
|
|
); \
|
2011-07-16 14:47:10 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-28 15:01:17 +11:00
|
|
|
#define WARN(C) do { \
|
2016-12-21 20:22:11 +11:00
|
|
|
DEBUG_ONLY ( \
|
2015-01-28 15:01:17 +11:00
|
|
|
if (C) { \
|
|
|
|
std::cerr << __FILE__ << ":" << __func__ << ":" << __LINE__ << ", " << #C << std::endl; \
|
|
|
|
} \
|
|
|
|
); \
|
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::clog << __FILE__ << ':' \
|
|
|
|
<< __LINE__ << ':' \
|
|
|
|
<< __PRETTY_FUNCTION__ << "; " \
|
|
|
|
<< #CONDITION << '\n'; \
|
|
|
|
breakpoint (); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
return (VALUE); \
|
|
|
|
} \
|
2011-11-04 16:50:41 +11:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2018-04-24 15:11:18 +10:00
|
|
|
#define RETURN_UNLESS(VALUE,CONDITION) do { \
|
|
|
|
if (const auto &__return_unless = (CONDITION); !__return_unless) { \
|
|
|
|
if constexpr (debug_enabled) { \
|
|
|
|
std::clog << __FILE__ << ':' \
|
|
|
|
<< __LINE__ << ':' \
|
|
|
|
<< __PRETTY_FUNCTION__ << "; " \
|
|
|
|
<< #CONDITION << '\n'; \
|
|
|
|
breakpoint (); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
return (VALUE); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-02-03 12:04:47 +11:00
|
|
|
#define _CHECK_PANIC(FMT,...) do { \
|
|
|
|
panic ("%s:%s:%i:%s\n" FMT, \
|
2016-08-03 18:11:04 +10:00
|
|
|
PACKAGE, __FILE__, __LINE__, __func__, \
|
2016-02-03 12:04:47 +11:00
|
|
|
__VA_ARGS__); \
|
|
|
|
} while(0)
|
2011-05-23 17:18:52 +10:00
|
|
|
|
|
|
|
|
2015-01-28 14:49:34 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-02-03 12:04:47 +11:00
|
|
|
#define CHECK(C) do { \
|
2016-12-21 20:22:11 +11:00
|
|
|
DEBUG_ONLY ( \
|
2016-02-03 12:04:47 +11:00
|
|
|
if (!(C)) \
|
2016-04-19 14:50:13 +10:00
|
|
|
panic (#C); \
|
2016-02-03 12:04:47 +11:00
|
|
|
); \
|
|
|
|
} while (0)
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2016-06-24 15:30:41 +10:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-04-18 21:43:58 +10:00
|
|
|
#define CHECK_SANITY(A) \
|
|
|
|
::util::debug::scoped_sanity PASTE(__scoped_sanity_checker,__LINE__) (A); \
|
|
|
|
(void)PASTE(__scoped_sanity_checker,__LINE__);
|
2016-06-24 15:30:41 +10:00
|
|
|
|
|
|
|
|
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 ( \
|
2016-08-03 18:11:29 +10:00
|
|
|
const auto &__a = (A); \
|
|
|
|
const auto &__b = (B); \
|
2016-02-03 12:04:47 +11:00
|
|
|
\
|
2017-05-18 18:20:19 +10:00
|
|
|
if (!::util::almost_equal (__a, __b)) { \
|
2016-02-03 12:04:47 +11:00
|
|
|
_CHECK_PANIC("expected equality\n" \
|
|
|
|
"__a: %s is %!\n" \
|
|
|
|
"__b: %s is %!\n", \
|
|
|
|
#A, __a, \
|
|
|
|
#B, __b); \
|
|
|
|
} \
|
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
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-01-28 15:01:17 +11:00
|
|
|
#define CHECK_LT(A,B) do { \
|
2016-12-21 20:22:11 +11:00
|
|
|
DEBUG_ONLY ( \
|
2016-08-03 18:11:29 +10:00
|
|
|
const auto &__a = (A); \
|
|
|
|
const auto &__b = (B); \
|
2016-02-03 12:04:47 +11:00
|
|
|
\
|
|
|
|
if (__a >= __b) { \
|
|
|
|
_CHECK_PANIC("expected less than\n" \
|
|
|
|
"__a: %s is %!\n" \
|
|
|
|
"__b: %s is %!\n", \
|
|
|
|
#A, __a, \
|
|
|
|
#B, __b); \
|
|
|
|
}; \
|
2015-01-28 15:01:17 +11:00
|
|
|
); \
|
2012-11-09 15:11:18 +11:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-01-28 15:01:17 +11:00
|
|
|
#define CHECK_LE(A,B) do { \
|
2016-12-21 20:22:11 +11:00
|
|
|
DEBUG_ONLY ( \
|
2016-08-03 18:11:29 +10:00
|
|
|
const auto &__a = (A); \
|
|
|
|
const auto &__b = (B); \
|
2016-02-03 12:04:47 +11:00
|
|
|
\
|
|
|
|
if (__a > __b) { \
|
|
|
|
_CHECK_PANIC("expected less than or equal\n" \
|
|
|
|
"__a: %s is %!\n" \
|
|
|
|
"__b: %s is %!\n", \
|
|
|
|
#A, __a, \
|
|
|
|
#B, __b); \
|
|
|
|
} \
|
2015-01-28 15:01:17 +11:00
|
|
|
); \
|
2014-02-12 17:05:37 +11:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-01-28 15:01:17 +11:00
|
|
|
#define CHECK_GT(A,B) do { \
|
2016-12-21 20:22:11 +11:00
|
|
|
DEBUG_ONLY ( \
|
2016-08-03 18:11:29 +10:00
|
|
|
const auto &__a = (A); \
|
|
|
|
const auto &__b = (B); \
|
2016-02-03 12:04:47 +11:00
|
|
|
\
|
|
|
|
if (__a <= __b) { \
|
|
|
|
_CHECK_PANIC ("expected greater than\n" \
|
|
|
|
"__a: %s is %!\n" \
|
|
|
|
"__b: %s is %!\n", \
|
|
|
|
#A, __a, \
|
|
|
|
#B, __b); \
|
|
|
|
} \
|
2015-01-28 15:01:17 +11:00
|
|
|
); \
|
2012-11-09 15:11:18 +11:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-01-28 15:01:17 +11:00
|
|
|
#define CHECK_GE(A,B) do { \
|
2016-12-21 20:22:11 +11:00
|
|
|
DEBUG_ONLY ( \
|
2016-08-03 18:11:29 +10:00
|
|
|
const auto &__a = (A); \
|
|
|
|
const auto &__b = (B); \
|
2016-02-03 12:04:47 +11:00
|
|
|
\
|
|
|
|
if (__a < __b) { \
|
|
|
|
_CHECK_PANIC ("expected greater or equal\n" \
|
|
|
|
"__a: %s is %!\n" \
|
|
|
|
"__b: %s is %!\n", \
|
|
|
|
#A, __a, \
|
|
|
|
#B, __b); \
|
|
|
|
}; \
|
2015-01-28 15:01:17 +11:00
|
|
|
); \
|
2014-02-12 17:05:37 +11:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2015-05-29 15:51:25 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-02-03 12:04:47 +11:00
|
|
|
#define CHECK_LIMIT(V,L,H) do { \
|
2016-12-21 20:22:11 +11:00
|
|
|
DEBUG_ONLY ( \
|
2016-08-03 18:11:29 +10:00
|
|
|
const auto &__v = (V); \
|
|
|
|
const auto &__l = (L); \
|
|
|
|
const auto &__h = (H); \
|
2016-02-03 12:04:47 +11:00
|
|
|
\
|
|
|
|
if (__v < __l || __v > __h) { \
|
|
|
|
_CHECK_PANIC ("expected limit\n" \
|
|
|
|
"__l: %s is %!\n" \
|
|
|
|
"__h: %s is %!\n" \
|
|
|
|
"__v: %s is %!\n", \
|
|
|
|
#L, __l, \
|
2016-12-21 20:22:20 +11:00
|
|
|
#H, __h, \
|
2016-02-03 12:04:47 +11:00
|
|
|
#V, __v); \
|
|
|
|
}; \
|
|
|
|
); \
|
2015-05-29 15:51:25 +10:00
|
|
|
} while (0)
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-01-28 15:01:17 +11:00
|
|
|
#define CHECK_NEQ(A,B) do { \
|
2016-12-21 20:22:11 +11:00
|
|
|
DEBUG_ONLY( \
|
2016-08-03 18:11:29 +10:00
|
|
|
const auto &__a = (A); \
|
|
|
|
const auto &__b = (B); \
|
2016-02-03 12:04:47 +11:00
|
|
|
\
|
2017-05-18 18:20:19 +10:00
|
|
|
if (::util::almost_equal (__a, __b)) { \
|
2016-02-03 12:04:47 +11:00
|
|
|
_CHECK_PANIC ("expected inequality\n" \
|
2016-06-20 13:07:10 +10:00
|
|
|
"__a: %s is %!\n" \
|
|
|
|
"__b: %s is %!\n", \
|
2016-02-03 12:04:47 +11:00
|
|
|
#A, __a, \
|
|
|
|
#B, __b); \
|
|
|
|
}; \
|
2015-01-28 15:01:17 +11:00
|
|
|
); \
|
2011-05-23 17:18:52 +10:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2015-02-13 18:02:09 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#define CHECK_ZERO(A) do { \
|
2016-12-21 20:22:11 +11:00
|
|
|
DEBUG_ONLY ( \
|
2016-08-03 18:11:29 +10:00
|
|
|
const auto &__a = (A); \
|
2016-02-03 12:04:47 +11:00
|
|
|
\
|
2017-05-18 18:20:19 +10:00
|
|
|
if (!::util::almost_zero (__a)) { \
|
2016-02-03 12:04:47 +11:00
|
|
|
_CHECK_PANIC ("expected zero\n" \
|
2016-06-20 13:07:10 +10:00
|
|
|
"__a: %s is %!\n" \
|
2016-02-03 12:04:47 +11:00
|
|
|
#A, __a); \
|
|
|
|
}; \
|
2015-02-13 18:02:09 +11:00
|
|
|
); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2015-02-05 20:35:11 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#define CHECK_NEZ(A) do { \
|
2016-12-21 20:22:11 +11:00
|
|
|
DEBUG_ONLY ( \
|
2016-08-03 18:11:29 +10:00
|
|
|
const auto &__a = (A); \
|
2017-08-18 11:15:00 +10:00
|
|
|
\
|
2017-05-18 18:20:19 +10:00
|
|
|
if (::util::exactly_zero (__a)) \
|
2017-08-02 16:17:24 +10:00
|
|
|
_CHECK_PANIC ("expected non-zero\n" \
|
2016-02-03 12:04:47 +11:00
|
|
|
"__a: %s is %!", \
|
|
|
|
#A, __a); \
|
2015-02-05 20:35:11 +11:00
|
|
|
); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2016-08-03 18:12:05 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#define CHECK_MOD(V,M) do { \
|
2016-12-21 20:22:11 +11:00
|
|
|
DEBUG_ONLY ( \
|
2016-08-03 18:12:05 +10:00
|
|
|
const auto &__check_mod_v = (V); \
|
|
|
|
const auto &__check_mod_m = (M); \
|
2017-08-18 11:15:00 +10:00
|
|
|
\
|
2017-05-18 18:20:19 +10:00
|
|
|
if (!::util::exactly_zero (__check_mod_v % __check_mod_m)) \
|
2016-08-03 18:12:05 +10:00
|
|
|
_CHECK_PANIC ("expected zero modulus\n" \
|
|
|
|
"__v: %s is %!\n" \
|
|
|
|
"__m: %s is %!", \
|
|
|
|
#V, __check_mod_v, \
|
|
|
|
#M, __check_mod_m); \
|
|
|
|
); \
|
|
|
|
} 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), \
|
2016-02-12 12:57:23 +11:00
|
|
|
__c) == std::end (__e)) { \
|
2016-02-05 14:27:58 +11:00
|
|
|
_CHECK_PANIC("expect enum\n" \
|
2016-02-12 12:57:53 +11:00
|
|
|
"__c: %s is %!", \
|
2016-02-05 14:27:58 +11:00
|
|
|
#C, __c); \
|
|
|
|
} \
|
|
|
|
} 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)) { \
|
|
|
|
_CHECK_PANIC ("expected finite value\n" \
|
|
|
|
"__v: %! is %!", \
|
|
|
|
#V, __v); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#else
|
|
|
|
#define CHECK_FINITE(V,...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-05 14:27:58 +11:00
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-02-03 12:04:47 +11:00
|
|
|
#define CHECK_THROWS(E,C) do { \
|
2016-12-21 20:22:11 +11:00
|
|
|
DEBUG_ONLY ( \
|
2016-02-03 12:04:47 +11:00
|
|
|
bool caught = false; \
|
|
|
|
\
|
|
|
|
try \
|
|
|
|
{ C; } \
|
|
|
|
catch (E) \
|
|
|
|
{ caught = true; } \
|
|
|
|
\
|
|
|
|
if (!caught) \
|
|
|
|
_CHECK_PANIC ("expected exception: %s", #E); \
|
|
|
|
); \
|
2011-05-23 17:18:52 +10:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-02-03 12:04:47 +11:00
|
|
|
#define CHECK_NOTHROW(C) do { \
|
2016-12-21 20:22:11 +11:00
|
|
|
DEBUG_ONLY ( \
|
2016-02-03 12:04:47 +11:00
|
|
|
try { \
|
|
|
|
C; \
|
|
|
|
} catch (const std::exception &e) { \
|
|
|
|
_CHECK_PANIC ("unexpected exception: %s", \
|
|
|
|
e.what ()); \
|
|
|
|
} catch (...) { \
|
|
|
|
_CHECK_PANIC ("unexpected exception: %s", \
|
|
|
|
"unknown"); \
|
|
|
|
} \
|
|
|
|
); \
|
2012-06-20 16:47:48 +10:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2017-08-18 11:15:00 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// 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 util::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 util::debug {
|
|
|
|
inline void
|
|
|
|
clobber (void)
|
|
|
|
{
|
|
|
|
asm volatile ("": : : "memory");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
void breakpoint (void);
|
|
|
|
|
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-02-28 11:49:13 +11:00
|
|
|
#include "log.hpp"
|
|
|
|
#include "backtrace.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
namespace util::debug::detail {
|
|
|
|
void panic [[noreturn]] (const char *msg);
|
|
|
|
|
|
|
|
template <typename ...Args, size_t N>
|
|
|
|
void panic [[noreturn]] (const char (&fmt)[N], const Args& ...args)
|
|
|
|
{
|
|
|
|
LOG_EMERGENCY (fmt, args...);
|
|
|
|
LOG_EMERGENCY ("%!", ::debug::backtrace ());
|
|
|
|
breakpoint ();
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void not_implemented [[noreturn]] (const char *msg);
|
|
|
|
void unreachable [[noreturn]] (const char *msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constexpr void
|
|
|
|
panic [[noreturn]] (const char *msg)
|
|
|
|
{
|
|
|
|
! msg
|
|
|
|
? panic (msg)
|
|
|
|
: util::debug::detail::panic (msg);
|
|
|
|
}
|
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
|
2016-07-28 13:36:23 +10:00
|
|
|
template <typename ...Args, size_t N>
|
2018-02-28 11:49:13 +11:00
|
|
|
constexpr void
|
|
|
|
panic [[noreturn]] (const char (&fmt)[N], const Args&... args)
|
|
|
|
{
|
|
|
|
util::debug::detail::panic (fmt, args...);
|
|
|
|
}
|
2016-02-03 12:04:47 +11:00
|
|
|
|
2012-08-10 17:40:19 +10:00
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-02-28 11:49:13 +11:00
|
|
|
// not_implemented/unreachable/panic must be callable from constexpr contexts.
|
|
|
|
// but they rely on functions that aren't constexpr to perform the controlled
|
|
|
|
// abort.
|
|
|
|
//
|
|
|
|
// we can work around this in the same way assert does by using a conditional
|
|
|
|
// that hides an extern function that actually does the work. as we can't
|
|
|
|
// utilise external state this has to be the message variable which will
|
|
|
|
// assume isn't ever null.
|
|
|
|
//
|
|
|
|
// to avoid warnings about a return from a noreturn function we recurse into
|
|
|
|
// ourself in the alternate case. this branch shouldn't ever be taken, but if
|
|
|
|
// it is we were planning on crashing anyway...
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
constexpr void
|
|
|
|
not_implemented [[noreturn]] (const char *msg)
|
|
|
|
{
|
|
|
|
! msg
|
|
|
|
? not_implemented (msg)
|
|
|
|
: util::debug::detail::not_implemented (msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
constexpr void
|
|
|
|
not_implemented [[noreturn]] (void)
|
|
|
|
{
|
|
|
|
not_implemented ("operation not implemented");
|
|
|
|
}
|
|
|
|
|
2012-06-04 14:50:58 +10:00
|
|
|
|
2016-02-10 13:05:53 +11:00
|
|
|
constexpr void unimplemented [[noreturn]] (void) { not_implemented (); }
|
2018-02-28 11:49:13 +11:00
|
|
|
|
|
|
|
|
2016-02-10 13:05:53 +11:00
|
|
|
constexpr void unimplemented [[noreturn]] (const char *msg) { not_implemented (msg); }
|
|
|
|
|
2012-08-10 17:40:19 +10:00
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-02-28 11:49:13 +11:00
|
|
|
constexpr void
|
|
|
|
unreachable [[noreturn]] (const char *msg)
|
|
|
|
{
|
|
|
|
! msg
|
|
|
|
? unreachable (msg)
|
|
|
|
: util::debug::detail::unreachable (msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
constexpr void
|
|
|
|
unreachable [[noreturn]] (void)
|
|
|
|
{
|
|
|
|
unreachable ("unreachable code executed");
|
|
|
|
}
|
2015-10-29 10:43:41 +11:00
|
|
|
|
2016-01-20 16:38:01 +11:00
|
|
|
|
2017-09-17 12:41:23 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// report a fatal error induced by an unhandled value, especially in switch
|
|
|
|
/// statements. will almost invariably abort the application.
|
|
|
|
template <typename T>
|
|
|
|
constexpr void
|
|
|
|
unhandled [[noreturn]] (T &&t) noexcept
|
|
|
|
{
|
|
|
|
panic ("unhandled value %!", std::forward<T> (t));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-20 16:38:01 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
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
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-02-15 17:21:01 +11:00
|
|
|
void await_debugger (void);
|
|
|
|
void prepare_debugger (void);
|
|
|
|
|
|
|
|
void force_console (void);
|
2012-05-01 12:14:25 +10:00
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-05-01 12:14:25 +10:00
|
|
|
void enable_fpe (void);
|
|
|
|
void disable_fpe (void);
|
|
|
|
|
2012-08-10 17:40:19 +10:00
|
|
|
|
2014-04-16 19:16:48 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2017-01-05 15:06:49 +11:00
|
|
|
namespace util::debug {
|
2012-08-10 17:40:19 +10:00
|
|
|
void init (void);
|
2015-02-13 17:30:19 +11:00
|
|
|
|
|
|
|
|
2018-01-01 13:40:34 +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
|
|
|
|
// arbitrarily 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.
|
2015-02-13 17:30:19 +11:00
|
|
|
template <typename T>
|
2016-08-10 17:36:25 +10:00
|
|
|
struct validator {
|
2018-01-01 13:40:34 +11:00
|
|
|
static bool is_valid (const T&) noexcept;
|
2016-08-10 17:36:25 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-01-01 13:40:34 +11:00
|
|
|
//-------------------------------------------------------------------------
|
2016-08-10 17:36:25 +10:00
|
|
|
template <typename T>
|
2018-01-01 13:40:34 +11:00
|
|
|
bool is_valid (const T &t) noexcept
|
|
|
|
{
|
|
|
|
return validator<T>::is_valid (t);
|
|
|
|
}
|
2015-02-13 17:30:19 +11:00
|
|
|
|
|
|
|
|
2018-01-01 13:40:34 +11:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// forwarding validator from a pointer type to a reference type.
|
|
|
|
//
|
|
|
|
// null pointers are assumed to be invalid
|
|
|
|
template <typename T>
|
|
|
|
struct validator<T*> {
|
|
|
|
static bool is_valid (const T *val) noexcept
|
|
|
|
{
|
|
|
|
return val && ::util::debug::is_valid (*val);
|
|
|
|
}
|
|
|
|
};
|
2015-02-13 17:30:19 +11:00
|
|
|
|
|
|
|
|
2018-01-01 13:40:34 +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)
|
2015-04-30 17:31:19 +10:00
|
|
|
{
|
|
|
|
(void)t;
|
2016-08-10 17:36:25 +10:00
|
|
|
CHECK (is_valid (t));
|
2015-04-30 17:31:19 +10:00
|
|
|
}
|
2015-02-13 17:30:19 +11:00
|
|
|
|
|
|
|
|
2018-01-01 13:40:34 +11:00
|
|
|
//-------------------------------------------------------------------------
|
2015-03-03 04:13:29 +11:00
|
|
|
template <
|
|
|
|
template<typename...> class T,
|
|
|
|
typename ...Args
|
|
|
|
>
|
2015-02-13 17:30:19 +11:00
|
|
|
void sanity (const T<Args...> &t)
|
2015-04-30 17:31:19 +10:00
|
|
|
{
|
|
|
|
(void)t;
|
2016-08-10 17:36:25 +10:00
|
|
|
CHECK (is_valid (t));
|
2015-04-30 17:31:19 +10:00
|
|
|
}
|
2018-04-18 21:43:58 +10:00
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
};
|
2012-08-10 17:40:19 +10:00
|
|
|
|
2015-02-05 20:35:25 +11:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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.
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "maths.hpp"
|
2015-10-29 10:43:41 +11:00
|
|
|
|
2015-02-05 20:35:25 +11:00
|
|
|
|
2011-05-23 17:18:52 +10:00
|
|
|
#endif // __DEBUG_HPP
|