Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: chrome_elf/breakpad/breakpad.cc

Issue 1841573002: [Chrome ELF] New NT registry API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: PRESUBMIT to allow chrome_elf directory files to use wstring. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome_elf/breakpad/breakpad.h ('k') | chrome_elf/chrome_elf.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This module contains the necessary code to register the Breakpad exception 5 // This module contains the necessary code to register the Breakpad exception
6 // handler. This implementation is based on Chrome's crash reporting code. 6 // handler. This implementation is based on Chrome's crash reporting code.
7 7
8 #include "chrome_elf/breakpad.h" 8 #include "chrome_elf/breakpad/breakpad.h"
9 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" 10 #include "breakpad/src/client/windows/handler/exception_handler.h"
15 #include "chrome/common/chrome_version.h" 11 #include "chrome/common/chrome_version.h"
16 #include "chrome/install_static/install_util.h" 12 #include "chrome/install_static/install_util.h"
13 #include "chrome_elf/nt_registry/nt_registry.h"
17 14
18 google_breakpad::ExceptionHandler* g_elf_breakpad = NULL; 15 google_breakpad::ExceptionHandler* g_elf_breakpad = NULL;
19 16
20 namespace { 17 namespace {
21 18
22 const wchar_t kBreakpadProductName[] = L"Chrome"; 19 const wchar_t kBreakpadProductName[] = L"Chrome";
23 const wchar_t kBreakpadVersionEntry[] = L"ver"; 20 const wchar_t kBreakpadVersionEntry[] = L"ver";
24 const wchar_t kBreakpadProdEntry[] = L"prod"; 21 const wchar_t kBreakpadProdEntry[] = L"prod";
25 const wchar_t kBreakpadPlatformEntry[] = L"plat"; 22 const wchar_t kBreakpadPlatformEntry[] = L"plat";
26 const wchar_t kBreakpadPlatformWin32[] = L"Win32"; 23 const wchar_t kBreakpadPlatformWin32[] = L"Win32";
27 const wchar_t kBreakpadProcessEntry[] = L"ptype"; 24 const wchar_t kBreakpadProcessEntry[] = L"ptype";
28 const wchar_t kBreakpadChannelEntry[] = L"channel"; 25 const wchar_t kBreakpadChannelEntry[] = L"channel";
29 26
30 // The protocol for connecting to the out-of-process Breakpad crash 27 // The protocol for connecting to the out-of-process Breakpad crash
31 // reporter is different for x86-32 and x86-64: the message sizes 28 // reporter is different for x86-32 and x86-64: the message sizes
32 // are different because the message struct contains a pointer. As 29 // are different because the message struct contains a pointer. As
33 // a result, there are two different named pipes to connect to. The 30 // a result, there are two different named pipes to connect to. The
34 // 64-bit one is distinguished with an "-x64" suffix. 31 // 64-bit one is distinguished with an "-x64" suffix.
35 const wchar_t kChromePipeName[] = L"\\\\.\\pipe\\ChromeCrashServices\\"; 32 const wchar_t kChromePipeName[] = L"\\\\.\\pipe\\ChromeCrashServices\\";
36 const wchar_t kGoogleUpdatePipeName[] = L"\\\\.\\pipe\\GoogleCrashServices\\"; 33 const wchar_t kGoogleUpdatePipeName[] = L"\\\\.\\pipe\\GoogleCrashServices\\";
37 const wchar_t kSystemPrincipalSid[] = L"S-1-5-18"; 34 const wchar_t kSystemPrincipalSid[] = L"S-1-5-18";
38 35
39 const wchar_t kNoErrorDialogs[] = L"noerrdialogs"; 36 const wchar_t kNoErrorDialogs[] = L"noerrdialogs";
40 37
41 google_breakpad::CustomClientInfo* GetCustomInfo() { 38 google_breakpad::CustomClientInfo* GetCustomInfo() {
42 base::string16 process = 39 std::wstring process =
43 install_static::IsNonBrowserProcess() ? L"renderer" : L"browser"; 40 install_static::IsNonBrowserProcess() ? L"renderer" : L"browser";
44 41
45 wchar_t exe_path[MAX_PATH] = {}; 42 wchar_t exe_path[MAX_PATH] = {};
46 base::string16 channel; 43 std::wstring channel;
47 if (GetModuleFileName(NULL, exe_path, arraysize(exe_path)) && 44 if (GetModuleFileName(NULL, exe_path, MAX_PATH) &&
48 install_static::IsSxSChrome(exe_path)) { 45 install_static::IsSxSChrome(exe_path)) {
49 channel = L"canary"; 46 channel = L"canary";
50 } 47 }
51 48
52 static google_breakpad::CustomInfoEntry ver_entry( 49 static google_breakpad::CustomInfoEntry ver_entry(
53 kBreakpadVersionEntry, TEXT(CHROME_VERSION_STRING)); 50 kBreakpadVersionEntry, TEXT(CHROME_VERSION_STRING));
54 static google_breakpad::CustomInfoEntry prod_entry( 51 static google_breakpad::CustomInfoEntry prod_entry(kBreakpadProdEntry,
55 kBreakpadProdEntry, kBreakpadProductName); 52 kBreakpadProductName);
56 static google_breakpad::CustomInfoEntry plat_entry( 53 static google_breakpad::CustomInfoEntry plat_entry(kBreakpadPlatformEntry,
57 kBreakpadPlatformEntry, kBreakpadPlatformWin32); 54 kBreakpadPlatformWin32);
58 static google_breakpad::CustomInfoEntry proc_entry( 55 static google_breakpad::CustomInfoEntry proc_entry(kBreakpadProcessEntry,
59 kBreakpadProcessEntry, process.c_str()); 56 process.c_str());
60 static google_breakpad::CustomInfoEntry channel_entry( 57 static google_breakpad::CustomInfoEntry channel_entry(kBreakpadChannelEntry,
61 kBreakpadChannelEntry, channel.c_str()); 58 channel.c_str());
62 static google_breakpad::CustomInfoEntry entries[] = { 59 static google_breakpad::CustomInfoEntry entries[] = {
63 ver_entry, prod_entry, plat_entry, proc_entry, channel_entry}; 60 ver_entry, prod_entry, plat_entry, proc_entry, channel_entry};
64 static google_breakpad::CustomClientInfo custom_info = { 61 static google_breakpad::CustomClientInfo custom_info = {
65 entries, arraysize(entries) }; 62 entries, (sizeof(entries) / sizeof(google_breakpad::CustomInfoEntry))};
66 return &custom_info; 63 return &custom_info;
67 } 64 }
68 65
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() { 66 bool IsHeadless() {
93 DWORD ret = ::GetEnvironmentVariable(L"CHROME_HEADLESS", NULL, 0); 67 DWORD ret = ::GetEnvironmentVariable(L"CHROME_HEADLESS", NULL, 0);
94 if (ret != 0) 68 if (ret != 0)
95 return true; 69 return true;
96 70
97 wchar_t* command_line = ::GetCommandLine(); 71 wchar_t* command_line = ::GetCommandLine();
98 72
99 // Note: Since this is a pure substring search rather than a check for a 73 // 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 74 // 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 75 // 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 76 // no other switches contain the string "noerrdialogs", it should not be an
103 // issue. 77 // issue.
104 return (command_line && wcsstr(command_line, kNoErrorDialogs)); 78 return (command_line && wcsstr(command_line, kNoErrorDialogs));
105 } 79 }
106 80
107 } // namespace 81 } // namespace
108 82
109 int GenerateCrashDump(EXCEPTION_POINTERS* exinfo) { 83 int GenerateCrashDump(EXCEPTION_POINTERS* exinfo) {
110 DWORD code = exinfo->ExceptionRecord->ExceptionCode; 84 DWORD code = exinfo->ExceptionRecord->ExceptionCode;
111 if (code == EXCEPTION_BREAKPOINT || code == EXCEPTION_SINGLE_STEP) 85 if (code == EXCEPTION_BREAKPOINT || code == EXCEPTION_SINGLE_STEP)
112 return EXCEPTION_CONTINUE_SEARCH; 86 return EXCEPTION_CONTINUE_SEARCH;
113 87
114 if (g_elf_breakpad != NULL) 88 if (g_elf_breakpad != NULL)
115 g_elf_breakpad->WriteMinidumpForException(exinfo); 89 g_elf_breakpad->WriteMinidumpForException(exinfo);
116 return EXCEPTION_CONTINUE_SEARCH; 90 return EXCEPTION_CONTINUE_SEARCH;
117 } 91 }
118 92
119 void InitializeCrashReporting() { 93 void InitializeCrashReporting() {
120 wchar_t exe_path[MAX_PATH] = {}; 94 wchar_t exe_path[MAX_PATH] = {};
121 if (!::GetModuleFileName(NULL, exe_path, arraysize(exe_path))) 95 if (!::GetModuleFileName(NULL, exe_path, MAX_PATH))
122 return; 96 return;
123 97
124 // Disable the message box for assertions. 98 // Disable the message box for assertions.
125 _CrtSetReportMode(_CRT_ASSERT, 0); 99 _CrtSetReportMode(_CRT_ASSERT, 0);
126 100
127 // Get the alternate dump directory. We use the temp path. 101 // 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 102 // N.B. We don't use base::GetTempDir() here to avoid running more code then
129 // necessary before crashes can be properly reported. 103 // necessary before crashes can be properly reported.
130 wchar_t temp_directory[MAX_PATH + 1] = {}; 104 wchar_t temp_directory[MAX_PATH + 1] = {};
131 DWORD length = GetTempPath(MAX_PATH, temp_directory); 105 DWORD length = GetTempPath(MAX_PATH, temp_directory);
132 if (length == 0) 106 if (length == 0)
133 return; 107 return;
134 108
135 // Minidump with stacks, PEB, TEBs and unloaded module list. 109 // Minidump with stacks, PEB, TEBs and unloaded module list.
136 MINIDUMP_TYPE dump_type = static_cast<MINIDUMP_TYPE>( 110 MINIDUMP_TYPE dump_type = static_cast<MINIDUMP_TYPE>(
137 MiniDumpWithProcessThreadData | // Get PEB and TEB. 111 MiniDumpWithProcessThreadData | // Get PEB and TEB.
138 MiniDumpWithUnloadedModules | // Get unloaded modules when available. 112 MiniDumpWithUnloadedModules | // Get unloaded modules when available.
139 MiniDumpWithIndirectlyReferencedMemory); // Get memory referenced by 113 MiniDumpWithIndirectlyReferencedMemory); // Get memory referenced by
140 // stack. 114 // stack.
141 115
142 #if defined(GOOGLE_CHROME_BUILD) && defined(OFFICIAL_BUILD) 116 #if defined(GOOGLE_CHROME_BUILD) && defined(OFFICIAL_BUILD)
143 bool is_official_chrome_build = true; 117 bool is_official_chrome_build = true;
144 #else 118 #else
145 bool is_official_chrome_build = false; 119 bool is_official_chrome_build = false;
146 #endif 120 #endif
147 121
148 base::string16 pipe_name; 122 std::wstring pipe_name;
149 123
150 bool enabled_by_policy = false; 124 bool enabled_by_policy = false;
151 bool use_policy = 125 bool use_policy =
152 install_static::ReportingIsEnforcedByPolicy(&enabled_by_policy); 126 install_static::ReportingIsEnforcedByPolicy(&enabled_by_policy);
153 127
154 if (!use_policy && IsHeadless()) { 128 if (!use_policy && IsHeadless()) {
155 pipe_name = kChromePipeName; 129 pipe_name = kChromePipeName;
156 } else if (use_policy ? enabled_by_policy 130 } else if (use_policy ? enabled_by_policy
157 : (is_official_chrome_build && 131 : (is_official_chrome_build &&
158 install_static::GetCollectStatsConsent())) { 132 install_static::GetCollectStatsConsent())) {
159 // Build the pipe name. It can be one of: 133 // Build the pipe name. It can be one of:
160 // 32-bit system: \\.\pipe\GoogleCrashServices\S-1-5-18 134 // 32-bit system: \\.\pipe\GoogleCrashServices\S-1-5-18
161 // 32-bit user: \\.\pipe\GoogleCrashServices\<user SID> 135 // 32-bit user: \\.\pipe\GoogleCrashServices\<user SID>
162 // 64-bit system: \\.\pipe\GoogleCrashServices\S-1-5-18-x64 136 // 64-bit system: \\.\pipe\GoogleCrashServices\S-1-5-18-x64
163 // 64-bit user: \\.\pipe\GoogleCrashServices\<user SID>-x64 137 // 64-bit user: \\.\pipe\GoogleCrashServices\<user SID>-x64
164 base::string16 user_sid = install_static::IsSystemInstall(exe_path) 138 std::wstring user_sid = install_static::IsSystemInstall(exe_path)
165 ? kSystemPrincipalSid 139 ? kSystemPrincipalSid
166 : GetUserSidString(); 140 : nt::GetCurrentUserSidString();
167 if (user_sid.empty()) 141 if (user_sid.empty())
168 return; 142 return;
169 143
170 pipe_name = kGoogleUpdatePipeName; 144 pipe_name = kGoogleUpdatePipeName;
171 pipe_name += user_sid; 145 pipe_name += user_sid;
172 146
173 #if defined(_WIN64) 147 #if defined(_WIN64)
174 pipe_name += L"-x64"; 148 pipe_name += L"-x64";
175 #endif 149 #endif
176 } else { 150 } else {
177 // Either this is a Chromium build, reporting is disabled by policy or the 151 // Either this is a Chromium build, reporting is disabled by policy or the
178 // user has not given consent. 152 // user has not given consent.
179 return; 153 return;
180 } 154 }
181 155
182 g_elf_breakpad = new google_breakpad::ExceptionHandler( 156 g_elf_breakpad = new google_breakpad::ExceptionHandler(
183 temp_directory, 157 temp_directory, NULL, NULL, NULL,
184 NULL, 158 google_breakpad::ExceptionHandler::HANDLER_ALL, dump_type,
185 NULL, 159 pipe_name.c_str(), GetCustomInfo());
186 NULL,
187 google_breakpad::ExceptionHandler::HANDLER_ALL,
188 dump_type,
189 pipe_name.c_str(),
190 GetCustomInfo());
191 160
192 if (g_elf_breakpad->IsOutOfProcess()) { 161 if (g_elf_breakpad->IsOutOfProcess()) {
193 // Tells breakpad to handle breakpoint and single step exceptions. 162 // Tells breakpad to handle breakpoint and single step exceptions.
194 g_elf_breakpad->set_handle_debug_exceptions(true); 163 g_elf_breakpad->set_handle_debug_exceptions(true);
195 } 164 }
196 } 165 }
OLDNEW
« no previous file with comments | « chrome_elf/breakpad/breakpad.h ('k') | chrome_elf/chrome_elf.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698