2020-08-15 11:31:16 +00:00
|
|
|
#ifndef ARBUTILS_EXCEPTION_HPP
|
|
|
|
#define ARBUTILS_EXCEPTION_HPP
|
|
|
|
|
2020-08-15 12:22:32 +00:00
|
|
|
#if !WINDOWS
|
2020-08-17 18:08:23 +00:00
|
|
|
#if PRETTYTRACES
|
2021-03-26 12:37:01 +00:00
|
|
|
#define BACKWARD_HAS_BFD 1
|
2020-08-17 18:08:23 +00:00
|
|
|
#endif
|
2020-08-15 11:31:16 +00:00
|
|
|
#include "../extern/backward.hpp"
|
2020-08-15 12:22:32 +00:00
|
|
|
#endif
|
2020-08-15 11:31:16 +00:00
|
|
|
|
|
|
|
namespace ArbUt {
|
2020-09-22 17:32:40 +00:00
|
|
|
/// @brief Implementation of std::logic_error that gives a stack trace when thrown.
|
2020-08-15 12:50:17 +00:00
|
|
|
class Exception : std::logic_error {
|
2020-08-15 12:22:32 +00:00
|
|
|
#if !WINDOWS
|
2020-08-15 11:31:16 +00:00
|
|
|
backward::StackTrace _stack;
|
2020-08-15 12:22:32 +00:00
|
|
|
#endif
|
2020-08-15 11:31:16 +00:00
|
|
|
|
|
|
|
public:
|
2020-09-22 17:32:40 +00:00
|
|
|
/// @brief Throw an exception with specified message.
|
2020-08-15 12:50:17 +00:00
|
|
|
explicit Exception(const std::string& msg) : std::logic_error(msg) {
|
2020-08-15 12:25:27 +00:00
|
|
|
#if !WINDOWS
|
2020-08-16 14:59:13 +00:00
|
|
|
_stack.load_here(32);
|
2020-12-23 11:04:20 +00:00
|
|
|
#if DEBUG
|
|
|
|
_stack.skip_n_firsts(2);
|
|
|
|
#else
|
|
|
|
_stack.skip_n_firsts(0);
|
|
|
|
#endif
|
2020-08-15 12:25:27 +00:00
|
|
|
#endif
|
|
|
|
}
|
2020-08-15 11:31:16 +00:00
|
|
|
|
2020-09-22 17:32:40 +00:00
|
|
|
/// @brief Copy operator.
|
2020-08-15 14:14:33 +00:00
|
|
|
Exception(const Exception& e) noexcept
|
|
|
|
: std::logic_error(e.what())
|
|
|
|
#if !WINDOWS
|
|
|
|
,
|
|
|
|
_stack(e._stack)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
}
|
2020-08-15 14:14:06 +00:00
|
|
|
|
2020-09-22 17:32:40 +00:00
|
|
|
/// @brief Returns the error message of the exception.
|
2020-10-15 15:54:06 +00:00
|
|
|
[[nodiscard]] const char* what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW final {
|
2020-08-16 17:45:19 +00:00
|
|
|
return logic_error::what();
|
|
|
|
}
|
2020-08-15 11:31:16 +00:00
|
|
|
|
2020-09-22 17:32:40 +00:00
|
|
|
/// @brief Returns the stacktrace of the exception.
|
|
|
|
/// @param depth The max number of stacks it should retrieve.
|
2020-09-24 19:02:22 +00:00
|
|
|
/// @param include_addr Whether or not the address should be shown if the line information could not be
|
|
|
|
/// retrieved.
|
2020-08-15 12:25:27 +00:00
|
|
|
[[nodiscard]] std::string GetStacktrace([[maybe_unused]] size_t depth = 6,
|
|
|
|
[[maybe_unused]] bool include_addr = true) const {
|
2020-08-15 12:22:32 +00:00
|
|
|
#if !WINDOWS
|
2020-08-18 16:41:18 +00:00
|
|
|
return BuildStacktraceFromStack(_stack, depth, include_addr);
|
|
|
|
#else
|
|
|
|
return "Stack trace not available on Windows.";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !WINDOWS
|
2020-09-22 17:32:40 +00:00
|
|
|
/// @brief Builds a string from a given stack.
|
|
|
|
/// @param stack The stack to build the string from.
|
|
|
|
/// @param depth The max number of stacks it should retrieve.
|
2020-09-24 19:02:22 +00:00
|
|
|
/// @param include_addr Whether or not the address should be shown if the line information could not be
|
|
|
|
/// retrieved.
|
2020-08-18 16:41:18 +00:00
|
|
|
static std::string BuildStacktraceFromStack(const backward::StackTrace& stack, size_t depth = 6,
|
|
|
|
bool include_addr = true) {
|
|
|
|
if (stack.size() == 0) {
|
2020-08-16 08:41:10 +00:00
|
|
|
return "No stack trace could be retrieved.";
|
|
|
|
}
|
2020-08-15 11:31:16 +00:00
|
|
|
backward::TraceResolver tr;
|
2020-08-16 17:15:45 +00:00
|
|
|
backward::SnippetFactory snippetFactory;
|
2020-08-18 16:41:18 +00:00
|
|
|
tr.load_stacktrace(stack);
|
2020-08-15 11:31:16 +00:00
|
|
|
std::stringstream ss;
|
2020-08-16 14:59:13 +00:00
|
|
|
ss << "Stacktrace with depth " << depth << ": " << std::endl;
|
2020-08-16 17:15:45 +00:00
|
|
|
size_t framesAppended = 0;
|
2020-12-23 11:04:20 +00:00
|
|
|
for (size_t i = 0; i < stack.size() && framesAppended < depth; ++i) {
|
2020-08-18 16:41:18 +00:00
|
|
|
backward::ResolvedTrace trace = tr.resolve(stack[i]);
|
2020-08-15 11:59:10 +00:00
|
|
|
if (trace.source.filename.empty()) {
|
2020-08-16 14:59:13 +00:00
|
|
|
AppendNoSourceStack(ss, trace, include_addr);
|
2020-12-23 11:04:20 +00:00
|
|
|
framesAppended++;
|
|
|
|
} else if (i != 0) {
|
|
|
|
AppendSourceStack(ss, trace.source, snippetFactory);
|
|
|
|
framesAppended++;
|
2020-08-16 17:15:45 +00:00
|
|
|
}
|
|
|
|
for (auto& t : trace.inliners) {
|
2020-12-23 11:04:20 +00:00
|
|
|
AppendSourceStack(ss, t, snippetFactory);
|
|
|
|
framesAppended++;
|
|
|
|
if (framesAppended >= depth)
|
|
|
|
break;
|
2020-08-15 11:31:16 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-23 11:04:20 +00:00
|
|
|
ss << "--- End of Stack ---";
|
2020-08-15 11:31:16 +00:00
|
|
|
return ss.str();
|
|
|
|
}
|
2020-08-16 14:59:13 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
static void AppendNoSourceStack(std::stringstream& ss, const backward::ResolvedTrace& trace,
|
|
|
|
bool include_addr) {
|
|
|
|
auto objectName =
|
|
|
|
(strrchr(trace.object_filename.c_str(), '/') ? strrchr(trace.object_filename.c_str(), '/') + 1
|
|
|
|
: trace.object_filename.c_str());
|
|
|
|
auto function = trace.object_function;
|
2020-12-23 11:04:20 +00:00
|
|
|
if (function.length() == 0) {
|
|
|
|
function = "[No function name]";
|
|
|
|
}
|
|
|
|
if (strcmp(objectName, "") == 0) {
|
|
|
|
objectName = "[No object name]";
|
2020-08-16 14:59:13 +00:00
|
|
|
}
|
|
|
|
ss << objectName;
|
|
|
|
if (include_addr) {
|
|
|
|
ss << "[" << trace.addr << "]";
|
|
|
|
}
|
|
|
|
ss << " " << function << std::endl;
|
|
|
|
}
|
2020-08-16 17:15:45 +00:00
|
|
|
static void AppendSourceStack(std::stringstream& ss, const backward::ResolvedTrace::SourceLoc& source,
|
2020-12-23 11:04:20 +00:00
|
|
|
backward::SnippetFactory& snippetFactory) {
|
2020-08-16 17:15:45 +00:00
|
|
|
auto fileName = (strrchr(source.filename.c_str(), '/') ? strrchr(source.filename.c_str(), '/') + 1
|
|
|
|
: source.filename.c_str());
|
2020-12-23 11:04:20 +00:00
|
|
|
|
|
|
|
auto snippetSearch = snippetFactory.get_snippet(source.filename, source.line, 1);
|
|
|
|
if (snippetSearch.empty()) {
|
|
|
|
ss << fileName << "[" << source.line << "]" << std::endl;
|
2020-08-16 14:59:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-12-23 11:04:20 +00:00
|
|
|
auto snippet = snippetSearch[0].second;
|
2020-08-16 14:59:13 +00:00
|
|
|
|
2020-08-16 17:45:19 +00:00
|
|
|
size_t p = snippet.find_first_not_of(" \t");
|
|
|
|
snippet.erase(0, p);
|
|
|
|
|
|
|
|
p = snippet.find_last_not_of(" \t");
|
|
|
|
if (std::string::npos != p)
|
2020-08-17 10:02:42 +00:00
|
|
|
snippet.erase(p + 1);
|
2020-08-16 17:15:45 +00:00
|
|
|
|
|
|
|
ss << fileName << "[" << source.line << "] " << snippet << std::endl;
|
2020-08-16 14:59:13 +00:00
|
|
|
}
|
2020-08-16 15:27:09 +00:00
|
|
|
#endif
|
2020-08-16 15:31:11 +00:00
|
|
|
};
|
2020-08-15 11:31:16 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 10:41:41 +00:00
|
|
|
#if !__cpp_consteval
|
|
|
|
#define consteval constexpr
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static consteval const char* file_name(const char* path) {
|
2020-10-09 11:48:21 +00:00
|
|
|
const char* file = path;
|
2020-12-13 10:41:41 +00:00
|
|
|
while (*path != 0) {
|
2020-10-09 11:48:21 +00:00
|
|
|
if (*path++ == '/') {
|
|
|
|
file = path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}
|
2020-08-17 10:02:42 +00:00
|
|
|
|
|
|
|
#define THROW(message) \
|
|
|
|
std::stringstream ___ss; \
|
2020-10-15 16:05:51 +00:00
|
|
|
___ss << "[" << file_name(__FILE__) << ":" << __LINE__ << "] " << message; \
|
2020-08-17 10:02:42 +00:00
|
|
|
throw ArbUt::Exception(___ss.str());
|
2020-10-09 11:48:21 +00:00
|
|
|
|
2020-08-17 10:02:42 +00:00
|
|
|
#define NOT_REACHABLE THROW("Not reachable");
|
|
|
|
#define NOT_IMPLEMENTED THROW("Not implemented");
|
|
|
|
|
2020-08-15 11:31:16 +00:00
|
|
|
#endif // ARBUTILS_EXCEPTION_HPP
|