2015-10-29 10:43:41 +11: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
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2018-01-09 16:28:25 +11:00
|
|
|
* Copyright 2015-2018, Danny Robson <danny@nerdcruft.net>
|
2015-10-29 10:43:41 +11:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef __UTIL_DEBUG_IPP
|
|
|
|
#error
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define __UTIL_DEBUG_IPP
|
|
|
|
|
2018-01-09 16:28:25 +11:00
|
|
|
#include "backtrace.hpp"
|
|
|
|
#include "format.hpp"
|
2016-02-03 12:04:47 +11:00
|
|
|
|
2015-10-29 10:43:41 +11:00
|
|
|
#include <limits>
|
2018-01-09 16:28:25 +11:00
|
|
|
#include <iostream>
|
|
|
|
|
2015-10-29 10:43:41 +11:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2017-01-05 15:06:49 +11:00
|
|
|
namespace util::debug::detail {
|
2015-10-29 10:43:41 +11:00
|
|
|
void panic [[noreturn]] (const char *msg);
|
2016-02-03 12:04:47 +11:00
|
|
|
|
2016-07-28 13:36:23 +10:00
|
|
|
template <typename ...Args, size_t N>
|
|
|
|
void panic [[noreturn]] (const char (&fmt)[N], const Args& ...args)
|
|
|
|
{
|
2018-01-10 17:19:39 +11:00
|
|
|
std::cerr << ::util::format::printf (fmt, args...) << ::debug::backtrace () << std::endl;
|
2018-01-09 16:28:25 +11:00
|
|
|
breakpoint ();
|
|
|
|
abort ();
|
2016-07-28 13:36:23 +10:00
|
|
|
}
|
2016-02-03 12:04:47 +11:00
|
|
|
|
2015-10-29 10:43:41 +11:00
|
|
|
void not_implemented [[noreturn]] (const char *msg);
|
|
|
|
void unreachable [[noreturn]] (const char *msg);
|
2017-01-05 15:06:49 +11:00
|
|
|
}
|
2015-10-29 10:43:41 +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]] (void)
|
|
|
|
{
|
|
|
|
not_implemented ("operation not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
constexpr void not_implemented [[noreturn]] (const char *msg)
|
|
|
|
{
|
|
|
|
! msg
|
|
|
|
? not_implemented (msg)
|
|
|
|
: util::debug::detail::not_implemented (msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
constexpr void unreachable [[noreturn]] (void)
|
|
|
|
{
|
|
|
|
unreachable ("unreachable code executed");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
constexpr void unreachable [[noreturn]] (const char *msg)
|
|
|
|
{
|
|
|
|
! msg
|
|
|
|
? unreachable (msg)
|
|
|
|
: util::debug::detail::unreachable (msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
constexpr void panic [[noreturn]] (const char *msg)
|
|
|
|
{
|
|
|
|
! msg
|
|
|
|
? panic (msg)
|
|
|
|
: util::debug::detail::panic (msg);
|
|
|
|
}
|
2016-02-03 12:04:47 +11:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-07-28 13:36:23 +10:00
|
|
|
template <typename ...Args, size_t N>
|
2016-02-03 12:04:47 +11:00
|
|
|
constexpr
|
|
|
|
void
|
2016-07-28 13:36:23 +10:00
|
|
|
panic [[noreturn]] (const char (&fmt)[N], const Args& ...args)
|
2016-02-03 12:04:47 +11:00
|
|
|
{
|
2016-08-01 17:23:53 +10:00
|
|
|
util::debug::detail::panic (fmt, args...);
|
2016-02-03 12:04:47 +11:00
|
|
|
}
|