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

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: Executable name check 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
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 <string>
10 #include <vector>
11
12 #include "base/bind.h"
13 #include "base/callback_helpers.h"
14 #include "base/environment.h"
15 #include "base/files/file_path.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/win/win_util.h"
18
19 #include "chrome/chrome_watcher/chrome_watcher_main_api.h"
20
21 #if BUILDFLAG(ENABLE_KASKO)
22 #include "components/crash/content/app/crashpad.h"
23 #include "syzygy/kasko/api/reporter.h"
24 #endif
25
26 #if BUILDFLAG(ENABLE_KASKO)
27
28 namespace {
29
30 // Helper function for determining the crash server to use. Defaults to the
31 // standard crash server, but can be overridden via an environment variable.
32 // Enables easy integration testing.
33 void GetKaskoCrashServerUrl(base::string16* crash_server) {
grt (UTC plus 2) 2016/04/05 17:22:08 return the string16 rather than taking it as an ou
manzagop (departed) 2016/04/06 19:36:03 Done.
34 const char kKaskoCrashServerUrl[] = "KASKO_CRASH_SERVER_URL";
grt (UTC plus 2) 2016/04/05 17:22:08 static const char
manzagop (departed) 2016/04/06 19:36:02 Done.
35 static const wchar_t kDefaultKaskoCrashServerUrl[] =
36 L"https://clients2.google.com/cr/report";
37
38 auto env = base::Environment::Create();
grt (UTC plus 2) 2016/04/05 17:22:08 #include <memory> std::unique_ptr<base::Environ
manzagop (departed) 2016/04/06 19:36:02 Ouch. Done.
39 std::string env_var;
40 if (env->GetVar(kKaskoCrashServerUrl, &env_var)) {
41 base::UTF8ToWide(env_var.c_str(), env_var.size(), crash_server);
grt (UTC plus 2) 2016/04/05 17:22:08 do you really mean UTF8ToUTF16 here (the destinati
manzagop (departed) 2016/04/06 19:36:02 Done.
42 } else {
43 *crash_server = kDefaultKaskoCrashServerUrl;
44 }
45 }
46
47 // Helper function for determining the crash reports directory to use. Defaults
48 // to the browser data directory, but can be overridden via an environment
49 // variable. Enables easy integration testing.
50 void GetKaskoCrashReportsBaseDir(const base::char16* browser_data_directory,
51 base::FilePath* base_dir) {
grt (UTC plus 2) 2016/04/05 17:22:08 return a base::FilePath rather than taking it as a
manzagop (departed) 2016/04/06 19:36:02 Done.
52 const char kKaskoCrashReportBaseDir[] = "KASKO_CRASH_REPORTS_BASE_DIR";
grt (UTC plus 2) 2016/04/05 17:22:08 static
manzagop (departed) 2016/04/06 19:36:03 Done.
53 auto env = base::Environment::Create();
grt (UTC plus 2) 2016/04/05 17:22:08 unique_ptr here, too
manzagop (departed) 2016/04/06 19:36:02 Done.
54 std::string env_var;
55 if (env->GetVar(kKaskoCrashReportBaseDir, &env_var)) {
56 base::string16 wide_env_var;
57 base::UTF8ToWide(env_var.c_str(), env_var.size(), &wide_env_var);
58 *base_dir = base::FilePath(wide_env_var);
59 } else {
60 *base_dir = base::FilePath(browser_data_directory);
61 }
62 }
63
64 void LoggedDeregisterEventSource(HANDLE event_source_handle) {
65 if (!::DeregisterEventSource(event_source_handle))
66 DPLOG(ERROR) << "DeregisterEventSource";
67 }
68
69 void LoggedLocalFree(PSID sid) {
70 if (::LocalFree(sid) != nullptr)
71 DPLOG(ERROR) << "LocalFree";
72 }
73
74 void OnCrashReportUpload(void* context,
75 const base::char16* report_id,
76 const base::char16* minidump_path,
77 const base::char16* const* keys,
78 const base::char16* const* values) {
79 // Open the event source.
80 HANDLE event_source_handle = ::RegisterEventSource(NULL, L"Chrome");
grt (UTC plus 2) 2016/04/05 17:22:08 i somewhat prefer using std::unique_ptr with a cus
grt (UTC plus 2) 2016/04/05 17:22:08 NULL -> nullptr everywhere
manzagop (departed) 2016/04/06 19:36:02 Done in this file. Did not change "untouched lines
manzagop (departed) 2016/04/06 19:36:02 Done. Also applied to sid below, but it's not as c
81 if (!event_source_handle) {
82 PLOG(ERROR) << "RegisterEventSource";
83 return;
84 }
85 // Ensure cleanup on scope exit.
86 base::ScopedClosureRunner deregister_event_source(
87 base::Bind(&LoggedDeregisterEventSource, event_source_handle));
88
89 // Get the user's SID for the log record.
90 base::string16 sid_string;
91 PSID sid = nullptr;
92 if (base::win::GetUserSidString(&sid_string)) {
93 if (!sid_string.empty()) {
94 if (!::ConvertStringSidToSid(sid_string.c_str(), &sid))
95 DPLOG(ERROR) << "ConvertStringSidToSid";
96 DCHECK(sid);
grt (UTC plus 2) 2016/04/05 17:22:08 do you also want this check if Get UserSidString f
manzagop (departed) 2016/04/06 19:36:02 It seems ok for ReportEvent (below) to get a null
97 }
98 }
99 // Ensure cleanup on scope exit.
100 base::ScopedClosureRunner free_sid(
101 base::Bind(&LoggedLocalFree, base::Unretained(sid)));
102
103 // Generate the message.
104 // Note that the format of this message must match the consumer in
105 // chrome/browser/crash_upload_list_win.cc.
106 base::string16 message =
107 L"Crash uploaded. Id=" + base::string16(report_id) + L".";
108
109 // Matches Omaha.
110 const int kCrashUploadEventId = 2;
111
112 // Report the event.
113 const base::char16* strings[] = {message.c_str()};
114 if (!::ReportEvent(event_source_handle, EVENTLOG_INFORMATION_TYPE,
115 0, // category
116 kCrashUploadEventId, sid,
grt (UTC plus 2) 2016/04/05 17:22:08 what if |sid| is null here?
manzagop (departed) 2016/04/06 19:36:02 Looks like it's optional. If it's missing the user
117 1, // count
118 0, strings, nullptr)) {
119 DPLOG(ERROR);
120 }
121 }
122
123 void AddCrashKey(const wchar_t *key, const wchar_t *value,
124 std::vector<kasko::api::CrashKey> *crash_keys) {
125 DCHECK(key);
126 DCHECK(value);
127 DCHECK(crash_keys);
128
129 kasko::api::CrashKey crash_key;
130 std::wcsncpy(crash_key.name, key, kasko::api::CrashKey::kNameMaxLength - 1);
131 std::wcsncpy(crash_key.value, value,
132 kasko::api::CrashKey::kValueMaxLength - 1);
133 crash_keys->push_back(crash_key);
134 }
135
136 } // namespace
137
138 bool InitializeKaskoReporter(const base::string16& endpoint,
139 const base::char16* browser_data_directory) {
140 base::string16 crash_server;
141 GetKaskoCrashServerUrl(&crash_server);
142 base::FilePath crash_reports_base_dir;
143 GetKaskoCrashReportsBaseDir(browser_data_directory, &crash_reports_base_dir);
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 void DumpHungProcess(DWORD main_thread_id, const base::string16& channel,
161 const base::char16* key, const base::Process& process) {
162 // Read the Crashpad module annotations for the process.
163 std::vector<kasko::api::CrashKey> annotations;
164 crash_reporter::ReadMainModuleAnnotationsForKasko(process, &annotations);
165 AddCrashKey(key, L"1", &annotations);
166
167 std::vector<const base::char16*> key_buffers;
168 std::vector<const base::char16*> value_buffers;
169 for (const auto& crash_key : annotations) {
170 key_buffers.push_back(crash_key.name);
171 value_buffers.push_back(crash_key.value);
172 }
173 key_buffers.push_back(nullptr);
174 value_buffers.push_back(nullptr);
175
176 // Synthesize an exception for the main thread. Populate the record with the
177 // current context of the thread to get the stack trace bucketed on the crash
178 // backend.
179 CONTEXT thread_context = {};
180 EXCEPTION_RECORD exception_record = {};
181 exception_record.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
182 EXCEPTION_POINTERS exception_pointers = {&exception_record, &thread_context};
183
184 base::win::ScopedHandle main_thread(::OpenThread(
185 THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION,
186 FALSE, main_thread_id));
187
188 bool have_context = false;
189 if (main_thread.IsValid()) {
190 DWORD suspend_count = ::SuspendThread(main_thread.Get());
191 const DWORD kSuspendFailed = static_cast<DWORD>(-1);
192 if (suspend_count != kSuspendFailed) {
193 // Best effort capture of the context.
194 thread_context.ContextFlags = CONTEXT_FLOATING_POINT | CONTEXT_SEGMENTS |
195 CONTEXT_INTEGER | CONTEXT_CONTROL;
196 if (::GetThreadContext(main_thread.Get(), &thread_context) == TRUE)
197 have_context = true;
198
199 ::ResumeThread(main_thread.Get());
200 }
201 }
202
203 // TODO(erikwright): Make the dump-type channel-dependent.
Sigurður Ásgeirsson 2016/04/05 13:52:29 erikwright won't likely be addressing this :)
manzagop (departed) 2016/04/06 19:36:03 Done.
204 if (have_context) {
205 kasko::api::SendReportForProcess(
206 process.Handle(), main_thread_id, &exception_pointers,
207 kasko::api::LARGER_DUMP_TYPE, key_buffers.data(), value_buffers.data());
208 } else {
209 kasko::api::SendReportForProcess(process.Handle(), 0, nullptr,
210 kasko::api::LARGER_DUMP_TYPE,
211 key_buffers.data(), value_buffers.data());
212 }
213 }
214
215 #endif // BUILDFLAG(ENABLE_KASKO)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698