From fa9669b60979b4b155bd643bb303a30cd56b7a28 Mon Sep 17 00:00:00 2001 From: blueboxd <48254131+blueboxd@users.noreply.github.com> Date: Sat, 22 Jul 2023 15:48:03 +0900 Subject: [PATCH] use CrashReporter annotation to log fatal messages in crash log --- base/logging.cc | 10 ++++++++++ base/logging.h | 29 +++++++++++++++++++++++++++++ chrome/app/chrome_exe_main_mac.cc | 8 ++++++++ sandbox/mac/sandbox_logging.cc | 7 +++++-- 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/base/logging.cc b/base/logging.cc index 0508c883614f3a..95c0ff8b306fd2 100644 --- a/base/logging.cc +++ b/base/logging.cc @@ -57,6 +57,10 @@ typedef HANDLE FileHandle; MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_12 #define USE_ASL #endif +CRASH_REPORTER_CLIENT_HIDDEN +struct crashreporter_annotations_t gCRAnnotations + __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION))) + = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0, 0 }; #endif // BUILDFLAG(IS_IOS) #if defined(USE_ASL) @@ -1027,6 +1031,12 @@ LogMessage::~LogMessage() { base::strlcpy(str_stack, str_newline.data(), std::size(str_stack)); base::debug::Alias(&str_stack); +#if BUILDFLAG(IS_MAC) + CRSetCrashLogMessage(str_newline.c_str()); + CRSetCrashLogBacktrace(base::debug::StackTrace().ToString().c_str()); + CRSetCrashLogMessage2(base::debug::TaskTrace().ToString().c_str()); +#endif + if (!GetLogAssertHandlerStack().empty()) { LogAssertHandlerFunction log_assert_handler = GetLogAssertHandlerStack().top(); diff --git a/base/logging.h b/base/logging.h index 1ae198e30b0b97..d8db7f9e6b55db 100644 --- a/base/logging.h +++ b/base/logging.h @@ -180,6 +180,35 @@ // Additional logging-related information can be found here: // https://chromium.googlesource.com/chromium/src/+/main/docs/linux/debugging.md#Logging +#if BUILDFLAG(IS_MAC) +#define CRASHREPORTER_ANNOTATIONS_SECTION "__crash_info" +#define CRASHREPORTER_ANNOTATIONS_VERSION 5 +#define CRASH_REPORTER_CLIENT_HIDDEN __attribute__((visibility("hidden"))) + +#define _crc_make_getter(attr) ((const char *)(unsigned long)gCRAnnotations.attr) +#define _crc_make_setter(attr, arg) (gCRAnnotations.attr = (uint64_t)(unsigned long)(arg)) +#define CRGetCrashLogMessage() _crc_make_getter(message) +#define CRSetCrashLogMessage(m) _crc_make_setter(message, m) +#define CRGetCrashLogMessage2() _crc_make_getter(message2) +#define CRSetCrashLogMessage2(m) _crc_make_setter(message2, m) +#define CRGetCrashLogBacktrace() _crc_make_getter(backtrace) +#define CRSetCrashLogBacktrace(b) _crc_make_setter(backtrace, b) + +struct crashreporter_annotations_t { + uint64_t version; // unsigned long + uint64_t message; // char * + uint64_t signature_string; // char * + uint64_t backtrace; // char * + uint64_t message2; // char * + uint64_t thread; // uint64_t + uint64_t dialog_mode; // unsigned int + uint64_t abort_cause; // unsigned int +}; + +CRASH_REPORTER_CLIENT_HIDDEN +extern struct crashreporter_annotations_t gCRAnnotations; +#endif + namespace logging { // TODO(avi): do we want to do a unification of character types here? diff --git a/chrome/app/chrome_exe_main_mac.cc b/chrome/app/chrome_exe_main_mac.cc index b889644fa19dba..26767056789cf8 100644 --- a/chrome/app/chrome_exe_main_mac.cc +++ b/chrome/app/chrome_exe_main_mac.cc @@ -20,6 +20,7 @@ #include #include "base/allocator/early_zone_registration_mac.h" +#include "base/logging.h" #include "build/branding_buildflags.h" #include "build/build_config.h" #include "chrome/common/chrome_version.h" @@ -36,6 +37,11 @@ extern "C" { void abort_report_np(const char* fmt, ...); } +CRASH_REPORTER_CLIENT_HIDDEN +struct crashreporter_annotations_t gCRAnnotations + __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION))) + = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0, 0 }; + namespace { typedef int (*ChromeMainPtr)(int, char**); @@ -144,6 +150,8 @@ __attribute__((used)) const char kGrossPaddingForCrbug1300598[36 * 1024] = {}; fputs(message, stderr); if (__builtin_available(macOS 10.11, *)) { abort_report_np("%s", message); + } else { + CRSetCrashLogMessage(message); } } va_end(valist); diff --git a/sandbox/mac/sandbox_logging.cc b/sandbox/mac/sandbox_logging.cc index 2879672766e78b..0a06ef31c5741e 100644 --- a/sandbox/mac/sandbox_logging.cc +++ b/sandbox/mac/sandbox_logging.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include "sandbox/mac/sandbox_logging.h" +#include "base/logging.h" #include #include @@ -115,9 +116,11 @@ void SendAslLog(Level level, const char* message) { asl_set(asl_message.get(), ASL_KEY_MSG, message); asl_send(asl_client.get(), asl_message.get()); - if (__builtin_available(macOS 10.11, *)) { - if (level == Level::FATAL) { + if (level == Level::FATAL) { + if (__builtin_available(macOS 10.11, *)) { abort_report_np(message); + } else { + CRSetCrashLogMessage(message); } } }