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 "base/strings/string16.h" | |
14 #include "breakpad/src/client/windows/handler/exception_handler.h" | |
15 #include "chrome/common/chrome_version.h" | |
16 #include "chrome/install_static/install_util.h" | |
17 | |
18 google_breakpad::ExceptionHandler* g_elf_breakpad = NULL; | |
19 | |
20 namespace { | |
21 | |
22 const wchar_t kBreakpadProductName[] = L"Chrome"; | |
23 const wchar_t kBreakpadVersionEntry[] = L"ver"; | |
24 const wchar_t kBreakpadProdEntry[] = L"prod"; | |
25 const wchar_t kBreakpadPlatformEntry[] = L"plat"; | |
26 const wchar_t kBreakpadPlatformWin32[] = L"Win32"; | |
27 const wchar_t kBreakpadProcessEntry[] = L"ptype"; | |
28 const wchar_t kBreakpadChannelEntry[] = L"channel"; | |
29 | |
30 // The protocol for connecting to the out-of-process Breakpad crash | |
31 // reporter is different for x86-32 and x86-64: the message sizes | |
32 // are different because the message struct contains a pointer. As | |
33 // a result, there are two different named pipes to connect to. The | |
34 // 64-bit one is distinguished with an "-x64" suffix. | |
35 const wchar_t kChromePipeName[] = L"\\\\.\\pipe\\ChromeCrashServices\\"; | |
36 const wchar_t kGoogleUpdatePipeName[] = L"\\\\.\\pipe\\GoogleCrashServices\\"; | |
37 const wchar_t kSystemPrincipalSid[] = L"S-1-5-18"; | |
38 | |
39 const wchar_t kNoErrorDialogs[] = L"noerrdialogs"; | |
40 | |
41 google_breakpad::CustomClientInfo* GetCustomInfo() { | |
42 base::string16 process = | |
43 install_static::IsNonBrowserProcess() ? L"renderer" : L"browser"; | |
44 | |
45 wchar_t exe_path[MAX_PATH] = {}; | |
46 base::string16 channel; | |
47 if (GetModuleFileName(NULL, exe_path, arraysize(exe_path)) && | |
48 install_static::IsSxSChrome(exe_path)) { | |
49 channel = L"canary"; | |
50 } | |
51 | |
52 static google_breakpad::CustomInfoEntry ver_entry( | |
53 kBreakpadVersionEntry, TEXT(CHROME_VERSION_STRING)); | |
54 static google_breakpad::CustomInfoEntry prod_entry( | |
55 kBreakpadProdEntry, kBreakpadProductName); | |
56 static google_breakpad::CustomInfoEntry plat_entry( | |
57 kBreakpadPlatformEntry, kBreakpadPlatformWin32); | |
58 static google_breakpad::CustomInfoEntry proc_entry( | |
59 kBreakpadProcessEntry, process.c_str()); | |
60 static google_breakpad::CustomInfoEntry channel_entry( | |
61 kBreakpadChannelEntry, channel.c_str()); | |
62 static google_breakpad::CustomInfoEntry entries[] = { | |
63 ver_entry, prod_entry, plat_entry, proc_entry, channel_entry}; | |
64 static google_breakpad::CustomClientInfo custom_info = { | |
65 entries, arraysize(entries) }; | |
66 return &custom_info; | |
67 } | |
68 | |
69 base::string16 GetUserSidString() { | |
70 // Get the current token. | |
71 HANDLE token = NULL; | |
72 base::string16 user_sid; | |
73 if (!::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &token)) | |
74 return user_sid; | |
75 | |
76 DWORD size = sizeof(TOKEN_USER) + SECURITY_MAX_SID_SIZE; | |
77 BYTE user_bytes[sizeof(TOKEN_USER) + SECURITY_MAX_SID_SIZE] = {}; | |
78 TOKEN_USER* user = reinterpret_cast<TOKEN_USER*>(user_bytes); | |
79 | |
80 wchar_t* sid_string = NULL; | |
81 if (::GetTokenInformation(token, TokenUser, user, size, &size) && | |
82 user->User.Sid && | |
83 ::ConvertSidToStringSid(user->User.Sid, &sid_string)) { | |
84 user_sid = sid_string; | |
85 ::LocalFree(sid_string); | |
86 } | |
87 | |
88 CloseHandle(token); | |
89 return user_sid; | |
90 } | |
91 | |
92 bool IsHeadless() { | |
93 DWORD ret = ::GetEnvironmentVariable(L"CHROME_HEADLESS", NULL, 0); | |
94 if (ret != 0) | |
95 return true; | |
96 | |
97 wchar_t* command_line = ::GetCommandLine(); | |
98 | |
99 // Note: Since this is a pure substring search rather than a check for a | |
100 // switch, there is a small chance that this code will match things that the | |
101 // Chrome code (which executes a similar check) does not. However, as long as | |
102 // no other switches contain the string "noerrdialogs", it should not be an | |
103 // issue. | |
104 return (command_line && wcsstr(command_line, kNoErrorDialogs)); | |
105 } | |
106 | |
107 } // namespace | |
108 | |
109 int GenerateCrashDump(EXCEPTION_POINTERS* exinfo) { | |
110 DWORD code = exinfo->ExceptionRecord->ExceptionCode; | |
111 if (code == EXCEPTION_BREAKPOINT || code == EXCEPTION_SINGLE_STEP) | |
112 return EXCEPTION_CONTINUE_SEARCH; | |
113 | |
114 if (g_elf_breakpad != NULL) | |
115 g_elf_breakpad->WriteMinidumpForException(exinfo); | |
116 return EXCEPTION_CONTINUE_SEARCH; | |
117 } | |
118 | |
119 void InitializeCrashReporting() { | |
120 wchar_t exe_path[MAX_PATH] = {}; | |
121 if (!::GetModuleFileName(NULL, exe_path, arraysize(exe_path))) | |
122 return; | |
123 | |
124 // Disable the message box for assertions. | |
125 _CrtSetReportMode(_CRT_ASSERT, 0); | |
126 | |
127 // Get the alternate dump directory. We use the temp path. | |
128 // N.B. We don't use base::GetTempDir() here to avoid running more code then | |
129 // necessary before crashes can be properly reported. | |
130 wchar_t temp_directory[MAX_PATH + 1] = {}; | |
131 DWORD length = GetTempPath(MAX_PATH, temp_directory); | |
132 if (length == 0) | |
133 return; | |
134 | |
135 // Minidump with stacks, PEB, TEBs and unloaded module list. | |
136 MINIDUMP_TYPE dump_type = static_cast<MINIDUMP_TYPE>( | |
137 MiniDumpWithProcessThreadData | // Get PEB and TEB. | |
138 MiniDumpWithUnloadedModules | // Get unloaded modules when available. | |
139 MiniDumpWithIndirectlyReferencedMemory); // Get memory referenced by | |
140 // stack. | |
141 | |
142 #if defined(GOOGLE_CHROME_BUILD) && defined(OFFICIAL_BUILD) | |
143 bool is_official_chrome_build = true; | |
144 #else | |
145 bool is_official_chrome_build = false; | |
146 #endif | |
147 | |
148 base::string16 pipe_name; | |
149 | |
150 bool enabled_by_policy = false; | |
151 bool use_policy = | |
152 install_static::ReportingIsEnforcedByPolicy(&enabled_by_policy); | |
153 | |
154 if (!use_policy && IsHeadless()) { | |
155 pipe_name = kChromePipeName; | |
156 } else if (use_policy ? enabled_by_policy | |
157 : (is_official_chrome_build && | |
158 install_static::GetCollectStatsConsent())) { | |
159 // Build the pipe name. It can be one of: | |
160 // 32-bit system: \\.\pipe\GoogleCrashServices\S-1-5-18 | |
161 // 32-bit user: \\.\pipe\GoogleCrashServices\<user SID> | |
162 // 64-bit system: \\.\pipe\GoogleCrashServices\S-1-5-18-x64 | |
163 // 64-bit user: \\.\pipe\GoogleCrashServices\<user SID>-x64 | |
164 base::string16 user_sid = install_static::IsSystemInstall(exe_path) | |
165 ? kSystemPrincipalSid | |
166 : GetUserSidString(); | |
167 if (user_sid.empty()) | |
168 return; | |
169 | |
170 pipe_name = kGoogleUpdatePipeName; | |
171 pipe_name += user_sid; | |
172 | |
173 #if defined(_WIN64) | |
174 pipe_name += L"-x64"; | |
175 #endif | |
176 } else { | |
177 // Either this is a Chromium build, reporting is disabled by policy or the | |
178 // user has not given consent. | |
179 return; | |
180 } | |
181 | |
182 g_elf_breakpad = new google_breakpad::ExceptionHandler( | |
183 temp_directory, | |
184 NULL, | |
185 NULL, | |
186 NULL, | |
187 google_breakpad::ExceptionHandler::HANDLER_ALL, | |
188 dump_type, | |
189 pipe_name.c_str(), | |
190 GetCustomInfo()); | |
191 | |
192 if (g_elf_breakpad->IsOutOfProcess()) { | |
193 // Tells breakpad to handle breakpoint and single step exceptions. | |
194 g_elf_breakpad->set_handle_debug_exceptions(true); | |
195 } | |
196 } | |
OLD | NEW |