2016-12-12 11:05:29 +01:00
|
|
|
// Copyright 2016 The Chromium Embedded Framework Authors. Portions copyright
|
|
|
|
// 2016 The Chromium Authors. All rights reserved. Use of this source code is
|
|
|
|
// governed by a BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/common/crash_reporting.h"
|
2016-12-12 11:05:29 +01:00
|
|
|
|
2024-04-23 22:06:00 +02:00
|
|
|
#include <string_view>
|
|
|
|
|
2016-12-12 11:05:29 +01:00
|
|
|
#include "base/base_switches.h"
|
|
|
|
#include "base/command_line.h"
|
|
|
|
#include "base/debug/crash_logging.h"
|
2020-07-08 19:23:29 +02:00
|
|
|
#include "base/logging.h"
|
2019-02-01 17:42:40 +01:00
|
|
|
#include "base/stl_util.h"
|
2016-12-12 11:05:29 +01:00
|
|
|
#include "base/strings/string_util.h"
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/include/cef_crash_util.h"
|
|
|
|
#include "cef/libcef/common/cef_switches.h"
|
2016-12-12 11:05:29 +01:00
|
|
|
#include "chrome/common/crash_keys.h"
|
2017-12-07 22:44:24 +01:00
|
|
|
#include "components/crash/core/common/crash_key.h"
|
2018-02-15 01:12:09 +01:00
|
|
|
#include "components/crash/core/common/crash_keys.h"
|
2016-12-12 11:05:29 +01:00
|
|
|
#include "content/public/common/content_switches.h"
|
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2023-09-15 21:51:43 +02:00
|
|
|
#include "base/apple/foundation_util.h"
|
2016-12-12 11:05:29 +01:00
|
|
|
#include "components/crash/core/common/crash_keys.h"
|
|
|
|
#include "content/public/common/content_paths.h"
|
|
|
|
#endif
|
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_POSIX)
|
2016-12-12 11:05:29 +01:00
|
|
|
#include "base/lazy_instance.h"
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/common/crash_reporter_client.h"
|
2022-03-15 20:42:15 +01:00
|
|
|
#include "components/crash/core/app/crashpad.h"
|
2016-12-12 11:05:29 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace crash_reporting {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2018-02-15 01:12:09 +01:00
|
|
|
|
|
|
|
const base::FilePath::CharType kChromeElfDllName[] =
|
|
|
|
FILE_PATH_LITERAL("chrome_elf.dll");
|
|
|
|
|
|
|
|
// exported in crash_reporter_client.cc:
|
|
|
|
// int __declspec(dllexport) __cdecl SetCrashKeyValueImpl.
|
|
|
|
typedef int(__cdecl* SetCrashKeyValue)(const char*,
|
|
|
|
size_t,
|
|
|
|
const char*,
|
|
|
|
size_t);
|
|
|
|
|
|
|
|
// int __declspec(dllexport) __cdecl IsCrashReportingEnabledImpl.
|
|
|
|
typedef int(__cdecl* IsCrashReportingEnabled)();
|
|
|
|
|
2024-04-23 22:06:00 +02:00
|
|
|
bool SetCrashKeyValueTrampoline(const std::string_view& key,
|
|
|
|
const std::string_view& value) {
|
2018-02-15 01:12:09 +01:00
|
|
|
static SetCrashKeyValue set_crash_key = []() {
|
|
|
|
HMODULE elf_module = GetModuleHandle(kChromeElfDllName);
|
|
|
|
return reinterpret_cast<SetCrashKeyValue>(
|
|
|
|
elf_module ? GetProcAddress(elf_module, "SetCrashKeyValueImpl")
|
|
|
|
: nullptr);
|
|
|
|
}();
|
|
|
|
if (set_crash_key) {
|
|
|
|
return !!(set_crash_key)(key.data(), key.size(), value.data(),
|
|
|
|
value.size());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsCrashReportingEnabledTrampoline() {
|
|
|
|
static IsCrashReportingEnabled is_crash_reporting_enabled = []() {
|
|
|
|
HMODULE elf_module = GetModuleHandle(kChromeElfDllName);
|
|
|
|
return reinterpret_cast<IsCrashReportingEnabled>(
|
|
|
|
elf_module ? GetProcAddress(elf_module, "IsCrashReportingEnabledImpl")
|
|
|
|
: nullptr);
|
|
|
|
}();
|
|
|
|
if (is_crash_reporting_enabled) {
|
|
|
|
return !!(is_crash_reporting_enabled)();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#endif // BUILDFLAG(IS_WIN)
|
2018-02-15 01:12:09 +01:00
|
|
|
|
2016-12-12 11:05:29 +01:00
|
|
|
bool g_crash_reporting_enabled = false;
|
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_POSIX)
|
2016-12-12 11:05:29 +01:00
|
|
|
base::LazyInstance<CefCrashReporterClient>::Leaky g_crash_reporter_client =
|
|
|
|
LAZY_INSTANCE_INITIALIZER;
|
|
|
|
|
|
|
|
void InitCrashReporter(const base::CommandLine& command_line,
|
|
|
|
const std::string& process_type) {
|
|
|
|
CefCrashReporterClient* crash_client = g_crash_reporter_client.Pointer();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!crash_client->HasCrashConfigFile()) {
|
2016-12-12 11:05:29 +01:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-12-12 11:05:29 +01:00
|
|
|
|
|
|
|
crash_reporter::SetCrashReporterClient(crash_client);
|
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2016-12-12 11:05:29 +01:00
|
|
|
// TODO(mark): Right now, InitializeCrashpad() needs to be called after
|
|
|
|
// CommandLine::Init() and configuration of chrome::DIR_CRASH_DUMPS. Ideally,
|
|
|
|
// Crashpad initialization could occur sooner, preferably even before the
|
|
|
|
// framework dylib is even loaded, to catch potential early crashes.
|
|
|
|
crash_reporter::InitializeCrashpad(process_type.empty(), process_type);
|
|
|
|
|
2023-09-15 21:51:43 +02:00
|
|
|
if (base::apple::AmIBundled()) {
|
2017-01-27 20:16:04 +01:00
|
|
|
// Mac Chrome is packaged with a main app bundle and a helper app bundle.
|
|
|
|
// The main app bundle should only be used for the browser process, so it
|
|
|
|
// should never see a --type switch (switches::kProcessType). Likewise,
|
|
|
|
// the helper should always have a --type switch.
|
|
|
|
//
|
|
|
|
// This check is done this late so there is already a call to
|
2023-09-15 21:51:43 +02:00
|
|
|
// base::apple::IsBackgroundOnlyProcess(), so there is no change in
|
2017-01-27 20:16:04 +01:00
|
|
|
// startup/initialization order.
|
|
|
|
|
|
|
|
// The helper's Info.plist marks it as a background only app.
|
2023-09-15 21:51:43 +02:00
|
|
|
if (base::apple::IsBackgroundOnlyProcess()) {
|
2017-01-27 20:16:04 +01:00
|
|
|
CHECK(command_line.HasSwitch(switches::kProcessType) &&
|
|
|
|
!process_type.empty())
|
|
|
|
<< "Helper application requires --type.";
|
|
|
|
} else {
|
|
|
|
CHECK(!command_line.HasSwitch(switches::kProcessType) &&
|
|
|
|
process_type.empty())
|
|
|
|
<< "Main application forbids --type, saw " << process_type;
|
|
|
|
}
|
2016-12-12 11:05:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
g_crash_reporting_enabled = true;
|
2022-01-24 18:58:02 +01:00
|
|
|
#else // !BUILDFLAG(IS_MAC)
|
2020-10-08 21:54:42 +02:00
|
|
|
if (process_type != switches::kZygoteProcess) {
|
2016-12-12 11:05:29 +01:00
|
|
|
// Crash reporting for subprocesses created using the zygote will be
|
|
|
|
// initialized in ZygoteForked.
|
2022-03-15 20:42:15 +01:00
|
|
|
crash_reporter::InitializeCrashpad(process_type.empty(), process_type);
|
2016-12-12 11:05:29 +01:00
|
|
|
|
|
|
|
g_crash_reporting_enabled = true;
|
|
|
|
}
|
2022-01-24 18:58:02 +01:00
|
|
|
#endif // !BUILDFLAG(IS_MAC)
|
2016-12-12 11:05:29 +01:00
|
|
|
}
|
2022-01-24 18:58:02 +01:00
|
|
|
#endif // BUILDFLAG(IS_POSIX)
|
2016-12-12 11:05:29 +01:00
|
|
|
|
|
|
|
// Used to exclude command-line flags from crash reporting.
|
|
|
|
bool IsBoringCEFSwitch(const std::string& flag) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (crash_keys::IsBoringChromeSwitch(flag)) {
|
2016-12-12 11:05:29 +01:00
|
|
|
return true;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-12-12 11:05:29 +01:00
|
|
|
|
|
|
|
static const char* const kIgnoreSwitches[] = {
|
2017-05-17 11:29:28 +02:00
|
|
|
// CEF internals.
|
|
|
|
switches::kLogFile,
|
|
|
|
|
|
|
|
// Chromium internals.
|
2019-02-01 17:42:40 +01:00
|
|
|
"content-image-texture-target",
|
|
|
|
"mojo-platform-channel-handle",
|
|
|
|
"primordial-pipe-token",
|
|
|
|
"service-pipe-token",
|
2018-02-15 01:12:09 +01:00
|
|
|
"service-request-channel-token",
|
2016-12-12 11:05:29 +01:00
|
|
|
};
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!base::StartsWith(flag, "--", base::CompareCase::SENSITIVE)) {
|
2016-12-12 11:05:29 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-12-12 11:05:29 +01:00
|
|
|
|
|
|
|
size_t end = flag.find("=");
|
|
|
|
size_t len = (end == std::string::npos) ? flag.length() - 2 : end - 2;
|
2024-01-20 23:48:57 +01:00
|
|
|
for (auto kIgnoreSwitche : kIgnoreSwitches) {
|
|
|
|
if (flag.compare(2, len, kIgnoreSwitche) == 0) {
|
2016-12-12 11:05:29 +01:00
|
|
|
return true;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-12-12 11:05:29 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-01-23 18:36:54 +01:00
|
|
|
bool Enabled() {
|
|
|
|
return g_crash_reporting_enabled;
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:06:00 +02:00
|
|
|
bool SetCrashKeyValue(const std::string_view& key,
|
|
|
|
const std::string_view& value) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!g_crash_reporting_enabled) {
|
2018-02-15 01:12:09 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2018-02-15 01:12:09 +01:00
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2018-02-15 01:12:09 +01:00
|
|
|
return SetCrashKeyValueTrampoline(key, value);
|
|
|
|
#else
|
|
|
|
return g_crash_reporter_client.Pointer()->SetCrashKeyValue(key, value);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_POSIX)
|
2016-12-12 11:05:29 +01:00
|
|
|
// Be aware that logging is not initialized at the time this method is called.
|
|
|
|
void BasicStartupComplete(base::CommandLine* command_line) {
|
2024-02-22 19:36:15 +01:00
|
|
|
g_crash_reporter_client.Pointer()->ReadCrashConfigFile();
|
2016-12-12 11:05:29 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void PreSandboxStartup(const base::CommandLine& command_line,
|
|
|
|
const std::string& process_type) {
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_POSIX)
|
2016-12-12 11:05:29 +01:00
|
|
|
// Initialize crash reporting here on macOS and Linux. Crash reporting on
|
|
|
|
// Windows is initialized from context.cc.
|
|
|
|
InitCrashReporter(command_line, process_type);
|
2022-01-24 18:58:02 +01:00
|
|
|
#elif BUILDFLAG(IS_WIN)
|
2018-02-15 01:12:09 +01:00
|
|
|
g_crash_reporting_enabled = IsCrashReportingEnabledTrampoline();
|
2016-12-12 11:05:29 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (g_crash_reporting_enabled) {
|
2017-05-17 11:29:28 +02:00
|
|
|
LOG(INFO) << "Crash reporting enabled for process: "
|
|
|
|
<< (process_type.empty() ? "browser" : process_type.c_str());
|
2016-12-12 11:05:29 +01:00
|
|
|
}
|
|
|
|
|
2017-12-07 22:44:24 +01:00
|
|
|
crash_reporter::InitializeCrashKeys();
|
|
|
|
|
2016-12-12 11:05:29 +01:00
|
|
|
// After platform crash reporting have been initialized, store the command
|
|
|
|
// line for crash reporting.
|
|
|
|
crash_keys::SetSwitchesFromCommandLine(command_line, &IsBoringCEFSwitch);
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_MAC)
|
2016-12-12 11:05:29 +01:00
|
|
|
void ZygoteForked(base::CommandLine* command_line,
|
|
|
|
const std::string& process_type) {
|
|
|
|
InitCrashReporter(*command_line, process_type);
|
|
|
|
|
|
|
|
if (g_crash_reporting_enabled) {
|
|
|
|
LOG(INFO) << "Crash reporting enabled for process: " << process_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the command line for the newly spawned process.
|
|
|
|
crash_keys::SetSwitchesFromCommandLine(*command_line, &IsBoringCEFSwitch);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // namespace crash_reporting
|
|
|
|
|
|
|
|
bool CefCrashReportingEnabled() {
|
2017-01-23 18:36:54 +01:00
|
|
|
return crash_reporting::Enabled();
|
2016-12-12 11:05:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefSetCrashKeyValue(const CefString& key, const CefString& value) {
|
2018-02-15 01:12:09 +01:00
|
|
|
if (!crash_reporting::SetCrashKeyValue(key.ToString(), value.ToString())) {
|
|
|
|
LOG(WARNING) << "Failed to set crash key: " << key.ToString()
|
|
|
|
<< " with value: " << value.ToString();
|
|
|
|
}
|
2016-12-12 11:05:29 +01:00
|
|
|
}
|