| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/crash/content/app/crashpad_mac.h" | 5 #include "components/crash/content/app/crashpad.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 #include <unistd.h> | 8 #include <unistd.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 #include <map> | 11 #include <map> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/auto_reset.h" | |
| 15 #include "base/debug/crash_logging.h" | |
| 16 #include "base/debug/dump_without_crashing.h" | |
| 17 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 18 #include "base/logging.h" | 15 #include "base/logging.h" |
| 19 #include "base/mac/bundle_locations.h" | 16 #include "base/mac/bundle_locations.h" |
| 20 #include "base/mac/foundation_util.h" | 17 #include "base/mac/foundation_util.h" |
| 21 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 22 #include "base/strings/string_piece.h" | 19 #include "base/strings/string_piece.h" |
| 23 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
| 24 #include "base/strings/sys_string_conversions.h" | 21 #include "base/strings/sys_string_conversions.h" |
| 25 #include "components/crash/content/app/crash_reporter_client.h" | 22 #include "components/crash/content/app/crash_reporter_client.h" |
| 26 #include "third_party/crashpad/crashpad/client/crash_report_database.h" | 23 #include "third_party/crashpad/crashpad/client/crash_report_database.h" |
| 27 #include "third_party/crashpad/crashpad/client/crashpad_client.h" | 24 #include "third_party/crashpad/crashpad/client/crashpad_client.h" |
| 28 #include "third_party/crashpad/crashpad/client/crashpad_info.h" | 25 #include "third_party/crashpad/crashpad/client/crashpad_info.h" |
| 29 #include "third_party/crashpad/crashpad/client/settings.h" | 26 #include "third_party/crashpad/crashpad/client/settings.h" |
| 30 #include "third_party/crashpad/crashpad/client/simple_string_dictionary.h" | 27 #include "third_party/crashpad/crashpad/client/simple_string_dictionary.h" |
| 31 #include "third_party/crashpad/crashpad/client/simulate_crash.h" | 28 #include "third_party/crashpad/crashpad/client/simulate_crash.h" |
| 32 | 29 |
| 33 namespace crash_reporter { | 30 namespace crash_reporter { |
| 31 namespace internal { |
| 34 | 32 |
| 35 namespace { | 33 base::FilePath PlatformCrashpadInitialization(bool initial_client, |
| 36 | 34 bool browser_process) { |
| 37 crashpad::SimpleStringDictionary* g_simple_string_dictionary; | |
| 38 crashpad::CrashReportDatabase* g_database; | |
| 39 | |
| 40 void SetCrashKeyValue(const base::StringPiece& key, | |
| 41 const base::StringPiece& value) { | |
| 42 g_simple_string_dictionary->SetKeyValue(key.data(), value.data()); | |
| 43 } | |
| 44 | |
| 45 void ClearCrashKey(const base::StringPiece& key) { | |
| 46 g_simple_string_dictionary->RemoveKey(key.data()); | |
| 47 } | |
| 48 | |
| 49 bool LogMessageHandler(int severity, | |
| 50 const char* file, | |
| 51 int line, | |
| 52 size_t message_start, | |
| 53 const std::string& string) { | |
| 54 // Only handle FATAL. | |
| 55 if (severity != logging::LOG_FATAL) { | |
| 56 return false; | |
| 57 } | |
| 58 | |
| 59 // In case of an out-of-memory condition, this code could be reentered when | |
| 60 // constructing and storing the key. Using a static is not thread-safe, but if | |
| 61 // multiple threads are in the process of a fatal crash at the same time, this | |
| 62 // should work. | |
| 63 static bool guarded = false; | |
| 64 if (guarded) { | |
| 65 return false; | |
| 66 } | |
| 67 base::AutoReset<bool> guard(&guarded, true); | |
| 68 | |
| 69 // Only log last path component. This matches logging.cc. | |
| 70 if (file) { | |
| 71 const char* slash = strrchr(file, '/'); | |
| 72 if (slash) { | |
| 73 file = slash + 1; | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 std::string message = base::StringPrintf("%s:%d: %s", file, line, | |
| 78 string.c_str() + message_start); | |
| 79 SetCrashKeyValue("LOG_FATAL", message); | |
| 80 | |
| 81 // Rather than including the code to force the crash here, allow the caller to | |
| 82 // do it. | |
| 83 return false; | |
| 84 } | |
| 85 | |
| 86 void DumpWithoutCrashing() { | |
| 87 CRASHPAD_SIMULATE_CRASH(); | |
| 88 } | |
| 89 | |
| 90 } // namespace | |
| 91 | |
| 92 void InitializeCrashpad(bool initial_client, const std::string& process_type) { | |
| 93 static bool initialized = false; | |
| 94 DCHECK(!initialized); | |
| 95 initialized = true; | |
| 96 | |
| 97 const bool browser_process = process_type.empty(); | |
| 98 CrashReporterClient* crash_reporter_client = GetCrashReporterClient(); | |
| 99 | |
| 100 if (initial_client) { | |
| 101 // "relauncher" is hard-coded because it's a Chrome --type, but this | |
| 102 // component can't see Chrome's switches. This is only used for argument | |
| 103 // sanitization. | |
| 104 DCHECK(browser_process || process_type == "relauncher"); | |
| 105 } else { | |
| 106 DCHECK(!browser_process); | |
| 107 } | |
| 108 | |
| 109 base::FilePath database_path; // Only valid in the browser process. | 35 base::FilePath database_path; // Only valid in the browser process. |
| 110 | 36 |
| 111 if (initial_client) { | 37 if (initial_client) { |
| 112 @autoreleasepool { | 38 @autoreleasepool { |
| 113 base::FilePath framework_bundle_path = base::mac::FrameworkBundlePath(); | 39 base::FilePath framework_bundle_path = base::mac::FrameworkBundlePath(); |
| 114 base::FilePath handler_path = | 40 base::FilePath handler_path = |
| 115 framework_bundle_path.Append("Helpers").Append("crashpad_handler"); | 41 framework_bundle_path.Append("Helpers").Append("crashpad_handler"); |
| 116 | 42 |
| 117 // Is there a way to recover if this fails? | 43 // Is there a way to recover if this fails? |
| 118 crash_reporter_client->GetCrashDumpLocation(&database_path); | 44 crash_reporter_client->GetCrashDumpLocation(&database_path); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 141 std::vector<std::string> arguments; | 67 std::vector<std::string> arguments; |
| 142 if (!browser_process) { | 68 if (!browser_process) { |
| 143 // If this is an initial client that's not the browser process, it's | 69 // If this is an initial client that's not the browser process, it's |
| 144 // important that the new Crashpad handler also not be connected to any | 70 // important that the new Crashpad handler also not be connected to any |
| 145 // existing handler. This argument tells the new Crashpad handler to | 71 // existing handler. This argument tells the new Crashpad handler to |
| 146 // sever this connection. | 72 // sever this connection. |
| 147 arguments.push_back( | 73 arguments.push_back( |
| 148 "--reset-own-crash-exception-port-to-system-default"); | 74 "--reset-own-crash-exception-port-to-system-default"); |
| 149 } | 75 } |
| 150 | 76 |
| 151 bool result = crashpad_client.StartHandler(handler_path, | 77 bool result = crashpad_client.StartHandler( |
| 152 database_path, | 78 handler_path, database_path, url, process_annotations, arguments); |
| 153 url, | |
| 154 process_annotations, | |
| 155 arguments); | |
| 156 if (result) { | 79 if (result) { |
| 157 result = crashpad_client.UseHandler(); | 80 result = crashpad_client.UseHandler(); |
| 158 } | 81 } |
| 159 | 82 |
| 160 // If this is an initial client that's not the browser process, it's | 83 // If this is an initial client that's not the browser process, it's |
| 161 // important to sever the connection to any existing handler. If | 84 // important to sever the connection to any existing handler. If |
| 162 // StartHandler() or UseHandler() failed, call UseSystemDefaultHandler() | 85 // StartHandler() or UseHandler() failed, call UseSystemDefaultHandler() |
| 163 // in that case to drop the link to the existing handler. | 86 // in that case to drop the link to the existing handler. |
| 164 if (!result && !browser_process) { | 87 if (!result && !browser_process) { |
| 165 crashpad::CrashpadClient::UseSystemDefaultHandler(); | 88 crashpad::CrashpadClient::UseSystemDefaultHandler(); |
| 166 } | 89 } |
| 167 } // @autoreleasepool | 90 } // @autoreleasepool |
| 168 } | 91 } |
| 169 | 92 |
| 170 crashpad::CrashpadInfo* crashpad_info = | 93 return database_path; |
| 171 crashpad::CrashpadInfo::GetCrashpadInfo(); | |
| 172 | |
| 173 #if defined(NDEBUG) | |
| 174 const bool is_debug_build = false; | |
| 175 #else | |
| 176 const bool is_debug_build = true; | |
| 177 #endif | |
| 178 | |
| 179 // Disable forwarding to the system's crash reporter in processes other than | |
| 180 // the browser process. For the browser, the system's crash reporter presents | |
| 181 // the crash UI to the user, so it's desirable there. Additionally, having | |
| 182 // crash reports appear in ~/Library/Logs/DiagnosticReports provides a | |
| 183 // fallback. Forwarding is turned off for debug-mode builds even for the | |
| 184 // browser process, because the system's crash reporter can take a very long | |
| 185 // time to chew on symbols. | |
| 186 if (!browser_process || is_debug_build) { | |
| 187 crashpad_info->set_system_crash_reporter_forwarding( | |
| 188 crashpad::TriState::kDisabled); | |
| 189 } | |
| 190 | |
| 191 g_simple_string_dictionary = new crashpad::SimpleStringDictionary(); | |
| 192 crashpad_info->set_simple_annotations(g_simple_string_dictionary); | |
| 193 | |
| 194 base::debug::SetCrashKeyReportingFunctions(SetCrashKeyValue, ClearCrashKey); | |
| 195 crash_reporter_client->RegisterCrashKeys(); | |
| 196 | |
| 197 SetCrashKeyValue("ptype", browser_process ? base::StringPiece("browser") | |
| 198 : base::StringPiece(process_type)); | |
| 199 SetCrashKeyValue("pid", base::IntToString(getpid())); | |
| 200 | |
| 201 logging::SetLogMessageHandler(LogMessageHandler); | |
| 202 | |
| 203 // If clients called CRASHPAD_SIMULATE_CRASH() instead of | |
| 204 // base::debug::DumpWithoutCrashing(), these dumps would appear as crashes in | |
| 205 // the correct function, at the correct file and line. This would be | |
| 206 // preferable to having all occurrences show up in DumpWithoutCrashing() at | |
| 207 // the same file and line. | |
| 208 base::debug::SetDumpWithoutCrashingFunction(DumpWithoutCrashing); | |
| 209 | |
| 210 if (browser_process) { | |
| 211 g_database = | |
| 212 crashpad::CrashReportDatabase::Initialize(database_path).release(); | |
| 213 | |
| 214 bool enable_uploads = false; | |
| 215 if (!crash_reporter_client->ReportingIsEnforcedByPolicy(&enable_uploads)) { | |
| 216 // Breakpad provided a --disable-breakpad switch to disable crash dumping | |
| 217 // (not just uploading) here. Crashpad doesn't need it: dumping is enabled | |
| 218 // unconditionally and uploading is gated on consent, which tests/bots | |
| 219 // shouldn't have. As a precaution, uploading is also disabled on bots | |
| 220 // even if consent is present. | |
| 221 enable_uploads = crash_reporter_client->GetCollectStatsConsent() && | |
| 222 !crash_reporter_client->IsRunningUnattended(); | |
| 223 } | |
| 224 | |
| 225 SetUploadsEnabled(enable_uploads); | |
| 226 } | |
| 227 } | 94 } |
| 228 | 95 |
| 229 void SetUploadsEnabled(bool enable_uploads) { | 96 } // namespace internal |
| 230 if (g_database) { | |
| 231 crashpad::Settings* settings = g_database->GetSettings(); | |
| 232 settings->SetUploadsEnabled(enable_uploads); | |
| 233 } | |
| 234 } | |
| 235 | |
| 236 bool GetUploadsEnabled() { | |
| 237 if (g_database) { | |
| 238 crashpad::Settings* settings = g_database->GetSettings(); | |
| 239 bool enable_uploads; | |
| 240 if (settings->GetUploadsEnabled(&enable_uploads)) { | |
| 241 return enable_uploads; | |
| 242 } | |
| 243 } | |
| 244 | |
| 245 return false; | |
| 246 } | |
| 247 | |
| 248 void GetUploadedReports(std::vector<UploadedReport>* uploaded_reports) { | |
| 249 uploaded_reports->clear(); | |
| 250 | |
| 251 if (!g_database) { | |
| 252 return; | |
| 253 } | |
| 254 | |
| 255 std::vector<crashpad::CrashReportDatabase::Report> completed_reports; | |
| 256 crashpad::CrashReportDatabase::OperationStatus status = | |
| 257 g_database->GetCompletedReports(&completed_reports); | |
| 258 if (status != crashpad::CrashReportDatabase::kNoError) { | |
| 259 return; | |
| 260 } | |
| 261 | |
| 262 for (const crashpad::CrashReportDatabase::Report& completed_report : | |
| 263 completed_reports) { | |
| 264 if (completed_report.uploaded) { | |
| 265 UploadedReport uploaded_report; | |
| 266 uploaded_report.local_id = completed_report.uuid.ToString(); | |
| 267 uploaded_report.remote_id = completed_report.id; | |
| 268 uploaded_report.creation_time = completed_report.creation_time; | |
| 269 | |
| 270 uploaded_reports->push_back(uploaded_report); | |
| 271 } | |
| 272 } | |
| 273 | |
| 274 struct { | |
| 275 bool operator()(const UploadedReport& a, const UploadedReport& b) { | |
| 276 return a.creation_time >= b.creation_time; | |
| 277 } | |
| 278 } sort_by_time; | |
| 279 std::sort(uploaded_reports->begin(), uploaded_reports->end(), sort_by_time); | |
| 280 } | |
| 281 | |
| 282 } // namespace crash_reporter | 97 } // namespace crash_reporter |
| OLD | NEW |