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 | |
cpu_(ooo_6.6-7.5)
2015/11/07 01:53:17
check that message start is less than string.size(
scottmg
2015/11/09 18:29:56
Done.
| |
78 std::string message = base::StringPrintf("%s:%d: %s", file, line, | |
79 string.c_str() + message_start); | |
80 SetCrashKeyValue("LOG_FATAL", message); | |
81 | |
82 // Rather than including the code to force the crash here, allow the caller to | |
83 // do it. | |
84 return false; | |
85 } | |
86 | |
87 void DumpWithoutCrashing() { | |
88 CRASHPAD_SIMULATE_CRASH(); | |
89 } | |
90 | |
91 } // namespace | |
92 | |
93 void InitializeCrashpad(bool initial_client, const std::string& process_type) { | |
94 static bool initialized = false; | |
95 DCHECK(!initialized); | |
96 initialized = true; | |
97 | |
98 const bool browser_process = process_type.empty(); | |
99 CrashReporterClient* crash_reporter_client = GetCrashReporterClient(); | |
100 | |
101 if (initial_client) { | |
102 // "relauncher" is hard-coded because it's a Chrome --type, but this | |
103 // component can't see Chrome's switches. This is only used for argument | |
104 // sanitization. | |
cpu_(ooo_6.6-7.5)
2015/11/07 01:53:18
yeah... wheee! leaky layers!!!
leave as-is
| |
105 DCHECK(browser_process || process_type == "relauncher"); | |
106 } else { | |
107 DCHECK(!browser_process); | |
108 } | |
109 | |
110 base::FilePath database_path; | |
111 database_path = | |
112 internal::PlatformCrashpadInitialization(initial_client, browser_process); | |
113 | |
114 crashpad::CrashpadInfo* crashpad_info = | |
115 crashpad::CrashpadInfo::GetCrashpadInfo(); | |
116 | |
117 #if defined(NDEBUG) | |
118 const bool is_debug_build = false; | |
119 #else | |
120 const bool is_debug_build = true; | |
121 #endif | |
122 | |
123 // Disable forwarding to the system's crash reporter in processes other than | |
124 // the browser process. For the browser, the system's crash reporter presents | |
125 // the crash UI to the user, so it's desirable there. Additionally, having | |
126 // crash reports appear in ~/Library/Logs/DiagnosticReports provides a | |
127 // fallback. Forwarding is turned off for debug-mode builds even for the | |
128 // browser process, because the system's crash reporter can take a very long | |
129 // time to chew on symbols. | |
130 if (!browser_process || is_debug_build) { | |
131 crashpad_info->set_system_crash_reporter_forwarding( | |
132 crashpad::TriState::kDisabled); | |
133 } | |
cpu_(ooo_6.6-7.5)
2015/11/07 01:53:18
http://www.boost.org/doc/libs/1_55_0/doc/html/trib
scottmg
2015/11/09 18:29:56
I'll see if I can figure out how to deps-in boost.
Mark Mentovai
2015/11/09 18:40:20
scottmg wrote:
scottmg
2015/11/09 18:58:47
Oh wow, I was just joking! I didn't know we used a
| |
134 | |
135 g_simple_string_dictionary = new crashpad::SimpleStringDictionary(); | |
136 crashpad_info->set_simple_annotations(g_simple_string_dictionary); | |
137 | |
138 base::debug::SetCrashKeyReportingFunctions(SetCrashKeyValue, ClearCrashKey); | |
139 #if !defined(OS_WIN) && !defined(COMPONENT_BUILD) | |
140 crash_reporter_client->RegisterCrashKeys(); | |
141 #endif | |
142 | |
143 SetCrashKeyValue("ptype", browser_process ? base::StringPiece("browser") | |
144 : base::StringPiece(process_type)); | |
145 #if defined(OS_POSIX) | |
146 SetCrashKeyValue("pid", base::IntToString(getpid())); | |
147 #elif defined(OS_WIN) | |
148 SetCrashKeyValue("pid", base::IntToString(GetCurrentProcessId())); | |
149 #else | |
150 #error SetCrashKeyValue pid | |
151 #endif | |
152 | |
153 logging::SetLogMessageHandler(LogMessageHandler); | |
154 | |
155 // If clients called CRASHPAD_SIMULATE_CRASH() instead of | |
156 // base::debug::DumpWithoutCrashing(), these dumps would appear as crashes in | |
157 // the correct function, at the correct file and line. This would be | |
158 // preferable to having all occurrences show up in DumpWithoutCrashing() at | |
159 // the same file and line. | |
160 base::debug::SetDumpWithoutCrashingFunction(DumpWithoutCrashing); | |
161 | |
162 if (browser_process) { | |
163 g_database = | |
164 crashpad::CrashReportDatabase::Initialize(database_path).release(); | |
165 | |
166 bool enable_uploads = false; | |
167 if (!crash_reporter_client->ReportingIsEnforcedByPolicy(&enable_uploads)) { | |
168 // Breakpad provided a --disable-breakpad switch to disable crash dumping | |
169 // (not just uploading) here. Crashpad doesn't need it: dumping is enabled | |
170 // unconditionally and uploading is gated on consent, which tests/bots | |
171 // shouldn't have. As a precaution, uploading is also disabled on bots | |
172 // even if consent is present. | |
173 enable_uploads = crash_reporter_client->GetCollectStatsConsent() && | |
174 !crash_reporter_client->IsRunningUnattended(); | |
175 } | |
176 | |
177 SetUploadsEnabled(enable_uploads); | |
178 } | |
179 } | |
180 | |
181 void SetUploadsEnabled(bool enable_uploads) { | |
182 if (g_database) { | |
183 crashpad::Settings* settings = g_database->GetSettings(); | |
184 settings->SetUploadsEnabled(enable_uploads); | |
185 } | |
186 } | |
187 | |
188 bool GetUploadsEnabled() { | |
189 if (g_database) { | |
190 crashpad::Settings* settings = g_database->GetSettings(); | |
191 bool enable_uploads; | |
192 if (settings->GetUploadsEnabled(&enable_uploads)) { | |
193 return enable_uploads; | |
194 } | |
195 } | |
196 | |
197 return false; | |
198 } | |
199 | |
200 void GetUploadedReports(std::vector<UploadedReport>* uploaded_reports) { | |
201 uploaded_reports->clear(); | |
202 | |
203 if (!g_database) { | |
204 return; | |
205 } | |
206 | |
207 std::vector<crashpad::CrashReportDatabase::Report> completed_reports; | |
208 crashpad::CrashReportDatabase::OperationStatus status = | |
209 g_database->GetCompletedReports(&completed_reports); | |
210 if (status != crashpad::CrashReportDatabase::kNoError) { | |
211 return; | |
212 } | |
213 | |
214 for (const crashpad::CrashReportDatabase::Report& completed_report : | |
215 completed_reports) { | |
216 if (completed_report.uploaded) { | |
217 UploadedReport uploaded_report; | |
218 uploaded_report.local_id = completed_report.uuid.ToString(); | |
219 uploaded_report.remote_id = completed_report.id; | |
220 uploaded_report.creation_time = completed_report.creation_time; | |
221 | |
222 uploaded_reports->push_back(uploaded_report); | |
223 } | |
224 } | |
225 | |
226 struct { | |
227 bool operator()(const UploadedReport& a, const UploadedReport& b) { | |
228 return a.creation_time >= b.creation_time; | |
229 } | |
230 } sort_by_time; | |
231 std::sort(uploaded_reports->begin(), uploaded_reports->end(), sort_by_time); | |
cpu_(ooo_6.6-7.5)
2015/11/07 01:53:18
you could use a lambda here ...
but whatever.
scottmg
2015/11/09 18:29:56
Done.
| |
232 } | |
233 | |
234 } // namespace crash_reporter | |
OLD | NEW |