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

View File

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