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

Side by Side Diff: chrome/chrome_watcher/kasko_util.cc

Issue 1844023002: Capture a report on failed browser rendez-vous. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address grt's second round Created 4 years, 8 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/chrome_watcher/kasko_util.h ('k') | third_party/kasko/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 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 #include "chrome/chrome_watcher/kasko_util.h"
6
7 #include <sddl.h>
8
9 #include <memory>
10 #include <string>
11 #include <vector>
12
13 #include "base/base_paths.h"
14 #include "base/bind.h"
15 #include "base/callback_helpers.h"
16 #include "base/environment.h"
17 #include "base/files/file_path.h"
18 #include "base/path_service.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/win/win_util.h"
21
22 #include "chrome/chrome_watcher/chrome_watcher_main_api.h"
23
24 #if BUILDFLAG(ENABLE_KASKO)
25 #include "components/crash/content/app/crashpad.h"
26 #include "syzygy/kasko/api/reporter.h"
27 #endif
28
29 #if BUILDFLAG(ENABLE_KASKO)
30
31 namespace {
32
33 // Helper function for determining the crash server to use. Defaults to the
34 // standard crash server, but can be overridden via an environment variable.
35 // Enables easy integration testing.
36 base::string16 GetKaskoCrashServerUrl() {
37 static const char kKaskoCrashServerUrl[] = "KASKO_CRASH_SERVER_URL";
38 static const wchar_t kDefaultKaskoCrashServerUrl[] =
39 L"https://clients2.google.com/cr/report";
40
41 std::unique_ptr<base::Environment> env(base::Environment::Create());
42 std::string env_var;
43 if (env->GetVar(kKaskoCrashServerUrl, &env_var)) {
44 return base::UTF8ToUTF16(env_var);
45 }
46 return kDefaultKaskoCrashServerUrl;
47 }
48
49 // Helper function for determining the crash reports directory to use. Defaults
50 // to the browser data directory, but can be overridden via an environment
51 // variable. Enables easy integration testing.
52 base::FilePath GetKaskoCrashReportsBaseDir(
53 const base::char16* browser_data_directory) {
54 static const char kKaskoCrashReportBaseDir[] = "KASKO_CRASH_REPORTS_BASE_DIR";
55 std::unique_ptr<base::Environment> env(base::Environment::Create());
56 std::string env_var;
57 if (env->GetVar(kKaskoCrashReportBaseDir, &env_var)) {
58 return base::FilePath(base::UTF8ToUTF16(env_var));
59 }
60 return base::FilePath(browser_data_directory);
61 }
62
63 struct EventSourceDeregisterer {
64 using pointer = HANDLE;
65 void operator()(HANDLE event_source_handle) const {
66 if (!::DeregisterEventSource(event_source_handle))
67 DPLOG(ERROR) << "DeregisterEventSource";
68 }
69 };
70 using ScopedEventSourceHandle =
71 std::unique_ptr<HANDLE, EventSourceDeregisterer>;
72
73 struct SidDeleter {
74 using pointer = PSID;
75 void operator()(PSID sid) const {
76 if (::LocalFree(sid) != nullptr)
77 DPLOG(ERROR) << "LocalFree";
78 }
79 };
80 using ScopedSid = std::unique_ptr<PSID, SidDeleter>;
81
82 void OnCrashReportUpload(void* context,
83 const base::char16* report_id,
84 const base::char16* minidump_path,
85 const base::char16* const* keys,
86 const base::char16* const* values) {
87 // Open the event source.
88 ScopedEventSourceHandle event_source_handle(
89 ::RegisterEventSource(nullptr, L"Chrome"));
90 if (!event_source_handle) {
91 PLOG(ERROR) << "RegisterEventSource";
92 return;
93 }
94
95 // Get the user's SID for the log record.
96 base::string16 sid_string;
97 PSID sid = nullptr;
98 if (base::win::GetUserSidString(&sid_string) && !sid_string.empty()) {
99 if (!::ConvertStringSidToSid(sid_string.c_str(), &sid))
100 DPLOG(ERROR) << "ConvertStringSidToSid";
101 DCHECK(sid);
102 }
103 // Ensure cleanup on scope exit.
104 ScopedSid scoped_sid;
105 if (sid)
106 scoped_sid.reset(sid);
107
108 // Generate the message.
109 // Note that the format of this message must match the consumer in
110 // chrome/browser/crash_upload_list_win.cc.
111 base::string16 message =
112 L"Crash uploaded. Id=" + base::string16(report_id) + L".";
113
114 // Matches Omaha.
115 const int kCrashUploadEventId = 2;
116
117 // Report the event.
118 const base::char16* strings[] = {message.c_str()};
119 if (!::ReportEvent(event_source_handle.get(), EVENTLOG_INFORMATION_TYPE,
120 0, // category
121 kCrashUploadEventId, sid,
122 1, // count
123 0, strings, nullptr)) {
124 DPLOG(ERROR);
125 }
126 }
127
128 void AddCrashKey(const wchar_t *key, const wchar_t *value,
129 std::vector<kasko::api::CrashKey> *crash_keys) {
130 DCHECK(key);
131 DCHECK(value);
132 DCHECK(crash_keys);
133
134 kasko::api::CrashKey crash_key;
135 std::wcsncpy(crash_key.name, key, kasko::api::CrashKey::kNameMaxLength - 1);
136 std::wcsncpy(crash_key.value, value,
137 kasko::api::CrashKey::kValueMaxLength - 1);
138 crash_keys->push_back(crash_key);
139 }
140
141 } // namespace
142
143 bool InitializeKaskoReporter(const base::string16& endpoint,
144 const base::char16* browser_data_directory) {
145 base::string16 crash_server = GetKaskoCrashServerUrl();
146 base::FilePath crash_reports_base_dir =
147 GetKaskoCrashReportsBaseDir(browser_data_directory);
148
149 return kasko::api::InitializeReporter(
150 endpoint.c_str(),
151 crash_server.c_str(),
152 crash_reports_base_dir.Append(L"Crash Reports").value().c_str(),
153 crash_reports_base_dir.Append(kPermanentlyFailedReportsSubdir)
154 .value()
155 .c_str(),
156 &OnCrashReportUpload,
157 nullptr);
158 }
159
160 void ShutdownKaskoReporter() {
161 kasko::api::ShutdownReporter();
162 }
163
164 bool EnsureTargetProcessValidForCapture(const base::Process& process) {
165 // Ensure the target process shares the current process's executable name.
166 base::FilePath exe_self;
167 if (!PathService::Get(base::FILE_EXE, &exe_self))
168 return false;
169
170 wchar_t exe_name_other[MAX_PATH];
171 DWORD exe_name_other_len = MAX_PATH;
172 // Note: requesting the Win32 path format.
173 if (QueryFullProcessImageName(process.Handle(), 0, exe_name_other,
grt (UTC plus 2) 2016/04/07 13:41:49 Uber nit: other spots in this file use :: in front
manzagop (departed) 2016/04/07 14:45:25 Done.
174 &exe_name_other_len) == 0) {
175 DPLOG(ERROR) << "Failed to get executable name for other process";
176 return false;
177 }
178
179 // QueryFullProcessImageName's documentation does not specify behavior when
180 // the buffer is too small, but we know that GetModuleFileNameEx succeeds and
181 // truncates the returned name in such a case. Given that paths longer than
182 // MAX_PATH may exist, the conservative approach is to reject names when
183 // the returned length is MAX_PATH.
184 if (exe_name_other_len == MAX_PATH)
185 return false;
grt (UTC plus 2) 2016/04/07 13:41:49 The most conservative thing to do is to only do th
manzagop (departed) 2016/04/07 14:45:25 Done.
186
187 return base::FilePath::CompareEqualIgnoreCase(exe_self.value(),
188 exe_name_other);
189 }
190
191 void DumpHungProcess(DWORD main_thread_id, const base::string16& channel,
192 const base::char16* key, const base::Process& process) {
193 // Read the Crashpad module annotations for the process.
194 std::vector<kasko::api::CrashKey> annotations;
195 crash_reporter::ReadMainModuleAnnotationsForKasko(process, &annotations);
196 AddCrashKey(key, L"1", &annotations);
197
198 std::vector<const base::char16*> key_buffers;
199 std::vector<const base::char16*> value_buffers;
200 for (const auto& crash_key : annotations) {
201 key_buffers.push_back(crash_key.name);
202 value_buffers.push_back(crash_key.value);
203 }
204 key_buffers.push_back(nullptr);
205 value_buffers.push_back(nullptr);
206
207 // Synthesize an exception for the main thread. Populate the record with the
208 // current context of the thread to get the stack trace bucketed on the crash
209 // backend.
210 CONTEXT thread_context = {};
211 EXCEPTION_RECORD exception_record = {};
212 exception_record.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
213 EXCEPTION_POINTERS exception_pointers = {&exception_record, &thread_context};
214
215 base::win::ScopedHandle main_thread(::OpenThread(
216 THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION,
217 FALSE, main_thread_id));
218
219 bool have_context = false;
220 if (main_thread.IsValid()) {
221 DWORD suspend_count = ::SuspendThread(main_thread.Get());
222 const DWORD kSuspendFailed = static_cast<DWORD>(-1);
223 if (suspend_count != kSuspendFailed) {
224 // Best effort capture of the context.
225 thread_context.ContextFlags = CONTEXT_FLOATING_POINT | CONTEXT_SEGMENTS |
226 CONTEXT_INTEGER | CONTEXT_CONTROL;
227 if (::GetThreadContext(main_thread.Get(), &thread_context) == TRUE)
228 have_context = true;
229
230 ::ResumeThread(main_thread.Get());
231 }
232 }
233
234 // TODO(manzagop): consider making the dump-type channel-dependent.
235 if (have_context) {
236 kasko::api::SendReportForProcess(
237 process.Handle(), main_thread_id, &exception_pointers,
238 kasko::api::LARGER_DUMP_TYPE, key_buffers.data(), value_buffers.data());
239 } else {
240 kasko::api::SendReportForProcess(process.Handle(), 0, nullptr,
241 kasko::api::LARGER_DUMP_TYPE,
242 key_buffers.data(), value_buffers.data());
243 }
244 }
245
246 #endif // BUILDFLAG(ENABLE_KASKO)
OLDNEW
« no previous file with comments | « chrome/chrome_watcher/kasko_util.h ('k') | third_party/kasko/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698