| 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? |
| 44 CrashReporterClient* crash_reporter_client = GetCrashReporterClient(); |
| 118 crash_reporter_client->GetCrashDumpLocation(&database_path); | 45 crash_reporter_client->GetCrashDumpLocation(&database_path); |
| 119 | 46 |
| 120 // TODO(mark): Reading the Breakpad keys is temporary and transitional. At | 47 // TODO(mark): Reading the Breakpad keys is temporary and transitional. At |
| 121 // the very least, they should be renamed to Crashpad. For the time being, | 48 // the very least, they should be renamed to Crashpad. For the time being, |
| 122 // this isn't the worst thing: Crashpad is still uploading to a | 49 // this isn't the worst thing: Crashpad is still uploading to a |
| 123 // Breakpad-type server, after all. | 50 // Breakpad-type server, after all. |
| 124 NSBundle* framework_bundle = base::mac::FrameworkBundle(); | 51 NSBundle* framework_bundle = base::mac::FrameworkBundle(); |
| 125 NSString* product = base::mac::ObjCCast<NSString>( | 52 NSString* product = base::mac::ObjCCast<NSString>( |
| 126 [framework_bundle objectForInfoDictionaryKey:@"BreakpadProduct"]); | 53 [framework_bundle objectForInfoDictionaryKey:@"BreakpadProduct"]); |
| 127 NSString* version = base::mac::ObjCCast<NSString>( | 54 NSString* version = base::mac::ObjCCast<NSString>( |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 // If this is an initial client that's not the browser process, it's | 88 // If this is an initial client that's not the browser process, it's |
| 162 // important to sever the connection to any existing handler. If | 89 // important to sever the connection to any existing handler. If |
| 163 // StartHandler() or UseHandler() failed, call UseSystemDefaultHandler() | 90 // StartHandler() or UseHandler() failed, call UseSystemDefaultHandler() |
| 164 // in that case to drop the link to the existing handler. | 91 // in that case to drop the link to the existing handler. |
| 165 if (!result && !browser_process) { | 92 if (!result && !browser_process) { |
| 166 crashpad::CrashpadClient::UseSystemDefaultHandler(); | 93 crashpad::CrashpadClient::UseSystemDefaultHandler(); |
| 167 } | 94 } |
| 168 } // @autoreleasepool | 95 } // @autoreleasepool |
| 169 } | 96 } |
| 170 | 97 |
| 171 crashpad::CrashpadInfo* crashpad_info = | 98 return database_path; |
| 172 crashpad::CrashpadInfo::GetCrashpadInfo(); | |
| 173 | |
| 174 #if defined(NDEBUG) | |
| 175 const bool is_debug_build = false; | |
| 176 #else | |
| 177 const bool is_debug_build = true; | |
| 178 #endif | |
| 179 | |
| 180 // Disable forwarding to the system's crash reporter in processes other than | |
| 181 // the browser process. For the browser, the system's crash reporter presents | |
| 182 // the crash UI to the user, so it's desirable there. Additionally, having | |
| 183 // crash reports appear in ~/Library/Logs/DiagnosticReports provides a | |
| 184 // fallback. Forwarding is turned off for debug-mode builds even for the | |
| 185 // browser process, because the system's crash reporter can take a very long | |
| 186 // time to chew on symbols. | |
| 187 if (!browser_process || is_debug_build) { | |
| 188 crashpad_info->set_system_crash_reporter_forwarding( | |
| 189 crashpad::TriState::kDisabled); | |
| 190 } | |
| 191 | |
| 192 g_simple_string_dictionary = new crashpad::SimpleStringDictionary(); | |
| 193 crashpad_info->set_simple_annotations(g_simple_string_dictionary); | |
| 194 | |
| 195 base::debug::SetCrashKeyReportingFunctions(SetCrashKeyValue, ClearCrashKey); | |
| 196 crash_reporter_client->RegisterCrashKeys(); | |
| 197 | |
| 198 SetCrashKeyValue("ptype", browser_process ? base::StringPiece("browser") | |
| 199 : base::StringPiece(process_type)); | |
| 200 SetCrashKeyValue("pid", base::IntToString(getpid())); | |
| 201 | |
| 202 logging::SetLogMessageHandler(LogMessageHandler); | |
| 203 | |
| 204 // If clients called CRASHPAD_SIMULATE_CRASH() instead of | |
| 205 // base::debug::DumpWithoutCrashing(), these dumps would appear as crashes in | |
| 206 // the correct function, at the correct file and line. This would be | |
| 207 // preferable to having all occurrences show up in DumpWithoutCrashing() at | |
| 208 // the same file and line. | |
| 209 base::debug::SetDumpWithoutCrashingFunction(DumpWithoutCrashing); | |
| 210 | |
| 211 if (browser_process) { | |
| 212 g_database = | |
| 213 crashpad::CrashReportDatabase::Initialize(database_path).release(); | |
| 214 | |
| 215 bool enable_uploads = false; | |
| 216 if (!crash_reporter_client->ReportingIsEnforcedByPolicy(&enable_uploads)) { | |
| 217 // Breakpad provided a --disable-breakpad switch to disable crash dumping | |
| 218 // (not just uploading) here. Crashpad doesn't need it: dumping is enabled | |
| 219 // unconditionally and uploading is gated on consent, which tests/bots | |
| 220 // shouldn't have. As a precaution, uploading is also disabled on bots | |
| 221 // even if consent is present. | |
| 222 enable_uploads = crash_reporter_client->GetCollectStatsConsent() && | |
| 223 !crash_reporter_client->IsRunningUnattended(); | |
| 224 } | |
| 225 | |
| 226 SetUploadsEnabled(enable_uploads); | |
| 227 } | |
| 228 } | 99 } |
| 229 | 100 |
| 230 void SetUploadsEnabled(bool enable_uploads) { | 101 } // namespace internal |
| 231 if (g_database) { | |
| 232 crashpad::Settings* settings = g_database->GetSettings(); | |
| 233 settings->SetUploadsEnabled(enable_uploads); | |
| 234 } | |
| 235 } | |
| 236 | |
| 237 bool GetUploadsEnabled() { | |
| 238 if (g_database) { | |
| 239 crashpad::Settings* settings = g_database->GetSettings(); | |
| 240 bool enable_uploads; | |
| 241 if (settings->GetUploadsEnabled(&enable_uploads)) { | |
| 242 return enable_uploads; | |
| 243 } | |
| 244 } | |
| 245 | |
| 246 return false; | |
| 247 } | |
| 248 | |
| 249 void GetUploadedReports(std::vector<UploadedReport>* uploaded_reports) { | |
| 250 uploaded_reports->clear(); | |
| 251 | |
| 252 if (!g_database) { | |
| 253 return; | |
| 254 } | |
| 255 | |
| 256 std::vector<crashpad::CrashReportDatabase::Report> completed_reports; | |
| 257 crashpad::CrashReportDatabase::OperationStatus status = | |
| 258 g_database->GetCompletedReports(&completed_reports); | |
| 259 if (status != crashpad::CrashReportDatabase::kNoError) { | |
| 260 return; | |
| 261 } | |
| 262 | |
| 263 for (const crashpad::CrashReportDatabase::Report& completed_report : | |
| 264 completed_reports) { | |
| 265 if (completed_report.uploaded) { | |
| 266 UploadedReport uploaded_report; | |
| 267 uploaded_report.local_id = completed_report.uuid.ToString(); | |
| 268 uploaded_report.remote_id = completed_report.id; | |
| 269 uploaded_report.creation_time = completed_report.creation_time; | |
| 270 | |
| 271 uploaded_reports->push_back(uploaded_report); | |
| 272 } | |
| 273 } | |
| 274 | |
| 275 struct { | |
| 276 bool operator()(const UploadedReport& a, const UploadedReport& b) { | |
| 277 return a.creation_time >= b.creation_time; | |
| 278 } | |
| 279 } sort_by_time; | |
| 280 std::sort(uploaded_reports->begin(), uploaded_reports->end(), sort_by_time); | |
| 281 } | |
| 282 | |
| 283 } // namespace crash_reporter | 102 } // namespace crash_reporter |
| OLD | NEW |