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