Improves exception stack traces by including inlined functions, and displaying snippets of code.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-08-16 19:15:45 +02:00
parent 010600c76b
commit 98ff6b3b69
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
2 changed files with 22 additions and 21 deletions

View File

@ -41,17 +41,24 @@ namespace ArbUt {
return "No stack trace could be retrieved.";
}
backward::TraceResolver tr;
backward::SnippetFactory snippetFactory;
tr.load_stacktrace(_stack);
std::stringstream ss;
ss << "Stacktrace with depth " << depth << ": " << std::endl;
bool foundExceptionClass = false;
size_t framesAppended = -1;
for (size_t i = 0; i < _stack.size(); ++i) {
size_t framesAppended = 0;
for (size_t i = 0; i < _stack.size() && framesAppended <= depth; ++i) {
backward::ResolvedTrace trace = tr.resolve(_stack[i]);
if (trace.source.filename.empty()) {
AppendNoSourceStack(ss, trace, include_addr);
} else {
AppendSourceStack(ss, trace, foundExceptionClass);
AppendSourceStack(ss, trace.source, foundExceptionClass, snippetFactory);
if (foundExceptionClass) {
framesAppended++;
}
}
for (auto& t : trace.inliners) {
AppendSourceStack(ss, t, foundExceptionClass, snippetFactory);
if (foundExceptionClass) {
framesAppended++;
if (framesAppended >= depth)
@ -83,11 +90,10 @@ namespace ArbUt {
}
ss << " " << function << std::endl;
}
static void AppendSourceStack(std::stringstream& ss, const backward::ResolvedTrace& trace,
bool& foundExceptionClass) {
auto fileName =
(strrchr(trace.source.filename.c_str(), '/') ? strrchr(trace.source.filename.c_str(), '/') + 1
: trace.source.filename.c_str());
static void AppendSourceStack(std::stringstream& ss, const backward::ResolvedTrace::SourceLoc& source,
bool& foundExceptionClass, backward::SnippetFactory& snippetFactory) {
auto fileName = (strrchr(source.filename.c_str(), '/') ? strrchr(source.filename.c_str(), '/') + 1
: source.filename.c_str());
if (strcmp(fileName, "Exception.hpp") == 0) {
foundExceptionClass = true;
return;
@ -95,12 +101,13 @@ namespace ArbUt {
return;
}
auto function = trace.object_function;
if (function.length() > 70) {
function = function.substr(0, 67);
function += "...";
auto snippet = snippetFactory.get_snippet(source.filename, source.line, 1)[0].second;
if (snippet.length() > 70) {
snippet = snippet.substr(0, 67);
snippet += "...";
}
ss << fileName << "[" << trace.source.line << "] " << function << std::endl;
ss << fileName << "[" << source.line << "] " << snippet << std::endl;
}
#endif
};

View File

@ -3,20 +3,14 @@
#include "../src/Exception.hpp"
using namespace ArbUt;
#ifndef WINDOWS
__attribute__((optnone))
#endif
static void
Thrower() {
throw ArbUt::Exception("foobar");
}
static void Thrower() { throw ArbUt::Exception("foobar"); }
TEST_CASE("Throw exception get stack trace") {
try {
Thrower();
} catch (const ArbUt::Exception& e) {
REQUIRE(e.GetStacktrace(1) == "Stacktrace with depth 1: \n"
"ExceptionTests.cpp[11] Thrower()\n");
"ExceptionTests.cpp[6] static void Thrower() { throw ArbUt::Exception(\"foobar\"); }\n");
}
}