Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash handler: Use print_error to include backtrace in logs #60782

Merged
merged 1 commit into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions platform/linuxbsd/crash_handler_linuxbsd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "core/config/project_settings.h"
#include "core/os/os.h"
#include "core/string/print_string.h"
#include "core/version.h"
#include "main/main.h"

Expand Down Expand Up @@ -61,21 +62,22 @@ static void handle_crash(int sig) {
msg = proj_settings->get("debug/settings/crash_handler/message");
}

// Dump the backtrace to stderr with a message to the user
fprintf(stderr, "\n================================================================\n");
fprintf(stderr, "%s: Program crashed with signal %d\n", __FUNCTION__, sig);

// 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);
}

// 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.
if (String(VERSION_HASH).is_empty()) {
fprintf(stderr, "Engine version: %s\n", VERSION_FULL_NAME);
print_error(vformat("Engine version: %s", VERSION_FULL_NAME));
} 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. %s\n", msg.utf8().get_data());
print_error(vformat("Dumping the backtrace. %s", msg));
char **strings = backtrace_symbols(bt_buffer, size);
if (strings) {
for (size_t i = 1; i < size; i++) {
Expand Down Expand Up @@ -117,13 +119,13 @@ static void handle_crash(int sig) {
output = output.substr(0, output.length() - 1);
}

fprintf(stderr, "[%ld] %s (%s)\n", (long int)i, fname, output.utf8().get_data());
print_error(vformat("[%d] %s (%s)", (int64_t)i, fname, output));
}

free(strings);
}
fprintf(stderr, "-- END OF BACKTRACE --\n");
fprintf(stderr, "================================================================\n");
print_error("-- END OF BACKTRACE --");
print_error("================================================================");

// Abort to pass the error to the OS
abort();
Expand Down
22 changes: 12 additions & 10 deletions platform/osx/crash_handler_osx.mm
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "core/config/project_settings.h"
#include "core/os/os.h"
#include "core/string/print_string.h"
#include "core/version.h"
#include "main/main.h"

Expand Down Expand Up @@ -85,21 +86,22 @@ static void handle_crash(int sig) {
msg = proj_settings->get("debug/settings/crash_handler/message");
}

// Dump the backtrace to stderr with a message to the user
fprintf(stderr, "\n================================================================\n");
fprintf(stderr, "%s: Program crashed with signal %d\n", __FUNCTION__, sig);

// 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);
}

// 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.
if (String(VERSION_HASH).is_empty()) {
fprintf(stderr, "Engine version: %s\n", VERSION_FULL_NAME);
print_error(vformat("Engine version: %s", VERSION_FULL_NAME));
} 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. %s\n", msg.utf8().get_data());
print_error(vformat("Dumping the backtrace. %s", msg));
char **strings = backtrace_symbols(bt_buffer, size);
if (strings) {
void *load_addr = (void *)load_address();
Expand Down Expand Up @@ -157,13 +159,13 @@ static void handle_crash(int sig) {
}
}

fprintf(stderr, "[%zu] %s\n", i, output.utf8().get_data());
print_error(vformat("[%d] %s", (int64_t)i, output));
}

free(strings);
}
fprintf(stderr, "-- END OF BACKTRACE --\n");
fprintf(stderr, "================================================================\n");
print_error("-- END OF BACKTRACE --");
print_error("================================================================");

// Abort to pass the error to the OS
abort();
Expand Down
44 changes: 23 additions & 21 deletions platform/windows/crash_handler_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "core/config/project_settings.h"
#include "core/os/os.h"
#include "core/string/print_string.h"
#include "core/version.h"
#include "main/main.h"

Expand Down Expand Up @@ -129,13 +130,28 @@ DWORD CrashHandlerException(EXCEPTION_POINTERS *ep) {
return EXCEPTION_CONTINUE_SEARCH;
}

fprintf(stderr, "\n================================================================\n");
fprintf(stderr, "%s: Program crashed\n", __FUNCTION__);
String msg;
const ProjectSettings *proj_settings = ProjectSettings::get_singleton();
if (proj_settings) {
msg = proj_settings->get("debug/settings/crash_handler/message");
}

// 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);
}

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).is_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:
if (!SymInitialize(process, nullptr, false)) {
return EXCEPTION_CONTINUE_SEARCH;
Expand Down Expand Up @@ -174,20 +190,6 @@ DWORD CrashHandlerException(EXCEPTION_POINTERS *ep) {
IMAGE_NT_HEADERS *h = ImageNtHeader(base);
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).is_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. %s\n", msg.utf8().get_data());

int n = 0;
do {
if (skip_first) {
Expand All @@ -197,12 +199,12 @@ DWORD CrashHandlerException(EXCEPTION_POINTERS *ep) {
std::string fnName = symbol(process, frame.AddrPC.Offset).undecorated_name();

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 {
fprintf(stderr, "[%d] %s\n", n, fnName.c_str());
print_error(vformat("[%d] %s", n, fnName.c_str()));
}
} else {
fprintf(stderr, "[%d] ???\n", n);
print_error(vformat("[%d] ???", n));
}

n++;
Expand All @@ -213,8 +215,8 @@ DWORD CrashHandlerException(EXCEPTION_POINTERS *ep) {
}
} while (frame.AddrReturn.Offset != 0 && n < 256);

fprintf(stderr, "-- END OF BACKTRACE --\n");
fprintf(stderr, "================================================================\n");
print_error("-- END OF BACKTRACE --");
print_error("================================================================");

SymCleanup(process);

Expand Down