-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: better stacktrace using cpptrace
- Loading branch information
Showing
6 changed files
with
117 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#pragma once | ||
|
||
#include <cpptrace/cpptrace.hpp> | ||
#include <helpers/logger.hpp> | ||
#include <istream> | ||
#include <ostream> | ||
|
||
static void safe_dump_stacktrace_to(std::ostream &out) { | ||
constexpr std::size_t N = 100; | ||
cpptrace::frame_ptr buffer[N]; | ||
std::size_t count = cpptrace::safe_generate_raw_trace(buffer, N); | ||
if (count > 0) { | ||
out << count << '\n'; | ||
for (std::size_t i = 0; i < count; i++) { | ||
cpptrace::safe_object_frame frame{}; | ||
cpptrace::get_safe_object_frame(buffer[i], &frame); | ||
out << frame.address_relative_to_object_start << ' ' << frame.raw_address << ' '; | ||
out.write(frame.object_path, sizeof(frame.object_path)); | ||
out << '\n'; | ||
} | ||
} | ||
} | ||
|
||
static std::unique_ptr<cpptrace::object_trace> load_stacktrace_from(std::istream &in) { | ||
cpptrace::object_trace trace{}; | ||
std::size_t count; | ||
in >> count; | ||
in.ignore(); // ignore newline | ||
for (std::size_t i = 0; i < count; i++) { | ||
cpptrace::safe_object_frame frame{}; | ||
in >> frame.address_relative_to_object_start; | ||
in.ignore(); // ignore space | ||
in >> frame.raw_address; | ||
in.ignore(); // ignore space | ||
in.read(frame.object_path, sizeof(frame.object_path)); | ||
in.ignore(); // ignore newline | ||
try { | ||
trace.frames.push_back(frame.resolve()); | ||
} catch (std::exception &ex) { | ||
logs::log(logs::debug, "Unable to parse stacktrace frame, skipping"); | ||
} | ||
} | ||
return std::make_unique<cpptrace::object_trace>(trace); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include "catch2/catch_all.hpp" | ||
using Catch::Matchers::EndsWith; | ||
using Catch::Matchers::Equals; | ||
|
||
#include <exceptions/exceptions.h> | ||
#include <fstream> | ||
|
||
TEST_CASE("Exceptions", "[Exceptions]") { | ||
std::ofstream ofs("stacktrace.txt"); | ||
safe_dump_stacktrace_to(ofs); | ||
ofs.close(); | ||
|
||
std::ifstream ifs("stacktrace.txt"); | ||
auto trace = load_stacktrace_from(ifs); | ||
REQUIRE(trace->frames.size() > 0); | ||
|
||
auto stacktrace = trace->resolve(); | ||
REQUIRE(stacktrace.frames.size() > 0); | ||
REQUIRE_THAT(stacktrace.frames[0].filename, EndsWith("src/moonlight-server/exceptions/exceptions.h")); | ||
REQUIRE_THAT(stacktrace.frames[0].symbol, Equals("safe_dump_stacktrace_to")); | ||
} |