-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce the new crash handler based on crashpad and sentry-na…
…tive
- Loading branch information
1 parent
097cf19
commit 3769725
Showing
9 changed files
with
153 additions
and
81 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
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,137 @@ | ||
// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved. | ||
// | ||
// 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. | ||
|
||
#include "endstone/detail/crash_handler.h" | ||
|
||
#include <sentry.h> | ||
|
||
#include <filesystem> | ||
#include <iostream> | ||
#include <string> | ||
|
||
#include <cpptrace/cpptrace.hpp> | ||
#include <fmt/format.h> | ||
|
||
#include "endstone/detail/os.h" | ||
#include "endstone/endstone.h" | ||
|
||
namespace fs = std::filesystem; | ||
|
||
namespace endstone::detail { | ||
|
||
namespace { | ||
|
||
void print_frame(std::ostream &stream, bool color, unsigned frame_number_width, std::size_t counter, | ||
const cpptrace::stacktrace_frame &frame) | ||
{ | ||
const auto *reset = color ? "\033[0m" : ""; | ||
const auto *green = color ? "\033[32m" : ""; | ||
const auto *yellow = color ? "\033[33m" : ""; | ||
const auto *blue = color ? "\033[34m" : ""; | ||
std::string line = fmt::format("[{:<{}}] ", counter, frame_number_width); | ||
if (frame.is_inline) { | ||
line += fmt::format("{:<{}}", "(inlined)", 2 * sizeof(cpptrace::frame_ptr) + 2); | ||
} | ||
else { | ||
line += fmt::format("{}0x{:<{}x}{}", blue, frame.raw_address, 2 * sizeof(cpptrace::frame_ptr), reset); | ||
line += fmt::format(" {}(0x{:09x}){}", green, frame.object_address, reset); | ||
} | ||
if (!frame.symbol.empty()) { | ||
line += fmt::format(" in {}{}{}", yellow, frame.symbol, reset); | ||
} | ||
if (!frame.filename.empty()) { | ||
line += fmt::format(" at {}{}{}", green, frame.filename, reset); | ||
if (frame.line.has_value()) { | ||
line += fmt::format(":{}{}{}", blue, frame.line.value(), reset); | ||
if (frame.column.has_value()) { | ||
line += fmt::format(":{}{}{}", blue, frame.column.value(), reset); | ||
} | ||
} | ||
} | ||
stream << line; | ||
} | ||
|
||
void print_crash_dump(const std::string &message, std::size_t skip = 0) | ||
{ | ||
printf("=== ENDSTONE CRASHED! - PLEASE REPORT THIS AS AN ISSUE ON GITHUB ===\n"); | ||
printf("Operation system: %s\n", os::get_name().c_str()); | ||
printf("Endstone version: %s\n", ENDSTONE_VERSION); | ||
printf("Api version : %s\n", ENDSTONE_API_VERSION); | ||
printf("Description : %s\n", message.c_str()); | ||
|
||
auto stacktrace = cpptrace::generate_trace(skip + 1); | ||
auto &stream = std::cerr; | ||
auto color = cpptrace::isatty(cpptrace::stderr_fileno); | ||
stream << "Stack trace (most recent call first):" << '\n'; | ||
|
||
auto &frames = stacktrace.frames; | ||
std::size_t counter = 0; | ||
if (frames.empty()) { | ||
stream << "<empty trace>" << '\n'; | ||
return; | ||
} | ||
const auto frame_number_width = std::to_string(frames.size()).length(); | ||
for (const auto &frame : frames) { | ||
print_frame(stream, color, frame_number_width, counter, frame); | ||
stream << '\n'; | ||
if (frame.line.has_value() && !frame.filename.empty()) { | ||
stream << cpptrace::get_snippet(frame.filename, frame.line.value(), 2, color); | ||
} | ||
counter++; | ||
} | ||
} | ||
|
||
sentry_value_t on_crash(const sentry_ucontext_t *ctx, sentry_value_t event, void *closure) | ||
{ | ||
#ifdef _WIN32 | ||
print_crash_dump("Exception unhandled: " + | ||
fmt::format("{0:#x}", ctx->exception_ptrs.ExceptionRecord->ExceptionCode)); | ||
#else | ||
print_crash_dump("Signal received: " + std::to_string(ctx.signum)); | ||
#endif | ||
|
||
// TODO(crash_handler): filter out crashes originates from BDS / plugins | ||
return event; | ||
} | ||
} // namespace | ||
|
||
CrashHandler::CrashHandler() | ||
{ | ||
constexpr auto dsn = | ||
"https://69c28eeaef4651abcf0bbeace6a1175c@o4508553519431680.ingest.de.sentry.io/4508569040519248"; | ||
#ifdef _WIN32 | ||
fs::path handler_path = (fs::path{os::get_module_pathname()}.parent_path()) / "crashpad_handler.exe"; | ||
#else | ||
fs::path handler_path = (fs::path{os::get_module_pathname()}.parent_path()) / "crashpad_handler"; | ||
#endif | ||
constexpr std::string_view release = "endstone@" ENDSTONE_VERSION; | ||
constexpr bool is_dev = release.find("dev") != std::string_view::npos; | ||
|
||
sentry_options_t *options = sentry_options_new(); | ||
sentry_options_set_dsn(options, dsn); | ||
sentry_options_set_database_path(options, ".sentry-native"); | ||
sentry_options_set_handler_path(options, handler_path.string().c_str()); | ||
sentry_options_set_release(options, std::string(release).c_str()); | ||
sentry_options_set_debug(options, 1); | ||
sentry_options_set_on_crash(options, on_crash, nullptr); | ||
sentry_options_set_environment(options, is_dev ? "development" : "production"); | ||
sentry_init(options); | ||
} | ||
|
||
CrashHandler::~CrashHandler() | ||
{ | ||
sentry_close(); | ||
} | ||
|
||
} // namespace endstone::detail |
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 was deleted.
Oops, something went wrong.