OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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/tools/crash_service/crash_service.h" | |
6 | |
7 #include <windows.h> | |
8 | |
9 #include <sddl.h> | |
10 #include <fstream> | |
11 #include <map> | |
12 | |
13 #include "base/command_line.h" | |
14 #include "base/file_util.h" | |
15 #include "base/logging.h" | |
16 #include "base/path_service.h" | |
17 #include "base/win/windows_version.h" | |
18 #include "breakpad/src/client/windows/crash_generation/client_info.h" | |
19 #include "breakpad/src/client/windows/crash_generation/crash_generation_server.h
" | |
20 #include "breakpad/src/client/windows/sender/crash_report_sender.h" | |
21 #include "chrome/common/chrome_constants.h" | |
22 #include "chrome/common/chrome_paths.h" | |
23 | |
24 namespace { | |
25 | |
26 const wchar_t kTestPipeName[] = L"\\\\.\\pipe\\ChromeCrashServices"; | |
27 | |
28 const wchar_t kCrashReportURL[] = L"https://clients2.google.com/cr/report"; | |
29 const wchar_t kCheckPointFile[] = L"crash_checkpoint.txt"; | |
30 | |
31 typedef std::map<std::wstring, std::wstring> CrashMap; | |
32 | |
33 bool CustomInfoToMap(const google_breakpad::ClientInfo* client_info, | |
34 const std::wstring& reporter_tag, CrashMap* map) { | |
35 google_breakpad::CustomClientInfo info = client_info->GetCustomInfo(); | |
36 | |
37 for (uintptr_t i = 0; i < info.count; ++i) { | |
38 (*map)[info.entries[i].name] = info.entries[i].value; | |
39 } | |
40 | |
41 (*map)[L"rept"] = reporter_tag; | |
42 | |
43 return !map->empty(); | |
44 } | |
45 | |
46 bool WriteCustomInfoToFile(const std::wstring& dump_path, const CrashMap& map) { | |
47 std::wstring file_path(dump_path); | |
48 size_t last_dot = file_path.rfind(L'.'); | |
49 if (last_dot == std::wstring::npos) | |
50 return false; | |
51 file_path.resize(last_dot); | |
52 file_path += L".txt"; | |
53 | |
54 std::wofstream file(file_path.c_str(), | |
55 std::ios_base::out | std::ios_base::app | std::ios::binary); | |
56 if (!file.is_open()) | |
57 return false; | |
58 | |
59 CrashMap::const_iterator pos; | |
60 for (pos = map.begin(); pos != map.end(); ++pos) { | |
61 std::wstring line = pos->first; | |
62 line += L':'; | |
63 line += pos->second; | |
64 line += L'\n'; | |
65 file.write(line.c_str(), static_cast<std::streamsize>(line.length())); | |
66 } | |
67 return true; | |
68 } | |
69 | |
70 // The window procedure task is to handle when a) the user logs off. | |
71 // b) the system shuts down or c) when the user closes the window. | |
72 LRESULT __stdcall CrashSvcWndProc(HWND hwnd, UINT message, | |
73 WPARAM wparam, LPARAM lparam) { | |
74 switch (message) { | |
75 case WM_CLOSE: | |
76 case WM_ENDSESSION: | |
77 case WM_DESTROY: | |
78 PostQuitMessage(0); | |
79 break; | |
80 default: | |
81 return DefWindowProc(hwnd, message, wparam, lparam); | |
82 } | |
83 return 0; | |
84 } | |
85 | |
86 // This is the main and only application window. | |
87 HWND g_top_window = NULL; | |
88 | |
89 bool CreateTopWindow(HINSTANCE instance, bool visible) { | |
90 WNDCLASSEXW wcx = {0}; | |
91 wcx.cbSize = sizeof(wcx); | |
92 wcx.style = CS_HREDRAW | CS_VREDRAW; | |
93 wcx.lpfnWndProc = CrashSvcWndProc; | |
94 wcx.hInstance = instance; | |
95 wcx.lpszClassName = L"crash_svc_class"; | |
96 ATOM atom = ::RegisterClassExW(&wcx); | |
97 DWORD style = visible ? WS_POPUPWINDOW | WS_VISIBLE : WS_OVERLAPPED; | |
98 | |
99 // The window size is zero but being a popup window still shows in the | |
100 // task bar and can be closed using the system menu or using task manager. | |
101 HWND window = CreateWindowExW(0, wcx.lpszClassName, L"crash service", style, | |
102 CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, | |
103 NULL, NULL, instance, NULL); | |
104 if (!window) | |
105 return false; | |
106 | |
107 ::UpdateWindow(window); | |
108 VLOG(1) << "window handle is " << window; | |
109 g_top_window = window; | |
110 return true; | |
111 } | |
112 | |
113 // Simple helper class to keep the process alive until the current request | |
114 // finishes. | |
115 class ProcessingLock { | |
116 public: | |
117 ProcessingLock() { | |
118 ::InterlockedIncrement(&op_count_); | |
119 } | |
120 ~ProcessingLock() { | |
121 ::InterlockedDecrement(&op_count_); | |
122 } | |
123 static bool IsWorking() { | |
124 return (op_count_ != 0); | |
125 } | |
126 private: | |
127 static volatile LONG op_count_; | |
128 }; | |
129 | |
130 volatile LONG ProcessingLock::op_count_ = 0; | |
131 | |
132 // This structure contains the information that the worker thread needs to | |
133 // send a crash dump to the server. | |
134 struct DumpJobInfo { | |
135 DWORD pid; | |
136 CrashService* self; | |
137 CrashMap map; | |
138 std::wstring dump_path; | |
139 | |
140 DumpJobInfo(DWORD process_id, CrashService* service, | |
141 const CrashMap& crash_map, const std::wstring& path) | |
142 : pid(process_id), self(service), map(crash_map), dump_path(path) { | |
143 } | |
144 }; | |
145 | |
146 } // namespace | |
147 | |
148 // Command line switches: | |
149 const char CrashService::kMaxReports[] = "max-reports"; | |
150 const char CrashService::kNoWindow[] = "no-window"; | |
151 const char CrashService::kReporterTag[] = "reporter"; | |
152 const char CrashService::kDumpsDir[] = "dumps-dir"; | |
153 const char CrashService::kPipeName[] = "pipe-name"; | |
154 | |
155 CrashService::CrashService(const std::wstring& report_dir) | |
156 : report_path_(report_dir), | |
157 sender_(NULL), | |
158 dumper_(NULL), | |
159 requests_handled_(0), | |
160 requests_sent_(0), | |
161 clients_connected_(0), | |
162 clients_terminated_(0) { | |
163 chrome::RegisterPathProvider(); | |
164 } | |
165 | |
166 CrashService::~CrashService() { | |
167 base::AutoLock lock(sending_); | |
168 delete dumper_; | |
169 delete sender_; | |
170 } | |
171 | |
172 | |
173 bool CrashService::Initialize(const std::wstring& command_line) { | |
174 using google_breakpad::CrashReportSender; | |
175 using google_breakpad::CrashGenerationServer; | |
176 | |
177 std::wstring pipe_name = kTestPipeName; | |
178 int max_reports = -1; | |
179 | |
180 // The checkpoint file allows CrashReportSender to enforce the the maximum | |
181 // reports per day quota. Does not seem to serve any other purpose. | |
182 base::FilePath checkpoint_path = report_path_.Append(kCheckPointFile); | |
183 | |
184 // The dumps path is typically : '<user profile>\Local settings\ | |
185 // Application data\Goggle\Chrome\Crash Reports' and the report path is | |
186 // Application data\Google\Chrome\Reported Crashes.txt | |
187 base::FilePath user_data_dir; | |
188 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)) { | |
189 LOG(ERROR) << "could not get DIR_USER_DATA"; | |
190 return false; | |
191 } | |
192 report_path_ = user_data_dir.Append(chrome::kCrashReportLog); | |
193 | |
194 CommandLine cmd_line = CommandLine::FromString(command_line); | |
195 | |
196 base::FilePath dumps_path; | |
197 if (cmd_line.HasSwitch(kDumpsDir)) { | |
198 dumps_path = base::FilePath(cmd_line.GetSwitchValueNative(kDumpsDir)); | |
199 } else { | |
200 if (!PathService::Get(chrome::DIR_CRASH_DUMPS, &dumps_path)) { | |
201 LOG(ERROR) << "could not get DIR_CRASH_DUMPS"; | |
202 return false; | |
203 } | |
204 } | |
205 | |
206 // We can override the send reports quota with a command line switch. | |
207 if (cmd_line.HasSwitch(kMaxReports)) | |
208 max_reports = _wtoi(cmd_line.GetSwitchValueNative(kMaxReports).c_str()); | |
209 | |
210 // Allow the global pipe name to be overridden for better testability. | |
211 if (cmd_line.HasSwitch(kPipeName)) | |
212 pipe_name = cmd_line.GetSwitchValueNative(kPipeName); | |
213 | |
214 #ifdef _WIN64 | |
215 pipe_name += L"-x64"; | |
216 #endif | |
217 | |
218 if (max_reports > 0) { | |
219 // Create the http sender object. | |
220 sender_ = new CrashReportSender(checkpoint_path.value()); | |
221 sender_->set_max_reports_per_day(max_reports); | |
222 } | |
223 | |
224 SECURITY_ATTRIBUTES security_attributes = {0}; | |
225 SECURITY_ATTRIBUTES* security_attributes_actual = NULL; | |
226 | |
227 if (base::win::GetVersion() >= base::win::VERSION_VISTA) { | |
228 SECURITY_DESCRIPTOR* security_descriptor = | |
229 reinterpret_cast<SECURITY_DESCRIPTOR*>( | |
230 GetSecurityDescriptorForLowIntegrity()); | |
231 DCHECK(security_descriptor != NULL); | |
232 | |
233 security_attributes.nLength = sizeof(security_attributes); | |
234 security_attributes.lpSecurityDescriptor = security_descriptor; | |
235 security_attributes.bInheritHandle = FALSE; | |
236 | |
237 security_attributes_actual = &security_attributes; | |
238 } | |
239 | |
240 // Create the OOP crash generator object. | |
241 dumper_ = new CrashGenerationServer(pipe_name, security_attributes_actual, | |
242 &CrashService::OnClientConnected, this, | |
243 &CrashService::OnClientDumpRequest, this, | |
244 &CrashService::OnClientExited, this, | |
245 NULL, NULL, | |
246 true, &dumps_path.value()); | |
247 | |
248 if (!dumper_) { | |
249 LOG(ERROR) << "could not create dumper"; | |
250 if (security_attributes.lpSecurityDescriptor) | |
251 LocalFree(security_attributes.lpSecurityDescriptor); | |
252 return false; | |
253 } | |
254 | |
255 if (!CreateTopWindow(::GetModuleHandleW(NULL), | |
256 !cmd_line.HasSwitch(kNoWindow))) { | |
257 LOG(ERROR) << "could not create window"; | |
258 if (security_attributes.lpSecurityDescriptor) | |
259 LocalFree(security_attributes.lpSecurityDescriptor); | |
260 return false; | |
261 } | |
262 | |
263 reporter_tag_ = L"crash svc"; | |
264 if (cmd_line.HasSwitch(kReporterTag)) | |
265 reporter_tag_ = cmd_line.GetSwitchValueNative(kReporterTag); | |
266 | |
267 // Log basic information. | |
268 VLOG(1) << "pipe name is " << pipe_name | |
269 << "\ndumps at " << dumps_path.value() | |
270 << "\nreports at " << report_path_.value(); | |
271 | |
272 if (sender_) { | |
273 VLOG(1) << "checkpoint is " << checkpoint_path.value() | |
274 << "\nserver is " << kCrashReportURL | |
275 << "\nmaximum " << sender_->max_reports_per_day() << " reports/day" | |
276 << "\nreporter is " << reporter_tag_; | |
277 } | |
278 // Start servicing clients. | |
279 if (!dumper_->Start()) { | |
280 LOG(ERROR) << "could not start dumper"; | |
281 if (security_attributes.lpSecurityDescriptor) | |
282 LocalFree(security_attributes.lpSecurityDescriptor); | |
283 return false; | |
284 } | |
285 | |
286 if (security_attributes.lpSecurityDescriptor) | |
287 LocalFree(security_attributes.lpSecurityDescriptor); | |
288 | |
289 // This is throwaway code. We don't need to sync with the browser process | |
290 // once Google Update is updated to a version supporting OOP crash handling. | |
291 // Create or open an event to signal the browser process that the crash | |
292 // service is initialized. | |
293 HANDLE running_event = | |
294 ::CreateEventW(NULL, TRUE, TRUE, L"g_chrome_crash_svc"); | |
295 // If the browser already had the event open, the CreateEvent call did not | |
296 // signal it. We need to do it manually. | |
297 ::SetEvent(running_event); | |
298 | |
299 return true; | |
300 } | |
301 | |
302 void CrashService::OnClientConnected(void* context, | |
303 const google_breakpad::ClientInfo* client_info) { | |
304 ProcessingLock lock; | |
305 VLOG(1) << "client start. pid = " << client_info->pid(); | |
306 CrashService* self = static_cast<CrashService*>(context); | |
307 ::InterlockedIncrement(&self->clients_connected_); | |
308 } | |
309 | |
310 void CrashService::OnClientExited(void* context, | |
311 const google_breakpad::ClientInfo* client_info) { | |
312 ProcessingLock lock; | |
313 VLOG(1) << "client end. pid = " << client_info->pid(); | |
314 CrashService* self = static_cast<CrashService*>(context); | |
315 ::InterlockedIncrement(&self->clients_terminated_); | |
316 | |
317 if (!self->sender_) | |
318 return; | |
319 | |
320 // When we are instructed to send reports we need to exit if there are | |
321 // no more clients to service. The next client that runs will start us. | |
322 // Only chrome.exe starts crash_service with a non-zero max_reports. | |
323 if (self->clients_connected_ > self->clients_terminated_) | |
324 return; | |
325 if (self->sender_->max_reports_per_day() > 0) { | |
326 // Wait for the other thread to send crashes, if applicable. The sender | |
327 // thread takes the sending_ lock, so the sleep is just to give it a | |
328 // chance to start. | |
329 ::Sleep(1000); | |
330 base::AutoLock lock(self->sending_); | |
331 // Some people can restart chrome very fast, check again if we have | |
332 // a new client before exiting for real. | |
333 if (self->clients_connected_ == self->clients_terminated_) { | |
334 VLOG(1) << "zero clients. exiting"; | |
335 ::PostMessage(g_top_window, WM_CLOSE, 0, 0); | |
336 } | |
337 } | |
338 } | |
339 | |
340 void CrashService::OnClientDumpRequest(void* context, | |
341 const google_breakpad::ClientInfo* client_info, | |
342 const std::wstring* file_path) { | |
343 ProcessingLock lock; | |
344 | |
345 if (!file_path) { | |
346 LOG(ERROR) << "dump with no file path"; | |
347 return; | |
348 } | |
349 if (!client_info) { | |
350 LOG(ERROR) << "dump with no client info"; | |
351 return; | |
352 } | |
353 | |
354 CrashService* self = static_cast<CrashService*>(context); | |
355 if (!self) { | |
356 LOG(ERROR) << "dump with no context"; | |
357 return; | |
358 } | |
359 | |
360 CrashMap map; | |
361 CustomInfoToMap(client_info, self->reporter_tag_, &map); | |
362 | |
363 // Move dump file to the directory under client breakpad dump location. | |
364 base::FilePath dump_location = base::FilePath(*file_path); | |
365 CrashMap::const_iterator it = map.find(L"breakpad-dump-location"); | |
366 if (it != map.end()) { | |
367 base::FilePath alternate_dump_location = base::FilePath(it->second); | |
368 file_util::CreateDirectoryW(alternate_dump_location); | |
369 alternate_dump_location = alternate_dump_location.Append( | |
370 dump_location.BaseName()); | |
371 base::Move(dump_location, alternate_dump_location); | |
372 dump_location = alternate_dump_location; | |
373 } | |
374 | |
375 DWORD pid = client_info->pid(); | |
376 VLOG(1) << "dump for pid = " << pid << " is " << dump_location.value(); | |
377 | |
378 if (!WriteCustomInfoToFile(dump_location.value(), map)) { | |
379 LOG(ERROR) << "could not write custom info file"; | |
380 } | |
381 | |
382 if (!self->sender_) | |
383 return; | |
384 | |
385 // Send the crash dump using a worker thread. This operation has retry | |
386 // logic in case there is no internet connection at the time. | |
387 DumpJobInfo* dump_job = new DumpJobInfo(pid, self, map, | |
388 dump_location.value()); | |
389 if (!::QueueUserWorkItem(&CrashService::AsyncSendDump, | |
390 dump_job, WT_EXECUTELONGFUNCTION)) { | |
391 LOG(ERROR) << "could not queue job"; | |
392 } | |
393 } | |
394 | |
395 // We are going to try sending the report several times. If we can't send, | |
396 // we sleep from one minute to several hours depending on the retry round. | |
397 unsigned long CrashService::AsyncSendDump(void* context) { | |
398 if (!context) | |
399 return 0; | |
400 | |
401 DumpJobInfo* info = static_cast<DumpJobInfo*>(context); | |
402 | |
403 std::wstring report_id = L"<unsent>"; | |
404 | |
405 const DWORD kOneMinute = 60*1000; | |
406 const DWORD kOneHour = 60*kOneMinute; | |
407 | |
408 const DWORD kSleepSchedule[] = { | |
409 24*kOneHour, | |
410 8*kOneHour, | |
411 4*kOneHour, | |
412 kOneHour, | |
413 15*kOneMinute, | |
414 0}; | |
415 | |
416 int retry_round = arraysize(kSleepSchedule) - 1; | |
417 | |
418 do { | |
419 ::Sleep(kSleepSchedule[retry_round]); | |
420 { | |
421 // Take the server lock while sending. This also prevent early | |
422 // termination of the service object. | |
423 base::AutoLock lock(info->self->sending_); | |
424 VLOG(1) << "trying to send report for pid = " << info->pid; | |
425 google_breakpad::ReportResult send_result | |
426 = info->self->sender_->SendCrashReport(kCrashReportURL, info->map, | |
427 info->dump_path, &report_id); | |
428 switch (send_result) { | |
429 case google_breakpad::RESULT_FAILED: | |
430 report_id = L"<network issue>"; | |
431 break; | |
432 case google_breakpad::RESULT_REJECTED: | |
433 report_id = L"<rejected>"; | |
434 ++info->self->requests_handled_; | |
435 retry_round = 0; | |
436 break; | |
437 case google_breakpad::RESULT_SUCCEEDED: | |
438 ++info->self->requests_sent_; | |
439 ++info->self->requests_handled_; | |
440 retry_round = 0; | |
441 break; | |
442 case google_breakpad::RESULT_THROTTLED: | |
443 report_id = L"<throttled>"; | |
444 break; | |
445 default: | |
446 report_id = L"<unknown>"; | |
447 break; | |
448 }; | |
449 } | |
450 | |
451 VLOG(1) << "dump for pid =" << info->pid << " crash2 id =" << report_id; | |
452 --retry_round; | |
453 } while (retry_round >= 0); | |
454 | |
455 if (!::DeleteFileW(info->dump_path.c_str())) | |
456 LOG(WARNING) << "could not delete " << info->dump_path; | |
457 | |
458 delete info; | |
459 return 0; | |
460 } | |
461 | |
462 int CrashService::ProcessingLoop() { | |
463 MSG msg; | |
464 while (GetMessage(&msg, NULL, 0, 0)) { | |
465 TranslateMessage(&msg); | |
466 DispatchMessage(&msg); | |
467 } | |
468 | |
469 VLOG(1) << "session ending.."; | |
470 while (ProcessingLock::IsWorking()) { | |
471 ::Sleep(50); | |
472 } | |
473 | |
474 VLOG(1) << "clients connected :" << clients_connected_ | |
475 << "\nclients terminated :" << clients_terminated_ | |
476 << "\ndumps serviced :" << requests_handled_ | |
477 << "\ndumps reported :" << requests_sent_; | |
478 | |
479 return static_cast<int>(msg.wParam); | |
480 } | |
481 | |
482 PSECURITY_DESCRIPTOR CrashService::GetSecurityDescriptorForLowIntegrity() { | |
483 // Build the SDDL string for the label. | |
484 std::wstring sddl = L"S:(ML;;NW;;;S-1-16-4096)"; | |
485 | |
486 DWORD error = ERROR_SUCCESS; | |
487 PSECURITY_DESCRIPTOR sec_desc = NULL; | |
488 | |
489 PACL sacl = NULL; | |
490 BOOL sacl_present = FALSE; | |
491 BOOL sacl_defaulted = FALSE; | |
492 | |
493 if (::ConvertStringSecurityDescriptorToSecurityDescriptorW(sddl.c_str(), | |
494 SDDL_REVISION, | |
495 &sec_desc, NULL)) { | |
496 if (::GetSecurityDescriptorSacl(sec_desc, &sacl_present, &sacl, | |
497 &sacl_defaulted)) { | |
498 return sec_desc; | |
499 } | |
500 } | |
501 | |
502 return NULL; | |
503 } | |
OLD | NEW |