Crash handler: Use print_error to include backtrace in logs

This commit is contained in:
Rémi Verschelde 2022-05-05 11:45:42 +02:00 committed by Relintai
parent 285c02bf76
commit 4ca6458457
3 changed files with 73 additions and 58 deletions

View File

@ -31,6 +31,7 @@
#include "crash_handler_osx.h" #include "crash_handler_osx.h"
#include "core/os/os.h" #include "core/os/os.h"
#include "core/print_string.h"
#include "core/project_settings.h" #include "core/project_settings.h"
#include "core/version.h" #include "core/version.h"
#include "main/main.h" #include "main/main.h"
@ -71,7 +72,7 @@ static uint64_t load_address() {
} }
static void handle_crash(int sig) { static void handle_crash(int sig) {
if (OS::get_singleton() == NULL) { if (OS::get_singleton() == nullptr) {
abort(); abort();
} }
@ -85,20 +86,22 @@ static void handle_crash(int sig) {
msg = proj_settings->get("debug/settings/crash_handler/message"); msg = proj_settings->get("debug/settings/crash_handler/message");
} }
// Dump the backtrace to stderr with a message to the user // Tell MainLoop about the crash. This can be handled by users too in Node.
fprintf(stderr, "\n================================================================\n"); if (OS::get_singleton()->get_main_loop()) {
fprintf(stderr, "%s: Program crashed with signal %d\n", __FUNCTION__, sig);
if (OS::get_singleton()->get_main_loop())
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_CRASH); OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_CRASH);
}
// Dump the backtrace to stderr with a message to the user
print_error("\n================================================================");
print_error(vformat("%s: Program crashed with signal %d", __FUNCTION__, sig));
// Print the engine version just before, so that people are reminded to include the version in backtrace reports. // Print the engine version just before, so that people are reminded to include the version in backtrace reports.
if (String(VERSION_HASH).empty()) { if (String(VERSION_HASH).empty()) {
fprintf(stderr, "Engine version: %s\n", VERSION_FULL_NAME); print_error(vformat("Engine version: %s", VERSION_FULL_NAME));
} else { } else {
fprintf(stderr, "Engine version: %s (%s)\n", VERSION_FULL_NAME, VERSION_HASH); print_error(vformat("Engine version: %s (%s)", VERSION_FULL_NAME, VERSION_HASH));
} }
fprintf(stderr, "Dumping the backtrace. %ls\n", msg.c_str()); print_error(vformat("Dumping the backtrace. %s", msg));
char **strings = backtrace_symbols(bt_buffer, size); char **strings = backtrace_symbols(bt_buffer, size);
if (strings) { if (strings) {
void *load_addr = (void *)load_address(); void *load_addr = (void *)load_address();
@ -113,16 +116,17 @@ static void handle_crash(int sig) {
if (dladdr(bt_buffer[i], &info) && info.dli_sname) { if (dladdr(bt_buffer[i], &info) && info.dli_sname) {
if (info.dli_sname[0] == '_') { if (info.dli_sname[0] == '_') {
int status; int status;
char *demangled = abi::__cxa_demangle(info.dli_sname, NULL, 0, &status); char *demangled = abi::__cxa_demangle(info.dli_sname, nullptr, 0, &status);
if (status == 0 && demangled) { if (status == 0 && demangled) {
snprintf(fname, 1024, "%s", demangled); snprintf(fname, 1024, "%s", demangled);
} }
if (demangled) if (demangled) {
free(demangled); free(demangled);
} }
} }
}
String output = fname; String output = fname;
@ -148,20 +152,20 @@ static void handle_crash(int sig) {
int ret; int ret;
String out = ""; String out = "";
Error err = OS::get_singleton()->execute(String("atos"), args, true, NULL, &out, &ret); Error err = OS::get_singleton()->execute(String("atos"), args, true, nullptr, &out, &ret);
if (err == OK && out.substr(0, 2) != "0x") { if (err == OK && out.substr(0, 2) != "0x") {
out.erase(out.length() - 1, 1); out.erase(out.length() - 1, 1);
output = out; output = out;
} }
} }
fprintf(stderr, "[%zu] %ls\n", i, output.c_str()); print_error(vformat("[%d] %s", (int64_t)i, output));
} }
free(strings); free(strings);
} }
fprintf(stderr, "-- END OF BACKTRACE --\n"); print_error("-- END OF BACKTRACE --");
fprintf(stderr, "================================================================\n"); print_error("================================================================");
// Abort to pass the error to the OS // Abort to pass the error to the OS
abort(); abort();
@ -177,13 +181,14 @@ CrashHandler::~CrashHandler() {
} }
void CrashHandler::disable() { void CrashHandler::disable() {
if (disabled) if (disabled) {
return; return;
}
#ifdef CRASH_HANDLER_ENABLED #ifdef CRASH_HANDLER_ENABLED
signal(SIGSEGV, NULL); signal(SIGSEGV, nullptr);
signal(SIGFPE, NULL); signal(SIGFPE, nullptr);
signal(SIGILL, NULL); signal(SIGILL, nullptr);
#endif #endif
disabled = true; disabled = true;

View File

@ -31,6 +31,7 @@
#include "crash_handler_windows.h" #include "crash_handler_windows.h"
#include "core/os/os.h" #include "core/os/os.h"
#include "core/print_string.h"
#include "core/project_settings.h" #include "core/project_settings.h"
#include "core/version.h" #include "core/version.h"
#include "main/main.h" #include "main/main.h"
@ -124,19 +125,36 @@ DWORD CrashHandlerException(EXCEPTION_POINTERS *ep) {
DWORD cbNeeded; DWORD cbNeeded;
std::vector<HMODULE> module_handles(1); std::vector<HMODULE> module_handles(1);
if (OS::get_singleton() == NULL || OS::get_singleton()->is_disable_crash_handler() || IsDebuggerPresent()) { if (OS::get_singleton() == nullptr || OS::get_singleton()->is_disable_crash_handler() || IsDebuggerPresent()) {
return EXCEPTION_CONTINUE_SEARCH; return EXCEPTION_CONTINUE_SEARCH;
} }
fprintf(stderr, "\n================================================================\n"); String msg;
fprintf(stderr, "%s: Program crashed\n", __FUNCTION__); const ProjectSettings *proj_settings = ProjectSettings::get_singleton();
if (proj_settings) {
msg = proj_settings->get("debug/settings/crash_handler/message");
}
if (OS::get_singleton()->get_main_loop()) // Tell MainLoop about the crash. This can be handled by users too in Node.
if (OS::get_singleton()->get_main_loop()) {
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_CRASH); OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_CRASH);
}
print_error("\n================================================================");
print_error(vformat("%s: Program crashed", __FUNCTION__));
// Print the engine version just before, so that people are reminded to include the version in backtrace reports.
if (String(VERSION_HASH).empty()) {
print_error(vformat("Engine version: %s", VERSION_FULL_NAME));
} else {
print_error(vformat("Engine version: %s (%s)", VERSION_FULL_NAME, VERSION_HASH));
}
print_error(vformat("Dumping the backtrace. %s", msg));
// Load the symbols: // Load the symbols:
if (!SymInitialize(process, NULL, false)) if (!SymInitialize(process, nullptr, false)) {
return EXCEPTION_CONTINUE_SEARCH; return EXCEPTION_CONTINUE_SEARCH;
}
SymSetOptions(SymGetOptions() | SYMOPT_LOAD_LINES | SYMOPT_UNDNAME); SymSetOptions(SymGetOptions() | SYMOPT_LOAD_LINES | SYMOPT_UNDNAME);
EnumProcessModules(process, &module_handles[0], module_handles.size() * sizeof(HMODULE), &cbNeeded); EnumProcessModules(process, &module_handles[0], module_handles.size() * sizeof(HMODULE), &cbNeeded);
@ -171,20 +189,6 @@ DWORD CrashHandlerException(EXCEPTION_POINTERS *ep) {
IMAGE_NT_HEADERS *h = ImageNtHeader(base); IMAGE_NT_HEADERS *h = ImageNtHeader(base);
DWORD image_type = h->FileHeader.Machine; DWORD image_type = h->FileHeader.Machine;
String msg;
const ProjectSettings *proj_settings = ProjectSettings::get_singleton();
if (proj_settings) {
msg = proj_settings->get("debug/settings/crash_handler/message");
}
// Print the engine version just before, so that people are reminded to include the version in backtrace reports.
if (String(VERSION_HASH).empty()) {
fprintf(stderr, "Engine version: %s\n", VERSION_FULL_NAME);
} else {
fprintf(stderr, "Engine version: %s (%s)\n", VERSION_FULL_NAME, VERSION_HASH);
}
fprintf(stderr, "Dumping the backtrace. %ls\n", msg.c_str());
int n = 0; int n = 0;
do { do {
if (skip_first) { if (skip_first) {
@ -193,22 +197,25 @@ DWORD CrashHandlerException(EXCEPTION_POINTERS *ep) {
if (frame.AddrPC.Offset != 0) { if (frame.AddrPC.Offset != 0) {
std::string fnName = symbol(process, frame.AddrPC.Offset).undecorated_name(); std::string fnName = symbol(process, frame.AddrPC.Offset).undecorated_name();
if (SymGetLineFromAddr64(process, frame.AddrPC.Offset, &offset_from_symbol, &line)) if (SymGetLineFromAddr64(process, frame.AddrPC.Offset, &offset_from_symbol, &line)) {
fprintf(stderr, "[%d] %s (%s:%d)\n", n, fnName.c_str(), line.FileName, line.LineNumber); print_error(vformat("[%d] %s (%s:%d)", n, fnName.c_str(), (char *)line.FileName, (int)line.LineNumber));
else } else {
fprintf(stderr, "[%d] %s\n", n, fnName.c_str()); print_error(vformat("[%d] %s", n, fnName.c_str()));
} else }
fprintf(stderr, "[%d] ???\n", n); } else {
print_error(vformat("[%d] ???", n));
}
n++; n++;
} }
if (!StackWalk64(image_type, process, hThread, &frame, context, NULL, SymFunctionTableAccess64, SymGetModuleBase64, NULL)) if (!StackWalk64(image_type, process, hThread, &frame, context, nullptr, SymFunctionTableAccess64, SymGetModuleBase64, nullptr)) {
break; break;
}
} while (frame.AddrReturn.Offset != 0 && n < 256); } while (frame.AddrReturn.Offset != 0 && n < 256);
fprintf(stderr, "-- END OF BACKTRACE --\n"); print_error("-- END OF BACKTRACE --");
fprintf(stderr, "================================================================\n"); print_error("================================================================");
SymCleanup(process); SymCleanup(process);
@ -225,8 +232,9 @@ CrashHandler::~CrashHandler() {
} }
void CrashHandler::disable() { void CrashHandler::disable() {
if (disabled) if (disabled) {
return; return;
}
disabled = true; disabled = true;
} }

View File

@ -31,6 +31,7 @@
#include "crash_handler_x11.h" #include "crash_handler_x11.h"
#include "core/os/os.h" #include "core/os/os.h"
#include "core/print_string.h"
#include "core/project_settings.h" #include "core/project_settings.h"
#include "core/version.h" #include "core/version.h"
#include "main/main.h" #include "main/main.h"
@ -61,21 +62,22 @@ static void handle_crash(int sig) {
msg = proj_settings->get("debug/settings/crash_handler/message"); msg = proj_settings->get("debug/settings/crash_handler/message");
} }
// Dump the backtrace to stderr with a message to the user // Tell MainLoop about the crash. This can be handled by users too in Node.
fprintf(stderr, "\n================================================================\n");
fprintf(stderr, "%s: Program crashed with signal %d\n", __FUNCTION__, sig);
if (OS::get_singleton()->get_main_loop()) { if (OS::get_singleton()->get_main_loop()) {
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_CRASH); OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_CRASH);
} }
// Dump the backtrace to stderr with a message to the user
print_error("\n================================================================");
print_error(vformat("%s: Program crashed with signal %d", __FUNCTION__, sig));
// Print the engine version just before, so that people are reminded to include the version in backtrace reports. // Print the engine version just before, so that people are reminded to include the version in backtrace reports.
if (String(VERSION_HASH).empty()) { if (String(VERSION_HASH).empty()) {
fprintf(stderr, "Engine version: %s\n", VERSION_FULL_NAME); print_error(vformat("Engine version: %s", VERSION_FULL_NAME));
} else { } else {
fprintf(stderr, "Engine version: %s (%s)\n", VERSION_FULL_NAME, VERSION_HASH); print_error(vformat("Engine version: %s (%s)", VERSION_FULL_NAME, VERSION_HASH));
} }
fprintf(stderr, "Dumping the backtrace. %ls\n", msg.c_str()); print_error(vformat("Dumping the backtrace. %s", msg));
char **strings = backtrace_symbols(bt_buffer, size); char **strings = backtrace_symbols(bt_buffer, size);
if (strings) { if (strings) {
for (size_t i = 1; i < size; i++) { for (size_t i = 1; i < size; i++) {
@ -117,13 +119,13 @@ static void handle_crash(int sig) {
output.erase(output.length() - 1, 1); output.erase(output.length() - 1, 1);
} }
fprintf(stderr, "[%ld] %s (%ls)\n", (long int)i, fname, output.c_str()); print_error(vformat("[%d] %s (%s)", (int64_t)i, fname, output));
} }
free(strings); free(strings);
} }
fprintf(stderr, "-- END OF BACKTRACE --\n"); print_error("-- END OF BACKTRACE --");
fprintf(stderr, "================================================================\n"); print_error("================================================================");
// Abort to pass the error to the OS // Abort to pass the error to the OS
abort(); abort();