| 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 logging::SetLogMessageHandler(LogMessageHandler); | |
| 158 | |
| 159 // If clients called CRASHPAD_SIMULATE_CRASH() instead of | |
| 160 // base::debug::DumpWithoutCrashing(), these dumps would appear as crashes in | |
| 161 // the correct function, at the correct file and line. This would be | |
| 162 // preferable to having all occurrences show up in DumpWithoutCrashing() at | |
| 163 // the same file and line. | |
| 164 base::debug::SetDumpWithoutCrashingFunction(DumpWithoutCrashing); | |
| 165 | |
| 166 if (browser_process) { | |
| 167 g_database = | |
| 168 crashpad::CrashReportDatabase::Initialize(database_path).release(); | |
| 169 | |
| 170 bool enable_uploads = false; | |
| 171 if (!crash_reporter_client->ReportingIsEnforcedByPolicy(&enable_uploads)) { | |
| 172 // Breakpad provided a --disable-breakpad switch to disable crash dumping | |
| 173 // (not just uploading) here. Crashpad doesn't need it: dumping is enabled | |
| 174 // unconditionally and uploading is gated on consent, which tests/bots | |
| 175 // shouldn't have. As a precaution, uploading is also disabled on bots | |
| 176 // even if consent is present. | |
| 177 enable_uploads = crash_reporter_client->GetCollectStatsConsent() && | |
| 178 !crash_reporter_client->IsRunningUnattended(); | |
| 179 } | |
| 180 | |
| 181 SetUploadsEnabled(enable_uploads); | |
| 182 } | |
| 183 } | |
| 184 | |
| 185 void SetUploadsEnabled(bool enable_uploads) { | |
| 186 if (g_database) { | |
| 187 crashpad::Settings* settings = g_database->GetSettings(); | |
| 188 settings->SetUploadsEnabled(enable_uploads); | |
| 189 } | |
| 190 } | |
| 191 | |
| 192 bool GetUploadsEnabled() { | |
| 193 if (g_database) { | |
| 194 crashpad::Settings* settings = g_database->GetSettings(); | |
| 195 bool enable_uploads; | |
| 196 if (settings->GetUploadsEnabled(&enable_uploads)) { | |
| 197 return enable_uploads; | |
| 198 } | |
| 199 } | |
| 200 | |
| 201 return false; | |
| 202 } | |
| 203 | |
| 204 void GetUploadedReports(std::vector<UploadedReport>* uploaded_reports) { | |
| 205 uploaded_reports->clear(); | |
| 206 | |
| 207 if (!g_database) { | |
| 208 return; | |
| 209 } | |
| 210 | |
| 211 std::vector<crashpad::CrashReportDatabase::Report> completed_reports; | |
| 212 crashpad::CrashReportDatabase::OperationStatus status = | |
| 213 g_database->GetCompletedReports(&completed_reports); | |
| 214 if (status != crashpad::CrashReportDatabase::kNoError) { | |
| 215 return; | |
| 216 } | |
| 217 | |
| 218 for (const crashpad::CrashReportDatabase::Report& completed_report : | |
| 219 completed_reports) { | |
| 220 if (completed_report.uploaded) { | |
| 221 UploadedReport uploaded_report; | |
| 222 uploaded_report.local_id = completed_report.uuid.ToString(); | |
| 223 uploaded_report.remote_id = completed_report.id; | |
| 224 uploaded_report.creation_time = completed_report.creation_time; | |
| 225 | |
| 226 uploaded_reports->push_back(uploaded_report); | |
| 227 } | |
| 228 } | |
| 229 | |
| 230 std::sort(uploaded_reports->begin(), uploaded_reports->end(), | |
| 231 [](const UploadedReport& a, const UploadedReport& b) { | |
| 232 return a.creation_time >= b.creation_time; | |
| 233 }); | |
| 234 } | |
| 235 | |
| 236 } // namespace crash_reporter | |
| OLD | NEW |