This source file includes following definitions.
- GetCustomInfo
- GetUserSidString
- IsHeadless
- GenerateCrashDump
- InitializeCrashReporting
#include "chrome_elf/breakpad.h"
#include <sddl.h>
#include "base/macros.h"
#include "breakpad/src/client/windows/handler/exception_handler.h"
#include "chrome_elf/chrome_elf_util.h"
#include "version.h"
google_breakpad::ExceptionHandler* g_elf_breakpad = NULL;
namespace {
const wchar_t kBreakpadProductName[] = L"Chrome";
const wchar_t kBreakpadVersionEntry[] = L"ver";
const wchar_t kBreakpadProdEntry[] = L"prod";
const wchar_t kBreakpadPlatformEntry[] = L"plat";
const wchar_t kBreakpadPlatformWin32[] = L"Win32";
const wchar_t kChromePipeName[] = L"\\\\.\\pipe\\ChromeCrashServices\\";
const wchar_t kGoogleUpdatePipeName[] = L"\\\\.\\pipe\\GoogleCrashServices\\";
const wchar_t kSystemPrincipalSid[] = L"S-1-5-18";
const wchar_t kNoErrorDialogs[] = L"noerrdialogs";
const wchar_t kChromeHeadless[] = L"CHROME_HEADLESS";
google_breakpad::CustomClientInfo* GetCustomInfo() {
static google_breakpad::CustomInfoEntry ver_entry(
kBreakpadVersionEntry, TEXT(CHROME_VERSION_STRING));
static google_breakpad::CustomInfoEntry prod_entry(
kBreakpadProdEntry, kBreakpadProductName);
static google_breakpad::CustomInfoEntry plat_entry(
kBreakpadPlatformEntry, kBreakpadPlatformWin32);
static google_breakpad::CustomInfoEntry entries[] = {
ver_entry, prod_entry, plat_entry };
static google_breakpad::CustomClientInfo custom_info = {
entries, arraysize(entries) };
return &custom_info;
}
base::string16 GetUserSidString() {
HANDLE token = NULL;
base::string16 user_sid;
if (!::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &token))
return user_sid;
DWORD size = sizeof(TOKEN_USER) + SECURITY_MAX_SID_SIZE;
BYTE user_bytes[sizeof(TOKEN_USER) + SECURITY_MAX_SID_SIZE] = {};
TOKEN_USER* user = reinterpret_cast<TOKEN_USER*>(user_bytes);
wchar_t* sid_string = NULL;
if (::GetTokenInformation(token, TokenUser, user, size, &size) &&
user->User.Sid &&
::ConvertSidToStringSid(user->User.Sid, &sid_string)) {
user_sid = sid_string;
::LocalFree(sid_string);
}
CloseHandle(token);
return user_sid;
}
bool IsHeadless() {
DWORD ret = ::GetEnvironmentVariable(L"CHROME_HEADLESS", NULL, 0);
if (ret != 0)
return true;
wchar_t* command_line = ::GetCommandLine();
return (command_line && wcsstr(command_line, kNoErrorDialogs));
}
}
int GenerateCrashDump(EXCEPTION_POINTERS* exinfo) {
DWORD code = exinfo->ExceptionRecord->ExceptionCode;
if (code == EXCEPTION_BREAKPOINT || code == EXCEPTION_SINGLE_STEP)
return EXCEPTION_CONTINUE_SEARCH;
if (g_elf_breakpad != NULL)
g_elf_breakpad->WriteMinidumpForException(exinfo);
return EXCEPTION_CONTINUE_SEARCH;
}
void InitializeCrashReporting() {
wchar_t exe_path[MAX_PATH] = {};
if(!::GetModuleFileName(NULL, exe_path, arraysize(exe_path)))
return;
_CrtSetReportMode(_CRT_ASSERT, 0);
wchar_t temp_directory[MAX_PATH + 1] = {};
DWORD length = GetTempPath(MAX_PATH, temp_directory);
if (length == 0)
return;
MINIDUMP_TYPE dump_type = static_cast<MINIDUMP_TYPE>(
MiniDumpWithProcessThreadData |
MiniDumpWithUnloadedModules |
MiniDumpWithIndirectlyReferencedMemory);
#if defined(GOOGLE_CHROME_BUILD)
bool is_chrome_build = true;
#else
bool is_chrome_build = false;
#endif
base::string16 pipe_name;
bool enabled_by_policy = false;
bool use_policy = ReportingIsEnforcedByPolicy(&enabled_by_policy);
if (!use_policy && IsHeadless()) {
pipe_name = kChromePipeName;
} else if (use_policy ? enabled_by_policy :
(is_chrome_build && AreUsageStatsEnabled(exe_path))) {
base::string16 user_sid = IsSystemInstall(exe_path) ? kSystemPrincipalSid :
GetUserSidString();
if (user_sid.empty())
return;
pipe_name = kGoogleUpdatePipeName;
pipe_name += user_sid;
#if defined(_WIN64)
pipe_name += L"-x64";
#endif
} else {
return;
}
g_elf_breakpad = new google_breakpad::ExceptionHandler(
temp_directory,
NULL,
NULL,
NULL,
google_breakpad::ExceptionHandler::HANDLER_ALL,
dump_type,
pipe_name.c_str(),
GetCustomInfo());
if (g_elf_breakpad->IsOutOfProcess()) {
g_elf_breakpad->set_handle_debug_exceptions(true);
}
}