From 81ed54d13ecea920583c521373d2a6079fc67040 Mon Sep 17 00:00:00 2001
From: yzct12345 <87620833+yzct12345@users.noreply.github.com>
Date: Fri, 13 Aug 2021 18:58:35 +0000
Subject: [PATCH] logging: Display backtrace on crash

This implements backtraces so we don't have to tell users how to use gdb anymore.

This prints a backtrace after abort or segfault is detected. It also fixes the log getting cut off with the last line containing only a bracket. This change lets us know what caused a crash not just what happened the few seconds before it.

I only know how to add support for Linux with GCC. Also this doesn't work outside of C/C++ such as in dynarmic or certain parts of graphics drivers. The good thing is that it'll try and just crash again but the stack frames are still there so the core dump will work just like before.
---
 src/common/CMakeLists.txt      |   3 +
 src/common/logging/backend.cpp | 112 ++++++++++++++++++++++++++++++++-
 2 files changed, 114 insertions(+), 1 deletion(-)

diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 57922b51ce..316c4dedcc 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -176,3 +176,6 @@ if (MSVC)
 else()
   target_link_libraries(common PRIVATE zstd)
 endif()
+if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux" AND CMAKE_CXX_COMPILER_ID STREQUAL GNU)
+  target_link_libraries(common PRIVATE backtrace)
+endif()
diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp
index 13edda9c9f..4e7cfdc99f 100644
--- a/src/common/logging/backend.cpp
+++ b/src/common/logging/backend.cpp
@@ -12,6 +12,14 @@
 #include <windows.h> // For OutputDebugStringW
 #endif
 
+#if defined(__linux__) && defined(__GNUG__) && !defined(__clang__)
+#define BOOST_STACKTRACE_USE_BACKTRACE
+#include <boost/stacktrace.hpp>
+#undef BOOST_STACKTRACE_USE_BACKTRACE
+#include <signal.h>
+#define YUZU_LINUX_GCC_BACKTRACE
+#endif
+
 #include "common/fs/file.h"
 #include "common/fs/fs.h"
 #include "common/fs/fs_paths.h"
@@ -154,6 +162,14 @@ public:
 
 bool initialization_in_progress_suppress_logging = false;
 
+#ifdef YUZU_LINUX_GCC_BACKTRACE
+[[noreturn]] void SleepForever() {
+    while (true) {
+        pause();
+    }
+}
+#endif
+
 /**
  * Static state as a singleton.
  */
@@ -225,9 +241,66 @@ private:
               while (max_logs_to_write-- && message_queue.Pop(entry)) {
                   write_logs();
               }
-          })} {}
+          })} {
+#ifdef YUZU_LINUX_GCC_BACKTRACE
+        int waker_pipefd[2];
+        int done_printing_pipefd[2];
+        if (pipe2(waker_pipefd, O_CLOEXEC) || pipe2(done_printing_pipefd, O_CLOEXEC)) {
+            abort();
+        }
+        backtrace_thread_waker_fd = waker_pipefd[1];
+        backtrace_done_printing_fd = done_printing_pipefd[0];
+        std::thread([this, wait_fd = waker_pipefd[0], done_fd = done_printing_pipefd[1]] {
+            Common::SetCurrentThreadName("yuzu:Crash");
+            for (u8 ignore = 0; read(wait_fd, &ignore, 1) != 1;)
+                ;
+            const int sig = received_signal;
+            if (sig <= 0) {
+                abort();
+            }
+            StopBackendThread();
+            const auto signal_entry =
+                CreateEntry(Class::Log, Level::Critical, "?", 0, "?",
+                            fmt::vformat("Received signal {}", fmt::make_format_args(sig)));
+            ForEachBackend([&signal_entry](Backend& backend) {
+                backend.EnableForStacktrace();
+                backend.Write(signal_entry);
+            });
+            const auto backtrace =
+                boost::stacktrace::stacktrace::from_dump(backtrace_storage.data(), 4096);
+            for (const auto& frame : backtrace.as_vector()) {
+                auto line = boost::stacktrace::detail::to_string(&frame, 1);
+                if (line.empty()) {
+                    abort();
+                }
+                line.pop_back(); // Remove newline
+                const auto frame_entry =
+                    CreateEntry(Class::Log, Level::Critical, "?", 0, "?", line);
+                ForEachBackend([&frame_entry](Backend& backend) { backend.Write(frame_entry); });
+            }
+            using namespace std::literals;
+            const auto rip_entry = CreateEntry(Class::Log, Level::Critical, "?", 0, "?", "RIP"s);
+            ForEachBackend([&rip_entry](Backend& backend) {
+                backend.Write(rip_entry);
+                backend.Flush();
+            });
+            for (const u8 anything = 0; write(done_fd, &anything, 1) != 1;)
+                ;
+            // Abort on original thread to help debugging
+            SleepForever();
+        }).detach();
+        signal(SIGSEGV, &HandleSignal);
+        signal(SIGABRT, &HandleSignal);
+#endif
+    }
 
     ~Impl() {
+#ifdef YUZU_LINUX_GCC_BACKTRACE
+        if (int zero_or_ignore = 0;
+            !received_signal.compare_exchange_strong(zero_or_ignore, SIGKILL)) {
+            SleepForever();
+        }
+#endif
         StopBackendThread();
     }
 
@@ -266,6 +339,36 @@ private:
         delete ptr;
     }
 
+#ifdef YUZU_LINUX_GCC_BACKTRACE
+    [[noreturn]] static void HandleSignal(int sig) {
+        signal(SIGABRT, SIG_DFL);
+        signal(SIGSEGV, SIG_DFL);
+        if (sig <= 0) {
+            abort();
+        }
+        instance->InstanceHandleSignal(sig);
+    }
+
+    [[noreturn]] void InstanceHandleSignal(int sig) {
+        if (int zero_or_ignore = 0; !received_signal.compare_exchange_strong(zero_or_ignore, sig)) {
+            if (received_signal == SIGKILL) {
+                abort();
+            }
+            SleepForever();
+        }
+        // Don't restart like boost suggests. We want to append to the log file and not lose dynamic
+        // symbols. This may segfault if it unwinds outside C/C++ code but we'll just have to fall
+        // back to core dumps.
+        boost::stacktrace::safe_dump_to(backtrace_storage.data(), 4096);
+        std::atomic_thread_fence(std::memory_order_seq_cst);
+        for (const int anything = 0; write(backtrace_thread_waker_fd, &anything, 1) != 1;)
+            ;
+        for (u8 ignore = 0; read(backtrace_done_printing_fd, &ignore, 1) != 1;)
+            ;
+        abort();
+    }
+#endif
+
     static inline std::unique_ptr<Impl, decltype(&Deleter)> instance{nullptr, Deleter};
 
     Filter filter;
@@ -276,6 +379,13 @@ private:
     std::thread backend_thread;
     MPSCQueue<Entry> message_queue{};
     std::chrono::steady_clock::time_point time_origin{std::chrono::steady_clock::now()};
+
+#ifdef YUZU_LINUX_GCC_BACKTRACE
+    std::atomic_int received_signal{0};
+    std::array<u8, 4096> backtrace_storage{};
+    int backtrace_thread_waker_fd;
+    int backtrace_done_printing_fd;
+#endif
 };
 } // namespace