OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "components/crash/content/app/crashpad.h" | |
6 | |
7 #include <string.h> | |
8 | |
9 #include <algorithm> | |
10 #include <map> | |
11 #include <vector> | |
12 | |
13 #include "base/auto_reset.h" | |
14 #include "base/debug/crash_logging.h" | |
15 #include "base/debug/dump_without_crashing.h" | |
16 #include "base/logging.h" | |
17 #include "base/strings/string_number_conversions.h" | |
18 #include "base/strings/string_piece.h" | |
19 #include "base/strings/stringprintf.h" | |
20 #include "base/strings/sys_string_conversions.h" | |
21 #include "build/build_config.h" | |
22 #include "components/crash/content/app/crash_reporter_client.h" | |
23 #include "third_party/crashpad/crashpad/client/crash_report_database.h" | |
24 #include "third_party/crashpad/crashpad/client/crashpad_client.h" | |
25 #include "third_party/crashpad/crashpad/client/crashpad_info.h" | |
26 #include "third_party/crashpad/crashpad/client/settings.h" | |
27 #include "third_party/crashpad/crashpad/client/simple_string_dictionary.h" | |
28 #include "third_party/crashpad/crashpad/client/simulate_crash.h" | |
29 | |
30 #if defined(OS_POSIX) | |
31 #include <unistd.h> | |
32 #endif // OS_POSIX | |
33 | |
34 namespace crash_reporter { | |
35 | |
36 namespace { | |
37 | |
38 crashpad::SimpleStringDictionary* g_simple_string_dictionary; | |
39 crashpad::CrashReportDatabase* g_database; | |
40 | |
41 void SetCrashKeyValue(const base::StringPiece& key, | |
42 const base::StringPiece& value) { | |
43 g_simple_string_dictionary->SetKeyValue(key.data(), value.data()); | |
44 } | |
45 | |
46 void ClearCrashKey(const base::StringPiece& key) { | |
47 g_simple_string_dictionary->RemoveKey(key.data()); | |
48 } | |
49 | |
50 bool LogMessageHandler(int severity, | |
51 const char* file, | |
52 int line, | |
53 size_t message_start, | |
54 const std::string& string) { | |
55 // Only handle FATAL. | |
56 if (severity != logging::LOG_FATAL) { | |
57 return false; | |
58 } | |
59 | |
60 // In case of an out-of-memory condition, this code could be reentered when | |
61 // constructing and storing the key. Using a static is not thread-safe, but if | |
62 // multiple threads are in the process of a fatal crash at the same time, this | |
63 // should work. | |
64 static bool guarded = false; | |
65 if (guarded) { | |
66 return false; | |
67 } | |
68 base::AutoReset<bool> guard(&guarded, true); | |
69 | |
70 // Only log last path component. This matches logging.cc. | |
71 if (file) { | |
72 const char* slash = strrchr(file, '/'); | |
73 if (slash) { | |
74 file = slash + 1; | |
75 } | |
76 } | |
77 | |
78 CHECK_LE(message_start, string.size()); | |
79 std::string message = base::StringPrintf("%s:%d: %s", file, line, | |
80 string.c_str() + message_start); | |
81 SetCrashKeyValue("LOG_FATAL", message); | |
82 | |
83 // Rather than including the code to force the crash here, allow the caller to | |
84 // do it. | |
85 return false; | |
86 } | |
87 | |
88 void DumpWithoutCrashing() { | |
89 CRASHPAD_SIMULATE_CRASH(); | |
90 } | |
91 | |
92 } // namespace | |
93 | |
94 void InitializeCrashpad(bool initial_client, const std::string& process_type) { | |
95 static bool initialized = false; | |
96 DCHECK(!initialized); | |
97 initialized = true; | |
98 | |
99 const bool browser_process = process_type.empty(); | |
100 CrashReporterClient* crash_reporter_client = GetCrashReporterClient(); | |
101 | |
102 if (initial_client) { | |
103 // "relauncher" is hard-coded because it's a Chrome --type, but this | |
104 // component can't see Chrome's switches. This is only used for argument | |
105 // sanitization. | |
106 DCHECK(browser_process || process_type == "relauncher"); | |
Mark Mentovai
2015/11/10 17:21:17
The relauncher side of this is Mac-specific.
scottmg
2015/11/16 21:48:40
Done.
| |
107 } else { | |
108 DCHECK(!browser_process); | |
109 } | |
110 | |
111 base::FilePath database_path; | |
112 database_path = | |
Mark Mentovai
2015/11/10 17:21:17
database_path will only be valid in the browser pr
Mark Mentovai
2015/11/10 17:21:17
Move the declaration to this line.
scottmg
2015/11/16 21:48:39
Done.
scottmg
2015/11/16 21:48:39
Done.
| |
113 internal::PlatformCrashpadInitialization(initial_client, browser_process); | |
114 | |
115 crashpad::CrashpadInfo* crashpad_info = | |
116 crashpad::CrashpadInfo::GetCrashpadInfo(); | |
117 | |
118 #if defined(NDEBUG) | |
119 const bool is_debug_build = false; | |
120 #else | |
121 const bool is_debug_build = true; | |
122 #endif | |
123 | |
124 // Disable forwarding to the system's crash reporter in processes other than | |
125 // the browser process. For the browser, the system's crash reporter presents | |
126 // the crash UI to the user, so it's desirable there. Additionally, having | |
127 // crash reports appear in ~/Library/Logs/DiagnosticReports provides a | |
Mark Mentovai
2015/11/10 17:21:17
This comment is Mac-specific.
scottmg
2015/11/16 21:48:39
Done.
| |
128 // fallback. Forwarding is turned off for debug-mode builds even for the | |
129 // browser process, because the system's crash reporter can take a very long | |
130 // time to chew on symbols. | |
131 if (!browser_process || is_debug_build) { | |
132 crashpad_info->set_system_crash_reporter_forwarding( | |
Mark Mentovai
2015/11/10 17:21:17
This doesn’t have any effect on Windows at all. If
scottmg
2015/11/16 21:48:40
I'm not sure if it's possible. I guess it might be
Mark Mentovai
2015/11/16 22:11:13
scottmg wrote:
| |
133 crashpad::TriState::kDisabled); | |
134 } | |
135 | |
136 g_simple_string_dictionary = new crashpad::SimpleStringDictionary(); | |
137 crashpad_info->set_simple_annotations(g_simple_string_dictionary); | |
138 | |
139 base::debug::SetCrashKeyReportingFunctions(SetCrashKeyValue, ClearCrashKey); | |
140 #if !defined(OS_WIN) && !defined(COMPONENT_BUILD) | |
141 crash_reporter_client->RegisterCrashKeys(); | |
142 #endif | |
143 | |
144 SetCrashKeyValue("ptype", browser_process ? base::StringPiece("browser") | |
145 : base::StringPiece(process_type)); | |
146 #if defined(OS_POSIX) | |
147 SetCrashKeyValue("pid", base::IntToString(getpid())); | |
148 #elif defined(OS_WIN) | |
149 SetCrashKeyValue("pid", base::IntToString(GetCurrentProcessId())); | |
150 #else | |
151 #error SetCrashKeyValue pid | |
152 #endif | |
153 | |
154 logging::SetLogMessageHandler(LogMessageHandler); | |
155 | |
156 // If clients called CRASHPAD_SIMULATE_CRASH() instead of | |
157 // base::debug::DumpWithoutCrashing(), these dumps would appear as crashes in | |
158 // the correct function, at the correct file and line. This would be | |
159 // preferable to having all occurrences show up in DumpWithoutCrashing() at | |
160 // the same file and line. | |
161 base::debug::SetDumpWithoutCrashingFunction(DumpWithoutCrashing); | |
162 | |
163 if (browser_process) { | |
164 g_database = | |
165 crashpad::CrashReportDatabase::Initialize(database_path).release(); | |
166 | |
167 bool enable_uploads = false; | |
168 if (!crash_reporter_client->ReportingIsEnforcedByPolicy(&enable_uploads)) { | |
169 // Breakpad provided a --disable-breakpad switch to disable crash dumping | |
170 // (not just uploading) here. Crashpad doesn't need it: dumping is enabled | |
171 // unconditionally and uploading is gated on consent, which tests/bots | |
172 // shouldn't have. As a precaution, uploading is also disabled on bots | |
173 // even if consent is present. | |
174 enable_uploads = crash_reporter_client->GetCollectStatsConsent() && | |
175 !crash_reporter_client->IsRunningUnattended(); | |
176 } | |
177 | |
178 SetUploadsEnabled(enable_uploads); | |
179 } | |
180 } | |
181 | |
182 void SetUploadsEnabled(bool enable_uploads) { | |
183 if (g_database) { | |
184 crashpad::Settings* settings = g_database->GetSettings(); | |
185 settings->SetUploadsEnabled(enable_uploads); | |
186 } | |
187 } | |
188 | |
189 bool GetUploadsEnabled() { | |
190 if (g_database) { | |
191 crashpad::Settings* settings = g_database->GetSettings(); | |
192 bool enable_uploads; | |
193 if (settings->GetUploadsEnabled(&enable_uploads)) { | |
194 return enable_uploads; | |
195 } | |
196 } | |
197 | |
198 return false; | |
199 } | |
200 | |
201 void GetUploadedReports(std::vector<UploadedReport>* uploaded_reports) { | |
202 uploaded_reports->clear(); | |
203 | |
204 if (!g_database) { | |
205 return; | |
206 } | |
207 | |
208 std::vector<crashpad::CrashReportDatabase::Report> completed_reports; | |
209 crashpad::CrashReportDatabase::OperationStatus status = | |
210 g_database->GetCompletedReports(&completed_reports); | |
211 if (status != crashpad::CrashReportDatabase::kNoError) { | |
212 return; | |
213 } | |
214 | |
215 for (const crashpad::CrashReportDatabase::Report& completed_report : | |
216 completed_reports) { | |
217 if (completed_report.uploaded) { | |
218 UploadedReport uploaded_report; | |
219 uploaded_report.local_id = completed_report.uuid.ToString(); | |
220 uploaded_report.remote_id = completed_report.id; | |
221 uploaded_report.creation_time = completed_report.creation_time; | |
222 | |
223 uploaded_reports->push_back(uploaded_report); | |
224 } | |
225 } | |
226 | |
227 std::sort(uploaded_reports->begin(), uploaded_reports->end(), | |
228 [](const UploadedReport& a, const UploadedReport& b) { | |
229 return a.creation_time >= b.creation_time; | |
230 }); | |
231 } | |
232 | |
233 } // namespace crash_reporter | |
OLD | NEW |