Skip to content

Commit

Permalink
use CrashReporter annotation to log fatal messages in crash log
Browse files Browse the repository at this point in the history
  • Loading branch information
blueboxd committed Jul 22, 2023
1 parent 1220d7a commit fa9669b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
10 changes: 10 additions & 0 deletions base/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down
29 changes: 29 additions & 0 deletions base/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
8 changes: 8 additions & 0 deletions chrome/app/chrome_exe_main_mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <memory>

#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"
Expand All @@ -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**);
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions sandbox/mac/sandbox_logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

#include "sandbox/mac/sandbox_logging.h"
#include "base/logging.h"

#include <errno.h>
#include <stdarg.h>
Expand Down Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit fa9669b

Please sign in to comment.