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

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