OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // This module contains the necessary code to register the Breakpad exception | |
6 // handler. This implementation is based on Chrome's crash reporting code. | |
7 | |
8 #include "chrome_elf/breakpad.h" | |
9 | |
10 #include <sddl.h> | |
11 | |
12 #include "base/macros.h" | |
13 #include "breakpad/src/client/windows/handler/exception_handler.h" | |
14 #include "chrome_elf/chrome_elf_util.h" | |
15 #include "version.h" // NOLINT | |
16 | |
17 google_breakpad::ExceptionHandler* g_elf_breakpad = NULL; | |
18 | |
19 namespace { | |
20 | |
21 const wchar_t kBreakpadProductName[] = L"ChromeElf"; | |
22 const wchar_t kBreakpadVersionEntry[] = L"ver"; | |
23 const wchar_t kBreakpadProdEntry[] = L"prod"; | |
24 const wchar_t kBreakpadPlatformEntry[] = L"plat"; | |
25 const wchar_t kBreakpadPlatformWin32[] = L"Win32"; | |
26 | |
27 // The protocol for connecting to the out-of-process Breakpad crash | |
28 // reporter is different for x86-32 and x86-64: the message sizes | |
29 // are different because the message struct contains a pointer. As | |
30 // a result, there are two different named pipes to connect to. The | |
31 // 64-bit one is distinguished with an "-x64" suffix. | |
32 const wchar_t kChromePipeName[] = L"\\\\.\\pipe\\ChromeCrashServices\\"; | |
33 const wchar_t kGoogleUpdatePipeName[] = L"\\\\.\\pipe\\GoogleCrashServices\\"; | |
34 const wchar_t kSystemPrincipalSid[] = L"S-1-5-18"; | |
35 | |
36 const wchar_t kNoErrorDialogs[] = L"noerrdialogs"; | |
37 const wchar_t kChromeHeadless[] = L"CHROME_HEADLESS"; | |
38 | |
39 google_breakpad::CustomClientInfo* GetCustomInfo() { | |
40 static google_breakpad::CustomInfoEntry ver_entry( | |
41 kBreakpadVersionEntry, TEXT(CHROME_VERSION_STRING)); | |
42 static google_breakpad::CustomInfoEntry prod_entry( | |
43 kBreakpadProdEntry, kBreakpadProductName); | |
44 static google_breakpad::CustomInfoEntry plat_entry( | |
45 kBreakpadPlatformEntry, kBreakpadPlatformWin32); | |
46 static google_breakpad::CustomInfoEntry entries[] = { | |
47 ver_entry, prod_entry, plat_entry }; | |
48 static google_breakpad::CustomClientInfo custom_info = { | |
49 entries, arraysize(entries) }; | |
50 return &custom_info; | |
51 } | |
52 | |
53 base::string16 GetUserSidString() { | |
54 // Get the current token. | |
55 HANDLE token = NULL; | |
56 base::string16 user_sid; | |
57 if (!::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &token)) | |
58 return user_sid; | |
59 | |
60 DWORD size = sizeof(TOKEN_USER) + SECURITY_MAX_SID_SIZE; | |
61 BYTE user_bytes[sizeof(TOKEN_USER) + SECURITY_MAX_SID_SIZE] = {}; | |
62 TOKEN_USER* user = reinterpret_cast<TOKEN_USER*>(user_bytes); | |
63 | |
64 wchar_t* sid_string; | |
65 if (::GetTokenInformation(token, TokenUser, user, size, &size) && | |
66 user->User.Sid && ::ConvertSidToStringSid(user->User.Sid, &sid_string)) { | |
grt (UTC plus 2)
2014/02/17 20:49:39
nit: i find this a little easier to read with ::Co
Cait (Slow)
2014/02/18 23:03:12
Done.
| |
67 user_sid = sid_string; | |
68 ::LocalFree(sid_string); | |
69 } | |
70 | |
71 CloseHandle(token); | |
72 return user_sid; | |
73 } | |
74 | |
75 bool IsHeadless() { | |
76 DWORD ret = ::GetEnvironmentVariable(L"CHROME_HEADLESS", NULL, 0); | |
77 bool has_env_var = ret != 0; | |
grt (UTC plus 2)
2014/02/17 20:49:39
if (ret != 0)
return true;
Cait (Slow)
2014/02/18 23:03:12
Done.
Cait (Slow)
2014/02/18 23:03:12
Done.
| |
78 | |
79 wchar_t* command_line = ::GetCommandLine(); | |
80 bool has_flag = (command_line && wcsstr(command_line, kNoErrorDialogs)); | |
grt (UTC plus 2)
2014/02/17 20:49:39
i'm a little worried that this will match things t
Cait (Slow)
2014/02/18 23:03:12
Done.
| |
81 | |
82 return has_env_var || has_flag; | |
83 } | |
84 | |
85 } // namespace | |
86 | |
87 int GenerateCrashDump(EXCEPTION_POINTERS* exinfo) { | |
88 DWORD code = exinfo->ExceptionRecord->ExceptionCode; | |
89 if (code == EXCEPTION_BREAKPOINT || code == EXCEPTION_SINGLE_STEP) | |
90 return EXCEPTION_CONTINUE_SEARCH; | |
91 | |
92 if (g_elf_breakpad != NULL) | |
93 g_elf_breakpad->WriteMinidumpForException(exinfo); | |
94 return EXCEPTION_CONTINUE_SEARCH; | |
95 } | |
96 | |
97 void InitializeCrashReporting() { | |
98 wchar_t exe_path[MAX_PATH] = {}; | |
99 if(!::GetModuleFileName(NULL, exe_path, arraysize(exe_path))) | |
100 return; | |
101 | |
102 // Do not set up crash reporting if the user has not consented to it. | |
103 if (!AreUsageStatsEnabled(exe_path)) | |
grt (UTC plus 2)
2014/02/17 20:49:39
policy trumps usagestats, so only check this if !u
Cait (Slow)
2014/02/18 23:03:12
Done.
| |
104 return; | |
105 | |
106 // Disable the message box for assertions. | |
107 _CrtSetReportMode(_CRT_ASSERT, 0); | |
108 | |
109 // Get the alternate dump directory. We use the temp path. | |
110 // N.B. We don't use base::GetTempDir() here to avoid running more code then | |
111 // necessary before crashes can be properly reported. | |
112 wchar_t temp_directory[MAX_PATH + 1] = {}; | |
113 DWORD length = GetTempPath(MAX_PATH, temp_directory); | |
114 if (length == 0) | |
115 return; | |
116 | |
117 // Minidump with stacks, PEB, TEBs and unloaded module list. | |
118 MINIDUMP_TYPE dump_type = static_cast<MINIDUMP_TYPE>( | |
119 MiniDumpWithProcessThreadData | // Get PEB and TEB. | |
120 MiniDumpWithUnloadedModules | // Get unloaded modules when available. | |
121 MiniDumpWithIndirectlyReferencedMemory); // Get memory referenced by | |
122 // stack. | |
123 | |
124 base::string16 pipe_name; | |
125 | |
126 bool enabled; | |
grt (UTC plus 2)
2014/02/17 20:49:39
nit: initialize to something. also, consider renam
Cait (Slow)
2014/02/18 23:03:12
Done.
| |
127 bool use_policy = ReportingIsEnforcedByPolicy(&enabled); | |
128 | |
129 if (!use_policy && IsHeadless()) { | |
130 pipe_name = kChromePipeName; | |
131 } else { | |
grt (UTC plus 2)
2014/02/17 20:49:39
consider:
} else if (use_policy ? enabled : AreU
Cait (Slow)
2014/02/18 23:03:12
Done.
| |
132 // Build the pipe name. It can be one of: | |
133 // 32-bit system: \\.\pipe\GoogleCrashServices\S-1-5-18 | |
134 // 32-bit user: \\.\pipe\GoogleCrashServices\<user SID> | |
135 // 64-bit system: \\.\pipe\GoogleCrashServices\S-1-5-18-x64 | |
136 // 64-bit user: \\.\pipe\GoogleCrashServices\<user SID>-x64 | |
137 base::string16 user_sid = IsSystemInstall(exe_path) ? kSystemPrincipalSid : | |
138 GetUserSidString(); | |
139 if (user_sid.empty()) | |
140 return; | |
141 | |
142 base::string16 pipe_name = kGoogleUpdatePipeName; | |
143 pipe_name += user_sid; | |
144 | |
145 #if defined(_WIN64) | |
146 pipe_name += L"-x64"; | |
147 #endif | |
148 } | |
149 | |
150 g_elf_breakpad = new google_breakpad::ExceptionHandler( | |
151 temp_directory, | |
152 NULL, | |
153 NULL, | |
154 NULL, | |
155 google_breakpad::ExceptionHandler::HANDLER_ALL, | |
156 dump_type, | |
157 pipe_name.c_str(), | |
158 GetCustomInfo()); | |
159 | |
160 if (g_elf_breakpad->IsOutOfProcess()) { | |
161 // Tells breakpad to handle breakpoint and single step exceptions. | |
162 g_elf_breakpad->set_handle_debug_exceptions(true); | |
163 } | |
164 } | |
OLD | NEW |